1 // SPDX-License-Identifier: GPL-2.0+
3 * Compaq Hot Plug Controller Driver
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
7 * Copyright (C) 2001 IBM Corp.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pci.h>
22 #include <linux/pci_hotplug.h>
25 #include "cpqphp_nvram.h"
31 static u16 unused_IRQ;
34 * detect_HRT_floating_pointer
36 * find the Hot Plug Resource Table in the specified region of memory.
39 static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
43 u8 temp1, temp2, temp3, temp4;
46 endp = (end - sizeof(struct hrt) + 1);
48 for (fp = begin; fp <= endp; fp += 16) {
49 temp1 = readb(fp + SIG0);
50 temp2 = readb(fp + SIG1);
51 temp3 = readb(fp + SIG2);
52 temp4 = readb(fp + SIG3);
65 dbg("Discovered Hotplug Resource Table at %p\n", fp);
70 int cpqhp_configure_device(struct controller *ctrl, struct pci_func *func)
72 struct pci_bus *child;
75 pci_lock_rescan_remove();
77 if (func->pci_dev == NULL)
78 func->pci_dev = pci_get_domain_bus_and_slot(0, func->bus,
79 PCI_DEVFN(func->device,
82 /* No pci device, we need to create it then */
83 if (func->pci_dev == NULL) {
84 dbg("INFO: pci_dev still null\n");
86 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
88 pci_bus_add_devices(ctrl->pci_dev->bus);
90 func->pci_dev = pci_get_domain_bus_and_slot(0, func->bus,
91 PCI_DEVFN(func->device,
93 if (func->pci_dev == NULL) {
94 dbg("ERROR: pci_dev still null\n");
99 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
100 pci_hp_add_bridge(func->pci_dev);
101 child = func->pci_dev->subordinate;
103 pci_bus_add_devices(child);
106 pci_dev_put(func->pci_dev);
109 pci_unlock_rescan_remove();
114 int cpqhp_unconfigure_device(struct pci_func *func)
118 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
120 pci_lock_rescan_remove();
121 for (j = 0; j < 8 ; j++) {
122 struct pci_dev *temp = pci_get_domain_bus_and_slot(0,
124 PCI_DEVFN(func->device,
128 pci_stop_and_remove_bus_device(temp);
131 pci_unlock_rescan_remove();
135 static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
139 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
141 if (vendID == 0xffffffff)
143 return pci_bus_read_config_dword(bus, devfn, offset, value);
150 * @bus_num: bus number of PCI device
151 * @dev_num: device number of PCI device
152 * @slot: pointer to u8 where slot number will be returned
154 int cpqhp_set_irq(u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
158 if (cpqhp_legacy_mode) {
159 struct pci_dev *fakedev;
160 struct pci_bus *fakebus;
163 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
164 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
165 if (!fakedev || !fakebus) {
171 fakedev->devfn = dev_num << 3;
172 fakedev->bus = fakebus;
173 fakebus->number = bus_num;
174 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
175 __func__, dev_num, bus_num, int_pin, irq_num);
176 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
179 dbg("%s: rc %d\n", __func__, rc);
183 /* set the Edge Level Control Register (ELCR) */
184 temp_word = inb(0x4d0);
185 temp_word |= inb(0x4d1) << 8;
187 temp_word |= 0x01 << irq_num;
189 /* This should only be for x86 as it sets the Edge Level
192 outb((u8)(temp_word & 0xFF), 0x4d0);
193 outb((u8)((temp_word & 0xFF00) >> 8), 0x4d1);
201 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 *dev_num)
207 ctrl->pci_bus->number = bus_num;
209 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
210 /* Scan for access first */
211 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
213 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
214 /* Yep we got one. Not a bridge ? */
215 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
221 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
222 /* Scan for access first */
223 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
225 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
226 /* Yep we got one. bridge ? */
227 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
228 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
229 /* XXX: no recursion, wtf? */
230 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
239 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
243 u8 tbus, tdevice, tslot;
245 len = cpqhp_routing_table_length();
246 for (loop = 0; loop < len; ++loop) {
247 tbus = cpqhp_routing_table->slots[loop].bus;
248 tdevice = cpqhp_routing_table->slots[loop].devfn;
249 tslot = cpqhp_routing_table->slots[loop].slot;
254 ctrl->pci_bus->number = tbus;
255 pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
256 if (!nobridge || (work == 0xffffffff))
259 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
260 pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
261 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
263 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
264 pci_bus_read_config_byte(ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
265 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
266 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
278 int cpqhp_get_bus_dev(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot)
280 /* plain (bridges allowed) */
281 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
285 /* More PCI configuration routines; this time centered around hotplug
293 * Reads configuration for all slots in a PCI bus and saves info.
295 * Note: For non-hot plug buses, the slot # saved is the device #
297 * returns 0 if success
299 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
306 struct pci_func *new_slot;
319 /* Decide which slots are supported */
323 * is_hot_plug is the slot mask
325 FirstSupported = is_hot_plug >> 4;
326 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
329 LastSupported = 0x1F;
332 /* Save PCI configuration space for all devices in supported slots */
333 ctrl->pci_bus->number = busnumber;
334 for (device = FirstSupported; device <= LastSupported; device++) {
336 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
338 if (ID == 0xFFFFFFFF) {
340 /* Setup slot structure with entry for empty
343 new_slot = cpqhp_slot_create(busnumber);
344 if (new_slot == NULL)
347 new_slot->bus = (u8) busnumber;
348 new_slot->device = (u8) device;
349 new_slot->function = 0;
350 new_slot->is_a_board = 0;
351 new_slot->presence_save = 0;
352 new_slot->switch_save = 0;
357 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
361 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
365 /* If multi-function device, set max_functions to 8 */
366 if (header_type & PCI_HEADER_TYPE_MFD)
375 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
376 /* Recurse the subordinate bus
377 * get the subordinate bus number
379 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
383 sub_bus = (int) secondary_bus;
385 /* Save secondary bus cfg spc
386 * with this recursive call.
388 rc = cpqhp_save_config(ctrl, sub_bus, 0);
391 ctrl->pci_bus->number = busnumber;
396 new_slot = cpqhp_slot_find(busnumber, device, index++);
398 (new_slot->function != (u8) function))
399 new_slot = cpqhp_slot_find(busnumber, device, index++);
402 /* Setup slot structure. */
403 new_slot = cpqhp_slot_create(busnumber);
404 if (new_slot == NULL)
408 new_slot->bus = (u8) busnumber;
409 new_slot->device = (u8) device;
410 new_slot->function = (u8) function;
411 new_slot->is_a_board = 1;
412 new_slot->switch_save = 0x10;
413 /* In case of unsupported board */
414 new_slot->status = DevError;
415 devfn = (new_slot->device << 3) | new_slot->function;
416 new_slot->pci_dev = pci_get_domain_bus_and_slot(0,
417 new_slot->bus, devfn);
419 for (cloop = 0; cloop < 0x20; cloop++) {
420 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
425 pci_dev_put(new_slot->pci_dev);
431 /* this loop skips to the next present function
432 * reading in Class Code and Header type.
434 while ((function < max_functions) && (!stop_it)) {
435 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
436 if (ID == 0xFFFFFFFF) {
440 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
444 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
451 } while (function < max_functions);
452 } /* End of FOR loop */
459 * cpqhp_save_slot_config
461 * Saves configuration info for all PCI devices in a given slot
462 * including subordinate buses.
464 * returns 0 if success
466 int cpqhp_save_slot_config(struct controller *ctrl, struct pci_func *new_slot)
481 ctrl->pci_bus->number = new_slot->bus;
482 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
484 if (ID == 0xFFFFFFFF)
487 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
488 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
490 if (header_type & PCI_HEADER_TYPE_MFD)
495 while (function < max_functions) {
496 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
497 /* Recurse the subordinate bus */
498 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
500 sub_bus = (int) secondary_bus;
502 /* Save the config headers for the secondary
505 rc = cpqhp_save_config(ctrl, sub_bus, 0);
508 ctrl->pci_bus->number = new_slot->bus;
512 new_slot->status = 0;
514 for (cloop = 0; cloop < 0x20; cloop++)
515 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
521 /* this loop skips to the next present function
522 * reading in the Class Code and the Header type.
524 while ((function < max_functions) && (!stop_it)) {
525 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
527 if (ID == 0xFFFFFFFF)
530 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
531 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
543 * cpqhp_save_base_addr_length
545 * Saves the length of all base address registers for the
546 * specified slot. this is for hot plug REPLACE
548 * returns 0 if success
550 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func *func)
560 struct pci_func *next;
562 struct pci_bus *pci_bus = ctrl->pci_bus;
565 func = cpqhp_slot_find(func->bus, func->device, index++);
567 while (func != NULL) {
568 pci_bus->number = func->bus;
569 devfn = PCI_DEVFN(func->device, func->function);
571 /* Check for Bridge */
572 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
574 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
575 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
577 sub_bus = (int) secondary_bus;
579 next = cpqhp_slot_list[sub_bus];
581 while (next != NULL) {
582 rc = cpqhp_save_base_addr_length(ctrl, next);
588 pci_bus->number = func->bus;
590 /* FIXME: this loop is duplicated in the non-bridge
591 * case. The two could be rolled together Figure out
592 * IO and memory base lengths
594 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
595 temp_register = 0xFFFFFFFF;
596 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
597 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
598 /* If this register is implemented */
602 * set base = amount of IO space
605 base = base & 0xFFFFFFFE;
611 base = base & 0xFFFFFFF0;
621 /* Save information in slot structure */
622 func->base_length[(cloop - 0x10) >> 2] =
624 func->base_type[(cloop - 0x10) >> 2] = type;
626 } /* End of base register loop */
628 } else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
629 /* Figure out IO and memory base lengths */
630 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
631 temp_register = 0xFFFFFFFF;
632 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
633 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
635 /* If this register is implemented */
639 * base = amount of IO space
642 base = base & 0xFFFFFFFE;
648 * base = amount of memory
651 base = base & 0xFFFFFFF0;
661 /* Save information in slot structure */
662 func->base_length[(cloop - 0x10) >> 2] = base;
663 func->base_type[(cloop - 0x10) >> 2] = type;
665 } /* End of base register loop */
667 } else { /* Some other unknown header type */
670 /* find the next device in this slot */
671 func = cpqhp_slot_find(func->bus, func->device, index++);
679 * cpqhp_save_used_resources
681 * Stores used resource information for existing boards. this is
682 * for boards that were in the system when this driver was loaded.
683 * this function is for hot plug ADD
685 * returns 0 if success
687 int cpqhp_save_used_resources(struct controller *ctrl, struct pci_func *func)
703 struct pci_resource *mem_node;
704 struct pci_resource *p_mem_node;
705 struct pci_resource *io_node;
706 struct pci_resource *bus_node;
707 struct pci_bus *pci_bus = ctrl->pci_bus;
710 func = cpqhp_slot_find(func->bus, func->device, index++);
712 while ((func != NULL) && func->is_a_board) {
713 pci_bus->number = func->bus;
714 devfn = PCI_DEVFN(func->device, func->function);
716 /* Save the command register */
717 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
721 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
723 /* Check for Bridge */
724 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
726 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
727 /* Clear Bridge Control Register */
729 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
730 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
731 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
733 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
737 bus_node->base = secondary_bus;
738 bus_node->length = temp_byte - secondary_bus + 1;
740 bus_node->next = func->bus_head;
741 func->bus_head = bus_node;
743 /* Save IO base and Limit registers */
744 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
745 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
747 if ((b_base <= b_length) && (save_command & 0x01)) {
748 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
752 io_node->base = (b_base & 0xF0) << 8;
753 io_node->length = (b_length - b_base + 0x10) << 8;
755 io_node->next = func->io_head;
756 func->io_head = io_node;
759 /* Save memory base and Limit registers */
760 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
761 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
763 if ((w_base <= w_length) && (save_command & 0x02)) {
764 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
768 mem_node->base = w_base << 16;
769 mem_node->length = (w_length - w_base + 0x10) << 16;
771 mem_node->next = func->mem_head;
772 func->mem_head = mem_node;
775 /* Save prefetchable memory base and Limit registers */
776 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
777 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
779 if ((w_base <= w_length) && (save_command & 0x02)) {
780 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
784 p_mem_node->base = w_base << 16;
785 p_mem_node->length = (w_length - w_base + 0x10) << 16;
787 p_mem_node->next = func->p_mem_head;
788 func->p_mem_head = p_mem_node;
790 /* Figure out IO and memory base lengths */
791 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
792 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
794 temp_register = 0xFFFFFFFF;
795 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
796 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
798 temp_register = base;
800 /* If this register is implemented */
802 if (((base & 0x03L) == 0x01)
803 && (save_command & 0x01)) {
805 * set temp_register = amount
806 * of IO space requested
808 temp_register = base & 0xFFFFFFFE;
809 temp_register = (~temp_register) + 1;
811 io_node = kmalloc(sizeof(*io_node),
817 save_base & (~0x03L);
818 io_node->length = temp_register;
820 io_node->next = func->io_head;
821 func->io_head = io_node;
823 if (((base & 0x0BL) == 0x08)
824 && (save_command & 0x02)) {
825 /* prefetchable memory base */
826 temp_register = base & 0xFFFFFFF0;
827 temp_register = (~temp_register) + 1;
829 p_mem_node = kmalloc(sizeof(*p_mem_node),
834 p_mem_node->base = save_base & (~0x0FL);
835 p_mem_node->length = temp_register;
837 p_mem_node->next = func->p_mem_head;
838 func->p_mem_head = p_mem_node;
840 if (((base & 0x0BL) == 0x00)
841 && (save_command & 0x02)) {
842 /* prefetchable memory base */
843 temp_register = base & 0xFFFFFFF0;
844 temp_register = (~temp_register) + 1;
846 mem_node = kmalloc(sizeof(*mem_node),
851 mem_node->base = save_base & (~0x0FL);
852 mem_node->length = temp_register;
854 mem_node->next = func->mem_head;
855 func->mem_head = mem_node;
859 } /* End of base register loop */
860 /* Standard header */
861 } else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
862 /* Figure out IO and memory base lengths */
863 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
864 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
866 temp_register = 0xFFFFFFFF;
867 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
868 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
870 temp_register = base;
872 /* If this register is implemented */
874 if (((base & 0x03L) == 0x01)
875 && (save_command & 0x01)) {
877 * set temp_register = amount
878 * of IO space requested
880 temp_register = base & 0xFFFFFFFE;
881 temp_register = (~temp_register) + 1;
883 io_node = kmalloc(sizeof(*io_node),
888 io_node->base = save_base & (~0x01L);
889 io_node->length = temp_register;
891 io_node->next = func->io_head;
892 func->io_head = io_node;
894 if (((base & 0x0BL) == 0x08)
895 && (save_command & 0x02)) {
896 /* prefetchable memory base */
897 temp_register = base & 0xFFFFFFF0;
898 temp_register = (~temp_register) + 1;
900 p_mem_node = kmalloc(sizeof(*p_mem_node),
905 p_mem_node->base = save_base & (~0x0FL);
906 p_mem_node->length = temp_register;
908 p_mem_node->next = func->p_mem_head;
909 func->p_mem_head = p_mem_node;
911 if (((base & 0x0BL) == 0x00)
912 && (save_command & 0x02)) {
913 /* prefetchable memory base */
914 temp_register = base & 0xFFFFFFF0;
915 temp_register = (~temp_register) + 1;
917 mem_node = kmalloc(sizeof(*mem_node),
922 mem_node->base = save_base & (~0x0FL);
923 mem_node->length = temp_register;
925 mem_node->next = func->mem_head;
926 func->mem_head = mem_node;
930 } /* End of base register loop */
933 /* find the next device in this slot */
934 func = cpqhp_slot_find(func->bus, func->device, index++);
942 * cpqhp_configure_board
944 * Copies saved configuration information to one slot.
945 * this is called recursively for bridge devices.
946 * this is for hot plug REPLACE!
948 * returns 0 if success
950 int cpqhp_configure_board(struct controller *ctrl, struct pci_func *func)
956 struct pci_func *next;
960 struct pci_bus *pci_bus = ctrl->pci_bus;
963 func = cpqhp_slot_find(func->bus, func->device, index++);
965 while (func != NULL) {
966 pci_bus->number = func->bus;
967 devfn = PCI_DEVFN(func->device, func->function);
969 /* Start at the top of config space so that the control
970 * registers are programmed last
972 for (cloop = 0x3C; cloop > 0; cloop -= 4)
973 pci_bus_write_config_dword(pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
975 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
977 /* If this is a bridge device, restore subordinate devices */
978 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
979 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
981 sub_bus = (int) secondary_bus;
983 next = cpqhp_slot_list[sub_bus];
985 while (next != NULL) {
986 rc = cpqhp_configure_board(ctrl, next);
994 /* Check all the base Address Registers to make sure
995 * they are the same. If not, the board is different.
998 for (cloop = 16; cloop < 40; cloop += 4) {
999 pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp);
1001 if (temp != func->config_space[cloop >> 2]) {
1002 dbg("Config space compare failure!!! offset = %x\n", cloop);
1003 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1004 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1010 func->configured = 1;
1012 func = cpqhp_slot_find(func->bus, func->device, index++);
1020 * cpqhp_valid_replace
1022 * this function checks to see if a board is the same as the
1023 * one it is replacing. this check will detect if the device's
1024 * vendor or device id's are the same
1026 * returns 0 if the board is the same nonzero otherwise
1028 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func *func)
1034 u32 temp_register = 0;
1037 struct pci_func *next;
1039 struct pci_bus *pci_bus = ctrl->pci_bus;
1042 if (!func->is_a_board)
1043 return(ADD_NOT_SUPPORTED);
1045 func = cpqhp_slot_find(func->bus, func->device, index++);
1047 while (func != NULL) {
1048 pci_bus->number = func->bus;
1049 devfn = PCI_DEVFN(func->device, func->function);
1051 pci_bus_read_config_dword(pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1053 /* No adapter present */
1054 if (temp_register == 0xFFFFFFFF)
1055 return(NO_ADAPTER_PRESENT);
1057 if (temp_register != func->config_space[0])
1058 return(ADAPTER_NOT_SAME);
1060 /* Check for same revision number and class code */
1061 pci_bus_read_config_dword(pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1063 /* Adapter not the same */
1064 if (temp_register != func->config_space[0x08 >> 2])
1065 return(ADAPTER_NOT_SAME);
1067 /* Check for Bridge */
1068 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1070 if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
1071 /* In order to continue checking, we must program the
1072 * bus registers in the bridge to respond to accesses
1073 * for its subordinate bus(es)
1076 temp_register = func->config_space[0x18 >> 2];
1077 pci_bus_write_config_dword(pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1079 secondary_bus = (temp_register >> 8) & 0xFF;
1081 next = cpqhp_slot_list[secondary_bus];
1083 while (next != NULL) {
1084 rc = cpqhp_valid_replace(ctrl, next);
1092 /* Check to see if it is a standard config header */
1093 else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
1094 /* Check subsystem vendor and ID */
1095 pci_bus_read_config_dword(pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1097 if (temp_register != func->config_space[0x2C >> 2]) {
1098 /* If it's a SMART-2 and the register isn't
1099 * filled in, ignore the difference because
1100 * they just have an old rev of the firmware
1102 if (!((func->config_space[0] == 0xAE100E11)
1103 && (temp_register == 0x00L)))
1104 return(ADAPTER_NOT_SAME);
1106 /* Figure out IO and memory base lengths */
1107 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1108 temp_register = 0xFFFFFFFF;
1109 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
1110 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
1112 /* If this register is implemented */
1116 * set base = amount of IO
1119 base = base & 0xFFFFFFFE;
1125 base = base & 0xFFFFFFF0;
1135 /* Check information in slot structure */
1136 if (func->base_length[(cloop - 0x10) >> 2] != base)
1137 return(ADAPTER_NOT_SAME);
1139 if (func->base_type[(cloop - 0x10) >> 2] != type)
1140 return(ADAPTER_NOT_SAME);
1142 } /* End of base register loop */
1144 } /* End of (type 0 config space) else */
1146 /* this is not a type 0 or 1 config space header so
1147 * we don't know how to do it
1149 return(DEVICE_TYPE_NOT_SUPPORTED);
1152 /* Get the next function */
1153 func = cpqhp_slot_find(func->bus, func->device, index++);
1162 * cpqhp_find_available_resources
1164 * Finds available memory, IO, and IRQ resources for programming
1165 * devices which may be added to the system
1166 * this function is for hot plug ADD!
1168 * returns 0 if success
1170 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1175 void __iomem *one_slot;
1176 void __iomem *rom_resource_table;
1177 struct pci_func *func = NULL;
1180 struct pci_resource *mem_node;
1181 struct pci_resource *p_mem_node;
1182 struct pci_resource *io_node;
1183 struct pci_resource *bus_node;
1185 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1186 dbg("rom_resource_table = %p\n", rom_resource_table);
1188 if (rom_resource_table == NULL)
1191 /* Sum all resources and setup resource maps */
1192 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1193 dbg("unused_IRQ = %x\n", unused_IRQ);
1196 while (unused_IRQ) {
1197 if (unused_IRQ & 1) {
1198 cpqhp_disk_irq = temp;
1201 unused_IRQ = unused_IRQ >> 1;
1205 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1206 unused_IRQ = unused_IRQ >> 1;
1209 while (unused_IRQ) {
1210 if (unused_IRQ & 1) {
1211 cpqhp_nic_irq = temp;
1214 unused_IRQ = unused_IRQ >> 1;
1218 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1219 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1224 cpqhp_nic_irq = ctrl->cfgspc_irq;
1226 if (!cpqhp_disk_irq)
1227 cpqhp_disk_irq = ctrl->cfgspc_irq;
1229 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1231 rc = compaq_nvram_load(rom_start, ctrl);
1235 one_slot = rom_resource_table + sizeof(struct hrt);
1237 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1238 dbg("number_of_entries = %d\n", i);
1240 if (!readb(one_slot + SECONDARY_BUS))
1243 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1245 while (i && readb(one_slot + SECONDARY_BUS)) {
1246 u8 dev_func = readb(one_slot + DEV_FUNC);
1247 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1248 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1249 u8 max_bus = readb(one_slot + MAX_BUS);
1250 u16 io_base = readw(one_slot + IO_BASE);
1251 u16 io_length = readw(one_slot + IO_LENGTH);
1252 u16 mem_base = readw(one_slot + MEM_BASE);
1253 u16 mem_length = readw(one_slot + MEM_LENGTH);
1254 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1255 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1257 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1258 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1259 primary_bus, secondary_bus, max_bus);
1261 /* If this entry isn't for our controller's bus, ignore it */
1262 if (primary_bus != ctrl->bus) {
1264 one_slot += sizeof(struct slot_rt);
1267 /* find out if this entry is for an occupied slot */
1268 ctrl->pci_bus->number = primary_bus;
1269 pci_bus_read_config_dword(ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1270 dbg("temp_D_word = %x\n", temp_dword);
1272 if (temp_dword != 0xFFFFFFFF) {
1274 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1276 while (func && (func->function != (dev_func & 0x07))) {
1277 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1278 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1281 /* If we can't find a match, skip this table entry */
1284 one_slot += sizeof(struct slot_rt);
1287 /* this may not work and shouldn't be used */
1288 if (secondary_bus != primary_bus)
1300 /* If we've got a valid IO base, use it */
1302 temp_dword = io_base + io_length;
1304 if ((io_base) && (temp_dword < 0x10000)) {
1305 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1309 io_node->base = io_base;
1310 io_node->length = io_length;
1312 dbg("found io_node(base, length) = %x, %x\n",
1313 io_node->base, io_node->length);
1314 dbg("populated slot =%d \n", populated_slot);
1315 if (!populated_slot) {
1316 io_node->next = ctrl->io_head;
1317 ctrl->io_head = io_node;
1319 io_node->next = func->io_head;
1320 func->io_head = io_node;
1324 /* If we've got a valid memory base, use it */
1325 temp_dword = mem_base + mem_length;
1326 if ((mem_base) && (temp_dword < 0x10000)) {
1327 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1331 mem_node->base = mem_base << 16;
1333 mem_node->length = mem_length << 16;
1335 dbg("found mem_node(base, length) = %x, %x\n",
1336 mem_node->base, mem_node->length);
1337 dbg("populated slot =%d \n", populated_slot);
1338 if (!populated_slot) {
1339 mem_node->next = ctrl->mem_head;
1340 ctrl->mem_head = mem_node;
1342 mem_node->next = func->mem_head;
1343 func->mem_head = mem_node;
1347 /* If we've got a valid prefetchable memory base, and
1348 * the base + length isn't greater than 0xFFFF
1350 temp_dword = pre_mem_base + pre_mem_length;
1351 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1352 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1356 p_mem_node->base = pre_mem_base << 16;
1358 p_mem_node->length = pre_mem_length << 16;
1359 dbg("found p_mem_node(base, length) = %x, %x\n",
1360 p_mem_node->base, p_mem_node->length);
1361 dbg("populated slot =%d \n", populated_slot);
1363 if (!populated_slot) {
1364 p_mem_node->next = ctrl->p_mem_head;
1365 ctrl->p_mem_head = p_mem_node;
1367 p_mem_node->next = func->p_mem_head;
1368 func->p_mem_head = p_mem_node;
1372 /* If we've got a valid bus number, use it
1373 * The second condition is to ignore bus numbers on
1374 * populated slots that don't have PCI-PCI bridges
1376 if (secondary_bus && (secondary_bus != primary_bus)) {
1377 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1381 bus_node->base = secondary_bus;
1382 bus_node->length = max_bus - secondary_bus + 1;
1383 dbg("found bus_node(base, length) = %x, %x\n",
1384 bus_node->base, bus_node->length);
1385 dbg("populated slot =%d \n", populated_slot);
1386 if (!populated_slot) {
1387 bus_node->next = ctrl->bus_head;
1388 ctrl->bus_head = bus_node;
1390 bus_node->next = func->bus_head;
1391 func->bus_head = bus_node;
1396 one_slot += sizeof(struct slot_rt);
1399 /* If all of the following fail, we don't have any resources for
1403 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1404 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1405 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1406 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1413 * cpqhp_return_board_resources
1415 * this routine returns all resources allocated to a board to
1416 * the available pool.
1418 * returns 0 if success
1420 int cpqhp_return_board_resources(struct pci_func *func, struct resource_lists *resources)
1423 struct pci_resource *node;
1424 struct pci_resource *t_node;
1425 dbg("%s\n", __func__);
1430 node = func->io_head;
1431 func->io_head = NULL;
1433 t_node = node->next;
1434 return_resource(&(resources->io_head), node);
1438 node = func->mem_head;
1439 func->mem_head = NULL;
1441 t_node = node->next;
1442 return_resource(&(resources->mem_head), node);
1446 node = func->p_mem_head;
1447 func->p_mem_head = NULL;
1449 t_node = node->next;
1450 return_resource(&(resources->p_mem_head), node);
1454 node = func->bus_head;
1455 func->bus_head = NULL;
1457 t_node = node->next;
1458 return_resource(&(resources->bus_head), node);
1462 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1463 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1464 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1465 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1472 * cpqhp_destroy_resource_list
1474 * Puts node back in the resource list pointed to by head
1476 void cpqhp_destroy_resource_list(struct resource_lists *resources)
1478 struct pci_resource *res, *tres;
1480 res = resources->io_head;
1481 resources->io_head = NULL;
1489 res = resources->mem_head;
1490 resources->mem_head = NULL;
1498 res = resources->p_mem_head;
1499 resources->p_mem_head = NULL;
1507 res = resources->bus_head;
1508 resources->bus_head = NULL;
1519 * cpqhp_destroy_board_resources
1521 * Puts node back in the resource list pointed to by head
1523 void cpqhp_destroy_board_resources(struct pci_func *func)
1525 struct pci_resource *res, *tres;
1527 res = func->io_head;
1528 func->io_head = NULL;
1536 res = func->mem_head;
1537 func->mem_head = NULL;
1545 res = func->p_mem_head;
1546 func->p_mem_head = NULL;
1554 res = func->bus_head;
1555 func->bus_head = NULL;