summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4/io/efcode/fcpci.c
blob: cdebc156f3af5f62e32c1d16a05f37e0f025c794 (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
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
/*
 * 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.
 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
 */

/*
 * fcpci.c: Framework PCI fcode ops
 */
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/pci.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/ddidmareq.h>
#include <sys/pci.h>
#include <sys/modctl.h>
#include <sys/ndi_impldefs.h>
#include <sys/fcode.h>
#include <sys/promif.h>
#include <sys/promimpl.h>
#include <sys/ddi_implfuncs.h>

#define	PCI_NPT_bits		(PCI_RELOCAT_B | PCI_PREFETCH_B | PCI_ALIAS_B)
#define	PCI_BDF_bits		(PCI_REG_BDFR_M & ~PCI_REG_REG_M)

#define	PCICFG_CONF_INDIRECT_MAP	1

static int pfc_map_in(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_map_out(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_dma_map_in(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_dma_map_out(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_dma_sync(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_dma_cleanup(dev_info_t *, fco_handle_t, fc_ci_t *);

static int pfc_register_fetch(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_register_store(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_config_fetch(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_config_store(dev_info_t *, fco_handle_t, fc_ci_t *);

static int pfc_probe_address(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_probe_space(dev_info_t *, fco_handle_t, fc_ci_t *);

static int pfc_config_child(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_get_fcode_size(dev_info_t *, fco_handle_t, fc_ci_t *);
static int pfc_get_fcode(dev_info_t *, fco_handle_t, fc_ci_t *);
int prom_get_fcode_size(char *);
int prom_get_fcode(char *, char *);
int pfc_update_assigned_prop(dev_info_t *, pci_regspec_t *);
int pfc_remove_assigned_prop(dev_info_t *, pci_regspec_t *);
int pci_alloc_resource(dev_info_t *, pci_regspec_t);
int pci_free_resource(dev_info_t *, pci_regspec_t);
int pci_alloc_mem_chunk(dev_info_t *,  uint64_t, uint64_t *,  uint64_t *);
int pci_alloc_io_chunk(dev_info_t *,  uint64_t,  uint64_t *, uint64_t *);
static int fcpci_indirect_map(dev_info_t *);

int fcpci_unloadable;

static ddi_dma_attr_t fcpci_dma_attr = {
	DMA_ATTR_V0,	/* version number */
	0x0,		/* lowest usable address */
	0xFFFFFFFFull,	/* high DMA address range */
	0xFFFFFFFFull,	/* DMA counter register */
	1,		/* DMA address alignment */
	1,		/* DMA burstsizes */
	1,		/* min effective DMA size */
	0xFFFFFFFFull,	/* max DMA xfer size */
	0xFFFFFFFFull,	/* segment boundary */
	1,		 /* s/g list length */
	1,		/* granularity of device */
	0		/* DMA transfer flags */
};

#define	HIADDR(n) ((uint32_t)(((uint64_t)(n) & 0xFFFFFFFF00000000)>> 32))
#define	LOADDR(n)((uint32_t)((uint64_t)(n) & 0x00000000FFFFFFFF))
#define	LADDR(lo, hi)    (((uint64_t)(hi) << 32) | (uint32_t)(lo))
#define	PCI_4GIG_LIMIT 0xFFFFFFFFUL
#define	PCI_MEMGRAN 0x100000
#define	PCI_IOGRAN 0x1000


/*
 * Module linkage information for the kernel.
 */
static struct modlmisc modlmisc = {
	&mod_miscops, "FCode pci bus functions"
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlmisc, NULL
};

int
_init(void)
{
	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	if (fcpci_unloadable)
		return (mod_remove(&modlinkage));
	return (EBUSY);
}

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


struct pfc_ops_v {
	char *svc_name;
	fc_ops_t *f;
};

static struct pfc_ops_v pov[] = {
	{	"map-in",		pfc_map_in},
	{	"map-out",		pfc_map_out},
	{	"dma-map-in",		pfc_dma_map_in},
	{	"dma-map-out",		pfc_dma_map_out},
	{	"dma-sync",		pfc_dma_sync},
	{	"rx@",			pfc_register_fetch},
	{	"rl@",			pfc_register_fetch},
	{	"rw@",			pfc_register_fetch},
	{	"rb@",			pfc_register_fetch},
	{	"rx!",			pfc_register_store},
	{	"rl!",			pfc_register_store},
	{	"rw!",			pfc_register_store},
	{	"rb!",			pfc_register_store},
	{	"config-l@",		pfc_config_fetch},
	{	"config-w@",		pfc_config_fetch},
	{	"config-b@",		pfc_config_fetch},
	{	"config-l!",		pfc_config_store},
	{	"config-w!",		pfc_config_store},
	{	"config-b!",		pfc_config_store},
	{	FC_PROBE_ADDRESS,	pfc_probe_address},
	{	FC_PROBE_SPACE,		pfc_probe_space},
	{	FC_SVC_EXIT,		pfc_dma_cleanup},
	{	FC_CONFIG_CHILD,	pfc_config_child},
	{	FC_GET_FCODE_SIZE,	pfc_get_fcode_size},
	{	FC_GET_FCODE,		pfc_get_fcode},
	{	NULL,			NULL}
};

static struct pfc_ops_v shared_pov[] = {
	{	FC_SVC_EXIT,		pfc_dma_cleanup},
	{	NULL,			NULL}
};

int pci_map_phys(dev_info_t *, pci_regspec_t *,
    caddr_t *, ddi_device_acc_attr_t *, ddi_acc_handle_t *);

void pci_unmap_phys(ddi_acc_handle_t *, pci_regspec_t *);

fco_handle_t
pci_fc_ops_alloc_handle(dev_info_t *ap, dev_info_t *child,
    void *fcode, size_t fcode_size, char *unit_address,
    struct pci_ops_bus_args *up)
{
	fco_handle_t rp;
	struct pci_ops_bus_args *bp = NULL;
	phandle_t h;

	rp = kmem_zalloc(sizeof (struct fc_resource_list), KM_SLEEP);
	rp->next_handle = fc_ops_alloc_handle(ap, child, fcode, fcode_size,
	    unit_address, NULL);
	rp->ap = ap;
	rp->child = child;
	rp->fcode = fcode;
	rp->fcode_size = fcode_size;
	if (unit_address) {
		char *buf;

		buf = kmem_zalloc(strlen(unit_address) + 1, KM_SLEEP);
		(void) strcpy(buf, unit_address);
		rp->unit_address = buf;
	}

	bp = kmem_zalloc(sizeof (struct pci_ops_bus_args), KM_SLEEP);
	*bp = *up;
	rp->bus_args = bp;

	/*
	 * Add the child's nodeid to our table...
	 */
	h = ddi_get_nodeid(rp->child);
	fc_add_dip_to_phandle(fc_handle_to_phandle_head(rp), rp->child, h);

	return (rp);
}

void
pci_fc_ops_free_handle(fco_handle_t rp)
{
	struct pci_ops_bus_args *bp;
	struct fc_resource *ip, *np;

	ASSERT(rp);

	if (rp->next_handle)
		fc_ops_free_handle(rp->next_handle);
	if (rp->unit_address)
		kmem_free(rp->unit_address, strlen(rp->unit_address) + 1);
	if ((bp = rp->bus_args) != NULL)
		kmem_free(bp, sizeof (struct pci_ops_bus_args));

	/*
	 * Release all the resources from the resource list
	 * XXX: We don't handle 'unknown' types, but we don't create them.
	 */
	for (ip = rp->head; ip != NULL; ip = np) {
		np = ip->next;
		switch (ip->type) {
		case RT_MAP:
			FC_DEBUG1(1, CE_CONT, "pci_fc_ops_free: "
			    "pci_unmap_phys(%p)\n", ip->fc_map_handle);
			pci_unmap_phys(&ip->fc_map_handle, ip->fc_regspec);
			kmem_free(ip->fc_regspec, sizeof (pci_regspec_t));
			break;
		case RT_DMA:
			/* DMA has to be freed up at exit time */
			cmn_err(CE_CONT, "pfc_fc_ops_free: DMA seen!\n");
			break;
		default:
			cmn_err(CE_CONT, "pci_fc_ops_free: "
			    "unknown resource type %d\n", ip->type);
			break;
		}
		fc_rem_resource(rp, ip);
		kmem_free(ip, sizeof (struct fc_resource));
	}
	kmem_free(rp, sizeof (struct fc_resource_list));
}

int
pci_fc_ops(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	struct pfc_ops_v *pv;
	char *name = fc_cell2ptr(cp->svc_name);

	ASSERT(rp);

	/*
	 * First try the generic fc_ops. If the ops is a shared op,
	 * also call our local function.
	 */
	if (fc_ops(ap, rp->next_handle, cp) == 0) {
		for (pv = shared_pov; pv->svc_name != NULL; ++pv)
			if (strcmp(pv->svc_name, name) == 0)
				return (pv->f(ap, rp, cp));
		return (0);
	}

	for (pv = pov; pv->svc_name != NULL; ++pv)
		if (strcmp(pv->svc_name, name) == 0)
			return (pv->f(ap, rp, cp));

	FC_DEBUG1(9, CE_CONT, "pci_fc_ops: <%s> not serviced\n", name);

	return (-1);
}

/*
 * Create a dma mapping for a given user address.
 */
static int
pfc_dma_map_in(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	ddi_dma_handle_t h;
	int error;
	caddr_t virt;
	size_t len;
	uint_t flags = DDI_DMA_RDWR | DDI_DMA_CONSISTENT;
	struct fc_resource *ip;
	ddi_dma_cookie_t c;
	struct buf *bp;
	uint_t ccnt;

	if (fc_cell2int(cp->nargs) != 3)
		return (fc_syntax_error(cp, "nargs must be 3"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	/*
	 * XXX: It's not clear what we should do with a non-cacheable request
	 */
	virt = fc_cell2ptr(fc_arg(cp, 2));
	len = fc_cell2size(fc_arg(cp, 1));
#ifdef	notdef
	cacheable = fc_cell2int(fc_arg(cp, 0));	/* XXX: do what? */
#endif

	FC_DEBUG2(6, CE_CONT, "pcf_dma_map_in: virt %p, len %d\n", virt, len);

	/*
	 * Set up the address space for physio from userland
	 */
	error = fc_physio_setup(&bp, virt, len);

	if (error)  {
		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: fc_physio_setup failed "
		    "error: %d  virt: %p  len %d\n", error, virt, len);
		return (fc_priv_error(cp, "fc_physio_setup failed"));
	}

	FC_DEBUG1(9, CE_CONT, "pfc_dma_map_in: dma_map_in; bp = %p\n", bp);
	error = fc_ddi_dma_alloc_handle(ap, &fcpci_dma_attr, DDI_DMA_SLEEP,
	    NULL, &h);
	if (error != DDI_SUCCESS)  {
		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: real dma-map-in failed "
		    "error: %d  virt: %p  len %d\n", error, virt, len);
		return (fc_priv_error(cp, "real dma-map-in failed"));
	}

	error = fc_ddi_dma_buf_bind_handle(h, bp, flags, DDI_DMA_SLEEP, NULL,
	    &c, &ccnt);
	if ((error != DDI_DMA_MAPPED) || (ccnt != 1)) {
		fc_ddi_dma_free_handle(&h);
		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: real dma-map-in failed "
		    "error: %d  virt: %p  len %d\n", error, virt, len);
		return (fc_priv_error(cp, "real dma-map-in failed"));
	}

	if (c.dmac_size < len)  {
		error = fc_ddi_dma_unbind_handle(h);
		if (error != DDI_SUCCESS) {
			return (fc_priv_error(cp, "ddi_dma_unbind error"));
		}
		fc_ddi_dma_free_handle(&h);
		return (fc_priv_error(cp, "ddi_dma_buf_bind size < len"));
	}

	FC_DEBUG1(9, CE_CONT, "pfc_dma_map_in: returning devaddr %x\n",
	    c.dmac_address);

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = fc_uint32_t2cell(c.dmac_address);	/* XXX size */

	/*
	 * Now we have to log this resource saving the handle and buf header
	 */
	ip = kmem_zalloc(sizeof (struct fc_resource), KM_SLEEP);
	ip->type = RT_DMA;
	ip->fc_dma_virt = virt;
	ip->fc_dma_len = len;
	ip->fc_dma_handle = h;
	ip->fc_dma_devaddr = c.dmac_address;
	ip->fc_dma_bp = bp;
	fc_add_resource(rp, ip);

	return (fc_success_op(ap, rp, cp));
}

static int
pfc_dma_sync(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	void *virt;
	size_t len;
	uint32_t devaddr;
	int error;
	struct fc_resource *ip;

	if (fc_cell2int(cp->nargs) != 3)
		return (fc_syntax_error(cp, "nargs must be 3"));

	virt = fc_cell2ptr(fc_arg(cp, 2));
	devaddr = fc_cell2uint32_t(fc_arg(cp, 1));
	len = fc_cell2size(fc_arg(cp, 0));

	/*
	 * Find if this virt is 'within' a request we know about
	 */
	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next) {
		if (ip->type != RT_DMA)
			continue;
		if (ip->fc_dma_devaddr != devaddr)
			continue;
		if (((char *)virt >= (char *)ip->fc_dma_virt) &&
		    (((char *)virt + len) <=
		    ((char *)ip->fc_dma_virt + ip->fc_dma_len)))
			break;
	}
	fc_unlock_resource_list(rp);

	if (ip == NULL)
		return (fc_priv_error(cp, "request not within a "
		    "known dma mapping"));

	/*
	 * We know about this request, so we trust it enough to sync it.
	 * Unfortunately, we don't know which direction, so we'll do
	 * both directions.
	 */

	error = fc_ddi_dma_sync(ip->fc_dma_handle,
	    (char *)virt - (char *)ip->fc_dma_virt, len, DDI_DMA_SYNC_FORCPU);
	error |= fc_ddi_dma_sync(ip->fc_dma_handle,
	    (char *)virt - (char *)ip->fc_dma_virt, len, DDI_DMA_SYNC_FORDEV);

	if (error)
		return (fc_priv_error(cp, "Call to ddi_dma_sync failed"));

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}

static int
pfc_dma_map_out(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	void *virt;
	size_t len;
	uint32_t devaddr;
	struct fc_resource *ip;
	int e;

	if (fc_cell2int(cp->nargs) != 3)
		return (fc_syntax_error(cp, "nargs must be 3"));

	virt = fc_cell2ptr(fc_arg(cp, 2));
	devaddr = fc_cell2uint32_t(fc_arg(cp, 1));
	len = fc_cell2size(fc_arg(cp, 0));

	/*
	 * Find if this virt matches a request we know about
	 */
	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next) {
		if (ip->type != RT_DMA)
			continue;
		if (ip->fc_dma_devaddr != devaddr)
			continue;
		if (ip->fc_dma_virt != virt)
			continue;
		if (len == ip->fc_dma_len)
			break;
	}
	fc_unlock_resource_list(rp);

	if (ip == NULL)
		return (fc_priv_error(cp, "request doesn't match a "
		    "known dma mapping"));

	/*
	 * ddi_dma_unbind_handle does an implied sync ...
	 */
	e = fc_ddi_dma_unbind_handle(ip->fc_dma_handle);
	if (e != DDI_SUCCESS) {
		cmn_err(CE_CONT, "pfc_dma_map_out: ddi_dma_unbind failed!\n");
	}
	fc_ddi_dma_free_handle(&ip->fc_dma_handle);

	/*
	 * Tear down the physio mappings
	 */
	fc_physio_free(&ip->fc_dma_bp, ip->fc_dma_virt, ip->fc_dma_len);

	/*
	 * remove the resource from the list and release it.
	 */
	fc_rem_resource(rp, ip);
	kmem_free(ip, sizeof (struct fc_resource));

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}

static struct fc_resource *
next_dma_resource(fco_handle_t rp)
{
	struct fc_resource *ip;

	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next)
		if (ip->type == RT_DMA)
			break;
	fc_unlock_resource_list(rp);

	return (ip);
}

static int
pfc_dma_cleanup(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	struct fc_resource *ip;
	int e;

	while ((ip = next_dma_resource(rp)) != NULL) {

		FC_DEBUG2(9, CE_CONT, "pfc_dma_cleanup: virt %x len %x\n",
		    ip->fc_dma_virt, ip->fc_dma_len);

		/*
		 * Free the dma handle
		 */
		e = fc_ddi_dma_unbind_handle(ip->fc_dma_handle);
		if (e != DDI_SUCCESS) {
			cmn_err(CE_CONT, "pfc_dma_cleanup: "
			    "ddi_dma_unbind failed!\n");
		}
		fc_ddi_dma_free_handle(&ip->fc_dma_handle);

		/*
		 * Tear down the userland mapping and free the buf header
		 */
		fc_physio_free(&ip->fc_dma_bp, ip->fc_dma_virt, ip->fc_dma_len);

		fc_rem_resource(rp, ip);
		kmem_free(ip, sizeof (struct fc_resource));
	}

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}

static int
pfc_map_in(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	size_t len;
	int error;
	caddr_t virt;
	pci_regspec_t p, *ph;
	struct fc_resource *ip;
	ddi_device_acc_attr_t acc;
	ddi_acc_handle_t h;

	if (fc_cell2int(cp->nargs) != 4)
		return (fc_syntax_error(cp, "nargs must be 4"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	p.pci_size_hi = 0;
	p.pci_size_low = len = fc_cell2size(fc_arg(cp, 0));

	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 1));
	p.pci_phys_mid = fc_cell2uint(fc_arg(cp, 2));
	p.pci_phys_low = fc_cell2uint(fc_arg(cp, 3));

	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;

	/*
	 * Fcode is expecting the bytes are not swapped.
	 */
	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	/*
	 * First We need to allocate the PCI Resource.
	 */
	error = pci_alloc_resource(rp->child, p);

	if (error)  {
		return (fc_priv_error(cp, "pci map-in failed"));
	}

	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);

	if (error)  {
		return (fc_priv_error(cp, "pci map-in failed"));
	}

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = fc_ptr2cell(virt);

	/*
	 * Log this resource ...
	 */
	ip = kmem_zalloc(sizeof (struct fc_resource), KM_SLEEP);
	ip->type = RT_MAP;
	ip->fc_map_virt = virt;
	ip->fc_map_len = len;
	ip->fc_map_handle = h;
	ph = kmem_zalloc(sizeof (pci_regspec_t), KM_SLEEP);
	*ph = p;
	ip->fc_regspec = ph;	/* cache a copy of the reg spec */
	fc_add_resource(rp, ip);

	return (fc_success_op(ap, rp, cp));
}

static int
pfc_map_out(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	caddr_t virt;
	size_t len;
	struct fc_resource *ip;

	if (fc_cell2int(cp->nargs) != 2)
		return (fc_syntax_error(cp, "nargs must be 2"));

	virt = fc_cell2ptr(fc_arg(cp, 1));

	len = fc_cell2size(fc_arg(cp, 0));

	/*
	 * Find if this request matches a mapping resource we set up.
	 */
	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next) {
		if (ip->type != RT_MAP)
			continue;
		if (ip->fc_map_virt != virt)
			continue;
		if (ip->fc_map_len == len)
			break;
	}
	fc_unlock_resource_list(rp);

	if (ip == NULL)
		return (fc_priv_error(cp, "request doesn't match a "
		    "known mapping"));

	pci_unmap_phys(&ip->fc_map_handle, ip->fc_regspec);

	kmem_free(ip->fc_regspec, sizeof (pci_regspec_t));

	/*
	 * remove the resource from the list and release it.
	 */
	fc_rem_resource(rp, ip);
	kmem_free(ip, sizeof (struct fc_resource));

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}

static int
pfc_register_fetch(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	size_t len;
	caddr_t virt;
	int error;
	uint64_t x;
	uint32_t l;
	uint16_t w;
	uint8_t b;
	char *name = fc_cell2ptr(cp->svc_name);
	struct fc_resource *ip;

	if (fc_cell2int(cp->nargs) != 1)
		return (fc_syntax_error(cp, "nargs must be 1"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	virt = fc_cell2ptr(fc_arg(cp, 0));

	/*
	 * Determine the access width .. we can switch on the 2nd
	 * character of the name which is "rx@", "rl@", "rb@" or "rw@"
	 */
	switch (*(name + 1)) {
	case 'x':	len = sizeof (x); break;
	case 'l':	len = sizeof (l); break;
	case 'w':	len = sizeof (w); break;
	case 'b':	len = sizeof (b); break;
	}

	/*
	 * Check the alignment ...
	 */
	if (((intptr_t)virt & (len - 1)) != 0)
		return (fc_priv_error(cp, "unaligned access"));

	/*
	 * Find if this virt is 'within' a request we know about
	 */
	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next) {
		if (ip->type != RT_MAP)
			continue;
		if ((virt >= (caddr_t)ip->fc_map_virt) && ((virt + len) <=
		    ((caddr_t)ip->fc_map_virt + ip->fc_map_len)))
			break;
	}
	fc_unlock_resource_list(rp);

	if (ip == NULL)
		return (fc_priv_error(cp, "request not within a "
		    "known mapping"));

	/*
	 * XXX: We need access handle versions of peek/poke to move
	 * beyond the prototype ... we assume that we have hardware
	 * byte swapping enabled for pci register access here which
	 * is a huge dependency on the current implementation.
	 */
	switch (len) {
	case sizeof (x):
		error = ddi_peek64(rp->child, (int64_t *)virt, (int64_t *)&x);
		break;
	case sizeof (l):
		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&l);
		break;
	case sizeof (w):
		error = ddi_peek16(rp->child, (int16_t *)virt, (int16_t *)&w);
		break;
	case sizeof (b):
		error = ddi_peek8(rp->child, (int8_t *)virt, (int8_t *)&b);
		break;
	}

	if (error) {
		return (fc_priv_error(cp, "access error"));
	}

	cp->nresults = fc_int2cell(1);
	switch (len) {
	case sizeof (x): fc_result(cp, 0) = x; break;
	case sizeof (l): fc_result(cp, 0) = fc_uint32_t2cell(l); break;
	case sizeof (w): fc_result(cp, 0) = fc_uint16_t2cell(w); break;
	case sizeof (b): fc_result(cp, 0) = fc_uint8_t2cell(b); break;
	}
	return (fc_success_op(ap, rp, cp));
}

static int
pfc_register_store(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	size_t len;
	caddr_t virt;
	int error;
	uint64_t x;
	uint32_t l;
	uint16_t w;
	uint8_t b;
	char *name = fc_cell2ptr(cp->svc_name);
	struct fc_resource *ip;

	if (fc_cell2int(cp->nargs) != 2)
		return (fc_syntax_error(cp, "nargs must be 2"));

	virt = fc_cell2ptr(fc_arg(cp, 0));

	/*
	 * Determine the access width .. we can switch on the 2nd
	 * character of the name which is "rl!", "rb!" or "rw!"
	 */
	switch (*(name + 1)) {
	case 'x': len = sizeof (x); x = fc_arg(cp, 1); break;
	case 'l': len = sizeof (l); l = fc_cell2uint32_t(fc_arg(cp, 1)); break;
	case 'w': len = sizeof (w); w = fc_cell2uint16_t(fc_arg(cp, 1)); break;
	case 'b': len = sizeof (b); b = fc_cell2uint8_t(fc_arg(cp, 1)); break;
	}

	/*
	 * Check the alignment ...
	 */
	if (((intptr_t)virt & (len - 1)) != 0)
		return (fc_priv_error(cp, "unaligned access"));

	/*
	 * Find if this virt is 'within' a request we know about
	 */
	fc_lock_resource_list(rp);
	for (ip = rp->head; ip != NULL; ip = ip->next) {
		if (ip->type != RT_MAP)
			continue;
		if ((virt >= (caddr_t)ip->fc_map_virt) && ((virt + len) <=
		    ((caddr_t)ip->fc_map_virt + ip->fc_map_len)))
			break;
	}
	fc_unlock_resource_list(rp);

	if (ip == NULL)
		return (fc_priv_error(cp, "request not within a "
		    "known mapping"));

	/*
	 * XXX: We need access handle versions of peek/poke to move
	 * beyond the prototype ... we assume that we have hardware
	 * byte swapping enabled for pci register access here which
	 * is a huge dependency on the current implementation.
	 */
	switch (len) {
	case sizeof (x):
		error = ddi_poke64(rp->child, (int64_t *)virt, x);
		break;
	case sizeof (l):
		error = ddi_poke32(rp->child, (int32_t *)virt, l);
		break;
	case sizeof (w):
		error = ddi_poke16(rp->child, (int16_t *)virt, w);
		break;
	case sizeof (b):
		error = ddi_poke8(rp->child, (int8_t *)virt, b);
		break;
	}

	if (error) {
		return (fc_priv_error(cp, "access error"));
	}

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}

static int
pfc_config_fetch(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	caddr_t virt, v;
	int error, reg, flags = 0;
	size_t len;
	uint32_t l, tmp;
	uint16_t w;
	uint8_t b;
	char *name = fc_cell2ptr(cp->svc_name);
	pci_regspec_t p;
	ddi_device_acc_attr_t acc;
	ddi_acc_handle_t h;

	if (fc_cell2int(cp->nargs) != 1)
		return (fc_syntax_error(cp, "nargs must be 1"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	/*
	 * Construct a config address pci reg property from the args.
	 * arg[0] is the configuration address.
	 */
	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 0));
	p.pci_phys_mid = p.pci_phys_low = 0;
	p.pci_size_hi = p.pci_size_low = 0;

	/*
	 * Verify that the address is a configuration space address
	 * ss must be zero.
	 */
	if ((p.pci_phys_hi & PCI_ADDR_MASK) != PCI_ADDR_CONFIG) {
		cmn_err(CE_CONT, "pfc_config_fetch: "
		    "invalid config addr: %x\n", p.pci_phys_hi);
		return (fc_priv_error(cp, "non-config addr"));
	}

	/*
	 * Extract the register number from the config address and
	 * remove the register number from the physical address.
	 */

	reg = (p.pci_phys_hi & PCI_REG_REG_M) |
	    (((p.pci_phys_hi & PCI_REG_EXTREG_M) >> PCI_REG_EXTREG_SHIFT) << 8);

	p.pci_phys_hi &= PCI_BDF_bits;

	/*
	 * Determine the access width .. we can switch on the 9th
	 * character of the name which is "config-{l,w,b}@"
	 */
	switch (*(name + 7)) {
	case 'l':	len = sizeof (l); break;
	case 'w':	len = sizeof (w); break;
	case 'b':	len = sizeof (b); break;
	}

	/*
	 * Verify that the access is properly aligned
	 */
	if ((reg & (len - 1)) != 0)
		return (fc_priv_error(cp, "unaligned access"));

	/*
	 * Map in configuration space (temporarily)
	 */
	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);

	if (error)  {
		return (fc_priv_error(cp, "pci config map-in failed"));
	}

	if (fcpci_indirect_map(rp->child) == DDI_SUCCESS)
		flags |= PCICFG_CONF_INDIRECT_MAP;

	if (flags & PCICFG_CONF_INDIRECT_MAP) {
		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
		error = DDI_SUCCESS;
	} else
		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&tmp);

	if (error == DDI_SUCCESS)
		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
			error = DDI_FAILURE;
			cmn_err(CE_CONT, "fcpcii: conf probe failed.l=%x", tmp);
		}

	if (error != DDI_SUCCESS) {
		return (fc_priv_error(cp, "pci config fetch failed"));
	}


	/*
	 * XXX: We need access handle versions of peek/poke to move
	 * beyond the prototype ... we assume that we have hardware
	 * byte swapping enabled for pci register access here which
	 * is a huge dependency on the current implementation.
	 */
	v = virt + reg;
	switch (len) {
	case sizeof (l):
		l = (int32_t)ddi_get32(h, (uint32_t *)v);
		break;
	case sizeof (w):
		w = (int16_t)ddi_get16(h, (uint16_t *)v);
		break;
	case sizeof (b):
		b = (int8_t)ddi_get8(h, (uint8_t *)v);
		break;
	}

	/*
	 * Remove the temporary config space mapping
	 */
	pci_unmap_phys(&h, &p);

	if (error) {
		return (fc_priv_error(cp, "access error"));
	}

	cp->nresults = fc_int2cell(1);
	switch (len) {
	case sizeof (l): fc_result(cp, 0) = fc_uint32_t2cell(l); break;
	case sizeof (w): fc_result(cp, 0) = fc_uint16_t2cell(w); break;
	case sizeof (b): fc_result(cp, 0) = fc_uint8_t2cell(b); break;
	}

	return (fc_success_op(ap, rp, cp));
}

static int
pfc_config_store(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	caddr_t virt, v;
	int error, reg, flags = 0;
	size_t len;
	uint32_t l, tmp;
	uint16_t w;
	uint8_t b;
	char *name = fc_cell2ptr(cp->svc_name);
	pci_regspec_t p;
	ddi_device_acc_attr_t acc;
	ddi_acc_handle_t h;

	if (fc_cell2int(cp->nargs) != 2)
		return (fc_syntax_error(cp, "nargs must be 2"));

	/*
	 * Construct a config address pci reg property from the args.
	 * arg[0] is the configuration address. arg[1] is the data.
	 */
	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 0));
	p.pci_phys_mid = p.pci_phys_low = 0;
	p.pci_size_hi = p.pci_size_low = 0;

	/*
	 * Verify that the address is a configuration space address
	 * ss must be zero.
	 */
	if ((p.pci_phys_hi & PCI_ADDR_MASK) != PCI_ADDR_CONFIG) {
		cmn_err(CE_CONT, "pfc_config_store: "
		    "invalid config addr: %x\n", p.pci_phys_hi);
		return (fc_priv_error(cp, "non-config addr"));
	}

	/*
	 * Extract the register number from the config address and
	 * remove the register number from the physical address.
	 */
	reg = (p.pci_phys_hi & PCI_REG_REG_M) |
	    (((p.pci_phys_hi & PCI_REG_EXTREG_M) >> PCI_REG_EXTREG_SHIFT) << 8);

	p.pci_phys_hi &= PCI_BDF_bits;

	/*
	 * Determine the access width .. we can switch on the 8th
	 * character of the name which is "config-{l,w,b}@"
	 */
	switch (*(name + 7)) {
	case 'l': len = sizeof (l); l = fc_cell2uint32_t(fc_arg(cp, 1)); break;
	case 'w': len = sizeof (w); w = fc_cell2uint16_t(fc_arg(cp, 1)); break;
	case 'b': len = sizeof (b); b = fc_cell2uint8_t(fc_arg(cp, 1)); break;
	}

	/*
	 * Verify that the access is properly aligned
	 */
	if ((reg & (len - 1)) != 0)
		return (fc_priv_error(cp, "unaligned access"));

	/*
	 * Map in configuration space (temporarily)
	 */
	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);

	if (error)  {
		return (fc_priv_error(cp, "pci config map-in failed"));
	}

	if (fcpci_indirect_map(rp->child) == DDI_SUCCESS)
		flags |= PCICFG_CONF_INDIRECT_MAP;

	if (flags & PCICFG_CONF_INDIRECT_MAP) {
		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
		error = DDI_SUCCESS;
	} else
		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&tmp);

	if (error == DDI_SUCCESS)
		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
			error = DDI_FAILURE;
			cmn_err(CE_CONT, "fcpci: conf probe failed.l=%x", tmp);
		}

	if (error != DDI_SUCCESS) {
		return (fc_priv_error(cp, "pci config store failed"));
	}


	/*
	 * XXX: We need access handle versions of peek/poke to move
	 * beyond the prototype ... we assume that we have hardware
	 * byte swapping enabled for pci register access here which
	 * is a huge dependency on the current implementation.
	 */
	v = virt + reg;
	switch (len) {
	case sizeof (l):
		ddi_put32(h, (uint32_t *)v, (uint32_t)l);
		break;
	case sizeof (w):
		ddi_put16(h, (uint16_t *)v, (uint16_t)w);
		break;
	case sizeof (b):
		ddi_put8(h, (uint8_t *)v, (uint8_t)b);
		break;
	}

	/*
	 * Remove the temporary config space mapping
	 */
	pci_unmap_phys(&h, &p);

	if (error) {
		return (fc_priv_error(cp, "access error"));
	}

	cp->nresults = fc_int2cell(0);
	return (fc_success_op(ap, rp, cp));
}


