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/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/workqueue.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/wait.h>
38 #include <linux/smp_lock.h>
39 #include <linux/pci.h>
42 static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,
43 u8 behind_bridge, struct resource_lists *resources);
44 static int configure_new_function(struct controller* ctrl, struct pci_func *func,
45 u8 behind_bridge, struct resource_lists *resources);
46 static void interrupt_event_handler(struct controller *ctrl);
48 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
49 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
50 static int event_finished;
51 static unsigned long pushbutton_pending; /* = 0 */
53 /* things needed for the long_delay function */
54 static struct semaphore delay_sem;
55 static wait_queue_head_t delay_wait;
57 /* delay is in jiffies to wait for */
58 static void long_delay(int delay)
60 DECLARE_WAITQUEUE(wait, current);
62 /* only allow 1 customer into the delay queue at once
63 * yes this makes some people wait even longer, but who really cares?
64 * this is for _huge_ delays to make the hardware happy as the
65 * signals bounce around
69 init_waitqueue_head(&delay_wait);
71 add_wait_queue(&delay_wait, &wait);
72 set_current_state(TASK_INTERRUPTIBLE);
73 schedule_timeout(delay);
74 remove_wait_queue(&delay_wait, &wait);
75 set_current_state(TASK_RUNNING);
81 /* FIXME: The following line needs to be somewhere else... */
82 #define WRONG_BUS_FREQUENCY 0x07
83 static u8 handle_switch_change(u8 change, struct controller * ctrl)
88 struct pci_func *func;
89 struct event_info *taskInfo;
95 dbg("cpqsbd: Switch interrupt received.\n");
97 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
98 if (change & (0x1L << hp_slot)) {
99 /**********************************
101 **********************************/
102 func = cpqhp_slot_find(ctrl->bus,
103 (hp_slot + ctrl->slot_device_offset), 0);
105 /* this is the structure that tells the worker thread
107 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
108 ctrl->next_event = (ctrl->next_event + 1) % 10;
109 taskInfo->hp_slot = hp_slot;
113 temp_word = ctrl->ctrl_int_comp >> 16;
114 func->presence_save = (temp_word >> hp_slot) & 0x01;
115 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
117 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
118 /**********************************
120 **********************************/
122 func->switch_save = 0;
124 taskInfo->event_type = INT_SWITCH_OPEN;
126 /**********************************
128 **********************************/
130 func->switch_save = 0x10;
132 taskInfo->event_type = INT_SWITCH_CLOSE;
141 * cpqhp_find_slot: find the struct slot of given device
142 * @ctrl: scan lots of this controller
143 * @device: the device id to find
145 static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
147 struct slot *slot = ctrl->slot;
149 while (slot && (slot->device != device)) {
157 static u8 handle_presence_change(u16 change, struct controller * ctrl)
163 struct pci_func *func;
164 struct event_info *taskInfo;
170 /**********************************
172 **********************************/
173 dbg("cpqsbd: Presence/Notify input change.\n");
174 dbg(" Changed bits are 0x%4.4x\n", change );
176 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
177 if (change & (0x0101 << hp_slot)) {
178 /**********************************
180 **********************************/
181 func = cpqhp_slot_find(ctrl->bus,
182 (hp_slot + ctrl->slot_device_offset), 0);
184 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
185 ctrl->next_event = (ctrl->next_event + 1) % 10;
186 taskInfo->hp_slot = hp_slot;
190 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
194 /* If the switch closed, must be a button
195 * If not in button mode, nevermind */
196 if (func->switch_save && (ctrl->push_button == 1)) {
197 temp_word = ctrl->ctrl_int_comp >> 16;
198 temp_byte = (temp_word >> hp_slot) & 0x01;
199 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
201 if (temp_byte != func->presence_save) {
202 /**************************************
203 * button Pressed (doesn't do anything)
204 **************************************/
205 dbg("hp_slot %d button pressed\n", hp_slot);
206 taskInfo->event_type = INT_BUTTON_PRESS;
208 /**********************************
209 * button Released - TAKE ACTION!!!!
210 **********************************/
211 dbg("hp_slot %d button released\n", hp_slot);
212 taskInfo->event_type = INT_BUTTON_RELEASE;
214 /* Cancel if we are still blinking */
215 if ((p_slot->state == BLINKINGON_STATE)
216 || (p_slot->state == BLINKINGOFF_STATE)) {
217 taskInfo->event_type = INT_BUTTON_CANCEL;
218 dbg("hp_slot %d button cancel\n", hp_slot);
219 } else if ((p_slot->state == POWERON_STATE)
220 || (p_slot->state == POWEROFF_STATE)) {
221 /* info(msg_button_ignore, p_slot->number); */
222 taskInfo->event_type = INT_BUTTON_IGNORE;
223 dbg("hp_slot %d button ignore\n", hp_slot);
227 /* Switch is open, assume a presence change
228 * Save the presence state */
229 temp_word = ctrl->ctrl_int_comp >> 16;
230 func->presence_save = (temp_word >> hp_slot) & 0x01;
231 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
233 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
234 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
236 taskInfo->event_type = INT_PRESENCE_ON;
239 taskInfo->event_type = INT_PRESENCE_OFF;
249 static u8 handle_power_fault(u8 change, struct controller * ctrl)
253 struct pci_func *func;
254 struct event_info *taskInfo;
259 /**********************************
261 **********************************/
263 info("power fault interrupt\n");
265 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
266 if (change & (0x01 << hp_slot)) {
267 /**********************************
269 **********************************/
270 func = cpqhp_slot_find(ctrl->bus,
271 (hp_slot + ctrl->slot_device_offset), 0);
273 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
274 ctrl->next_event = (ctrl->next_event + 1) % 10;
275 taskInfo->hp_slot = hp_slot;
279 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
280 /**********************************
281 * power fault Cleared
282 **********************************/
285 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
287 /**********************************
289 **********************************/
290 taskInfo->event_type = INT_POWER_FAULT;
293 amber_LED_on (ctrl, hp_slot);
294 green_LED_off (ctrl, hp_slot);
297 /* this is a fatal condition, we want
298 * to crash the machine to protect from
299 * data corruption. simulated_NMI
300 * shouldn't ever return */
302 simulated_NMI(hp_slot, ctrl); */
304 /* The following code causes a software
305 * crash just in case simulated_NMI did
308 panic(msg_power_fault); */
310 /* set power fault status for this board */
312 info("power fault bit %x set\n", hp_slot);
323 * sort_by_size: sort nodes on the list by their length, smallest first.
324 * @head: list to sort
327 static int sort_by_size(struct pci_resource **head)
329 struct pci_resource *current_res;
330 struct pci_resource *next_res;
331 int out_of_order = 1;
336 if (!((*head)->next))
339 while (out_of_order) {
342 /* Special case for swapping list head */
343 if (((*head)->next) &&
344 ((*head)->length > (*head)->next->length)) {
347 *head = (*head)->next;
348 current_res->next = (*head)->next;
349 (*head)->next = current_res;
354 while (current_res->next && current_res->next->next) {
355 if (current_res->next->length > current_res->next->next->length) {
357 next_res = current_res->next;
358 current_res->next = current_res->next->next;
359 current_res = current_res->next;
360 next_res->next = current_res->next;
361 current_res->next = next_res;
363 current_res = current_res->next;
365 } /* End of out_of_order loop */
372 * sort_by_max_size: sort nodes on the list by their length, largest first.
373 * @head: list to sort
376 static int sort_by_max_size(struct pci_resource **head)
378 struct pci_resource *current_res;
379 struct pci_resource *next_res;
380 int out_of_order = 1;
385 if (!((*head)->next))
388 while (out_of_order) {
391 /* Special case for swapping list head */
392 if (((*head)->next) &&
393 ((*head)->length < (*head)->next->length)) {
396 *head = (*head)->next;
397 current_res->next = (*head)->next;
398 (*head)->next = current_res;
403 while (current_res->next && current_res->next->next) {
404 if (current_res->next->length < current_res->next->next->length) {
406 next_res = current_res->next;
407 current_res->next = current_res->next->next;
408 current_res = current_res->next;
409 next_res->next = current_res->next;
410 current_res->next = next_res;
412 current_res = current_res->next;
414 } /* End of out_of_order loop */
421 * do_pre_bridge_resource_split: find node of resources that are unused
424 static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
425 struct pci_resource **orig_head, u32 alignment)
427 struct pci_resource *prevnode = NULL;
428 struct pci_resource *node;
429 struct pci_resource *split_node;
432 dbg("do_pre_bridge_resource_split\n");
434 if (!(*head) || !(*orig_head))
437 rc = cpqhp_resource_sort_and_combine(head);
442 if ((*head)->base != (*orig_head)->base)
445 if ((*head)->length == (*orig_head)->length)
449 /* If we got here, there the bridge requires some of the resource, but
450 * we may be able to split some off of the front */
454 if (node->length & (alignment -1)) {
455 /* this one isn't an aligned length, so we'll make a new entry
456 * and split it up. */
457 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
462 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
464 split_node->base = node->base;
465 split_node->length = temp_dword;
467 node->length -= temp_dword;
468 node->base += split_node->length;
470 /* Put it in the list */
472 split_node->next = node;
475 if (node->length < alignment)
483 while (prevnode->next != node)
484 prevnode = prevnode->next;
486 prevnode->next = node->next;
495 * do_bridge_resource_split: find one node of resources that aren't in use
498 static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
500 struct pci_resource *prevnode = NULL;
501 struct pci_resource *node;
505 rc = cpqhp_resource_sort_and_combine(head);
518 if (node->length < alignment)
521 if (node->base & (alignment - 1)) {
522 /* Short circuit if adjusted size is too small */
523 temp_dword = (node->base | (alignment-1)) + 1;
524 if ((node->length - (temp_dword - node->base)) < alignment)
527 node->length -= (temp_dword - node->base);
528 node->base = temp_dword;
531 if (node->length & (alignment - 1))
532 /* There's stuff in use after this node */
543 * get_io_resource: find first node of given size not in ISA aliasing window.
544 * @head: list to search
545 * @size: size of node to find, must be a power of two.
547 * Description: this function sorts the resource list by size and then returns
548 * returns the first node of "size" length that is not in the ISA aliasing
549 * window. If it finds a node larger than "size" it will split it up.
552 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
554 struct pci_resource *prevnode;
555 struct pci_resource *node;
556 struct pci_resource *split_node;
562 if ( cpqhp_resource_sort_and_combine(head) )
565 if ( sort_by_size(head) )
568 for (node = *head; node; node = node->next) {
569 if (node->length < size)
572 if (node->base & (size - 1)) {
573 /* this one isn't base aligned properly
574 * so we'll make a new entry and split it up */
575 temp_dword = (node->base | (size-1)) + 1;
577 /* Short circuit if adjusted size is too small */
578 if ((node->length - (temp_dword - node->base)) < size)
581 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
586 split_node->base = node->base;
587 split_node->length = temp_dword - node->base;
588 node->base = temp_dword;
589 node->length -= split_node->length;
591 /* Put it in the list */
592 split_node->next = node->next;
593 node->next = split_node;
594 } /* End of non-aligned base */
596 /* Don't need to check if too small since we already did */
597 if (node->length > size) {
598 /* this one is longer than we need
599 * so we'll make a new entry and split it up */
600 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
605 split_node->base = node->base + size;
606 split_node->length = node->length - size;
609 /* Put it in the list */
610 split_node->next = node->next;
611 node->next = split_node;
612 } /* End of too big on top end */
614 /* For IO make sure it's not in the ISA aliasing space */
615 if (node->base & 0x300L)
618 /* If we got here, then it is the right size
619 * Now take it out of the list and break */
624 while (prevnode->next != node)
625 prevnode = prevnode->next;
627 prevnode->next = node->next;
638 * get_max_resource: get largest node which has at least the given size.
639 * @head: the list to search the node in
640 * @size: the minimum size of the node to find
642 * Description: Gets the largest node that is at least "size" big from the
643 * list pointed to by head. It aligns the node on top and bottom
644 * to "size" alignment before returning it.
646 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
648 struct pci_resource *max;
649 struct pci_resource *temp;
650 struct pci_resource *split_node;
653 if (cpqhp_resource_sort_and_combine(head))
656 if (sort_by_max_size(head))
659 for (max = *head; max; max = max->next) {
660 /* If not big enough we could probably just bail,
661 * instead we'll continue to the next. */
662 if (max->length < size)
665 if (max->base & (size - 1)) {
666 /* this one isn't base aligned properly
667 * so we'll make a new entry and split it up */
668 temp_dword = (max->base | (size-1)) + 1;
670 /* Short circuit if adjusted size is too small */
671 if ((max->length - (temp_dword - max->base)) < size)
674 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
679 split_node->base = max->base;
680 split_node->length = temp_dword - max->base;
681 max->base = temp_dword;
682 max->length -= split_node->length;
684 split_node->next = max->next;
685 max->next = split_node;
688 if ((max->base + max->length) & (size - 1)) {
689 /* this one isn't end aligned properly at the top
690 * so we'll make a new entry and split it up */
691 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
695 temp_dword = ((max->base + max->length) & ~(size - 1));
696 split_node->base = temp_dword;
697 split_node->length = max->length + max->base
699 max->length -= split_node->length;
701 split_node->next = max->next;
702 max->next = split_node;
705 /* Make sure it didn't shrink too much when we aligned it */
706 if (max->length < size)
709 /* Now take it out of the list */
714 while (temp && temp->next != max) {
718 temp->next = max->next;
730 * get_resource: find resource of given size and split up larger ones.
731 * @head: the list to search for resources
732 * @size: the size limit to use
734 * Description: This function sorts the resource list by size and then
735 * returns the first node of "size" length. If it finds a node
736 * larger than "size" it will split it up.
738 * size must be a power of two.
740 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
742 struct pci_resource *prevnode;
743 struct pci_resource *node;
744 struct pci_resource *split_node;
747 if (cpqhp_resource_sort_and_combine(head))
750 if (sort_by_size(head))
753 for (node = *head; node; node = node->next) {
754 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
755 __FUNCTION__, size, node, node->base, node->length);
756 if (node->length < size)
759 if (node->base & (size - 1)) {
760 dbg("%s: not aligned\n", __FUNCTION__);
761 /* this one isn't base aligned properly
762 * so we'll make a new entry and split it up */
763 temp_dword = (node->base | (size-1)) + 1;
765 /* Short circuit if adjusted size is too small */
766 if ((node->length - (temp_dword - node->base)) < size)
769 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
774 split_node->base = node->base;
775 split_node->length = temp_dword - node->base;
776 node->base = temp_dword;
777 node->length -= split_node->length;
779 split_node->next = node->next;
780 node->next = split_node;
781 } /* End of non-aligned base */
783 /* Don't need to check if too small since we already did */
784 if (node->length > size) {
785 dbg("%s: too big\n", __FUNCTION__);
786 /* this one is longer than we need
787 * so we'll make a new entry and split it up */
788 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
793 split_node->base = node->base + size;
794 split_node->length = node->length - size;
797 /* Put it in the list */
798 split_node->next = node->next;
799 node->next = split_node;
800 } /* End of too big on top end */
802 dbg("%s: got one!!!\n", __FUNCTION__);
803 /* If we got here, then it is the right size
804 * Now take it out of the list */
809 while (prevnode->next != node)
810 prevnode = prevnode->next;
812 prevnode->next = node->next;
822 * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
823 * @head: the list to sort and clean up
825 * Description: Sorts all of the nodes in the list in ascending order by
826 * their base addresses. Also does garbage collection by
827 * combining adjacent nodes.
829 * returns 0 if success
831 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
833 struct pci_resource *node1;
834 struct pci_resource *node2;
835 int out_of_order = 1;
837 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
842 dbg("*head->next = %p\n",(*head)->next);
845 return 0; /* only one item on the list, already sorted! */
847 dbg("*head->base = 0x%x\n",(*head)->base);
848 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
849 while (out_of_order) {
852 /* Special case for swapping list head */
853 if (((*head)->next) &&
854 ((*head)->base > (*head)->next->base)) {
856 (*head) = (*head)->next;
857 node1->next = (*head)->next;
858 (*head)->next = node1;
864 while (node1->next && node1->next->next) {
865 if (node1->next->base > node1->next->next->base) {
868 node1->next = node1->next->next;
870 node2->next = node1->next;
875 } /* End of out_of_order loop */
879 while (node1 && node1->next) {
880 if ((node1->base + node1->length) == node1->next->base) {
883 node1->length += node1->next->length;
885 node1->next = node1->next->next;
895 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data, struct pt_regs *regs)
897 struct controller *ctrl = data;
898 u8 schedule_flag = 0;
905 misc = readw(ctrl->hpc_reg + MISC);
906 /***************************************
907 * Check to see if it was our interrupt
908 ***************************************/
909 if (!(misc & 0x000C)) {
914 /**********************************
915 * Serial Output interrupt Pending
916 **********************************/
918 /* Clear the interrupt */
920 writew(misc, ctrl->hpc_reg + MISC);
922 /* Read to clear posted writes */
923 misc = readw(ctrl->hpc_reg + MISC);
925 dbg ("%s - waking up\n", __FUNCTION__);
926 wake_up_interruptible(&ctrl->queue);
930 /* General-interrupt-input interrupt Pending */
931 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
933 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
935 /* Clear the interrupt */
936 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
938 /* Read it back to clear any posted writes */
939 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
942 /* Clear all interrupts */
943 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
945 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
946 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
947 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
950 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
952 /* Bus reset has completed */
954 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
955 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
956 wake_up_interruptible(&ctrl->queue);
960 up(&event_semaphore);
961 dbg("Signal event_semaphore\n");
968 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
969 * @busnumber - bus where new node is to be located
971 * Returns pointer to the new node or NULL if unsuccessful
973 struct pci_func *cpqhp_slot_create(u8 busnumber)
975 struct pci_func *new_slot;
976 struct pci_func *next;
978 new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
980 if (new_slot == NULL) {
986 memset(new_slot, 0, sizeof(struct pci_func));
988 new_slot->next = NULL;
989 new_slot->configured = 1;
991 if (cpqhp_slot_list[busnumber] == NULL) {
992 cpqhp_slot_list[busnumber] = new_slot;
994 next = cpqhp_slot_list[busnumber];
995 while (next->next != NULL)
997 next->next = new_slot;
1004 * slot_remove - Removes a node from the linked list of slots.
1005 * @old_slot: slot to remove
1007 * Returns 0 if successful, !0 otherwise.
1009 static int slot_remove(struct pci_func * old_slot)
1011 struct pci_func *next;
1013 if (old_slot == NULL)
1016 next = cpqhp_slot_list[old_slot->bus];
1022 if (next == old_slot) {
1023 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1024 cpqhp_destroy_board_resources(old_slot);
1029 while ((next->next != old_slot) && (next->next != NULL)) {
1033 if (next->next == old_slot) {
1034 next->next = old_slot->next;
1035 cpqhp_destroy_board_resources(old_slot);
1044 * bridge_slot_remove - Removes a node from the linked list of slots.
1045 * @bridge: bridge to remove
1047 * Returns 0 if successful, !0 otherwise.
1049 static int bridge_slot_remove(struct pci_func *bridge)
1051 u8 subordinateBus, secondaryBus;
1053 struct pci_func *next;
1055 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1056 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1058 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1059 next = cpqhp_slot_list[tempBus];
1061 while (!slot_remove(next)) {
1062 next = cpqhp_slot_list[tempBus];
1066 next = cpqhp_slot_list[bridge->bus];
1071 if (next == bridge) {
1072 cpqhp_slot_list[bridge->bus] = bridge->next;
1076 while ((next->next != bridge) && (next->next != NULL))
1079 if (next->next != bridge)
1081 next->next = bridge->next;
1089 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1091 * @device: device to find
1092 * @index: is 0 for first function found, 1 for the second...
1094 * Returns pointer to the node if successful, %NULL otherwise.
1096 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1099 struct pci_func *func;
1101 func = cpqhp_slot_list[bus];
1103 if ((func == NULL) || ((func->device == device) && (index == 0)))
1106 if (func->device == device)
1109 while (func->next != NULL) {
1112 if (func->device == device)
1123 /* DJZ: I don't think is_bridge will work as is.
1125 static int is_bridge(struct pci_func * func)
1127 /* Check the header type */
1128 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1136 * set_controller_speed - set the frequency and/or mode of a specific
1137 * controller segment.
1139 * @ctrl: controller to change frequency/mode for.
1140 * @adapter_speed: the speed of the adapter we want to match.
1141 * @hp_slot: the slot number where the adapter is installed.
1143 * Returns 0 if we successfully change frequency and/or mode to match the
1147 static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1151 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1153 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
1155 if (ctrl->speed == adapter_speed)
1158 /* We don't allow freq/mode changes if we find another adapter running
1159 * in another slot on this controller */
1160 for(slot = ctrl->slot; slot; slot = slot->next) {
1161 if (slot->device == (hp_slot + ctrl->slot_device_offset))
1163 if (!slot->hotplug_slot && !slot->hotplug_slot->info)
1165 if (slot->hotplug_slot->info->adapter_status == 0)
1167 /* If another adapter is running on the same segment but at a
1168 * lower speed/mode, we allow the new adapter to function at
1169 * this rate if supported */
1170 if (ctrl->speed < adapter_speed)
1176 /* If the controller doesn't support freq/mode changes and the
1177 * controller is running at a higher mode, we bail */
1178 if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1181 /* But we allow the adapter to run at a lower rate if possible */
1182 if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1185 /* We try to set the max speed supported by both the adapter and
1187 if (ctrl->speed_capability < adapter_speed) {
1188 if (ctrl->speed == ctrl->speed_capability)
1190 adapter_speed = ctrl->speed_capability;
1193 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1194 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
1197 wait_for_ctrl_irq(ctrl);
1199 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1203 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1205 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1207 switch(adapter_speed) {
1208 case(PCI_SPEED_133MHz_PCIX):
1212 case(PCI_SPEED_100MHz_PCIX):
1216 case(PCI_SPEED_66MHz_PCIX):
1220 case(PCI_SPEED_66MHz):
1224 default: /* 33MHz PCI 2.2 */
1230 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
1234 /* Reenable interrupts */
1235 writel(0, ctrl->hpc_reg + INT_MASK);
1237 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1239 /* Restart state machine */
1241 pci_read_config_byte(ctrl->pci_dev, 0x43, ®);
1242 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
1244 /* Only if mode change...*/
1245 if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1246 ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1249 wait_for_ctrl_irq(ctrl);
1252 /* Restore LED/Slot state */
1253 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1254 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
1257 wait_for_ctrl_irq(ctrl);
1259 ctrl->speed = adapter_speed;
1260 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1262 info("Successfully changed frequency/mode for adapter in slot %d\n",
1267 /* the following routines constitute the bulk of the
1268 hotplug controller logic
1273 * board_replaced - Called after a board has been replaced in the system.
1275 * This is only used if we don't have resources for hot add
1276 * Turns power on for the board
1277 * Checks to see if board is the same
1278 * If board is same, reconfigures it
1279 * If board isn't same, turns it back off.
1282 static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1291 hp_slot = func->device - ctrl->slot_device_offset;
1293 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
1294 /**********************************
1295 * The switch is open.
1296 **********************************/
1297 rc = INTERLOCK_OPEN;
1298 } else if (is_slot_enabled (ctrl, hp_slot)) {
1299 /**********************************
1300 * The board is already on
1301 **********************************/
1302 rc = CARD_FUNCTIONING;
1304 down(&ctrl->crit_sect);
1306 /* turn on board without attaching to the bus */
1307 enable_slot_power (ctrl, hp_slot);
1311 /* Wait for SOBS to be unset */
1312 wait_for_ctrl_irq (ctrl);
1314 /* Change bits in slot power register to force another shift out
1315 * NOTE: this is to work around the timer bug */
1316 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1317 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1318 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1322 /* Wait for SOBS to be unset */
1323 wait_for_ctrl_irq (ctrl);
1325 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1326 if (ctrl->speed != adapter_speed)
1327 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1328 rc = WRONG_BUS_FREQUENCY;
1330 /* turn off board without attaching to the bus */
1331 disable_slot_power (ctrl, hp_slot);
1335 /* Wait for SOBS to be unset */
1336 wait_for_ctrl_irq (ctrl);
1338 up(&ctrl->crit_sect);
1343 down(&ctrl->crit_sect);
1345 slot_enable (ctrl, hp_slot);
1346 green_LED_blink (ctrl, hp_slot);
1348 amber_LED_off (ctrl, hp_slot);
1352 /* Wait for SOBS to be unset */
1353 wait_for_ctrl_irq (ctrl);
1355 up(&ctrl->crit_sect);
1357 /* Wait for ~1 second because of hot plug spec */
1360 /* Check for a power fault */
1361 if (func->status == 0xFF) {
1362 /* power fault occurred, but it was benign */
1366 rc = cpqhp_valid_replace(ctrl, func);
1369 /* It must be the same board */
1371 rc = cpqhp_configure_board(ctrl, func);
1374 /* If configuration fails, turn it off
1375 * Get slot won't work for devices behind
1376 * bridges, but in this case it will always be
1377 * called for the "base" bus/dev/func of an
1380 down(&ctrl->crit_sect);
1382 amber_LED_on (ctrl, hp_slot);
1383 green_LED_off (ctrl, hp_slot);
1384 slot_disable (ctrl, hp_slot);
1388 /* Wait for SOBS to be unset */
1389 wait_for_ctrl_irq (ctrl);
1391 up(&ctrl->crit_sect);
1400 func->switch_save = 0x10;
1403 while (((func = cpqhp_slot_find(func->bus, func->device, index)) != NULL) && !rc) {
1404 rc |= cpqhp_configure_board(ctrl, func);
1409 /* If configuration fails, turn it off
1410 * Get slot won't work for devices behind
1411 * bridges, but in this case it will always be
1412 * called for the "base" bus/dev/func of an
1415 down(&ctrl->crit_sect);
1417 amber_LED_on (ctrl, hp_slot);
1418 green_LED_off (ctrl, hp_slot);
1419 slot_disable (ctrl, hp_slot);
1423 /* Wait for SOBS to be unset */
1424 wait_for_ctrl_irq (ctrl);
1426 up(&ctrl->crit_sect);
1430 /* Done configuring so turn LED on full time */
1432 down(&ctrl->crit_sect);
1434 green_LED_on (ctrl, hp_slot);
1438 /* Wait for SOBS to be unset */
1439 wait_for_ctrl_irq (ctrl);
1441 up(&ctrl->crit_sect);
1444 /* Something is wrong
1446 * Get slot won't work for devices behind bridges, but
1447 * in this case it will always be called for the "base"
1448 * bus/dev/func of an adapter. */
1450 down(&ctrl->crit_sect);
1452 amber_LED_on (ctrl, hp_slot);
1453 green_LED_off (ctrl, hp_slot);
1454 slot_disable (ctrl, hp_slot);
1458 /* Wait for SOBS to be unset */
1459 wait_for_ctrl_irq (ctrl);
1461 up(&ctrl->crit_sect);
1471 * board_added - Called after a board has been added to the system.
1473 * Turns power on for the board
1477 static u32 board_added(struct pci_func *func, struct controller *ctrl)
1483 u32 temp_register = 0xFFFFFFFF;
1485 struct pci_func *new_slot = NULL;
1486 struct slot *p_slot;
1487 struct resource_lists res_lists;
1489 hp_slot = func->device - ctrl->slot_device_offset;
1490 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1491 __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1493 down(&ctrl->crit_sect);
1495 /* turn on board without attaching to the bus */
1496 enable_slot_power(ctrl, hp_slot);
1500 /* Wait for SOBS to be unset */
1501 wait_for_ctrl_irq (ctrl);
1503 /* Change bits in slot power register to force another shift out
1504 * NOTE: this is to work around the timer bug */
1505 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1506 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1507 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1511 /* Wait for SOBS to be unset */
1512 wait_for_ctrl_irq (ctrl);
1514 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1515 if (ctrl->speed != adapter_speed)
1516 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1517 rc = WRONG_BUS_FREQUENCY;
1519 /* turn off board without attaching to the bus */
1520 disable_slot_power (ctrl, hp_slot);
1524 /* Wait for SOBS to be unset */
1525 wait_for_ctrl_irq(ctrl);
1527 up(&ctrl->crit_sect);
1532 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1534 /* turn on board and blink green LED */
1536 dbg("%s: before down\n", __FUNCTION__);
1537 down(&ctrl->crit_sect);
1538 dbg("%s: after down\n", __FUNCTION__);
1540 dbg("%s: before slot_enable\n", __FUNCTION__);
1541 slot_enable (ctrl, hp_slot);
1543 dbg("%s: before green_LED_blink\n", __FUNCTION__);
1544 green_LED_blink (ctrl, hp_slot);
1546 dbg("%s: before amber_LED_blink\n", __FUNCTION__);
1547 amber_LED_off (ctrl, hp_slot);
1549 dbg("%s: before set_SOGO\n", __FUNCTION__);
1552 /* Wait for SOBS to be unset */
1553 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
1554 wait_for_ctrl_irq (ctrl);
1555 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
1557 dbg("%s: before up\n", __FUNCTION__);
1558 up(&ctrl->crit_sect);
1559 dbg("%s: after up\n", __FUNCTION__);
1561 /* Wait for ~1 second because of hot plug spec */
1562 dbg("%s: before long_delay\n", __FUNCTION__);
1564 dbg("%s: after long_delay\n", __FUNCTION__);
1566 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1567 /* Check for a power fault */
1568 if (func->status == 0xFF) {
1569 /* power fault occurred, but it was benign */
1570 temp_register = 0xFFFFFFFF;
1571 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1575 /* Get vendor/device ID u32 */
1576 ctrl->pci_bus->number = func->bus;
1577 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1578 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
1579 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1582 /* Something's wrong here */
1583 temp_register = 0xFFFFFFFF;
1584 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1586 /* Preset return code. It will be changed later if things go okay. */
1587 rc = NO_ADAPTER_PRESENT;
1590 /* All F's is an empty slot or an invalid board */
1591 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1592 res_lists.io_head = ctrl->io_head;
1593 res_lists.mem_head = ctrl->mem_head;
1594 res_lists.p_mem_head = ctrl->p_mem_head;
1595 res_lists.bus_head = ctrl->bus_head;
1596 res_lists.irqs = NULL;
1598 rc = configure_new_device(ctrl, func, 0, &res_lists);
1600 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1601 ctrl->io_head = res_lists.io_head;
1602 ctrl->mem_head = res_lists.mem_head;
1603 ctrl->p_mem_head = res_lists.p_mem_head;
1604 ctrl->bus_head = res_lists.bus_head;
1606 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1607 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1608 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1609 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1612 down(&ctrl->crit_sect);
1614 amber_LED_on (ctrl, hp_slot);
1615 green_LED_off (ctrl, hp_slot);
1616 slot_disable (ctrl, hp_slot);
1620 /* Wait for SOBS to be unset */
1621 wait_for_ctrl_irq (ctrl);
1623 up(&ctrl->crit_sect);
1626 cpqhp_save_slot_config(ctrl, func);
1631 func->switch_save = 0x10;
1632 func->is_a_board = 0x01;
1634 /* next, we will instantiate the linux pci_dev structures (with
1635 * appropriate driver notification, if already present) */
1636 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
1639 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1640 if (new_slot && !new_slot->pci_dev) {
1641 cpqhp_configure_device(ctrl, new_slot);
1645 down(&ctrl->crit_sect);
1647 green_LED_on (ctrl, hp_slot);
1651 /* Wait for SOBS to be unset */
1652 wait_for_ctrl_irq (ctrl);
1654 up(&ctrl->crit_sect);
1656 down(&ctrl->crit_sect);
1658 amber_LED_on (ctrl, hp_slot);
1659 green_LED_off (ctrl, hp_slot);
1660 slot_disable (ctrl, hp_slot);
1664 /* Wait for SOBS to be unset */
1665 wait_for_ctrl_irq (ctrl);
1667 up(&ctrl->crit_sect);
1676 * remove_board - Turns off slot and LED's
1679 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1687 struct resource_lists res_lists;
1688 struct pci_func *temp_func;
1690 if (cpqhp_unconfigure_device(func))
1693 device = func->device;
1695 hp_slot = func->device - ctrl->slot_device_offset;
1696 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1698 /* When we get here, it is safe to change base address registers.
1699 * We will attempt to save the base address register lengths */
1700 if (replace_flag || !ctrl->add_support)
1701 rc = cpqhp_save_base_addr_length(ctrl, func);
1702 else if (!func->bus_head && !func->mem_head &&
1703 !func->p_mem_head && !func->io_head) {
1704 /* Here we check to see if we've saved any of the board's
1705 * resources already. If so, we'll skip the attempt to
1706 * determine what's being used. */
1708 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1710 if (temp_func->bus_head || temp_func->mem_head
1711 || temp_func->p_mem_head || temp_func->io_head) {
1715 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1719 rc = cpqhp_save_used_resources(ctrl, func);
1721 /* Change status to shutdown */
1722 if (func->is_a_board)
1723 func->status = 0x01;
1724 func->configured = 0;
1726 down(&ctrl->crit_sect);
1728 green_LED_off (ctrl, hp_slot);
1729 slot_disable (ctrl, hp_slot);
1733 /* turn off SERR for slot */
1734 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1735 temp_byte &= ~(0x01 << hp_slot);
1736 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1738 /* Wait for SOBS to be unset */
1739 wait_for_ctrl_irq (ctrl);
1741 up(&ctrl->crit_sect);
1743 if (!replace_flag && ctrl->add_support) {
1745 res_lists.io_head = ctrl->io_head;
1746 res_lists.mem_head = ctrl->mem_head;
1747 res_lists.p_mem_head = ctrl->p_mem_head;
1748 res_lists.bus_head = ctrl->bus_head;
1750 cpqhp_return_board_resources(func, &res_lists);
1752 ctrl->io_head = res_lists.io_head;
1753 ctrl->mem_head = res_lists.mem_head;
1754 ctrl->p_mem_head = res_lists.p_mem_head;
1755 ctrl->bus_head = res_lists.bus_head;
1757 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1758 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1759 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1760 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1762 if (is_bridge(func)) {
1763 bridge_slot_remove(func);
1767 func = cpqhp_slot_find(ctrl->bus, device, 0);
1770 /* Setup slot structure with entry for empty slot */
1771 func = cpqhp_slot_create(ctrl->bus);
1776 func->bus = ctrl->bus;
1777 func->device = device;
1779 func->configured = 0;
1780 func->switch_save = 0x10;
1781 func->is_a_board = 0;
1782 func->p_task_event = NULL;
1788 static void pushbutton_helper_thread(unsigned long data)
1790 pushbutton_pending = data;
1791 up(&event_semaphore);
1795 /* this is the main worker thread */
1796 static int event_thread(void* data)
1798 struct controller *ctrl;
1800 daemonize("phpd_event");
1805 dbg("!!!!event_thread sleeping\n");
1806 down_interruptible (&event_semaphore);
1807 dbg("event_thread woken finished = %d\n", event_finished);
1808 if (event_finished) break;
1810 if (pushbutton_pending)
1811 cpqhp_pushbutton_thread(pushbutton_pending);
1813 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1814 interrupt_event_handler(ctrl);
1816 dbg("event_thread signals exit\n");
1822 int cpqhp_event_start_thread(void)
1826 /* initialize our semaphores */
1827 init_MUTEX(&delay_sem);
1828 init_MUTEX_LOCKED(&event_semaphore);
1829 init_MUTEX_LOCKED(&event_exit);
1832 pid = kernel_thread(event_thread, 0, 0);
1834 err ("Can't start up our event thread\n");
1837 dbg("Our event thread pid = %d\n", pid);
1842 void cpqhp_event_stop_thread(void)
1845 dbg("event_thread finish command given\n");
1846 up(&event_semaphore);
1847 dbg("wait for event_thread to exit\n");
1852 static int update_slot_info(struct controller *ctrl, struct slot *slot)
1854 struct hotplug_slot_info *info;
1857 info = kmalloc(sizeof(*info), GFP_KERNEL);
1861 info->power_status = get_slot_enabled(ctrl, slot);
1862 info->attention_status = cpq_get_attention_status(ctrl, slot);
1863 info->latch_status = cpq_get_latch_status(ctrl, slot);
1864 info->adapter_status = get_presence_status(ctrl, slot);
1865 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1870 static void interrupt_event_handler(struct controller *ctrl)
1874 struct pci_func *func;
1876 struct slot *p_slot;
1881 for (loop = 0; loop < 10; loop++) {
1882 /* dbg("loop %d\n", loop); */
1883 if (ctrl->event_queue[loop].event_type != 0) {
1884 hp_slot = ctrl->event_queue[loop].hp_slot;
1886 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1890 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1894 dbg("hp_slot %d, func %p, p_slot %p\n",
1895 hp_slot, func, p_slot);
1897 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1898 dbg("button pressed\n");
1899 } else if (ctrl->event_queue[loop].event_type ==
1900 INT_BUTTON_CANCEL) {
1901 dbg("button cancel\n");
1902 del_timer(&p_slot->task_event);
1904 down(&ctrl->crit_sect);
1906 if (p_slot->state == BLINKINGOFF_STATE) {
1908 dbg("turn on green LED\n");
1909 green_LED_on (ctrl, hp_slot);
1910 } else if (p_slot->state == BLINKINGON_STATE) {
1912 dbg("turn off green LED\n");
1913 green_LED_off (ctrl, hp_slot);
1916 info(msg_button_cancel, p_slot->number);
1918 p_slot->state = STATIC_STATE;
1920 amber_LED_off (ctrl, hp_slot);
1924 /* Wait for SOBS to be unset */
1925 wait_for_ctrl_irq (ctrl);
1927 up(&ctrl->crit_sect);
1929 /*** button Released (No action on press...) */
1930 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1931 dbg("button release\n");
1933 if (is_slot_enabled (ctrl, hp_slot)) {
1934 dbg("slot is on\n");
1935 p_slot->state = BLINKINGOFF_STATE;
1936 info(msg_button_off, p_slot->number);
1938 dbg("slot is off\n");
1939 p_slot->state = BLINKINGON_STATE;
1940 info(msg_button_on, p_slot->number);
1942 down(&ctrl->crit_sect);
1944 dbg("blink green LED and turn off amber\n");
1946 amber_LED_off (ctrl, hp_slot);
1947 green_LED_blink (ctrl, hp_slot);
1951 /* Wait for SOBS to be unset */
1952 wait_for_ctrl_irq (ctrl);
1954 up(&ctrl->crit_sect);
1955 init_timer(&p_slot->task_event);
1956 p_slot->hp_slot = hp_slot;
1957 p_slot->ctrl = ctrl;
1958 /* p_slot->physical_slot = physical_slot; */
1959 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1960 p_slot->task_event.function = pushbutton_helper_thread;
1961 p_slot->task_event.data = (u32) p_slot;
1963 dbg("add_timer p_slot = %p\n", p_slot);
1964 add_timer(&p_slot->task_event);
1966 /***********POWER FAULT */
1967 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1968 dbg("power fault\n");
1970 /* refresh notification */
1972 update_slot_info(ctrl, p_slot);
1975 ctrl->event_queue[loop].event_type = 0;
1979 } /* End of FOR loop */
1987 * cpqhp_pushbutton_thread
1989 * Scheduled procedure to handle blocking stuff for the pushbuttons
1990 * Handles all pending events and exits.
1993 void cpqhp_pushbutton_thread(unsigned long slot)
1997 struct pci_func *func;
1998 struct slot *p_slot = (struct slot *) slot;
1999 struct controller *ctrl = (struct controller *) p_slot->ctrl;
2001 pushbutton_pending = 0;
2002 hp_slot = p_slot->hp_slot;
2004 device = p_slot->device;
2006 if (is_slot_enabled(ctrl, hp_slot)) {
2007 p_slot->state = POWEROFF_STATE;
2008 /* power Down board */
2009 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
2010 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
2012 dbg("Error! func NULL in %s\n", __FUNCTION__);
2016 if (func != NULL && ctrl != NULL) {
2017 if (cpqhp_process_SS(ctrl, func) != 0) {
2018 amber_LED_on (ctrl, hp_slot);
2019 green_LED_on (ctrl, hp_slot);
2023 /* Wait for SOBS to be unset */
2024 wait_for_ctrl_irq (ctrl);
2028 p_slot->state = STATIC_STATE;
2030 p_slot->state = POWERON_STATE;
2033 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
2034 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
2036 dbg("Error! func NULL in %s\n", __FUNCTION__);
2040 if (func != NULL && ctrl != NULL) {
2041 if (cpqhp_process_SI(ctrl, func) != 0) {
2042 amber_LED_on(ctrl, hp_slot);
2043 green_LED_off(ctrl, hp_slot);
2047 /* Wait for SOBS to be unset */
2048 wait_for_ctrl_irq (ctrl);
2052 p_slot->state = STATIC_STATE;
2059 int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
2065 struct slot* p_slot;
2066 int physical_slot = 0;
2070 device = func->device;
2071 hp_slot = device - ctrl->slot_device_offset;
2072 p_slot = cpqhp_find_slot(ctrl, device);
2074 physical_slot = p_slot->number;
2076 /* Check to see if the interlock is closed */
2077 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2079 if (tempdword & (0x01 << hp_slot)) {
2083 if (func->is_a_board) {
2084 rc = board_replaced(func, ctrl);
2089 func = cpqhp_slot_create(ctrl->bus);
2093 func->bus = ctrl->bus;
2094 func->device = device;
2096 func->configured = 0;
2097 func->is_a_board = 1;
2099 /* We have to save the presence info for these slots */
2100 temp_word = ctrl->ctrl_int_comp >> 16;
2101 func->presence_save = (temp_word >> hp_slot) & 0x01;
2102 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2104 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2105 func->switch_save = 0;
2107 func->switch_save = 0x10;
2110 rc = board_added(func, ctrl);
2112 if (is_bridge(func)) {
2113 bridge_slot_remove(func);
2117 /* Setup slot structure with entry for empty slot */
2118 func = cpqhp_slot_create(ctrl->bus);
2123 func->bus = ctrl->bus;
2124 func->device = device;
2126 func->configured = 0;
2127 func->is_a_board = 0;
2129 /* We have to save the presence info for these slots */
2130 temp_word = ctrl->ctrl_int_comp >> 16;
2131 func->presence_save = (temp_word >> hp_slot) & 0x01;
2132 func->presence_save |=
2133 (temp_word >> (hp_slot + 7)) & 0x02;
2135 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2136 func->switch_save = 0;
2138 func->switch_save = 0x10;
2144 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2148 update_slot_info(ctrl, p_slot);
2154 int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2156 u8 device, class_code, header_type, BCR;
2161 struct slot* p_slot;
2162 struct pci_bus *pci_bus = ctrl->pci_bus;
2163 int physical_slot=0;
2165 device = func->device;
2166 func = cpqhp_slot_find(ctrl->bus, device, index++);
2167 p_slot = cpqhp_find_slot(ctrl, device);
2169 physical_slot = p_slot->number;
2172 /* Make sure there are no video controllers here */
2173 while (func && !rc) {
2174 pci_bus->number = func->bus;
2175 devfn = PCI_DEVFN(func->device, func->function);
2177 /* Check the Class Code */
2178 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2182 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2183 /* Display/Video adapter (not supported) */
2184 rc = REMOVE_NOT_SUPPORTED;
2186 /* See if it's a bridge */
2187 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2191 /* If it's a bridge, check the VGA Enable bit */
2192 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2193 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2197 /* If the VGA Enable bit is set, remove isn't
2199 if (BCR & PCI_BRIDGE_CTL_VGA) {
2200 rc = REMOVE_NOT_SUPPORTED;
2205 func = cpqhp_slot_find(ctrl->bus, device, index++);
2208 func = cpqhp_slot_find(ctrl->bus, device, 0);
2209 if ((func != NULL) && !rc) {
2210 /* FIXME: Replace flag should be passed into process_SS */
2211 replace_flag = !(ctrl->add_support);
2212 rc = remove_board(func, replace_flag, ctrl);
2218 update_slot_info(ctrl, p_slot);
2224 * switch_leds: switch the leds, go from one site to the other.
2225 * @ctrl: controller to use
2226 * @num_of_slots: number of slots to use
2227 * @direction: 1 to start from the left side, 0 to start right.
2229 static void switch_leds(struct controller *ctrl, const int num_of_slots,
2230 u32 *work_LED, const int direction)
2234 for (loop = 0; loop < num_of_slots; loop++) {
2236 *work_LED = *work_LED >> 1;
2238 *work_LED = *work_LED << 1;
2239 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2243 /* Wait for SOGO interrupt */
2244 wait_for_ctrl_irq(ctrl);
2246 /* Get ready for next iteration */
2247 long_delay((2*HZ)/10);
2252 * hardware_test - runs hardware tests
2254 * For hot plug ctrl folks to play with.
2255 * test_num is the number written to the "test" file in sysfs
2258 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2265 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2269 /* Do stuff here! */
2271 /* Do that funky LED thing */
2272 /* so we can restore them later */
2273 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2274 work_LED = 0x01010101;
2275 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2276 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2277 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2278 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2280 work_LED = 0x01010000;
2281 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2282 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2283 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2284 work_LED = 0x00000101;
2285 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2286 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2287 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2289 work_LED = 0x01010000;
2290 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2291 for (loop = 0; loop < num_of_slots; loop++) {
2294 /* Wait for SOGO interrupt */
2295 wait_for_ctrl_irq (ctrl);
2297 /* Get ready for next iteration */
2298 long_delay((3*HZ)/10);
2299 work_LED = work_LED >> 16;
2300 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2304 /* Wait for SOGO interrupt */
2305 wait_for_ctrl_irq (ctrl);
2307 /* Get ready for next iteration */
2308 long_delay((3*HZ)/10);
2309 work_LED = work_LED << 16;
2310 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2311 work_LED = work_LED << 1;
2312 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2315 /* put it back the way it was */
2316 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2320 /* Wait for SOBS to be unset */
2321 wait_for_ctrl_irq (ctrl);
2324 /* Do other stuff here! */
2335 * configure_new_device - Configures the PCI header information of one board.
2337 * @ctrl: pointer to controller structure
2338 * @func: pointer to function structure
2339 * @behind_bridge: 1 if this is a recursive call, 0 if not
2340 * @resources: pointer to set of resource lists
2342 * Returns 0 if success
2345 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2346 u8 behind_bridge, struct resource_lists * resources)
2348 u8 temp_byte, function, max_functions, stop_it;
2351 struct pci_func *new_slot;
2356 dbg("%s\n", __FUNCTION__);
2357 /* Check for Multi-function device */
2358 ctrl->pci_bus->number = func->bus;
2359 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2361 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2365 if (temp_byte & 0x80) /* Multi-function device */
2373 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2376 dbg("configure_new_function failed %d\n",rc);
2380 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2383 cpqhp_return_board_resources(new_slot, resources);
2393 /* The following loop skips to the next present function
2394 * and creates a board structure */
2396 while ((function < max_functions) && (!stop_it)) {
2397 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2399 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2401 } else { /* There's something there */
2402 /* Setup slot structure. */
2403 new_slot = cpqhp_slot_create(func->bus);
2405 if (new_slot == NULL)
2408 new_slot->bus = func->bus;
2409 new_slot->device = func->device;
2410 new_slot->function = function;
2411 new_slot->is_a_board = 1;
2412 new_slot->status = 0;
2418 } while (function < max_functions);
2419 dbg("returning from configure_new_device\n");
2426 Configuration logic that involves the hotplug data structures and
2432 * configure_new_function - Configures the PCI header information of one device
2434 * @ctrl: pointer to controller structure
2435 * @func: pointer to function structure
2436 * @behind_bridge: 1 if this is a recursive call, 0 if not
2437 * @resources: pointer to set of resource lists
2439 * Calls itself recursively for bridged devices.
2440 * Returns 0 if success
2443 static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2445 struct resource_lists *resources)
2460 struct pci_resource *mem_node;
2461 struct pci_resource *p_mem_node;
2462 struct pci_resource *io_node;
2463 struct pci_resource *bus_node;
2464 struct pci_resource *hold_mem_node;
2465 struct pci_resource *hold_p_mem_node;
2466 struct pci_resource *hold_IO_node;
2467 struct pci_resource *hold_bus_node;
2468 struct irq_mapping irqs;
2469 struct pci_func *new_slot;
2470 struct pci_bus *pci_bus;
2471 struct resource_lists temp_resources;
2473 pci_bus = ctrl->pci_bus;
2474 pci_bus->number = func->bus;
2475 devfn = PCI_DEVFN(func->device, func->function);
2477 /* Check for Bridge */
2478 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2482 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2483 /* set Primary bus */
2484 dbg("set Primary bus = %d\n", func->bus);
2485 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2489 /* find range of busses to use */
2490 dbg("find ranges of buses to use\n");
2491 bus_node = get_max_resource(&(resources->bus_head), 1);
2493 /* If we don't have any busses to allocate, we can't continue */
2497 /* set Secondary bus */
2498 temp_byte = bus_node->base;
2499 dbg("set Secondary bus = %d\n", bus_node->base);
2500 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2504 /* set subordinate bus */
2505 temp_byte = bus_node->base + bus_node->length - 1;
2506 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2507 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2511 /* set subordinate Latency Timer and base Latency Timer */
2513 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2516 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2520 /* set Cache Line size */
2522 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2526 /* Setup the IO, memory, and prefetchable windows */
2527 io_node = get_max_resource(&(resources->io_head), 0x1000);
2530 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2533 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2536 dbg("Setup the IO, memory, and prefetchable windows\n");
2538 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2539 io_node->length, io_node->next);
2541 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2542 mem_node->length, mem_node->next);
2543 dbg("p_mem_node\n");
2544 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2545 p_mem_node->length, p_mem_node->next);
2547 /* set up the IRQ info */
2548 if (!resources->irqs) {
2549 irqs.barber_pole = 0;
2550 irqs.interrupt[0] = 0;
2551 irqs.interrupt[1] = 0;
2552 irqs.interrupt[2] = 0;
2553 irqs.interrupt[3] = 0;
2556 irqs.barber_pole = resources->irqs->barber_pole;
2557 irqs.interrupt[0] = resources->irqs->interrupt[0];
2558 irqs.interrupt[1] = resources->irqs->interrupt[1];
2559 irqs.interrupt[2] = resources->irqs->interrupt[2];
2560 irqs.interrupt[3] = resources->irqs->interrupt[3];
2561 irqs.valid_INT = resources->irqs->valid_INT;
2564 /* set up resource lists that are now aligned on top and bottom
2565 * for anything behind the bridge. */
2566 temp_resources.bus_head = bus_node;
2567 temp_resources.io_head = io_node;
2568 temp_resources.mem_head = mem_node;
2569 temp_resources.p_mem_head = p_mem_node;
2570 temp_resources.irqs = &irqs;
2572 /* Make copies of the nodes we are going to pass down so that
2573 * if there is a problem,we can just use these to free resources */
2574 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2575 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2576 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2577 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2579 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2580 kfree(hold_bus_node);
2581 kfree(hold_IO_node);
2582 kfree(hold_mem_node);
2583 kfree(hold_p_mem_node);
2588 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2590 bus_node->base += 1;
2591 bus_node->length -= 1;
2592 bus_node->next = NULL;
2594 /* If we have IO resources copy them and fill in the bridge's
2595 * IO range registers */
2597 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2598 io_node->next = NULL;
2600 /* set IO base and Limit registers */
2601 temp_byte = io_node->base >> 8;
2602 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2604 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2605 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2607 kfree(hold_IO_node);
2608 hold_IO_node = NULL;
2611 /* If we have memory resources copy them and fill in the
2612 * bridge's memory range registers. Otherwise, fill in the
2613 * range registers with values that disable them. */
2615 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2616 mem_node->next = NULL;
2618 /* set Mem base and Limit registers */
2619 temp_word = mem_node->base >> 16;
2620 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2622 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2623 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2626 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2629 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2631 kfree(hold_mem_node);
2632 hold_mem_node = NULL;
2635 /* If we have prefetchable memory resources copy them and
2636 * fill in the bridge's memory range registers. Otherwise,
2637 * fill in the range registers with values that disable them. */
2639 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2640 p_mem_node->next = NULL;
2642 /* set Pre Mem base and Limit registers */
2643 temp_word = p_mem_node->base >> 16;
2644 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2646 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2647 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2650 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2653 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2655 kfree(hold_p_mem_node);
2656 hold_p_mem_node = NULL;
2659 /* Adjust this to compensate for extra adjustment in first loop */
2664 /* Here we actually find the devices and configure them */
2665 for (device = 0; (device <= 0x1F) && !rc; device++) {
2666 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2669 pci_bus->number = hold_bus_node->base;
2670 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2671 pci_bus->number = func->bus;
2673 if (ID != 0xFFFFFFFF) { /* device present */
2674 /* Setup slot structure. */
2675 new_slot = cpqhp_slot_create(hold_bus_node->base);
2677 if (new_slot == NULL) {
2682 new_slot->bus = hold_bus_node->base;
2683 new_slot->device = device;
2684 new_slot->function = 0;
2685 new_slot->is_a_board = 1;
2686 new_slot->status = 0;
2688 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2689 dbg("configure_new_device rc=0x%x\n",rc);
2690 } /* End of IF (device in slot?) */
2691 } /* End of FOR loop */
2695 /* save the interrupt routing information */
2696 if (resources->irqs) {
2697 resources->irqs->interrupt[0] = irqs.interrupt[0];
2698 resources->irqs->interrupt[1] = irqs.interrupt[1];
2699 resources->irqs->interrupt[2] = irqs.interrupt[2];
2700 resources->irqs->interrupt[3] = irqs.interrupt[3];
2701 resources->irqs->valid_INT = irqs.valid_INT;
2702 } else if (!behind_bridge) {
2703 /* We need to hook up the interrupts here */
2704 for (cloop = 0; cloop < 4; cloop++) {
2705 if (irqs.valid_INT & (0x01 << cloop)) {
2706 rc = cpqhp_set_irq(func->bus, func->device,
2707 0x0A + cloop, irqs.interrupt[cloop]);
2711 } /* end of for loop */
2713 /* Return unused bus resources
2714 * First use the temporary node to store information for
2716 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2717 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2719 hold_bus_node->next = func->bus_head;
2720 func->bus_head = hold_bus_node;
2722 temp_byte = temp_resources.bus_head->base - 1;
2724 /* set subordinate bus */
2725 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2727 if (temp_resources.bus_head->length == 0) {
2728 kfree(temp_resources.bus_head);
2729 temp_resources.bus_head = NULL;
2731 return_resource(&(resources->bus_head), temp_resources.bus_head);
2735 /* If we have IO space available and there is some left,
2736 * return the unused portion */
2737 if (hold_IO_node && temp_resources.io_head) {
2738 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2739 &hold_IO_node, 0x1000);
2741 /* Check if we were able to split something off */
2743 hold_IO_node->base = io_node->base + io_node->length;
2745 temp_byte = (hold_IO_node->base) >> 8;
2746 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2748 return_resource(&(resources->io_head), io_node);
2751 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2753 /* Check if we were able to split something off */
2755 /* First use the temporary node to store
2756 * information for the board */
2757 hold_IO_node->length = io_node->base - hold_IO_node->base;
2759 /* If we used any, add it to the board's list */
2760 if (hold_IO_node->length) {
2761 hold_IO_node->next = func->io_head;
2762 func->io_head = hold_IO_node;
2764 temp_byte = (io_node->base - 1) >> 8;
2765 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2767 return_resource(&(resources->io_head), io_node);
2769 /* it doesn't need any IO */
2771 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2773 return_resource(&(resources->io_head), io_node);
2774 kfree(hold_IO_node);
2777 /* it used most of the range */
2778 hold_IO_node->next = func->io_head;
2779 func->io_head = hold_IO_node;
2781 } else if (hold_IO_node) {
2782 /* it used the whole range */
2783 hold_IO_node->next = func->io_head;
2784 func->io_head = hold_IO_node;
2786 /* If we have memory space available and there is some left,
2787 * return the unused portion */
2788 if (hold_mem_node && temp_resources.mem_head) {
2789 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2790 &hold_mem_node, 0x100000);
2792 /* Check if we were able to split something off */
2794 hold_mem_node->base = mem_node->base + mem_node->length;
2796 temp_word = (hold_mem_node->base) >> 16;
2797 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2799 return_resource(&(resources->mem_head), mem_node);
2802 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2804 /* Check if we were able to split something off */
2806 /* First use the temporary node to store
2807 * information for the board */
2808 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2810 if (hold_mem_node->length) {
2811 hold_mem_node->next = func->mem_head;
2812 func->mem_head = hold_mem_node;
2814 /* configure end address */
2815 temp_word = (mem_node->base - 1) >> 16;
2816 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2818 /* Return unused resources to the pool */
2819 return_resource(&(resources->mem_head), mem_node);
2821 /* it doesn't need any Mem */
2823 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2825 return_resource(&(resources->mem_head), mem_node);
2826 kfree(hold_mem_node);
2829 /* it used most of the range */
2830 hold_mem_node->next = func->mem_head;
2831 func->mem_head = hold_mem_node;
2833 } else if (hold_mem_node) {
2834 /* it used the whole range */
2835 hold_mem_node->next = func->mem_head;
2836 func->mem_head = hold_mem_node;
2838 /* If we have prefetchable memory space available and there
2839 * is some left at the end, return the unused portion */
2840 if (hold_p_mem_node && temp_resources.p_mem_head) {
2841 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2842 &hold_p_mem_node, 0x100000);
2844 /* Check if we were able to split something off */
2846 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2848 temp_word = (hold_p_mem_node->base) >> 16;
2849 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2851 return_resource(&(resources->p_mem_head), p_mem_node);
2854 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2856 /* Check if we were able to split something off */
2858 /* First use the temporary node to store
2859 * information for the board */
2860 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2862 /* If we used any, add it to the board's list */
2863 if (hold_p_mem_node->length) {
2864 hold_p_mem_node->next = func->p_mem_head;
2865 func->p_mem_head = hold_p_mem_node;
2867 temp_word = (p_mem_node->base - 1) >> 16;
2868 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2870 return_resource(&(resources->p_mem_head), p_mem_node);
2872 /* it doesn't need any PMem */
2874 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2876 return_resource(&(resources->p_mem_head), p_mem_node);
2877 kfree(hold_p_mem_node);
2880 /* it used the most of the range */
2881 hold_p_mem_node->next = func->p_mem_head;
2882 func->p_mem_head = hold_p_mem_node;
2884 } else if (hold_p_mem_node) {
2885 /* it used the whole range */
2886 hold_p_mem_node->next = func->p_mem_head;
2887 func->p_mem_head = hold_p_mem_node;
2889 /* We should be configuring an IRQ and the bridge's base address
2890 * registers if it needs them. Although we have never seen such
2894 command = 0x0157; /* = PCI_COMMAND_IO |
2895 * PCI_COMMAND_MEMORY |
2896 * PCI_COMMAND_MASTER |
2897 * PCI_COMMAND_INVALIDATE |
2898 * PCI_COMMAND_PARITY |
2899 * PCI_COMMAND_SERR */
2900 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2902 /* set Bridge Control Register */
2903 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2904 * PCI_BRIDGE_CTL_SERR |
2905 * PCI_BRIDGE_CTL_NO_ISA */
2906 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2907 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2908 /* Standard device */
2909 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2911 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2912 /* Display (video) adapter (not supported) */
2913 return DEVICE_TYPE_NOT_SUPPORTED;
2915 /* Figure out IO and memory needs */
2916 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2917 temp_register = 0xFFFFFFFF;
2919 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2920 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2922 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2923 dbg("CND: base = 0x%x\n", temp_register);
2925 if (temp_register) { /* If this register is implemented */
2926 if ((temp_register & 0x03L) == 0x01) {
2929 /* set base = amount of IO space */
2930 base = temp_register & 0xFFFFFFFC;
2933 dbg("CND: length = 0x%x\n", base);
2934 io_node = get_io_resource(&(resources->io_head), base);
2935 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2936 io_node->base, io_node->length, io_node->next);
2937 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2939 /* allocate the resource to the board */
2941 base = io_node->base;
2943 io_node->next = func->io_head;
2944 func->io_head = io_node;
2947 } else if ((temp_register & 0x0BL) == 0x08) {
2948 /* Map prefetchable memory */
2949 base = temp_register & 0xFFFFFFF0;
2952 dbg("CND: length = 0x%x\n", base);
2953 p_mem_node = get_resource(&(resources->p_mem_head), base);
2955 /* allocate the resource to the board */
2957 base = p_mem_node->base;
2959 p_mem_node->next = func->p_mem_head;
2960 func->p_mem_head = p_mem_node;
2963 } else if ((temp_register & 0x0BL) == 0x00) {
2965 base = temp_register & 0xFFFFFFF0;
2968 dbg("CND: length = 0x%x\n", base);
2969 mem_node = get_resource(&(resources->mem_head), base);
2971 /* allocate the resource to the board */
2973 base = mem_node->base;
2975 mem_node->next = func->mem_head;
2976 func->mem_head = mem_node;
2979 } else if ((temp_register & 0x0BL) == 0x04) {
2981 base = temp_register & 0xFFFFFFF0;
2984 dbg("CND: length = 0x%x\n", base);
2985 mem_node = get_resource(&(resources->mem_head), base);
2987 /* allocate the resource to the board */
2989 base = mem_node->base;
2991 mem_node->next = func->mem_head;
2992 func->mem_head = mem_node;
2995 } else if ((temp_register & 0x0BL) == 0x06) {
2996 /* Those bits are reserved, we can't handle this */
2999 /* Requesting space below 1M */
3000 return NOT_ENOUGH_RESOURCES;
3003 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
3005 /* Check for 64-bit base */
3006 if ((temp_register & 0x07L) == 0x04) {
3009 /* Upper 32 bits of address always zero
3010 * on today's systems */
3011 /* FIXME this is probably not true on
3012 * Alpha and ia64??? */
3014 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
3017 } /* End of base register loop */
3018 if (cpqhp_legacy_mode) {
3019 /* Figure out which interrupt pin this function uses */
3020 rc = pci_bus_read_config_byte (pci_bus, devfn,
3021 PCI_INTERRUPT_PIN, &temp_byte);
3023 /* If this function needs an interrupt and we are behind
3024 * a bridge and the pin is tied to something that's
3025 * alread mapped, set this one the same */
3026 if (temp_byte && resources->irqs &&
3027 (resources->irqs->valid_INT &
3028 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3029 /* We have to share with something already set up */
3030 IRQ = resources->irqs->interrupt[(temp_byte +
3031 resources->irqs->barber_pole - 1) & 0x03];
3033 /* Program IRQ based on card type */
3034 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3036 if (class_code == PCI_BASE_CLASS_STORAGE) {
3037 IRQ = cpqhp_disk_irq;
3039 IRQ = cpqhp_nic_irq;
3044 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3047 if (!behind_bridge) {
3048 rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3052 /* TBD - this code may also belong in the other clause
3053 * of this If statement */
3054 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3055 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3060 rc = pci_bus_write_config_byte(pci_bus, devfn,
3061 PCI_LATENCY_TIMER, temp_byte);
3063 /* Cache Line size */
3065 rc = pci_bus_write_config_byte(pci_bus, devfn,
3066 PCI_CACHE_LINE_SIZE, temp_byte);
3068 /* disable ROM base Address */
3070 rc = pci_bus_write_config_word(pci_bus, devfn,
3071 PCI_ROM_ADDRESS, temp_dword);
3074 temp_word = 0x0157; /* = PCI_COMMAND_IO |
3075 * PCI_COMMAND_MEMORY |
3076 * PCI_COMMAND_MASTER |
3077 * PCI_COMMAND_INVALIDATE |
3078 * PCI_COMMAND_PARITY |
3079 * PCI_COMMAND_SERR */
3080 rc = pci_bus_write_config_word (pci_bus, devfn,
3081 PCI_COMMAND, temp_word);
3082 } else { /* End of Not-A-Bridge else */
3083 /* It's some strange type of PCI adapter (Cardbus?) */
3084 return DEVICE_TYPE_NOT_SUPPORTED;
3087 func->configured = 1;
3091 cpqhp_destroy_resource_list (&temp_resources);
3093 return_resource(&(resources-> bus_head), hold_bus_node);
3094 return_resource(&(resources-> io_head), hold_IO_node);
3095 return_resource(&(resources-> mem_head), hold_mem_node);
3096 return_resource(&(resources-> p_mem_head), hold_p_mem_node);