summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/xen/io/xnb.c
blob: 95a05680c93740c5a9f242c35e67284229670fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#ifdef DEBUG
#define	XNB_DEBUG 1
#endif /* DEBUG */

#include "xnb.h"

#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/mac.h>
#include <sys/dlpi.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/pattr.h>
#include <vm/seg_kmem.h>
#include <vm/hat_i86.h>
#include <xen/sys/xenbus_impl.h>
#include <xen/sys/xendev.h>
#include <sys/balloon_impl.h>
#include <sys/evtchn_impl.h>
#include <sys/gnttab.h>

#include <sys/gld.h>
#include <inet/ip.h>
#include <inet/ip_impl.h>
#include <sys/vnic_impl.h> /* blech. */

/*
 * The terms "transmit" and "receive" are used in their traditional
 * sense here - packets from other parts of this system are
 * "transmitted" to the peer domain and those originating from the
 * peer are "received".
 *
 * In some cases this can be confusing, because various data
 * structures are shared with the domU driver, which has the opposite
 * view of what constitutes "transmit" and "receive".  In naming the
 * shared structures the domU driver always wins.
 */

/*
 * XXPV dme: things to do, as well as various things indicated
 * throughout the source:
 * - copy avoidance outbound.
 * - copy avoidance inbound.
 * - transfer credit limiting.
 * - MAC address based filtering.
 */

/*
 * Linux expects to have some headroom in received buffers.  The Linux
 * frontend driver (netfront) checks to see if the headroom is
 * available and will re-allocate the buffer to make room if
 * necessary.  To avoid this we add TX_BUFFER_HEADROOM bytes of
 * headroom to each packet we pass to the peer.
 */
#define	TX_BUFFER_HEADROOM	16

static boolean_t	xnb_cksum_offload = B_TRUE;

static boolean_t	xnb_connect_rings(dev_info_t *);
static void		xnb_disconnect_rings(dev_info_t *);
static void		xnb_oe_state_change(dev_info_t *, ddi_eventcookie_t,
    void *, void *);
static void		xnb_hp_state_change(dev_info_t *, ddi_eventcookie_t,
    void *, void *);

static int	xnb_rxbuf_constructor(void *, void *, int);
static void	xnb_rxbuf_destructor(void *, void *);
static xnb_rxbuf_t *xnb_rxbuf_get(xnb_t *, int);
static void	xnb_rxbuf_put(xnb_t *, xnb_rxbuf_t *);
static void	xnb_rx_notify_peer(xnb_t *);
static void	xnb_rx_complete(xnb_rxbuf_t *);
static void	xnb_rx_mark_complete(xnb_t *, RING_IDX, int16_t);
static void	xnb_rx_schedule_unmop(xnb_t *, gnttab_map_grant_ref_t *);
static void	xnb_rx_perform_pending_unmop(xnb_t *);

#ifdef XNB_DEBUG
#define	NR_GRANT_ENTRIES \
	(NR_GRANT_FRAMES * PAGESIZE / sizeof (grant_entry_t))
#endif /* XNB_DEBUG */

/* XXPV dme: are these really invalid? */
#define	INVALID_GRANT_HANDLE	((grant_handle_t)-1)
#define	INVALID_GRANT_REF	((grant_ref_t)-1)

static kmem_cache_t *xnb_rxbuf_cachep;
static kmutex_t	xnb_alloc_page_lock;

/*
 * Statistics.
 */
static char *aux_statistics[] = {
	"tx_cksum_deferred",
	"rx_cksum_no_need",
	"tx_notify_deferred",
	"tx_notify_sent",
	"rx_notify_deferred",
	"rx_notify_sent",
	"tx_too_early",
	"rx_too_early",
	"rx_allocb_failed",
	"mac_full",
	"spurious_intr",
	"allocation_success",
	"allocation_failure",
	"small_allocation_success",
	"small_allocation_failure",
	"csum_hardware",
	"csum_software",
};

static int
xnb_ks_aux_update(kstat_t *ksp, int flag)
{
	xnb_t *xnbp;
	kstat_named_t *knp;

	if (flag != KSTAT_READ)
		return (EACCES);

	xnbp = ksp->ks_private;
	knp = ksp->ks_data;

	/*
	 * Assignment order should match that of the names in
	 * aux_statistics.
	 */
	(knp++)->value.ui64 = xnbp->x_stat_tx_cksum_deferred;
	(knp++)->value.ui64 = xnbp->x_stat_rx_cksum_no_need;
	(knp++)->value.ui64 = xnbp->x_stat_tx_notify_deferred;
	(knp++)->value.ui64 = xnbp->x_stat_tx_notify_sent;
	(knp++)->value.ui64 = xnbp->x_stat_rx_notify_deferred;
	(knp++)->value.ui64 = xnbp->x_stat_rx_notify_sent;
	(knp++)->value.ui64 = xnbp->x_stat_tx_too_early;
	(knp++)->value.ui64 = xnbp->x_stat_rx_too_early;
	(knp++)->value.ui64 = xnbp->x_stat_rx_allocb_failed;
	(knp++)->value.ui64 = xnbp->x_stat_mac_full;
	(knp++)->value.ui64 = xnbp->x_stat_spurious_intr;
	(knp++)->value.ui64 = xnbp->x_stat_allocation_success;
	(knp++)->value.ui64 = xnbp->x_stat_allocation_failure;
	(knp++)->value.ui64 = xnbp->x_stat_small_allocation_success;
	(knp++)->value.ui64 = xnbp->x_stat_small_allocation_failure;
	(knp++)->value.ui64 = xnbp->x_stat_csum_hardware;
	(knp++)->value.ui64 = xnbp->x_stat_csum_software;

	return (0);
}

static boolean_t
xnb_ks_init(xnb_t *xnbp)
{
	int nstat = sizeof (aux_statistics) /
	    sizeof (aux_statistics[0]);
	char **cp = aux_statistics;
	kstat_named_t *knp;

	/*
	 * Create and initialise kstats.
	 */
	xnbp->x_kstat_aux = kstat_create(ddi_driver_name(xnbp->x_devinfo),
	    ddi_get_instance(xnbp->x_devinfo), "aux_statistics", "net",
	    KSTAT_TYPE_NAMED, nstat, 0);
	if (xnbp->x_kstat_aux == NULL)
		return (B_FALSE);

	xnbp->x_kstat_aux->ks_private = xnbp;
	xnbp->x_kstat_aux->ks_update = xnb_ks_aux_update;

	knp = xnbp->x_kstat_aux->ks_data;
	while (nstat > 0) {
		kstat_named_init(knp, *cp, KSTAT_DATA_UINT64);

		knp++;
		cp++;
		nstat--;
	}

	kstat_install(xnbp->x_kstat_aux);

	return (B_TRUE);
}