static int
pfc_get_fcode(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	caddr_t name_virt, fcode_virt;
	char *name, *fcode;
	int fcode_len, status;

	if (fc_cell2int(cp->nargs) != 3)
		return (fc_syntax_error(cp, "nargs must be 3"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	name_virt = fc_cell2ptr(fc_arg(cp, 0));

	fcode_virt = fc_cell2ptr(fc_arg(cp, 1));

	fcode_len = fc_cell2int(fc_arg(cp, 2));

	name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);

	if (copyinstr(fc_cell2ptr(name_virt), name,
	    FC_SVC_NAME_LEN - 1, NULL))  {
		status = 0;
	} else {

		fcode = kmem_zalloc(fcode_len, KM_SLEEP);

		if ((status = prom_get_fcode(name, fcode)) != 0) {

			if (copyout((void *)fcode, (void *)fcode_virt,
			    fcode_len)) {
				cmn_err(CE_WARN, " pfc_get_fcode: Unable "
				    "to copy out fcode image\n");
				status = 0;
			}
		}

		kmem_free(fcode, fcode_len);
	}

	kmem_free(name, FC_SVC_NAME_LEN);

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = status;

	return (fc_success_op(ap, rp, cp));
}

static int
pfc_get_fcode_size(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	caddr_t virt;
	char *name;
	int len;

	if (fc_cell2int(cp->nargs) != 1)
		return (fc_syntax_error(cp, "nargs must be 1"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	virt = fc_cell2ptr(fc_arg(cp, 0));

	name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);

	if (copyinstr(fc_cell2ptr(virt), name,
	    FC_SVC_NAME_LEN - 1, NULL))  {
		len = 0;
	} else {
		len = prom_get_fcode_size(name);
	}

	kmem_free(name, FC_SVC_NAME_LEN);

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = len;

	return (fc_success_op(ap, rp, cp));
}

/*
 * Return the physical probe address: lo=0, mid=0, hi-config-addr
 */
static int
pfc_probe_address(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	if (fc_cell2int(cp->nargs) != 0)
		return (fc_syntax_error(cp, "nargs must be 0"));

	if (fc_cell2int(cp->nresults) < 2)
		return (fc_syntax_error(cp, "nresults must be >= 3"));

	cp->nresults = fc_int2cell(2);
	fc_result(cp, 1) = fc_int2cell(0);	/* phys.lo */
	fc_result(cp, 0) = fc_int2cell(0);	/* phys.mid */

	return (fc_success_op(ap, rp, cp));
}

/*
 * Return the phys.hi component of the probe address.
 */
static int
pfc_probe_space(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	struct pci_ops_bus_args *ba = rp->bus_args;

	ASSERT(ba);

	if (fc_cell2int(cp->nargs) != 0)
		return (fc_syntax_error(cp, "nargs must be 0"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = fc_uint32_t2cell(ba->config_address); /* phys.hi */

	return (fc_success_op(ap, rp, cp));
}

static int
pfc_config_child(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
	fc_phandle_t h;

	if (fc_cell2int(cp->nargs) != 0)
		return (fc_syntax_error(cp, "nargs must be 0"));

	if (fc_cell2int(cp->nresults) < 1)
		return (fc_syntax_error(cp, "nresults must be >= 1"));

	h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), rp->child);

	cp->nresults = fc_int2cell(1);
	fc_result(cp, 0) = fc_phandle2cell(h);

	return (fc_success_op(ap, rp, cp));
}

int
pci_alloc_mem_chunk(dev_info_t *dip, uint64_t mem_align, uint64_t *mem_size,
    uint64_t *mem_answer)
{
	ndi_ra_request_t req;
	int rval;

	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
	req.ra_flags = NDI_RA_ALLOC_BOUNDED;
	req.ra_boundbase = 0;
	req.ra_boundlen = PCI_4GIG_LIMIT;
	req.ra_len = *mem_size;
	req.ra_align_mask = mem_align - 1;

	rval = ndi_ra_alloc(dip, &req, mem_answer, mem_size,
	    NDI_RA_TYPE_MEM, NDI_RA_PASS);

	return (rval);
}
int
pci_alloc_io_chunk(dev_info_t *dip, uint64_t io_align, uint64_t *io_size,
    uint64_t *io_answer)
{
	ndi_ra_request_t req;
	int rval;

	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
	req.ra_flags = (NDI_RA_ALLOC_BOUNDED | NDI_RA_ALLOC_PARTIAL_OK);
	req.ra_boundbase = 0;
	req.ra_boundlen = PCI_4GIG_LIMIT;
	req.ra_len = *io_size;
	req.ra_align_mask = io_align - 1;

	rval = ndi_ra_alloc(dip, &req, io_answer, io_size,
	    NDI_RA_TYPE_IO, NDI_RA_PASS);

	return (rval);
}

int
pci_alloc_resource(dev_info_t *dip, pci_regspec_t phys_spec)
{
	uint64_t answer;
	uint64_t alen;
	int offset, tmp;
	pci_regspec_t config;
	caddr_t virt, v;
	ddi_device_acc_attr_t acc;
	ddi_acc_handle_t h;
	ndi_ra_request_t request;
	pci_regspec_t *assigned;
	int assigned_len, entries, i, l, flags = 0, error;

	l = phys_spec.pci_size_low;

	if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "assigned-addresses", (caddr_t)&assigned,
	    &assigned_len) == DDI_PROP_SUCCESS) {

		entries = assigned_len / (sizeof (pci_regspec_t));

		/*
		 * Walk through the assigned-addresses entries. If there is
		 * a match, there is no need to allocate the resource.
		 */
		for (i = 0; i < entries; i++) {
			if (assigned[i].pci_phys_hi == phys_spec.pci_phys_hi) {
				if (assigned[i].pci_size_low >=
				    phys_spec.pci_size_low) {
					kmem_free(assigned, assigned_len);
					return (0);
				}
				/*
				 * Fcode wants to assign more than what
				 * probe found.
				 */
				(void) pci_free_resource(dip, assigned[i]);
				/*
				 * Go on to allocate resources.
				 */
				break;
			}
			/*
			 * Check if Fcode wants to map using different
			 * NPT bits.
			 */
			if (PCI_REG_BDFR_G(assigned[i].pci_phys_hi) ==
			    PCI_REG_BDFR_G(phys_spec.pci_phys_hi)) {
				/*
				 * It is an error to change SS bits
				 */
				if (PCI_REG_ADDR_G(assigned[i].pci_phys_hi) !=
				    PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {

					FC_DEBUG2(2, CE_WARN, "Fcode changing "
					    "ss bits in reg %x -- %x",
					    assigned[i].pci_phys_hi,
					    phys_spec.pci_phys_hi);
				}

				/*
				 * Allocate enough
				 */
				l = MAX(assigned[i].pci_size_low,
				    phys_spec.pci_size_low);

				phys_spec.pci_size_low = l;

				(void) pci_free_resource(dip, assigned[i]);
				/*
				 * Go on to allocate resources.
				 */
				break;
			}
		}
		kmem_free(assigned, assigned_len);
	}

	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));

	config.pci_phys_hi = PCI_CONF_ADDR_MASK & phys_spec.pci_phys_hi;
	config.pci_phys_hi &= ~PCI_REG_REG_M;
	config.pci_phys_mid = config.pci_phys_low = 0;
	config.pci_size_hi = config.pci_size_low = 0;

	/*
	 * Map in configuration space (temporarily)
	 */
	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	if (error = pci_map_phys(dip, &config, &virt, &acc, &h)) {
		return (1);
	}

	if (fcpci_indirect_map(dip) == DDI_SUCCESS)
		flags |= PCICFG_CONF_INDIRECT_MAP;

	if (flags & PCICFG_CONF_INDIRECT_MAP) {
		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
		error = DDI_SUCCESS;
	} else
		error = ddi_peek32(dip, (int32_t *)virt, (int32_t *)&tmp);

	if (error == DDI_SUCCESS)
		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
			error = DDI_FAILURE;
		}

	if (error != DDI_SUCCESS) {
		return (1);
	}

	request.ra_flags |= NDI_RA_ALIGN_SIZE;
	request.ra_boundbase = 0;
	request.ra_boundlen = PCI_4GIG_LIMIT;

	offset = PCI_REG_REG_G(phys_spec.pci_phys_hi);

	v = virt + offset;

	if (PCI_REG_REG_G(phys_spec.pci_phys_hi) == PCI_CONF_ROM) {
		request.ra_len = l;
		request.ra_flags ^= NDI_RA_ALLOC_BOUNDED;

		/* allocate memory space from the allocator */

		if (ndi_ra_alloc(ddi_get_parent(dip),
		    &request, &answer, &alen, NDI_RA_TYPE_MEM,
		    NDI_RA_PASS) != NDI_SUCCESS) {
			pci_unmap_phys(&h, &config);
			return (1);
		}
		FC_DEBUG3(1, CE_CONT, "ROM addr = [0x%x.%x] len [0x%x]\n",
		    HIADDR(answer), LOADDR(answer), alen);

		/* program the low word */

		ddi_put32(h, (uint32_t *)v, LOADDR(answer));

		phys_spec.pci_phys_low = LOADDR(answer);
		phys_spec.pci_phys_mid = HIADDR(answer);
	} else {
		request.ra_len = l;

		switch (PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {
		case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
			request.ra_flags ^= NDI_RA_ALLOC_BOUNDED;

			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
				/*
				 * If it is a non relocatable address,
				 * then specify the address we want.
				 */
				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
				request.ra_addr = (uint64_t)LADDR(
				    phys_spec.pci_phys_low,
				    phys_spec.pci_phys_mid);
			}

			/* allocate memory space from the allocator */

			if (ndi_ra_alloc(ddi_get_parent(dip),
			    &request, &answer, &alen, NDI_RA_TYPE_MEM,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				if (request.ra_flags == NDI_RA_ALLOC_SPECIFIED)
					cmn_err(CE_WARN, "Unable to allocate "
					    "non relocatable address 0x%p\n",
					    (void *) request.ra_addr);
				return (1);
			}
			FC_DEBUG3(1, CE_CONT,
			    "64 addr = [0x%x.%x] len [0x%x]\n",
			    HIADDR(answer),
			    LOADDR(answer),
			    alen);

			/* program the low word */

			ddi_put32(h, (uint32_t *)v, LOADDR(answer));

			/* program the high word with value zero */
			v += 4;
			ddi_put32(h, (uint32_t *)v, HIADDR(answer));

			phys_spec.pci_phys_low = LOADDR(answer);
			phys_spec.pci_phys_mid = HIADDR(answer);
			/*
			 * currently support 32b address space
			 * assignments only.
			 */
			phys_spec.pci_phys_hi ^= PCI_ADDR_MEM64 ^
			    PCI_ADDR_MEM32;

			break;

		case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
			request.ra_flags |= NDI_RA_ALLOC_BOUNDED;

			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
				/*
				 * If it is a non relocatable address,
				 * then specify the address we want.
				 */
				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
				request.ra_addr = (uint64_t)
				    phys_spec.pci_phys_low;
			}

			/* allocate memory space from the allocator */

			if (ndi_ra_alloc(ddi_get_parent(dip),
			    &request, &answer, &alen, NDI_RA_TYPE_MEM,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				if (request.ra_flags == NDI_RA_ALLOC_SPECIFIED)
					cmn_err(CE_WARN, "Unable to allocate "
					    "non relocatable address 0x%p\n",
					    (void *) request.ra_addr);
				return (1);
			}

			FC_DEBUG3(1, CE_CONT,
			    "32 addr = [0x%x.%x] len [0x%x]\n",
			    HIADDR(answer),
			    LOADDR(answer),
			    alen);

			/* program the low word */

			ddi_put32(h, (uint32_t *)v, LOADDR(answer));

			phys_spec.pci_phys_low = LOADDR(answer);

			break;
		case PCI_REG_ADDR_G(PCI_ADDR_IO):
			request.ra_flags |= NDI_RA_ALLOC_BOUNDED;

			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
				/*
				 * If it is a non relocatable address,
				 * then specify the address we want.
				 */
				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
				request.ra_addr = (uint64_t)
				    phys_spec.pci_phys_low;
			}

			/* allocate I/O space from the allocator */

			if (ndi_ra_alloc(ddi_get_parent(dip),
			    &request, &answer, &alen, NDI_RA_TYPE_IO,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				if (request.ra_flags ==
				    NDI_RA_ALLOC_SPECIFIED)
					cmn_err(CE_WARN, "Unable to allocate "
					    "non relocatable IO Space 0x%p\n",
					    (void *) request.ra_addr);
				return (1);
			}
			FC_DEBUG3(1, CE_CONT,
			    "I/O addr = [0x%x.%x] len [0x%x]\n",
			    HIADDR(answer),
			    LOADDR(answer),
			    alen);

			ddi_put32(h, (uint32_t *)v, LOADDR(answer));

			phys_spec.pci_phys_low = LOADDR(answer);

			break;
		default:
			pci_unmap_phys(&h, &config);
			return (1);
		} /* switch */
	}

	/*
	 * Now that memory locations are assigned,
	 * update the assigned address property.
	 */
	if (pfc_update_assigned_prop(dip, &phys_spec)) {
		pci_unmap_phys(&h, &config);
		return (1);
	}

	pci_unmap_phys(&h, &config);

	return (0);
}

