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 *detect_HRT_floating_pointer(void *begin, void *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 struct pci_dev fakedev;
157 struct pci_bus fakebus;
159 if (cpqhp_legacy_mode) {
160 fakedev.devfn = dev_num << 3;
161 fakedev.bus = &fakebus;
162 fakebus.number = bus_num;
163 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
164 __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
165 rc = pcibios_set_irq_routing(&fakedev, int_pin - 0x0a, irq_num);
166 dbg("%s: rc %d\n", __FUNCTION__, rc);
170 // set the Edge Level Control Register (ELCR)
171 temp_word = inb(0x4d0);
172 temp_word |= inb(0x4d1) << 8;
174 temp_word |= 0x01 << irq_num;
176 // This should only be for x86 as it sets the Edge Level Control Register
177 outb((u8) (temp_word & 0xFF), 0x4d0);
178 outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
186 * WTF??? This function isn't in the code, yet a function calls it, but the
187 * compiler optimizes it away? strange. Here as a placeholder to keep the
190 static int PCI_ScanBusNonBridge (u8 bus, u8 device)
195 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
201 ctrl->pci_bus->number = bus_num;
203 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
204 //Scan for access first
205 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
207 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
208 //Yep we got one. Not a bridge ?
209 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
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 bridge bus_num %d dev_num %d\n", bus_num, tdevice);
220 //Yep we got one. bridge ?
221 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
222 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
223 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
224 if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
233 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
235 struct irq_routing_table *PCIIRQRoutingInfoLength;
240 u8 tbus, tdevice, tslot;
242 PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
243 if (!PCIIRQRoutingInfoLength)
246 len = (PCIIRQRoutingInfoLength->size -
247 sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
248 // Make sure I got at least one entry
250 if (PCIIRQRoutingInfoLength != NULL)
251 kfree(PCIIRQRoutingInfoLength );
255 for (loop = 0; loop < len; ++loop) {
256 tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
257 tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn;
258 tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
263 ctrl->pci_bus->number = tbus;
264 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
265 if (!nobridge || (work == 0xffffffff)) {
266 if (PCIIRQRoutingInfoLength != NULL)
267 kfree(PCIIRQRoutingInfoLength );
271 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
272 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
273 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
275 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
276 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
277 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
278 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
280 if (PCIIRQRoutingInfoLength != NULL)
281 kfree(PCIIRQRoutingInfoLength );
285 if (PCIIRQRoutingInfoLength != NULL)
286 kfree(PCIIRQRoutingInfoLength );
292 if (PCIIRQRoutingInfoLength != NULL)
293 kfree(PCIIRQRoutingInfoLength );
298 int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
300 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0); //plain (bridges allowed)
304 /* More PCI configuration routines; this time centered around hotplug controller */
310 * Reads configuration for all slots in a PCI bus and saves info.
312 * Note: For non-hot plug busses, the slot # saved is the device #
314 * returns 0 if success
316 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
323 struct pci_func *new_slot;
335 // Decide which slots are supported
338 //*********************************
339 // is_hot_plug is the slot mask
340 //*********************************
341 FirstSupported = is_hot_plug >> 4;
342 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
345 LastSupported = 0x1F;
348 // Save PCI configuration space for all devices in supported slots
349 ctrl->pci_bus->number = busnumber;
350 for (device = FirstSupported; device <= LastSupported; device++) {
352 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
354 if (ID != 0xFFFFFFFF) { // device in slot
355 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
359 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
363 // If multi-function device, set max_functions to 8
364 if (header_type & 0x80)
374 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // P-P Bridge
375 // Recurse the subordinate bus
376 // get the subordinate bus number
377 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
381 sub_bus = (int) secondary_bus;
383 // Save secondary bus cfg spc
384 // with this recursive call.
385 rc = cpqhp_save_config(ctrl, sub_bus, 0);
388 ctrl->pci_bus->number = busnumber;
393 new_slot = cpqhp_slot_find(busnumber, device, index++);
395 (new_slot->function != (u8) function))
396 new_slot = cpqhp_slot_find(busnumber, device, index++);
399 // Setup slot structure.
400 new_slot = cpqhp_slot_create(busnumber);
402 if (new_slot == NULL)
406 new_slot->bus = (u8) busnumber;
407 new_slot->device = (u8) device;
408 new_slot->function = (u8) function;
409 new_slot->is_a_board = 1;
410 new_slot->switch_save = 0x10;
411 // In case of unsupported board
412 new_slot->status = DevError;
413 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
415 for (cloop = 0; cloop < 0x20; cloop++) {
416 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
425 // this loop skips to the next present function
426 // reading in Class Code and Header type.
428 while ((function < max_functions)&&(!stop_it)) {
429 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
430 if (ID == 0xFFFFFFFF) { // nothing there.
432 } else { // Something there
433 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
437 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
445 } while (function < max_functions);
446 } // End of IF (device in slot?)
447 else if (is_hot_plug) {
448 // Setup slot structure with entry for empty slot
449 new_slot = cpqhp_slot_create(busnumber);
451 if (new_slot == NULL) {
455 new_slot->bus = (u8) busnumber;
456 new_slot->device = (u8) device;
457 new_slot->function = 0;
458 new_slot->is_a_board = 0;
459 new_slot->presence_save = 0;
460 new_slot->switch_save = 0;
469 * cpqhp_save_slot_config
471 * Saves configuration info for all PCI devices in a given slot
472 * including subordinate busses.
474 * returns 0 if success
476 int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
491 ctrl->pci_bus->number = new_slot->bus;
492 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
494 if (ID != 0xFFFFFFFF) { // device in slot
495 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
496 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
498 if (header_type & 0x80) // Multi-function device
506 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
507 // Recurse the subordinate bus
508 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
510 sub_bus = (int) secondary_bus;
512 // Save the config headers for the secondary bus.
513 rc = cpqhp_save_config(ctrl, sub_bus, 0);
516 ctrl->pci_bus->number = new_slot->bus;
520 new_slot->status = 0;
522 for (cloop = 0; cloop < 0x20; cloop++) {
523 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
530 // this loop skips to the next present function
531 // reading in the Class Code and the Header type.
533 while ((function < max_functions) && (!stop_it)) {
534 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
536 if (ID == 0xFFFFFFFF) { // nothing there.
538 } else { // Something there
539 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
541 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
547 } while (function < max_functions);
548 } // End of IF (device in slot?)
558 * cpqhp_save_base_addr_length
560 * Saves the length of all base address registers for the
561 * specified slot. this is for hot plug REPLACE
563 * returns 0 if success
565 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
575 struct pci_func *next;
577 struct pci_bus *pci_bus = ctrl->pci_bus;
580 func = cpqhp_slot_find(func->bus, func->device, index++);
582 while (func != NULL) {
583 pci_bus->number = func->bus;
584 devfn = PCI_DEVFN(func->device, func->function);
587 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
589 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
591 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
593 sub_bus = (int) secondary_bus;
595 next = cpqhp_slot_list[sub_bus];
597 while (next != NULL) {
598 rc = cpqhp_save_base_addr_length(ctrl, next);
604 pci_bus->number = func->bus;
606 //FIXME: this loop is duplicated in the non-bridge case. The two could be rolled together
607 // Figure out IO and memory base lengths
608 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
609 temp_register = 0xFFFFFFFF;
610 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
611 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
613 if (base) { // If this register is implemented
616 // set base = amount of IO space requested
617 base = base & 0xFFFFFFFE;
623 base = base & 0xFFFFFFF0;
633 // Save information in slot structure
634 func->base_length[(cloop - 0x10) >> 2] =
636 func->base_type[(cloop - 0x10) >> 2] = type;
638 } // End of base register loop
641 } else if ((header_type & 0x7F) == 0x00) { // PCI-PCI Bridge
642 // Figure out IO and memory base lengths
643 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
644 temp_register = 0xFFFFFFFF;
645 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
646 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
648 if (base) { // If this register is implemented
651 // base = amount of IO space requested
652 base = base & 0xFFFFFFFE;
658 // base = amount of memory space requested
659 base = base & 0xFFFFFFF0;
669 // Save information in slot structure
670 func->base_length[(cloop - 0x10) >> 2] = base;
671 func->base_type[(cloop - 0x10) >> 2] = type;
673 } // End of base register loop
675 } else { // Some other unknown header type
678 // find the next device in this slot
679 func = cpqhp_slot_find(func->bus, func->device, index++);
687 * cpqhp_save_used_resources
689 * Stores used resource information for existing boards. this is
690 * for boards that were in the system when this driver was loaded.
691 * this function is for hot plug ADD
693 * returns 0 if success
695 int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
711 struct pci_resource *mem_node;
712 struct pci_resource *p_mem_node;
713 struct pci_resource *io_node;
714 struct pci_resource *bus_node;
715 struct pci_bus *pci_bus = ctrl->pci_bus;
718 func = cpqhp_slot_find(func->bus, func->device, index++);
720 while ((func != NULL) && func->is_a_board) {
721 pci_bus->number = func->bus;
722 devfn = PCI_DEVFN(func->device, func->function);
724 // Save the command register
725 pci_bus_read_config_word (pci_bus, devfn, PCI_COMMAND, &save_command);
729 pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
732 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
734 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
735 // Clear Bridge Control Register
737 pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
738 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
739 pci_bus_read_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
741 bus_node =(struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
745 bus_node->base = secondary_bus;
746 bus_node->length = temp_byte - secondary_bus + 1;
748 bus_node->next = func->bus_head;
749 func->bus_head = bus_node;
751 // Save IO base and Limit registers
752 pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_BASE, &b_base);
753 pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_LIMIT, &b_length);
755 if ((b_base <= b_length) && (save_command & 0x01)) {
756 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
760 io_node->base = (b_base & 0xF0) << 8;
761 io_node->length = (b_length - b_base + 0x10) << 8;
763 io_node->next = func->io_head;
764 func->io_head = io_node;
767 // Save memory base and Limit registers
768 pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
769 pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
771 if ((w_base <= w_length) && (save_command & 0x02)) {
772 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
776 mem_node->base = w_base << 16;
777 mem_node->length = (w_length - w_base + 0x10) << 16;
779 mem_node->next = func->mem_head;
780 func->mem_head = mem_node;
783 // Save prefetchable memory base and Limit registers
784 pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
785 pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
787 if ((w_base <= w_length) && (save_command & 0x02)) {
788 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
792 p_mem_node->base = w_base << 16;
793 p_mem_node->length = (w_length - w_base + 0x10) << 16;
795 p_mem_node->next = func->p_mem_head;
796 func->p_mem_head = p_mem_node;
798 // Figure out IO and memory base lengths
799 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
800 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
802 temp_register = 0xFFFFFFFF;
803 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
804 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
806 temp_register = base;
808 if (base) { // If this register is implemented
809 if (((base & 0x03L) == 0x01)
810 && (save_command & 0x01)) {
812 // set temp_register = amount of IO space requested
813 temp_register = base & 0xFFFFFFFE;
814 temp_register = (~temp_register) + 1;
816 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
821 save_base & (~0x03L);
822 io_node->length = temp_register;
824 io_node->next = func->io_head;
825 func->io_head = io_node;
827 if (((base & 0x0BL) == 0x08)
828 && (save_command & 0x02)) {
829 // prefetchable memory base
830 temp_register = base & 0xFFFFFFF0;
831 temp_register = (~temp_register) + 1;
833 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
837 p_mem_node->base = save_base & (~0x0FL);
838 p_mem_node->length = temp_register;
840 p_mem_node->next = func->p_mem_head;
841 func->p_mem_head = p_mem_node;
843 if (((base & 0x0BL) == 0x00)
844 && (save_command & 0x02)) {
845 // prefetchable memory base
846 temp_register = base & 0xFFFFFFF0;
847 temp_register = (~temp_register) + 1;
849 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
853 mem_node->base = save_base & (~0x0FL);
854 mem_node->length = temp_register;
856 mem_node->next = func->mem_head;
857 func->mem_head = mem_node;
861 } // End of base register loop
862 } else if ((header_type & 0x7F) == 0x00) { // Standard header
863 // Figure out IO and memory base lengths
864 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
865 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
867 temp_register = 0xFFFFFFFF;
868 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
869 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
871 temp_register = base;
873 if (base) { // If this register is implemented
874 if (((base & 0x03L) == 0x01)
875 && (save_command & 0x01)) {
877 // set temp_register = amount of IO space requested
878 temp_register = base & 0xFFFFFFFE;
879 temp_register = (~temp_register) + 1;
881 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
885 io_node->base = save_base & (~0x01L);
886 io_node->length = temp_register;
888 io_node->next = func->io_head;
889 func->io_head = io_node;
891 if (((base & 0x0BL) == 0x08)
892 && (save_command & 0x02)) {
893 // prefetchable memory base
894 temp_register = base & 0xFFFFFFF0;
895 temp_register = (~temp_register) + 1;
897 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
901 p_mem_node->base = save_base & (~0x0FL);
902 p_mem_node->length = temp_register;
904 p_mem_node->next = func->p_mem_head;
905 func->p_mem_head = p_mem_node;
907 if (((base & 0x0BL) == 0x00)
908 && (save_command & 0x02)) {
909 // prefetchable memory base
910 temp_register = base & 0xFFFFFFF0;
911 temp_register = (~temp_register) + 1;
913 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
917 mem_node->base = save_base & (~0x0FL);
918 mem_node->length = temp_register;
920 mem_node->next = func->mem_head;
921 func->mem_head = mem_node;
925 } // End of base register loop
926 } else { // Some other unknown header type
929 // find the next device in this slot
930 func = cpqhp_slot_find(func->bus, func->device, index++);
938 * cpqhp_configure_board
940 * Copies saved configuration information to one slot.
941 * this is called recursively for bridge devices.
942 * this is for hot plug REPLACE!
944 * returns 0 if success
946 int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
952 struct pci_func *next;
956 struct pci_bus *pci_bus = ctrl->pci_bus;
959 func = cpqhp_slot_find(func->bus, func->device, index++);
961 while (func != NULL) {
962 pci_bus->number = func->bus;
963 devfn = PCI_DEVFN(func->device, func->function);
965 // Start at the top of config space so that the control
966 // registers are programmed last
967 for (cloop = 0x3C; cloop > 0; cloop -= 4) {
968 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
971 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
973 // If this is a bridge device, restore subordinate devices
974 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
975 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
977 sub_bus = (int) secondary_bus;
979 next = cpqhp_slot_list[sub_bus];
981 while (next != NULL) {
982 rc = cpqhp_configure_board(ctrl, next);
990 // Check all the base Address Registers to make sure
991 // they are the same. If not, the board is different.
993 for (cloop = 16; cloop < 40; cloop += 4) {
994 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
996 if (temp != func->config_space[cloop >> 2]) {
997 dbg("Config space compare failure!!! offset = %x\n", cloop);
998 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
999 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1005 func->configured = 1;
1007 func = cpqhp_slot_find(func->bus, func->device, index++);
1015 * cpqhp_valid_replace
1017 * this function checks to see if a board is the same as the
1018 * one it is replacing. this check will detect if the device's
1019 * vendor or device id's are the same
1021 * returns 0 if the board is the same nonzero otherwise
1023 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1029 u32 temp_register = 0;
1032 struct pci_func *next;
1034 struct pci_bus *pci_bus = ctrl->pci_bus;
1037 if (!func->is_a_board)
1038 return(ADD_NOT_SUPPORTED);
1040 func = cpqhp_slot_find(func->bus, func->device, index++);
1042 while (func != NULL) {
1043 pci_bus->number = func->bus;
1044 devfn = PCI_DEVFN(func->device, func->function);
1046 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1048 // No adapter present
1049 if (temp_register == 0xFFFFFFFF)
1050 return(NO_ADAPTER_PRESENT);
1052 if (temp_register != func->config_space[0])
1053 return(ADAPTER_NOT_SAME);
1055 // Check for same revision number and class code
1056 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1058 // Adapter not the same
1059 if (temp_register != func->config_space[0x08 >> 2])
1060 return(ADAPTER_NOT_SAME);
1063 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1065 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
1066 // In order to continue checking, we must program the
1067 // bus registers in the bridge to respond to accesses
1068 // for it's subordinate bus(es)
1070 temp_register = func->config_space[0x18 >> 2];
1071 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1073 secondary_bus = (temp_register >> 8) & 0xFF;
1075 next = cpqhp_slot_list[secondary_bus];
1077 while (next != NULL) {
1078 rc = cpqhp_valid_replace(ctrl, next);
1086 // Check to see if it is a standard config header
1087 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1088 // Check subsystem vendor and ID
1089 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1091 if (temp_register != func->config_space[0x2C >> 2]) {
1092 // If it's a SMART-2 and the register isn't filled
1093 // in, ignore the difference because
1094 // they just have an old rev of the firmware
1096 if (!((func->config_space[0] == 0xAE100E11)
1097 && (temp_register == 0x00L)))
1098 return(ADAPTER_NOT_SAME);
1100 // Figure out IO and memory base lengths
1101 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1102 temp_register = 0xFFFFFFFF;
1103 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1104 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
1105 if (base) { // If this register is implemented
1108 // set base = amount of IO space requested
1109 base = base & 0xFFFFFFFE;
1115 base = base & 0xFFFFFFF0;
1125 // Check information in slot structure
1126 if (func->base_length[(cloop - 0x10) >> 2] != base)
1127 return(ADAPTER_NOT_SAME);
1129 if (func->base_type[(cloop - 0x10) >> 2] != type)
1130 return(ADAPTER_NOT_SAME);
1132 } // End of base register loop
1134 } // End of (type 0 config space) else
1136 // this is not a type 0 or 1 config space header so
1137 // we don't know how to do it
1138 return(DEVICE_TYPE_NOT_SUPPORTED);
1141 // Get the next function
1142 func = cpqhp_slot_find(func->bus, func->device, index++);
1151 * cpqhp_find_available_resources
1153 * Finds available memory, IO, and IRQ resources for programming
1154 * devices which may be added to the system
1155 * this function is for hot plug ADD!
1157 * returns 0 if success
1159 int cpqhp_find_available_resources (struct controller *ctrl, void *rom_start)
1165 struct pci_func *func = NULL;
1168 struct pci_resource *mem_node;
1169 struct pci_resource *p_mem_node;
1170 struct pci_resource *io_node;
1171 struct pci_resource *bus_node;
1172 void *rom_resource_table;
1174 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1175 dbg("rom_resource_table = %p\n", rom_resource_table);
1177 if (rom_resource_table == NULL) {
1180 // Sum all resources and setup resource maps
1181 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1182 dbg("unused_IRQ = %x\n", unused_IRQ);
1185 while (unused_IRQ) {
1186 if (unused_IRQ & 1) {
1187 cpqhp_disk_irq = temp;
1190 unused_IRQ = unused_IRQ >> 1;
1194 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1195 unused_IRQ = unused_IRQ >> 1;
1198 while (unused_IRQ) {
1199 if (unused_IRQ & 1) {
1200 cpqhp_nic_irq = temp;
1203 unused_IRQ = unused_IRQ >> 1;
1207 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1208 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1212 if (!cpqhp_nic_irq) {
1213 cpqhp_nic_irq = ctrl->cfgspc_irq;
1216 if (!cpqhp_disk_irq) {
1217 cpqhp_disk_irq = ctrl->cfgspc_irq;
1220 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1222 rc = compaq_nvram_load(rom_start, ctrl);
1226 one_slot = rom_resource_table + sizeof (struct hrt);
1228 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1229 dbg("number_of_entries = %d\n", i);
1231 if (!readb(one_slot + SECONDARY_BUS))
1234 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1236 while (i && readb(one_slot + SECONDARY_BUS)) {
1237 u8 dev_func = readb(one_slot + DEV_FUNC);
1238 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1239 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1240 u8 max_bus = readb(one_slot + MAX_BUS);
1241 u16 io_base = readw(one_slot + IO_BASE);
1242 u16 io_length = readw(one_slot + IO_LENGTH);
1243 u16 mem_base = readw(one_slot + MEM_BASE);
1244 u16 mem_length = readw(one_slot + MEM_LENGTH);
1245 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1246 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1248 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1249 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1250 primary_bus, secondary_bus, max_bus);
1252 // If this entry isn't for our controller's bus, ignore it
1253 if (primary_bus != ctrl->bus) {
1255 one_slot += sizeof (struct slot_rt);
1258 // find out if this entry is for an occupied slot
1259 ctrl->pci_bus->number = primary_bus;
1260 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1261 dbg("temp_D_word = %x\n", temp_dword);
1263 if (temp_dword != 0xFFFFFFFF) {
1265 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1267 while (func && (func->function != (dev_func & 0x07))) {
1268 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1269 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1272 // If we can't find a match, skip this table entry
1275 one_slot += sizeof (struct slot_rt);
1278 // this may not work and shouldn't be used
1279 if (secondary_bus != primary_bus)
1291 // If we've got a valid IO base, use it
1293 temp_dword = io_base + io_length;
1295 if ((io_base) && (temp_dword < 0x10000)) {
1296 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
1300 io_node->base = io_base;
1301 io_node->length = io_length;
1303 dbg("found io_node(base, length) = %x, %x\n", io_node->base, io_node->length);
1304 dbg("populated slot =%d \n", populated_slot);
1305 if (!populated_slot) {
1306 io_node->next = ctrl->io_head;
1307 ctrl->io_head = io_node;
1309 io_node->next = func->io_head;
1310 func->io_head = io_node;
1314 // If we've got a valid memory base, use it
1315 temp_dword = mem_base + mem_length;
1316 if ((mem_base) && (temp_dword < 0x10000)) {
1317 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
1321 mem_node->base = mem_base << 16;
1323 mem_node->length = mem_length << 16;
1325 dbg("found mem_node(base, length) = %x, %x\n", mem_node->base, mem_node->length);
1326 dbg("populated slot =%d \n", populated_slot);
1327 if (!populated_slot) {
1328 mem_node->next = ctrl->mem_head;
1329 ctrl->mem_head = mem_node;
1331 mem_node->next = func->mem_head;
1332 func->mem_head = mem_node;
1336 // If we've got a valid prefetchable memory base, and
1337 // the base + length isn't greater than 0xFFFF
1338 temp_dword = pre_mem_base + pre_mem_length;
1339 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1340 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
1344 p_mem_node->base = pre_mem_base << 16;
1346 p_mem_node->length = pre_mem_length << 16;
1347 dbg("found p_mem_node(base, length) = %x, %x\n", p_mem_node->base, p_mem_node->length);
1348 dbg("populated slot =%d \n", populated_slot);
1350 if (!populated_slot) {
1351 p_mem_node->next = ctrl->p_mem_head;
1352 ctrl->p_mem_head = p_mem_node;
1354 p_mem_node->next = func->p_mem_head;
1355 func->p_mem_head = p_mem_node;
1359 // If we've got a valid bus number, use it
1360 // The second condition is to ignore bus numbers on
1361 // populated slots that don't have PCI-PCI bridges
1362 if (secondary_bus && (secondary_bus != primary_bus)) {
1363 bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
1367 bus_node->base = secondary_bus;
1368 bus_node->length = max_bus - secondary_bus + 1;
1369 dbg("found bus_node(base, length) = %x, %x\n", bus_node->base, bus_node->length);
1370 dbg("populated slot =%d \n", populated_slot);
1371 if (!populated_slot) {
1372 bus_node->next = ctrl->bus_head;
1373 ctrl->bus_head = bus_node;
1375 bus_node->next = func->bus_head;
1376 func->bus_head = bus_node;
1381 one_slot += sizeof (struct slot_rt);
1384 // If all of the following fail, we don't have any resources for
1387 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1388 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1389 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1390 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1397 * cpqhp_return_board_resources
1399 * this routine returns all resources allocated to a board to
1400 * the available pool.
1402 * returns 0 if success
1404 int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1407 struct pci_resource *node;
1408 struct pci_resource *t_node;
1409 dbg("%s\n", __FUNCTION__);
1414 node = func->io_head;
1415 func->io_head = NULL;
1417 t_node = node->next;
1418 return_resource(&(resources->io_head), node);
1422 node = func->mem_head;
1423 func->mem_head = NULL;
1425 t_node = node->next;
1426 return_resource(&(resources->mem_head), node);
1430 node = func->p_mem_head;
1431 func->p_mem_head = NULL;
1433 t_node = node->next;
1434 return_resource(&(resources->p_mem_head), node);
1438 node = func->bus_head;
1439 func->bus_head = NULL;
1441 t_node = node->next;
1442 return_resource(&(resources->bus_head), node);
1446 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1447 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1448 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1449 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1456 * cpqhp_destroy_resource_list
1458 * Puts node back in the resource list pointed to by head
1460 void cpqhp_destroy_resource_list (struct resource_lists * resources)
1462 struct pci_resource *res, *tres;
1464 res = resources->io_head;
1465 resources->io_head = NULL;
1473 res = resources->mem_head;
1474 resources->mem_head = NULL;
1482 res = resources->p_mem_head;
1483 resources->p_mem_head = NULL;
1491 res = resources->bus_head;
1492 resources->bus_head = NULL;
1503 * cpqhp_destroy_board_resources
1505 * Puts node back in the resource list pointed to by head
1507 void cpqhp_destroy_board_resources (struct pci_func * func)
1509 struct pci_resource *res, *tres;
1511 res = func->io_head;
1512 func->io_head = NULL;
1520 res = func->mem_head;
1521 func->mem_head = NULL;
1529 res = func->p_mem_head;
1530 func->p_mem_head = NULL;
1538 res = func->bus_head;
1539 func->bus_head = NULL;