summaryrefslogtreecommitdiff
path: root/drivers/pci/hotplug/shpchprm_acpi.c
blob: d37b31658edf68590fba02176a6667222c901ed8 (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
/*
 * SHPCHPRM ACPI: PHP Resource Manager for ACPI platform
 *
 * Copyright (C) 2003-2004 Intel Corporation
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to <kristen.c.accardi@intel.com>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/acpi.h>
#include <linux/efi.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#ifdef	CONFIG_IA64
#include <asm/iosapic.h>
#endif
#include <acpi/acpi.h>
#include <acpi/acpi_bus.h>
#include <acpi/actypes.h>
#include "shpchp.h"
#include "shpchprm.h"

#define	PCI_MAX_BUS		0x100
#define	ACPI_STA_DEVICE_PRESENT	0x01

#define	METHOD_NAME__SUN	"_SUN"
#define	METHOD_NAME__HPP	"_HPP"
#define	METHOD_NAME_OSHP	"OSHP"

#define	PHP_RES_BUS		0xA0
#define	PHP_RES_IO		0xA1
#define	PHP_RES_MEM		0xA2
#define	PHP_RES_PMEM		0xA3

#define	BRIDGE_TYPE_P2P		0x00
#define	BRIDGE_TYPE_HOST	0x01

/* this should go to drivers/acpi/include/ */
struct acpi__hpp {
	u8	cache_line_size;
	u8	latency_timer;
	u8	enable_serr;
	u8	enable_perr;
};

struct acpi_php_slot {
	struct acpi_php_slot	*next;
	struct acpi_bridge	*bridge;
	acpi_handle		handle;
	int	seg;
	int	bus;
	int	dev;
	int	fun;
	u32	sun;
	struct pci_resource *mem_head;
	struct pci_resource *p_mem_head;
	struct pci_resource *io_head;
	struct pci_resource *bus_head;
	void	*slot_ops;	/* _STA, _EJx, etc */
	struct slot *slot;
};		/* per func */

struct acpi_bridge {
	struct acpi_bridge	*parent;
	struct acpi_bridge	*next;
	struct acpi_bridge	*child;
	acpi_handle	handle;
	int seg;
	int pbus;				/* pdev->bus->number		*/
	int pdevice;				/* PCI_SLOT(pdev->devfn)	*/
	int pfunction;				/* PCI_DEVFN(pdev->devfn)	*/
	int bus;				/* pdev->subordinate->number	*/
	struct acpi__hpp		*_hpp;
	struct acpi_php_slot	*slots;
	struct pci_resource 	*tmem_head;	/* total from crs	*/
	struct pci_resource 	*tp_mem_head;	/* total from crs	*/
	struct pci_resource 	*tio_head;	/* total from crs	*/
	struct pci_resource 	*tbus_head;	/* total from crs	*/
	struct pci_resource 	*mem_head;	/* available	*/
	struct pci_resource 	*p_mem_head;	/* available	*/
	struct pci_resource 	*io_head;	/* available	*/
	struct pci_resource 	*bus_head;	/* available	*/
	int scanned;
	int type;
};

static struct acpi_bridge *acpi_bridges_head;

static u8 * acpi_path_name( acpi_handle	handle)
{
	acpi_status		status;
	static u8	path_name[ACPI_PATHNAME_MAX];
	struct acpi_buffer		ret_buf = { ACPI_PATHNAME_MAX, path_name };

	memset(path_name, 0, sizeof (path_name));
	status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &ret_buf);

	if (ACPI_FAILURE(status))
		return NULL;
	else
		return path_name;	
}

static void acpi_get__hpp ( struct acpi_bridge	*ab);
static void acpi_run_oshp ( struct acpi_bridge	*ab);

static int acpi_add_slot_to_php_slots(
	struct acpi_bridge	*ab,
	int				bus_num,
	acpi_handle		handle,
	u32				adr,
	u32				sun
	)
{
	struct acpi_php_slot	*aps;
	static long	samesun = -1;

	aps = (struct acpi_php_slot *) kmalloc (sizeof(struct acpi_php_slot), GFP_KERNEL);
	if (!aps) {
		err ("acpi_shpchprm: alloc for aps fail\n");
		return -1;
	}
	memset(aps, 0, sizeof(struct acpi_php_slot));

	aps->handle = handle;
	aps->bus = bus_num;
	aps->dev = (adr >> 16) & 0xffff;
	aps->fun = adr & 0xffff;
	aps->sun = sun;

	aps->next = ab->slots;	/* cling to the bridge */
	aps->bridge = ab;
	ab->slots = aps;

	ab->scanned += 1;
	if (!ab->_hpp)
		acpi_get__hpp(ab);

	acpi_run_oshp(ab);

	if (sun != samesun) {
		info("acpi_shpchprm:   Slot sun(%x) at s:b:d:f=0x%02x:%02x:%02x:%02x\n", aps->sun, ab->seg, 
			aps->bus, aps->dev, aps->fun);
		samesun = sun;
	}
	return 0;
}