int
pci_free_resource(dev_info_t *dip, pci_regspec_t phys_spec)
{
	int offset, tmp;
	pci_regspec_t config;
	caddr_t virt, v;
	ddi_device_acc_attr_t acc;
	ddi_acc_handle_t h;
	ndi_ra_request_t request;
	int l, error, flags = 0;

	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));

	config.pci_phys_hi = PCI_CONF_ADDR_MASK & phys_spec.pci_phys_hi;
	config.pci_phys_hi &= ~PCI_REG_REG_M;
	config.pci_phys_mid = config.pci_phys_low = 0;
	config.pci_size_hi = config.pci_size_low = 0;

	/*
	 * Map in configuration space (temporarily)
	 */
	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	if (error = pci_map_phys(dip, &config, &virt, &acc, &h)) {
		return (1);
	}
	if (fcpci_indirect_map(dip) == DDI_SUCCESS)
		flags |= PCICFG_CONF_INDIRECT_MAP;

	if (flags & PCICFG_CONF_INDIRECT_MAP) {
		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
		error = DDI_SUCCESS;
	} else
		error = ddi_peek32(dip, (int32_t *)virt, (int32_t *)&tmp);

	if (error == DDI_SUCCESS)
		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
			error = DDI_FAILURE;
		}
	if (error != DDI_SUCCESS) {
		return (1);
	}


	offset = PCI_REG_REG_G(phys_spec.pci_phys_hi);

	v = virt + offset;

	/*
	 * Pick up the size to be freed. It may be different from
	 * what probe finds.
	 */
	l = phys_spec.pci_size_low;

	if (PCI_REG_REG_G(phys_spec.pci_phys_hi) == PCI_CONF_ROM) {
		/* free memory back to the allocator */
		if (ndi_ra_free(ddi_get_parent(dip), phys_spec.pci_phys_low,
		    l, NDI_RA_TYPE_MEM,
		    NDI_RA_PASS) != NDI_SUCCESS) {
			pci_unmap_phys(&h, &config);
			return (1);
		}

		/* Unmap the BAR by writing a zero */

		ddi_put32(h, (uint32_t *)v, 0);
	} else {
		switch (PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {
		case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
			/* free memory back to the allocator */
			if (ndi_ra_free(ddi_get_parent(dip),
			    LADDR(phys_spec.pci_phys_low,
			    phys_spec.pci_phys_mid),
			    l, NDI_RA_TYPE_MEM,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				return (1);
			}

			break;

		case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
			/* free memory back to the allocator */
			if (ndi_ra_free(ddi_get_parent(dip),
			    phys_spec.pci_phys_low,
			    l, NDI_RA_TYPE_MEM,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				return (1);
			}

			break;
		case PCI_REG_ADDR_G(PCI_ADDR_IO):
			/* free I/O space back to the allocator */
			if (ndi_ra_free(ddi_get_parent(dip),
			    phys_spec.pci_phys_low,
			    l, NDI_RA_TYPE_IO,
			    NDI_RA_PASS) != NDI_SUCCESS) {
				pci_unmap_phys(&h, &config);
				return (1);
			}
			break;
		default:
			pci_unmap_phys(&h, &config);
			return (1);
		} /* switch */
	}

	/*
	 * Now that memory locations are assigned,
	 * update the assigned address property.
	 */

	FC_DEBUG1(1, CE_CONT, "updating assigned-addresss for %x\n",
	    phys_spec.pci_phys_hi);

	if (pfc_remove_assigned_prop(dip, &phys_spec)) {
		pci_unmap_phys(&h, &config);
		return (1);
	}

	pci_unmap_phys(&h, &config);

	return (0);
}


int
pci_map_phys(dev_info_t *dip, pci_regspec_t *phys_spec,
	caddr_t *addrp, ddi_device_acc_attr_t *accattrp,
	ddi_acc_handle_t *handlep)
{
	ddi_map_req_t mr;
	ddi_acc_hdl_t *hp;
	int result;

	*handlep = impl_acc_hdl_alloc(KM_SLEEP, NULL);
	hp = impl_acc_hdl_get(*handlep);
	hp->ah_vers = VERS_ACCHDL;
	hp->ah_dip = dip;
	hp->ah_rnumber = 0;
	hp->ah_offset = 0;
	hp->ah_len = 0;
	hp->ah_acc = *accattrp;

	mr.map_op = DDI_MO_MAP_LOCKED;
	mr.map_type = DDI_MT_REGSPEC;
	mr.map_obj.rp = (struct regspec *)phys_spec;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = hp;
	mr.map_vers = DDI_MAP_VERSION;

	result = ddi_map(dip, &mr, 0, 0, addrp);

	if (result != DDI_SUCCESS) {
		impl_acc_hdl_free(*handlep);
		*handlep = (ddi_acc_handle_t)NULL;
	} else {
		hp->ah_addr = *addrp;
	}

	return (result);
}

void
pci_unmap_phys(ddi_acc_handle_t *handlep, pci_regspec_t *ph)
{
	ddi_map_req_t mr;
	ddi_acc_hdl_t *hp;

	hp = impl_acc_hdl_get(*handlep);
	ASSERT(hp);

	mr.map_op = DDI_MO_UNMAP;
	mr.map_type = DDI_MT_REGSPEC;
	mr.map_obj.rp = (struct regspec *)ph;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = hp;
	mr.map_vers = DDI_MAP_VERSION;

	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
	    hp->ah_len, &hp->ah_addr);

	impl_acc_hdl_free(*handlep);


	*handlep = (ddi_acc_handle_t)NULL;
}

int
pfc_update_assigned_prop(dev_info_t *dip, pci_regspec_t *newone)
{
	int		alen;
	pci_regspec_t	*assigned;
	caddr_t		newreg;
	uint_t		status;

	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "assigned-addresses", (caddr_t)&assigned, &alen);
	switch (status) {
		case DDI_PROP_SUCCESS:
		break;
		case DDI_PROP_NO_MEMORY:
			return (1);
		default:
			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
			    "assigned-addresses", (int *)newone,
			    sizeof (*newone)/sizeof (int));
			return (0);
	}

	/*
	 * Allocate memory for the existing
	 * assigned-addresses(s) plus one and then
	 * build it.
	 */

	newreg = kmem_zalloc(alen+sizeof (*newone), KM_SLEEP);

	bcopy(assigned, newreg, alen);
	bcopy(newone, newreg + alen, sizeof (*newone));

	/*
	 * Write out the new "assigned-addresses" spec
	 */
	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
	    "assigned-addresses", (int *)newreg,
	    (alen + sizeof (*newone))/sizeof (int));

	kmem_free((caddr_t)newreg, alen+sizeof (*newone));
	kmem_free(assigned, alen);

	return (0);
}
int
pfc_remove_assigned_prop(dev_info_t *dip, pci_regspec_t *oldone)
{
	int		alen, new_len, num_entries, i;
	pci_regspec_t	*assigned;
	uint_t		status;

	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "assigned-addresses", (caddr_t)&assigned, &alen);
	switch (status) {
		case DDI_PROP_SUCCESS:
		break;
		case DDI_PROP_NO_MEMORY:
			return (1);
		default:
			return (0);
	}

	num_entries = alen / sizeof (pci_regspec_t);
	new_len = alen - sizeof (pci_regspec_t);

	/*
	 * Search for the memory being removed.
	 */
	for (i = 0; i < num_entries; i++) {
		if (assigned[i].pci_phys_hi == oldone->pci_phys_hi) {
			if (new_len == 0) {
				(void) ndi_prop_remove(DDI_DEV_T_NONE, dip,
				    "assigned-addresses");
				break;
			}
			if ((new_len - (i * sizeof (pci_regspec_t)))
			    == 0) {
				FC_DEBUG1(1, CE_CONT, "assigned-address entry "
				    "%x removed from property (last entry)\n",
				    oldone->pci_phys_hi);
			} else {
				bcopy((void *)(assigned + i + 1),
				    (void *)(assigned + i),
				    (new_len - (i * sizeof (pci_regspec_t))));

				FC_DEBUG1(1, CE_CONT, "assigned-address entry "
				    "%x removed from property\n",
				    oldone->pci_phys_hi);
			}
			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
			    dip, "assigned-addresses", (int *)assigned,
			    (new_len/sizeof (int)));

			break;
		}
	}

	kmem_free(assigned, alen);

	return (0);
}
/*
 * we recognize the non transparent bridge child nodes with the
 * following property. This is specific to this implementation only.
 * This property is specific to AP nodes only.
 */
#define	PCICFG_DEV_CONF_MAP_PROP		"pci-parent-indirect"

/*
 * If a non transparent bridge drives a hotplug/hotswap bus, then
 * the following property must be defined for the node either by
 * the driver or the OBP.
 */
#define	PCICFG_BUS_CONF_MAP_PROP		"pci-conf-indirect"

/*
 * this function is called only for SPARC platforms, where we may have
 * a mix n' match of direct vs indirectly mapped configuration space.
 */
/*ARGSUSED*/
static int
fcpci_indirect_map(dev_info_t *dip)
{
	int rc = DDI_FAILURE;

	if (ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
	    PCICFG_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
		rc = DDI_SUCCESS;
	else
		if (ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(dip),
		    0, PCICFG_BUS_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
			rc = DDI_SUCCESS;

	return (rc);
}