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>
36 #include <linux/pci_hotplug.h>
39 #include "cpqphp_nvram.h"
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)
86 struct pci_bus *child;
89 pci_lock_rescan_remove();
91 if (func->pci_dev == NULL)
92 func->pci_dev = pci_get_bus_and_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_get_bus_and_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_hp_add_bridge(func->pci_dev);
111 child = func->pci_dev->subordinate;
113 pci_bus_add_devices(child);
116 pci_dev_put(func->pci_dev);
119 pci_unlock_rescan_remove();
124 int cpqhp_unconfigure_device(struct pci_func *func)
128 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
130 pci_lock_rescan_remove();
131 for (j=0; j<8 ; j++) {
132 struct pci_dev *temp = pci_get_bus_and_slot(func->bus, PCI_DEVFN(func->device, j));
135 pci_stop_and_remove_bus_device(temp);
138 pci_unlock_rescan_remove();
142 static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
146 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
148 if (vendID == 0xffffffff)
150 return pci_bus_read_config_dword (bus, devfn, offset, value);
157 * @bus_num: bus number of PCI device
158 * @dev_num: device number of PCI device
159 * @slot: pointer to u8 where slot number will be returned
161 int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
165 if (cpqhp_legacy_mode) {
166 struct pci_dev *fakedev;
167 struct pci_bus *fakebus;
170 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
171 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
172 if (!fakedev || !fakebus) {
178 fakedev->devfn = dev_num << 3;
179 fakedev->bus = fakebus;
180 fakebus->number = bus_num;
181 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
182 __func__, dev_num, bus_num, int_pin, irq_num);
183 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
186 dbg("%s: rc %d\n", __func__, rc);
190 /* set the Edge Level Control Register (ELCR) */
191 temp_word = inb(0x4d0);
192 temp_word |= inb(0x4d1) << 8;
194 temp_word |= 0x01 << irq_num;
196 /* This should only be for x86 as it sets the Edge Level
199 outb((u8) (temp_word & 0xFF), 0x4d0); outb((u8) ((temp_word &
200 0xFF00) >> 8), 0x4d1); rc = 0; }
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 /* XXX: no recursion, wtf? */
235 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
244 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
248 u8 tbus, tdevice, tslot;
250 len = cpqhp_routing_table_length();
251 for (loop = 0; loop < len; ++loop) {
252 tbus = cpqhp_routing_table->slots[loop].bus;
253 tdevice = cpqhp_routing_table->slots[loop].devfn;
254 tslot = cpqhp_routing_table->slots[loop].slot;
259 ctrl->pci_bus->number = tbus;
260 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
261 if (!nobridge || (work == 0xffffffff))
264 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
265 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
266 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
268 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
269 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
270 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
271 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
283 int cpqhp_get_bus_dev (struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot)
285 /* plain (bridges allowed) */
286 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
290 /* More PCI configuration routines; this time centered around hotplug
298 * Reads configuration for all slots in a PCI bus and saves info.
300 * Note: For non-hot plug buses, the slot # saved is the device #
302 * returns 0 if success
304 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
311 struct pci_func *new_slot;
323 /* Decide which slots are supported */
327 * is_hot_plug is the slot mask
329 FirstSupported = is_hot_plug >> 4;
330 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
333 LastSupported = 0x1F;
336 /* Save PCI configuration space for all devices in supported slots */
337 ctrl->pci_bus->number = busnumber;
338 for (device = FirstSupported; device <= LastSupported; device++) {
340 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
342 if (ID == 0xFFFFFFFF) {
344 /* Setup slot structure with entry for empty
347 new_slot = cpqhp_slot_create(busnumber);
348 if (new_slot == NULL)
351 new_slot->bus = (u8) busnumber;
352 new_slot->device = (u8) device;
353 new_slot->function = 0;
354 new_slot->is_a_board = 0;
355 new_slot->presence_save = 0;
356 new_slot->switch_save = 0;
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)
379 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
380 /* Recurse the subordinate bus
381 * 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.
392 rc = cpqhp_save_config(ctrl, sub_bus, 0);
395 ctrl->pci_bus->number = busnumber;
400 new_slot = cpqhp_slot_find(busnumber, device, index++);
402 (new_slot->function != (u8) function))
403 new_slot = cpqhp_slot_find(busnumber, device, index++);
406 /* Setup slot structure. */
407 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_get_bus_and_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]));
427 pci_dev_put(new_slot->pci_dev);
433 /* this loop skips to the next present function
434 * reading in Class Code and Header type.
436 while ((function < max_functions) && (!stop_it)) {
437 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
438 if (ID == 0xFFFFFFFF) {
442 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
446 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
453 } while (function < max_functions);
454 } /* End of FOR loop */
461 * cpqhp_save_slot_config
463 * Saves configuration info for all PCI devices in a given slot
464 * including subordinate buses.
466 * returns 0 if success
468 int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func *new_slot)
483 ctrl->pci_bus->number = new_slot->bus;
484 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
486 if (ID == 0xFFFFFFFF)
489 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
490 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
492 if (header_type & 0x80) /* Multi-function device */
497 while (function < max_functions) {
498 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
499 /* Recurse the subordinate bus */
500 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
502 sub_bus = (int) secondary_bus;
504 /* Save the config headers for the secondary
507 rc = cpqhp_save_config(ctrl, sub_bus, 0);
510 ctrl->pci_bus->number = new_slot->bus;
514 new_slot->status = 0;
516 for (cloop = 0; cloop < 0x20; cloop++)
517 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
523 /* this loop skips to the next present function
524 * reading in the Class Code and the Header type.
526 while ((function < max_functions) && (!stop_it)) {
527 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
529 if (ID == 0xFFFFFFFF)
532 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
533 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
545 * cpqhp_save_base_addr_length
547 * Saves the length of all base address registers for the
548 * specified slot. this is for hot plug REPLACE
550 * returns 0 if success
552 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func *func)
562 struct pci_func *next;
564 struct pci_bus *pci_bus = ctrl->pci_bus;
567 func = cpqhp_slot_find(func->bus, func->device, index++);
569 while (func != NULL) {
570 pci_bus->number = func->bus;
571 devfn = PCI_DEVFN(func->device, func->function);
573 /* Check for Bridge */
574 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
576 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
577 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
579 sub_bus = (int) secondary_bus;
581 next = cpqhp_slot_list[sub_bus];
583 while (next != NULL) {
584 rc = cpqhp_save_base_addr_length(ctrl, next);
590 pci_bus->number = func->bus;
592 /* FIXME: this loop is duplicated in the non-bridge
593 * case. The two could be rolled together Figure out
594 * IO and memory base lengths
596 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
597 temp_register = 0xFFFFFFFF;
598 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
599 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
600 /* If this register is implemented */
604 * set base = amount of IO space
607 base = base & 0xFFFFFFFE;
613 base = base & 0xFFFFFFF0;
623 /* Save information in slot structure */
624 func->base_length[(cloop - 0x10) >> 2] =
626 func->base_type[(cloop - 0x10) >> 2] = type;
628 } /* End of base register loop */
630 } else if ((header_type & 0x7F) == 0x00) {
631 /* Figure out IO and memory base lengths */
632 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
633 temp_register = 0xFFFFFFFF;
634 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
635 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
637 /* If this register is implemented */
641 * base = amount of IO space
644 base = base & 0xFFFFFFFE;
650 * base = amount of memory
653 base = base & 0xFFFFFFF0;
663 /* Save information in slot structure */
664 func->base_length[(cloop - 0x10) >> 2] = base;
665 func->base_type[(cloop - 0x10) >> 2] = type;
667 } /* End of base register loop */
669 } else { /* Some other unknown header type */
672 /* find the next device in this slot */
673 func = cpqhp_slot_find(func->bus, func->device, index++);
681 * cpqhp_save_used_resources
683 * Stores used resource information for existing boards. this is
684 * for boards that were in the system when this driver was loaded.
685 * this function is for hot plug ADD
687 * returns 0 if success
689 int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func *func)
705 struct pci_resource *mem_node;
706 struct pci_resource *p_mem_node;
707 struct pci_resource *io_node;
708 struct pci_resource *bus_node;
709 struct pci_bus *pci_bus = ctrl->pci_bus;
712 func = cpqhp_slot_find(func->bus, func->device, index++);
714 while ((func != NULL) && func->is_a_board) {
715 pci_bus->number = func->bus;
716 devfn = PCI_DEVFN(func->device, func->function);
718 /* Save the command register */
719 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
723 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
725 /* Check for Bridge */
726 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
728 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
729 /* Clear Bridge Control Register */
731 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
732 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
733 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
735 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
739 bus_node->base = secondary_bus;
740 bus_node->length = temp_byte - secondary_bus + 1;
742 bus_node->next = func->bus_head;
743 func->bus_head = bus_node;
745 /* Save IO base and Limit registers */
746 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
747 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
749 if ((b_base <= b_length) && (save_command & 0x01)) {
750 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
754 io_node->base = (b_base & 0xF0) << 8;
755 io_node->length = (b_length - b_base + 0x10) << 8;
757 io_node->next = func->io_head;
758 func->io_head = io_node;
761 /* Save memory base and Limit registers */
762 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
763 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
765 if ((w_base <= w_length) && (save_command & 0x02)) {
766 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
770 mem_node->base = w_base << 16;
771 mem_node->length = (w_length - w_base + 0x10) << 16;
773 mem_node->next = func->mem_head;
774 func->mem_head = mem_node;
777 /* Save prefetchable memory base and Limit registers */
778 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
779 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
781 if ((w_base <= w_length) && (save_command & 0x02)) {
782 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
786 p_mem_node->base = w_base << 16;
787 p_mem_node->length = (w_length - w_base + 0x10) << 16;
789 p_mem_node->next = func->p_mem_head;
790 func->p_mem_head = p_mem_node;
792 /* Figure out IO and memory base lengths */
793 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
794 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
796 temp_register = 0xFFFFFFFF;
797 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
798 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
800 temp_register = base;
802 /* If this register is implemented */
804 if (((base & 0x03L) == 0x01)
805 && (save_command & 0x01)) {
807 * set temp_register = amount
808 * of IO space requested
810 temp_register = base & 0xFFFFFFFE;
811 temp_register = (~temp_register) + 1;
813 io_node = kmalloc(sizeof(*io_node),
819 save_base & (~0x03L);
820 io_node->length = temp_register;
822 io_node->next = func->io_head;
823 func->io_head = io_node;
825 if (((base & 0x0BL) == 0x08)
826 && (save_command & 0x02)) {
827 /* prefetchable memory base */
828 temp_register = base & 0xFFFFFFF0;
829 temp_register = (~temp_register) + 1;
831 p_mem_node = kmalloc(sizeof(*p_mem_node),
836 p_mem_node->base = save_base & (~0x0FL);
837 p_mem_node->length = temp_register;
839 p_mem_node->next = func->p_mem_head;
840 func->p_mem_head = p_mem_node;
842 if (((base & 0x0BL) == 0x00)
843 && (save_command & 0x02)) {
844 /* prefetchable memory base */
845 temp_register = base & 0xFFFFFFF0;
846 temp_register = (~temp_register) + 1;
848 mem_node = kmalloc(sizeof(*mem_node),
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 /* Standard header */
863 } else if ((header_type & 0x7F) == 0x00) {
864 /* Figure out IO and memory base lengths */
865 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
866 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
868 temp_register = 0xFFFFFFFF;
869 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
870 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
872 temp_register = base;
874 /* If this register is implemented */
876 if (((base & 0x03L) == 0x01)
877 && (save_command & 0x01)) {
879 * set temp_register = amount
880 * of IO space requested
882 temp_register = base & 0xFFFFFFFE;
883 temp_register = (~temp_register) + 1;
885 io_node = kmalloc(sizeof(*io_node),
890 io_node->base = save_base & (~0x01L);
891 io_node->length = temp_register;
893 io_node->next = func->io_head;
894 func->io_head = io_node;
896 if (((base & 0x0BL) == 0x08)
897 && (save_command & 0x02)) {
898 /* prefetchable memory base */
899 temp_register = base & 0xFFFFFFF0;
900 temp_register = (~temp_register) + 1;
902 p_mem_node = kmalloc(sizeof(*p_mem_node),
907 p_mem_node->base = save_base & (~0x0FL);
908 p_mem_node->length = temp_register;
910 p_mem_node->next = func->p_mem_head;
911 func->p_mem_head = p_mem_node;
913 if (((base & 0x0BL) == 0x00)
914 && (save_command & 0x02)) {
915 /* prefetchable memory base */
916 temp_register = base & 0xFFFFFFF0;
917 temp_register = (~temp_register) + 1;
919 mem_node = kmalloc(sizeof(*mem_node),
924 mem_node->base = save_base & (~0x0FL);
925 mem_node->length = temp_register;
927 mem_node->next = func->mem_head;
928 func->mem_head = mem_node;
932 } /* End of base register loop */
935 /* find the next device in this slot */
936 func = cpqhp_slot_find(func->bus, func->device, index++);
944 * cpqhp_configure_board
946 * Copies saved configuration information to one slot.
947 * this is called recursively for bridge devices.
948 * this is for hot plug REPLACE!
950 * returns 0 if success
952 int cpqhp_configure_board(struct controller *ctrl, struct pci_func *func)
958 struct pci_func *next;
962 struct pci_bus *pci_bus = ctrl->pci_bus;
965 func = cpqhp_slot_find(func->bus, func->device, index++);
967 while (func != NULL) {
968 pci_bus->number = func->bus;
969 devfn = PCI_DEVFN(func->device, func->function);
971 /* Start at the top of config space so that the control
972 * registers are programmed last
974 for (cloop = 0x3C; cloop > 0; cloop -= 4)
975 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
977 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
979 /* If this is a bridge device, restore subordinate devices */
980 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
981 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
983 sub_bus = (int) secondary_bus;
985 next = cpqhp_slot_list[sub_bus];
987 while (next != NULL) {
988 rc = cpqhp_configure_board(ctrl, next);
996 /* Check all the base Address Registers to make sure
997 * they are the same. If not, the board is different.
1000 for (cloop = 16; cloop < 40; cloop += 4) {
1001 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
1003 if (temp != func->config_space[cloop >> 2]) {
1004 dbg("Config space compare failure!!! offset = %x\n", cloop);
1005 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1006 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1012 func->configured = 1;
1014 func = cpqhp_slot_find(func->bus, func->device, index++);
1022 * cpqhp_valid_replace
1024 * this function checks to see if a board is the same as the
1025 * one it is replacing. this check will detect if the device's
1026 * vendor or device id's are the same
1028 * returns 0 if the board is the same nonzero otherwise
1030 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func *func)
1036 u32 temp_register = 0;
1039 struct pci_func *next;
1041 struct pci_bus *pci_bus = ctrl->pci_bus;
1044 if (!func->is_a_board)
1045 return(ADD_NOT_SUPPORTED);
1047 func = cpqhp_slot_find(func->bus, func->device, index++);
1049 while (func != NULL) {
1050 pci_bus->number = func->bus;
1051 devfn = PCI_DEVFN(func->device, func->function);
1053 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1055 /* No adapter present */
1056 if (temp_register == 0xFFFFFFFF)
1057 return(NO_ADAPTER_PRESENT);
1059 if (temp_register != func->config_space[0])
1060 return(ADAPTER_NOT_SAME);
1062 /* Check for same revision number and class code */
1063 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1065 /* Adapter not the same */
1066 if (temp_register != func->config_space[0x08 >> 2])
1067 return(ADAPTER_NOT_SAME);
1069 /* Check for Bridge */
1070 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1072 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1073 /* In order to continue checking, we must program the
1074 * bus registers in the bridge to respond to accesses
1075 * for its subordinate bus(es)
1078 temp_register = func->config_space[0x18 >> 2];
1079 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1081 secondary_bus = (temp_register >> 8) & 0xFF;
1083 next = cpqhp_slot_list[secondary_bus];
1085 while (next != NULL) {
1086 rc = cpqhp_valid_replace(ctrl, next);
1094 /* Check to see if it is a standard config header */
1095 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1096 /* Check subsystem vendor and ID */
1097 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1099 if (temp_register != func->config_space[0x2C >> 2]) {
1100 /* If it's a SMART-2 and the register isn't
1101 * filled in, ignore the difference because
1102 * they just have an old rev of the firmware
1104 if (!((func->config_space[0] == 0xAE100E11)
1105 && (temp_register == 0x00L)))
1106 return(ADAPTER_NOT_SAME);
1108 /* Figure out IO and memory base lengths */
1109 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1110 temp_register = 0xFFFFFFFF;
1111 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1112 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
1114 /* If this register is implemented */
1118 * set base = amount of IO
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
1151 return(DEVICE_TYPE_NOT_SUPPORTED);
1154 /* Get the next function */
1155 func = cpqhp_slot_find(func->bus, func->device, index++);
1164 * cpqhp_find_available_resources
1166 * Finds available memory, IO, and IRQ resources for programming
1167 * devices which may be added to the system
1168 * this function is for hot plug ADD!
1170 * returns 0 if success
1172 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1177 void __iomem *one_slot;
1178 void __iomem *rom_resource_table;
1179 struct pci_func *func = NULL;
1182 struct pci_resource *mem_node;
1183 struct pci_resource *p_mem_node;
1184 struct pci_resource *io_node;
1185 struct pci_resource *bus_node;
1187 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1188 dbg("rom_resource_table = %p\n", rom_resource_table);
1190 if (rom_resource_table == NULL)
1193 /* Sum all resources and setup resource maps */
1194 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1195 dbg("unused_IRQ = %x\n", unused_IRQ);
1198 while (unused_IRQ) {
1199 if (unused_IRQ & 1) {
1200 cpqhp_disk_irq = temp;
1203 unused_IRQ = unused_IRQ >> 1;
1207 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1208 unused_IRQ = unused_IRQ >> 1;
1211 while (unused_IRQ) {
1212 if (unused_IRQ & 1) {
1213 cpqhp_nic_irq = temp;
1216 unused_IRQ = unused_IRQ >> 1;
1220 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1221 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1226 cpqhp_nic_irq = ctrl->cfgspc_irq;
1228 if (!cpqhp_disk_irq)
1229 cpqhp_disk_irq = ctrl->cfgspc_irq;
1231 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1233 rc = compaq_nvram_load(rom_start, ctrl);
1237 one_slot = rom_resource_table + sizeof (struct hrt);
1239 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1240 dbg("number_of_entries = %d\n", i);
1242 if (!readb(one_slot + SECONDARY_BUS))
1245 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1247 while (i && readb(one_slot + SECONDARY_BUS)) {
1248 u8 dev_func = readb(one_slot + DEV_FUNC);
1249 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1250 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1251 u8 max_bus = readb(one_slot + MAX_BUS);
1252 u16 io_base = readw(one_slot + IO_BASE);
1253 u16 io_length = readw(one_slot + IO_LENGTH);
1254 u16 mem_base = readw(one_slot + MEM_BASE);
1255 u16 mem_length = readw(one_slot + MEM_LENGTH);
1256 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1257 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1259 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1260 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1261 primary_bus, secondary_bus, max_bus);
1263 /* If this entry isn't for our controller's bus, ignore it */
1264 if (primary_bus != ctrl->bus) {
1266 one_slot += sizeof (struct slot_rt);
1269 /* find out if this entry is for an occupied slot */
1270 ctrl->pci_bus->number = primary_bus;
1271 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1272 dbg("temp_D_word = %x\n", temp_dword);
1274 if (temp_dword != 0xFFFFFFFF) {
1276 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1278 while (func && (func->function != (dev_func & 0x07))) {
1279 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1280 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1283 /* If we can't find a match, skip this table entry */
1286 one_slot += sizeof (struct slot_rt);
1289 /* this may not work and shouldn't be used */
1290 if (secondary_bus != primary_bus)
1302 /* If we've got a valid IO base, use it */
1304 temp_dword = io_base + io_length;
1306 if ((io_base) && (temp_dword < 0x10000)) {
1307 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1311 io_node->base = io_base;
1312 io_node->length = io_length;
1314 dbg("found io_node(base, length) = %x, %x\n",
1315 io_node->base, io_node->length);
1316 dbg("populated slot =%d \n", populated_slot);
1317 if (!populated_slot) {
1318 io_node->next = ctrl->io_head;
1319 ctrl->io_head = io_node;
1321 io_node->next = func->io_head;
1322 func->io_head = io_node;
1326 /* If we've got a valid memory base, use it */
1327 temp_dword = mem_base + mem_length;
1328 if ((mem_base) && (temp_dword < 0x10000)) {
1329 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1333 mem_node->base = mem_base << 16;
1335 mem_node->length = mem_length << 16;
1337 dbg("found mem_node(base, length) = %x, %x\n",
1338 mem_node->base, mem_node->length);
1339 dbg("populated slot =%d \n", populated_slot);
1340 if (!populated_slot) {
1341 mem_node->next = ctrl->mem_head;
1342 ctrl->mem_head = mem_node;
1344 mem_node->next = func->mem_head;
1345 func->mem_head = mem_node;
1349 /* If we've got a valid prefetchable memory base, and
1350 * 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
1378 if (secondary_bus && (secondary_bus != primary_bus)) {
1379 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1383 bus_node->base = secondary_bus;
1384 bus_node->length = max_bus - secondary_bus + 1;
1385 dbg("found bus_node(base, length) = %x, %x\n",
1386 bus_node->base, bus_node->length);
1387 dbg("populated slot =%d \n", populated_slot);
1388 if (!populated_slot) {
1389 bus_node->next = ctrl->bus_head;
1390 ctrl->bus_head = bus_node;
1392 bus_node->next = func->bus_head;
1393 func->bus_head = bus_node;
1398 one_slot += sizeof (struct slot_rt);
1401 /* If all of the following fail, we don't have any resources for
1405 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1406 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1407 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1408 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1415 * cpqhp_return_board_resources
1417 * this routine returns all resources allocated to a board to
1418 * the available pool.
1420 * returns 0 if success
1422 int cpqhp_return_board_resources(struct pci_func *func, struct resource_lists *resources)
1425 struct pci_resource *node;
1426 struct pci_resource *t_node;
1427 dbg("%s\n", __func__);
1432 node = func->io_head;
1433 func->io_head = NULL;
1435 t_node = node->next;
1436 return_resource(&(resources->io_head), node);
1440 node = func->mem_head;
1441 func->mem_head = NULL;
1443 t_node = node->next;
1444 return_resource(&(resources->mem_head), node);
1448 node = func->p_mem_head;
1449 func->p_mem_head = NULL;
1451 t_node = node->next;
1452 return_resource(&(resources->p_mem_head), node);
1456 node = func->bus_head;
1457 func->bus_head = NULL;
1459 t_node = node->next;
1460 return_resource(&(resources->bus_head), node);
1464 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1465 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1466 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1467 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1474 * cpqhp_destroy_resource_list
1476 * Puts node back in the resource list pointed to by head
1478 void cpqhp_destroy_resource_list (struct resource_lists *resources)
1480 struct pci_resource *res, *tres;
1482 res = resources->io_head;
1483 resources->io_head = NULL;
1491 res = resources->mem_head;
1492 resources->mem_head = NULL;
1500 res = resources->p_mem_head;
1501 resources->p_mem_head = NULL;
1509 res = resources->bus_head;
1510 resources->bus_head = NULL;
1521 * cpqhp_destroy_board_resources
1523 * Puts node back in the resource list pointed to by head
1525 void cpqhp_destroy_board_resources (struct pci_func *func)
1527 struct pci_resource *res, *tres;
1529 res = func->io_head;
1530 func->io_head = NULL;
1538 res = func->mem_head;
1539 func->mem_head = NULL;
1547 res = func->p_mem_head;
1548 func->p_mem_head = NULL;
1556 res = func->bus_head;
1557 func->bus_head = NULL;