static void acpi_get__hpp ( struct acpi_bridge	*ab)
{
	acpi_status		status;
	u8			nui[4];
	struct acpi_buffer	ret_buf = { 0, NULL};
	union acpi_object	*ext_obj, *package;
	u8			*path_name = acpi_path_name(ab->handle);
	int			i, len = 0;

	/* get _hpp */
	status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
	switch (status) {
	case AE_BUFFER_OVERFLOW:
		ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
		if (!ret_buf.pointer) {
			err ("acpi_shpchprm:%s alloc for _HPP fail\n", path_name);
			return;
		}
		status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
		if (ACPI_SUCCESS(status))
			break;
	default:
		if (ACPI_FAILURE(status)) {
			err("acpi_shpchprm:%s _HPP fail=0x%x\n", path_name, status);
			return;
		}
	}

	ext_obj = (union acpi_object *) ret_buf.pointer;
	if (ext_obj->type != ACPI_TYPE_PACKAGE) {
		err ("acpi_shpchprm:%s _HPP obj not a package\n", path_name);
		goto free_and_return;
	}

	len = ext_obj->package.count;
	package = (union acpi_object *) ret_buf.pointer;
	for ( i = 0; (i < len) || (i < 4); i++) {
		ext_obj = (union acpi_object *) &package->package.elements[i];
		switch (ext_obj->type) {
		case ACPI_TYPE_INTEGER:
			nui[i] = (u8)ext_obj->integer.value;
			break;
		default:
			err ("acpi_shpchprm:%s _HPP obj type incorrect\n", path_name);
			goto free_and_return;
		}
	}

	ab->_hpp = kmalloc (sizeof (struct acpi__hpp), GFP_KERNEL);
	if (!ab->_hpp) {
		err ("acpi_shpchprm:%s alloc for _HPP failed\n", path_name);
		goto free_and_return;
	}
	memset(ab->_hpp, 0, sizeof(struct acpi__hpp));

	ab->_hpp->cache_line_size	= nui[0];
	ab->_hpp->latency_timer		= nui[1];
	ab->_hpp->enable_serr		= nui[2];
	ab->_hpp->enable_perr		= nui[3];

	dbg("  _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
	dbg("  _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
	dbg("  _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
	dbg("  _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);

free_and_return:
	kfree(ret_buf.pointer);
}

static void acpi_run_oshp ( struct acpi_bridge	*ab)
{
	acpi_status		status;
	u8			*path_name = acpi_path_name(ab->handle);

	/* run OSHP */
	status = acpi_evaluate_object(ab->handle, METHOD_NAME_OSHP, NULL, NULL);
	if (ACPI_FAILURE(status)) {
		err("acpi_pciehprm:%s OSHP fails=0x%x\n", path_name, status);
	} else
		dbg("acpi_pciehprm:%s OSHP passes =0x%x\n", path_name, status);
	return;
}

static acpi_status acpi_evaluate_crs(
	acpi_handle		handle,
	struct acpi_resource	**retbuf
	)
{
	acpi_status		status;
	struct acpi_buffer		crsbuf;
	u8			*path_name = acpi_path_name(handle);

	crsbuf.length  = 0;
	crsbuf.pointer = NULL;

	status = acpi_get_current_resources (handle, &crsbuf);

	switch (status) {
	case AE_BUFFER_OVERFLOW:
		break;		/* found */
	case AE_NOT_FOUND:
		dbg("acpi_shpchprm:%s _CRS not found\n", path_name);
		return status;
	default:
		err ("acpi_shpchprm:%s _CRS fail=0x%x\n", path_name, status);
		return status;
	}

	crsbuf.pointer = kmalloc (crsbuf.length, GFP_KERNEL);
	if (!crsbuf.pointer) {
		err ("acpi_shpchprm: alloc %ld bytes for %s _CRS fail\n", (ulong)crsbuf.length, path_name);
		return AE_NO_MEMORY;
	}

	status = acpi_get_current_resources (handle, &crsbuf);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm: %s _CRS fail=0x%x.\n", path_name, status);
		kfree(crsbuf.pointer);
		return status;
	}

	*retbuf = crsbuf.pointer;

	return status;
}

static void free_pci_resource ( struct pci_resource	*aprh)
{
	struct pci_resource	*res, *next;

	for (res = aprh; res; res = next) {
		next = res->next;
		kfree(res);
	}
}

static void print_pci_resource ( struct pci_resource	*aprh)
{
	struct pci_resource	*res;

	for (res = aprh; res; res = res->next)
		dbg("        base= 0x%x length= 0x%x\n", res->base, res->length);
}

static void print_slot_resources( struct acpi_php_slot	*aps)
{
	if (aps->bus_head) {
		dbg("    BUS Resources:\n");
		print_pci_resource (aps->bus_head);
	}

	if (aps->io_head) {
		dbg("    IO Resources:\n");
		print_pci_resource (aps->io_head);
	}

	if (aps->mem_head) {
		dbg("    MEM Resources:\n");
		print_pci_resource (aps->mem_head);
	}

	if (aps->p_mem_head) {
		dbg("    PMEM Resources:\n");
		print_pci_resource (aps->p_mem_head);
	}
}

static void print_pci_resources( struct acpi_bridge	*ab)
{
	if (ab->tbus_head) {
		dbg("    Total BUS Resources:\n");
		print_pci_resource (ab->tbus_head);
	}
	if (ab->bus_head) {
		dbg("    BUS Resources:\n");
		print_pci_resource (ab->bus_head);
	}

	if (ab->tio_head) {
		dbg("    Total IO Resources:\n");
		print_pci_resource (ab->tio_head);
	}
	if (ab->io_head) {
		dbg("    IO Resources:\n");
		print_pci_resource (ab->io_head);
	}

	if (ab->tmem_head) {
		dbg("    Total MEM Resources:\n");
		print_pci_resource (ab->tmem_head);
	}
	if (ab->mem_head) {
		dbg("    MEM Resources:\n");
		print_pci_resource (ab->mem_head);
	}

	if (ab->tp_mem_head) {
		dbg("    Total PMEM Resources:\n");
		print_pci_resource (ab->tp_mem_head);
	}
	if (ab->p_mem_head) {
		dbg("    PMEM Resources:\n");
		print_pci_resource (ab->p_mem_head);
	}
	if (ab->_hpp) {
		dbg("    _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
		dbg("    _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
		dbg("    _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
		dbg("    _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);
	}
}

static int shpchprm_delete_resource(
	struct pci_resource **aprh,
	ulong base,
	ulong size)
{
	struct pci_resource *res;
	struct pci_resource *prevnode;
	struct pci_resource *split_node;
	ulong tbase;

	shpchp_resource_sort_and_combine(aprh);

	for (res = *aprh; res; res = res->next) {
		if (res->base > base)
			continue;

		if ((res->base + res->length) < (base + size))
			continue;

		if (res->base < base) {
			tbase = base;

			if ((res->length - (tbase - res->base)) < size)
				continue;

			split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
			if (!split_node)
				return -ENOMEM;

			split_node->base = res->base;
			split_node->length = tbase - res->base;
			res->base = tbase;
			res->length -= split_node->length;

			split_node->next = res->next;
			res->next = split_node;
		}

		if (res->length >= size) {
			split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
			if (!split_node)
				return -ENOMEM;

			split_node->base = res->base + size;
			split_node->length = res->length - size;
			res->length = size;

			split_node->next = res->next;
			res->next = split_node;
		}

		if (*aprh == res) {
			*aprh = res->next;
		} else {
			prevnode = *aprh;
			while (prevnode->next != res)
				prevnode = prevnode->next;

			prevnode->next = res->next;
		}
		res->next = NULL;
		kfree(res);
		break;
	}

	return 0;
}

static int shpchprm_delete_resources(
	struct pci_resource **aprh,
	struct pci_resource *this
	)
{
	struct pci_resource *res;

	for (res = this; res; res = res->next)
		shpchprm_delete_resource(aprh, res->base, res->length);

	return 0;
}

static int shpchprm_add_resource(
	struct pci_resource **aprh,
	ulong base,
	ulong size)
{
	struct pci_resource *res;

	for (res = *aprh; res; res = res->next) {
		if ((res->base + res->length) == base) {
			res->length += size;
			size = 0L;
			break;
		}
		if (res->next == *aprh)
			break;
	}

	if (size) {
		res = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
		if (!res) {
			err ("acpi_shpchprm: alloc for res fail\n");
			return -ENOMEM;
		}
		memset(res, 0, sizeof (struct pci_resource));

		res->base = base;
		res->length = size;
		res->next = *aprh;
		*aprh = res;
	}

	return 0;
}

static int shpchprm_add_resources(
	struct pci_resource **aprh,
	struct pci_resource *this
	)
{
	struct pci_resource *res;
	int	rc = 0;

	for (res = this; res && !rc; res = res->next)
		rc = shpchprm_add_resource(aprh, res->base, res->length);

	return rc;
}

static void acpi_parse_io (
	struct acpi_bridge		*ab,
	union acpi_resource_data	*data
	)
{
	struct acpi_resource_io	*dataio;
	dataio = (struct acpi_resource_io *) data;

	dbg("Io Resource\n");
	dbg("  %d bit decode\n", ACPI_DECODE_16 == dataio->io_decode ? 16:10);
	dbg("  Range minimum base: %08X\n", dataio->min_base_address);
	dbg("  Range maximum base: %08X\n", dataio->max_base_address);
	dbg("  Alignment: %08X\n", dataio->alignment);
	dbg("  Range Length: %08X\n", dataio->range_length);
}

static void acpi_parse_fixed_io (
	struct acpi_bridge		*ab,
	union acpi_resource_data	*data
	)
{
	struct acpi_resource_fixed_io  *datafio;
	datafio = (struct acpi_resource_fixed_io *) data;

	dbg("Fixed Io Resource\n");
	dbg("  Range base address: %08X", datafio->base_address);
	dbg("  Range length: %08X", datafio->range_length);
}

static void acpi_parse_address16_32 (
	struct acpi_bridge		*ab,
	union acpi_resource_data	*data,
	acpi_resource_type		id
	)
{
	/* 
	 * acpi_resource_address16 == acpi_resource_address32
	 * acpi_resource_address16 *data16 = (acpi_resource_address16 *) data;
	 */
	struct acpi_resource_address32 *data32 = (struct acpi_resource_address32 *) data;
	struct pci_resource **aprh, **tprh;

	if (id == ACPI_RSTYPE_ADDRESS16)
		dbg("acpi_shpchprm:16-Bit Address Space Resource\n");
	else
		dbg("acpi_shpchprm:32-Bit Address Space Resource\n");

	switch (data32->resource_type) {
	case ACPI_MEMORY_RANGE: 
		dbg("  Resource Type: Memory Range\n");
		aprh = &ab->mem_head;
		tprh = &ab->tmem_head;

		switch (data32->attribute.memory.cache_attribute) {
		case ACPI_NON_CACHEABLE_MEMORY:
			dbg("  Type Specific: Noncacheable memory\n");
			break; 
		case ACPI_CACHABLE_MEMORY:
			dbg("  Type Specific: Cacheable memory\n");
			break; 
		case ACPI_WRITE_COMBINING_MEMORY:
			dbg("  Type Specific: Write-combining memory\n");
			break; 
		case ACPI_PREFETCHABLE_MEMORY:
			aprh = &ab->p_mem_head;
			dbg("  Type Specific: Prefetchable memory\n");
			break; 
		default:
			dbg("  Type Specific: Invalid cache attribute\n");
			break;
		}

		dbg("  Type Specific: Read%s\n", ACPI_READ_WRITE_MEMORY == data32->attribute.memory.read_write_attribute ? "/Write":" Only");
		break;

	case ACPI_IO_RANGE: 
		dbg("  Resource Type: I/O Range\n");
		aprh = &ab->io_head;
		tprh = &ab->tio_head;

		switch (data32->attribute.io.range_attribute) {
		case ACPI_NON_ISA_ONLY_RANGES:
			dbg("  Type Specific: Non-ISA Io Addresses\n");
			break; 
		case ACPI_ISA_ONLY_RANGES:
			dbg("  Type Specific: ISA Io Addresses\n");
			break; 
		case ACPI_ENTIRE_RANGE:
			dbg("  Type Specific: ISA and non-ISA Io Addresses\n");
			break; 
		default:
			dbg("  Type Specific: Invalid range attribute\n");
			break;
		}
		break;

	case ACPI_BUS_NUMBER_RANGE: 
		dbg("  Resource Type: Bus Number Range(fixed)\n");
		/* fixup to be compatible with the rest of php driver */
		data32->min_address_range++;
		data32->address_length--;
		aprh = &ab->bus_head;
		tprh = &ab->tbus_head;
		break; 
	default: 
		dbg("  Resource Type: Invalid resource type. Exiting.\n");
		return;
	}

	dbg("  Resource %s\n", ACPI_CONSUMER == data32->producer_consumer ? "Consumer":"Producer");
	dbg("  %s decode\n", ACPI_SUB_DECODE == data32->decode ? "Subtractive":"Positive");
	dbg("  Min address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->min_address_fixed ? "":"not");
	dbg("  Max address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->max_address_fixed ? "":"not");
	dbg("  Granularity: %08X\n", data32->granularity);
	dbg("  Address range min: %08X\n", data32->min_address_range);
	dbg("  Address range max: %08X\n", data32->max_address_range);
	dbg("  Address translation offset: %08X\n", data32->address_translation_offset);
	dbg("  Address Length: %08X\n", data32->address_length);

	if (0xFF != data32->resource_source.index) {
		dbg("  Resource Source Index: %X\n", data32->resource_source.index);
		/* dbg("  Resource Source: %s\n", data32->resource_source.string_ptr); */
	}

	shpchprm_add_resource(aprh, data32->min_address_range, data32->address_length);
}

static acpi_status acpi_parse_crs(
	struct acpi_bridge	*ab,
	struct acpi_resource	*crsbuf
	)
{
	acpi_status		status = AE_OK;
	struct acpi_resource	*resource = crsbuf;
	u8			count = 0;
	u8			done = 0;

	while (!done) {
		dbg("acpi_shpchprm: PCI bus 0x%x Resource structure %x.\n", ab->bus, count++);
		switch (resource->id) {
		case ACPI_RSTYPE_IRQ:
			dbg("Irq -------- Resource\n");
			break; 
		case ACPI_RSTYPE_DMA:
			dbg("DMA -------- Resource\n");
			break; 
		case ACPI_RSTYPE_START_DPF:
			dbg("Start DPF -------- Resource\n");
			break; 
		case ACPI_RSTYPE_END_DPF:
			dbg("End DPF -------- Resource\n");
			break; 
		case ACPI_RSTYPE_IO:
			acpi_parse_io (ab, &resource->data);
			break; 
		case ACPI_RSTYPE_FIXED_IO:
			acpi_parse_fixed_io (ab, &resource->data);
			break; 
		case ACPI_RSTYPE_VENDOR:
			dbg("Vendor -------- Resource\n");
			break; 
		case ACPI_RSTYPE_END_TAG:
			dbg("End_tag -------- Resource\n");
			done = 1;
			break; 
		case ACPI_RSTYPE_MEM24:
			dbg("Mem24 -------- Resource\n");
			break; 
		case ACPI_RSTYPE_MEM32:
			dbg("Mem32 -------- Resource\n");
			break; 
		case ACPI_RSTYPE_FIXED_MEM32:
			dbg("Fixed Mem32 -------- Resource\n");
			break; 
		case ACPI_RSTYPE_ADDRESS16:
			acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS16);
			break; 
		case ACPI_RSTYPE_ADDRESS32:
			acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS32);
			break; 
		case ACPI_RSTYPE_ADDRESS64:
			info("Address64 -------- Resource unparsed\n");
			break; 
		case ACPI_RSTYPE_EXT_IRQ:
			dbg("Ext Irq -------- Resource\n");
			break; 
		default:
			dbg("Invalid -------- resource type 0x%x\n", resource->id);
			break;
		}

		resource = (struct acpi_resource *) ((char *)resource + resource->length);
	}

	return status;
}

static acpi_status acpi_get_crs( struct acpi_bridge	*ab)
{
	acpi_status		status;
	struct acpi_resource	*crsbuf;

	status = acpi_evaluate_crs(ab->handle, &crsbuf);
	if (ACPI_SUCCESS(status)) {
		status = acpi_parse_crs(ab, crsbuf);
		kfree(crsbuf);

		shpchp_resource_sort_and_combine(&ab->bus_head);
		shpchp_resource_sort_and_combine(&ab->io_head);
		shpchp_resource_sort_and_combine(&ab->mem_head);
		shpchp_resource_sort_and_combine(&ab->p_mem_head);

		shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
		shpchprm_add_resources (&ab->tio_head, ab->io_head);
		shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
		shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
	}

	return status;
}

/* find acpi_bridge downword from ab.  */
static struct acpi_bridge *
find_acpi_bridge_by_bus(
	struct acpi_bridge *ab,
	int seg,
	int bus		/* pdev->subordinate->number */
	)
{
	struct acpi_bridge	*lab = NULL;

	if (!ab)
		return NULL;

	if ((ab->bus == bus) && (ab->seg == seg))
		return ab;

	if (ab->child)
		lab = find_acpi_bridge_by_bus(ab->child, seg, bus);

	if (!lab)
	if (ab->next)
		lab = find_acpi_bridge_by_bus(ab->next, seg, bus);

	return lab;
}

/*
 * Build a device tree of ACPI PCI Bridges
 */
static void shpchprm_acpi_register_a_bridge (
	struct acpi_bridge	**head,
	struct acpi_bridge	*pab,	/* parent bridge to which child bridge is added */
	struct acpi_bridge	*cab	/* child bridge to add */
	)
{
	struct acpi_bridge	*lpab;
	struct acpi_bridge	*lcab;

	lpab = find_acpi_bridge_by_bus(*head, pab->seg, pab->bus);
	if (!lpab) {
		if (!(pab->type & BRIDGE_TYPE_HOST))
			warn("PCI parent bridge s:b(%x:%x) not in list.\n", pab->seg, pab->bus);
		pab->next = *head;
		*head = pab;
		lpab = pab;
	}

	if ((cab->type & BRIDGE_TYPE_HOST) && (pab == cab))
		return;

	lcab = find_acpi_bridge_by_bus(*head, cab->seg, cab->bus);
	if (lcab) {
		if ((pab->bus != lcab->parent->bus) || (lcab->bus != cab->bus))
			err("PCI child bridge s:b(%x:%x) in list with diff parent.\n", cab->seg, cab->bus);
		return;
	} else
		lcab = cab;

	lcab->parent = lpab;
	lcab->next = lpab->child;
	lpab->child = lcab;
}

static acpi_status shpchprm_acpi_build_php_slots_callback(
	acpi_handle	handle,
	u32		Level,
	void		*context,
	void		**retval
	)
{
	ulong		bus_num;
	ulong		seg_num;
	ulong		sun, adr;
	ulong		padr = 0;
	acpi_handle		phandle = NULL;
	struct acpi_bridge	*pab = (struct acpi_bridge *)context;
	struct acpi_bridge	*lab;
	acpi_status		status;
	u8			*path_name = acpi_path_name(handle);

	/* get _SUN */
	status = acpi_evaluate_integer(handle, METHOD_NAME__SUN, NULL, &sun);
	switch(status) {
	case AE_NOT_FOUND:
		return AE_OK;
	default:
		if (ACPI_FAILURE(status)) {
			err("acpi_shpchprm:%s _SUN fail=0x%x\n", path_name, status);
			return status;
		}
	}

	/* get _ADR. _ADR must exist if _SUN exists */
	status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
		return status;
	}

	dbg("acpi_shpchprm:%s sun=0x%08x adr=0x%08x\n", path_name, (u32)sun, (u32)adr);

	status = acpi_get_parent(handle, &phandle);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:%s get_parent fail=0x%x\n", path_name, status);
		return (status);
	}

	bus_num = pab->bus;
	seg_num = pab->seg;

	if (pab->bus == bus_num) {
		lab = pab;
	} else {
		dbg("WARN: pab is not parent\n");
		lab = find_acpi_bridge_by_bus(pab, seg_num, bus_num);
		if (!lab) {
			dbg("acpi_shpchprm: alloc new P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
			lab = (struct acpi_bridge *)kmalloc(sizeof(struct acpi_bridge), GFP_KERNEL);
			if (!lab) {
				err("acpi_shpchprm: alloc for ab fail\n");
				return AE_NO_MEMORY;
			}
			memset(lab, 0, sizeof(struct acpi_bridge));

			lab->handle = phandle;
			lab->pbus = pab->bus;
			lab->pdevice = (int)(padr >> 16) & 0xffff;
			lab->pfunction = (int)(padr & 0xffff);
			lab->bus = (int)bus_num;
			lab->scanned = 0;
			lab->type = BRIDGE_TYPE_P2P;

			shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, lab);
		} else
			dbg("acpi_shpchprm: found P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
	}

	acpi_add_slot_to_php_slots(lab, (int)bus_num, handle, (u32)adr, (u32)sun);
	return (status);
}

static int shpchprm_acpi_build_php_slots(
	struct acpi_bridge	*ab,
	u32			depth
	)
{
	acpi_status	status;
	u8		*path_name = acpi_path_name(ab->handle);

	/* Walk down this pci bridge to get _SUNs if any behind P2P */
	status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
				ab->handle,
				depth,
				shpchprm_acpi_build_php_slots_callback,
				ab,
				NULL );
	if (ACPI_FAILURE(status)) {
		dbg("acpi_shpchprm:%s walk for _SUN on pci bridge seg:bus(%x:%x) fail=0x%x\n", path_name, ab->seg, ab->bus, status);
		return -1;
	}

	return 0;
}

static void build_a_bridge(
	struct acpi_bridge	*pab,
	struct acpi_bridge	*ab
	)
{
	u8		*path_name = acpi_path_name(ab->handle);

	shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, ab);

	switch (ab->type) {
	case BRIDGE_TYPE_HOST:
		dbg("acpi_shpchprm: Registered PCI HOST Bridge(%02x)    on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
			ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
		break;
	case BRIDGE_TYPE_P2P:
		dbg("acpi_shpchprm: Registered PCI  P2P Bridge(%02x-%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
			ab->pbus, ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
		break;
	};

	/* build any immediate PHP slots under this pci bridge */
	shpchprm_acpi_build_php_slots(ab, 1);
}

static struct acpi_bridge * add_p2p_bridge(
	acpi_handle handle,
	struct acpi_bridge	*pab,	/* parent */
	ulong	adr
	)
{
	struct acpi_bridge	*ab;
	struct pci_dev	*pdev;
	ulong		devnum, funcnum;
	u8			*path_name = acpi_path_name(handle);

	ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
	if (!ab) {
		err("acpi_shpchprm: alloc for ab fail\n");
		return NULL;
	}
	memset(ab, 0, sizeof(struct acpi_bridge));

	devnum = (adr >> 16) & 0xffff;
	funcnum = adr & 0xffff;

	pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
	if (!pdev || !pdev->subordinate) {
		err("acpi_shpchprm:%s is not a P2P Bridge\n", path_name);
		kfree(ab);
		return NULL;
	}

	ab->handle = handle;
	ab->seg = pab->seg;
	ab->pbus = pab->bus;		/* or pdev->bus->number */
	ab->pdevice = devnum;		/* or PCI_SLOT(pdev->devfn) */
	ab->pfunction = funcnum;	/* or PCI_FUNC(pdev->devfn) */
	ab->bus = pdev->subordinate->number;
	ab->scanned = 0;
	ab->type = BRIDGE_TYPE_P2P;

	dbg("acpi_shpchprm: P2P(%x-%x) on pci=b:d:f(%x:%x:%x) acpi=b:d:f(%x:%x:%x) [%s]\n",
		pab->bus, ab->bus, pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
		pab->bus, (u32)devnum, (u32)funcnum, path_name);

	build_a_bridge(pab, ab);

	return ab;
}

static acpi_status scan_p2p_bridge(
	acpi_handle		handle,
	u32			Level,
	void			*context,
	void			**retval
	)
{
	struct acpi_bridge	*pab = (struct acpi_bridge *)context;
	struct acpi_bridge	*ab;
	acpi_status		status;
	ulong		adr = 0;
	u8			*path_name = acpi_path_name(handle);
	ulong		devnum, funcnum;
	struct pci_dev	*pdev;

	/* get device, function */
	status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND)
			err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
		return AE_OK;
	}

	devnum = (adr >> 16) & 0xffff;
	funcnum = adr & 0xffff;

	pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
	if (!pdev)
		return AE_OK;
	if (!pdev->subordinate)
		return AE_OK;

	ab = add_p2p_bridge(handle, pab, adr);
	if (ab) {
		status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
					handle,
					(u32)1,
					scan_p2p_bridge,
					ab,
					NULL);
		if (ACPI_FAILURE(status))
			dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
	}

	return AE_OK;
}

static struct acpi_bridge * add_host_bridge(
	acpi_handle handle,
	ulong	segnum,
	ulong	busnum
	)
{
	ulong			adr = 0;
	acpi_status		status;
	struct acpi_bridge	*ab;
	u8			*path_name = acpi_path_name(handle);

	/* get device, function: host br adr is always 0000 though.  */
	status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
		return NULL;
	}
	dbg("acpi_shpchprm: ROOT PCI seg(0x%x)bus(0x%x)dev(0x%x)func(0x%x) [%s]\n", (u32)segnum, (u32)busnum, 
		(u32)(adr >> 16) & 0xffff, (u32)adr & 0xffff, path_name);

	ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
	if (!ab) {
		err("acpi_shpchprm: alloc for ab fail\n");
		return NULL;
	}
	memset(ab, 0, sizeof(struct acpi_bridge));

	ab->handle = handle;
	ab->seg = (int)segnum;
	ab->bus = ab->pbus = (int)busnum;
	ab->pdevice = (int)(adr >> 16) & 0xffff;
	ab->pfunction = (int)(adr & 0xffff);
	ab->scanned = 0;
	ab->type = BRIDGE_TYPE_HOST;

	/* get root pci bridge's current resources */
	status = acpi_get_crs(ab);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:%s evaluate _CRS fail=0x%x\n", path_name, status);
		kfree(ab);
		return NULL;
	}
	build_a_bridge(ab, ab);

	return ab;
}

static acpi_status acpi_scan_from_root_pci_callback (
	acpi_handle	handle,
	u32			Level,
	void		*context,
	void		**retval
	)
{
	ulong		segnum = 0;
	ulong		busnum = 0;
	acpi_status		status;
	struct acpi_bridge	*ab;
	u8			*path_name = acpi_path_name(handle);

	/* get bus number of this pci root bridge */
	status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &segnum);
	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND) {
			err("acpi_shpchprm:%s evaluate _SEG fail=0x%x\n", path_name, status);
			return status;
		}
		segnum = 0;
	}

	/* get bus number of this pci root bridge */
	status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL, &busnum);
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:%s evaluate _BBN fail=0x%x\n", path_name, status);
		return (status);
	}

	ab = add_host_bridge(handle, segnum, busnum);
	if (ab) {
		status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
					handle,
					1,
					scan_p2p_bridge,
					ab,
					NULL);
		if (ACPI_FAILURE(status))
			dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
	}

	return AE_OK;
}

