commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / pci / hotplug / ibmphp_ebda.c
1 /*
2  * IBM Hot Plug Controller Driver
3  *
4  * Written By: Tong Yu, IBM Corporation
5  *
6  * Copyright (c) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (c) 2001-2003 IBM Corp.
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <gregkh@us.ibm.com>
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/mm.h>
34 #include <linux/slab.h>
35 #include <linux/pci.h>
36 #include <linux/list.h>
37 #include <linux/init.h>
38 #include "ibmphp.h"
39
40 /*
41  * POST builds data blocks(in this data block definition, a char-1
42  * byte, short(or word)-2 byte, long(dword)-4 byte) in the Extended
43  * BIOS Data Area which describe the configuration of the hot-plug
44  * controllers and resources used by the PCI Hot-Plug devices.
45  *
46  * This file walks EBDA, maps data block from physical addr,
47  * reconstruct linked lists about all system resource(MEM, PFM, IO)
48  * already assigned by POST, as well as linked lists about hot plug
49  * controllers (ctlr#, slot#, bus&slot features...)
50  */
51
52 /* Global lists */
53 LIST_HEAD (ibmphp_ebda_pci_rsrc_head);
54 LIST_HEAD (ibmphp_slot_head);
55
56 /* Local variables */
57 static struct ebda_hpc_list *hpc_list_ptr;
58 static struct ebda_rsrc_list *rsrc_list_ptr;
59 static struct rio_table_hdr *rio_table_ptr = NULL;
60 static LIST_HEAD (ebda_hpc_head);
61 static LIST_HEAD (bus_info_head);
62 static LIST_HEAD (rio_vg_head);
63 static LIST_HEAD (rio_lo_head);
64 static LIST_HEAD (opt_vg_head);
65 static LIST_HEAD (opt_lo_head);
66 static void *io_mem;
67
68 /* Local functions */
69 static int ebda_rsrc_controller (void);
70 static int ebda_rsrc_rsrc (void);
71 static int ebda_rio_table (void);
72
73 static struct ebda_hpc_list * __init alloc_ebda_hpc_list (void)
74 {
75         struct ebda_hpc_list *list;
76
77         list = kmalloc (sizeof (struct ebda_hpc_list), GFP_KERNEL);
78         if (!list)
79                 return NULL;
80         memset (list, 0, sizeof (*list));
81         return list;
82 }
83
84 static struct controller *alloc_ebda_hpc (u32 slot_count, u32 bus_count)
85 {
86         struct controller *controller;
87         struct ebda_hpc_slot *slots;
88         struct ebda_hpc_bus *buses;
89
90         controller = kmalloc (sizeof (struct controller), GFP_KERNEL);
91         if (!controller)
92                 return NULL;
93         memset (controller, 0, sizeof (*controller));
94
95         slots = kmalloc (sizeof (struct ebda_hpc_slot) * slot_count, GFP_KERNEL);
96         if (!slots) {
97                 kfree (controller);
98                 return NULL;
99         }
100         memset (slots, 0, sizeof (*slots) * slot_count);
101         controller->slots = slots;
102
103         buses = kmalloc (sizeof (struct ebda_hpc_bus) * bus_count, GFP_KERNEL);
104         if (!buses) {
105                 kfree (controller->slots);
106                 kfree (controller);
107                 return NULL;
108         }
109         memset (buses, 0, sizeof (*buses) * bus_count);
110         controller->buses = buses;
111
112         return controller;
113 }
114
115 static void free_ebda_hpc (struct controller *controller)
116 {
117         kfree (controller->slots);
118         controller->slots = NULL;
119         kfree (controller->buses);
120         controller->buses = NULL;
121         controller->ctrl_dev = NULL;
122         kfree (controller);
123 }
124
125 static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list (void)
126 {
127         struct ebda_rsrc_list *list;
128
129         list = kmalloc (sizeof (struct ebda_rsrc_list), GFP_KERNEL);
130         if (!list)
131                 return NULL;
132         memset (list, 0, sizeof (*list));
133         return list;
134 }
135
136 static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc (void)
137 {
138         struct ebda_pci_rsrc *resource;
139
140         resource = kmalloc (sizeof (struct ebda_pci_rsrc), GFP_KERNEL);
141         if (!resource)
142                 return NULL;
143         memset (resource, 0, sizeof (*resource));
144         return resource;
145 }
146
147 static void __init print_bus_info (void)
148 {
149         struct bus_info *ptr;
150         struct list_head *ptr1;
151         
152         list_for_each (ptr1, &bus_info_head) {
153                 ptr = list_entry (ptr1, struct bus_info, bus_info_list);
154                 debug ("%s - slot_min = %x\n", __FUNCTION__, ptr->slot_min);
155                 debug ("%s - slot_max = %x\n", __FUNCTION__, ptr->slot_max);
156                 debug ("%s - slot_count = %x\n", __FUNCTION__, ptr->slot_count);
157                 debug ("%s - bus# = %x\n", __FUNCTION__, ptr->busno);
158                 debug ("%s - current_speed = %x\n", __FUNCTION__, ptr->current_speed);
159                 debug ("%s - controller_id = %x\n", __FUNCTION__, ptr->controller_id);
160                 
161                 debug ("%s - slots_at_33_conv = %x\n", __FUNCTION__, ptr->slots_at_33_conv);
162                 debug ("%s - slots_at_66_conv = %x\n", __FUNCTION__, ptr->slots_at_66_conv);
163                 debug ("%s - slots_at_66_pcix = %x\n", __FUNCTION__, ptr->slots_at_66_pcix);
164                 debug ("%s - slots_at_100_pcix = %x\n", __FUNCTION__, ptr->slots_at_100_pcix);
165                 debug ("%s - slots_at_133_pcix = %x\n", __FUNCTION__, ptr->slots_at_133_pcix);
166
167         }
168 }
169
170 static void print_lo_info (void)
171 {
172         struct rio_detail *ptr;
173         struct list_head *ptr1;
174         debug ("print_lo_info ---- \n");        
175         list_for_each (ptr1, &rio_lo_head) {
176                 ptr = list_entry (ptr1, struct rio_detail, rio_detail_list);
177                 debug ("%s - rio_node_id = %x\n", __FUNCTION__, ptr->rio_node_id);
178                 debug ("%s - rio_type = %x\n", __FUNCTION__, ptr->rio_type);
179                 debug ("%s - owner_id = %x\n", __FUNCTION__, ptr->owner_id);
180                 debug ("%s - first_slot_num = %x\n", __FUNCTION__, ptr->first_slot_num);
181                 debug ("%s - wpindex = %x\n", __FUNCTION__, ptr->wpindex);
182                 debug ("%s - chassis_num = %x\n", __FUNCTION__, ptr->chassis_num);
183
184         }
185 }
186
187 static void print_vg_info (void)
188 {
189         struct rio_detail *ptr;
190         struct list_head *ptr1;
191         debug ("%s --- \n", __FUNCTION__);
192         list_for_each (ptr1, &rio_vg_head) {
193                 ptr = list_entry (ptr1, struct rio_detail, rio_detail_list);
194                 debug ("%s - rio_node_id = %x\n", __FUNCTION__, ptr->rio_node_id);
195                 debug ("%s - rio_type = %x\n", __FUNCTION__, ptr->rio_type);
196                 debug ("%s - owner_id = %x\n", __FUNCTION__, ptr->owner_id);
197                 debug ("%s - first_slot_num = %x\n", __FUNCTION__, ptr->first_slot_num);
198                 debug ("%s - wpindex = %x\n", __FUNCTION__, ptr->wpindex);
199                 debug ("%s - chassis_num = %x\n", __FUNCTION__, ptr->chassis_num);
200
201         }
202 }
203
204 static void __init print_ebda_pci_rsrc (void)
205 {
206         struct ebda_pci_rsrc *ptr;
207         struct list_head *ptr1;
208
209         list_for_each (ptr1, &ibmphp_ebda_pci_rsrc_head) {
210                 ptr = list_entry (ptr1, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
211                 debug ("%s - rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n", 
212                         __FUNCTION__, ptr->rsrc_type ,ptr->bus_num, ptr->dev_fun,ptr->start_addr, ptr->end_addr);
213         }
214 }
215
216 static void __init print_ibm_slot (void)
217 {
218         struct slot *ptr;
219         struct list_head *ptr1;
220
221         list_for_each (ptr1, &ibmphp_slot_head) {
222                 ptr = list_entry (ptr1, struct slot, ibm_slot_list);
223                 debug ("%s - slot_number: %x \n", __FUNCTION__, ptr->number); 
224         }
225 }
226
227 static void __init print_opt_vg (void)
228 {
229         struct opt_rio *ptr;
230         struct list_head *ptr1;
231         debug ("%s --- \n", __FUNCTION__);
232         list_for_each (ptr1, &opt_vg_head) {
233                 ptr = list_entry (ptr1, struct opt_rio, opt_rio_list);
234                 debug ("%s - rio_type %x \n", __FUNCTION__, ptr->rio_type); 
235                 debug ("%s - chassis_num: %x \n", __FUNCTION__, ptr->chassis_num); 
236                 debug ("%s - first_slot_num: %x \n", __FUNCTION__, ptr->first_slot_num); 
237                 debug ("%s - middle_num: %x \n", __FUNCTION__, ptr->middle_num); 
238         }
239 }
240
241 static void __init print_ebda_hpc (void)
242 {
243         struct controller *hpc_ptr;
244         struct list_head *ptr1;
245         u16 index;
246
247         list_for_each (ptr1, &ebda_hpc_head) {
248
249                 hpc_ptr = list_entry (ptr1, struct controller, ebda_hpc_list); 
250
251                 for (index = 0; index < hpc_ptr->slot_count; index++) {
252                         debug ("%s - physical slot#: %x\n", __FUNCTION__, hpc_ptr->slots[index].slot_num);
253                         debug ("%s - pci bus# of the slot: %x\n", __FUNCTION__, hpc_ptr->slots[index].slot_bus_num);
254                         debug ("%s - index into ctlr addr: %x\n", __FUNCTION__, hpc_ptr->slots[index].ctl_index);
255                         debug ("%s - cap of the slot: %x\n", __FUNCTION__, hpc_ptr->slots[index].slot_cap);
256                 }
257
258                 for (index = 0; index < hpc_ptr->bus_count; index++) {
259                         debug ("%s - bus# of each bus controlled by this ctlr: %x\n", __FUNCTION__, hpc_ptr->buses[index].bus_num);
260                 }
261
262                 debug ("%s - type of hpc: %x\n", __FUNCTION__, hpc_ptr->ctlr_type);
263                 switch (hpc_ptr->ctlr_type) {
264                 case 1:
265                         debug ("%s - bus: %x\n", __FUNCTION__, hpc_ptr->u.pci_ctlr.bus);
266                         debug ("%s - dev_fun: %x\n", __FUNCTION__, hpc_ptr->u.pci_ctlr.dev_fun);
267                         debug ("%s - irq: %x\n", __FUNCTION__, hpc_ptr->irq);
268                         break;
269
270                 case 0:
271                         debug ("%s - io_start: %x\n", __FUNCTION__, hpc_ptr->u.isa_ctlr.io_start);
272                         debug ("%s - io_end: %x\n", __FUNCTION__, hpc_ptr->u.isa_ctlr.io_end);
273                         debug ("%s - irq: %x\n", __FUNCTION__, hpc_ptr->irq);
274                         break;
275
276                 case 2:
277                 case 4:
278                         debug ("%s - wpegbbar: %lx\n", __FUNCTION__, hpc_ptr->u.wpeg_ctlr.wpegbbar);
279                         debug ("%s - i2c_addr: %x\n", __FUNCTION__, hpc_ptr->u.wpeg_ctlr.i2c_addr);
280                         debug ("%s - irq: %x\n", __FUNCTION__, hpc_ptr->irq);
281                         break;
282                 }
283         }
284 }
285
286 int __init ibmphp_access_ebda (void)
287 {
288         u8 format, num_ctlrs, rio_complete, hs_complete;
289         u16 ebda_seg, num_entries, next_offset, offset, blk_id, sub_addr, rc, re, rc_id, re_id, base;
290
291
292         rio_complete = 0;
293         hs_complete = 0;
294
295         io_mem = ioremap ((0x40 << 4) + 0x0e, 2);
296         if (!io_mem )
297                 return -ENOMEM;
298         ebda_seg = readw (io_mem);
299         iounmap (io_mem);
300         debug ("returned ebda segment: %x\n", ebda_seg);
301         
302         io_mem = ioremap (ebda_seg<<4, 65000);
303         if (!io_mem )
304                 return -ENOMEM;
305         next_offset = 0x180;
306
307         for (;;) {
308                 offset = next_offset;
309                 next_offset = readw (io_mem + offset);  /* offset of next blk */
310
311                 offset += 2;
312                 if (next_offset == 0)   /* 0 indicate it's last blk */
313                         break;
314                 blk_id = readw (io_mem + offset);       /* this blk id */
315
316                 offset += 2;
317                 /* check if it is hot swap block or rio block */
318                 if (blk_id != 0x4853 && blk_id != 0x4752)
319                         continue;
320                 /* found hs table */
321                 if (blk_id == 0x4853) {
322                         debug ("now enter hot swap block---\n");
323                         debug ("hot blk id: %x\n", blk_id);
324                         format = readb (io_mem + offset);
325
326                         offset += 1;
327                         if (format != 4) {
328                                 iounmap (io_mem);
329                                 return -ENODEV;
330                         }
331                         debug ("hot blk format: %x\n", format);
332                         /* hot swap sub blk */
333                         base = offset;
334
335                         sub_addr = base;
336                         re = readw (io_mem + sub_addr); /* next sub blk */
337
338                         sub_addr += 2;
339                         rc_id = readw (io_mem + sub_addr);      /* sub blk id */
340
341                         sub_addr += 2;
342                         if (rc_id != 0x5243) {
343                                 iounmap (io_mem);
344                                 return -ENODEV;
345                         }
346                         /* rc sub blk signature  */
347                         num_ctlrs = readb (io_mem + sub_addr);
348
349                         sub_addr += 1;
350                         hpc_list_ptr = alloc_ebda_hpc_list ();
351                         if (!hpc_list_ptr) {
352                                 iounmap (io_mem);
353                                 return -ENOMEM;
354                         }
355                         hpc_list_ptr->format = format;
356                         hpc_list_ptr->num_ctlrs = num_ctlrs;
357                         hpc_list_ptr->phys_addr = sub_addr;     /*  offset of RSRC_CONTROLLER blk */
358                         debug ("info about hpc descriptor---\n");
359                         debug ("hot blk format: %x\n", format);
360                         debug ("num of controller: %x\n", num_ctlrs);
361                         debug ("offset of hpc data structure enteries: %x\n ", sub_addr);
362
363                         sub_addr = base + re;   /* re sub blk */
364                         rc = readw (io_mem + sub_addr); /* next sub blk */
365
366                         sub_addr += 2;
367                         re_id = readw (io_mem + sub_addr);      /* sub blk id */
368
369                         sub_addr += 2;
370                         if (re_id != 0x5245) {
371                                 iounmap (io_mem);
372                                 return -ENODEV;
373                         }
374
375                         /* signature of re */
376                         num_entries = readw (io_mem + sub_addr);
377
378                         sub_addr += 2;  /* offset of RSRC_ENTRIES blk */
379                         rsrc_list_ptr = alloc_ebda_rsrc_list ();
380                         if (!rsrc_list_ptr ) {
381                                 iounmap (io_mem);
382                                 return -ENOMEM;
383                         }
384                         rsrc_list_ptr->format = format;
385                         rsrc_list_ptr->num_entries = num_entries;
386                         rsrc_list_ptr->phys_addr = sub_addr;
387
388                         debug ("info about rsrc descriptor---\n");
389                         debug ("format: %x\n", format);
390                         debug ("num of rsrc: %x\n", num_entries);
391                         debug ("offset of rsrc data structure enteries: %x\n ", sub_addr);
392
393                         hs_complete = 1;
394                 }
395                 /* found rio table */
396                 else if (blk_id == 0x4752) {
397                         debug ("now enter io table ---\n");
398                         debug ("rio blk id: %x\n", blk_id);
399
400                         rio_table_ptr = kmalloc (sizeof (struct rio_table_hdr), GFP_KERNEL);
401                         if (!rio_table_ptr)
402                                 return -ENOMEM; 
403                         memset (rio_table_ptr, 0, sizeof (struct rio_table_hdr) );
404                         rio_table_ptr->ver_num = readb (io_mem + offset);
405                         rio_table_ptr->scal_count = readb (io_mem + offset + 1);
406                         rio_table_ptr->riodev_count = readb (io_mem + offset + 2);
407                         rio_table_ptr->offset = offset +3 ;
408                         
409                         debug ("info about rio table hdr ---\n");
410                         debug ("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ", rio_table_ptr->ver_num, rio_table_ptr->scal_count, rio_table_ptr->riodev_count, rio_table_ptr->offset);
411
412                         rio_complete = 1;
413                 }
414         }
415
416         if (!hs_complete && !rio_complete) {
417                 iounmap (io_mem);
418                 return -ENODEV;
419         }
420
421         if (rio_table_ptr) {
422                 if (rio_complete == 1 && rio_table_ptr->ver_num == 3) {
423                         rc = ebda_rio_table ();
424                         if (rc) {
425                                 iounmap (io_mem);
426                                 return rc;
427                         }
428                 }
429         }
430         rc = ebda_rsrc_controller ();
431         if (rc) {
432                 iounmap (io_mem);
433                 return rc;
434         }
435
436         rc = ebda_rsrc_rsrc ();
437         if (rc) {
438                 iounmap (io_mem);
439                 return rc;
440         }
441
442         iounmap (io_mem);
443         return 0;
444 }
445
446 /*
447  * map info of scalability details and rio details from physical address
448  */
449 static int __init ebda_rio_table (void)
450 {
451         u16 offset;
452         u8 i;
453         struct rio_detail *rio_detail_ptr;
454
455         offset = rio_table_ptr->offset;
456         offset += 12 * rio_table_ptr->scal_count;
457
458         // we do concern about rio details
459         for (i = 0; i < rio_table_ptr->riodev_count; i++) {
460                 rio_detail_ptr = kmalloc (sizeof (struct rio_detail), GFP_KERNEL);
461                 if (!rio_detail_ptr)
462                         return -ENOMEM;
463                 memset (rio_detail_ptr, 0, sizeof (struct rio_detail));
464                 rio_detail_ptr->rio_node_id = readb (io_mem + offset);
465                 rio_detail_ptr->bbar = readl (io_mem + offset + 1);
466                 rio_detail_ptr->rio_type = readb (io_mem + offset + 5);
467                 rio_detail_ptr->owner_id = readb (io_mem + offset + 6);
468                 rio_detail_ptr->port0_node_connect = readb (io_mem + offset + 7);
469                 rio_detail_ptr->port0_port_connect = readb (io_mem + offset + 8);
470                 rio_detail_ptr->port1_node_connect = readb (io_mem + offset + 9);
471                 rio_detail_ptr->port1_port_connect = readb (io_mem + offset + 10);
472                 rio_detail_ptr->first_slot_num = readb (io_mem + offset + 11);
473                 rio_detail_ptr->status = readb (io_mem + offset + 12);
474                 rio_detail_ptr->wpindex = readb (io_mem + offset + 13);
475                 rio_detail_ptr->chassis_num = readb (io_mem + offset + 14);
476 //              debug ("rio_node_id: %x\nbbar: %x\nrio_type: %x\nowner_id: %x\nport0_node: %x\nport0_port: %x\nport1_node: %x\nport1_port: %x\nfirst_slot_num: %x\nstatus: %x\n", rio_detail_ptr->rio_node_id, rio_detail_ptr->bbar, rio_detail_ptr->rio_type, rio_detail_ptr->owner_id, rio_detail_ptr->port0_node_connect, rio_detail_ptr->port0_port_connect, rio_detail_ptr->port1_node_connect, rio_detail_ptr->port1_port_connect, rio_detail_ptr->first_slot_num, rio_detail_ptr->status);
477                 //create linked list of chassis
478                 if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5) 
479                         list_add (&rio_detail_ptr->rio_detail_list, &rio_vg_head);
480                 //create linked list of expansion box                           
481                 else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7) 
482                         list_add (&rio_detail_ptr->rio_detail_list, &rio_lo_head);
483                 else 
484                         // not in my concern
485                         kfree (rio_detail_ptr);
486                 offset += 15;
487         }
488         print_lo_info ();
489         print_vg_info ();
490         return 0;
491 }
492
493 /*
494  * reorganizing linked list of chassis   
495  */
496 static struct opt_rio *search_opt_vg (u8 chassis_num)
497 {
498         struct opt_rio *ptr;
499         struct list_head *ptr1;
500         list_for_each (ptr1, &opt_vg_head) {
501                 ptr = list_entry (ptr1, struct opt_rio, opt_rio_list);
502                 if (ptr->chassis_num == chassis_num)
503                         return ptr;
504         }               
505         return NULL;
506 }
507
508 static int __init combine_wpg_for_chassis (void)
509 {
510         struct opt_rio *opt_rio_ptr = NULL;
511         struct rio_detail *rio_detail_ptr = NULL;
512         struct list_head *list_head_ptr = NULL;
513         
514         list_for_each (list_head_ptr, &rio_vg_head) {
515                 rio_detail_ptr = list_entry (list_head_ptr, struct rio_detail, rio_detail_list);
516                 opt_rio_ptr = search_opt_vg (rio_detail_ptr->chassis_num);
517                 if (!opt_rio_ptr) {
518                         opt_rio_ptr = (struct opt_rio *) kmalloc (sizeof (struct opt_rio), GFP_KERNEL);
519                         if (!opt_rio_ptr)
520                                 return -ENOMEM;
521                         memset (opt_rio_ptr, 0, sizeof (struct opt_rio));
522                         opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
523                         opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
524                         opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
525                         opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
526                         list_add (&opt_rio_ptr->opt_rio_list, &opt_vg_head);
527                 } else {        
528                         opt_rio_ptr->first_slot_num = min (opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
529                         opt_rio_ptr->middle_num = max (opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
530                 }       
531         }
532         print_opt_vg ();
533         return 0;       
534 }       
535
536 /*
537  * reorgnizing linked list of expansion box      
538  */
539 static struct opt_rio_lo *search_opt_lo (u8 chassis_num)
540 {
541         struct opt_rio_lo *ptr;
542         struct list_head *ptr1;
543         list_for_each (ptr1, &opt_lo_head) {
544                 ptr = list_entry (ptr1, struct opt_rio_lo, opt_rio_lo_list);
545                 if (ptr->chassis_num == chassis_num)
546                         return ptr;
547         }               
548         return NULL;
549 }
550
551 static int combine_wpg_for_expansion (void)
552 {
553         struct opt_rio_lo *opt_rio_lo_ptr = NULL;
554         struct rio_detail *rio_detail_ptr = NULL;
555         struct list_head *list_head_ptr = NULL;
556         
557         list_for_each (list_head_ptr, &rio_lo_head) {
558                 rio_detail_ptr = list_entry (list_head_ptr, struct rio_detail, rio_detail_list);
559                 opt_rio_lo_ptr = search_opt_lo (rio_detail_ptr->chassis_num);
560                 if (!opt_rio_lo_ptr) {
561                         opt_rio_lo_ptr = (struct opt_rio_lo *) kmalloc (sizeof (struct opt_rio_lo), GFP_KERNEL);
562                         if (!opt_rio_lo_ptr)
563                                 return -ENOMEM;
564                         memset (opt_rio_lo_ptr, 0, sizeof (struct opt_rio_lo));
565                         opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
566                         opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
567                         opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
568                         opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
569                         opt_rio_lo_ptr->pack_count = 1;
570                         
571                         list_add (&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
572                 } else {        
573                         opt_rio_lo_ptr->first_slot_num = min (opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
574                         opt_rio_lo_ptr->middle_num = max (opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
575                         opt_rio_lo_ptr->pack_count = 2;
576                 }       
577         }
578         return 0;       
579 }
580         
581
582 /* Since we don't know the max slot number per each chassis, hence go
583  * through the list of all chassis to find out the range
584  * Arguments: slot_num, 1st slot number of the chassis we think we are on, 
585  * var (0 = chassis, 1 = expansion box) 
586  */
587 static int first_slot_num (u8 slot_num, u8 first_slot, u8 var)
588 {
589         struct opt_rio *opt_vg_ptr = NULL;
590         struct opt_rio_lo *opt_lo_ptr = NULL;
591         struct list_head *ptr = NULL;
592         int rc = 0;
593
594         if (!var) {
595                 list_for_each (ptr, &opt_vg_head) {
596                         opt_vg_ptr = list_entry (ptr, struct opt_rio, opt_rio_list);
597                         if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) { 
598                                 rc = -ENODEV;
599                                 break;
600                         }
601                 }
602         } else {
603                 list_for_each (ptr, &opt_lo_head) {
604                         opt_lo_ptr = list_entry (ptr, struct opt_rio_lo, opt_rio_lo_list);
605                         if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
606                                 rc = -ENODEV;
607                                 break;
608                         }
609                 }
610         }
611         return rc;
612 }
613
614 static struct opt_rio_lo * find_rxe_num (u8 slot_num)
615 {
616         struct opt_rio_lo *opt_lo_ptr;
617         struct list_head *ptr;
618
619         list_for_each (ptr, &opt_lo_head) {
620                 opt_lo_ptr = list_entry (ptr, struct opt_rio_lo, opt_rio_lo_list);
621                 //check to see if this slot_num belongs to expansion box
622                 if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_lo_ptr->first_slot_num, 1))) 
623                         return opt_lo_ptr;
624         }
625         return NULL;
626 }
627
628 static struct opt_rio * find_chassis_num (u8 slot_num)
629 {
630         struct opt_rio *opt_vg_ptr;
631         struct list_head *ptr;
632
633         list_for_each (ptr, &opt_vg_head) {
634                 opt_vg_ptr = list_entry (ptr, struct opt_rio, opt_rio_list);
635                 //check to see if this slot_num belongs to chassis 
636                 if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_vg_ptr->first_slot_num, 0))) 
637                         return opt_vg_ptr;
638         }
639         return NULL;
640 }
641
642 /* This routine will find out how many slots are in the chassis, so that
643  * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
644  */
645 static u8 calculate_first_slot (u8 slot_num)
646 {
647         u8 first_slot = 1;
648         struct list_head * list;
649         struct slot * slot_cur;
650         
651         list_for_each (list, &ibmphp_slot_head) {
652                 slot_cur = list_entry (list, struct slot, ibm_slot_list);
653                 if (slot_cur->ctrl) {
654                         if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num)) 
655                                 first_slot = slot_cur->ctrl->ending_slot_num;
656                 }
657         }                       
658         return first_slot + 1;
659
660 }
661 static char *create_file_name (struct slot * slot_cur)
662 {
663         struct opt_rio *opt_vg_ptr = NULL;
664         struct opt_rio_lo *opt_lo_ptr = NULL;
665         static char str[30];
666         int which = 0; /* rxe = 1, chassis = 0 */
667         u8 number = 1; /* either chassis or rxe # */
668         u8 first_slot = 1;
669         u8 slot_num;
670         u8 flag = 0;
671
672         if (!slot_cur) {
673                 err ("Structure passed is empty \n");
674                 return NULL;
675         }
676         
677         slot_num = slot_cur->number;
678
679         memset (str, 0, sizeof(str));
680         
681         if (rio_table_ptr) {
682                 if (rio_table_ptr->ver_num == 3) {
683                         opt_vg_ptr = find_chassis_num (slot_num);
684                         opt_lo_ptr = find_rxe_num (slot_num);
685                 }
686         }
687         if (opt_vg_ptr) {
688                 if (opt_lo_ptr) {
689                         if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
690                                 number = opt_lo_ptr->chassis_num;
691                                 first_slot = opt_lo_ptr->first_slot_num;
692                                 which = 1; /* it is RXE */
693                         } else {
694                                 first_slot = opt_vg_ptr->first_slot_num;
695                                 number = opt_vg_ptr->chassis_num;
696                                 which = 0;
697                         }
698                 } else {
699                         first_slot = opt_vg_ptr->first_slot_num;
700                         number = opt_vg_ptr->chassis_num;
701                         which = 0;
702                 }
703                 ++flag;
704         } else if (opt_lo_ptr) {
705                 number = opt_lo_ptr->chassis_num;
706                 first_slot = opt_lo_ptr->first_slot_num;
707                 which = 1;
708                 ++flag;
709         } else if (rio_table_ptr) {
710                 if (rio_table_ptr->ver_num == 3) {
711                         /* if both NULL and we DO have correct RIO table in BIOS */
712                         return NULL;
713                 }
714         } 
715         if (!flag) {
716                 if (slot_cur->ctrl->ctlr_type == 4) {
717                         first_slot = calculate_first_slot (slot_num);
718                         which = 1;
719                 } else {
720                         which = 0;
721                 }
722         }
723
724         sprintf(str, "%s%dslot%d",
725                 which == 0 ? "chassis" : "rxe",
726                 number, slot_num - first_slot + 1);
727         return str;
728 }
729
730 static int fillslotinfo(struct hotplug_slot *hotplug_slot)
731 {
732         struct slot *slot;
733         int rc = 0;
734
735         if (!hotplug_slot || !hotplug_slot->private)
736                 return -EINVAL;
737
738         slot = hotplug_slot->private;
739         rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
740         if (rc)
741                 return rc;
742
743         // power - enabled:1  not:0
744         hotplug_slot->info->power_status = SLOT_POWER(slot->status);
745
746         // attention - off:0, on:1, blinking:2
747         hotplug_slot->info->attention_status = SLOT_ATTN(slot->status, slot->ext_status);
748
749         // latch - open:1 closed:0
750         hotplug_slot->info->latch_status = SLOT_LATCH(slot->status);
751
752         // pci board - present:1 not:0
753         if (SLOT_PRESENT (slot->status))
754                 hotplug_slot->info->adapter_status = 1;
755         else
756                 hotplug_slot->info->adapter_status = 0;
757 /*
758         if (slot->bus_on->supported_bus_mode
759                 && (slot->bus_on->supported_speed == BUS_SPEED_66))
760                 hotplug_slot->info->max_bus_speed_status = BUS_SPEED_66PCIX;
761         else
762                 hotplug_slot->info->max_bus_speed_status = slot->bus_on->supported_speed;
763 */
764
765         return rc;
766 }
767
768 static void release_slot(struct hotplug_slot *hotplug_slot)
769 {
770         struct slot *slot;
771
772         if (!hotplug_slot || !hotplug_slot->private)
773                 return;
774
775         slot = hotplug_slot->private;
776         kfree(slot->hotplug_slot->info);
777         kfree(slot->hotplug_slot->name);
778         kfree(slot->hotplug_slot);
779         slot->ctrl = NULL;
780         slot->bus_on = NULL;
781
782         /* we don't want to actually remove the resources, since free_resources will do just that */
783         ibmphp_unconfigure_card(&slot, -1);
784
785         kfree (slot);
786 }
787
788 static struct pci_driver ibmphp_driver;
789
790 /*
791  * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
792  * each hpc from physical address to a list of hot plug controllers based on
793  * hpc descriptors.
794  */
795 static int __init ebda_rsrc_controller (void)
796 {
797         u16 addr, addr_slot, addr_bus;
798         u8 ctlr_id, temp, bus_index;
799         u16 ctlr, slot, bus;
800         u16 slot_num, bus_num, index;
801         struct hotplug_slot *hp_slot_ptr;
802         struct controller *hpc_ptr;
803         struct ebda_hpc_bus *bus_ptr;
804         struct ebda_hpc_slot *slot_ptr;
805         struct bus_info *bus_info_ptr1, *bus_info_ptr2;
806         int rc;
807         struct slot *tmp_slot;
808         struct list_head *list;
809
810         addr = hpc_list_ptr->phys_addr;
811         for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
812                 bus_index = 1;
813                 ctlr_id = readb (io_mem + addr);
814                 addr += 1;
815                 slot_num = readb (io_mem + addr);
816
817                 addr += 1;
818                 addr_slot = addr;       /* offset of slot structure */
819                 addr += (slot_num * 4);
820
821                 bus_num = readb (io_mem + addr);
822
823                 addr += 1;
824                 addr_bus = addr;        /* offset of bus */
825                 addr += (bus_num * 9);  /* offset of ctlr_type */
826                 temp = readb (io_mem + addr);
827
828                 addr += 1;
829                 /* init hpc structure */
830                 hpc_ptr = alloc_ebda_hpc (slot_num, bus_num);
831                 if (!hpc_ptr ) {
832                         rc = -ENOMEM;
833                         goto error_no_hpc;
834                 }
835                 hpc_ptr->ctlr_id = ctlr_id;
836                 hpc_ptr->ctlr_relative_id = ctlr;
837                 hpc_ptr->slot_count = slot_num;
838                 hpc_ptr->bus_count = bus_num;
839                 debug ("now enter ctlr data struture ---\n");
840                 debug ("ctlr id: %x\n", ctlr_id);
841                 debug ("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
842                 debug ("count of slots controlled by this ctlr: %x\n", slot_num);
843                 debug ("count of buses controlled by this ctlr: %x\n", bus_num);
844
845                 /* init slot structure, fetch slot, bus, cap... */
846                 slot_ptr = hpc_ptr->slots;
847                 for (slot = 0; slot < slot_num; slot++) {
848                         slot_ptr->slot_num = readb (io_mem + addr_slot);
849                         slot_ptr->slot_bus_num = readb (io_mem + addr_slot + slot_num);
850                         slot_ptr->ctl_index = readb (io_mem + addr_slot + 2*slot_num);
851                         slot_ptr->slot_cap = readb (io_mem + addr_slot + 3*slot_num);
852
853                         // create bus_info lined list --- if only one slot per bus: slot_min = slot_max 
854
855                         bus_info_ptr2 = ibmphp_find_same_bus_num (slot_ptr->slot_bus_num);
856                         if (!bus_info_ptr2) {
857                                 bus_info_ptr1 = (struct bus_info *) kmalloc (sizeof (struct bus_info), GFP_KERNEL);
858                                 if (!bus_info_ptr1) {
859                                         rc = -ENOMEM;
860                                         goto error_no_hp_slot;
861                                 }
862                                 memset (bus_info_ptr1, 0, sizeof (struct bus_info));
863                                 bus_info_ptr1->slot_min = slot_ptr->slot_num;
864                                 bus_info_ptr1->slot_max = slot_ptr->slot_num;
865                                 bus_info_ptr1->slot_count += 1;
866                                 bus_info_ptr1->busno = slot_ptr->slot_bus_num;
867                                 bus_info_ptr1->index = bus_index++;
868                                 bus_info_ptr1->current_speed = 0xff;
869                                 bus_info_ptr1->current_bus_mode = 0xff;
870                                 
871                                 bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
872                                 
873                                 list_add_tail (&bus_info_ptr1->bus_info_list, &bus_info_head);
874
875                         } else {
876                                 bus_info_ptr2->slot_min = min (bus_info_ptr2->slot_min, slot_ptr->slot_num);
877                                 bus_info_ptr2->slot_max = max (bus_info_ptr2->slot_max, slot_ptr->slot_num);
878                                 bus_info_ptr2->slot_count += 1;
879
880                         }
881
882                         // end of creating the bus_info linked list
883
884                         slot_ptr++;
885                         addr_slot += 1;
886                 }
887
888                 /* init bus structure */
889                 bus_ptr = hpc_ptr->buses;
890                 for (bus = 0; bus < bus_num; bus++) {
891                         bus_ptr->bus_num = readb (io_mem + addr_bus + bus);
892                         bus_ptr->slots_at_33_conv = readb (io_mem + addr_bus + bus_num + 8 * bus);
893                         bus_ptr->slots_at_66_conv = readb (io_mem + addr_bus + bus_num + 8 * bus + 1);
894
895                         bus_ptr->slots_at_66_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 2);
896
897                         bus_ptr->slots_at_100_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 3);
898
899                         bus_ptr->slots_at_133_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 4);
900
901                         bus_info_ptr2 = ibmphp_find_same_bus_num (bus_ptr->bus_num);
902                         if (bus_info_ptr2) {
903                                 bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
904                                 bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
905                                 bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
906                                 bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
907                                 bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix; 
908                         }
909                         bus_ptr++;
910                 }
911
912                 hpc_ptr->ctlr_type = temp;
913
914                 switch (hpc_ptr->ctlr_type) {
915                         case 1:
916                                 hpc_ptr->u.pci_ctlr.bus = readb (io_mem + addr);
917                                 hpc_ptr->u.pci_ctlr.dev_fun = readb (io_mem + addr + 1);
918                                 hpc_ptr->irq = readb (io_mem + addr + 2);
919                                 addr += 3;
920                                 debug ("ctrl bus = %x, ctlr devfun = %x, irq = %x\n", 
921                                         hpc_ptr->u.pci_ctlr.bus,
922                                         hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
923                                 break;
924
925                         case 0:
926                                 hpc_ptr->u.isa_ctlr.io_start = readw (io_mem + addr);
927                                 hpc_ptr->u.isa_ctlr.io_end = readw (io_mem + addr + 2);
928                                 if (!request_region (hpc_ptr->u.isa_ctlr.io_start,
929                                                      (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
930                                                      "ibmphp")) {
931                                         rc = -ENODEV;
932                                         goto error_no_hp_slot;
933                                 }
934                                 hpc_ptr->irq = readb (io_mem + addr + 4);
935                                 addr += 5;
936                                 break;
937
938                         case 2:
939                         case 4:
940                                 hpc_ptr->u.wpeg_ctlr.wpegbbar = readl (io_mem + addr);
941                                 hpc_ptr->u.wpeg_ctlr.i2c_addr = readb (io_mem + addr + 4);
942                                 hpc_ptr->irq = readb (io_mem + addr + 5);
943                                 addr += 6;
944                                 break;
945                         default:
946                                 rc = -ENODEV;
947                                 goto error_no_hp_slot;
948                 }
949
950                 //reorganize chassis' linked list
951                 combine_wpg_for_chassis ();
952                 combine_wpg_for_expansion ();
953                 hpc_ptr->revision = 0xff;
954                 hpc_ptr->options = 0xff;
955                 hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
956                 hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;
957
958                 // register slots with hpc core as well as create linked list of ibm slot
959                 for (index = 0; index < hpc_ptr->slot_count; index++) {
960
961                         hp_slot_ptr = kmalloc(sizeof(*hp_slot_ptr), GFP_KERNEL);
962                         if (!hp_slot_ptr) {
963                                 rc = -ENOMEM;
964                                 goto error_no_hp_slot;
965                         }
966                         memset(hp_slot_ptr, 0, sizeof(*hp_slot_ptr));
967
968                         hp_slot_ptr->info = kmalloc (sizeof(struct hotplug_slot_info), GFP_KERNEL);
969                         if (!hp_slot_ptr->info) {
970                                 rc = -ENOMEM;
971                                 goto error_no_hp_info;
972                         }
973                         memset(hp_slot_ptr->info, 0, sizeof(struct hotplug_slot_info));
974
975                         hp_slot_ptr->name = kmalloc(30, GFP_KERNEL);
976                         if (!hp_slot_ptr->name) {
977                                 rc = -ENOMEM;
978                                 goto error_no_hp_name;
979                         }
980
981                         tmp_slot = kmalloc(sizeof(*tmp_slot), GFP_KERNEL);
982                         if (!tmp_slot) {
983                                 rc = -ENOMEM;
984                                 goto error_no_slot;
985                         }
986                         memset(tmp_slot, 0, sizeof(*tmp_slot));
987
988                         tmp_slot->flag = TRUE;
989
990                         tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
991                         if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
992                                 tmp_slot->supported_speed =  3;
993                         else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
994                                 tmp_slot->supported_speed =  2;
995                         else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
996                                 tmp_slot->supported_speed =  1;
997                                 
998                         if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
999                                 tmp_slot->supported_bus_mode = 1;
1000                         else
1001                                 tmp_slot->supported_bus_mode = 0;
1002
1003
1004                         tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;
1005
1006                         bus_info_ptr1 = ibmphp_find_same_bus_num (hpc_ptr->slots[index].slot_bus_num);
1007                         if (!bus_info_ptr1) {
1008                                 rc = -ENODEV;
1009                                 goto error;
1010                         }
1011                         tmp_slot->bus_on = bus_info_ptr1;
1012                         bus_info_ptr1 = NULL;
1013                         tmp_slot->ctrl = hpc_ptr;
1014
1015                         tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
1016                         tmp_slot->number = hpc_ptr->slots[index].slot_num;
1017                         tmp_slot->hotplug_slot = hp_slot_ptr;
1018
1019                         hp_slot_ptr->private = tmp_slot;
1020                         hp_slot_ptr->release = release_slot;
1021
1022                         rc = fillslotinfo(hp_slot_ptr);
1023                         if (rc)
1024                                 goto error;
1025
1026                         rc = ibmphp_init_devno ((struct slot **) &hp_slot_ptr->private);
1027                         if (rc)
1028                                 goto error;
1029                         hp_slot_ptr->ops = &ibmphp_hotplug_slot_ops;
1030
1031                         // end of registering ibm slot with hotplug core
1032
1033                         list_add (& ((struct slot *)(hp_slot_ptr->private))->ibm_slot_list, &ibmphp_slot_head);
1034                 }
1035
1036                 print_bus_info ();
1037                 list_add (&hpc_ptr->ebda_hpc_list, &ebda_hpc_head );
1038
1039         }                       /* each hpc  */
1040
1041         list_for_each (list, &ibmphp_slot_head) {
1042                 tmp_slot = list_entry (list, struct slot, ibm_slot_list);
1043
1044                 snprintf (tmp_slot->hotplug_slot->name, 30, "%s", create_file_name (tmp_slot));
1045                 pci_hp_register (tmp_slot->hotplug_slot);
1046         }
1047
1048         print_ebda_hpc ();
1049         print_ibm_slot ();
1050         return 0;
1051
1052 error:
1053         kfree (hp_slot_ptr->private);
1054 error_no_slot:
1055         kfree (hp_slot_ptr->name);
1056 error_no_hp_name:
1057         kfree (hp_slot_ptr->info);
1058 error_no_hp_info:
1059         kfree (hp_slot_ptr);
1060 error_no_hp_slot:
1061         free_ebda_hpc (hpc_ptr);
1062 error_no_hpc:
1063         iounmap (io_mem);
1064         return rc;
1065 }
1066
1067 /* 
1068  * map info (bus, devfun, start addr, end addr..) of i/o, memory,
1069  * pfm from the physical addr to a list of resource.
1070  */
1071 static int __init ebda_rsrc_rsrc (void)
1072 {
1073         u16 addr;
1074         short rsrc;
1075         u8 type, rsrc_type;
1076         struct ebda_pci_rsrc *rsrc_ptr;
1077
1078         addr = rsrc_list_ptr->phys_addr;
1079         debug ("now entering rsrc land\n");
1080         debug ("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
1081
1082         for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
1083                 type = readb (io_mem + addr);
1084
1085                 addr += 1;
1086                 rsrc_type = type & EBDA_RSRC_TYPE_MASK;
1087
1088                 if (rsrc_type == EBDA_IO_RSRC_TYPE) {
1089                         rsrc_ptr = alloc_ebda_pci_rsrc ();
1090                         if (!rsrc_ptr) {
1091                                 iounmap (io_mem);
1092                                 return -ENOMEM;
1093                         }
1094                         rsrc_ptr->rsrc_type = type;
1095
1096                         rsrc_ptr->bus_num = readb (io_mem + addr);
1097                         rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1098                         rsrc_ptr->start_addr = readw (io_mem + addr + 2);
1099                         rsrc_ptr->end_addr = readw (io_mem + addr + 4);
1100                         addr += 6;
1101
1102                         debug ("rsrc from io type ----\n");
1103                         debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1104                                 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1105
1106                         list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1107                 }
1108
1109                 if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
1110                         rsrc_ptr = alloc_ebda_pci_rsrc ();
1111                         if (!rsrc_ptr ) {
1112                                 iounmap (io_mem);
1113                                 return -ENOMEM;
1114                         }
1115                         rsrc_ptr->rsrc_type = type;
1116
1117                         rsrc_ptr->bus_num = readb (io_mem + addr);
1118                         rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1119                         rsrc_ptr->start_addr = readl (io_mem + addr + 2);
1120                         rsrc_ptr->end_addr = readl (io_mem + addr + 6);
1121                         addr += 10;
1122
1123                         debug ("rsrc from mem or pfm ---\n");
1124                         debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n", 
1125                                 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1126
1127                         list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1128                 }
1129         }
1130         kfree (rsrc_list_ptr);
1131         rsrc_list_ptr = NULL;
1132         print_ebda_pci_rsrc ();
1133         return 0;
1134 }
1135
1136 u16 ibmphp_get_total_controllers (void)
1137 {
1138         return hpc_list_ptr->num_ctlrs;
1139 }
1140
1141 struct slot *ibmphp_get_slot_from_physical_num (u8 physical_num)
1142 {
1143         struct slot *slot;
1144         struct list_head *list;
1145
1146         list_for_each (list, &ibmphp_slot_head) {
1147                 slot = list_entry (list, struct slot, ibm_slot_list);
1148                 if (slot->number == physical_num)
1149                         return slot;
1150         }
1151         return NULL;
1152 }
1153
1154 /* To find:
1155  *      - the smallest slot number
1156  *      - the largest slot number
1157  *      - the total number of the slots based on each bus
1158  *        (if only one slot per bus slot_min = slot_max )
1159  */
1160 struct bus_info *ibmphp_find_same_bus_num (u32 num)
1161 {
1162         struct bus_info *ptr;
1163         struct list_head  *ptr1;
1164
1165         list_for_each (ptr1, &bus_info_head) {
1166                 ptr = list_entry (ptr1, struct bus_info, bus_info_list); 
1167                 if (ptr->busno == num) 
1168                          return ptr;
1169         }
1170         return NULL;
1171 }
1172
1173 /*  Finding relative bus number, in order to map corresponding
1174  *  bus register
1175  */
1176 int ibmphp_get_bus_index (u8 num)
1177 {
1178         struct bus_info *ptr;
1179         struct list_head  *ptr1;
1180
1181         list_for_each (ptr1, &bus_info_head) {
1182                 ptr = list_entry (ptr1, struct bus_info, bus_info_list);
1183                 if (ptr->busno == num)  
1184                         return ptr->index;
1185         }
1186         return -ENODEV;
1187 }
1188
1189 void ibmphp_free_bus_info_queue (void)
1190 {
1191         struct bus_info *bus_info;
1192         struct list_head *list;
1193         struct list_head *next;
1194
1195         list_for_each_safe (list, next, &bus_info_head ) {
1196                 bus_info = list_entry (list, struct bus_info, bus_info_list);
1197                 kfree (bus_info);
1198         }
1199 }
1200
1201 void ibmphp_free_ebda_hpc_queue (void)
1202 {
1203         struct controller *controller = NULL;
1204         struct list_head *list;
1205         struct list_head *next;
1206         int pci_flag = 0;
1207
1208         list_for_each_safe (list, next, &ebda_hpc_head) {
1209                 controller = list_entry (list, struct controller, ebda_hpc_list);
1210                 if (controller->ctlr_type == 0)
1211                         release_region (controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
1212                 else if ((controller->ctlr_type == 1) && (!pci_flag)) {
1213                         ++pci_flag;
1214                         pci_unregister_driver (&ibmphp_driver);
1215                 }
1216                 free_ebda_hpc (controller);
1217         }
1218 }
1219
1220 void ibmphp_free_ebda_pci_rsrc_queue (void)
1221 {
1222         struct ebda_pci_rsrc *resource;
1223         struct list_head *list;
1224         struct list_head *next;
1225
1226         list_for_each_safe (list, next, &ibmphp_ebda_pci_rsrc_head) {
1227                 resource = list_entry (list, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
1228                 kfree (resource);
1229                 resource = NULL;
1230         }
1231 }
1232
1233 static struct pci_device_id id_table[] = {
1234         {
1235                 .vendor         = PCI_VENDOR_ID_IBM,
1236                 .device         = HPC_DEVICE_ID,
1237                 .subvendor      = PCI_VENDOR_ID_IBM,
1238                 .subdevice      = HPC_SUBSYSTEM_ID,
1239                 .class          = ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1240         }, {}
1241 };              
1242
1243 MODULE_DEVICE_TABLE(pci, id_table);
1244
1245 static int ibmphp_probe (struct pci_dev *, const struct pci_device_id *);
1246 static struct pci_driver ibmphp_driver = {
1247         .name           = "ibmphp",
1248         .id_table       = id_table,
1249         .probe          = ibmphp_probe,
1250 };
1251
1252 int ibmphp_register_pci (void)
1253 {
1254         struct controller *ctrl;
1255         struct list_head *tmp;
1256         int rc = 0;
1257
1258         list_for_each (tmp, &ebda_hpc_head) {
1259                 ctrl = list_entry (tmp, struct controller, ebda_hpc_list);
1260                 if (ctrl->ctlr_type == 1) {
1261                         rc = pci_module_init (&ibmphp_driver);
1262                         break;
1263                 }
1264         }
1265         return rc;
1266 }
1267 static int ibmphp_probe (struct pci_dev * dev, const struct pci_device_id *ids)
1268 {
1269         struct controller *ctrl;
1270         struct list_head *tmp;
1271
1272         debug ("inside ibmphp_probe \n");
1273         
1274         list_for_each (tmp, &ebda_hpc_head) {
1275                 ctrl = list_entry (tmp, struct controller, ebda_hpc_list);
1276                 if (ctrl->ctlr_type == 1) {
1277                         if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
1278                                 ctrl->ctrl_dev = dev;
1279                                 debug ("found device!!! \n");
1280                                 debug ("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
1281                                 return 0;
1282                         }
1283                 }
1284         }
1285         return -ENODEV;
1286 }
1287