static void
xnb_ks_free(xnb_t *xnbp)
{
	kstat_delete(xnbp->x_kstat_aux);
}

/*
 * Software checksum calculation and insertion for an arbitrary packet.
 */
/*ARGSUSED*/
static mblk_t *
xnb_software_csum(xnb_t *xnbp, mblk_t *mp)
{
	/*
	 * XXPV dme: shouldn't rely on vnic_fix_cksum(), not least
	 * because it doesn't cover all of the interesting cases :-(
	 */
	(void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0,
	    HCK_FULLCKSUM, KM_NOSLEEP);

	return (vnic_fix_cksum(mp));
}

mblk_t *
xnb_process_cksum_flags(xnb_t *xnbp, mblk_t *mp, uint32_t capab)
{
	struct ether_header *ehp;
	uint16_t sap;
	uint32_t offset;
	ipha_t *ipha;

	ASSERT(mp->b_next == NULL);

	/*
	 * Check that the packet is contained in a single mblk.  In
	 * the "from peer" path this is true today, but will change
	 * when scatter gather support is added.  In the "to peer"
	 * path we cannot be sure, but in most cases it will be true
	 * (in the xnbo case the packet has come from a MAC device
	 * which is unlikely to split packets).
	 */
	if (mp->b_cont != NULL)
		goto software;

	/*
	 * If the MAC has no hardware capability don't do any further
	 * checking.
	 */
	if (capab == 0)
		goto software;

	ASSERT(MBLKL(mp) >= sizeof (struct ether_header));
	ehp = (struct ether_header *)mp->b_rptr;

	if (ntohs(ehp->ether_type) == VLAN_TPID) {
		struct ether_vlan_header *evhp;

		ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header));
		evhp = (struct ether_vlan_header *)mp->b_rptr;
		sap = ntohs(evhp->ether_type);
		offset = sizeof (struct ether_vlan_header);
	} else {
		sap = ntohs(ehp->ether_type);
		offset = sizeof (struct ether_header);
	}

	/*
	 * We only attempt to do IPv4 packets in hardware.
	 */
	if (sap != ETHERTYPE_IP)
		goto software;

	/*
	 * We know that this is an IPv4 packet.
	 */
	ipha = (ipha_t *)(mp->b_rptr + offset);

	switch (ipha->ipha_protocol) {
	case IPPROTO_TCP:
	case IPPROTO_UDP:
		/*
		 * This is a TCP/IPv4 or UDP/IPv4 packet.
		 *
		 * If the capabilities indicate that full checksum
		 * offload is available, use it.
		 */
		if ((capab & HCKSUM_INET_FULL_V4) != 0) {
			(void) hcksum_assoc(mp, NULL, NULL,
			    0, 0, 0, 0,
			    HCK_FULLCKSUM, KM_NOSLEEP);

			xnbp->x_stat_csum_hardware++;

			return (mp);
		}

		/*
		 * XXPV dme: If the capabilities indicate that partial
		 * checksum offload is available, we should use it.
		 */

		break;

	default:
		/* Use software. */
		break;
	}

software:
	/*
	 * We are not able to use any offload so do the whole thing in
	 * software.
	 */
	xnbp->x_stat_csum_software++;

	return (xnb_software_csum(xnbp, mp));
}

int
xnb_attach(dev_info_t *dip, xnb_flavour_t *flavour, void *flavour_data)
{
	xnb_t *xnbp;
	char *xsname, mac[ETHERADDRL * 3];

	xnbp = kmem_zalloc(sizeof (*xnbp), KM_SLEEP);

	xnbp->x_flavour = flavour;
	xnbp->x_flavour_data = flavour_data;
	xnbp->x_devinfo = dip;
	xnbp->x_evtchn = INVALID_EVTCHN;
	xnbp->x_irq = B_FALSE;
	xnbp->x_tx_ring_handle = INVALID_GRANT_HANDLE;
	xnbp->x_rx_ring_handle = INVALID_GRANT_HANDLE;
	xnbp->x_cksum_offload = xnb_cksum_offload;
	xnbp->x_connected = B_FALSE;
	xnbp->x_hotplugged = B_FALSE;
	xnbp->x_detachable = B_FALSE;
	xnbp->x_peer = xvdi_get_oeid(dip);
	xnbp->x_rx_pages_writable = B_FALSE;

	xnbp->x_rx_buf_count = 0;
	xnbp->x_rx_unmop_count = 0;

	xnbp->x_tx_va = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
	ASSERT(xnbp->x_tx_va != NULL);

	if (ddi_get_iblock_cookie(dip, 0, &xnbp->x_icookie)
	    != DDI_SUCCESS)
		goto failure;

	mutex_init(&xnbp->x_tx_lock, NULL, MUTEX_DRIVER, xnbp->x_icookie);
	mutex_init(&xnbp->x_rx_lock, NULL, MUTEX_DRIVER, xnbp->x_icookie);

	/* set driver private pointer now */
	ddi_set_driver_private(dip, xnbp);

	if (!xnb_ks_init(xnbp))
		goto late_failure;

	/*
	 * Receive notification of changes in the state of the
	 * driver in the guest domain.
	 */
	if (xvdi_add_event_handler(dip, XS_OE_STATE,
	    xnb_oe_state_change) != DDI_SUCCESS)
		goto very_late_failure;

	/*
	 * Receive notification of hotplug events.
	 */
	if (xvdi_add_event_handler(dip, XS_HP_STATE,
	    xnb_hp_state_change) != DDI_SUCCESS)
		goto very_late_failure;

	xsname = xvdi_get_xsname(dip);

	if (xenbus_printf(XBT_NULL, xsname,
	    "feature-no-csum-offload", "%d",
	    xnbp->x_cksum_offload ? 0 : 1) != 0)
		goto very_very_late_failure;

	if (xenbus_scanf(XBT_NULL, xsname,
	    "mac", "%s", mac) != 0) {
		cmn_err(CE_WARN, "xnb_attach: "
		    "cannot read mac address from %s",
		    xsname);
		goto very_very_late_failure;
	}

	if (ether_aton(mac, xnbp->x_mac_addr) != ETHERADDRL) {
		cmn_err(CE_WARN,
		    "xnb_attach: cannot parse mac address %s",
		    mac);
		goto very_very_late_failure;
	}

	(void) xvdi_switch_state(dip, XBT_NULL, XenbusStateInitWait);
	(void) xvdi_post_event(dip, XEN_HP_ADD);

	return (DDI_SUCCESS);

very_very_late_failure: /* not that the naming is getting silly or anything */
	xvdi_remove_event_handler(dip, NULL);

very_late_failure:
	xnb_ks_free(xnbp);

late_failure:
	mutex_destroy(&xnbp->x_rx_lock);
	mutex_destroy(&xnbp->x_tx_lock);

failure:
	vmem_free(heap_arena, xnbp->x_tx_va, PAGESIZE);
	kmem_free(xnbp, sizeof (*xnbp));
	return (DDI_FAILURE);
}

