2 * Compaq Hot Plug Controller Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <greg@kroah.com>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/workqueue.h>
34 #include <linux/proc_fs.h>
35 #include <linux/pci.h>
36 #include <linux/pci_hotplug.h>
39 #include "cpqphp_nvram.h"
45 static u16 unused_IRQ
;
48 * detect_HRT_floating_pointer
50 * find the Hot Plug Resource Table in the specified region of memory.
53 static void __iomem
*detect_HRT_floating_pointer(void __iomem
*begin
, void __iomem
*end
)
57 u8 temp1
, temp2
, temp3
, temp4
;
60 endp
= (end
- sizeof(struct hrt
) + 1);
62 for (fp
= begin
; fp
<= endp
; fp
+= 16) {
63 temp1
= readb(fp
+ SIG0
);
64 temp2
= readb(fp
+ SIG1
);
65 temp3
= readb(fp
+ SIG2
);
66 temp4
= readb(fp
+ SIG3
);
79 dbg("Discovered Hotplug Resource Table at %p\n", fp
);
84 int cpqhp_configure_device (struct controller
* ctrl
, struct pci_func
* func
)
87 struct pci_bus
*child
;
90 if (func
->pci_dev
== NULL
)
91 func
->pci_dev
= pci_get_bus_and_slot(func
->bus
,PCI_DEVFN(func
->device
, func
->function
));
93 /* No pci device, we need to create it then */
94 if (func
->pci_dev
== NULL
) {
95 dbg("INFO: pci_dev still null\n");
97 num
= pci_scan_slot(ctrl
->pci_dev
->bus
, PCI_DEVFN(func
->device
, func
->function
));
99 pci_bus_add_devices(ctrl
->pci_dev
->bus
);
101 func
->pci_dev
= pci_get_bus_and_slot(func
->bus
, PCI_DEVFN(func
->device
, func
->function
));
102 if (func
->pci_dev
== NULL
) {
103 dbg("ERROR: pci_dev still null\n");
108 if (func
->pci_dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) {
109 pci_read_config_byte(func
->pci_dev
, PCI_SECONDARY_BUS
, &bus
);
110 child
= (struct pci_bus
*) pci_add_new_bus(func
->pci_dev
->bus
, (func
->pci_dev
), bus
);
111 pci_do_scan_bus(child
);
114 pci_dev_put(func
->pci_dev
);
120 int cpqhp_unconfigure_device(struct pci_func
* func
)
124 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__
, func
->bus
, func
->device
, func
->function
);
126 for (j
=0; j
<8 ; j
++) {
127 struct pci_dev
* temp
= pci_get_bus_and_slot(func
->bus
, PCI_DEVFN(func
->device
, j
));
130 pci_remove_bus_device(temp
);
136 static int PCI_RefinedAccessConfig(struct pci_bus
*bus
, unsigned int devfn
, u8 offset
, u32
*value
)
140 if (pci_bus_read_config_dword (bus
, devfn
, PCI_VENDOR_ID
, &vendID
) == -1)
142 if (vendID
== 0xffffffff)
144 return pci_bus_read_config_dword (bus
, devfn
, offset
, value
);
151 * @bus_num: bus number of PCI device
152 * @dev_num: device number of PCI device
153 * @slot: pointer to u8 where slot number will be returned
155 int cpqhp_set_irq (u8 bus_num
, u8 dev_num
, u8 int_pin
, u8 irq_num
)
159 if (cpqhp_legacy_mode
) {
160 struct pci_dev
*fakedev
;
161 struct pci_bus
*fakebus
;
164 fakedev
= kmalloc(sizeof(*fakedev
), GFP_KERNEL
);
165 fakebus
= kmalloc(sizeof(*fakebus
), GFP_KERNEL
);
166 if (!fakedev
|| !fakebus
) {
172 fakedev
->devfn
= dev_num
<< 3;
173 fakedev
->bus
= fakebus
;
174 fakebus
->number
= bus_num
;
175 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
176 __func__
, dev_num
, bus_num
, int_pin
, irq_num
);
177 rc
= pcibios_set_irq_routing(fakedev
, int_pin
- 1, irq_num
);
180 dbg("%s: rc %d\n", __func__
, rc
);
184 /* set the Edge Level Control Register (ELCR) */
185 temp_word
= inb(0x4d0);
186 temp_word
|= inb(0x4d1) << 8;
188 temp_word
|= 0x01 << irq_num
;
190 /* This should only be for x86 as it sets the Edge Level
193 outb((u8
) (temp_word
& 0xFF), 0x4d0); outb((u8
) ((temp_word
&
194 0xFF00) >> 8), 0x4d1); rc
= 0; }
200 static int PCI_ScanBusForNonBridge(struct controller
*ctrl
, u8 bus_num
, u8
* dev_num
)
206 ctrl
->pci_bus
->number
= bus_num
;
208 for (tdevice
= 0; tdevice
< 0xFF; tdevice
++) {
209 /* Scan for access first */
210 if (PCI_RefinedAccessConfig(ctrl
->pci_bus
, tdevice
, 0x08, &work
) == -1)
212 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num
, tdevice
);
213 /* Yep we got one. Not a bridge ? */
214 if ((work
>> 8) != PCI_TO_PCI_BRIDGE_CLASS
) {
220 for (tdevice
= 0; tdevice
< 0xFF; tdevice
++) {
221 /* Scan for access first */
222 if (PCI_RefinedAccessConfig(ctrl
->pci_bus
, tdevice
, 0x08, &work
) == -1)
224 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num
, tdevice
);
225 /* Yep we got one. bridge ? */
226 if ((work
>> 8) == PCI_TO_PCI_BRIDGE_CLASS
) {
227 pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(tdevice
, 0), PCI_SECONDARY_BUS
, &tbus
);
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 busses, 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
;
316 /* Decide which slots are supported */
320 * is_hot_plug is the slot mask
322 FirstSupported
= is_hot_plug
>> 4;
323 LastSupported
= FirstSupported
+ (is_hot_plug
& 0x0F) - 1;
326 LastSupported
= 0x1F;
329 /* Save PCI configuration space for all devices in supported slots */
330 ctrl
->pci_bus
->number
= busnumber
;
331 for (device
= FirstSupported
; device
<= LastSupported
; device
++) {
333 rc
= pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(device
, 0), PCI_VENDOR_ID
, &ID
);
335 if (ID
== 0xFFFFFFFF) {
337 /* Setup slot structure with entry for empty
340 new_slot
= cpqhp_slot_create(busnumber
);
341 if (new_slot
== NULL
)
344 new_slot
->bus
= (u8
) busnumber
;
345 new_slot
->device
= (u8
) device
;
346 new_slot
->function
= 0;
347 new_slot
->is_a_board
= 0;
348 new_slot
->presence_save
= 0;
349 new_slot
->switch_save
= 0;
354 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, 0), 0x0B, &class_code
);
358 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, 0), PCI_HEADER_TYPE
, &header_type
);
362 /* If multi-function device, set max_functions to 8 */
363 if (header_type
& 0x80)
372 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
373 /* Recurse the subordinate bus
374 * get the subordinate bus number
376 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_SECONDARY_BUS
, &secondary_bus
);
380 sub_bus
= (int) secondary_bus
;
382 /* Save secondary bus cfg spc
383 * with this recursive call.
385 rc
= cpqhp_save_config(ctrl
, sub_bus
, 0);
388 ctrl
->pci_bus
->number
= busnumber
;
393 new_slot
= cpqhp_slot_find(busnumber
, device
, index
++);
395 (new_slot
->function
!= (u8
) function
))
396 new_slot
= cpqhp_slot_find(busnumber
, device
, index
++);
399 /* Setup slot structure. */
400 new_slot
= cpqhp_slot_create(busnumber
);
401 if (new_slot
== NULL
)
405 new_slot
->bus
= (u8
) busnumber
;
406 new_slot
->device
= (u8
) device
;
407 new_slot
->function
= (u8
) function
;
408 new_slot
->is_a_board
= 1;
409 new_slot
->switch_save
= 0x10;
410 /* In case of unsupported board */
411 new_slot
->status
= DevError
;
412 new_slot
->pci_dev
= pci_get_bus_and_slot(new_slot
->bus
, (new_slot
->device
<< 3) | new_slot
->function
);
414 for (cloop
= 0; cloop
< 0x20; cloop
++) {
415 rc
= pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
420 pci_dev_put(new_slot
->pci_dev
);
426 /* this loop skips to the next present function
427 * reading in Class Code and Header type.
429 while ((function
< max_functions
) && (!stop_it
)) {
430 rc
= pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_VENDOR_ID
, &ID
);
431 if (ID
== 0xFFFFFFFF) {
435 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), 0x0B, &class_code
);
439 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_HEADER_TYPE
, &header_type
);
446 } while (function
< max_functions
);
447 } /* End of FOR loop */
454 * cpqhp_save_slot_config
456 * Saves configuration info for all PCI devices in a given slot
457 * including subordinate busses.
459 * returns 0 if success
461 int cpqhp_save_slot_config (struct controller
*ctrl
, struct pci_func
* new_slot
)
476 ctrl
->pci_bus
->number
= new_slot
->bus
;
477 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_VENDOR_ID
, &ID
);
479 if (ID
== 0xFFFFFFFF)
482 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), 0x0B, &class_code
);
483 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_HEADER_TYPE
, &header_type
);
485 if (header_type
& 0x80) /* Multi-function device */
490 while (function
< max_functions
) {
491 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
492 /* Recurse the subordinate bus */
493 pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_SECONDARY_BUS
, &secondary_bus
);
495 sub_bus
= (int) secondary_bus
;
497 /* Save the config headers for the secondary
500 rc
= cpqhp_save_config(ctrl
, sub_bus
, 0);
503 ctrl
->pci_bus
->number
= new_slot
->bus
;
507 new_slot
->status
= 0;
509 for (cloop
= 0; cloop
< 0x20; cloop
++)
510 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
516 /* this loop skips to the next present function
517 * reading in the Class Code and the Header type.
519 while ((function
< max_functions
) && (!stop_it
)) {
520 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_VENDOR_ID
, &ID
);
522 if (ID
== 0xFFFFFFFF)
525 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), 0x0B, &class_code
);
526 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_HEADER_TYPE
, &header_type
);
538 * cpqhp_save_base_addr_length
540 * Saves the length of all base address registers for the
541 * specified slot. this is for hot plug REPLACE
543 * returns 0 if success
545 int cpqhp_save_base_addr_length(struct controller
*ctrl
, struct pci_func
* func
)
555 struct pci_func
*next
;
557 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
560 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
562 while (func
!= NULL
) {
563 pci_bus
->number
= func
->bus
;
564 devfn
= PCI_DEVFN(func
->device
, func
->function
);
566 /* Check for Bridge */
567 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
569 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
570 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
572 sub_bus
= (int) secondary_bus
;
574 next
= cpqhp_slot_list
[sub_bus
];
576 while (next
!= NULL
) {
577 rc
= cpqhp_save_base_addr_length(ctrl
, next
);
583 pci_bus
->number
= func
->bus
;
585 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
586 temp_register
= 0xFFFFFFFF;
587 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
588 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
589 /* If this register is implemented */
593 * set base = amount of IO space
596 base
= base
& 0xFFFFFFFE;
602 base
= base
& 0xFFFFFFF0;
612 /* Save information in slot structure */
613 func
->base_length
[(cloop
- 0x10) >> 2] =
615 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
617 } /* End of base register loop */
619 } else if ((header_type
& 0x7F) == 0x00) {
620 /* Figure out IO and memory base lengths */
621 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
622 temp_register
= 0xFFFFFFFF;
623 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
624 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
626 /* If this register is implemented */
630 * base = amount of IO space
633 base
= base
& 0xFFFFFFFE;
639 * base = amount of memory
642 base
= base
& 0xFFFFFFF0;
652 /* Save information in slot structure */
653 func
->base_length
[(cloop
- 0x10) >> 2] = base
;
654 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
656 } /* End of base register loop */
658 } else { /* Some other unknown header type */
661 /* find the next device in this slot */
662 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
670 * cpqhp_save_used_resources
672 * Stores used resource information for existing boards. this is
673 * for boards that were in the system when this driver was loaded.
674 * this function is for hot plug ADD
676 * returns 0 if success
678 int cpqhp_save_used_resources (struct controller
*ctrl
, struct pci_func
* func
)
694 struct pci_resource
*mem_node
;
695 struct pci_resource
*p_mem_node
;
696 struct pci_resource
*io_node
;
697 struct pci_resource
*bus_node
;
698 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
701 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
703 while ((func
!= NULL
) && func
->is_a_board
) {
704 pci_bus
->number
= func
->bus
;
705 devfn
= PCI_DEVFN(func
->device
, func
->function
);
707 /* Save the command register */
708 pci_bus_read_config_word(pci_bus
, devfn
, PCI_COMMAND
, &save_command
);
712 pci_bus_write_config_word(pci_bus
, devfn
, PCI_COMMAND
, command
);
714 /* Check for Bridge */
715 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
717 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
718 /* Clear Bridge Control Register */
720 pci_bus_write_config_word(pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
721 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
722 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, &temp_byte
);
724 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
728 bus_node
->base
= secondary_bus
;
729 bus_node
->length
= temp_byte
- secondary_bus
+ 1;
731 bus_node
->next
= func
->bus_head
;
732 func
->bus_head
= bus_node
;
734 /* Save IO base and Limit registers */
735 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, &b_base
);
736 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, &b_length
);
738 if ((b_base
<= b_length
) && (save_command
& 0x01)) {
739 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
743 io_node
->base
= (b_base
& 0xF0) << 8;
744 io_node
->length
= (b_length
- b_base
+ 0x10) << 8;
746 io_node
->next
= func
->io_head
;
747 func
->io_head
= io_node
;
750 /* Save memory base and Limit registers */
751 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, &w_base
);
752 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, &w_length
);
754 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
755 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
759 mem_node
->base
= w_base
<< 16;
760 mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
762 mem_node
->next
= func
->mem_head
;
763 func
->mem_head
= mem_node
;
766 /* Save prefetchable memory base and Limit registers */
767 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, &w_base
);
768 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, &w_length
);
770 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
771 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
775 p_mem_node
->base
= w_base
<< 16;
776 p_mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
778 p_mem_node
->next
= func
->p_mem_head
;
779 func
->p_mem_head
= p_mem_node
;
781 /* Figure out IO and memory base lengths */
782 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
783 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &save_base
);
785 temp_register
= 0xFFFFFFFF;
786 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
787 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
789 temp_register
= base
;
791 /* If this register is implemented */
793 if (((base
& 0x03L
) == 0x01)
794 && (save_command
& 0x01)) {
796 * set temp_register = amount
797 * of IO space requested
799 temp_register
= base
& 0xFFFFFFFE;
800 temp_register
= (~temp_register
) + 1;
802 io_node
= kmalloc(sizeof(*io_node
),
808 save_base
& (~0x03L
);
809 io_node
->length
= temp_register
;
811 io_node
->next
= func
->io_head
;
812 func
->io_head
= io_node
;
814 if (((base
& 0x0BL
) == 0x08)
815 && (save_command
& 0x02)) {
816 /* prefetchable memory base */
817 temp_register
= base
& 0xFFFFFFF0;
818 temp_register
= (~temp_register
) + 1;
820 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
825 p_mem_node
->base
= save_base
& (~0x0FL
);
826 p_mem_node
->length
= temp_register
;
828 p_mem_node
->next
= func
->p_mem_head
;
829 func
->p_mem_head
= p_mem_node
;
831 if (((base
& 0x0BL
) == 0x00)
832 && (save_command
& 0x02)) {
833 /* prefetchable memory base */
834 temp_register
= base
& 0xFFFFFFF0;
835 temp_register
= (~temp_register
) + 1;
837 mem_node
= kmalloc(sizeof(*mem_node
),
842 mem_node
->base
= save_base
& (~0x0FL
);
843 mem_node
->length
= temp_register
;
845 mem_node
->next
= func
->mem_head
;
846 func
->mem_head
= mem_node
;
850 } /* End of base register loop */
851 /* Standard header */
852 } else if ((header_type
& 0x7F) == 0x00) {
853 /* Figure out IO and memory base lengths */
854 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
855 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &save_base
);
857 temp_register
= 0xFFFFFFFF;
858 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
859 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
861 temp_register
= base
;
863 /* If this register is implemented */
865 if (((base
& 0x03L
) == 0x01)
866 && (save_command
& 0x01)) {
868 * set temp_register = amount
869 * of IO space requested
871 temp_register
= base
& 0xFFFFFFFE;
872 temp_register
= (~temp_register
) + 1;
874 io_node
= kmalloc(sizeof(*io_node
),
879 io_node
->base
= save_base
& (~0x01L
);
880 io_node
->length
= temp_register
;
882 io_node
->next
= func
->io_head
;
883 func
->io_head
= io_node
;
885 if (((base
& 0x0BL
) == 0x08)
886 && (save_command
& 0x02)) {
887 /* prefetchable memory base */
888 temp_register
= base
& 0xFFFFFFF0;
889 temp_register
= (~temp_register
) + 1;
891 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
896 p_mem_node
->base
= save_base
& (~0x0FL
);
897 p_mem_node
->length
= temp_register
;
899 p_mem_node
->next
= func
->p_mem_head
;
900 func
->p_mem_head
= p_mem_node
;
902 if (((base
& 0x0BL
) == 0x00)
903 && (save_command
& 0x02)) {
904 /* prefetchable memory base */
905 temp_register
= base
& 0xFFFFFFF0;
906 temp_register
= (~temp_register
) + 1;
908 mem_node
= kmalloc(sizeof(*mem_node
),
913 mem_node
->base
= save_base
& (~0x0FL
);
914 mem_node
->length
= temp_register
;
916 mem_node
->next
= func
->mem_head
;
917 func
->mem_head
= mem_node
;
921 } /* End of base register loop */
924 /* find the next device in this slot */
925 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
933 * cpqhp_configure_board
935 * Copies saved configuration information to one slot.
936 * this is called recursively for bridge devices.
937 * this is for hot plug REPLACE!
939 * returns 0 if success
941 int cpqhp_configure_board(struct controller
*ctrl
, struct pci_func
* func
)
947 struct pci_func
*next
;
951 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
954 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
956 while (func
!= NULL
) {
957 pci_bus
->number
= func
->bus
;
958 devfn
= PCI_DEVFN(func
->device
, func
->function
);
960 /* Start at the top of config space so that the control
961 * registers are programmed last
963 for (cloop
= 0x3C; cloop
> 0; cloop
-= 4)
964 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, func
->config_space
[cloop
>> 2]);
966 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
968 /* If this is a bridge device, restore subordinate devices */
969 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
970 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
972 sub_bus
= (int) secondary_bus
;
974 next
= cpqhp_slot_list
[sub_bus
];
976 while (next
!= NULL
) {
977 rc
= cpqhp_configure_board(ctrl
, next
);
985 /* Check all the base Address Registers to make sure
986 * they are the same. If not, the board is different.
989 for (cloop
= 16; cloop
< 40; cloop
+= 4) {
990 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp
);
992 if (temp
!= func
->config_space
[cloop
>> 2]) {
993 dbg("Config space compare failure!!! offset = %x\n", cloop
);
994 dbg("bus = %x, device = %x, function = %x\n", func
->bus
, func
->device
, func
->function
);
995 dbg("temp = %x, config space = %x\n\n", temp
, func
->config_space
[cloop
>> 2]);
1001 func
->configured
= 1;
1003 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1011 * cpqhp_valid_replace
1013 * this function checks to see if a board is the same as the
1014 * one it is replacing. this check will detect if the device's
1015 * vendor or device id's are the same
1017 * returns 0 if the board is the same nonzero otherwise
1019 int cpqhp_valid_replace(struct controller
*ctrl
, struct pci_func
* func
)
1025 u32 temp_register
= 0;
1028 struct pci_func
*next
;
1030 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
1033 if (!func
->is_a_board
)
1034 return(ADD_NOT_SUPPORTED
);
1036 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1038 while (func
!= NULL
) {
1039 pci_bus
->number
= func
->bus
;
1040 devfn
= PCI_DEVFN(func
->device
, func
->function
);
1042 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_VENDOR_ID
, &temp_register
);
1044 /* No adapter present */
1045 if (temp_register
== 0xFFFFFFFF)
1046 return(NO_ADAPTER_PRESENT
);
1048 if (temp_register
!= func
->config_space
[0])
1049 return(ADAPTER_NOT_SAME
);
1051 /* Check for same revision number and class code */
1052 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_CLASS_REVISION
, &temp_register
);
1054 /* Adapter not the same */
1055 if (temp_register
!= func
->config_space
[0x08 >> 2])
1056 return(ADAPTER_NOT_SAME
);
1058 /* Check for Bridge */
1059 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
1061 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
1062 /* In order to continue checking, we must program the
1063 * bus registers in the bridge to respond to accesses
1064 * for its subordinate bus(es)
1067 temp_register
= func
->config_space
[0x18 >> 2];
1068 pci_bus_write_config_dword (pci_bus
, devfn
, PCI_PRIMARY_BUS
, temp_register
);
1070 secondary_bus
= (temp_register
>> 8) & 0xFF;
1072 next
= cpqhp_slot_list
[secondary_bus
];
1074 while (next
!= NULL
) {
1075 rc
= cpqhp_valid_replace(ctrl
, next
);
1083 /* Check to see if it is a standard config header */
1084 else if ((header_type
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
1085 /* Check subsystem vendor and ID */
1086 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_SUBSYSTEM_VENDOR_ID
, &temp_register
);
1088 if (temp_register
!= func
->config_space
[0x2C >> 2]) {
1089 /* If it's a SMART-2 and the register isn't
1090 * filled in, ignore the difference because
1091 * they just have an old rev of the firmware
1093 if (!((func
->config_space
[0] == 0xAE100E11)
1094 && (temp_register
== 0x00L
)))
1095 return(ADAPTER_NOT_SAME
);
1097 /* Figure out IO and memory base lengths */
1098 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
1099 temp_register
= 0xFFFFFFFF;
1100 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
1101 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
1103 /* If this register is implemented */
1107 * set base = amount of IO
1110 base
= base
& 0xFFFFFFFE;
1116 base
= base
& 0xFFFFFFF0;
1126 /* Check information in slot structure */
1127 if (func
->base_length
[(cloop
- 0x10) >> 2] != base
)
1128 return(ADAPTER_NOT_SAME
);
1130 if (func
->base_type
[(cloop
- 0x10) >> 2] != type
)
1131 return(ADAPTER_NOT_SAME
);
1133 } /* End of base register loop */
1135 } /* End of (type 0 config space) else */
1137 /* this is not a type 0 or 1 config space header so
1138 * we don't know how to do it
1140 return(DEVICE_TYPE_NOT_SUPPORTED
);
1143 /* Get the next function */
1144 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1153 * cpqhp_find_available_resources
1155 * Finds available memory, IO, and IRQ resources for programming
1156 * devices which may be added to the system
1157 * this function is for hot plug ADD!
1159 * returns 0 if success
1161 int cpqhp_find_available_resources(struct controller
*ctrl
, void __iomem
*rom_start
)
1166 void __iomem
*one_slot
;
1167 void __iomem
*rom_resource_table
;
1168 struct pci_func
*func
= NULL
;
1171 struct pci_resource
*mem_node
;
1172 struct pci_resource
*p_mem_node
;
1173 struct pci_resource
*io_node
;
1174 struct pci_resource
*bus_node
;
1176 rom_resource_table
= detect_HRT_floating_pointer(rom_start
, rom_start
+0xffff);
1177 dbg("rom_resource_table = %p\n", rom_resource_table
);
1179 if (rom_resource_table
== NULL
)
1182 /* Sum all resources and setup resource maps */
1183 unused_IRQ
= readl(rom_resource_table
+ UNUSED_IRQ
);
1184 dbg("unused_IRQ = %x\n", unused_IRQ
);
1187 while (unused_IRQ
) {
1188 if (unused_IRQ
& 1) {
1189 cpqhp_disk_irq
= temp
;
1192 unused_IRQ
= unused_IRQ
>> 1;
1196 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq
);
1197 unused_IRQ
= unused_IRQ
>> 1;
1200 while (unused_IRQ
) {
1201 if (unused_IRQ
& 1) {
1202 cpqhp_nic_irq
= temp
;
1205 unused_IRQ
= unused_IRQ
>> 1;
1209 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq
);
1210 unused_IRQ
= readl(rom_resource_table
+ PCIIRQ
);
1215 cpqhp_nic_irq
= ctrl
->cfgspc_irq
;
1217 if (!cpqhp_disk_irq
)
1218 cpqhp_disk_irq
= ctrl
->cfgspc_irq
;
1220 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq
, cpqhp_nic_irq
);
1222 rc
= compaq_nvram_load(rom_start
, ctrl
);
1226 one_slot
= rom_resource_table
+ sizeof (struct hrt
);
1228 i
= readb(rom_resource_table
+ NUMBER_OF_ENTRIES
);
1229 dbg("number_of_entries = %d\n", i
);
1231 if (!readb(one_slot
+ SECONDARY_BUS
))
1234 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1236 while (i
&& readb(one_slot
+ SECONDARY_BUS
)) {
1237 u8 dev_func
= readb(one_slot
+ DEV_FUNC
);
1238 u8 primary_bus
= readb(one_slot
+ PRIMARY_BUS
);
1239 u8 secondary_bus
= readb(one_slot
+ SECONDARY_BUS
);
1240 u8 max_bus
= readb(one_slot
+ MAX_BUS
);
1241 u16 io_base
= readw(one_slot
+ IO_BASE
);
1242 u16 io_length
= readw(one_slot
+ IO_LENGTH
);
1243 u16 mem_base
= readw(one_slot
+ MEM_BASE
);
1244 u16 mem_length
= readw(one_slot
+ MEM_LENGTH
);
1245 u16 pre_mem_base
= readw(one_slot
+ PRE_MEM_BASE
);
1246 u16 pre_mem_length
= readw(one_slot
+ PRE_MEM_LENGTH
);
1248 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1249 dev_func
, io_base
, io_length
, mem_base
, mem_length
, pre_mem_base
, pre_mem_length
,
1250 primary_bus
, secondary_bus
, max_bus
);
1252 /* If this entry isn't for our controller's bus, ignore it */
1253 if (primary_bus
!= ctrl
->bus
) {
1255 one_slot
+= sizeof (struct slot_rt
);
1258 /* find out if this entry is for an occupied slot */
1259 ctrl
->pci_bus
->number
= primary_bus
;
1260 pci_bus_read_config_dword (ctrl
->pci_bus
, dev_func
, PCI_VENDOR_ID
, &temp_dword
);
1261 dbg("temp_D_word = %x\n", temp_dword
);
1263 if (temp_dword
!= 0xFFFFFFFF) {
1265 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, 0);
1267 while (func
&& (func
->function
!= (dev_func
& 0x07))) {
1268 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func
, primary_bus
, dev_func
>> 3, index
);
1269 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, index
++);
1272 /* If we can't find a match, skip this table entry */
1275 one_slot
+= sizeof (struct slot_rt
);
1278 /* this may not work and shouldn't be used */
1279 if (secondary_bus
!= primary_bus
)
1291 /* If we've got a valid IO base, use it */
1293 temp_dword
= io_base
+ io_length
;
1295 if ((io_base
) && (temp_dword
< 0x10000)) {
1296 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
1300 io_node
->base
= io_base
;
1301 io_node
->length
= io_length
;
1303 dbg("found io_node(base, length) = %x, %x\n",
1304 io_node
->base
, io_node
->length
);
1305 dbg("populated slot =%d \n", populated_slot
);
1306 if (!populated_slot
) {
1307 io_node
->next
= ctrl
->io_head
;
1308 ctrl
->io_head
= io_node
;
1310 io_node
->next
= func
->io_head
;
1311 func
->io_head
= io_node
;
1315 /* If we've got a valid memory base, use it */
1316 temp_dword
= mem_base
+ mem_length
;
1317 if ((mem_base
) && (temp_dword
< 0x10000)) {
1318 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
1322 mem_node
->base
= mem_base
<< 16;
1324 mem_node
->length
= mem_length
<< 16;
1326 dbg("found mem_node(base, length) = %x, %x\n",
1327 mem_node
->base
, mem_node
->length
);
1328 dbg("populated slot =%d \n", populated_slot
);
1329 if (!populated_slot
) {
1330 mem_node
->next
= ctrl
->mem_head
;
1331 ctrl
->mem_head
= mem_node
;
1333 mem_node
->next
= func
->mem_head
;
1334 func
->mem_head
= mem_node
;
1338 /* If we've got a valid prefetchable memory base, and
1339 * the base + length isn't greater than 0xFFFF
1341 temp_dword
= pre_mem_base
+ pre_mem_length
;
1342 if ((pre_mem_base
) && (temp_dword
< 0x10000)) {
1343 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
1347 p_mem_node
->base
= pre_mem_base
<< 16;
1349 p_mem_node
->length
= pre_mem_length
<< 16;
1350 dbg("found p_mem_node(base, length) = %x, %x\n",
1351 p_mem_node
->base
, p_mem_node
->length
);
1352 dbg("populated slot =%d \n", populated_slot
);
1354 if (!populated_slot
) {
1355 p_mem_node
->next
= ctrl
->p_mem_head
;
1356 ctrl
->p_mem_head
= p_mem_node
;
1358 p_mem_node
->next
= func
->p_mem_head
;
1359 func
->p_mem_head
= p_mem_node
;
1363 /* If we've got a valid bus number, use it
1364 * The second condition is to ignore bus numbers on
1365 * populated slots that don't have PCI-PCI bridges
1367 if (secondary_bus
&& (secondary_bus
!= primary_bus
)) {
1368 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
1372 bus_node
->base
= secondary_bus
;
1373 bus_node
->length
= max_bus
- secondary_bus
+ 1;
1374 dbg("found bus_node(base, length) = %x, %x\n",
1375 bus_node
->base
, bus_node
->length
);
1376 dbg("populated slot =%d \n", populated_slot
);
1377 if (!populated_slot
) {
1378 bus_node
->next
= ctrl
->bus_head
;
1379 ctrl
->bus_head
= bus_node
;
1381 bus_node
->next
= func
->bus_head
;
1382 func
->bus_head
= bus_node
;
1387 one_slot
+= sizeof (struct slot_rt
);
1390 /* If all of the following fail, we don't have any resources for
1394 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1395 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1396 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1397 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1404 * cpqhp_return_board_resources
1406 * this routine returns all resources allocated to a board to
1407 * the available pool.
1409 * returns 0 if success
1411 int cpqhp_return_board_resources(struct pci_func
* func
, struct resource_lists
* resources
)
1414 struct pci_resource
*node
;
1415 struct pci_resource
*t_node
;
1416 dbg("%s\n", __func__
);
1421 node
= func
->io_head
;
1422 func
->io_head
= NULL
;
1424 t_node
= node
->next
;
1425 return_resource(&(resources
->io_head
), node
);
1429 node
= func
->mem_head
;
1430 func
->mem_head
= NULL
;
1432 t_node
= node
->next
;
1433 return_resource(&(resources
->mem_head
), node
);
1437 node
= func
->p_mem_head
;
1438 func
->p_mem_head
= NULL
;
1440 t_node
= node
->next
;
1441 return_resource(&(resources
->p_mem_head
), node
);
1445 node
= func
->bus_head
;
1446 func
->bus_head
= NULL
;
1448 t_node
= node
->next
;
1449 return_resource(&(resources
->bus_head
), node
);
1453 rc
|= cpqhp_resource_sort_and_combine(&(resources
->mem_head
));
1454 rc
|= cpqhp_resource_sort_and_combine(&(resources
->p_mem_head
));
1455 rc
|= cpqhp_resource_sort_and_combine(&(resources
->io_head
));
1456 rc
|= cpqhp_resource_sort_and_combine(&(resources
->bus_head
));
1463 * cpqhp_destroy_resource_list
1465 * Puts node back in the resource list pointed to by head
1467 void cpqhp_destroy_resource_list (struct resource_lists
* resources
)
1469 struct pci_resource
*res
, *tres
;
1471 res
= resources
->io_head
;
1472 resources
->io_head
= NULL
;
1480 res
= resources
->mem_head
;
1481 resources
->mem_head
= NULL
;
1489 res
= resources
->p_mem_head
;
1490 resources
->p_mem_head
= NULL
;
1498 res
= resources
->bus_head
;
1499 resources
->bus_head
= NULL
;
1510 * cpqhp_destroy_board_resources
1512 * Puts node back in the resource list pointed to by head
1514 void cpqhp_destroy_board_resources (struct pci_func
* func
)
1516 struct pci_resource
*res
, *tres
;
1518 res
= func
->io_head
;
1519 func
->io_head
= NULL
;
1527 res
= func
->mem_head
;
1528 func
->mem_head
= NULL
;
1536 res
= func
->p_mem_head
;
1537 func
->p_mem_head
= NULL
;
1545 res
= func
->bus_head
;
1546 func
->bus_head
= NULL
;