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