summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/nxge/nxge_hio_guest.c
blob: dc26a0de7a16fc0061ec9e11f7175b231c0f4a5a (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
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * nxge_hio_guest.c
 *
 * This file manages the virtualization resources for a guest domain.
 *
 */

#include <sys/nxge/nxge_impl.h>
#include <sys/nxge/nxge_fzc.h>
#include <sys/nxge/nxge_rxdma.h>
#include <sys/nxge/nxge_txdma.h>
#include <sys/nxge/nxge_hio.h>

/*
 * nxge_guest_regs_map
 *
 *	Map in a guest domain's register set(s).
 *
 * Arguments:
 * 	nxge
 *
 * Notes:
 *	Note that we set <is_vraddr> to TRUE.
 *
 * Context:
 *	Guest domain
 */
static ddi_device_acc_attr_t nxge_guest_register_access_attributes = {
	DDI_DEVICE_ATTR_V0,
	DDI_STRUCTURE_LE_ACC,
	DDI_STRICTORDER_ACC,
};

int
nxge_guest_regs_map(nxge_t *nxge)
{
	dev_regs_t 	*regs;
	off_t		regsize;
	int rv;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_guest_regs_map"));

	/* So we can allocate properly-aligned memory. */
	nxge->niu_type = N2_NIU; /* Version 1.0 only */
	nxge->function_num = nxge->instance; /* HIOXXX Looking for ideas. */

	nxge->dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
	regs = nxge->dev_regs;

	if ((rv = ddi_dev_regsize(nxge->dip, 0, &regsize)) != DDI_SUCCESS) {
		NXGE_ERROR_MSG((nxge, HIO_CTL, "ddi_dev_regsize() failed"));
		return (NXGE_ERROR);
	}

	rv = ddi_regs_map_setup(nxge->dip, 0, (caddr_t *)&regs->nxge_regp, 0, 0,
	    &nxge_guest_register_access_attributes, &regs->nxge_regh);

	if (rv != DDI_SUCCESS) {
		NXGE_ERROR_MSG((nxge, HIO_CTL, "ddi_regs_map_setup() failed"));
		return (NXGE_ERROR);
	}

	nxge->npi_handle.regh = regs->nxge_regh;
	nxge->npi_handle.regp = (npi_reg_ptr_t)regs->nxge_regp;
	nxge->npi_handle.is_vraddr = B_TRUE;
	nxge->npi_handle.function.instance = nxge->instance;
	nxge->npi_handle.function.function = nxge->function_num;
	nxge->npi_handle.nxgep = (void *)nxge;

	/* NPI_REG_ADD_HANDLE_SET() */
	nxge->npi_reg_handle.regh = regs->nxge_regh;
	nxge->npi_reg_handle.regp = (npi_reg_ptr_t)regs->nxge_regp;
	nxge->npi_reg_handle.is_vraddr = B_TRUE;
	nxge->npi_reg_handle.function.instance = nxge->instance;
	nxge->npi_reg_handle.function.function = nxge->function_num;
	nxge->npi_reg_handle.nxgep = (void *)nxge;

	/* NPI_VREG_ADD_HANDLE_SET() */
	nxge->npi_vreg_handle.regh = regs->nxge_regh;
	nxge->npi_vreg_handle.regp = (npi_reg_ptr_t)regs->nxge_regp;
	nxge->npi_vreg_handle.is_vraddr = B_TRUE;
	nxge->npi_vreg_handle.function.instance = nxge->instance;
	nxge->npi_vreg_handle.function.function = nxge->function_num;
	nxge->npi_vreg_handle.nxgep = (void *)nxge;

	regs->nxge_vir_regp = regs->nxge_regp;
	regs->nxge_vir_regh = regs->nxge_regh;

	/*
	 * We do NOT set the PCI, MSI-X, 2nd Virtualization,
	 * or FCODE reg variables.
	 */

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_guest_regs_map"));

	return (NXGE_OK);
}

void
nxge_guest_regs_map_free(
	nxge_t *nxge)
{
	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_guest_regs_map_free"));

	if (nxge->dev_regs) {
		if (nxge->dev_regs->nxge_regh) {
			NXGE_DEBUG_MSG((nxge, DDI_CTL,
			    "==> nxge_unmap_regs: device registers"));
			ddi_regs_map_free(&nxge->dev_regs->nxge_regh);
			nxge->dev_regs->nxge_regh = NULL;
		}
		kmem_free(nxge->dev_regs, sizeof (dev_regs_t));
		nxge->dev_regs = 0;
	}

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_guest_regs_map_free"));
}

#if defined(sun4v)

/*
 * -------------------------------------------------------------
 * Local prototypes
 * -------------------------------------------------------------
 */
static nxge_hio_dc_t *nxge_guest_dc_alloc(
	nxge_t *, nxge_hio_vr_t *, nxge_grp_type_t);

static void res_map_parse(nxge_t *, nxge_grp_type_t, uint64_t);
static void nxge_check_guest_state(nxge_hio_vr_t *);

/*
 * nxge_hio_vr_add
 *
 *	If we have been given a virtualization region (VR),
 *	then initialize it.
 *
 * Arguments:
 * 	nxge
 *
 * Notes:
 *
 * Context:
 *	Guest domain
 */
int
nxge_hio_vr_add(nxge_t *nxge)
{
	extern nxge_status_t	nxge_mac_register(p_nxge_t);

	nxge_hio_data_t		*nhd = (nxge_hio_data_t *)nxge->nxge_hw_p->hio;
	nxge_hio_vr_t		*vr;
	nxge_hio_dc_t		*dc;
	int			*reg_val;
	uint_t			reg_len;
	uint8_t			vr_index;
	nxhv_vr_fp_t		*fp;
	uint64_t		vr_address, vr_size;
	uint32_t		cookie;
	nxhv_dc_fp_t		*tx, *rx;
	uint64_t		tx_map, rx_map;
	uint64_t		hv_rv;
	int			i;
	nxge_status_t		status;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_hio_vr_add"));

	if (nhd->type == NXGE_HIO_TYPE_SERVICE) {
		/*
		 * Can't add VR to the service domain from which we came.
		 */
		ASSERT(nhd->type == NXGE_HIO_TYPE_GUEST);
		return (DDI_FAILURE);
	}

	/*
	 * Get our HV cookie.
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxge->dip,
	    0, "reg", &reg_val, &reg_len) != DDI_PROP_SUCCESS) {
		NXGE_DEBUG_MSG((nxge, VPD_CTL, "`reg' property not found"));
		return (DDI_FAILURE);
	}

	cookie = (uint32_t)(reg_val[0]);
	ddi_prop_free(reg_val);

	fp = &nhd->hio.vr;
	hv_rv = (*fp->getinfo)(cookie, &vr_address, &vr_size);
	if (hv_rv != 0) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "vr->getinfo() failed"));
		return (DDI_FAILURE);
	}

	/*
	 * In the guest domain, we can use any VR data structure
	 * we want, because we're not supposed to know which VR
	 * the service domain has allocated to us.
	 *
	 * In the current version, the least significant nybble of
	 * the cookie is the VR region, but that could change
	 * very easily.
	 *
	 * In the future, a guest may have more than one VR allocated
	 * to it, which is why we go through this exercise.
	 */
	MUTEX_ENTER(&nhd->lock);
	for (vr_index = 0; vr_index < FUNC_VIR_MAX; vr_index++) {
		if (nhd->vr[vr_index].nxge == 0) {
			nhd->vr[vr_index].nxge = (uintptr_t)nxge;
			break;
		}
	}
	MUTEX_EXIT(&nhd->lock);

	if (vr_index == FUNC_VIR_MAX) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL, "nxge_hio_vr_add "
		    "no VRs available"));
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "nxge_hio_vr_add(%d): cookie(0x%x)\n",
		    nxge->instance, cookie));
		return (DDI_FAILURE);
	}

	vr = &nhd->vr[vr_index];

	vr->nxge = (uintptr_t)nxge;
	vr->cookie = (uint32_t)cookie;
	vr->address = vr_address;
	vr->size = vr_size;
	vr->region = vr_index;

	/*
	 * This is redundant data, but useful nonetheless.  It helps
	 * us to keep track of which RDCs & TDCs belong to us.
	 */
	if (nxge->tx_set.lg.count == 0)
		(void) nxge_grp_add(nxge, NXGE_TRANSMIT_GROUP);
	if (nxge->rx_set.lg.count == 0)
		(void) nxge_grp_add(nxge, NXGE_RECEIVE_GROUP);

	/*
	 * See nxge_intr.c.
	 */
	if (nxge_hio_intr_init(nxge) != NXGE_OK) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "nxge_hio_intr_init() failed"));
		return (DDI_FAILURE);
	}

	/*
	 * Now we find out which RDCs & TDCs have been allocated to us.
	 */
	tx = &nhd->hio.tx;
	if (tx->get_map) {
		/*
		 * The map we get back is a bitmap of the
		 * virtual Tx DMA channels we own -
		 * they are NOT real channel numbers.
		 */
		hv_rv = (*tx->get_map)(vr->cookie, &tx_map);
		if (hv_rv != 0) {
			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
			    "tx->get_map() failed"));
			return (DDI_FAILURE);
		}
		res_map_parse(nxge, NXGE_TRANSMIT_GROUP, tx_map);

		/*
		 * For each channel, mark these two fields
		 * while we have the VR data structure.
		 */
		for (i = 0; i < VP_CHANNEL_MAX; i++) {
			if ((1 << i) & tx_map) {
				dc = nxge_guest_dc_alloc(nxge, vr,
				    NXGE_TRANSMIT_GROUP);
				if (dc == 0) {
					NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
					    "DC add failed"));
					return (DDI_FAILURE);
				}
				dc->channel = (nxge_channel_t)i;
			}
		}
	}

	rx = &nhd->hio.rx;
	if (rx->get_map) {
		/*
		 * I repeat, the map we get back is a bitmap of
		 * the virtual Rx DMA channels we own -
		 * they are NOT real channel numbers.
		 */
		hv_rv = (*rx->get_map)(vr->cookie, &rx_map);
		if (hv_rv != 0) {
			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
			    "rx->get_map() failed"));
			return (DDI_FAILURE);
		}
		res_map_parse(nxge, NXGE_RECEIVE_GROUP, rx_map);

		/*
		 * For each channel, mark these two fields
		 * while we have the VR data structure.
		 */
		for (i = 0; i < VP_CHANNEL_MAX; i++) {
			if ((1 << i) & rx_map) {
				dc = nxge_guest_dc_alloc(nxge, vr,
				    NXGE_RECEIVE_GROUP);
				if (dc == 0) {
					NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
					    "DC add failed"));
					return (DDI_FAILURE);
				}
				dc->channel = (nxge_channel_t)i;
			}
		}
	}

	status = nxge_mac_register(nxge);
	if (status != NXGE_OK) {
		cmn_err(CE_WARN, "nxge(%d): nxge_mac_register failed\n",
		    nxge->instance);
		return (DDI_FAILURE);
	}

	nxge->hio_vr = vr;	/* For faster lookups. */

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_hio_vr_add"));

	return (DDI_SUCCESS);
}

