vserver 1.9.5.x5
[linux-2.6.git] / drivers / pci / hotplug / shpchprm_acpi.c
1 /*
2  * SHPCHPRM ACPI: PHP Resource Manager for ACPI platform
3  *
4  * Copyright (C) 2003-2004 Intel Corporation
5  *
6  * All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16  * NON INFRINGEMENT.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * Send feedback to <dely.l.sy@intel.com>
24  *
25  */
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/acpi.h>
34 #include <linux/efi.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #ifdef  CONFIG_IA64
38 #include <asm/iosapic.h>
39 #endif
40 #include <acpi/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/actypes.h>
43 #include "shpchp.h"
44 #include "shpchprm.h"
45
46 #define PCI_MAX_BUS             0x100
47 #define ACPI_STA_DEVICE_PRESENT 0x01
48
49 #define METHOD_NAME__SUN        "_SUN"
50 #define METHOD_NAME__HPP        "_HPP"
51 #define METHOD_NAME_OSHP        "OSHP"
52
53 #define PHP_RES_BUS             0xA0
54 #define PHP_RES_IO              0xA1
55 #define PHP_RES_MEM             0xA2
56 #define PHP_RES_PMEM            0xA3
57
58 #define BRIDGE_TYPE_P2P         0x00
59 #define BRIDGE_TYPE_HOST        0x01
60
61 /* this should go to drivers/acpi/include/ */
62 struct acpi__hpp {
63         u8      cache_line_size;
64         u8      latency_timer;
65         u8      enable_serr;
66         u8      enable_perr;
67 };
68
69 struct acpi_php_slot {
70         struct acpi_php_slot    *next;
71         struct acpi_bridge      *bridge;
72         acpi_handle             handle;
73         int     seg;
74         int     bus;
75         int     dev;
76         int     fun;
77         u32     sun;
78         struct pci_resource *mem_head;
79         struct pci_resource *p_mem_head;
80         struct pci_resource *io_head;
81         struct pci_resource *bus_head;
82         void    *slot_ops;      /* _STA, _EJx, etc */
83         struct slot *slot;
84 };              /* per func */
85
86 struct acpi_bridge {
87         struct acpi_bridge      *parent;
88         struct acpi_bridge      *next;
89         struct acpi_bridge      *child;
90         acpi_handle     handle;
91         int seg;
92         int pbus;                               /* pdev->bus->number            */
93         int pdevice;                            /* PCI_SLOT(pdev->devfn)        */
94         int pfunction;                          /* PCI_DEVFN(pdev->devfn)       */
95         int bus;                                /* pdev->subordinate->number    */
96         struct acpi__hpp                *_hpp;
97         struct acpi_php_slot    *slots;
98         struct pci_resource     *tmem_head;     /* total from crs       */
99         struct pci_resource     *tp_mem_head;   /* total from crs       */
100         struct pci_resource     *tio_head;      /* total from crs       */
101         struct pci_resource     *tbus_head;     /* total from crs       */
102         struct pci_resource     *mem_head;      /* available    */
103         struct pci_resource     *p_mem_head;    /* available    */
104         struct pci_resource     *io_head;       /* available    */
105         struct pci_resource     *bus_head;      /* available    */
106         int scanned;
107         int type;
108 };
109
110 static struct acpi_bridge *acpi_bridges_head;
111
112 static u8 * acpi_path_name( acpi_handle handle)
113 {
114         acpi_status             status;
115         static u8       path_name[ACPI_PATHNAME_MAX];
116         struct acpi_buffer              ret_buf = { ACPI_PATHNAME_MAX, path_name };
117
118         memset(path_name, 0, sizeof (path_name));
119         status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &ret_buf);
120
121         if (ACPI_FAILURE(status))
122                 return NULL;
123         else
124                 return path_name;       
125 }
126
127 static void acpi_get__hpp ( struct acpi_bridge  *ab);
128 static void acpi_run_oshp ( struct acpi_bridge  *ab);
129
130 static int acpi_add_slot_to_php_slots(
131         struct acpi_bridge      *ab,
132         int                             bus_num,
133         acpi_handle             handle,
134         u32                             adr,
135         u32                             sun
136         )
137 {
138         struct acpi_php_slot    *aps;
139         static long     samesun = -1;
140
141         aps = (struct acpi_php_slot *) kmalloc (sizeof(struct acpi_php_slot), GFP_KERNEL);
142         if (!aps) {
143                 err ("acpi_shpchprm: alloc for aps fail\n");
144                 return -1;
145         }
146         memset(aps, 0, sizeof(struct acpi_php_slot));
147
148         aps->handle = handle;
149         aps->bus = bus_num;
150         aps->dev = (adr >> 16) & 0xffff;
151         aps->fun = adr & 0xffff;
152         aps->sun = sun;
153
154         aps->next = ab->slots;  /* cling to the bridge */
155         aps->bridge = ab;
156         ab->slots = aps;
157
158         ab->scanned += 1;
159         if (!ab->_hpp)
160                 acpi_get__hpp(ab);
161
162         acpi_run_oshp(ab);
163
164         if (sun != samesun) {
165                 info("acpi_shpchprm:   Slot sun(%x) at s:b:d:f=0x%02x:%02x:%02x:%02x\n", aps->sun, ab->seg, 
166                         aps->bus, aps->dev, aps->fun);
167                 samesun = sun;
168         }
169         return 0;
170 }
171
172 static void acpi_get__hpp ( struct acpi_bridge  *ab)
173 {
174         acpi_status             status;
175         u8                      nui[4];
176         struct acpi_buffer      ret_buf = { 0, NULL};
177         union acpi_object       *ext_obj, *package;
178         u8                      *path_name = acpi_path_name(ab->handle);
179         int                     i, len = 0;
180
181         /* get _hpp */
182         status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
183         switch (status) {
184         case AE_BUFFER_OVERFLOW:
185                 ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
186                 if (!ret_buf.pointer) {
187                         err ("acpi_shpchprm:%s alloc for _HPP fail\n", path_name);
188                         return;
189                 }
190                 status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
191                 if (ACPI_SUCCESS(status))
192                         break;
193         default:
194                 if (ACPI_FAILURE(status)) {
195                         err("acpi_shpchprm:%s _HPP fail=0x%x\n", path_name, status);
196                         return;
197                 }
198         }
199
200         ext_obj = (union acpi_object *) ret_buf.pointer;
201         if (ext_obj->type != ACPI_TYPE_PACKAGE) {
202                 err ("acpi_shpchprm:%s _HPP obj not a package\n", path_name);
203                 goto free_and_return;
204         }
205
206         len = ext_obj->package.count;
207         package = (union acpi_object *) ret_buf.pointer;
208         for ( i = 0; (i < len) || (i < 4); i++) {
209                 ext_obj = (union acpi_object *) &package->package.elements[i];
210                 switch (ext_obj->type) {
211                 case ACPI_TYPE_INTEGER:
212                         nui[i] = (u8)ext_obj->integer.value;
213                         break;
214                 default:
215                         err ("acpi_shpchprm:%s _HPP obj type incorrect\n", path_name);
216                         goto free_and_return;
217                 }
218         }
219
220         ab->_hpp = kmalloc (sizeof (struct acpi__hpp), GFP_KERNEL);
221         if (!ab->_hpp) {
222                 err ("acpi_shpchprm:%s alloc for _HPP failed\n", path_name);
223                 goto free_and_return;
224         }
225         memset(ab->_hpp, 0, sizeof(struct acpi__hpp));
226
227         ab->_hpp->cache_line_size       = nui[0];
228         ab->_hpp->latency_timer         = nui[1];
229         ab->_hpp->enable_serr           = nui[2];
230         ab->_hpp->enable_perr           = nui[3];
231
232         dbg("  _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
233         dbg("  _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
234         dbg("  _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
235         dbg("  _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);
236
237 free_and_return:
238         kfree(ret_buf.pointer);
239 }
240
241 static void acpi_run_oshp ( struct acpi_bridge  *ab)
242 {
243         acpi_status             status;
244         u8                      *path_name = acpi_path_name(ab->handle);
245         struct acpi_buffer      ret_buf = { 0, NULL};
246
247         /* run OSHP */
248         status = acpi_evaluate_object(ab->handle, METHOD_NAME_OSHP, NULL, &ret_buf);
249         if (ACPI_FAILURE(status)) {
250                 err("acpi_pciehprm:%s OSHP fails=0x%x\n", path_name, status);
251         } else
252                 dbg("acpi_pciehprm:%s OSHP passes =0x%x\n", path_name, status);
253         return;
254 }
255
256 static acpi_status acpi_evaluate_crs(
257         acpi_handle             handle,
258         struct acpi_resource    **retbuf
259         )
260 {
261         acpi_status             status;
262         struct acpi_buffer              crsbuf;
263         u8                      *path_name = acpi_path_name(handle);
264
265         crsbuf.length  = 0;
266         crsbuf.pointer = NULL;
267
268         status = acpi_get_current_resources (handle, &crsbuf);
269
270         switch (status) {
271         case AE_BUFFER_OVERFLOW:
272                 break;          /* found */
273         case AE_NOT_FOUND:
274                 dbg("acpi_shpchprm:%s _CRS not found\n", path_name);
275                 return status;
276         default:
277                 err ("acpi_shpchprm:%s _CRS fail=0x%x\n", path_name, status);
278                 return status;
279         }
280
281         crsbuf.pointer = kmalloc (crsbuf.length, GFP_KERNEL);
282         if (!crsbuf.pointer) {
283                 err ("acpi_shpchprm: alloc %ld bytes for %s _CRS fail\n", (ulong)crsbuf.length, path_name);
284                 return AE_NO_MEMORY;
285         }
286
287         status = acpi_get_current_resources (handle, &crsbuf);
288         if (ACPI_FAILURE(status)) {
289                 err("acpi_shpchprm: %s _CRS fail=0x%x.\n", path_name, status);
290                 kfree(crsbuf.pointer);
291                 return status;
292         }
293
294         *retbuf = crsbuf.pointer;
295
296         return status;
297 }
298
299 static void free_pci_resource ( struct pci_resource     *aprh)
300 {
301         struct pci_resource     *res, *next;
302
303         for (res = aprh; res; res = next) {
304                 next = res->next;
305                 kfree(res);
306         }
307 }
308
309 static void print_pci_resource ( struct pci_resource    *aprh)
310 {
311         struct pci_resource     *res;
312
313         for (res = aprh; res; res = res->next)
314                 dbg("        base= 0x%x length= 0x%x\n", res->base, res->length);
315 }
316
317 static void print_slot_resources( struct acpi_php_slot  *aps)
318 {
319         if (aps->bus_head) {
320                 dbg("    BUS Resources:\n");
321                 print_pci_resource (aps->bus_head);
322         }
323
324         if (aps->io_head) {
325                 dbg("    IO Resources:\n");
326                 print_pci_resource (aps->io_head);
327         }
328
329         if (aps->mem_head) {
330                 dbg("    MEM Resources:\n");
331                 print_pci_resource (aps->mem_head);
332         }
333
334         if (aps->p_mem_head) {
335                 dbg("    PMEM Resources:\n");
336                 print_pci_resource (aps->p_mem_head);
337         }
338 }
339
340 static void print_pci_resources( struct acpi_bridge     *ab)
341 {
342         if (ab->tbus_head) {
343                 dbg("    Total BUS Resources:\n");
344                 print_pci_resource (ab->tbus_head);
345         }
346         if (ab->bus_head) {
347                 dbg("    BUS Resources:\n");
348                 print_pci_resource (ab->bus_head);
349         }
350
351         if (ab->tio_head) {
352                 dbg("    Total IO Resources:\n");
353                 print_pci_resource (ab->tio_head);
354         }
355         if (ab->io_head) {
356                 dbg("    IO Resources:\n");
357                 print_pci_resource (ab->io_head);
358         }
359
360         if (ab->tmem_head) {
361                 dbg("    Total MEM Resources:\n");
362                 print_pci_resource (ab->tmem_head);
363         }
364         if (ab->mem_head) {
365                 dbg("    MEM Resources:\n");
366                 print_pci_resource (ab->mem_head);
367         }
368
369         if (ab->tp_mem_head) {
370                 dbg("    Total PMEM Resources:\n");
371                 print_pci_resource (ab->tp_mem_head);
372         }
373         if (ab->p_mem_head) {
374                 dbg("    PMEM Resources:\n");
375                 print_pci_resource (ab->p_mem_head);
376         }
377         if (ab->_hpp) {
378                 dbg("    _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
379                 dbg("    _HPP: latency timer  =0x%x\n", ab->_hpp->latency_timer);
380                 dbg("    _HPP: enable SERR    =0x%x\n", ab->_hpp->enable_serr);
381                 dbg("    _HPP: enable PERR    =0x%x\n", ab->_hpp->enable_perr);
382         }
383 }
384
385 static int shpchprm_delete_resource(
386         struct pci_resource **aprh,
387         ulong base,
388         ulong size)
389 {
390         struct pci_resource *res;
391         struct pci_resource *prevnode;
392         struct pci_resource *split_node;
393         ulong tbase;
394
395         shpchp_resource_sort_and_combine(aprh);
396
397         for (res = *aprh; res; res = res->next) {
398                 if (res->base > base)
399                         continue;
400
401                 if ((res->base + res->length) < (base + size))
402                         continue;
403
404                 if (res->base < base) {
405                         tbase = base;
406
407                         if ((res->length - (tbase - res->base)) < size)
408                                 continue;
409
410                         split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
411                         if (!split_node)
412                                 return -ENOMEM;
413
414                         split_node->base = res->base;
415                         split_node->length = tbase - res->base;
416                         res->base = tbase;
417                         res->length -= split_node->length;
418
419                         split_node->next = res->next;
420                         res->next = split_node;
421                 }
422
423                 if (res->length >= size) {
424                         split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
425                         if (!split_node)
426                                 return -ENOMEM;
427
428                         split_node->base = res->base + size;
429                         split_node->length = res->length - size;
430                         res->length = size;
431
432                         split_node->next = res->next;
433                         res->next = split_node;
434                 }
435
436                 if (*aprh == res) {
437                         *aprh = res->next;
438                 } else {
439                         prevnode = *aprh;
440                         while (prevnode->next != res)
441                                 prevnode = prevnode->next;
442
443                         prevnode->next = res->next;
444                 }
445                 res->next = NULL;
446                 kfree(res);
447                 break;
448         }
449
450         return 0;
451 }
452
453 static int shpchprm_delete_resources(
454         struct pci_resource **aprh,
455         struct pci_resource *this
456         )
457 {
458         struct pci_resource *res;
459
460         for (res = this; res; res = res->next)
461                 shpchprm_delete_resource(aprh, res->base, res->length);
462
463         return 0;
464 }
465
466 static int shpchprm_add_resource(
467         struct pci_resource **aprh,
468         ulong base,
469         ulong size)
470 {
471         struct pci_resource *res;
472
473         for (res = *aprh; res; res = res->next) {
474                 if ((res->base + res->length) == base) {
475                         res->length += size;
476                         size = 0L;
477                         break;
478                 }
479                 if (res->next == *aprh)
480                         break;
481         }
482
483         if (size) {
484                 res = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
485                 if (!res) {
486                         err ("acpi_shpchprm: alloc for res fail\n");
487                         return -ENOMEM;
488                 }
489                 memset(res, 0, sizeof (struct pci_resource));
490
491                 res->base = base;
492                 res->length = size;
493                 res->next = *aprh;
494                 *aprh = res;
495         }
496
497         return 0;
498 }
499
500 static int shpchprm_add_resources(
501         struct pci_resource **aprh,
502         struct pci_resource *this
503         )
504 {
505         struct pci_resource *res;
506         int     rc = 0;
507
508         for (res = this; res && !rc; res = res->next)
509                 rc = shpchprm_add_resource(aprh, res->base, res->length);
510
511         return rc;
512 }
513
514 static void acpi_parse_io (
515         struct acpi_bridge              *ab,
516         union acpi_resource_data        *data
517         )
518 {
519         struct acpi_resource_io *dataio;
520         dataio = (struct acpi_resource_io *) data;
521
522         dbg("Io Resource\n");
523         dbg("  %d bit decode\n", ACPI_DECODE_16 == dataio->io_decode ? 16:10);
524         dbg("  Range minimum base: %08X\n", dataio->min_base_address);
525         dbg("  Range maximum base: %08X\n", dataio->max_base_address);
526         dbg("  Alignment: %08X\n", dataio->alignment);
527         dbg("  Range Length: %08X\n", dataio->range_length);
528 }
529
530 static void acpi_parse_fixed_io (
531         struct acpi_bridge              *ab,
532         union acpi_resource_data        *data
533         )
534 {
535         struct acpi_resource_fixed_io  *datafio;
536         datafio = (struct acpi_resource_fixed_io *) data;
537
538         dbg("Fixed Io Resource\n");
539         dbg("  Range base address: %08X", datafio->base_address);
540         dbg("  Range length: %08X", datafio->range_length);
541 }
542
543 static void acpi_parse_address16_32 (
544         struct acpi_bridge              *ab,
545         union acpi_resource_data        *data,
546         acpi_resource_type              id
547         )
548 {
549         /* 
550          * acpi_resource_address16 == acpi_resource_address32
551          * acpi_resource_address16 *data16 = (acpi_resource_address16 *) data;
552          */
553         struct acpi_resource_address32 *data32 = (struct acpi_resource_address32 *) data;
554         struct pci_resource **aprh, **tprh;
555
556         if (id == ACPI_RSTYPE_ADDRESS16)
557                 dbg("acpi_shpchprm:16-Bit Address Space Resource\n");
558         else
559                 dbg("acpi_shpchprm:32-Bit Address Space Resource\n");
560
561         switch (data32->resource_type) {
562         case ACPI_MEMORY_RANGE: 
563                 dbg("  Resource Type: Memory Range\n");
564                 aprh = &ab->mem_head;
565                 tprh = &ab->tmem_head;
566
567                 switch (data32->attribute.memory.cache_attribute) {
568                 case ACPI_NON_CACHEABLE_MEMORY:
569                         dbg("  Type Specific: Noncacheable memory\n");
570                         break; 
571                 case ACPI_CACHABLE_MEMORY:
572                         dbg("  Type Specific: Cacheable memory\n");
573                         break; 
574                 case ACPI_WRITE_COMBINING_MEMORY:
575                         dbg("  Type Specific: Write-combining memory\n");
576                         break; 
577                 case ACPI_PREFETCHABLE_MEMORY:
578                         aprh = &ab->p_mem_head;
579                         dbg("  Type Specific: Prefetchable memory\n");
580                         break; 
581                 default:
582                         dbg("  Type Specific: Invalid cache attribute\n");
583                         break;
584                 }
585
586                 dbg("  Type Specific: Read%s\n", ACPI_READ_WRITE_MEMORY == data32->attribute.memory.read_write_attribute ? "/Write":" Only");
587                 break;
588
589         case ACPI_IO_RANGE: 
590                 dbg("  Resource Type: I/O Range\n");
591                 aprh = &ab->io_head;
592                 tprh = &ab->tio_head;
593
594                 switch (data32->attribute.io.range_attribute) {
595                 case ACPI_NON_ISA_ONLY_RANGES:
596                         dbg("  Type Specific: Non-ISA Io Addresses\n");
597                         break; 
598                 case ACPI_ISA_ONLY_RANGES:
599                         dbg("  Type Specific: ISA Io Addresses\n");
600                         break; 
601                 case ACPI_ENTIRE_RANGE:
602                         dbg("  Type Specific: ISA and non-ISA Io Addresses\n");
603                         break; 
604                 default:
605                         dbg("  Type Specific: Invalid range attribute\n");
606                         break;
607                 }
608                 break;
609
610         case ACPI_BUS_NUMBER_RANGE: 
611                 dbg("  Resource Type: Bus Number Range(fixed)\n");
612                 /* fixup to be compatible with the rest of php driver */
613                 data32->min_address_range++;
614                 data32->address_length--;
615                 aprh = &ab->bus_head;
616                 tprh = &ab->tbus_head;
617                 break; 
618         default: 
619                 dbg("  Resource Type: Invalid resource type. Exiting.\n");
620                 return;
621         }
622
623         dbg("  Resource %s\n", ACPI_CONSUMER == data32->producer_consumer ? "Consumer":"Producer");
624         dbg("  %s decode\n", ACPI_SUB_DECODE == data32->decode ? "Subtractive":"Positive");
625         dbg("  Min address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->min_address_fixed ? "":"not");
626         dbg("  Max address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->max_address_fixed ? "":"not");
627         dbg("  Granularity: %08X\n", data32->granularity);
628         dbg("  Address range min: %08X\n", data32->min_address_range);
629         dbg("  Address range max: %08X\n", data32->max_address_range);
630         dbg("  Address translation offset: %08X\n", data32->address_translation_offset);
631         dbg("  Address Length: %08X\n", data32->address_length);
632
633         if (0xFF != data32->resource_source.index) {
634                 dbg("  Resource Source Index: %X\n", data32->resource_source.index);
635                 /* dbg("  Resource Source: %s\n", data32->resource_source.string_ptr); */
636         }
637
638         shpchprm_add_resource(aprh, data32->min_address_range, data32->address_length);
639 }
640
641 static acpi_status acpi_parse_crs(
642         struct acpi_bridge      *ab,
643         struct acpi_resource    *crsbuf
644         )
645 {
646         acpi_status             status = AE_OK;
647         struct acpi_resource    *resource = crsbuf;
648         u8                      count = 0;
649         u8                      done = 0;
650
651         while (!done) {
652                 dbg("acpi_shpchprm: PCI bus 0x%x Resource structure %x.\n", ab->bus, count++);
653                 switch (resource->id) {
654                 case ACPI_RSTYPE_IRQ:
655                         dbg("Irq -------- Resource\n");
656                         break; 
657                 case ACPI_RSTYPE_DMA:
658                         dbg("DMA -------- Resource\n");
659                         break; 
660                 case ACPI_RSTYPE_START_DPF:
661                         dbg("Start DPF -------- Resource\n");
662                         break; 
663                 case ACPI_RSTYPE_END_DPF:
664                         dbg("End DPF -------- Resource\n");
665                         break; 
666                 case ACPI_RSTYPE_IO:
667                         acpi_parse_io (ab, &resource->data);
668                         break; 
669                 case ACPI_RSTYPE_FIXED_IO:
670                         acpi_parse_fixed_io (ab, &resource->data);
671                         break; 
672                 case ACPI_RSTYPE_VENDOR:
673                         dbg("Vendor -------- Resource\n");
674                         break; 
675                 case ACPI_RSTYPE_END_TAG:
676                         dbg("End_tag -------- Resource\n");
677                         done = 1;
678                         break; 
679                 case ACPI_RSTYPE_MEM24:
680                         dbg("Mem24 -------- Resource\n");
681                         break; 
682                 case ACPI_RSTYPE_MEM32:
683                         dbg("Mem32 -------- Resource\n");
684                         break; 
685                 case ACPI_RSTYPE_FIXED_MEM32:
686                         dbg("Fixed Mem32 -------- Resource\n");
687                         break; 
688                 case ACPI_RSTYPE_ADDRESS16:
689                         acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS16);
690                         break; 
691                 case ACPI_RSTYPE_ADDRESS32:
692                         acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS32);
693                         break; 
694                 case ACPI_RSTYPE_ADDRESS64:
695                         info("Address64 -------- Resource unparsed\n");
696                         break; 
697                 case ACPI_RSTYPE_EXT_IRQ:
698                         dbg("Ext Irq -------- Resource\n");
699                         break; 
700                 default:
701                         dbg("Invalid -------- resource type 0x%x\n", resource->id);
702                         break;
703                 }
704
705                 resource = (struct acpi_resource *) ((char *)resource + resource->length);
706         }
707
708         return status;
709 }
710
711 static acpi_status acpi_get_crs( struct acpi_bridge     *ab)
712 {
713         acpi_status             status;
714         struct acpi_resource    *crsbuf;
715
716         status = acpi_evaluate_crs(ab->handle, &crsbuf);
717         if (ACPI_SUCCESS(status)) {
718                 status = acpi_parse_crs(ab, crsbuf);
719                 kfree(crsbuf);
720
721                 shpchp_resource_sort_and_combine(&ab->bus_head);
722                 shpchp_resource_sort_and_combine(&ab->io_head);
723                 shpchp_resource_sort_and_combine(&ab->mem_head);
724                 shpchp_resource_sort_and_combine(&ab->p_mem_head);
725
726                 shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
727                 shpchprm_add_resources (&ab->tio_head, ab->io_head);
728                 shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
729                 shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
730         }
731
732         return status;
733 }
734
735 /* find acpi_bridge downword from ab.  */
736 static struct acpi_bridge *
737 find_acpi_bridge_by_bus(
738         struct acpi_bridge *ab,
739         int seg,
740         int bus         /* pdev->subordinate->number */
741         )
742 {
743         struct acpi_bridge      *lab = NULL;
744
745         if (!ab)
746                 return NULL;
747
748         if ((ab->bus == bus) && (ab->seg == seg))
749                 return ab;
750
751         if (ab->child)
752                 lab = find_acpi_bridge_by_bus(ab->child, seg, bus);
753
754         if (!lab)
755         if (ab->next)
756                 lab = find_acpi_bridge_by_bus(ab->next, seg, bus);
757
758         return lab;
759 }
760
761 /*
762  * Build a device tree of ACPI PCI Bridges
763  */
764 static void shpchprm_acpi_register_a_bridge (
765         struct acpi_bridge      **head,
766         struct acpi_bridge      *pab,   /* parent bridge to which child bridge is added */
767         struct acpi_bridge      *cab    /* child bridge to add */
768         )
769 {
770         struct acpi_bridge      *lpab;
771         struct acpi_bridge      *lcab;
772
773         lpab = find_acpi_bridge_by_bus(*head, pab->seg, pab->bus);
774         if (!lpab) {
775                 if (!(pab->type & BRIDGE_TYPE_HOST))
776                         warn("PCI parent bridge s:b(%x:%x) not in list.\n", pab->seg, pab->bus);
777                 pab->next = *head;
778                 *head = pab;
779                 lpab = pab;
780         }
781
782         if ((cab->type & BRIDGE_TYPE_HOST) && (pab == cab))
783                 return;
784
785         lcab = find_acpi_bridge_by_bus(*head, cab->seg, cab->bus);
786         if (lcab) {
787                 if ((pab->bus != lcab->parent->bus) || (lcab->bus != cab->bus))
788                         err("PCI child bridge s:b(%x:%x) in list with diff parent.\n", cab->seg, cab->bus);
789                 return;
790         } else
791                 lcab = cab;
792
793         lcab->parent = lpab;
794         lcab->next = lpab->child;
795         lpab->child = lcab;
796 }
797
798 static acpi_status shpchprm_acpi_build_php_slots_callback(
799         acpi_handle     handle,
800         u32             Level,
801         void            *context,
802         void            **retval
803         )
804 {
805         ulong           bus_num;
806         ulong           seg_num;
807         ulong           sun, adr;
808         ulong           padr = 0;
809         acpi_handle             phandle = NULL;
810         struct acpi_bridge      *pab = (struct acpi_bridge *)context;
811         struct acpi_bridge      *lab;
812         acpi_status             status;
813         u8                      *path_name = acpi_path_name(handle);
814
815         /* get _SUN */
816         status = acpi_evaluate_integer(handle, METHOD_NAME__SUN, NULL, &sun);
817         switch(status) {
818         case AE_NOT_FOUND:
819                 return AE_OK;
820         default:
821                 if (ACPI_FAILURE(status)) {
822                         err("acpi_shpchprm:%s _SUN fail=0x%x\n", path_name, status);
823                         return status;
824                 }
825         }
826
827         /* get _ADR. _ADR must exist if _SUN exists */
828         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
829         if (ACPI_FAILURE(status)) {
830                 err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
831                 return status;
832         }
833
834         dbg("acpi_shpchprm:%s sun=0x%08x adr=0x%08x\n", path_name, (u32)sun, (u32)adr);
835
836         status = acpi_get_parent(handle, &phandle);
837         if (ACPI_FAILURE(status)) {
838                 err("acpi_shpchprm:%s get_parent fail=0x%x\n", path_name, status);
839                 return (status);
840         }
841
842         bus_num = pab->bus;
843         seg_num = pab->seg;
844
845         if (pab->bus == bus_num) {
846                 lab = pab;
847         } else {
848                 dbg("WARN: pab is not parent\n");
849                 lab = find_acpi_bridge_by_bus(pab, seg_num, bus_num);
850                 if (!lab) {
851                         dbg("acpi_shpchprm: alloc new P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
852                         lab = (struct acpi_bridge *)kmalloc(sizeof(struct acpi_bridge), GFP_KERNEL);
853                         if (!lab) {
854                                 err("acpi_shpchprm: alloc for ab fail\n");
855                                 return AE_NO_MEMORY;
856                         }
857                         memset(lab, 0, sizeof(struct acpi_bridge));
858
859                         lab->handle = phandle;
860                         lab->pbus = pab->bus;
861                         lab->pdevice = (int)(padr >> 16) & 0xffff;
862                         lab->pfunction = (int)(padr & 0xffff);
863                         lab->bus = (int)bus_num;
864                         lab->scanned = 0;
865                         lab->type = BRIDGE_TYPE_P2P;
866
867                         shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, lab);
868                 } else
869                         dbg("acpi_shpchprm: found P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
870         }
871
872         acpi_add_slot_to_php_slots(lab, (int)bus_num, handle, (u32)adr, (u32)sun);
873         return (status);
874 }
875
876 static int shpchprm_acpi_build_php_slots(
877         struct acpi_bridge      *ab,
878         u32                     depth
879         )
880 {
881         acpi_status     status;
882         u8              *path_name = acpi_path_name(ab->handle);
883
884         /* Walk down this pci bridge to get _SUNs if any behind P2P */
885         status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
886                                 ab->handle,
887                                 depth,
888                                 shpchprm_acpi_build_php_slots_callback,
889                                 ab,
890                                 NULL );
891         if (ACPI_FAILURE(status)) {
892                 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);
893                 return -1;
894         }
895
896         return 0;
897 }
898
899 static void build_a_bridge(
900         struct acpi_bridge      *pab,
901         struct acpi_bridge      *ab
902         )
903 {
904         u8              *path_name = acpi_path_name(ab->handle);
905
906         shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, ab);
907
908         switch (ab->type) {
909         case BRIDGE_TYPE_HOST:
910                 dbg("acpi_shpchprm: Registered PCI HOST Bridge(%02x)    on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
911                         ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
912                 break;
913         case BRIDGE_TYPE_P2P:
914                 dbg("acpi_shpchprm: Registered PCI  P2P Bridge(%02x-%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
915                         ab->pbus, ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
916                 break;
917         };
918
919         /* build any immediate PHP slots under this pci bridge */
920         shpchprm_acpi_build_php_slots(ab, 1);
921 }
922
923 static struct acpi_bridge * add_p2p_bridge(
924         acpi_handle handle,
925         struct acpi_bridge      *pab,   /* parent */
926         ulong   adr
927         )
928 {
929         struct acpi_bridge      *ab;
930         struct pci_dev  *pdev;
931         ulong           devnum, funcnum;
932         u8                      *path_name = acpi_path_name(handle);
933
934         ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
935         if (!ab) {
936                 err("acpi_shpchprm: alloc for ab fail\n");
937                 return NULL;
938         }
939         memset(ab, 0, sizeof(struct acpi_bridge));
940
941         devnum = (adr >> 16) & 0xffff;
942         funcnum = adr & 0xffff;
943
944         pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
945         if (!pdev || !pdev->subordinate) {
946                 err("acpi_shpchprm:%s is not a P2P Bridge\n", path_name);
947                 kfree(ab);
948                 return NULL;
949         }
950
951         ab->handle = handle;
952         ab->seg = pab->seg;
953         ab->pbus = pab->bus;            /* or pdev->bus->number */
954         ab->pdevice = devnum;           /* or PCI_SLOT(pdev->devfn) */
955         ab->pfunction = funcnum;        /* or PCI_FUNC(pdev->devfn) */
956         ab->bus = pdev->subordinate->number;
957         ab->scanned = 0;
958         ab->type = BRIDGE_TYPE_P2P;
959
960         dbg("acpi_shpchprm: P2P(%x-%x) on pci=b:d:f(%x:%x:%x) acpi=b:d:f(%x:%x:%x) [%s]\n",
961                 pab->bus, ab->bus, pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
962                 pab->bus, (u32)devnum, (u32)funcnum, path_name);
963
964         build_a_bridge(pab, ab);
965
966         return ab;
967 }
968
969 static acpi_status scan_p2p_bridge(
970         acpi_handle             handle,
971         u32                     Level,
972         void                    *context,
973         void                    **retval
974         )
975 {
976         struct acpi_bridge      *pab = (struct acpi_bridge *)context;
977         struct acpi_bridge      *ab;
978         acpi_status             status;
979         ulong           adr = 0;
980         u8                      *path_name = acpi_path_name(handle);
981         ulong           devnum, funcnum;
982         struct pci_dev  *pdev;
983
984         /* get device, function */
985         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
986         if (ACPI_FAILURE(status)) {
987                 if (status != AE_NOT_FOUND)
988                         err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
989                 return AE_OK;
990         }
991
992         devnum = (adr >> 16) & 0xffff;
993         funcnum = adr & 0xffff;
994
995         pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
996         if (!pdev)
997                 return AE_OK;
998         if (!pdev->subordinate)
999                 return AE_OK;
1000
1001         ab = add_p2p_bridge(handle, pab, adr);
1002         if (ab) {
1003                 status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
1004                                         handle,
1005                                         (u32)1,
1006                                         scan_p2p_bridge,
1007                                         ab,
1008                                         NULL);
1009                 if (ACPI_FAILURE(status))
1010                         dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
1011         }
1012
1013         return AE_OK;
1014 }
1015
1016 static struct acpi_bridge * add_host_bridge(
1017         acpi_handle handle,
1018         ulong   segnum,
1019         ulong   busnum
1020         )
1021 {
1022         ulong                   adr = 0;
1023         acpi_status             status;
1024         struct acpi_bridge      *ab;
1025         u8                      *path_name = acpi_path_name(handle);
1026
1027         /* get device, function: host br adr is always 0000 though.  */
1028         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
1029         if (ACPI_FAILURE(status)) {
1030                 err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
1031                 return NULL;
1032         }
1033         dbg("acpi_shpchprm: ROOT PCI seg(0x%x)bus(0x%x)dev(0x%x)func(0x%x) [%s]\n", (u32)segnum, (u32)busnum, 
1034                 (u32)(adr >> 16) & 0xffff, (u32)adr & 0xffff, path_name);
1035
1036         ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
1037         if (!ab) {
1038                 err("acpi_shpchprm: alloc for ab fail\n");
1039                 return NULL;
1040         }
1041         memset(ab, 0, sizeof(struct acpi_bridge));
1042
1043         ab->handle = handle;
1044         ab->seg = (int)segnum;
1045         ab->bus = ab->pbus = (int)busnum;
1046         ab->pdevice = (int)(adr >> 16) & 0xffff;
1047         ab->pfunction = (int)(adr & 0xffff);
1048         ab->scanned = 0;
1049         ab->type = BRIDGE_TYPE_HOST;
1050
1051         /* get root pci bridge's current resources */
1052         status = acpi_get_crs(ab);
1053         if (ACPI_FAILURE(status)) {
1054                 err("acpi_shpchprm:%s evaluate _CRS fail=0x%x\n", path_name, status);
1055                 kfree(ab);
1056                 return NULL;
1057         }
1058         build_a_bridge(ab, ab);
1059
1060         return ab;
1061 }
1062
1063 static acpi_status acpi_scan_from_root_pci_callback (
1064         acpi_handle     handle,
1065         u32                     Level,
1066         void            *context,
1067         void            **retval
1068         )
1069 {
1070         ulong           segnum = 0;
1071         ulong           busnum = 0;
1072         acpi_status             status;
1073         struct acpi_bridge      *ab;
1074         u8                      *path_name = acpi_path_name(handle);
1075
1076         /* get bus number of this pci root bridge */
1077         status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &segnum);
1078         if (ACPI_FAILURE(status)) {
1079                 if (status != AE_NOT_FOUND) {
1080                         err("acpi_shpchprm:%s evaluate _SEG fail=0x%x\n", path_name, status);
1081                         return status;
1082                 }
1083                 segnum = 0;
1084         }
1085
1086         /* get bus number of this pci root bridge */
1087         status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL, &busnum);
1088         if (ACPI_FAILURE(status)) {
1089                 err("acpi_shpchprm:%s evaluate _BBN fail=0x%x\n", path_name, status);
1090                 return (status);
1091         }
1092
1093         ab = add_host_bridge(handle, segnum, busnum);
1094         if (ab) {
1095                 status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
1096                                         handle,
1097                                         1,
1098                                         scan_p2p_bridge,
1099                                         ab,
1100                                         NULL);
1101                 if (ACPI_FAILURE(status))
1102                         dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
1103         }
1104
1105         return AE_OK;
1106 }
1107
1108 static int shpchprm_acpi_scan_pci (void)
1109 {
1110         acpi_status     status;
1111
1112         /*
1113          * TBD: traverse LDM device tree with the help of
1114          *  unified ACPI augmented for php device population.
1115          */
1116         status = acpi_get_devices ( PCI_ROOT_HID_STRING,
1117                                 acpi_scan_from_root_pci_callback,
1118                                 NULL,
1119                                 NULL );
1120         if (ACPI_FAILURE(status)) {
1121                 err("acpi_shpchprm:get_device PCI ROOT HID fail=0x%x\n", status);
1122                 return -1;
1123         }
1124
1125         return 0;
1126 }
1127
1128 int shpchprm_init(enum php_ctlr_type ctlr_type)
1129 {
1130         int     rc;
1131
1132         if (ctlr_type != PCI)
1133                 return -ENODEV;
1134
1135         dbg("shpchprm ACPI init <enter>\n");
1136         acpi_bridges_head = NULL;
1137
1138         /* construct PCI bus:device tree of acpi_handles */
1139         rc = shpchprm_acpi_scan_pci();
1140         if (rc)
1141                 return rc;
1142
1143         dbg("shpchprm ACPI init %s\n", (rc)?"fail":"success");
1144         return rc;
1145 }
1146
1147 static void free_a_slot(struct acpi_php_slot *aps)
1148 {
1149         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);
1150
1151         free_pci_resource (aps->io_head);
1152         free_pci_resource (aps->bus_head);
1153         free_pci_resource (aps->mem_head);
1154         free_pci_resource (aps->p_mem_head);
1155
1156         kfree(aps);
1157 }
1158
1159 static void free_a_bridge( struct acpi_bridge   *ab)
1160 {
1161         struct acpi_php_slot    *aps, *next;
1162
1163         switch (ab->type) {
1164         case BRIDGE_TYPE_HOST:
1165                 dbg("Free ACPI PCI HOST Bridge(%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
1166                         ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
1167                 break;
1168         case BRIDGE_TYPE_P2P:
1169                 dbg("Free ACPI PCI P2P Bridge(%x-%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
1170                         ab->pbus, ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
1171                 break;
1172         };
1173
1174         /* free slots first */
1175         for (aps = ab->slots; aps; aps = next) {
1176                 next = aps->next;
1177                 free_a_slot(aps);
1178         }
1179
1180         free_pci_resource (ab->io_head);
1181         free_pci_resource (ab->tio_head);
1182         free_pci_resource (ab->bus_head);
1183         free_pci_resource (ab->tbus_head);
1184         free_pci_resource (ab->mem_head);
1185         free_pci_resource (ab->tmem_head);
1186         free_pci_resource (ab->p_mem_head);
1187         free_pci_resource (ab->tp_mem_head);
1188
1189         kfree(ab);
1190 }
1191
1192 static void shpchprm_free_bridges ( struct acpi_bridge  *ab)
1193 {
1194         if (!ab)
1195                 return;
1196
1197         if (ab->child)
1198                 shpchprm_free_bridges (ab->child);
1199
1200         if (ab->next)
1201                 shpchprm_free_bridges (ab->next);
1202
1203         free_a_bridge(ab);
1204 }
1205
1206 void shpchprm_cleanup(void)
1207 {
1208         shpchprm_free_bridges (acpi_bridges_head);
1209 }
1210
1211 static int get_number_of_slots (
1212         struct acpi_bridge      *ab,
1213         int                             selfonly
1214         )
1215 {
1216         struct acpi_php_slot    *aps;
1217         int     prev_slot = -1;
1218         int     slot_num = 0;
1219
1220         for ( aps = ab->slots; aps; aps = aps->next)
1221                 if (aps->dev != prev_slot) {
1222                         prev_slot = aps->dev;
1223                         slot_num++;
1224                 }
1225
1226         if (ab->child)
1227                 slot_num += get_number_of_slots (ab->child, 0);
1228
1229         if (selfonly)
1230                 return slot_num;
1231
1232         if (ab->next)
1233                 slot_num += get_number_of_slots (ab->next, 0);
1234
1235         return slot_num;
1236 }
1237
1238 static int print_acpi_resources (struct acpi_bridge     *ab)
1239 {
1240         struct acpi_php_slot    *aps;
1241         int     i;
1242
1243         switch (ab->type) {
1244         case BRIDGE_TYPE_HOST:
1245                 dbg("PCI HOST Bridge (%x) [%s]\n", ab->bus, acpi_path_name(ab->handle));
1246                 break;
1247         case BRIDGE_TYPE_P2P:
1248                 dbg("PCI P2P Bridge (%x-%x) [%s]\n", ab->pbus, ab->bus, acpi_path_name(ab->handle));
1249                 break;
1250         };
1251
1252         print_pci_resources (ab);
1253
1254         for ( i = -1, aps = ab->slots; aps; aps = aps->next) {
1255                 if (aps->dev == i)
1256                         continue;
1257                 dbg("  Slot sun(%x) s:b:d:f(%02x:%02x:%02x:%02x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
1258                 print_slot_resources(aps);
1259                 i = aps->dev;
1260         }
1261
1262         if (ab->child)
1263                 print_acpi_resources (ab->child);
1264
1265         if (ab->next)
1266                 print_acpi_resources (ab->next);
1267
1268         return 0;
1269 }
1270
1271 int shpchprm_print_pirt(void)
1272 {
1273         dbg("SHPCHPRM ACPI Slots\n");
1274         if (acpi_bridges_head)
1275                 print_acpi_resources (acpi_bridges_head);
1276         return 0;
1277 }
1278
1279 static struct acpi_php_slot * get_acpi_slot (
1280         struct acpi_bridge *ab,
1281         u32 sun
1282         )
1283 {
1284         struct acpi_php_slot    *aps = NULL;
1285
1286         for ( aps = ab->slots; aps; aps = aps->next)
1287                 if (aps->sun == sun)
1288                         return aps;
1289
1290         if (!aps && ab->child) {
1291                 aps = (struct acpi_php_slot *)get_acpi_slot (ab->child, sun);
1292                 if (aps)
1293                         return aps;
1294         }
1295
1296         if (!aps && ab->next) {
1297                 aps = (struct acpi_php_slot *)get_acpi_slot (ab->next, sun);
1298                 if (aps)
1299                         return aps;
1300         }
1301
1302         return aps;
1303
1304 }
1305
1306 #if 0
1307 static void * shpchprm_get_slot(struct slot *slot)
1308 {
1309         struct acpi_bridge      *ab = acpi_bridges_head;
1310         struct acpi_php_slot    *aps = get_acpi_slot (ab, slot->number);
1311
1312         aps->slot = slot;
1313
1314         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);
1315
1316         return (void *)aps;
1317 }
1318 #endif
1319
1320 static void shpchprm_dump_func_res( struct pci_func *fun)
1321 {
1322         struct pci_func *func = fun;
1323
1324         if (func->bus_head) {
1325                 dbg(":    BUS Resources:\n");
1326                 print_pci_resource (func->bus_head);
1327         }
1328         if (func->io_head) {
1329                 dbg(":    IO Resources:\n");
1330                 print_pci_resource (func->io_head);
1331         }
1332         if (func->mem_head) {
1333                 dbg(":    MEM Resources:\n");
1334                 print_pci_resource (func->mem_head);
1335         }
1336         if (func->p_mem_head) {
1337                 dbg(":    PMEM Resources:\n");
1338                 print_pci_resource (func->p_mem_head);
1339         }
1340 }
1341
1342 static void shpchprm_dump_ctrl_res( struct controller *ctlr)
1343 {
1344         struct controller *ctrl = ctlr;
1345
1346         if (ctrl->bus_head) {
1347                 dbg(":    BUS Resources:\n");
1348                 print_pci_resource (ctrl->bus_head);
1349         }
1350         if (ctrl->io_head) {
1351                 dbg(":    IO Resources:\n");
1352                 print_pci_resource (ctrl->io_head);
1353         }
1354         if (ctrl->mem_head) {
1355                 dbg(":    MEM Resources:\n");
1356                 print_pci_resource (ctrl->mem_head);
1357         }
1358         if (ctrl->p_mem_head) {
1359                 dbg(":    PMEM Resources:\n");
1360                 print_pci_resource (ctrl->p_mem_head);
1361         }
1362 }
1363
1364 static int shpchprm_get_used_resources (
1365         struct controller *ctrl,
1366         struct pci_func *func
1367         )
1368 {
1369         return shpchp_save_used_resources (ctrl, func, !DISABLE_CARD);
1370 }
1371
1372 static int configure_existing_function(
1373         struct controller *ctrl,
1374         struct pci_func *func
1375         )
1376 {
1377         int rc;
1378
1379         /* see how much resources the func has used. */
1380         rc = shpchprm_get_used_resources (ctrl, func);
1381
1382         if (!rc) {
1383                 /* subtract the resources used by the func from ctrl resources */
1384                 rc  = shpchprm_delete_resources (&ctrl->bus_head, func->bus_head);
1385                 rc |= shpchprm_delete_resources (&ctrl->io_head, func->io_head);
1386                 rc |= shpchprm_delete_resources (&ctrl->mem_head, func->mem_head);
1387                 rc |= shpchprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
1388                 if (rc)
1389                         warn("aCEF: cannot del used resources\n");
1390         } else
1391                 err("aCEF: cannot get used resources\n");
1392
1393         return rc;
1394 }
1395
1396 static int bind_pci_resources_to_slots ( struct controller *ctrl)
1397 {
1398         struct pci_func *func, new_func;
1399         int busn = ctrl->slot_bus;
1400         int devn, funn;
1401         u32     vid;
1402
1403         for (devn = 0; devn < 32; devn++) {
1404                 for (funn = 0; funn < 8; funn++) {
1405                         /*
1406                         if (devn == ctrl->device && funn == ctrl->function)
1407                                 continue;
1408                         */
1409                         /* find out if this entry is for an occupied slot */
1410                         vid = 0xFFFFFFFF;
1411                         pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
1412
1413                         if (vid != 0xFFFFFFFF) {
1414                                 func = shpchp_slot_find(busn, devn, funn);
1415                                 if (!func) {
1416                                         memset(&new_func, 0, sizeof(struct pci_func));
1417                                         new_func.bus = busn;
1418                                         new_func.device = devn;
1419                                         new_func.function = funn;
1420                                         new_func.is_a_board = 1;
1421                                         configure_existing_function(ctrl, &new_func);
1422                                         shpchprm_dump_func_res(&new_func);
1423                                 } else {
1424                                         configure_existing_function(ctrl, func);
1425                                         shpchprm_dump_func_res(func);
1426                                 }
1427                                 dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
1428                         }
1429                 }
1430         }
1431
1432         return 0;
1433 }
1434
1435 static int bind_pci_resources(
1436         struct controller       *ctrl,
1437         struct acpi_bridge      *ab
1438         )
1439 {
1440         int     status = 0;
1441
1442         if (ab->bus_head) {
1443                 dbg("bapr:  BUS Resources add on PCI 0x%x\n", ab->bus);
1444                 status = shpchprm_add_resources (&ctrl->bus_head, ab->bus_head);
1445                 if (shpchprm_delete_resources (&ab->bus_head, ctrl->bus_head))
1446                         warn("bapr:  cannot sub BUS Resource on PCI 0x%x\n", ab->bus);
1447                 if (status) {
1448                         err("bapr:  BUS Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1449                         return status;
1450                 }
1451         } else
1452                 info("bapr:  No BUS Resource on PCI 0x%x.\n", ab->bus);
1453
1454         if (ab->io_head) {
1455                 dbg("bapr:  IO Resources add on PCI 0x%x\n", ab->bus);
1456                 status = shpchprm_add_resources (&ctrl->io_head, ab->io_head);
1457                 if (shpchprm_delete_resources (&ab->io_head, ctrl->io_head))
1458                         warn("bapr:  cannot sub IO Resource on PCI 0x%x\n", ab->bus);
1459                 if (status) {
1460                         err("bapr:  IO Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1461                         return status;
1462                 }
1463         } else
1464                 info("bapr:  No  IO Resource on PCI 0x%x.\n", ab->bus);
1465
1466         if (ab->mem_head) {
1467                 dbg("bapr:  MEM Resources add on PCI 0x%x\n", ab->bus);
1468                 status = shpchprm_add_resources (&ctrl->mem_head, ab->mem_head);
1469                 if (shpchprm_delete_resources (&ab->mem_head, ctrl->mem_head))
1470                         warn("bapr:  cannot sub MEM Resource on PCI 0x%x\n", ab->bus);
1471                 if (status) {
1472                         err("bapr:  MEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1473                         return status;
1474                 }
1475         } else
1476                 info("bapr:  No MEM Resource on PCI 0x%x.\n", ab->bus);
1477
1478         if (ab->p_mem_head) {
1479                 dbg("bapr:  PMEM Resources add on PCI 0x%x\n", ab->bus);
1480                 status = shpchprm_add_resources (&ctrl->p_mem_head, ab->p_mem_head);
1481                 if (shpchprm_delete_resources (&ab->p_mem_head, ctrl->p_mem_head))
1482                         warn("bapr:  cannot sub PMEM Resource on PCI 0x%x\n", ab->bus);
1483                 if (status) {
1484                         err("bapr:  PMEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
1485                         return status;
1486                 }
1487         } else
1488                 info("bapr:  No PMEM Resource on PCI 0x%x.\n", ab->bus);
1489
1490         return status;
1491 }
1492
1493 static int no_pci_resources( struct acpi_bridge *ab)
1494 {
1495         return !(ab->p_mem_head || ab->mem_head || ab->io_head || ab->bus_head);
1496 }
1497
1498 static int find_pci_bridge_resources (
1499         struct controller *ctrl,
1500         struct acpi_bridge *ab
1501         )
1502 {
1503         int     rc = 0;
1504         struct pci_func func;
1505
1506         memset(&func, 0, sizeof(struct pci_func));
1507
1508         func.bus = ab->pbus;
1509         func.device = ab->pdevice;
1510         func.function = ab->pfunction;
1511         func.is_a_board = 1;
1512
1513         /* Get used resources for this PCI bridge */
1514         rc = shpchp_save_used_resources (ctrl, &func, !DISABLE_CARD);
1515
1516         ab->io_head = func.io_head;
1517         ab->mem_head = func.mem_head;
1518         ab->p_mem_head = func.p_mem_head;
1519         ab->bus_head = func.bus_head;
1520         if (ab->bus_head)
1521                 shpchprm_delete_resource(&ab->bus_head, ctrl->bus, 1);
1522
1523         return rc;
1524 }
1525
1526 static int get_pci_resources_from_bridge(
1527         struct controller *ctrl,
1528         struct acpi_bridge *ab
1529         )
1530 {
1531         int     rc = 0;
1532
1533         dbg("grfb:  Get Resources for PCI 0x%x from actual PCI bridge 0x%x.\n", ctrl->bus, ab->bus);
1534
1535         rc = find_pci_bridge_resources (ctrl, ab);
1536
1537         shpchp_resource_sort_and_combine(&ab->bus_head);
1538         shpchp_resource_sort_and_combine(&ab->io_head);
1539         shpchp_resource_sort_and_combine(&ab->mem_head);
1540         shpchp_resource_sort_and_combine(&ab->p_mem_head);
1541
1542         shpchprm_add_resources (&ab->tbus_head, ab->bus_head);
1543         shpchprm_add_resources (&ab->tio_head, ab->io_head);
1544         shpchprm_add_resources (&ab->tmem_head, ab->mem_head);
1545         shpchprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
1546
1547         return rc;
1548 }
1549
1550 static int get_pci_resources(
1551         struct controller       *ctrl,
1552         struct acpi_bridge      *ab
1553         )
1554 {
1555         int     rc = 0;
1556
1557         if (no_pci_resources(ab)) {
1558                 dbg("spbr:PCI 0x%x has no resources. Get parent resources.\n", ab->bus);
1559                 rc = get_pci_resources_from_bridge(ctrl, ab);
1560         }
1561
1562         return rc;
1563 }
1564
1565 int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
1566 {
1567         int offset = devnum - ctrl->slot_device_offset;
1568
1569         dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
1570         *sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc *offset);
1571         return 0;
1572 }
1573
1574 /*
1575  * Get resources for this ctrl.
1576  *  1. get total resources from ACPI _CRS or bridge (this ctrl)
1577  *  2. find used resources of existing adapters
1578  *      3. subtract used resources from total resources
1579  */
1580 int shpchprm_find_available_resources( struct controller *ctrl)
1581 {
1582         int rc = 0;
1583         struct acpi_bridge      *ab;
1584
1585         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->pci_dev->subordinate->number);
1586         if (!ab) {
1587                 err("pfar:cannot locate acpi bridge of PCI 0x%x.\n", ctrl->pci_dev->subordinate->number);
1588                 return -1;
1589         }
1590         if (no_pci_resources(ab)) {
1591                 rc = get_pci_resources(ctrl, ab);
1592                 if (rc) {
1593                         err("pfar:cannot get pci resources of PCI 0x%x.\n",ctrl->pci_dev->subordinate->number);
1594                         return -1;
1595                 }
1596         }
1597
1598         rc = bind_pci_resources(ctrl, ab);
1599         dbg("pfar:pre-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
1600         shpchprm_dump_ctrl_res(ctrl);
1601
1602         bind_pci_resources_to_slots (ctrl);
1603
1604         dbg("pfar:post-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
1605         shpchprm_dump_ctrl_res(ctrl);
1606
1607         return rc;
1608 }
1609
1610 int shpchprm_set_hpp(
1611         struct controller *ctrl,
1612         struct pci_func *func,
1613         u8      card_type
1614         )
1615 {
1616         struct acpi_bridge      *ab;
1617         struct pci_bus lpci_bus, *pci_bus;
1618         int                             rc = 0;
1619         unsigned int    devfn;
1620         u8                              cls= 0x08;      /* default cache line size      */
1621         u8                              lt = 0x40;      /* default latency timer        */
1622         u8                              ep = 0;
1623         u8                              es = 0;
1624
1625         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
1626         pci_bus = &lpci_bus;
1627         pci_bus->number = func->bus;
1628         devfn = PCI_DEVFN(func->device, func->function);
1629
1630         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
1631
1632         if (ab) {
1633                 if (ab->_hpp) {
1634                         lt  = (u8)ab->_hpp->latency_timer;
1635                         cls = (u8)ab->_hpp->cache_line_size;
1636                         ep  = (u8)ab->_hpp->enable_perr;
1637                         es  = (u8)ab->_hpp->enable_serr;
1638                 } else
1639                         dbg("_hpp: no _hpp for B/D/F=%#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
1640         } else
1641                 dbg("_hpp: no acpi bridge for B/D/F = %#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
1642
1643
1644         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
1645                 /* set subordinate Latency Timer */
1646                 rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, lt);
1647         }
1648
1649         /* set base Latency Timer */
1650         rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, lt);
1651         dbg("  set latency timer  =0x%02x: %x\n", lt, rc);
1652
1653         rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, cls);
1654         dbg("  set cache_line_size=0x%02x: %x\n", cls, rc);
1655
1656         return rc;
1657 }
1658
1659 void shpchprm_enable_card(
1660         struct controller *ctrl,
1661         struct pci_func *func,
1662         u8 card_type)
1663 {
1664         u16 command, cmd, bcommand, bcmd;
1665         struct pci_bus lpci_bus, *pci_bus;
1666         struct acpi_bridge      *ab;
1667         unsigned int devfn;
1668         int rc;
1669
1670         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
1671         pci_bus = &lpci_bus;
1672         pci_bus->number = func->bus;
1673         devfn = PCI_DEVFN(func->device, func->function);
1674
1675         rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
1676
1677         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
1678                 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
1679         }
1680
1681         cmd = command  = command | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
1682                 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
1683         bcmd = bcommand  = bcommand | PCI_BRIDGE_CTL_NO_ISA;
1684
1685         ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
1686         if (ab) {
1687                 if (ab->_hpp) {
1688                         if (ab->_hpp->enable_perr) {
1689                                 command |= PCI_COMMAND_PARITY;
1690                                 bcommand |= PCI_BRIDGE_CTL_PARITY;
1691                         } else {
1692                                 command &= ~PCI_COMMAND_PARITY;
1693                                 bcommand &= ~PCI_BRIDGE_CTL_PARITY;
1694                         }
1695                         if (ab->_hpp->enable_serr) {
1696                                 command |= PCI_COMMAND_SERR;
1697                                 bcommand |= PCI_BRIDGE_CTL_SERR;
1698                         } else {
1699                                 command &= ~PCI_COMMAND_SERR;
1700                                 bcommand &= ~PCI_BRIDGE_CTL_SERR;
1701                         }
1702                 } else
1703                         dbg("no _hpp for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
1704         } else
1705                 dbg("no acpi bridge for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
1706
1707         if (command != cmd) {
1708                 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
1709         }
1710         if ((card_type == PCI_HEADER_TYPE_BRIDGE) && (bcommand != bcmd)) {
1711                 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
1712         }
1713 }
1714