2 * Compaq Hot Plug Controller Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <greg@kroah.com>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/workqueue.h>
34 #include <linux/proc_fs.h>
35 #include <linux/pci.h>
38 #include "cpqphp_nvram.h"
39 #include "../../../arch/i386/pci/pci.h" /* horrible hack showing how processor dependent we are... */
45 static u16 unused_IRQ;
48 * detect_HRT_floating_pointer
50 * find the Hot Plug Resource Table in the specified region of memory.
53 static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
57 u8 temp1, temp2, temp3, temp4;
60 endp = (end - sizeof(struct hrt) + 1);
62 for (fp = begin; fp <= endp; fp += 16) {
63 temp1 = readb(fp + SIG0);
64 temp2 = readb(fp + SIG1);
65 temp3 = readb(fp + SIG2);
66 temp4 = readb(fp + SIG3);
79 dbg("Discovered Hotplug Resource Table at %p\n", fp);
84 int cpqhp_configure_device (struct controller* ctrl, struct pci_func* func)
87 struct pci_bus *child;
90 if (func->pci_dev == NULL)
91 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
93 /* No pci device, we need to create it then */
94 if (func->pci_dev == NULL) {
95 dbg("INFO: pci_dev still null\n");
97 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
99 pci_bus_add_devices(ctrl->pci_dev->bus);
101 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
102 if (func->pci_dev == NULL) {
103 dbg("ERROR: pci_dev still null\n");
108 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
109 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
110 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
111 pci_do_scan_bus(child);
118 int cpqhp_unconfigure_device(struct pci_func* func)
122 dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus, func->device, func->function);
124 for (j=0; j<8 ; j++) {
125 struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
127 pci_remove_bus_device(temp);
132 static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
136 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
138 if (vendID == 0xffffffff)
140 return pci_bus_read_config_dword (bus, devfn, offset, value);
147 * @bus_num: bus number of PCI device
148 * @dev_num: device number of PCI device
149 * @slot: pointer to u8 where slot number will be returned
151 int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
155 if (cpqhp_legacy_mode) {
156 struct pci_dev *fakedev;
157 struct pci_bus *fakebus;
160 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
161 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
162 if (!fakedev || !fakebus) {
168 fakedev->devfn = dev_num << 3;
169 fakedev->bus = fakebus;
170 fakebus->number = bus_num;
171 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
172 __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
173 rc = pcibios_set_irq_routing(fakedev, int_pin - 0x0a, irq_num);
176 dbg("%s: rc %d\n", __FUNCTION__, rc);
180 // set the Edge Level Control Register (ELCR)
181 temp_word = inb(0x4d0);
182 temp_word |= inb(0x4d1) << 8;
184 temp_word |= 0x01 << irq_num;
186 // This should only be for x86 as it sets the Edge Level Control Register
187 outb((u8) (temp_word & 0xFF), 0x4d0);
188 outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
197 * WTF??? This function isn't in the code, yet a function calls it, but the
198 * compiler optimizes it away? strange. Here as a placeholder to keep the
201 static int PCI_ScanBusNonBridge (u8 bus, u8 device)
206 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
212 ctrl->pci_bus->number = bus_num;
214 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
215 //Scan for access first
216 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
218 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
219 //Yep we got one. Not a bridge ?
220 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
226 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
227 //Scan for access first
228 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
230 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
231 //Yep we got one. bridge ?
232 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
233 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
234 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
235 if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
244 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
246 struct irq_routing_table *PCIIRQRoutingInfoLength;
251 u8 tbus, tdevice, tslot;
253 PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
254 if (!PCIIRQRoutingInfoLength)
257 len = (PCIIRQRoutingInfoLength->size -
258 sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
259 // Make sure I got at least one entry
261 kfree(PCIIRQRoutingInfoLength );
265 for (loop = 0; loop < len; ++loop) {
266 tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
267 tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn;
268 tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
273 ctrl->pci_bus->number = tbus;
274 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
275 if (!nobridge || (work == 0xffffffff)) {
276 kfree(PCIIRQRoutingInfoLength );
280 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
281 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
282 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
284 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
285 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
286 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
287 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
289 kfree(PCIIRQRoutingInfoLength );
293 kfree(PCIIRQRoutingInfoLength );
299 kfree(PCIIRQRoutingInfoLength );
304 int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
306 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0); //plain (bridges allowed)
310 /* More PCI configuration routines; this time centered around hotplug controller */
316 * Reads configuration for all slots in a PCI bus and saves info.
318 * Note: For non-hot plug busses, the slot # saved is the device #
320 * returns 0 if success
322 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
329 struct pci_func *new_slot;
341 // Decide which slots are supported
344 //*********************************
345 // is_hot_plug is the slot mask
346 //*********************************
347 FirstSupported = is_hot_plug >> 4;
348 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
351 LastSupported = 0x1F;
354 // Save PCI configuration space for all devices in supported slots
355 ctrl->pci_bus->number = busnumber;
356 for (device = FirstSupported; device <= LastSupported; device++) {
358 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
360 if (ID != 0xFFFFFFFF) { // device in slot
361 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
365 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
369 // If multi-function device, set max_functions to 8
370 if (header_type & 0x80)
380 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // P-P Bridge
381 // Recurse the subordinate bus
382 // get the subordinate bus number
383 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
387 sub_bus = (int) secondary_bus;
389 // Save secondary bus cfg spc
390 // with this recursive call.
391 rc = cpqhp_save_config(ctrl, sub_bus, 0);
394 ctrl->pci_bus->number = busnumber;
399 new_slot = cpqhp_slot_find(busnumber, device, index++);
401 (new_slot->function != (u8) function))
402 new_slot = cpqhp_slot_find(busnumber, device, index++);
405 // Setup slot structure.
406 new_slot = cpqhp_slot_create(busnumber);
408 if (new_slot == NULL)
412 new_slot->bus = (u8) busnumber;
413 new_slot->device = (u8) device;
414 new_slot->function = (u8) function;
415 new_slot->is_a_board = 1;
416 new_slot->switch_save = 0x10;
417 // In case of unsupported board
418 new_slot->status = DevError;
419 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
421 for (cloop = 0; cloop < 0x20; cloop++) {
422 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
431 // this loop skips to the next present function
432 // reading in Class Code and Header type.
434 while ((function < max_functions)&&(!stop_it)) {
435 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
436 if (ID == 0xFFFFFFFF) { // nothing there.
438 } else { // Something there
439 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
443 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
451 } while (function < max_functions);
452 } // End of IF (device in slot?)
453 else if (is_hot_plug) {
454 // Setup slot structure with entry for empty slot
455 new_slot = cpqhp_slot_create(busnumber);
457 if (new_slot == NULL) {
461 new_slot->bus = (u8) busnumber;
462 new_slot->device = (u8) device;
463 new_slot->function = 0;
464 new_slot->is_a_board = 0;
465 new_slot->presence_save = 0;
466 new_slot->switch_save = 0;
475 * cpqhp_save_slot_config
477 * Saves configuration info for all PCI devices in a given slot
478 * including subordinate busses.
480 * returns 0 if success
482 int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
497 ctrl->pci_bus->number = new_slot->bus;
498 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
500 if (ID != 0xFFFFFFFF) { // device in slot
501 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
502 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
504 if (header_type & 0x80) // Multi-function device
512 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
513 // Recurse the subordinate bus
514 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
516 sub_bus = (int) secondary_bus;
518 // Save the config headers for the secondary bus.
519 rc = cpqhp_save_config(ctrl, sub_bus, 0);
522 ctrl->pci_bus->number = new_slot->bus;
526 new_slot->status = 0;
528 for (cloop = 0; cloop < 0x20; cloop++) {
529 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
536 // this loop skips to the next present function
537 // reading in the Class Code and the Header type.
539 while ((function < max_functions) && (!stop_it)) {
540 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
542 if (ID == 0xFFFFFFFF) { // nothing there.
544 } else { // Something there
545 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
547 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
553 } while (function < max_functions);
554 } // End of IF (device in slot?)
564 * cpqhp_save_base_addr_length
566 * Saves the length of all base address registers for the
567 * specified slot. this is for hot plug REPLACE
569 * returns 0 if success
571 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
581 struct pci_func *next;
583 struct pci_bus *pci_bus = ctrl->pci_bus;
586 func = cpqhp_slot_find(func->bus, func->device, index++);
588 while (func != NULL) {
589 pci_bus->number = func->bus;
590 devfn = PCI_DEVFN(func->device, func->function);
593 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
595 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
597 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
599 sub_bus = (int) secondary_bus;
601 next = cpqhp_slot_list[sub_bus];
603 while (next != NULL) {
604 rc = cpqhp_save_base_addr_length(ctrl, next);
610 pci_bus->number = func->bus;
612 //FIXME: this loop is duplicated in the non-bridge case. The two could be rolled together
613 // Figure out IO and memory base lengths
614 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
615 temp_register = 0xFFFFFFFF;
616 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
617 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
619 if (base) { // If this register is implemented
622 // set base = amount of IO space requested
623 base = base & 0xFFFFFFFE;
629 base = base & 0xFFFFFFF0;
639 // Save information in slot structure
640 func->base_length[(cloop - 0x10) >> 2] =
642 func->base_type[(cloop - 0x10) >> 2] = type;
644 } // End of base register loop
647 } else if ((header_type & 0x7F) == 0x00) { // PCI-PCI Bridge
648 // Figure out IO and memory base lengths
649 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
650 temp_register = 0xFFFFFFFF;
651 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
652 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
654 if (base) { // If this register is implemented
657 // base = amount of IO space requested
658 base = base & 0xFFFFFFFE;
664 // base = amount of memory space requested
665 base = base & 0xFFFFFFF0;
675 // Save information in slot structure
676 func->base_length[(cloop - 0x10) >> 2] = base;
677 func->base_type[(cloop - 0x10) >> 2] = type;
679 } // End of base register loop
681 } else { // Some other unknown header type
684 // find the next device in this slot
685 func = cpqhp_slot_find(func->bus, func->device, index++);
693 * cpqhp_save_used_resources
695 * Stores used resource information for existing boards. this is
696 * for boards that were in the system when this driver was loaded.
697 * this function is for hot plug ADD
699 * returns 0 if success
701 int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
717 struct pci_resource *mem_node;
718 struct pci_resource *p_mem_node;
719 struct pci_resource *io_node;
720 struct pci_resource *bus_node;
721 struct pci_bus *pci_bus = ctrl->pci_bus;
724 func = cpqhp_slot_find(func->bus, func->device, index++);
726 while ((func != NULL) && func->is_a_board) {
727 pci_bus->number = func->bus;
728 devfn = PCI_DEVFN(func->device, func->function);
730 // Save the command register
731 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
735 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
738 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
740 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
741 // Clear Bridge Control Register
743 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
744 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
745 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
747 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
751 bus_node->base = secondary_bus;
752 bus_node->length = temp_byte - secondary_bus + 1;
754 bus_node->next = func->bus_head;
755 func->bus_head = bus_node;
757 // Save IO base and Limit registers
758 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
759 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
761 if ((b_base <= b_length) && (save_command & 0x01)) {
762 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
766 io_node->base = (b_base & 0xF0) << 8;
767 io_node->length = (b_length - b_base + 0x10) << 8;
769 io_node->next = func->io_head;
770 func->io_head = io_node;
773 // Save memory base and Limit registers
774 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
775 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
777 if ((w_base <= w_length) && (save_command & 0x02)) {
778 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
782 mem_node->base = w_base << 16;
783 mem_node->length = (w_length - w_base + 0x10) << 16;
785 mem_node->next = func->mem_head;
786 func->mem_head = mem_node;
789 // Save prefetchable memory base and Limit registers
790 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
791 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
793 if ((w_base <= w_length) && (save_command & 0x02)) {
794 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
798 p_mem_node->base = w_base << 16;
799 p_mem_node->length = (w_length - w_base + 0x10) << 16;
801 p_mem_node->next = func->p_mem_head;
802 func->p_mem_head = p_mem_node;
804 // Figure out IO and memory base lengths
805 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
806 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
808 temp_register = 0xFFFFFFFF;
809 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
810 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
812 temp_register = base;
814 if (base) { // If this register is implemented
815 if (((base & 0x03L) == 0x01)
816 && (save_command & 0x01)) {
818 // set temp_register = amount of IO space requested
819 temp_register = base & 0xFFFFFFFE;
820 temp_register = (~temp_register) + 1;
822 io_node = kmalloc(sizeof(*io_node),
828 save_base & (~0x03L);
829 io_node->length = temp_register;
831 io_node->next = func->io_head;
832 func->io_head = io_node;
834 if (((base & 0x0BL) == 0x08)
835 && (save_command & 0x02)) {
836 // prefetchable memory base
837 temp_register = base & 0xFFFFFFF0;
838 temp_register = (~temp_register) + 1;
840 p_mem_node = kmalloc(sizeof(*p_mem_node),
845 p_mem_node->base = save_base & (~0x0FL);
846 p_mem_node->length = temp_register;
848 p_mem_node->next = func->p_mem_head;
849 func->p_mem_head = p_mem_node;
851 if (((base & 0x0BL) == 0x00)
852 && (save_command & 0x02)) {
853 // prefetchable memory base
854 temp_register = base & 0xFFFFFFF0;
855 temp_register = (~temp_register) + 1;
857 mem_node = kmalloc(sizeof(*mem_node),
862 mem_node->base = save_base & (~0x0FL);
863 mem_node->length = temp_register;
865 mem_node->next = func->mem_head;
866 func->mem_head = mem_node;
870 } // End of base register loop
871 } else if ((header_type & 0x7F) == 0x00) { // Standard header
872 // Figure out IO and memory base lengths
873 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
874 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
876 temp_register = 0xFFFFFFFF;
877 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
878 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
880 temp_register = base;
882 if (base) { // If this register is implemented
883 if (((base & 0x03L) == 0x01)
884 && (save_command & 0x01)) {
886 // set temp_register = amount of IO space requested
887 temp_register = base & 0xFFFFFFFE;
888 temp_register = (~temp_register) + 1;
890 io_node = kmalloc(sizeof(*io_node),
895 io_node->base = save_base & (~0x01L);
896 io_node->length = temp_register;
898 io_node->next = func->io_head;
899 func->io_head = io_node;
901 if (((base & 0x0BL) == 0x08)
902 && (save_command & 0x02)) {
903 // prefetchable memory base
904 temp_register = base & 0xFFFFFFF0;
905 temp_register = (~temp_register) + 1;
907 p_mem_node = kmalloc(sizeof(*p_mem_node),
912 p_mem_node->base = save_base & (~0x0FL);
913 p_mem_node->length = temp_register;
915 p_mem_node->next = func->p_mem_head;
916 func->p_mem_head = p_mem_node;
918 if (((base & 0x0BL) == 0x00)
919 && (save_command & 0x02)) {
920 // prefetchable memory base
921 temp_register = base & 0xFFFFFFF0;
922 temp_register = (~temp_register) + 1;
924 mem_node = kmalloc(sizeof(*mem_node),
929 mem_node->base = save_base & (~0x0FL);
930 mem_node->length = temp_register;
932 mem_node->next = func->mem_head;
933 func->mem_head = mem_node;
937 } // End of base register loop
938 } else { // Some other unknown header type
941 // find the next device in this slot
942 func = cpqhp_slot_find(func->bus, func->device, index++);
950 * cpqhp_configure_board
952 * Copies saved configuration information to one slot.
953 * this is called recursively for bridge devices.
954 * this is for hot plug REPLACE!
956 * returns 0 if success
958 int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
964 struct pci_func *next;
968 struct pci_bus *pci_bus = ctrl->pci_bus;
971 func = cpqhp_slot_find(func->bus, func->device, index++);
973 while (func != NULL) {
974 pci_bus->number = func->bus;
975 devfn = PCI_DEVFN(func->device, func->function);
977 // Start at the top of config space so that the control
978 // registers are programmed last
979 for (cloop = 0x3C; cloop > 0; cloop -= 4) {
980 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
983 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
985 // If this is a bridge device, restore subordinate devices
986 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
987 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
989 sub_bus = (int) secondary_bus;
991 next = cpqhp_slot_list[sub_bus];
993 while (next != NULL) {
994 rc = cpqhp_configure_board(ctrl, next);
1002 // Check all the base Address Registers to make sure
1003 // they are the same. If not, the board is different.
1005 for (cloop = 16; cloop < 40; cloop += 4) {
1006 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
1008 if (temp != func->config_space[cloop >> 2]) {
1009 dbg("Config space compare failure!!! offset = %x\n", cloop);
1010 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1011 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1017 func->configured = 1;
1019 func = cpqhp_slot_find(func->bus, func->device, index++);
1027 * cpqhp_valid_replace
1029 * this function checks to see if a board is the same as the
1030 * one it is replacing. this check will detect if the device's
1031 * vendor or device id's are the same
1033 * returns 0 if the board is the same nonzero otherwise
1035 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1041 u32 temp_register = 0;
1044 struct pci_func *next;
1046 struct pci_bus *pci_bus = ctrl->pci_bus;
1049 if (!func->is_a_board)
1050 return(ADD_NOT_SUPPORTED);
1052 func = cpqhp_slot_find(func->bus, func->device, index++);
1054 while (func != NULL) {
1055 pci_bus->number = func->bus;
1056 devfn = PCI_DEVFN(func->device, func->function);
1058 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1060 // No adapter present
1061 if (temp_register == 0xFFFFFFFF)
1062 return(NO_ADAPTER_PRESENT);
1064 if (temp_register != func->config_space[0])
1065 return(ADAPTER_NOT_SAME);
1067 // Check for same revision number and class code
1068 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1070 // Adapter not the same
1071 if (temp_register != func->config_space[0x08 >> 2])
1072 return(ADAPTER_NOT_SAME);
1075 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1077 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
1078 // In order to continue checking, we must program the
1079 // bus registers in the bridge to respond to accesses
1080 // for it's subordinate bus(es)
1082 temp_register = func->config_space[0x18 >> 2];
1083 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1085 secondary_bus = (temp_register >> 8) & 0xFF;
1087 next = cpqhp_slot_list[secondary_bus];
1089 while (next != NULL) {
1090 rc = cpqhp_valid_replace(ctrl, next);
1098 // Check to see if it is a standard config header
1099 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1100 // Check subsystem vendor and ID
1101 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1103 if (temp_register != func->config_space[0x2C >> 2]) {
1104 // If it's a SMART-2 and the register isn't filled
1105 // in, ignore the difference because
1106 // they just have an old rev of the firmware
1108 if (!((func->config_space[0] == 0xAE100E11)
1109 && (temp_register == 0x00L)))
1110 return(ADAPTER_NOT_SAME);
1112 // Figure out IO and memory base lengths
1113 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1114 temp_register = 0xFFFFFFFF;
1115 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1116 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
1117 if (base) { // If this register is implemented
1120 // set base = amount of IO space requested
1121 base = base & 0xFFFFFFFE;
1127 base = base & 0xFFFFFFF0;
1137 // Check information in slot structure
1138 if (func->base_length[(cloop - 0x10) >> 2] != base)
1139 return(ADAPTER_NOT_SAME);
1141 if (func->base_type[(cloop - 0x10) >> 2] != type)
1142 return(ADAPTER_NOT_SAME);
1144 } // End of base register loop
1146 } // End of (type 0 config space) else
1148 // this is not a type 0 or 1 config space header so
1149 // we don't know how to do it
1150 return(DEVICE_TYPE_NOT_SUPPORTED);
1153 // Get the next function
1154 func = cpqhp_slot_find(func->bus, func->device, index++);
1163 * cpqhp_find_available_resources
1165 * Finds available memory, IO, and IRQ resources for programming
1166 * devices which may be added to the system
1167 * this function is for hot plug ADD!
1169 * returns 0 if success
1171 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1176 void __iomem *one_slot;
1177 void __iomem *rom_resource_table;
1178 struct pci_func *func = NULL;
1181 struct pci_resource *mem_node;
1182 struct pci_resource *p_mem_node;
1183 struct pci_resource *io_node;
1184 struct pci_resource *bus_node;
1186 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1187 dbg("rom_resource_table = %p\n", rom_resource_table);
1189 if (rom_resource_table == NULL) {
1192 // Sum all resources and setup resource maps
1193 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1194 dbg("unused_IRQ = %x\n", unused_IRQ);
1197 while (unused_IRQ) {
1198 if (unused_IRQ & 1) {
1199 cpqhp_disk_irq = temp;
1202 unused_IRQ = unused_IRQ >> 1;
1206 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1207 unused_IRQ = unused_IRQ >> 1;
1210 while (unused_IRQ) {
1211 if (unused_IRQ & 1) {
1212 cpqhp_nic_irq = temp;
1215 unused_IRQ = unused_IRQ >> 1;
1219 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1220 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1224 if (!cpqhp_nic_irq) {
1225 cpqhp_nic_irq = ctrl->cfgspc_irq;
1228 if (!cpqhp_disk_irq) {
1229 cpqhp_disk_irq = ctrl->cfgspc_irq;
1232 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1234 rc = compaq_nvram_load(rom_start, ctrl);
1238 one_slot = rom_resource_table + sizeof (struct hrt);
1240 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1241 dbg("number_of_entries = %d\n", i);
1243 if (!readb(one_slot + SECONDARY_BUS))
1246 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1248 while (i && readb(one_slot + SECONDARY_BUS)) {
1249 u8 dev_func = readb(one_slot + DEV_FUNC);
1250 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1251 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1252 u8 max_bus = readb(one_slot + MAX_BUS);
1253 u16 io_base = readw(one_slot + IO_BASE);
1254 u16 io_length = readw(one_slot + IO_LENGTH);
1255 u16 mem_base = readw(one_slot + MEM_BASE);
1256 u16 mem_length = readw(one_slot + MEM_LENGTH);
1257 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1258 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1260 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1261 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1262 primary_bus, secondary_bus, max_bus);
1264 // If this entry isn't for our controller's bus, ignore it
1265 if (primary_bus != ctrl->bus) {
1267 one_slot += sizeof (struct slot_rt);
1270 // find out if this entry is for an occupied slot
1271 ctrl->pci_bus->number = primary_bus;
1272 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1273 dbg("temp_D_word = %x\n", temp_dword);
1275 if (temp_dword != 0xFFFFFFFF) {
1277 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1279 while (func && (func->function != (dev_func & 0x07))) {
1280 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1281 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1284 // If we can't find a match, skip this table entry
1287 one_slot += sizeof (struct slot_rt);
1290 // this may not work and shouldn't be used
1291 if (secondary_bus != primary_bus)
1303 // If we've got a valid IO base, use it
1305 temp_dword = io_base + io_length;
1307 if ((io_base) && (temp_dword < 0x10000)) {
1308 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1312 io_node->base = io_base;
1313 io_node->length = io_length;
1315 dbg("found io_node(base, length) = %x, %x\n",
1316 io_node->base, io_node->length);
1317 dbg("populated slot =%d \n", populated_slot);
1318 if (!populated_slot) {
1319 io_node->next = ctrl->io_head;
1320 ctrl->io_head = io_node;
1322 io_node->next = func->io_head;
1323 func->io_head = io_node;
1327 // If we've got a valid memory base, use it
1328 temp_dword = mem_base + mem_length;
1329 if ((mem_base) && (temp_dword < 0x10000)) {
1330 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1334 mem_node->base = mem_base << 16;
1336 mem_node->length = mem_length << 16;
1338 dbg("found mem_node(base, length) = %x, %x\n",
1339 mem_node->base, mem_node->length);
1340 dbg("populated slot =%d \n", populated_slot);
1341 if (!populated_slot) {
1342 mem_node->next = ctrl->mem_head;
1343 ctrl->mem_head = mem_node;
1345 mem_node->next = func->mem_head;
1346 func->mem_head = mem_node;
1350 // If we've got a valid prefetchable memory base, and
1351 // the base + length isn't greater than 0xFFFF
1352 temp_dword = pre_mem_base + pre_mem_length;
1353 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1354 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1358 p_mem_node->base = pre_mem_base << 16;
1360 p_mem_node->length = pre_mem_length << 16;
1361 dbg("found p_mem_node(base, length) = %x, %x\n",
1362 p_mem_node->base, p_mem_node->length);
1363 dbg("populated slot =%d \n", populated_slot);
1365 if (!populated_slot) {
1366 p_mem_node->next = ctrl->p_mem_head;
1367 ctrl->p_mem_head = p_mem_node;
1369 p_mem_node->next = func->p_mem_head;
1370 func->p_mem_head = p_mem_node;
1374 // If we've got a valid bus number, use it
1375 // The second condition is to ignore bus numbers on
1376 // populated slots that don't have PCI-PCI bridges
1377 if (secondary_bus && (secondary_bus != primary_bus)) {
1378 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1382 bus_node->base = secondary_bus;
1383 bus_node->length = max_bus - secondary_bus + 1;
1384 dbg("found bus_node(base, length) = %x, %x\n",
1385 bus_node->base, bus_node->length);
1386 dbg("populated slot =%d \n", populated_slot);
1387 if (!populated_slot) {
1388 bus_node->next = ctrl->bus_head;
1389 ctrl->bus_head = bus_node;
1391 bus_node->next = func->bus_head;
1392 func->bus_head = bus_node;
1397 one_slot += sizeof (struct slot_rt);
1400 // If all of the following fail, we don't have any resources for
1403 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1404 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1405 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1406 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1413 * cpqhp_return_board_resources
1415 * this routine returns all resources allocated to a board to
1416 * the available pool.
1418 * returns 0 if success
1420 int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1423 struct pci_resource *node;
1424 struct pci_resource *t_node;
1425 dbg("%s\n", __FUNCTION__);
1430 node = func->io_head;
1431 func->io_head = NULL;
1433 t_node = node->next;
1434 return_resource(&(resources->io_head), node);
1438 node = func->mem_head;
1439 func->mem_head = NULL;
1441 t_node = node->next;
1442 return_resource(&(resources->mem_head), node);
1446 node = func->p_mem_head;
1447 func->p_mem_head = NULL;
1449 t_node = node->next;
1450 return_resource(&(resources->p_mem_head), node);
1454 node = func->bus_head;
1455 func->bus_head = NULL;
1457 t_node = node->next;
1458 return_resource(&(resources->bus_head), node);
1462 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1463 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1464 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1465 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1472 * cpqhp_destroy_resource_list
1474 * Puts node back in the resource list pointed to by head
1476 void cpqhp_destroy_resource_list (struct resource_lists * resources)
1478 struct pci_resource *res, *tres;
1480 res = resources->io_head;
1481 resources->io_head = NULL;
1489 res = resources->mem_head;
1490 resources->mem_head = NULL;
1498 res = resources->p_mem_head;
1499 resources->p_mem_head = NULL;
1507 res = resources->bus_head;
1508 resources->bus_head = NULL;
1519 * cpqhp_destroy_board_resources
1521 * Puts node back in the resource list pointed to by head
1523 void cpqhp_destroy_board_resources (struct pci_func * func)
1525 struct pci_resource *res, *tres;
1527 res = func->io_head;
1528 func->io_head = NULL;
1536 res = func->mem_head;
1537 func->mem_head = NULL;
1545 res = func->p_mem_head;
1546 func->p_mem_head = NULL;
1554 res = func->bus_head;
1555 func->bus_head = NULL;