Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * IBM Hot Plug Controller Driver
0004  *
0005  * Written By: Irene Zubarev, IBM Corporation
0006  *
0007  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
0008  * Copyright (C) 2001,2002 IBM Corp.
0009  *
0010  * All rights reserved.
0011  *
0012  * Send feedback to <gregkh@us.ibm.com>
0013  *
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/pci.h>
0019 #include <linux/list.h>
0020 #include "ibmphp.h"
0021 
0022 
0023 static int configure_device(struct pci_func *);
0024 static int configure_bridge(struct pci_func **, u8);
0025 static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
0026 static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
0027 static u8 find_sec_number(u8 primary_busno, u8 slotno);
0028 
0029 /*
0030  * NOTE..... If BIOS doesn't provide default routing, we assign:
0031  * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
0032  * If adapter is bridged, then we assign 11 to it and devices behind it.
0033  * We also assign the same irq numbers for multi function devices.
0034  * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
0035  */
0036 static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
0037 {
0038     int j;
0039     for (j = 0; j < 4; j++) {
0040         if (cur_func->irq[j] == 0xff) {
0041             switch (class_code) {
0042                 case PCI_BASE_CLASS_STORAGE:
0043                     cur_func->irq[j] = SCSI_IRQ;
0044                     break;
0045                 case PCI_BASE_CLASS_NETWORK:
0046                     cur_func->irq[j] = LAN_IRQ;
0047                     break;
0048                 default:
0049                     cur_func->irq[j] = OTHER_IRQ;
0050                     break;
0051             }
0052         }
0053     }
0054 }
0055 
0056 /*
0057  * Configures the device to be added (will allocate needed resources if it
0058  * can), the device can be a bridge or a regular pci device, can also be
0059  * multi-functional
0060  *
0061  * Input: function to be added
0062  *
0063  * TO DO:  The error case with Multifunction device or multi function bridge,
0064  * if there is an error, will need to go through all previous functions and
0065  * unconfigure....or can add some code into unconfigure_card....
0066  */
0067 int ibmphp_configure_card(struct pci_func *func, u8 slotno)
0068 {
0069     u16 vendor_id;
0070     u32 class;
0071     u8 class_code;
0072     u8 hdr_type, device, sec_number;
0073     u8 function;
0074     struct pci_func *newfunc;   /* for multi devices */
0075     struct pci_func *cur_func, *prev_func;
0076     int rc, i, j;
0077     int cleanup_count;
0078     u8 flag;
0079     u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
0080 
0081     debug("inside configure_card, func->busno = %x\n", func->busno);
0082 
0083     device = func->device;
0084     cur_func = func;
0085 
0086     /* We only get bus and device from IRQ routing table.  So at this point,
0087      * func->busno is correct, and func->device contains only device (at the 5
0088      * highest bits)
0089      */
0090 
0091     /* For every function on the card */
0092     for (function = 0x00; function < 0x08; function++) {
0093         unsigned int devfn = PCI_DEVFN(device, function);
0094         ibmphp_pci_bus->number = cur_func->busno;
0095 
0096         cur_func->function = function;
0097 
0098         debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
0099             cur_func->busno, cur_func->device, cur_func->function);
0100 
0101         pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
0102 
0103         debug("vendor_id is %x\n", vendor_id);
0104         if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
0105             /* found correct device!!! */
0106             debug("found valid device, vendor_id = %x\n", vendor_id);
0107 
0108             ++valid_device;
0109 
0110             /* header: x x x x x x x x
0111              *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
0112              *         |_=> 0 = single function device, 1 = multi-function device
0113              */
0114 
0115             pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
0116             pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
0117 
0118             class_code = class >> 24;
0119             debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
0120             class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
0121             if (class == PCI_CLASS_NOT_DEFINED_VGA) {
0122                 err("The device %x is VGA compatible and as is not supported for hot plugging. "
0123                      "Please choose another device.\n", cur_func->device);
0124                 return -ENODEV;
0125             } else if (class == PCI_CLASS_DISPLAY_VGA) {
0126                 err("The device %x is not supported for hot plugging. Please choose another device.\n",
0127                      cur_func->device);
0128                 return -ENODEV;
0129             }
0130             switch (hdr_type) {
0131                 case PCI_HEADER_TYPE_NORMAL:
0132                     debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
0133                     assign_alt_irq(cur_func, class_code);
0134                     rc = configure_device(cur_func);
0135                     if (rc < 0) {
0136                         /* We need to do this in case some other BARs were properly inserted */
0137                         err("was not able to configure devfunc %x on bus %x.\n",
0138                              cur_func->device, cur_func->busno);
0139                         cleanup_count = 6;
0140                         goto error;
0141                     }
0142                     cur_func->next = NULL;
0143                     function = 0x8;
0144                     break;
0145                 case PCI_HEADER_TYPE_MULTIDEVICE:
0146                     assign_alt_irq(cur_func, class_code);
0147                     rc = configure_device(cur_func);
0148                     if (rc < 0) {
0149                         /* We need to do this in case some other BARs were properly inserted */
0150                         err("was not able to configure devfunc %x on bus %x...bailing out\n",
0151                              cur_func->device, cur_func->busno);
0152                         cleanup_count = 6;
0153                         goto error;
0154                     }
0155                     newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
0156                     if (!newfunc)
0157                         return -ENOMEM;
0158 
0159                     newfunc->busno = cur_func->busno;
0160                     newfunc->device = device;
0161                     cur_func->next = newfunc;
0162                     cur_func = newfunc;
0163                     for (j = 0; j < 4; j++)
0164                         newfunc->irq[j] = cur_func->irq[j];
0165                     break;
0166                 case PCI_HEADER_TYPE_MULTIBRIDGE:
0167                     class >>= 8;
0168                     if (class != PCI_CLASS_BRIDGE_PCI) {
0169                         err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
0170                              cur_func->device);
0171                         return -ENODEV;
0172                     }
0173                     assign_alt_irq(cur_func, class_code);
0174                     rc = configure_bridge(&cur_func, slotno);
0175                     if (rc == -ENODEV) {
0176                         err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
0177                         err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
0178                         return rc;
0179                     }
0180                     if (rc) {
0181                         /* We need to do this in case some other BARs were properly inserted */
0182                         err("was not able to hot-add PPB properly.\n");
0183                         func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
0184                         cleanup_count = 2;
0185                         goto error;
0186                     }
0187 
0188                     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
0189                     flag = 0;
0190                     for (i = 0; i < 32; i++) {
0191                         if (func->devices[i]) {
0192                             newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
0193                             if (!newfunc)
0194                                 return -ENOMEM;
0195 
0196                             newfunc->busno = sec_number;
0197                             newfunc->device = (u8) i;
0198                             for (j = 0; j < 4; j++)
0199                                 newfunc->irq[j] = cur_func->irq[j];
0200 
0201                             if (flag) {
0202                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
0203                                 prev_func->next = newfunc;
0204                             } else
0205                                 cur_func->next = newfunc;
0206 
0207                             rc = ibmphp_configure_card(newfunc, slotno);
0208                             /* This could only happen if kmalloc failed */
0209                             if (rc) {
0210                                 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
0211                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
0212                                 cleanup_count = 2;
0213                                 goto error;
0214                             }
0215                             flag = 1;
0216                         }
0217                     }
0218 
0219                     newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
0220                     if (!newfunc)
0221                         return -ENOMEM;
0222 
0223                     newfunc->busno = cur_func->busno;
0224                     newfunc->device = device;
0225                     for (j = 0; j < 4; j++)
0226                         newfunc->irq[j] = cur_func->irq[j];
0227                     for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
0228                     prev_func->next = newfunc;
0229                     cur_func = newfunc;
0230                     break;
0231                 case PCI_HEADER_TYPE_BRIDGE:
0232                     class >>= 8;
0233                     debug("class now is %x\n", class);
0234                     if (class != PCI_CLASS_BRIDGE_PCI) {
0235                         err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
0236                              cur_func->device);
0237                         return -ENODEV;
0238                     }
0239 
0240                     assign_alt_irq(cur_func, class_code);
0241 
0242                     debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
0243                     rc = configure_bridge(&cur_func, slotno);
0244                     if (rc == -ENODEV) {
0245                         err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
0246                         err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
0247                         return rc;
0248                     }
0249                     if (rc) {
0250                         /* We need to do this in case some other BARs were properly inserted */
0251                         func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
0252                         err("was not able to hot-add PPB properly.\n");
0253                         cleanup_count = 2;
0254                         goto error;
0255                     }
0256                     debug("cur_func->busno = %x, device = %x, function = %x\n",
0257                         cur_func->busno, device, function);
0258                     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
0259                     debug("after configuring bridge..., sec_number = %x\n", sec_number);
0260                     flag = 0;
0261                     for (i = 0; i < 32; i++) {
0262                         if (func->devices[i]) {
0263                             debug("inside for loop, device is %x\n", i);
0264                             newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
0265                             if (!newfunc)
0266                                 return -ENOMEM;
0267 
0268                             newfunc->busno = sec_number;
0269                             newfunc->device = (u8) i;
0270                             for (j = 0; j < 4; j++)
0271                                 newfunc->irq[j] = cur_func->irq[j];
0272 
0273                             if (flag) {
0274                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
0275                                 prev_func->next = newfunc;
0276                             } else
0277                                 cur_func->next = newfunc;
0278 
0279                             rc = ibmphp_configure_card(newfunc, slotno);
0280 
0281                             /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
0282                             if (rc) {
0283                                 /* We need to do this in case some other BARs were properly inserted */
0284                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
0285                                 cleanup_count = 2;
0286                                 goto error;
0287                             }
0288                             flag = 1;
0289                         }
0290                     }
0291 
0292                     function = 0x8;
0293                     break;
0294                 default:
0295                     err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
0296                     return -ENXIO;
0297             }   /* end of switch */
0298         }   /* end of valid device */
0299     }   /* end of for */
0300 
0301     if (!valid_device) {
0302         err("Cannot find any valid devices on the card.  Or unable to read from card.\n");
0303         return -ENODEV;
0304     }
0305 
0306     return 0;
0307 
0308 error:
0309     for (i = 0; i < cleanup_count; i++) {
0310         if (cur_func->io[i]) {
0311             ibmphp_remove_resource(cur_func->io[i]);
0312             cur_func->io[i] = NULL;
0313         } else if (cur_func->pfmem[i]) {
0314             ibmphp_remove_resource(cur_func->pfmem[i]);
0315             cur_func->pfmem[i] = NULL;
0316         } else if (cur_func->mem[i]) {
0317             ibmphp_remove_resource(cur_func->mem[i]);
0318             cur_func->mem[i] = NULL;
0319         }
0320     }
0321     return rc;
0322 }
0323 
0324 /*
0325  * This function configures the pci BARs of a single device.
0326  * Input: pointer to the pci_func
0327  * Output: configured PCI, 0, or error
0328  */
0329 static int configure_device(struct pci_func *func)
0330 {
0331     u32 bar[6];
0332     u32 address[] = {
0333         PCI_BASE_ADDRESS_0,
0334         PCI_BASE_ADDRESS_1,
0335         PCI_BASE_ADDRESS_2,
0336         PCI_BASE_ADDRESS_3,
0337         PCI_BASE_ADDRESS_4,
0338         PCI_BASE_ADDRESS_5,
0339         0
0340     };
0341     u8 irq;
0342     int count;
0343     int len[6];
0344     struct resource_node *io[6];
0345     struct resource_node *mem[6];
0346     struct resource_node *mem_tmp;
0347     struct resource_node *pfmem[6];
0348     unsigned int devfn;
0349 
0350     debug("%s - inside\n", __func__);
0351 
0352     devfn = PCI_DEVFN(func->device, func->function);
0353     ibmphp_pci_bus->number = func->busno;
0354 
0355     for (count = 0; address[count]; count++) {  /* for 6 BARs */
0356 
0357         /* not sure if i need this.  per scott, said maybe need * something like this
0358            if devices don't adhere 100% to the spec, so don't want to write
0359            to the reserved bits
0360 
0361         pcibios_read_config_byte(cur_func->busno, cur_func->device,
0362         PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
0363         if (tmp & 0x01) // IO
0364             pcibios_write_config_dword(cur_func->busno, cur_func->device,
0365             PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
0366         else  // Memory
0367             pcibios_write_config_dword(cur_func->busno, cur_func->device,
0368             PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
0369          */
0370         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
0371         pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
0372 
0373         if (!bar[count])    /* This BAR is not implemented */
0374             continue;
0375 
0376         debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
0377 
0378         if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
0379             /* This is IO */
0380             debug("inside IO SPACE\n");
0381 
0382             len[count] = bar[count] & 0xFFFFFFFC;
0383             len[count] = ~len[count] + 1;
0384 
0385             debug("len[count] in IO %x, count %d\n", len[count], count);
0386 
0387             io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0388 
0389             if (!io[count])
0390                 return -ENOMEM;
0391 
0392             io[count]->type = IO;
0393             io[count]->busno = func->busno;
0394             io[count]->devfunc = PCI_DEVFN(func->device, func->function);
0395             io[count]->len = len[count];
0396             if (ibmphp_check_resource(io[count], 0) == 0) {
0397                 ibmphp_add_resource(io[count]);
0398                 func->io[count] = io[count];
0399             } else {
0400                 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
0401                      func->busno, func->device, func->function, len[count]);
0402                 kfree(io[count]);
0403                 return -EIO;
0404             }
0405             pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
0406 
0407             /* _______________This is for debugging purposes only_____________________ */
0408             debug("b4 writing, the IO address is %x\n", func->io[count]->start);
0409             pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
0410             debug("after writing.... the start address is %x\n", bar[count]);
0411             /* _________________________________________________________________________*/
0412 
0413         } else {
0414             /* This is Memory */
0415             if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
0416                 /* pfmem */
0417                 debug("PFMEM SPACE\n");
0418 
0419                 len[count] = bar[count] & 0xFFFFFFF0;
0420                 len[count] = ~len[count] + 1;
0421 
0422                 debug("len[count] in PFMEM %x, count %d\n", len[count], count);
0423 
0424                 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0425                 if (!pfmem[count])
0426                     return -ENOMEM;
0427 
0428                 pfmem[count]->type = PFMEM;
0429                 pfmem[count]->busno = func->busno;
0430                 pfmem[count]->devfunc = PCI_DEVFN(func->device,
0431                             func->function);
0432                 pfmem[count]->len = len[count];
0433                 pfmem[count]->fromMem = 0;
0434                 if (ibmphp_check_resource(pfmem[count], 0) == 0) {
0435                     ibmphp_add_resource(pfmem[count]);
0436                     func->pfmem[count] = pfmem[count];
0437                 } else {
0438                     mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
0439                     if (!mem_tmp) {
0440                         kfree(pfmem[count]);
0441                         return -ENOMEM;
0442                     }
0443                     mem_tmp->type = MEM;
0444                     mem_tmp->busno = pfmem[count]->busno;
0445                     mem_tmp->devfunc = pfmem[count]->devfunc;
0446                     mem_tmp->len = pfmem[count]->len;
0447                     debug("there's no pfmem... going into mem.\n");
0448                     if (ibmphp_check_resource(mem_tmp, 0) == 0) {
0449                         ibmphp_add_resource(mem_tmp);
0450                         pfmem[count]->fromMem = 1;
0451                         pfmem[count]->rangeno = mem_tmp->rangeno;
0452                         pfmem[count]->start = mem_tmp->start;
0453                         pfmem[count]->end = mem_tmp->end;
0454                         ibmphp_add_pfmem_from_mem(pfmem[count]);
0455                         func->pfmem[count] = pfmem[count];
0456                     } else {
0457                         err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
0458                              func->busno, func->device, len[count]);
0459                         kfree(mem_tmp);
0460                         kfree(pfmem[count]);
0461                         return -EIO;
0462                     }
0463                 }
0464 
0465                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
0466 
0467                 /*_______________This is for debugging purposes only______________________________*/
0468                 debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
0469                 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
0470                 debug("after writing, start address is %x\n", bar[count]);
0471                 /*_________________________________________________________________________________*/
0472 
0473                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {    /* takes up another dword */
0474                     debug("inside the mem 64 case, count %d\n", count);
0475                     count += 1;
0476                     /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
0477                     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
0478                 }
0479             } else {
0480                 /* regular memory */
0481                 debug("REGULAR MEM SPACE\n");
0482 
0483                 len[count] = bar[count] & 0xFFFFFFF0;
0484                 len[count] = ~len[count] + 1;
0485 
0486                 debug("len[count] in Mem %x, count %d\n", len[count], count);
0487 
0488                 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0489                 if (!mem[count])
0490                     return -ENOMEM;
0491 
0492                 mem[count]->type = MEM;
0493                 mem[count]->busno = func->busno;
0494                 mem[count]->devfunc = PCI_DEVFN(func->device,
0495                             func->function);
0496                 mem[count]->len = len[count];
0497                 if (ibmphp_check_resource(mem[count], 0) == 0) {
0498                     ibmphp_add_resource(mem[count]);
0499                     func->mem[count] = mem[count];
0500                 } else {
0501                     err("cannot allocate requested mem for bus %x, device %x, len %x\n",
0502                          func->busno, func->device, len[count]);
0503                     kfree(mem[count]);
0504                     return -EIO;
0505                 }
0506                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
0507                 /* _______________________This is for debugging purposes only _______________________*/
0508                 debug("b4 writing, start address is %x\n", func->mem[count]->start);
0509                 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
0510                 debug("after writing, the address is %x\n", bar[count]);
0511                 /* __________________________________________________________________________________*/
0512 
0513                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
0514                     /* takes up another dword */
0515                     debug("inside mem 64 case, reg. mem, count %d\n", count);
0516                     count += 1;
0517                     /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
0518                     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
0519                 }
0520             }
0521         }       /* end of mem */
0522     }           /* end of for */
0523 
0524     func->bus = 0;      /* To indicate that this is not a PPB */
0525     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
0526     if ((irq > 0x00) && (irq < 0x05))
0527         pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
0528 
0529     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
0530     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
0531 
0532     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
0533     pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
0534 
0535     return 0;
0536 }
0537 
0538 /******************************************************************************
0539  * This routine configures a PCI-2-PCI bridge and the functions behind it
0540  * Parameters: pci_func
0541  * Returns:
0542  ******************************************************************************/
0543 static int configure_bridge(struct pci_func **func_passed, u8 slotno)
0544 {
0545     int count;
0546     int i;
0547     int rc;
0548     u8 sec_number;
0549     u8 io_base;
0550     u16 pfmem_base;
0551     u32 bar[2];
0552     u32 len[2];
0553     u8 flag_io = 0;
0554     u8 flag_mem = 0;
0555     u8 flag_pfmem = 0;
0556     u8 need_io_upper = 0;
0557     u8 need_pfmem_upper = 0;
0558     struct res_needed *amount_needed = NULL;
0559     struct resource_node *io = NULL;
0560     struct resource_node *bus_io[2] = {NULL, NULL};
0561     struct resource_node *mem = NULL;
0562     struct resource_node *bus_mem[2] = {NULL, NULL};
0563     struct resource_node *mem_tmp = NULL;
0564     struct resource_node *pfmem = NULL;
0565     struct resource_node *bus_pfmem[2] = {NULL, NULL};
0566     struct bus_node *bus;
0567     u32 address[] = {
0568         PCI_BASE_ADDRESS_0,
0569         PCI_BASE_ADDRESS_1,
0570         0
0571     };
0572     struct pci_func *func = *func_passed;
0573     unsigned int devfn;
0574     u8 irq;
0575     int retval;
0576 
0577     debug("%s - enter\n", __func__);
0578 
0579     devfn = PCI_DEVFN(func->function, func->device);
0580     ibmphp_pci_bus->number = func->busno;
0581 
0582     /* Configuring necessary info for the bridge so that we could see the devices
0583      * behind it
0584      */
0585 
0586     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
0587 
0588     /* _____________________For debugging purposes only __________________________
0589     pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
0590     debug("primary # written into the bridge is %x\n", pri_number);
0591      ___________________________________________________________________________*/
0592 
0593     /* in EBDA, only get allocated 1 additional bus # per slot */
0594     sec_number = find_sec_number(func->busno, slotno);
0595     if (sec_number == 0xff) {
0596         err("cannot allocate secondary bus number for the bridged device\n");
0597         return -EINVAL;
0598     }
0599 
0600     debug("after find_sec_number, the number we got is %x\n", sec_number);
0601     debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
0602 
0603     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
0604 
0605     /* __________________For debugging purposes only __________________________________
0606     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
0607     debug("sec_number after write/read is %x\n", sec_number);
0608      ________________________________________________________________________________*/
0609 
0610     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
0611 
0612     /* __________________For debugging purposes only ____________________________________
0613     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
0614     debug("subordinate number after write/read is %x\n", sec_number);
0615      __________________________________________________________________________________*/
0616 
0617     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
0618     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
0619     pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
0620 
0621     debug("func->busno is %x\n", func->busno);
0622     debug("sec_number after writing is %x\n", sec_number);
0623 
0624 
0625     /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0626        !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
0627        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
0628 
0629 
0630     /* First we need to allocate mem/io for the bridge itself in case it needs it */
0631     for (count = 0; address[count]; count++) {  /* for 2 BARs */
0632         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
0633         pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
0634 
0635         if (!bar[count]) {
0636             /* This BAR is not implemented */
0637             debug("so we come here then, eh?, count = %d\n", count);
0638             continue;
0639         }
0640         //  tmp_bar = bar[count];
0641 
0642         debug("Bar %d wants %x\n", count, bar[count]);
0643 
0644         if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
0645             /* This is IO */
0646             len[count] = bar[count] & 0xFFFFFFFC;
0647             len[count] = ~len[count] + 1;
0648 
0649             debug("len[count] in IO = %x\n", len[count]);
0650 
0651             bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0652 
0653             if (!bus_io[count]) {
0654                 retval = -ENOMEM;
0655                 goto error;
0656             }
0657             bus_io[count]->type = IO;
0658             bus_io[count]->busno = func->busno;
0659             bus_io[count]->devfunc = PCI_DEVFN(func->device,
0660                             func->function);
0661             bus_io[count]->len = len[count];
0662             if (ibmphp_check_resource(bus_io[count], 0) == 0) {
0663                 ibmphp_add_resource(bus_io[count]);
0664                 func->io[count] = bus_io[count];
0665             } else {
0666                 err("cannot allocate requested io for bus %x, device %x, len %x\n",
0667                      func->busno, func->device, len[count]);
0668                 kfree(bus_io[count]);
0669                 return -EIO;
0670             }
0671 
0672             pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
0673 
0674         } else {
0675             /* This is Memory */
0676             if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
0677                 /* pfmem */
0678                 len[count] = bar[count] & 0xFFFFFFF0;
0679                 len[count] = ~len[count] + 1;
0680 
0681                 debug("len[count] in PFMEM = %x\n", len[count]);
0682 
0683                 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0684                 if (!bus_pfmem[count]) {
0685                     retval = -ENOMEM;
0686                     goto error;
0687                 }
0688                 bus_pfmem[count]->type = PFMEM;
0689                 bus_pfmem[count]->busno = func->busno;
0690                 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
0691                             func->function);
0692                 bus_pfmem[count]->len = len[count];
0693                 bus_pfmem[count]->fromMem = 0;
0694                 if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
0695                     ibmphp_add_resource(bus_pfmem[count]);
0696                     func->pfmem[count] = bus_pfmem[count];
0697                 } else {
0698                     mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
0699                     if (!mem_tmp) {
0700                         retval = -ENOMEM;
0701                         goto error;
0702                     }
0703                     mem_tmp->type = MEM;
0704                     mem_tmp->busno = bus_pfmem[count]->busno;
0705                     mem_tmp->devfunc = bus_pfmem[count]->devfunc;
0706                     mem_tmp->len = bus_pfmem[count]->len;
0707                     if (ibmphp_check_resource(mem_tmp, 0) == 0) {
0708                         ibmphp_add_resource(mem_tmp);
0709                         bus_pfmem[count]->fromMem = 1;
0710                         bus_pfmem[count]->rangeno = mem_tmp->rangeno;
0711                         ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
0712                         func->pfmem[count] = bus_pfmem[count];
0713                     } else {
0714                         err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
0715                              func->busno, func->device, len[count]);
0716                         kfree(mem_tmp);
0717                         kfree(bus_pfmem[count]);
0718                         return -EIO;
0719                     }
0720                 }
0721 
0722                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
0723 
0724                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
0725                     /* takes up another dword */
0726                     count += 1;
0727                     /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
0728                     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
0729 
0730                 }
0731             } else {
0732                 /* regular memory */
0733                 len[count] = bar[count] & 0xFFFFFFF0;
0734                 len[count] = ~len[count] + 1;
0735 
0736                 debug("len[count] in Memory is %x\n", len[count]);
0737 
0738                 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
0739                 if (!bus_mem[count]) {
0740                     retval = -ENOMEM;
0741                     goto error;
0742                 }
0743                 bus_mem[count]->type = MEM;
0744                 bus_mem[count]->busno = func->busno;
0745                 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
0746                             func->function);
0747                 bus_mem[count]->len = len[count];
0748                 if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
0749                     ibmphp_add_resource(bus_mem[count]);
0750                     func->mem[count] = bus_mem[count];
0751                 } else {
0752                     err("cannot allocate requested mem for bus %x, device %x, len %x\n",
0753                          func->busno, func->device, len[count]);
0754                     kfree(bus_mem[count]);
0755                     return -EIO;
0756                 }
0757 
0758                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
0759 
0760                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
0761                     /* takes up another dword */
0762                     count += 1;
0763                     /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
0764                     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
0765 
0766                 }
0767             }
0768         }       /* end of mem */
0769     }           /* end of for  */
0770 
0771     /* Now need to see how much space the devices behind the bridge needed */
0772     amount_needed = scan_behind_bridge(func, sec_number);
0773     if (amount_needed == NULL)
0774         return -ENOMEM;
0775 
0776     ibmphp_pci_bus->number = func->busno;
0777     debug("after coming back from scan_behind_bridge\n");
0778     debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
0779     debug("amount_needed->io = %x\n", amount_needed->io);
0780     debug("amount_needed->mem = %x\n", amount_needed->mem);
0781     debug("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
0782 
0783     if (amount_needed->not_correct) {
0784         debug("amount_needed is not correct\n");
0785         for (count = 0; address[count]; count++) {
0786             /* for 2 BARs */
0787             if (bus_io[count]) {
0788                 ibmphp_remove_resource(bus_io[count]);
0789                 func->io[count] = NULL;
0790             } else if (bus_pfmem[count]) {
0791                 ibmphp_remove_resource(bus_pfmem[count]);
0792                 func->pfmem[count] = NULL;
0793             } else if (bus_mem[count]) {
0794                 ibmphp_remove_resource(bus_mem[count]);
0795                 func->mem[count] = NULL;
0796             }
0797         }
0798         kfree(amount_needed);
0799         return -ENODEV;
0800     }
0801 
0802     if (!amount_needed->io) {
0803         debug("it doesn't want IO?\n");
0804         flag_io = 1;
0805     } else {
0806         debug("it wants %x IO behind the bridge\n", amount_needed->io);
0807         io = kzalloc(sizeof(*io), GFP_KERNEL);
0808 
0809         if (!io) {
0810             retval = -ENOMEM;
0811             goto error;
0812         }
0813         io->type = IO;
0814         io->busno = func->busno;
0815         io->devfunc = PCI_DEVFN(func->device, func->function);
0816         io->len = amount_needed->io;
0817         if (ibmphp_check_resource(io, 1) == 0) {
0818             debug("were we able to add io\n");
0819             ibmphp_add_resource(io);
0820             flag_io = 1;
0821         }
0822     }
0823 
0824     if (!amount_needed->mem) {
0825         debug("it doesn't want n.e.memory?\n");
0826         flag_mem = 1;
0827     } else {
0828         debug("it wants %x memory behind the bridge\n", amount_needed->mem);
0829         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
0830         if (!mem) {
0831             retval = -ENOMEM;
0832             goto error;
0833         }
0834         mem->type = MEM;
0835         mem->busno = func->busno;
0836         mem->devfunc = PCI_DEVFN(func->device, func->function);
0837         mem->len = amount_needed->mem;
0838         if (ibmphp_check_resource(mem, 1) == 0) {
0839             ibmphp_add_resource(mem);
0840             flag_mem = 1;
0841             debug("were we able to add mem\n");
0842         }
0843     }
0844 
0845     if (!amount_needed->pfmem) {
0846         debug("it doesn't want n.e.pfmem mem?\n");
0847         flag_pfmem = 1;
0848     } else {
0849         debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
0850         pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
0851         if (!pfmem) {
0852             retval = -ENOMEM;
0853             goto error;
0854         }
0855         pfmem->type = PFMEM;
0856         pfmem->busno = func->busno;
0857         pfmem->devfunc = PCI_DEVFN(func->device, func->function);
0858         pfmem->len = amount_needed->pfmem;
0859         pfmem->fromMem = 0;
0860         if (ibmphp_check_resource(pfmem, 1) == 0) {
0861             ibmphp_add_resource(pfmem);
0862             flag_pfmem = 1;
0863         } else {
0864             mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
0865             if (!mem_tmp) {
0866                 retval = -ENOMEM;
0867                 goto error;
0868             }
0869             mem_tmp->type = MEM;
0870             mem_tmp->busno = pfmem->busno;
0871             mem_tmp->devfunc = pfmem->devfunc;
0872             mem_tmp->len = pfmem->len;
0873             if (ibmphp_check_resource(mem_tmp, 1) == 0) {
0874                 ibmphp_add_resource(mem_tmp);
0875                 pfmem->fromMem = 1;
0876                 pfmem->rangeno = mem_tmp->rangeno;
0877                 ibmphp_add_pfmem_from_mem(pfmem);
0878                 flag_pfmem = 1;
0879             }
0880         }
0881     }
0882 
0883     debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
0884     debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
0885 
0886     if (flag_io && flag_mem && flag_pfmem) {
0887         /* If on bootup, there was a bridged card in this slot,
0888          * then card was removed and ibmphp got unloaded and loaded
0889          * back again, there's no way for us to remove the bus
0890          * struct, so no need to kmalloc, can use existing node
0891          */
0892         bus = ibmphp_find_res_bus(sec_number);
0893         if (!bus) {
0894             bus = kzalloc(sizeof(*bus), GFP_KERNEL);
0895             if (!bus) {
0896                 retval = -ENOMEM;
0897                 goto error;
0898             }
0899             bus->busno = sec_number;
0900             debug("b4 adding new bus\n");
0901             rc = add_new_bus(bus, io, mem, pfmem, func->busno);
0902         } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
0903             rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
0904         else {
0905             err("expected bus structure not empty?\n");
0906             retval = -EIO;
0907             goto error;
0908         }
0909         if (rc) {
0910             if (rc == -ENOMEM) {
0911                 ibmphp_remove_bus(bus, func->busno);
0912                 kfree(amount_needed);
0913                 return rc;
0914             }
0915             retval = rc;
0916             goto error;
0917         }
0918         pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
0919         pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
0920 
0921         if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
0922             debug("io 32\n");
0923             need_io_upper = 1;
0924         }
0925         if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
0926             debug("pfmem 64\n");
0927             need_pfmem_upper = 1;
0928         }
0929 
0930         if (bus->noIORanges) {
0931             pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
0932             pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
0933 
0934             /* _______________This is for debugging purposes only ____________________
0935             pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
0936             debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
0937             pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
0938             debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
0939              ________________________________________________________________________*/
0940 
0941             if (need_io_upper) {    /* since can't support n.e.ways */
0942                 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
0943                 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
0944             }
0945         } else {
0946             pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
0947             pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
0948         }
0949 
0950         if (bus->noMemRanges) {
0951             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
0952             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
0953 
0954             /* ____________________This is for debugging purposes only ________________________
0955             pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
0956             debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
0957             pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
0958             debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
0959              __________________________________________________________________________________*/
0960 
0961         } else {
0962             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
0963             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
0964         }
0965         if (bus->noPFMemRanges) {
0966             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
0967             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
0968 
0969             /* __________________________This is for debugging purposes only _______________________
0970             pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
0971             debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
0972             pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
0973             debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
0974              ______________________________________________________________________________________*/
0975 
0976             if (need_pfmem_upper) { /* since can't support n.e.ways */
0977                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
0978                 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
0979             }
0980         } else {
0981             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
0982             pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
0983         }
0984 
0985         debug("b4 writing control information\n");
0986 
0987         pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
0988         if ((irq > 0x00) && (irq < 0x05))
0989             pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
0990         /*
0991         pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
0992         pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
0993         pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
0994          */
0995 
0996         pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
0997         pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
0998         for (i = 0; i < 32; i++) {
0999             if (amount_needed->devices[i]) {
1000                 debug("device where devices[i] is 1 = %x\n", i);
1001                 func->devices[i] = 1;
1002             }
1003         }
1004         func->bus = 1;  /* For unconfiguring, to indicate it's PPB */
1005         func_passed = &func;
1006         debug("func->busno b4 returning is %x\n", func->busno);
1007         debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1008         kfree(amount_needed);
1009         return 0;
1010     } else {
1011         err("Configuring bridge was unsuccessful...\n");
1012         mem_tmp = NULL;
1013         retval = -EIO;
1014         goto error;
1015     }
1016 
1017 error:
1018     kfree(amount_needed);
1019     if (pfmem)
1020         ibmphp_remove_resource(pfmem);
1021     if (io)
1022         ibmphp_remove_resource(io);
1023     if (mem)
1024         ibmphp_remove_resource(mem);
1025     for (i = 0; i < 2; i++) {   /* for 2 BARs */
1026         if (bus_io[i]) {
1027             ibmphp_remove_resource(bus_io[i]);
1028             func->io[i] = NULL;
1029         } else if (bus_pfmem[i]) {
1030             ibmphp_remove_resource(bus_pfmem[i]);
1031             func->pfmem[i] = NULL;
1032         } else if (bus_mem[i]) {
1033             ibmphp_remove_resource(bus_mem[i]);
1034             func->mem[i] = NULL;
1035         }
1036     }
1037     return retval;
1038 }
1039 
1040 /*****************************************************************************
1041  * This function adds up the amount of resources needed behind the PPB bridge
1042  * and passes it to the configure_bridge function
1043  * Input: bridge function
1044  * Output: amount of resources needed
1045  *****************************************************************************/
1046 static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1047 {
1048     int count, len[6];
1049     u16 vendor_id;
1050     u8 hdr_type;
1051     u8 device, function;
1052     unsigned int devfn;
1053     int howmany = 0;    /*this is to see if there are any devices behind the bridge */
1054 
1055     u32 bar[6], class;
1056     u32 address[] = {
1057         PCI_BASE_ADDRESS_0,
1058         PCI_BASE_ADDRESS_1,
1059         PCI_BASE_ADDRESS_2,
1060         PCI_BASE_ADDRESS_3,
1061         PCI_BASE_ADDRESS_4,
1062         PCI_BASE_ADDRESS_5,
1063         0
1064     };
1065     struct res_needed *amount;
1066 
1067     amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1068     if (amount == NULL)
1069         return NULL;
1070 
1071     ibmphp_pci_bus->number = busno;
1072 
1073     debug("the bus_no behind the bridge is %x\n", busno);
1074     debug("scanning devices behind the bridge...\n");
1075     for (device = 0; device < 32; device++) {
1076         amount->devices[device] = 0;
1077         for (function = 0; function < 8; function++) {
1078             devfn = PCI_DEVFN(device, function);
1079 
1080             pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1081 
1082             if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1083                 /* found correct device!!! */
1084                 howmany++;
1085 
1086                 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1087                 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1088 
1089                 debug("hdr_type behind the bridge is %x\n", hdr_type);
1090                 if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1091                     err("embedded bridges not supported for hot-plugging.\n");
1092                     amount->not_correct = 1;
1093                     return amount;
1094                 }
1095 
1096                 class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1097                 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1098                     err("The device %x is VGA compatible and as is not supported for hot plugging.  Please choose another device.\n", device);
1099                     amount->not_correct = 1;
1100                     return amount;
1101                 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1102                     err("The device %x is not supported for hot plugging.  Please choose another device.\n", device);
1103                     amount->not_correct = 1;
1104                     return amount;
1105                 }
1106 
1107                 amount->devices[device] = 1;
1108 
1109                 for (count = 0; address[count]; count++) {
1110                     /* for 6 BARs */
1111                     /*
1112                     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1113                     if (tmp & 0x01) // IO
1114                         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1115                     else // MEMORY
1116                         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1117                     */
1118                     pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1119                     pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1120 
1121                     debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1122 
1123                     if (!bar[count])    /* This BAR is not implemented */
1124                         continue;
1125 
1126                     //tmp_bar = bar[count];
1127 
1128                     debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1129 
1130                     if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1131                         /* This is IO */
1132                         len[count] = bar[count] & 0xFFFFFFFC;
1133                         len[count] = ~len[count] + 1;
1134                         amount->io += len[count];
1135                     } else {
1136                         /* This is Memory */
1137                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1138                             /* pfmem */
1139                             len[count] = bar[count] & 0xFFFFFFF0;
1140                             len[count] = ~len[count] + 1;
1141                             amount->pfmem += len[count];
1142                             if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1143                                 /* takes up another dword */
1144                                 count += 1;
1145 
1146                         } else {
1147                             /* regular memory */
1148                             len[count] = bar[count] & 0xFFFFFFF0;
1149                             len[count] = ~len[count] + 1;
1150                             amount->mem += len[count];
1151                             if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1152                                 /* takes up another dword */
1153                                 count += 1;
1154                             }
1155                         }
1156                     }
1157                 }   /* end for */
1158             }   /* end if (valid) */
1159         }   /* end for */
1160     }   /* end for */
1161 
1162     if (!howmany)
1163         amount->not_correct = 1;
1164     else
1165         amount->not_correct = 0;
1166     if ((amount->io) && (amount->io < IOBRIDGE))
1167         amount->io = IOBRIDGE;
1168     if ((amount->mem) && (amount->mem < MEMBRIDGE))
1169         amount->mem = MEMBRIDGE;
1170     if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1171         amount->pfmem = MEMBRIDGE;
1172     return amount;
1173 }
1174 
1175 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1176  * upon bootup in the system, since we don't allocate func to such case, we need to read
1177  * the start addresses from pci config space and then find the corresponding entries in
1178  * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1179  * Change: we also call these functions even if we configured the card ourselves (i.e., not
1180  * the bootup case), since it should work same way
1181  */
1182 static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1183 {
1184     u32 start_address;
1185     u32 address[] = {
1186         PCI_BASE_ADDRESS_0,
1187         PCI_BASE_ADDRESS_1,
1188         PCI_BASE_ADDRESS_2,
1189         PCI_BASE_ADDRESS_3,
1190         PCI_BASE_ADDRESS_4,
1191         PCI_BASE_ADDRESS_5,
1192         0
1193     };
1194     int count;
1195     struct resource_node *io;
1196     struct resource_node *mem;
1197     struct resource_node *pfmem;
1198     struct bus_node *bus;
1199     u32 end_address;
1200     u32 temp_end;
1201     u32 size;
1202     u32 tmp_address;
1203     unsigned int devfn;
1204 
1205     debug("%s - enter\n", __func__);
1206 
1207     bus = ibmphp_find_res_bus(busno);
1208     if (!bus) {
1209         debug("cannot find corresponding bus.\n");
1210         return -EINVAL;
1211     }
1212 
1213     devfn = PCI_DEVFN(device, function);
1214     ibmphp_pci_bus->number = busno;
1215     for (count = 0; address[count]; count++) {  /* for 6 BARs */
1216         pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1217 
1218         /* We can do this here, b/c by that time the device driver of the card has been stopped */
1219 
1220         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1221         pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1222         pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1223 
1224         debug("start_address is %x\n", start_address);
1225         debug("busno, device, function %x %x %x\n", busno, device, function);
1226         if (!size) {
1227             /* This BAR is not implemented */
1228             debug("is this bar no implemented?, count = %d\n", count);
1229             continue;
1230         }
1231         tmp_address = start_address;
1232         if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1233             /* This is IO */
1234             start_address &= PCI_BASE_ADDRESS_IO_MASK;
1235             size = size & 0xFFFFFFFC;
1236             size = ~size + 1;
1237             end_address = start_address + size - 1;
1238             if (ibmphp_find_resource(bus, start_address, &io, IO))
1239                 goto report_search_failure;
1240 
1241             debug("io->start = %x\n", io->start);
1242             temp_end = io->end;
1243             start_address = io->end + 1;
1244             ibmphp_remove_resource(io);
1245             /* This is needed b/c of the old I/O restrictions in the BIOS */
1246             while (temp_end < end_address) {
1247                 if (ibmphp_find_resource(bus, start_address,
1248                              &io, IO))
1249                     goto report_search_failure;
1250 
1251                 debug("io->start = %x\n", io->start);
1252                 temp_end = io->end;
1253                 start_address = io->end + 1;
1254                 ibmphp_remove_resource(io);
1255             }
1256 
1257             /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1258         } else {
1259             /* This is Memory */
1260             if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1261                 /* pfmem */
1262                 debug("start address of pfmem is %x\n", start_address);
1263                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1264 
1265                 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1266                     err("cannot find corresponding PFMEM resource to remove\n");
1267                     return -EIO;
1268                 }
1269                 if (pfmem) {
1270                     debug("pfmem->start = %x\n", pfmem->start);
1271 
1272                     ibmphp_remove_resource(pfmem);
1273                 }
1274             } else {
1275                 /* regular memory */
1276                 debug("start address of mem is %x\n", start_address);
1277                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1278 
1279                 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1280                     err("cannot find corresponding MEM resource to remove\n");
1281                     return -EIO;
1282                 }
1283                 if (mem) {
1284                     debug("mem->start = %x\n", mem->start);
1285 
1286                     ibmphp_remove_resource(mem);
1287                 }
1288             }
1289             if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1290                 /* takes up another dword */
1291                 count += 1;
1292             }
1293         }   /* end of mem */
1294     }   /* end of for */
1295 
1296     return 0;
1297 
1298 report_search_failure:
1299     err("cannot find corresponding IO resource to remove\n");
1300     return -EIO;
1301 }
1302 
1303 static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1304 {
1305     int count;
1306     int bus_no, pri_no, sub_no, sec_no = 0;
1307     u32 start_address, tmp_address;
1308     u8 sec_number, sub_number, pri_number;
1309     struct resource_node *io = NULL;
1310     struct resource_node *mem = NULL;
1311     struct resource_node *pfmem = NULL;
1312     struct bus_node *bus;
1313     u32 address[] = {
1314         PCI_BASE_ADDRESS_0,
1315         PCI_BASE_ADDRESS_1,
1316         0
1317     };
1318     unsigned int devfn;
1319 
1320     devfn = PCI_DEVFN(device, function);
1321     ibmphp_pci_bus->number = busno;
1322     bus_no = (int) busno;
1323     debug("busno is %x\n", busno);
1324     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1325     debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1326 
1327     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1328     debug("sec_number is %x\n", sec_number);
1329     sec_no = (int) sec_number;
1330     pri_no = (int) pri_number;
1331     if (pri_no != bus_no) {
1332         err("primary numbers in our structures and pci config space don't match.\n");
1333         return -EINVAL;
1334     }
1335 
1336     pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1337     sub_no = (int) sub_number;
1338     debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1339     if (sec_no != sub_number) {
1340         err("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1341         return -ENODEV;
1342     }
1343 
1344     bus = ibmphp_find_res_bus(sec_number);
1345     if (!bus) {
1346         err("cannot find Bus structure for the bridged device\n");
1347         return -EINVAL;
1348     }
1349     debug("bus->busno is %x\n", bus->busno);
1350     debug("sec_number is %x\n", sec_number);
1351 
1352     ibmphp_remove_bus(bus, busno);
1353 
1354     for (count = 0; address[count]; count++) {
1355         /* for 2 BARs */
1356         pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1357 
1358         if (!start_address) {
1359             /* This BAR is not implemented */
1360             continue;
1361         }
1362 
1363         tmp_address = start_address;
1364 
1365         if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1366             /* This is IO */
1367             start_address &= PCI_BASE_ADDRESS_IO_MASK;
1368             if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1369                 err("cannot find corresponding IO resource to remove\n");
1370                 return -EIO;
1371             }
1372             if (io)
1373                 debug("io->start = %x\n", io->start);
1374 
1375             ibmphp_remove_resource(io);
1376 
1377             /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1378         } else {
1379             /* This is Memory */
1380             if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1381                 /* pfmem */
1382                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1383                 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1384                     err("cannot find corresponding PFMEM resource to remove\n");
1385                     return -EINVAL;
1386                 }
1387                 if (pfmem) {
1388                     debug("pfmem->start = %x\n", pfmem->start);
1389 
1390                     ibmphp_remove_resource(pfmem);
1391                 }
1392             } else {
1393                 /* regular memory */
1394                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1395                 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1396                     err("cannot find corresponding MEM resource to remove\n");
1397                     return -EINVAL;
1398                 }
1399                 if (mem) {
1400                     debug("mem->start = %x\n", mem->start);
1401 
1402                     ibmphp_remove_resource(mem);
1403                 }
1404             }
1405             if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1406                 /* takes up another dword */
1407                 count += 1;
1408             }
1409         }   /* end of mem */
1410     }   /* end of for */
1411     debug("%s - exiting, returning success\n", __func__);
1412     return 0;
1413 }
1414 
1415 static int unconfigure_boot_card(struct slot *slot_cur)
1416 {
1417     u16 vendor_id;
1418     u32 class;
1419     u8 hdr_type;
1420     u8 device;
1421     u8 busno;
1422     u8 function;
1423     int rc;
1424     unsigned int devfn;
1425     u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1426 
1427     debug("%s - enter\n", __func__);
1428 
1429     device = slot_cur->device;
1430     busno = slot_cur->bus;
1431 
1432     debug("b4 for loop, device is %x\n", device);
1433     /* For every function on the card */
1434     for (function = 0x0; function < 0x08; function++) {
1435         devfn = PCI_DEVFN(device, function);
1436         ibmphp_pci_bus->number = busno;
1437 
1438         pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1439 
1440         if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1441             /* found correct device!!! */
1442             ++valid_device;
1443 
1444             debug("%s - found correct device\n", __func__);
1445 
1446             /* header: x x x x x x x x
1447              *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1448              *         |_=> 0 = single function device, 1 = multi-function device
1449              */
1450 
1451             pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1452             pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1453 
1454             debug("hdr_type %x, class %x\n", hdr_type, class);
1455             class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
1456             if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1457                 err("The device %x function %x is VGA compatible and is not supported for hot removing.  Please choose another device.\n", device, function);
1458                 return -ENODEV;
1459             } else if (class == PCI_CLASS_DISPLAY_VGA) {
1460                 err("The device %x function %x is not supported for hot removing.  Please choose another device.\n", device, function);
1461                 return -ENODEV;
1462             }
1463 
1464             switch (hdr_type) {
1465                 case PCI_HEADER_TYPE_NORMAL:
1466                     rc = unconfigure_boot_device(busno, device, function);
1467                     if (rc) {
1468                         err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1469                              device, function, busno);
1470                         return rc;
1471                     }
1472                     function = 0x8;
1473                     break;
1474                 case PCI_HEADER_TYPE_MULTIDEVICE:
1475                     rc = unconfigure_boot_device(busno, device, function);
1476                     if (rc) {
1477                         err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1478                              device, function, busno);
1479                         return rc;
1480                     }
1481                     break;
1482                 case PCI_HEADER_TYPE_BRIDGE:
1483                     class >>= 8;
1484                     if (class != PCI_CLASS_BRIDGE_PCI) {
1485                         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);
1486                         return -ENODEV;
1487                     }
1488                     rc = unconfigure_boot_bridge(busno, device, function);
1489                     if (rc != 0) {
1490                         err("was not able to hot-remove PPB properly.\n");
1491                         return rc;
1492                     }
1493 
1494                     function = 0x8;
1495                     break;
1496                 case PCI_HEADER_TYPE_MULTIBRIDGE:
1497                     class >>= 8;
1498                     if (class != PCI_CLASS_BRIDGE_PCI) {
1499                         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);
1500                         return -ENODEV;
1501                     }
1502                     rc = unconfigure_boot_bridge(busno, device, function);
1503                     if (rc != 0) {
1504                         err("was not able to hot-remove PPB properly.\n");
1505                         return rc;
1506                     }
1507                     break;
1508                 default:
1509                     err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1510                     return -1;
1511             }   /* end of switch */
1512         }   /* end of valid device */
1513     }   /* end of for */
1514 
1515     if (!valid_device) {
1516         err("Could not find device to unconfigure.  Or could not read the card.\n");
1517         return -1;
1518     }
1519     return 0;
1520 }
1521 
1522 /*
1523  * free the resources of the card (multi, single, or bridged)
1524  * Parameters: slot, flag to say if this is for removing entire module or just
1525  * unconfiguring the device
1526  * TO DO:  will probably need to add some code in case there was some resource,
1527  * to remove it... this is from when we have errors in the configure_card...
1528  *          !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1529  * Returns: 0, -1, -ENODEV
1530  */
1531 int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1532 {
1533     int i;
1534     int count;
1535     int rc;
1536     struct slot *sl = *slot_cur;
1537     struct pci_func *cur_func = NULL;
1538     struct pci_func *temp_func;
1539 
1540     debug("%s - enter\n", __func__);
1541 
1542     if (!the_end) {
1543         /* Need to unconfigure the card */
1544         rc = unconfigure_boot_card(sl);
1545         if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1546             /* In all other cases, will still need to get rid of func structure if it exists */
1547             return rc;
1548         }
1549     }
1550 
1551     if (sl->func) {
1552         cur_func = sl->func;
1553         while (cur_func) {
1554             /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1555             if (cur_func->bus) {
1556                 /* in other words, it's a PPB */
1557                 count = 2;
1558             } else {
1559                 count = 6;
1560             }
1561 
1562             for (i = 0; i < count; i++) {
1563                 if (cur_func->io[i]) {
1564                     debug("io[%d] exists\n", i);
1565                     if (the_end > 0)
1566                         ibmphp_remove_resource(cur_func->io[i]);
1567                     cur_func->io[i] = NULL;
1568                 }
1569                 if (cur_func->mem[i]) {
1570                     debug("mem[%d] exists\n", i);
1571                     if (the_end > 0)
1572                         ibmphp_remove_resource(cur_func->mem[i]);
1573                     cur_func->mem[i] = NULL;
1574                 }
1575                 if (cur_func->pfmem[i]) {
1576                     debug("pfmem[%d] exists\n", i);
1577                     if (the_end > 0)
1578                         ibmphp_remove_resource(cur_func->pfmem[i]);
1579                     cur_func->pfmem[i] = NULL;
1580                 }
1581             }
1582 
1583             temp_func = cur_func->next;
1584             kfree(cur_func);
1585             cur_func = temp_func;
1586         }
1587     }
1588 
1589     sl->func = NULL;
1590     *slot_cur = sl;
1591     debug("%s - exit\n", __func__);
1592     return 0;
1593 }
1594 
1595 /*
1596  * add a new bus resulting from hot-plugging a PPB bridge with devices
1597  *
1598  * Input: bus and the amount of resources needed (we know we can assign those,
1599  *        since they've been checked already
1600  * Output: bus added to the correct spot
1601  *         0, -1, error
1602  */
1603 static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1604 {
1605     struct range_node *io_range = NULL;
1606     struct range_node *mem_range = NULL;
1607     struct range_node *pfmem_range = NULL;
1608     struct bus_node *cur_bus = NULL;
1609 
1610     /* Trying to find the parent bus number */
1611     if (parent_busno != 0xFF) {
1612         cur_bus = ibmphp_find_res_bus(parent_busno);
1613         if (!cur_bus) {
1614             err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1615             return -ENODEV;
1616         }
1617 
1618         list_add(&bus->bus_list, &cur_bus->bus_list);
1619     }
1620     if (io) {
1621         io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1622         if (!io_range)
1623             return -ENOMEM;
1624 
1625         io_range->start = io->start;
1626         io_range->end = io->end;
1627         io_range->rangeno = 1;
1628         bus->noIORanges = 1;
1629         bus->rangeIO = io_range;
1630     }
1631     if (mem) {
1632         mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1633         if (!mem_range)
1634             return -ENOMEM;
1635 
1636         mem_range->start = mem->start;
1637         mem_range->end = mem->end;
1638         mem_range->rangeno = 1;
1639         bus->noMemRanges = 1;
1640         bus->rangeMem = mem_range;
1641     }
1642     if (pfmem) {
1643         pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1644         if (!pfmem_range)
1645             return -ENOMEM;
1646 
1647         pfmem_range->start = pfmem->start;
1648         pfmem_range->end = pfmem->end;
1649         pfmem_range->rangeno = 1;
1650         bus->noPFMemRanges = 1;
1651         bus->rangePFMem = pfmem_range;
1652     }
1653     return 0;
1654 }
1655 
1656 /*
1657  * find the 1st available bus number for PPB to set as its secondary bus
1658  * Parameters: bus_number of the primary bus
1659  * Returns: bus_number of the secondary bus or 0xff in case of failure
1660  */
1661 static u8 find_sec_number(u8 primary_busno, u8 slotno)
1662 {
1663     int min, max;
1664     u8 busno;
1665     struct bus_info *bus;
1666     struct bus_node *bus_cur;
1667 
1668     bus = ibmphp_find_same_bus_num(primary_busno);
1669     if (!bus) {
1670         err("cannot get slot range of the bus from the BIOS\n");
1671         return 0xff;
1672     }
1673     max = bus->slot_max;
1674     min = bus->slot_min;
1675     if ((slotno > max) || (slotno < min)) {
1676         err("got the wrong range\n");
1677         return 0xff;
1678     }
1679     busno = (u8) (slotno - (u8) min);
1680     busno += primary_busno + 0x01;
1681     bus_cur = ibmphp_find_res_bus(busno);
1682     /* either there is no such bus number, or there are no ranges, which
1683      * can only happen if we removed the bridged device in previous load
1684      * of the driver, and now only have the skeleton bus struct
1685      */
1686     if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1687         return busno;
1688     return 0xff;
1689 }