/*ARGSUSED*/
void
xnb_detach(dev_info_t *dip)
{
	xnb_t *xnbp = ddi_get_driver_private(dip);

	ASSERT(xnbp != NULL);
	ASSERT(!xnbp->x_connected);
	ASSERT(xnbp->x_rx_buf_count == 0);

	xnb_disconnect_rings(dip);

	xvdi_remove_event_handler(dip, NULL);

	xnb_ks_free(xnbp);

	ddi_set_driver_private(dip, NULL);

	mutex_destroy(&xnbp->x_tx_lock);
	mutex_destroy(&xnbp->x_rx_lock);

	ASSERT(xnbp->x_tx_va != NULL);
	vmem_free(heap_arena, xnbp->x_tx_va, PAGESIZE);

	kmem_free(xnbp, sizeof (*xnbp));
}


static mfn_t
xnb_alloc_page(xnb_t *xnbp)
{
#define	WARNING_RATE_LIMIT 100
#define	BATCH_SIZE 256
	static mfn_t mfns[BATCH_SIZE];	/* common across all instances */
	static int nth = BATCH_SIZE;
	mfn_t mfn;

	mutex_enter(&xnb_alloc_page_lock);
	if (nth == BATCH_SIZE) {
		if (balloon_alloc_pages(BATCH_SIZE, mfns) != BATCH_SIZE) {
			xnbp->x_stat_allocation_failure++;
			mutex_exit(&xnb_alloc_page_lock);

			/*
			 * Try for a single page in low memory situations.
			 */
			if (balloon_alloc_pages(1, &mfn) != 1) {
				xnbp->x_stat_small_allocation_failure++;
				if ((xnbp->x_stat_small_allocation_failure
				    % WARNING_RATE_LIMIT) == 0) {
					cmn_err(CE_WARN, "xnb_alloc_page: "
					    "Cannot allocate memory to "
					    "transfer packets to peer.");
				}
				return (0);
			} else {
				xnbp->x_stat_small_allocation_success++;
				return (mfn);
			}
		}

		nth = 0;
		xnbp->x_stat_allocation_success++;
	}

	mfn = mfns[nth++];
	mutex_exit(&xnb_alloc_page_lock);

	ASSERT(mfn != 0);

	return (mfn);
#undef BATCH_SIZE
#undef WARNING_RATE_LIMIT
}

/*ARGSUSED*/
static void
xnb_free_page(xnb_t *xnbp, mfn_t mfn)
{
	int r;

	/*
	 * This happens only in the error path, so batching is
	 * not worth the complication.
	 */
	if ((r = balloon_free_pages(1, &mfn, NULL, NULL)) != 1) {
		cmn_err(CE_WARN, "free_page: cannot decrease memory "
		    "reservation (%d): page kept but unusable (mfn = 0x%lx).",
		    r, mfn);
	}
}

