Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6] / drivers / pci / hotplug / ibmphp_pci.c
1 /*
2  * IBM Hot Plug Controller Driver
3  * 
4  * Written By: Irene Zubarev, 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/slab.h>
32 #include <linux/pci.h>
33 #include <linux/list.h>
34 #include "ibmphp.h"
35
36
37 static int configure_device(struct pci_func *);
38 static int configure_bridge(struct pci_func **, u8);
39 static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
40 static int add_new_bus (struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
41 static u8 find_sec_number (u8 primary_busno, u8 slotno);
42
43 /*
44  * NOTE..... If BIOS doesn't provide default routing, we assign:
45  * 9 for SCSI, 10 for LAN adapters, and 11 for everything else. 
46  * If adapter is bridged, then we assign 11 to it and devices behind it.
47  * We also assign the same irq numbers for multi function devices.
48  * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
49  */
50 static void assign_alt_irq (struct pci_func * cur_func, u8 class_code)
51 {
52         int j;
53         for (j = 0; j < 4; j++) {
54                 if (cur_func->irq[j] == 0xff) {
55                         switch (class_code) {
56                                 case PCI_BASE_CLASS_STORAGE:
57                                         cur_func->irq[j] = SCSI_IRQ;
58                                         break;
59                                 case PCI_BASE_CLASS_NETWORK:
60                                         cur_func->irq[j] = LAN_IRQ;
61                                         break;
62                                 default:
63                                         cur_func->irq[j] = OTHER_IRQ;
64                                         break;
65                         }
66                 }
67         }
68 }
69
70 /*
71  * Configures the device to be added (will allocate needed resources if it
72  * can), the device can be a bridge or a regular pci device, can also be
73  * multi-functional
74  * 
75  * Input: function to be added
76  * 
77  * TO DO:  The error case with Multifunction device or multi function bridge,
78  * if there is an error, will need to go through all previous functions and 
79  * unconfigure....or can add some code into unconfigure_card....
80  */
81 int ibmphp_configure_card (struct pci_func *func, u8 slotno)
82 {
83         u16 vendor_id;
84         u32 class;
85         u8 class_code;
86         u8 hdr_type, device, sec_number;
87         u8 function;
88         struct pci_func *newfunc;       /* for multi devices */
89         struct pci_func *cur_func, *prev_func;
90         int rc, i, j;
91         int cleanup_count;
92         u8 flag;
93         u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
94
95         debug ("inside configure_card, func->busno = %x\n", func->busno);
96
97         device = func->device;
98         cur_func = func;
99
100         /* We only get bus and device from IRQ routing table.  So at this point,
101          * func->busno is correct, and func->device contains only device (at the 5 
102          * highest bits)
103          */
104
105         /* For every function on the card */
106         for (function = 0x00; function < 0x08; function++) {
107                 unsigned int devfn = PCI_DEVFN(device, function);
108                 ibmphp_pci_bus->number = cur_func->busno;
109
110                 cur_func->function = function;
111
112                 debug ("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->funcion = %x\n",
113                         cur_func->busno, cur_func->device, cur_func->function);
114
115                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
116
117                 debug ("vendor_id is %x\n", vendor_id);
118                 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
119                         /* found correct device!!! */
120                         debug ("found valid device, vendor_id = %x\n", vendor_id);
121
122                         ++valid_device;
123
124                         /* header: x x x x x x x x
125                          *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
126                          *         |_=> 0 = single function device, 1 = multi-function device
127                          */
128
129                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
130                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
131
132                         class_code = class >> 24;
133                         debug ("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
134                         class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
135                         if (class == PCI_CLASS_NOT_DEFINED_VGA) {
136                                 err ("The device %x is VGA compatible and as is not supported for hot plugging. "
137                                      "Please choose another device.\n", cur_func->device);
138                                 return -ENODEV;
139                         } else if (class == PCI_CLASS_DISPLAY_VGA) {
140                                 err ("The device %x is not supported for hot plugging. "
141                                      "Please choose another device.\n", cur_func->device);
142                                 return -ENODEV;
143                         }
144                         switch (hdr_type) {
145                                 case PCI_HEADER_TYPE_NORMAL:
146                                         debug ("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
147                                         assign_alt_irq (cur_func, class_code);
148                                         if ((rc = configure_device (cur_func)) < 0) {
149                                                 /* We need to do this in case some other BARs were properly inserted */
150                                                 err ("was not able to configure devfunc %x on bus %x.\n",
151                                                      cur_func->device, cur_func->busno);
152                                                 cleanup_count = 6;
153                                                 goto error;
154                                         }       
155                                         cur_func->next = NULL;
156                                         function = 0x8;
157                                         break;
158                                 case PCI_HEADER_TYPE_MULTIDEVICE:
159                                         assign_alt_irq (cur_func, class_code);
160                                         if ((rc = configure_device (cur_func)) < 0) {
161                                                 /* We need to do this in case some other BARs were properly inserted */
162                                                 err ("was not able to configure devfunc %x on bus %x...bailing out\n",
163                                                      cur_func->device, cur_func->busno);
164                                                 cleanup_count = 6;
165                                                 goto error;
166                                         }
167                                         newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
168                                         if (!newfunc) {
169                                                 err ("out of system memory\n");
170                                                 return -ENOMEM;
171                                         }
172                                         memset (newfunc, 0, sizeof (struct pci_func));
173                                         newfunc->busno = cur_func->busno;
174                                         newfunc->device = device;
175                                         cur_func->next = newfunc;
176                                         cur_func = newfunc;
177                                         for (j = 0; j < 4; j++)
178                                                 newfunc->irq[j] = cur_func->irq[j];
179                                         break;
180                                 case PCI_HEADER_TYPE_MULTIBRIDGE:
181                                         class >>= 8;
182                                         if (class != PCI_CLASS_BRIDGE_PCI) {
183                                                 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
184                                                      "Please insert another card.\n", cur_func->device);
185                                                 return -ENODEV;
186                                         }
187                                         assign_alt_irq (cur_func, class_code);
188                                         rc = configure_bridge (&cur_func, slotno);
189                                         if (rc == -ENODEV) {
190                                                 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
191                                                 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
192                                                 return rc;
193                                         }
194                                         if (rc) {
195                                                 /* We need to do this in case some other BARs were properly inserted */
196                                                 err ("was not able to hot-add PPB properly.\n");
197                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
198                                                 cleanup_count = 2;
199                                                 goto error;
200                                         }
201
202                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
203                                         flag = FALSE;
204                                         for (i = 0; i < 32; i++) {
205                                                 if (func->devices[i]) {
206                                                         newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
207                                                         if (!newfunc) {
208                                                                 err ("out of system memory\n");
209                                                                 return -ENOMEM;
210                                                         }
211                                                         memset (newfunc, 0, sizeof (struct pci_func));
212                                                         newfunc->busno = sec_number;
213                                                         newfunc->device = (u8) i;
214                                                         for (j = 0; j < 4; j++)
215                                                                 newfunc->irq[j] = cur_func->irq[j];
216
217                                                         if (flag) {
218                                                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
219                                                                 prev_func->next = newfunc;
220                                                         } else
221                                                                 cur_func->next = newfunc;
222
223                                                         rc = ibmphp_configure_card (newfunc, slotno);
224                                                         /* This could only happen if kmalloc failed */
225                                                         if (rc) {
226                                                                 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
227                                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
228                                                                 cleanup_count = 2;
229                                                                 goto error;
230                                                         }
231                                                         flag = TRUE;
232                                                 }
233                                         }
234
235                                         newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
236                                         if (!newfunc) {
237                                                 err ("out of system memory\n");
238                                                 return -ENOMEM;
239                                         }
240                                         memset (newfunc, 0, sizeof (struct pci_func));
241                                         newfunc->busno = cur_func->busno;
242                                         newfunc->device = device;
243                                         for (j = 0; j < 4; j++)
244                                                 newfunc->irq[j] = cur_func->irq[j];
245                                         for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
246                                         prev_func->next = newfunc;
247                                         cur_func = newfunc;
248                                         break;
249                                 case PCI_HEADER_TYPE_BRIDGE:
250                                         class >>= 8;
251                                         debug ("class now is %x\n", class);
252                                         if (class != PCI_CLASS_BRIDGE_PCI) {
253                                                 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
254                                                      "Please insert another card.\n", cur_func->device);
255                                                 return -ENODEV;
256                                         }
257
258                                         assign_alt_irq (cur_func, class_code);
259
260                                         debug ("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
261                                         rc = configure_bridge (&cur_func, slotno);
262                                         if (rc == -ENODEV) {
263                                                 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
264                                                 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
265                                                 return rc;
266                                         }
267                                         if (rc) {
268                                                 /* We need to do this in case some other BARs were properly inserted */
269                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
270                                                 err ("was not able to hot-add PPB properly.\n");
271                                                 cleanup_count = 2;
272                                                 goto error;
273                                         }
274                                         debug ("cur_func->busno = %x, device = %x, function = %x\n",
275                                                 cur_func->busno, device, function);
276                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
277                                         debug ("after configuring bridge..., sec_number = %x\n", sec_number);
278                                         flag = FALSE;
279                                         for (i = 0; i < 32; i++) {
280                                                 if (func->devices[i]) {
281                                                         debug ("inside for loop, device is %x\n", i);
282                                                         newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
283                                                         if (!newfunc) {
284                                                                 err (" out of system memory\n");
285                                                                 return -ENOMEM;
286                                                         }
287                                                         memset (newfunc, 0, sizeof (struct pci_func));
288                                                         newfunc->busno = sec_number;
289                                                         newfunc->device = (u8) i;
290                                                         for (j = 0; j < 4; j++)
291                                                                 newfunc->irq[j] = cur_func->irq[j];
292
293                                                         if (flag) {
294                                                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
295                                                                 prev_func->next = newfunc;
296                                                         } else
297                                                                 cur_func->next = newfunc;
298
299                                                         rc = ibmphp_configure_card (newfunc, slotno);
300
301                                                         /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
302                                                         if (rc) {
303                                                                 /* We need to do this in case some other BARs were properly inserted */
304                                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
305                                                                 cleanup_count = 2;
306                                                                 goto error;
307                                                         }
308                                                         flag = TRUE;
309                                                 }
310                                         }
311
312                                         function = 0x8;
313                                         break;
314                                 default:
315                                         err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
316                                         return -ENXIO;
317                                         break;
318                         }       /* end of switch */
319                 }       /* end of valid device */
320         }       /* end of for */
321
322         if (!valid_device) {
323                 err ("Cannot find any valid devices on the card.  Or unable to read from card.\n");
324                 return -ENODEV;
325         }
326
327         return 0;
328
329 error:
330         for (i = 0; i < cleanup_count; i++) {
331                 if (cur_func->io[i]) {
332                         ibmphp_remove_resource (cur_func->io[i]);
333                         cur_func->io[i] = NULL;
334                 } else if (cur_func->pfmem[i]) {
335                         ibmphp_remove_resource (cur_func->pfmem[i]);
336                         cur_func->pfmem[i] = NULL;
337                 } else if (cur_func->mem[i]) {
338                         ibmphp_remove_resource (cur_func->mem[i]);
339                         cur_func->mem[i] = NULL;
340                 }
341         }
342         return rc;
343 }
344
345 /*
346  * This function configures the pci BARs of a single device.  
347  * Input: pointer to the pci_func
348  * Output: configured PCI, 0, or error
349  */
350 static int configure_device (struct pci_func *func)
351 {
352         u32 bar[6];
353         u32 address[] = {
354                 PCI_BASE_ADDRESS_0,
355                 PCI_BASE_ADDRESS_1,
356                 PCI_BASE_ADDRESS_2,
357                 PCI_BASE_ADDRESS_3,
358                 PCI_BASE_ADDRESS_4,
359                 PCI_BASE_ADDRESS_5,
360                 0
361         };
362         u8 irq;
363         int count;
364         int len[6];
365         struct resource_node *io[6];
366         struct resource_node *mem[6];
367         struct resource_node *mem_tmp;
368         struct resource_node *pfmem[6];
369         unsigned int devfn;
370
371         debug ("%s - inside\n", __FUNCTION__);
372
373         devfn = PCI_DEVFN(func->device, func->function);
374         ibmphp_pci_bus->number = func->busno;
375
376         for (count = 0; address[count]; count++) {      /* for 6 BARs */
377
378                 /* not sure if i need this.  per scott, said maybe need smth like this
379                    if devices don't adhere 100% to the spec, so don't want to write
380                    to the reserved bits
381
382                 pcibios_read_config_byte(cur_func->busno, cur_func->device, 
383                 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
384                 if (tmp & 0x01) // IO
385                         pcibios_write_config_dword(cur_func->busno, cur_func->device, 
386                         PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
387                 else  // Memory
388                         pcibios_write_config_dword(cur_func->busno, cur_func->device, 
389                         PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
390                  */
391                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
392                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
393
394                 if (!bar[count])        /* This BAR is not implemented */
395                         continue;
396
397                 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
398
399                 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
400                         /* This is IO */
401                         debug ("inside IO SPACE\n");
402
403                         len[count] = bar[count] & 0xFFFFFFFC;
404                         len[count] = ~len[count] + 1;
405
406                         debug ("len[count] in IO %x, count %d\n", len[count], count);
407
408                         io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
409
410                         if (!io[count]) {
411                                 err ("out of system memory\n");
412                                 return -ENOMEM;
413                         }
414                         memset (io[count], 0, sizeof (struct resource_node));
415                         io[count]->type = IO;
416                         io[count]->busno = func->busno;
417                         io[count]->devfunc = PCI_DEVFN(func->device, func->function);
418                         io[count]->len = len[count];
419                         if (ibmphp_check_resource(io[count], 0) == 0) {
420                                 ibmphp_add_resource (io[count]);
421                                 func->io[count] = io[count];
422                         } else {
423                                 err ("cannot allocate requested io for bus %x device %x function %x len %x\n",
424                                      func->busno, func->device, func->function, len[count]);
425                                 kfree (io[count]);
426                                 return -EIO;
427                         }
428                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
429         
430                         /* _______________This is for debugging purposes only_____________________ */ 
431                         debug ("b4 writing, the IO address is %x\n", func->io[count]->start);
432                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
433                         debug ("after writing.... the start address is %x\n", bar[count]);
434                         /* _________________________________________________________________________*/
435
436                 } else {
437                         /* This is Memory */
438                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
439                                 /* pfmem */
440                                 debug ("PFMEM SPACE\n");
441
442                                 len[count] = bar[count] & 0xFFFFFFF0;
443                                 len[count] = ~len[count] + 1;
444
445                                 debug ("len[count] in PFMEM %x, count %d\n", len[count], count);
446
447                                 pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
448                                 if (!pfmem[count]) {
449                                         err ("out of system memory\n");
450                                         return -ENOMEM;
451                                 }
452                                 memset (pfmem[count], 0, sizeof (struct resource_node));
453                                 pfmem[count]->type = PFMEM;
454                                 pfmem[count]->busno = func->busno;
455                                 pfmem[count]->devfunc = PCI_DEVFN(func->device,
456                                                         func->function);
457                                 pfmem[count]->len = len[count];
458                                 pfmem[count]->fromMem = FALSE;
459                                 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
460                                         ibmphp_add_resource (pfmem[count]);
461                                         func->pfmem[count] = pfmem[count];
462                                 } else {
463                                         mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
464                                         if (!mem_tmp) {
465                                                 err ("out of system memory\n");
466                                                 kfree (pfmem[count]);
467                                                 return -ENOMEM;
468                                         }
469                                         memset (mem_tmp, 0, sizeof (struct resource_node));
470                                         mem_tmp->type = MEM;
471                                         mem_tmp->busno = pfmem[count]->busno;
472                                         mem_tmp->devfunc = pfmem[count]->devfunc;
473                                         mem_tmp->len = pfmem[count]->len;
474                                         debug ("there's no pfmem... going into mem.\n");
475                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
476                                                 ibmphp_add_resource (mem_tmp);
477                                                 pfmem[count]->fromMem = TRUE;
478                                                 pfmem[count]->rangeno = mem_tmp->rangeno;
479                                                 pfmem[count]->start = mem_tmp->start;
480                                                 pfmem[count]->end = mem_tmp->end;
481                                                 ibmphp_add_pfmem_from_mem (pfmem[count]);
482                                                 func->pfmem[count] = pfmem[count];
483                                         } else {
484                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
485                                                      func->busno, func->device, len[count]);
486                                                 kfree (mem_tmp);
487                                                 kfree (pfmem[count]);
488                                                 return -EIO;
489                                         }
490                                 }
491
492                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
493
494                                 /*_______________This is for debugging purposes only______________________________*/                            
495                                 debug ("b4 writing, start address is %x\n", func->pfmem[count]->start);
496                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
497                                 debug ("after writing, start address is %x\n", bar[count]);
498                                 /*_________________________________________________________________________________*/
499
500                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {        /* takes up another dword */
501                                         debug ("inside the mem 64 case, count %d\n", count);
502                                         count += 1;
503                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
504                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
505                                 }
506                         } else {
507                                 /* regular memory */
508                                 debug ("REGULAR MEM SPACE\n");
509
510                                 len[count] = bar[count] & 0xFFFFFFF0;
511                                 len[count] = ~len[count] + 1;
512
513                                 debug ("len[count] in Mem %x, count %d\n", len[count], count);
514
515                                 mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
516                                 if (!mem[count]) {
517                                         err ("out of system memory\n");
518                                         return -ENOMEM;
519                                 }
520                                 memset (mem[count], 0, sizeof (struct resource_node));
521                                 mem[count]->type = MEM;
522                                 mem[count]->busno = func->busno;
523                                 mem[count]->devfunc = PCI_DEVFN(func->device,
524                                                         func->function);
525                                 mem[count]->len = len[count];
526                                 if (ibmphp_check_resource (mem[count], 0) == 0) {
527                                         ibmphp_add_resource (mem[count]);
528                                         func->mem[count] = mem[count];
529                                 } else {
530                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
531                                              func->busno, func->device, len[count]);
532                                         kfree (mem[count]);
533                                         return -EIO;
534                                 }
535                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
536                                 /* _______________________This is for debugging purposes only _______________________*/
537                                 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
538                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
539                                 debug ("after writing, the address is %x\n", bar[count]);
540                                 /* __________________________________________________________________________________*/
541
542                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
543                                         /* takes up another dword */
544                                         debug ("inside mem 64 case, reg. mem, count %d\n", count);
545                                         count += 1;
546                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
547                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
548                                 }
549                         }
550                 }               /* end of mem */
551         }                       /* end of for */
552
553         func->bus = 0;          /* To indicate that this is not a PPB */
554         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
555         if ((irq > 0x00) && (irq < 0x05))
556                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
557
558         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
559         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
560
561         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
562         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
563
564         return 0;
565 }
566
567 /******************************************************************************
568  * This routine configures a PCI-2-PCI bridge and the functions behind it
569  * Parameters: pci_func
570  * Returns: 
571  ******************************************************************************/
572 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
573 {
574         int count;
575         int i;
576         int rc;
577         u8 sec_number;
578         u8 io_base;
579         u16 pfmem_base;
580         u32 bar[2];
581         u32 len[2];
582         u8 flag_io = FALSE;
583         u8 flag_mem = FALSE;
584         u8 flag_pfmem = FALSE;
585         u8 need_io_upper = FALSE;
586         u8 need_pfmem_upper = FALSE;
587         struct res_needed *amount_needed = NULL;
588         struct resource_node *io = NULL;
589         struct resource_node *bus_io[2] = {NULL, NULL};
590         struct resource_node *mem = NULL;
591         struct resource_node *bus_mem[2] = {NULL, NULL};
592         struct resource_node *mem_tmp = NULL;
593         struct resource_node *pfmem = NULL;
594         struct resource_node *bus_pfmem[2] = {NULL, NULL};
595         struct bus_node *bus;
596         u32 address[] = {
597                 PCI_BASE_ADDRESS_0,
598                 PCI_BASE_ADDRESS_1,
599                 0
600         };
601         struct pci_func *func = *func_passed;
602         unsigned int devfn;
603         u8 irq;
604         int retval;
605
606         debug ("%s - enter\n", __FUNCTION__);
607
608         devfn = PCI_DEVFN(func->function, func->device);
609         ibmphp_pci_bus->number = func->busno;
610
611         /* Configuring necessary info for the bridge so that we could see the devices
612          * behind it
613          */
614
615         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
616
617         /* _____________________For debugging purposes only __________________________
618         pci_bus_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
619         debug ("primary # written into the bridge is %x\n", pri_number);
620          ___________________________________________________________________________*/
621
622         /* in EBDA, only get allocated 1 additional bus # per slot */
623         sec_number = find_sec_number (func->busno, slotno);
624         if (sec_number == 0xff) {
625                 err ("cannot allocate secondary bus number for the bridged device\n");
626                 return -EINVAL;
627         }
628
629         debug ("after find_sec_number, the number we got is %x\n", sec_number);
630         debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
631
632         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
633         
634         /* __________________For debugging purposes only __________________________________
635         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
636         debug ("sec_number after write/read is %x\n", sec_number);
637          ________________________________________________________________________________*/
638
639         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
640
641         /* __________________For debugging purposes only ____________________________________
642         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
643         debug ("subordinate number after write/read is %x\n", sec_number);
644          __________________________________________________________________________________*/
645
646         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
647         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
648         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
649
650         debug ("func->busno is %x\n", func->busno);
651         debug ("sec_number after writing is %x\n", sec_number);
652
653
654         /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
655            !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!! 
656            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
657
658
659         /* First we need to allocate mem/io for the bridge itself in case it needs it */
660         for (count = 0; address[count]; count++) {      /* for 2 BARs */
661                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
662                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
663
664                 if (!bar[count]) {
665                         /* This BAR is not implemented */
666                         debug ("so we come here then, eh?, count = %d\n", count);
667                         continue;
668                 }
669                 //  tmp_bar = bar[count];
670
671                 debug ("Bar %d wants %x\n", count, bar[count]);
672
673                 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
674                         /* This is IO */
675                         len[count] = bar[count] & 0xFFFFFFFC;
676                         len[count] = ~len[count] + 1;
677
678                         debug ("len[count] in IO = %x\n", len[count]);
679
680                         bus_io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
681                 
682                         if (!bus_io[count]) {
683                                 err ("out of system memory\n");
684                                 retval = -ENOMEM;
685                                 goto error;
686                         }
687                         memset (bus_io[count], 0, sizeof (struct resource_node));
688                         bus_io[count]->type = IO;
689                         bus_io[count]->busno = func->busno;
690                         bus_io[count]->devfunc = PCI_DEVFN(func->device,
691                                                         func->function);
692                         bus_io[count]->len = len[count];
693                         if (ibmphp_check_resource (bus_io[count], 0) == 0) {
694                                 ibmphp_add_resource (bus_io[count]);
695                                 func->io[count] = bus_io[count];
696                         } else {
697                                 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
698                                      func->busno, func->device, len[count]);
699                                 kfree (bus_io[count]);
700                                 return -EIO;
701                         }
702
703                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
704
705                 } else {
706                         /* This is Memory */
707                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
708                                 /* pfmem */
709                                 len[count] = bar[count] & 0xFFFFFFF0;
710                                 len[count] = ~len[count] + 1;
711
712                                 debug ("len[count] in PFMEM = %x\n", len[count]);
713
714                                 bus_pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
715                                 if (!bus_pfmem[count]) {
716                                         err ("out of system memory\n");
717                                         retval = -ENOMEM;
718                                         goto error;
719                                 }
720                                 memset (bus_pfmem[count], 0, sizeof (struct resource_node));
721                                 bus_pfmem[count]->type = PFMEM;
722                                 bus_pfmem[count]->busno = func->busno;
723                                 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
724                                                         func->function);
725                                 bus_pfmem[count]->len = len[count];
726                                 bus_pfmem[count]->fromMem = FALSE;
727                                 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
728                                         ibmphp_add_resource (bus_pfmem[count]);
729                                         func->pfmem[count] = bus_pfmem[count];
730                                 } else {
731                                         mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
732                                         if (!mem_tmp) {
733                                                 err ("out of system memory\n");
734                                                 retval = -ENOMEM;
735                                                 goto error;
736                                         }
737                                         memset (mem_tmp, 0, sizeof (struct resource_node));
738                                         mem_tmp->type = MEM;
739                                         mem_tmp->busno = bus_pfmem[count]->busno;
740                                         mem_tmp->devfunc = bus_pfmem[count]->devfunc;
741                                         mem_tmp->len = bus_pfmem[count]->len;
742                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
743                                                 ibmphp_add_resource (mem_tmp);
744                                                 bus_pfmem[count]->fromMem = TRUE;
745                                                 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
746                                                 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
747                                                 func->pfmem[count] = bus_pfmem[count];
748                                         } else {
749                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n", 
750                                                      func->busno, func->device, len[count]);
751                                                 kfree (mem_tmp);
752                                                 kfree (bus_pfmem[count]);
753                                                 return -EIO;
754                                         }
755                                 }
756
757                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
758
759                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
760                                         /* takes up another dword */
761                                         count += 1;
762                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
763                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
764
765                                 }
766                         } else {
767                                 /* regular memory */
768                                 len[count] = bar[count] & 0xFFFFFFF0;
769                                 len[count] = ~len[count] + 1;
770
771                                 debug ("len[count] in Memory is %x\n", len[count]);
772
773                                 bus_mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
774                                 if (!bus_mem[count]) {
775                                         err ("out of system memory\n");
776                                         retval = -ENOMEM;
777                                         goto error;
778                                 }
779                                 memset (bus_mem[count], 0, sizeof (struct resource_node));
780                                 bus_mem[count]->type = MEM;
781                                 bus_mem[count]->busno = func->busno;
782                                 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
783                                                         func->function);
784                                 bus_mem[count]->len = len[count];
785                                 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
786                                         ibmphp_add_resource (bus_mem[count]);
787                                         func->mem[count] = bus_mem[count];
788                                 } else {
789                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
790                                              func->busno, func->device, len[count]);
791                                         kfree (bus_mem[count]);
792                                         return -EIO;
793                                 }
794
795                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
796
797                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
798                                         /* takes up another dword */
799                                         count += 1;
800                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
801                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
802
803                                 }
804                         }
805                 }               /* end of mem */
806         }                       /* end of for  */
807
808         /* Now need to see how much space the devices behind the bridge needed */
809         amount_needed = scan_behind_bridge (func, sec_number);
810         if (amount_needed == NULL)
811                 return -ENOMEM;
812
813         ibmphp_pci_bus->number = func->busno;
814         debug ("after coming back from scan_behind_bridge\n");
815         debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
816         debug ("amount_needed->io = %x\n", amount_needed->io);
817         debug ("amount_needed->mem = %x\n", amount_needed->mem);
818         debug ("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
819
820         if (amount_needed->not_correct) {               
821                 debug ("amount_needed is not correct\n");
822                 for (count = 0; address[count]; count++) {
823                         /* for 2 BARs */
824                         if (bus_io[count]) {
825                                 ibmphp_remove_resource (bus_io[count]);
826                                 func->io[count] = NULL;
827                         } else if (bus_pfmem[count]) {
828                                 ibmphp_remove_resource (bus_pfmem[count]);
829                                 func->pfmem[count] = NULL;
830                         } else if (bus_mem[count]) {
831                                 ibmphp_remove_resource (bus_mem[count]);
832                                 func->mem[count] = NULL;
833                         }
834                 }
835                 kfree (amount_needed);
836                 return -ENODEV;
837         }
838
839         if (!amount_needed->io) {
840                 debug ("it doesn't want IO?\n");
841                 flag_io = TRUE;
842         } else {
843                 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
844                 io = kmalloc(sizeof(*io), GFP_KERNEL);
845                 
846                 if (!io) {
847                         err ("out of system memory\n");
848                         retval = -ENOMEM;
849                         goto error;
850                 }
851                 memset (io, 0, sizeof (struct resource_node));
852                 io->type = IO;
853                 io->busno = func->busno;
854                 io->devfunc = PCI_DEVFN(func->device, func->function);
855                 io->len = amount_needed->io;
856                 if (ibmphp_check_resource (io, 1) == 0) {
857                         debug ("were we able to add io\n");
858                         ibmphp_add_resource (io);
859                         flag_io = TRUE;
860                 }
861         }
862
863         if (!amount_needed->mem) {
864                 debug ("it doesn't want n.e.memory?\n");
865                 flag_mem = TRUE;
866         } else {
867                 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
868                 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
869                 if (!mem) {
870                         err ("out of system memory\n");
871                         retval = -ENOMEM;
872                         goto error;
873                 }
874                 memset (mem, 0, sizeof (struct resource_node));
875                 mem->type = MEM;
876                 mem->busno = func->busno;
877                 mem->devfunc = PCI_DEVFN(func->device, func->function);
878                 mem->len = amount_needed->mem;
879                 if (ibmphp_check_resource (mem, 1) == 0) {
880                         ibmphp_add_resource (mem);
881                         flag_mem = TRUE;
882                         debug ("were we able to add mem\n");
883                 }
884         }
885
886         if (!amount_needed->pfmem) {
887                 debug ("it doesn't want n.e.pfmem mem?\n");
888                 flag_pfmem = TRUE;
889         } else {
890                 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
891                 pfmem = kmalloc(sizeof(*pfmem), GFP_KERNEL);
892                 if (!pfmem) {
893                         err ("out of system memory\n");
894                         retval = -ENOMEM;
895                         goto error;
896                 }
897                 memset (pfmem, 0, sizeof (struct resource_node));
898                 pfmem->type = PFMEM;
899                 pfmem->busno = func->busno;
900                 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
901                 pfmem->len = amount_needed->pfmem;
902                 pfmem->fromMem = FALSE;
903                 if (ibmphp_check_resource (pfmem, 1) == 0) {
904                         ibmphp_add_resource (pfmem);
905                         flag_pfmem = TRUE;
906                 } else {
907                         mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
908                         if (!mem_tmp) {
909                                 err ("out of system memory\n");
910                                 retval = -ENOMEM;
911                                 goto error;
912                         }
913                         memset (mem_tmp, 0, sizeof (struct resource_node));
914                         mem_tmp->type = MEM;
915                         mem_tmp->busno = pfmem->busno;
916                         mem_tmp->devfunc = pfmem->devfunc;
917                         mem_tmp->len = pfmem->len;
918                         if (ibmphp_check_resource (mem_tmp, 1) == 0) {
919                                 ibmphp_add_resource (mem_tmp);
920                                 pfmem->fromMem = TRUE;
921                                 pfmem->rangeno = mem_tmp->rangeno;
922                                 ibmphp_add_pfmem_from_mem (pfmem);
923                                 flag_pfmem = TRUE;
924                         }
925                 }
926         }
927
928         debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
929         debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
930
931         if (flag_io && flag_mem && flag_pfmem) {
932                 /* If on bootup, there was a bridged card in this slot,
933                  * then card was removed and ibmphp got unloaded and loaded
934                  * back again, there's no way for us to remove the bus
935                  * struct, so no need to kmalloc, can use existing node
936                  */
937                 bus = ibmphp_find_res_bus (sec_number);
938                 if (!bus) {
939                         bus = kmalloc(sizeof(*bus), GFP_KERNEL);
940                         if (!bus) {
941                                 err ("out of system memory\n");
942                                 retval = -ENOMEM;
943                                 goto error;
944                         }
945                         memset (bus, 0, sizeof (struct bus_node));
946                         bus->busno = sec_number;
947                         debug ("b4 adding new bus\n");
948                         rc = add_new_bus (bus, io, mem, pfmem, func->busno);
949                 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
950                         rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
951                 else {
952                         err ("expected bus structure not empty?\n");
953                         retval = -EIO;
954                         goto error;
955                 }
956                 if (rc) {
957                         if (rc == -ENOMEM) {
958                                 ibmphp_remove_bus (bus, func->busno);
959                                 kfree (amount_needed);
960                                 return rc;
961                         }
962                         retval = rc;
963                         goto error;
964                 }
965                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
966                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
967
968                 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
969                         debug ("io 32\n");
970                         need_io_upper = TRUE;
971                 }
972                 if ((io_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
973                         debug ("pfmem 64\n");
974                         need_pfmem_upper = TRUE;
975                 }
976
977                 if (bus->noIORanges) {
978                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
979                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8); 
980
981                         /* _______________This is for debugging purposes only ____________________
982                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
983                         debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
984                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
985                         debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
986                          ________________________________________________________________________*/
987
988                         if (need_io_upper) {    /* since can't support n.e.ways */
989                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
990                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
991                         }
992                 } else {
993                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
994                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
995                 }
996
997                 if (bus->noMemRanges) {
998                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
999                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
1000                         
1001                         /* ____________________This is for debugging purposes only ________________________
1002                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
1003                         debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1004                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
1005                         debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1006                          __________________________________________________________________________________*/
1007
1008                 } else {
1009                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
1010                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
1011                 }
1012                 if (bus->noPFMemRanges) {
1013                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
1014                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
1015
1016                         /* __________________________This is for debugging purposes only _______________________
1017                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1018                         debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1019                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1020                         debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1021                          ______________________________________________________________________________________*/
1022
1023                         if (need_pfmem_upper) { /* since can't support n.e.ways */
1024                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1025                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1026                         }
1027                 } else {
1028                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1029                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1030                 }
1031
1032                 debug ("b4 writing control information\n");
1033
1034                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1035                 if ((irq > 0x00) && (irq < 0x05))
1036                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1037                 /*    
1038                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1039                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1040                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1041                  */
1042
1043                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1044                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1045                 for (i = 0; i < 32; i++) {
1046                         if (amount_needed->devices[i]) {
1047                                 debug ("device where devices[i] is 1 = %x\n", i);
1048                                 func->devices[i] = 1;
1049                         }
1050                 }
1051                 func->bus = 1;  /* For unconfiguring, to indicate it's PPB */
1052                 func_passed = &func;
1053                 debug ("func->busno b4 returning is %x\n", func->busno);
1054                 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1055                 kfree (amount_needed);
1056                 return 0;
1057         } else {
1058                 err ("Configuring bridge was unsuccessful...\n");
1059                 mem_tmp = NULL;
1060                 retval = -EIO;
1061                 goto error;
1062         }
1063
1064 error:
1065         kfree(amount_needed);
1066         if (pfmem)
1067                 ibmphp_remove_resource (pfmem);
1068         if (io)
1069                 ibmphp_remove_resource (io);
1070         if (mem)
1071                 ibmphp_remove_resource (mem);
1072         for (i = 0; i < 2; i++) {       /* for 2 BARs */
1073                 if (bus_io[i]) {
1074                         ibmphp_remove_resource (bus_io[i]);
1075                         func->io[i] = NULL;
1076                 } else if (bus_pfmem[i]) {
1077                         ibmphp_remove_resource (bus_pfmem[i]);
1078                         func->pfmem[i] = NULL;
1079                 } else if (bus_mem[i]) {
1080                         ibmphp_remove_resource (bus_mem[i]);
1081                         func->mem[i] = NULL;
1082                 }
1083         }
1084         return retval;
1085 }
1086
1087 /*****************************************************************************
1088  * This function adds up the amount of resources needed behind the PPB bridge
1089  * and passes it to the configure_bridge function
1090  * Input: bridge function
1091  * Ouput: amount of resources needed
1092  *****************************************************************************/
1093 static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno)
1094 {
1095         int count, len[6];
1096         u16 vendor_id;
1097         u8 hdr_type;
1098         u8 device, function;
1099         unsigned int devfn;
1100         int howmany = 0;        /*this is to see if there are any devices behind the bridge */
1101
1102         u32 bar[6], class;
1103         u32 address[] = {
1104                 PCI_BASE_ADDRESS_0,
1105                 PCI_BASE_ADDRESS_1,
1106                 PCI_BASE_ADDRESS_2,
1107                 PCI_BASE_ADDRESS_3,
1108                 PCI_BASE_ADDRESS_4,
1109                 PCI_BASE_ADDRESS_5,
1110                 0
1111         };
1112         struct res_needed *amount;
1113
1114         amount = kmalloc(sizeof(*amount), GFP_KERNEL);
1115         if (amount == NULL)
1116                 return NULL;
1117         memset (amount, 0, sizeof (struct res_needed));
1118
1119         ibmphp_pci_bus->number = busno;
1120
1121         debug ("the bus_no behind the bridge is %x\n", busno);
1122         debug ("scanning devices behind the bridge...\n");
1123         for (device = 0; device < 32; device++) {
1124                 amount->devices[device] = 0;
1125                 for (function = 0; function < 8; function++) {
1126                         devfn = PCI_DEVFN(device, function);
1127
1128                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1129
1130                         if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1131                                 /* found correct device!!! */
1132                                 howmany++;
1133
1134                                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1135                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1136
1137                                 debug ("hdr_type behind the bridge is %x\n", hdr_type);
1138                                 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
1139                                         err ("embedded bridges not supported for hot-plugging.\n");
1140                                         amount->not_correct = TRUE;
1141                                         return amount;
1142                                 }
1143
1144                                 class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1145                                 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1146                                         err ("The device %x is VGA compatible and as is not supported for hot plugging. "
1147                                              "Please choose another device.\n", device);
1148                                         amount->not_correct = TRUE;
1149                                         return amount;
1150                                 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1151                                         err ("The device %x is not supported for hot plugging. "
1152                                              "Please choose another device.\n", device);
1153                                         amount->not_correct = TRUE;
1154                                         return amount;
1155                                 }
1156
1157                                 amount->devices[device] = 1;
1158
1159                                 for (count = 0; address[count]; count++) {
1160                                         /* for 6 BARs */
1161                                         /*
1162                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp);
1163                                         if (tmp & 0x01) // IO
1164                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1165                                         else // MEMORY
1166                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1167                                         */
1168                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1169                                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
1170
1171                                         debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
1172
1173                                         if (!bar[count])        /* This BAR is not implemented */
1174                                                 continue;
1175
1176                                         //tmp_bar = bar[count];
1177
1178                                         debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1179
1180                                         if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1181                                                 /* This is IO */
1182                                                 len[count] = bar[count] & 0xFFFFFFFC;
1183                                                 len[count] = ~len[count] + 1;
1184                                                 amount->io += len[count];
1185                                         } else {
1186                                                 /* This is Memory */
1187                                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1188                                                         /* pfmem */
1189                                                         len[count] = bar[count] & 0xFFFFFFF0;
1190                                                         len[count] = ~len[count] + 1;
1191                                                         amount->pfmem += len[count];
1192                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1193                                                                 /* takes up another dword */
1194                                                                 count += 1;
1195
1196                                                 } else {
1197                                                         /* regular memory */
1198                                                         len[count] = bar[count] & 0xFFFFFFF0;
1199                                                         len[count] = ~len[count] + 1;
1200                                                         amount->mem += len[count];
1201                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1202                                                                 /* takes up another dword */
1203                                                                 count += 1;
1204                                                         }
1205                                                 }
1206                                         }
1207                                 }       /* end for */
1208                         }       /* end if (valid) */
1209                 }       /* end for */
1210         }       /* end for */
1211
1212         if (!howmany)
1213                 amount->not_correct = TRUE;
1214         else
1215                 amount->not_correct = FALSE;
1216         if ((amount->io) && (amount->io < IOBRIDGE))
1217                 amount->io = IOBRIDGE;
1218         if ((amount->mem) && (amount->mem < MEMBRIDGE))
1219                 amount->mem = MEMBRIDGE;
1220         if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1221                 amount->pfmem = MEMBRIDGE;
1222         return amount;
1223 }
1224
1225 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card 
1226  * upon bootup in the system, since we don't allocate func to such case, we need to read 
1227  * the start addresses from pci config space and then find the corresponding entries in 
1228  * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1229  * Change: we also call these functions even if we configured the card ourselves (i.e., not
1230  * the bootup case), since it should work same way
1231  */
1232 static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
1233 {
1234         u32 start_address;
1235         u32 address[] = {
1236                 PCI_BASE_ADDRESS_0,
1237                 PCI_BASE_ADDRESS_1,
1238                 PCI_BASE_ADDRESS_2,
1239                 PCI_BASE_ADDRESS_3,
1240                 PCI_BASE_ADDRESS_4,
1241                 PCI_BASE_ADDRESS_5,
1242                 0
1243         };
1244         int count;
1245         struct resource_node *io;
1246         struct resource_node *mem;
1247         struct resource_node *pfmem;
1248         struct bus_node *bus;
1249         u32 end_address;
1250         u32 temp_end;
1251         u32 size;
1252         u32 tmp_address;
1253         unsigned int devfn;
1254
1255         debug ("%s - enter\n", __FUNCTION__);
1256
1257         bus = ibmphp_find_res_bus (busno);
1258         if (!bus) {
1259                 debug ("cannot find corresponding bus.\n");
1260                 return -EINVAL;
1261         }
1262
1263         devfn = PCI_DEVFN(device, function);
1264         ibmphp_pci_bus->number = busno;
1265         for (count = 0; address[count]; count++) {      /* for 6 BARs */
1266                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1267
1268                 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1269
1270                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1271                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size);
1272                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address);
1273
1274                 debug ("start_address is %x\n", start_address);
1275                 debug ("busno, device, function %x %x %x\n", busno, device, function);
1276                 if (!size) {
1277                         /* This BAR is not implemented */
1278                         debug ("is this bar no implemented?, count = %d\n", count);
1279                         continue;
1280                 }
1281                 tmp_address = start_address;
1282                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1283                         /* This is IO */
1284                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
1285                         size = size & 0xFFFFFFFC;
1286                         size = ~size + 1;
1287                         end_address = start_address + size - 1;
1288                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1289                                 err ("cannot find corresponding IO resource to remove\n");
1290                                 return -EIO;
1291                         }
1292                         debug ("io->start = %x\n", io->start);
1293                         temp_end = io->end;
1294                         start_address = io->end + 1;
1295                         ibmphp_remove_resource (io);
1296                         /* This is needed b/c of the old I/O restrictions in the BIOS */
1297                         while (temp_end < end_address) {
1298                                 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1299                                         err ("cannot find corresponding IO resource to remove\n");
1300                                         return -EIO;
1301                                 }
1302                                 debug ("io->start = %x\n", io->start);
1303                                 temp_end = io->end;
1304                                 start_address = io->end + 1;
1305                                 ibmphp_remove_resource (io);
1306                         }
1307
1308                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1309                 } else {
1310                         /* This is Memory */
1311                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1312                                 /* pfmem */
1313                                 debug ("start address of pfmem is %x\n", start_address);
1314                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1315
1316                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1317                                         err ("cannot find corresponding PFMEM resource to remove\n");
1318                                         return -EIO;
1319                                 }
1320                                 if (pfmem) {
1321                                         debug ("pfmem->start = %x\n", pfmem->start);
1322
1323                                         ibmphp_remove_resource(pfmem);
1324                                 }
1325                         } else {
1326                                 /* regular memory */
1327                                 debug ("start address of mem is %x\n", start_address);
1328                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1329
1330                                 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1331                                         err ("cannot find corresponding MEM resource to remove\n");
1332                                         return -EIO;
1333                                 }
1334                                 if (mem) {
1335                                         debug ("mem->start = %x\n", mem->start);
1336
1337                                         ibmphp_remove_resource(mem);
1338                                 }
1339                         }
1340                         if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1341                                 /* takes up another dword */
1342                                 count += 1;
1343                         }
1344                 }       /* end of mem */
1345         }       /* end of for */
1346
1347         return 0;
1348 }
1349
1350 static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
1351 {
1352         int count;
1353         int bus_no, pri_no, sub_no, sec_no = 0;
1354         u32 start_address, tmp_address;
1355         u8 sec_number, sub_number, pri_number;
1356         struct resource_node *io = NULL;
1357         struct resource_node *mem = NULL;
1358         struct resource_node *pfmem = NULL;
1359         struct bus_node *bus;
1360         u32 address[] = {
1361                 PCI_BASE_ADDRESS_0,
1362                 PCI_BASE_ADDRESS_1,
1363                 0
1364         };
1365         unsigned int devfn;
1366
1367         devfn = PCI_DEVFN(device, function);
1368         ibmphp_pci_bus->number = busno;
1369         bus_no = (int) busno;
1370         debug ("busno is %x\n", busno);
1371         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1372         debug ("%s - busno = %x, primary_number = %x\n", __FUNCTION__, busno, pri_number);
1373
1374         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1375         debug ("sec_number is %x\n", sec_number);
1376         sec_no = (int) sec_number;
1377         pri_no = (int) pri_number;
1378         if (pri_no != bus_no) {
1379                 err ("primary numbers in our structures and pci config space don't match.\n");
1380                 return -EINVAL;
1381         }
1382
1383         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1384         sub_no = (int) sub_number;
1385         debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1386         if (sec_no != sub_number) {
1387                 err ("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1388                 return -ENODEV;
1389         }
1390
1391         bus = ibmphp_find_res_bus (sec_number);
1392         debug ("bus->busno is %x\n", bus->busno);
1393         debug ("sec_number is %x\n", sec_number);
1394         if (!bus) {
1395                 err ("cannot find Bus structure for the bridged device\n");
1396                 return -EINVAL;
1397         }
1398
1399         ibmphp_remove_bus (bus, busno);
1400
1401         for (count = 0; address[count]; count++) {
1402                 /* for 2 BARs */
1403                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1404
1405                 if (!start_address) {
1406                         /* This BAR is not implemented */
1407                         continue;
1408                 }
1409
1410                 tmp_address = start_address;
1411
1412                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1413                         /* This is IO */
1414                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
1415                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1416                                 err ("cannot find corresponding IO resource to remove\n");
1417                                 return -EIO;
1418                         }
1419                         if (io)
1420                                 debug ("io->start = %x\n", io->start);
1421
1422                         ibmphp_remove_resource (io);
1423
1424                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1425                 } else {
1426                         /* This is Memory */
1427                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1428                                 /* pfmem */
1429                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1430                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1431                                         err ("cannot find corresponding PFMEM resource to remove\n");
1432                                         return -EINVAL;
1433                                 }
1434                                 if (pfmem) {
1435                                         debug ("pfmem->start = %x\n", pfmem->start);
1436
1437                                         ibmphp_remove_resource(pfmem);
1438                                 }
1439                         } else {
1440                                 /* regular memory */
1441                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1442                                 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1443                                         err ("cannot find corresponding MEM resource to remove\n");
1444                                         return -EINVAL;
1445                                 }
1446                                 if (mem) {
1447                                         debug ("mem->start = %x\n", mem->start);
1448
1449                                         ibmphp_remove_resource(mem);
1450                                 }
1451                         }
1452                         if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1453                                 /* takes up another dword */
1454                                 count += 1;
1455                         }
1456                 }       /* end of mem */
1457         }       /* end of for */
1458         debug ("%s - exiting, returning success\n", __FUNCTION__);
1459         return 0;
1460 }
1461
1462 static int unconfigure_boot_card (struct slot *slot_cur)
1463 {
1464         u16 vendor_id;
1465         u32 class;
1466         u8 hdr_type;
1467         u8 device;
1468         u8 busno;
1469         u8 function;
1470         int rc;
1471         unsigned int devfn;
1472         u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1473
1474         debug ("%s - enter\n", __FUNCTION__);
1475
1476         device = slot_cur->device;
1477         busno = slot_cur->bus;
1478
1479         debug ("b4 for loop, device is %x\n", device);
1480         /* For every function on the card */
1481         for (function = 0x0; function < 0x08; function++) {
1482                 devfn = PCI_DEVFN(device, function);
1483                 ibmphp_pci_bus->number = busno;
1484
1485                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1486
1487                 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1488                         /* found correct device!!! */
1489                         ++valid_device;
1490
1491                         debug ("%s - found correct device\n", __FUNCTION__);
1492
1493                         /* header: x x x x x x x x
1494                          *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1495                          *         |_=> 0 = single function device, 1 = multi-function device
1496                          */
1497
1498                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1499                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1500
1501                         debug ("hdr_type %x, class %x\n", hdr_type, class);
1502                         class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1503                         if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1504                                 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
1505                                      "Please choose another device.\n", device, function);
1506                                 return -ENODEV;
1507                         } else if (class == PCI_CLASS_DISPLAY_VGA) {
1508                                 err ("The device %x function %x is not supported for hot removing. "
1509                                      "Please choose another device.\n", device, function);
1510                                 return -ENODEV;
1511                         }
1512
1513                         switch (hdr_type) {
1514                                 case PCI_HEADER_TYPE_NORMAL:
1515                                         rc = unconfigure_boot_device (busno, device, function);
1516                                         if (rc) {
1517                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1518                                                      device, function, busno);
1519                                                 return rc;
1520                                         }
1521                                         function = 0x8;
1522                                         break;
1523                                 case PCI_HEADER_TYPE_MULTIDEVICE:
1524                                         rc = unconfigure_boot_device (busno, device, function);
1525                                         if (rc) {
1526                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1527                                                      device, function, busno);
1528                                                 return rc;
1529                                         }
1530                                         break;
1531                                 case PCI_HEADER_TYPE_BRIDGE:
1532                                         class >>= 8;
1533                                         if (class != PCI_CLASS_BRIDGE_PCI) {
1534                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
1535                                                      "and is not supported for hot-removing. "
1536                                                      "Please try another card.\n", device, function);
1537                                                 return -ENODEV;
1538                                         }
1539                                         rc = unconfigure_boot_bridge (busno, device, function);
1540                                         if (rc != 0) {
1541                                                 err ("was not able to hot-remove PPB properly.\n");
1542                                                 return rc;
1543                                         }
1544
1545                                         function = 0x8;
1546                                         break;
1547                                 case PCI_HEADER_TYPE_MULTIBRIDGE:
1548                                         class >>= 8;
1549                                         if (class != PCI_CLASS_BRIDGE_PCI) {
1550                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
1551                                                      "and is not supported for hot-removing. "
1552                                                      "Please try another card.\n", device, function);
1553                                                 return -ENODEV;
1554                                         }
1555                                         rc = unconfigure_boot_bridge (busno, device, function);
1556                                         if (rc != 0) {
1557                                                 err ("was not able to hot-remove PPB properly.\n");
1558                                                 return rc;
1559                                         }
1560                                         break;
1561                                 default:
1562                                         err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
1563                                         return -1;
1564                                         break;
1565                         }       /* end of switch */
1566                 }       /* end of valid device */
1567         }       /* end of for */
1568
1569         if (!valid_device) {
1570                 err ("Could not find device to unconfigure.  Or could not read the card.\n");
1571                 return -1;
1572         }
1573         return 0;
1574 }
1575
1576 /*
1577  * free the resources of the card (multi, single, or bridged)
1578  * Parameters: slot, flag to say if this is for removing entire module or just
1579  * unconfiguring the device
1580  * TO DO:  will probably need to add some code in case there was some resource,
1581  * to remove it... this is from when we have errors in the configure_card...
1582  *                      !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1583  * Returns: 0, -1, -ENODEV 
1584  */
1585 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1586 {
1587         int i;
1588         int count;
1589         int rc;
1590         struct slot *sl = *slot_cur;
1591         struct pci_func *cur_func = NULL;
1592         struct pci_func *temp_func;
1593
1594         debug ("%s - enter\n", __FUNCTION__);
1595
1596         if (!the_end) {
1597                 /* Need to unconfigure the card */
1598                 rc = unconfigure_boot_card (sl);
1599                 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1600                         /* In all other cases, will still need to get rid of func structure if it exists */
1601                         return rc;
1602                 }
1603         }
1604
1605         if (sl->func) {
1606                 cur_func = sl->func;
1607                 while (cur_func) {
1608                         /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1609                         if (cur_func->bus) {
1610                                 /* in other words, it's a PPB */
1611                                 count = 2;
1612                         } else {
1613                                 count = 6;
1614                         }
1615
1616                         for (i = 0; i < count; i++) {
1617                                 if (cur_func->io[i]) {
1618                                         debug ("io[%d] exists\n", i);
1619                                         if (the_end > 0)
1620                                                 ibmphp_remove_resource (cur_func->io[i]);
1621                                         cur_func->io[i] = NULL;
1622                                 }
1623                                 if (cur_func->mem[i]) {
1624                                         debug ("mem[%d] exists\n", i);
1625                                         if (the_end > 0)
1626                                                 ibmphp_remove_resource (cur_func->mem[i]);
1627                                         cur_func->mem[i] = NULL;
1628                                 }
1629                                 if (cur_func->pfmem[i]) {
1630                                         debug ("pfmem[%d] exists\n", i);
1631                                         if (the_end > 0)
1632                                                 ibmphp_remove_resource (cur_func->pfmem[i]);
1633                                         cur_func->pfmem[i] = NULL;
1634                                 }
1635                         }
1636
1637                         temp_func = cur_func->next;
1638                         kfree (cur_func);
1639                         cur_func = temp_func;
1640                 }
1641         }
1642
1643         sl->func = NULL;
1644         *slot_cur = sl;
1645         debug ("%s - exit\n", __FUNCTION__);
1646         return 0;
1647 }
1648
1649 /*
1650  * add a new bus resulting from hot-plugging a PPB bridge with devices
1651  *
1652  * Input: bus and the amount of resources needed (we know we can assign those,
1653  *        since they've been checked already
1654  * Output: bus added to the correct spot
1655  *         0, -1, error 
1656  */
1657 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1658 {
1659         struct range_node *io_range = NULL;
1660         struct range_node *mem_range = NULL;
1661         struct range_node *pfmem_range = NULL;
1662         struct bus_node *cur_bus = NULL;
1663
1664         /* Trying to find the parent bus number */
1665         if (parent_busno != 0xFF) {
1666                 cur_bus = ibmphp_find_res_bus (parent_busno);
1667                 if (!cur_bus) {
1668                         err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1669                         return -ENODEV;
1670                 }
1671         
1672                 list_add (&bus->bus_list, &cur_bus->bus_list);
1673         }
1674         if (io) {
1675                 io_range = kmalloc(sizeof(*io_range), GFP_KERNEL);
1676                 if (!io_range) {
1677                         err ("out of system memory\n");
1678                         return -ENOMEM;
1679                 }
1680                 memset (io_range, 0, sizeof (struct range_node));
1681                 io_range->start = io->start;
1682                 io_range->end = io->end;
1683                 io_range->rangeno = 1;
1684                 bus->noIORanges = 1;
1685                 bus->rangeIO = io_range;
1686         }
1687         if (mem) {
1688                 mem_range = kmalloc(sizeof(*mem_range), GFP_KERNEL);
1689                 if (!mem_range) {
1690                         err ("out of system memory\n");
1691                         return -ENOMEM;
1692                 }
1693                 memset (mem_range, 0, sizeof (struct range_node));
1694                 mem_range->start = mem->start;
1695                 mem_range->end = mem->end;
1696                 mem_range->rangeno = 1;
1697                 bus->noMemRanges = 1;
1698                 bus->rangeMem = mem_range;
1699         }
1700         if (pfmem) {
1701                 pfmem_range = kmalloc(sizeof(*pfmem_range), GFP_KERNEL);
1702                 if (!pfmem_range) {     
1703                         err ("out of system memory\n");
1704                         return -ENOMEM;
1705                 }
1706                 memset (pfmem_range, 0, sizeof (struct range_node));
1707                 pfmem_range->start = pfmem->start;
1708                 pfmem_range->end = pfmem->end;
1709                 pfmem_range->rangeno = 1;
1710                 bus->noPFMemRanges = 1;
1711                 bus->rangePFMem = pfmem_range;
1712         }
1713         return 0;
1714 }
1715
1716 /*
1717  * find the 1st available bus number for PPB to set as its secondary bus
1718  * Parameters: bus_number of the primary bus
1719  * Returns: bus_number of the secondary bus or 0xff in case of failure
1720  */
1721 static u8 find_sec_number (u8 primary_busno, u8 slotno)
1722 {
1723         int min, max;
1724         u8 busno;
1725         struct bus_info *bus;
1726         struct bus_node *bus_cur;
1727
1728         bus = ibmphp_find_same_bus_num (primary_busno);
1729         if (!bus) {
1730                 err ("cannot get slot range of the bus from the BIOS\n");
1731                 return 0xff;
1732         }
1733         max = bus->slot_max;
1734         min = bus->slot_min;
1735         if ((slotno > max) || (slotno < min)) {
1736                 err ("got the wrong range\n");
1737                 return 0xff;
1738         }
1739         busno = (u8) (slotno - (u8) min);
1740         busno += primary_busno + 0x01;
1741         bus_cur = ibmphp_find_res_bus (busno);
1742         /* either there is no such bus number, or there are no ranges, which
1743          * can only happen if we removed the bridged device in previous load
1744          * of the driver, and now only have the skeleton bus struct
1745          */
1746         if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1747                 return busno;
1748         return 0xff;
1749 }
1750