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"
40 #include <asm/pci_x86.h>
46 static u16 unused_IRQ
;
49 * detect_HRT_floating_pointer
51 * find the Hot Plug Resource Table in the specified region of memory.
54 static void __iomem
*detect_HRT_floating_pointer(void __iomem
*begin
, void __iomem
*end
)
58 u8 temp1
, temp2
, temp3
, temp4
;
61 endp
= (end
- sizeof(struct hrt
) + 1);
63 for (fp
= begin
; fp
<= endp
; fp
+= 16) {
64 temp1
= readb(fp
+ SIG0
);
65 temp2
= readb(fp
+ SIG1
);
66 temp3
= readb(fp
+ SIG2
);
67 temp4
= readb(fp
+ SIG3
);
80 dbg("Discovered Hotplug Resource Table at %p\n", fp
);
85 int cpqhp_configure_device (struct controller
* ctrl
, struct pci_func
* func
)
88 struct pci_bus
*child
;
91 if (func
->pci_dev
== NULL
)
92 func
->pci_dev
= pci_find_slot(func
->bus
, PCI_DEVFN(func
->device
, func
->function
));
94 /* No pci device, we need to create it then */
95 if (func
->pci_dev
== NULL
) {
96 dbg("INFO: pci_dev still null\n");
98 num
= pci_scan_slot(ctrl
->pci_dev
->bus
, PCI_DEVFN(func
->device
, func
->function
));
100 pci_bus_add_devices(ctrl
->pci_dev
->bus
);
102 func
->pci_dev
= pci_find_slot(func
->bus
, PCI_DEVFN(func
->device
, func
->function
));
103 if (func
->pci_dev
== NULL
) {
104 dbg("ERROR: pci_dev still null\n");
109 if (func
->pci_dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) {
110 pci_read_config_byte(func
->pci_dev
, PCI_SECONDARY_BUS
, &bus
);
111 child
= (struct pci_bus
*) pci_add_new_bus(func
->pci_dev
->bus
, (func
->pci_dev
), bus
);
112 pci_do_scan_bus(child
);
119 int cpqhp_unconfigure_device(struct pci_func
* func
)
123 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__
, func
->bus
, func
->device
, func
->function
);
125 for (j
=0; j
<8 ; j
++) {
126 struct pci_dev
* temp
= pci_find_slot(func
->bus
, PCI_DEVFN(func
->device
, j
));
128 pci_remove_bus_device(temp
);
133 static int PCI_RefinedAccessConfig(struct pci_bus
*bus
, unsigned int devfn
, u8 offset
, u32
*value
)
137 if (pci_bus_read_config_dword (bus
, devfn
, PCI_VENDOR_ID
, &vendID
) == -1)
139 if (vendID
== 0xffffffff)
141 return pci_bus_read_config_dword (bus
, devfn
, offset
, value
);
148 * @bus_num: bus number of PCI device
149 * @dev_num: device number of PCI device
150 * @slot: pointer to u8 where slot number will be returned
152 int cpqhp_set_irq (u8 bus_num
, u8 dev_num
, u8 int_pin
, u8 irq_num
)
156 if (cpqhp_legacy_mode
) {
157 struct pci_dev
*fakedev
;
158 struct pci_bus
*fakebus
;
161 fakedev
= kmalloc(sizeof(*fakedev
), GFP_KERNEL
);
162 fakebus
= kmalloc(sizeof(*fakebus
), GFP_KERNEL
);
163 if (!fakedev
|| !fakebus
) {
169 fakedev
->devfn
= dev_num
<< 3;
170 fakedev
->bus
= fakebus
;
171 fakebus
->number
= bus_num
;
172 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
173 __func__
, dev_num
, bus_num
, int_pin
, irq_num
);
174 rc
= pcibios_set_irq_routing(fakedev
, int_pin
- 1, irq_num
);
177 dbg("%s: rc %d\n", __func__
, rc
);
181 /* set the Edge Level Control Register (ELCR) */
182 temp_word
= inb(0x4d0);
183 temp_word
|= inb(0x4d1) << 8;
185 temp_word
|= 0x01 << irq_num
;
187 /* This should only be for x86 as it sets the Edge Level
190 outb((u8
) (temp_word
& 0xFF), 0x4d0); outb((u8
) ((temp_word
&
191 0xFF00) >> 8), 0x4d1); rc
= 0; }
198 * WTF??? This function isn't in the code, yet a function calls it, but the
199 * compiler optimizes it away? strange. Here as a placeholder to keep the
202 static int PCI_ScanBusNonBridge (u8 bus
, u8 device
)
207 static int PCI_ScanBusForNonBridge(struct controller
*ctrl
, u8 bus_num
, u8
* dev_num
)
213 ctrl
->pci_bus
->number
= bus_num
;
215 for (tdevice
= 0; tdevice
< 0xFF; tdevice
++) {
216 /* Scan for access first */
217 if (PCI_RefinedAccessConfig(ctrl
->pci_bus
, tdevice
, 0x08, &work
) == -1)
219 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num
, tdevice
);
220 /* Yep we got one. Not a bridge ? */
221 if ((work
>> 8) != PCI_TO_PCI_BRIDGE_CLASS
) {
227 for (tdevice
= 0; tdevice
< 0xFF; tdevice
++) {
228 /* Scan for access first */
229 if (PCI_RefinedAccessConfig(ctrl
->pci_bus
, tdevice
, 0x08, &work
) == -1)
231 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num
, tdevice
);
232 /* Yep we got one. bridge ? */
233 if ((work
>> 8) == PCI_TO_PCI_BRIDGE_CLASS
) {
234 pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(tdevice
, 0), PCI_SECONDARY_BUS
, &tbus
);
235 dbg("Recurse on bus_num %d tdevice %d\n", tbus
, tdevice
);
236 if (PCI_ScanBusNonBridge(tbus
, tdevice
) == 0)
245 static int PCI_GetBusDevHelper(struct controller
*ctrl
, u8
*bus_num
, u8
*dev_num
, u8 slot
, u8 nobridge
)
247 struct irq_routing_table
*PCIIRQRoutingInfoLength
;
252 u8 tbus
, tdevice
, tslot
;
254 PCIIRQRoutingInfoLength
= pcibios_get_irq_routing_table();
255 if (!PCIIRQRoutingInfoLength
)
258 len
= (PCIIRQRoutingInfoLength
->size
-
259 sizeof(struct irq_routing_table
)) / sizeof(struct irq_info
);
260 /* Make sure I got at least one entry */
262 kfree(PCIIRQRoutingInfoLength
);
266 for (loop
= 0; loop
< len
; ++loop
) {
267 tbus
= PCIIRQRoutingInfoLength
->slots
[loop
].bus
;
268 tdevice
= PCIIRQRoutingInfoLength
->slots
[loop
].devfn
;
269 tslot
= PCIIRQRoutingInfoLength
->slots
[loop
].slot
;
274 ctrl
->pci_bus
->number
= tbus
;
275 pci_bus_read_config_dword (ctrl
->pci_bus
, *dev_num
, PCI_VENDOR_ID
, &work
);
276 if (!nobridge
|| (work
== 0xffffffff)) {
277 kfree(PCIIRQRoutingInfoLength
);
281 dbg("bus_num %d devfn %d\n", *bus_num
, *dev_num
);
282 pci_bus_read_config_dword (ctrl
->pci_bus
, *dev_num
, PCI_CLASS_REVISION
, &work
);
283 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work
>> 8, PCI_TO_PCI_BRIDGE_CLASS
);
285 if ((work
>> 8) == PCI_TO_PCI_BRIDGE_CLASS
) {
286 pci_bus_read_config_byte (ctrl
->pci_bus
, *dev_num
, PCI_SECONDARY_BUS
, &tbus
);
287 dbg("Scan bus for Non Bridge: bus %d\n", tbus
);
288 if (PCI_ScanBusForNonBridge(ctrl
, tbus
, dev_num
) == 0) {
290 kfree(PCIIRQRoutingInfoLength
);
294 kfree(PCIIRQRoutingInfoLength
);
300 kfree(PCIIRQRoutingInfoLength
);
305 int cpqhp_get_bus_dev (struct controller
*ctrl
, u8
* bus_num
, u8
* dev_num
, u8 slot
)
307 /* plain (bridges allowed) */
308 return PCI_GetBusDevHelper(ctrl
, bus_num
, dev_num
, slot
, 0);
312 /* More PCI configuration routines; this time centered around hotplug
320 * Reads configuration for all slots in a PCI bus and saves info.
322 * Note: For non-hot plug busses, the slot # saved is the device #
324 * returns 0 if success
326 int cpqhp_save_config(struct controller
*ctrl
, int busnumber
, int is_hot_plug
)
333 struct pci_func
*new_slot
;
345 /* Decide which slots are supported */
349 * is_hot_plug is the slot mask
351 FirstSupported
= is_hot_plug
>> 4;
352 LastSupported
= FirstSupported
+ (is_hot_plug
& 0x0F) - 1;
355 LastSupported
= 0x1F;
358 /* Save PCI configuration space for all devices in supported slots */
359 ctrl
->pci_bus
->number
= busnumber
;
360 for (device
= FirstSupported
; device
<= LastSupported
; device
++) {
362 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(device
, 0), PCI_VENDOR_ID
, &ID
);
364 if (ID
!= 0xFFFFFFFF) { /* device in slot */
365 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(device
, 0), 0x0B, &class_code
);
369 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(device
, 0), PCI_HEADER_TYPE
, &header_type
);
373 /* If multi-function device, set max_functions to 8 */
374 if (header_type
& 0x80)
383 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
384 /* Recurse the subordinate bus
385 * get the subordinate bus number
387 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_SECONDARY_BUS
, &secondary_bus
);
391 sub_bus
= (int) secondary_bus
;
393 /* Save secondary bus cfg spc
394 * with this recursive call.
396 rc
= cpqhp_save_config(ctrl
, sub_bus
, 0);
399 ctrl
->pci_bus
->number
= busnumber
;
404 new_slot
= cpqhp_slot_find(busnumber
, device
, index
++);
406 (new_slot
->function
!= (u8
) function
))
407 new_slot
= cpqhp_slot_find(busnumber
, device
, index
++);
410 /* Setup slot structure. */
411 new_slot
= cpqhp_slot_create(busnumber
);
413 if (new_slot
== NULL
)
417 new_slot
->bus
= (u8
) busnumber
;
418 new_slot
->device
= (u8
) device
;
419 new_slot
->function
= (u8
) function
;
420 new_slot
->is_a_board
= 1;
421 new_slot
->switch_save
= 0x10;
422 /* In case of unsupported board */
423 new_slot
->status
= DevError
;
424 new_slot
->pci_dev
= pci_find_slot(new_slot
->bus
, (new_slot
->device
<< 3) | new_slot
->function
);
426 for (cloop
= 0; cloop
< 0x20; cloop
++) {
427 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
436 /* this loop skips to the next present function
437 * reading in Class Code and Header type.
440 while ((function
< max_functions
)&&(!stop_it
)) {
441 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_VENDOR_ID
, &ID
);
442 if (ID
== 0xFFFFFFFF) { /* nothing there. */
444 } else { /* Something there */
445 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(device
, function
), 0x0B, &class_code
);
449 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_HEADER_TYPE
, &header_type
);
457 } while (function
< max_functions
);
458 } /* End of IF (device in slot?) */
459 else if (is_hot_plug
) {
460 /* Setup slot structure with entry for empty slot */
461 new_slot
= cpqhp_slot_create(busnumber
);
463 if (new_slot
== NULL
) {
467 new_slot
->bus
= (u8
) busnumber
;
468 new_slot
->device
= (u8
) device
;
469 new_slot
->function
= 0;
470 new_slot
->is_a_board
= 0;
471 new_slot
->presence_save
= 0;
472 new_slot
->switch_save
= 0;
474 } /* End of FOR loop */
481 * cpqhp_save_slot_config
483 * Saves configuration info for all PCI devices in a given slot
484 * including subordinate busses.
486 * returns 0 if success
488 int cpqhp_save_slot_config (struct controller
*ctrl
, struct pci_func
* new_slot
)
503 ctrl
->pci_bus
->number
= new_slot
->bus
;
504 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_VENDOR_ID
, &ID
);
506 if (ID
== 0xFFFFFFFF)
509 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), 0x0B, &class_code
);
510 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_HEADER_TYPE
, &header_type
);
512 if (header_type
& 0x80) /* Multi-function device */
517 while (function
< max_functions
) {
518 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
519 /* Recurse the subordinate bus */
520 pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_SECONDARY_BUS
, &secondary_bus
);
522 sub_bus
= (int) secondary_bus
;
524 /* Save the config headers for the secondary
527 rc
= cpqhp_save_config(ctrl
, sub_bus
, 0);
530 ctrl
->pci_bus
->number
= new_slot
->bus
;
534 new_slot
->status
= 0;
536 for (cloop
= 0; cloop
< 0x20; cloop
++)
537 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
543 /* this loop skips to the next present function
544 * reading in the Class Code and the Header type.
546 while ((function
< max_functions
) && (!stop_it
)) {
547 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_VENDOR_ID
, &ID
);
549 if (ID
== 0xFFFFFFFF)
552 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), 0x0B, &class_code
);
553 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_HEADER_TYPE
, &header_type
);
565 * cpqhp_save_base_addr_length
567 * Saves the length of all base address registers for the
568 * specified slot. this is for hot plug REPLACE
570 * returns 0 if success
572 int cpqhp_save_base_addr_length(struct controller
*ctrl
, struct pci_func
* func
)
582 struct pci_func
*next
;
584 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
587 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
589 while (func
!= NULL
) {
590 pci_bus
->number
= func
->bus
;
591 devfn
= PCI_DEVFN(func
->device
, func
->function
);
593 /* Check for Bridge */
594 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
596 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
597 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
599 sub_bus
= (int) secondary_bus
;
601 next
= cpqhp_slot_list
[sub_bus
];
603 while (next
!= NULL
) {
604 rc
= cpqhp_save_base_addr_length(ctrl
, next
);
610 pci_bus
->number
= func
->bus
;
612 /* FIXME: this loop is duplicated in the non-bridge
613 * case. The two could be rolled together Figure out
614 * IO and memory base lengths
616 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
617 temp_register
= 0xFFFFFFFF;
618 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
619 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
620 /* If this register is implemented */
624 * set base = amount of IO space
627 base
= base
& 0xFFFFFFFE;
633 base
= base
& 0xFFFFFFF0;
643 /* Save information in slot structure */
644 func
->base_length
[(cloop
- 0x10) >> 2] =
646 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
648 } /* End of base register loop */
650 } else if ((header_type
& 0x7F) == 0x00) {
651 /* Figure out IO and memory base lengths */
652 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
653 temp_register
= 0xFFFFFFFF;
654 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
655 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
657 /* If this register is implemented */
661 * base = amount of IO space
664 base
= base
& 0xFFFFFFFE;
670 * base = amount of memory
673 base
= base
& 0xFFFFFFF0;
683 /* Save information in slot structure */
684 func
->base_length
[(cloop
- 0x10) >> 2] = base
;
685 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
687 } /* End of base register loop */
689 } else { /* Some other unknown header type */
692 /* find the next device in this slot */
693 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
701 * cpqhp_save_used_resources
703 * Stores used resource information for existing boards. this is
704 * for boards that were in the system when this driver was loaded.
705 * this function is for hot plug ADD
707 * returns 0 if success
709 int cpqhp_save_used_resources (struct controller
*ctrl
, struct pci_func
* func
)
725 struct pci_resource
*mem_node
;
726 struct pci_resource
*p_mem_node
;
727 struct pci_resource
*io_node
;
728 struct pci_resource
*bus_node
;
729 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
732 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
734 while ((func
!= NULL
) && func
->is_a_board
) {
735 pci_bus
->number
= func
->bus
;
736 devfn
= PCI_DEVFN(func
->device
, func
->function
);
738 /* Save the command register */
739 pci_bus_read_config_word(pci_bus
, devfn
, PCI_COMMAND
, &save_command
);
743 pci_bus_write_config_word(pci_bus
, devfn
, PCI_COMMAND
, command
);
745 /* Check for Bridge */
746 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
748 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
749 /* Clear Bridge Control Register */
751 pci_bus_write_config_word(pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
752 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
753 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, &temp_byte
);
755 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
759 bus_node
->base
= secondary_bus
;
760 bus_node
->length
= temp_byte
- secondary_bus
+ 1;
762 bus_node
->next
= func
->bus_head
;
763 func
->bus_head
= bus_node
;
765 /* Save IO base and Limit registers */
766 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, &b_base
);
767 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, &b_length
);
769 if ((b_base
<= b_length
) && (save_command
& 0x01)) {
770 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
774 io_node
->base
= (b_base
& 0xF0) << 8;
775 io_node
->length
= (b_length
- b_base
+ 0x10) << 8;
777 io_node
->next
= func
->io_head
;
778 func
->io_head
= io_node
;
781 /* Save memory base and Limit registers */
782 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, &w_base
);
783 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, &w_length
);
785 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
786 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
790 mem_node
->base
= w_base
<< 16;
791 mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
793 mem_node
->next
= func
->mem_head
;
794 func
->mem_head
= mem_node
;
797 /* Save prefetchable memory base and Limit registers */
798 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, &w_base
);
799 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, &w_length
);
801 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
802 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
806 p_mem_node
->base
= w_base
<< 16;
807 p_mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
809 p_mem_node
->next
= func
->p_mem_head
;
810 func
->p_mem_head
= p_mem_node
;
812 /* Figure out IO and memory base lengths */
813 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
814 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &save_base
);
816 temp_register
= 0xFFFFFFFF;
817 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
818 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
820 temp_register
= base
;
822 /* If this register is implemented */
824 if (((base
& 0x03L
) == 0x01)
825 && (save_command
& 0x01)) {
827 * set temp_register = amount
828 * of IO space requested
830 temp_register
= base
& 0xFFFFFFFE;
831 temp_register
= (~temp_register
) + 1;
833 io_node
= kmalloc(sizeof(*io_node
),
839 save_base
& (~0x03L
);
840 io_node
->length
= temp_register
;
842 io_node
->next
= func
->io_head
;
843 func
->io_head
= io_node
;
845 if (((base
& 0x0BL
) == 0x08)
846 && (save_command
& 0x02)) {
847 /* prefetchable memory base */
848 temp_register
= base
& 0xFFFFFFF0;
849 temp_register
= (~temp_register
) + 1;
851 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
856 p_mem_node
->base
= save_base
& (~0x0FL
);
857 p_mem_node
->length
= temp_register
;
859 p_mem_node
->next
= func
->p_mem_head
;
860 func
->p_mem_head
= p_mem_node
;
862 if (((base
& 0x0BL
) == 0x00)
863 && (save_command
& 0x02)) {
864 /* prefetchable memory base */
865 temp_register
= base
& 0xFFFFFFF0;
866 temp_register
= (~temp_register
) + 1;
868 mem_node
= kmalloc(sizeof(*mem_node
),
873 mem_node
->base
= save_base
& (~0x0FL
);
874 mem_node
->length
= temp_register
;
876 mem_node
->next
= func
->mem_head
;
877 func
->mem_head
= mem_node
;
881 } /* End of base register loop */
882 /* Standard header */
883 } else if ((header_type
& 0x7F) == 0x00) {
884 /* Figure out IO and memory base lengths */
885 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
886 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &save_base
);
888 temp_register
= 0xFFFFFFFF;
889 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
890 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
892 temp_register
= base
;
894 /* If this register is implemented */
896 if (((base
& 0x03L
) == 0x01)
897 && (save_command
& 0x01)) {
899 * set temp_register = amount
900 * of IO space requested
902 temp_register
= base
& 0xFFFFFFFE;
903 temp_register
= (~temp_register
) + 1;
905 io_node
= kmalloc(sizeof(*io_node
),
910 io_node
->base
= save_base
& (~0x01L
);
911 io_node
->length
= temp_register
;
913 io_node
->next
= func
->io_head
;
914 func
->io_head
= io_node
;
916 if (((base
& 0x0BL
) == 0x08)
917 && (save_command
& 0x02)) {
918 /* prefetchable memory base */
919 temp_register
= base
& 0xFFFFFFF0;
920 temp_register
= (~temp_register
) + 1;
922 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
927 p_mem_node
->base
= save_base
& (~0x0FL
);
928 p_mem_node
->length
= temp_register
;
930 p_mem_node
->next
= func
->p_mem_head
;
931 func
->p_mem_head
= p_mem_node
;
933 if (((base
& 0x0BL
) == 0x00)
934 && (save_command
& 0x02)) {
935 /* prefetchable memory base */
936 temp_register
= base
& 0xFFFFFFF0;
937 temp_register
= (~temp_register
) + 1;
939 mem_node
= kmalloc(sizeof(*mem_node
),
944 mem_node
->base
= save_base
& (~0x0FL
);
945 mem_node
->length
= temp_register
;
947 mem_node
->next
= func
->mem_head
;
948 func
->mem_head
= mem_node
;
952 } /* End of base register loop */
953 /* Some other unknown header type */
957 /* find the next device in this slot */
958 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
966 * cpqhp_configure_board
968 * Copies saved configuration information to one slot.
969 * this is called recursively for bridge devices.
970 * this is for hot plug REPLACE!
972 * returns 0 if success
974 int cpqhp_configure_board(struct controller
*ctrl
, struct pci_func
* func
)
980 struct pci_func
*next
;
984 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
987 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
989 while (func
!= NULL
) {
990 pci_bus
->number
= func
->bus
;
991 devfn
= PCI_DEVFN(func
->device
, func
->function
);
993 /* Start at the top of config space so that the control
994 * registers are programmed last
996 for (cloop
= 0x3C; cloop
> 0; cloop
-= 4) {
997 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, func
->config_space
[cloop
>> 2]);
1000 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
1002 /* If this is a bridge device, restore subordinate devices */
1003 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
1004 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
1006 sub_bus
= (int) secondary_bus
;
1008 next
= cpqhp_slot_list
[sub_bus
];
1010 while (next
!= NULL
) {
1011 rc
= cpqhp_configure_board(ctrl
, next
);
1019 /* Check all the base Address Registers to make sure
1020 * they are the same. If not, the board is different.
1023 for (cloop
= 16; cloop
< 40; cloop
+= 4) {
1024 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp
);
1026 if (temp
!= func
->config_space
[cloop
>> 2]) {
1027 dbg("Config space compare failure!!! offset = %x\n", cloop
);
1028 dbg("bus = %x, device = %x, function = %x\n", func
->bus
, func
->device
, func
->function
);
1029 dbg("temp = %x, config space = %x\n\n", temp
, func
->config_space
[cloop
>> 2]);
1035 func
->configured
= 1;
1037 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1045 * cpqhp_valid_replace
1047 * this function checks to see if a board is the same as the
1048 * one it is replacing. this check will detect if the device's
1049 * vendor or device id's are the same
1051 * returns 0 if the board is the same nonzero otherwise
1053 int cpqhp_valid_replace(struct controller
*ctrl
, struct pci_func
* func
)
1059 u32 temp_register
= 0;
1062 struct pci_func
*next
;
1064 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
1067 if (!func
->is_a_board
)
1068 return(ADD_NOT_SUPPORTED
);
1070 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1072 while (func
!= NULL
) {
1073 pci_bus
->number
= func
->bus
;
1074 devfn
= PCI_DEVFN(func
->device
, func
->function
);
1076 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_VENDOR_ID
, &temp_register
);
1078 /* No adapter present */
1079 if (temp_register
== 0xFFFFFFFF)
1080 return(NO_ADAPTER_PRESENT
);
1082 if (temp_register
!= func
->config_space
[0])
1083 return(ADAPTER_NOT_SAME
);
1085 /* Check for same revision number and class code */
1086 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_CLASS_REVISION
, &temp_register
);
1088 /* Adapter not the same */
1089 if (temp_register
!= func
->config_space
[0x08 >> 2])
1090 return(ADAPTER_NOT_SAME
);
1092 /* Check for Bridge */
1093 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
1095 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
1096 /* In order to continue checking, we must program the
1097 * bus registers in the bridge to respond to accesses
1098 * for its subordinate bus(es)
1101 temp_register
= func
->config_space
[0x18 >> 2];
1102 pci_bus_write_config_dword (pci_bus
, devfn
, PCI_PRIMARY_BUS
, temp_register
);
1104 secondary_bus
= (temp_register
>> 8) & 0xFF;
1106 next
= cpqhp_slot_list
[secondary_bus
];
1108 while (next
!= NULL
) {
1109 rc
= cpqhp_valid_replace(ctrl
, next
);
1117 /* Check to see if it is a standard config header */
1118 else if ((header_type
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
1119 /* Check subsystem vendor and ID */
1120 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_SUBSYSTEM_VENDOR_ID
, &temp_register
);
1122 if (temp_register
!= func
->config_space
[0x2C >> 2]) {
1123 /* If it's a SMART-2 and the register isn't
1124 * filled in, ignore the difference because
1125 * they just have an old rev of the firmware
1127 if (!((func
->config_space
[0] == 0xAE100E11)
1128 && (temp_register
== 0x00L
)))
1129 return(ADAPTER_NOT_SAME
);
1131 /* Figure out IO and memory base lengths */
1132 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
1133 temp_register
= 0xFFFFFFFF;
1134 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
1135 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
1137 /* If this register is implemented */
1141 * set base = amount of IO
1144 base
= base
& 0xFFFFFFFE;
1150 base
= base
& 0xFFFFFFF0;
1160 /* Check information in slot structure */
1161 if (func
->base_length
[(cloop
- 0x10) >> 2] != base
)
1162 return(ADAPTER_NOT_SAME
);
1164 if (func
->base_type
[(cloop
- 0x10) >> 2] != type
)
1165 return(ADAPTER_NOT_SAME
);
1167 } /* End of base register loop */
1169 } /* End of (type 0 config space) else */
1171 /* this is not a type 0 or 1 config space header so
1172 * we don't know how to do it
1174 return(DEVICE_TYPE_NOT_SUPPORTED
);
1177 /* Get the next function */
1178 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1187 * cpqhp_find_available_resources
1189 * Finds available memory, IO, and IRQ resources for programming
1190 * devices which may be added to the system
1191 * this function is for hot plug ADD!
1193 * returns 0 if success
1195 int cpqhp_find_available_resources(struct controller
*ctrl
, void __iomem
*rom_start
)
1200 void __iomem
*one_slot
;
1201 void __iomem
*rom_resource_table
;
1202 struct pci_func
*func
= NULL
;
1205 struct pci_resource
*mem_node
;
1206 struct pci_resource
*p_mem_node
;
1207 struct pci_resource
*io_node
;
1208 struct pci_resource
*bus_node
;
1210 rom_resource_table
= detect_HRT_floating_pointer(rom_start
, rom_start
+0xffff);
1211 dbg("rom_resource_table = %p\n", rom_resource_table
);
1213 if (rom_resource_table
== NULL
) {
1216 /* Sum all resources and setup resource maps */
1217 unused_IRQ
= readl(rom_resource_table
+ UNUSED_IRQ
);
1218 dbg("unused_IRQ = %x\n", unused_IRQ
);
1221 while (unused_IRQ
) {
1222 if (unused_IRQ
& 1) {
1223 cpqhp_disk_irq
= temp
;
1226 unused_IRQ
= unused_IRQ
>> 1;
1230 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq
);
1231 unused_IRQ
= unused_IRQ
>> 1;
1234 while (unused_IRQ
) {
1235 if (unused_IRQ
& 1) {
1236 cpqhp_nic_irq
= temp
;
1239 unused_IRQ
= unused_IRQ
>> 1;
1243 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq
);
1244 unused_IRQ
= readl(rom_resource_table
+ PCIIRQ
);
1248 if (!cpqhp_nic_irq
) {
1249 cpqhp_nic_irq
= ctrl
->cfgspc_irq
;
1252 if (!cpqhp_disk_irq
) {
1253 cpqhp_disk_irq
= ctrl
->cfgspc_irq
;
1256 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq
, cpqhp_nic_irq
);
1258 rc
= compaq_nvram_load(rom_start
, ctrl
);
1262 one_slot
= rom_resource_table
+ sizeof (struct hrt
);
1264 i
= readb(rom_resource_table
+ NUMBER_OF_ENTRIES
);
1265 dbg("number_of_entries = %d\n", i
);
1267 if (!readb(one_slot
+ SECONDARY_BUS
))
1270 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1272 while (i
&& readb(one_slot
+ SECONDARY_BUS
)) {
1273 u8 dev_func
= readb(one_slot
+ DEV_FUNC
);
1274 u8 primary_bus
= readb(one_slot
+ PRIMARY_BUS
);
1275 u8 secondary_bus
= readb(one_slot
+ SECONDARY_BUS
);
1276 u8 max_bus
= readb(one_slot
+ MAX_BUS
);
1277 u16 io_base
= readw(one_slot
+ IO_BASE
);
1278 u16 io_length
= readw(one_slot
+ IO_LENGTH
);
1279 u16 mem_base
= readw(one_slot
+ MEM_BASE
);
1280 u16 mem_length
= readw(one_slot
+ MEM_LENGTH
);
1281 u16 pre_mem_base
= readw(one_slot
+ PRE_MEM_BASE
);
1282 u16 pre_mem_length
= readw(one_slot
+ PRE_MEM_LENGTH
);
1284 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1285 dev_func
, io_base
, io_length
, mem_base
, mem_length
, pre_mem_base
, pre_mem_length
,
1286 primary_bus
, secondary_bus
, max_bus
);
1288 /* If this entry isn't for our controller's bus, ignore it */
1289 if (primary_bus
!= ctrl
->bus
) {
1291 one_slot
+= sizeof (struct slot_rt
);
1294 /* find out if this entry is for an occupied slot */
1295 ctrl
->pci_bus
->number
= primary_bus
;
1296 pci_bus_read_config_dword (ctrl
->pci_bus
, dev_func
, PCI_VENDOR_ID
, &temp_dword
);
1297 dbg("temp_D_word = %x\n", temp_dword
);
1299 if (temp_dword
!= 0xFFFFFFFF) {
1301 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, 0);
1303 while (func
&& (func
->function
!= (dev_func
& 0x07))) {
1304 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func
, primary_bus
, dev_func
>> 3, index
);
1305 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, index
++);
1308 /* If we can't find a match, skip this table entry */
1311 one_slot
+= sizeof (struct slot_rt
);
1314 /* this may not work and shouldn't be used */
1315 if (secondary_bus
!= primary_bus
)
1327 /* If we've got a valid IO base, use it */
1329 temp_dword
= io_base
+ io_length
;
1331 if ((io_base
) && (temp_dword
< 0x10000)) {
1332 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
1336 io_node
->base
= io_base
;
1337 io_node
->length
= io_length
;
1339 dbg("found io_node(base, length) = %x, %x\n",
1340 io_node
->base
, io_node
->length
);
1341 dbg("populated slot =%d \n", populated_slot
);
1342 if (!populated_slot
) {
1343 io_node
->next
= ctrl
->io_head
;
1344 ctrl
->io_head
= io_node
;
1346 io_node
->next
= func
->io_head
;
1347 func
->io_head
= io_node
;
1351 /* If we've got a valid memory base, use it */
1352 temp_dword
= mem_base
+ mem_length
;
1353 if ((mem_base
) && (temp_dword
< 0x10000)) {
1354 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
1358 mem_node
->base
= mem_base
<< 16;
1360 mem_node
->length
= mem_length
<< 16;
1362 dbg("found mem_node(base, length) = %x, %x\n",
1363 mem_node
->base
, mem_node
->length
);
1364 dbg("populated slot =%d \n", populated_slot
);
1365 if (!populated_slot
) {
1366 mem_node
->next
= ctrl
->mem_head
;
1367 ctrl
->mem_head
= mem_node
;
1369 mem_node
->next
= func
->mem_head
;
1370 func
->mem_head
= mem_node
;
1374 /* If we've got a valid prefetchable memory base, and
1375 * the base + length isn't greater than 0xFFFF
1377 temp_dword
= pre_mem_base
+ pre_mem_length
;
1378 if ((pre_mem_base
) && (temp_dword
< 0x10000)) {
1379 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
1383 p_mem_node
->base
= pre_mem_base
<< 16;
1385 p_mem_node
->length
= pre_mem_length
<< 16;
1386 dbg("found p_mem_node(base, length) = %x, %x\n",
1387 p_mem_node
->base
, p_mem_node
->length
);
1388 dbg("populated slot =%d \n", populated_slot
);
1390 if (!populated_slot
) {
1391 p_mem_node
->next
= ctrl
->p_mem_head
;
1392 ctrl
->p_mem_head
= p_mem_node
;
1394 p_mem_node
->next
= func
->p_mem_head
;
1395 func
->p_mem_head
= p_mem_node
;
1399 /* If we've got a valid bus number, use it
1400 * The second condition is to ignore bus numbers on
1401 * populated slots that don't have PCI-PCI bridges
1403 if (secondary_bus
&& (secondary_bus
!= primary_bus
)) {
1404 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
1408 bus_node
->base
= secondary_bus
;
1409 bus_node
->length
= max_bus
- secondary_bus
+ 1;
1410 dbg("found bus_node(base, length) = %x, %x\n",
1411 bus_node
->base
, bus_node
->length
);
1412 dbg("populated slot =%d \n", populated_slot
);
1413 if (!populated_slot
) {
1414 bus_node
->next
= ctrl
->bus_head
;
1415 ctrl
->bus_head
= bus_node
;
1417 bus_node
->next
= func
->bus_head
;
1418 func
->bus_head
= bus_node
;
1423 one_slot
+= sizeof (struct slot_rt
);
1426 /* If all of the following fail, we don't have any resources for
1430 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1431 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1432 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1433 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1440 * cpqhp_return_board_resources
1442 * this routine returns all resources allocated to a board to
1443 * the available pool.
1445 * returns 0 if success
1447 int cpqhp_return_board_resources(struct pci_func
* func
, struct resource_lists
* resources
)
1450 struct pci_resource
*node
;
1451 struct pci_resource
*t_node
;
1452 dbg("%s\n", __func__
);
1457 node
= func
->io_head
;
1458 func
->io_head
= NULL
;
1460 t_node
= node
->next
;
1461 return_resource(&(resources
->io_head
), node
);
1465 node
= func
->mem_head
;
1466 func
->mem_head
= NULL
;
1468 t_node
= node
->next
;
1469 return_resource(&(resources
->mem_head
), node
);
1473 node
= func
->p_mem_head
;
1474 func
->p_mem_head
= NULL
;
1476 t_node
= node
->next
;
1477 return_resource(&(resources
->p_mem_head
), node
);
1481 node
= func
->bus_head
;
1482 func
->bus_head
= NULL
;
1484 t_node
= node
->next
;
1485 return_resource(&(resources
->bus_head
), node
);
1489 rc
|= cpqhp_resource_sort_and_combine(&(resources
->mem_head
));
1490 rc
|= cpqhp_resource_sort_and_combine(&(resources
->p_mem_head
));
1491 rc
|= cpqhp_resource_sort_and_combine(&(resources
->io_head
));
1492 rc
|= cpqhp_resource_sort_and_combine(&(resources
->bus_head
));
1499 * cpqhp_destroy_resource_list
1501 * Puts node back in the resource list pointed to by head
1503 void cpqhp_destroy_resource_list (struct resource_lists
* resources
)
1505 struct pci_resource
*res
, *tres
;
1507 res
= resources
->io_head
;
1508 resources
->io_head
= NULL
;
1516 res
= resources
->mem_head
;
1517 resources
->mem_head
= NULL
;
1525 res
= resources
->p_mem_head
;
1526 resources
->p_mem_head
= NULL
;
1534 res
= resources
->bus_head
;
1535 resources
->bus_head
= NULL
;
1546 * cpqhp_destroy_board_resources
1548 * Puts node back in the resource list pointed to by head
1550 void cpqhp_destroy_board_resources (struct pci_func
* func
)
1552 struct pci_resource
*res
, *tres
;
1554 res
= func
->io_head
;
1555 func
->io_head
= NULL
;
1563 res
= func
->mem_head
;
1564 func
->mem_head
= NULL
;
1572 res
= func
->p_mem_head
;
1573 func
->p_mem_head
= NULL
;
1581 res
= func
->bus_head
;
1582 func
->bus_head
= NULL
;