/*
 * nxge_guest_dc_alloc
 *
 *	Find a free nxge_hio_dc_t data structure.
 *
 * Arguments:
 * 	nxge
 * 	type	TRANSMIT or RECEIVE.
 *
 * Notes:
 *
 * Context:
 *	Guest domain
 */
nxge_hio_dc_t *
nxge_guest_dc_alloc(
	nxge_t *nxge,
	nxge_hio_vr_t *vr,
	nxge_grp_type_t type)
{
	nxge_hio_data_t *nhd = (nxge_hio_data_t *)nxge->nxge_hw_p->hio;
	nxge_hio_dc_t *dc;
	int limit, i;

	/*
	 * In the guest domain, there may be more than one VR.
	 * each one of which will be using the same slots, or
	 * virtual channel numbers.  So the <nhd>'s rdc & tdc
	 * tables must be shared.
	 */
	if (type == NXGE_TRANSMIT_GROUP) {
		dc = &nhd->tdc[0];
		limit = NXGE_MAX_TDCS;
	} else {
		dc = &nhd->rdc[0];
		limit = NXGE_MAX_RDCS;
	}

	MUTEX_ENTER(&nhd->lock);
	for (i = 0; i < limit; i++, dc++) {
		if (dc->vr == 0) {
			dc->vr = vr;
			dc->cookie = vr->cookie;
			MUTEX_EXIT(&nhd->lock);
			return (dc);
		}
	}
	MUTEX_EXIT(&nhd->lock);

	return (0);
}

