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/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/wait.h>
37 #include <linux/smp_lock.h>
38 #include <linux/pci.h>
39 #include <linux/pci_hotplug.h>
42 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
*func
,
43 u8 behind_bridge
, struct resource_lists
*resources
);
44 static int configure_new_function(struct controller
* ctrl
, struct pci_func
*func
,
45 u8 behind_bridge
, struct resource_lists
*resources
);
46 static void interrupt_event_handler(struct controller
*ctrl
);
48 static struct semaphore event_semaphore
; /* mutex for process loop (up if something to process) */
49 static struct semaphore event_exit
; /* guard ensure thread has exited before calling it quits */
50 static int event_finished
;
51 static unsigned long pushbutton_pending
; /* = 0 */
53 /* things needed for the long_delay function */
54 static struct semaphore delay_sem
;
55 static wait_queue_head_t delay_wait
;
57 /* delay is in jiffies to wait for */
58 static void long_delay(int delay
)
60 DECLARE_WAITQUEUE(wait
, current
);
62 /* only allow 1 customer into the delay queue at once
63 * yes this makes some people wait even longer, but who really cares?
64 * this is for _huge_ delays to make the hardware happy as the
65 * signals bounce around
69 init_waitqueue_head(&delay_wait
);
71 add_wait_queue(&delay_wait
, &wait
);
72 msleep_interruptible(jiffies_to_msecs(delay
));
73 remove_wait_queue(&delay_wait
, &wait
);
79 /* FIXME: The following line needs to be somewhere else... */
80 #define WRONG_BUS_FREQUENCY 0x07
81 static u8
handle_switch_change(u8 change
, struct controller
* ctrl
)
86 struct pci_func
*func
;
87 struct event_info
*taskInfo
;
93 dbg("cpqsbd: Switch interrupt received.\n");
95 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
96 if (change
& (0x1L
<< hp_slot
)) {
97 /**********************************
99 **********************************/
100 func
= cpqhp_slot_find(ctrl
->bus
,
101 (hp_slot
+ ctrl
->slot_device_offset
), 0);
103 /* this is the structure that tells the worker thread
105 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
106 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
107 taskInfo
->hp_slot
= hp_slot
;
111 temp_word
= ctrl
->ctrl_int_comp
>> 16;
112 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
113 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
115 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
116 /**********************************
118 **********************************/
120 func
->switch_save
= 0;
122 taskInfo
->event_type
= INT_SWITCH_OPEN
;
124 /**********************************
126 **********************************/
128 func
->switch_save
= 0x10;
130 taskInfo
->event_type
= INT_SWITCH_CLOSE
;
139 * cpqhp_find_slot: find the struct slot of given device
140 * @ctrl: scan lots of this controller
141 * @device: the device id to find
143 static struct slot
*cpqhp_find_slot(struct controller
*ctrl
, u8 device
)
145 struct slot
*slot
= ctrl
->slot
;
147 while (slot
&& (slot
->device
!= device
)) {
155 static u8
handle_presence_change(u16 change
, struct controller
* ctrl
)
161 struct pci_func
*func
;
162 struct event_info
*taskInfo
;
168 /**********************************
170 **********************************/
171 dbg("cpqsbd: Presence/Notify input change.\n");
172 dbg(" Changed bits are 0x%4.4x\n", change
);
174 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
175 if (change
& (0x0101 << hp_slot
)) {
176 /**********************************
178 **********************************/
179 func
= cpqhp_slot_find(ctrl
->bus
,
180 (hp_slot
+ ctrl
->slot_device_offset
), 0);
182 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
183 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
184 taskInfo
->hp_slot
= hp_slot
;
188 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ (readb(ctrl
->hpc_reg
+ SLOT_MASK
) >> 4));
192 /* If the switch closed, must be a button
193 * If not in button mode, nevermind */
194 if (func
->switch_save
&& (ctrl
->push_button
== 1)) {
195 temp_word
= ctrl
->ctrl_int_comp
>> 16;
196 temp_byte
= (temp_word
>> hp_slot
) & 0x01;
197 temp_byte
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
199 if (temp_byte
!= func
->presence_save
) {
200 /**************************************
201 * button Pressed (doesn't do anything)
202 **************************************/
203 dbg("hp_slot %d button pressed\n", hp_slot
);
204 taskInfo
->event_type
= INT_BUTTON_PRESS
;
206 /**********************************
207 * button Released - TAKE ACTION!!!!
208 **********************************/
209 dbg("hp_slot %d button released\n", hp_slot
);
210 taskInfo
->event_type
= INT_BUTTON_RELEASE
;
212 /* Cancel if we are still blinking */
213 if ((p_slot
->state
== BLINKINGON_STATE
)
214 || (p_slot
->state
== BLINKINGOFF_STATE
)) {
215 taskInfo
->event_type
= INT_BUTTON_CANCEL
;
216 dbg("hp_slot %d button cancel\n", hp_slot
);
217 } else if ((p_slot
->state
== POWERON_STATE
)
218 || (p_slot
->state
== POWEROFF_STATE
)) {
219 /* info(msg_button_ignore, p_slot->number); */
220 taskInfo
->event_type
= INT_BUTTON_IGNORE
;
221 dbg("hp_slot %d button ignore\n", hp_slot
);
225 /* Switch is open, assume a presence change
226 * Save the presence state */
227 temp_word
= ctrl
->ctrl_int_comp
>> 16;
228 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
229 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
231 if ((!(ctrl
->ctrl_int_comp
& (0x010000 << hp_slot
))) ||
232 (!(ctrl
->ctrl_int_comp
& (0x01000000 << hp_slot
)))) {
234 taskInfo
->event_type
= INT_PRESENCE_ON
;
237 taskInfo
->event_type
= INT_PRESENCE_OFF
;
247 static u8
handle_power_fault(u8 change
, struct controller
* ctrl
)
251 struct pci_func
*func
;
252 struct event_info
*taskInfo
;
257 /**********************************
259 **********************************/
261 info("power fault interrupt\n");
263 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
264 if (change
& (0x01 << hp_slot
)) {
265 /**********************************
267 **********************************/
268 func
= cpqhp_slot_find(ctrl
->bus
,
269 (hp_slot
+ ctrl
->slot_device_offset
), 0);
271 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
272 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
273 taskInfo
->hp_slot
= hp_slot
;
277 if (ctrl
->ctrl_int_comp
& (0x00000100 << hp_slot
)) {
278 /**********************************
279 * power fault Cleared
280 **********************************/
283 taskInfo
->event_type
= INT_POWER_FAULT_CLEAR
;
285 /**********************************
287 **********************************/
288 taskInfo
->event_type
= INT_POWER_FAULT
;
291 amber_LED_on (ctrl
, hp_slot
);
292 green_LED_off (ctrl
, hp_slot
);
295 /* this is a fatal condition, we want
296 * to crash the machine to protect from
297 * data corruption. simulated_NMI
298 * shouldn't ever return */
300 simulated_NMI(hp_slot, ctrl); */
302 /* The following code causes a software
303 * crash just in case simulated_NMI did
306 panic(msg_power_fault); */
308 /* set power fault status for this board */
310 info("power fault bit %x set\n", hp_slot
);
321 * sort_by_size: sort nodes on the list by their length, smallest first.
322 * @head: list to sort
325 static int sort_by_size(struct pci_resource
**head
)
327 struct pci_resource
*current_res
;
328 struct pci_resource
*next_res
;
329 int out_of_order
= 1;
334 if (!((*head
)->next
))
337 while (out_of_order
) {
340 /* Special case for swapping list head */
341 if (((*head
)->next
) &&
342 ((*head
)->length
> (*head
)->next
->length
)) {
345 *head
= (*head
)->next
;
346 current_res
->next
= (*head
)->next
;
347 (*head
)->next
= current_res
;
352 while (current_res
->next
&& current_res
->next
->next
) {
353 if (current_res
->next
->length
> current_res
->next
->next
->length
) {
355 next_res
= current_res
->next
;
356 current_res
->next
= current_res
->next
->next
;
357 current_res
= current_res
->next
;
358 next_res
->next
= current_res
->next
;
359 current_res
->next
= next_res
;
361 current_res
= current_res
->next
;
363 } /* End of out_of_order loop */
370 * sort_by_max_size: sort nodes on the list by their length, largest first.
371 * @head: list to sort
374 static int sort_by_max_size(struct pci_resource
**head
)
376 struct pci_resource
*current_res
;
377 struct pci_resource
*next_res
;
378 int out_of_order
= 1;
383 if (!((*head
)->next
))
386 while (out_of_order
) {
389 /* Special case for swapping list head */
390 if (((*head
)->next
) &&
391 ((*head
)->length
< (*head
)->next
->length
)) {
394 *head
= (*head
)->next
;
395 current_res
->next
= (*head
)->next
;
396 (*head
)->next
= current_res
;
401 while (current_res
->next
&& current_res
->next
->next
) {
402 if (current_res
->next
->length
< current_res
->next
->next
->length
) {
404 next_res
= current_res
->next
;
405 current_res
->next
= current_res
->next
->next
;
406 current_res
= current_res
->next
;
407 next_res
->next
= current_res
->next
;
408 current_res
->next
= next_res
;
410 current_res
= current_res
->next
;
412 } /* End of out_of_order loop */
419 * do_pre_bridge_resource_split: find node of resources that are unused
422 static struct pci_resource
*do_pre_bridge_resource_split(struct pci_resource
**head
,
423 struct pci_resource
**orig_head
, u32 alignment
)
425 struct pci_resource
*prevnode
= NULL
;
426 struct pci_resource
*node
;
427 struct pci_resource
*split_node
;
430 dbg("do_pre_bridge_resource_split\n");
432 if (!(*head
) || !(*orig_head
))
435 rc
= cpqhp_resource_sort_and_combine(head
);
440 if ((*head
)->base
!= (*orig_head
)->base
)
443 if ((*head
)->length
== (*orig_head
)->length
)
447 /* If we got here, there the bridge requires some of the resource, but
448 * we may be able to split some off of the front */
452 if (node
->length
& (alignment
-1)) {
453 /* this one isn't an aligned length, so we'll make a new entry
454 * and split it up. */
455 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
460 temp_dword
= (node
->length
| (alignment
-1)) + 1 - alignment
;
462 split_node
->base
= node
->base
;
463 split_node
->length
= temp_dword
;
465 node
->length
-= temp_dword
;
466 node
->base
+= split_node
->length
;
468 /* Put it in the list */
470 split_node
->next
= node
;
473 if (node
->length
< alignment
)
481 while (prevnode
->next
!= node
)
482 prevnode
= prevnode
->next
;
484 prevnode
->next
= node
->next
;
493 * do_bridge_resource_split: find one node of resources that aren't in use
496 static struct pci_resource
*do_bridge_resource_split(struct pci_resource
**head
, u32 alignment
)
498 struct pci_resource
*prevnode
= NULL
;
499 struct pci_resource
*node
;
503 rc
= cpqhp_resource_sort_and_combine(head
);
516 if (node
->length
< alignment
)
519 if (node
->base
& (alignment
- 1)) {
520 /* Short circuit if adjusted size is too small */
521 temp_dword
= (node
->base
| (alignment
-1)) + 1;
522 if ((node
->length
- (temp_dword
- node
->base
)) < alignment
)
525 node
->length
-= (temp_dword
- node
->base
);
526 node
->base
= temp_dword
;
529 if (node
->length
& (alignment
- 1))
530 /* There's stuff in use after this node */
541 * get_io_resource: find first node of given size not in ISA aliasing window.
542 * @head: list to search
543 * @size: size of node to find, must be a power of two.
545 * Description: this function sorts the resource list by size and then returns
546 * returns the first node of "size" length that is not in the ISA aliasing
547 * window. If it finds a node larger than "size" it will split it up.
550 static struct pci_resource
*get_io_resource(struct pci_resource
**head
, u32 size
)
552 struct pci_resource
*prevnode
;
553 struct pci_resource
*node
;
554 struct pci_resource
*split_node
;
560 if ( cpqhp_resource_sort_and_combine(head
) )
563 if ( sort_by_size(head
) )
566 for (node
= *head
; node
; node
= node
->next
) {
567 if (node
->length
< size
)
570 if (node
->base
& (size
- 1)) {
571 /* this one isn't base aligned properly
572 * so we'll make a new entry and split it up */
573 temp_dword
= (node
->base
| (size
-1)) + 1;
575 /* Short circuit if adjusted size is too small */
576 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
579 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
584 split_node
->base
= node
->base
;
585 split_node
->length
= temp_dword
- node
->base
;
586 node
->base
= temp_dword
;
587 node
->length
-= split_node
->length
;
589 /* Put it in the list */
590 split_node
->next
= node
->next
;
591 node
->next
= split_node
;
592 } /* End of non-aligned base */
594 /* Don't need to check if too small since we already did */
595 if (node
->length
> size
) {
596 /* this one is longer than we need
597 * so we'll make a new entry and split it up */
598 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
603 split_node
->base
= node
->base
+ size
;
604 split_node
->length
= node
->length
- size
;
607 /* Put it in the list */
608 split_node
->next
= node
->next
;
609 node
->next
= split_node
;
610 } /* End of too big on top end */
612 /* For IO make sure it's not in the ISA aliasing space */
613 if (node
->base
& 0x300L
)
616 /* If we got here, then it is the right size
617 * Now take it out of the list and break */
622 while (prevnode
->next
!= node
)
623 prevnode
= prevnode
->next
;
625 prevnode
->next
= node
->next
;
636 * get_max_resource: get largest node which has at least the given size.
637 * @head: the list to search the node in
638 * @size: the minimum size of the node to find
640 * Description: Gets the largest node that is at least "size" big from the
641 * list pointed to by head. It aligns the node on top and bottom
642 * to "size" alignment before returning it.
644 static struct pci_resource
*get_max_resource(struct pci_resource
**head
, u32 size
)
646 struct pci_resource
*max
;
647 struct pci_resource
*temp
;
648 struct pci_resource
*split_node
;
651 if (cpqhp_resource_sort_and_combine(head
))
654 if (sort_by_max_size(head
))
657 for (max
= *head
; max
; max
= max
->next
) {
658 /* If not big enough we could probably just bail,
659 * instead we'll continue to the next. */
660 if (max
->length
< size
)
663 if (max
->base
& (size
- 1)) {
664 /* this one isn't base aligned properly
665 * so we'll make a new entry and split it up */
666 temp_dword
= (max
->base
| (size
-1)) + 1;
668 /* Short circuit if adjusted size is too small */
669 if ((max
->length
- (temp_dword
- max
->base
)) < size
)
672 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
677 split_node
->base
= max
->base
;
678 split_node
->length
= temp_dword
- max
->base
;
679 max
->base
= temp_dword
;
680 max
->length
-= split_node
->length
;
682 split_node
->next
= max
->next
;
683 max
->next
= split_node
;
686 if ((max
->base
+ max
->length
) & (size
- 1)) {
687 /* this one isn't end aligned properly at the top
688 * so we'll make a new entry and split it up */
689 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
693 temp_dword
= ((max
->base
+ max
->length
) & ~(size
- 1));
694 split_node
->base
= temp_dword
;
695 split_node
->length
= max
->length
+ max
->base
697 max
->length
-= split_node
->length
;
699 split_node
->next
= max
->next
;
700 max
->next
= split_node
;
703 /* Make sure it didn't shrink too much when we aligned it */
704 if (max
->length
< size
)
707 /* Now take it out of the list */
712 while (temp
&& temp
->next
!= max
) {
716 temp
->next
= max
->next
;
728 * get_resource: find resource of given size and split up larger ones.
729 * @head: the list to search for resources
730 * @size: the size limit to use
732 * Description: This function sorts the resource list by size and then
733 * returns the first node of "size" length. If it finds a node
734 * larger than "size" it will split it up.
736 * size must be a power of two.
738 static struct pci_resource
*get_resource(struct pci_resource
**head
, u32 size
)
740 struct pci_resource
*prevnode
;
741 struct pci_resource
*node
;
742 struct pci_resource
*split_node
;
745 if (cpqhp_resource_sort_and_combine(head
))
748 if (sort_by_size(head
))
751 for (node
= *head
; node
; node
= node
->next
) {
752 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
753 __FUNCTION__
, size
, node
, node
->base
, node
->length
);
754 if (node
->length
< size
)
757 if (node
->base
& (size
- 1)) {
758 dbg("%s: not aligned\n", __FUNCTION__
);
759 /* this one isn't base aligned properly
760 * so we'll make a new entry and split it up */
761 temp_dword
= (node
->base
| (size
-1)) + 1;
763 /* Short circuit if adjusted size is too small */
764 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
767 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
772 split_node
->base
= node
->base
;
773 split_node
->length
= temp_dword
- node
->base
;
774 node
->base
= temp_dword
;
775 node
->length
-= split_node
->length
;
777 split_node
->next
= node
->next
;
778 node
->next
= split_node
;
779 } /* End of non-aligned base */
781 /* Don't need to check if too small since we already did */
782 if (node
->length
> size
) {
783 dbg("%s: too big\n", __FUNCTION__
);
784 /* this one is longer than we need
785 * so we'll make a new entry and split it up */
786 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
791 split_node
->base
= node
->base
+ size
;
792 split_node
->length
= node
->length
- size
;
795 /* Put it in the list */
796 split_node
->next
= node
->next
;
797 node
->next
= split_node
;
798 } /* End of too big on top end */
800 dbg("%s: got one!!!\n", __FUNCTION__
);
801 /* If we got here, then it is the right size
802 * Now take it out of the list */
807 while (prevnode
->next
!= node
)
808 prevnode
= prevnode
->next
;
810 prevnode
->next
= node
->next
;
820 * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
821 * @head: the list to sort and clean up
823 * Description: Sorts all of the nodes in the list in ascending order by
824 * their base addresses. Also does garbage collection by
825 * combining adjacent nodes.
827 * returns 0 if success
829 int cpqhp_resource_sort_and_combine(struct pci_resource
**head
)
831 struct pci_resource
*node1
;
832 struct pci_resource
*node2
;
833 int out_of_order
= 1;
835 dbg("%s: head = %p, *head = %p\n", __FUNCTION__
, head
, *head
);
840 dbg("*head->next = %p\n",(*head
)->next
);
843 return 0; /* only one item on the list, already sorted! */
845 dbg("*head->base = 0x%x\n",(*head
)->base
);
846 dbg("*head->next->base = 0x%x\n",(*head
)->next
->base
);
847 while (out_of_order
) {
850 /* Special case for swapping list head */
851 if (((*head
)->next
) &&
852 ((*head
)->base
> (*head
)->next
->base
)) {
854 (*head
) = (*head
)->next
;
855 node1
->next
= (*head
)->next
;
856 (*head
)->next
= node1
;
862 while (node1
->next
&& node1
->next
->next
) {
863 if (node1
->next
->base
> node1
->next
->next
->base
) {
866 node1
->next
= node1
->next
->next
;
868 node2
->next
= node1
->next
;
873 } /* End of out_of_order loop */
877 while (node1
&& node1
->next
) {
878 if ((node1
->base
+ node1
->length
) == node1
->next
->base
) {
881 node1
->length
+= node1
->next
->length
;
883 node1
->next
= node1
->next
->next
;
893 irqreturn_t
cpqhp_ctrl_intr(int IRQ
, void *data
)
895 struct controller
*ctrl
= data
;
896 u8 schedule_flag
= 0;
903 misc
= readw(ctrl
->hpc_reg
+ MISC
);
904 /***************************************
905 * Check to see if it was our interrupt
906 ***************************************/
907 if (!(misc
& 0x000C)) {
912 /**********************************
913 * Serial Output interrupt Pending
914 **********************************/
916 /* Clear the interrupt */
918 writew(misc
, ctrl
->hpc_reg
+ MISC
);
920 /* Read to clear posted writes */
921 misc
= readw(ctrl
->hpc_reg
+ MISC
);
923 dbg ("%s - waking up\n", __FUNCTION__
);
924 wake_up_interruptible(&ctrl
->queue
);
928 /* General-interrupt-input interrupt Pending */
929 Diff
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) ^ ctrl
->ctrl_int_comp
;
931 ctrl
->ctrl_int_comp
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
933 /* Clear the interrupt */
934 writel(Diff
, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
936 /* Read it back to clear any posted writes */
937 temp_dword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
940 /* Clear all interrupts */
941 writel(0xFFFFFFFF, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
943 schedule_flag
+= handle_switch_change((u8
)(Diff
& 0xFFL
), ctrl
);
944 schedule_flag
+= handle_presence_change((u16
)((Diff
& 0xFFFF0000L
) >> 16), ctrl
);
945 schedule_flag
+= handle_power_fault((u8
)((Diff
& 0xFF00L
) >> 8), ctrl
);
948 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
950 /* Bus reset has completed */
952 writeb(reset
, ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
953 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
954 wake_up_interruptible(&ctrl
->queue
);
958 up(&event_semaphore
);
959 dbg("Signal event_semaphore\n");
966 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
967 * @busnumber - bus where new node is to be located
969 * Returns pointer to the new node or NULL if unsuccessful
971 struct pci_func
*cpqhp_slot_create(u8 busnumber
)
973 struct pci_func
*new_slot
;
974 struct pci_func
*next
;
976 new_slot
= kzalloc(sizeof(*new_slot
), GFP_KERNEL
);
977 if (new_slot
== NULL
) {
983 new_slot
->next
= NULL
;
984 new_slot
->configured
= 1;
986 if (cpqhp_slot_list
[busnumber
] == NULL
) {
987 cpqhp_slot_list
[busnumber
] = new_slot
;
989 next
= cpqhp_slot_list
[busnumber
];
990 while (next
->next
!= NULL
)
992 next
->next
= new_slot
;
999 * slot_remove - Removes a node from the linked list of slots.
1000 * @old_slot: slot to remove
1002 * Returns 0 if successful, !0 otherwise.
1004 static int slot_remove(struct pci_func
* old_slot
)
1006 struct pci_func
*next
;
1008 if (old_slot
== NULL
)
1011 next
= cpqhp_slot_list
[old_slot
->bus
];
1017 if (next
== old_slot
) {
1018 cpqhp_slot_list
[old_slot
->bus
] = old_slot
->next
;
1019 cpqhp_destroy_board_resources(old_slot
);
1024 while ((next
->next
!= old_slot
) && (next
->next
!= NULL
)) {
1028 if (next
->next
== old_slot
) {
1029 next
->next
= old_slot
->next
;
1030 cpqhp_destroy_board_resources(old_slot
);
1039 * bridge_slot_remove - Removes a node from the linked list of slots.
1040 * @bridge: bridge to remove
1042 * Returns 0 if successful, !0 otherwise.
1044 static int bridge_slot_remove(struct pci_func
*bridge
)
1046 u8 subordinateBus
, secondaryBus
;
1048 struct pci_func
*next
;
1050 secondaryBus
= (bridge
->config_space
[0x06] >> 8) & 0xFF;
1051 subordinateBus
= (bridge
->config_space
[0x06] >> 16) & 0xFF;
1053 for (tempBus
= secondaryBus
; tempBus
<= subordinateBus
; tempBus
++) {
1054 next
= cpqhp_slot_list
[tempBus
];
1056 while (!slot_remove(next
)) {
1057 next
= cpqhp_slot_list
[tempBus
];
1061 next
= cpqhp_slot_list
[bridge
->bus
];
1066 if (next
== bridge
) {
1067 cpqhp_slot_list
[bridge
->bus
] = bridge
->next
;
1071 while ((next
->next
!= bridge
) && (next
->next
!= NULL
))
1074 if (next
->next
!= bridge
)
1076 next
->next
= bridge
->next
;
1084 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1086 * @device: device to find
1087 * @index: is 0 for first function found, 1 for the second...
1089 * Returns pointer to the node if successful, %NULL otherwise.
1091 struct pci_func
*cpqhp_slot_find(u8 bus
, u8 device
, u8 index
)
1094 struct pci_func
*func
;
1096 func
= cpqhp_slot_list
[bus
];
1098 if ((func
== NULL
) || ((func
->device
== device
) && (index
== 0)))
1101 if (func
->device
== device
)
1104 while (func
->next
!= NULL
) {
1107 if (func
->device
== device
)
1118 /* DJZ: I don't think is_bridge will work as is.
1120 static int is_bridge(struct pci_func
* func
)
1122 /* Check the header type */
1123 if (((func
->config_space
[0x03] >> 16) & 0xFF) == 0x01)
1131 * set_controller_speed - set the frequency and/or mode of a specific
1132 * controller segment.
1134 * @ctrl: controller to change frequency/mode for.
1135 * @adapter_speed: the speed of the adapter we want to match.
1136 * @hp_slot: the slot number where the adapter is installed.
1138 * Returns 0 if we successfully change frequency and/or mode to match the
1142 static u8
set_controller_speed(struct controller
*ctrl
, u8 adapter_speed
, u8 hp_slot
)
1146 u8 slot_power
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1148 u32 leds
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
1150 if (ctrl
->speed
== adapter_speed
)
1153 /* We don't allow freq/mode changes if we find another adapter running
1154 * in another slot on this controller */
1155 for(slot
= ctrl
->slot
; slot
; slot
= slot
->next
) {
1156 if (slot
->device
== (hp_slot
+ ctrl
->slot_device_offset
))
1158 if (!slot
->hotplug_slot
&& !slot
->hotplug_slot
->info
)
1160 if (slot
->hotplug_slot
->info
->adapter_status
== 0)
1162 /* If another adapter is running on the same segment but at a
1163 * lower speed/mode, we allow the new adapter to function at
1164 * this rate if supported */
1165 if (ctrl
->speed
< adapter_speed
)
1171 /* If the controller doesn't support freq/mode changes and the
1172 * controller is running at a higher mode, we bail */
1173 if ((ctrl
->speed
> adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1176 /* But we allow the adapter to run at a lower rate if possible */
1177 if ((ctrl
->speed
< adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1180 /* We try to set the max speed supported by both the adapter and
1182 if (ctrl
->speed_capability
< adapter_speed
) {
1183 if (ctrl
->speed
== ctrl
->speed_capability
)
1185 adapter_speed
= ctrl
->speed_capability
;
1188 writel(0x0L
, ctrl
->hpc_reg
+ LED_CONTROL
);
1189 writeb(0x00, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1192 wait_for_ctrl_irq(ctrl
);
1194 if (adapter_speed
!= PCI_SPEED_133MHz_PCIX
)
1198 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1200 reg16
= readw(ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1202 switch(adapter_speed
) {
1203 case(PCI_SPEED_133MHz_PCIX
):
1207 case(PCI_SPEED_100MHz_PCIX
):
1211 case(PCI_SPEED_66MHz_PCIX
):
1215 case(PCI_SPEED_66MHz
):
1219 default: /* 33MHz PCI 2.2 */
1225 writew(reg16
, ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1229 /* Reenable interrupts */
1230 writel(0, ctrl
->hpc_reg
+ INT_MASK
);
1232 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1234 /* Restart state machine */
1236 pci_read_config_byte(ctrl
->pci_dev
, 0x43, ®
);
1237 pci_write_config_byte(ctrl
->pci_dev
, 0x43, reg
);
1239 /* Only if mode change...*/
1240 if (((ctrl
->speed
== PCI_SPEED_66MHz
) && (adapter_speed
== PCI_SPEED_66MHz_PCIX
)) ||
1241 ((ctrl
->speed
== PCI_SPEED_66MHz_PCIX
) && (adapter_speed
== PCI_SPEED_66MHz
)))
1244 wait_for_ctrl_irq(ctrl
);
1247 /* Restore LED/Slot state */
1248 writel(leds
, ctrl
->hpc_reg
+ LED_CONTROL
);
1249 writeb(slot_power
, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1252 wait_for_ctrl_irq(ctrl
);
1254 ctrl
->speed
= adapter_speed
;
1255 slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1257 info("Successfully changed frequency/mode for adapter in slot %d\n",
1262 /* the following routines constitute the bulk of the
1263 hotplug controller logic
1268 * board_replaced - Called after a board has been replaced in the system.
1270 * This is only used if we don't have resources for hot add
1271 * Turns power on for the board
1272 * Checks to see if board is the same
1273 * If board is same, reconfigures it
1274 * If board isn't same, turns it back off.
1277 static u32
board_replaced(struct pci_func
*func
, struct controller
*ctrl
)
1284 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1286 if (readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) & (0x01L
<< hp_slot
)) {
1287 /**********************************
1288 * The switch is open.
1289 **********************************/
1290 rc
= INTERLOCK_OPEN
;
1291 } else if (is_slot_enabled (ctrl
, hp_slot
)) {
1292 /**********************************
1293 * The board is already on
1294 **********************************/
1295 rc
= CARD_FUNCTIONING
;
1297 mutex_lock(&ctrl
->crit_sect
);
1299 /* turn on board without attaching to the bus */
1300 enable_slot_power (ctrl
, hp_slot
);
1304 /* Wait for SOBS to be unset */
1305 wait_for_ctrl_irq (ctrl
);
1307 /* Change bits in slot power register to force another shift out
1308 * NOTE: this is to work around the timer bug */
1309 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1310 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1311 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1315 /* Wait for SOBS to be unset */
1316 wait_for_ctrl_irq (ctrl
);
1318 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1319 if (ctrl
->speed
!= adapter_speed
)
1320 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1321 rc
= WRONG_BUS_FREQUENCY
;
1323 /* turn off board without attaching to the bus */
1324 disable_slot_power (ctrl
, hp_slot
);
1328 /* Wait for SOBS to be unset */
1329 wait_for_ctrl_irq (ctrl
);
1331 mutex_unlock(&ctrl
->crit_sect
);
1336 mutex_lock(&ctrl
->crit_sect
);
1338 slot_enable (ctrl
, hp_slot
);
1339 green_LED_blink (ctrl
, hp_slot
);
1341 amber_LED_off (ctrl
, hp_slot
);
1345 /* Wait for SOBS to be unset */
1346 wait_for_ctrl_irq (ctrl
);
1348 mutex_unlock(&ctrl
->crit_sect
);
1350 /* Wait for ~1 second because of hot plug spec */
1353 /* Check for a power fault */
1354 if (func
->status
== 0xFF) {
1355 /* power fault occurred, but it was benign */
1359 rc
= cpqhp_valid_replace(ctrl
, func
);
1362 /* It must be the same board */
1364 rc
= cpqhp_configure_board(ctrl
, func
);
1366 /* If configuration fails, turn it off
1367 * Get slot won't work for devices behind
1368 * bridges, but in this case it will always be
1369 * called for the "base" bus/dev/func of an
1372 mutex_lock(&ctrl
->crit_sect
);
1374 amber_LED_on (ctrl
, hp_slot
);
1375 green_LED_off (ctrl
, hp_slot
);
1376 slot_disable (ctrl
, hp_slot
);
1380 /* Wait for SOBS to be unset */
1381 wait_for_ctrl_irq (ctrl
);
1383 mutex_unlock(&ctrl
->crit_sect
);
1391 /* Something is wrong
1393 * Get slot won't work for devices behind bridges, but
1394 * in this case it will always be called for the "base"
1395 * bus/dev/func of an adapter. */
1397 mutex_lock(&ctrl
->crit_sect
);
1399 amber_LED_on (ctrl
, hp_slot
);
1400 green_LED_off (ctrl
, hp_slot
);
1401 slot_disable (ctrl
, hp_slot
);
1405 /* Wait for SOBS to be unset */
1406 wait_for_ctrl_irq (ctrl
);
1408 mutex_unlock(&ctrl
->crit_sect
);
1418 * board_added - Called after a board has been added to the system.
1420 * Turns power on for the board
1424 static u32
board_added(struct pci_func
*func
, struct controller
*ctrl
)
1430 u32 temp_register
= 0xFFFFFFFF;
1432 struct pci_func
*new_slot
= NULL
;
1433 struct slot
*p_slot
;
1434 struct resource_lists res_lists
;
1436 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1437 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1438 __FUNCTION__
, func
->device
, ctrl
->slot_device_offset
, hp_slot
);
1440 mutex_lock(&ctrl
->crit_sect
);
1442 /* turn on board without attaching to the bus */
1443 enable_slot_power(ctrl
, hp_slot
);
1447 /* Wait for SOBS to be unset */
1448 wait_for_ctrl_irq (ctrl
);
1450 /* Change bits in slot power register to force another shift out
1451 * NOTE: this is to work around the timer bug */
1452 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1453 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1454 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1458 /* Wait for SOBS to be unset */
1459 wait_for_ctrl_irq (ctrl
);
1461 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1462 if (ctrl
->speed
!= adapter_speed
)
1463 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1464 rc
= WRONG_BUS_FREQUENCY
;
1466 /* turn off board without attaching to the bus */
1467 disable_slot_power (ctrl
, hp_slot
);
1471 /* Wait for SOBS to be unset */
1472 wait_for_ctrl_irq(ctrl
);
1474 mutex_unlock(&ctrl
->crit_sect
);
1479 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1481 /* turn on board and blink green LED */
1483 dbg("%s: before down\n", __FUNCTION__
);
1484 mutex_lock(&ctrl
->crit_sect
);
1485 dbg("%s: after down\n", __FUNCTION__
);
1487 dbg("%s: before slot_enable\n", __FUNCTION__
);
1488 slot_enable (ctrl
, hp_slot
);
1490 dbg("%s: before green_LED_blink\n", __FUNCTION__
);
1491 green_LED_blink (ctrl
, hp_slot
);
1493 dbg("%s: before amber_LED_blink\n", __FUNCTION__
);
1494 amber_LED_off (ctrl
, hp_slot
);
1496 dbg("%s: before set_SOGO\n", __FUNCTION__
);
1499 /* Wait for SOBS to be unset */
1500 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__
);
1501 wait_for_ctrl_irq (ctrl
);
1502 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__
);
1504 dbg("%s: before up\n", __FUNCTION__
);
1505 mutex_unlock(&ctrl
->crit_sect
);
1506 dbg("%s: after up\n", __FUNCTION__
);
1508 /* Wait for ~1 second because of hot plug spec */
1509 dbg("%s: before long_delay\n", __FUNCTION__
);
1511 dbg("%s: after long_delay\n", __FUNCTION__
);
1513 dbg("%s: func status = %x\n", __FUNCTION__
, func
->status
);
1514 /* Check for a power fault */
1515 if (func
->status
== 0xFF) {
1516 /* power fault occurred, but it was benign */
1517 temp_register
= 0xFFFFFFFF;
1518 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__
, temp_register
);
1522 /* Get vendor/device ID u32 */
1523 ctrl
->pci_bus
->number
= func
->bus
;
1524 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), PCI_VENDOR_ID
, &temp_register
);
1525 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__
, rc
);
1526 dbg("%s: temp_register is %x\n", __FUNCTION__
, temp_register
);
1529 /* Something's wrong here */
1530 temp_register
= 0xFFFFFFFF;
1531 dbg("%s: temp register set to %x by error\n", __FUNCTION__
, temp_register
);
1533 /* Preset return code. It will be changed later if things go okay. */
1534 rc
= NO_ADAPTER_PRESENT
;
1537 /* All F's is an empty slot or an invalid board */
1538 if (temp_register
!= 0xFFFFFFFF) { /* Check for a board in the slot */
1539 res_lists
.io_head
= ctrl
->io_head
;
1540 res_lists
.mem_head
= ctrl
->mem_head
;
1541 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1542 res_lists
.bus_head
= ctrl
->bus_head
;
1543 res_lists
.irqs
= NULL
;
1545 rc
= configure_new_device(ctrl
, func
, 0, &res_lists
);
1547 dbg("%s: back from configure_new_device\n", __FUNCTION__
);
1548 ctrl
->io_head
= res_lists
.io_head
;
1549 ctrl
->mem_head
= res_lists
.mem_head
;
1550 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1551 ctrl
->bus_head
= res_lists
.bus_head
;
1553 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1554 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1555 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1556 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1559 mutex_lock(&ctrl
->crit_sect
);
1561 amber_LED_on (ctrl
, hp_slot
);
1562 green_LED_off (ctrl
, hp_slot
);
1563 slot_disable (ctrl
, hp_slot
);
1567 /* Wait for SOBS to be unset */
1568 wait_for_ctrl_irq (ctrl
);
1570 mutex_unlock(&ctrl
->crit_sect
);
1573 cpqhp_save_slot_config(ctrl
, func
);
1578 func
->switch_save
= 0x10;
1579 func
->is_a_board
= 0x01;
1581 /* next, we will instantiate the linux pci_dev structures (with
1582 * appropriate driver notification, if already present) */
1583 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__
);
1586 new_slot
= cpqhp_slot_find(ctrl
->bus
, func
->device
, index
++);
1587 if (new_slot
&& !new_slot
->pci_dev
) {
1588 cpqhp_configure_device(ctrl
, new_slot
);
1592 mutex_lock(&ctrl
->crit_sect
);
1594 green_LED_on (ctrl
, hp_slot
);
1598 /* Wait for SOBS to be unset */
1599 wait_for_ctrl_irq (ctrl
);
1601 mutex_unlock(&ctrl
->crit_sect
);
1603 mutex_lock(&ctrl
->crit_sect
);
1605 amber_LED_on (ctrl
, hp_slot
);
1606 green_LED_off (ctrl
, hp_slot
);
1607 slot_disable (ctrl
, hp_slot
);
1611 /* Wait for SOBS to be unset */
1612 wait_for_ctrl_irq (ctrl
);
1614 mutex_unlock(&ctrl
->crit_sect
);
1623 * remove_board - Turns off slot and LED's
1626 static u32
remove_board(struct pci_func
* func
, u32 replace_flag
, struct controller
* ctrl
)
1634 struct resource_lists res_lists
;
1635 struct pci_func
*temp_func
;
1637 if (cpqhp_unconfigure_device(func
))
1640 device
= func
->device
;
1642 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1643 dbg("In %s, hp_slot = %d\n", __FUNCTION__
, hp_slot
);
1645 /* When we get here, it is safe to change base address registers.
1646 * We will attempt to save the base address register lengths */
1647 if (replace_flag
|| !ctrl
->add_support
)
1648 rc
= cpqhp_save_base_addr_length(ctrl
, func
);
1649 else if (!func
->bus_head
&& !func
->mem_head
&&
1650 !func
->p_mem_head
&& !func
->io_head
) {
1651 /* Here we check to see if we've saved any of the board's
1652 * resources already. If so, we'll skip the attempt to
1653 * determine what's being used. */
1655 temp_func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1657 if (temp_func
->bus_head
|| temp_func
->mem_head
1658 || temp_func
->p_mem_head
|| temp_func
->io_head
) {
1662 temp_func
= cpqhp_slot_find(temp_func
->bus
, temp_func
->device
, index
++);
1666 rc
= cpqhp_save_used_resources(ctrl
, func
);
1668 /* Change status to shutdown */
1669 if (func
->is_a_board
)
1670 func
->status
= 0x01;
1671 func
->configured
= 0;
1673 mutex_lock(&ctrl
->crit_sect
);
1675 green_LED_off (ctrl
, hp_slot
);
1676 slot_disable (ctrl
, hp_slot
);
1680 /* turn off SERR for slot */
1681 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_SERR
);
1682 temp_byte
&= ~(0x01 << hp_slot
);
1683 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_SERR
);
1685 /* Wait for SOBS to be unset */
1686 wait_for_ctrl_irq (ctrl
);
1688 mutex_unlock(&ctrl
->crit_sect
);
1690 if (!replace_flag
&& ctrl
->add_support
) {
1692 res_lists
.io_head
= ctrl
->io_head
;
1693 res_lists
.mem_head
= ctrl
->mem_head
;
1694 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1695 res_lists
.bus_head
= ctrl
->bus_head
;
1697 cpqhp_return_board_resources(func
, &res_lists
);
1699 ctrl
->io_head
= res_lists
.io_head
;
1700 ctrl
->mem_head
= res_lists
.mem_head
;
1701 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1702 ctrl
->bus_head
= res_lists
.bus_head
;
1704 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1705 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1706 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1707 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1709 if (is_bridge(func
)) {
1710 bridge_slot_remove(func
);
1714 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
1717 /* Setup slot structure with entry for empty slot */
1718 func
= cpqhp_slot_create(ctrl
->bus
);
1723 func
->bus
= ctrl
->bus
;
1724 func
->device
= device
;
1726 func
->configured
= 0;
1727 func
->switch_save
= 0x10;
1728 func
->is_a_board
= 0;
1729 func
->p_task_event
= NULL
;
1735 static void pushbutton_helper_thread(unsigned long data
)
1737 pushbutton_pending
= data
;
1738 up(&event_semaphore
);
1742 /* this is the main worker thread */
1743 static int event_thread(void* data
)
1745 struct controller
*ctrl
;
1747 daemonize("phpd_event");
1750 dbg("!!!!event_thread sleeping\n");
1751 down_interruptible (&event_semaphore
);
1752 dbg("event_thread woken finished = %d\n", event_finished
);
1753 if (event_finished
) break;
1755 if (pushbutton_pending
)
1756 cpqhp_pushbutton_thread(pushbutton_pending
);
1758 for (ctrl
= cpqhp_ctrl_list
; ctrl
; ctrl
=ctrl
->next
)
1759 interrupt_event_handler(ctrl
);
1761 dbg("event_thread signals exit\n");
1767 int cpqhp_event_start_thread(void)
1771 /* initialize our semaphores */
1772 init_MUTEX(&delay_sem
);
1773 init_MUTEX_LOCKED(&event_semaphore
);
1774 init_MUTEX_LOCKED(&event_exit
);
1777 pid
= kernel_thread(event_thread
, NULL
, 0);
1779 err ("Can't start up our event thread\n");
1782 dbg("Our event thread pid = %d\n", pid
);
1787 void cpqhp_event_stop_thread(void)
1790 dbg("event_thread finish command given\n");
1791 up(&event_semaphore
);
1792 dbg("wait for event_thread to exit\n");
1797 static int update_slot_info(struct controller
*ctrl
, struct slot
*slot
)
1799 struct hotplug_slot_info
*info
;
1802 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
1806 info
->power_status
= get_slot_enabled(ctrl
, slot
);
1807 info
->attention_status
= cpq_get_attention_status(ctrl
, slot
);
1808 info
->latch_status
= cpq_get_latch_status(ctrl
, slot
);
1809 info
->adapter_status
= get_presence_status(ctrl
, slot
);
1810 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
1815 static void interrupt_event_handler(struct controller
*ctrl
)
1819 struct pci_func
*func
;
1821 struct slot
*p_slot
;
1826 for (loop
= 0; loop
< 10; loop
++) {
1827 /* dbg("loop %d\n", loop); */
1828 if (ctrl
->event_queue
[loop
].event_type
!= 0) {
1829 hp_slot
= ctrl
->event_queue
[loop
].hp_slot
;
1831 func
= cpqhp_slot_find(ctrl
->bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
1835 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1839 dbg("hp_slot %d, func %p, p_slot %p\n",
1840 hp_slot
, func
, p_slot
);
1842 if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_PRESS
) {
1843 dbg("button pressed\n");
1844 } else if (ctrl
->event_queue
[loop
].event_type
==
1845 INT_BUTTON_CANCEL
) {
1846 dbg("button cancel\n");
1847 del_timer(&p_slot
->task_event
);
1849 mutex_lock(&ctrl
->crit_sect
);
1851 if (p_slot
->state
== BLINKINGOFF_STATE
) {
1853 dbg("turn on green LED\n");
1854 green_LED_on (ctrl
, hp_slot
);
1855 } else if (p_slot
->state
== BLINKINGON_STATE
) {
1857 dbg("turn off green LED\n");
1858 green_LED_off (ctrl
, hp_slot
);
1861 info(msg_button_cancel
, p_slot
->number
);
1863 p_slot
->state
= STATIC_STATE
;
1865 amber_LED_off (ctrl
, hp_slot
);
1869 /* Wait for SOBS to be unset */
1870 wait_for_ctrl_irq (ctrl
);
1872 mutex_unlock(&ctrl
->crit_sect
);
1874 /*** button Released (No action on press...) */
1875 else if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_RELEASE
) {
1876 dbg("button release\n");
1878 if (is_slot_enabled (ctrl
, hp_slot
)) {
1879 dbg("slot is on\n");
1880 p_slot
->state
= BLINKINGOFF_STATE
;
1881 info(msg_button_off
, p_slot
->number
);
1883 dbg("slot is off\n");
1884 p_slot
->state
= BLINKINGON_STATE
;
1885 info(msg_button_on
, p_slot
->number
);
1887 mutex_lock(&ctrl
->crit_sect
);
1889 dbg("blink green LED and turn off amber\n");
1891 amber_LED_off (ctrl
, hp_slot
);
1892 green_LED_blink (ctrl
, hp_slot
);
1896 /* Wait for SOBS to be unset */
1897 wait_for_ctrl_irq (ctrl
);
1899 mutex_unlock(&ctrl
->crit_sect
);
1900 init_timer(&p_slot
->task_event
);
1901 p_slot
->hp_slot
= hp_slot
;
1902 p_slot
->ctrl
= ctrl
;
1903 /* p_slot->physical_slot = physical_slot; */
1904 p_slot
->task_event
.expires
= jiffies
+ 5 * HZ
; /* 5 second delay */
1905 p_slot
->task_event
.function
= pushbutton_helper_thread
;
1906 p_slot
->task_event
.data
= (u32
) p_slot
;
1908 dbg("add_timer p_slot = %p\n", p_slot
);
1909 add_timer(&p_slot
->task_event
);
1911 /***********POWER FAULT */
1912 else if (ctrl
->event_queue
[loop
].event_type
== INT_POWER_FAULT
) {
1913 dbg("power fault\n");
1915 /* refresh notification */
1917 update_slot_info(ctrl
, p_slot
);
1920 ctrl
->event_queue
[loop
].event_type
= 0;
1924 } /* End of FOR loop */
1932 * cpqhp_pushbutton_thread
1934 * Scheduled procedure to handle blocking stuff for the pushbuttons
1935 * Handles all pending events and exits.
1938 void cpqhp_pushbutton_thread(unsigned long slot
)
1942 struct pci_func
*func
;
1943 struct slot
*p_slot
= (struct slot
*) slot
;
1944 struct controller
*ctrl
= (struct controller
*) p_slot
->ctrl
;
1946 pushbutton_pending
= 0;
1947 hp_slot
= p_slot
->hp_slot
;
1949 device
= p_slot
->device
;
1951 if (is_slot_enabled(ctrl
, hp_slot
)) {
1952 p_slot
->state
= POWEROFF_STATE
;
1953 /* power Down board */
1954 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1955 dbg("In power_down_board, func = %p, ctrl = %p\n", func
, ctrl
);
1957 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1961 if (func
!= NULL
&& ctrl
!= NULL
) {
1962 if (cpqhp_process_SS(ctrl
, func
) != 0) {
1963 amber_LED_on (ctrl
, hp_slot
);
1964 green_LED_on (ctrl
, hp_slot
);
1968 /* Wait for SOBS to be unset */
1969 wait_for_ctrl_irq (ctrl
);
1973 p_slot
->state
= STATIC_STATE
;
1975 p_slot
->state
= POWERON_STATE
;
1978 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1979 dbg("In add_board, func = %p, ctrl = %p\n", func
, ctrl
);
1981 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1985 if (func
!= NULL
&& ctrl
!= NULL
) {
1986 if (cpqhp_process_SI(ctrl
, func
) != 0) {
1987 amber_LED_on(ctrl
, hp_slot
);
1988 green_LED_off(ctrl
, hp_slot
);
1992 /* Wait for SOBS to be unset */
1993 wait_for_ctrl_irq (ctrl
);
1997 p_slot
->state
= STATIC_STATE
;
2004 int cpqhp_process_SI(struct controller
*ctrl
, struct pci_func
*func
)
2010 struct slot
* p_slot
;
2011 int physical_slot
= 0;
2015 device
= func
->device
;
2016 hp_slot
= device
- ctrl
->slot_device_offset
;
2017 p_slot
= cpqhp_find_slot(ctrl
, device
);
2019 physical_slot
= p_slot
->number
;
2021 /* Check to see if the interlock is closed */
2022 tempdword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
2024 if (tempdword
& (0x01 << hp_slot
)) {
2028 if (func
->is_a_board
) {
2029 rc
= board_replaced(func
, ctrl
);
2034 func
= cpqhp_slot_create(ctrl
->bus
);
2038 func
->bus
= ctrl
->bus
;
2039 func
->device
= device
;
2041 func
->configured
= 0;
2042 func
->is_a_board
= 1;
2044 /* We have to save the presence info for these slots */
2045 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2046 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2047 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
2049 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2050 func
->switch_save
= 0;
2052 func
->switch_save
= 0x10;
2055 rc
= board_added(func
, ctrl
);
2057 if (is_bridge(func
)) {
2058 bridge_slot_remove(func
);
2062 /* Setup slot structure with entry for empty slot */
2063 func
= cpqhp_slot_create(ctrl
->bus
);
2068 func
->bus
= ctrl
->bus
;
2069 func
->device
= device
;
2071 func
->configured
= 0;
2072 func
->is_a_board
= 0;
2074 /* We have to save the presence info for these slots */
2075 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2076 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2077 func
->presence_save
|=
2078 (temp_word
>> (hp_slot
+ 7)) & 0x02;
2080 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2081 func
->switch_save
= 0;
2083 func
->switch_save
= 0x10;
2089 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2093 update_slot_info(ctrl
, p_slot
);
2099 int cpqhp_process_SS(struct controller
*ctrl
, struct pci_func
*func
)
2101 u8 device
, class_code
, header_type
, BCR
;
2106 struct slot
* p_slot
;
2107 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
2108 int physical_slot
=0;
2110 device
= func
->device
;
2111 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2112 p_slot
= cpqhp_find_slot(ctrl
, device
);
2114 physical_slot
= p_slot
->number
;
2117 /* Make sure there are no video controllers here */
2118 while (func
&& !rc
) {
2119 pci_bus
->number
= func
->bus
;
2120 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2122 /* Check the Class Code */
2123 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2127 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2128 /* Display/Video adapter (not supported) */
2129 rc
= REMOVE_NOT_SUPPORTED
;
2131 /* See if it's a bridge */
2132 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
2136 /* If it's a bridge, check the VGA Enable bit */
2137 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
2138 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, &BCR
);
2142 /* If the VGA Enable bit is set, remove isn't
2144 if (BCR
& PCI_BRIDGE_CTL_VGA
) {
2145 rc
= REMOVE_NOT_SUPPORTED
;
2150 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2153 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
2154 if ((func
!= NULL
) && !rc
) {
2155 /* FIXME: Replace flag should be passed into process_SS */
2156 replace_flag
= !(ctrl
->add_support
);
2157 rc
= remove_board(func
, replace_flag
, ctrl
);
2163 update_slot_info(ctrl
, p_slot
);
2169 * switch_leds: switch the leds, go from one site to the other.
2170 * @ctrl: controller to use
2171 * @num_of_slots: number of slots to use
2172 * @direction: 1 to start from the left side, 0 to start right.
2174 static void switch_leds(struct controller
*ctrl
, const int num_of_slots
,
2175 u32
*work_LED
, const int direction
)
2179 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2181 *work_LED
= *work_LED
>> 1;
2183 *work_LED
= *work_LED
<< 1;
2184 writel(*work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2188 /* Wait for SOGO interrupt */
2189 wait_for_ctrl_irq(ctrl
);
2191 /* Get ready for next iteration */
2192 long_delay((2*HZ
)/10);
2197 * hardware_test - runs hardware tests
2199 * For hot plug ctrl folks to play with.
2200 * test_num is the number written to the "test" file in sysfs
2203 int cpqhp_hardware_test(struct controller
*ctrl
, int test_num
)
2210 num_of_slots
= readb(ctrl
->hpc_reg
+ SLOT_MASK
) & 0x0f;
2214 /* Do stuff here! */
2216 /* Do that funky LED thing */
2217 /* so we can restore them later */
2218 save_LED
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
2219 work_LED
= 0x01010101;
2220 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2221 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2222 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2223 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2225 work_LED
= 0x01010000;
2226 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2227 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2228 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2229 work_LED
= 0x00000101;
2230 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2231 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2232 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2234 work_LED
= 0x01010000;
2235 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2236 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2239 /* Wait for SOGO interrupt */
2240 wait_for_ctrl_irq (ctrl
);
2242 /* Get ready for next iteration */
2243 long_delay((3*HZ
)/10);
2244 work_LED
= work_LED
>> 16;
2245 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2249 /* Wait for SOGO interrupt */
2250 wait_for_ctrl_irq (ctrl
);
2252 /* Get ready for next iteration */
2253 long_delay((3*HZ
)/10);
2254 work_LED
= work_LED
<< 16;
2255 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2256 work_LED
= work_LED
<< 1;
2257 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2260 /* put it back the way it was */
2261 writel(save_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2265 /* Wait for SOBS to be unset */
2266 wait_for_ctrl_irq (ctrl
);
2269 /* Do other stuff here! */
2280 * configure_new_device - Configures the PCI header information of one board.
2282 * @ctrl: pointer to controller structure
2283 * @func: pointer to function structure
2284 * @behind_bridge: 1 if this is a recursive call, 0 if not
2285 * @resources: pointer to set of resource lists
2287 * Returns 0 if success
2290 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
* func
,
2291 u8 behind_bridge
, struct resource_lists
* resources
)
2293 u8 temp_byte
, function
, max_functions
, stop_it
;
2296 struct pci_func
*new_slot
;
2301 dbg("%s\n", __FUNCTION__
);
2302 /* Check for Multi-function device */
2303 ctrl
->pci_bus
->number
= func
->bus
;
2304 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), 0x0E, &temp_byte
);
2306 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2310 if (temp_byte
& 0x80) /* Multi-function device */
2318 rc
= configure_new_function(ctrl
, new_slot
, behind_bridge
, resources
);
2321 dbg("configure_new_function failed %d\n",rc
);
2325 new_slot
= cpqhp_slot_find(new_slot
->bus
, new_slot
->device
, index
++);
2328 cpqhp_return_board_resources(new_slot
, resources
);
2338 /* The following loop skips to the next present function
2339 * and creates a board structure */
2341 while ((function
< max_functions
) && (!stop_it
)) {
2342 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, function
), 0x00, &ID
);
2344 if (ID
== 0xFFFFFFFF) { /* There's nothing there. */
2346 } else { /* There's something there */
2347 /* Setup slot structure. */
2348 new_slot
= cpqhp_slot_create(func
->bus
);
2350 if (new_slot
== NULL
)
2353 new_slot
->bus
= func
->bus
;
2354 new_slot
->device
= func
->device
;
2355 new_slot
->function
= function
;
2356 new_slot
->is_a_board
= 1;
2357 new_slot
->status
= 0;
2363 } while (function
< max_functions
);
2364 dbg("returning from configure_new_device\n");
2371 Configuration logic that involves the hotplug data structures and
2377 * configure_new_function - Configures the PCI header information of one device
2379 * @ctrl: pointer to controller structure
2380 * @func: pointer to function structure
2381 * @behind_bridge: 1 if this is a recursive call, 0 if not
2382 * @resources: pointer to set of resource lists
2384 * Calls itself recursively for bridged devices.
2385 * Returns 0 if success
2388 static int configure_new_function(struct controller
*ctrl
, struct pci_func
*func
,
2390 struct resource_lists
*resources
)
2405 struct pci_resource
*mem_node
;
2406 struct pci_resource
*p_mem_node
;
2407 struct pci_resource
*io_node
;
2408 struct pci_resource
*bus_node
;
2409 struct pci_resource
*hold_mem_node
;
2410 struct pci_resource
*hold_p_mem_node
;
2411 struct pci_resource
*hold_IO_node
;
2412 struct pci_resource
*hold_bus_node
;
2413 struct irq_mapping irqs
;
2414 struct pci_func
*new_slot
;
2415 struct pci_bus
*pci_bus
;
2416 struct resource_lists temp_resources
;
2418 pci_bus
= ctrl
->pci_bus
;
2419 pci_bus
->number
= func
->bus
;
2420 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2422 /* Check for Bridge */
2423 rc
= pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &temp_byte
);
2427 if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) { /* PCI-PCI Bridge */
2428 /* set Primary bus */
2429 dbg("set Primary bus = %d\n", func
->bus
);
2430 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_PRIMARY_BUS
, func
->bus
);
2434 /* find range of busses to use */
2435 dbg("find ranges of buses to use\n");
2436 bus_node
= get_max_resource(&(resources
->bus_head
), 1);
2438 /* If we don't have any busses to allocate, we can't continue */
2442 /* set Secondary bus */
2443 temp_byte
= bus_node
->base
;
2444 dbg("set Secondary bus = %d\n", bus_node
->base
);
2445 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, temp_byte
);
2449 /* set subordinate bus */
2450 temp_byte
= bus_node
->base
+ bus_node
->length
- 1;
2451 dbg("set subordinate bus = %d\n", bus_node
->base
+ bus_node
->length
- 1);
2452 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2456 /* set subordinate Latency Timer and base Latency Timer */
2458 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SEC_LATENCY_TIMER
, temp_byte
);
2461 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_LATENCY_TIMER
, temp_byte
);
2465 /* set Cache Line size */
2467 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, temp_byte
);
2471 /* Setup the IO, memory, and prefetchable windows */
2472 io_node
= get_max_resource(&(resources
->io_head
), 0x1000);
2475 mem_node
= get_max_resource(&(resources
->mem_head
), 0x100000);
2478 p_mem_node
= get_max_resource(&(resources
->p_mem_head
), 0x100000);
2481 dbg("Setup the IO, memory, and prefetchable windows\n");
2483 dbg("(base, len, next) (%x, %x, %p)\n", io_node
->base
,
2484 io_node
->length
, io_node
->next
);
2486 dbg("(base, len, next) (%x, %x, %p)\n", mem_node
->base
,
2487 mem_node
->length
, mem_node
->next
);
2488 dbg("p_mem_node\n");
2489 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node
->base
,
2490 p_mem_node
->length
, p_mem_node
->next
);
2492 /* set up the IRQ info */
2493 if (!resources
->irqs
) {
2494 irqs
.barber_pole
= 0;
2495 irqs
.interrupt
[0] = 0;
2496 irqs
.interrupt
[1] = 0;
2497 irqs
.interrupt
[2] = 0;
2498 irqs
.interrupt
[3] = 0;
2501 irqs
.barber_pole
= resources
->irqs
->barber_pole
;
2502 irqs
.interrupt
[0] = resources
->irqs
->interrupt
[0];
2503 irqs
.interrupt
[1] = resources
->irqs
->interrupt
[1];
2504 irqs
.interrupt
[2] = resources
->irqs
->interrupt
[2];
2505 irqs
.interrupt
[3] = resources
->irqs
->interrupt
[3];
2506 irqs
.valid_INT
= resources
->irqs
->valid_INT
;
2509 /* set up resource lists that are now aligned on top and bottom
2510 * for anything behind the bridge. */
2511 temp_resources
.bus_head
= bus_node
;
2512 temp_resources
.io_head
= io_node
;
2513 temp_resources
.mem_head
= mem_node
;
2514 temp_resources
.p_mem_head
= p_mem_node
;
2515 temp_resources
.irqs
= &irqs
;
2517 /* Make copies of the nodes we are going to pass down so that
2518 * if there is a problem,we can just use these to free resources */
2519 hold_bus_node
= kmalloc(sizeof(*hold_bus_node
), GFP_KERNEL
);
2520 hold_IO_node
= kmalloc(sizeof(*hold_IO_node
), GFP_KERNEL
);
2521 hold_mem_node
= kmalloc(sizeof(*hold_mem_node
), GFP_KERNEL
);
2522 hold_p_mem_node
= kmalloc(sizeof(*hold_p_mem_node
), GFP_KERNEL
);
2524 if (!hold_bus_node
|| !hold_IO_node
|| !hold_mem_node
|| !hold_p_mem_node
) {
2525 kfree(hold_bus_node
);
2526 kfree(hold_IO_node
);
2527 kfree(hold_mem_node
);
2528 kfree(hold_p_mem_node
);
2533 memcpy(hold_bus_node
, bus_node
, sizeof(struct pci_resource
));
2535 bus_node
->base
+= 1;
2536 bus_node
->length
-= 1;
2537 bus_node
->next
= NULL
;
2539 /* If we have IO resources copy them and fill in the bridge's
2540 * IO range registers */
2542 memcpy(hold_IO_node
, io_node
, sizeof(struct pci_resource
));
2543 io_node
->next
= NULL
;
2545 /* set IO base and Limit registers */
2546 temp_byte
= io_node
->base
>> 8;
2547 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2549 temp_byte
= (io_node
->base
+ io_node
->length
- 1) >> 8;
2550 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2552 kfree(hold_IO_node
);
2553 hold_IO_node
= NULL
;
2556 /* If we have memory resources copy them and fill in the
2557 * bridge's memory range registers. Otherwise, fill in the
2558 * range registers with values that disable them. */
2560 memcpy(hold_mem_node
, mem_node
, sizeof(struct pci_resource
));
2561 mem_node
->next
= NULL
;
2563 /* set Mem base and Limit registers */
2564 temp_word
= mem_node
->base
>> 16;
2565 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2567 temp_word
= (mem_node
->base
+ mem_node
->length
- 1) >> 16;
2568 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2571 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2574 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2576 kfree(hold_mem_node
);
2577 hold_mem_node
= NULL
;
2580 memcpy(hold_p_mem_node
, p_mem_node
, sizeof(struct pci_resource
));
2581 p_mem_node
->next
= NULL
;
2583 /* set Pre Mem base and Limit registers */
2584 temp_word
= p_mem_node
->base
>> 16;
2585 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2587 temp_word
= (p_mem_node
->base
+ p_mem_node
->length
- 1) >> 16;
2588 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2590 /* Adjust this to compensate for extra adjustment in first loop */
2595 /* Here we actually find the devices and configure them */
2596 for (device
= 0; (device
<= 0x1F) && !rc
; device
++) {
2597 irqs
.barber_pole
= (irqs
.barber_pole
+ 1) & 0x03;
2600 pci_bus
->number
= hold_bus_node
->base
;
2601 pci_bus_read_config_dword (pci_bus
, PCI_DEVFN(device
, 0), 0x00, &ID
);
2602 pci_bus
->number
= func
->bus
;
2604 if (ID
!= 0xFFFFFFFF) { /* device present */
2605 /* Setup slot structure. */
2606 new_slot
= cpqhp_slot_create(hold_bus_node
->base
);
2608 if (new_slot
== NULL
) {
2613 new_slot
->bus
= hold_bus_node
->base
;
2614 new_slot
->device
= device
;
2615 new_slot
->function
= 0;
2616 new_slot
->is_a_board
= 1;
2617 new_slot
->status
= 0;
2619 rc
= configure_new_device(ctrl
, new_slot
, 1, &temp_resources
);
2620 dbg("configure_new_device rc=0x%x\n",rc
);
2621 } /* End of IF (device in slot?) */
2622 } /* End of FOR loop */
2626 /* save the interrupt routing information */
2627 if (resources
->irqs
) {
2628 resources
->irqs
->interrupt
[0] = irqs
.interrupt
[0];
2629 resources
->irqs
->interrupt
[1] = irqs
.interrupt
[1];
2630 resources
->irqs
->interrupt
[2] = irqs
.interrupt
[2];
2631 resources
->irqs
->interrupt
[3] = irqs
.interrupt
[3];
2632 resources
->irqs
->valid_INT
= irqs
.valid_INT
;
2633 } else if (!behind_bridge
) {
2634 /* We need to hook up the interrupts here */
2635 for (cloop
= 0; cloop
< 4; cloop
++) {
2636 if (irqs
.valid_INT
& (0x01 << cloop
)) {
2637 rc
= cpqhp_set_irq(func
->bus
, func
->device
,
2638 0x0A + cloop
, irqs
.interrupt
[cloop
]);
2642 } /* end of for loop */
2644 /* Return unused bus resources
2645 * First use the temporary node to store information for
2647 if (hold_bus_node
&& bus_node
&& temp_resources
.bus_head
) {
2648 hold_bus_node
->length
= bus_node
->base
- hold_bus_node
->base
;
2650 hold_bus_node
->next
= func
->bus_head
;
2651 func
->bus_head
= hold_bus_node
;
2653 temp_byte
= temp_resources
.bus_head
->base
- 1;
2655 /* set subordinate bus */
2656 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2658 if (temp_resources
.bus_head
->length
== 0) {
2659 kfree(temp_resources
.bus_head
);
2660 temp_resources
.bus_head
= NULL
;
2662 return_resource(&(resources
->bus_head
), temp_resources
.bus_head
);
2666 /* If we have IO space available and there is some left,
2667 * return the unused portion */
2668 if (hold_IO_node
&& temp_resources
.io_head
) {
2669 io_node
= do_pre_bridge_resource_split(&(temp_resources
.io_head
),
2670 &hold_IO_node
, 0x1000);
2672 /* Check if we were able to split something off */
2674 hold_IO_node
->base
= io_node
->base
+ io_node
->length
;
2676 temp_byte
= (hold_IO_node
->base
) >> 8;
2677 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2679 return_resource(&(resources
->io_head
), io_node
);
2682 io_node
= do_bridge_resource_split(&(temp_resources
.io_head
), 0x1000);
2684 /* Check if we were able to split something off */
2686 /* First use the temporary node to store
2687 * information for the board */
2688 hold_IO_node
->length
= io_node
->base
- hold_IO_node
->base
;
2690 /* If we used any, add it to the board's list */
2691 if (hold_IO_node
->length
) {
2692 hold_IO_node
->next
= func
->io_head
;
2693 func
->io_head
= hold_IO_node
;
2695 temp_byte
= (io_node
->base
- 1) >> 8;
2696 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2698 return_resource(&(resources
->io_head
), io_node
);
2700 /* it doesn't need any IO */
2702 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_word
);
2704 return_resource(&(resources
->io_head
), io_node
);
2705 kfree(hold_IO_node
);
2708 /* it used most of the range */
2709 hold_IO_node
->next
= func
->io_head
;
2710 func
->io_head
= hold_IO_node
;
2712 } else if (hold_IO_node
) {
2713 /* it used the whole range */
2714 hold_IO_node
->next
= func
->io_head
;
2715 func
->io_head
= hold_IO_node
;
2717 /* If we have memory space available and there is some left,
2718 * return the unused portion */
2719 if (hold_mem_node
&& temp_resources
.mem_head
) {
2720 mem_node
= do_pre_bridge_resource_split(&(temp_resources
. mem_head
),
2721 &hold_mem_node
, 0x100000);
2723 /* Check if we were able to split something off */
2725 hold_mem_node
->base
= mem_node
->base
+ mem_node
->length
;
2727 temp_word
= (hold_mem_node
->base
) >> 16;
2728 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2730 return_resource(&(resources
->mem_head
), mem_node
);
2733 mem_node
= do_bridge_resource_split(&(temp_resources
.mem_head
), 0x100000);
2735 /* Check if we were able to split something off */
2737 /* First use the temporary node to store
2738 * information for the board */
2739 hold_mem_node
->length
= mem_node
->base
- hold_mem_node
->base
;
2741 if (hold_mem_node
->length
) {
2742 hold_mem_node
->next
= func
->mem_head
;
2743 func
->mem_head
= hold_mem_node
;
2745 /* configure end address */
2746 temp_word
= (mem_node
->base
- 1) >> 16;
2747 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2749 /* Return unused resources to the pool */
2750 return_resource(&(resources
->mem_head
), mem_node
);
2752 /* it doesn't need any Mem */
2754 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2756 return_resource(&(resources
->mem_head
), mem_node
);
2757 kfree(hold_mem_node
);
2760 /* it used most of the range */
2761 hold_mem_node
->next
= func
->mem_head
;
2762 func
->mem_head
= hold_mem_node
;
2764 } else if (hold_mem_node
) {
2765 /* it used the whole range */
2766 hold_mem_node
->next
= func
->mem_head
;
2767 func
->mem_head
= hold_mem_node
;
2769 /* If we have prefetchable memory space available and there
2770 * is some left at the end, return the unused portion */
2771 if (hold_p_mem_node
&& temp_resources
.p_mem_head
) {
2772 p_mem_node
= do_pre_bridge_resource_split(&(temp_resources
.p_mem_head
),
2773 &hold_p_mem_node
, 0x100000);
2775 /* Check if we were able to split something off */
2777 hold_p_mem_node
->base
= p_mem_node
->base
+ p_mem_node
->length
;
2779 temp_word
= (hold_p_mem_node
->base
) >> 16;
2780 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2782 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2785 p_mem_node
= do_bridge_resource_split(&(temp_resources
.p_mem_head
), 0x100000);
2787 /* Check if we were able to split something off */
2789 /* First use the temporary node to store
2790 * information for the board */
2791 hold_p_mem_node
->length
= p_mem_node
->base
- hold_p_mem_node
->base
;
2793 /* If we used any, add it to the board's list */
2794 if (hold_p_mem_node
->length
) {
2795 hold_p_mem_node
->next
= func
->p_mem_head
;
2796 func
->p_mem_head
= hold_p_mem_node
;
2798 temp_word
= (p_mem_node
->base
- 1) >> 16;
2799 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2801 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2803 /* it doesn't need any PMem */
2805 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2807 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2808 kfree(hold_p_mem_node
);
2811 /* it used the most of the range */
2812 hold_p_mem_node
->next
= func
->p_mem_head
;
2813 func
->p_mem_head
= hold_p_mem_node
;
2815 } else if (hold_p_mem_node
) {
2816 /* it used the whole range */
2817 hold_p_mem_node
->next
= func
->p_mem_head
;
2818 func
->p_mem_head
= hold_p_mem_node
;
2820 /* We should be configuring an IRQ and the bridge's base address
2821 * registers if it needs them. Although we have never seen such
2825 command
= 0x0157; /* = PCI_COMMAND_IO |
2826 * PCI_COMMAND_MEMORY |
2827 * PCI_COMMAND_MASTER |
2828 * PCI_COMMAND_INVALIDATE |
2829 * PCI_COMMAND_PARITY |
2830 * PCI_COMMAND_SERR */
2831 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_COMMAND
, command
);
2833 /* set Bridge Control Register */
2834 command
= 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2835 * PCI_BRIDGE_CTL_SERR |
2836 * PCI_BRIDGE_CTL_NO_ISA */
2837 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
2838 } else if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
2839 /* Standard device */
2840 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2842 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2843 /* Display (video) adapter (not supported) */
2844 return DEVICE_TYPE_NOT_SUPPORTED
;
2846 /* Figure out IO and memory needs */
2847 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
2848 temp_register
= 0xFFFFFFFF;
2850 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus
->number
, devfn
, cloop
);
2851 rc
= pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
2853 rc
= pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp_register
);
2854 dbg("CND: base = 0x%x\n", temp_register
);
2856 if (temp_register
) { /* If this register is implemented */
2857 if ((temp_register
& 0x03L
) == 0x01) {
2860 /* set base = amount of IO space */
2861 base
= temp_register
& 0xFFFFFFFC;
2864 dbg("CND: length = 0x%x\n", base
);
2865 io_node
= get_io_resource(&(resources
->io_head
), base
);
2866 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2867 io_node
->base
, io_node
->length
, io_node
->next
);
2868 dbg("func (%p) io_head (%p)\n", func
, func
->io_head
);
2870 /* allocate the resource to the board */
2872 base
= io_node
->base
;
2874 io_node
->next
= func
->io_head
;
2875 func
->io_head
= io_node
;
2878 } else if ((temp_register
& 0x0BL
) == 0x08) {
2879 /* Map prefetchable memory */
2880 base
= temp_register
& 0xFFFFFFF0;
2883 dbg("CND: length = 0x%x\n", base
);
2884 p_mem_node
= get_resource(&(resources
->p_mem_head
), base
);
2886 /* allocate the resource to the board */
2888 base
= p_mem_node
->base
;
2890 p_mem_node
->next
= func
->p_mem_head
;
2891 func
->p_mem_head
= p_mem_node
;
2894 } else if ((temp_register
& 0x0BL
) == 0x00) {
2896 base
= temp_register
& 0xFFFFFFF0;
2899 dbg("CND: length = 0x%x\n", base
);
2900 mem_node
= get_resource(&(resources
->mem_head
), base
);
2902 /* allocate the resource to the board */
2904 base
= mem_node
->base
;
2906 mem_node
->next
= func
->mem_head
;
2907 func
->mem_head
= mem_node
;
2910 } else if ((temp_register
& 0x0BL
) == 0x04) {
2912 base
= temp_register
& 0xFFFFFFF0;
2915 dbg("CND: length = 0x%x\n", base
);
2916 mem_node
= get_resource(&(resources
->mem_head
), base
);
2918 /* allocate the resource to the board */
2920 base
= mem_node
->base
;
2922 mem_node
->next
= func
->mem_head
;
2923 func
->mem_head
= mem_node
;
2926 } else if ((temp_register
& 0x0BL
) == 0x06) {
2927 /* Those bits are reserved, we can't handle this */
2930 /* Requesting space below 1M */
2931 return NOT_ENOUGH_RESOURCES
;
2934 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2936 /* Check for 64-bit base */
2937 if ((temp_register
& 0x07L
) == 0x04) {
2940 /* Upper 32 bits of address always zero
2941 * on today's systems */
2942 /* FIXME this is probably not true on
2943 * Alpha and ia64??? */
2945 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2948 } /* End of base register loop */
2949 if (cpqhp_legacy_mode
) {
2950 /* Figure out which interrupt pin this function uses */
2951 rc
= pci_bus_read_config_byte (pci_bus
, devfn
,
2952 PCI_INTERRUPT_PIN
, &temp_byte
);
2954 /* If this function needs an interrupt and we are behind
2955 * a bridge and the pin is tied to something that's
2956 * alread mapped, set this one the same */
2957 if (temp_byte
&& resources
->irqs
&&
2958 (resources
->irqs
->valid_INT
&
2959 (0x01 << ((temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03)))) {
2960 /* We have to share with something already set up */
2961 IRQ
= resources
->irqs
->interrupt
[(temp_byte
+
2962 resources
->irqs
->barber_pole
- 1) & 0x03];
2964 /* Program IRQ based on card type */
2965 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2967 if (class_code
== PCI_BASE_CLASS_STORAGE
) {
2968 IRQ
= cpqhp_disk_irq
;
2970 IRQ
= cpqhp_nic_irq
;
2975 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_INTERRUPT_LINE
, IRQ
);
2978 if (!behind_bridge
) {
2979 rc
= cpqhp_set_irq(func
->bus
, func
->device
, temp_byte
+ 0x09, IRQ
);
2983 /* TBD - this code may also belong in the other clause
2984 * of this If statement */
2985 resources
->irqs
->interrupt
[(temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03] = IRQ
;
2986 resources
->irqs
->valid_INT
|= 0x01 << (temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03;
2991 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
2992 PCI_LATENCY_TIMER
, temp_byte
);
2994 /* Cache Line size */
2996 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
2997 PCI_CACHE_LINE_SIZE
, temp_byte
);
2999 /* disable ROM base Address */
3001 rc
= pci_bus_write_config_word(pci_bus
, devfn
,
3002 PCI_ROM_ADDRESS
, temp_dword
);
3005 temp_word
= 0x0157; /* = PCI_COMMAND_IO |
3006 * PCI_COMMAND_MEMORY |
3007 * PCI_COMMAND_MASTER |
3008 * PCI_COMMAND_INVALIDATE |
3009 * PCI_COMMAND_PARITY |
3010 * PCI_COMMAND_SERR */
3011 rc
= pci_bus_write_config_word (pci_bus
, devfn
,
3012 PCI_COMMAND
, temp_word
);
3013 } else { /* End of Not-A-Bridge else */
3014 /* It's some strange type of PCI adapter (Cardbus?) */
3015 return DEVICE_TYPE_NOT_SUPPORTED
;
3018 func
->configured
= 1;
3022 cpqhp_destroy_resource_list (&temp_resources
);
3024 return_resource(&(resources
-> bus_head
), hold_bus_node
);
3025 return_resource(&(resources
-> io_head
), hold_IO_node
);
3026 return_resource(&(resources
-> mem_head
), hold_mem_node
);
3027 return_resource(&(resources
-> p_mem_head
), hold_p_mem_node
);