linux-old/drivers/hotplug/shpchp_ctrl.c
<<
>>
Prefs
   1/*
   2 * Standard Hot Plug Controller Driver
   3 *
   4 * Copyright (C) 1995,2001 Compaq Computer Corporation
   5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
   6 * Copyright (C) 2001 IBM Corp.
   7 * Copyright (C) 2003-2004 Intel Corporation
   8 *
   9 * All rights reserved.
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or (at
  14 * your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful, but
  17 * WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19 * NON INFRINGEMENT.  See the GNU General Public License for more
  20 * details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25 *
  26 * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com>
  27 *
  28 */
  29
  30#include <linux/config.h>
  31#include <linux/module.h>
  32#include <linux/kernel.h>
  33#include <linux/types.h>
  34#include <linux/slab.h>
  35#include <linux/tqueue.h>
  36#include <linux/interrupt.h>
  37#include <linux/delay.h>
  38#include <linux/wait.h>
  39#include <linux/smp_lock.h>
  40#include <linux/pci.h>
  41#include "shpchp.h"
  42#include "shpchprm.h"
  43
  44static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
  45        u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
  46static int configure_new_function( struct controller *ctrl, struct pci_func *func,
  47        u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
  48static void interrupt_event_handler(struct controller *ctrl);
  49
  50static struct semaphore event_semaphore;        /* mutex for process loop (up if something to process) */
  51static struct semaphore event_exit;             /* guard ensure thread has exited before calling it quits */
  52static int event_finished;
  53static unsigned long pushbutton_pending;        /* = 0 */
  54
  55u8 shpchp_disk_irq;
  56u8 shpchp_nic_irq;
  57
  58u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
  59{
  60        struct controller *ctrl = (struct controller *) inst_id;
  61        struct slot *p_slot;
  62        u8 rc = 0;
  63        u8 getstatus;
  64        struct pci_func *func;
  65        struct event_info *taskInfo;
  66
  67        /* Attention Button Change */
  68        dbg("shpchp:  Attention button interrupt received.\n");
  69        
  70        func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  71
  72        /* This is the structure that tells the worker thread what to do */
  73        taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  74        p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  75
  76        p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  77        p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  78        
  79        ctrl->next_event = (ctrl->next_event + 1) % 10;
  80        taskInfo->hp_slot = hp_slot;
  81
  82        rc++;
  83
  84        /*
  85         *  Button pressed - See if need to TAKE ACTION!!!
  86         */
  87        info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
  88        taskInfo->event_type = INT_BUTTON_PRESS;
  89
  90        if ((p_slot->state == BLINKINGON_STATE)
  91            || (p_slot->state == BLINKINGOFF_STATE)) {
  92                /* Cancel if we are still blinking; this means that we press the
  93                 * attention again before the 5 sec. limit expires to cancel hot-add
  94                 * or hot-remove
  95                 */
  96                taskInfo->event_type = INT_BUTTON_CANCEL;
  97                info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
  98        } else if ((p_slot->state == POWERON_STATE)
  99                   || (p_slot->state == POWEROFF_STATE)) {
 100                /* Ignore if the slot is on power-on or power-off state; this 
 101                 * means that the previous attention button action to hot-add or
 102                 * hot-remove is undergoing
 103                 */
 104                taskInfo->event_type = INT_BUTTON_IGNORE;
 105                info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
 106        }
 107
 108        if (rc)
 109                up(&event_semaphore);   /* Signal event thread that new event is posted */
 110
 111        return 0;
 112
 113}
 114
 115u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
 116{
 117        struct controller *ctrl = (struct controller *) inst_id;
 118        struct slot *p_slot;
 119        u8 rc = 0;
 120        u8 getstatus;
 121        struct pci_func *func;
 122        struct event_info *taskInfo;
 123
 124        /* Switch Change */
 125        dbg("shpchp:  Switch interrupt received.\n");
 126
 127        func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
 128
 129        /* This is the structure that tells the worker thread
 130         * what to do
 131         */
 132        taskInfo = &(ctrl->event_queue[ctrl->next_event]);
 133        ctrl->next_event = (ctrl->next_event + 1) % 10;
 134        taskInfo->hp_slot = hp_slot;
 135
 136        rc++;
 137        p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
 138        p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
 139        p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
 140        dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
 141                func->presence_save, func->pwr_save);
 142
 143        if (getstatus) {
 144                /*
 145                 * Switch opened
 146                 */
 147                info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
 148                func->switch_save = 0;
 149                taskInfo->event_type = INT_SWITCH_OPEN;
 150                if (func->pwr_save && func->presence_save) {
 151                        taskInfo->event_type = INT_POWER_FAULT;
 152                        err("Surprise Removal of card\n");
 153                }
 154        } else {
 155                /*
 156                 *  Switch closed
 157                 */
 158                info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
 159                func->switch_save = 0x10;
 160                taskInfo->event_type = INT_SWITCH_CLOSE;
 161        }
 162
 163        if (rc)
 164                up(&event_semaphore);   /* Signal event thread that new event is posted */
 165
 166        return rc;
 167}
 168
 169u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
 170{
 171        struct controller *ctrl = (struct controller *) inst_id;
 172        struct slot *p_slot;
 173        u8 rc = 0;
 174        struct pci_func *func;
 175        struct event_info *taskInfo;
 176
 177        /* Presence Change */
 178        dbg("shpchp:  Presence/Notify input change.\n");
 179
 180        func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
 181
 182        /* This is the structure that tells the worker thread
 183         * what to do
 184         */
 185        taskInfo = &(ctrl->event_queue[ctrl->next_event]);
 186        ctrl->next_event = (ctrl->next_event + 1) % 10;
 187        taskInfo->hp_slot = hp_slot;
 188
 189        rc++;
 190        p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
 191
 192        /* 
 193         * Save the presence state
 194         */
 195        p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
 196        if (func->presence_save) {
 197                /*
 198                 * Card Present
 199                 */
 200                info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
 201                taskInfo->event_type = INT_PRESENCE_ON;
 202        } else {
 203                /*
 204                 * Not Present
 205                 */
 206                info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
 207                taskInfo->event_type = INT_PRESENCE_OFF;
 208        }
 209
 210        if (rc)
 211                up(&event_semaphore);   /* Signal event thread that new event is posted */
 212
 213        return rc;
 214}
 215
 216u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
 217{
 218        struct controller *ctrl = (struct controller *) inst_id;
 219        struct slot *p_slot;
 220        u8 rc = 0;
 221        struct pci_func *func;
 222        struct event_info *taskInfo;
 223
 224        /* Power fault */
 225        dbg("shpchp:  Power fault interrupt received.\n");
 226
 227        func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
 228
 229        /* This is the structure that tells the worker thread
 230         * what to do
 231         */
 232        taskInfo = &(ctrl->event_queue[ctrl->next_event]);
 233        ctrl->next_event = (ctrl->next_event + 1) % 10;
 234        taskInfo->hp_slot = hp_slot;
 235
 236        rc++;
 237        p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
 238
 239        if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
 240                /*
 241                 * Power fault Cleared
 242                 */
 243                info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
 244                func->status = 0x00;
 245                taskInfo->event_type = INT_POWER_FAULT_CLEAR;
 246        } else {
 247                /*
 248                 *  Power fault
 249                 */
 250                info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
 251                taskInfo->event_type = INT_POWER_FAULT;
 252                /* Set power fault status for this board */
 253                func->status = 0xFF;
 254                info("power fault bit %x set\n", hp_slot);
 255        }
 256        if (rc)
 257                up(&event_semaphore);   /* Signal event thread that new event is posted */
 258
 259        return rc;
 260}
 261
 262
 263/*
 264 * sort_by_size
 265 *
 266 * Sorts nodes on the list by their length.
 267 * Smallest first.
 268 *
 269 */
 270static int sort_by_size(struct pci_resource **head)
 271{
 272        struct pci_resource *current_res;
 273        struct pci_resource *next_res;
 274        int out_of_order = 1;
 275
 276        if (!(*head))
 277                return(1);
 278
 279        if (!((*head)->next))
 280                return(0);
 281
 282        while (out_of_order) {
 283                out_of_order = 0;
 284
 285                /* Special case for swapping list head */
 286                if (((*head)->next) &&
 287                    ((*head)->length > (*head)->next->length)) {
 288                        out_of_order++;
 289                        current_res = *head;
 290                        *head = (*head)->next;
 291                        current_res->next = (*head)->next;
 292                        (*head)->next = current_res;
 293                }
 294
 295                current_res = *head;
 296
 297                while (current_res->next && current_res->next->next) {
 298                        if (current_res->next->length > current_res->next->next->length) {
 299                                out_of_order++;
 300                                next_res = current_res->next;
 301                                current_res->next = current_res->next->next;
 302                                current_res = current_res->next;
 303                                next_res->next = current_res->next;
 304                                current_res->next = next_res;
 305                        } else
 306                                current_res = current_res->next;
 307                }
 308        }  /* End of out_of_order loop */
 309
 310        return(0);
 311}
 312
 313
 314/*
 315 * sort_by_max_size
 316 *
 317 * Sorts nodes on the list by their length.
 318 * Largest first.
 319 *
 320 */
 321static int sort_by_max_size(struct pci_resource **head)
 322{
 323        struct pci_resource *current_res;
 324        struct pci_resource *next_res;
 325        int out_of_order = 1;
 326
 327        if (!(*head))
 328                return(1);
 329
 330        if (!((*head)->next))
 331                return(0);
 332
 333        while (out_of_order) {
 334                out_of_order = 0;
 335
 336                /* Special case for swapping list head */
 337                if (((*head)->next) &&
 338                    ((*head)->length < (*head)->next->length)) {
 339                        out_of_order++;
 340                        current_res = *head;
 341                        *head = (*head)->next;
 342                        current_res->next = (*head)->next;
 343                        (*head)->next = current_res;
 344                }
 345
 346                current_res = *head;
 347
 348                while (current_res->next && current_res->next->next) {
 349                        if (current_res->next->length < current_res->next->next->length) {
 350                                out_of_order++;
 351                                next_res = current_res->next;
 352                                current_res->next = current_res->next->next;
 353                                current_res = current_res->next;
 354                                next_res->next = current_res->next;
 355                                current_res->next = next_res;
 356                        } else
 357                                current_res = current_res->next;
 358                }
 359        }  /* End of out_of_order loop */
 360
 361        return(0);
 362}
 363
 364
 365/*
 366 * do_pre_bridge_resource_split
 367 *
 368 *      Returns zero or one node of resources that aren't in use
 369 *
 370 */
 371static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
 372{
 373        struct pci_resource *prevnode = NULL;
 374        struct pci_resource *node;
 375        struct pci_resource *split_node;
 376        u32 rc;
 377        u32 temp_dword;
 378        dbg("do_pre_bridge_resource_split\n");
 379
 380        if (!(*head) || !(*orig_head))
 381                return(NULL);
 382
 383        rc = shpchp_resource_sort_and_combine(head);
 384
 385        if (rc)
 386                return(NULL);
 387
 388        if ((*head)->base != (*orig_head)->base)
 389                return(NULL);
 390
 391        if ((*head)->length == (*orig_head)->length)
 392                return(NULL);
 393
 394
 395        /* If we got here, there the bridge requires some of the resource, but
 396         *  we may be able to split some off of the front
 397         */     
 398        node = *head;
 399
 400        if (node->length & (alignment -1)) {
 401                /* This one isn't an aligned length, so we'll make a new entry
 402                 * and split it up.
 403                 */
 404                split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 405
 406                if (!split_node)
 407                        return(NULL);
 408
 409                temp_dword = (node->length | (alignment-1)) + 1 - alignment;
 410
 411                split_node->base = node->base;
 412                split_node->length = temp_dword;
 413
 414                node->length -= temp_dword;
 415                node->base += split_node->length;
 416
 417                /* Put it in the list */
 418                *head = split_node;
 419                split_node->next = node;
 420        }
 421
 422        if (node->length < alignment) {
 423                return(NULL);
 424        }
 425
 426        /* Now unlink it */
 427        if (*head == node) {
 428                *head = node->next;
 429                node->next = NULL;
 430        } else {
 431                prevnode = *head;
 432                while (prevnode->next != node)
 433                        prevnode = prevnode->next;
 434
 435                prevnode->next = node->next;
 436                node->next = NULL;
 437        }
 438
 439        return(node);
 440}
 441
 442
 443/*
 444 * do_bridge_resource_split
 445 *
 446 *      Returns zero or one node of resources that aren't in use
 447 *
 448 */
 449static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
 450{
 451        struct pci_resource *prevnode = NULL;
 452        struct pci_resource *node;
 453        u32 rc;
 454        u32 temp_dword;
 455
 456        if (!(*head))
 457                return(NULL);
 458
 459        rc = shpchp_resource_sort_and_combine(head);
 460
 461        if (rc)
 462                return(NULL);
 463
 464        node = *head;
 465
 466        while (node->next) {
 467                prevnode = node;
 468                node = node->next;
 469                kfree(prevnode);
 470        }
 471
 472        if (node->length < alignment) {
 473                kfree(node);
 474                return(NULL);
 475        }
 476
 477        if (node->base & (alignment - 1)) {
 478                /* Short circuit if adjusted size is too small */
 479                temp_dword = (node->base | (alignment-1)) + 1;
 480                if ((node->length - (temp_dword - node->base)) < alignment) {
 481                        kfree(node);
 482                        return(NULL);
 483                }
 484
 485                node->length -= (temp_dword - node->base);
 486                node->base = temp_dword;
 487        }
 488
 489        if (node->length & (alignment - 1)) {
 490                /* There's stuff in use after this node */
 491                kfree(node);
 492                return(NULL);
 493        }
 494
 495        return(node);
 496}
 497
 498
 499/*
 500 * get_io_resource
 501 *
 502 * this function sorts the resource list by size and then
 503 * returns the first node of "size" length that is not in the
 504 * ISA aliasing window.  If it finds a node larger than "size"
 505 * it will split it up.
 506 *
 507 * size must be a power of two.
 508 */
 509static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
 510{
 511        struct pci_resource *prevnode;
 512        struct pci_resource *node;
 513        struct pci_resource *split_node = NULL;
 514        u32 temp_dword;
 515
 516        if (!(*head))
 517                return(NULL);
 518
 519        if ( shpchp_resource_sort_and_combine(head) )
 520                return(NULL);
 521
 522        if ( sort_by_size(head) )
 523                return(NULL);
 524
 525        for (node = *head; node; node = node->next) {
 526                if (node->length < size)
 527                        continue;
 528
 529                if (node->base & (size - 1)) {
 530                        /* This one isn't base aligned properly
 531                           so we'll make a new entry and split it up */
 532                        temp_dword = (node->base | (size-1)) + 1;
 533
 534                        /*/ Short circuit if adjusted size is too small */
 535                        if ((node->length - (temp_dword - node->base)) < size)
 536                                continue;
 537
 538                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 539
 540                        if (!split_node)
 541                                return(NULL);
 542
 543                        split_node->base = node->base;
 544                        split_node->length = temp_dword - node->base;
 545                        node->base = temp_dword;
 546                        node->length -= split_node->length;
 547
 548                        /* Put it in the list */
 549                        split_node->next = node->next;
 550                        node->next = split_node;
 551                } /* End of non-aligned base */
 552
 553                /* Don't need to check if too small since we already did */
 554                if (node->length > size) {
 555                        /* This one is longer than we need
 556                           so we'll make a new entry and split it up */
 557                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 558
 559                        if (!split_node)
 560                                return(NULL);
 561
 562                        split_node->base = node->base + size;
 563                        split_node->length = node->length - size;
 564                        node->length = size;
 565
 566                        /* Put it in the list */
 567                        split_node->next = node->next;
 568                        node->next = split_node;
 569                }  /* End of too big on top end */
 570
 571                /* For IO make sure it's not in the ISA aliasing space */
 572                if (node->base & 0x300L)
 573                        continue;
 574
 575                /* If we got here, then it is the right size 
 576                   Now take it out of the list */
 577                if (*head == node) {
 578                        *head = node->next;
 579                } else {
 580                        prevnode = *head;
 581                        while (prevnode->next != node)
 582                                prevnode = prevnode->next;
 583
 584                        prevnode->next = node->next;
 585                }
 586                node->next = NULL;
 587                /* Stop looping */
 588                break;
 589        }
 590
 591        return(node);
 592}
 593
 594
 595/*
 596 * get_max_resource
 597 *
 598 * Gets the largest node that is at least "size" big from the
 599 * list pointed to by head.  It aligns the node on top and bottom
 600 * to "size" alignment before returning it.
 601 * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
 602 *  This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
 603 */
 604static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
 605{
 606        struct pci_resource *max;
 607        struct pci_resource *temp;
 608        struct pci_resource *split_node;
 609        u32 temp_dword;
 610        u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
 611        int i;
 612
 613        if (!(*head))
 614                return(NULL);
 615
 616        if (shpchp_resource_sort_and_combine(head))
 617                return(NULL);
 618
 619        if (sort_by_max_size(head))
 620                return(NULL);
 621
 622        for (max = *head;max; max = max->next) {
 623
 624                /* If not big enough we could probably just bail, 
 625                   instead we'll continue to the next. */
 626                if (max->length < size)
 627                        continue;
 628
 629                if (max->base & (size - 1)) {
 630                        /* this one isn't base aligned properly
 631                           so we'll make a new entry and split it up */
 632                        temp_dword = (max->base | (size-1)) + 1;
 633
 634                        /* Short circuit if adjusted size is too small */
 635                        if ((max->length - (temp_dword - max->base)) < size)
 636                                continue;
 637
 638                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 639
 640                        if (!split_node)
 641                                return(NULL);
 642
 643                        split_node->base = max->base;
 644                        split_node->length = temp_dword - max->base;
 645                        max->base = temp_dword;
 646                        max->length -= split_node->length;
 647
 648                        /* Put it next in the list */
 649                        split_node->next = max->next;
 650                        max->next = split_node;
 651                }
 652
 653                if ((max->base + max->length) & (size - 1)) {
 654                        /* this one isn't end aligned properly at the top
 655                           so we'll make a new entry and split it up */
 656                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 657
 658                        if (!split_node)
 659                                return(NULL);
 660                        temp_dword = ((max->base + max->length) & ~(size - 1));
 661                        split_node->base = temp_dword;
 662                        split_node->length = max->length + max->base
 663                                             - split_node->base;
 664                        max->length -= split_node->length;
 665
 666                        /* Put it in the list */
 667                        split_node->next = max->next;
 668                        max->next = split_node;
 669                }
 670
 671                /* Make sure it didn't shrink too much when we aligned it */
 672                if (max->length < size)
 673                        continue;
 674
 675                for ( i = 0; max_size[i] > size; i++) {
 676                        if (max->length > max_size[i]) {
 677                                split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), 
 678                                        GFP_KERNEL);
 679                                if (!split_node)
 680                                        break;  /* return (NULL); */
 681                                split_node->base = max->base + max_size[i];
 682                                split_node->length = max->length - max_size[i];
 683                                max->length = max_size[i];
 684                                /* Put it next in the list */
 685                                split_node->next = max->next;
 686                                max->next = split_node;
 687                                break;
 688                        }
 689                }
 690
 691                /* Now take it out of the list */
 692                temp = (struct pci_resource*) *head;
 693                if (temp == max) {
 694                        *head = max->next;
 695                } else {
 696                        while (temp && temp->next != max) {
 697                                temp = temp->next;
 698                        }
 699
 700                        temp->next = max->next;
 701                }
 702
 703                max->next = NULL;
 704                return(max);
 705        }
 706
 707        /* If we get here, we couldn't find one */
 708        return(NULL);
 709}
 710
 711
 712/*
 713 * get_resource
 714 *
 715 * this function sorts the resource list by size and then
 716 * returns the first node of "size" length.  If it finds a node
 717 * larger than "size" it will split it up.
 718 *
 719 * size must be a power of two.
 720 */
 721static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
 722{
 723        struct pci_resource *prevnode;
 724        struct pci_resource *node;
 725        struct pci_resource *split_node;
 726        u32 temp_dword;
 727
 728        if (!(*head))
 729                return(NULL);
 730
 731        if ( shpchp_resource_sort_and_combine(head) )
 732                return(NULL);
 733
 734        if ( sort_by_size(head) )
 735                return(NULL);
 736
 737        for (node = *head; node; node = node->next) {
 738                dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
 739                    __FUNCTION__, size, node, node->base, node->length);
 740                if (node->length < size)
 741                        continue;
 742
 743                if (node->base & (size - 1)) {
 744                        dbg("%s: not aligned\n", __FUNCTION__);
 745                        /* This one isn't base aligned properly
 746                           so we'll make a new entry and split it up */
 747                        temp_dword = (node->base | (size-1)) + 1;
 748
 749                        /* Short circuit if adjusted size is too small */
 750                        if ((node->length - (temp_dword - node->base)) < size)
 751                                continue;
 752
 753                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 754
 755                        if (!split_node)
 756                                return(NULL);
 757
 758                        split_node->base = node->base;
 759                        split_node->length = temp_dword - node->base;
 760                        node->base = temp_dword;
 761                        node->length -= split_node->length;
 762
 763                        /* Put it in the list */
 764                        split_node->next = node->next;
 765                        node->next = split_node;
 766                } /* End of non-aligned base */
 767
 768                /* Don't need to check if too small since we already did */
 769                if (node->length > size) {
 770                        dbg("%s: too big\n", __FUNCTION__);
 771                        /* This one is longer than we need
 772                           so we'll make a new entry and split it up */
 773                        split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
 774
 775                        if (!split_node)
 776                                return(NULL);
 777
 778                        split_node->base = node->base + size;
 779                        split_node->length = node->length - size;
 780                        node->length = size;
 781
 782                        /* Put it in the list */
 783                        split_node->next = node->next;
 784                        node->next = split_node;
 785                }  /* End of too big on top end */
 786
 787                dbg("%s: got one!!!\n", __FUNCTION__);
 788                /* If we got here, then it is the right size
 789                   Now take it out of the list */
 790                if (*head == node) {
 791                        *head = node->next;
 792                } else {
 793                        prevnode = *head;
 794                        while (prevnode->next != node)
 795                                prevnode = prevnode->next;
 796
 797                        prevnode->next = node->next;
 798                }
 799                node->next = NULL;
 800                /* Stop looping */
 801                break;
 802        }
 803        return(node);
 804}
 805
 806
 807/*
 808 * shpchp_resource_sort_and_combine
 809 *
 810 * Sorts all of the nodes in the list in ascending order by
 811 * their base addresses.  Also does garbage collection by
 812 * combining adjacent nodes.
 813 *
 814 * returns 0 if success
 815 */
 816int shpchp_resource_sort_and_combine(struct pci_resource **head)
 817{
 818        struct pci_resource *node1;
 819        struct pci_resource *node2;
 820        int out_of_order = 1;
 821
 822        dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
 823
 824        if (!(*head))
 825                return(1);
 826
 827        dbg("*head->next = %p\n",(*head)->next);
 828
 829        if (!(*head)->next)
 830                return(0);      /* only one item on the list, already sorted! */
 831
 832        dbg("*head->base = 0x%x\n",(*head)->base);
 833        dbg("*head->next->base = 0x%x\n",(*head)->next->base);
 834        while (out_of_order) {
 835                out_of_order = 0;
 836
 837                /* Special case for swapping list head */
 838                if (((*head)->next) &&
 839                    ((*head)->base > (*head)->next->base)) {
 840                        node1 = *head;
 841                        (*head) = (*head)->next;
 842                        node1->next = (*head)->next;
 843                        (*head)->next = node1;
 844                        out_of_order++;
 845                }
 846
 847                node1 = (*head);
 848
 849                while (node1->next && node1->next->next) {
 850                        if (node1->next->base > node1->next->next->base) {
 851                                out_of_order++;
 852                                node2 = node1->next;
 853                                node1->next = node1->next->next;
 854                                node1 = node1->next;
 855                                node2->next = node1->next;
 856                                node1->next = node2;
 857                        } else
 858                                node1 = node1->next;
 859                }
 860        }  /* End of out_of_order loop */
 861
 862        node1 = *head;
 863
 864        while (node1 && node1->next) {
 865                if ((node1->base + node1->length) == node1->next->base) {
 866                        /* Combine */
 867                        dbg("8..\n");
 868                        node1->length += node1->next->length;
 869                        node2 = node1->next;
 870                        node1->next = node1->next->next;
 871                        kfree(node2);
 872                } else
 873                        node1 = node1->next;
 874        }
 875
 876        return(0);
 877}
 878
 879
 880/**
 881 * shpchp_slot_create - Creates a node and adds it to the proper bus.
 882 * @busnumber - bus where new node is to be located
 883 *
 884 * Returns pointer to the new node or NULL if unsuccessful
 885 */
 886struct pci_func *shpchp_slot_create(u8 busnumber)
 887{
 888        struct pci_func *new_slot;
 889        struct pci_func *next;
 890
 891        new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL);
 892
 893        if (new_slot == NULL) {
 894                return(new_slot);
 895        }
 896
 897        memset(new_slot, 0, sizeof(struct pci_func));
 898
 899        new_slot->next = NULL;
 900        new_slot->configured = 1;
 901
 902        if (shpchp_slot_list[busnumber] == NULL) {
 903                shpchp_slot_list[busnumber] = new_slot;
 904        } else {
 905                next = shpchp_slot_list[busnumber];
 906                while (next->next != NULL)
 907                        next = next->next;
 908                next->next = new_slot;
 909        }
 910        return(new_slot);
 911}
 912
 913
 914/*
 915 * slot_remove - Removes a node from the linked list of slots.
 916 * @old_slot: slot to remove
 917 *
 918 * Returns 0 if successful, !0 otherwise.
 919 */
 920static int slot_remove(struct pci_func * old_slot)
 921{
 922        struct pci_func *next;
 923
 924        if (old_slot == NULL)
 925                return(1);
 926
 927        next = shpchp_slot_list[old_slot->bus];
 928
 929        if (next == NULL) {
 930                return(1);
 931        }
 932
 933        if (next == old_slot) {
 934                shpchp_slot_list[old_slot->bus] = old_slot->next;
 935                shpchp_destroy_board_resources(old_slot);
 936                kfree(old_slot);
 937                return(0);
 938        }
 939
 940        while ((next->next != old_slot) && (next->next != NULL)) {
 941                next = next->next;
 942        }
 943
 944        if (next->next == old_slot) {
 945                next->next = old_slot->next;
 946                shpchp_destroy_board_resources(old_slot);
 947                kfree(old_slot);
 948                return(0);
 949        } else
 950                return(2);
 951}
 952
 953
 954/**
 955 * bridge_slot_remove - Removes a node from the linked list of slots.
 956 * @bridge: bridge to remove
 957 *
 958 * Returns 0 if successful, !0 otherwise.
 959 */
 960static int bridge_slot_remove(struct pci_func *bridge)
 961{
 962        u8 subordinateBus, secondaryBus;
 963        u8 tempBus;
 964        struct pci_func *next;
 965
 966        if (bridge == NULL)
 967                return(1);
 968
 969        secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
 970        subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
 971
 972        for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
 973                next = shpchp_slot_list[tempBus];
 974
 975                while (!slot_remove(next)) {
 976                        next = shpchp_slot_list[tempBus];
 977                }
 978        }
 979
 980        next = shpchp_slot_list[bridge->bus];
 981
 982        if (next == NULL) {
 983                return(1);
 984        }
 985
 986        if (next == bridge) {
 987                shpchp_slot_list[bridge->bus] = bridge->next;
 988                kfree(bridge);
 989                return(0);
 990        }
 991
 992        while ((next->next != bridge) && (next->next != NULL)) {
 993                next = next->next;
 994        }
 995
 996        if (next->next == bridge) {
 997                next->next = bridge->next;
 998                kfree(bridge);
 999                return(0);
1000        } else
1001                return(2);
1002}
1003
1004
1005/**
1006 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1007 * @bus: bus to find
1008 * @device: device to find
1009 * @index: is 0 for first function found, 1 for the second...
1010 *
1011 * Returns pointer to the node if successful, %NULL otherwise.
1012 */
1013struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
1014{
1015        int found = -1;
1016        struct pci_func *func;
1017
1018        func = shpchp_slot_list[bus];
1019
1020        if ((func == NULL) || ((func->device == device) && (index == 0)))
1021                return(func);
1022
1023        if (func->device == device)
1024                found++;
1025
1026        while (func->next != NULL) {
1027                func = func->next;
1028
1029                if (func->device == device)
1030                        found++;
1031
1032                if (found == index)
1033                        return(func);
1034        }
1035
1036        return(NULL);
1037}
1038
1039static int is_bridge(struct pci_func * func)
1040{
1041        /* Check the header type */
1042        if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01) {
1043                dbg("%s: Is a bridge\n", __FUNCTION__);
1044                return 1;
1045        } else {
1046                dbg("%s: Not a bridge\n", __FUNCTION__);
1047                return 0;
1048        }
1049}
1050
1051
1052/* the following routines constitute the bulk of the 
1053   hotplug controller logic
1054 */
1055
1056
1057/**
1058 * board_added - Called after a board has been added to the system.
1059 *
1060 * Turns power on for the board
1061 * Configures board
1062 *
1063 */
1064static u32 board_added(struct pci_func * func, struct controller * ctrl)
1065{
1066        u8 hp_slot, slot;
1067        u8 slots_not_empty = 0;
1068        int index;
1069        u32 temp_register = 0xFFFFFFFF;
1070        u32 retval, rc = 0;
1071        struct pci_func *new_func = NULL;
1072        struct pci_func *t_func = NULL;
1073        struct slot *p_slot, *pslot;
1074        struct resource_lists res_lists;
1075        enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
1076        u8 pi, mode;
1077
1078        p_slot = shpchp_find_slot(ctrl, func->device);
1079        hp_slot = func->device - ctrl->slot_device_offset;
1080
1081        dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, 
1082                func->device, ctrl->slot_device_offset, hp_slot);
1083
1084        /* Wait for exclusive access to hardware */
1085        down(&ctrl->crit_sect);
1086        
1087        /* Power on slot without connecting to bus */
1088        rc = p_slot->hpc_ops->power_on_slot(p_slot);
1089        if (rc) {
1090                err("%s: Failed to power on slot\n", __FUNCTION__);
1091                /* Done with exclusive hardware access */
1092                up(&ctrl->crit_sect);
1093                return -1;
1094        }
1095                        
1096        /* Wait for the command to complete */
1097        wait_for_ctrl_irq(ctrl);
1098        
1099        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1100        if (rc) {
1101                err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
1102                /* Done with exclusive hardware access */
1103                up(&ctrl->crit_sect);
1104                return -1;
1105        }
1106
1107        rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
1108        /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
1109        /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC,  0xa = PCI-X 133 Mhz 266, */
1110        /* 0xd = PCI-X 133 Mhz 533 */
1111        /* This encoding is different from the one used in cur_bus_speed & */
1112        /* max_bus_speed */
1113
1114        if (rc  || adapter_speed == PCI_SPEED_UNKNOWN) {
1115                err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
1116                /* Done with exclusive hardware access */
1117                up(&ctrl->crit_sect);
1118                return WRONG_BUS_FREQUENCY;
1119        }
1120
1121        rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
1122        if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
1123                err("%s: Can't get bus operation speed\n", __FUNCTION__);
1124                /* Done with exclusive hardware access */
1125                up(&ctrl->crit_sect);
1126                return WRONG_BUS_FREQUENCY;
1127        }
1128
1129        rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1130        if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1131                err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1132                max_bus_speed = bus_speed;
1133        }
1134
1135        /* Done with exclusive hardware access */
1136        up(&ctrl->crit_sect);
1137
1138        rc  = p_slot->hpc_ops->get_prog_int(p_slot, &pi);
1139        if (rc) {
1140                err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
1141                pi = 1;
1142        }
1143
1144        if (pi == 2) {
1145                for ( slot = 0; slot < ctrl->num_slots; slot++) {
1146                        if (slot != hp_slot) {
1147                                pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1148                                t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1149                                slots_not_empty |= t_func->is_a_board;
1150                        }
1151                }
1152
1153                switch (adapter_speed) {
1154                case PCI_SPEED_133MHz_PCIX_533: 
1155                case PCI_SPEED_133MHz_PCIX_266:
1156                        if ((( bus_speed < 0xa ) || (bus_speed < 0xd)) && (max_bus_speed > bus_speed) &&
1157                                ((max_bus_speed <= 0xa) || (max_bus_speed <= 0xd)) && (!slots_not_empty)) {
1158                        
1159                                /* Wait for exclusive access to hardware */
1160                                down(&ctrl->crit_sect);
1161
1162                                rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1163                                if (rc) {
1164                                        err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1165                                        /* Done with exclusive hardware access */
1166                                        up(&ctrl->crit_sect);                           
1167                                        return WRONG_BUS_FREQUENCY;
1168                                }
1169                                
1170                                /* Wait for the command to complete */
1171                                wait_for_ctrl_irq (ctrl);
1172                
1173                                rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1174                                if (rc) {
1175                                        err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1176                                                          __FUNCTION__);
1177                                        err("%s: Error code (%d)\n", __FUNCTION__, rc);
1178                                        /* Done with exclusive hardware access */
1179                                        up(&ctrl->crit_sect);                           
1180                                        return WRONG_BUS_FREQUENCY;
1181                                }
1182                                /* Done with exclusive hardware access */
1183                                up(&ctrl->crit_sect);
1184                        }
1185                        break;
1186                case PCI_SPEED_133MHz_PCIX_ECC:
1187                case PCI_SPEED_133MHz_PCIX:
1188
1189                        rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1190
1191                        if (rc) {
1192                                err("%s: PI is 1 \n", __FUNCTION__);
1193                                return WRONG_BUS_FREQUENCY;
1194                        }
1195
1196                        if (mode) { /* Bus - Mode 1 ECC */
1197
1198                                if (bus_speed > 0x7)  {
1199                                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1200                                        return WRONG_BUS_FREQUENCY;
1201                                }
1202
1203                                if ((bus_speed < 0x7) && (max_bus_speed <= 0x7) &&
1204                                        (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1205
1206                                        /* Wait for exclusive access to hardware */
1207                                        down(&ctrl->crit_sect);
1208
1209                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1210                                        if (rc) {
1211                                                err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1212                                                /* Done with exclusive hardware access */
1213                                                up(&ctrl->crit_sect);                           
1214                                                return WRONG_BUS_FREQUENCY;
1215                                        }
1216                                
1217                                        /* Wait for the command to complete */
1218                                        wait_for_ctrl_irq (ctrl);
1219                
1220                                        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1221                                        if (rc) {
1222                                                err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1223                                                          __FUNCTION__);
1224                                                err("%s: Error code (%d)\n", __FUNCTION__, rc);
1225                                                /* Done with exclusive hardware access */
1226                                                up(&ctrl->crit_sect);                           
1227                                                return WRONG_BUS_FREQUENCY;
1228                                        }
1229                                        /* Done with exclusive hardware access */
1230                                        up(&ctrl->crit_sect);
1231                                }
1232                        } else {
1233                                if (bus_speed > 0x4)  {
1234                                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1235                                        return WRONG_BUS_FREQUENCY;
1236                                }
1237
1238                                if ((bus_speed < 0x4) && (max_bus_speed <= 0x4) &&
1239                                        (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1240
1241                                        /* Wait for exclusive access to hardware */
1242                                        down(&ctrl->crit_sect);
1243
1244                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1245                                        if (rc) {
1246                                                err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1247                                                /* Done with exclusive hardware access */
1248                                                up(&ctrl->crit_sect);                           
1249                                                return WRONG_BUS_FREQUENCY;
1250                                        }
1251                                
1252                                        /* Wait for the command to complete */
1253                                        wait_for_ctrl_irq (ctrl);
1254                
1255                                        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1256                                        if (rc) {
1257                                                err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1258                                                          __FUNCTION__);
1259                                                err("%s: Error code (%d)\n", __FUNCTION__, rc);
1260                                                /* Done with exclusive hardware access */
1261                                                up(&ctrl->crit_sect);                           
1262                                                return WRONG_BUS_FREQUENCY;
1263                                        }
1264                                        /* Done with exclusive hardware access */
1265                                        up(&ctrl->crit_sect);
1266                                }
1267                        }
1268                        break;
1269                case PCI_SPEED_66MHz_PCIX_ECC:
1270                case PCI_SPEED_66MHz_PCIX:
1271
1272                        rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1273
1274                        if (rc) {
1275                                err("%s: PI is 1 \n", __FUNCTION__);
1276                                return WRONG_BUS_FREQUENCY;
1277                        }
1278
1279                        if (mode) { /* Bus - Mode 1 ECC */
1280
1281                                if (bus_speed > 0x5)  {
1282                                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1283                                        return WRONG_BUS_FREQUENCY;
1284                                }
1285
1286                                if ((bus_speed < 0x5) && (max_bus_speed <= 0x5) &&
1287                                        (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1288
1289                                        /* Wait for exclusive access to hardware */
1290                                        down(&ctrl->crit_sect);
1291
1292                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1293                                        if (rc) {
1294                                                err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1295                                                /* Done with exclusive hardware access */
1296                                                up(&ctrl->crit_sect);                           
1297                                                return WRONG_BUS_FREQUENCY;
1298                                        }
1299                                
1300                                        /* Wait for the command to complete */
1301                                        wait_for_ctrl_irq (ctrl);
1302                
1303                                        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1304                                        if (rc) {
1305                                                err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1306                                                          __FUNCTION__);
1307                                                err("%s: Error code (%d)\n", __FUNCTION__, rc);
1308                                                /* Done with exclusive hardware access */
1309                                                up(&ctrl->crit_sect);                           
1310                                                return WRONG_BUS_FREQUENCY;
1311                                        }
1312                                        /* Done with exclusive hardware access */
1313                                        up(&ctrl->crit_sect);
1314                                }
1315                        } else {
1316                                if (bus_speed > 0x2) {
1317                                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1318                                        return WRONG_BUS_FREQUENCY;
1319                                }
1320
1321                                if ((bus_speed < 0x2) && (max_bus_speed <= 0x2) &&
1322                                        (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1323
1324                                        /* Wait for exclusive access to hardware */
1325                                        down(&ctrl->crit_sect);
1326
1327                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1328                                        if (rc) {
1329                                                err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1330                                                /* Done with exclusive hardware access */
1331                                                up(&ctrl->crit_sect);                           
1332                                                return WRONG_BUS_FREQUENCY;
1333                                        }
1334                                
1335                                        /* Wait for the command to complete */
1336                                        wait_for_ctrl_irq (ctrl);
1337                
1338                                        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1339                                        if (rc) {
1340                                                err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1341                                                          __FUNCTION__);
1342                                                err("%s: Error code (%d)\n", __FUNCTION__, rc);
1343                                                /* Done with exclusive hardware access */
1344                                                up(&ctrl->crit_sect);                           
1345                                                return WRONG_BUS_FREQUENCY;
1346                                        }
1347                                        /* Done with exclusive hardware access */
1348                                        up(&ctrl->crit_sect);
1349                                }
1350                        }
1351                        break;
1352                case PCI_SPEED_66MHz:
1353                        if (bus_speed > 0x1) {
1354                                err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1355                                return WRONG_BUS_FREQUENCY;
1356                        }
1357                        if (bus_speed == 0x1)
1358                                ;
1359                        if ((bus_speed == 0x0) && ( max_bus_speed == 0x1))  {
1360                                /* Wait for exclusive access to hardware */
1361                                down(&ctrl->crit_sect);
1362
1363                                rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1364                                if (rc) {
1365                                        err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1366                                        /* Done with exclusive hardware access */
1367                                        up(&ctrl->crit_sect);                           
1368                                        return WRONG_BUS_FREQUENCY;
1369                                }
1370                                
1371                                /* Wait for the command to complete */
1372                                wait_for_ctrl_irq (ctrl);
1373                
1374                                rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1375                                if (rc) {
1376                                        err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1377                                                          __FUNCTION__);
1378                                        err("%s: Error code (%d)\n", __FUNCTION__, rc);
1379                                        /* Done with exclusive hardware access */
1380                                        up(&ctrl->crit_sect);                           
1381                                        return WRONG_BUS_FREQUENCY;
1382                                }
1383                                /* Done with exclusive hardware access */
1384                                up(&ctrl->crit_sect);
1385                        }
1386                        break;  
1387                case PCI_SPEED_33MHz:
1388                        if (bus_speed > 0x0) {
1389                                err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1390                                return WRONG_BUS_FREQUENCY;
1391                        }
1392                        break;
1393                default:
1394                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1395                        return WRONG_BUS_FREQUENCY;
1396                }
1397        } else {
1398                /* if adpater_speed == bus_speed, nothing to do here */
1399                if (adapter_speed != bus_speed) {
1400                        for ( slot = 0; slot < ctrl->num_slots; slot++) {
1401                                if (slot != hp_slot) {
1402                                        pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1403                                        t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1404                                        slots_not_empty |= t_func->is_a_board;
1405                                }
1406                        }
1407
1408                        if (slots_not_empty != 0) { /* Other slots on the same bus are occupied */
1409                                if ( adapter_speed < bus_speed ) {
1410                                        err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1411                                        return WRONG_BUS_FREQUENCY;
1412                                }
1413                                /* Do nothing if adapter_speed >= bus_speed */
1414                        }
1415                }
1416                        
1417                if ((adapter_speed != bus_speed) && (slots_not_empty == 0))  {
1418                        /* Other slots on the same bus are empty */
1419                        
1420                        rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1421                        if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1422                                err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1423                                max_bus_speed = bus_speed;
1424                        }
1425
1426                        if (max_bus_speed == bus_speed) {
1427                                /* if adapter_speed >= bus_speed, do nothing */
1428                                if (adapter_speed < bus_speed) {
1429                                /* 
1430                                 * Try to lower bus speed to accommodate the adapter if other slots 
1431                                 * on the same controller are empty
1432                                 */
1433                                        
1434                                        /* Wait for exclusive access to hardware */
1435                                        down(&ctrl->crit_sect);
1436
1437                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1438                                        if (rc) {
1439                                                err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1440                                                up(&ctrl->crit_sect);
1441                                                return WRONG_BUS_FREQUENCY;
1442                                        }
1443                                
1444                                        /* Wait for the command to complete */
1445                                        wait_for_ctrl_irq (ctrl);
1446                
1447                                        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1448                                        if (rc) {
1449                                                err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1450                                                                  __FUNCTION__);
1451                                                err("%s: Error code (%d)\n", __FUNCTION__, rc);
1452                                                up(&ctrl->crit_sect);
1453                                                return WRONG_BUS_FREQUENCY;
1454                                        }
1455                                        /* Done with exclusive hardware access */
1456                                        up(&ctrl->crit_sect);
1457
1458                                } 
1459                        } else {
1460                                /* Wait for exclusive access to hardware */
1461                                down(&ctrl->crit_sect);
1462
1463                                /* max_bus_speed != bus_speed. Note: max_bus_speed should be > than bus_speed */
1464                                if (adapter_speed < max_bus_speed) 
1465                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1466                                else  
1467                                        rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1468                                
1469                                if (rc) {
1470                                        err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1471                                        /* Done with exclusive hardware access */
1472                                        up(&ctrl->crit_sect);
1473                                        return WRONG_BUS_FREQUENCY;
1474                                }
1475                                
1476                                /* Wait for the command to complete */
1477                                wait_for_ctrl_irq (ctrl);
1478                
1479                                rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1480                                if (rc) {
1481                                        err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n", 
1482                                                __FUNCTION__);
1483                                        err("%s: Error code (%d)\n", __FUNCTION__, rc);
1484                                        /* Done with exclusive hardware access */
1485                                        up(&ctrl->crit_sect);
1486                                        return WRONG_BUS_FREQUENCY;
1487                                }
1488                                /* Done with exclusive hardware access */
1489                                up(&ctrl->crit_sect);
1490
1491                        }
1492                }
1493        }
1494
1495        /* Wait for exclusive access to hardware */
1496        down(&ctrl->crit_sect);
1497
1498        /* Turn on board, blink green LED, turn off Amber LED */
1499        rc = p_slot->hpc_ops->slot_enable(p_slot);
1500        
1501        if (rc) {
1502                err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1503                /* Done with exclusive hardware access */
1504                up(&ctrl->crit_sect);
1505                return rc;
1506        }
1507        /* Wait for the command to complete */
1508        wait_for_ctrl_irq (ctrl);
1509
1510        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1511        if (rc) {
1512                err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1513                /* Done with exclusive hardware access */
1514                up(&ctrl->crit_sect);
1515                return rc;  
1516        }
1517
1518        /* Done with exclusive hardware access */
1519        up(&ctrl->crit_sect);
1520
1521        /* Wait for ~1 second */
1522        dbg("%s: before long_delay\n", __FUNCTION__);
1523        wait_for_ctrl_irq (ctrl);
1524        dbg("%s: afterlong_delay\n", __FUNCTION__);
1525
1526        dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1527        /* Check for a power fault */
1528        if (func->status == 0xFF) {
1529                /* power fault occurred, but it was benign */
1530                temp_register = 0xFFFFFFFF;
1531                dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1532                rc = POWER_FAILURE;
1533                func->status = 0;
1534        } else {
1535                /* Get vendor/device ID u32 */
1536                rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1537                dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1538                dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1539
1540                if (rc != 0) {
1541                        /* Something's wrong here */
1542                        temp_register = 0xFFFFFFFF;
1543                        dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1544                }
1545                /* Preset return code.  It will be changed later if things go okay. */
1546                rc = NO_ADAPTER_PRESENT;
1547        }
1548
1549        /* All F's is an empty slot or an invalid board */
1550        if (temp_register != 0xFFFFFFFF) {        /* Check for a board in the slot */
1551                res_lists.io_head = ctrl->io_head;
1552                res_lists.mem_head = ctrl->mem_head;
1553                res_lists.p_mem_head = ctrl->p_mem_head;
1554                res_lists.bus_head = ctrl->bus_head;
1555                res_lists.irqs = NULL;
1556
1557                rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1558                dbg("%s: back from configure_new_device\n", __FUNCTION__);
1559
1560                ctrl->io_head = res_lists.io_head;
1561                ctrl->mem_head = res_lists.mem_head;
1562                ctrl->p_mem_head = res_lists.p_mem_head;
1563                ctrl->bus_head = res_lists.bus_head;
1564
1565                shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1566                shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1567                shpchp_resource_sort_and_combine(&(ctrl->io_head));
1568                shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1569
1570                if (rc) {
1571                        /* Wait for exclusive access to hardware */
1572                        down(&ctrl->crit_sect);
1573
1574                        /* turn off slot, turn on Amber LED, turn off Green LED */
1575                        retval = p_slot->hpc_ops->slot_disable(p_slot);
1576                        if (retval) {
1577                                err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1578                                /* Done with exclusive hardware access */
1579                                up(&ctrl->crit_sect);
1580                                return retval;
1581                        }
1582                        /* Wait for the command to complete */
1583                        wait_for_ctrl_irq(ctrl);
1584                        
1585                        retval = p_slot->hpc_ops->check_cmd_status(ctrl);
1586                        if (retval) {
1587                                err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1588                                /* Done with exclusive hardware access */
1589                                up(&ctrl->crit_sect);
1590                                return retval;  
1591                        }
1592
1593                        /* Done with exclusive hardware access */
1594                        up(&ctrl->crit_sect);
1595
1596                        return(rc);
1597                }
1598                shpchp_save_slot_config(ctrl, func);
1599
1600                func->status = 0;
1601                func->switch_save = 0x10;
1602                func->is_a_board = 0x01;
1603                func->pwr_save = 1;
1604        
1605                /* Next, we will instantiate the linux pci_dev structures 
1606                 * (with appropriate driver notification, if already present) 
1607                 */
1608                index = 0;
1609                do {
1610                        new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
1611                        if (new_func && !new_func->pci_dev) {
1612                                dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
1613                                shpchp_configure_device(ctrl, new_func);
1614                        }
1615                } while (new_func);
1616
1617                /* Wait for exclusive access to hardware */
1618                down(&ctrl->crit_sect);
1619
1620                p_slot->hpc_ops->green_led_on(p_slot);
1621
1622                /* Wait for the command to complete */
1623                wait_for_ctrl_irq(ctrl); 
1624
1625
1626                /* Done with exclusive hardware access */
1627                up(&ctrl->crit_sect);
1628
1629        } else {
1630                /* Wait for exclusive access to hardware */
1631                down(&ctrl->crit_sect);
1632
1633                /* turn off slot, turn on Amber LED, turn off Green LED */
1634                rc = p_slot->hpc_ops->slot_disable(p_slot);
1635                if (rc) {
1636                        err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1637                        /* Done with exclusive hardware access */
1638                        up(&ctrl->crit_sect);
1639                        return rc;
1640                }
1641                /* Wait for the command to complete */
1642                wait_for_ctrl_irq(ctrl); 
1643
1644                rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1645                if (rc) {
1646                        err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1647                        /* Done with exclusive hardware access */
1648                        up(&ctrl->crit_sect);
1649                        return rc;  
1650                }
1651
1652                /* Done with exclusive hardware access */
1653                up(&ctrl->crit_sect);
1654
1655                return(rc);
1656        }
1657        return 0;
1658}
1659
1660
1661/**
1662 * remove_board - Turns off slot and LED's
1663 *
1664 */
1665static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1666{
1667        int index;
1668        u8 skip = 0;
1669        u8 device;
1670        u8 hp_slot;
1671        u32 rc;
1672        struct resource_lists res_lists;
1673        struct pci_func *temp_func;
1674        struct slot *p_slot;
1675
1676        if (func == NULL)
1677                return(1);
1678
1679        if (shpchp_unconfigure_device(func))
1680                return(1);
1681
1682        device = func->device;
1683
1684        hp_slot = func->device - ctrl->slot_device_offset;
1685        p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1686
1687        dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1688
1689        if ((ctrl->add_support) &&
1690                !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1691                /* Here we check to see if we've saved any of the board's
1692                 * resources already.  If so, we'll skip the attempt to
1693                 * determine what's being used.
1694                 */
1695                index = 0;
1696
1697                temp_func = func;
1698
1699                while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
1700                        if (temp_func->bus_head || temp_func->mem_head
1701                            || temp_func->p_mem_head || temp_func->io_head) {
1702                                skip = 1;
1703                                break;
1704                        }
1705                }
1706
1707                if (!skip)
1708                        rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
1709        }
1710        /* Change status to shutdown */
1711        if (func->is_a_board)
1712                func->status = 0x01;
1713        func->configured = 0;
1714
1715        /* Wait for exclusive access to hardware */
1716        down(&ctrl->crit_sect);
1717
1718        /* Turn off slot, turn on Amber LED, turn off Green LED */
1719        rc = p_slot->hpc_ops->slot_disable(p_slot);
1720        if (rc) {
1721                err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1722                /* Done with exclusive hardware access */
1723                up(&ctrl->crit_sect);
1724                return rc;
1725        }
1726        /* Wait for the command to complete */
1727        wait_for_ctrl_irq(ctrl); 
1728
1729        rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1730        if (rc) {
1731                err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1732                /* Done with exclusive hardware access */
1733                up(&ctrl->crit_sect);
1734                return rc;  
1735        }
1736        
1737        rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
1738        if (rc) {
1739                err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
1740                /* Done with exclusive hardware access */
1741                up(&ctrl->crit_sect);
1742                return rc;
1743        }
1744        /* Wait for the command to complete */
1745        wait_for_ctrl_irq(ctrl); 
1746
1747        /* Done with exclusive hardware access */
1748        up(&ctrl->crit_sect);
1749
1750        if (ctrl->add_support) {
1751                while (func) {
1752                        res_lists.io_head = ctrl->io_head;
1753                        res_lists.mem_head = ctrl->mem_head;
1754                        res_lists.p_mem_head = ctrl->p_mem_head;
1755                        res_lists.bus_head = ctrl->bus_head;
1756
1757                        dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", 
1758                                func->bus, func->device, func->function);
1759
1760                        shpchp_return_board_resources(func, &res_lists);
1761
1762                        ctrl->io_head = res_lists.io_head;
1763                        ctrl->mem_head = res_lists.mem_head;
1764                        ctrl->p_mem_head = res_lists.p_mem_head;
1765                        ctrl->bus_head = res_lists.bus_head;
1766
1767                        shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1768                        shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1769                        shpchp_resource_sort_and_combine(&(ctrl->io_head));
1770                        shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1771
1772                        if (is_bridge(func)) {
1773                                dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", 
1774                                        ctrl->seg, func->bus, func->device, func->function);
1775                                bridge_slot_remove(func);
1776                        } else {
1777                                dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", 
1778                                        ctrl->seg, func->bus, func->device, func->function);
1779                                slot_remove(func);
1780                        }
1781
1782                        func = shpchp_slot_find(ctrl->slot_bus, device, 0);
1783                }
1784
1785                /* Setup slot structure with entry for empty slot */
1786                func = shpchp_slot_create(ctrl->slot_bus);
1787
1788                if (func == NULL) {
1789                        return(1);
1790                }
1791
1792                func->bus = ctrl->slot_bus;
1793                func->device = device;
1794                func->function = 0;
1795                func->configured = 0;
1796                func->switch_save = 0x10;
1797                func->pwr_save = 0;
1798                func->is_a_board = 0;
1799        }
1800
1801        return 0;
1802}
1803
1804
1805static void pushbutton_helper_thread (unsigned long data)
1806{
1807        pushbutton_pending = data;
1808
1809        up(&event_semaphore);
1810}
1811
1812
1813/* This is the main worker thread */
1814static int event_thread(void* data)
1815{
1816        struct controller *ctrl;
1817        lock_kernel();
1818        daemonize();
1819
1820        /* New name */
1821        strcpy(current->comm, "shpchpd_event");
1822        
1823        unlock_kernel();
1824
1825        while (1) {
1826                dbg("!!!!event_thread sleeping\n");
1827                down_interruptible (&event_semaphore);
1828                dbg("event_thread woken finished = %d\n", event_finished);
1829                if (event_finished || signal_pending(current))
1830                        break;
1831                /* Do stuff here */
1832                if (pushbutton_pending)
1833                        shpchp_pushbutton_thread(pushbutton_pending);
1834                else
1835                        for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
1836                                interrupt_event_handler(ctrl);
1837        }
1838        dbg("event_thread signals exit\n");
1839        up(&event_exit);
1840        return 0;
1841}
1842
1843int shpchp_event_start_thread (void)
1844{
1845        int pid;
1846
1847        /* Initialize our semaphores */
1848        init_MUTEX_LOCKED(&event_exit);
1849        event_finished=0;
1850
1851        init_MUTEX_LOCKED(&event_semaphore);
1852        pid = kernel_thread(event_thread, 0, 0);
1853
1854        if (pid < 0) {
1855                err ("Can't start up our event thread\n");
1856                return -1;
1857        }
1858        dbg("Our event thread pid = %d\n", pid);
1859        return 0;
1860}
1861
1862
1863void shpchp_event_stop_thread (void)
1864{
1865        event_finished = 1;
1866        dbg("event_thread finish command given\n");
1867        up(&event_semaphore);
1868        dbg("wait for event_thread to exit\n");
1869        down(&event_exit);
1870}
1871
1872
1873static int update_slot_info (struct slot *slot)
1874{
1875        struct hotplug_slot_info *info;
1876        char buffer[SLOT_NAME_SIZE];
1877        int result;
1878
1879        info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
1880        if (!info)
1881                return -ENOMEM;
1882
1883        make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot);
1884
1885        slot->hpc_ops->get_power_status(slot, &(info->power_status));
1886        slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1887        slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1888        slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1889
1890        result = pci_hp_change_slot_info(buffer, info);
1891        kfree (info);
1892        return result;
1893}
1894
1895static void interrupt_event_handler(struct controller *ctrl)
1896{
1897        int loop = 0;
1898        int change = 1;
1899        struct pci_func *func;
1900        u8 hp_slot;
1901        u8 getstatus;
1902        struct slot *p_slot;
1903        
1904        dbg("%s:\n", __FUNCTION__);
1905        while (change) {
1906                change = 0;
1907
1908                for (loop = 0; loop < 10; loop++) {
1909                        if (ctrl->event_queue[loop].event_type != 0) {
1910                                dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop, 
1911                                        ctrl->event_queue[loop].event_type);
1912                                hp_slot = ctrl->event_queue[loop].hp_slot;
1913
1914                                func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1915
1916                                p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1917
1918                                dbg("%s:hp_slot %d, func %p, p_slot %p\n", __FUNCTION__,hp_slot, func, p_slot);
1919
1920                                if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1921                                        dbg("%s: button cancel\n", __FUNCTION__);
1922                                        del_timer(&p_slot->task_event);
1923
1924                                        switch (p_slot->state) {
1925                                        case BLINKINGOFF_STATE:
1926                                                /* Wait for exclusive access to hardware */
1927                                                down(&ctrl->crit_sect);
1928
1929                                                p_slot->hpc_ops->green_led_on(p_slot);
1930                                                /* Wait for the command to complete */
1931                                                wait_for_ctrl_irq(ctrl); 
1932
1933                                                p_slot->hpc_ops->set_attention_status(p_slot, 0);
1934
1935                                                /* Wait for the command to complete */
1936                                                wait_for_ctrl_irq(ctrl); 
1937
1938                                                /* Done with exclusive hardware access */
1939                                                up(&ctrl->crit_sect);
1940                                                break;
1941                                        case BLINKINGON_STATE:
1942                                                /* Wait for exclusive access to hardware */
1943                                                down(&ctrl->crit_sect);
1944
1945                                                p_slot->hpc_ops->green_led_off(p_slot);
1946                                                /* Wait for the command to complete */
1947                                                wait_for_ctrl_irq(ctrl); 
1948
1949                                                p_slot->hpc_ops->set_attention_status(p_slot, 0);
1950                                                /* Wait for the command to complete */
1951                                                wait_for_ctrl_irq(ctrl); 
1952
1953                                                /* Done with exclusive hardware access */
1954                                                up(&ctrl->crit_sect);
1955
1956                                                break;
1957                                        default:
1958                                                warn("Not a valid state\n");
1959                                                return;
1960                                        }
1961                                        info(msg_button_cancel, p_slot->number);
1962                                        p_slot->state = STATIC_STATE;
1963                                }
1964                                /* Button Pressed (No action on 1st press...) */
1965                                else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1966                                        dbg("%s: Button pressed\n", __FUNCTION__);
1967
1968                                        p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1969                                        if (getstatus) {
1970                                                /* slot is on */
1971                                                dbg("%s: slot is on\n", __FUNCTION__);
1972                                                p_slot->state = BLINKINGOFF_STATE;
1973                                                info(msg_button_off, p_slot->number);
1974                                        } else {
1975                                                /* slot is off */
1976                                                dbg("%s: slot is off\n", __FUNCTION__);
1977                                                p_slot->state = BLINKINGON_STATE;
1978                                                info(msg_button_on, p_slot->number);
1979                                        }
1980
1981                                        /* Wait for exclusive access to hardware */
1982                                        down(&ctrl->crit_sect);
1983
1984                                        /* blink green LED and turn off amber */
1985                                        p_slot->hpc_ops->green_led_blink(p_slot);
1986                                        /* Wait for the command to complete */
1987                                        wait_for_ctrl_irq(ctrl); 
1988                                        
1989                                        p_slot->hpc_ops->set_attention_status(p_slot, 0);
1990
1991                                        /* Wait for the command to complete */
1992                                        wait_for_ctrl_irq(ctrl); 
1993
1994                                        /* Done with exclusive hardware access */
1995                                        up(&ctrl->crit_sect);
1996
1997                                        init_timer(&p_slot->task_event);
1998                                        p_slot->task_event.expires = jiffies + 5 * HZ;   /* 5 second delay */
1999                                        p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
2000                                        p_slot->task_event.data = (unsigned long) p_slot;
2001
2002                                        dbg("%s: add_timer p_slot = %p\n", __FUNCTION__, (void *) p_slot);
2003                                        add_timer(&p_slot->task_event);
2004                                }
2005                                /***********POWER FAULT********************/
2006                                else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
2007                                        dbg("%s: power fault\n", __FUNCTION__);
2008                                        /* Wait for exclusive access to hardware */
2009                                        down(&ctrl->crit_sect);
2010
2011                                        p_slot->hpc_ops->set_attention_status(p_slot, 1);
2012                                        
2013                                        /* Wait for the command to complete */
2014                                        wait_for_ctrl_irq(ctrl); 
2015
2016                                        p_slot->hpc_ops->green_led_off(p_slot);
2017
2018                                        /* Wait for the command to complete */
2019                                        wait_for_ctrl_irq(ctrl); 
2020                                        
2021                                        /* Done with exclusive hardware access */
2022                                        up(&ctrl->crit_sect);
2023                                } else {
2024                                        /* refresh notification */
2025                                        if (p_slot)
2026                                                update_slot_info(p_slot);
2027                                }
2028
2029                                ctrl->event_queue[loop].event_type = 0;
2030
2031                                change = 1;
2032                        }
2033                }               /* End of FOR loop */
2034        }
2035
2036        return;
2037}
2038
2039
2040/**
2041 * shpchp_pushbutton_thread
2042 *
2043 * Scheduled procedure to handle blocking stuff for the pushbuttons
2044 * Handles all pending events and exits.
2045 *
2046 */
2047void shpchp_pushbutton_thread (unsigned long slot)
2048{
2049        struct slot *p_slot = (struct slot *) slot;
2050        u8 getstatus;
2051        
2052        pushbutton_pending = 0;
2053
2054        if (!p_slot) {
2055                dbg("%s: Error! slot NULL\n", __FUNCTION__);
2056                return;
2057        }
2058
2059        p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2060        if (getstatus) {
2061                p_slot->state = POWEROFF_STATE;
2062                dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
2063
2064                shpchp_disable_slot(p_slot);
2065                p_slot->state = STATIC_STATE;
2066        } else {
2067                p_slot->state = POWERON_STATE;
2068                dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
2069
2070                if (shpchp_enable_slot(p_slot)) {
2071                        /* Wait for exclusive access to hardware */
2072                        down(&p_slot->ctrl->crit_sect);
2073
2074                        p_slot->hpc_ops->green_led_off(p_slot);
2075
2076                        /* Wait for the command to complete */
2077                        wait_for_ctrl_irq(p_slot->ctrl);
2078
2079                        /* Done with exclusive hardware access */
2080                        up(&p_slot->ctrl->crit_sect);
2081                }
2082                p_slot->state = STATIC_STATE;
2083        }
2084
2085        return;
2086}
2087
2088
2089int shpchp_enable_slot (struct slot *p_slot)
2090{
2091        u8 getstatus = 0;
2092        int rc;
2093        struct pci_func *func;
2094
2095        func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2096        if (!func) {
2097                dbg("%s: Error! slot NULL\n", __FUNCTION__);
2098                return (1);
2099        }
2100
2101        /* Check to see if (latch closed, card present, power off) */
2102        down(&p_slot->ctrl->crit_sect);
2103        rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2104        if (rc || !getstatus) {
2105                info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2106                up(&p_slot->ctrl->crit_sect);
2107                return (1);
2108        }
2109        rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2110        if (rc || getstatus) {
2111                info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2112                up(&p_slot->ctrl->crit_sect);
2113                return (1);
2114        }
2115        rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2116        if (rc || getstatus) {
2117                info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
2118                up(&p_slot->ctrl->crit_sect);
2119                return (1);
2120        }
2121        up(&p_slot->ctrl->crit_sect);
2122
2123        slot_remove(func);
2124
2125        func = shpchp_slot_create(p_slot->bus);
2126        if (func == NULL)
2127                return (1);
2128
2129        func->bus = p_slot->bus;
2130        func->device = p_slot->device;
2131        func->function = 0;
2132        func->configured = 0;
2133        func->is_a_board = 1;
2134
2135        /* We have to save the presence info for these slots */
2136        p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2137        p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
2138        dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
2139        p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2140        func->switch_save = !getstatus? 0x10:0;
2141
2142        rc = board_added(func, p_slot->ctrl);
2143        if (rc) {
2144                if (is_bridge(func))
2145                        bridge_slot_remove(func);
2146                else
2147                        slot_remove(func);
2148
2149                /* Setup slot structure with entry for empty slot */
2150                func = shpchp_slot_create(p_slot->bus);
2151                if (func == NULL)
2152                        return (1);     /* Out of memory */
2153
2154                func->bus = p_slot->bus;
2155                func->device = p_slot->device;
2156                func->function = 0;
2157                func->configured = 0;
2158                func->is_a_board = 1;
2159
2160                /* We have to save the presence info for these slots */
2161                p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2162                p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2163                func->switch_save = !getstatus? 0x10:0;
2164        }
2165
2166        if (p_slot)
2167                update_slot_info(p_slot);
2168
2169        return rc;
2170}
2171
2172
2173int shpchp_disable_slot (struct slot *p_slot)
2174{
2175        u8 class_code, header_type, BCR;
2176        u8 index = 0;
2177        u8 getstatus = 0;
2178        u32 rc = 0;
2179        int ret = 0;
2180        unsigned int devfn;
2181        struct pci_bus *pci_bus = p_slot->ctrl->pci_dev->subordinate;
2182        struct pci_func *func;
2183
2184        if (!p_slot->ctrl)
2185                return (1);
2186
2187        /* Check to see if (latch closed, card present, power on) */
2188        down(&p_slot->ctrl->crit_sect);
2189
2190        ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2191        if (ret || !getstatus) {
2192                info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2193                up(&p_slot->ctrl->crit_sect);
2194                return (1);
2195        }
2196        ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2197        if (ret || getstatus) {
2198                info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2199                up(&p_slot->ctrl->crit_sect);
2200                return (1);
2201        }
2202        ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2203        if (ret || !getstatus) {
2204                info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
2205                up(&p_slot->ctrl->crit_sect);
2206                return (1);
2207        }
2208        up(&p_slot->ctrl->crit_sect);
2209
2210        func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2211
2212        /* Make sure there are no video controllers here
2213         * for all func of p_slot
2214         */
2215        while (func && !rc) {
2216                pci_bus->number = func->bus;
2217                devfn = PCI_DEVFN(func->device, func->function);
2218
2219                /* Check the Class Code */
2220                rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2221                if (rc)
2222                        return rc;
2223
2224                if (class_code == PCI_BASE_CLASS_DISPLAY) {
2225                        /* Display/Video adapter (not supported) */
2226                        rc = REMOVE_NOT_SUPPORTED;
2227                } else {
2228                        /* See if it's a bridge */
2229                        rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2230                        if (rc)
2231                                return rc;
2232
2233                        /* If it's a bridge, check the VGA Enable bit */
2234                        if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2235                                rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2236                                if (rc)
2237                                        return rc;
2238
2239                                /* If the VGA Enable bit is set, remove isn't supported */
2240                                if (BCR & PCI_BRIDGE_CTL_VGA) {
2241                                        rc = REMOVE_NOT_SUPPORTED;
2242                                }
2243                        }
2244                }
2245
2246                func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2247        }
2248
2249        func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2250        if ((func != NULL) && !rc) {
2251                rc = remove_board(func, p_slot->ctrl);
2252        } else if (!rc)
2253                rc = 1;
2254
2255        if (p_slot)
2256                update_slot_info(p_slot);
2257
2258        return(rc);
2259}
2260
2261
2262/**
2263 * configure_new_device - Configures the PCI header information of one board.
2264 *
2265 * @ctrl: pointer to controller structure
2266 * @func: pointer to function structure
2267 * @behind_bridge: 1 if this is a recursive call, 0 if not
2268 * @resources: pointer to set of resource lists
2269 *
2270 * Returns 0 if success
2271 *
2272 */
2273static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
2274        u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
2275{
2276        u8 temp_byte, function, max_functions, stop_it;
2277        int rc;
2278        u32 ID;
2279        struct pci_func *new_slot;
2280        struct pci_bus lpci_bus, *pci_bus;
2281        int index;
2282
2283        new_slot = func;
2284
2285        dbg("%s\n", __FUNCTION__);
2286        memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2287        pci_bus = &lpci_bus;
2288        pci_bus->number = func->bus;
2289
2290        /* Check for Multi-function device */
2291        rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2292        if (rc) {
2293                dbg("%s: rc = %d\n", __FUNCTION__, rc);
2294                return rc;
2295        }
2296
2297        if (temp_byte & 0x80)   /* Multi-function device */
2298                max_functions = 8;
2299        else
2300                max_functions = 1;
2301
2302        function = 0;
2303
2304        do {
2305                rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
2306
2307                if (rc) {
2308                        dbg("configure_new_function failed %d\n",rc);
2309                        index = 0;
2310
2311                        while (new_slot) {
2312                                new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
2313
2314                                if (new_slot)
2315                                        shpchp_return_board_resources(new_slot, resources);
2316                        }
2317
2318                        return(rc);
2319                }
2320
2321                function++;
2322
2323                stop_it = 0;
2324
2325                /*  The following loop skips to the next present function
2326                 *  and creates a board structure
2327                 */
2328
2329                while ((function < max_functions) && (!stop_it)) {
2330                        pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2331
2332                        if (ID == 0xFFFFFFFF) {   /* There's nothing there. */
2333                                function++;
2334                        } else {  /* There's something there */
2335                                /* Setup slot structure. */
2336                                new_slot = shpchp_slot_create(func->bus);
2337
2338                                if (new_slot == NULL) {
2339                                        /* Out of memory */
2340                                        return(1);
2341                                }
2342
2343                                new_slot->bus = func->bus;
2344                                new_slot->device = func->device;
2345                                new_slot->function = function;
2346                                new_slot->is_a_board = 1;
2347                                new_slot->status = 0;
2348
2349                                stop_it++;
2350                        }
2351                }
2352
2353        } while (function < max_functions);
2354        dbg("returning from configure_new_device\n");
2355
2356        return 0;
2357}
2358
2359
2360/*
2361 * Configuration logic that involves the hotplug data structures and 
2362 * their bookkeeping
2363 */
2364
2365
2366/**
2367 * configure_new_function - Configures the PCI header information of one device
2368 *
2369 * @ctrl: pointer to controller structure
2370 * @func: pointer to function structure
2371 * @behind_bridge: 1 if this is a recursive call, 0 if not
2372 * @resources: pointer to set of resource lists
2373 *
2374 * Calls itself recursively for bridged devices.
2375 * Returns 0 if success
2376 *
2377 */
2378static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2379        u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
2380{
2381        int cloop;
2382        u8 temp_byte;
2383        u8 device;
2384        u8 class_code;
2385        u16 temp_word;
2386        u32 rc;
2387        u32 temp_register;
2388        u32 base;
2389        u32 ID;
2390        unsigned int devfn;
2391        struct pci_resource *mem_node;
2392        struct pci_resource *p_mem_node;
2393        struct pci_resource *io_node;
2394        struct pci_resource *bus_node;
2395        struct pci_resource *hold_mem_node;
2396        struct pci_resource *hold_p_mem_node;
2397        struct pci_resource *hold_IO_node;
2398        struct pci_resource *hold_bus_node;
2399        struct irq_mapping irqs;
2400        struct pci_func *new_slot;
2401        struct pci_bus lpci_bus, *pci_bus;
2402        struct resource_lists temp_resources;
2403#if defined(CONFIG_X86_64)
2404        u8 IRQ = 0;
2405#endif
2406        
2407        memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2408        pci_bus = &lpci_bus;
2409        pci_bus->number = func->bus;
2410        devfn = PCI_DEVFN(func->device, func->function);
2411
2412        /* Check for Bridge */
2413        rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2414        if (rc)
2415                return rc;
2416
2417        if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2418                /* set Primary bus */
2419                dbg("set Primary bus = 0x%x\n", func->bus);
2420                rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2421                if (rc)
2422                        return rc;
2423
2424                /* find range of busses to use */
2425                bus_node = get_max_resource(&resources->bus_head, 1L);
2426
2427                /* If we don't have any busses to allocate, we can't continue */
2428                if (!bus_node) {
2429                        err("Got NO bus resource to use\n");
2430                        return -ENOMEM;
2431                }
2432                dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2433
2434                /* set Secondary bus */
2435                temp_byte = (u8)bus_node->base;
2436                dbg("set Secondary bus = 0x%x\n", temp_byte);
2437                rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2438                if (rc)
2439                        return rc;
2440
2441                /* set subordinate bus */
2442                temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2443                dbg("set subordinate bus = 0x%x\n", temp_byte);
2444                rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2445                if (rc)
2446                        return rc;
2447
2448                /* Set HP parameters (Cache Line Size, Latency Timer) */
2449                rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2450                if (rc)
2451                        return rc;
2452
2453                /* Setup the IO, memory, and prefetchable windows */
2454
2455                io_node = get_max_resource(&(resources->io_head), 0x1000L);
2456                if (io_node) {
2457                        dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2458                }
2459
2460                mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2461                if (mem_node) {
2462                        dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2463                }
2464
2465                if (resources->p_mem_head)
2466                        p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2467                else {
2468                        /*
2469                         * In some platform implementation, MEM and PMEM are not
2470                         *  distinguished, and hence ACPI _CRS has only MEM entries
2471                         *  for both MEM and PMEM.
2472                         */
2473                        dbg("using MEM for PMEM\n");
2474                        p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2475                }
2476                if (p_mem_node) {
2477                        dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2478                }
2479
2480                /* Set up the IRQ info */
2481                if (!resources->irqs) {
2482                        irqs.barber_pole = 0;
2483                        irqs.interrupt[0] = 0;
2484                        irqs.interrupt[1] = 0;
2485                        irqs.interrupt[2] = 0;
2486                        irqs.interrupt[3] = 0;
2487                        irqs.valid_INT = 0;
2488                } else {
2489                        irqs.barber_pole = resources->irqs->barber_pole;
2490                        irqs.interrupt[0] = resources->irqs->interrupt[0];
2491                        irqs.interrupt[1] = resources->irqs->interrupt[1];
2492                        irqs.interrupt[2] = resources->irqs->interrupt[2];
2493                        irqs.interrupt[3] = resources->irqs->interrupt[3];
2494                        irqs.valid_INT = resources->irqs->valid_INT;
2495                }
2496
2497                /* Set up resource lists that are now aligned on top and bottom
2498                 * for anything behind the bridge.
2499                 */
2500                temp_resources.bus_head = bus_node;
2501                temp_resources.io_head = io_node;
2502                temp_resources.mem_head = mem_node;
2503                temp_resources.p_mem_head = p_mem_node;
2504                temp_resources.irqs = &irqs;
2505
2506                /* Make copies of the nodes we are going to pass down so that
2507                 * if there is a problem,we can just use these to free resources
2508                 */
2509                hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2510                hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2511                hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2512                hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2513
2514                if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2515                        if (hold_bus_node)
2516                                kfree(hold_bus_node);
2517                        if (hold_IO_node)
2518                                kfree(hold_IO_node);
2519                        if (hold_mem_node)
2520                                kfree(hold_mem_node);
2521                        if (hold_p_mem_node)
2522                                kfree(hold_p_mem_node);
2523
2524                        return(1);
2525                }
2526
2527                memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2528
2529                bus_node->base += 1;
2530                bus_node->length -= 1;
2531                bus_node->next = NULL;
2532
2533                /* If we have IO resources copy them and fill in the bridge's
2534                 * IO range registers
2535                 */
2536                if (io_node) {
2537                        memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2538                        io_node->next = NULL;
2539
2540                        /* Set IO base and Limit registers */
2541                        RES_CHECK(io_node->base, 8);
2542                        temp_byte = (u8)(io_node->base >> 8);
2543                        rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2544
2545                        RES_CHECK(io_node->base + io_node->length - 1, 8);
2546                        temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2547                        rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2548                } else {
2549                        kfree(hold_IO_node);
2550                        hold_IO_node = NULL;
2551                }
2552
2553                /* If we have memory resources copy them and fill in the bridge's
2554                 * memory range registers.  Otherwise, fill in the range
2555                 * registers with values that disable them.
2556                 */
2557                if (mem_node) {
2558                        memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2559                        mem_node->next = NULL;
2560
2561                        /* Set Mem base and Limit registers */
2562                        RES_CHECK(mem_node->base, 16);
2563                        temp_word = (u32)(mem_node->base >> 16);
2564                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2565
2566                        RES_CHECK(mem_node->base + mem_node->length - 1, 16);
2567                        temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
2568                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2569                } else {
2570                        temp_word = 0xFFFF;
2571                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2572
2573                        temp_word = 0x0000;
2574                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2575
2576                        kfree(hold_mem_node);
2577                        hold_mem_node = NULL;
2578                }
2579
2580                /* If we have prefetchable memory resources copy them and 
2581                 * fill in the bridge's memory range registers.  Otherwise,
2582                 * fill in the range registers with values that disable them.
2583                 */
2584                if (p_mem_node) {
2585                        memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2586                        p_mem_node->next = NULL;
2587
2588                        /* Set Pre Mem base and Limit registers */
2589                        RES_CHECK(p_mem_node->base, 16);
2590                        temp_word = (u32)(p_mem_node->base >> 16);
2591                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2592
2593                        RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
2594                        temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
2595                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2596                } else {
2597                        temp_word = 0xFFFF;
2598                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2599
2600                        temp_word = 0x0000;
2601                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2602
2603                        kfree(hold_p_mem_node);
2604                        hold_p_mem_node = NULL;
2605                }
2606
2607                /* Adjust this to compensate for extra adjustment in first loop */
2608                irqs.barber_pole--;
2609
2610                rc = 0;
2611
2612                /* Here we actually find the devices and configure them */
2613                for (device = 0; (device <= 0x1F) && !rc; device++) {
2614                        irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2615
2616                        ID = 0xFFFFFFFF;
2617                        pci_bus->number = hold_bus_node->base;
2618                        pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
2619                        pci_bus->number = func->bus;
2620
2621                        if (ID != 0xFFFFFFFF) {   /*  device Present */
2622                                /* Setup slot structure. */
2623                                new_slot = shpchp_slot_create(hold_bus_node->base);
2624
2625                                if (new_slot == NULL) {
2626                                        /* Out of memory */
2627                                        rc = -ENOMEM;
2628                                        continue;
2629                                }
2630
2631                                new_slot->bus = hold_bus_node->base;
2632                                new_slot->device = device;
2633                                new_slot->function = 0;
2634                                new_slot->is_a_board = 1;
2635                                new_slot->status = 0;
2636
2637                                rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
2638                                dbg("configure_new_device rc=0x%x\n",rc);
2639                        }       /* End of IF (device in slot?) */
2640                }               /* End of FOR loop */
2641
2642                if (rc) {
2643                        shpchp_destroy_resource_list(&temp_resources);
2644
2645                        return_resource(&(resources->bus_head), hold_bus_node);
2646                        return_resource(&(resources->io_head), hold_IO_node);
2647                        return_resource(&(resources->mem_head), hold_mem_node);
2648                        return_resource(&(resources->p_mem_head), hold_p_mem_node);
2649                        return(rc);
2650                }
2651
2652                /* save the interrupt routing information */
2653                if (resources->irqs) {
2654                        resources->irqs->interrupt[0] = irqs.interrupt[0];
2655                        resources->irqs->interrupt[1] = irqs.interrupt[1];
2656                        resources->irqs->interrupt[2] = irqs.interrupt[2];
2657                        resources->irqs->interrupt[3] = irqs.interrupt[3];
2658                        resources->irqs->valid_INT = irqs.valid_INT;
2659                } else if (!behind_bridge) {
2660                        /* We need to hook up the interrupts here */
2661                        for (cloop = 0; cloop < 4; cloop++) {
2662                                if (irqs.valid_INT & (0x01 << cloop)) {
2663                                        rc = shpchp_set_irq(func->bus, func->device,
2664                                                           0x0A + cloop, irqs.interrupt[cloop]);
2665                                        if (rc) {
2666                                                shpchp_destroy_resource_list (&temp_resources);
2667                                                return_resource(&(resources->bus_head), hold_bus_node);
2668                                                return_resource(&(resources->io_head), hold_IO_node);
2669                                                return_resource(&(resources->mem_head), hold_mem_node);
2670                                                return_resource(&(resources->p_mem_head), hold_p_mem_node);
2671                                                return rc;
2672                                        }
2673                                }
2674                        }       /* end of for loop */
2675                }
2676
2677                /* Return unused bus resources
2678                 * First use the temporary node to store information for the board
2679                 */
2680                if (hold_bus_node && bus_node && temp_resources.bus_head) {
2681                        hold_bus_node->length = bus_node->base - hold_bus_node->base;
2682
2683                        hold_bus_node->next = func->bus_head;
2684                        func->bus_head = hold_bus_node;
2685
2686                        temp_byte = (u8)(temp_resources.bus_head->base - 1);
2687
2688                        /* set subordinate bus */
2689                        dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2690                        rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2691
2692                        if (temp_resources.bus_head->length == 0) {
2693                                kfree(temp_resources.bus_head);
2694                                temp_resources.bus_head = NULL;
2695                        } else {
2696                                dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2697                                        func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2698                                return_resource(&(resources->bus_head), temp_resources.bus_head);
2699                        }
2700                }
2701
2702                /* If we have IO space available and there is some left,
2703                 * return the unused portion
2704                 */
2705                if (hold_IO_node && temp_resources.io_head) {
2706                        io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2707                                                               &hold_IO_node, 0x1000);
2708
2709                        /* Check if we were able to split something off */
2710                        if (io_node) {
2711                                hold_IO_node->base = io_node->base + io_node->length;
2712
2713                                RES_CHECK(hold_IO_node->base, 8);
2714                                temp_byte = (u8)((hold_IO_node->base) >> 8);
2715                                rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2716
2717                                return_resource(&(resources->io_head), io_node);
2718                        }
2719
2720                        io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2721
2722                        /*  Check if we were able to split something off */
2723                        if (io_node) {
2724                                /* First use the temporary node to store information for the board */
2725                                hold_IO_node->length = io_node->base - hold_IO_node->base;
2726
2727                                /* If we used any, add it to the board's list */
2728                                if (hold_IO_node->length) {
2729                                        hold_IO_node->next = func->io_head;
2730                                        func->io_head = hold_IO_node;
2731
2732                                        RES_CHECK(io_node->base - 1, 8);
2733                                        temp_byte = (u8)((io_node->base - 1) >> 8);
2734                                        rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2735
2736                                        return_resource(&(resources->io_head), io_node);
2737                                } else {
2738                                        /* it doesn't need any IO */
2739                                        temp_byte = 0x00;
2740                                        rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2741
2742                                        return_resource(&(resources->io_head), io_node);
2743                                        kfree(hold_IO_node);
2744                                }
2745                        } else {
2746                                /* It used most of the range */
2747                                hold_IO_node->next = func->io_head;
2748                                func->io_head = hold_IO_node;
2749                        }
2750                } else if (hold_IO_node) {
2751                        /* it used the whole range */
2752                        hold_IO_node->next = func->io_head;
2753                        func->io_head = hold_IO_node;
2754                }
2755
2756                /* If we have memory space available and there is some left,
2757                 * return the unused portion
2758                 */
2759                if (hold_mem_node && temp_resources.mem_head) {
2760                        mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2761
2762                        /* Check if we were able to split something off */
2763                        if (mem_node) {
2764                                hold_mem_node->base = mem_node->base + mem_node->length;
2765
2766                                RES_CHECK(hold_mem_node->base, 16);
2767                                temp_word = (u32)((hold_mem_node->base) >> 16);
2768                                rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2769
2770                                return_resource(&(resources->mem_head), mem_node);
2771                        }
2772
2773                        mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2774
2775                        /* Check if we were able to split something off */
2776                        if (mem_node) {
2777                                /* First use the temporary node to store information for the board */
2778                                hold_mem_node->length = mem_node->base - hold_mem_node->base;
2779
2780                                if (hold_mem_node->length) {
2781                                        hold_mem_node->next = func->mem_head;
2782                                        func->mem_head = hold_mem_node;
2783
2784                                        /* configure end address */
2785                                        RES_CHECK(mem_node->base - 1, 16);
2786                                        temp_word = (u32)((mem_node->base - 1) >> 16);
2787                                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2788
2789                                        /* Return unused resources to the pool */
2790                                        return_resource(&(resources->mem_head), mem_node);
2791                                } else {
2792                                        /* it doesn't need any Mem */
2793                                        temp_word = 0x0000;
2794                                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2795
2796                                        return_resource(&(resources->mem_head), mem_node);
2797                                        kfree(hold_mem_node);
2798                                }
2799                        } else {
2800                                /* It used most of the range */
2801                                hold_mem_node->next = func->mem_head;
2802                                func->mem_head = hold_mem_node;
2803                        }
2804                } else if (hold_mem_node) {
2805                        /* It used the whole range */
2806                        hold_mem_node->next = func->mem_head;
2807                        func->mem_head = hold_mem_node;
2808                }
2809
2810                /* If we have prefetchable memory space available and there is some 
2811                 * left at the end, return the unused portion
2812                 */
2813                if (hold_p_mem_node && temp_resources.p_mem_head) {
2814                        p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2815                                                                  &hold_p_mem_node, 0x100000L);
2816
2817                        /* Check if we were able to split something off */
2818                        if (p_mem_node) {
2819                                hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2820
2821                                RES_CHECK(hold_p_mem_node->base, 16);
2822                                temp_word = (u32)((hold_p_mem_node->base) >> 16);
2823                                rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2824
2825                                return_resource(&(resources->p_mem_head), p_mem_node);
2826                        }
2827
2828                        p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2829
2830                        /* Check if we were able to split something off */
2831                        if (p_mem_node) {
2832                                /* First use the temporary node to store information for the board */
2833                                hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2834
2835                                /* If we used any, add it to the board's list */
2836                                if (hold_p_mem_node->length) {
2837                                        hold_p_mem_node->next = func->p_mem_head;
2838                                        func->p_mem_head = hold_p_mem_node;
2839
2840                                        RES_CHECK(p_mem_node->base - 1, 16);
2841                                        temp_word = (u32)((p_mem_node->base - 1) >> 16);
2842                                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2843
2844                                        return_resource(&(resources->p_mem_head), p_mem_node);
2845                                } else {
2846                                        /* It doesn't need any PMem */
2847                                        temp_word = 0x0000;
2848                                        rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2849
2850                                        return_resource(&(resources->p_mem_head), p_mem_node);
2851                                        kfree(hold_p_mem_node);
2852                                }
2853                        } else {
2854                                /* It used the most of the range */
2855                                hold_p_mem_node->next = func->p_mem_head;
2856                                func->p_mem_head = hold_p_mem_node;
2857                        }
2858                } else if (hold_p_mem_node) {
2859                        /* It used the whole range */
2860                        hold_p_mem_node->next = func->p_mem_head;
2861                        func->p_mem_head = hold_p_mem_node;
2862                }
2863
2864                /* We should be configuring an IRQ and the bridge's base address
2865                 * registers if it needs them.  Although we have never seen such
2866                 * a device
2867                 */
2868
2869                shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2870
2871                dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, 
2872                        func->bus, func->device, func->function);
2873        } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2874                /* Standard device */
2875                u64     base64;
2876                rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2877
2878                if (class_code == PCI_BASE_CLASS_DISPLAY)
2879                        return (DEVICE_TYPE_NOT_SUPPORTED);
2880
2881                /* Figure out IO and memory needs */
2882                for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2883                        temp_register = 0xFFFFFFFF;
2884
2885                        rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2886                        rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2887                        dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, 
2888                                func->bus, func->device, func->function);
2889
2890                        if (!temp_register)
2891                                continue;
2892
2893                        base64 = 0L;
2894                        if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2895                                /* Map IO */
2896
2897                                /* Set base = amount of IO space */
2898                                base = temp_register & 0xFFFFFFFC;
2899                                base = ~base + 1;
2900
2901                                dbg("NEED IO length(0x%x)\n", base);
2902                                io_node = get_io_resource(&(resources->io_head),(ulong)base);
2903
2904                                /* Allocate the resource to the board */
2905                                if (io_node) {
2906                                        dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2907                                        base = (u32)io_node->base;
2908                                        io_node->next = func->io_head;
2909                                        func->io_head = io_node;
2910                                } else {
2911                                        err("Got NO IO resource(length=0x%x)\n", base);
2912                                        return -ENOMEM;
2913                                }
2914                        } else {        /* Map MEM */
2915                                int prefetchable = 1;
2916                                struct pci_resource **res_node = &func->p_mem_head;
2917                                char *res_type_str = "PMEM";
2918                                u32     temp_register2;
2919
2920                                if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2921                                        prefetchable = 0;
2922                                        res_node = &func->mem_head;
2923                                        res_type_str++;
2924                                }
2925
2926                                base = temp_register & 0xFFFFFFF0;
2927                                base = ~base + 1;
2928
2929                                switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2930                                case PCI_BASE_ADDRESS_MEM_TYPE_32:
2931                                        dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2932
2933                                        if (prefetchable && resources->p_mem_head)
2934                                                mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2935                                        else {
2936                                                if (prefetchable)
2937                                                        dbg("using MEM for PMEM\n");
2938                                                mem_node=get_resource(&(resources->mem_head), (ulong)base);
2939                                        }
2940
2941                                        /* Allocate the resource to the board */
2942                                        if (mem_node) {
2943                                                base = (u32)mem_node->base; 
2944                                                mem_node->next = *res_node;
2945                                                *res_node = mem_node;
2946                                                dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base, mem_node->length);
2947                                        } else {
2948                                                err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2949                                                return -ENOMEM;
2950                                        }
2951                                        break;
2952                                case PCI_BASE_ADDRESS_MEM_TYPE_64:
2953                                        rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2954                                        dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2, temp_register, base);
2955
2956                                        if (prefetchable && resources->p_mem_head)
2957                                                mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2958                                        else {
2959                                                if (prefetchable)
2960                                                        dbg("using MEM for PMEM\n");
2961                                                mem_node = get_resource(&(resources->mem_head), (ulong)base);
2962                                        }
2963
2964                                        /* Allocate the resource to the board */
2965                                        if (mem_node) {
2966                                                base64 = mem_node->base; 
2967                                                mem_node->next = *res_node;
2968                                                *res_node = mem_node;
2969                                                dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32), (u32)base64, mem_node->length);
2970                                        } else {
2971                                                err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2972                                                return -ENOMEM;
2973                                        }
2974                                        break;
2975                                default:
2976                                        dbg("reserved BAR type=0x%x\n", temp_register);
2977                                        break;
2978                                }
2979
2980                        }
2981
2982                        if (base64) {
2983                                rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2984                                cloop += 4;
2985                                base64 >>= 32;
2986
2987                                if (base64) {
2988                                        dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2989                                        base64 = 0x0L;
2990                                }
2991
2992                                rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2993                        } else {
2994                                rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2995                        }
2996                }               /* End of base register loop */
2997
2998#if defined(CONFIG_X86_64)
2999                /* Figure out which interrupt pin this function uses */
3000                rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
3001
3002                /* If this function needs an interrupt and we are behind a bridge
3003                   and the pin is tied to something that's alread mapped,
3004                   set this one the same
3005                 */
3006                if (temp_byte && resources->irqs && 
3007                    (resources->irqs->valid_INT & 
3008                     (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3009                        /* We have to share with something already set up */
3010                        IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
3011                } else {
3012                        /* Program IRQ based on card type */
3013                        rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3014
3015                        if (class_code == PCI_BASE_CLASS_STORAGE) {
3016                                IRQ = shpchp_disk_irq;
3017                        } else {
3018                                IRQ = shpchp_nic_irq;
3019                        }
3020                }
3021
3022                /* IRQ Line */
3023                rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3024
3025                if (!behind_bridge) {
3026                        rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3027                        if (rc)
3028                                return(1);
3029                } else {
3030                        /* TBD - this code may also belong in the other clause of this If statement */
3031                        resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3032                        resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3033                }
3034#endif
3035                /* Disable ROM base Address */
3036                temp_word = 0x00L;
3037                rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
3038
3039                /* Set HP parameters (Cache Line Size, Latency Timer) */
3040                rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3041                if (rc)
3042                        return rc;
3043
3044                shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3045
3046                dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
3047        }                       /* End of Not-A-Bridge else */
3048        else {
3049                /* It's some strange type of PCI adapter (Cardbus?) */
3050                return(DEVICE_TYPE_NOT_SUPPORTED);
3051        }
3052
3053        func->configured = 1;
3054
3055        return 0;
3056}
3057
3058