int
nxge_hio_get_dc_htable_idx(nxge_t *nxge, vpc_type_t type, uint32_t channel)
{
	nxge_hio_dc_t   *dc;

	ASSERT(isLDOMguest(nxge));

	dc = nxge_grp_dc_find(nxge, type, channel);
	if (dc == NULL)
		return (-1);

	return (dc->ldg.vector);
}

/*
 * res_map_parse
 *
 *	Parse a resource map.  The resources are DMA channels, receive
 *	or transmit, depending on <type>.
 *
 * Arguments:
 * 	nxge
 * 	type	Transmit or receive.
 *	res_map	The resource map to parse.
 *
 * Notes:
 *
 * Context:
 *	Guest domain
 */
void
res_map_parse(
	nxge_t *nxge,
	nxge_grp_type_t type,
	uint64_t res_map)
{
	uint8_t slots, mask, slot;
	int first, count;

	nxge_hw_pt_cfg_t *hardware;
	nxge_grp_t *group;

	/* Slots are numbered 0 - 7. */
	slots = (uint8_t)(res_map & 0xff);

	/* Count the number of bits in the bitmap. */
	for (slot = 0, count = 0, mask = 1; slot < 8; slot++) {
		if (slots & mask)
			count++;
		if (count == 1)
			first = slot;
		mask <<= 1;
	}

	hardware = &nxge->pt_config.hw_config;
	group = (type == NXGE_TRANSMIT_GROUP) ?
	    nxge->tx_set.group[0] : nxge->rx_set.group[0];

	/*
	 * A guest domain has one Tx & one Rx group, so far.
	 * In the future, there may be more than one.
	 */
	if (type == NXGE_TRANSMIT_GROUP) {
		nxge_dma_pt_cfg_t *port = &nxge->pt_config;
		nxge_tdc_grp_t *tdc_grp = &nxge->pt_config.tdc_grps[0];

		hardware->tdc.start = first;
		hardware->tdc.count = count;
		hardware->tdc.owned = count;

		tdc_grp->start_tdc = first;
		tdc_grp->max_tdcs = (uint8_t)count;
		tdc_grp->grp_index = group->index;
		tdc_grp->map = slots;

		group->map = slots;

		/*
		 * Pointless in a guest domain.  This bitmap is used
		 * in only one place: nxge_txc_init(),
		 * a service-domain-only function.
		 */
		port->tx_dma_map = slots;

		nxge->tx_set.owned.map |= slots;
	} else {
		nxge_rdc_grp_t *rdc_grp = &nxge->pt_config.rdc_grps[0];

		hardware->start_rdc = first;
		hardware->max_rdcs = count;

		rdc_grp->start_rdc = (uint8_t)first;
		rdc_grp->max_rdcs = (uint8_t)count;
		rdc_grp->def_rdc = (uint8_t)first;

		rdc_grp->map = slots;
		group->map = slots;

		nxge->rx_set.owned.map |= slots;
	}
}