mblk_t *
xnb_to_peer(xnb_t *xnbp, mblk_t *mp)
{
	mblk_t *free = mp, *prev = NULL;
	size_t len;
	gnttab_transfer_t *gop;
	boolean_t notify;
	RING_IDX loop, prod, end;

	/*
	 * For each packet the sequence of operations is:
	 *
	 * 1. get a new page from the hypervisor.
	 * 2. get a request slot from the ring.
	 * 3. copy the data into the new page.
	 * 4. transfer the page to the peer.
	 * 5. update the request slot.
	 * 6. kick the peer.
	 * 7. free mp.
	 *
	 * In order to reduce the number of hypercalls, we prepare
	 * several packets for the peer and perform a single hypercall
	 * to transfer them.
	 */

	mutex_enter(&xnbp->x_tx_lock);

	/*
	 * If we are not connected to the peer or have not yet
	 * finished hotplug it is too early to pass packets to the
	 * peer.
	 */
	if (!(xnbp->x_connected && xnbp->x_hotplugged)) {
		mutex_exit(&xnbp->x_tx_lock);
		xnbp->x_stat_tx_too_early++;
		return (mp);
	}

	loop = xnbp->x_rx_ring.req_cons;
	prod = xnbp->x_rx_ring.rsp_prod_pvt;
	gop = xnbp->x_tx_top;

	/*
	 * Similar to RING_HAS_UNCONSUMED_REQUESTS(&xnbp->x_rx_ring) but
	 * using local variables.
	 */
#define	XNB_RING_HAS_UNCONSUMED_REQUESTS(_r)		\
	((((_r)->sring->req_prod - loop) <		\
		(RING_SIZE(_r) - (loop - prod))) ?	\
	    ((_r)->sring->req_prod - loop) :		\
	    (RING_SIZE(_r) - (loop - prod)))

	while ((mp != NULL) &&
	    XNB_RING_HAS_UNCONSUMED_REQUESTS(&xnbp->x_rx_ring)) {

		mfn_t mfn;
		pfn_t pfn;
		netif_rx_request_t *rxreq;
		netif_rx_response_t *rxresp;
		char *valoop;
		size_t offset;
		mblk_t *ml;
		uint16_t cksum_flags;

		/* 1 */
		if ((mfn = xnb_alloc_page(xnbp)) == 0) {
			xnbp->x_stat_xmit_defer++;
			break;
		}

		/* 2 */
		rxreq = RING_GET_REQUEST(&xnbp->x_rx_ring, loop);

#ifdef XNB_DEBUG
		if (!(rxreq->id < NET_RX_RING_SIZE))
			cmn_err(CE_PANIC, "xnb_to_peer: "
			    "id %d out of range in request 0x%p",
			    rxreq->id, (void *)rxreq);
		if (rxreq->gref >= NR_GRANT_ENTRIES)
			cmn_err(CE_PANIC, "xnb_to_peer: "
			    "grant ref %d out of range in request 0x%p",
			    rxreq->gref, (void *)rxreq);
#endif /* XNB_DEBUG */

		/* Assign a pfn and map the new page at the allocated va. */
		pfn = xen_assign_pfn(mfn);
		hat_devload(kas.a_hat, xnbp->x_tx_va, PAGESIZE,
		    pfn, PROT_READ | PROT_WRITE, HAT_LOAD);

		offset = TX_BUFFER_HEADROOM;

		/* 3 */
		len = 0;
		valoop = xnbp->x_tx_va + offset;
		for (ml = mp; ml != NULL; ml = ml->b_cont) {
			size_t chunk = ml->b_wptr - ml->b_rptr;

			bcopy(ml->b_rptr, valoop, chunk);
			valoop += chunk;
			len += chunk;
		}

		ASSERT(len + offset < PAGESIZE);

		/* Release the pfn. */
		hat_unload(kas.a_hat, xnbp->x_tx_va, PAGESIZE,
		    HAT_UNLOAD_UNMAP);
		xen_release_pfn(pfn);

		/* 4 */
		gop->mfn = mfn;
		gop->domid = xnbp->x_peer;
		gop->ref = rxreq->gref;

		/* 5.1 */
		rxresp = RING_GET_RESPONSE(&xnbp->x_rx_ring, prod);
		rxresp->offset = offset;
		rxresp->flags = 0;

		cksum_flags = xnbp->x_flavour->xf_cksum_to_peer(xnbp, mp);
		if (cksum_flags != 0)
			xnbp->x_stat_tx_cksum_deferred++;
		rxresp->flags |= cksum_flags;

		rxresp->id = RING_GET_REQUEST(&xnbp->x_rx_ring, prod)->id;
		rxresp->status = len;

		loop++;
		prod++;
		gop++;
		prev = mp;
		mp = mp->b_next;
	}

	/*
	 * Did we actually do anything?
	 */
	if (loop == xnbp->x_rx_ring.req_cons) {
		mutex_exit(&xnbp->x_tx_lock);
		return (mp);
	}

	end = loop;

	/*
	 * Unlink the end of the 'done' list from the remainder.
	 */
	ASSERT(prev != NULL);
	prev->b_next = NULL;

	if (HYPERVISOR_grant_table_op(GNTTABOP_transfer, xnbp->x_tx_top,
	    loop - xnbp->x_rx_ring.req_cons) != 0) {
		cmn_err(CE_WARN, "xnb_to_peer: transfer operation failed");
	}

	loop = xnbp->x_rx_ring.req_cons;
	prod = xnbp->x_rx_ring.rsp_prod_pvt;
	gop = xnbp->x_tx_top;

	while (loop < end) {
		int16_t status = NETIF_RSP_OKAY;

		if (gop->status != 0) {
			status = NETIF_RSP_ERROR;

			/*
			 * If the status is anything other than
			 * GNTST_bad_page then we don't own the page
			 * any more, so don't try to give it back.
			 */
			if (gop->status != GNTST_bad_page)
				gop->mfn = 0;
		} else {
			/* The page is no longer ours. */
			gop->mfn = 0;
		}

		if (gop->mfn != 0)
			/*
			 * Give back the page, as we won't be using
			 * it.
			 */
			xnb_free_page(xnbp, gop->mfn);
		else
			/*
			 * We gave away a page, update our accounting
			 * now.
			 */
			balloon_drv_subtracted(1);

		/* 5.2 */
		if (status != NETIF_RSP_OKAY) {
			RING_GET_RESPONSE(&xnbp->x_rx_ring, prod)->status =
			    status;
		} else {
			xnbp->x_stat_opackets++;
			xnbp->x_stat_obytes += len;
		}

		loop++;
		prod++;
		gop++;
	}

	xnbp->x_rx_ring.req_cons = loop;
	xnbp->x_rx_ring.rsp_prod_pvt = prod;

	/* 6 */
	/*LINTED: constant in conditional context*/
	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&xnbp->x_rx_ring, notify);
	if (notify) {
		ec_notify_via_evtchn(xnbp->x_evtchn);
		xnbp->x_stat_tx_notify_sent++;
	} else {
		xnbp->x_stat_tx_notify_deferred++;
	}

	if (mp != NULL)
		xnbp->x_stat_xmit_defer++;

	mutex_exit(&xnbp->x_tx_lock);

	/* Free mblk_t's that we consumed. */
	freemsgchain(free);

	return (mp);
}

/*ARGSUSED*/
static int
xnb_rxbuf_constructor(void *buf, void *arg, int kmflag)
{
	xnb_rxbuf_t *rxp = buf;

	bzero(rxp, sizeof (*rxp));

	rxp->xr_free_rtn.free_func = xnb_rx_complete;
	rxp->xr_free_rtn.free_arg = (caddr_t)rxp;

	rxp->xr_mop.host_addr =
	    (uint64_t)(uintptr_t)vmem_alloc(heap_arena, PAGESIZE,
	    ((kmflag & KM_NOSLEEP) == KM_NOSLEEP) ?
	    VM_NOSLEEP : VM_SLEEP);

	if (rxp->xr_mop.host_addr == NULL) {
		cmn_err(CE_WARN, "xnb_rxbuf_constructor: "
		    "cannot get address space");
		return (-1);
	}

	/*
	 * Have the hat ensure that page table exists for the VA.
	 */
	hat_prepare_mapping(kas.a_hat,
	    (caddr_t)(uintptr_t)rxp->xr_mop.host_addr);

	return (0);
}

