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