/*
 * nxge_hio_vr_release
 *
 *	Release a virtualization region (VR).
 *
 * Arguments:
 * 	nxge
 *
 * Notes:
 *	We must uninitialize all DMA channels associated with the VR, too.
 *
 *	The service domain will re-initialize these DMA channels later.
 *	See nxge_hio.c:nxge_hio_share_free() for details.
 *
 * Context:
 *	Guest domain
 */
int
nxge_hio_vr_release(nxge_t *nxge)
{
	nxge_hio_data_t	*nhd = (nxge_hio_data_t *)nxge->nxge_hw_p->hio;
	int		vr_index;

	NXGE_DEBUG_MSG((nxge, MEM2_CTL, "==> nxge_hio_vr_release"));

	if (nxge->hio_vr == NULL) {
		return (NXGE_OK);
	}

	/*
	 * Uninitialize interrupts.
	 */
	nxge_hio_intr_uninit(nxge);

	/*
	 * Uninitialize the receive DMA channels.
	 */
	nxge_uninit_rxdma_channels(nxge);

	/*
	 * Uninitialize the transmit DMA channels.
	 */
	nxge_uninit_txdma_channels(nxge);

	/*
	 * Remove both groups. Assumption: only two groups!
	 */
	if (nxge->rx_set.group[0] != NULL)
		nxge_grp_remove(nxge, nxge->rx_set.group[0]);
	if (nxge->tx_set.group[0] != NULL)
		nxge_grp_remove(nxge, nxge->tx_set.group[0]);

	NXGE_DEBUG_MSG((nxge, MEM2_CTL, "<== nxge_hio_vr_release"));

	/*
	 * Clean up.
	 */
	MUTEX_ENTER(&nhd->lock);
	for (vr_index = 0; vr_index < FUNC_VIR_MAX; vr_index++) {
		if (nhd->vr[vr_index].nxge == (uintptr_t)nxge) {
			nhd->vr[vr_index].nxge = (uintptr_t)NULL;
			break;
		}
	}
	MUTEX_EXIT(&nhd->lock);

	return (NXGE_OK);
}