/*ARGSUSED*/
static void
xnb_rxbuf_destructor(void *buf, void *arg)
{
	xnb_rxbuf_t *rxp = buf;

	ASSERT(rxp->xr_mop.host_addr != NULL);
	ASSERT((rxp->xr_flags & XNB_RXBUF_INUSE) == 0);

	hat_release_mapping(kas.a_hat,
	    (caddr_t)(uintptr_t)rxp->xr_mop.host_addr);
	vmem_free(heap_arena,
	    (caddr_t)(uintptr_t)rxp->xr_mop.host_addr, PAGESIZE);
}

static void
xnb_rx_notify_peer(xnb_t *xnbp)
{
	boolean_t notify;

	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));

	/*LINTED: constant in conditional context*/
	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&xnbp->x_tx_ring, notify);
	if (notify) {
		ec_notify_via_evtchn(xnbp->x_evtchn);
		xnbp->x_stat_rx_notify_sent++;
	} else {
		xnbp->x_stat_rx_notify_deferred++;
	}
}

static void
xnb_rx_complete(xnb_rxbuf_t *rxp)
{
	xnb_t *xnbp = rxp->xr_xnbp;

	ASSERT((rxp->xr_flags & XNB_RXBUF_INUSE) == XNB_RXBUF_INUSE);

	mutex_enter(&xnbp->x_rx_lock);

	xnb_rx_schedule_unmop(xnbp, &rxp->xr_mop);
	xnb_rx_perform_pending_unmop(xnbp);

	if (xnbp->x_connected) {
		xnb_rx_mark_complete(xnbp, rxp->xr_id, rxp->xr_status);
		xnb_rx_notify_peer(xnbp);
	}

	xnb_rxbuf_put(xnbp, rxp);

	mutex_exit(&xnbp->x_rx_lock);
}

static void
xnb_rx_mark_complete(xnb_t *xnbp, RING_IDX id, int16_t status)
{
	RING_IDX i;
	netif_tx_response_t *txresp;

	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));

	i = xnbp->x_tx_ring.rsp_prod_pvt;

	txresp = RING_GET_RESPONSE(&xnbp->x_tx_ring, i);
	txresp->id = id;
	txresp->status = status;

	xnbp->x_tx_ring.rsp_prod_pvt = i + 1;

	/*
	 * Note that we don't push the change to the peer here - that
	 * is the callers responsibility.
	 */
}

/*
 * XXPV dme: currently pending unmap operations are stored on a
 * per-instance basis.  Should they be per-driver?  The locking would
 * have to change (obviously), but there might be an improvement from
 * batching more together.  Right now they are all 'done' either at
 * the tail of each receive operation (copy case) or on each
 * completion (non-copy case).  Should that be changed to some
 * interval (watermark?) to improve the chance of batching?
 */
static void
xnb_rx_schedule_unmop(xnb_t *xnbp, gnttab_map_grant_ref_t *mop)
{
	gnttab_unmap_grant_ref_t *unmop;

	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));
	ASSERT(xnbp->x_rx_unmop_count <= NET_TX_RING_SIZE);

	unmop = &xnbp->x_rx_unmop[xnbp->x_rx_unmop_count];
	xnbp->x_rx_unmop_count++;

	unmop->host_addr = mop->host_addr;
	unmop->dev_bus_addr = mop->dev_bus_addr;
	unmop->handle = mop->handle;

#ifdef XNB_DEBUG
	if (xnbp->x_rx_unmop_count <= NET_TX_RING_SIZE)
		ASSERT(xnbp->x_rx_unmop[xnbp->x_rx_unmop_count].host_addr
		    == NULL);
#endif /* XNB_DEBUG */

}

static void
xnb_rx_perform_pending_unmop(xnb_t *xnbp)
{
#ifdef XNB_DEBUG
	RING_IDX loop;
	gnttab_unmap_grant_ref_t *unmop;
#endif /* XNB_DEBUG */

	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));

	if (xnbp->x_rx_unmop_count == 0)
		return;

	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
	    xnbp->x_rx_unmop, xnbp->x_rx_unmop_count) < 0) {
		cmn_err(CE_WARN, "xnb_rx_perform_pending_unmop: "
		    "unmap grant operation failed, "
		    "%d pages lost", xnbp->x_rx_unmop_count);
	}

#ifdef XNB_DEBUG
	for (loop = 0, unmop = xnbp->x_rx_unmop;
	    loop < xnbp->x_rx_unmop_count;
	    loop++, unmop++) {
		if (unmop->status != 0) {
			cmn_err(CE_WARN, "xnb_rx_perform_pending_unmop: "
			    "unmap grant reference failed (%d)",
			    unmop->status);
		}
	}
#endif /* XNB_DEBUG */

	xnbp->x_rx_unmop_count = 0;

#ifdef XNB_DEBUG
	bzero(xnbp->x_rx_unmop, sizeof (xnbp->x_rx_unmop));
#endif /* XNB_DEBUG */
}

static xnb_rxbuf_t *
xnb_rxbuf_get(xnb_t *xnbp, int flags)
{
	xnb_rxbuf_t *rxp;

	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));

	rxp = kmem_cache_alloc(xnb_rxbuf_cachep, flags);
	if (rxp != NULL) {
		ASSERT((rxp->xr_flags & XNB_RXBUF_INUSE) == 0);
		rxp->xr_flags |= XNB_RXBUF_INUSE;

		rxp->xr_xnbp = xnbp;
		rxp->xr_mop.dom = xnbp->x_peer;

		rxp->xr_mop.flags = GNTMAP_host_map;
		if (!xnbp->x_rx_pages_writable)
			rxp->xr_mop.flags |= GNTMAP_readonly;

		xnbp->x_rx_buf_count++;
	}

	return (rxp);
}

static void
xnb_rxbuf_put(xnb_t *xnbp, xnb_rxbuf_t *rxp)
{
	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));
	ASSERT((rxp->xr_flags & XNB_RXBUF_INUSE) == XNB_RXBUF_INUSE);

	rxp->xr_flags &= ~XNB_RXBUF_INUSE;
	xnbp->x_rx_buf_count--;

	kmem_cache_free(xnb_rxbuf_cachep, rxp);
}

