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)
87 struct pci_bus *child;
90 if (func->pci_dev == NULL)
91 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
93 /* No pci device, we need to create it then */
94 if (func->pci_dev == NULL) {
95 dbg("INFO: pci_dev still null\n");
97 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
99 pci_bus_add_devices(ctrl->pci_dev->bus);
101 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
102 if (func->pci_dev == NULL) {
103 dbg("ERROR: pci_dev still null\n");
108 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
109 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
110 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
111 pci_do_scan_bus(child);
118 int cpqhp_unconfigure_device(struct pci_func* func)
122 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
124 for (j=0; j<8 ; j++) {
125 struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
127 pci_remove_bus_device(temp);
132 static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
136 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
138 if (vendID == 0xffffffff)
140 return pci_bus_read_config_dword (bus, devfn, offset, value);
147 * @bus_num: bus number of PCI device
148 * @dev_num: device number of PCI device
149 * @slot: pointer to u8 where slot number will be returned
151 int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
155 if (cpqhp_legacy_mode) {
156 struct pci_dev *fakedev;
157 struct pci_bus *fakebus;
160 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
161 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
162 if (!fakedev || !fakebus) {
168 fakedev->devfn = dev_num << 3;
169 fakedev->bus = fakebus;
170 fakebus->number = bus_num;
171 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
172 __func__, dev_num, bus_num, int_pin, irq_num);
173 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
176 dbg("%s: rc %d\n", __func__, rc);
180 /* set the Edge Level Control Register (ELCR) */
181 temp_word = inb(0x4d0);
182 temp_word |= inb(0x4d1) << 8;
184 temp_word |= 0x01 << irq_num;
186 /* This should only be for x86 as it sets the Edge Level
189 outb((u8) (temp_word & 0xFF), 0x4d0); outb((u8) ((temp_word &
190 0xFF00) >> 8), 0x4d1); rc = 0; }
196 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
202 ctrl->pci_bus->number = bus_num;
204 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
205 /* Scan for access first */
206 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
208 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
209 /* Yep we got one. Not a bridge ? */
210 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
216 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
217 /* Scan for access first */
218 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
220 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
221 /* Yep we got one. bridge ? */
222 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
223 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
224 /* XXX: no recursion, wtf? */
225 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
234 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
238 u8 tbus, tdevice, tslot;
240 len = cpqhp_routing_table_length();
241 for (loop = 0; loop < len; ++loop) {
242 tbus = cpqhp_routing_table->slots[loop].bus;
243 tdevice = cpqhp_routing_table->slots[loop].devfn;
244 tslot = cpqhp_routing_table->slots[loop].slot;
249 ctrl->pci_bus->number = tbus;
250 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
251 if (!nobridge || (work == 0xffffffff))
254 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
255 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
256 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
258 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
259 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
260 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
261 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
273 int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
275 /* plain (bridges allowed) */
276 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
280 /* More PCI configuration routines; this time centered around hotplug
288 * Reads configuration for all slots in a PCI bus and saves info.
290 * Note: For non-hot plug busses, the slot # saved is the device #
292 * returns 0 if success
294 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
301 struct pci_func *new_slot;
313 /* Decide which slots are supported */
317 * is_hot_plug is the slot mask
319 FirstSupported = is_hot_plug >> 4;
320 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
323 LastSupported = 0x1F;
326 /* Save PCI configuration space for all devices in supported slots */
327 ctrl->pci_bus->number = busnumber;
328 for (device = FirstSupported; device <= LastSupported; device++) {
330 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
332 if (ID == 0xFFFFFFFF) {
334 /* Setup slot structure with entry for empty
337 new_slot = cpqhp_slot_create(busnumber);
338 if (new_slot == NULL)
341 new_slot->bus = (u8) busnumber;
342 new_slot->device = (u8) device;
343 new_slot->function = 0;
344 new_slot->is_a_board = 0;
345 new_slot->presence_save = 0;
346 new_slot->switch_save = 0;
351 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
355 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
359 /* If multi-function device, set max_functions to 8 */
360 if (header_type & 0x80)
369 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
370 /* Recurse the subordinate bus
371 * get the subordinate bus number
373 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
377 sub_bus = (int) secondary_bus;
379 /* Save secondary bus cfg spc
380 * with this recursive call.
382 rc = cpqhp_save_config(ctrl, sub_bus, 0);
385 ctrl->pci_bus->number = busnumber;
390 new_slot = cpqhp_slot_find(busnumber, device, index++);
392 (new_slot->function != (u8) function))
393 new_slot = cpqhp_slot_find(busnumber, device, index++);
396 /* Setup slot structure. */
397 new_slot = cpqhp_slot_create(busnumber);
398 if (new_slot == NULL)
402 new_slot->bus = (u8) busnumber;
403 new_slot->device = (u8) device;
404 new_slot->function = (u8) function;
405 new_slot->is_a_board = 1;
406 new_slot->switch_save = 0x10;
407 /* In case of unsupported board */
408 new_slot->status = DevError;
409 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
411 for (cloop = 0; cloop < 0x20; cloop++) {
412 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
421 /* this loop skips to the next present function
422 * reading in Class Code and Header type.
424 while ((function < max_functions) && (!stop_it)) {
425 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
426 if (ID == 0xFFFFFFFF) {
430 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
434 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
441 } while (function < max_functions);
442 } /* End of FOR loop */
449 * cpqhp_save_slot_config
451 * Saves configuration info for all PCI devices in a given slot
452 * including subordinate busses.
454 * returns 0 if success
456 int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
471 ctrl->pci_bus->number = new_slot->bus;
472 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
474 if (ID == 0xFFFFFFFF)
477 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
478 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
480 if (header_type & 0x80) /* Multi-function device */
485 while (function < max_functions) {
486 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
487 /* Recurse the subordinate bus */
488 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
490 sub_bus = (int) secondary_bus;
492 /* Save the config headers for the secondary
495 rc = cpqhp_save_config(ctrl, sub_bus, 0);
498 ctrl->pci_bus->number = new_slot->bus;
502 new_slot->status = 0;
504 for (cloop = 0; cloop < 0x20; cloop++)
505 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
511 /* this loop skips to the next present function
512 * reading in the Class Code and the Header type.
514 while ((function < max_functions) && (!stop_it)) {
515 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
517 if (ID == 0xFFFFFFFF)
520 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
521 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
533 * cpqhp_save_base_addr_length
535 * Saves the length of all base address registers for the
536 * specified slot. this is for hot plug REPLACE
538 * returns 0 if success
540 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
550 struct pci_func *next;
552 struct pci_bus *pci_bus = ctrl->pci_bus;
555 func = cpqhp_slot_find(func->bus, func->device, index++);
557 while (func != NULL) {
558 pci_bus->number = func->bus;
559 devfn = PCI_DEVFN(func->device, func->function);
561 /* Check for Bridge */
562 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
564 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
565 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
567 sub_bus = (int) secondary_bus;
569 next = cpqhp_slot_list[sub_bus];
571 while (next != NULL) {
572 rc = cpqhp_save_base_addr_length(ctrl, next);
578 pci_bus->number = func->bus;
580 /* FIXME: this loop is duplicated in the non-bridge
581 * case. The two could be rolled together Figure out
582 * IO and memory base lengths
584 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
585 temp_register = 0xFFFFFFFF;
586 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
587 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
588 /* If this register is implemented */
592 * set base = amount of IO space
595 base = base & 0xFFFFFFFE;
601 base = base & 0xFFFFFFF0;
611 /* Save information in slot structure */
612 func->base_length[(cloop - 0x10) >> 2] =
614 func->base_type[(cloop - 0x10) >> 2] = type;
616 } /* End of base register loop */
618 } else if ((header_type & 0x7F) == 0x00) {
619 /* Figure out IO and memory base lengths */
620 for (cloop = 0x10; cloop <= 0x24; 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 this register is implemented */
629 * base = amount of IO space
632 base = base & 0xFFFFFFFE;
638 * base = amount of memory
641 base = base & 0xFFFFFFF0;
651 /* Save information in slot structure */
652 func->base_length[(cloop - 0x10) >> 2] = base;
653 func->base_type[(cloop - 0x10) >> 2] = type;
655 } /* End of base register loop */
657 } else { /* Some other unknown header type */
660 /* find the next device in this slot */
661 func = cpqhp_slot_find(func->bus, func->device, index++);
669 * cpqhp_save_used_resources
671 * Stores used resource information for existing boards. this is
672 * for boards that were in the system when this driver was loaded.
673 * this function is for hot plug ADD
675 * returns 0 if success
677 int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
693 struct pci_resource *mem_node;
694 struct pci_resource *p_mem_node;
695 struct pci_resource *io_node;
696 struct pci_resource *bus_node;
697 struct pci_bus *pci_bus = ctrl->pci_bus;
700 func = cpqhp_slot_find(func->bus, func->device, index++);
702 while ((func != NULL) && func->is_a_board) {
703 pci_bus->number = func->bus;
704 devfn = PCI_DEVFN(func->device, func->function);
706 /* Save the command register */
707 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
711 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
713 /* Check for Bridge */
714 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
716 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
717 /* Clear Bridge Control Register */
719 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
720 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
721 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
723 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
727 bus_node->base = secondary_bus;
728 bus_node->length = temp_byte - secondary_bus + 1;
730 bus_node->next = func->bus_head;
731 func->bus_head = bus_node;
733 /* Save IO base and Limit registers */
734 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
735 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
737 if ((b_base <= b_length) && (save_command & 0x01)) {
738 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
742 io_node->base = (b_base & 0xF0) << 8;
743 io_node->length = (b_length - b_base + 0x10) << 8;
745 io_node->next = func->io_head;
746 func->io_head = io_node;
749 /* Save memory base and Limit registers */
750 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
751 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
753 if ((w_base <= w_length) && (save_command & 0x02)) {
754 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
758 mem_node->base = w_base << 16;
759 mem_node->length = (w_length - w_base + 0x10) << 16;
761 mem_node->next = func->mem_head;
762 func->mem_head = mem_node;
765 /* Save prefetchable memory base and Limit registers */
766 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
767 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
769 if ((w_base <= w_length) && (save_command & 0x02)) {
770 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
774 p_mem_node->base = w_base << 16;
775 p_mem_node->length = (w_length - w_base + 0x10) << 16;
777 p_mem_node->next = func->p_mem_head;
778 func->p_mem_head = p_mem_node;
780 /* Figure out IO and memory base lengths */
781 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
782 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
784 temp_register = 0xFFFFFFFF;
785 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
786 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
788 temp_register = base;
790 /* If this register is implemented */
792 if (((base & 0x03L) == 0x01)
793 && (save_command & 0x01)) {
795 * set temp_register = amount
796 * of IO space requested
798 temp_register = base & 0xFFFFFFFE;
799 temp_register = (~temp_register) + 1;
801 io_node = kmalloc(sizeof(*io_node),
807 save_base & (~0x03L);
808 io_node->length = temp_register;
810 io_node->next = func->io_head;
811 func->io_head = io_node;
813 if (((base & 0x0BL) == 0x08)
814 && (save_command & 0x02)) {
815 /* prefetchable memory base */
816 temp_register = base & 0xFFFFFFF0;
817 temp_register = (~temp_register) + 1;
819 p_mem_node = kmalloc(sizeof(*p_mem_node),
824 p_mem_node->base = save_base & (~0x0FL);
825 p_mem_node->length = temp_register;
827 p_mem_node->next = func->p_mem_head;
828 func->p_mem_head = p_mem_node;
830 if (((base & 0x0BL) == 0x00)
831 && (save_command & 0x02)) {
832 /* prefetchable memory base */
833 temp_register = base & 0xFFFFFFF0;
834 temp_register = (~temp_register) + 1;
836 mem_node = kmalloc(sizeof(*mem_node),
841 mem_node->base = save_base & (~0x0FL);
842 mem_node->length = temp_register;
844 mem_node->next = func->mem_head;
845 func->mem_head = mem_node;
849 } /* End of base register loop */
850 /* Standard header */
851 } else if ((header_type & 0x7F) == 0x00) {
852 /* Figure out IO and memory base lengths */
853 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
854 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
856 temp_register = 0xFFFFFFFF;
857 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
858 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
860 temp_register = base;
862 /* If this register is implemented */
864 if (((base & 0x03L) == 0x01)
865 && (save_command & 0x01)) {
867 * set temp_register = amount
868 * of IO space requested
870 temp_register = base & 0xFFFFFFFE;
871 temp_register = (~temp_register) + 1;
873 io_node = kmalloc(sizeof(*io_node),
878 io_node->base = save_base & (~0x01L);
879 io_node->length = temp_register;
881 io_node->next = func->io_head;
882 func->io_head = io_node;
884 if (((base & 0x0BL) == 0x08)
885 && (save_command & 0x02)) {
886 /* prefetchable memory base */
887 temp_register = base & 0xFFFFFFF0;
888 temp_register = (~temp_register) + 1;
890 p_mem_node = kmalloc(sizeof(*p_mem_node),
895 p_mem_node->base = save_base & (~0x0FL);
896 p_mem_node->length = temp_register;
898 p_mem_node->next = func->p_mem_head;
899 func->p_mem_head = p_mem_node;
901 if (((base & 0x0BL) == 0x00)
902 && (save_command & 0x02)) {
903 /* prefetchable memory base */
904 temp_register = base & 0xFFFFFFF0;
905 temp_register = (~temp_register) + 1;
907 mem_node = kmalloc(sizeof(*mem_node),
912 mem_node->base = save_base & (~0x0FL);
913 mem_node->length = temp_register;
915 mem_node->next = func->mem_head;
916 func->mem_head = mem_node;
920 } /* End of base register loop */
923 /* find the next device in this slot */
924 func = cpqhp_slot_find(func->bus, func->device, index++);
932 * cpqhp_configure_board
934 * Copies saved configuration information to one slot.
935 * this is called recursively for bridge devices.
936 * this is for hot plug REPLACE!
938 * returns 0 if success
940 int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
946 struct pci_func *next;
950 struct pci_bus *pci_bus = ctrl->pci_bus;
953 func = cpqhp_slot_find(func->bus, func->device, index++);
955 while (func != NULL) {
956 pci_bus->number = func->bus;
957 devfn = PCI_DEVFN(func->device, func->function);
959 /* Start at the top of config space so that the control
960 * registers are programmed last
962 for (cloop = 0x3C; cloop > 0; cloop -= 4)
963 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
965 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
967 /* If this is a bridge device, restore subordinate devices */
968 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
969 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
971 sub_bus = (int) secondary_bus;
973 next = cpqhp_slot_list[sub_bus];
975 while (next != NULL) {
976 rc = cpqhp_configure_board(ctrl, next);
984 /* Check all the base Address Registers to make sure
985 * they are the same. If not, the board is different.
988 for (cloop = 16; cloop < 40; cloop += 4) {
989 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
991 if (temp != func->config_space[cloop >> 2]) {
992 dbg("Config space compare failure!!! offset = %x\n", cloop);
993 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
994 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1000 func->configured = 1;
1002 func = cpqhp_slot_find(func->bus, func->device, index++);
1010 * cpqhp_valid_replace
1012 * this function checks to see if a board is the same as the
1013 * one it is replacing. this check will detect if the device's
1014 * vendor or device id's are the same
1016 * returns 0 if the board is the same nonzero otherwise
1018 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1024 u32 temp_register = 0;
1027 struct pci_func *next;
1029 struct pci_bus *pci_bus = ctrl->pci_bus;
1032 if (!func->is_a_board)
1033 return(ADD_NOT_SUPPORTED);
1035 func = cpqhp_slot_find(func->bus, func->device, index++);
1037 while (func != NULL) {
1038 pci_bus->number = func->bus;
1039 devfn = PCI_DEVFN(func->device, func->function);
1041 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1043 /* No adapter present */
1044 if (temp_register == 0xFFFFFFFF)
1045 return(NO_ADAPTER_PRESENT);
1047 if (temp_register != func->config_space[0])
1048 return(ADAPTER_NOT_SAME);
1050 /* Check for same revision number and class code */
1051 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1053 /* Adapter not the same */
1054 if (temp_register != func->config_space[0x08 >> 2])
1055 return(ADAPTER_NOT_SAME);
1057 /* Check for Bridge */
1058 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1060 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1061 /* In order to continue checking, we must program the
1062 * bus registers in the bridge to respond to accesses
1063 * for its subordinate bus(es)
1066 temp_register = func->config_space[0x18 >> 2];
1067 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1069 secondary_bus = (temp_register >> 8) & 0xFF;
1071 next = cpqhp_slot_list[secondary_bus];
1073 while (next != NULL) {
1074 rc = cpqhp_valid_replace(ctrl, next);
1082 /* Check to see if it is a standard config header */
1083 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1084 /* Check subsystem vendor and ID */
1085 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1087 if (temp_register != func->config_space[0x2C >> 2]) {
1088 /* If it's a SMART-2 and the register isn't
1089 * filled in, ignore the difference because
1090 * they just have an old rev of the firmware
1092 if (!((func->config_space[0] == 0xAE100E11)
1093 && (temp_register == 0x00L)))
1094 return(ADAPTER_NOT_SAME);
1096 /* Figure out IO and memory base lengths */
1097 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1098 temp_register = 0xFFFFFFFF;
1099 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1100 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
1102 /* If this register is implemented */
1106 * set base = amount of IO
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
1139 return(DEVICE_TYPE_NOT_SUPPORTED);
1142 /* Get the next function */
1143 func = cpqhp_slot_find(func->bus, func->device, index++);
1152 * cpqhp_find_available_resources
1154 * Finds available memory, IO, and IRQ resources for programming
1155 * devices which may be added to the system
1156 * this function is for hot plug ADD!
1158 * returns 0 if success
1160 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1165 void __iomem *one_slot;
1166 void __iomem *rom_resource_table;
1167 struct pci_func *func = NULL;
1170 struct pci_resource *mem_node;
1171 struct pci_resource *p_mem_node;
1172 struct pci_resource *io_node;
1173 struct pci_resource *bus_node;
1175 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1176 dbg("rom_resource_table = %p\n", rom_resource_table);
1178 if (rom_resource_table == NULL)
1181 /* Sum all resources and setup resource maps */
1182 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1183 dbg("unused_IRQ = %x\n", unused_IRQ);
1186 while (unused_IRQ) {
1187 if (unused_IRQ & 1) {
1188 cpqhp_disk_irq = temp;
1191 unused_IRQ = unused_IRQ >> 1;
1195 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1196 unused_IRQ = unused_IRQ >> 1;
1199 while (unused_IRQ) {
1200 if (unused_IRQ & 1) {
1201 cpqhp_nic_irq = temp;
1204 unused_IRQ = unused_IRQ >> 1;
1208 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1209 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1214 cpqhp_nic_irq = ctrl->cfgspc_irq;
1216 if (!cpqhp_disk_irq)
1217 cpqhp_disk_irq = ctrl->cfgspc_irq;
1219 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1221 rc = compaq_nvram_load(rom_start, ctrl);
1225 one_slot = rom_resource_table + sizeof (struct hrt);
1227 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1228 dbg("number_of_entries = %d\n", i);
1230 if (!readb(one_slot + SECONDARY_BUS))
1233 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1235 while (i && readb(one_slot + SECONDARY_BUS)) {
1236 u8 dev_func = readb(one_slot + DEV_FUNC);
1237 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1238 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1239 u8 max_bus = readb(one_slot + MAX_BUS);
1240 u16 io_base = readw(one_slot + IO_BASE);
1241 u16 io_length = readw(one_slot + IO_LENGTH);
1242 u16 mem_base = readw(one_slot + MEM_BASE);
1243 u16 mem_length = readw(one_slot + MEM_LENGTH);
1244 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1245 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1247 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1248 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1249 primary_bus, secondary_bus, max_bus);
1251 /* If this entry isn't for our controller's bus, ignore it */
1252 if (primary_bus != ctrl->bus) {
1254 one_slot += sizeof (struct slot_rt);
1257 /* find out if this entry is for an occupied slot */
1258 ctrl->pci_bus->number = primary_bus;
1259 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1260 dbg("temp_D_word = %x\n", temp_dword);
1262 if (temp_dword != 0xFFFFFFFF) {
1264 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1266 while (func && (func->function != (dev_func & 0x07))) {
1267 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1268 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1271 /* If we can't find a match, skip this table entry */
1274 one_slot += sizeof (struct slot_rt);
1277 /* this may not work and shouldn't be used */
1278 if (secondary_bus != primary_bus)
1290 /* If we've got a valid IO base, use it */
1292 temp_dword = io_base + io_length;
1294 if ((io_base) && (temp_dword < 0x10000)) {
1295 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1299 io_node->base = io_base;
1300 io_node->length = io_length;
1302 dbg("found io_node(base, length) = %x, %x\n",
1303 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 = kmalloc(sizeof(*mem_node), 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",
1326 mem_node->base, mem_node->length);
1327 dbg("populated slot =%d \n", populated_slot);
1328 if (!populated_slot) {
1329 mem_node->next = ctrl->mem_head;
1330 ctrl->mem_head = mem_node;
1332 mem_node->next = func->mem_head;
1333 func->mem_head = mem_node;
1337 /* If we've got a valid prefetchable memory base, and
1338 * the base + length isn't greater than 0xFFFF
1340 temp_dword = pre_mem_base + pre_mem_length;
1341 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1342 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1346 p_mem_node->base = pre_mem_base << 16;
1348 p_mem_node->length = pre_mem_length << 16;
1349 dbg("found p_mem_node(base, length) = %x, %x\n",
1350 p_mem_node->base, p_mem_node->length);
1351 dbg("populated slot =%d \n", populated_slot);
1353 if (!populated_slot) {
1354 p_mem_node->next = ctrl->p_mem_head;
1355 ctrl->p_mem_head = p_mem_node;
1357 p_mem_node->next = func->p_mem_head;
1358 func->p_mem_head = p_mem_node;
1362 /* If we've got a valid bus number, use it
1363 * The second condition is to ignore bus numbers on
1364 * populated slots that don't have PCI-PCI bridges
1366 if (secondary_bus && (secondary_bus != primary_bus)) {
1367 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1371 bus_node->base = secondary_bus;
1372 bus_node->length = max_bus - secondary_bus + 1;
1373 dbg("found bus_node(base, length) = %x, %x\n",
1374 bus_node->base, bus_node->length);
1375 dbg("populated slot =%d \n", populated_slot);
1376 if (!populated_slot) {
1377 bus_node->next = ctrl->bus_head;
1378 ctrl->bus_head = bus_node;
1380 bus_node->next = func->bus_head;
1381 func->bus_head = bus_node;
1386 one_slot += sizeof (struct slot_rt);
1389 /* If all of the following fail, we don't have any resources for
1393 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1394 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1395 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1396 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1403 * cpqhp_return_board_resources
1405 * this routine returns all resources allocated to a board to
1406 * the available pool.
1408 * returns 0 if success
1410 int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1413 struct pci_resource *node;
1414 struct pci_resource *t_node;
1415 dbg("%s\n", __func__);
1420 node = func->io_head;
1421 func->io_head = NULL;
1423 t_node = node->next;
1424 return_resource(&(resources->io_head), node);
1428 node = func->mem_head;
1429 func->mem_head = NULL;
1431 t_node = node->next;
1432 return_resource(&(resources->mem_head), node);
1436 node = func->p_mem_head;
1437 func->p_mem_head = NULL;
1439 t_node = node->next;
1440 return_resource(&(resources->p_mem_head), node);
1444 node = func->bus_head;
1445 func->bus_head = NULL;
1447 t_node = node->next;
1448 return_resource(&(resources->bus_head), node);
1452 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1453 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1454 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1455 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1462 * cpqhp_destroy_resource_list
1464 * Puts node back in the resource list pointed to by head
1466 void cpqhp_destroy_resource_list (struct resource_lists * resources)
1468 struct pci_resource *res, *tres;
1470 res = resources->io_head;
1471 resources->io_head = NULL;
1479 res = resources->mem_head;
1480 resources->mem_head = NULL;
1488 res = resources->p_mem_head;
1489 resources->p_mem_head = NULL;
1497 res = resources->bus_head;
1498 resources->bus_head = NULL;
1509 * cpqhp_destroy_board_resources
1511 * Puts node back in the resource list pointed to by head
1513 void cpqhp_destroy_board_resources (struct pci_func * func)
1515 struct pci_resource *res, *tres;
1517 res = func->io_head;
1518 func->io_head = NULL;
1526 res = func->mem_head;
1527 func->mem_head = NULL;
1535 res = func->p_mem_head;
1536 func->p_mem_head = NULL;
1544 res = func->bus_head;
1545 func->bus_head = NULL;