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>
40 #include <linux/kthread.h>
43 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
*func
,
44 u8 behind_bridge
, struct resource_lists
*resources
);
45 static int configure_new_function(struct controller
* ctrl
, struct pci_func
*func
,
46 u8 behind_bridge
, struct resource_lists
*resources
);
47 static void interrupt_event_handler(struct controller
*ctrl
);
50 static struct task_struct
*cpqhp_event_thread
;
51 static unsigned long pushbutton_pending
; /* = 0 */
53 /* delay is in jiffies to wait for */
54 static void long_delay(int delay
)
57 * XXX(hch): if someone is bored please convert all callers
58 * to call msleep_interruptible directly. They really want
59 * to specify timeouts in natural units and spend a lot of
60 * effort converting them to jiffies..
62 msleep_interruptible(jiffies_to_msecs(delay
));
66 /* FIXME: The following line needs to be somewhere else... */
67 #define WRONG_BUS_FREQUENCY 0x07
68 static u8
handle_switch_change(u8 change
, struct controller
* ctrl
)
73 struct pci_func
*func
;
74 struct event_info
*taskInfo
;
80 dbg("cpqsbd: Switch interrupt received.\n");
82 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
83 if (change
& (0x1L
<< hp_slot
)) {
84 /**********************************
86 **********************************/
87 func
= cpqhp_slot_find(ctrl
->bus
,
88 (hp_slot
+ ctrl
->slot_device_offset
), 0);
90 /* this is the structure that tells the worker thread
92 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
93 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
94 taskInfo
->hp_slot
= hp_slot
;
98 temp_word
= ctrl
->ctrl_int_comp
>> 16;
99 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
100 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
102 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
103 /**********************************
105 **********************************/
107 func
->switch_save
= 0;
109 taskInfo
->event_type
= INT_SWITCH_OPEN
;
111 /**********************************
113 **********************************/
115 func
->switch_save
= 0x10;
117 taskInfo
->event_type
= INT_SWITCH_CLOSE
;
126 * cpqhp_find_slot: find the struct slot of given device
127 * @ctrl: scan lots of this controller
128 * @device: the device id to find
130 static struct slot
*cpqhp_find_slot(struct controller
*ctrl
, u8 device
)
132 struct slot
*slot
= ctrl
->slot
;
134 while (slot
&& (slot
->device
!= device
)) {
142 static u8
handle_presence_change(u16 change
, struct controller
* ctrl
)
148 struct pci_func
*func
;
149 struct event_info
*taskInfo
;
155 /**********************************
157 **********************************/
158 dbg("cpqsbd: Presence/Notify input change.\n");
159 dbg(" Changed bits are 0x%4.4x\n", change
);
161 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
162 if (change
& (0x0101 << hp_slot
)) {
163 /**********************************
165 **********************************/
166 func
= cpqhp_slot_find(ctrl
->bus
,
167 (hp_slot
+ ctrl
->slot_device_offset
), 0);
169 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
170 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
171 taskInfo
->hp_slot
= hp_slot
;
175 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ (readb(ctrl
->hpc_reg
+ SLOT_MASK
) >> 4));
179 /* If the switch closed, must be a button
180 * If not in button mode, nevermind */
181 if (func
->switch_save
&& (ctrl
->push_button
== 1)) {
182 temp_word
= ctrl
->ctrl_int_comp
>> 16;
183 temp_byte
= (temp_word
>> hp_slot
) & 0x01;
184 temp_byte
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
186 if (temp_byte
!= func
->presence_save
) {
187 /**************************************
188 * button Pressed (doesn't do anything)
189 **************************************/
190 dbg("hp_slot %d button pressed\n", hp_slot
);
191 taskInfo
->event_type
= INT_BUTTON_PRESS
;
193 /**********************************
194 * button Released - TAKE ACTION!!!!
195 **********************************/
196 dbg("hp_slot %d button released\n", hp_slot
);
197 taskInfo
->event_type
= INT_BUTTON_RELEASE
;
199 /* Cancel if we are still blinking */
200 if ((p_slot
->state
== BLINKINGON_STATE
)
201 || (p_slot
->state
== BLINKINGOFF_STATE
)) {
202 taskInfo
->event_type
= INT_BUTTON_CANCEL
;
203 dbg("hp_slot %d button cancel\n", hp_slot
);
204 } else if ((p_slot
->state
== POWERON_STATE
)
205 || (p_slot
->state
== POWEROFF_STATE
)) {
206 /* info(msg_button_ignore, p_slot->number); */
207 taskInfo
->event_type
= INT_BUTTON_IGNORE
;
208 dbg("hp_slot %d button ignore\n", hp_slot
);
212 /* Switch is open, assume a presence change
213 * Save the presence state */
214 temp_word
= ctrl
->ctrl_int_comp
>> 16;
215 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
216 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
218 if ((!(ctrl
->ctrl_int_comp
& (0x010000 << hp_slot
))) ||
219 (!(ctrl
->ctrl_int_comp
& (0x01000000 << hp_slot
)))) {
221 taskInfo
->event_type
= INT_PRESENCE_ON
;
224 taskInfo
->event_type
= INT_PRESENCE_OFF
;
234 static u8
handle_power_fault(u8 change
, struct controller
* ctrl
)
238 struct pci_func
*func
;
239 struct event_info
*taskInfo
;
244 /**********************************
246 **********************************/
248 info("power fault interrupt\n");
250 for (hp_slot
= 0; hp_slot
< 6; hp_slot
++) {
251 if (change
& (0x01 << hp_slot
)) {
252 /**********************************
254 **********************************/
255 func
= cpqhp_slot_find(ctrl
->bus
,
256 (hp_slot
+ ctrl
->slot_device_offset
), 0);
258 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
259 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
260 taskInfo
->hp_slot
= hp_slot
;
264 if (ctrl
->ctrl_int_comp
& (0x00000100 << hp_slot
)) {
265 /**********************************
266 * power fault Cleared
267 **********************************/
270 taskInfo
->event_type
= INT_POWER_FAULT_CLEAR
;
272 /**********************************
274 **********************************/
275 taskInfo
->event_type
= INT_POWER_FAULT
;
278 amber_LED_on (ctrl
, hp_slot
);
279 green_LED_off (ctrl
, hp_slot
);
282 /* this is a fatal condition, we want
283 * to crash the machine to protect from
284 * data corruption. simulated_NMI
285 * shouldn't ever return */
287 simulated_NMI(hp_slot, ctrl); */
289 /* The following code causes a software
290 * crash just in case simulated_NMI did
293 panic(msg_power_fault); */
295 /* set power fault status for this board */
297 info("power fault bit %x set\n", hp_slot
);
308 * sort_by_size: sort nodes on the list by their length, smallest first.
309 * @head: list to sort
312 static int sort_by_size(struct pci_resource
**head
)
314 struct pci_resource
*current_res
;
315 struct pci_resource
*next_res
;
316 int out_of_order
= 1;
321 if (!((*head
)->next
))
324 while (out_of_order
) {
327 /* Special case for swapping list head */
328 if (((*head
)->next
) &&
329 ((*head
)->length
> (*head
)->next
->length
)) {
332 *head
= (*head
)->next
;
333 current_res
->next
= (*head
)->next
;
334 (*head
)->next
= current_res
;
339 while (current_res
->next
&& current_res
->next
->next
) {
340 if (current_res
->next
->length
> current_res
->next
->next
->length
) {
342 next_res
= current_res
->next
;
343 current_res
->next
= current_res
->next
->next
;
344 current_res
= current_res
->next
;
345 next_res
->next
= current_res
->next
;
346 current_res
->next
= next_res
;
348 current_res
= current_res
->next
;
350 } /* End of out_of_order loop */
357 * sort_by_max_size: sort nodes on the list by their length, largest first.
358 * @head: list to sort
361 static int sort_by_max_size(struct pci_resource
**head
)
363 struct pci_resource
*current_res
;
364 struct pci_resource
*next_res
;
365 int out_of_order
= 1;
370 if (!((*head
)->next
))
373 while (out_of_order
) {
376 /* Special case for swapping list head */
377 if (((*head
)->next
) &&
378 ((*head
)->length
< (*head
)->next
->length
)) {
381 *head
= (*head
)->next
;
382 current_res
->next
= (*head
)->next
;
383 (*head
)->next
= current_res
;
388 while (current_res
->next
&& current_res
->next
->next
) {
389 if (current_res
->next
->length
< current_res
->next
->next
->length
) {
391 next_res
= current_res
->next
;
392 current_res
->next
= current_res
->next
->next
;
393 current_res
= current_res
->next
;
394 next_res
->next
= current_res
->next
;
395 current_res
->next
= next_res
;
397 current_res
= current_res
->next
;
399 } /* End of out_of_order loop */
406 * do_pre_bridge_resource_split: find node of resources that are unused
409 static struct pci_resource
*do_pre_bridge_resource_split(struct pci_resource
**head
,
410 struct pci_resource
**orig_head
, u32 alignment
)
412 struct pci_resource
*prevnode
= NULL
;
413 struct pci_resource
*node
;
414 struct pci_resource
*split_node
;
417 dbg("do_pre_bridge_resource_split\n");
419 if (!(*head
) || !(*orig_head
))
422 rc
= cpqhp_resource_sort_and_combine(head
);
427 if ((*head
)->base
!= (*orig_head
)->base
)
430 if ((*head
)->length
== (*orig_head
)->length
)
434 /* If we got here, there the bridge requires some of the resource, but
435 * we may be able to split some off of the front */
439 if (node
->length
& (alignment
-1)) {
440 /* this one isn't an aligned length, so we'll make a new entry
441 * and split it up. */
442 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
447 temp_dword
= (node
->length
| (alignment
-1)) + 1 - alignment
;
449 split_node
->base
= node
->base
;
450 split_node
->length
= temp_dword
;
452 node
->length
-= temp_dword
;
453 node
->base
+= split_node
->length
;
455 /* Put it in the list */
457 split_node
->next
= node
;
460 if (node
->length
< alignment
)
468 while (prevnode
->next
!= node
)
469 prevnode
= prevnode
->next
;
471 prevnode
->next
= node
->next
;
480 * do_bridge_resource_split: find one node of resources that aren't in use
483 static struct pci_resource
*do_bridge_resource_split(struct pci_resource
**head
, u32 alignment
)
485 struct pci_resource
*prevnode
= NULL
;
486 struct pci_resource
*node
;
490 rc
= cpqhp_resource_sort_and_combine(head
);
503 if (node
->length
< alignment
)
506 if (node
->base
& (alignment
- 1)) {
507 /* Short circuit if adjusted size is too small */
508 temp_dword
= (node
->base
| (alignment
-1)) + 1;
509 if ((node
->length
- (temp_dword
- node
->base
)) < alignment
)
512 node
->length
-= (temp_dword
- node
->base
);
513 node
->base
= temp_dword
;
516 if (node
->length
& (alignment
- 1))
517 /* There's stuff in use after this node */
528 * get_io_resource: find first node of given size not in ISA aliasing window.
529 * @head: list to search
530 * @size: size of node to find, must be a power of two.
532 * Description: this function sorts the resource list by size and then returns
533 * returns the first node of "size" length that is not in the ISA aliasing
534 * window. If it finds a node larger than "size" it will split it up.
537 static struct pci_resource
*get_io_resource(struct pci_resource
**head
, u32 size
)
539 struct pci_resource
*prevnode
;
540 struct pci_resource
*node
;
541 struct pci_resource
*split_node
;
547 if ( cpqhp_resource_sort_and_combine(head
) )
550 if ( sort_by_size(head
) )
553 for (node
= *head
; node
; node
= node
->next
) {
554 if (node
->length
< size
)
557 if (node
->base
& (size
- 1)) {
558 /* this one isn't base aligned properly
559 * so we'll make a new entry and split it up */
560 temp_dword
= (node
->base
| (size
-1)) + 1;
562 /* Short circuit if adjusted size is too small */
563 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
566 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
571 split_node
->base
= node
->base
;
572 split_node
->length
= temp_dword
- node
->base
;
573 node
->base
= temp_dword
;
574 node
->length
-= split_node
->length
;
576 /* Put it in the list */
577 split_node
->next
= node
->next
;
578 node
->next
= split_node
;
579 } /* End of non-aligned base */
581 /* Don't need to check if too small since we already did */
582 if (node
->length
> size
) {
583 /* this one is longer than we need
584 * so we'll make a new entry and split it up */
585 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
590 split_node
->base
= node
->base
+ size
;
591 split_node
->length
= node
->length
- size
;
594 /* Put it in the list */
595 split_node
->next
= node
->next
;
596 node
->next
= split_node
;
597 } /* End of too big on top end */
599 /* For IO make sure it's not in the ISA aliasing space */
600 if (node
->base
& 0x300L
)
603 /* If we got here, then it is the right size
604 * Now take it out of the list and break */
609 while (prevnode
->next
!= node
)
610 prevnode
= prevnode
->next
;
612 prevnode
->next
= node
->next
;
623 * get_max_resource: get largest node which has at least the given size.
624 * @head: the list to search the node in
625 * @size: the minimum size of the node to find
627 * Description: Gets the largest node that is at least "size" big from the
628 * list pointed to by head. It aligns the node on top and bottom
629 * to "size" alignment before returning it.
631 static struct pci_resource
*get_max_resource(struct pci_resource
**head
, u32 size
)
633 struct pci_resource
*max
;
634 struct pci_resource
*temp
;
635 struct pci_resource
*split_node
;
638 if (cpqhp_resource_sort_and_combine(head
))
641 if (sort_by_max_size(head
))
644 for (max
= *head
; max
; max
= max
->next
) {
645 /* If not big enough we could probably just bail,
646 * instead we'll continue to the next. */
647 if (max
->length
< size
)
650 if (max
->base
& (size
- 1)) {
651 /* this one isn't base aligned properly
652 * so we'll make a new entry and split it up */
653 temp_dword
= (max
->base
| (size
-1)) + 1;
655 /* Short circuit if adjusted size is too small */
656 if ((max
->length
- (temp_dword
- max
->base
)) < size
)
659 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
664 split_node
->base
= max
->base
;
665 split_node
->length
= temp_dword
- max
->base
;
666 max
->base
= temp_dword
;
667 max
->length
-= split_node
->length
;
669 split_node
->next
= max
->next
;
670 max
->next
= split_node
;
673 if ((max
->base
+ max
->length
) & (size
- 1)) {
674 /* this one isn't end aligned properly at the top
675 * so we'll make a new entry and split it up */
676 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
680 temp_dword
= ((max
->base
+ max
->length
) & ~(size
- 1));
681 split_node
->base
= temp_dword
;
682 split_node
->length
= max
->length
+ max
->base
684 max
->length
-= split_node
->length
;
686 split_node
->next
= max
->next
;
687 max
->next
= split_node
;
690 /* Make sure it didn't shrink too much when we aligned it */
691 if (max
->length
< size
)
694 /* Now take it out of the list */
699 while (temp
&& temp
->next
!= max
) {
703 temp
->next
= max
->next
;
715 * get_resource: find resource of given size and split up larger ones.
716 * @head: the list to search for resources
717 * @size: the size limit to use
719 * Description: This function sorts the resource list by size and then
720 * returns the first node of "size" length. If it finds a node
721 * larger than "size" it will split it up.
723 * size must be a power of two.
725 static struct pci_resource
*get_resource(struct pci_resource
**head
, u32 size
)
727 struct pci_resource
*prevnode
;
728 struct pci_resource
*node
;
729 struct pci_resource
*split_node
;
732 if (cpqhp_resource_sort_and_combine(head
))
735 if (sort_by_size(head
))
738 for (node
= *head
; node
; node
= node
->next
) {
739 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
740 __FUNCTION__
, size
, node
, node
->base
, node
->length
);
741 if (node
->length
< size
)
744 if (node
->base
& (size
- 1)) {
745 dbg("%s: not aligned\n", __FUNCTION__
);
746 /* this one isn't base aligned properly
747 * so we'll make a new entry and split it up */
748 temp_dword
= (node
->base
| (size
-1)) + 1;
750 /* Short circuit if adjusted size is too small */
751 if ((node
->length
- (temp_dword
- node
->base
)) < size
)
754 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
759 split_node
->base
= node
->base
;
760 split_node
->length
= temp_dword
- node
->base
;
761 node
->base
= temp_dword
;
762 node
->length
-= split_node
->length
;
764 split_node
->next
= node
->next
;
765 node
->next
= split_node
;
766 } /* End of non-aligned base */
768 /* Don't need to check if too small since we already did */
769 if (node
->length
> size
) {
770 dbg("%s: too big\n", __FUNCTION__
);
771 /* this one is longer than we need
772 * so we'll make a new entry and split it up */
773 split_node
= kmalloc(sizeof(*split_node
), GFP_KERNEL
);
778 split_node
->base
= node
->base
+ size
;
779 split_node
->length
= node
->length
- size
;
782 /* Put it in the list */
783 split_node
->next
= node
->next
;
784 node
->next
= split_node
;
785 } /* End of too big on top end */
787 dbg("%s: got one!!!\n", __FUNCTION__
);
788 /* If we got here, then it is the right size
789 * Now take it out of the list */
794 while (prevnode
->next
!= node
)
795 prevnode
= prevnode
->next
;
797 prevnode
->next
= node
->next
;
807 * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
808 * @head: the list to sort and clean up
810 * Description: Sorts all of the nodes in the list in ascending order by
811 * their base addresses. Also does garbage collection by
812 * combining adjacent nodes.
814 * returns 0 if success
816 int cpqhp_resource_sort_and_combine(struct pci_resource
**head
)
818 struct pci_resource
*node1
;
819 struct pci_resource
*node2
;
820 int out_of_order
= 1;
822 dbg("%s: head = %p, *head = %p\n", __FUNCTION__
, head
, *head
);
827 dbg("*head->next = %p\n",(*head
)->next
);
830 return 0; /* only one item on the list, already sorted! */
832 dbg("*head->base = 0x%x\n",(*head
)->base
);
833 dbg("*head->next->base = 0x%x\n",(*head
)->next
->base
);
834 while (out_of_order
) {
837 /* Special case for swapping list head */
838 if (((*head
)->next
) &&
839 ((*head
)->base
> (*head
)->next
->base
)) {
841 (*head
) = (*head
)->next
;
842 node1
->next
= (*head
)->next
;
843 (*head
)->next
= node1
;
849 while (node1
->next
&& node1
->next
->next
) {
850 if (node1
->next
->base
> node1
->next
->next
->base
) {
853 node1
->next
= node1
->next
->next
;
855 node2
->next
= node1
->next
;
860 } /* End of out_of_order loop */
864 while (node1
&& node1
->next
) {
865 if ((node1
->base
+ node1
->length
) == node1
->next
->base
) {
868 node1
->length
+= node1
->next
->length
;
870 node1
->next
= node1
->next
->next
;
880 irqreturn_t
cpqhp_ctrl_intr(int IRQ
, void *data
)
882 struct controller
*ctrl
= data
;
883 u8 schedule_flag
= 0;
890 misc
= readw(ctrl
->hpc_reg
+ MISC
);
891 /***************************************
892 * Check to see if it was our interrupt
893 ***************************************/
894 if (!(misc
& 0x000C)) {
899 /**********************************
900 * Serial Output interrupt Pending
901 **********************************/
903 /* Clear the interrupt */
905 writew(misc
, ctrl
->hpc_reg
+ MISC
);
907 /* Read to clear posted writes */
908 misc
= readw(ctrl
->hpc_reg
+ MISC
);
910 dbg ("%s - waking up\n", __FUNCTION__
);
911 wake_up_interruptible(&ctrl
->queue
);
915 /* General-interrupt-input interrupt Pending */
916 Diff
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) ^ ctrl
->ctrl_int_comp
;
918 ctrl
->ctrl_int_comp
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
920 /* Clear the interrupt */
921 writel(Diff
, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
923 /* Read it back to clear any posted writes */
924 temp_dword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
927 /* Clear all interrupts */
928 writel(0xFFFFFFFF, ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
930 schedule_flag
+= handle_switch_change((u8
)(Diff
& 0xFFL
), ctrl
);
931 schedule_flag
+= handle_presence_change((u16
)((Diff
& 0xFFFF0000L
) >> 16), ctrl
);
932 schedule_flag
+= handle_power_fault((u8
)((Diff
& 0xFF00L
) >> 8), ctrl
);
935 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
937 /* Bus reset has completed */
939 writeb(reset
, ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
940 reset
= readb(ctrl
->hpc_reg
+ RESET_FREQ_MODE
);
941 wake_up_interruptible(&ctrl
->queue
);
945 wake_up_process(cpqhp_event_thread
);
946 dbg("Waking even thread");
953 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
954 * @busnumber - bus where new node is to be located
956 * Returns pointer to the new node or NULL if unsuccessful
958 struct pci_func
*cpqhp_slot_create(u8 busnumber
)
960 struct pci_func
*new_slot
;
961 struct pci_func
*next
;
963 new_slot
= kzalloc(sizeof(*new_slot
), GFP_KERNEL
);
964 if (new_slot
== NULL
) {
970 new_slot
->next
= NULL
;
971 new_slot
->configured
= 1;
973 if (cpqhp_slot_list
[busnumber
] == NULL
) {
974 cpqhp_slot_list
[busnumber
] = new_slot
;
976 next
= cpqhp_slot_list
[busnumber
];
977 while (next
->next
!= NULL
)
979 next
->next
= new_slot
;
986 * slot_remove - Removes a node from the linked list of slots.
987 * @old_slot: slot to remove
989 * Returns 0 if successful, !0 otherwise.
991 static int slot_remove(struct pci_func
* old_slot
)
993 struct pci_func
*next
;
995 if (old_slot
== NULL
)
998 next
= cpqhp_slot_list
[old_slot
->bus
];
1004 if (next
== old_slot
) {
1005 cpqhp_slot_list
[old_slot
->bus
] = old_slot
->next
;
1006 cpqhp_destroy_board_resources(old_slot
);
1011 while ((next
->next
!= old_slot
) && (next
->next
!= NULL
)) {
1015 if (next
->next
== old_slot
) {
1016 next
->next
= old_slot
->next
;
1017 cpqhp_destroy_board_resources(old_slot
);
1026 * bridge_slot_remove - Removes a node from the linked list of slots.
1027 * @bridge: bridge to remove
1029 * Returns 0 if successful, !0 otherwise.
1031 static int bridge_slot_remove(struct pci_func
*bridge
)
1033 u8 subordinateBus
, secondaryBus
;
1035 struct pci_func
*next
;
1037 secondaryBus
= (bridge
->config_space
[0x06] >> 8) & 0xFF;
1038 subordinateBus
= (bridge
->config_space
[0x06] >> 16) & 0xFF;
1040 for (tempBus
= secondaryBus
; tempBus
<= subordinateBus
; tempBus
++) {
1041 next
= cpqhp_slot_list
[tempBus
];
1043 while (!slot_remove(next
)) {
1044 next
= cpqhp_slot_list
[tempBus
];
1048 next
= cpqhp_slot_list
[bridge
->bus
];
1053 if (next
== bridge
) {
1054 cpqhp_slot_list
[bridge
->bus
] = bridge
->next
;
1058 while ((next
->next
!= bridge
) && (next
->next
!= NULL
))
1061 if (next
->next
!= bridge
)
1063 next
->next
= bridge
->next
;
1071 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1073 * @device: device to find
1074 * @index: is 0 for first function found, 1 for the second...
1076 * Returns pointer to the node if successful, %NULL otherwise.
1078 struct pci_func
*cpqhp_slot_find(u8 bus
, u8 device
, u8 index
)
1081 struct pci_func
*func
;
1083 func
= cpqhp_slot_list
[bus
];
1085 if ((func
== NULL
) || ((func
->device
== device
) && (index
== 0)))
1088 if (func
->device
== device
)
1091 while (func
->next
!= NULL
) {
1094 if (func
->device
== device
)
1105 /* DJZ: I don't think is_bridge will work as is.
1107 static int is_bridge(struct pci_func
* func
)
1109 /* Check the header type */
1110 if (((func
->config_space
[0x03] >> 16) & 0xFF) == 0x01)
1118 * set_controller_speed - set the frequency and/or mode of a specific
1119 * controller segment.
1121 * @ctrl: controller to change frequency/mode for.
1122 * @adapter_speed: the speed of the adapter we want to match.
1123 * @hp_slot: the slot number where the adapter is installed.
1125 * Returns 0 if we successfully change frequency and/or mode to match the
1129 static u8
set_controller_speed(struct controller
*ctrl
, u8 adapter_speed
, u8 hp_slot
)
1133 u8 slot_power
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1135 u32 leds
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
1137 if (ctrl
->speed
== adapter_speed
)
1140 /* We don't allow freq/mode changes if we find another adapter running
1141 * in another slot on this controller */
1142 for(slot
= ctrl
->slot
; slot
; slot
= slot
->next
) {
1143 if (slot
->device
== (hp_slot
+ ctrl
->slot_device_offset
))
1145 if (!slot
->hotplug_slot
&& !slot
->hotplug_slot
->info
)
1147 if (slot
->hotplug_slot
->info
->adapter_status
== 0)
1149 /* If another adapter is running on the same segment but at a
1150 * lower speed/mode, we allow the new adapter to function at
1151 * this rate if supported */
1152 if (ctrl
->speed
< adapter_speed
)
1158 /* If the controller doesn't support freq/mode changes and the
1159 * controller is running at a higher mode, we bail */
1160 if ((ctrl
->speed
> adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1163 /* But we allow the adapter to run at a lower rate if possible */
1164 if ((ctrl
->speed
< adapter_speed
) && (!ctrl
->pcix_speed_capability
))
1167 /* We try to set the max speed supported by both the adapter and
1169 if (ctrl
->speed_capability
< adapter_speed
) {
1170 if (ctrl
->speed
== ctrl
->speed_capability
)
1172 adapter_speed
= ctrl
->speed_capability
;
1175 writel(0x0L
, ctrl
->hpc_reg
+ LED_CONTROL
);
1176 writeb(0x00, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1179 wait_for_ctrl_irq(ctrl
);
1181 if (adapter_speed
!= PCI_SPEED_133MHz_PCIX
)
1185 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1187 reg16
= readw(ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1189 switch(adapter_speed
) {
1190 case(PCI_SPEED_133MHz_PCIX
):
1194 case(PCI_SPEED_100MHz_PCIX
):
1198 case(PCI_SPEED_66MHz_PCIX
):
1202 case(PCI_SPEED_66MHz
):
1206 default: /* 33MHz PCI 2.2 */
1212 writew(reg16
, ctrl
->hpc_reg
+ NEXT_CURR_FREQ
);
1216 /* Reenable interrupts */
1217 writel(0, ctrl
->hpc_reg
+ INT_MASK
);
1219 pci_write_config_byte(ctrl
->pci_dev
, 0x41, reg
);
1221 /* Restart state machine */
1223 pci_read_config_byte(ctrl
->pci_dev
, 0x43, ®
);
1224 pci_write_config_byte(ctrl
->pci_dev
, 0x43, reg
);
1226 /* Only if mode change...*/
1227 if (((ctrl
->speed
== PCI_SPEED_66MHz
) && (adapter_speed
== PCI_SPEED_66MHz_PCIX
)) ||
1228 ((ctrl
->speed
== PCI_SPEED_66MHz_PCIX
) && (adapter_speed
== PCI_SPEED_66MHz
)))
1231 wait_for_ctrl_irq(ctrl
);
1234 /* Restore LED/Slot state */
1235 writel(leds
, ctrl
->hpc_reg
+ LED_CONTROL
);
1236 writeb(slot_power
, ctrl
->hpc_reg
+ SLOT_ENABLE
);
1239 wait_for_ctrl_irq(ctrl
);
1241 ctrl
->speed
= adapter_speed
;
1242 slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1244 info("Successfully changed frequency/mode for adapter in slot %d\n",
1249 /* the following routines constitute the bulk of the
1250 hotplug controller logic
1255 * board_replaced - Called after a board has been replaced in the system.
1257 * This is only used if we don't have resources for hot add
1258 * Turns power on for the board
1259 * Checks to see if board is the same
1260 * If board is same, reconfigures it
1261 * If board isn't same, turns it back off.
1264 static u32
board_replaced(struct pci_func
*func
, struct controller
*ctrl
)
1271 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1273 if (readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
) & (0x01L
<< hp_slot
)) {
1274 /**********************************
1275 * The switch is open.
1276 **********************************/
1277 rc
= INTERLOCK_OPEN
;
1278 } else if (is_slot_enabled (ctrl
, hp_slot
)) {
1279 /**********************************
1280 * The board is already on
1281 **********************************/
1282 rc
= CARD_FUNCTIONING
;
1284 mutex_lock(&ctrl
->crit_sect
);
1286 /* turn on board without attaching to the bus */
1287 enable_slot_power (ctrl
, hp_slot
);
1291 /* Wait for SOBS to be unset */
1292 wait_for_ctrl_irq (ctrl
);
1294 /* Change bits in slot power register to force another shift out
1295 * NOTE: this is to work around the timer bug */
1296 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1297 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1298 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1302 /* Wait for SOBS to be unset */
1303 wait_for_ctrl_irq (ctrl
);
1305 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1306 if (ctrl
->speed
!= adapter_speed
)
1307 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1308 rc
= WRONG_BUS_FREQUENCY
;
1310 /* turn off board without attaching to the bus */
1311 disable_slot_power (ctrl
, hp_slot
);
1315 /* Wait for SOBS to be unset */
1316 wait_for_ctrl_irq (ctrl
);
1318 mutex_unlock(&ctrl
->crit_sect
);
1323 mutex_lock(&ctrl
->crit_sect
);
1325 slot_enable (ctrl
, hp_slot
);
1326 green_LED_blink (ctrl
, hp_slot
);
1328 amber_LED_off (ctrl
, hp_slot
);
1332 /* Wait for SOBS to be unset */
1333 wait_for_ctrl_irq (ctrl
);
1335 mutex_unlock(&ctrl
->crit_sect
);
1337 /* Wait for ~1 second because of hot plug spec */
1340 /* Check for a power fault */
1341 if (func
->status
== 0xFF) {
1342 /* power fault occurred, but it was benign */
1346 rc
= cpqhp_valid_replace(ctrl
, func
);
1349 /* It must be the same board */
1351 rc
= cpqhp_configure_board(ctrl
, func
);
1353 /* If configuration fails, turn it off
1354 * Get slot won't work for devices behind
1355 * bridges, but in this case it will always be
1356 * called for the "base" bus/dev/func of an
1359 mutex_lock(&ctrl
->crit_sect
);
1361 amber_LED_on (ctrl
, hp_slot
);
1362 green_LED_off (ctrl
, hp_slot
);
1363 slot_disable (ctrl
, hp_slot
);
1367 /* Wait for SOBS to be unset */
1368 wait_for_ctrl_irq (ctrl
);
1370 mutex_unlock(&ctrl
->crit_sect
);
1378 /* Something is wrong
1380 * Get slot won't work for devices behind bridges, but
1381 * in this case it will always be called for the "base"
1382 * bus/dev/func of an adapter. */
1384 mutex_lock(&ctrl
->crit_sect
);
1386 amber_LED_on (ctrl
, hp_slot
);
1387 green_LED_off (ctrl
, hp_slot
);
1388 slot_disable (ctrl
, hp_slot
);
1392 /* Wait for SOBS to be unset */
1393 wait_for_ctrl_irq (ctrl
);
1395 mutex_unlock(&ctrl
->crit_sect
);
1405 * board_added - Called after a board has been added to the system.
1407 * Turns power on for the board
1411 static u32
board_added(struct pci_func
*func
, struct controller
*ctrl
)
1417 u32 temp_register
= 0xFFFFFFFF;
1419 struct pci_func
*new_slot
= NULL
;
1420 struct slot
*p_slot
;
1421 struct resource_lists res_lists
;
1423 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1424 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1425 __FUNCTION__
, func
->device
, ctrl
->slot_device_offset
, hp_slot
);
1427 mutex_lock(&ctrl
->crit_sect
);
1429 /* turn on board without attaching to the bus */
1430 enable_slot_power(ctrl
, hp_slot
);
1434 /* Wait for SOBS to be unset */
1435 wait_for_ctrl_irq (ctrl
);
1437 /* Change bits in slot power register to force another shift out
1438 * NOTE: this is to work around the timer bug */
1439 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_POWER
);
1440 writeb(0x00, ctrl
->hpc_reg
+ SLOT_POWER
);
1441 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_POWER
);
1445 /* Wait for SOBS to be unset */
1446 wait_for_ctrl_irq (ctrl
);
1448 adapter_speed
= get_adapter_speed(ctrl
, hp_slot
);
1449 if (ctrl
->speed
!= adapter_speed
)
1450 if (set_controller_speed(ctrl
, adapter_speed
, hp_slot
))
1451 rc
= WRONG_BUS_FREQUENCY
;
1453 /* turn off board without attaching to the bus */
1454 disable_slot_power (ctrl
, hp_slot
);
1458 /* Wait for SOBS to be unset */
1459 wait_for_ctrl_irq(ctrl
);
1461 mutex_unlock(&ctrl
->crit_sect
);
1466 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1468 /* turn on board and blink green LED */
1470 dbg("%s: before down\n", __FUNCTION__
);
1471 mutex_lock(&ctrl
->crit_sect
);
1472 dbg("%s: after down\n", __FUNCTION__
);
1474 dbg("%s: before slot_enable\n", __FUNCTION__
);
1475 slot_enable (ctrl
, hp_slot
);
1477 dbg("%s: before green_LED_blink\n", __FUNCTION__
);
1478 green_LED_blink (ctrl
, hp_slot
);
1480 dbg("%s: before amber_LED_blink\n", __FUNCTION__
);
1481 amber_LED_off (ctrl
, hp_slot
);
1483 dbg("%s: before set_SOGO\n", __FUNCTION__
);
1486 /* Wait for SOBS to be unset */
1487 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__
);
1488 wait_for_ctrl_irq (ctrl
);
1489 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__
);
1491 dbg("%s: before up\n", __FUNCTION__
);
1492 mutex_unlock(&ctrl
->crit_sect
);
1493 dbg("%s: after up\n", __FUNCTION__
);
1495 /* Wait for ~1 second because of hot plug spec */
1496 dbg("%s: before long_delay\n", __FUNCTION__
);
1498 dbg("%s: after long_delay\n", __FUNCTION__
);
1500 dbg("%s: func status = %x\n", __FUNCTION__
, func
->status
);
1501 /* Check for a power fault */
1502 if (func
->status
== 0xFF) {
1503 /* power fault occurred, but it was benign */
1504 temp_register
= 0xFFFFFFFF;
1505 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__
, temp_register
);
1509 /* Get vendor/device ID u32 */
1510 ctrl
->pci_bus
->number
= func
->bus
;
1511 rc
= pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), PCI_VENDOR_ID
, &temp_register
);
1512 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__
, rc
);
1513 dbg("%s: temp_register is %x\n", __FUNCTION__
, temp_register
);
1516 /* Something's wrong here */
1517 temp_register
= 0xFFFFFFFF;
1518 dbg("%s: temp register set to %x by error\n", __FUNCTION__
, temp_register
);
1520 /* Preset return code. It will be changed later if things go okay. */
1521 rc
= NO_ADAPTER_PRESENT
;
1524 /* All F's is an empty slot or an invalid board */
1525 if (temp_register
!= 0xFFFFFFFF) { /* Check for a board in the slot */
1526 res_lists
.io_head
= ctrl
->io_head
;
1527 res_lists
.mem_head
= ctrl
->mem_head
;
1528 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1529 res_lists
.bus_head
= ctrl
->bus_head
;
1530 res_lists
.irqs
= NULL
;
1532 rc
= configure_new_device(ctrl
, func
, 0, &res_lists
);
1534 dbg("%s: back from configure_new_device\n", __FUNCTION__
);
1535 ctrl
->io_head
= res_lists
.io_head
;
1536 ctrl
->mem_head
= res_lists
.mem_head
;
1537 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1538 ctrl
->bus_head
= res_lists
.bus_head
;
1540 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1541 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1542 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1543 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1546 mutex_lock(&ctrl
->crit_sect
);
1548 amber_LED_on (ctrl
, hp_slot
);
1549 green_LED_off (ctrl
, hp_slot
);
1550 slot_disable (ctrl
, hp_slot
);
1554 /* Wait for SOBS to be unset */
1555 wait_for_ctrl_irq (ctrl
);
1557 mutex_unlock(&ctrl
->crit_sect
);
1560 cpqhp_save_slot_config(ctrl
, func
);
1565 func
->switch_save
= 0x10;
1566 func
->is_a_board
= 0x01;
1568 /* next, we will instantiate the linux pci_dev structures (with
1569 * appropriate driver notification, if already present) */
1570 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__
);
1573 new_slot
= cpqhp_slot_find(ctrl
->bus
, func
->device
, index
++);
1574 if (new_slot
&& !new_slot
->pci_dev
) {
1575 cpqhp_configure_device(ctrl
, new_slot
);
1579 mutex_lock(&ctrl
->crit_sect
);
1581 green_LED_on (ctrl
, hp_slot
);
1585 /* Wait for SOBS to be unset */
1586 wait_for_ctrl_irq (ctrl
);
1588 mutex_unlock(&ctrl
->crit_sect
);
1590 mutex_lock(&ctrl
->crit_sect
);
1592 amber_LED_on (ctrl
, hp_slot
);
1593 green_LED_off (ctrl
, hp_slot
);
1594 slot_disable (ctrl
, hp_slot
);
1598 /* Wait for SOBS to be unset */
1599 wait_for_ctrl_irq (ctrl
);
1601 mutex_unlock(&ctrl
->crit_sect
);
1610 * remove_board - Turns off slot and LED's
1613 static u32
remove_board(struct pci_func
* func
, u32 replace_flag
, struct controller
* ctrl
)
1621 struct resource_lists res_lists
;
1622 struct pci_func
*temp_func
;
1624 if (cpqhp_unconfigure_device(func
))
1627 device
= func
->device
;
1629 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
1630 dbg("In %s, hp_slot = %d\n", __FUNCTION__
, hp_slot
);
1632 /* When we get here, it is safe to change base address registers.
1633 * We will attempt to save the base address register lengths */
1634 if (replace_flag
|| !ctrl
->add_support
)
1635 rc
= cpqhp_save_base_addr_length(ctrl
, func
);
1636 else if (!func
->bus_head
&& !func
->mem_head
&&
1637 !func
->p_mem_head
&& !func
->io_head
) {
1638 /* Here we check to see if we've saved any of the board's
1639 * resources already. If so, we'll skip the attempt to
1640 * determine what's being used. */
1642 temp_func
= cpqhp_slot_find(func
->bus
, func
->device
, index
++);
1644 if (temp_func
->bus_head
|| temp_func
->mem_head
1645 || temp_func
->p_mem_head
|| temp_func
->io_head
) {
1649 temp_func
= cpqhp_slot_find(temp_func
->bus
, temp_func
->device
, index
++);
1653 rc
= cpqhp_save_used_resources(ctrl
, func
);
1655 /* Change status to shutdown */
1656 if (func
->is_a_board
)
1657 func
->status
= 0x01;
1658 func
->configured
= 0;
1660 mutex_lock(&ctrl
->crit_sect
);
1662 green_LED_off (ctrl
, hp_slot
);
1663 slot_disable (ctrl
, hp_slot
);
1667 /* turn off SERR for slot */
1668 temp_byte
= readb(ctrl
->hpc_reg
+ SLOT_SERR
);
1669 temp_byte
&= ~(0x01 << hp_slot
);
1670 writeb(temp_byte
, ctrl
->hpc_reg
+ SLOT_SERR
);
1672 /* Wait for SOBS to be unset */
1673 wait_for_ctrl_irq (ctrl
);
1675 mutex_unlock(&ctrl
->crit_sect
);
1677 if (!replace_flag
&& ctrl
->add_support
) {
1679 res_lists
.io_head
= ctrl
->io_head
;
1680 res_lists
.mem_head
= ctrl
->mem_head
;
1681 res_lists
.p_mem_head
= ctrl
->p_mem_head
;
1682 res_lists
.bus_head
= ctrl
->bus_head
;
1684 cpqhp_return_board_resources(func
, &res_lists
);
1686 ctrl
->io_head
= res_lists
.io_head
;
1687 ctrl
->mem_head
= res_lists
.mem_head
;
1688 ctrl
->p_mem_head
= res_lists
.p_mem_head
;
1689 ctrl
->bus_head
= res_lists
.bus_head
;
1691 cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
1692 cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
1693 cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
1694 cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
1696 if (is_bridge(func
)) {
1697 bridge_slot_remove(func
);
1701 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
1704 /* Setup slot structure with entry for empty slot */
1705 func
= cpqhp_slot_create(ctrl
->bus
);
1710 func
->bus
= ctrl
->bus
;
1711 func
->device
= device
;
1713 func
->configured
= 0;
1714 func
->switch_save
= 0x10;
1715 func
->is_a_board
= 0;
1716 func
->p_task_event
= NULL
;
1722 static void pushbutton_helper_thread(unsigned long data
)
1724 pushbutton_pending
= data
;
1725 wake_up_process(cpqhp_event_thread
);
1729 /* this is the main worker thread */
1730 static int event_thread(void* data
)
1732 struct controller
*ctrl
;
1735 dbg("!!!!event_thread sleeping\n");
1736 set_current_state(TASK_INTERRUPTIBLE
);
1739 if (kthread_should_stop())
1742 if (pushbutton_pending
)
1743 cpqhp_pushbutton_thread(pushbutton_pending
);
1745 for (ctrl
= cpqhp_ctrl_list
; ctrl
; ctrl
=ctrl
->next
)
1746 interrupt_event_handler(ctrl
);
1748 dbg("event_thread signals exit\n");
1752 int cpqhp_event_start_thread(void)
1754 cpqhp_event_thread
= kthread_run(event_thread
, NULL
, "phpd_event");
1755 if (IS_ERR(cpqhp_event_thread
)) {
1756 err ("Can't start up our event thread\n");
1757 return PTR_ERR(cpqhp_event_thread
);
1764 void cpqhp_event_stop_thread(void)
1766 kthread_stop(cpqhp_event_thread
);
1770 static int update_slot_info(struct controller
*ctrl
, struct slot
*slot
)
1772 struct hotplug_slot_info
*info
;
1775 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
1779 info
->power_status
= get_slot_enabled(ctrl
, slot
);
1780 info
->attention_status
= cpq_get_attention_status(ctrl
, slot
);
1781 info
->latch_status
= cpq_get_latch_status(ctrl
, slot
);
1782 info
->adapter_status
= get_presence_status(ctrl
, slot
);
1783 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
1788 static void interrupt_event_handler(struct controller
*ctrl
)
1792 struct pci_func
*func
;
1794 struct slot
*p_slot
;
1799 for (loop
= 0; loop
< 10; loop
++) {
1800 /* dbg("loop %d\n", loop); */
1801 if (ctrl
->event_queue
[loop
].event_type
!= 0) {
1802 hp_slot
= ctrl
->event_queue
[loop
].hp_slot
;
1804 func
= cpqhp_slot_find(ctrl
->bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
1808 p_slot
= cpqhp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1812 dbg("hp_slot %d, func %p, p_slot %p\n",
1813 hp_slot
, func
, p_slot
);
1815 if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_PRESS
) {
1816 dbg("button pressed\n");
1817 } else if (ctrl
->event_queue
[loop
].event_type
==
1818 INT_BUTTON_CANCEL
) {
1819 dbg("button cancel\n");
1820 del_timer(&p_slot
->task_event
);
1822 mutex_lock(&ctrl
->crit_sect
);
1824 if (p_slot
->state
== BLINKINGOFF_STATE
) {
1826 dbg("turn on green LED\n");
1827 green_LED_on (ctrl
, hp_slot
);
1828 } else if (p_slot
->state
== BLINKINGON_STATE
) {
1830 dbg("turn off green LED\n");
1831 green_LED_off (ctrl
, hp_slot
);
1834 info(msg_button_cancel
, p_slot
->number
);
1836 p_slot
->state
= STATIC_STATE
;
1838 amber_LED_off (ctrl
, hp_slot
);
1842 /* Wait for SOBS to be unset */
1843 wait_for_ctrl_irq (ctrl
);
1845 mutex_unlock(&ctrl
->crit_sect
);
1847 /*** button Released (No action on press...) */
1848 else if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_RELEASE
) {
1849 dbg("button release\n");
1851 if (is_slot_enabled (ctrl
, hp_slot
)) {
1852 dbg("slot is on\n");
1853 p_slot
->state
= BLINKINGOFF_STATE
;
1854 info(msg_button_off
, p_slot
->number
);
1856 dbg("slot is off\n");
1857 p_slot
->state
= BLINKINGON_STATE
;
1858 info(msg_button_on
, p_slot
->number
);
1860 mutex_lock(&ctrl
->crit_sect
);
1862 dbg("blink green LED and turn off amber\n");
1864 amber_LED_off (ctrl
, hp_slot
);
1865 green_LED_blink (ctrl
, hp_slot
);
1869 /* Wait for SOBS to be unset */
1870 wait_for_ctrl_irq (ctrl
);
1872 mutex_unlock(&ctrl
->crit_sect
);
1873 init_timer(&p_slot
->task_event
);
1874 p_slot
->hp_slot
= hp_slot
;
1875 p_slot
->ctrl
= ctrl
;
1876 /* p_slot->physical_slot = physical_slot; */
1877 p_slot
->task_event
.expires
= jiffies
+ 5 * HZ
; /* 5 second delay */
1878 p_slot
->task_event
.function
= pushbutton_helper_thread
;
1879 p_slot
->task_event
.data
= (u32
) p_slot
;
1881 dbg("add_timer p_slot = %p\n", p_slot
);
1882 add_timer(&p_slot
->task_event
);
1884 /***********POWER FAULT */
1885 else if (ctrl
->event_queue
[loop
].event_type
== INT_POWER_FAULT
) {
1886 dbg("power fault\n");
1888 /* refresh notification */
1890 update_slot_info(ctrl
, p_slot
);
1893 ctrl
->event_queue
[loop
].event_type
= 0;
1897 } /* End of FOR loop */
1905 * cpqhp_pushbutton_thread
1907 * Scheduled procedure to handle blocking stuff for the pushbuttons
1908 * Handles all pending events and exits.
1911 void cpqhp_pushbutton_thread(unsigned long slot
)
1915 struct pci_func
*func
;
1916 struct slot
*p_slot
= (struct slot
*) slot
;
1917 struct controller
*ctrl
= (struct controller
*) p_slot
->ctrl
;
1919 pushbutton_pending
= 0;
1920 hp_slot
= p_slot
->hp_slot
;
1922 device
= p_slot
->device
;
1924 if (is_slot_enabled(ctrl
, hp_slot
)) {
1925 p_slot
->state
= POWEROFF_STATE
;
1926 /* power Down board */
1927 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1928 dbg("In power_down_board, func = %p, ctrl = %p\n", func
, ctrl
);
1930 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1934 if (cpqhp_process_SS(ctrl
, func
) != 0) {
1935 amber_LED_on(ctrl
, hp_slot
);
1936 green_LED_on(ctrl
, hp_slot
);
1940 /* Wait for SOBS to be unset */
1941 wait_for_ctrl_irq(ctrl
);
1944 p_slot
->state
= STATIC_STATE
;
1946 p_slot
->state
= POWERON_STATE
;
1949 func
= cpqhp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1950 dbg("In add_board, func = %p, ctrl = %p\n", func
, ctrl
);
1952 dbg("Error! func NULL in %s\n", __FUNCTION__
);
1956 if (func
!= NULL
&& ctrl
!= NULL
) {
1957 if (cpqhp_process_SI(ctrl
, func
) != 0) {
1958 amber_LED_on(ctrl
, hp_slot
);
1959 green_LED_off(ctrl
, hp_slot
);
1963 /* Wait for SOBS to be unset */
1964 wait_for_ctrl_irq (ctrl
);
1968 p_slot
->state
= STATIC_STATE
;
1975 int cpqhp_process_SI(struct controller
*ctrl
, struct pci_func
*func
)
1981 struct slot
* p_slot
;
1982 int physical_slot
= 0;
1986 device
= func
->device
;
1987 hp_slot
= device
- ctrl
->slot_device_offset
;
1988 p_slot
= cpqhp_find_slot(ctrl
, device
);
1990 physical_slot
= p_slot
->number
;
1992 /* Check to see if the interlock is closed */
1993 tempdword
= readl(ctrl
->hpc_reg
+ INT_INPUT_CLEAR
);
1995 if (tempdword
& (0x01 << hp_slot
)) {
1999 if (func
->is_a_board
) {
2000 rc
= board_replaced(func
, ctrl
);
2005 func
= cpqhp_slot_create(ctrl
->bus
);
2009 func
->bus
= ctrl
->bus
;
2010 func
->device
= device
;
2012 func
->configured
= 0;
2013 func
->is_a_board
= 1;
2015 /* We have to save the presence info for these slots */
2016 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2017 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2018 func
->presence_save
|= (temp_word
>> (hp_slot
+ 7)) & 0x02;
2020 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2021 func
->switch_save
= 0;
2023 func
->switch_save
= 0x10;
2026 rc
= board_added(func
, ctrl
);
2028 if (is_bridge(func
)) {
2029 bridge_slot_remove(func
);
2033 /* Setup slot structure with entry for empty slot */
2034 func
= cpqhp_slot_create(ctrl
->bus
);
2039 func
->bus
= ctrl
->bus
;
2040 func
->device
= device
;
2042 func
->configured
= 0;
2043 func
->is_a_board
= 0;
2045 /* We have to save the presence info for these slots */
2046 temp_word
= ctrl
->ctrl_int_comp
>> 16;
2047 func
->presence_save
= (temp_word
>> hp_slot
) & 0x01;
2048 func
->presence_save
|=
2049 (temp_word
>> (hp_slot
+ 7)) & 0x02;
2051 if (ctrl
->ctrl_int_comp
& (0x1L
<< hp_slot
)) {
2052 func
->switch_save
= 0;
2054 func
->switch_save
= 0x10;
2060 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2064 update_slot_info(ctrl
, p_slot
);
2070 int cpqhp_process_SS(struct controller
*ctrl
, struct pci_func
*func
)
2072 u8 device
, class_code
, header_type
, BCR
;
2077 struct slot
* p_slot
;
2078 struct pci_bus
*pci_bus
= ctrl
->pci_bus
;
2079 int physical_slot
=0;
2081 device
= func
->device
;
2082 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2083 p_slot
= cpqhp_find_slot(ctrl
, device
);
2085 physical_slot
= p_slot
->number
;
2088 /* Make sure there are no video controllers here */
2089 while (func
&& !rc
) {
2090 pci_bus
->number
= func
->bus
;
2091 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2093 /* Check the Class Code */
2094 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2098 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2099 /* Display/Video adapter (not supported) */
2100 rc
= REMOVE_NOT_SUPPORTED
;
2102 /* See if it's a bridge */
2103 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
2107 /* If it's a bridge, check the VGA Enable bit */
2108 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
2109 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, &BCR
);
2113 /* If the VGA Enable bit is set, remove isn't
2115 if (BCR
& PCI_BRIDGE_CTL_VGA
) {
2116 rc
= REMOVE_NOT_SUPPORTED
;
2121 func
= cpqhp_slot_find(ctrl
->bus
, device
, index
++);
2124 func
= cpqhp_slot_find(ctrl
->bus
, device
, 0);
2125 if ((func
!= NULL
) && !rc
) {
2126 /* FIXME: Replace flag should be passed into process_SS */
2127 replace_flag
= !(ctrl
->add_support
);
2128 rc
= remove_board(func
, replace_flag
, ctrl
);
2134 update_slot_info(ctrl
, p_slot
);
2140 * switch_leds: switch the leds, go from one site to the other.
2141 * @ctrl: controller to use
2142 * @num_of_slots: number of slots to use
2143 * @direction: 1 to start from the left side, 0 to start right.
2145 static void switch_leds(struct controller
*ctrl
, const int num_of_slots
,
2146 u32
*work_LED
, const int direction
)
2150 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2152 *work_LED
= *work_LED
>> 1;
2154 *work_LED
= *work_LED
<< 1;
2155 writel(*work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2159 /* Wait for SOGO interrupt */
2160 wait_for_ctrl_irq(ctrl
);
2162 /* Get ready for next iteration */
2163 long_delay((2*HZ
)/10);
2168 * hardware_test - runs hardware tests
2170 * For hot plug ctrl folks to play with.
2171 * test_num is the number written to the "test" file in sysfs
2174 int cpqhp_hardware_test(struct controller
*ctrl
, int test_num
)
2181 num_of_slots
= readb(ctrl
->hpc_reg
+ SLOT_MASK
) & 0x0f;
2185 /* Do stuff here! */
2187 /* Do that funky LED thing */
2188 /* so we can restore them later */
2189 save_LED
= readl(ctrl
->hpc_reg
+ LED_CONTROL
);
2190 work_LED
= 0x01010101;
2191 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2192 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2193 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2194 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2196 work_LED
= 0x01010000;
2197 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2198 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2199 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2200 work_LED
= 0x00000101;
2201 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2202 switch_leds(ctrl
, num_of_slots
, &work_LED
, 0);
2203 switch_leds(ctrl
, num_of_slots
, &work_LED
, 1);
2205 work_LED
= 0x01010000;
2206 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2207 for (loop
= 0; loop
< num_of_slots
; loop
++) {
2210 /* Wait for SOGO interrupt */
2211 wait_for_ctrl_irq (ctrl
);
2213 /* Get ready for next iteration */
2214 long_delay((3*HZ
)/10);
2215 work_LED
= work_LED
>> 16;
2216 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2220 /* Wait for SOGO interrupt */
2221 wait_for_ctrl_irq (ctrl
);
2223 /* Get ready for next iteration */
2224 long_delay((3*HZ
)/10);
2225 work_LED
= work_LED
<< 16;
2226 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2227 work_LED
= work_LED
<< 1;
2228 writel(work_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2231 /* put it back the way it was */
2232 writel(save_LED
, ctrl
->hpc_reg
+ LED_CONTROL
);
2236 /* Wait for SOBS to be unset */
2237 wait_for_ctrl_irq (ctrl
);
2240 /* Do other stuff here! */
2251 * configure_new_device - Configures the PCI header information of one board.
2253 * @ctrl: pointer to controller structure
2254 * @func: pointer to function structure
2255 * @behind_bridge: 1 if this is a recursive call, 0 if not
2256 * @resources: pointer to set of resource lists
2258 * Returns 0 if success
2261 static u32
configure_new_device(struct controller
* ctrl
, struct pci_func
* func
,
2262 u8 behind_bridge
, struct resource_lists
* resources
)
2264 u8 temp_byte
, function
, max_functions
, stop_it
;
2267 struct pci_func
*new_slot
;
2272 dbg("%s\n", __FUNCTION__
);
2273 /* Check for Multi-function device */
2274 ctrl
->pci_bus
->number
= func
->bus
;
2275 rc
= pci_bus_read_config_byte (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, func
->function
), 0x0E, &temp_byte
);
2277 dbg("%s: rc = %d\n", __FUNCTION__
, rc
);
2281 if (temp_byte
& 0x80) /* Multi-function device */
2289 rc
= configure_new_function(ctrl
, new_slot
, behind_bridge
, resources
);
2292 dbg("configure_new_function failed %d\n",rc
);
2296 new_slot
= cpqhp_slot_find(new_slot
->bus
, new_slot
->device
, index
++);
2299 cpqhp_return_board_resources(new_slot
, resources
);
2309 /* The following loop skips to the next present function
2310 * and creates a board structure */
2312 while ((function
< max_functions
) && (!stop_it
)) {
2313 pci_bus_read_config_dword (ctrl
->pci_bus
, PCI_DEVFN(func
->device
, function
), 0x00, &ID
);
2315 if (ID
== 0xFFFFFFFF) { /* There's nothing there. */
2317 } else { /* There's something there */
2318 /* Setup slot structure. */
2319 new_slot
= cpqhp_slot_create(func
->bus
);
2321 if (new_slot
== NULL
)
2324 new_slot
->bus
= func
->bus
;
2325 new_slot
->device
= func
->device
;
2326 new_slot
->function
= function
;
2327 new_slot
->is_a_board
= 1;
2328 new_slot
->status
= 0;
2334 } while (function
< max_functions
);
2335 dbg("returning from configure_new_device\n");
2342 Configuration logic that involves the hotplug data structures and
2348 * configure_new_function - Configures the PCI header information of one device
2350 * @ctrl: pointer to controller structure
2351 * @func: pointer to function structure
2352 * @behind_bridge: 1 if this is a recursive call, 0 if not
2353 * @resources: pointer to set of resource lists
2355 * Calls itself recursively for bridged devices.
2356 * Returns 0 if success
2359 static int configure_new_function(struct controller
*ctrl
, struct pci_func
*func
,
2361 struct resource_lists
*resources
)
2376 struct pci_resource
*mem_node
;
2377 struct pci_resource
*p_mem_node
;
2378 struct pci_resource
*io_node
;
2379 struct pci_resource
*bus_node
;
2380 struct pci_resource
*hold_mem_node
;
2381 struct pci_resource
*hold_p_mem_node
;
2382 struct pci_resource
*hold_IO_node
;
2383 struct pci_resource
*hold_bus_node
;
2384 struct irq_mapping irqs
;
2385 struct pci_func
*new_slot
;
2386 struct pci_bus
*pci_bus
;
2387 struct resource_lists temp_resources
;
2389 pci_bus
= ctrl
->pci_bus
;
2390 pci_bus
->number
= func
->bus
;
2391 devfn
= PCI_DEVFN(func
->device
, func
->function
);
2393 /* Check for Bridge */
2394 rc
= pci_bus_read_config_byte(pci_bus
, devfn
, PCI_HEADER_TYPE
, &temp_byte
);
2398 if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) { /* PCI-PCI Bridge */
2399 /* set Primary bus */
2400 dbg("set Primary bus = %d\n", func
->bus
);
2401 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_PRIMARY_BUS
, func
->bus
);
2405 /* find range of busses to use */
2406 dbg("find ranges of buses to use\n");
2407 bus_node
= get_max_resource(&(resources
->bus_head
), 1);
2409 /* If we don't have any busses to allocate, we can't continue */
2413 /* set Secondary bus */
2414 temp_byte
= bus_node
->base
;
2415 dbg("set Secondary bus = %d\n", bus_node
->base
);
2416 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SECONDARY_BUS
, temp_byte
);
2420 /* set subordinate bus */
2421 temp_byte
= bus_node
->base
+ bus_node
->length
- 1;
2422 dbg("set subordinate bus = %d\n", bus_node
->base
+ bus_node
->length
- 1);
2423 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2427 /* set subordinate Latency Timer and base Latency Timer */
2429 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_SEC_LATENCY_TIMER
, temp_byte
);
2432 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_LATENCY_TIMER
, temp_byte
);
2436 /* set Cache Line size */
2438 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, temp_byte
);
2442 /* Setup the IO, memory, and prefetchable windows */
2443 io_node
= get_max_resource(&(resources
->io_head
), 0x1000);
2446 mem_node
= get_max_resource(&(resources
->mem_head
), 0x100000);
2449 p_mem_node
= get_max_resource(&(resources
->p_mem_head
), 0x100000);
2452 dbg("Setup the IO, memory, and prefetchable windows\n");
2454 dbg("(base, len, next) (%x, %x, %p)\n", io_node
->base
,
2455 io_node
->length
, io_node
->next
);
2457 dbg("(base, len, next) (%x, %x, %p)\n", mem_node
->base
,
2458 mem_node
->length
, mem_node
->next
);
2459 dbg("p_mem_node\n");
2460 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node
->base
,
2461 p_mem_node
->length
, p_mem_node
->next
);
2463 /* set up the IRQ info */
2464 if (!resources
->irqs
) {
2465 irqs
.barber_pole
= 0;
2466 irqs
.interrupt
[0] = 0;
2467 irqs
.interrupt
[1] = 0;
2468 irqs
.interrupt
[2] = 0;
2469 irqs
.interrupt
[3] = 0;
2472 irqs
.barber_pole
= resources
->irqs
->barber_pole
;
2473 irqs
.interrupt
[0] = resources
->irqs
->interrupt
[0];
2474 irqs
.interrupt
[1] = resources
->irqs
->interrupt
[1];
2475 irqs
.interrupt
[2] = resources
->irqs
->interrupt
[2];
2476 irqs
.interrupt
[3] = resources
->irqs
->interrupt
[3];
2477 irqs
.valid_INT
= resources
->irqs
->valid_INT
;
2480 /* set up resource lists that are now aligned on top and bottom
2481 * for anything behind the bridge. */
2482 temp_resources
.bus_head
= bus_node
;
2483 temp_resources
.io_head
= io_node
;
2484 temp_resources
.mem_head
= mem_node
;
2485 temp_resources
.p_mem_head
= p_mem_node
;
2486 temp_resources
.irqs
= &irqs
;
2488 /* Make copies of the nodes we are going to pass down so that
2489 * if there is a problem,we can just use these to free resources */
2490 hold_bus_node
= kmalloc(sizeof(*hold_bus_node
), GFP_KERNEL
);
2491 hold_IO_node
= kmalloc(sizeof(*hold_IO_node
), GFP_KERNEL
);
2492 hold_mem_node
= kmalloc(sizeof(*hold_mem_node
), GFP_KERNEL
);
2493 hold_p_mem_node
= kmalloc(sizeof(*hold_p_mem_node
), GFP_KERNEL
);
2495 if (!hold_bus_node
|| !hold_IO_node
|| !hold_mem_node
|| !hold_p_mem_node
) {
2496 kfree(hold_bus_node
);
2497 kfree(hold_IO_node
);
2498 kfree(hold_mem_node
);
2499 kfree(hold_p_mem_node
);
2504 memcpy(hold_bus_node
, bus_node
, sizeof(struct pci_resource
));
2506 bus_node
->base
+= 1;
2507 bus_node
->length
-= 1;
2508 bus_node
->next
= NULL
;
2510 /* If we have IO resources copy them and fill in the bridge's
2511 * IO range registers */
2513 memcpy(hold_IO_node
, io_node
, sizeof(struct pci_resource
));
2514 io_node
->next
= NULL
;
2516 /* set IO base and Limit registers */
2517 temp_byte
= io_node
->base
>> 8;
2518 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2520 temp_byte
= (io_node
->base
+ io_node
->length
- 1) >> 8;
2521 rc
= pci_bus_write_config_byte(pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2523 kfree(hold_IO_node
);
2524 hold_IO_node
= NULL
;
2527 /* If we have memory resources copy them and fill in the
2528 * bridge's memory range registers. Otherwise, fill in the
2529 * range registers with values that disable them. */
2531 memcpy(hold_mem_node
, mem_node
, sizeof(struct pci_resource
));
2532 mem_node
->next
= NULL
;
2534 /* set Mem base and Limit registers */
2535 temp_word
= mem_node
->base
>> 16;
2536 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2538 temp_word
= (mem_node
->base
+ mem_node
->length
- 1) >> 16;
2539 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2542 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2545 rc
= pci_bus_write_config_word(pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2547 kfree(hold_mem_node
);
2548 hold_mem_node
= NULL
;
2551 memcpy(hold_p_mem_node
, p_mem_node
, sizeof(struct pci_resource
));
2552 p_mem_node
->next
= NULL
;
2554 /* set Pre Mem base and Limit registers */
2555 temp_word
= p_mem_node
->base
>> 16;
2556 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2558 temp_word
= (p_mem_node
->base
+ p_mem_node
->length
- 1) >> 16;
2559 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2561 /* Adjust this to compensate for extra adjustment in first loop */
2566 /* Here we actually find the devices and configure them */
2567 for (device
= 0; (device
<= 0x1F) && !rc
; device
++) {
2568 irqs
.barber_pole
= (irqs
.barber_pole
+ 1) & 0x03;
2571 pci_bus
->number
= hold_bus_node
->base
;
2572 pci_bus_read_config_dword (pci_bus
, PCI_DEVFN(device
, 0), 0x00, &ID
);
2573 pci_bus
->number
= func
->bus
;
2575 if (ID
!= 0xFFFFFFFF) { /* device present */
2576 /* Setup slot structure. */
2577 new_slot
= cpqhp_slot_create(hold_bus_node
->base
);
2579 if (new_slot
== NULL
) {
2584 new_slot
->bus
= hold_bus_node
->base
;
2585 new_slot
->device
= device
;
2586 new_slot
->function
= 0;
2587 new_slot
->is_a_board
= 1;
2588 new_slot
->status
= 0;
2590 rc
= configure_new_device(ctrl
, new_slot
, 1, &temp_resources
);
2591 dbg("configure_new_device rc=0x%x\n",rc
);
2592 } /* End of IF (device in slot?) */
2593 } /* End of FOR loop */
2597 /* save the interrupt routing information */
2598 if (resources
->irqs
) {
2599 resources
->irqs
->interrupt
[0] = irqs
.interrupt
[0];
2600 resources
->irqs
->interrupt
[1] = irqs
.interrupt
[1];
2601 resources
->irqs
->interrupt
[2] = irqs
.interrupt
[2];
2602 resources
->irqs
->interrupt
[3] = irqs
.interrupt
[3];
2603 resources
->irqs
->valid_INT
= irqs
.valid_INT
;
2604 } else if (!behind_bridge
) {
2605 /* We need to hook up the interrupts here */
2606 for (cloop
= 0; cloop
< 4; cloop
++) {
2607 if (irqs
.valid_INT
& (0x01 << cloop
)) {
2608 rc
= cpqhp_set_irq(func
->bus
, func
->device
,
2609 0x0A + cloop
, irqs
.interrupt
[cloop
]);
2613 } /* end of for loop */
2615 /* Return unused bus resources
2616 * First use the temporary node to store information for
2618 if (hold_bus_node
&& bus_node
&& temp_resources
.bus_head
) {
2619 hold_bus_node
->length
= bus_node
->base
- hold_bus_node
->base
;
2621 hold_bus_node
->next
= func
->bus_head
;
2622 func
->bus_head
= hold_bus_node
;
2624 temp_byte
= temp_resources
.bus_head
->base
- 1;
2626 /* set subordinate bus */
2627 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, temp_byte
);
2629 if (temp_resources
.bus_head
->length
== 0) {
2630 kfree(temp_resources
.bus_head
);
2631 temp_resources
.bus_head
= NULL
;
2633 return_resource(&(resources
->bus_head
), temp_resources
.bus_head
);
2637 /* If we have IO space available and there is some left,
2638 * return the unused portion */
2639 if (hold_IO_node
&& temp_resources
.io_head
) {
2640 io_node
= do_pre_bridge_resource_split(&(temp_resources
.io_head
),
2641 &hold_IO_node
, 0x1000);
2643 /* Check if we were able to split something off */
2645 hold_IO_node
->base
= io_node
->base
+ io_node
->length
;
2647 temp_byte
= (hold_IO_node
->base
) >> 8;
2648 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_BASE
, temp_byte
);
2650 return_resource(&(resources
->io_head
), io_node
);
2653 io_node
= do_bridge_resource_split(&(temp_resources
.io_head
), 0x1000);
2655 /* Check if we were able to split something off */
2657 /* First use the temporary node to store
2658 * information for the board */
2659 hold_IO_node
->length
= io_node
->base
- hold_IO_node
->base
;
2661 /* If we used any, add it to the board's list */
2662 if (hold_IO_node
->length
) {
2663 hold_IO_node
->next
= func
->io_head
;
2664 func
->io_head
= hold_IO_node
;
2666 temp_byte
= (io_node
->base
- 1) >> 8;
2667 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_byte
);
2669 return_resource(&(resources
->io_head
), io_node
);
2671 /* it doesn't need any IO */
2673 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_IO_LIMIT
, temp_word
);
2675 return_resource(&(resources
->io_head
), io_node
);
2676 kfree(hold_IO_node
);
2679 /* it used most of the range */
2680 hold_IO_node
->next
= func
->io_head
;
2681 func
->io_head
= hold_IO_node
;
2683 } else if (hold_IO_node
) {
2684 /* it used the whole range */
2685 hold_IO_node
->next
= func
->io_head
;
2686 func
->io_head
= hold_IO_node
;
2688 /* If we have memory space available and there is some left,
2689 * return the unused portion */
2690 if (hold_mem_node
&& temp_resources
.mem_head
) {
2691 mem_node
= do_pre_bridge_resource_split(&(temp_resources
. mem_head
),
2692 &hold_mem_node
, 0x100000);
2694 /* Check if we were able to split something off */
2696 hold_mem_node
->base
= mem_node
->base
+ mem_node
->length
;
2698 temp_word
= (hold_mem_node
->base
) >> 16;
2699 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_BASE
, temp_word
);
2701 return_resource(&(resources
->mem_head
), mem_node
);
2704 mem_node
= do_bridge_resource_split(&(temp_resources
.mem_head
), 0x100000);
2706 /* Check if we were able to split something off */
2708 /* First use the temporary node to store
2709 * information for the board */
2710 hold_mem_node
->length
= mem_node
->base
- hold_mem_node
->base
;
2712 if (hold_mem_node
->length
) {
2713 hold_mem_node
->next
= func
->mem_head
;
2714 func
->mem_head
= hold_mem_node
;
2716 /* configure end address */
2717 temp_word
= (mem_node
->base
- 1) >> 16;
2718 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2720 /* Return unused resources to the pool */
2721 return_resource(&(resources
->mem_head
), mem_node
);
2723 /* it doesn't need any Mem */
2725 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_MEMORY_LIMIT
, temp_word
);
2727 return_resource(&(resources
->mem_head
), mem_node
);
2728 kfree(hold_mem_node
);
2731 /* it used most of the range */
2732 hold_mem_node
->next
= func
->mem_head
;
2733 func
->mem_head
= hold_mem_node
;
2735 } else if (hold_mem_node
) {
2736 /* it used the whole range */
2737 hold_mem_node
->next
= func
->mem_head
;
2738 func
->mem_head
= hold_mem_node
;
2740 /* If we have prefetchable memory space available and there
2741 * is some left at the end, return the unused portion */
2742 if (hold_p_mem_node
&& temp_resources
.p_mem_head
) {
2743 p_mem_node
= do_pre_bridge_resource_split(&(temp_resources
.p_mem_head
),
2744 &hold_p_mem_node
, 0x100000);
2746 /* Check if we were able to split something off */
2748 hold_p_mem_node
->base
= p_mem_node
->base
+ p_mem_node
->length
;
2750 temp_word
= (hold_p_mem_node
->base
) >> 16;
2751 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, temp_word
);
2753 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2756 p_mem_node
= do_bridge_resource_split(&(temp_resources
.p_mem_head
), 0x100000);
2758 /* Check if we were able to split something off */
2760 /* First use the temporary node to store
2761 * information for the board */
2762 hold_p_mem_node
->length
= p_mem_node
->base
- hold_p_mem_node
->base
;
2764 /* If we used any, add it to the board's list */
2765 if (hold_p_mem_node
->length
) {
2766 hold_p_mem_node
->next
= func
->p_mem_head
;
2767 func
->p_mem_head
= hold_p_mem_node
;
2769 temp_word
= (p_mem_node
->base
- 1) >> 16;
2770 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2772 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2774 /* it doesn't need any PMem */
2776 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, temp_word
);
2778 return_resource(&(resources
->p_mem_head
), p_mem_node
);
2779 kfree(hold_p_mem_node
);
2782 /* it used the most of the range */
2783 hold_p_mem_node
->next
= func
->p_mem_head
;
2784 func
->p_mem_head
= hold_p_mem_node
;
2786 } else if (hold_p_mem_node
) {
2787 /* it used the whole range */
2788 hold_p_mem_node
->next
= func
->p_mem_head
;
2789 func
->p_mem_head
= hold_p_mem_node
;
2791 /* We should be configuring an IRQ and the bridge's base address
2792 * registers if it needs them. Although we have never seen such
2796 command
= 0x0157; /* = PCI_COMMAND_IO |
2797 * PCI_COMMAND_MEMORY |
2798 * PCI_COMMAND_MASTER |
2799 * PCI_COMMAND_INVALIDATE |
2800 * PCI_COMMAND_PARITY |
2801 * PCI_COMMAND_SERR */
2802 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_COMMAND
, command
);
2804 /* set Bridge Control Register */
2805 command
= 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2806 * PCI_BRIDGE_CTL_SERR |
2807 * PCI_BRIDGE_CTL_NO_ISA */
2808 rc
= pci_bus_write_config_word (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, command
);
2809 } else if ((temp_byte
& 0x7F) == PCI_HEADER_TYPE_NORMAL
) {
2810 /* Standard device */
2811 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2813 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
2814 /* Display (video) adapter (not supported) */
2815 return DEVICE_TYPE_NOT_SUPPORTED
;
2817 /* Figure out IO and memory needs */
2818 for (cloop
= 0x10; cloop
<= 0x24; cloop
+= 4) {
2819 temp_register
= 0xFFFFFFFF;
2821 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus
->number
, devfn
, cloop
);
2822 rc
= pci_bus_write_config_dword (pci_bus
, devfn
, cloop
, temp_register
);
2824 rc
= pci_bus_read_config_dword (pci_bus
, devfn
, cloop
, &temp_register
);
2825 dbg("CND: base = 0x%x\n", temp_register
);
2827 if (temp_register
) { /* If this register is implemented */
2828 if ((temp_register
& 0x03L
) == 0x01) {
2831 /* set base = amount of IO space */
2832 base
= temp_register
& 0xFFFFFFFC;
2835 dbg("CND: length = 0x%x\n", base
);
2836 io_node
= get_io_resource(&(resources
->io_head
), base
);
2837 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2838 io_node
->base
, io_node
->length
, io_node
->next
);
2839 dbg("func (%p) io_head (%p)\n", func
, func
->io_head
);
2841 /* allocate the resource to the board */
2843 base
= io_node
->base
;
2845 io_node
->next
= func
->io_head
;
2846 func
->io_head
= io_node
;
2849 } else if ((temp_register
& 0x0BL
) == 0x08) {
2850 /* Map prefetchable memory */
2851 base
= temp_register
& 0xFFFFFFF0;
2854 dbg("CND: length = 0x%x\n", base
);
2855 p_mem_node
= get_resource(&(resources
->p_mem_head
), base
);
2857 /* allocate the resource to the board */
2859 base
= p_mem_node
->base
;
2861 p_mem_node
->next
= func
->p_mem_head
;
2862 func
->p_mem_head
= p_mem_node
;
2865 } else if ((temp_register
& 0x0BL
) == 0x00) {
2867 base
= temp_register
& 0xFFFFFFF0;
2870 dbg("CND: length = 0x%x\n", base
);
2871 mem_node
= get_resource(&(resources
->mem_head
), base
);
2873 /* allocate the resource to the board */
2875 base
= mem_node
->base
;
2877 mem_node
->next
= func
->mem_head
;
2878 func
->mem_head
= mem_node
;
2881 } else if ((temp_register
& 0x0BL
) == 0x04) {
2883 base
= temp_register
& 0xFFFFFFF0;
2886 dbg("CND: length = 0x%x\n", base
);
2887 mem_node
= get_resource(&(resources
->mem_head
), base
);
2889 /* allocate the resource to the board */
2891 base
= mem_node
->base
;
2893 mem_node
->next
= func
->mem_head
;
2894 func
->mem_head
= mem_node
;
2897 } else if ((temp_register
& 0x0BL
) == 0x06) {
2898 /* Those bits are reserved, we can't handle this */
2901 /* Requesting space below 1M */
2902 return NOT_ENOUGH_RESOURCES
;
2905 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2907 /* Check for 64-bit base */
2908 if ((temp_register
& 0x07L
) == 0x04) {
2911 /* Upper 32 bits of address always zero
2912 * on today's systems */
2913 /* FIXME this is probably not true on
2914 * Alpha and ia64??? */
2916 rc
= pci_bus_write_config_dword(pci_bus
, devfn
, cloop
, base
);
2919 } /* End of base register loop */
2920 if (cpqhp_legacy_mode
) {
2921 /* Figure out which interrupt pin this function uses */
2922 rc
= pci_bus_read_config_byte (pci_bus
, devfn
,
2923 PCI_INTERRUPT_PIN
, &temp_byte
);
2925 /* If this function needs an interrupt and we are behind
2926 * a bridge and the pin is tied to something that's
2927 * alread mapped, set this one the same */
2928 if (temp_byte
&& resources
->irqs
&&
2929 (resources
->irqs
->valid_INT
&
2930 (0x01 << ((temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03)))) {
2931 /* We have to share with something already set up */
2932 IRQ
= resources
->irqs
->interrupt
[(temp_byte
+
2933 resources
->irqs
->barber_pole
- 1) & 0x03];
2935 /* Program IRQ based on card type */
2936 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
2938 if (class_code
== PCI_BASE_CLASS_STORAGE
) {
2939 IRQ
= cpqhp_disk_irq
;
2941 IRQ
= cpqhp_nic_irq
;
2946 rc
= pci_bus_write_config_byte (pci_bus
, devfn
, PCI_INTERRUPT_LINE
, IRQ
);
2949 if (!behind_bridge
) {
2950 rc
= cpqhp_set_irq(func
->bus
, func
->device
, temp_byte
+ 0x09, IRQ
);
2954 /* TBD - this code may also belong in the other clause
2955 * of this If statement */
2956 resources
->irqs
->interrupt
[(temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03] = IRQ
;
2957 resources
->irqs
->valid_INT
|= 0x01 << (temp_byte
+ resources
->irqs
->barber_pole
- 1) & 0x03;
2962 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
2963 PCI_LATENCY_TIMER
, temp_byte
);
2965 /* Cache Line size */
2967 rc
= pci_bus_write_config_byte(pci_bus
, devfn
,
2968 PCI_CACHE_LINE_SIZE
, temp_byte
);
2970 /* disable ROM base Address */
2972 rc
= pci_bus_write_config_word(pci_bus
, devfn
,
2973 PCI_ROM_ADDRESS
, temp_dword
);
2976 temp_word
= 0x0157; /* = PCI_COMMAND_IO |
2977 * PCI_COMMAND_MEMORY |
2978 * PCI_COMMAND_MASTER |
2979 * PCI_COMMAND_INVALIDATE |
2980 * PCI_COMMAND_PARITY |
2981 * PCI_COMMAND_SERR */
2982 rc
= pci_bus_write_config_word (pci_bus
, devfn
,
2983 PCI_COMMAND
, temp_word
);
2984 } else { /* End of Not-A-Bridge else */
2985 /* It's some strange type of PCI adapter (Cardbus?) */
2986 return DEVICE_TYPE_NOT_SUPPORTED
;
2989 func
->configured
= 1;
2993 cpqhp_destroy_resource_list (&temp_resources
);
2995 return_resource(&(resources
-> bus_head
), hold_bus_node
);
2996 return_resource(&(resources
-> io_head
), hold_IO_node
);
2997 return_resource(&(resources
-> mem_head
), hold_mem_node
);
2998 return_resource(&(resources
-> p_mem_head
), hold_p_mem_node
);