static int shpchprm_acpi_scan_pci (void)
{
	acpi_status	status;

	/*
	 * TBD: traverse LDM device tree with the help of
	 *  unified ACPI augmented for php device population.
	 */
	status = acpi_get_devices ( PCI_ROOT_HID_STRING,
				acpi_scan_from_root_pci_callback,
				NULL,
				NULL );
	if (ACPI_FAILURE(status)) {
		err("acpi_shpchprm:get_device PCI ROOT HID fail=0x%x\n", status);
		return -1;
	}

	return 0;
}

int shpchprm_init(enum php_ctlr_type ctlr_type)
{
	int	rc;

	if (ctlr_type != PCI)
		return -ENODEV;

	dbg("shpchprm ACPI init <enter>\n");
	acpi_bridges_head = NULL;

	/* construct PCI bus:device tree of acpi_handles */
	rc = shpchprm_acpi_scan_pci();
	if (rc)
		return rc;

	dbg("shpchprm ACPI init %s\n", (rc)?"fail":"success");
	return rc;
}

static void free_a_slot(struct acpi_php_slot *aps)
{
	dbg("        free a php func of slot(0x%02x) on PCI b:d:f=0x%02x:%02x:%02x\n", aps->sun, aps->bus, aps->dev, aps->fun);

	free_pci_resource (aps->io_head);
	free_pci_resource (aps->bus_head);
	free_pci_resource (aps->mem_head);
	free_pci_resource (aps->p_mem_head);

	kfree(aps);
}