static mblk_t *
xnb_recv(xnb_t *xnbp)
{
	RING_IDX start, end, loop;
	gnttab_map_grant_ref_t *mop;
	xnb_rxbuf_t **rxpp;
	netif_tx_request_t *txreq;
	boolean_t work_to_do;
	mblk_t *head, *tail;
	/*
	 * If the peer granted a read-only mapping to the page then we
	 * must copy the data, as the local protocol stack (should the
	 * packet be destined for this host) will modify the packet
	 * 'in place'.
	 */
	boolean_t copy = !xnbp->x_rx_pages_writable;

	/*
	 * For each individual request, the sequence of actions is:
	 *
	 * 1. get the request.
	 * 2. map the page based on the grant ref.
	 * 3. allocate an mblk, copy the data to it.
	 * 4. release the grant.
	 * 5. update the ring.
	 * 6. pass the packet upward.
	 * 7. kick the peer.
	 *
	 * In fact, we try to perform the grant operations in batches,
	 * so there are two loops.
	 */

	head = tail = NULL;
around:
	ASSERT(MUTEX_HELD(&xnbp->x_rx_lock));

	/*LINTED: constant in conditional context*/
	RING_FINAL_CHECK_FOR_REQUESTS(&xnbp->x_tx_ring, work_to_do);
	if (!work_to_do) {
finished:
		xnb_rx_notify_peer(xnbp);

		return (head);
	}

	start = xnbp->x_tx_ring.req_cons;
	end = xnbp->x_tx_ring.sring->req_prod;

	for (loop = start, mop = xnbp->x_rx_mop, rxpp = xnbp->x_rx_bufp;
	    loop != end;
	    loop++, mop++, rxpp++) {
		xnb_rxbuf_t *rxp;

		rxp = xnb_rxbuf_get(xnbp, KM_NOSLEEP);
		if (rxp == NULL)
			break;

		ASSERT(xnbp->x_rx_pages_writable ||
		    ((rxp->xr_mop.flags & GNTMAP_readonly)
		    == GNTMAP_readonly));

		rxp->xr_mop.ref =
		    RING_GET_REQUEST(&xnbp->x_tx_ring, loop)->gref;

		ASSERT(rxp->xr_mop.ref < NR_GRANT_ENTRIES);

		*mop = rxp->xr_mop;
		*rxpp = rxp;
	}

	if ((loop - start) == 0)
		goto finished;

	end = loop;

	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
	    xnbp->x_rx_mop, end - start) != 0) {

		cmn_err(CE_WARN, "xnb_recv: map grant operation failed");

		loop = start;
		rxpp = xnbp->x_rx_bufp;

		while (loop != end) {
			xnb_rxbuf_put(xnbp, *rxpp);

			loop++;
			rxpp++;
		}

		goto finished;
	}

	for (loop = start, mop = xnbp->x_rx_mop, rxpp = xnbp->x_rx_bufp;
	    loop != end;
	    loop++, mop++, rxpp++) {
		mblk_t *mp = NULL;
		int16_t status = NETIF_RSP_OKAY;
		xnb_rxbuf_t *rxp = *rxpp;

		if (mop->status != 0) {
			cmn_err(CE_WARN, "xnb_recv: "
			    "failed to map buffer: %d",
			    mop->status);
			status = NETIF_RSP_ERROR;
		}

		txreq = RING_GET_REQUEST(&xnbp->x_tx_ring, loop);

		if (status == NETIF_RSP_OKAY) {
			if (copy) {
				mp = allocb(txreq->size, BPRI_MED);
				if (mp == NULL) {
					status = NETIF_RSP_ERROR;
					xnbp->x_stat_rx_allocb_failed++;
				} else {
					bcopy((caddr_t)(uintptr_t)
					    mop->host_addr + txreq->offset,
					    mp->b_wptr, txreq->size);
					mp->b_wptr += txreq->size;
				}
			} else {
				mp = desballoc((unsigned char *)(uintptr_t)
				    mop->host_addr + txreq->offset,
				    txreq->size, 0, &rxp->xr_free_rtn);
				if (mp == NULL) {
					status = NETIF_RSP_ERROR;
					xnbp->x_stat_rx_allocb_failed++;
				} else {
					rxp->xr_id = txreq->id;
					rxp->xr_status = status;
					rxp->xr_mop = *mop;

					mp->b_wptr += txreq->size;
				}
			}

			/*
			 * If we have a buffer and there are checksum
			 * flags, process them appropriately.
			 */
			if ((mp != NULL) &&
			    ((txreq->flags &
			    (NETTXF_csum_blank | NETTXF_data_validated))
			    != 0)) {
				mp = xnbp->x_flavour->xf_cksum_from_peer(xnbp,
				    mp, txreq->flags);
				xnbp->x_stat_rx_cksum_no_need++;
			}
		}

		if (copy || (mp == NULL)) {
			xnb_rx_mark_complete(xnbp, txreq->id, status);
			xnb_rx_schedule_unmop(xnbp, mop);
		}

		if (mp != NULL) {
			xnbp->x_stat_ipackets++;
			xnbp->x_stat_rbytes += txreq->size;

			mp->b_next = NULL;
			if (head == NULL) {
				ASSERT(tail == NULL);
				head = mp;
			} else {
				ASSERT(tail != NULL);
				tail->b_next = mp;
			}
			tail = mp;
		}
	}

	/*
	 * This has to be here rather than in the 'finished' code
	 * because we can only handle NET_TX_RING_SIZE pending unmap
	 * operations, which may be exceeded by multiple trips around
	 * the receive loop during heavy load (one trip around the
	 * loop cannot generate more than NET_TX_RING_SIZE unmap
	 * operations).
	 */
	xnb_rx_perform_pending_unmop(xnbp);
	if (copy) {
		for (loop = start, rxpp = xnbp->x_rx_bufp;
		    loop != end;
		    loop++, rxpp++)
			xnb_rxbuf_put(xnbp, *rxpp);
	}

	xnbp->x_tx_ring.req_cons = loop;

	goto around;
	/* NOTREACHED */
}

/*
 *  intr() -- ring interrupt service routine
 */
static uint_t
xnb_intr(caddr_t arg)
{
	xnb_t *xnbp = (xnb_t *)arg;
	mblk_t *mp;

	xnbp->x_stat_intr++;

	mutex_enter(&xnbp->x_rx_lock);

	ASSERT(xnbp->x_connected);

	mp = xnb_recv(xnbp);

	mutex_exit(&xnbp->x_rx_lock);

	if (!xnbp->x_hotplugged) {
		xnbp->x_stat_rx_too_early++;
		goto fail;
	}
	if (mp == NULL) {
		xnbp->x_stat_spurious_intr++;
		goto fail;
	}

	xnbp->x_flavour->xf_recv(xnbp, mp);

	return (DDI_INTR_CLAIMED);

fail:
	freemsgchain(mp);
	return (DDI_INTR_CLAIMED);
}

