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