static void free_a_bridge( struct acpi_bridge	*ab)
{
	struct acpi_php_slot	*aps, *next;

	switch (ab->type) {
	case BRIDGE_TYPE_HOST:
		dbg("Free ACPI PCI HOST Bridge(%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
			ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
		break;
	case BRIDGE_TYPE_P2P:
		dbg("Free ACPI PCI P2P Bridge(%x-%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
			ab->pbus, ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
		break;
	};

	/* free slots first */
	for (aps = ab->slots; aps; aps = next) {
		next = aps->next;
		free_a_slot(aps);
	}

	free_pci_resource (ab->io_head);
	free_pci_resource (ab->tio_head);
	free_pci_resource (ab->bus_head);
	free_pci_resource (ab->tbus_head);
	free_pci_resource (ab->mem_head);
	free_pci_resource (ab->tmem_head);
	free_pci_resource (ab->p_mem_head);
	free_pci_resource (ab->tp_mem_head);

	kfree(ab);
}

static void shpchprm_free_bridges ( struct acpi_bridge	*ab)
{
	if (!ab)
		return;

	if (ab->child)
		shpchprm_free_bridges (ab->child);

	if (ab->next)
		shpchprm_free_bridges (ab->next);

	free_a_bridge(ab);
}

void shpchprm_cleanup(void)
{
	shpchprm_free_bridges (acpi_bridges_head);
}

static int get_number_of_slots (
	struct acpi_bridge	*ab,
	int				selfonly
	)
{
	struct acpi_php_slot	*aps;
	int	prev_slot = -1;
	int	slot_num = 0;

	for ( aps = ab->slots; aps; aps = aps->next)
		if (aps->dev != prev_slot) {
			prev_slot = aps->dev;
			slot_num++;
		}

	if (ab->child)
		slot_num += get_number_of_slots (ab->child, 0);

	if (selfonly)
		return slot_num;

	if (ab->next)
		slot_num += get_number_of_slots (ab->next, 0);

	return slot_num;
}

static int print_acpi_resources (struct acpi_bridge	*ab)
{
	struct acpi_php_slot	*aps;
	int	i;

	switch (ab->type) {
	case BRIDGE_TYPE_HOST:
		dbg("PCI HOST Bridge (%x) [%s]\n", ab->bus, acpi_path_name(ab->handle));
		break;
	case BRIDGE_TYPE_P2P:
		dbg("PCI P2P Bridge (%x-%x) [%s]\n", ab->pbus, ab->bus, acpi_path_name(ab->handle));
		break;
	};

	print_pci_resources (ab);

	for ( i = -1, aps = ab->slots; aps; aps = aps->next) {
		if (aps->dev == i)
			continue;
		dbg("  Slot sun(%x) s:b:d:f(%02x:%02x:%02x:%02x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
		print_slot_resources(aps);
		i = aps->dev;
	}

	if (ab->child)
		print_acpi_resources (ab->child);

	if (ab->next)
		print_acpi_resources (ab->next);

	return 0;
}

int shpchprm_print_pirt(void)
{
	dbg("SHPCHPRM ACPI Slots\n");
	if (acpi_bridges_head)
		print_acpi_resources (acpi_bridges_head);
	return 0;
}

static struct acpi_php_slot * get_acpi_slot (
	struct acpi_bridge *ab,
	u32 sun
	)
{
	struct acpi_php_slot	*aps = NULL;

	for ( aps = ab->slots; aps; aps = aps->next)
		if (aps->sun == sun)
			return aps;

	if (!aps && ab->child) {
		aps = (struct acpi_php_slot *)get_acpi_slot (ab->child, sun);
		if (aps)
			return aps;
	}

	if (!aps && ab->next) {
		aps = (struct acpi_php_slot *)get_acpi_slot (ab->next, sun);
		if (aps)
			return aps;
	}

	return aps;

}

#if 0
static void * shpchprm_get_slot(struct slot *slot)
{
	struct acpi_bridge	*ab = acpi_bridges_head;
	struct acpi_php_slot	*aps = get_acpi_slot (ab, slot->number);

	aps->slot = slot;

	dbg("Got acpi slot sun(%x): s:b:d:f(%x:%x:%x:%x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);

	return (void *)aps;
}
#endif

static void shpchprm_dump_func_res( struct pci_func *fun)
{
	struct pci_func *func = fun;

	if (func->bus_head) {
		dbg(":    BUS Resources:\n");
		print_pci_resource (func->bus_head);
	}
	if (func->io_head) {
		dbg(":    IO Resources:\n");
		print_pci_resource (func->io_head);
	}
	if (func->mem_head) {
		dbg(":    MEM Resources:\n");
		print_pci_resource (func->mem_head);
	}
	if (func->p_mem_head) {
		dbg(":    PMEM Resources:\n");
		print_pci_resource (func->p_mem_head);
	}
}

static void shpchprm_dump_ctrl_res( struct controller *ctlr)
{
	struct controller *ctrl = ctlr;

	if (ctrl->bus_head) {
		dbg(":    BUS Resources:\n");
		print_pci_resource (ctrl->bus_head);
	}
	if (ctrl->io_head) {
		dbg(":    IO Resources:\n");
		print_pci_resource (ctrl->io_head);
	}
	if (ctrl->mem_head) {
		dbg(":    MEM Resources:\n");
		print_pci_resource (ctrl->mem_head);
	}
	if (ctrl->p_mem_head) {
		dbg(":    PMEM Resources:\n");
		print_pci_resource (ctrl->p_mem_head);
	}
}

static int shpchprm_get_used_resources (
	struct controller *ctrl,
	struct pci_func *func
	)
{
	return shpchp_save_used_resources (ctrl, func, !DISABLE_CARD);
}

static int configure_existing_function(
	struct controller *ctrl,
	struct pci_func *func
	)
{
	int rc;

	/* see how much resources the func has used. */
	rc = shpchprm_get_used_resources (ctrl, func);

	if (!rc) {
		/* subtract the resources used by the func from ctrl resources */
		rc  = shpchprm_delete_resources (&ctrl->bus_head, func->bus_head);
		rc |= shpchprm_delete_resources (&ctrl->io_head, func->io_head);
		rc |= shpchprm_delete_resources (&ctrl->mem_head, func->mem_head);
		rc |= shpchprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
		if (rc)
			warn("aCEF: cannot del used resources\n");
	} else
		err("aCEF: cannot get used resources\n");

	return rc;
}

static int bind_pci_resources_to_slots ( struct controller *ctrl)
{
	struct pci_func *func, new_func;
	int busn = ctrl->slot_bus;
	int devn, funn;
	u32	vid;

	for (devn = 0; devn < 32; devn++) {
		for (funn = 0; funn < 8; funn++) {
			/*
			if (devn == ctrl->device && funn == ctrl->function)
				continue;
			*/
			/* find out if this entry is for an occupied slot */
			vid = 0xFFFFFFFF;
			pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);

			if (vid != 0xFFFFFFFF) {
				func = shpchp_slot_find(busn, devn, funn);
				if (!func) {
					memset(&new_func, 0, sizeof(struct pci_func));
					new_func.bus = busn;
					new_func.device = devn;
					new_func.function = funn;
					new_func.is_a_board = 1;
					configure_existing_function(ctrl, &new_func);
					shpchprm_dump_func_res(&new_func);
				} else {
					configure_existing_function(ctrl, func);
					shpchprm_dump_func_res(func);
				}
				dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
			}
		}
	}

	return 0;
}

static int bind_pci_resources(
	struct controller 	*ctrl,
	struct acpi_bridge	*ab
	)
{
	int	status = 0;

	if (ab->bus_head) {
		dbg("bapr:  BUS Resources add on PCI 0x%x\n", ab->bus);
		status = shpchprm_add_resources (&ctrl->bus_head, ab->bus_head);
		if (shpchprm_delete_resources (&ab->bus_head, ctrl->bus_head))
			warn("bapr:  cannot sub BUS Resource on PCI 0x%x\n", ab->bus);
		if (status) {
			err("bapr:  BUS Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
			return status;
		}
	} else
		info("bapr:  No BUS Resource on PCI 0x%x.\n", ab->bus);

	if (ab->io_head) {
		dbg("bapr:  IO Resources add on PCI 0x%x\n", ab->bus);
		status = shpchprm_add_resources (&ctrl->io_head, ab->io_head);
		if (shpchprm_delete_resources (&ab->io_head, ctrl->io_head))
			warn("bapr:  cannot sub IO Resource on PCI 0x%x\n", ab->bus);
		if (status) {
			err("bapr:  IO Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
			return status;
		}
	} else
		info("bapr:  No  IO Resource on PCI 0x%x.\n", ab->bus);

	if (ab->mem_head) {
		dbg("bapr:  MEM Resources add on PCI 0x%x\n", ab->bus);
		status = shpchprm_add_resources (&ctrl->mem_head, ab->mem_head);
		if (shpchprm_delete_resources (&ab->mem_head, ctrl->mem_head))
			warn("bapr:  cannot sub MEM Resource on PCI 0x%x\n", ab->bus);
		if (status) {
			err("bapr:  MEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
			return status;
		}
	} else
		info("bapr:  No MEM Resource on PCI 0x%x.\n", ab->bus);

	if (ab->p_mem_head) {
		dbg("bapr:  PMEM Resources add on PCI 0x%x\n", ab->bus);
		status = shpchprm_add_resources (&ctrl->p_mem_head, ab->p_mem_head);
		if (shpchprm_delete_resources (&ab->p_mem_head, ctrl->p_mem_head))
			warn("bapr:  cannot sub PMEM Resource on PCI 0x%x\n", ab->bus);
		if (status) {
			err("bapr:  PMEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
			return status;
		}
	} else
		info("bapr:  No PMEM Resource on PCI 0x%x.\n", ab->bus);

	return status;
}

static int no_pci_resources( struct acpi_bridge *ab)
{
	return !(ab->p_mem_head || ab->mem_head || ab->io_head || ab->bus_head);
}

static int find_pci_bridge_resources (
	struct controller *ctrl,
	struct acpi_bridge *ab
	)
{
	int	rc = 0;
	struct pci_func func;

	memset(&func, 0, sizeof(struct pci_func));

	func.bus = ab->pbus;
	func.device = ab->pdevice;
	func.function = ab->pfunction;
	func.is_a_board = 1;

	/* Get used resources for this PCI bridge */
	rc = shpchp_save_used_resources (ctrl, &func, !DISABLE_CARD);

	ab->io_head = func.io_head;
	ab->mem_head = func.mem_head;
	ab->p_mem_head = func.p_mem_head;
	ab->bus_head = func.bus_head;
	if (ab->bus_head)
		shpchprm_delete_resource(&ab->bus_head, ctrl->bus, 1);

	return rc;
}

static int get_pci_resources_from_bridge(
	struct controller *ctrl,
	struct acpi_bridge *ab
	)
{
	int	rc = 0;

	dbg("grfb:  Get Resources for PCI 0x%x from actual PCI bridge 0x%x.\n", ctrl->bus, ab->bus);

	rc = find_pci_bridge_resources (ctrl, ab);

	shpchp_resource_sort_and_combine(&ab->bus_head);
	shpchp_resource_sort_and_combine(&ab->io_head);
	shpchp_resource_sort_and_combine(&ab->mem_head);
	shpchp_resource_sort_and_combine(&ab->p_mem_head);

	shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
	shpchprm_add_resources (&ab->tio_head, ab->io_head);
	shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
	shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);

	return rc;
}

static int get_pci_resources(
	struct controller	*ctrl,
	struct acpi_bridge	*ab
	)
{
	int	rc = 0;

	if (no_pci_resources(ab)) {
		dbg("spbr:PCI 0x%x has no resources. Get parent resources.\n", ab->bus);
		rc = get_pci_resources_from_bridge(ctrl, ab);
	}

	return rc;
}

int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
{
	int offset = devnum - ctrl->slot_device_offset;

	dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
	*sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc *offset);
	return 0;
}

/*
 * Get resources for this ctrl.
 *  1. get total resources from ACPI _CRS or bridge (this ctrl)
 *  2. find used resources of existing adapters
 *	3. subtract used resources from total resources
 */
int shpchprm_find_available_resources( struct controller *ctrl)
{
	int rc = 0;
	struct acpi_bridge	*ab;

	ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->pci_dev->subordinate->number);
	if (!ab) {
		err("pfar:cannot locate acpi bridge of PCI 0x%x.\n", ctrl->pci_dev->subordinate->number);
		return -1;
	}
	if (no_pci_resources(ab)) {
		rc = get_pci_resources(ctrl, ab);
		if (rc) {
			err("pfar:cannot get pci resources of PCI 0x%x.\n",ctrl->pci_dev->subordinate->number);
			return -1;
		}
	}

	rc = bind_pci_resources(ctrl, ab);
	dbg("pfar:pre-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
	shpchprm_dump_ctrl_res(ctrl);

	bind_pci_resources_to_slots (ctrl);

	dbg("pfar:post-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
	shpchprm_dump_ctrl_res(ctrl);

	return rc;
}

int shpchprm_set_hpp(
	struct controller *ctrl,
	struct pci_func *func,
	u8	card_type
	)
{
	struct acpi_bridge	*ab;
	struct pci_bus lpci_bus, *pci_bus;
	int				rc = 0;
	unsigned int	devfn;
	u8				cls= 0x08;	/* default cache line size	*/
	u8				lt = 0x40;	/* default latency timer	*/
	u8				ep = 0;
	u8				es = 0;

	memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
	pci_bus = &lpci_bus;
	pci_bus->number = func->bus;
	devfn = PCI_DEVFN(func->device, func->function);

	ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->slot_bus);

	if (ab) {
		if (ab->_hpp) {
			lt  = (u8)ab->_hpp->latency_timer;
			cls = (u8)ab->_hpp->cache_line_size;
			ep  = (u8)ab->_hpp->enable_perr;
			es  = (u8)ab->_hpp->enable_serr;
		} else
			dbg("_hpp: no _hpp for B/D/F=%#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
	} else
		dbg("_hpp: no acpi bridge for B/D/F = %#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);


	if (card_type == PCI_HEADER_TYPE_BRIDGE) {
		/* set subordinate Latency Timer */
		rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, lt);
	}

	/* set base Latency Timer */
	rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, lt);
	dbg("  set latency timer  =0x%02x: %x\n", lt, rc);

	rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, cls);
	dbg("  set cache_line_size=0x%02x: %x\n", cls, rc);

	return rc;
}

void shpchprm_enable_card(
	struct controller *ctrl,
	struct pci_func *func,
	u8 card_type)
{
	u16 command, cmd, bcommand, bcmd;
	struct pci_bus lpci_bus, *pci_bus;
	struct acpi_bridge	*ab;
	unsigned int devfn;
	int rc;

	memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
	pci_bus = &lpci_bus;
	pci_bus->number = func->bus;
	devfn = PCI_DEVFN(func->device, func->function);

	rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);

	if (card_type == PCI_HEADER_TYPE_BRIDGE) {
		rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
	}

	cmd = command  = command | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
		| PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
	bcmd = bcommand  = bcommand | PCI_BRIDGE_CTL_NO_ISA;

	ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->slot_bus);
	if (ab) {
		if (ab->_hpp) {
			if (ab->_hpp->enable_perr) {
				command |= PCI_COMMAND_PARITY;
				bcommand |= PCI_BRIDGE_CTL_PARITY;
			} else {
				command &= ~PCI_COMMAND_PARITY;
				bcommand &= ~PCI_BRIDGE_CTL_PARITY;
			}
			if (ab->_hpp->enable_serr) {
				command |= PCI_COMMAND_SERR;
				bcommand |= PCI_BRIDGE_CTL_SERR;
			} else {
				command &= ~PCI_COMMAND_SERR;
				bcommand &= ~PCI_BRIDGE_CTL_SERR;
			}
		} else
			dbg("no _hpp for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
	} else
		dbg("no acpi bridge for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);

	if (command != cmd) {
		rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
	}
	if ((card_type == PCI_HEADER_TYPE_BRIDGE) && (bcommand != bcmd)) {
		rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
	}
}