static boolean_t
xnb_connect_rings(dev_info_t *dip)
{
	xnb_t *xnbp = ddi_get_driver_private(dip);
	char *oename;
	struct gnttab_map_grant_ref map_op;
	evtchn_port_t evtchn;
	int i;

	/*
	 * Cannot attempt to connect the rings if already connected.
	 */
	ASSERT(!xnbp->x_connected);

	oename = xvdi_get_oename(dip);

	if (xenbus_gather(XBT_NULL, oename,
	    "event-channel", "%u", &evtchn,
	    "tx-ring-ref", "%lu", &xnbp->x_tx_ring_ref,
	    "rx-ring-ref", "%lu", &xnbp->x_rx_ring_ref,
	    NULL) != 0) {
		cmn_err(CE_WARN, "xnb_connect_rings: "
		    "cannot read other-end details from %s",
		    oename);
		goto fail;
	}

	if (xenbus_scanf(XBT_NULL, oename,
	    "feature-tx-writable", "%d", &i) != 0)
		i = 0;
	if (i != 0)
		xnbp->x_rx_pages_writable = B_TRUE;

	if (xenbus_scanf(XBT_NULL, oename,
	    "feature-no-csum-offload", "%d", &i) != 0)
		i = 0;
	if ((i == 1) || !xnbp->x_cksum_offload)
		xnbp->x_cksum_offload = B_FALSE;

	/*
	 * 1. allocate a vaddr for the tx page, one for the rx page.
	 * 2. call GNTTABOP_map_grant_ref to map the relevant pages
	 *    into the allocated vaddr (one for tx, one for rx).
	 * 3. call EVTCHNOP_bind_interdomain to have the event channel
	 *    bound to this domain.
	 * 4. associate the event channel with an interrupt.
	 * 5. declare ourselves connected.
	 * 6. enable the interrupt.
	 */

	/* 1.tx */
	xnbp->x_tx_ring_addr = vmem_xalloc(heap_arena, PAGESIZE, PAGESIZE,
	    0, 0, 0, 0, VM_SLEEP);
	ASSERT(xnbp->x_tx_ring_addr != NULL);

	/* 2.tx */
	map_op.host_addr = (uint64_t)((long)xnbp->x_tx_ring_addr);
	map_op.flags = GNTMAP_host_map;
	map_op.ref = xnbp->x_tx_ring_ref;
	map_op.dom = xnbp->x_peer;
	hat_prepare_mapping(kas.a_hat, xnbp->x_tx_ring_addr);
	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
	    &map_op, 1) != 0 || map_op.status != 0) {
		cmn_err(CE_WARN, "xnb_connect_rings: cannot map tx-ring page.");
		goto fail;
	}
	xnbp->x_tx_ring_handle = map_op.handle;

	/*LINTED: constant in conditional context*/
	BACK_RING_INIT(&xnbp->x_tx_ring,
	    (netif_tx_sring_t *)xnbp->x_tx_ring_addr, PAGESIZE);

	/* 1.rx */
	xnbp->x_rx_ring_addr = vmem_xalloc(heap_arena, PAGESIZE, PAGESIZE,
	    0, 0, 0, 0, VM_SLEEP);
	ASSERT(xnbp->x_rx_ring_addr != NULL);

	/* 2.rx */
	map_op.host_addr = (uint64_t)((long)xnbp->x_rx_ring_addr);
	map_op.flags = GNTMAP_host_map;
	map_op.ref = xnbp->x_rx_ring_ref;
	map_op.dom = xnbp->x_peer;
	hat_prepare_mapping(kas.a_hat, xnbp->x_rx_ring_addr);
	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
	    &map_op, 1) != 0 || map_op.status != 0) {
		cmn_err(CE_WARN, "xnb_connect_rings: cannot map rx-ring page.");
		goto fail;
	}
	xnbp->x_rx_ring_handle = map_op.handle;

	/*LINTED: constant in conditional context*/
	BACK_RING_INIT(&xnbp->x_rx_ring,
	    (netif_rx_sring_t *)xnbp->x_rx_ring_addr, PAGESIZE);

	/* 3 */
	if (xvdi_bind_evtchn(dip, evtchn) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "xnb_connect_rings: "
		    "cannot bind event channel %d", xnbp->x_evtchn);
		xnbp->x_evtchn = INVALID_EVTCHN;
		goto fail;
	}
	xnbp->x_evtchn = xvdi_get_evtchn(dip);

	/*
	 * It would be good to set the state to XenbusStateConnected
	 * here as well, but then what if ddi_add_intr() failed?
	 * Changing the state in the store will be noticed by the peer
	 * and cannot be "taken back".
	 */
	mutex_enter(&xnbp->x_tx_lock);
	mutex_enter(&xnbp->x_rx_lock);

	/* 5.1 */
	xnbp->x_connected = B_TRUE;

	mutex_exit(&xnbp->x_rx_lock);
	mutex_exit(&xnbp->x_tx_lock);

	/* 4, 6 */
	if (ddi_add_intr(dip, 0, NULL, NULL, xnb_intr, (caddr_t)xnbp)
	    != DDI_SUCCESS) {
		cmn_err(CE_WARN, "xnb_connect_rings: cannot add interrupt");
		goto fail;
	}
	xnbp->x_irq = B_TRUE;

	/* 5.2 */
	(void) xvdi_switch_state(dip, XBT_NULL, XenbusStateConnected);

	return (B_TRUE);

fail:
	mutex_enter(&xnbp->x_tx_lock);
	mutex_enter(&xnbp->x_rx_lock);

	xnbp->x_connected = B_FALSE;

	mutex_exit(&xnbp->x_rx_lock);
	mutex_exit(&xnbp->x_tx_lock);

	return (B_FALSE);
}