#if defined(NIU_LP_WORKAROUND)
/*
 * nxge_tdc_lp_conf
 *
 *	Configure the logical pages for a TDC.
 *
 * Arguments:
 * 	nxge
 * 	channel	The TDC to configure.
 *
 * Notes:
 *
 * Context:
 *	Guest domain
 */
nxge_status_t
nxge_tdc_lp_conf(
	p_nxge_t nxge,
	int channel)
{
	nxge_hio_dc_t		*dc;
	nxge_dma_common_t	*data;
	nxge_dma_common_t	*control;
	tx_ring_t 		*ring;

	uint64_t		hv_rv;
	uint64_t		ra, size;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_tdc_lp_conf"));

	ring = nxge->tx_rings->rings[channel];

	if (ring->hv_set) {
		/* This shouldn't happen. */
		return (NXGE_OK);
	}

	if (!(dc = nxge_grp_dc_find(nxge, VP_BOUND_TX, channel)))
		return (NXGE_ERROR);

	/*
	 * Initialize logical page 0 for data buffers.
	 *
	 * <orig_ioaddr_pp> & <orig_alength> are initialized in
	 * nxge_main.c:nxge_dma_mem_alloc().
	 */
	data = nxge->tx_buf_pool_p->dma_buf_pool_p[channel];
	ring->hv_tx_buf_base_ioaddr_pp = (uint64_t)data->orig_ioaddr_pp;
	ring->hv_tx_buf_ioaddr_size = (uint64_t)data->orig_alength;

	hv_rv = hv_niu_vrtx_logical_page_conf(dc->cookie,
	    (uint64_t)channel, 0,
	    ring->hv_tx_buf_base_ioaddr_pp,
	    ring->hv_tx_buf_ioaddr_size);

	if (hv_rv != 0) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "<== nxge_tdc_lp_conf: channel %d "
		    "(page 0 data buf) hv: %d "
		    "ioaddr_pp $%p size 0x%llx ",
		    channel, hv_rv,
		    ring->hv_tx_buf_base_ioaddr_pp,
		    ring->hv_tx_buf_ioaddr_size));
		return (NXGE_ERROR | hv_rv);
	}

	ra = size = 0;
	hv_rv = hv_niu_vrtx_logical_page_info(dc->cookie,
	    (uint64_t)channel, 0, &ra, &size);

	NXGE_DEBUG_MSG((nxge, HIO_CTL,
	    "==> nxge_tdc_lp_conf: channel %d "
	    "(page 0 data buf) hv_rv 0x%llx "
	    "set ioaddr_pp $%p set size 0x%llx "
	    "get ra ioaddr_pp $%p get size 0x%llx ",
	    channel, hv_rv, ring->hv_tx_buf_base_ioaddr_pp,
	    ring->hv_tx_buf_ioaddr_size, ra, size));

	/*
	 * Initialize logical page 1 for control buffers.
	 */
	control = nxge->tx_cntl_pool_p->dma_buf_pool_p[channel];
	ring->hv_tx_cntl_base_ioaddr_pp = (uint64_t)control->orig_ioaddr_pp;
	ring->hv_tx_cntl_ioaddr_size = (uint64_t)control->orig_alength;

	hv_rv = hv_niu_vrtx_logical_page_conf(dc->cookie,
	    (uint64_t)channel, (uint64_t)1,
	    ring->hv_tx_cntl_base_ioaddr_pp,
	    ring->hv_tx_cntl_ioaddr_size);

	if (hv_rv != 0) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "<== nxge_tdc_lp_conf: channel %d "
		    "(page 1 cntl buf) hv_rv 0x%llx "
		    "ioaddr_pp $%p size 0x%llx ",
		    channel, hv_rv,
		    ring->hv_tx_cntl_base_ioaddr_pp,
		    ring->hv_tx_cntl_ioaddr_size));
		return (NXGE_ERROR | hv_rv);
	}

	ra = size = 0;
	hv_rv = hv_niu_vrtx_logical_page_info(dc->cookie,
	    (uint64_t)channel, (uint64_t)1, &ra, &size);

	NXGE_DEBUG_MSG((nxge, HIO_CTL,
	    "==> nxge_tdc_lp_conf: channel %d "
	    "(page 1 cntl buf) hv_rv 0x%llx "
	    "set ioaddr_pp $%p set size 0x%llx "
	    "get ra ioaddr_pp $%p get size 0x%llx ",
	    channel, hv_rv, ring->hv_tx_cntl_base_ioaddr_pp,
	    ring->hv_tx_cntl_ioaddr_size, ra, size));

	ring->hv_set = B_TRUE;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_tdc_lp_conf"));

	return (NXGE_OK);
}

