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>
41 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
*func
,
42 u8 behind_bridge
, struct resource_lists
*resources
);
43 static int configure_new_function(struct controller
* ctrl
, struct pci_func
*func
,
44 u8 behind_bridge
, struct resource_lists
*resources
);
45 static void interrupt_event_handler(struct controller
*ctrl
);
47 static struct semaphore event_semaphore
; /* mutex for process loop (up if something to process) */
48 static struct semaphore event_exit
; /* guard ensure thread has exited before calling it quits */
49 static int event_finished
;
50 static unsigned long pushbutton_pending
; /* = 0 */
52 /* things needed for the long_delay function */
53 static struct semaphore delay_sem
;
54 static wait_queue_head_t delay_wait
;
56 /* delay is in jiffies to wait for */
57 static void long_delay(int delay
)
59 DECLARE_WAITQUEUE(wait
, current
);
61 /* only allow 1 customer into the delay queue at once
62 * yes this makes some people wait even longer, but who really cares?
63 * this is for _huge_ delays to make the hardware happy as the
64 * signals bounce around
68 init_waitqueue_head(&delay_wait
);
70 add_wait_queue(&delay_wait
, &wait
);
71 msleep_interruptible(jiffies_to_msecs(delay
));
72 remove_wait_queue(&delay_wait
, &wait
);
78 /* FIXME: The following line needs to be somewhere else... */
79 #define WRONG_BUS_FREQUENCY 0x07
80 static u8
handle_switch_change(u8 change
, struct controller
* ctrl
)
85 struct pci_func
*func
;
86 struct event_info
*taskInfo
;
92 dbg("cpqsbd: Switch interrupt received.\n");
94 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
95 if (change
& (0x1L
<< hp_slot
)) {
96 /**********************************
98 **********************************/
99 func
= cpqhp_slot_find(ctrl
->bus
,
100 (hp_slot
+ ctrl
->slot_device_offset
), 0);
102 /* this is the structure that tells the worker thread
104 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
105 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
106 taskInfo
->hp_slot
= hp_slot
;
110 temp_word
= ctrl
->ctrl_int_comp
>> 16;
111 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
112 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
114 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
115 /**********************************
117 **********************************/
119 func
->switch_save
= 0;
121 taskInfo
->event_type
= INT_SWITCH_OPEN
;
123 /**********************************
125 **********************************/
127 func
->switch_save
= 0x10;
129 taskInfo
->event_type
= INT_SWITCH_CLOSE
;
138 * cpqhp_find_slot: find the struct slot of given device
139 * @ctrl: scan lots of this controller
140 * @device: the device id to find
142 static struct slot
*cpqhp_find_slot(struct controller
*ctrl
, u8 device
)
144 struct slot
*slot
= ctrl
->slot
;
146 while (slot
&& (slot
->device
!= device
)) {
154 static u8
handle_presence_change(u16 change
, struct controller
* ctrl
)
160 struct pci_func
*func
;
161 struct event_info
*taskInfo
;
167 /**********************************
169 **********************************/
170 dbg("cpqsbd: Presence/Notify input change.\n");
171 dbg(" Changed bits are 0x%4.4x\n", change
);
173 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
174 if (change
& (0x0101 << hp_slot
)) {
175 /**********************************
177 **********************************/
178 func
= cpqhp_slot_find(ctrl
->bus
,
179 (hp_slot
+ ctrl
->slot_device_offset
), 0);
181 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
182 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
183 taskInfo
->hp_slot
= hp_slot
;
187 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ (readb(ctrl
->hpc_reg
+ SLOT_MASK
) >> 4));
191 /* If the switch closed, must be a button
192 * If not in button mode, nevermind */
193 if (func
->switch_save
&& (ctrl
->push_button
== 1)) {
194 temp_word
= ctrl
->ctrl_int_comp
>> 16;
195 temp_byte
= (temp_word
>> hp_slot
) & 0x01;
196 temp_byte
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
198 if (temp_byte
!= func
->presence_save
) {
199 /**************************************
200 * button Pressed (doesn't do anything)
201 **************************************/
202 dbg("hp_slot %d button pressed\n", hp_slot
);
203 taskInfo
->event_type
= INT_BUTTON_PRESS
;
205 /**********************************
206 * button Released - TAKE ACTION!!!!
207 **********************************/
208 dbg("hp_slot %d button released\n", hp_slot
);
209 taskInfo
->event_type
= INT_BUTTON_RELEASE
;
211 /* Cancel if we are still blinking */
212 if ((p_slot
->state
== BLINKINGON_STATE
)
213 || (p_slot
->state
== BLINKINGOFF_STATE
)) {
214 taskInfo
->event_type
= INT_BUTTON_CANCEL
;
215 dbg("hp_slot %d button cancel\n", hp_slot
);
216 } else if ((p_slot
->state
== POWERON_STATE
)
217 || (p_slot
->state
== POWEROFF_STATE
)) {
218 /* info(msg_button_ignore, p_slot->number); */
219 taskInfo
->event_type
= INT_BUTTON_IGNORE
;
220 dbg("hp_slot %d button ignore\n", hp_slot
);
224 /* Switch is open, assume a presence change
225 * Save the presence state */
226 temp_word
= ctrl
->ctrl_int_comp
>> 16;
227 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
228 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
230 if ((!(ctrl
->ctrl_int_comp
& (0x010000 << hp_slot
))) ||
231 (!(ctrl
->ctrl_int_comp
& (0x01000000 << hp_slot
)))) {
233 taskInfo
->event_type
= INT_PRESENCE_ON
;
236 taskInfo
->event_type
= INT_PRESENCE_OFF
;
246 static u8
handle_power_fault(u8 change
, struct controller
* ctrl
)
250 struct pci_func
*func
;
251 struct event_info
*taskInfo
;
256 /**********************************
258 **********************************/
260 info("power fault interrupt\n");
262 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
263 if (change
& (0x01 << hp_slot
)) {
264 /**********************************
266 **********************************/
267 func
= cpqhp_slot_find(ctrl
->bus
,
268 (hp_slot
+ ctrl
->slot_device_offset
), 0);
270 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
271 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
272 taskInfo
->hp_slot
= hp_slot
;
276 if (ctrl
->ctrl_int_comp
& (0x00000100 << hp_slot
)) {
277 /**********************************
278 * power fault Cleared
279 **********************************/
282 taskInfo
->event_type
= INT_POWER_FAULT_CLEAR
;
284 /**********************************
286 **********************************/
287 taskInfo
->event_type
= INT_POWER_FAULT
;
290 amber_LED_on (ctrl
, hp_slot
);
291 green_LED_off (ctrl
, hp_slot
);
294 /* this is a fatal condition, we want
295 * to crash the machine to protect from
296 * data corruption. simulated_NMI
297 * shouldn't ever return */
299 simulated_NMI(hp_slot, ctrl); */
301 /* The following code causes a software
302 * crash just in case simulated_NMI did
305 panic(msg_power_fault); */
307 /* set power fault status for this board */
309 info("power fault bit %x set\n", hp_slot
);
320 * sort_by_size: sort nodes on the list by their length, smallest first.
321 * @head: list to sort
324 static int sort_by_size(struct pci_resource
**head
)
326 struct pci_resource
*current_res
;
327 struct pci_resource
*next_res
;
328 int out_of_order
= 1;
333 if (!((*head
)->next
))
336 while (out_of_order
) {
339 /* Special case for swapping list head */
340 if (((*head
)->next
) &&
341 ((*head
)->length
> (*head
)->next
->length
)) {
344 *head
= (*head
)->next
;
345 current_res
->next
= (*head
)->next
;
346 (*head
)->next
= current_res
;
351 while (current_res
->next
&& current_res
->next
->next
) {
352 if (current_res
->next
->length
> current_res
->next
->next
->length
) {
354 next_res
= current_res
->next
;
355 current_res
->next
= current_res
->next
->next
;
356 current_res
= current_res
->next
;
357 next_res
->next
= current_res
->next
;
358 current_res
->next
= next_res
;
360 current_res
= current_res
->next
;
362 } /* End of out_of_order loop */
369 * sort_by_max_size: sort nodes on the list by their length, largest first.
370 * @head: list to sort
373 static int sort_by_max_size(struct pci_resource
**head
)
375 struct pci_resource
*current_res
;
376 struct pci_resource
*next_res
;
377 int out_of_order
= 1;
382 if (!((*head
)->next
))
385 while (out_of_order
) {
388 /* Special case for swapping list head */
389 if (((*head
)->next
) &&
390 ((*head
)->length
< (*head
)->next
->length
)) {
393 *head
= (*head
)->next
;
394 current_res
->next
= (*head
)->next
;
395 (*head
)->next
= current_res
;
400 while (current_res
->next
&& current_res
->next
->next
) {
401 if (current_res
->next
->length
< current_res
->next
->next
->length
) {
403 next_res
= current_res
->next
;
404 current_res
->next
= current_res
->next
->next
;
405 current_res
= current_res
->next
;
406 next_res
->next
= current_res
->next
;
407 current_res
->next
= next_res
;
409 current_res
= current_res
->next
;
411 } /* End of out_of_order loop */
418 * do_pre_bridge_resource_split: find node of resources that are unused
421 static struct pci_resource
*do_pre_bridge_resource_split(struct pci_resource
**head
,
422 struct pci_resource
**orig_head
, u32 alignment
)
424 struct pci_resource
*prevnode
= NULL
;
425 struct pci_resource
*node
;
426 struct pci_resource
*split_node
;
429 dbg("do_pre_bridge_resource_split\n");
431 if (!(*head
) || !(*orig_head
))
434 rc
= cpqhp_resource_sort_and_combine(head
);
439 if ((*head
)->base
!= (*orig_head
)->base
)
442 if ((*head
)->length
== (*orig_head
)->length
)
446 /* If we got here, there the bridge requires some of the resource, but
447 * we may be able to split some off of the front */
451 if (node
->length
& (alignment
-1)) {
452 /* this one isn't an aligned length, so we'll make a new entry
453 * and split it up. */
454 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
459 temp_dword
= (node
->length
| (alignment
-1)) + 1 - alignment
;
461 split_node
->base
= node
->base
;
462 split_node
->length
= temp_dword
;
464 node
->length
-= temp_dword
;
465 node
->base
+= split_node
->length
;
467 /* Put it in the list */
469 split_node
->next
= node
;
472 if (node
->length
< alignment
)
480 while (prevnode
->next
!= node
)
481 prevnode
= prevnode
->next
;
483 prevnode
->next
= node
->next
;
492 * do_bridge_resource_split: find one node of resources that aren't in use
495 static struct pci_resource
*do_bridge_resource_split(struct pci_resource
**head
, u32 alignment
)
497 struct pci_resource
*prevnode
= NULL
;
498 struct pci_resource
*node
;
502 rc
= cpqhp_resource_sort_and_combine(head
);
515 if (node
->length
< alignment
)
518 if (node
->base
& (alignment
- 1)) {
519 /* Short circuit if adjusted size is too small */
520 temp_dword
= (node
->base
| (alignment
-1)) + 1;
521 if ((node
->length
- (temp_dword
- node
->base
)) < alignment
)
524 node
->length
-= (temp_dword
- node
->base
);
525 node
->base
= temp_dword
;
528 if (node
->length
& (alignment
- 1))
529 /* There's stuff in use after this node */
540 * get_io_resource: find first node of given size not in ISA aliasing window.
541 * @head: list to search
542 * @size: size of node to find, must be a power of two.
544 * Description: this function sorts the resource list by size and then returns
545 * returns the first node of "size" length that is not in the ISA aliasing
546 * window. If it finds a node larger than "size" it will split it up.
549 static struct pci_resource
*get_io_resource(struct pci_resource
**head
, u32 size
)
551 struct pci_resource
*prevnode
;
552 struct pci_resource
*node
;
553 struct pci_resource
*split_node
;
559 if ( cpqhp_resource_sort_and_combine(head
) )
562 if ( sort_by_size(head
) )
565 for (node
= *head
; node
; node
= node
->next
) {
566 if (node
->length
< size
)
569 if (node
->base
& (size
- 1)) {
570 /* this one isn't base aligned properly
571 * so we'll make a new entry and split it up */
572 temp_dword
= (node
->base
| (size
-1)) + 1;
574 /* Short circuit if adjusted size is too small */
575 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
578 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
583 split_node
->base
= node
->base
;
584 split_node
->length
= temp_dword
- node
->base
;
585 node
->base
= temp_dword
;
586 node
->length
-= split_node
->length
;
588 /* Put it in the list */
589 split_node
->next
= node
->next
;
590 node
->next
= split_node
;
591 } /* End of non-aligned base */
593 /* Don't need to check if too small since we already did */
594 if (node
->length
> size
) {
595 /* this one is longer than we need
596 * so we'll make a new entry and split it up */
597 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
602 split_node
->base
= node
->base
+ size
;
603 split_node
->length
= node
->length
- size
;
606 /* Put it in the list */
607 split_node
->next
= node
->next
;
608 node
->next
= split_node
;
609 } /* End of too big on top end */
611 /* For IO make sure it's not in the ISA aliasing space */
612 if (node
->base
& 0x300L
)
615 /* If we got here, then it is the right size
616 * Now take it out of the list and break */
621 while (prevnode
->next
!= node
)
622 prevnode
= prevnode
->next
;
624 prevnode
->next
= node
->next
;
635 * get_max_resource: get largest node which has at least the given size.
636 * @head: the list to search the node in
637 * @size: the minimum size of the node to find
639 * Description: Gets the largest node that is at least "size" big from the
640 * list pointed to by head. It aligns the node on top and bottom
641 * to "size" alignment before returning it.
643 static struct pci_resource
*get_max_resource(struct pci_resource
**head
, u32 size
)
645 struct pci_resource
*max
;
646 struct pci_resource
*temp
;
647 struct pci_resource
*split_node
;
650 if (cpqhp_resource_sort_and_combine(head
))
653 if (sort_by_max_size(head
))
656 for (max
= *head
; max
; max
= max
->next
) {
657 /* If not big enough we could probably just bail,
658 * instead we'll continue to the next. */
659 if (max
->length
< size
)
662 if (max
->base
& (size
- 1)) {
663 /* this one isn't base aligned properly
664 * so we'll make a new entry and split it up */
665 temp_dword
= (max
->base
| (size
-1)) + 1;
667 /* Short circuit if adjusted size is too small */
668 if ((max
->length
- (temp_dword
- max
->base
)) < size
)
671 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
676 split_node
->base
= max
->base
;
677 split_node
->length
= temp_dword
- max
->base
;
678 max
->base
= temp_dword
;
679 max
->length
-= split_node
->length
;
681 split_node
->next
= max
->next
;
682 max
->next
= split_node
;
685 if ((max
->base
+ max
->length
) & (size
- 1)) {
686 /* this one isn't end aligned properly at the top
687 * so we'll make a new entry and split it up */
688 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
692 temp_dword
= ((max
->base
+ max
->length
) & ~(size
- 1));
693 split_node
->base
= temp_dword
;
694 split_node
->length
= max
->length
+ max
->base
696 max
->length
-= split_node
->length
;
698 split_node
->next
= max
->next
;
699 max
->next
= split_node
;
702 /* Make sure it didn't shrink too much when we aligned it */
703 if (max
->length
< size
)
706 /* Now take it out of the list */
711 while (temp
&& temp
->next
!= max
) {
715 temp
->next
= max
->next
;
727 * get_resource: find resource of given size and split up larger ones.
728 * @head: the list to search for resources
729 * @size: the size limit to use
731 * Description: This function sorts the resource list by size and then
732 * returns the first node of "size" length. If it finds a node
733 * larger than "size" it will split it up.
735 * size must be a power of two.
737 static struct pci_resource
*get_resource(struct pci_resource
**head
, u32 size
)
739 struct pci_resource
*prevnode
;
740 struct pci_resource
*node
;
741 struct pci_resource
*split_node
;
744 if (cpqhp_resource_sort_and_combine(head
))
747 if (sort_by_size(head
))
750 for (node
= *head
; node
; node
= node
->next
) {
751 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
752 __FUNCTION__
, size
, node
, node
->base
, node
->length
);
753 if (node
->length
< size
)
756 if (node
->base
& (size
- 1)) {
757 dbg("%s: not aligned\n", __FUNCTION__
);
758 /* this one isn't base aligned properly
759 * so we'll make a new entry and split it up */
760 temp_dword
= (node
->base
| (size
-1)) + 1;
762 /* Short circuit if adjusted size is too small */
763 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
766 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
771 split_node
->base
= node
->base
;
772 split_node
->length
= temp_dword
- node
->base
;
773 node
->base
= temp_dword
;
774 node
->length
-= split_node
->length
;
776 split_node
->next
= node
->next
;
777 node
->next
= split_node
;
778 } /* End of non-aligned base */
780 /* Don't need to check if too small since we already did */
781 if (node
->length
> size
) {
782 dbg("%s: too big\n", __FUNCTION__
);
783 /* this one is longer than we need
784 * so we'll make a new entry and split it up */
785 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
790 split_node
->base
= node
->base
+ size
;
791 split_node
->length
= node
->length
- size
;
794 /* Put it in the list */
795 split_node
->next
= node
->next
;
796 node
->next
= split_node
;
797 } /* End of too big on top end */
799 dbg("%s: got one!!!\n", __FUNCTION__
);
800 /* If we got here, then it is the right size
801 * Now take it out of the list */
806 while (prevnode
->next
!= node
)
807 prevnode
= prevnode
->next
;
809 prevnode
->next
= node
->next
;
819 * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
820 * @head: the list to sort and clean up
822 * Description: Sorts all of the nodes in the list in ascending order by
823 * their base addresses. Also does garbage collection by
824 * combining adjacent nodes.
826 * returns 0 if success
828 int cpqhp_resource_sort_and_combine(struct pci_resource
**head
)
830 struct pci_resource
*node1
;
831 struct pci_resource
*node2
;
832 int out_of_order
= 1;
834 dbg("%s: head = %p, *head = %p\n", __FUNCTION__
, head
, *head
);
839 dbg("*head->next = %p\n",(*head
)->next
);
842 return 0; /* only one item on the list, already sorted! */
844 dbg("*head->base = 0x%x\n",(*head
)->base
);
845 dbg("*head->next->base = 0x%x\n",(*head
)->next
->base
);
846 while (out_of_order
) {
849 /* Special case for swapping list head */
850 if (((*head
)->next
) &&
851 ((*head
)->base
> (*head
)->next
->base
)) {
853 (*head
) = (*head
)->next
;
854 node1
->next
= (*head
)->next
;
855 (*head
)->next
= node1
;
861 while (node1
->next
&& node1
->next
->next
) {
862 if (node1
->next
->base
> node1
->next
->next
->base
) {
865 node1
->next
= node1
->next
->next
;
867 node2
->next
= node1
->next
;
872 } /* End of out_of_order loop */
876 while (node1
&& node1
->next
) {
877 if ((node1
->base
+ node1
->length
) == node1
->next
->base
) {
880 node1
->length
+= node1
->next
->length
;
882 node1
->next
= node1
->next
->next
;
892 irqreturn_t
cpqhp_ctrl_intr(int IRQ
, void *data
, struct pt_regs
*regs
)
894 struct controller
*ctrl
= data
;
895 u8 schedule_flag
= 0;
902 misc
= readw(ctrl
->hpc_reg
+ MISC
);
903 /***************************************
904 * Check to see if it was our interrupt
905 ***************************************/
906 if (!(misc
& 0x000C)) {
911 /**********************************
912 * Serial Output interrupt Pending
913 **********************************/
915 /* Clear the interrupt */
917 writew(misc
, ctrl
->hpc_reg
+ MISC
);
919 /* Read to clear posted writes */
920 misc
= readw(ctrl
->hpc_reg
+ MISC
);
922 dbg ("%s - waking up\n", __FUNCTION__
);
923 wake_up_interruptible(&ctrl
->queue
);
927 /* General-interrupt-input interrupt Pending */
928 Diff
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) ^ ctrl
->ctrl_int_comp
;
930 ctrl
->ctrl_int_comp
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
932 /* Clear the interrupt */
933 writel(Diff
, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
935 /* Read it back to clear any posted writes */
936 temp_dword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
939 /* Clear all interrupts */
940 writel(0xFFFFFFFF, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
942 schedule_flag
+= handle_switch_change((u8
)(Diff
& 0xFFL
), ctrl
);
943 schedule_flag
+= handle_presence_change((u16
)((Diff
& 0xFFFF0000L
) >> 16), ctrl
);
944 schedule_flag
+= handle_power_fault((u8
)((Diff
& 0xFF00L
) >> 8), ctrl
);
947 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
949 /* Bus reset has completed */
951 writeb(reset
, ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
952 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
953 wake_up_interruptible(&ctrl
->queue
);
957 up(&event_semaphore
);
958 dbg("Signal event_semaphore\n");
965 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
966 * @busnumber - bus where new node is to be located
968 * Returns pointer to the new node or NULL if unsuccessful
970 struct pci_func
*cpqhp_slot_create(u8 busnumber
)
972 struct pci_func
*new_slot
;
973 struct pci_func
*next
;
975 new_slot
= kmalloc(sizeof(*new_slot
), GFP_KERNEL
);
977 if (new_slot
== NULL
) {
983 memset(new_slot
, 0, sizeof(struct pci_func
));
985 new_slot
->next
= NULL
;
986 new_slot
->configured
= 1;
988 if (cpqhp_slot_list
[busnumber
] == NULL
) {
989 cpqhp_slot_list
[busnumber
] = new_slot
;
991 next
= cpqhp_slot_list
[busnumber
];
992 while (next
->next
!= NULL
)
994 next
->next
= new_slot
;
1001 * slot_remove - Removes a node from the linked list of slots.
1002 * @old_slot: slot to remove
1004 * Returns 0 if successful, !0 otherwise.
1006 static int slot_remove(struct pci_func
* old_slot
)
1008 struct pci_func
*next
;
1010 if (old_slot
== NULL
)
1013 next
= cpqhp_slot_list
[old_slot
->bus
];
1019 if (next
== old_slot
) {
1020 cpqhp_slot_list
[old_slot
->bus
] = old_slot
->next
;
1021 cpqhp_destroy_board_resources(old_slot
);
1026 while ((next
->next
!= old_slot
) && (next
->next
!= NULL
)) {
1030 if (next
->next
== old_slot
) {
1031 next
->next
= old_slot
->next
;
1032 cpqhp_destroy_board_resources(old_slot
);
1041 * bridge_slot_remove - Removes a node from the linked list of slots.
1042 * @bridge: bridge to remove
1044 * Returns 0 if successful, !0 otherwise.
1046 static int bridge_slot_remove(struct pci_func
*bridge
)
1048 u8 subordinateBus
, secondaryBus
;
1050 struct pci_func
*next
;
1052 secondaryBus
= (bridge
->config_space
[0x06] >> 8) & 0xFF;
1053 subordinateBus
= (bridge
->config_space
[0x06] >> 16) & 0xFF;
1055 for (tempBus
= secondaryBus
; tempBus
<= subordinateBus
; tempBus
++) {
1056 next
= cpqhp_slot_list
[tempBus
];
1058 while (!slot_remove(next
)) {
1059 next
= cpqhp_slot_list
[tempBus
];
1063 next
= cpqhp_slot_list
[bridge
->bus
];
1068 if (next
== bridge
) {
1069 cpqhp_slot_list
[bridge
->bus
] = bridge
->next
;
1073 while ((next
->next
!= bridge
) && (next
->next
!= NULL
))
1076 if (next
->next
!= bridge
)
1078 next
->next
= bridge
->next
;
1086 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1088 * @device: device to find
1089 * @index: is 0 for first function found, 1 for the second...
1091 * Returns pointer to the node if successful, %NULL otherwise.
1093 struct pci_func
*cpqhp_slot_find(u8 bus
, u8 device
, u8 index
)
1096 struct pci_func
*func
;
1098 func
= cpqhp_slot_list
[bus
];
1100 if ((func
== NULL
) || ((func
->device
== device
) && (index
== 0)))
1103 if (func
->device
== device
)
1106 while (func
->next
!= NULL
) {
1109 if (func
->device
== device
)
1120 /* DJZ: I don't think is_bridge will work as is.
1122 static int is_bridge(struct pci_func
* func
)
1124 /* Check the header type */
1125 if (((func
->config_space
[0x03] >> 16) & 0xFF) == 0x01)
1133 * set_controller_speed - set the frequency and/or mode of a specific
1134 * controller segment.
1136 * @ctrl: controller to change frequency/mode for.
1137 * @adapter_speed: the speed of the adapter we want to match.
1138 * @hp_slot: the slot number where the adapter is installed.
1140 * Returns 0 if we successfully change frequency and/or mode to match the
1144 static u8
set_controller_speed(struct controller
*ctrl
, u8 adapter_speed
, u8 hp_slot
)
1148 u8 slot_power
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1150 u32 leds
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
1152 if (ctrl
->speed
== adapter_speed
)
1155 /* We don't allow freq/mode changes if we find another adapter running
1156 * in another slot on this controller */
1157 for(slot
= ctrl
->slot
; slot
; slot
= slot
->next
) {
1158 if (slot
->device
== (hp_slot
+ ctrl
->slot_device_offset
))
1160 if (!slot
->hotplug_slot
&& !slot
->hotplug_slot
->info
)
1162 if (slot
->hotplug_slot
->info
->adapter_status
== 0)
1164 /* If another adapter is running on the same segment but at a
1165 * lower speed/mode, we allow the new adapter to function at
1166 * this rate if supported */
1167 if (ctrl
->speed
< adapter_speed
)
1173 /* If the controller doesn't support freq/mode changes and the
1174 * controller is running at a higher mode, we bail */
1175 if ((ctrl
->speed
> adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1178 /* But we allow the adapter to run at a lower rate if possible */
1179 if ((ctrl
->speed
< adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1182 /* We try to set the max speed supported by both the adapter and
1184 if (ctrl
->speed_capability
< adapter_speed
) {
1185 if (ctrl
->speed
== ctrl
->speed_capability
)
1187 adapter_speed
= ctrl
->speed_capability
;
1190 writel(0x0L
, ctrl
->hpc_reg
+ LED_CONTROL
);
1191 writeb(0x00, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1194 wait_for_ctrl_irq(ctrl
);
1196 if (adapter_speed
!= PCI_SPEED_133MHz_PCIX
)
1200 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1202 reg16
= readw(ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1204 switch(adapter_speed
) {
1205 case(PCI_SPEED_133MHz_PCIX
):
1209 case(PCI_SPEED_100MHz_PCIX
):
1213 case(PCI_SPEED_66MHz_PCIX
):
1217 case(PCI_SPEED_66MHz
):
1221 default: /* 33MHz PCI 2.2 */
1227 writew(reg16
, ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1231 /* Reenable interrupts */
1232 writel(0, ctrl
->hpc_reg
+ INT_MASK
);
1234 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1236 /* Restart state machine */
1238 pci_read_config_byte(ctrl
->pci_dev
, 0x43, ®
);
1239 pci_write_config_byte(ctrl
->pci_dev
, 0x43, reg
);
1241 /* Only if mode change...*/
1242 if (((ctrl
->speed
== PCI_SPEED_66MHz
) && (adapter_speed
== PCI_SPEED_66MHz_PCIX
)) ||
1243 ((ctrl
->speed
== PCI_SPEED_66MHz_PCIX
) && (adapter_speed
== PCI_SPEED_66MHz
)))
1246 wait_for_ctrl_irq(ctrl
);
1249 /* Restore LED/Slot state */
1250 writel(leds
, ctrl
->hpc_reg
+ LED_CONTROL
);
1251 writeb(slot_power
, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1254 wait_for_ctrl_irq(ctrl
);
1256 ctrl
->speed
= adapter_speed
;
1257 slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1259 info("Successfully changed frequency/mode for adapter in slot %d\n",
1264 /* the following routines constitute the bulk of the
1265 hotplug controller logic
1270 * board_replaced - Called after a board has been replaced in the system.
1272 * This is only used if we don't have resources for hot add
1273 * Turns power on for the board
1274 * Checks to see if board is the same
1275 * If board is same, reconfigures it
1276 * If board isn't same, turns it back off.
1279 static u32
board_replaced(struct pci_func
*func
, struct controller
*ctrl
)
1286 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1288 if (readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) & (0x01L
<< hp_slot
)) {
1289 /**********************************
1290 * The switch is open.
1291 **********************************/
1292 rc
= INTERLOCK_OPEN
;
1293 } else if (is_slot_enabled (ctrl
, hp_slot
)) {
1294 /**********************************
1295 * The board is already on
1296 **********************************/
1297 rc
= CARD_FUNCTIONING
;
1299 mutex_lock(&ctrl
->crit_sect
);
1301 /* turn on board without attaching to the bus */
1302 enable_slot_power (ctrl
, hp_slot
);
1306 /* Wait for SOBS to be unset */
1307 wait_for_ctrl_irq (ctrl
);
1309 /* Change bits in slot power register to force another shift out
1310 * NOTE: this is to work around the timer bug */
1311 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1312 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1313 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1317 /* Wait for SOBS to be unset */
1318 wait_for_ctrl_irq (ctrl
);
1320 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1321 if (ctrl
->speed
!= adapter_speed
)
1322 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1323 rc
= WRONG_BUS_FREQUENCY
;
1325 /* turn off board without attaching to the bus */
1326 disable_slot_power (ctrl
, hp_slot
);
1330 /* Wait for SOBS to be unset */
1331 wait_for_ctrl_irq (ctrl
);
1333 mutex_unlock(&ctrl
->crit_sect
);
1338 mutex_lock(&ctrl
->crit_sect
);
1340 slot_enable (ctrl
, hp_slot
);
1341 green_LED_blink (ctrl
, hp_slot
);
1343 amber_LED_off (ctrl
, hp_slot
);
1347 /* Wait for SOBS to be unset */
1348 wait_for_ctrl_irq (ctrl
);
1350 mutex_unlock(&ctrl
->crit_sect
);
1352 /* Wait for ~1 second because of hot plug spec */
1355 /* Check for a power fault */
1356 if (func
->status
== 0xFF) {
1357 /* power fault occurred, but it was benign */
1361 rc
= cpqhp_valid_replace(ctrl
, func
);
1364 /* It must be the same board */
1366 rc
= cpqhp_configure_board(ctrl
, func
);
1368 /* If configuration fails, turn it off
1369 * Get slot won't work for devices behind
1370 * bridges, but in this case it will always be
1371 * called for the "base" bus/dev/func of an
1374 mutex_lock(&ctrl
->crit_sect
);
1376 amber_LED_on (ctrl
, hp_slot
);
1377 green_LED_off (ctrl
, hp_slot
);
1378 slot_disable (ctrl
, hp_slot
);
1382 /* Wait for SOBS to be unset */
1383 wait_for_ctrl_irq (ctrl
);
1385 mutex_unlock(&ctrl
->crit_sect
);
1393 /* Something is wrong
1395 * Get slot won't work for devices behind bridges, but
1396 * in this case it will always be called for the "base"
1397 * bus/dev/func of an adapter. */
1399 mutex_lock(&ctrl
->crit_sect
);
1401 amber_LED_on (ctrl
, hp_slot
);
1402 green_LED_off (ctrl
, hp_slot
);
1403 slot_disable (ctrl
, hp_slot
);
1407 /* Wait for SOBS to be unset */
1408 wait_for_ctrl_irq (ctrl
);
1410 mutex_unlock(&ctrl
->crit_sect
);
1420 * board_added - Called after a board has been added to the system.
1422 * Turns power on for the board
1426 static u32
board_added(struct pci_func
*func
, struct controller
*ctrl
)
1432 u32 temp_register
= 0xFFFFFFFF;
1434 struct pci_func
*new_slot
= NULL
;
1435 struct slot
*p_slot
;
1436 struct resource_lists res_lists
;
1438 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1439 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1440 __FUNCTION__
, func
->device
, ctrl
->slot_device_offset
, hp_slot
);
1442 mutex_lock(&ctrl
->crit_sect
);
1444 /* turn on board without attaching to the bus */
1445 enable_slot_power(ctrl
, hp_slot
);
1449 /* Wait for SOBS to be unset */
1450 wait_for_ctrl_irq (ctrl
);
1452 /* Change bits in slot power register to force another shift out
1453 * NOTE: this is to work around the timer bug */
1454 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1455 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1456 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1460 /* Wait for SOBS to be unset */
1461 wait_for_ctrl_irq (ctrl
);
1463 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1464 if (ctrl
->speed
!= adapter_speed
)
1465 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1466 rc
= WRONG_BUS_FREQUENCY
;
1468 /* turn off board without attaching to the bus */
1469 disable_slot_power (ctrl
, hp_slot
);
1473 /* Wait for SOBS to be unset */
1474 wait_for_ctrl_irq(ctrl
);
1476 mutex_unlock(&ctrl
->crit_sect
);
1481 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1483 /* turn on board and blink green LED */
1485 dbg("%s: before down\n", __FUNCTION__
);
1486 mutex_lock(&ctrl
->crit_sect
);
1487 dbg("%s: after down\n", __FUNCTION__
);
1489 dbg("%s: before slot_enable\n", __FUNCTION__
);
1490 slot_enable (ctrl
, hp_slot
);
1492 dbg("%s: before green_LED_blink\n", __FUNCTION__
);
1493 green_LED_blink (ctrl
, hp_slot
);
1495 dbg("%s: before amber_LED_blink\n", __FUNCTION__
);
1496 amber_LED_off (ctrl
, hp_slot
);
1498 dbg("%s: before set_SOGO\n", __FUNCTION__
);
1501 /* Wait for SOBS to be unset */
1502 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__
);
1503 wait_for_ctrl_irq (ctrl
);
1504 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__
);
1506 dbg("%s: before up\n", __FUNCTION__
);
1507 mutex_unlock(&ctrl
->crit_sect
);
1508 dbg("%s: after up\n", __FUNCTION__
);
1510 /* Wait for ~1 second because of hot plug spec */
1511 dbg("%s: before long_delay\n", __FUNCTION__
);
1513 dbg("%s: after long_delay\n", __FUNCTION__
);
1515 dbg("%s: func status = %x\n", __FUNCTION__
, func
->status
);
1516 /* Check for a power fault */
1517 if (func
->status
== 0xFF) {
1518 /* power fault occurred, but it was benign */
1519 temp_register
= 0xFFFFFFFF;
1520 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__
, temp_register
);
1524 /* Get vendor/device ID u32 */
1525 ctrl
->pci_bus
->number
= func
->bus
;
1526 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), PCI_VENDOR_ID
, &temp_register
);
1527 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__
, rc
);
1528 dbg("%s: temp_register is %x\n", __FUNCTION__
, temp_register
);
1531 /* Something's wrong here */
1532 temp_register
= 0xFFFFFFFF;
1533 dbg("%s: temp register set to %x by error\n", __FUNCTION__
, temp_register
);
1535 /* Preset return code. It will be changed later if things go okay. */
1536 rc
= NO_ADAPTER_PRESENT
;
1539 /* All F's is an empty slot or an invalid board */
1540 if (temp_register
!= 0xFFFFFFFF) { /* Check for a board in the slot */
1541 res_lists
.io_head
= ctrl
->io_head
;
1542 res_lists
.mem_head
= ctrl
->mem_head
;
1543 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1544 res_lists
.bus_head
= ctrl
->bus_head
;
1545 res_lists
.irqs
= NULL
;
1547 rc
= configure_new_device(ctrl
, func
, 0, &res_lists
);
1549 dbg("%s: back from configure_new_device\n", __FUNCTION__
);
1550 ctrl
->io_head
= res_lists
.io_head
;
1551 ctrl
->mem_head
= res_lists
.mem_head
;
1552 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1553 ctrl
->bus_head
= res_lists
.bus_head
;
1555 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1556 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1557 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1558 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1561 mutex_lock(&ctrl
->crit_sect
);
1563 amber_LED_on (ctrl
, hp_slot
);
1564 green_LED_off (ctrl
, hp_slot
);
1565 slot_disable (ctrl
, hp_slot
);
1569 /* Wait for SOBS to be unset */
1570 wait_for_ctrl_irq (ctrl
);
1572 mutex_unlock(&ctrl
->crit_sect
);
1575 cpqhp_save_slot_config(ctrl
, func
);
1580 func
->switch_save
= 0x10;
1581 func
->is_a_board
= 0x01;
1583 /* next, we will instantiate the linux pci_dev structures (with
1584 * appropriate driver notification, if already present) */
1585 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__
);
1588 new_slot
= cpqhp_slot_find(ctrl
->bus
, func
->device
, index
++);
1589 if (new_slot
&& !new_slot
->pci_dev
) {
1590 cpqhp_configure_device(ctrl
, new_slot
);
1594 mutex_lock(&ctrl
->crit_sect
);
1596 green_LED_on (ctrl
, hp_slot
);
1600 /* Wait for SOBS to be unset */
1601 wait_for_ctrl_irq (ctrl
);
1603 mutex_unlock(&ctrl
->crit_sect
);
1605 mutex_lock(&ctrl
->crit_sect
);
1607 amber_LED_on (ctrl
, hp_slot
);
1608 green_LED_off (ctrl
, hp_slot
);
1609 slot_disable (ctrl
, hp_slot
);
1613 /* Wait for SOBS to be unset */
1614 wait_for_ctrl_irq (ctrl
);
1616 mutex_unlock(&ctrl
->crit_sect
);
1625 * remove_board - Turns off slot and LED's
1628 static u32
remove_board(struct pci_func
* func
, u32 replace_flag
, struct controller
* ctrl
)
1636 struct resource_lists res_lists
;
1637 struct pci_func
*temp_func
;
1639 if (cpqhp_unconfigure_device(func
))
1642 device
= func
->device
;
1644 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1645 dbg("In %s, hp_slot = %d\n", __FUNCTION__
, hp_slot
);
1647 /* When we get here, it is safe to change base address registers.
1648 * We will attempt to save the base address register lengths */
1649 if (replace_flag
|| !ctrl
->add_support
)
1650 rc
= cpqhp_save_base_addr_length(ctrl
, func
);
1651 else if (!func
->bus_head
&& !func
->mem_head
&&
1652 !func
->p_mem_head
&& !func
->io_head
) {
1653 /* Here we check to see if we've saved any of the board's
1654 * resources already. If so, we'll skip the attempt to
1655 * determine what's being used. */
1657 temp_func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1659 if (temp_func
->bus_head
|| temp_func
->mem_head
1660 || temp_func
->p_mem_head
|| temp_func
->io_head
) {
1664 temp_func
= cpqhp_slot_find(temp_func
->bus
, temp_func
->device
, index
++);
1668 rc
= cpqhp_save_used_resources(ctrl
, func
);
1670 /* Change status to shutdown */
1671 if (func
->is_a_board
)
1672 func
->status
= 0x01;
1673 func
->configured
= 0;
1675 mutex_lock(&ctrl
->crit_sect
);
1677 green_LED_off (ctrl
, hp_slot
);
1678 slot_disable (ctrl
, hp_slot
);
1682 /* turn off SERR for slot */
1683 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_SERR
);
1684 temp_byte
&= ~(0x01 << hp_slot
);
1685 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_SERR
);
1687 /* Wait for SOBS to be unset */
1688 wait_for_ctrl_irq (ctrl
);
1690 mutex_unlock(&ctrl
->crit_sect
);
1692 if (!replace_flag
&& ctrl
->add_support
) {
1694 res_lists
.io_head
= ctrl
->io_head
;
1695 res_lists
.mem_head
= ctrl
->mem_head
;
1696 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1697 res_lists
.bus_head
= ctrl
->bus_head
;
1699 cpqhp_return_board_resources(func
, &res_lists
);
1701 ctrl
->io_head
= res_lists
.io_head
;
1702 ctrl
->mem_head
= res_lists
.mem_head
;
1703 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1704 ctrl
->bus_head
= res_lists
.bus_head
;
1706 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1707 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1708 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1709 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1711 if (is_bridge(func
)) {
1712 bridge_slot_remove(func
);
1716 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
1719 /* Setup slot structure with entry for empty slot */
1720 func
= cpqhp_slot_create(ctrl
->bus
);
1725 func
->bus
= ctrl
->bus
;
1726 func
->device
= device
;
1728 func
->configured
= 0;
1729 func
->switch_save
= 0x10;
1730 func
->is_a_board
= 0;
1731 func
->p_task_event
= NULL
;
1737 static void pushbutton_helper_thread(unsigned long data
)
1739 pushbutton_pending
= data
;
1740 up(&event_semaphore
);
1744 /* this is the main worker thread */
1745 static int event_thread(void* data
)
1747 struct controller
*ctrl
;
1749 daemonize("phpd_event");
1754 dbg("!!!!event_thread sleeping\n");
1755 down_interruptible (&event_semaphore
);
1756 dbg("event_thread woken finished = %d\n", event_finished
);
1757 if (event_finished
) break;
1759 if (pushbutton_pending
)
1760 cpqhp_pushbutton_thread(pushbutton_pending
);
1762 for (ctrl
= cpqhp_ctrl_list
; ctrl
; ctrl
=ctrl
->next
)
1763 interrupt_event_handler(ctrl
);
1765 dbg("event_thread signals exit\n");
1771 int cpqhp_event_start_thread(void)
1775 /* initialize our semaphores */
1776 init_MUTEX(&delay_sem
);
1777 init_MUTEX_LOCKED(&event_semaphore
);
1778 init_MUTEX_LOCKED(&event_exit
);
1781 pid
= kernel_thread(event_thread
, NULL
, 0);
1783 err ("Can't start up our event thread\n");
1786 dbg("Our event thread pid = %d\n", pid
);
1791 void cpqhp_event_stop_thread(void)
1794 dbg("event_thread finish command given\n");
1795 up(&event_semaphore
);
1796 dbg("wait for event_thread to exit\n");
1801 static int update_slot_info(struct controller
*ctrl
, struct slot
*slot
)
1803 struct hotplug_slot_info
*info
;
1806 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
1810 info
->power_status
= get_slot_enabled(ctrl
, slot
);
1811 info
->attention_status
= cpq_get_attention_status(ctrl
, slot
);
1812 info
->latch_status
= cpq_get_latch_status(ctrl
, slot
);
1813 info
->adapter_status
= get_presence_status(ctrl
, slot
);
1814 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
1819 static void interrupt_event_handler(struct controller
*ctrl
)
1823 struct pci_func
*func
;
1825 struct slot
*p_slot
;
1830 for (loop
= 0; loop
< 10; loop
++) {
1831 /* dbg("loop %d\n", loop); */
1832 if (ctrl
->event_queue
[loop
].event_type
!= 0) {
1833 hp_slot
= ctrl
->event_queue
[loop
].hp_slot
;
1835 func
= cpqhp_slot_find(ctrl
->bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
1839 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1843 dbg("hp_slot %d, func %p, p_slot %p\n",
1844 hp_slot
, func
, p_slot
);
1846 if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_PRESS
) {
1847 dbg("button pressed\n");
1848 } else if (ctrl
->event_queue
[loop
].event_type
==
1849 INT_BUTTON_CANCEL
) {
1850 dbg("button cancel\n");
1851 del_timer(&p_slot
->task_event
);
1853 mutex_lock(&ctrl
->crit_sect
);
1855 if (p_slot
->state
== BLINKINGOFF_STATE
) {
1857 dbg("turn on green LED\n");
1858 green_LED_on (ctrl
, hp_slot
);
1859 } else if (p_slot
->state
== BLINKINGON_STATE
) {
1861 dbg("turn off green LED\n");
1862 green_LED_off (ctrl
, hp_slot
);
1865 info(msg_button_cancel
, p_slot
->number
);
1867 p_slot
->state
= STATIC_STATE
;
1869 amber_LED_off (ctrl
, hp_slot
);
1873 /* Wait for SOBS to be unset */
1874 wait_for_ctrl_irq (ctrl
);
1876 mutex_unlock(&ctrl
->crit_sect
);
1878 /*** button Released (No action on press...) */
1879 else if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_RELEASE
) {
1880 dbg("button release\n");
1882 if (is_slot_enabled (ctrl
, hp_slot
)) {
1883 dbg("slot is on\n");
1884 p_slot
->state
= BLINKINGOFF_STATE
;
1885 info(msg_button_off
, p_slot
->number
);
1887 dbg("slot is off\n");
1888 p_slot
->state
= BLINKINGON_STATE
;
1889 info(msg_button_on
, p_slot
->number
);
1891 mutex_lock(&ctrl
->crit_sect
);
1893 dbg("blink green LED and turn off amber\n");
1895 amber_LED_off (ctrl
, hp_slot
);
1896 green_LED_blink (ctrl
, hp_slot
);
1900 /* Wait for SOBS to be unset */
1901 wait_for_ctrl_irq (ctrl
);
1903 mutex_unlock(&ctrl
->crit_sect
);
1904 init_timer(&p_slot
->task_event
);
1905 p_slot
->hp_slot
= hp_slot
;
1906 p_slot
->ctrl
= ctrl
;
1907 /* p_slot->physical_slot = physical_slot; */
1908 p_slot
->task_event
.expires
= jiffies
+ 5 * HZ
; /* 5 second delay */
1909 p_slot
->task_event
.function
= pushbutton_helper_thread
;
1910 p_slot
->task_event
.data
= (u32
) p_slot
;
1912 dbg("add_timer p_slot = %p\n", p_slot
);
1913 add_timer(&p_slot
->task_event
);
1915 /***********POWER FAULT */
1916 else if (ctrl
->event_queue
[loop
].event_type
== INT_POWER_FAULT
) {
1917 dbg("power fault\n");
1919 /* refresh notification */
1921 update_slot_info(ctrl
, p_slot
);
1924 ctrl
->event_queue
[loop
].event_type
= 0;
1928 } /* End of FOR loop */
1936 * cpqhp_pushbutton_thread
1938 * Scheduled procedure to handle blocking stuff for the pushbuttons
1939 * Handles all pending events and exits.
1942 void cpqhp_pushbutton_thread(unsigned long slot
)
1946 struct pci_func
*func
;
1947 struct slot
*p_slot
= (struct slot
*) slot
;
1948 struct controller
*ctrl
= (struct controller
*) p_slot
->ctrl
;
1950 pushbutton_pending
= 0;
1951 hp_slot
= p_slot
->hp_slot
;
1953 device
= p_slot
->device
;
1955 if (is_slot_enabled(ctrl
, hp_slot
)) {
1956 p_slot
->state
= POWEROFF_STATE
;
1957 /* power Down board */
1958 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1959 dbg("In power_down_board, func = %p, ctrl = %p\n", func
, ctrl
);
1961 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1965 if (func
!= NULL
&& ctrl
!= NULL
) {
1966 if (cpqhp_process_SS(ctrl
, func
) != 0) {
1967 amber_LED_on (ctrl
, hp_slot
);
1968 green_LED_on (ctrl
, hp_slot
);
1972 /* Wait for SOBS to be unset */
1973 wait_for_ctrl_irq (ctrl
);
1977 p_slot
->state
= STATIC_STATE
;
1979 p_slot
->state
= POWERON_STATE
;
1982 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1983 dbg("In add_board, func = %p, ctrl = %p\n", func
, ctrl
);
1985 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1989 if (func
!= NULL
&& ctrl
!= NULL
) {
1990 if (cpqhp_process_SI(ctrl
, func
) != 0) {
1991 amber_LED_on(ctrl
, hp_slot
);
1992 green_LED_off(ctrl
, hp_slot
);
1996 /* Wait for SOBS to be unset */
1997 wait_for_ctrl_irq (ctrl
);
2001 p_slot
->state
= STATIC_STATE
;
2008 int cpqhp_process_SI(struct controller
*ctrl
, struct pci_func
*func
)
2014 struct slot
* p_slot
;
2015 int physical_slot
= 0;
2019 device
= func
->device
;
2020 hp_slot
= device
- ctrl
->slot_device_offset
;
2021 p_slot
= cpqhp_find_slot(ctrl
, device
);
2023 physical_slot
= p_slot
->number
;
2025 /* Check to see if the interlock is closed */
2026 tempdword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
2028 if (tempdword
& (0x01 << hp_slot
)) {
2032 if (func
->is_a_board
) {
2033 rc
= board_replaced(func
, ctrl
);
2038 func
= cpqhp_slot_create(ctrl
->bus
);
2042 func
->bus
= ctrl
->bus
;
2043 func
->device
= device
;
2045 func
->configured
= 0;
2046 func
->is_a_board
= 1;
2048 /* We have to save the presence info for these slots */
2049 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2050 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2051 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
2053 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2054 func
->switch_save
= 0;
2056 func
->switch_save
= 0x10;
2059 rc
= board_added(func
, ctrl
);
2061 if (is_bridge(func
)) {
2062 bridge_slot_remove(func
);
2066 /* Setup slot structure with entry for empty slot */
2067 func
= cpqhp_slot_create(ctrl
->bus
);
2072 func
->bus
= ctrl
->bus
;
2073 func
->device
= device
;
2075 func
->configured
= 0;
2076 func
->is_a_board
= 0;
2078 /* We have to save the presence info for these slots */
2079 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2080 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2081 func
->presence_save
|=
2082 (temp_word
>> (hp_slot
+ 7)) & 0x02;
2084 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2085 func
->switch_save
= 0;
2087 func
->switch_save
= 0x10;
2093 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2097 update_slot_info(ctrl
, p_slot
);
2103 int cpqhp_process_SS(struct controller
*ctrl
, struct pci_func
*func
)
2105 u8 device
, class_code
, header_type
, BCR
;
2110 struct slot
* p_slot
;
2111 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
2112 int physical_slot
=0;
2114 device
= func
->device
;
2115 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2116 p_slot
= cpqhp_find_slot(ctrl
, device
);
2118 physical_slot
= p_slot
->number
;
2121 /* Make sure there are no video controllers here */
2122 while (func
&& !rc
) {
2123 pci_bus
->number
= func
->bus
;
2124 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2126 /* Check the Class Code */
2127 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2131 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2132 /* Display/Video adapter (not supported) */
2133 rc
= REMOVE_NOT_SUPPORTED
;
2135 /* See if it's a bridge */
2136 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
2140 /* If it's a bridge, check the VGA Enable bit */
2141 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
2142 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, &BCR
);
2146 /* If the VGA Enable bit is set, remove isn't
2148 if (BCR
& PCI_BRIDGE_CTL_VGA
) {
2149 rc
= REMOVE_NOT_SUPPORTED
;
2154 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2157 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
2158 if ((func
!= NULL
) && !rc
) {
2159 /* FIXME: Replace flag should be passed into process_SS */
2160 replace_flag
= !(ctrl
->add_support
);
2161 rc
= remove_board(func
, replace_flag
, ctrl
);
2167 update_slot_info(ctrl
, p_slot
);
2173 * switch_leds: switch the leds, go from one site to the other.
2174 * @ctrl: controller to use
2175 * @num_of_slots: number of slots to use
2176 * @direction: 1 to start from the left side, 0 to start right.
2178 static void switch_leds(struct controller
*ctrl
, const int num_of_slots
,
2179 u32
*work_LED
, const int direction
)
2183 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2185 *work_LED
= *work_LED
>> 1;
2187 *work_LED
= *work_LED
<< 1;
2188 writel(*work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2192 /* Wait for SOGO interrupt */
2193 wait_for_ctrl_irq(ctrl
);
2195 /* Get ready for next iteration */
2196 long_delay((2*HZ
)/10);
2201 * hardware_test - runs hardware tests
2203 * For hot plug ctrl folks to play with.
2204 * test_num is the number written to the "test" file in sysfs
2207 int cpqhp_hardware_test(struct controller
*ctrl
, int test_num
)
2214 num_of_slots
= readb(ctrl
->hpc_reg
+ SLOT_MASK
) & 0x0f;
2218 /* Do stuff here! */
2220 /* Do that funky LED thing */
2221 /* so we can restore them later */
2222 save_LED
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
2223 work_LED
= 0x01010101;
2224 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2225 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2226 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2227 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2229 work_LED
= 0x01010000;
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);
2233 work_LED
= 0x00000101;
2234 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2235 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2236 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2238 work_LED
= 0x01010000;
2239 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2240 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2243 /* Wait for SOGO interrupt */
2244 wait_for_ctrl_irq (ctrl
);
2246 /* Get ready for next iteration */
2247 long_delay((3*HZ
)/10);
2248 work_LED
= work_LED
>> 16;
2249 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2253 /* Wait for SOGO interrupt */
2254 wait_for_ctrl_irq (ctrl
);
2256 /* Get ready for next iteration */
2257 long_delay((3*HZ
)/10);
2258 work_LED
= work_LED
<< 16;
2259 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2260 work_LED
= work_LED
<< 1;
2261 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2264 /* put it back the way it was */
2265 writel(save_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2269 /* Wait for SOBS to be unset */
2270 wait_for_ctrl_irq (ctrl
);
2273 /* Do other stuff here! */
2284 * configure_new_device - Configures the PCI header information of one board.
2286 * @ctrl: pointer to controller structure
2287 * @func: pointer to function structure
2288 * @behind_bridge: 1 if this is a recursive call, 0 if not
2289 * @resources: pointer to set of resource lists
2291 * Returns 0 if success
2294 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
* func
,
2295 u8 behind_bridge
, struct resource_lists
* resources
)
2297 u8 temp_byte
, function
, max_functions
, stop_it
;
2300 struct pci_func
*new_slot
;
2305 dbg("%s\n", __FUNCTION__
);
2306 /* Check for Multi-function device */
2307 ctrl
->pci_bus
->number
= func
->bus
;
2308 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), 0x0E, &temp_byte
);
2310 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2314 if (temp_byte
& 0x80) /* Multi-function device */
2322 rc
= configure_new_function(ctrl
, new_slot
, behind_bridge
, resources
);
2325 dbg("configure_new_function failed %d\n",rc
);
2329 new_slot
= cpqhp_slot_find(new_slot
->bus
, new_slot
->device
, index
++);
2332 cpqhp_return_board_resources(new_slot
, resources
);
2342 /* The following loop skips to the next present function
2343 * and creates a board structure */
2345 while ((function
< max_functions
) && (!stop_it
)) {
2346 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, function
), 0x00, &ID
);
2348 if (ID
== 0xFFFFFFFF) { /* There's nothing there. */
2350 } else { /* There's something there */
2351 /* Setup slot structure. */
2352 new_slot
= cpqhp_slot_create(func
->bus
);
2354 if (new_slot
== NULL
)
2357 new_slot
->bus
= func
->bus
;
2358 new_slot
->device
= func
->device
;
2359 new_slot
->function
= function
;
2360 new_slot
->is_a_board
= 1;
2361 new_slot
->status
= 0;
2367 } while (function
< max_functions
);
2368 dbg("returning from configure_new_device\n");
2375 Configuration logic that involves the hotplug data structures and
2381 * configure_new_function - Configures the PCI header information of one device
2383 * @ctrl: pointer to controller structure
2384 * @func: pointer to function structure
2385 * @behind_bridge: 1 if this is a recursive call, 0 if not
2386 * @resources: pointer to set of resource lists
2388 * Calls itself recursively for bridged devices.
2389 * Returns 0 if success
2392 static int configure_new_function(struct controller
*ctrl
, struct pci_func
*func
,
2394 struct resource_lists
*resources
)
2409 struct pci_resource
*mem_node
;
2410 struct pci_resource
*p_mem_node
;
2411 struct pci_resource
*io_node
;
2412 struct pci_resource
*bus_node
;
2413 struct pci_resource
*hold_mem_node
;
2414 struct pci_resource
*hold_p_mem_node
;
2415 struct pci_resource
*hold_IO_node
;
2416 struct pci_resource
*hold_bus_node
;
2417 struct irq_mapping irqs
;
2418 struct pci_func
*new_slot
;
2419 struct pci_bus
*pci_bus
;
2420 struct resource_lists temp_resources
;
2422 pci_bus
= ctrl
->pci_bus
;
2423 pci_bus
->number
= func
->bus
;
2424 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2426 /* Check for Bridge */
2427 rc
= pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &temp_byte
);
2431 if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) { /* PCI-PCI Bridge */
2432 /* set Primary bus */
2433 dbg("set Primary bus = %d\n", func
->bus
);
2434 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_PRIMARY_BUS
, func
->bus
);
2438 /* find range of busses to use */
2439 dbg("find ranges of buses to use\n");
2440 bus_node
= get_max_resource(&(resources
->bus_head
), 1);
2442 /* If we don't have any busses to allocate, we can't continue */
2446 /* set Secondary bus */
2447 temp_byte
= bus_node
->base
;
2448 dbg("set Secondary bus = %d\n", bus_node
->base
);
2449 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, temp_byte
);
2453 /* set subordinate bus */
2454 temp_byte
= bus_node
->base
+ bus_node
->length
- 1;
2455 dbg("set subordinate bus = %d\n", bus_node
->base
+ bus_node
->length
- 1);
2456 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2460 /* set subordinate Latency Timer and base Latency Timer */
2462 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SEC_LATENCY_TIMER
, temp_byte
);
2465 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_LATENCY_TIMER
, temp_byte
);
2469 /* set Cache Line size */
2471 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, temp_byte
);
2475 /* Setup the IO, memory, and prefetchable windows */
2476 io_node
= get_max_resource(&(resources
->io_head
), 0x1000);
2479 mem_node
= get_max_resource(&(resources
->mem_head
), 0x100000);
2482 p_mem_node
= get_max_resource(&(resources
->p_mem_head
), 0x100000);
2485 dbg("Setup the IO, memory, and prefetchable windows\n");
2487 dbg("(base, len, next) (%x, %x, %p)\n", io_node
->base
,
2488 io_node
->length
, io_node
->next
);
2490 dbg("(base, len, next) (%x, %x, %p)\n", mem_node
->base
,
2491 mem_node
->length
, mem_node
->next
);
2492 dbg("p_mem_node\n");
2493 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node
->base
,
2494 p_mem_node
->length
, p_mem_node
->next
);
2496 /* set up the IRQ info */
2497 if (!resources
->irqs
) {
2498 irqs
.barber_pole
= 0;
2499 irqs
.interrupt
[0] = 0;
2500 irqs
.interrupt
[1] = 0;
2501 irqs
.interrupt
[2] = 0;
2502 irqs
.interrupt
[3] = 0;
2505 irqs
.barber_pole
= resources
->irqs
->barber_pole
;
2506 irqs
.interrupt
[0] = resources
->irqs
->interrupt
[0];
2507 irqs
.interrupt
[1] = resources
->irqs
->interrupt
[1];
2508 irqs
.interrupt
[2] = resources
->irqs
->interrupt
[2];
2509 irqs
.interrupt
[3] = resources
->irqs
->interrupt
[3];
2510 irqs
.valid_INT
= resources
->irqs
->valid_INT
;
2513 /* set up resource lists that are now aligned on top and bottom
2514 * for anything behind the bridge. */
2515 temp_resources
.bus_head
= bus_node
;
2516 temp_resources
.io_head
= io_node
;
2517 temp_resources
.mem_head
= mem_node
;
2518 temp_resources
.p_mem_head
= p_mem_node
;
2519 temp_resources
.irqs
= &irqs
;
2521 /* Make copies of the nodes we are going to pass down so that
2522 * if there is a problem,we can just use these to free resources */
2523 hold_bus_node
= kmalloc(sizeof(*hold_bus_node
), GFP_KERNEL
);
2524 hold_IO_node
= kmalloc(sizeof(*hold_IO_node
), GFP_KERNEL
);
2525 hold_mem_node
= kmalloc(sizeof(*hold_mem_node
), GFP_KERNEL
);
2526 hold_p_mem_node
= kmalloc(sizeof(*hold_p_mem_node
), GFP_KERNEL
);
2528 if (!hold_bus_node
|| !hold_IO_node
|| !hold_mem_node
|| !hold_p_mem_node
) {
2529 kfree(hold_bus_node
);
2530 kfree(hold_IO_node
);
2531 kfree(hold_mem_node
);
2532 kfree(hold_p_mem_node
);
2537 memcpy(hold_bus_node
, bus_node
, sizeof(struct pci_resource
));
2539 bus_node
->base
+= 1;
2540 bus_node
->length
-= 1;
2541 bus_node
->next
= NULL
;
2543 /* If we have IO resources copy them and fill in the bridge's
2544 * IO range registers */
2546 memcpy(hold_IO_node
, io_node
, sizeof(struct pci_resource
));
2547 io_node
->next
= NULL
;
2549 /* set IO base and Limit registers */
2550 temp_byte
= io_node
->base
>> 8;
2551 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2553 temp_byte
= (io_node
->base
+ io_node
->length
- 1) >> 8;
2554 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2556 kfree(hold_IO_node
);
2557 hold_IO_node
= NULL
;
2560 /* If we have memory resources copy them and fill in the
2561 * bridge's memory range registers. Otherwise, fill in the
2562 * range registers with values that disable them. */
2564 memcpy(hold_mem_node
, mem_node
, sizeof(struct pci_resource
));
2565 mem_node
->next
= NULL
;
2567 /* set Mem base and Limit registers */
2568 temp_word
= mem_node
->base
>> 16;
2569 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2571 temp_word
= (mem_node
->base
+ mem_node
->length
- 1) >> 16;
2572 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2575 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2578 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2580 kfree(hold_mem_node
);
2581 hold_mem_node
= NULL
;
2584 memcpy(hold_p_mem_node
, p_mem_node
, sizeof(struct pci_resource
));
2585 p_mem_node
->next
= NULL
;
2587 /* set Pre Mem base and Limit registers */
2588 temp_word
= p_mem_node
->base
>> 16;
2589 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2591 temp_word
= (p_mem_node
->base
+ p_mem_node
->length
- 1) >> 16;
2592 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2594 /* Adjust this to compensate for extra adjustment in first loop */
2599 /* Here we actually find the devices and configure them */
2600 for (device
= 0; (device
<= 0x1F) && !rc
; device
++) {
2601 irqs
.barber_pole
= (irqs
.barber_pole
+ 1) & 0x03;
2604 pci_bus
->number
= hold_bus_node
->base
;
2605 pci_bus_read_config_dword (pci_bus
, PCI_DEVFN(device
, 0), 0x00, &ID
);
2606 pci_bus
->number
= func
->bus
;
2608 if (ID
!= 0xFFFFFFFF) { /* device present */
2609 /* Setup slot structure. */
2610 new_slot
= cpqhp_slot_create(hold_bus_node
->base
);
2612 if (new_slot
== NULL
) {
2617 new_slot
->bus
= hold_bus_node
->base
;
2618 new_slot
->device
= device
;
2619 new_slot
->function
= 0;
2620 new_slot
->is_a_board
= 1;
2621 new_slot
->status
= 0;
2623 rc
= configure_new_device(ctrl
, new_slot
, 1, &temp_resources
);
2624 dbg("configure_new_device rc=0x%x\n",rc
);
2625 } /* End of IF (device in slot?) */
2626 } /* End of FOR loop */
2630 /* save the interrupt routing information */
2631 if (resources
->irqs
) {
2632 resources
->irqs
->interrupt
[0] = irqs
.interrupt
[0];
2633 resources
->irqs
->interrupt
[1] = irqs
.interrupt
[1];
2634 resources
->irqs
->interrupt
[2] = irqs
.interrupt
[2];
2635 resources
->irqs
->interrupt
[3] = irqs
.interrupt
[3];
2636 resources
->irqs
->valid_INT
= irqs
.valid_INT
;
2637 } else if (!behind_bridge
) {
2638 /* We need to hook up the interrupts here */
2639 for (cloop
= 0; cloop
< 4; cloop
++) {
2640 if (irqs
.valid_INT
& (0x01 << cloop
)) {
2641 rc
= cpqhp_set_irq(func
->bus
, func
->device
,
2642 0x0A + cloop
, irqs
.interrupt
[cloop
]);
2646 } /* end of for loop */
2648 /* Return unused bus resources
2649 * First use the temporary node to store information for
2651 if (hold_bus_node
&& bus_node
&& temp_resources
.bus_head
) {
2652 hold_bus_node
->length
= bus_node
->base
- hold_bus_node
->base
;
2654 hold_bus_node
->next
= func
->bus_head
;
2655 func
->bus_head
= hold_bus_node
;
2657 temp_byte
= temp_resources
.bus_head
->base
- 1;
2659 /* set subordinate bus */
2660 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2662 if (temp_resources
.bus_head
->length
== 0) {
2663 kfree(temp_resources
.bus_head
);
2664 temp_resources
.bus_head
= NULL
;
2666 return_resource(&(resources
->bus_head
), temp_resources
.bus_head
);
2670 /* If we have IO space available and there is some left,
2671 * return the unused portion */
2672 if (hold_IO_node
&& temp_resources
.io_head
) {
2673 io_node
= do_pre_bridge_resource_split(&(temp_resources
.io_head
),
2674 &hold_IO_node
, 0x1000);
2676 /* Check if we were able to split something off */
2678 hold_IO_node
->base
= io_node
->base
+ io_node
->length
;
2680 temp_byte
= (hold_IO_node
->base
) >> 8;
2681 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2683 return_resource(&(resources
->io_head
), io_node
);
2686 io_node
= do_bridge_resource_split(&(temp_resources
.io_head
), 0x1000);
2688 /* Check if we were able to split something off */
2690 /* First use the temporary node to store
2691 * information for the board */
2692 hold_IO_node
->length
= io_node
->base
- hold_IO_node
->base
;
2694 /* If we used any, add it to the board's list */
2695 if (hold_IO_node
->length
) {
2696 hold_IO_node
->next
= func
->io_head
;
2697 func
->io_head
= hold_IO_node
;
2699 temp_byte
= (io_node
->base
- 1) >> 8;
2700 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2702 return_resource(&(resources
->io_head
), io_node
);
2704 /* it doesn't need any IO */
2706 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_word
);
2708 return_resource(&(resources
->io_head
), io_node
);
2709 kfree(hold_IO_node
);
2712 /* it used most of the range */
2713 hold_IO_node
->next
= func
->io_head
;
2714 func
->io_head
= hold_IO_node
;
2716 } else if (hold_IO_node
) {
2717 /* it used the whole range */
2718 hold_IO_node
->next
= func
->io_head
;
2719 func
->io_head
= hold_IO_node
;
2721 /* If we have memory space available and there is some left,
2722 * return the unused portion */
2723 if (hold_mem_node
&& temp_resources
.mem_head
) {
2724 mem_node
= do_pre_bridge_resource_split(&(temp_resources
. mem_head
),
2725 &hold_mem_node
, 0x100000);
2727 /* Check if we were able to split something off */
2729 hold_mem_node
->base
= mem_node
->base
+ mem_node
->length
;
2731 temp_word
= (hold_mem_node
->base
) >> 16;
2732 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2734 return_resource(&(resources
->mem_head
), mem_node
);
2737 mem_node
= do_bridge_resource_split(&(temp_resources
.mem_head
), 0x100000);
2739 /* Check if we were able to split something off */
2741 /* First use the temporary node to store
2742 * information for the board */
2743 hold_mem_node
->length
= mem_node
->base
- hold_mem_node
->base
;
2745 if (hold_mem_node
->length
) {
2746 hold_mem_node
->next
= func
->mem_head
;
2747 func
->mem_head
= hold_mem_node
;
2749 /* configure end address */
2750 temp_word
= (mem_node
->base
- 1) >> 16;
2751 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2753 /* Return unused resources to the pool */
2754 return_resource(&(resources
->mem_head
), mem_node
);
2756 /* it doesn't need any Mem */
2758 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2760 return_resource(&(resources
->mem_head
), mem_node
);
2761 kfree(hold_mem_node
);
2764 /* it used most of the range */
2765 hold_mem_node
->next
= func
->mem_head
;
2766 func
->mem_head
= hold_mem_node
;
2768 } else if (hold_mem_node
) {
2769 /* it used the whole range */
2770 hold_mem_node
->next
= func
->mem_head
;
2771 func
->mem_head
= hold_mem_node
;
2773 /* If we have prefetchable memory space available and there
2774 * is some left at the end, return the unused portion */
2775 if (hold_p_mem_node
&& temp_resources
.p_mem_head
) {
2776 p_mem_node
= do_pre_bridge_resource_split(&(temp_resources
.p_mem_head
),
2777 &hold_p_mem_node
, 0x100000);
2779 /* Check if we were able to split something off */
2781 hold_p_mem_node
->base
= p_mem_node
->base
+ p_mem_node
->length
;
2783 temp_word
= (hold_p_mem_node
->base
) >> 16;
2784 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2786 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2789 p_mem_node
= do_bridge_resource_split(&(temp_resources
.p_mem_head
), 0x100000);
2791 /* Check if we were able to split something off */
2793 /* First use the temporary node to store
2794 * information for the board */
2795 hold_p_mem_node
->length
= p_mem_node
->base
- hold_p_mem_node
->base
;
2797 /* If we used any, add it to the board's list */
2798 if (hold_p_mem_node
->length
) {
2799 hold_p_mem_node
->next
= func
->p_mem_head
;
2800 func
->p_mem_head
= hold_p_mem_node
;
2802 temp_word
= (p_mem_node
->base
- 1) >> 16;
2803 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2805 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2807 /* it doesn't need any PMem */
2809 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2811 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2812 kfree(hold_p_mem_node
);
2815 /* it used the most of the range */
2816 hold_p_mem_node
->next
= func
->p_mem_head
;
2817 func
->p_mem_head
= hold_p_mem_node
;
2819 } else if (hold_p_mem_node
) {
2820 /* it used the whole range */
2821 hold_p_mem_node
->next
= func
->p_mem_head
;
2822 func
->p_mem_head
= hold_p_mem_node
;
2824 /* We should be configuring an IRQ and the bridge's base address
2825 * registers if it needs them. Although we have never seen such
2829 command
= 0x0157; /* = PCI_COMMAND_IO |
2830 * PCI_COMMAND_MEMORY |
2831 * PCI_COMMAND_MASTER |
2832 * PCI_COMMAND_INVALIDATE |
2833 * PCI_COMMAND_PARITY |
2834 * PCI_COMMAND_SERR */
2835 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_COMMAND
, command
);
2837 /* set Bridge Control Register */
2838 command
= 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2839 * PCI_BRIDGE_CTL_SERR |
2840 * PCI_BRIDGE_CTL_NO_ISA */
2841 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
2842 } else if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
2843 /* Standard device */
2844 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2846 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2847 /* Display (video) adapter (not supported) */
2848 return DEVICE_TYPE_NOT_SUPPORTED
;
2850 /* Figure out IO and memory needs */
2851 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
2852 temp_register
= 0xFFFFFFFF;
2854 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus
->number
, devfn
, cloop
);
2855 rc
= pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
2857 rc
= pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp_register
);
2858 dbg("CND: base = 0x%x\n", temp_register
);
2860 if (temp_register
) { /* If this register is implemented */
2861 if ((temp_register
& 0x03L
) == 0x01) {
2864 /* set base = amount of IO space */
2865 base
= temp_register
& 0xFFFFFFFC;
2868 dbg("CND: length = 0x%x\n", base
);
2869 io_node
= get_io_resource(&(resources
->io_head
), base
);
2870 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2871 io_node
->base
, io_node
->length
, io_node
->next
);
2872 dbg("func (%p) io_head (%p)\n", func
, func
->io_head
);
2874 /* allocate the resource to the board */
2876 base
= io_node
->base
;
2878 io_node
->next
= func
->io_head
;
2879 func
->io_head
= io_node
;
2882 } else if ((temp_register
& 0x0BL
) == 0x08) {
2883 /* Map prefetchable memory */
2884 base
= temp_register
& 0xFFFFFFF0;
2887 dbg("CND: length = 0x%x\n", base
);
2888 p_mem_node
= get_resource(&(resources
->p_mem_head
), base
);
2890 /* allocate the resource to the board */
2892 base
= p_mem_node
->base
;
2894 p_mem_node
->next
= func
->p_mem_head
;
2895 func
->p_mem_head
= p_mem_node
;
2898 } else if ((temp_register
& 0x0BL
) == 0x00) {
2900 base
= temp_register
& 0xFFFFFFF0;
2903 dbg("CND: length = 0x%x\n", base
);
2904 mem_node
= get_resource(&(resources
->mem_head
), base
);
2906 /* allocate the resource to the board */
2908 base
= mem_node
->base
;
2910 mem_node
->next
= func
->mem_head
;
2911 func
->mem_head
= mem_node
;
2914 } else if ((temp_register
& 0x0BL
) == 0x04) {
2916 base
= temp_register
& 0xFFFFFFF0;
2919 dbg("CND: length = 0x%x\n", base
);
2920 mem_node
= get_resource(&(resources
->mem_head
), base
);
2922 /* allocate the resource to the board */
2924 base
= mem_node
->base
;
2926 mem_node
->next
= func
->mem_head
;
2927 func
->mem_head
= mem_node
;
2930 } else if ((temp_register
& 0x0BL
) == 0x06) {
2931 /* Those bits are reserved, we can't handle this */
2934 /* Requesting space below 1M */
2935 return NOT_ENOUGH_RESOURCES
;
2938 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2940 /* Check for 64-bit base */
2941 if ((temp_register
& 0x07L
) == 0x04) {
2944 /* Upper 32 bits of address always zero
2945 * on today's systems */
2946 /* FIXME this is probably not true on
2947 * Alpha and ia64??? */
2949 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2952 } /* End of base register loop */
2953 if (cpqhp_legacy_mode
) {
2954 /* Figure out which interrupt pin this function uses */
2955 rc
= pci_bus_read_config_byte (pci_bus
, devfn
,
2956 PCI_INTERRUPT_PIN
, &temp_byte
);
2958 /* If this function needs an interrupt and we are behind
2959 * a bridge and the pin is tied to something that's
2960 * alread mapped, set this one the same */
2961 if (temp_byte
&& resources
->irqs
&&
2962 (resources
->irqs
->valid_INT
&
2963 (0x01 << ((temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03)))) {
2964 /* We have to share with something already set up */
2965 IRQ
= resources
->irqs
->interrupt
[(temp_byte
+
2966 resources
->irqs
->barber_pole
- 1) & 0x03];
2968 /* Program IRQ based on card type */
2969 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2971 if (class_code
== PCI_BASE_CLASS_STORAGE
) {
2972 IRQ
= cpqhp_disk_irq
;
2974 IRQ
= cpqhp_nic_irq
;
2979 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_INTERRUPT_LINE
, IRQ
);
2982 if (!behind_bridge
) {
2983 rc
= cpqhp_set_irq(func
->bus
, func
->device
, temp_byte
+ 0x09, IRQ
);
2987 /* TBD - this code may also belong in the other clause
2988 * of this If statement */
2989 resources
->irqs
->interrupt
[(temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03] = IRQ
;
2990 resources
->irqs
->valid_INT
|= 0x01 << (temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03;
2995 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
2996 PCI_LATENCY_TIMER
, temp_byte
);
2998 /* Cache Line size */
3000 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
3001 PCI_CACHE_LINE_SIZE
, temp_byte
);
3003 /* disable ROM base Address */
3005 rc
= pci_bus_write_config_word(pci_bus
, devfn
,
3006 PCI_ROM_ADDRESS
, temp_dword
);
3009 temp_word
= 0x0157; /* = PCI_COMMAND_IO |
3010 * PCI_COMMAND_MEMORY |
3011 * PCI_COMMAND_MASTER |
3012 * PCI_COMMAND_INVALIDATE |
3013 * PCI_COMMAND_PARITY |
3014 * PCI_COMMAND_SERR */
3015 rc
= pci_bus_write_config_word (pci_bus
, devfn
,
3016 PCI_COMMAND
, temp_word
);
3017 } else { /* End of Not-A-Bridge else */
3018 /* It's some strange type of PCI adapter (Cardbus?) */
3019 return DEVICE_TYPE_NOT_SUPPORTED
;
3022 func
->configured
= 1;
3026 cpqhp_destroy_resource_list (&temp_resources
);
3028 return_resource(&(resources
-> bus_head
), hold_bus_node
);
3029 return_resource(&(resources
-> io_head
), hold_IO_node
);
3030 return_resource(&(resources
-> mem_head
), hold_mem_node
);
3031 return_resource(&(resources
-> p_mem_head
), hold_p_mem_node
);