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