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); outb((u8) ((temp_word &
193 0xFF00) >> 8), 0x4d1); rc = 0; }
199 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 *dev_num)
205 ctrl->pci_bus->number = bus_num;
207 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
208 /* Scan for access first */
209 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
211 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
212 /* Yep we got one. Not a bridge ? */
213 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
219 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
220 /* Scan for access first */
221 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
223 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
224 /* Yep we got one. bridge ? */
225 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
226 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
227 /* XXX: no recursion, wtf? */
228 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
237 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
241 u8 tbus, tdevice, tslot;
243 len = cpqhp_routing_table_length();
244 for (loop = 0; loop < len; ++loop) {
245 tbus = cpqhp_routing_table->slots[loop].bus;
246 tdevice = cpqhp_routing_table->slots[loop].devfn;
247 tslot = cpqhp_routing_table->slots[loop].slot;
252 ctrl->pci_bus->number = tbus;
253 pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
254 if (!nobridge || (work == 0xffffffff))
257 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
258 pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
259 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
261 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
262 pci_bus_read_config_byte(ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
263 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
264 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
276 int cpqhp_get_bus_dev(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot)
278 /* plain (bridges allowed) */
279 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
283 /* More PCI configuration routines; this time centered around hotplug
291 * Reads configuration for all slots in a PCI bus and saves info.
293 * Note: For non-hot plug buses, the slot # saved is the device #
295 * returns 0 if success
297 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
304 struct pci_func *new_slot;
317 /* Decide which slots are supported */
321 * is_hot_plug is the slot mask
323 FirstSupported = is_hot_plug >> 4;
324 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
327 LastSupported = 0x1F;
330 /* Save PCI configuration space for all devices in supported slots */
331 ctrl->pci_bus->number = busnumber;
332 for (device = FirstSupported; device <= LastSupported; device++) {
334 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
336 if (ID == 0xFFFFFFFF) {
338 /* Setup slot structure with entry for empty
341 new_slot = cpqhp_slot_create(busnumber);
342 if (new_slot == NULL)
345 new_slot->bus = (u8) busnumber;
346 new_slot->device = (u8) device;
347 new_slot->function = 0;
348 new_slot->is_a_board = 0;
349 new_slot->presence_save = 0;
350 new_slot->switch_save = 0;
355 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
359 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
363 /* If multi-function device, set max_functions to 8 */
364 if (header_type & 0x80)
373 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
374 /* Recurse the subordinate bus
375 * get the subordinate bus number
377 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
381 sub_bus = (int) secondary_bus;
383 /* Save secondary bus cfg spc
384 * with this recursive call.
386 rc = cpqhp_save_config(ctrl, sub_bus, 0);
389 ctrl->pci_bus->number = busnumber;
394 new_slot = cpqhp_slot_find(busnumber, device, index++);
396 (new_slot->function != (u8) function))
397 new_slot = cpqhp_slot_find(busnumber, device, index++);
400 /* Setup slot structure. */
401 new_slot = cpqhp_slot_create(busnumber);
402 if (new_slot == NULL)
406 new_slot->bus = (u8) busnumber;
407 new_slot->device = (u8) device;
408 new_slot->function = (u8) function;
409 new_slot->is_a_board = 1;
410 new_slot->switch_save = 0x10;
411 /* In case of unsupported board */
412 new_slot->status = DevError;
413 devfn = (new_slot->device << 3) | new_slot->function;
414 new_slot->pci_dev = pci_get_domain_bus_and_slot(0,
415 new_slot->bus, devfn);
417 for (cloop = 0; cloop < 0x20; cloop++) {
418 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
423 pci_dev_put(new_slot->pci_dev);
429 /* this loop skips to the next present function
430 * reading in Class Code and Header type.
432 while ((function < max_functions) && (!stop_it)) {
433 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
434 if (ID == 0xFFFFFFFF) {
438 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
442 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
449 } while (function < max_functions);
450 } /* End of FOR loop */
457 * cpqhp_save_slot_config
459 * Saves configuration info for all PCI devices in a given slot
460 * including subordinate buses.
462 * returns 0 if success
464 int cpqhp_save_slot_config(struct controller *ctrl, struct pci_func *new_slot)
479 ctrl->pci_bus->number = new_slot->bus;
480 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
482 if (ID == 0xFFFFFFFF)
485 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
486 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
488 if (header_type & 0x80) /* Multi-function device */
493 while (function < max_functions) {
494 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
495 /* Recurse the subordinate bus */
496 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
498 sub_bus = (int) secondary_bus;
500 /* Save the config headers for the secondary
503 rc = cpqhp_save_config(ctrl, sub_bus, 0);
506 ctrl->pci_bus->number = new_slot->bus;
510 new_slot->status = 0;
512 for (cloop = 0; cloop < 0x20; cloop++)
513 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
519 /* this loop skips to the next present function
520 * reading in the Class Code and the Header type.
522 while ((function < max_functions) && (!stop_it)) {
523 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
525 if (ID == 0xFFFFFFFF)
528 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
529 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
541 * cpqhp_save_base_addr_length
543 * Saves the length of all base address registers for the
544 * specified slot. this is for hot plug REPLACE
546 * returns 0 if success
548 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func *func)
558 struct pci_func *next;
560 struct pci_bus *pci_bus = ctrl->pci_bus;
563 func = cpqhp_slot_find(func->bus, func->device, index++);
565 while (func != NULL) {
566 pci_bus->number = func->bus;
567 devfn = PCI_DEVFN(func->device, func->function);
569 /* Check for Bridge */
570 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
572 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
573 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
575 sub_bus = (int) secondary_bus;
577 next = cpqhp_slot_list[sub_bus];
579 while (next != NULL) {
580 rc = cpqhp_save_base_addr_length(ctrl, next);
586 pci_bus->number = func->bus;
588 /* FIXME: this loop is duplicated in the non-bridge
589 * case. The two could be rolled together Figure out
590 * IO and memory base lengths
592 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
593 temp_register = 0xFFFFFFFF;
594 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
595 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
596 /* If this register is implemented */
600 * set base = amount of IO space
603 base = base & 0xFFFFFFFE;
609 base = base & 0xFFFFFFF0;
619 /* Save information in slot structure */
620 func->base_length[(cloop - 0x10) >> 2] =
622 func->base_type[(cloop - 0x10) >> 2] = type;
624 } /* End of base register loop */
626 } else if ((header_type & 0x7F) == 0x00) {
627 /* Figure out IO and memory base lengths */
628 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
629 temp_register = 0xFFFFFFFF;
630 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
631 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
633 /* If this register is implemented */
637 * base = amount of IO space
640 base = base & 0xFFFFFFFE;
646 * base = amount of memory
649 base = base & 0xFFFFFFF0;
659 /* Save information in slot structure */
660 func->base_length[(cloop - 0x10) >> 2] = base;
661 func->base_type[(cloop - 0x10) >> 2] = type;
663 } /* End of base register loop */
665 } else { /* Some other unknown header type */
668 /* find the next device in this slot */
669 func = cpqhp_slot_find(func->bus, func->device, index++);
677 * cpqhp_save_used_resources
679 * Stores used resource information for existing boards. this is
680 * for boards that were in the system when this driver was loaded.
681 * this function is for hot plug ADD
683 * returns 0 if success
685 int cpqhp_save_used_resources(struct controller *ctrl, struct pci_func *func)
701 struct pci_resource *mem_node;
702 struct pci_resource *p_mem_node;
703 struct pci_resource *io_node;
704 struct pci_resource *bus_node;
705 struct pci_bus *pci_bus = ctrl->pci_bus;
708 func = cpqhp_slot_find(func->bus, func->device, index++);
710 while ((func != NULL) && func->is_a_board) {
711 pci_bus->number = func->bus;
712 devfn = PCI_DEVFN(func->device, func->function);
714 /* Save the command register */
715 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
719 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
721 /* Check for Bridge */
722 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
724 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
725 /* Clear Bridge Control Register */
727 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
728 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
729 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
731 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
735 bus_node->base = secondary_bus;
736 bus_node->length = temp_byte - secondary_bus + 1;
738 bus_node->next = func->bus_head;
739 func->bus_head = bus_node;
741 /* Save IO base and Limit registers */
742 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
743 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
745 if ((b_base <= b_length) && (save_command & 0x01)) {
746 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
750 io_node->base = (b_base & 0xF0) << 8;
751 io_node->length = (b_length - b_base + 0x10) << 8;
753 io_node->next = func->io_head;
754 func->io_head = io_node;
757 /* Save memory base and Limit registers */
758 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
759 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
761 if ((w_base <= w_length) && (save_command & 0x02)) {
762 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
766 mem_node->base = w_base << 16;
767 mem_node->length = (w_length - w_base + 0x10) << 16;
769 mem_node->next = func->mem_head;
770 func->mem_head = mem_node;
773 /* Save prefetchable memory base and Limit registers */
774 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
775 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
777 if ((w_base <= w_length) && (save_command & 0x02)) {
778 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
782 p_mem_node->base = w_base << 16;
783 p_mem_node->length = (w_length - w_base + 0x10) << 16;
785 p_mem_node->next = func->p_mem_head;
786 func->p_mem_head = p_mem_node;
788 /* Figure out IO and memory base lengths */
789 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
790 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
792 temp_register = 0xFFFFFFFF;
793 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
794 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
796 temp_register = base;
798 /* If this register is implemented */
800 if (((base & 0x03L) == 0x01)
801 && (save_command & 0x01)) {
803 * set temp_register = amount
804 * of IO space requested
806 temp_register = base & 0xFFFFFFFE;
807 temp_register = (~temp_register) + 1;
809 io_node = kmalloc(sizeof(*io_node),
815 save_base & (~0x03L);
816 io_node->length = temp_register;
818 io_node->next = func->io_head;
819 func->io_head = io_node;
821 if (((base & 0x0BL) == 0x08)
822 && (save_command & 0x02)) {
823 /* prefetchable memory base */
824 temp_register = base & 0xFFFFFFF0;
825 temp_register = (~temp_register) + 1;
827 p_mem_node = kmalloc(sizeof(*p_mem_node),
832 p_mem_node->base = save_base & (~0x0FL);
833 p_mem_node->length = temp_register;
835 p_mem_node->next = func->p_mem_head;
836 func->p_mem_head = p_mem_node;
838 if (((base & 0x0BL) == 0x00)
839 && (save_command & 0x02)) {
840 /* prefetchable memory base */
841 temp_register = base & 0xFFFFFFF0;
842 temp_register = (~temp_register) + 1;
844 mem_node = kmalloc(sizeof(*mem_node),
849 mem_node->base = save_base & (~0x0FL);
850 mem_node->length = temp_register;
852 mem_node->next = func->mem_head;
853 func->mem_head = mem_node;
857 } /* End of base register loop */
858 /* Standard header */
859 } else if ((header_type & 0x7F) == 0x00) {
860 /* Figure out IO and memory base lengths */
861 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
862 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
864 temp_register = 0xFFFFFFFF;
865 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
866 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
868 temp_register = base;
870 /* If this register is implemented */
872 if (((base & 0x03L) == 0x01)
873 && (save_command & 0x01)) {
875 * set temp_register = amount
876 * of IO space requested
878 temp_register = base & 0xFFFFFFFE;
879 temp_register = (~temp_register) + 1;
881 io_node = kmalloc(sizeof(*io_node),
886 io_node->base = save_base & (~0x01L);
887 io_node->length = temp_register;
889 io_node->next = func->io_head;
890 func->io_head = io_node;
892 if (((base & 0x0BL) == 0x08)
893 && (save_command & 0x02)) {
894 /* prefetchable memory base */
895 temp_register = base & 0xFFFFFFF0;
896 temp_register = (~temp_register) + 1;
898 p_mem_node = kmalloc(sizeof(*p_mem_node),
903 p_mem_node->base = save_base & (~0x0FL);
904 p_mem_node->length = temp_register;
906 p_mem_node->next = func->p_mem_head;
907 func->p_mem_head = p_mem_node;
909 if (((base & 0x0BL) == 0x00)
910 && (save_command & 0x02)) {
911 /* prefetchable memory base */
912 temp_register = base & 0xFFFFFFF0;
913 temp_register = (~temp_register) + 1;
915 mem_node = kmalloc(sizeof(*mem_node),
920 mem_node->base = save_base & (~0x0FL);
921 mem_node->length = temp_register;
923 mem_node->next = func->mem_head;
924 func->mem_head = mem_node;
928 } /* End of base register loop */
931 /* find the next device in this slot */
932 func = cpqhp_slot_find(func->bus, func->device, index++);
940 * cpqhp_configure_board
942 * Copies saved configuration information to one slot.
943 * this is called recursively for bridge devices.
944 * this is for hot plug REPLACE!
946 * returns 0 if success
948 int cpqhp_configure_board(struct controller *ctrl, struct pci_func *func)
954 struct pci_func *next;
958 struct pci_bus *pci_bus = ctrl->pci_bus;
961 func = cpqhp_slot_find(func->bus, func->device, index++);
963 while (func != NULL) {
964 pci_bus->number = func->bus;
965 devfn = PCI_DEVFN(func->device, func->function);
967 /* Start at the top of config space so that the control
968 * registers are programmed last
970 for (cloop = 0x3C; cloop > 0; cloop -= 4)
971 pci_bus_write_config_dword(pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
973 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
975 /* If this is a bridge device, restore subordinate devices */
976 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
977 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
979 sub_bus = (int) secondary_bus;
981 next = cpqhp_slot_list[sub_bus];
983 while (next != NULL) {
984 rc = cpqhp_configure_board(ctrl, next);
992 /* Check all the base Address Registers to make sure
993 * they are the same. If not, the board is different.
996 for (cloop = 16; cloop < 40; cloop += 4) {
997 pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp);
999 if (temp != func->config_space[cloop >> 2]) {
1000 dbg("Config space compare failure!!! offset = %x\n", cloop);
1001 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1002 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1008 func->configured = 1;
1010 func = cpqhp_slot_find(func->bus, func->device, index++);
1018 * cpqhp_valid_replace
1020 * this function checks to see if a board is the same as the
1021 * one it is replacing. this check will detect if the device's
1022 * vendor or device id's are the same
1024 * returns 0 if the board is the same nonzero otherwise
1026 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func *func)
1032 u32 temp_register = 0;
1035 struct pci_func *next;
1037 struct pci_bus *pci_bus = ctrl->pci_bus;
1040 if (!func->is_a_board)
1041 return(ADD_NOT_SUPPORTED);
1043 func = cpqhp_slot_find(func->bus, func->device, index++);
1045 while (func != NULL) {
1046 pci_bus->number = func->bus;
1047 devfn = PCI_DEVFN(func->device, func->function);
1049 pci_bus_read_config_dword(pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1051 /* No adapter present */
1052 if (temp_register == 0xFFFFFFFF)
1053 return(NO_ADAPTER_PRESENT);
1055 if (temp_register != func->config_space[0])
1056 return(ADAPTER_NOT_SAME);
1058 /* Check for same revision number and class code */
1059 pci_bus_read_config_dword(pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1061 /* Adapter not the same */
1062 if (temp_register != func->config_space[0x08 >> 2])
1063 return(ADAPTER_NOT_SAME);
1065 /* Check for Bridge */
1066 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1068 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1069 /* In order to continue checking, we must program the
1070 * bus registers in the bridge to respond to accesses
1071 * for its subordinate bus(es)
1074 temp_register = func->config_space[0x18 >> 2];
1075 pci_bus_write_config_dword(pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1077 secondary_bus = (temp_register >> 8) & 0xFF;
1079 next = cpqhp_slot_list[secondary_bus];
1081 while (next != NULL) {
1082 rc = cpqhp_valid_replace(ctrl, next);
1090 /* Check to see if it is a standard config header */
1091 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
1092 /* Check subsystem vendor and ID */
1093 pci_bus_read_config_dword(pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1095 if (temp_register != func->config_space[0x2C >> 2]) {
1096 /* If it's a SMART-2 and the register isn't
1097 * filled in, ignore the difference because
1098 * they just have an old rev of the firmware
1100 if (!((func->config_space[0] == 0xAE100E11)
1101 && (temp_register == 0x00L)))
1102 return(ADAPTER_NOT_SAME);
1104 /* Figure out IO and memory base lengths */
1105 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1106 temp_register = 0xFFFFFFFF;
1107 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
1108 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
1110 /* If this register is implemented */
1114 * set base = amount of IO
1117 base = base & 0xFFFFFFFE;
1123 base = base & 0xFFFFFFF0;
1133 /* Check information in slot structure */
1134 if (func->base_length[(cloop - 0x10) >> 2] != base)
1135 return(ADAPTER_NOT_SAME);
1137 if (func->base_type[(cloop - 0x10) >> 2] != type)
1138 return(ADAPTER_NOT_SAME);
1140 } /* End of base register loop */
1142 } /* End of (type 0 config space) else */
1144 /* this is not a type 0 or 1 config space header so
1145 * we don't know how to do it
1147 return(DEVICE_TYPE_NOT_SUPPORTED);
1150 /* Get the next function */
1151 func = cpqhp_slot_find(func->bus, func->device, index++);
1160 * cpqhp_find_available_resources
1162 * Finds available memory, IO, and IRQ resources for programming
1163 * devices which may be added to the system
1164 * this function is for hot plug ADD!
1166 * returns 0 if success
1168 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1173 void __iomem *one_slot;
1174 void __iomem *rom_resource_table;
1175 struct pci_func *func = NULL;
1178 struct pci_resource *mem_node;
1179 struct pci_resource *p_mem_node;
1180 struct pci_resource *io_node;
1181 struct pci_resource *bus_node;
1183 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1184 dbg("rom_resource_table = %p\n", rom_resource_table);
1186 if (rom_resource_table == NULL)
1189 /* Sum all resources and setup resource maps */
1190 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1191 dbg("unused_IRQ = %x\n", unused_IRQ);
1194 while (unused_IRQ) {
1195 if (unused_IRQ & 1) {
1196 cpqhp_disk_irq = temp;
1199 unused_IRQ = unused_IRQ >> 1;
1203 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1204 unused_IRQ = unused_IRQ >> 1;
1207 while (unused_IRQ) {
1208 if (unused_IRQ & 1) {
1209 cpqhp_nic_irq = temp;
1212 unused_IRQ = unused_IRQ >> 1;
1216 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1217 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1222 cpqhp_nic_irq = ctrl->cfgspc_irq;
1224 if (!cpqhp_disk_irq)
1225 cpqhp_disk_irq = ctrl->cfgspc_irq;
1227 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1229 rc = compaq_nvram_load(rom_start, ctrl);
1233 one_slot = rom_resource_table + sizeof(struct hrt);
1235 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1236 dbg("number_of_entries = %d\n", i);
1238 if (!readb(one_slot + SECONDARY_BUS))
1241 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1243 while (i && readb(one_slot + SECONDARY_BUS)) {
1244 u8 dev_func = readb(one_slot + DEV_FUNC);
1245 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1246 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1247 u8 max_bus = readb(one_slot + MAX_BUS);
1248 u16 io_base = readw(one_slot + IO_BASE);
1249 u16 io_length = readw(one_slot + IO_LENGTH);
1250 u16 mem_base = readw(one_slot + MEM_BASE);
1251 u16 mem_length = readw(one_slot + MEM_LENGTH);
1252 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1253 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1255 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1256 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1257 primary_bus, secondary_bus, max_bus);
1259 /* If this entry isn't for our controller's bus, ignore it */
1260 if (primary_bus != ctrl->bus) {
1262 one_slot += sizeof(struct slot_rt);
1265 /* find out if this entry is for an occupied slot */
1266 ctrl->pci_bus->number = primary_bus;
1267 pci_bus_read_config_dword(ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1268 dbg("temp_D_word = %x\n", temp_dword);
1270 if (temp_dword != 0xFFFFFFFF) {
1272 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1274 while (func && (func->function != (dev_func & 0x07))) {
1275 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1276 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1279 /* If we can't find a match, skip this table entry */
1282 one_slot += sizeof(struct slot_rt);
1285 /* this may not work and shouldn't be used */
1286 if (secondary_bus != primary_bus)
1298 /* If we've got a valid IO base, use it */
1300 temp_dword = io_base + io_length;
1302 if ((io_base) && (temp_dword < 0x10000)) {
1303 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1307 io_node->base = io_base;
1308 io_node->length = io_length;
1310 dbg("found io_node(base, length) = %x, %x\n",
1311 io_node->base, io_node->length);
1312 dbg("populated slot =%d \n", populated_slot);
1313 if (!populated_slot) {
1314 io_node->next = ctrl->io_head;
1315 ctrl->io_head = io_node;
1317 io_node->next = func->io_head;
1318 func->io_head = io_node;
1322 /* If we've got a valid memory base, use it */
1323 temp_dword = mem_base + mem_length;
1324 if ((mem_base) && (temp_dword < 0x10000)) {
1325 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1329 mem_node->base = mem_base << 16;
1331 mem_node->length = mem_length << 16;
1333 dbg("found mem_node(base, length) = %x, %x\n",
1334 mem_node->base, mem_node->length);
1335 dbg("populated slot =%d \n", populated_slot);
1336 if (!populated_slot) {
1337 mem_node->next = ctrl->mem_head;
1338 ctrl->mem_head = mem_node;
1340 mem_node->next = func->mem_head;
1341 func->mem_head = mem_node;
1345 /* If we've got a valid prefetchable memory base, and
1346 * the base + length isn't greater than 0xFFFF
1348 temp_dword = pre_mem_base + pre_mem_length;
1349 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1350 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1354 p_mem_node->base = pre_mem_base << 16;
1356 p_mem_node->length = pre_mem_length << 16;
1357 dbg("found p_mem_node(base, length) = %x, %x\n",
1358 p_mem_node->base, p_mem_node->length);
1359 dbg("populated slot =%d \n", populated_slot);
1361 if (!populated_slot) {
1362 p_mem_node->next = ctrl->p_mem_head;
1363 ctrl->p_mem_head = p_mem_node;
1365 p_mem_node->next = func->p_mem_head;
1366 func->p_mem_head = p_mem_node;
1370 /* If we've got a valid bus number, use it
1371 * The second condition is to ignore bus numbers on
1372 * populated slots that don't have PCI-PCI bridges
1374 if (secondary_bus && (secondary_bus != primary_bus)) {
1375 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1379 bus_node->base = secondary_bus;
1380 bus_node->length = max_bus - secondary_bus + 1;
1381 dbg("found bus_node(base, length) = %x, %x\n",
1382 bus_node->base, bus_node->length);
1383 dbg("populated slot =%d \n", populated_slot);
1384 if (!populated_slot) {
1385 bus_node->next = ctrl->bus_head;
1386 ctrl->bus_head = bus_node;
1388 bus_node->next = func->bus_head;
1389 func->bus_head = bus_node;
1394 one_slot += sizeof(struct slot_rt);
1397 /* If all of the following fail, we don't have any resources for
1401 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1402 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1403 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1404 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1411 * cpqhp_return_board_resources
1413 * this routine returns all resources allocated to a board to
1414 * the available pool.
1416 * returns 0 if success
1418 int cpqhp_return_board_resources(struct pci_func *func, struct resource_lists *resources)
1421 struct pci_resource *node;
1422 struct pci_resource *t_node;
1423 dbg("%s\n", __func__);
1428 node = func->io_head;
1429 func->io_head = NULL;
1431 t_node = node->next;
1432 return_resource(&(resources->io_head), node);
1436 node = func->mem_head;
1437 func->mem_head = NULL;
1439 t_node = node->next;
1440 return_resource(&(resources->mem_head), node);
1444 node = func->p_mem_head;
1445 func->p_mem_head = NULL;
1447 t_node = node->next;
1448 return_resource(&(resources->p_mem_head), node);
1452 node = func->bus_head;
1453 func->bus_head = NULL;
1455 t_node = node->next;
1456 return_resource(&(resources->bus_head), node);
1460 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1461 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1462 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1463 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1470 * cpqhp_destroy_resource_list
1472 * Puts node back in the resource list pointed to by head
1474 void cpqhp_destroy_resource_list(struct resource_lists *resources)
1476 struct pci_resource *res, *tres;
1478 res = resources->io_head;
1479 resources->io_head = NULL;
1487 res = resources->mem_head;
1488 resources->mem_head = NULL;
1496 res = resources->p_mem_head;
1497 resources->p_mem_head = NULL;
1505 res = resources->bus_head;
1506 resources->bus_head = NULL;
1517 * cpqhp_destroy_board_resources
1519 * Puts node back in the resource list pointed to by head
1521 void cpqhp_destroy_board_resources(struct pci_func *func)
1523 struct pci_resource *res, *tres;
1525 res = func->io_head;
1526 func->io_head = NULL;
1534 res = func->mem_head;
1535 func->mem_head = NULL;
1543 res = func->p_mem_head;
1544 func->p_mem_head = NULL;
1552 res = func->bus_head;
1553 func->bus_head = NULL;