/*
 * nxge_rdc_lp_conf
 *
 *	Configure an RDC's logical pages.
 *
 * Arguments:
 * 	nxge
 * 	channel	The RDC to configure.
 *
 * Notes:
 *
 * Context:
 *	Guest domain
 */
nxge_status_t
nxge_rdc_lp_conf(
	p_nxge_t nxge,
	int channel)
{
	nxge_hio_dc_t		*dc;
	nxge_dma_common_t	*data;
	nxge_dma_common_t	*control;
	rx_rbr_ring_t		*ring;

	uint64_t		hv_rv;
	uint64_t		ra, size;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_rdc_lp_conf"));

	ring = nxge->rx_rbr_rings->rbr_rings[channel];

	if (ring->hv_set) {
		return (NXGE_OK);
	}

	if (!(dc = nxge_grp_dc_find(nxge, VP_BOUND_RX, channel)))
		return (NXGE_ERROR);

	/*
	 * Initialize logical page 0 for data buffers.
	 *
	 * <orig_ioaddr_pp> & <orig_alength> are initialized in
	 * nxge_main.c:nxge_dma_mem_alloc().
	 */
	data = nxge->rx_buf_pool_p->dma_buf_pool_p[channel];
	ring->hv_rx_buf_base_ioaddr_pp = (uint64_t)data->orig_ioaddr_pp;
	ring->hv_rx_buf_ioaddr_size = (uint64_t)data->orig_alength;

	hv_rv = hv_niu_vrrx_logical_page_conf(dc->cookie,
	    (uint64_t)channel, 0,
	    ring->hv_rx_buf_base_ioaddr_pp,
	    ring->hv_rx_buf_ioaddr_size);

	if (hv_rv != 0) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "<== nxge_rdc_lp_conf: channel %d "
		    "(page 0 data buf) hv_rv 0x%llx "
		    "ioaddr_pp $%p size 0x%llx ",
		    channel, hv_rv,
		    ring->hv_rx_buf_base_ioaddr_pp,
		    ring->hv_rx_buf_ioaddr_size));
		return (NXGE_ERROR | hv_rv);
	}

	ra = size = 0;
	hv_rv = hv_niu_vrrx_logical_page_info(dc->cookie,
	    (uint64_t)channel, 0, &ra, &size);

	NXGE_DEBUG_MSG((nxge, HIO_CTL,
	    "==> nxge_rdc_lp_conf: channel %d "
	    "(page 0 data buf) hv_rv 0x%llx "
	    "set ioaddr_pp $%p set size 0x%llx "
	    "get ra ioaddr_pp $%p get size 0x%llx ",
	    channel, hv_rv, ring->hv_rx_buf_base_ioaddr_pp,
	    ring->hv_rx_buf_ioaddr_size, ra, size));

	/*
	 * Initialize logical page 1 for control buffers.
	 */
	control = nxge->rx_cntl_pool_p->dma_buf_pool_p[channel];
	ring->hv_rx_cntl_base_ioaddr_pp = (uint64_t)control->orig_ioaddr_pp;
	ring->hv_rx_cntl_ioaddr_size = (uint64_t)control->orig_alength;

	hv_rv = hv_niu_vrrx_logical_page_conf(dc->cookie,
	    (uint64_t)channel, (uint64_t)1,
	    ring->hv_rx_cntl_base_ioaddr_pp,
	    ring->hv_rx_cntl_ioaddr_size);

	if (hv_rv != 0) {
		NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
		    "<== nxge_rdc_lp_conf: channel %d "
		    "(page 1 cntl buf) hv_rv 0x%llx "
		    "ioaddr_pp $%p size 0x%llx ",
		    channel, hv_rv,
		    ring->hv_rx_cntl_base_ioaddr_pp,
		    ring->hv_rx_cntl_ioaddr_size));
		return (NXGE_ERROR | hv_rv);
	}

	ra = size = 0;
	hv_rv = hv_niu_vrrx_logical_page_info(dc->cookie,
	    (uint64_t)channel, (uint64_t)1, &ra, &size);

	NXGE_DEBUG_MSG((nxge, HIO_CTL,
	    "==> nxge_rdc_lp_conf: channel %d "
	    "(page 1 cntl buf) hv_rv 0x%llx "
	    "set ioaddr_pp $%p set size 0x%llx "
	    "get ra ioaddr_pp $%p get size 0x%llx ",
	    channel, hv_rv, ring->hv_rx_cntl_base_ioaddr_pp,
	    ring->hv_rx_cntl_ioaddr_size, ra, size));

	ring->hv_set = B_TRUE;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_rdc_lp_conf"));

	return (NXGE_OK);
}
#endif	/* defined(NIU_LP_WORKAROUND) */

