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