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 /* XXX: no recursion, wtf? */
229 dbg("Recurse on bus_num %d tdevice %d\n", tbus
, tdevice
);
238 static int PCI_GetBusDevHelper(struct controller
*ctrl
, u8
*bus_num
, u8
*dev_num
, u8 slot
, u8 nobridge
)
242 u8 tbus
, tdevice
, tslot
;
244 len
= cpqhp_routing_table_length();
245 for (loop
= 0; loop
< len
; ++loop
) {
246 tbus
= cpqhp_routing_table
->slots
[loop
].bus
;
247 tdevice
= cpqhp_routing_table
->slots
[loop
].devfn
;
248 tslot
= cpqhp_routing_table
->slots
[loop
].slot
;
253 ctrl
->pci_bus
->number
= tbus
;
254 pci_bus_read_config_dword (ctrl
->pci_bus
, *dev_num
, PCI_VENDOR_ID
, &work
);
255 if (!nobridge
|| (work
== 0xffffffff))
258 dbg("bus_num %d devfn %d\n", *bus_num
, *dev_num
);
259 pci_bus_read_config_dword (ctrl
->pci_bus
, *dev_num
, PCI_CLASS_REVISION
, &work
);
260 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work
>> 8, PCI_TO_PCI_BRIDGE_CLASS
);
262 if ((work
>> 8) == PCI_TO_PCI_BRIDGE_CLASS
) {
263 pci_bus_read_config_byte (ctrl
->pci_bus
, *dev_num
, PCI_SECONDARY_BUS
, &tbus
);
264 dbg("Scan bus for Non Bridge: bus %d\n", tbus
);
265 if (PCI_ScanBusForNonBridge(ctrl
, tbus
, dev_num
) == 0) {
277 int cpqhp_get_bus_dev (struct controller
*ctrl
, u8
* bus_num
, u8
* dev_num
, u8 slot
)
279 /* plain (bridges allowed) */
280 return PCI_GetBusDevHelper(ctrl
, bus_num
, dev_num
, slot
, 0);
284 /* More PCI configuration routines; this time centered around hotplug
292 * Reads configuration for all slots in a PCI bus and saves info.
294 * Note: For non-hot plug busses, the slot # saved is the device #
296 * returns 0 if success
298 int cpqhp_save_config(struct controller
*ctrl
, int busnumber
, int is_hot_plug
)
305 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 new_slot
->pci_dev
= pci_get_bus_and_slot(new_slot
->bus
, (new_slot
->device
<< 3) | new_slot
->function
);
415 for (cloop
= 0; cloop
< 0x20; cloop
++) {
416 rc
= pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
421 pci_dev_put(new_slot
->pci_dev
);
427 /* this loop skips to the next present function
428 * reading in Class Code and Header type.
430 while ((function
< max_functions
) && (!stop_it
)) {
431 rc
= pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_VENDOR_ID
, &ID
);
432 if (ID
== 0xFFFFFFFF) {
436 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), 0x0B, &class_code
);
440 rc
= pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(device
, function
), PCI_HEADER_TYPE
, &header_type
);
447 } while (function
< max_functions
);
448 } /* End of FOR loop */
455 * cpqhp_save_slot_config
457 * Saves configuration info for all PCI devices in a given slot
458 * including subordinate busses.
460 * returns 0 if success
462 int cpqhp_save_slot_config (struct controller
*ctrl
, struct pci_func
* new_slot
)
477 ctrl
->pci_bus
->number
= new_slot
->bus
;
478 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_VENDOR_ID
, &ID
);
480 if (ID
== 0xFFFFFFFF)
483 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), 0x0B, &class_code
);
484 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, 0), PCI_HEADER_TYPE
, &header_type
);
486 if (header_type
& 0x80) /* Multi-function device */
491 while (function
< max_functions
) {
492 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
493 /* Recurse the subordinate bus */
494 pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_SECONDARY_BUS
, &secondary_bus
);
496 sub_bus
= (int) secondary_bus
;
498 /* Save the config headers for the secondary
501 rc
= cpqhp_save_config(ctrl
, sub_bus
, 0);
504 ctrl
->pci_bus
->number
= new_slot
->bus
;
508 new_slot
->status
= 0;
510 for (cloop
= 0; cloop
< 0x20; cloop
++)
511 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), cloop
<< 2, (u32
*) & (new_slot
-> config_space
[cloop
]));
517 /* this loop skips to the next present function
518 * reading in the Class Code and the Header type.
520 while ((function
< max_functions
) && (!stop_it
)) {
521 pci_bus_read_config_dword(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_VENDOR_ID
, &ID
);
523 if (ID
== 0xFFFFFFFF)
526 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), 0x0B, &class_code
);
527 pci_bus_read_config_byte(ctrl
->pci_bus
, PCI_DEVFN(new_slot
->device
, function
), PCI_HEADER_TYPE
, &header_type
);
539 * cpqhp_save_base_addr_length
541 * Saves the length of all base address registers for the
542 * specified slot. this is for hot plug REPLACE
544 * returns 0 if success
546 int cpqhp_save_base_addr_length(struct controller
*ctrl
, struct pci_func
* func
)
556 struct pci_func
*next
;
558 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
561 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
563 while (func
!= NULL
) {
564 pci_bus
->number
= func
->bus
;
565 devfn
= PCI_DEVFN(func
->device
, func
->function
);
567 /* Check for Bridge */
568 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
570 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
571 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
573 sub_bus
= (int) secondary_bus
;
575 next
= cpqhp_slot_list
[sub_bus
];
577 while (next
!= NULL
) {
578 rc
= cpqhp_save_base_addr_length(ctrl
, next
);
584 pci_bus
->number
= func
->bus
;
586 /* FIXME: this loop is duplicated in the non-bridge
587 * case. The two could be rolled together Figure out
588 * IO and memory base lengths
590 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
591 temp_register
= 0xFFFFFFFF;
592 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
593 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
594 /* If this register is implemented */
598 * set base = amount of IO space
601 base
= base
& 0xFFFFFFFE;
607 base
= base
& 0xFFFFFFF0;
617 /* Save information in slot structure */
618 func
->base_length
[(cloop
- 0x10) >> 2] =
620 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
622 } /* End of base register loop */
624 } else if ((header_type
& 0x7F) == 0x00) {
625 /* Figure out IO and memory base lengths */
626 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
627 temp_register
= 0xFFFFFFFF;
628 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
629 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
631 /* If this register is implemented */
635 * base = amount of IO space
638 base
= base
& 0xFFFFFFFE;
644 * base = amount of memory
647 base
= base
& 0xFFFFFFF0;
657 /* Save information in slot structure */
658 func
->base_length
[(cloop
- 0x10) >> 2] = base
;
659 func
->base_type
[(cloop
- 0x10) >> 2] = type
;
661 } /* End of base register loop */
663 } else { /* Some other unknown header type */
666 /* find the next device in this slot */
667 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
675 * cpqhp_save_used_resources
677 * Stores used resource information for existing boards. this is
678 * for boards that were in the system when this driver was loaded.
679 * this function is for hot plug ADD
681 * returns 0 if success
683 int cpqhp_save_used_resources (struct controller
*ctrl
, struct pci_func
* func
)
699 struct pci_resource
*mem_node
;
700 struct pci_resource
*p_mem_node
;
701 struct pci_resource
*io_node
;
702 struct pci_resource
*bus_node
;
703 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
706 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
708 while ((func
!= NULL
) && func
->is_a_board
) {
709 pci_bus
->number
= func
->bus
;
710 devfn
= PCI_DEVFN(func
->device
, func
->function
);
712 /* Save the command register */
713 pci_bus_read_config_word(pci_bus
, devfn
, PCI_COMMAND
, &save_command
);
717 pci_bus_write_config_word(pci_bus
, devfn
, PCI_COMMAND
, command
);
719 /* Check for Bridge */
720 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
722 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
723 /* Clear Bridge Control Register */
725 pci_bus_write_config_word(pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
726 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
727 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, &temp_byte
);
729 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
733 bus_node
->base
= secondary_bus
;
734 bus_node
->length
= temp_byte
- secondary_bus
+ 1;
736 bus_node
->next
= func
->bus_head
;
737 func
->bus_head
= bus_node
;
739 /* Save IO base and Limit registers */
740 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, &b_base
);
741 pci_bus_read_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, &b_length
);
743 if ((b_base
<= b_length
) && (save_command
& 0x01)) {
744 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
748 io_node
->base
= (b_base
& 0xF0) << 8;
749 io_node
->length
= (b_length
- b_base
+ 0x10) << 8;
751 io_node
->next
= func
->io_head
;
752 func
->io_head
= io_node
;
755 /* Save memory base and Limit registers */
756 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, &w_base
);
757 pci_bus_read_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, &w_length
);
759 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
760 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
764 mem_node
->base
= w_base
<< 16;
765 mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
767 mem_node
->next
= func
->mem_head
;
768 func
->mem_head
= mem_node
;
771 /* Save prefetchable memory base and Limit registers */
772 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, &w_base
);
773 pci_bus_read_config_word(pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, &w_length
);
775 if ((w_base
<= w_length
) && (save_command
& 0x02)) {
776 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
780 p_mem_node
->base
= w_base
<< 16;
781 p_mem_node
->length
= (w_length
- w_base
+ 0x10) << 16;
783 p_mem_node
->next
= func
->p_mem_head
;
784 func
->p_mem_head
= p_mem_node
;
786 /* Figure out IO and memory base lengths */
787 for (cloop
= 0x10; cloop
<= 0x14; cloop
+= 4) {
788 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &save_base
);
790 temp_register
= 0xFFFFFFFF;
791 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
792 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
794 temp_register
= base
;
796 /* If this register is implemented */
798 if (((base
& 0x03L
) == 0x01)
799 && (save_command
& 0x01)) {
801 * set temp_register = amount
802 * of IO space requested
804 temp_register
= base
& 0xFFFFFFFE;
805 temp_register
= (~temp_register
) + 1;
807 io_node
= kmalloc(sizeof(*io_node
),
813 save_base
& (~0x03L
);
814 io_node
->length
= temp_register
;
816 io_node
->next
= func
->io_head
;
817 func
->io_head
= io_node
;
819 if (((base
& 0x0BL
) == 0x08)
820 && (save_command
& 0x02)) {
821 /* prefetchable memory base */
822 temp_register
= base
& 0xFFFFFFF0;
823 temp_register
= (~temp_register
) + 1;
825 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
830 p_mem_node
->base
= save_base
& (~0x0FL
);
831 p_mem_node
->length
= temp_register
;
833 p_mem_node
->next
= func
->p_mem_head
;
834 func
->p_mem_head
= p_mem_node
;
836 if (((base
& 0x0BL
) == 0x00)
837 && (save_command
& 0x02)) {
838 /* prefetchable memory base */
839 temp_register
= base
& 0xFFFFFFF0;
840 temp_register
= (~temp_register
) + 1;
842 mem_node
= kmalloc(sizeof(*mem_node
),
847 mem_node
->base
= save_base
& (~0x0FL
);
848 mem_node
->length
= temp_register
;
850 mem_node
->next
= func
->mem_head
;
851 func
->mem_head
= mem_node
;
855 } /* End of base register loop */
856 /* Standard header */
857 } else if ((header_type
& 0x7F) == 0x00) {
858 /* Figure out IO and memory base lengths */
859 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
860 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &save_base
);
862 temp_register
= 0xFFFFFFFF;
863 pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, temp_register
);
864 pci_bus_read_config_dword(pci_bus
, devfn
, cloop
, &base
);
866 temp_register
= base
;
868 /* If this register is implemented */
870 if (((base
& 0x03L
) == 0x01)
871 && (save_command
& 0x01)) {
873 * set temp_register = amount
874 * of IO space requested
876 temp_register
= base
& 0xFFFFFFFE;
877 temp_register
= (~temp_register
) + 1;
879 io_node
= kmalloc(sizeof(*io_node
),
884 io_node
->base
= save_base
& (~0x01L
);
885 io_node
->length
= temp_register
;
887 io_node
->next
= func
->io_head
;
888 func
->io_head
= io_node
;
890 if (((base
& 0x0BL
) == 0x08)
891 && (save_command
& 0x02)) {
892 /* prefetchable memory base */
893 temp_register
= base
& 0xFFFFFFF0;
894 temp_register
= (~temp_register
) + 1;
896 p_mem_node
= kmalloc(sizeof(*p_mem_node
),
901 p_mem_node
->base
= save_base
& (~0x0FL
);
902 p_mem_node
->length
= temp_register
;
904 p_mem_node
->next
= func
->p_mem_head
;
905 func
->p_mem_head
= p_mem_node
;
907 if (((base
& 0x0BL
) == 0x00)
908 && (save_command
& 0x02)) {
909 /* prefetchable memory base */
910 temp_register
= base
& 0xFFFFFFF0;
911 temp_register
= (~temp_register
) + 1;
913 mem_node
= kmalloc(sizeof(*mem_node
),
918 mem_node
->base
= save_base
& (~0x0FL
);
919 mem_node
->length
= temp_register
;
921 mem_node
->next
= func
->mem_head
;
922 func
->mem_head
= mem_node
;
926 } /* End of base register loop */
929 /* find the next device in this slot */
930 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
938 * cpqhp_configure_board
940 * Copies saved configuration information to one slot.
941 * this is called recursively for bridge devices.
942 * this is for hot plug REPLACE!
944 * returns 0 if success
946 int cpqhp_configure_board(struct controller
*ctrl
, struct pci_func
* func
)
952 struct pci_func
*next
;
956 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
959 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
961 while (func
!= NULL
) {
962 pci_bus
->number
= func
->bus
;
963 devfn
= PCI_DEVFN(func
->device
, func
->function
);
965 /* Start at the top of config space so that the control
966 * registers are programmed last
968 for (cloop
= 0x3C; cloop
> 0; cloop
-= 4)
969 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, func
->config_space
[cloop
>> 2]);
971 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
973 /* If this is a bridge device, restore subordinate devices */
974 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
975 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_SECONDARY_BUS
, &secondary_bus
);
977 sub_bus
= (int) secondary_bus
;
979 next
= cpqhp_slot_list
[sub_bus
];
981 while (next
!= NULL
) {
982 rc
= cpqhp_configure_board(ctrl
, next
);
990 /* Check all the base Address Registers to make sure
991 * they are the same. If not, the board is different.
994 for (cloop
= 16; cloop
< 40; cloop
+= 4) {
995 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp
);
997 if (temp
!= func
->config_space
[cloop
>> 2]) {
998 dbg("Config space compare failure!!! offset = %x\n", cloop
);
999 dbg("bus = %x, device = %x, function = %x\n", func
->bus
, func
->device
, func
->function
);
1000 dbg("temp = %x, config space = %x\n\n", temp
, func
->config_space
[cloop
>> 2]);
1006 func
->configured
= 1;
1008 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1016 * cpqhp_valid_replace
1018 * this function checks to see if a board is the same as the
1019 * one it is replacing. this check will detect if the device's
1020 * vendor or device id's are the same
1022 * returns 0 if the board is the same nonzero otherwise
1024 int cpqhp_valid_replace(struct controller
*ctrl
, struct pci_func
* func
)
1030 u32 temp_register
= 0;
1033 struct pci_func
*next
;
1035 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
1038 if (!func
->is_a_board
)
1039 return(ADD_NOT_SUPPORTED
);
1041 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1043 while (func
!= NULL
) {
1044 pci_bus
->number
= func
->bus
;
1045 devfn
= PCI_DEVFN(func
->device
, func
->function
);
1047 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_VENDOR_ID
, &temp_register
);
1049 /* No adapter present */
1050 if (temp_register
== 0xFFFFFFFF)
1051 return(NO_ADAPTER_PRESENT
);
1053 if (temp_register
!= func
->config_space
[0])
1054 return(ADAPTER_NOT_SAME
);
1056 /* Check for same revision number and class code */
1057 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_CLASS_REVISION
, &temp_register
);
1059 /* Adapter not the same */
1060 if (temp_register
!= func
->config_space
[0x08 >> 2])
1061 return(ADAPTER_NOT_SAME
);
1063 /* Check for Bridge */
1064 pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
1066 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
1067 /* In order to continue checking, we must program the
1068 * bus registers in the bridge to respond to accesses
1069 * for its subordinate bus(es)
1072 temp_register
= func
->config_space
[0x18 >> 2];
1073 pci_bus_write_config_dword (pci_bus
, devfn
, PCI_PRIMARY_BUS
, temp_register
);
1075 secondary_bus
= (temp_register
>> 8) & 0xFF;
1077 next
= cpqhp_slot_list
[secondary_bus
];
1079 while (next
!= NULL
) {
1080 rc
= cpqhp_valid_replace(ctrl
, next
);
1088 /* Check to see if it is a standard config header */
1089 else if ((header_type
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
1090 /* Check subsystem vendor and ID */
1091 pci_bus_read_config_dword (pci_bus
, devfn
, PCI_SUBSYSTEM_VENDOR_ID
, &temp_register
);
1093 if (temp_register
!= func
->config_space
[0x2C >> 2]) {
1094 /* If it's a SMART-2 and the register isn't
1095 * filled in, ignore the difference because
1096 * they just have an old rev of the firmware
1098 if (!((func
->config_space
[0] == 0xAE100E11)
1099 && (temp_register
== 0x00L
)))
1100 return(ADAPTER_NOT_SAME
);
1102 /* Figure out IO and memory base lengths */
1103 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
1104 temp_register
= 0xFFFFFFFF;
1105 pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
1106 pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &base
);
1108 /* If this register is implemented */
1112 * set base = amount of IO
1115 base
= base
& 0xFFFFFFFE;
1121 base
= base
& 0xFFFFFFF0;
1131 /* Check information in slot structure */
1132 if (func
->base_length
[(cloop
- 0x10) >> 2] != base
)
1133 return(ADAPTER_NOT_SAME
);
1135 if (func
->base_type
[(cloop
- 0x10) >> 2] != type
)
1136 return(ADAPTER_NOT_SAME
);
1138 } /* End of base register loop */
1140 } /* End of (type 0 config space) else */
1142 /* this is not a type 0 or 1 config space header so
1143 * we don't know how to do it
1145 return(DEVICE_TYPE_NOT_SUPPORTED
);
1148 /* Get the next function */
1149 func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1158 * cpqhp_find_available_resources
1160 * Finds available memory, IO, and IRQ resources for programming
1161 * devices which may be added to the system
1162 * this function is for hot plug ADD!
1164 * returns 0 if success
1166 int cpqhp_find_available_resources(struct controller
*ctrl
, void __iomem
*rom_start
)
1171 void __iomem
*one_slot
;
1172 void __iomem
*rom_resource_table
;
1173 struct pci_func
*func
= NULL
;
1176 struct pci_resource
*mem_node
;
1177 struct pci_resource
*p_mem_node
;
1178 struct pci_resource
*io_node
;
1179 struct pci_resource
*bus_node
;
1181 rom_resource_table
= detect_HRT_floating_pointer(rom_start
, rom_start
+0xffff);
1182 dbg("rom_resource_table = %p\n", rom_resource_table
);
1184 if (rom_resource_table
== NULL
)
1187 /* Sum all resources and setup resource maps */
1188 unused_IRQ
= readl(rom_resource_table
+ UNUSED_IRQ
);
1189 dbg("unused_IRQ = %x\n", unused_IRQ
);
1192 while (unused_IRQ
) {
1193 if (unused_IRQ
& 1) {
1194 cpqhp_disk_irq
= temp
;
1197 unused_IRQ
= unused_IRQ
>> 1;
1201 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq
);
1202 unused_IRQ
= unused_IRQ
>> 1;
1205 while (unused_IRQ
) {
1206 if (unused_IRQ
& 1) {
1207 cpqhp_nic_irq
= temp
;
1210 unused_IRQ
= unused_IRQ
>> 1;
1214 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq
);
1215 unused_IRQ
= readl(rom_resource_table
+ PCIIRQ
);
1220 cpqhp_nic_irq
= ctrl
->cfgspc_irq
;
1222 if (!cpqhp_disk_irq
)
1223 cpqhp_disk_irq
= ctrl
->cfgspc_irq
;
1225 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq
, cpqhp_nic_irq
);
1227 rc
= compaq_nvram_load(rom_start
, ctrl
);
1231 one_slot
= rom_resource_table
+ sizeof (struct hrt
);
1233 i
= readb(rom_resource_table
+ NUMBER_OF_ENTRIES
);
1234 dbg("number_of_entries = %d\n", i
);
1236 if (!readb(one_slot
+ SECONDARY_BUS
))
1239 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1241 while (i
&& readb(one_slot
+ SECONDARY_BUS
)) {
1242 u8 dev_func
= readb(one_slot
+ DEV_FUNC
);
1243 u8 primary_bus
= readb(one_slot
+ PRIMARY_BUS
);
1244 u8 secondary_bus
= readb(one_slot
+ SECONDARY_BUS
);
1245 u8 max_bus
= readb(one_slot
+ MAX_BUS
);
1246 u16 io_base
= readw(one_slot
+ IO_BASE
);
1247 u16 io_length
= readw(one_slot
+ IO_LENGTH
);
1248 u16 mem_base
= readw(one_slot
+ MEM_BASE
);
1249 u16 mem_length
= readw(one_slot
+ MEM_LENGTH
);
1250 u16 pre_mem_base
= readw(one_slot
+ PRE_MEM_BASE
);
1251 u16 pre_mem_length
= readw(one_slot
+ PRE_MEM_LENGTH
);
1253 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1254 dev_func
, io_base
, io_length
, mem_base
, mem_length
, pre_mem_base
, pre_mem_length
,
1255 primary_bus
, secondary_bus
, max_bus
);
1257 /* If this entry isn't for our controller's bus, ignore it */
1258 if (primary_bus
!= ctrl
->bus
) {
1260 one_slot
+= sizeof (struct slot_rt
);
1263 /* find out if this entry is for an occupied slot */
1264 ctrl
->pci_bus
->number
= primary_bus
;
1265 pci_bus_read_config_dword (ctrl
->pci_bus
, dev_func
, PCI_VENDOR_ID
, &temp_dword
);
1266 dbg("temp_D_word = %x\n", temp_dword
);
1268 if (temp_dword
!= 0xFFFFFFFF) {
1270 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, 0);
1272 while (func
&& (func
->function
!= (dev_func
& 0x07))) {
1273 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func
, primary_bus
, dev_func
>> 3, index
);
1274 func
= cpqhp_slot_find(primary_bus
, dev_func
>> 3, index
++);
1277 /* If we can't find a match, skip this table entry */
1280 one_slot
+= sizeof (struct slot_rt
);
1283 /* this may not work and shouldn't be used */
1284 if (secondary_bus
!= primary_bus
)
1296 /* If we've got a valid IO base, use it */
1298 temp_dword
= io_base
+ io_length
;
1300 if ((io_base
) && (temp_dword
< 0x10000)) {
1301 io_node
= kmalloc(sizeof(*io_node
), GFP_KERNEL
);
1305 io_node
->base
= io_base
;
1306 io_node
->length
= io_length
;
1308 dbg("found io_node(base, length) = %x, %x\n",
1309 io_node
->base
, io_node
->length
);
1310 dbg("populated slot =%d \n", populated_slot
);
1311 if (!populated_slot
) {
1312 io_node
->next
= ctrl
->io_head
;
1313 ctrl
->io_head
= io_node
;
1315 io_node
->next
= func
->io_head
;
1316 func
->io_head
= io_node
;
1320 /* If we've got a valid memory base, use it */
1321 temp_dword
= mem_base
+ mem_length
;
1322 if ((mem_base
) && (temp_dword
< 0x10000)) {
1323 mem_node
= kmalloc(sizeof(*mem_node
), GFP_KERNEL
);
1327 mem_node
->base
= mem_base
<< 16;
1329 mem_node
->length
= mem_length
<< 16;
1331 dbg("found mem_node(base, length) = %x, %x\n",
1332 mem_node
->base
, mem_node
->length
);
1333 dbg("populated slot =%d \n", populated_slot
);
1334 if (!populated_slot
) {
1335 mem_node
->next
= ctrl
->mem_head
;
1336 ctrl
->mem_head
= mem_node
;
1338 mem_node
->next
= func
->mem_head
;
1339 func
->mem_head
= mem_node
;
1343 /* If we've got a valid prefetchable memory base, and
1344 * the base + length isn't greater than 0xFFFF
1346 temp_dword
= pre_mem_base
+ pre_mem_length
;
1347 if ((pre_mem_base
) && (temp_dword
< 0x10000)) {
1348 p_mem_node
= kmalloc(sizeof(*p_mem_node
), GFP_KERNEL
);
1352 p_mem_node
->base
= pre_mem_base
<< 16;
1354 p_mem_node
->length
= pre_mem_length
<< 16;
1355 dbg("found p_mem_node(base, length) = %x, %x\n",
1356 p_mem_node
->base
, p_mem_node
->length
);
1357 dbg("populated slot =%d \n", populated_slot
);
1359 if (!populated_slot
) {
1360 p_mem_node
->next
= ctrl
->p_mem_head
;
1361 ctrl
->p_mem_head
= p_mem_node
;
1363 p_mem_node
->next
= func
->p_mem_head
;
1364 func
->p_mem_head
= p_mem_node
;
1368 /* If we've got a valid bus number, use it
1369 * The second condition is to ignore bus numbers on
1370 * populated slots that don't have PCI-PCI bridges
1372 if (secondary_bus
&& (secondary_bus
!= primary_bus
)) {
1373 bus_node
= kmalloc(sizeof(*bus_node
), GFP_KERNEL
);
1377 bus_node
->base
= secondary_bus
;
1378 bus_node
->length
= max_bus
- secondary_bus
+ 1;
1379 dbg("found bus_node(base, length) = %x, %x\n",
1380 bus_node
->base
, bus_node
->length
);
1381 dbg("populated slot =%d \n", populated_slot
);
1382 if (!populated_slot
) {
1383 bus_node
->next
= ctrl
->bus_head
;
1384 ctrl
->bus_head
= bus_node
;
1386 bus_node
->next
= func
->bus_head
;
1387 func
->bus_head
= bus_node
;
1392 one_slot
+= sizeof (struct slot_rt
);
1395 /* If all of the following fail, we don't have any resources for
1399 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1400 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1401 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1402 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1409 * cpqhp_return_board_resources
1411 * this routine returns all resources allocated to a board to
1412 * the available pool.
1414 * returns 0 if success
1416 int cpqhp_return_board_resources(struct pci_func
* func
, struct resource_lists
* resources
)
1419 struct pci_resource
*node
;
1420 struct pci_resource
*t_node
;
1421 dbg("%s\n", __func__
);
1426 node
= func
->io_head
;
1427 func
->io_head
= NULL
;
1429 t_node
= node
->next
;
1430 return_resource(&(resources
->io_head
), node
);
1434 node
= func
->mem_head
;
1435 func
->mem_head
= NULL
;
1437 t_node
= node
->next
;
1438 return_resource(&(resources
->mem_head
), node
);
1442 node
= func
->p_mem_head
;
1443 func
->p_mem_head
= NULL
;
1445 t_node
= node
->next
;
1446 return_resource(&(resources
->p_mem_head
), node
);
1450 node
= func
->bus_head
;
1451 func
->bus_head
= NULL
;
1453 t_node
= node
->next
;
1454 return_resource(&(resources
->bus_head
), node
);
1458 rc
|= cpqhp_resource_sort_and_combine(&(resources
->mem_head
));
1459 rc
|= cpqhp_resource_sort_and_combine(&(resources
->p_mem_head
));
1460 rc
|= cpqhp_resource_sort_and_combine(&(resources
->io_head
));
1461 rc
|= cpqhp_resource_sort_and_combine(&(resources
->bus_head
));
1468 * cpqhp_destroy_resource_list
1470 * Puts node back in the resource list pointed to by head
1472 void cpqhp_destroy_resource_list (struct resource_lists
* resources
)
1474 struct pci_resource
*res
, *tres
;
1476 res
= resources
->io_head
;
1477 resources
->io_head
= NULL
;
1485 res
= resources
->mem_head
;
1486 resources
->mem_head
= NULL
;
1494 res
= resources
->p_mem_head
;
1495 resources
->p_mem_head
= NULL
;
1503 res
= resources
->bus_head
;
1504 resources
->bus_head
= NULL
;
1515 * cpqhp_destroy_board_resources
1517 * Puts node back in the resource list pointed to by head
1519 void cpqhp_destroy_board_resources (struct pci_func
* func
)
1521 struct pci_resource
*res
, *tres
;
1523 res
= func
->io_head
;
1524 func
->io_head
= NULL
;
1532 res
= func
->mem_head
;
1533 func
->mem_head
= NULL
;
1541 res
= func
->p_mem_head
;
1542 func
->p_mem_head
= NULL
;
1550 res
= func
->bus_head
;
1551 func
->bus_head
= NULL
;