/*
 * This value is in milliseconds.
 */
#define	NXGE_GUEST_TIMER	500 /* 1/2 second, for now */

/*
 * nxge_hio_start_timer
 *
 *	Start the timer which checks for Tx hangs.
 *
 * Arguments:
 * 	nxge
 *
 * Notes:
 *	This function is called from nxge_attach().
 *
 *	This function kicks off the guest domain equivalent of
 *	nxge_check_hw_state().  It is called only once, from attach.
 *
 * Context:
 *	Guest domain
 */
void
nxge_hio_start_timer(
	nxge_t *nxge)
{
	nxge_hio_data_t *nhd = (nxge_hio_data_t *)nxge->nxge_hw_p->hio;
	nxge_hio_vr_t *vr;
	int region;

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "==> nxge_hio_timer_start"));

	MUTEX_ENTER(&nhd->lock);

	/*
	 * Find our VR data structure.  (We are currently assuming
	 * one VR per guest domain.  That may change in the future.)
	 */
	for (region = FUNC0_VIR0; region < NXGE_VR_SR_MAX; region++) {
		if (nhd->vr[region].nxge == (uintptr_t)nxge)
			break;
	}

	MUTEX_EXIT(&nhd->lock);

	if (region == NXGE_VR_SR_MAX) {
		return;
	}

	vr = (nxge_hio_vr_t *)&nhd->vr[region];

	nxge->nxge_timerid = timeout((void(*)(void *))nxge_check_guest_state,
	    (void *)vr, drv_usectohz(1000 * NXGE_GUEST_TIMER));

	NXGE_DEBUG_MSG((nxge, HIO_CTL, "<== nxge_hio_timer_start"));
}

