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