ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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 = (struct pci_func *) kmalloc (sizeof (struct pci_func), 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 = (struct pci_func *) kmalloc (sizeof (struct pci_func), 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 = (struct pci_func *) kmalloc (sizeof (struct pci_func), 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 = (struct pci_func *) kmalloc (sizeof (struct pci_func), 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 = ((func->device << 3) | (func->function & 0x7));
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 = ((func->device << 3) | (func->function & 0x7));
456                                 pfmem[count]->len = len[count];
457                                 pfmem[count]->fromMem = FALSE;
458                                 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
459                                         ibmphp_add_resource (pfmem[count]);
460                                         func->pfmem[count] = pfmem[count];
461                                 } else {
462                                         mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
463                                         if (!mem_tmp) {
464                                                 err ("out of system memory\n");
465                                                 kfree (pfmem[count]);
466                                                 return -ENOMEM;
467                                         }
468                                         memset (mem_tmp, 0, sizeof (struct resource_node));
469                                         mem_tmp->type = MEM;
470                                         mem_tmp->busno = pfmem[count]->busno;
471                                         mem_tmp->devfunc = pfmem[count]->devfunc;
472                                         mem_tmp->len = pfmem[count]->len;
473                                         debug ("there's no pfmem... going into mem.\n");
474                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
475                                                 ibmphp_add_resource (mem_tmp);
476                                                 pfmem[count]->fromMem = TRUE;
477                                                 pfmem[count]->rangeno = mem_tmp->rangeno;
478                                                 pfmem[count]->start = mem_tmp->start;
479                                                 pfmem[count]->end = mem_tmp->end;
480                                                 ibmphp_add_pfmem_from_mem (pfmem[count]);
481                                                 func->pfmem[count] = pfmem[count];
482                                         } else {
483                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
484                                                      func->busno, func->device, len[count]);
485                                                 kfree (mem_tmp);
486                                                 kfree (pfmem[count]);
487                                                 return -EIO;
488                                         }
489                                 }
490
491                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
492
493                                 /*_______________This is for debugging purposes only______________________________*/                            
494                                 debug ("b4 writing, start address is %x\n", func->pfmem[count]->start);
495                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
496                                 debug ("after writing, start address is %x\n", bar[count]);
497                                 /*_________________________________________________________________________________*/
498
499                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {        /* takes up another dword */
500                                         debug ("inside the mem 64 case, count %d\n", count);
501                                         count += 1;
502                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
503                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
504                                 }
505                         } else {
506                                 /* regular memory */
507                                 debug ("REGULAR MEM SPACE\n");
508
509                                 len[count] = bar[count] & 0xFFFFFFF0;
510                                 len[count] = ~len[count] + 1;
511
512                                 debug ("len[count] in Mem %x, count %d\n", len[count], count);
513
514                                 mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
515                                 if (!mem[count]) {
516                                         err ("out of system memory\n");
517                                         return -ENOMEM;
518                                 }
519                                 memset (mem[count], 0, sizeof (struct resource_node));
520                                 mem[count]->type = MEM;
521                                 mem[count]->busno = func->busno;
522                                 mem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
523                                 mem[count]->len = len[count];
524                                 if (ibmphp_check_resource (mem[count], 0) == 0) {
525                                         ibmphp_add_resource (mem[count]);
526                                         func->mem[count] = mem[count];
527                                 } else {
528                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
529                                              func->busno, func->device, len[count]);
530                                         kfree (mem[count]);
531                                         return -EIO;
532                                 }
533                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
534                                 /* _______________________This is for debugging purposes only _______________________*/
535                                 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
536                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
537                                 debug ("after writing, the address is %x\n", bar[count]);
538                                 /* __________________________________________________________________________________*/
539
540                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
541                                         /* takes up another dword */
542                                         debug ("inside mem 64 case, reg. mem, count %d\n", count);
543                                         count += 1;
544                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
545                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
546                                 }
547                         }
548                 }               /* end of mem */
549         }                       /* end of for */
550
551         func->bus = 0;          /* To indicate that this is not a PPB */
552         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
553         if ((irq > 0x00) && (irq < 0x05))
554                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
555
556         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
557         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
558
559         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
560         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
561
562         return 0;
563 }
564
565 /******************************************************************************
566  * This routine configures a PCI-2-PCI bridge and the functions behind it
567  * Parameters: pci_func
568  * Returns: 
569  ******************************************************************************/
570 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
571 {
572         int count;
573         int i;
574         int rc;
575         u8 sec_number;
576         u8 io_base;
577         u16 pfmem_base;
578         u32 bar[2];
579         u32 len[2];
580         u8 flag_io = FALSE;
581         u8 flag_mem = FALSE;
582         u8 flag_pfmem = FALSE;
583         u8 need_io_upper = FALSE;
584         u8 need_pfmem_upper = FALSE;
585         struct res_needed *amount_needed = NULL;
586         struct resource_node *io = NULL;
587         struct resource_node *bus_io[2] = {NULL, NULL};
588         struct resource_node *mem = NULL;
589         struct resource_node *bus_mem[2] = {NULL, NULL};
590         struct resource_node *mem_tmp = NULL;
591         struct resource_node *pfmem = NULL;
592         struct resource_node *bus_pfmem[2] = {NULL, NULL};
593         struct bus_node *bus;
594         u32 address[] = {
595                 PCI_BASE_ADDRESS_0,
596                 PCI_BASE_ADDRESS_1,
597                 0
598         };
599         struct pci_func *func = *func_passed;
600         unsigned int devfn;
601         u8 irq;
602         int retval;
603
604         debug ("%s - enter\n", __FUNCTION__);
605
606         devfn = PCI_DEVFN(func->function, func->device);
607         ibmphp_pci_bus->number = func->busno;
608
609         /* Configuring necessary info for the bridge so that we could see the devices
610          * behind it
611          */
612
613         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
614
615         /* _____________________For debugging purposes only __________________________
616         pci_bus_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
617         debug ("primary # written into the bridge is %x\n", pri_number);
618          ___________________________________________________________________________*/
619
620         /* in EBDA, only get allocated 1 additional bus # per slot */
621         sec_number = find_sec_number (func->busno, slotno);
622         if (sec_number == 0xff) {
623                 err ("cannot allocate secondary bus number for the bridged device\n");
624                 return -EINVAL;
625         }
626
627         debug ("after find_sec_number, the number we got is %x\n", sec_number);
628         debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
629
630         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
631         
632         /* __________________For debugging purposes only __________________________________
633         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
634         debug ("sec_number after write/read is %x\n", sec_number);
635          ________________________________________________________________________________*/
636
637         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
638
639         /* __________________For debugging purposes only ____________________________________
640         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
641         debug ("subordinate number after write/read is %x\n", sec_number);
642          __________________________________________________________________________________*/
643
644         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
645         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
646         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
647
648         debug ("func->busno is %x\n", func->busno);
649         debug ("sec_number after writing is %x\n", sec_number);
650
651
652         /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
653            !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!! 
654            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
655
656
657         /* First we need to allocate mem/io for the bridge itself in case it needs it */
658         for (count = 0; address[count]; count++) {      /* for 2 BARs */
659                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
660                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
661
662                 if (!bar[count]) {
663                         /* This BAR is not implemented */
664                         debug ("so we come here then, eh?, count = %d\n", count);
665                         continue;
666                 }
667                 //  tmp_bar = bar[count];
668
669                 debug ("Bar %d wants %x\n", count, bar[count]);
670
671                 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
672                         /* This is IO */
673                         len[count] = bar[count] & 0xFFFFFFFC;
674                         len[count] = ~len[count] + 1;
675
676                         debug ("len[count] in IO = %x\n", len[count]);
677
678                         bus_io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
679                 
680                         if (!bus_io[count]) {
681                                 err ("out of system memory\n");
682                                 retval = -ENOMEM;
683                                 goto error;
684                         }
685                         memset (bus_io[count], 0, sizeof (struct resource_node));
686                         bus_io[count]->type = IO;
687                         bus_io[count]->busno = func->busno;
688                         bus_io[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
689                         bus_io[count]->len = len[count];
690                         if (ibmphp_check_resource (bus_io[count], 0) == 0) {
691                                 ibmphp_add_resource (bus_io[count]);
692                                 func->io[count] = bus_io[count];
693                         } else {
694                                 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
695                                      func->busno, func->device, len[count]);
696                                 kfree (bus_io[count]);
697                                 return -EIO;
698                         }
699
700                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
701
702                 } else {
703                         /* This is Memory */
704                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
705                                 /* pfmem */
706                                 len[count] = bar[count] & 0xFFFFFFF0;
707                                 len[count] = ~len[count] + 1;
708
709                                 debug ("len[count] in PFMEM = %x\n", len[count]);
710
711                                 bus_pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
712                                 if (!bus_pfmem[count]) {
713                                         err ("out of system memory\n");
714                                         retval = -ENOMEM;
715                                         goto error;
716                                 }
717                                 memset (bus_pfmem[count], 0, sizeof (struct resource_node));
718                                 bus_pfmem[count]->type = PFMEM;
719                                 bus_pfmem[count]->busno = func->busno;
720                                 bus_pfmem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
721                                 bus_pfmem[count]->len = len[count];
722                                 bus_pfmem[count]->fromMem = FALSE;
723                                 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
724                                         ibmphp_add_resource (bus_pfmem[count]);
725                                         func->pfmem[count] = bus_pfmem[count];
726                                 } else {
727                                         mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
728                                         if (!mem_tmp) {
729                                                 err ("out of system memory\n");
730                                                 retval = -ENOMEM;
731                                                 goto error;
732                                         }
733                                         memset (mem_tmp, 0, sizeof (struct resource_node));
734                                         mem_tmp->type = MEM;
735                                         mem_tmp->busno = bus_pfmem[count]->busno;
736                                         mem_tmp->devfunc = bus_pfmem[count]->devfunc;
737                                         mem_tmp->len = bus_pfmem[count]->len;
738                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
739                                                 ibmphp_add_resource (mem_tmp);
740                                                 bus_pfmem[count]->fromMem = TRUE;
741                                                 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
742                                                 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
743                                                 func->pfmem[count] = bus_pfmem[count];
744                                         } else {
745                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n", 
746                                                      func->busno, func->device, len[count]);
747                                                 kfree (mem_tmp);
748                                                 kfree (bus_pfmem[count]);
749                                                 return -EIO;
750                                         }
751                                 }
752
753                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
754
755                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
756                                         /* takes up another dword */
757                                         count += 1;
758                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
759                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
760
761                                 }
762                         } else {
763                                 /* regular memory */
764                                 len[count] = bar[count] & 0xFFFFFFF0;
765                                 len[count] = ~len[count] + 1;
766
767                                 debug ("len[count] in Memory is %x\n", len[count]);
768
769                                 bus_mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
770                                 if (!bus_mem[count]) {
771                                         err ("out of system memory\n");
772                                         retval = -ENOMEM;
773                                         goto error;
774                                 }
775                                 memset (bus_mem[count], 0, sizeof (struct resource_node));
776                                 bus_mem[count]->type = MEM;
777                                 bus_mem[count]->busno = func->busno;
778                                 bus_mem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
779                                 bus_mem[count]->len = len[count];
780                                 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
781                                         ibmphp_add_resource (bus_mem[count]);
782                                         func->mem[count] = bus_mem[count];
783                                 } else {
784                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
785                                              func->busno, func->device, len[count]);
786                                         kfree (bus_mem[count]);
787                                         return -EIO;
788                                 }
789
790                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
791
792                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
793                                         /* takes up another dword */
794                                         count += 1;
795                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
796                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
797
798                                 }
799                         }
800                 }               /* end of mem */
801         }                       /* end of for  */
802
803         /* Now need to see how much space the devices behind the bridge needed */
804         amount_needed = scan_behind_bridge (func, sec_number);
805         if (amount_needed == NULL)
806                 return -ENOMEM;
807
808         ibmphp_pci_bus->number = func->busno;
809         debug ("after coming back from scan_behind_bridge\n");
810         debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
811         debug ("amount_needed->io = %x\n", amount_needed->io);
812         debug ("amount_needed->mem = %x\n", amount_needed->mem);
813         debug ("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
814
815         if (amount_needed->not_correct) {               
816                 debug ("amount_needed is not correct\n");
817                 for (count = 0; address[count]; count++) {
818                         /* for 2 BARs */
819                         if (bus_io[count]) {
820                                 ibmphp_remove_resource (bus_io[count]);
821                                 func->io[count] = NULL;
822                         } else if (bus_pfmem[count]) {
823                                 ibmphp_remove_resource (bus_pfmem[count]);
824                                 func->pfmem[count] = NULL;
825                         } else if (bus_mem[count]) {
826                                 ibmphp_remove_resource (bus_mem[count]);
827                                 func->mem[count] = NULL;
828                         }
829                 }
830                 kfree (amount_needed);
831                 return -ENODEV;
832         }
833
834         if (!amount_needed->io) {
835                 debug ("it doesn't want IO?\n");
836                 flag_io = TRUE;
837         } else {
838                 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
839                 io = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
840                 
841                 if (!io) {
842                         err ("out of system memory\n");
843                         retval = -ENOMEM;
844                         goto error;
845                 }
846                 memset (io, 0, sizeof (struct resource_node));
847                 io->type = IO;
848                 io->busno = func->busno;
849                 io->devfunc = ((func->device << 3) | (func->function & 0x7));
850                 io->len = amount_needed->io;
851                 if (ibmphp_check_resource (io, 1) == 0) {
852                         debug ("were we able to add io\n");
853                         ibmphp_add_resource (io);
854                         flag_io = TRUE;
855                 }
856         }
857
858         if (!amount_needed->mem) {
859                 debug ("it doesn't want n.e.memory?\n");
860                 flag_mem = TRUE;
861         } else {
862                 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
863                 mem = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
864                 if (!mem) {
865                         err ("out of system memory\n");
866                         retval = -ENOMEM;
867                         goto error;
868                 }
869                 memset (mem, 0, sizeof (struct resource_node));
870                 mem->type = MEM;
871                 mem->busno = func->busno;
872                 mem->devfunc = ((func->device << 3) | (func->function & 0x7));
873                 mem->len = amount_needed->mem;
874                 if (ibmphp_check_resource (mem, 1) == 0) {
875                         ibmphp_add_resource (mem);
876                         flag_mem = TRUE;
877                         debug ("were we able to add mem\n");
878                 }
879         }
880
881         if (!amount_needed->pfmem) {
882                 debug ("it doesn't want n.e.pfmem mem?\n");
883                 flag_pfmem = TRUE;
884         } else {
885                 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
886                 pfmem = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
887                 if (!pfmem) {
888                         err ("out of system memory\n");
889                         retval = -ENOMEM;
890                         goto error;
891                 }
892                 memset (pfmem, 0, sizeof (struct resource_node));
893                 pfmem->type = PFMEM;
894                 pfmem->busno = func->busno;
895                 pfmem->devfunc = ((func->device << 3) | (func->function & 0x7));
896                 pfmem->len = amount_needed->pfmem;
897                 pfmem->fromMem = FALSE;
898                 if (ibmphp_check_resource (pfmem, 1) == 0) {
899                         ibmphp_add_resource (pfmem);
900                         flag_pfmem = TRUE;
901                 } else {
902                         mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
903                         if (!mem_tmp) {
904                                 err ("out of system memory\n");
905                                 retval = -ENOMEM;
906                                 goto error;
907                         }
908                         memset (mem_tmp, 0, sizeof (struct resource_node));
909                         mem_tmp->type = MEM;
910                         mem_tmp->busno = pfmem->busno;
911                         mem_tmp->devfunc = pfmem->devfunc;
912                         mem_tmp->len = pfmem->len;
913                         if (ibmphp_check_resource (mem_tmp, 1) == 0) {
914                                 ibmphp_add_resource (mem_tmp);
915                                 pfmem->fromMem = TRUE;
916                                 pfmem->rangeno = mem_tmp->rangeno;
917                                 ibmphp_add_pfmem_from_mem (pfmem);
918                                 flag_pfmem = TRUE;
919                         }
920                 }
921         }
922
923         debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
924         debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
925
926         if (flag_io && flag_mem && flag_pfmem) {
927                 /* If on bootup, there was a bridged card in this slot,
928                  * then card was removed and ibmphp got unloaded and loaded
929                  * back again, there's no way for us to remove the bus
930                  * struct, so no need to kmalloc, can use existing node
931                  */
932                 bus = ibmphp_find_res_bus (sec_number);
933                 if (!bus) {
934                         bus = kmalloc (sizeof (struct bus_node), GFP_KERNEL);
935                         if (!bus) {
936                                 err ("out of system memory\n");
937                                 retval = -ENOMEM;
938                                 goto error;
939                         }
940                         memset (bus, 0, sizeof (struct bus_node));
941                         bus->busno = sec_number;
942                         debug ("b4 adding new bus\n");
943                         rc = add_new_bus (bus, io, mem, pfmem, func->busno);
944                 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
945                         rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
946                 else {
947                         err ("expected bus structure not empty?\n");
948                         retval = -EIO;
949                         goto error;
950                 }
951                 if (rc) {
952                         if (rc == -ENOMEM) {
953                                 ibmphp_remove_bus (bus, func->busno);
954                                 kfree (amount_needed);
955                                 return rc;
956                         }
957                         retval = rc;
958                         goto error;
959                 }
960                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
961                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
962
963                 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
964                         debug ("io 32\n");
965                         need_io_upper = TRUE;
966                 }
967                 if ((io_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
968                         debug ("pfmem 64\n");
969                         need_pfmem_upper = TRUE;
970                 }
971
972                 if (bus->noIORanges) {
973                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
974                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8); 
975
976                         /* _______________This is for debugging purposes only ____________________
977                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
978                         debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
979                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
980                         debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
981                          ________________________________________________________________________*/
982
983                         if (need_io_upper) {    /* since can't support n.e.ways */
984                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
985                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
986                         }
987                 } else {
988                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
989                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
990                 }
991
992                 if (bus->noMemRanges) {
993                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
994                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
995                         
996                         /* ____________________This is for debugging purposes only ________________________
997                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
998                         debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
999                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
1000                         debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1001                          __________________________________________________________________________________*/
1002
1003                 } else {
1004                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
1005                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
1006                 }
1007                 if (bus->noPFMemRanges) {
1008                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
1009                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
1010
1011                         /* __________________________This is for debugging purposes only _______________________
1012                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1013                         debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1014                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1015                         debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1016                          ______________________________________________________________________________________*/
1017
1018                         if (need_pfmem_upper) { /* since can't support n.e.ways */
1019                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1020                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1021                         }
1022                 } else {
1023                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1024                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1025                 }
1026
1027                 debug ("b4 writing control information\n");
1028
1029                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1030                 if ((irq > 0x00) && (irq < 0x05))
1031                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1032                 /*    
1033                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1034                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1035                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1036                  */
1037
1038                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1039                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1040                 for (i = 0; i < 32; i++) {
1041                         if (amount_needed->devices[i]) {
1042                                 debug ("device where devices[i] is 1 = %x\n", i);
1043                                 func->devices[i] = 1;
1044                         }
1045                 }
1046                 func->bus = 1;  /* For unconfiguring, to indicate it's PPB */
1047                 func_passed = &func;
1048                 debug ("func->busno b4 returning is %x\n", func->busno);
1049                 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1050                 kfree (amount_needed);
1051                 return 0;
1052         } else {
1053                 err ("Configuring bridge was unsuccessful...\n");
1054                 mem_tmp = NULL;
1055                 retval = -EIO;
1056                 goto error;
1057         }
1058
1059 error:
1060         if (amount_needed)
1061                 kfree (amount_needed);
1062         if (pfmem)
1063                 ibmphp_remove_resource (pfmem);
1064         if (io)
1065                 ibmphp_remove_resource (io);
1066         if (mem)
1067                 ibmphp_remove_resource (mem);
1068         for (i = 0; i < 2; i++) {       /* for 2 BARs */
1069                 if (bus_io[i]) {
1070                         ibmphp_remove_resource (bus_io[i]);
1071                         func->io[i] = NULL;
1072                 } else if (bus_pfmem[i]) {
1073                         ibmphp_remove_resource (bus_pfmem[i]);
1074                         func->pfmem[i] = NULL;
1075                 } else if (bus_mem[i]) {
1076                         ibmphp_remove_resource (bus_mem[i]);
1077                         func->mem[i] = NULL;
1078                 }
1079         }
1080         return retval;
1081 }
1082
1083 /*****************************************************************************
1084  * This function adds up the amount of resources needed behind the PPB bridge
1085  * and passes it to the configure_bridge function
1086  * Input: bridge function
1087  * Ouput: amount of resources needed
1088  *****************************************************************************/
1089 static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno)
1090 {
1091         int count, len[6];
1092         u16 vendor_id;
1093         u8 hdr_type;
1094         u8 device, function;
1095         unsigned int devfn;
1096         int howmany = 0;        /*this is to see if there are any devices behind the bridge */
1097
1098         u32 bar[6], class;
1099         u32 address[] = {
1100                 PCI_BASE_ADDRESS_0,
1101                 PCI_BASE_ADDRESS_1,
1102                 PCI_BASE_ADDRESS_2,
1103                 PCI_BASE_ADDRESS_3,
1104                 PCI_BASE_ADDRESS_4,
1105                 PCI_BASE_ADDRESS_5,
1106                 0
1107         };
1108         struct res_needed *amount;
1109
1110         amount = kmalloc (sizeof (struct res_needed), GFP_KERNEL);
1111         if (amount == NULL)
1112                 return NULL;
1113         memset (amount, 0, sizeof (struct res_needed));
1114
1115         ibmphp_pci_bus->number = busno;
1116
1117         debug ("the bus_no behind the bridge is %x\n", busno);
1118         debug ("scanning devices behind the bridge...\n");
1119         for (device = 0; device < 32; device++) {
1120                 amount->devices[device] = 0;
1121                 for (function = 0; function < 8; function++) {
1122                         devfn = PCI_DEVFN(device, function);
1123
1124                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1125
1126                         if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1127                                 /* found correct device!!! */
1128                                 howmany++;
1129
1130                                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1131                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1132
1133                                 debug ("hdr_type behind the bridge is %x\n", hdr_type);
1134                                 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
1135                                         err ("embedded bridges not supported for hot-plugging.\n");
1136                                         amount->not_correct = TRUE;
1137                                         return amount;
1138                                 }
1139
1140                                 class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1141                                 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1142                                         err ("The device %x is VGA compatible and as is not supported for hot plugging. "
1143                                              "Please choose another device.\n", device);
1144                                         amount->not_correct = TRUE;
1145                                         return amount;
1146                                 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1147                                         err ("The device %x is not supported for hot plugging. "
1148                                              "Please choose another device.\n", device);
1149                                         amount->not_correct = TRUE;
1150                                         return amount;
1151                                 }
1152
1153                                 amount->devices[device] = 1;
1154
1155                                 for (count = 0; address[count]; count++) {
1156                                         /* for 6 BARs */
1157                                         /*
1158                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp);
1159                                         if (tmp & 0x01) // IO
1160                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1161                                         else // MEMORY
1162                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1163                                         */
1164                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1165                                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
1166
1167                                         debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
1168
1169                                         if (!bar[count])        /* This BAR is not implemented */
1170                                                 continue;
1171
1172                                         //tmp_bar = bar[count];
1173
1174                                         debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1175
1176                                         if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1177                                                 /* This is IO */
1178                                                 len[count] = bar[count] & 0xFFFFFFFC;
1179                                                 len[count] = ~len[count] + 1;
1180                                                 amount->io += len[count];
1181                                         } else {
1182                                                 /* This is Memory */
1183                                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1184                                                         /* pfmem */
1185                                                         len[count] = bar[count] & 0xFFFFFFF0;
1186                                                         len[count] = ~len[count] + 1;
1187                                                         amount->pfmem += len[count];
1188                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1189                                                                 /* takes up another dword */
1190                                                                 count += 1;
1191
1192                                                 } else {
1193                                                         /* regular memory */
1194                                                         len[count] = bar[count] & 0xFFFFFFF0;
1195                                                         len[count] = ~len[count] + 1;
1196                                                         amount->mem += len[count];
1197                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1198                                                                 /* takes up another dword */
1199                                                                 count += 1;
1200                                                         }
1201                                                 }
1202                                         }
1203                                 }       /* end for */
1204                         }       /* end if (valid) */
1205                 }       /* end for */
1206         }       /* end for */
1207
1208         if (!howmany)
1209                 amount->not_correct = TRUE;
1210         else
1211                 amount->not_correct = FALSE;
1212         if ((amount->io) && (amount->io < IOBRIDGE))
1213                 amount->io = IOBRIDGE;
1214         if ((amount->mem) && (amount->mem < MEMBRIDGE))
1215                 amount->mem = MEMBRIDGE;
1216         if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1217                 amount->pfmem = MEMBRIDGE;
1218         return amount;
1219 }
1220
1221 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card 
1222  * upon bootup in the system, since we don't allocate func to such case, we need to read 
1223  * the start addresses from pci config space and then find the corresponding entries in 
1224  * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1225  * Change: we also call these functions even if we configured the card ourselves (i.e., not
1226  * the bootup case), since it should work same way
1227  */
1228 static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
1229 {
1230         u32 start_address;
1231         u32 address[] = {
1232                 PCI_BASE_ADDRESS_0,
1233                 PCI_BASE_ADDRESS_1,
1234                 PCI_BASE_ADDRESS_2,
1235                 PCI_BASE_ADDRESS_3,
1236                 PCI_BASE_ADDRESS_4,
1237                 PCI_BASE_ADDRESS_5,
1238                 0
1239         };
1240         int count;
1241         struct resource_node *io;
1242         struct resource_node *mem;
1243         struct resource_node *pfmem;
1244         struct bus_node *bus;
1245         u32 end_address;
1246         u32 temp_end;
1247         u32 size;
1248         u32 tmp_address;
1249         unsigned int devfn;
1250
1251         debug ("%s - enter\n", __FUNCTION__);
1252
1253         bus = ibmphp_find_res_bus (busno);
1254         if (!bus) {
1255                 debug ("cannot find corresponding bus.\n");
1256                 return -EINVAL;
1257         }
1258
1259         devfn = PCI_DEVFN(device, function);
1260         ibmphp_pci_bus->number = busno;
1261         for (count = 0; address[count]; count++) {      /* for 6 BARs */
1262                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1263
1264                 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1265
1266                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1267                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size);
1268                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address);
1269
1270                 debug ("start_address is %x\n", start_address);
1271                 debug ("busno, device, function %x %x %x\n", busno, device, function);
1272                 if (!size) {
1273                         /* This BAR is not implemented */
1274                         debug ("is this bar no implemented?, count = %d\n", count);
1275                         continue;
1276                 }
1277                 tmp_address = start_address;
1278                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1279                         /* This is IO */
1280                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
1281                         size = size & 0xFFFFFFFC;
1282                         size = ~size + 1;
1283                         end_address = start_address + size - 1;
1284                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1285                                 err ("cannot find corresponding IO resource to remove\n");
1286                                 return -EIO;
1287                         }
1288                         debug ("io->start = %x\n", io->start);
1289                         temp_end = io->end;
1290                         start_address = io->end + 1;
1291                         ibmphp_remove_resource (io);
1292                         /* This is needed b/c of the old I/O restrictions in the BIOS */
1293                         while (temp_end < end_address) {
1294                                 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1295                                         err ("cannot find corresponding IO resource to remove\n");
1296                                         return -EIO;
1297                                 }
1298                                 debug ("io->start = %x\n", io->start);
1299                                 temp_end = io->end;
1300                                 start_address = io->end + 1;
1301                                 ibmphp_remove_resource (io);
1302                         }
1303
1304                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1305                 } else {
1306                         /* This is Memory */
1307                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1308                                 /* pfmem */
1309                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1310                                 debug ("start address of pfmem is %x\n", start_address);
1311
1312                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1313                                         err ("cannot find corresponding PFMEM resource to remove\n");
1314                                         return -EIO;
1315                                 }
1316                                 if (pfmem)
1317                                         debug ("pfmem->start = %x\n", pfmem->start);
1318
1319                                 ibmphp_remove_resource (pfmem);
1320
1321                                 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1322                                         /* takes up another dword */
1323                                         count += 1;
1324                                 }
1325
1326                         } else {
1327                                 /* regular memory */
1328                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1329                                 debug ("start address of mem is %x\n", start_address);
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                                 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1340                                         /* takes up another dword */
1341                                         count += 1;
1342                                 }
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_SECONDARY_BUS, &sec_number);
1384         sec_no = (int) sec_no;
1385
1386         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1387         sub_no = (int) sub_number;
1388         debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1389         if (sec_no != sub_number) {
1390                 err ("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1391                 return -ENODEV;
1392         }
1393
1394         bus = ibmphp_find_res_bus (sec_number);
1395         debug ("bus->busno is %x\n", bus->busno);
1396         debug ("sec_number is %x\n", sec_number);
1397         if (!bus) {
1398                 err ("cannot find Bus structure for the bridged device\n");
1399                 return -EINVAL;
1400         }
1401
1402         ibmphp_remove_bus (bus, busno);
1403
1404         for (count = 0; address[count]; count++) {
1405                 /* for 2 BARs */
1406                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1407
1408                 if (!start_address) {
1409                         /* This BAR is not implemented */
1410                         continue;
1411                 }
1412
1413                 tmp_address = start_address;
1414
1415                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1416                         /* This is IO */
1417                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
1418                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1419                                 err ("cannot find corresponding IO resource to remove\n");
1420                                 return -EIO;
1421                         }
1422                         if (io)
1423                                 debug ("io->start = %x\n", io->start);
1424
1425                         ibmphp_remove_resource (io);
1426
1427                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1428                 } else {
1429                         /* This is Memory */
1430                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1431                                 /* pfmem */
1432                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1433                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1434                                         err ("cannot find corresponding PFMEM resource to remove\n");
1435                                         return -EINVAL;
1436                                 }
1437                                 if (pfmem)
1438                                         debug ("pfmem->start = %x\n", pfmem->start);
1439
1440                                 ibmphp_remove_resource (pfmem);
1441
1442                                 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1443                                         /* takes up another dword */
1444                                         count += 1;
1445                                 }
1446
1447                         } else {
1448                                 /* regular memory */
1449                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1450                                 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1451                                         err ("cannot find corresponding MEM resource to remove\n");
1452                                         return -EINVAL;
1453                                 }
1454                                 if (mem)
1455                                         debug ("mem->start = %x\n", mem->start);
1456
1457                                 ibmphp_remove_resource (mem);
1458
1459                                 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1460                                         /* takes up another dword */
1461                                         count += 1;
1462                                 }
1463                         }
1464                 }       /* end of mem */
1465         }       /* end of for */
1466         debug ("%s - exiting, returning success\n", __FUNCTION__);
1467         return 0;
1468 }
1469
1470 static int unconfigure_boot_card (struct slot *slot_cur)
1471 {
1472         u16 vendor_id;
1473         u32 class;
1474         u8 hdr_type;
1475         u8 device;
1476         u8 busno;
1477         u8 function;
1478         int rc;
1479         unsigned int devfn;
1480         u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1481
1482         debug ("%s - enter\n", __FUNCTION__);
1483
1484         device = slot_cur->device;
1485         busno = slot_cur->bus;
1486
1487         debug ("b4 for loop, device is %x\n", device);
1488         /* For every function on the card */
1489         for (function = 0x0; function < 0x08; function++) {
1490                 devfn = PCI_DEVFN(device, function);
1491                 ibmphp_pci_bus->number = busno;
1492
1493                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1494
1495                 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1496                         /* found correct device!!! */
1497                         ++valid_device;
1498
1499                         debug ("%s - found correct device\n", __FUNCTION__);
1500
1501                         /* header: x x x x x x x x
1502                          *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1503                          *         |_=> 0 = single function device, 1 = multi-function device
1504                          */
1505
1506                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1507                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1508
1509                         debug ("hdr_type %x, class %x\n", hdr_type, class);
1510                         class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1511                         if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1512                                 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
1513                                      "Please choose another device.\n", device, function);
1514                                 return -ENODEV;
1515                         } else if (class == PCI_CLASS_DISPLAY_VGA) {
1516                                 err ("The device %x function %x is not supported for hot removing. "
1517                                      "Please choose another device.\n", device, function);
1518                                 return -ENODEV;
1519                         }
1520
1521                         switch (hdr_type) {
1522                                 case PCI_HEADER_TYPE_NORMAL:
1523                                         rc = unconfigure_boot_device (busno, device, function);
1524                                         if (rc) {
1525                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1526                                                      device, function, busno);
1527                                                 return rc;
1528                                         }
1529                                         function = 0x8;
1530                                         break;
1531                                 case PCI_HEADER_TYPE_MULTIDEVICE:
1532                                         rc = unconfigure_boot_device (busno, device, function);
1533                                         if (rc) {
1534                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1535                                                      device, function, busno);
1536                                                 return rc;
1537                                         }
1538                                         break;
1539                                 case PCI_HEADER_TYPE_BRIDGE:
1540                                         class >>= 8;
1541                                         if (class != PCI_CLASS_BRIDGE_PCI) {
1542                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
1543                                                      "and is not supported for hot-removing. "
1544                                                      "Please try another card.\n", device, function);
1545                                                 return -ENODEV;
1546                                         }
1547                                         rc = unconfigure_boot_bridge (busno, device, function);
1548                                         if (rc != 0) {
1549                                                 err ("was not able to hot-remove PPB properly.\n");
1550                                                 return rc;
1551                                         }
1552
1553                                         function = 0x8;
1554                                         break;
1555                                 case PCI_HEADER_TYPE_MULTIBRIDGE:
1556                                         class >>= 8;
1557                                         if (class != PCI_CLASS_BRIDGE_PCI) {
1558                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
1559                                                      "and is not supported for hot-removing. "
1560                                                      "Please try another card.\n", device, function);
1561                                                 return -ENODEV;
1562                                         }
1563                                         rc = unconfigure_boot_bridge (busno, device, function);
1564                                         if (rc != 0) {
1565                                                 err ("was not able to hot-remove PPB properly.\n");
1566                                                 return rc;
1567                                         }
1568                                         break;
1569                                 default:
1570                                         err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
1571                                         return -1;
1572                                         break;
1573                         }       /* end of switch */
1574                 }       /* end of valid device */
1575         }       /* end of for */
1576
1577         if (!valid_device) {
1578                 err ("Could not find device to unconfigure.  Or could not read the card.\n");
1579                 return -1;
1580         }
1581         return 0;
1582 }
1583
1584 /*
1585  * free the resources of the card (multi, single, or bridged)
1586  * Parameters: slot, flag to say if this is for removing entire module or just
1587  * unconfiguring the device
1588  * TO DO:  will probably need to add some code in case there was some resource,
1589  * to remove it... this is from when we have errors in the configure_card...
1590  *                      !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1591  * Returns: 0, -1, -ENODEV 
1592  */
1593 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1594 {
1595         int i;
1596         int count;
1597         int rc;
1598         struct slot *sl = *slot_cur;
1599         struct pci_func *cur_func = NULL;
1600         struct pci_func *temp_func;
1601
1602         debug ("%s - enter\n", __FUNCTION__);
1603
1604         if (!the_end) {
1605                 /* Need to unconfigure the card */
1606                 rc = unconfigure_boot_card (sl);
1607                 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1608                         /* In all other cases, will still need to get rid of func structure if it exists */
1609                         return rc;
1610                 }
1611         }
1612
1613         if (sl->func) {
1614                 cur_func = sl->func;
1615                 while (cur_func) {
1616                         /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1617                         if (cur_func->bus) {
1618                                 /* in other words, it's a PPB */
1619                                 count = 2;
1620                         } else {
1621                                 count = 6;
1622                         }
1623
1624                         for (i = 0; i < count; i++) {
1625                                 if (cur_func->io[i]) {
1626                                         debug ("io[%d] exists\n", i);
1627                                         if (the_end > 0)
1628                                                 ibmphp_remove_resource (cur_func->io[i]);
1629                                         cur_func->io[i] = NULL;
1630                                 }
1631                                 if (cur_func->mem[i]) {
1632                                         debug ("mem[%d] exists\n", i);
1633                                         if (the_end > 0)
1634                                                 ibmphp_remove_resource (cur_func->mem[i]);
1635                                         cur_func->mem[i] = NULL;
1636                                 }
1637                                 if (cur_func->pfmem[i]) {
1638                                         debug ("pfmem[%d] exists\n", i);
1639                                         if (the_end > 0)
1640                                                 ibmphp_remove_resource (cur_func->pfmem[i]);
1641                                         cur_func->pfmem[i] = NULL;
1642                                 }
1643                         }
1644
1645                         temp_func = cur_func->next;
1646                         kfree (cur_func);
1647                         cur_func = temp_func;
1648                 }
1649         }
1650
1651         sl->func = NULL;
1652         *slot_cur = sl;
1653         debug ("%s - exit\n", __FUNCTION__);
1654         return 0;
1655 }
1656
1657 /*
1658  * add a new bus resulting from hot-plugging a PPB bridge with devices
1659  *
1660  * Input: bus and the amount of resources needed (we know we can assign those,
1661  *        since they've been checked already
1662  * Output: bus added to the correct spot
1663  *         0, -1, error 
1664  */
1665 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1666 {
1667         struct range_node *io_range = NULL;
1668         struct range_node *mem_range = NULL;
1669         struct range_node *pfmem_range = NULL;
1670         struct bus_node *cur_bus = NULL;
1671
1672         /* Trying to find the parent bus number */
1673         if (parent_busno != 0xFF) {
1674                 cur_bus = ibmphp_find_res_bus (parent_busno);
1675                 if (!cur_bus) {
1676                         err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1677                         return -ENODEV;
1678                 }
1679         
1680                 list_add (&bus->bus_list, &cur_bus->bus_list);
1681         }
1682         if (io) {
1683                 io_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1684                 if (!io_range) {
1685                         err ("out of system memory\n");
1686                         return -ENOMEM;
1687                 }
1688                 memset (io_range, 0, sizeof (struct range_node));
1689                 io_range->start = io->start;
1690                 io_range->end = io->end;
1691                 io_range->rangeno = 1;
1692                 bus->noIORanges = 1;
1693                 bus->rangeIO = io_range;
1694         }
1695         if (mem) {
1696                 mem_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1697                 if (!mem_range) {
1698                         err ("out of system memory\n");
1699                         return -ENOMEM;
1700                 }
1701                 memset (mem_range, 0, sizeof (struct range_node));
1702                 mem_range->start = mem->start;
1703                 mem_range->end = mem->end;
1704                 mem_range->rangeno = 1;
1705                 bus->noMemRanges = 1;
1706                 bus->rangeMem = mem_range;
1707         }
1708         if (pfmem) {
1709                 pfmem_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1710                 if (!pfmem_range) {     
1711                         err ("out of system memory\n");
1712                         return -ENOMEM;
1713                 }
1714                 memset (pfmem_range, 0, sizeof (struct range_node));
1715                 pfmem_range->start = pfmem->start;
1716                 pfmem_range->end = pfmem->end;
1717                 pfmem_range->rangeno = 1;
1718                 bus->noPFMemRanges = 1;
1719                 bus->rangePFMem = pfmem_range;
1720         }
1721         return 0;
1722 }
1723
1724 /*
1725  * find the 1st available bus number for PPB to set as its secondary bus
1726  * Parameters: bus_number of the primary bus
1727  * Returns: bus_number of the secondary bus or 0xff in case of failure
1728  */
1729 static u8 find_sec_number (u8 primary_busno, u8 slotno)
1730 {
1731         int min, max;
1732         u8 busno;
1733         struct bus_info *bus;
1734         struct bus_node *bus_cur;
1735
1736         bus = ibmphp_find_same_bus_num (primary_busno);
1737         if (!bus) {
1738                 err ("cannot get slot range of the bus from the BIOS\n");
1739                 return 0xff;
1740         }
1741         max = bus->slot_max;
1742         min = bus->slot_min;
1743         if ((slotno > max) || (slotno < min)) {
1744                 err ("got the wrong range\n");
1745                 return 0xff;
1746         }
1747         busno = (u8) (slotno - (u8) min);
1748         busno += primary_busno + 0x01;
1749         bus_cur = ibmphp_find_res_bus (busno);
1750         /* either there is no such bus number, or there are no ranges, which
1751          * can only happen if we removed the bridged device in previous load
1752          * of the driver, and now only have the skeleton bus struct
1753          */
1754         if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1755                 return busno;
1756         return 0xff;
1757 }
1758