/*
 * nxge_check_guest_state
 *
 *	Essentially, check for Tx hangs.  In the future, if we are
 *	polling the hardware, we may do so here.
 *
 * Arguments:
 * 	vr	The virtualization region (VR) data structure.
 *
 * Notes:
 *	This function is the guest domain equivalent of
 *	nxge_check_hw_state().  Since we have no hardware to
 * 	check, we simply call nxge_check_tx_hang().
 *
 * Context:
 *	Guest domain
 */
void
nxge_check_guest_state(
	nxge_hio_vr_t *vr)
{
	nxge_t *nxge = (nxge_t *)vr->nxge;

	NXGE_DEBUG_MSG((nxge, SYSERR_CTL, "==> nxge_check_guest_state"));

	MUTEX_ENTER(nxge->genlock);
	nxge->nxge_timerid = 0;

	if (nxge->nxge_mac_state == NXGE_MAC_STARTED) {
		nxge_check_tx_hang(nxge);

		nxge->nxge_timerid = timeout((void(*)(void *))
		    nxge_check_guest_state, (caddr_t)vr,
		    drv_usectohz(1000 * NXGE_GUEST_TIMER));
	}

nxge_check_guest_state_exit:
	MUTEX_EXIT(nxge->genlock);
	NXGE_DEBUG_MSG((nxge, SYSERR_CTL, "<== nxge_check_guest_state"));
}

nxge_status_t
nxge_hio_rdc_intr_arm(p_nxge_t nxge, boolean_t arm)
{
	nxge_grp_t	*group;
	uint32_t	channel;
	nxge_hio_dc_t	*dc;
	nxge_ldg_t	*ldgp;

	/*
	 * Validate state of guest interface before
	 * proceeeding.
	 */
	if (!isLDOMguest(nxge))
		return (NXGE_ERROR);
	if (nxge->nxge_mac_state != NXGE_MAC_STARTED)
		return (NXGE_ERROR);

	/*
	 * In guest domain, always and only dealing with
	 * group 0 for an instance of nxge.
	 */
	group = nxge->rx_set.group[0];

	/*
	 * Look to arm the the RDCs for the group.
	 */
	for (channel = 0; channel < NXGE_MAX_RDCS; channel++) {
		if ((1 << channel) & group->map) {
			/*
			 * Get the RDC.
			 */
			dc = nxge_grp_dc_find(nxge, VP_BOUND_RX, channel);
			if (dc == NULL)
				return (NXGE_ERROR);

			/*
			 * Get the RDC's ldg group.
			 */
			ldgp = &nxge->ldgvp->ldgp[dc->ldg.vector];
			if (ldgp == NULL)
				return (NXGE_ERROR);

			/*
			 * Set the state of the group.
			 */
			ldgp->arm = arm;

			nxge_hio_ldgimgn(nxge, ldgp);
		}
	}

	return (NXGE_OK);
}

nxge_status_t
nxge_hio_rdc_enable(p_nxge_t nxge)
{
	nxge_grp_t	*group;
	npi_handle_t	handle;
	uint32_t	channel;
	npi_status_t	rval;

	/*
	 * Validate state of guest interface before
	 * proceeeding.
	 */
	if (!isLDOMguest(nxge))
		return (NXGE_ERROR);
	if (nxge->nxge_mac_state != NXGE_MAC_STARTED)
		return (NXGE_ERROR);

	/*
	 * In guest domain, always and only dealing with
	 * group 0 for an instance of nxge.
	 */
	group = nxge->rx_set.group[0];

	/*
	 * Get the PIO handle.
	 */
	handle = NXGE_DEV_NPI_HANDLE(nxge);

	for (channel = 0; channel < NXGE_MAX_RDCS; channel++) {
		/*
		 * If this channel is in the map, then enable
		 * it.
		 */
		if ((1 << channel) & group->map) {
			/*
			 * Enable the RDC and clear the empty bit.
			 */
			rval = npi_rxdma_cfg_rdc_enable(handle, channel);
			if (rval != NPI_SUCCESS)
				return (NXGE_ERROR);

			(void) npi_rxdma_channel_rbr_empty_clear(handle,
			    channel);
		}
	}

	return (NXGE_OK);
}
#endif	/* defined(sun4v) */