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/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/workqueue.h>
35 #include <linux/proc_fs.h>
36 #include <linux/pci.h>
39 #include "cpqphp_nvram.h"
40 #include "../../../arch/i386/pci/pci.h" /* horrible hack showing how processor dependent we are... */
46 static u16 unused_IRQ;
49 * detect_HRT_floating_pointer
51 * find the Hot Plug Resource Table in the specified region of memory.
54 static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
58 u8 temp1, temp2, temp3, temp4;
61 endp = (end - sizeof(struct hrt) + 1);
63 for (fp = begin; fp <= endp; fp += 16) {
64 temp1 = readb(fp + SIG0);
65 temp2 = readb(fp + SIG1);
66 temp3 = readb(fp + SIG2);
67 temp4 = readb(fp + SIG3);
80 dbg("Discovered Hotplug Resource Table at %p\n", fp);
85 int cpqhp_configure_device (struct controller* ctrl, struct pci_func* func)
88 struct pci_bus *child;
91 if (func->pci_dev == NULL)
92 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
94 /* No pci device, we need to create it then */
95 if (func->pci_dev == NULL) {
96 dbg("INFO: pci_dev still null\n");
98 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
100 pci_bus_add_devices(ctrl->pci_dev->bus);
102 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
103 if (func->pci_dev == NULL) {
104 dbg("ERROR: pci_dev still null\n");
109 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
110 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
111 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
112 pci_do_scan_bus(child);
119 int cpqhp_unconfigure_device(struct pci_func* func)
123 dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus, func->device, func->function);
125 for (j=0; j<8 ; j++) {
126 struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
128 pci_remove_bus_device(temp);
133 static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
137 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
139 if (vendID == 0xffffffff)
141 return pci_bus_read_config_dword (bus, devfn, offset, value);
148 * @bus_num: bus number of PCI device
149 * @dev_num: device number of PCI device
150 * @slot: pointer to u8 where slot number will be returned
152 int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
156 if (cpqhp_legacy_mode) {
157 struct pci_dev *fakedev;
158 struct pci_bus *fakebus;
161 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
162 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
163 if (!fakedev || !fakebus) {
169 fakedev->devfn = dev_num << 3;
170 fakedev->bus = fakebus;
171 fakebus->number = bus_num;
172 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
173 __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
174 rc = pcibios_set_irq_routing(fakedev, int_pin - 0x0a, irq_num);
177 dbg("%s: rc %d\n", __FUNCTION__, rc);
181 // set the Edge Level Control Register (ELCR)
182 temp_word = inb(0x4d0);
183 temp_word |= inb(0x4d1) << 8;
185 temp_word |= 0x01 << irq_num;
187 // This should only be for x86 as it sets the Edge Level Control Register
188 outb((u8) (temp_word & 0xFF), 0x4d0);
189 outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
198 * WTF??? This function isn't in the code, yet a function calls it, but the
199 * compiler optimizes it away? strange. Here as a placeholder to keep the
202 static int PCI_ScanBusNonBridge (u8 bus, u8 device)
207 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
213 ctrl->pci_bus->number = bus_num;
215 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
216 //Scan for access first
217 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
219 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
220 //Yep we got one. Not a bridge ?
221 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
227 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
228 //Scan for access first
229 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
231 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
232 //Yep we got one. bridge ?
233 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
234 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
235 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
236 if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
245 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
247 struct irq_routing_table *PCIIRQRoutingInfoLength;
252 u8 tbus, tdevice, tslot;
254 PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
255 if (!PCIIRQRoutingInfoLength)
258 len = (PCIIRQRoutingInfoLength->size -
259 sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
260 // Make sure I got at least one entry
262 if (PCIIRQRoutingInfoLength != NULL)
263 kfree(PCIIRQRoutingInfoLength );
267 for (loop = 0; loop < len; ++loop) {
268 tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
269 tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn;
270 tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
275 ctrl->pci_bus->number = tbus;
276 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
277 if (!nobridge || (work == 0xffffffff)) {
278 if (PCIIRQRoutingInfoLength != NULL)
279 kfree(PCIIRQRoutingInfoLength );
283 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
284 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
285 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
287 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
288 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
289 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
290 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
292 if (PCIIRQRoutingInfoLength != NULL)
293 kfree(PCIIRQRoutingInfoLength );
297 if (PCIIRQRoutingInfoLength != NULL)
298 kfree(PCIIRQRoutingInfoLength );
304 if (PCIIRQRoutingInfoLength != NULL)
305 kfree(PCIIRQRoutingInfoLength );
310 int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
312 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0); //plain (bridges allowed)
316 /* More PCI configuration routines; this time centered around hotplug controller */
322 * Reads configuration for all slots in a PCI bus and saves info.
324 * Note: For non-hot plug busses, the slot # saved is the device #
326 * returns 0 if success
328 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
335 struct pci_func *new_slot;
347 // Decide which slots are supported
350 //*********************************
351 // is_hot_plug is the slot mask
352 //*********************************
353 FirstSupported = is_hot_plug >> 4;
354 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
357 LastSupported = 0x1F;
360 // Save PCI configuration space for all devices in supported slots
361 ctrl->pci_bus->number = busnumber;
362 for (device = FirstSupported; device <= LastSupported; device++) {
364 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
366 if (ID != 0xFFFFFFFF) { // device in slot
367 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
371 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
375 // If multi-function device, set max_functions to 8
376 if (header_type & 0x80)
386 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // P-P Bridge
387 // Recurse the subordinate bus
388 // get the subordinate bus number
389 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
393 sub_bus = (int) secondary_bus;
395 // Save secondary bus cfg spc
396 // with this recursive call.
397 rc = cpqhp_save_config(ctrl, sub_bus, 0);
400 ctrl->pci_bus->number = busnumber;
405 new_slot = cpqhp_slot_find(busnumber, device, index++);
407 (new_slot->function != (u8) function))
408 new_slot = cpqhp_slot_find(busnumber, device, index++);
411 // Setup slot structure.
412 new_slot = cpqhp_slot_create(busnumber);
414 if (new_slot == NULL)
418 new_slot->bus = (u8) busnumber;
419 new_slot->device = (u8) device;
420 new_slot->function = (u8) function;
421 new_slot->is_a_board = 1;
422 new_slot->switch_save = 0x10;
423 // In case of unsupported board
424 new_slot->status = DevError;
425 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
427 for (cloop = 0; cloop < 0x20; cloop++) {
428 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
437 // this loop skips to the next present function
438 // reading in Class Code and Header type.
440 while ((function < max_functions)&&(!stop_it)) {
441 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
442 if (ID == 0xFFFFFFFF) { // nothing there.
444 } else { // Something there
445 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
449 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
457 } while (function < max_functions);
458 } // End of IF (device in slot?)
459 else if (is_hot_plug) {
460 // Setup slot structure with entry for empty slot
461 new_slot = cpqhp_slot_create(busnumber);
463 if (new_slot == NULL) {
467 new_slot->bus = (u8) busnumber;
468 new_slot->device = (u8) device;
469 new_slot->function = 0;
470 new_slot->is_a_board = 0;
471 new_slot->presence_save = 0;
472 new_slot->switch_save = 0;
481 * cpqhp_save_slot_config
483 * Saves configuration info for all PCI devices in a given slot
484 * including subordinate busses.
486 * returns 0 if success
488 int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
503 ctrl->pci_bus->number = new_slot->bus;
504 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
506 if (ID != 0xFFFFFFFF) { // device in slot
507 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
508 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
510 if (header_type & 0x80) // Multi-function device
518 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
519 // Recurse the subordinate bus
520 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
522 sub_bus = (int) secondary_bus;
524 // Save the config headers for the secondary bus.
525 rc = cpqhp_save_config(ctrl, sub_bus, 0);
528 ctrl->pci_bus->number = new_slot->bus;
532 new_slot->status = 0;
534 for (cloop = 0; cloop < 0x20; cloop++) {
535 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
542 // this loop skips to the next present function
543 // reading in the Class Code and the Header type.
545 while ((function < max_functions) && (!stop_it)) {
546 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
548 if (ID == 0xFFFFFFFF) { // nothing there.
550 } else { // Something there
551 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
553 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
559 } while (function < max_functions);
560 } // End of IF (device in slot?)
570 * cpqhp_save_base_addr_length
572 * Saves the length of all base address registers for the
573 * specified slot. this is for hot plug REPLACE
575 * returns 0 if success
577 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
587 struct pci_func *next;
589 struct pci_bus *pci_bus = ctrl->pci_bus;
592 func = cpqhp_slot_find(func->bus, func->device, index++);
594 while (func != NULL) {
595 pci_bus->number = func->bus;
596 devfn = PCI_DEVFN(func->device, func->function);
599 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
601 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
603 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
605 sub_bus = (int) secondary_bus;
607 next = cpqhp_slot_list[sub_bus];
609 while (next != NULL) {
610 rc = cpqhp_save_base_addr_length(ctrl, next);
616 pci_bus->number = func->bus;
618 //FIXME: this loop is duplicated in the non-bridge case. The two could be rolled together
619 // Figure out IO and memory base lengths
620 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
621 temp_register = 0xFFFFFFFF;
622 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
623 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
625 if (base) { // If this register is implemented
628 // set base = amount of IO space requested
629 base = base & 0xFFFFFFFE;
635 base = base & 0xFFFFFFF0;
645 // Save information in slot structure
646 func->base_length[(cloop - 0x10) >> 2] =
648 func->base_type[(cloop - 0x10) >> 2] = type;
650 } // End of base register loop
653 } else if ((header_type & 0x7F) == 0x00) { // PCI-PCI Bridge
654 // Figure out IO and memory base lengths
655 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
656 temp_register = 0xFFFFFFFF;
657 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
658 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
660 if (base) { // If this register is implemented
663 // base = amount of IO space requested
664 base = base & 0xFFFFFFFE;
670 // base = amount of memory space requested
671 base = base & 0xFFFFFFF0;
681 // Save information in slot structure
682 func->base_length[(cloop - 0x10) >> 2] = base;
683 func->base_type[(cloop - 0x10) >> 2] = type;
685 } // End of base register loop
687 } else { // Some other unknown header type
690 // find the next device in this slot
691 func = cpqhp_slot_find(func->bus, func->device, index++);
699 * cpqhp_save_used_resources
701 * Stores used resource information for existing boards. this is
702 * for boards that were in the system when this driver was loaded.
703 * this function is for hot plug ADD
705 * returns 0 if success
707 int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
723 struct pci_resource *mem_node;
724 struct pci_resource *p_mem_node;
725 struct pci_resource *io_node;
726 struct pci_resource *bus_node;
727 struct pci_bus *pci_bus = ctrl->pci_bus;
730 func = cpqhp_slot_find(func->bus, func->device, index++);
732 while ((func != NULL) && func->is_a_board) {
733 pci_bus->number = func->bus;
734 devfn = PCI_DEVFN(func->device, func->function);
736 // Save the command register
737 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
741 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
744 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
746 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
747 // Clear Bridge Control Register
749 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
750 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
751 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
753 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
757 bus_node->base = secondary_bus;
758 bus_node->length = temp_byte - secondary_bus + 1;
760 bus_node->next = func->bus_head;
761 func->bus_head = bus_node;
763 // Save IO base and Limit registers
764 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
765 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
767 if ((b_base <= b_length) && (save_command & 0x01)) {
768 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
772 io_node->base = (b_base & 0xF0) << 8;
773 io_node->length = (b_length - b_base + 0x10) << 8;
775 io_node->next = func->io_head;
776 func->io_head = io_node;
779 // Save memory base and Limit registers
780 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
781 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
783 if ((w_base <= w_length) && (save_command & 0x02)) {
784 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
788 mem_node->base = w_base << 16;
789 mem_node->length = (w_length - w_base + 0x10) << 16;
791 mem_node->next = func->mem_head;
792 func->mem_head = mem_node;
795 // Save prefetchable memory base and Limit registers
796 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
797 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
799 if ((w_base <= w_length) && (save_command & 0x02)) {
800 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
804 p_mem_node->base = w_base << 16;
805 p_mem_node->length = (w_length - w_base + 0x10) << 16;
807 p_mem_node->next = func->p_mem_head;
808 func->p_mem_head = p_mem_node;
810 // Figure out IO and memory base lengths
811 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
812 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
814 temp_register = 0xFFFFFFFF;
815 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
816 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
818 temp_register = base;
820 if (base) { // If this register is implemented
821 if (((base & 0x03L) == 0x01)
822 && (save_command & 0x01)) {
824 // set temp_register = amount of IO space requested
825 temp_register = base & 0xFFFFFFFE;
826 temp_register = (~temp_register) + 1;
828 io_node = kmalloc(sizeof(*io_node),
834 save_base & (~0x03L);
835 io_node->length = temp_register;
837 io_node->next = func->io_head;
838 func->io_head = io_node;
840 if (((base & 0x0BL) == 0x08)
841 && (save_command & 0x02)) {
842 // prefetchable memory base
843 temp_register = base & 0xFFFFFFF0;
844 temp_register = (~temp_register) + 1;
846 p_mem_node = kmalloc(sizeof(*p_mem_node),
851 p_mem_node->base = save_base & (~0x0FL);
852 p_mem_node->length = temp_register;
854 p_mem_node->next = func->p_mem_head;
855 func->p_mem_head = p_mem_node;
857 if (((base & 0x0BL) == 0x00)
858 && (save_command & 0x02)) {
859 // prefetchable memory base
860 temp_register = base & 0xFFFFFFF0;
861 temp_register = (~temp_register) + 1;
863 mem_node = kmalloc(sizeof(*mem_node),
868 mem_node->base = save_base & (~0x0FL);
869 mem_node->length = temp_register;
871 mem_node->next = func->mem_head;
872 func->mem_head = mem_node;
876 } // End of base register loop
877 } else if ((header_type & 0x7F) == 0x00) { // Standard header
878 // Figure out IO and memory base lengths
879 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
880 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
882 temp_register = 0xFFFFFFFF;
883 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
884 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
886 temp_register = base;
888 if (base) { // If this register is implemented
889 if (((base & 0x03L) == 0x01)
890 && (save_command & 0x01)) {
892 // set temp_register = amount of IO space requested
893 temp_register = base & 0xFFFFFFFE;
894 temp_register = (~temp_register) + 1;
896 io_node = kmalloc(sizeof(*io_node),
901 io_node->base = save_base & (~0x01L);
902 io_node->length = temp_register;
904 io_node->next = func->io_head;
905 func->io_head = io_node;
907 if (((base & 0x0BL) == 0x08)
908 && (save_command & 0x02)) {
909 // prefetchable memory base
910 temp_register = base & 0xFFFFFFF0;
911 temp_register = (~temp_register) + 1;
913 p_mem_node = kmalloc(sizeof(*p_mem_node),
918 p_mem_node->base = save_base & (~0x0FL);
919 p_mem_node->length = temp_register;
921 p_mem_node->next = func->p_mem_head;
922 func->p_mem_head = p_mem_node;
924 if (((base & 0x0BL) == 0x00)
925 && (save_command & 0x02)) {
926 // prefetchable memory base
927 temp_register = base & 0xFFFFFFF0;
928 temp_register = (~temp_register) + 1;
930 mem_node = kmalloc(sizeof(*mem_node),
935 mem_node->base = save_base & (~0x0FL);
936 mem_node->length = temp_register;
938 mem_node->next = func->mem_head;
939 func->mem_head = mem_node;
943 } // End of base register loop
944 } else { // Some other unknown header type
947 // find the next device in this slot
948 func = cpqhp_slot_find(func->bus, func->device, index++);
956 * cpqhp_configure_board
958 * Copies saved configuration information to one slot.
959 * this is called recursively for bridge devices.
960 * this is for hot plug REPLACE!
962 * returns 0 if success
964 int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
970 struct pci_func *next;
974 struct pci_bus *pci_bus = ctrl->pci_bus;
977 func = cpqhp_slot_find(func->bus, func->device, index++);
979 while (func != NULL) {
980 pci_bus->number = func->bus;
981 devfn = PCI_DEVFN(func->device, func->function);
983 // Start at the top of config space so that the control
984 // registers are programmed last
985 for (cloop = 0x3C; cloop > 0; cloop -= 4) {
986 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
989 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
991 // If this is a bridge device, restore subordinate devices
992 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
993 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
995 sub_bus = (int) secondary_bus;
997 next = cpqhp_slot_list[sub_bus];
999 while (next != NULL) {
1000 rc = cpqhp_configure_board(ctrl, next);
1008 // Check all the base Address Registers to make sure
1009 // they are the same. If not, the board is different.
1011 for (cloop = 16; cloop < 40; cloop += 4) {
1012 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
1014 if (temp != func->config_space[cloop >> 2]) {
1015 dbg("Config space compare failure!!! offset = %x\n", cloop);
1016 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1017 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1023 func->configured = 1;
1025 func = cpqhp_slot_find(func->bus, func->device, index++);
1033 * cpqhp_valid_replace
1035 * this function checks to see if a board is the same as the
1036 * one it is replacing. this check will detect if the device's
1037 * vendor or device id's are the same
1039 * returns 0 if the board is the same nonzero otherwise
1041 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1047 u32 temp_register = 0;
1050 struct pci_func *next;
1052 struct pci_bus *pci_bus = ctrl->pci_bus;
1055 if (!func->is_a_board)
1056 return(ADD_NOT_SUPPORTED);
1058 func = cpqhp_slot_find(func->bus, func->device, index++);
1060 while (func != NULL) {
1061 pci_bus->number = func->bus;
1062 devfn = PCI_DEVFN(func->device, func->function);
1064 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1066 // No adapter present
1067 if (temp_register == 0xFFFFFFFF)
1068 return(NO_ADAPTER_PRESENT);
1070 if (temp_register != func->config_space[0])
1071 return(ADAPTER_NOT_SAME);
1073 // Check for same revision number and class code
1074 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1076 // Adapter not the same
1077 if (temp_register != func->config_space[0x08 >> 2])
1078 return(ADAPTER_NOT_SAME);
1081 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1083 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
1084 // In order to continue checking, we must program the
1085 // bus registers in the bridge to respond to accesses
1086 // for it's subordinate bus(es)
1088 temp_register = func->config_space[0x18 >> 2];
1089 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1091 secondary_bus = (temp_register >> 8) & 0xFF;
1093 next = cpqhp_slot_list[secondary_bus];
1095 while (next != NULL) {
1096 rc = cpqhp_valid_replace(ctrl, next);
1104 // Check to see if it is a standard config header
1105 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1106 // Check subsystem vendor and ID
1107 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1109 if (temp_register != func->config_space[0x2C >> 2]) {
1110 // If it's a SMART-2 and the register isn't filled
1111 // in, ignore the difference because
1112 // they just have an old rev of the firmware
1114 if (!((func->config_space[0] == 0xAE100E11)
1115 && (temp_register == 0x00L)))
1116 return(ADAPTER_NOT_SAME);
1118 // Figure out IO and memory base lengths
1119 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1120 temp_register = 0xFFFFFFFF;
1121 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1122 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
1123 if (base) { // If this register is implemented
1126 // set base = amount of IO space requested
1127 base = base & 0xFFFFFFFE;
1133 base = base & 0xFFFFFFF0;
1143 // Check information in slot structure
1144 if (func->base_length[(cloop - 0x10) >> 2] != base)
1145 return(ADAPTER_NOT_SAME);
1147 if (func->base_type[(cloop - 0x10) >> 2] != type)
1148 return(ADAPTER_NOT_SAME);
1150 } // End of base register loop
1152 } // End of (type 0 config space) else
1154 // this is not a type 0 or 1 config space header so
1155 // we don't know how to do it
1156 return(DEVICE_TYPE_NOT_SUPPORTED);
1159 // Get the next function
1160 func = cpqhp_slot_find(func->bus, func->device, index++);
1169 * cpqhp_find_available_resources
1171 * Finds available memory, IO, and IRQ resources for programming
1172 * devices which may be added to the system
1173 * this function is for hot plug ADD!
1175 * returns 0 if success
1177 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1182 void __iomem *one_slot;
1183 void __iomem *rom_resource_table;
1184 struct pci_func *func = NULL;
1187 struct pci_resource *mem_node;
1188 struct pci_resource *p_mem_node;
1189 struct pci_resource *io_node;
1190 struct pci_resource *bus_node;
1192 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1193 dbg("rom_resource_table = %p\n", rom_resource_table);
1195 if (rom_resource_table == NULL) {
1198 // Sum all resources and setup resource maps
1199 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1200 dbg("unused_IRQ = %x\n", unused_IRQ);
1203 while (unused_IRQ) {
1204 if (unused_IRQ & 1) {
1205 cpqhp_disk_irq = temp;
1208 unused_IRQ = unused_IRQ >> 1;
1212 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1213 unused_IRQ = unused_IRQ >> 1;
1216 while (unused_IRQ) {
1217 if (unused_IRQ & 1) {
1218 cpqhp_nic_irq = temp;
1221 unused_IRQ = unused_IRQ >> 1;
1225 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1226 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1230 if (!cpqhp_nic_irq) {
1231 cpqhp_nic_irq = ctrl->cfgspc_irq;
1234 if (!cpqhp_disk_irq) {
1235 cpqhp_disk_irq = ctrl->cfgspc_irq;
1238 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1240 rc = compaq_nvram_load(rom_start, ctrl);
1244 one_slot = rom_resource_table + sizeof (struct hrt);
1246 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1247 dbg("number_of_entries = %d\n", i);
1249 if (!readb(one_slot + SECONDARY_BUS))
1252 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1254 while (i && readb(one_slot + SECONDARY_BUS)) {
1255 u8 dev_func = readb(one_slot + DEV_FUNC);
1256 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1257 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1258 u8 max_bus = readb(one_slot + MAX_BUS);
1259 u16 io_base = readw(one_slot + IO_BASE);
1260 u16 io_length = readw(one_slot + IO_LENGTH);
1261 u16 mem_base = readw(one_slot + MEM_BASE);
1262 u16 mem_length = readw(one_slot + MEM_LENGTH);
1263 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1264 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1266 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1267 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1268 primary_bus, secondary_bus, max_bus);
1270 // If this entry isn't for our controller's bus, ignore it
1271 if (primary_bus != ctrl->bus) {
1273 one_slot += sizeof (struct slot_rt);
1276 // find out if this entry is for an occupied slot
1277 ctrl->pci_bus->number = primary_bus;
1278 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1279 dbg("temp_D_word = %x\n", temp_dword);
1281 if (temp_dword != 0xFFFFFFFF) {
1283 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1285 while (func && (func->function != (dev_func & 0x07))) {
1286 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1287 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1290 // If we can't find a match, skip this table entry
1293 one_slot += sizeof (struct slot_rt);
1296 // this may not work and shouldn't be used
1297 if (secondary_bus != primary_bus)
1309 // If we've got a valid IO base, use it
1311 temp_dword = io_base + io_length;
1313 if ((io_base) && (temp_dword < 0x10000)) {
1314 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1318 io_node->base = io_base;
1319 io_node->length = io_length;
1321 dbg("found io_node(base, length) = %x, %x\n",
1322 io_node->base, io_node->length);
1323 dbg("populated slot =%d \n", populated_slot);
1324 if (!populated_slot) {
1325 io_node->next = ctrl->io_head;
1326 ctrl->io_head = io_node;
1328 io_node->next = func->io_head;
1329 func->io_head = io_node;
1333 // If we've got a valid memory base, use it
1334 temp_dword = mem_base + mem_length;
1335 if ((mem_base) && (temp_dword < 0x10000)) {
1336 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1340 mem_node->base = mem_base << 16;
1342 mem_node->length = mem_length << 16;
1344 dbg("found mem_node(base, length) = %x, %x\n",
1345 mem_node->base, mem_node->length);
1346 dbg("populated slot =%d \n", populated_slot);
1347 if (!populated_slot) {
1348 mem_node->next = ctrl->mem_head;
1349 ctrl->mem_head = mem_node;
1351 mem_node->next = func->mem_head;
1352 func->mem_head = mem_node;
1356 // If we've got a valid prefetchable memory base, and
1357 // the base + length isn't greater than 0xFFFF
1358 temp_dword = pre_mem_base + pre_mem_length;
1359 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1360 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1364 p_mem_node->base = pre_mem_base << 16;
1366 p_mem_node->length = pre_mem_length << 16;
1367 dbg("found p_mem_node(base, length) = %x, %x\n",
1368 p_mem_node->base, p_mem_node->length);
1369 dbg("populated slot =%d \n", populated_slot);
1371 if (!populated_slot) {
1372 p_mem_node->next = ctrl->p_mem_head;
1373 ctrl->p_mem_head = p_mem_node;
1375 p_mem_node->next = func->p_mem_head;
1376 func->p_mem_head = p_mem_node;
1380 // If we've got a valid bus number, use it
1381 // The second condition is to ignore bus numbers on
1382 // populated slots that don't have PCI-PCI bridges
1383 if (secondary_bus && (secondary_bus != primary_bus)) {
1384 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1388 bus_node->base = secondary_bus;
1389 bus_node->length = max_bus - secondary_bus + 1;
1390 dbg("found bus_node(base, length) = %x, %x\n",
1391 bus_node->base, bus_node->length);
1392 dbg("populated slot =%d \n", populated_slot);
1393 if (!populated_slot) {
1394 bus_node->next = ctrl->bus_head;
1395 ctrl->bus_head = bus_node;
1397 bus_node->next = func->bus_head;
1398 func->bus_head = bus_node;
1403 one_slot += sizeof (struct slot_rt);
1406 // If all of the following fail, we don't have any resources for
1409 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1410 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1411 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1412 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1419 * cpqhp_return_board_resources
1421 * this routine returns all resources allocated to a board to
1422 * the available pool.
1424 * returns 0 if success
1426 int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1429 struct pci_resource *node;
1430 struct pci_resource *t_node;
1431 dbg("%s\n", __FUNCTION__);
1436 node = func->io_head;
1437 func->io_head = NULL;
1439 t_node = node->next;
1440 return_resource(&(resources->io_head), node);
1444 node = func->mem_head;
1445 func->mem_head = NULL;
1447 t_node = node->next;
1448 return_resource(&(resources->mem_head), node);
1452 node = func->p_mem_head;
1453 func->p_mem_head = NULL;
1455 t_node = node->next;
1456 return_resource(&(resources->p_mem_head), node);
1460 node = func->bus_head;
1461 func->bus_head = NULL;
1463 t_node = node->next;
1464 return_resource(&(resources->bus_head), node);
1468 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1469 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1470 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1471 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1478 * cpqhp_destroy_resource_list
1480 * Puts node back in the resource list pointed to by head
1482 void cpqhp_destroy_resource_list (struct resource_lists * resources)
1484 struct pci_resource *res, *tres;
1486 res = resources->io_head;
1487 resources->io_head = NULL;
1495 res = resources->mem_head;
1496 resources->mem_head = NULL;
1504 res = resources->p_mem_head;
1505 resources->p_mem_head = NULL;
1513 res = resources->bus_head;
1514 resources->bus_head = NULL;
1525 * cpqhp_destroy_board_resources
1527 * Puts node back in the resource list pointed to by head
1529 void cpqhp_destroy_board_resources (struct pci_func * func)
1531 struct pci_resource *res, *tres;
1533 res = func->io_head;
1534 func->io_head = NULL;
1542 res = func->mem_head;
1543 func->mem_head = NULL;
1551 res = func->p_mem_head;
1552 func->p_mem_head = NULL;
1560 res = func->bus_head;
1561 func->bus_head = NULL;