static void
xnb_disconnect_rings(dev_info_t *dip)
{
	xnb_t *xnbp = ddi_get_driver_private(dip);

	if (xnbp->x_irq) {
		ddi_remove_intr(dip, 0, NULL);
		xnbp->x_irq = B_FALSE;
	}

	if (xnbp->x_evtchn != INVALID_EVTCHN) {
		xvdi_free_evtchn(dip);
		xnbp->x_evtchn = INVALID_EVTCHN;
	}

	if (xnbp->x_rx_ring_handle != INVALID_GRANT_HANDLE) {
		struct gnttab_unmap_grant_ref unmap_op;

		unmap_op.host_addr = (uint64_t)(uintptr_t)xnbp->x_rx_ring_addr;
		unmap_op.dev_bus_addr = 0;
		unmap_op.handle = xnbp->x_rx_ring_handle;
		if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
		    &unmap_op, 1) != 0)
			cmn_err(CE_WARN, "xnb_disconnect_rings: "
			    "cannot unmap rx-ring page (%d)",
			    unmap_op.status);

		xnbp->x_rx_ring_handle = INVALID_GRANT_HANDLE;
	}

	if (xnbp->x_rx_ring_addr != NULL) {
		hat_release_mapping(kas.a_hat, xnbp->x_rx_ring_addr);
		vmem_free(heap_arena, xnbp->x_rx_ring_addr, PAGESIZE);
		xnbp->x_rx_ring_addr = NULL;
	}

	if (xnbp->x_tx_ring_handle != INVALID_GRANT_HANDLE) {
		struct gnttab_unmap_grant_ref unmap_op;

		unmap_op.host_addr = (uint64_t)(uintptr_t)xnbp->x_tx_ring_addr;
		unmap_op.dev_bus_addr = 0;
		unmap_op.handle = xnbp->x_tx_ring_handle;
		if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
		    &unmap_op, 1) != 0)
			cmn_err(CE_WARN, "xnb_disconnect_rings: "
			    "cannot unmap tx-ring page (%d)",
			    unmap_op.status);

		xnbp->x_tx_ring_handle = INVALID_GRANT_HANDLE;
	}

	if (xnbp->x_tx_ring_addr != NULL) {
		hat_release_mapping(kas.a_hat, xnbp->x_tx_ring_addr);
		vmem_free(heap_arena, xnbp->x_tx_ring_addr, PAGESIZE);
		xnbp->x_tx_ring_addr = NULL;
	}
}

/*ARGSUSED*/
static void
xnb_oe_state_change(dev_info_t *dip, ddi_eventcookie_t id,
    void *arg, void *impl_data)
{
	xnb_t *xnbp = ddi_get_driver_private(dip);
	XenbusState new_state = *(XenbusState *)impl_data;

	ASSERT(xnbp != NULL);

	switch (new_state) {
	case XenbusStateConnected:
		if (xnb_connect_rings(dip)) {
			xnbp->x_flavour->xf_peer_connected(xnbp);
		} else {
			xnbp->x_flavour->xf_peer_disconnected(xnbp);
			xnb_disconnect_rings(dip);
			(void) xvdi_switch_state(dip, XBT_NULL,
			    XenbusStateClosed);
			(void) xvdi_post_event(dip, XEN_HP_REMOVE);
		}

		/*
		 * Now that we've attempted to connect it's reasonable
		 * to allow an attempt to detach.
		 */
		xnbp->x_detachable = B_TRUE;

		break;

	case XenbusStateClosing:
		(void) xvdi_switch_state(dip, XBT_NULL, XenbusStateClosing);

		break;

	case XenbusStateClosed:
		xnbp->x_flavour->xf_peer_disconnected(xnbp);

		mutex_enter(&xnbp->x_tx_lock);
		mutex_enter(&xnbp->x_rx_lock);

		xnb_disconnect_rings(dip);
		xnbp->x_connected = B_FALSE;

		mutex_exit(&xnbp->x_rx_lock);
		mutex_exit(&xnbp->x_tx_lock);

		(void) xvdi_switch_state(dip, XBT_NULL, XenbusStateClosed);
		(void) xvdi_post_event(dip, XEN_HP_REMOVE);
		/*
		 * In all likelyhood this is already set (in the above
		 * case), but if the peer never attempted to connect
		 * and the domain is destroyed we get here without
		 * having been through the case above, so we set it to
		 * be sure.
		 */
		xnbp->x_detachable = B_TRUE;

		break;

	default:
		break;
	}
}

/*ARGSUSED*/
static void
xnb_hp_state_change(dev_info_t *dip, ddi_eventcookie_t id,
    void *arg, void *impl_data)
{
	xnb_t *xnbp = ddi_get_driver_private(dip);
	xendev_hotplug_state_t state = *(xendev_hotplug_state_t *)impl_data;
	boolean_t success;

	ASSERT(xnbp != NULL);

	switch (state) {
	case Connected:

		success = xnbp->x_flavour->xf_hotplug_connected(xnbp);

		mutex_enter(&xnbp->x_tx_lock);
		mutex_enter(&xnbp->x_rx_lock);

		xnbp->x_hotplugged = success;

		mutex_exit(&xnbp->x_rx_lock);
		mutex_exit(&xnbp->x_tx_lock);
		break;

	default:
		break;
	}
}

static struct modldrv modldrv = {
	&mod_miscops, "xnb module %I%",
};

static struct modlinkage modlinkage = {
	MODREV_1, &modldrv, NULL
};

int
_init(void)
{
	int i;

	mutex_init(&xnb_alloc_page_lock, NULL, MUTEX_DRIVER, NULL);

	xnb_rxbuf_cachep = kmem_cache_create("xnb_rxbuf_cachep",
	    sizeof (xnb_rxbuf_t), 0, xnb_rxbuf_constructor,
	    xnb_rxbuf_destructor, NULL, NULL, NULL, 0);
	ASSERT(xnb_rxbuf_cachep != NULL);

	i = mod_install(&modlinkage);
	if (i != DDI_SUCCESS) {
		kmem_cache_destroy(xnb_rxbuf_cachep);
		mutex_destroy(&xnb_alloc_page_lock);
	}
	return (i);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

int
_fini(void)
{
	int i;

	i = mod_remove(&modlinkage);
	if (i == DDI_SUCCESS) {
		kmem_cache_destroy(xnb_rxbuf_cachep);
		mutex_destroy(&xnb_alloc_page_lock);
	}
	return (i);
}