2  * IBM Hot Plug Controller Driver
 
   4  * Written By: Irene Zubarev, IBM Corporation
 
   6  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
 
   7  * Copyright (C) 2001,2002 IBM Corp.
 
  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.
 
  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
 
  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.
 
  26  * Send feedback to <gregkh@us.ibm.com>
 
  30 #include <linux/module.h>
 
  31 #include <linux/slab.h>
 
  32 #include <linux/pci.h>
 
  33 #include <linux/list.h>
 
  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);
 
  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)
 
  50 static void assign_alt_irq (struct pci_func * cur_func, u8 class_code)
 
  53         for (j = 0; j < 4; j++) {
 
  54                 if (cur_func->irq[j] == 0xff) {
 
  56                                 case PCI_BASE_CLASS_STORAGE:
 
  57                                         cur_func->irq[j] = SCSI_IRQ;
 
  59                                 case PCI_BASE_CLASS_NETWORK:
 
  60                                         cur_func->irq[j] = LAN_IRQ;
 
  63                                         cur_func->irq[j] = OTHER_IRQ;
 
  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
 
  75  * Input: function to be added
 
  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....
 
  81 int ibmphp_configure_card (struct pci_func *func, u8 slotno)
 
  86         u8 hdr_type, device, sec_number;
 
  88         struct pci_func *newfunc;       /* for multi devices */
 
  89         struct pci_func *cur_func, *prev_func;
 
  93         u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
 
  95         debug ("inside configure_card, func->busno = %x\n", func->busno);
 
  97         device = func->device;
 
 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 
 
 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;
 
 110                 cur_func->function = function;
 
 112                 debug ("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->funcion = %x\n",
 
 113                         cur_func->busno, cur_func->device, cur_func->function);
 
 115                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
 
 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);
 
 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
 
 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);
 
 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);
 
 139                         } else if (class == PCI_CLASS_DISPLAY_VGA) {
 
 140                                 err ("The device %x is not supported for hot plugging. "
 
 141                                      "Please choose another device.\n", cur_func->device);
 
 145                                 case PCI_HEADER_TYPE_NORMAL:
 
 146                                         debug ("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
 
 147                                         assign_alt_irq (cur_func, class_code);
 
 148                                         if ((rc = configure_device (cur_func)) < 0) {
 
 149                                                 /* We need to do this in case some other BARs were properly inserted */
 
 150                                                 err ("was not able to configure devfunc %x on bus %x.\n",
 
 151                                                      cur_func->device, cur_func->busno);
 
 155                                         cur_func->next = NULL;
 
 158                                 case PCI_HEADER_TYPE_MULTIDEVICE:
 
 159                                         assign_alt_irq (cur_func, class_code);
 
 160                                         if ((rc = configure_device (cur_func)) < 0) {
 
 161                                                 /* We need to do this in case some other BARs were properly inserted */
 
 162                                                 err ("was not able to configure devfunc %x on bus %x...bailing out\n",
 
 163                                                      cur_func->device, cur_func->busno);
 
 167                                         newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 
 169                                                 err ("out of system memory\n");
 
 172                                         newfunc->busno = cur_func->busno;
 
 173                                         newfunc->device = device;
 
 174                                         cur_func->next = newfunc;
 
 176                                         for (j = 0; j < 4; j++)
 
 177                                                 newfunc->irq[j] = cur_func->irq[j];
 
 179                                 case PCI_HEADER_TYPE_MULTIBRIDGE:
 
 181                                         if (class != PCI_CLASS_BRIDGE_PCI) {
 
 182                                                 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
 
 183                                                      "Please insert another card.\n", cur_func->device);
 
 186                                         assign_alt_irq (cur_func, class_code);
 
 187                                         rc = configure_bridge (&cur_func, slotno);
 
 189                                                 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 
 190                                                 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 
 194                                                 /* We need to do this in case some other BARs were properly inserted */
 
 195                                                 err ("was not able to hot-add PPB properly.\n");
 
 196                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 
 201                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 
 203                                         for (i = 0; i < 32; i++) {
 
 204                                                 if (func->devices[i]) {
 
 205                                                         newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 
 207                                                                 err ("out of system memory\n");
 
 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];
 
 216                                                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
 
 217                                                                 prev_func->next = newfunc;
 
 219                                                                 cur_func->next = newfunc;
 
 221                                                         rc = ibmphp_configure_card (newfunc, slotno);
 
 222                                                         /* This could only happen if kmalloc failed */
 
 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 */
 
 233                                         newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 
 235                                                 err ("out of system memory\n");
 
 238                                         newfunc->busno = cur_func->busno;
 
 239                                         newfunc->device = device;
 
 240                                         for (j = 0; j < 4; j++)
 
 241                                                 newfunc->irq[j] = cur_func->irq[j];
 
 242                                         for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
 
 243                                         prev_func->next = newfunc;
 
 246                                 case PCI_HEADER_TYPE_BRIDGE:
 
 248                                         debug ("class now is %x\n", class);
 
 249                                         if (class != PCI_CLASS_BRIDGE_PCI) {
 
 250                                                 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
 
 251                                                      "Please insert another card.\n", cur_func->device);
 
 255                                         assign_alt_irq (cur_func, class_code);
 
 257                                         debug ("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
 
 258                                         rc = configure_bridge (&cur_func, slotno);
 
 260                                                 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 
 261                                                 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 
 265                                                 /* We need to do this in case some other BARs were properly inserted */
 
 266                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 
 267                                                 err ("was not able to hot-add PPB properly.\n");
 
 271                                         debug ("cur_func->busno = %x, device = %x, function = %x\n",
 
 272                                                 cur_func->busno, device, function);
 
 273                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 
 274                                         debug ("after configuring bridge..., sec_number = %x\n", sec_number);
 
 276                                         for (i = 0; i < 32; i++) {
 
 277                                                 if (func->devices[i]) {
 
 278                                                         debug ("inside for loop, device is %x\n", i);
 
 279                                                         newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 
 281                                                                 err (" out of system memory\n");
 
 284                                                         newfunc->busno = sec_number;
 
 285                                                         newfunc->device = (u8) i;
 
 286                                                         for (j = 0; j < 4; j++)
 
 287                                                                 newfunc->irq[j] = cur_func->irq[j];
 
 290                                                                 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
 
 291                                                                 prev_func->next = newfunc;
 
 293                                                                 cur_func->next = newfunc;
 
 295                                                         rc = ibmphp_configure_card (newfunc, slotno);
 
 297                                                         /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
 
 299                                                                 /* We need to do this in case some other BARs were properly inserted */
 
 300                                                                 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 
 311                                         err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
 
 314                         }       /* end of switch */
 
 315                 }       /* end of valid device */
 
 319                 err ("Cannot find any valid devices on the card.  Or unable to read from card.\n");
 
 326         for (i = 0; i < cleanup_count; i++) {
 
 327                 if (cur_func->io[i]) {
 
 328                         ibmphp_remove_resource (cur_func->io[i]);
 
 329                         cur_func->io[i] = NULL;
 
 330                 } else if (cur_func->pfmem[i]) {
 
 331                         ibmphp_remove_resource (cur_func->pfmem[i]);
 
 332                         cur_func->pfmem[i] = NULL;
 
 333                 } else if (cur_func->mem[i]) {
 
 334                         ibmphp_remove_resource (cur_func->mem[i]);
 
 335                         cur_func->mem[i] = NULL;
 
 342  * This function configures the pci BARs of a single device.  
 
 343  * Input: pointer to the pci_func
 
 344  * Output: configured PCI, 0, or error
 
 346 static int configure_device (struct pci_func *func)
 
 361         struct resource_node *io[6];
 
 362         struct resource_node *mem[6];
 
 363         struct resource_node *mem_tmp;
 
 364         struct resource_node *pfmem[6];
 
 367         debug ("%s - inside\n", __FUNCTION__);
 
 369         devfn = PCI_DEVFN(func->device, func->function);
 
 370         ibmphp_pci_bus->number = func->busno;
 
 372         for (count = 0; address[count]; count++) {      /* for 6 BARs */
 
 374                 /* not sure if i need this.  per scott, said maybe need smth like this
 
 375                    if devices don't adhere 100% to the spec, so don't want to write
 
 378                 pcibios_read_config_byte(cur_func->busno, cur_func->device, 
 
 379                 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
 
 380                 if (tmp & 0x01) // IO
 
 381                         pcibios_write_config_dword(cur_func->busno, cur_func->device, 
 
 382                         PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
 
 384                         pcibios_write_config_dword(cur_func->busno, cur_func->device, 
 
 385                         PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
 
 387                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 
 388                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
 390                 if (!bar[count])        /* This BAR is not implemented */
 
 393                 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
 
 395                 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 
 397                         debug ("inside IO SPACE\n");
 
 399                         len[count] = bar[count] & 0xFFFFFFFC;
 
 400                         len[count] = ~len[count] + 1;
 
 402                         debug ("len[count] in IO %x, count %d\n", len[count], count);
 
 404                         io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 407                                 err ("out of system memory\n");
 
 410                         io[count]->type = IO;
 
 411                         io[count]->busno = func->busno;
 
 412                         io[count]->devfunc = PCI_DEVFN(func->device, func->function);
 
 413                         io[count]->len = len[count];
 
 414                         if (ibmphp_check_resource(io[count], 0) == 0) {
 
 415                                 ibmphp_add_resource (io[count]);
 
 416                                 func->io[count] = io[count];
 
 418                                 err ("cannot allocate requested io for bus %x device %x function %x len %x\n",
 
 419                                      func->busno, func->device, func->function, len[count]);
 
 423                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 
 425                         /* _______________This is for debugging purposes only_____________________ */ 
 
 426                         debug ("b4 writing, the IO address is %x\n", func->io[count]->start);
 
 427                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
 428                         debug ("after writing.... the start address is %x\n", bar[count]);
 
 429                         /* _________________________________________________________________________*/
 
 433                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 
 435                                 debug ("PFMEM SPACE\n");
 
 437                                 len[count] = bar[count] & 0xFFFFFFF0;
 
 438                                 len[count] = ~len[count] + 1;
 
 440                                 debug ("len[count] in PFMEM %x, count %d\n", len[count], count);
 
 442                                 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 444                                         err ("out of system memory\n");
 
 447                                 pfmem[count]->type = PFMEM;
 
 448                                 pfmem[count]->busno = func->busno;
 
 449                                 pfmem[count]->devfunc = PCI_DEVFN(func->device,
 
 451                                 pfmem[count]->len = len[count];
 
 452                                 pfmem[count]->fromMem = 0;
 
 453                                 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
 
 454                                         ibmphp_add_resource (pfmem[count]);
 
 455                                         func->pfmem[count] = pfmem[count];
 
 457                                         mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 
 459                                                 err ("out of system memory\n");
 
 460                                                 kfree (pfmem[count]);
 
 464                                         mem_tmp->busno = pfmem[count]->busno;
 
 465                                         mem_tmp->devfunc = pfmem[count]->devfunc;
 
 466                                         mem_tmp->len = pfmem[count]->len;
 
 467                                         debug ("there's no pfmem... going into mem.\n");
 
 468                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
 
 469                                                 ibmphp_add_resource (mem_tmp);
 
 470                                                 pfmem[count]->fromMem = 1;
 
 471                                                 pfmem[count]->rangeno = mem_tmp->rangeno;
 
 472                                                 pfmem[count]->start = mem_tmp->start;
 
 473                                                 pfmem[count]->end = mem_tmp->end;
 
 474                                                 ibmphp_add_pfmem_from_mem (pfmem[count]);
 
 475                                                 func->pfmem[count] = pfmem[count];
 
 477                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
 
 478                                                      func->busno, func->device, len[count]);
 
 480                                                 kfree (pfmem[count]);
 
 485                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 
 487                                 /*_______________This is for debugging purposes only______________________________*/                            
 
 488                                 debug ("b4 writing, start address is %x\n", func->pfmem[count]->start);
 
 489                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
 490                                 debug ("after writing, start address is %x\n", bar[count]);
 
 491                                 /*_________________________________________________________________________________*/
 
 493                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {        /* takes up another dword */
 
 494                                         debug ("inside the mem 64 case, count %d\n", count);
 
 496                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 
 497                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
 
 501                                 debug ("REGULAR MEM SPACE\n");
 
 503                                 len[count] = bar[count] & 0xFFFFFFF0;
 
 504                                 len[count] = ~len[count] + 1;
 
 506                                 debug ("len[count] in Mem %x, count %d\n", len[count], count);
 
 508                                 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 510                                         err ("out of system memory\n");
 
 513                                 mem[count]->type = MEM;
 
 514                                 mem[count]->busno = func->busno;
 
 515                                 mem[count]->devfunc = PCI_DEVFN(func->device,
 
 517                                 mem[count]->len = len[count];
 
 518                                 if (ibmphp_check_resource (mem[count], 0) == 0) {
 
 519                                         ibmphp_add_resource (mem[count]);
 
 520                                         func->mem[count] = mem[count];
 
 522                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
 
 523                                              func->busno, func->device, len[count]);
 
 527                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 
 528                                 /* _______________________This is for debugging purposes only _______________________*/
 
 529                                 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
 
 530                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
 531                                 debug ("after writing, the address is %x\n", bar[count]);
 
 532                                 /* __________________________________________________________________________________*/
 
 534                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
 535                                         /* takes up another dword */
 
 536                                         debug ("inside mem 64 case, reg. mem, count %d\n", count);
 
 538                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 
 539                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
 
 545         func->bus = 0;          /* To indicate that this is not a PPB */
 
 546         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
 
 547         if ((irq > 0x00) && (irq < 0x05))
 
 548                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
 
 550         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 
 551         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 
 553         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
 
 554         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
 
 559 /******************************************************************************
 
 560  * This routine configures a PCI-2-PCI bridge and the functions behind it
 
 561  * Parameters: pci_func
 
 563  ******************************************************************************/
 
 564 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
 
 577         u8 need_io_upper = 0;
 
 578         u8 need_pfmem_upper = 0;
 
 579         struct res_needed *amount_needed = NULL;
 
 580         struct resource_node *io = NULL;
 
 581         struct resource_node *bus_io[2] = {NULL, NULL};
 
 582         struct resource_node *mem = NULL;
 
 583         struct resource_node *bus_mem[2] = {NULL, NULL};
 
 584         struct resource_node *mem_tmp = NULL;
 
 585         struct resource_node *pfmem = NULL;
 
 586         struct resource_node *bus_pfmem[2] = {NULL, NULL};
 
 587         struct bus_node *bus;
 
 593         struct pci_func *func = *func_passed;
 
 598         debug ("%s - enter\n", __FUNCTION__);
 
 600         devfn = PCI_DEVFN(func->function, func->device);
 
 601         ibmphp_pci_bus->number = func->busno;
 
 603         /* Configuring necessary info for the bridge so that we could see the devices
 
 607         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
 
 609         /* _____________________For debugging purposes only __________________________
 
 610         pci_bus_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
 
 611         debug ("primary # written into the bridge is %x\n", pri_number);
 
 612          ___________________________________________________________________________*/
 
 614         /* in EBDA, only get allocated 1 additional bus # per slot */
 
 615         sec_number = find_sec_number (func->busno, slotno);
 
 616         if (sec_number == 0xff) {
 
 617                 err ("cannot allocate secondary bus number for the bridged device\n");
 
 621         debug ("after find_sec_number, the number we got is %x\n", sec_number);
 
 622         debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
 
 624         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
 
 626         /* __________________For debugging purposes only __________________________________
 
 627         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 
 628         debug ("sec_number after write/read is %x\n", sec_number);
 
 629          ________________________________________________________________________________*/
 
 631         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
 
 633         /* __________________For debugging purposes only ____________________________________
 
 634         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
 
 635         debug ("subordinate number after write/read is %x\n", sec_number);
 
 636          __________________________________________________________________________________*/
 
 638         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 
 639         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 
 640         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
 
 642         debug ("func->busno is %x\n", func->busno);
 
 643         debug ("sec_number after writing is %x\n", sec_number);
 
 646         /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 647            !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!! 
 
 648            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
 
 651         /* First we need to allocate mem/io for the bridge itself in case it needs it */
 
 652         for (count = 0; address[count]; count++) {      /* for 2 BARs */
 
 653                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 
 654                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
 657                         /* This BAR is not implemented */
 
 658                         debug ("so we come here then, eh?, count = %d\n", count);
 
 661                 //  tmp_bar = bar[count];
 
 663                 debug ("Bar %d wants %x\n", count, bar[count]);
 
 665                 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 
 667                         len[count] = bar[count] & 0xFFFFFFFC;
 
 668                         len[count] = ~len[count] + 1;
 
 670                         debug ("len[count] in IO = %x\n", len[count]);
 
 672                         bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 674                         if (!bus_io[count]) {
 
 675                                 err ("out of system memory\n");
 
 679                         bus_io[count]->type = IO;
 
 680                         bus_io[count]->busno = func->busno;
 
 681                         bus_io[count]->devfunc = PCI_DEVFN(func->device,
 
 683                         bus_io[count]->len = len[count];
 
 684                         if (ibmphp_check_resource (bus_io[count], 0) == 0) {
 
 685                                 ibmphp_add_resource (bus_io[count]);
 
 686                                 func->io[count] = bus_io[count];
 
 688                                 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
 
 689                                      func->busno, func->device, len[count]);
 
 690                                 kfree (bus_io[count]);
 
 694                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 
 698                         if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 
 700                                 len[count] = bar[count] & 0xFFFFFFF0;
 
 701                                 len[count] = ~len[count] + 1;
 
 703                                 debug ("len[count] in PFMEM = %x\n", len[count]);
 
 705                                 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 706                                 if (!bus_pfmem[count]) {
 
 707                                         err ("out of system memory\n");
 
 711                                 bus_pfmem[count]->type = PFMEM;
 
 712                                 bus_pfmem[count]->busno = func->busno;
 
 713                                 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
 
 715                                 bus_pfmem[count]->len = len[count];
 
 716                                 bus_pfmem[count]->fromMem = 0;
 
 717                                 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
 
 718                                         ibmphp_add_resource (bus_pfmem[count]);
 
 719                                         func->pfmem[count] = bus_pfmem[count];
 
 721                                         mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 
 723                                                 err ("out of system memory\n");
 
 728                                         mem_tmp->busno = bus_pfmem[count]->busno;
 
 729                                         mem_tmp->devfunc = bus_pfmem[count]->devfunc;
 
 730                                         mem_tmp->len = bus_pfmem[count]->len;
 
 731                                         if (ibmphp_check_resource (mem_tmp, 0) == 0) {
 
 732                                                 ibmphp_add_resource (mem_tmp);
 
 733                                                 bus_pfmem[count]->fromMem = 1;
 
 734                                                 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
 
 735                                                 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
 
 736                                                 func->pfmem[count] = bus_pfmem[count];
 
 738                                                 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n", 
 
 739                                                      func->busno, func->device, len[count]);
 
 741                                                 kfree (bus_pfmem[count]);
 
 746                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 
 748                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
 749                                         /* takes up another dword */
 
 751                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 
 752                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
 
 757                                 len[count] = bar[count] & 0xFFFFFFF0;
 
 758                                 len[count] = ~len[count] + 1;
 
 760                                 debug ("len[count] in Memory is %x\n", len[count]);
 
 762                                 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 
 763                                 if (!bus_mem[count]) {
 
 764                                         err ("out of system memory\n");
 
 768                                 bus_mem[count]->type = MEM;
 
 769                                 bus_mem[count]->busno = func->busno;
 
 770                                 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
 
 772                                 bus_mem[count]->len = len[count];
 
 773                                 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
 
 774                                         ibmphp_add_resource (bus_mem[count]);
 
 775                                         func->mem[count] = bus_mem[count];
 
 777                                         err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
 
 778                                              func->busno, func->device, len[count]);
 
 779                                         kfree (bus_mem[count]);
 
 783                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 
 785                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
 786                                         /* takes up another dword */
 
 788                                         /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 
 789                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
 
 796         /* Now need to see how much space the devices behind the bridge needed */
 
 797         amount_needed = scan_behind_bridge (func, sec_number);
 
 798         if (amount_needed == NULL)
 
 801         ibmphp_pci_bus->number = func->busno;
 
 802         debug ("after coming back from scan_behind_bridge\n");
 
 803         debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
 
 804         debug ("amount_needed->io = %x\n", amount_needed->io);
 
 805         debug ("amount_needed->mem = %x\n", amount_needed->mem);
 
 806         debug ("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
 
 808         if (amount_needed->not_correct) {               
 
 809                 debug ("amount_needed is not correct\n");
 
 810                 for (count = 0; address[count]; count++) {
 
 813                                 ibmphp_remove_resource (bus_io[count]);
 
 814                                 func->io[count] = NULL;
 
 815                         } else if (bus_pfmem[count]) {
 
 816                                 ibmphp_remove_resource (bus_pfmem[count]);
 
 817                                 func->pfmem[count] = NULL;
 
 818                         } else if (bus_mem[count]) {
 
 819                                 ibmphp_remove_resource (bus_mem[count]);
 
 820                                 func->mem[count] = NULL;
 
 823                 kfree (amount_needed);
 
 827         if (!amount_needed->io) {
 
 828                 debug ("it doesn't want IO?\n");
 
 831                 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
 
 832                 io = kzalloc(sizeof(*io), GFP_KERNEL);
 
 835                         err ("out of system memory\n");
 
 840                 io->busno = func->busno;
 
 841                 io->devfunc = PCI_DEVFN(func->device, func->function);
 
 842                 io->len = amount_needed->io;
 
 843                 if (ibmphp_check_resource (io, 1) == 0) {
 
 844                         debug ("were we able to add io\n");
 
 845                         ibmphp_add_resource (io);
 
 850         if (!amount_needed->mem) {
 
 851                 debug ("it doesn't want n.e.memory?\n");
 
 854                 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
 
 855                 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
 
 857                         err ("out of system memory\n");
 
 862                 mem->busno = func->busno;
 
 863                 mem->devfunc = PCI_DEVFN(func->device, func->function);
 
 864                 mem->len = amount_needed->mem;
 
 865                 if (ibmphp_check_resource (mem, 1) == 0) {
 
 866                         ibmphp_add_resource (mem);
 
 868                         debug ("were we able to add mem\n");
 
 872         if (!amount_needed->pfmem) {
 
 873                 debug ("it doesn't want n.e.pfmem mem?\n");
 
 876                 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
 
 877                 pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
 
 879                         err ("out of system memory\n");
 
 884                 pfmem->busno = func->busno;
 
 885                 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
 
 886                 pfmem->len = amount_needed->pfmem;
 
 888                 if (ibmphp_check_resource (pfmem, 1) == 0) {
 
 889                         ibmphp_add_resource (pfmem);
 
 892                         mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 
 894                                 err ("out of system memory\n");
 
 899                         mem_tmp->busno = pfmem->busno;
 
 900                         mem_tmp->devfunc = pfmem->devfunc;
 
 901                         mem_tmp->len = pfmem->len;
 
 902                         if (ibmphp_check_resource (mem_tmp, 1) == 0) {
 
 903                                 ibmphp_add_resource (mem_tmp);
 
 905                                 pfmem->rangeno = mem_tmp->rangeno;
 
 906                                 ibmphp_add_pfmem_from_mem (pfmem);
 
 912         debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
 
 913         debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
 
 915         if (flag_io && flag_mem && flag_pfmem) {
 
 916                 /* If on bootup, there was a bridged card in this slot,
 
 917                  * then card was removed and ibmphp got unloaded and loaded
 
 918                  * back again, there's no way for us to remove the bus
 
 919                  * struct, so no need to kmalloc, can use existing node
 
 921                 bus = ibmphp_find_res_bus (sec_number);
 
 923                         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
 
 925                                 err ("out of system memory\n");
 
 929                         bus->busno = sec_number;
 
 930                         debug ("b4 adding new bus\n");
 
 931                         rc = add_new_bus (bus, io, mem, pfmem, func->busno);
 
 932                 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
 
 933                         rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
 
 935                         err ("expected bus structure not empty?\n");
 
 941                                 ibmphp_remove_bus (bus, func->busno);
 
 942                                 kfree (amount_needed);
 
 948                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
 
 949                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
 
 951                 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
 
 955                 if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
 
 956                         debug ("pfmem 64\n");
 
 957                         need_pfmem_upper = 1;
 
 960                 if (bus->noIORanges) {
 
 961                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
 
 962                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8); 
 
 964                         /* _______________This is for debugging purposes only ____________________
 
 965                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
 
 966                         debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 
 967                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
 
 968                         debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 
 969                          ________________________________________________________________________*/
 
 971                         if (need_io_upper) {    /* since can't support n.e.ways */
 
 972                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
 
 973                                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
 
 976                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
 
 977                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
 
 980                 if (bus->noMemRanges) {
 
 981                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
 
 982                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
 
 984                         /* ____________________This is for debugging purposes only ________________________
 
 985                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
 
 986                         debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 
 987                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
 
 988                         debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 
 989                          __________________________________________________________________________________*/
 
 992                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
 
 993                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
 
 995                 if (bus->noPFMemRanges) {
 
 996                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
 
 997                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
 
 999                         /* __________________________This is for debugging purposes only _______________________
 
1000                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
 
1001                         debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 
1002                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
 
1003                         debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 
1004                          ______________________________________________________________________________________*/
 
1006                         if (need_pfmem_upper) { /* since can't support n.e.ways */
 
1007                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
 
1008                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
 
1011                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
 
1012                         pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
 
1015                 debug ("b4 writing control information\n");
 
1017                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
 
1018                 if ((irq > 0x00) && (irq < 0x05))
 
1019                         pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
 
1021                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
 
1022                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
 
1023                 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
 
1026                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
 
1027                 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
 
1028                 for (i = 0; i < 32; i++) {
 
1029                         if (amount_needed->devices[i]) {
 
1030                                 debug ("device where devices[i] is 1 = %x\n", i);
 
1031                                 func->devices[i] = 1;
 
1034                 func->bus = 1;  /* For unconfiguring, to indicate it's PPB */
 
1035                 func_passed = &func;
 
1036                 debug ("func->busno b4 returning is %x\n", func->busno);
 
1037                 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
 
1038                 kfree (amount_needed);
 
1041                 err ("Configuring bridge was unsuccessful...\n");
 
1048         kfree(amount_needed);
 
1050                 ibmphp_remove_resource (pfmem);
 
1052                 ibmphp_remove_resource (io);
 
1054                 ibmphp_remove_resource (mem);
 
1055         for (i = 0; i < 2; i++) {       /* for 2 BARs */
 
1057                         ibmphp_remove_resource (bus_io[i]);
 
1059                 } else if (bus_pfmem[i]) {
 
1060                         ibmphp_remove_resource (bus_pfmem[i]);
 
1061                         func->pfmem[i] = NULL;
 
1062                 } else if (bus_mem[i]) {
 
1063                         ibmphp_remove_resource (bus_mem[i]);
 
1064                         func->mem[i] = NULL;
 
1070 /*****************************************************************************
 
1071  * This function adds up the amount of resources needed behind the PPB bridge
 
1072  * and passes it to the configure_bridge function
 
1073  * Input: bridge function
 
1074  * Ouput: amount of resources needed
 
1075  *****************************************************************************/
 
1076 static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno)
 
1081         u8 device, function;
 
1083         int howmany = 0;        /*this is to see if there are any devices behind the bridge */
 
1095         struct res_needed *amount;
 
1097         amount = kzalloc(sizeof(*amount), GFP_KERNEL);
 
1101         ibmphp_pci_bus->number = busno;
 
1103         debug ("the bus_no behind the bridge is %x\n", busno);
 
1104         debug ("scanning devices behind the bridge...\n");
 
1105         for (device = 0; device < 32; device++) {
 
1106                 amount->devices[device] = 0;
 
1107                 for (function = 0; function < 8; function++) {
 
1108                         devfn = PCI_DEVFN(device, function);
 
1110                         pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
 
1112                         if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
 
1113                                 /* found correct device!!! */
 
1116                                 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
 
1117                                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
 
1119                                 debug ("hdr_type behind the bridge is %x\n", hdr_type);
 
1120                                 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
 
1121                                         err ("embedded bridges not supported for hot-plugging.\n");
 
1122                                         amount->not_correct = 1;
 
1126                                 class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
 
1127                                 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
 
1128                                         err ("The device %x is VGA compatible and as is not supported for hot plugging. "
 
1129                                              "Please choose another device.\n", device);
 
1130                                         amount->not_correct = 1;
 
1132                                 } else if (class == PCI_CLASS_DISPLAY_VGA) {
 
1133                                         err ("The device %x is not supported for hot plugging. "
 
1134                                              "Please choose another device.\n", device);
 
1135                                         amount->not_correct = 1;
 
1139                                 amount->devices[device] = 1;
 
1141                                 for (count = 0; address[count]; count++) {
 
1144                                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp);
 
1145                                         if (tmp & 0x01) // IO
 
1146                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
 
1148                                                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 
1150                                         pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 
1151                                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
 
1153                                         debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
 
1155                                         if (!bar[count])        /* This BAR is not implemented */
 
1158                                         //tmp_bar = bar[count];
 
1160                                         debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
 
1162                                         if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 
1164                                                 len[count] = bar[count] & 0xFFFFFFFC;
 
1165                                                 len[count] = ~len[count] + 1;
 
1166                                                 amount->io += len[count];
 
1168                                                 /* This is Memory */
 
1169                                                 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 
1171                                                         len[count] = bar[count] & 0xFFFFFFF0;
 
1172                                                         len[count] = ~len[count] + 1;
 
1173                                                         amount->pfmem += len[count];
 
1174                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
 
1175                                                                 /* takes up another dword */
 
1179                                                         /* regular memory */
 
1180                                                         len[count] = bar[count] & 0xFFFFFFF0;
 
1181                                                         len[count] = ~len[count] + 1;
 
1182                                                         amount->mem += len[count];
 
1183                                                         if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
1184                                                                 /* takes up another dword */
 
1190                         }       /* end if (valid) */
 
1195                 amount->not_correct = 1;
 
1197                 amount->not_correct = 0;
 
1198         if ((amount->io) && (amount->io < IOBRIDGE))
 
1199                 amount->io = IOBRIDGE;
 
1200         if ((amount->mem) && (amount->mem < MEMBRIDGE))
 
1201                 amount->mem = MEMBRIDGE;
 
1202         if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
 
1203                 amount->pfmem = MEMBRIDGE;
 
1207 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card 
 
1208  * upon bootup in the system, since we don't allocate func to such case, we need to read 
 
1209  * the start addresses from pci config space and then find the corresponding entries in 
 
1210  * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
 
1211  * Change: we also call these functions even if we configured the card ourselves (i.e., not
 
1212  * the bootup case), since it should work same way
 
1214 static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
 
1227         struct resource_node *io;
 
1228         struct resource_node *mem;
 
1229         struct resource_node *pfmem;
 
1230         struct bus_node *bus;
 
1237         debug ("%s - enter\n", __FUNCTION__);
 
1239         bus = ibmphp_find_res_bus (busno);
 
1241                 debug ("cannot find corresponding bus.\n");
 
1245         devfn = PCI_DEVFN(device, function);
 
1246         ibmphp_pci_bus->number = busno;
 
1247         for (count = 0; address[count]; count++) {      /* for 6 BARs */
 
1248                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
 
1250                 /* We can do this here, b/c by that time the device driver of the card has been stopped */
 
1252                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 
1253                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size);
 
1254                 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address);
 
1256                 debug ("start_address is %x\n", start_address);
 
1257                 debug ("busno, device, function %x %x %x\n", busno, device, function);
 
1259                         /* This BAR is not implemented */
 
1260                         debug ("is this bar no implemented?, count = %d\n", count);
 
1263                 tmp_address = start_address;
 
1264                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
 
1266                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
 
1267                         size = size & 0xFFFFFFFC;
 
1269                         end_address = start_address + size - 1;
 
1270                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
 
1271                                 err ("cannot find corresponding IO resource to remove\n");
 
1274                         debug ("io->start = %x\n", io->start);
 
1276                         start_address = io->end + 1;
 
1277                         ibmphp_remove_resource (io);
 
1278                         /* This is needed b/c of the old I/O restrictions in the BIOS */
 
1279                         while (temp_end < end_address) {
 
1280                                 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
 
1281                                         err ("cannot find corresponding IO resource to remove\n");
 
1284                                 debug ("io->start = %x\n", io->start);
 
1286                                 start_address = io->end + 1;
 
1287                                 ibmphp_remove_resource (io);
 
1290                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
 
1292                         /* This is Memory */
 
1293                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 
1295                                 debug ("start address of pfmem is %x\n", start_address);
 
1296                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
 
1298                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
 
1299                                         err ("cannot find corresponding PFMEM resource to remove\n");
 
1303                                         debug ("pfmem->start = %x\n", pfmem->start);
 
1305                                         ibmphp_remove_resource(pfmem);
 
1308                                 /* regular memory */
 
1309                                 debug ("start address of mem is %x\n", start_address);
 
1310                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
 
1312                                 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
 
1313                                         err ("cannot find corresponding MEM resource to remove\n");
 
1317                                         debug ("mem->start = %x\n", mem->start);
 
1319                                         ibmphp_remove_resource(mem);
 
1322                         if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
1323                                 /* takes up another dword */
 
1332 static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
 
1335         int bus_no, pri_no, sub_no, sec_no = 0;
 
1336         u32 start_address, tmp_address;
 
1337         u8 sec_number, sub_number, pri_number;
 
1338         struct resource_node *io = NULL;
 
1339         struct resource_node *mem = NULL;
 
1340         struct resource_node *pfmem = NULL;
 
1341         struct bus_node *bus;
 
1349         devfn = PCI_DEVFN(device, function);
 
1350         ibmphp_pci_bus->number = busno;
 
1351         bus_no = (int) busno;
 
1352         debug ("busno is %x\n", busno);
 
1353         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
 
1354         debug ("%s - busno = %x, primary_number = %x\n", __FUNCTION__, busno, pri_number);
 
1356         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 
1357         debug ("sec_number is %x\n", sec_number);
 
1358         sec_no = (int) sec_number;
 
1359         pri_no = (int) pri_number;
 
1360         if (pri_no != bus_no) {
 
1361                 err ("primary numbers in our structures and pci config space don't match.\n");
 
1365         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
 
1366         sub_no = (int) sub_number;
 
1367         debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
 
1368         if (sec_no != sub_number) {
 
1369                 err ("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
 
1373         bus = ibmphp_find_res_bus (sec_number);
 
1375                 err ("cannot find Bus structure for the bridged device\n");
 
1378         debug("bus->busno is %x\n", bus->busno);
 
1379         debug("sec_number is %x\n", sec_number);
 
1381         ibmphp_remove_bus (bus, busno);
 
1383         for (count = 0; address[count]; count++) {
 
1385                 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
 
1387                 if (!start_address) {
 
1388                         /* This BAR is not implemented */
 
1392                 tmp_address = start_address;
 
1394                 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
 
1396                         start_address &= PCI_BASE_ADDRESS_IO_MASK;
 
1397                         if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
 
1398                                 err ("cannot find corresponding IO resource to remove\n");
 
1402                                 debug ("io->start = %x\n", io->start);
 
1404                         ibmphp_remove_resource (io);
 
1406                         /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
 
1408                         /* This is Memory */
 
1409                         if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 
1411                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
 
1412                                 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
 
1413                                         err ("cannot find corresponding PFMEM resource to remove\n");
 
1417                                         debug ("pfmem->start = %x\n", pfmem->start);
 
1419                                         ibmphp_remove_resource(pfmem);
 
1422                                 /* regular memory */
 
1423                                 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
 
1424                                 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
 
1425                                         err ("cannot find corresponding MEM resource to remove\n");
 
1429                                         debug ("mem->start = %x\n", mem->start);
 
1431                                         ibmphp_remove_resource(mem);
 
1434                         if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 
1435                                 /* takes up another dword */
 
1440         debug ("%s - exiting, returning success\n", __FUNCTION__);
 
1444 static int unconfigure_boot_card (struct slot *slot_cur)
 
1454         u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
 
1456         debug ("%s - enter\n", __FUNCTION__);
 
1458         device = slot_cur->device;
 
1459         busno = slot_cur->bus;
 
1461         debug ("b4 for loop, device is %x\n", device);
 
1462         /* For every function on the card */
 
1463         for (function = 0x0; function < 0x08; function++) {
 
1464                 devfn = PCI_DEVFN(device, function);
 
1465                 ibmphp_pci_bus->number = busno;
 
1467                 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
 
1469                 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
 
1470                         /* found correct device!!! */
 
1473                         debug ("%s - found correct device\n", __FUNCTION__);
 
1475                         /* header: x x x x x x x x
 
1476                          *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
 
1477                          *         |_=> 0 = single function device, 1 = multi-function device
 
1480                         pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
 
1481                         pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
 
1483                         debug ("hdr_type %x, class %x\n", hdr_type, class);
 
1484                         class >>= 8;    /* to take revision out, class = class.subclass.prog i/f */
 
1485                         if (class == PCI_CLASS_NOT_DEFINED_VGA) {
 
1486                                 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
 
1487                                      "Please choose another device.\n", device, function);
 
1489                         } else if (class == PCI_CLASS_DISPLAY_VGA) {
 
1490                                 err ("The device %x function %x is not supported for hot removing. "
 
1491                                      "Please choose another device.\n", device, function);
 
1496                                 case PCI_HEADER_TYPE_NORMAL:
 
1497                                         rc = unconfigure_boot_device (busno, device, function);
 
1499                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
 
1500                                                      device, function, busno);
 
1505                                 case PCI_HEADER_TYPE_MULTIDEVICE:
 
1506                                         rc = unconfigure_boot_device (busno, device, function);
 
1508                                                 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
 
1509                                                      device, function, busno);
 
1513                                 case PCI_HEADER_TYPE_BRIDGE:
 
1515                                         if (class != PCI_CLASS_BRIDGE_PCI) {
 
1516                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
 
1517                                                      "and is not supported for hot-removing. "
 
1518                                                      "Please try another card.\n", device, function);
 
1521                                         rc = unconfigure_boot_bridge (busno, device, function);
 
1523                                                 err ("was not able to hot-remove PPB properly.\n");
 
1529                                 case PCI_HEADER_TYPE_MULTIBRIDGE:
 
1531                                         if (class != PCI_CLASS_BRIDGE_PCI) {
 
1532                                                 err ("This device %x function %x is not PCI-to-PCI bridge, "
 
1533                                                      "and is not supported for hot-removing. "
 
1534                                                      "Please try another card.\n", device, function);
 
1537                                         rc = unconfigure_boot_bridge (busno, device, function);
 
1539                                                 err ("was not able to hot-remove PPB properly.\n");
 
1544                                         err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
 
1547                         }       /* end of switch */
 
1548                 }       /* end of valid device */
 
1551         if (!valid_device) {
 
1552                 err ("Could not find device to unconfigure.  Or could not read the card.\n");
 
1559  * free the resources of the card (multi, single, or bridged)
 
1560  * Parameters: slot, flag to say if this is for removing entire module or just
 
1561  * unconfiguring the device
 
1562  * TO DO:  will probably need to add some code in case there was some resource,
 
1563  * to remove it... this is from when we have errors in the configure_card...
 
1564  *                      !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
 
1565  * Returns: 0, -1, -ENODEV 
 
1567 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
 
1572         struct slot *sl = *slot_cur;
 
1573         struct pci_func *cur_func = NULL;
 
1574         struct pci_func *temp_func;
 
1576         debug ("%s - enter\n", __FUNCTION__);
 
1579                 /* Need to unconfigure the card */
 
1580                 rc = unconfigure_boot_card (sl);
 
1581                 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
 
1582                         /* In all other cases, will still need to get rid of func structure if it exists */
 
1588                 cur_func = sl->func;
 
1590                         /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
 
1591                         if (cur_func->bus) {
 
1592                                 /* in other words, it's a PPB */
 
1598                         for (i = 0; i < count; i++) {
 
1599                                 if (cur_func->io[i]) {
 
1600                                         debug ("io[%d] exists\n", i);
 
1602                                                 ibmphp_remove_resource (cur_func->io[i]);
 
1603                                         cur_func->io[i] = NULL;
 
1605                                 if (cur_func->mem[i]) {
 
1606                                         debug ("mem[%d] exists\n", i);
 
1608                                                 ibmphp_remove_resource (cur_func->mem[i]);
 
1609                                         cur_func->mem[i] = NULL;
 
1611                                 if (cur_func->pfmem[i]) {
 
1612                                         debug ("pfmem[%d] exists\n", i);
 
1614                                                 ibmphp_remove_resource (cur_func->pfmem[i]);
 
1615                                         cur_func->pfmem[i] = NULL;
 
1619                         temp_func = cur_func->next;
 
1621                         cur_func = temp_func;
 
1627         debug ("%s - exit\n", __FUNCTION__);
 
1632  * add a new bus resulting from hot-plugging a PPB bridge with devices
 
1634  * Input: bus and the amount of resources needed (we know we can assign those,
 
1635  *        since they've been checked already
 
1636  * Output: bus added to the correct spot
 
1639 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
 
1641         struct range_node *io_range = NULL;
 
1642         struct range_node *mem_range = NULL;
 
1643         struct range_node *pfmem_range = NULL;
 
1644         struct bus_node *cur_bus = NULL;
 
1646         /* Trying to find the parent bus number */
 
1647         if (parent_busno != 0xFF) {
 
1648                 cur_bus = ibmphp_find_res_bus (parent_busno);
 
1650                         err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
 
1654                 list_add (&bus->bus_list, &cur_bus->bus_list);
 
1657                 io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
 
1659                         err ("out of system memory\n");
 
1662                 io_range->start = io->start;
 
1663                 io_range->end = io->end;
 
1664                 io_range->rangeno = 1;
 
1665                 bus->noIORanges = 1;
 
1666                 bus->rangeIO = io_range;
 
1669                 mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
 
1671                         err ("out of system memory\n");
 
1674                 mem_range->start = mem->start;
 
1675                 mem_range->end = mem->end;
 
1676                 mem_range->rangeno = 1;
 
1677                 bus->noMemRanges = 1;
 
1678                 bus->rangeMem = mem_range;
 
1681                 pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
 
1683                         err ("out of system memory\n");
 
1686                 pfmem_range->start = pfmem->start;
 
1687                 pfmem_range->end = pfmem->end;
 
1688                 pfmem_range->rangeno = 1;
 
1689                 bus->noPFMemRanges = 1;
 
1690                 bus->rangePFMem = pfmem_range;
 
1696  * find the 1st available bus number for PPB to set as its secondary bus
 
1697  * Parameters: bus_number of the primary bus
 
1698  * Returns: bus_number of the secondary bus or 0xff in case of failure
 
1700 static u8 find_sec_number (u8 primary_busno, u8 slotno)
 
1704         struct bus_info *bus;
 
1705         struct bus_node *bus_cur;
 
1707         bus = ibmphp_find_same_bus_num (primary_busno);
 
1709                 err ("cannot get slot range of the bus from the BIOS\n");
 
1712         max = bus->slot_max;
 
1713         min = bus->slot_min;
 
1714         if ((slotno > max) || (slotno < min)) {
 
1715                 err ("got the wrong range\n");
 
1718         busno = (u8) (slotno - (u8) min);
 
1719         busno += primary_busno + 0x01;
 
1720         bus_cur = ibmphp_find_res_bus (busno);
 
1721         /* either there is no such bus number, or there are no ranges, which
 
1722          * can only happen if we removed the bridged device in previous load
 
1723          * of the driver, and now only have the skeleton bus struct
 
1725         if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))