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,u8 behind_bridge, struct resource_lists *resources);
43 static int configure_new_function(struct controller* ctrl, struct pci_func *func,u8 behind_bridge, struct resource_lists *resources);
44 static void interrupt_event_handler(struct controller *ctrl);
46 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
47 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
48 static int event_finished;
49 static unsigned long pushbutton_pending; /* = 0 */
51 /* things needed for the long_delay function */
52 static struct semaphore delay_sem;
53 static wait_queue_head_t delay_wait;
55 /* delay is in jiffies to wait for */
56 static void long_delay (int delay)
58 DECLARE_WAITQUEUE(wait, current);
60 /* only allow 1 customer into the delay queue at once
61 * yes this makes some people wait even longer, but who really cares?
62 * this is for _huge_ delays to make the hardware happy as the
63 * signals bounce around
67 init_waitqueue_head (&delay_wait);
69 add_wait_queue(&delay_wait, &wait);
70 set_current_state(TASK_INTERRUPTIBLE);
71 schedule_timeout(delay);
72 remove_wait_queue(&delay_wait, &wait);
73 set_current_state(TASK_RUNNING);
79 //FIXME: The following line needs to be somewhere else...
80 #define WRONG_BUS_FREQUENCY 0x07
81 static u8 handle_switch_change(u8 change, struct controller * ctrl)
86 struct pci_func *func;
87 struct event_info *taskInfo;
93 dbg("cpqsbd: Switch interrupt received.\n");
95 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
96 if (change & (0x1L << hp_slot)) {
97 //*********************************
99 //*********************************
100 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
102 //this is the structure that tells the worker thread
104 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
105 ctrl->next_event = (ctrl->next_event + 1) % 10;
106 taskInfo->hp_slot = hp_slot;
110 temp_word = ctrl->ctrl_int_comp >> 16;
111 func->presence_save = (temp_word >> hp_slot) & 0x01;
112 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
114 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
115 //*********************************
117 //*********************************
119 func->switch_save = 0;
121 taskInfo->event_type = INT_SWITCH_OPEN;
123 //*********************************
125 //*********************************
127 func->switch_save = 0x10;
129 taskInfo->event_type = INT_SWITCH_CLOSE;
141 struct slot *cpqhp_find_slot (struct controller * ctrl, u8 device)
150 while (slot && (slot->device != device)) {
158 static u8 handle_presence_change(u16 change, struct controller * ctrl)
164 struct pci_func *func;
165 struct event_info *taskInfo;
171 //*********************************
173 //*********************************
174 dbg("cpqsbd: Presence/Notify input change.\n");
175 dbg(" Changed bits are 0x%4.4x\n", change );
177 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
178 if (change & (0x0101 << hp_slot)) {
179 //*********************************
181 //*********************************
182 func = cpqhp_slot_find(ctrl->bus, (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)))) {
235 //*********************************
237 //*********************************
238 taskInfo->event_type = INT_PRESENCE_ON;
240 //*********************************
242 //*********************************
243 taskInfo->event_type = INT_PRESENCE_OFF;
253 static u8 handle_power_fault(u8 change, struct controller * ctrl)
257 struct pci_func *func;
258 struct event_info *taskInfo;
263 //*********************************
265 //*********************************
267 info("power fault interrupt\n");
269 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
270 if (change & (0x01 << hp_slot)) {
271 //*********************************
273 //*********************************
274 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
276 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
277 ctrl->next_event = (ctrl->next_event + 1) % 10;
278 taskInfo->hp_slot = hp_slot;
282 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
283 //*********************************
284 // power fault Cleared
285 //*********************************
288 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
290 //*********************************
292 //*********************************
293 taskInfo->event_type = INT_POWER_FAULT;
296 amber_LED_on (ctrl, hp_slot);
297 green_LED_off (ctrl, hp_slot);
300 // this is a fatal condition, we want to crash the
301 // machine to protect from data corruption
302 // simulated_NMI shouldn't ever return
304 //simulated_NMI(hp_slot, ctrl);
306 //The following code causes a software crash just in
307 //case simulated_NMI did return
309 //panic(msg_power_fault);
311 // set power fault status for this board
313 info("power fault bit %x set\n", hp_slot);
326 * Sorts nodes on the list by their length.
330 static int sort_by_size(struct pci_resource **head)
332 struct pci_resource *current_res;
333 struct pci_resource *next_res;
334 int out_of_order = 1;
339 if (!((*head)->next))
342 while (out_of_order) {
345 // Special case for swapping list head
346 if (((*head)->next) &&
347 ((*head)->length > (*head)->next->length)) {
350 *head = (*head)->next;
351 current_res->next = (*head)->next;
352 (*head)->next = current_res;
357 while (current_res->next && current_res->next->next) {
358 if (current_res->next->length > current_res->next->next->length) {
360 next_res = current_res->next;
361 current_res->next = current_res->next->next;
362 current_res = current_res->next;
363 next_res->next = current_res->next;
364 current_res->next = next_res;
366 current_res = current_res->next;
368 } // End of out_of_order loop
377 * Sorts nodes on the list by their length.
381 static int sort_by_max_size(struct pci_resource **head)
383 struct pci_resource *current_res;
384 struct pci_resource *next_res;
385 int out_of_order = 1;
390 if (!((*head)->next))
393 while (out_of_order) {
396 // Special case for swapping list head
397 if (((*head)->next) &&
398 ((*head)->length < (*head)->next->length)) {
401 *head = (*head)->next;
402 current_res->next = (*head)->next;
403 (*head)->next = current_res;
408 while (current_res->next && current_res->next->next) {
409 if (current_res->next->length < current_res->next->next->length) {
411 next_res = current_res->next;
412 current_res->next = current_res->next->next;
413 current_res = current_res->next;
414 next_res->next = current_res->next;
415 current_res->next = next_res;
417 current_res = current_res->next;
419 } // End of out_of_order loop
426 * do_pre_bridge_resource_split
428 * Returns zero or one node of resources that aren't in use
431 static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
433 struct pci_resource *prevnode = NULL;
434 struct pci_resource *node;
435 struct pci_resource *split_node;
438 dbg("do_pre_bridge_resource_split\n");
440 if (!(*head) || !(*orig_head))
443 rc = cpqhp_resource_sort_and_combine(head);
448 if ((*head)->base != (*orig_head)->base)
451 if ((*head)->length == (*orig_head)->length)
455 // If we got here, there the bridge requires some of the resource, but
456 // we may be able to split some off of the front
460 if (node->length & (alignment -1)) {
461 // this one isn't an aligned length, so we'll make a new entry
463 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
468 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
470 split_node->base = node->base;
471 split_node->length = temp_dword;
473 node->length -= temp_dword;
474 node->base += split_node->length;
476 // Put it in the list
478 split_node->next = node;
481 if (node->length < alignment) {
491 while (prevnode->next != node)
492 prevnode = prevnode->next;
494 prevnode->next = node->next;
503 * do_bridge_resource_split
505 * Returns zero or one node of resources that aren't in use
508 static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
510 struct pci_resource *prevnode = NULL;
511 struct pci_resource *node;
518 rc = cpqhp_resource_sort_and_combine(head);
531 if (node->length < alignment) {
536 if (node->base & (alignment - 1)) {
537 // Short circuit if adjusted size is too small
538 temp_dword = (node->base | (alignment-1)) + 1;
539 if ((node->length - (temp_dword - node->base)) < alignment) {
544 node->length -= (temp_dword - node->base);
545 node->base = temp_dword;
548 if (node->length & (alignment - 1)) {
549 // There's stuff in use after this node
561 * this function sorts the resource list by size and then
562 * returns the first node of "size" length that is not in the
563 * ISA aliasing window. If it finds a node larger than "size"
564 * it will split it up.
566 * size must be a power of two.
568 static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
570 struct pci_resource *prevnode;
571 struct pci_resource *node;
572 struct pci_resource *split_node;
578 if ( cpqhp_resource_sort_and_combine(head) )
581 if ( sort_by_size(head) )
584 for (node = *head; node; node = node->next) {
585 if (node->length < size)
588 if (node->base & (size - 1)) {
589 // this one isn't base aligned properly
590 // so we'll make a new entry and split it up
591 temp_dword = (node->base | (size-1)) + 1;
593 // Short circuit if adjusted size is too small
594 if ((node->length - (temp_dword - node->base)) < size)
597 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
602 split_node->base = node->base;
603 split_node->length = temp_dword - node->base;
604 node->base = temp_dword;
605 node->length -= split_node->length;
607 // Put it in the list
608 split_node->next = node->next;
609 node->next = split_node;
610 } // End of non-aligned base
612 // Don't need to check if too small since we already did
613 if (node->length > size) {
614 // this one is longer than we need
615 // so we'll make a new entry and split it up
616 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
621 split_node->base = node->base + size;
622 split_node->length = node->length - size;
625 // Put it in the list
626 split_node->next = node->next;
627 node->next = split_node;
628 } // End of too big on top end
630 // For IO make sure it's not in the ISA aliasing space
631 if (node->base & 0x300L)
634 // If we got here, then it is the right size
635 // Now take it out of the list
640 while (prevnode->next != node)
641 prevnode = prevnode->next;
643 prevnode->next = node->next;
657 * Gets the largest node that is at least "size" big from the
658 * list pointed to by head. It aligns the node on top and bottom
659 * to "size" alignment before returning it.
661 static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
663 struct pci_resource *max;
664 struct pci_resource *temp;
665 struct pci_resource *split_node;
671 if (cpqhp_resource_sort_and_combine(head))
674 if (sort_by_max_size(head))
677 for (max = *head;max; max = max->next) {
679 // If not big enough we could probably just bail,
680 // instead we'll continue to the next.
681 if (max->length < size)
684 if (max->base & (size - 1)) {
685 // this one isn't base aligned properly
686 // so we'll make a new entry and split it up
687 temp_dword = (max->base | (size-1)) + 1;
689 // Short circuit if adjusted size is too small
690 if ((max->length - (temp_dword - max->base)) < size)
693 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
698 split_node->base = max->base;
699 split_node->length = temp_dword - max->base;
700 max->base = temp_dword;
701 max->length -= split_node->length;
703 // Put it next in the list
704 split_node->next = max->next;
705 max->next = split_node;
708 if ((max->base + max->length) & (size - 1)) {
709 // this one isn't end aligned properly at the top
710 // so we'll make a new entry and split it up
711 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
715 temp_dword = ((max->base + max->length) & ~(size - 1));
716 split_node->base = temp_dword;
717 split_node->length = max->length + max->base
719 max->length -= split_node->length;
721 // Put it in the list
722 split_node->next = max->next;
723 max->next = split_node;
726 // Make sure it didn't shrink too much when we aligned it
727 if (max->length < size)
730 // Now take it out of the list
731 temp = (struct pci_resource*) *head;
735 while (temp && temp->next != max) {
739 temp->next = max->next;
746 // If we get here, we couldn't find one
754 * this function sorts the resource list by size and then
755 * returns the first node of "size" length. If it finds a node
756 * larger than "size" it will split it up.
758 * size must be a power of two.
760 static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
762 struct pci_resource *prevnode;
763 struct pci_resource *node;
764 struct pci_resource *split_node;
770 if ( cpqhp_resource_sort_and_combine(head) )
773 if ( sort_by_size(head) )
776 for (node = *head; node; node = node->next) {
777 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
778 __FUNCTION__, size, node, node->base, node->length);
779 if (node->length < size)
782 if (node->base & (size - 1)) {
783 dbg("%s: not aligned\n", __FUNCTION__);
784 // this one isn't base aligned properly
785 // so we'll make a new entry and split it up
786 temp_dword = (node->base | (size-1)) + 1;
788 // Short circuit if adjusted size is too small
789 if ((node->length - (temp_dword - node->base)) < size)
792 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
797 split_node->base = node->base;
798 split_node->length = temp_dword - node->base;
799 node->base = temp_dword;
800 node->length -= split_node->length;
802 // Put it in the list
803 split_node->next = node->next;
804 node->next = split_node;
805 } // End of non-aligned base
807 // Don't need to check if too small since we already did
808 if (node->length > size) {
809 dbg("%s: too big\n", __FUNCTION__);
810 // this one is longer than we need
811 // so we'll make a new entry and split it up
812 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
817 split_node->base = node->base + size;
818 split_node->length = node->length - size;
821 // Put it in the list
822 split_node->next = node->next;
823 node->next = split_node;
824 } // End of too big on top end
826 dbg("%s: got one!!!\n", __FUNCTION__);
827 // If we got here, then it is the right size
828 // Now take it out of the list
833 while (prevnode->next != node)
834 prevnode = prevnode->next;
836 prevnode->next = node->next;
847 * cpqhp_resource_sort_and_combine
849 * Sorts all of the nodes in the list in ascending order by
850 * their base addresses. Also does garbage collection by
851 * combining adjacent nodes.
853 * returns 0 if success
855 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
857 struct pci_resource *node1;
858 struct pci_resource *node2;
859 int out_of_order = 1;
861 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
866 dbg("*head->next = %p\n",(*head)->next);
869 return(0); /* only one item on the list, already sorted! */
871 dbg("*head->base = 0x%x\n",(*head)->base);
872 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
873 while (out_of_order) {
876 // Special case for swapping list head
877 if (((*head)->next) &&
878 ((*head)->base > (*head)->next->base)) {
880 (*head) = (*head)->next;
881 node1->next = (*head)->next;
882 (*head)->next = node1;
888 while (node1->next && node1->next->next) {
889 if (node1->next->base > node1->next->next->base) {
892 node1->next = node1->next->next;
894 node2->next = node1->next;
899 } // End of out_of_order loop
903 while (node1 && node1->next) {
904 if ((node1->base + node1->length) == node1->next->base) {
907 node1->length += node1->next->length;
909 node1->next = node1->next->next;
919 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data, struct pt_regs *regs)
921 struct controller *ctrl = data;
922 u8 schedule_flag = 0;
929 misc = readw(ctrl->hpc_reg + MISC);
930 //*********************************
931 // Check to see if it was our interrupt
932 //*********************************
933 if (!(misc & 0x000C)) {
938 //*********************************
939 // Serial Output interrupt Pending
940 //*********************************
942 // Clear the interrupt
944 writew(misc, ctrl->hpc_reg + MISC);
946 // Read to clear posted writes
947 misc = readw(ctrl->hpc_reg + MISC);
949 dbg ("%s - waking up\n", __FUNCTION__);
950 wake_up_interruptible(&ctrl->queue);
954 // General-interrupt-input interrupt Pending
955 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
957 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
959 // Clear the interrupt
960 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
962 // Read it back to clear any posted writes
963 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
966 // Clear all interrupts
967 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
970 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
971 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
972 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
975 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
977 /* Bus reset has completed */
979 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
980 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
981 wake_up_interruptible(&ctrl->queue);
985 up(&event_semaphore);
986 dbg("Signal event_semaphore\n");
993 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
994 * @busnumber - bus where new node is to be located
996 * Returns pointer to the new node or NULL if unsuccessful
998 struct pci_func *cpqhp_slot_create(u8 busnumber)
1000 struct pci_func *new_slot;
1001 struct pci_func *next;
1003 new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL);
1005 if (new_slot == NULL) {
1006 // I'm not dead yet!
1011 memset(new_slot, 0, sizeof(struct pci_func));
1013 new_slot->next = NULL;
1014 new_slot->configured = 1;
1016 if (cpqhp_slot_list[busnumber] == NULL) {
1017 cpqhp_slot_list[busnumber] = new_slot;
1019 next = cpqhp_slot_list[busnumber];
1020 while (next->next != NULL)
1022 next->next = new_slot;
1029 * slot_remove - Removes a node from the linked list of slots.
1030 * @old_slot: slot to remove
1032 * Returns 0 if successful, !0 otherwise.
1034 static int slot_remove(struct pci_func * old_slot)
1036 struct pci_func *next;
1038 if (old_slot == NULL)
1041 next = cpqhp_slot_list[old_slot->bus];
1047 if (next == old_slot) {
1048 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1049 cpqhp_destroy_board_resources(old_slot);
1054 while ((next->next != old_slot) && (next->next != NULL)) {
1058 if (next->next == old_slot) {
1059 next->next = old_slot->next;
1060 cpqhp_destroy_board_resources(old_slot);
1069 * bridge_slot_remove - Removes a node from the linked list of slots.
1070 * @bridge: bridge to remove
1072 * Returns 0 if successful, !0 otherwise.
1074 static int bridge_slot_remove(struct pci_func *bridge)
1076 u8 subordinateBus, secondaryBus;
1078 struct pci_func *next;
1083 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1084 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1086 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1087 next = cpqhp_slot_list[tempBus];
1089 while (!slot_remove(next)) {
1090 next = cpqhp_slot_list[tempBus];
1094 next = cpqhp_slot_list[bridge->bus];
1100 if (next == bridge) {
1101 cpqhp_slot_list[bridge->bus] = bridge->next;
1106 while ((next->next != bridge) && (next->next != NULL)) {
1110 if (next->next == bridge) {
1111 next->next = bridge->next;
1120 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1122 * @device: device to find
1123 * @index: is 0 for first function found, 1 for the second...
1125 * Returns pointer to the node if successful, %NULL otherwise.
1127 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1130 struct pci_func *func;
1132 func = cpqhp_slot_list[bus];
1134 if ((func == NULL) || ((func->device == device) && (index == 0)))
1137 if (func->device == device)
1140 while (func->next != NULL) {
1143 if (func->device == device)
1154 // DJZ: I don't think is_bridge will work as is.
1156 static int is_bridge(struct pci_func * func)
1158 // Check the header type
1159 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1166 /* the following routines constitute the bulk of the
1167 hotplug controller logic
1172 * board_replaced - Called after a board has been replaced in the system.
1174 * This is only used if we don't have resources for hot add
1175 * Turns power on for the board
1176 * Checks to see if board is the same
1177 * If board is same, reconfigures it
1178 * If board isn't same, turns it back off.
1181 static u32 board_replaced(struct pci_func * func, struct controller * ctrl)
1190 hp_slot = func->device - ctrl->slot_device_offset;
1192 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
1193 //*********************************
1194 // The switch is open.
1195 //*********************************
1196 rc = INTERLOCK_OPEN;
1197 } else if (is_slot_enabled (ctrl, hp_slot)) {
1198 //*********************************
1199 // The board is already on
1200 //*********************************
1201 rc = CARD_FUNCTIONING;
1203 // Wait for exclusive access to hardware
1204 down(&ctrl->crit_sect);
1206 // turn on board without attaching to the bus
1207 enable_slot_power (ctrl, hp_slot);
1211 // Wait for SOBS to be unset
1212 wait_for_ctrl_irq (ctrl);
1214 // Change bits in slot power register to force another shift out
1215 // NOTE: this is to work around the timer bug
1216 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1217 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1218 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1222 // Wait for SOBS to be unset
1223 wait_for_ctrl_irq (ctrl);
1225 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1226 if (ctrl->speed != adapter_speed)
1227 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1228 rc = WRONG_BUS_FREQUENCY;
1230 // turn off board without attaching to the bus
1231 disable_slot_power (ctrl, hp_slot);
1235 // Wait for SOBS to be unset
1236 wait_for_ctrl_irq (ctrl);
1238 // Done with exclusive hardware access
1239 up(&ctrl->crit_sect);
1244 // Wait for exclusive access to hardware
1245 down(&ctrl->crit_sect);
1247 slot_enable (ctrl, hp_slot);
1248 green_LED_blink (ctrl, hp_slot);
1250 amber_LED_off (ctrl, hp_slot);
1254 // Wait for SOBS to be unset
1255 wait_for_ctrl_irq (ctrl);
1257 // Done with exclusive hardware access
1258 up(&ctrl->crit_sect);
1260 // Wait for ~1 second because of hot plug spec
1263 // Check for a power fault
1264 if (func->status == 0xFF) {
1265 // power fault occurred, but it was benign
1269 rc = cpqhp_valid_replace(ctrl, func);
1272 // It must be the same board
1274 rc = cpqhp_configure_board(ctrl, func);
1277 // If configuration fails, turn it off
1278 // Get slot won't work for devices behind bridges, but
1279 // in this case it will always be called for the "base"
1280 // bus/dev/func of an adapter.
1282 // Wait for exclusive access to hardware
1283 down(&ctrl->crit_sect);
1285 amber_LED_on (ctrl, hp_slot);
1286 green_LED_off (ctrl, hp_slot);
1287 slot_disable (ctrl, hp_slot);
1291 // Wait for SOBS to be unset
1292 wait_for_ctrl_irq (ctrl);
1294 // Done with exclusive hardware access
1295 up(&ctrl->crit_sect);
1304 func->switch_save = 0x10;
1307 while (((func = cpqhp_slot_find(func->bus, func->device, index)) != NULL) && !rc) {
1308 rc |= cpqhp_configure_board(ctrl, func);
1313 // If configuration fails, turn it off
1314 // Get slot won't work for devices behind bridges, but
1315 // in this case it will always be called for the "base"
1316 // bus/dev/func of an adapter.
1318 // Wait for exclusive access to hardware
1319 down(&ctrl->crit_sect);
1321 amber_LED_on (ctrl, hp_slot);
1322 green_LED_off (ctrl, hp_slot);
1323 slot_disable (ctrl, hp_slot);
1327 // Wait for SOBS to be unset
1328 wait_for_ctrl_irq (ctrl);
1330 // Done with exclusive hardware access
1331 up(&ctrl->crit_sect);
1335 // Done configuring so turn LED on full time
1337 // Wait for exclusive access to hardware
1338 down(&ctrl->crit_sect);
1340 green_LED_on (ctrl, hp_slot);
1344 // Wait for SOBS to be unset
1345 wait_for_ctrl_irq (ctrl);
1347 // Done with exclusive hardware access
1348 up(&ctrl->crit_sect);
1351 // Something is wrong
1353 // Get slot won't work for devices behind bridges, but
1354 // in this case it will always be called for the "base"
1355 // bus/dev/func of an adapter.
1357 // Wait for exclusive access to hardware
1358 down(&ctrl->crit_sect);
1360 amber_LED_on (ctrl, hp_slot);
1361 green_LED_off (ctrl, hp_slot);
1362 slot_disable (ctrl, hp_slot);
1366 // Wait for SOBS to be unset
1367 wait_for_ctrl_irq (ctrl);
1369 // Done with exclusive hardware access
1370 up(&ctrl->crit_sect);
1380 * board_added - Called after a board has been added to the system.
1382 * Turns power on for the board
1386 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1392 u32 temp_register = 0xFFFFFFFF;
1394 struct pci_func *new_slot = NULL;
1395 struct slot *p_slot;
1396 struct resource_lists res_lists;
1398 hp_slot = func->device - ctrl->slot_device_offset;
1399 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1400 __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1402 // Wait for exclusive access to hardware
1403 down(&ctrl->crit_sect);
1405 // turn on board without attaching to the bus
1406 enable_slot_power (ctrl, hp_slot);
1410 // Wait for SOBS to be unset
1411 wait_for_ctrl_irq (ctrl);
1413 // Change bits in slot power register to force another shift out
1414 // NOTE: this is to work around the timer bug
1415 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1416 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1417 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1421 // Wait for SOBS to be unset
1422 wait_for_ctrl_irq (ctrl);
1424 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1425 if (ctrl->speed != adapter_speed)
1426 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1427 rc = WRONG_BUS_FREQUENCY;
1429 // turn off board without attaching to the bus
1430 disable_slot_power (ctrl, hp_slot);
1434 // Wait for SOBS to be unset
1435 wait_for_ctrl_irq (ctrl);
1437 // Done with exclusive hardware access
1438 up(&ctrl->crit_sect);
1443 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1445 // turn on board and blink green LED
1447 // Wait for exclusive access to hardware
1448 dbg("%s: before down\n", __FUNCTION__);
1449 down(&ctrl->crit_sect);
1450 dbg("%s: after down\n", __FUNCTION__);
1452 dbg("%s: before slot_enable\n", __FUNCTION__);
1453 slot_enable (ctrl, hp_slot);
1455 dbg("%s: before green_LED_blink\n", __FUNCTION__);
1456 green_LED_blink (ctrl, hp_slot);
1458 dbg("%s: before amber_LED_blink\n", __FUNCTION__);
1459 amber_LED_off (ctrl, hp_slot);
1461 dbg("%s: before set_SOGO\n", __FUNCTION__);
1464 // Wait for SOBS to be unset
1465 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
1466 wait_for_ctrl_irq (ctrl);
1467 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
1469 // Done with exclusive hardware access
1470 dbg("%s: before up\n", __FUNCTION__);
1471 up(&ctrl->crit_sect);
1472 dbg("%s: after up\n", __FUNCTION__);
1474 // Wait for ~1 second because of hot plug spec
1475 dbg("%s: before long_delay\n", __FUNCTION__);
1477 dbg("%s: after long_delay\n", __FUNCTION__);
1479 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1480 // Check for a power fault
1481 if (func->status == 0xFF) {
1482 // power fault occurred, but it was benign
1483 temp_register = 0xFFFFFFFF;
1484 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1488 // Get vendor/device ID u32
1489 ctrl->pci_bus->number = func->bus;
1490 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1491 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
1492 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1495 // Something's wrong here
1496 temp_register = 0xFFFFFFFF;
1497 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1499 // Preset return code. It will be changed later if things go okay.
1500 rc = NO_ADAPTER_PRESENT;
1503 // All F's is an empty slot or an invalid board
1504 if (temp_register != 0xFFFFFFFF) { // Check for a board in the slot
1505 res_lists.io_head = ctrl->io_head;
1506 res_lists.mem_head = ctrl->mem_head;
1507 res_lists.p_mem_head = ctrl->p_mem_head;
1508 res_lists.bus_head = ctrl->bus_head;
1509 res_lists.irqs = NULL;
1511 rc = configure_new_device(ctrl, func, 0, &res_lists);
1513 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1514 ctrl->io_head = res_lists.io_head;
1515 ctrl->mem_head = res_lists.mem_head;
1516 ctrl->p_mem_head = res_lists.p_mem_head;
1517 ctrl->bus_head = res_lists.bus_head;
1519 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1520 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1521 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1522 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1525 // Wait for exclusive access to hardware
1526 down(&ctrl->crit_sect);
1528 amber_LED_on (ctrl, hp_slot);
1529 green_LED_off (ctrl, hp_slot);
1530 slot_disable (ctrl, hp_slot);
1534 // Wait for SOBS to be unset
1535 wait_for_ctrl_irq (ctrl);
1537 // Done with exclusive hardware access
1538 up(&ctrl->crit_sect);
1541 cpqhp_save_slot_config(ctrl, func);
1546 func->switch_save = 0x10;
1547 func->is_a_board = 0x01;
1549 //next, we will instantiate the linux pci_dev structures (with appropriate driver notification, if already present)
1550 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
1553 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1554 if (new_slot && !new_slot->pci_dev) {
1555 cpqhp_configure_device(ctrl, new_slot);
1559 // Wait for exclusive access to hardware
1560 down(&ctrl->crit_sect);
1562 green_LED_on (ctrl, hp_slot);
1566 // Wait for SOBS to be unset
1567 wait_for_ctrl_irq (ctrl);
1569 // Done with exclusive hardware access
1570 up(&ctrl->crit_sect);
1572 // Wait for exclusive access to hardware
1573 down(&ctrl->crit_sect);
1575 amber_LED_on (ctrl, hp_slot);
1576 green_LED_off (ctrl, hp_slot);
1577 slot_disable (ctrl, hp_slot);
1581 // Wait for SOBS to be unset
1582 wait_for_ctrl_irq (ctrl);
1584 // Done with exclusive hardware access
1585 up(&ctrl->crit_sect);
1594 * remove_board - Turns off slot and LED's
1597 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1605 struct resource_lists res_lists;
1606 struct pci_func *temp_func;
1611 if (cpqhp_unconfigure_device(func))
1614 device = func->device;
1616 hp_slot = func->device - ctrl->slot_device_offset;
1617 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1619 // When we get here, it is safe to change base Address Registers.
1620 // We will attempt to save the base Address Register Lengths
1621 if (replace_flag || !ctrl->add_support)
1622 rc = cpqhp_save_base_addr_length(ctrl, func);
1623 else if (!func->bus_head && !func->mem_head &&
1624 !func->p_mem_head && !func->io_head) {
1625 // Here we check to see if we've saved any of the board's
1626 // resources already. If so, we'll skip the attempt to
1627 // determine what's being used.
1629 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1631 if (temp_func->bus_head || temp_func->mem_head
1632 || temp_func->p_mem_head || temp_func->io_head) {
1636 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1640 rc = cpqhp_save_used_resources(ctrl, func);
1642 // Change status to shutdown
1643 if (func->is_a_board)
1644 func->status = 0x01;
1645 func->configured = 0;
1647 // Wait for exclusive access to hardware
1648 down(&ctrl->crit_sect);
1650 green_LED_off (ctrl, hp_slot);
1651 slot_disable (ctrl, hp_slot);
1655 // turn off SERR for slot
1656 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1657 temp_byte &= ~(0x01 << hp_slot);
1658 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1660 // Wait for SOBS to be unset
1661 wait_for_ctrl_irq (ctrl);
1663 // Done with exclusive hardware access
1664 up(&ctrl->crit_sect);
1666 if (!replace_flag && ctrl->add_support) {
1668 res_lists.io_head = ctrl->io_head;
1669 res_lists.mem_head = ctrl->mem_head;
1670 res_lists.p_mem_head = ctrl->p_mem_head;
1671 res_lists.bus_head = ctrl->bus_head;
1673 cpqhp_return_board_resources(func, &res_lists);
1675 ctrl->io_head = res_lists.io_head;
1676 ctrl->mem_head = res_lists.mem_head;
1677 ctrl->p_mem_head = res_lists.p_mem_head;
1678 ctrl->bus_head = res_lists.bus_head;
1680 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1681 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1682 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1683 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1685 if (is_bridge(func)) {
1686 bridge_slot_remove(func);
1690 func = cpqhp_slot_find(ctrl->bus, device, 0);
1693 // Setup slot structure with entry for empty slot
1694 func = cpqhp_slot_create(ctrl->bus);
1701 func->bus = ctrl->bus;
1702 func->device = device;
1704 func->configured = 0;
1705 func->switch_save = 0x10;
1706 func->is_a_board = 0;
1707 func->p_task_event = NULL;
1714 static void pushbutton_helper_thread (unsigned long data)
1716 pushbutton_pending = data;
1717 up(&event_semaphore);
1721 // this is the main worker thread
1722 static int event_thread(void* data)
1724 struct controller *ctrl;
1726 daemonize("phpd_event");
1731 dbg("!!!!event_thread sleeping\n");
1732 down_interruptible (&event_semaphore);
1733 dbg("event_thread woken finished = %d\n", event_finished);
1734 if (event_finished) break;
1736 if (pushbutton_pending)
1737 cpqhp_pushbutton_thread(pushbutton_pending);
1739 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1740 interrupt_event_handler(ctrl);
1742 dbg("event_thread signals exit\n");
1748 int cpqhp_event_start_thread (void)
1752 /* initialize our semaphores */
1753 init_MUTEX(&delay_sem);
1754 init_MUTEX_LOCKED(&event_semaphore);
1755 init_MUTEX_LOCKED(&event_exit);
1758 pid = kernel_thread(event_thread, 0, 0);
1760 err ("Can't start up our event thread\n");
1763 dbg("Our event thread pid = %d\n", pid);
1768 void cpqhp_event_stop_thread (void)
1771 dbg("event_thread finish command given\n");
1772 up(&event_semaphore);
1773 dbg("wait for event_thread to exit\n");
1778 static int update_slot_info (struct controller *ctrl, struct slot *slot)
1780 struct hotplug_slot_info *info;
1783 info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
1787 info->power_status = get_slot_enabled(ctrl, slot);
1788 info->attention_status = cpq_get_attention_status(ctrl, slot);
1789 info->latch_status = cpq_get_latch_status(ctrl, slot);
1790 info->adapter_status = get_presence_status(ctrl, slot);
1791 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1796 static void interrupt_event_handler(struct controller *ctrl)
1800 struct pci_func *func;
1802 struct slot *p_slot;
1807 for (loop = 0; loop < 10; loop++) {
1808 //dbg("loop %d\n", loop);
1809 if (ctrl->event_queue[loop].event_type != 0) {
1810 hp_slot = ctrl->event_queue[loop].hp_slot;
1812 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1816 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1820 dbg("hp_slot %d, func %p, p_slot %p\n",
1821 hp_slot, func, p_slot);
1823 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1824 dbg("button pressed\n");
1825 } else if (ctrl->event_queue[loop].event_type ==
1826 INT_BUTTON_CANCEL) {
1827 dbg("button cancel\n");
1828 del_timer(&p_slot->task_event);
1830 // Wait for exclusive access to hardware
1831 down(&ctrl->crit_sect);
1833 if (p_slot->state == BLINKINGOFF_STATE) {
1835 // turn on green LED
1836 dbg("turn on green LED\n");
1837 green_LED_on (ctrl, hp_slot);
1838 } else if (p_slot->state == BLINKINGON_STATE) {
1840 // turn off green LED
1841 dbg("turn off green LED\n");
1842 green_LED_off (ctrl, hp_slot);
1845 info(msg_button_cancel, p_slot->number);
1847 p_slot->state = STATIC_STATE;
1849 amber_LED_off (ctrl, hp_slot);
1853 // Wait for SOBS to be unset
1854 wait_for_ctrl_irq (ctrl);
1856 // Done with exclusive hardware access
1857 up(&ctrl->crit_sect);
1859 // ***********button Released (No action on press...)
1860 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1861 dbg("button release\n");
1863 if (is_slot_enabled (ctrl, hp_slot)) {
1865 dbg("slot is on\n");
1866 p_slot->state = BLINKINGOFF_STATE;
1867 info(msg_button_off, p_slot->number);
1870 dbg("slot is off\n");
1871 p_slot->state = BLINKINGON_STATE;
1872 info(msg_button_on, p_slot->number);
1874 // Wait for exclusive access to hardware
1875 down(&ctrl->crit_sect);
1877 dbg("blink green LED and turn off amber\n");
1879 amber_LED_off (ctrl, hp_slot);
1880 green_LED_blink (ctrl, hp_slot);
1884 // Wait for SOBS to be unset
1885 wait_for_ctrl_irq (ctrl);
1887 // Done with exclusive hardware access
1888 up(&ctrl->crit_sect);
1889 init_timer(&p_slot->task_event);
1890 p_slot->hp_slot = hp_slot;
1891 p_slot->ctrl = ctrl;
1892 // p_slot->physical_slot = physical_slot;
1893 p_slot->task_event.expires = jiffies + 5 * HZ; // 5 second delay
1894 p_slot->task_event.function = pushbutton_helper_thread;
1895 p_slot->task_event.data = (u32) p_slot;
1897 dbg("add_timer p_slot = %p\n", p_slot);
1898 add_timer(&p_slot->task_event);
1900 // ***********POWER FAULT
1901 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1902 dbg("power fault\n");
1904 /* refresh notification */
1906 update_slot_info(ctrl, p_slot);
1909 ctrl->event_queue[loop].event_type = 0;
1913 } // End of FOR loop
1921 * cpqhp_pushbutton_thread
1923 * Scheduled procedure to handle blocking stuff for the pushbuttons
1924 * Handles all pending events and exits.
1927 void cpqhp_pushbutton_thread (unsigned long slot)
1931 struct pci_func *func;
1932 struct slot *p_slot = (struct slot *) slot;
1933 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1935 pushbutton_pending = 0;
1936 hp_slot = p_slot->hp_slot;
1938 device = p_slot->device;
1940 if (is_slot_enabled (ctrl, hp_slot)) {
1941 p_slot->state = POWEROFF_STATE;
1943 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1944 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1946 dbg("Error! func NULL in %s\n", __FUNCTION__);
1950 if (func != NULL && ctrl != NULL) {
1951 if (cpqhp_process_SS(ctrl, func) != 0) {
1952 amber_LED_on (ctrl, hp_slot);
1953 green_LED_on (ctrl, hp_slot);
1957 // Wait for SOBS to be unset
1958 wait_for_ctrl_irq (ctrl);
1962 p_slot->state = STATIC_STATE;
1964 p_slot->state = POWERON_STATE;
1967 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1968 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1970 dbg("Error! func NULL in %s\n", __FUNCTION__);
1974 if (func != NULL && ctrl != NULL) {
1975 if (cpqhp_process_SI(ctrl, func) != 0) {
1976 amber_LED_on (ctrl, hp_slot);
1977 green_LED_off (ctrl, hp_slot);
1981 // Wait for SOBS to be unset
1982 wait_for_ctrl_irq (ctrl);
1986 p_slot->state = STATIC_STATE;
1993 int cpqhp_process_SI (struct controller *ctrl, struct pci_func *func)
1999 struct slot* p_slot;
2000 int physical_slot = 0;
2007 device = func->device;
2008 hp_slot = device - ctrl->slot_device_offset;
2009 p_slot = cpqhp_find_slot(ctrl, device);
2011 physical_slot = p_slot->number;
2014 // Check to see if the interlock is closed
2015 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2017 if (tempdword & (0x01 << hp_slot)) {
2021 if (func->is_a_board) {
2022 rc = board_replaced(func, ctrl);
2027 func = cpqhp_slot_create(ctrl->bus);
2032 func->bus = ctrl->bus;
2033 func->device = device;
2035 func->configured = 0;
2036 func->is_a_board = 1;
2038 // We have to save the presence info for these slots
2039 temp_word = ctrl->ctrl_int_comp >> 16;
2040 func->presence_save = (temp_word >> hp_slot) & 0x01;
2041 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2043 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2044 func->switch_save = 0;
2046 func->switch_save = 0x10;
2049 rc = board_added(func, ctrl);
2051 if (is_bridge(func)) {
2052 bridge_slot_remove(func);
2056 // Setup slot structure with entry for empty slot
2057 func = cpqhp_slot_create(ctrl->bus);
2064 func->bus = ctrl->bus;
2065 func->device = device;
2067 func->configured = 0;
2068 func->is_a_board = 0;
2070 // We have to save the presence info for these slots
2071 temp_word = ctrl->ctrl_int_comp >> 16;
2072 func->presence_save = (temp_word >> hp_slot) & 0x01;
2073 func->presence_save |=
2074 (temp_word >> (hp_slot + 7)) & 0x02;
2076 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2077 func->switch_save = 0;
2079 func->switch_save = 0x10;
2085 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2089 update_slot_info(ctrl, p_slot);
2095 int cpqhp_process_SS (struct controller *ctrl, struct pci_func *func)
2097 u8 device, class_code, header_type, BCR;
2102 struct slot* p_slot;
2103 struct pci_bus *pci_bus = ctrl->pci_bus;
2104 int physical_slot=0;
2106 device = func->device;
2107 func = cpqhp_slot_find(ctrl->bus, device, index++);
2108 p_slot = cpqhp_find_slot(ctrl, device);
2110 physical_slot = p_slot->number;
2113 // Make sure there are no video controllers here
2114 while (func && !rc) {
2115 pci_bus->number = func->bus;
2116 devfn = PCI_DEVFN(func->device, func->function);
2118 // Check the Class Code
2119 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2123 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2124 /* Display/Video adapter (not supported) */
2125 rc = REMOVE_NOT_SUPPORTED;
2127 // See if it's a bridge
2128 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2132 // If it's a bridge, check the VGA Enable bit
2133 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2134 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2138 // If the VGA Enable bit is set, remove isn't supported
2139 if (BCR & PCI_BRIDGE_CTL_VGA) {
2140 rc = REMOVE_NOT_SUPPORTED;
2145 func = cpqhp_slot_find(ctrl->bus, device, index++);
2148 func = cpqhp_slot_find(ctrl->bus, device, 0);
2149 if ((func != NULL) && !rc) {
2150 //FIXME: Replace flag should be passed into process_SS
2151 replace_flag = !(ctrl->add_support);
2152 rc = remove_board(func, replace_flag, ctrl);
2158 update_slot_info(ctrl, p_slot);
2166 * hardware_test - runs hardware tests
2168 * For hot plug ctrl folks to play with.
2169 * test_num is the number entered in the GUI
2172 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2179 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2185 // Do that funky LED thing
2186 save_LED = readl(ctrl->hpc_reg + LED_CONTROL); // so we can restore them later
2187 work_LED = 0x01010101;
2188 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2189 for (loop = 0; loop < num_of_slots; loop++) {
2192 // Wait for SOGO interrupt
2193 wait_for_ctrl_irq (ctrl);
2195 // Get ready for next iteration
2196 work_LED = work_LED << 1;
2197 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2198 long_delay((2*HZ)/10);
2200 for (loop = 0; loop < num_of_slots; loop++) {
2201 work_LED = work_LED >> 1;
2202 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2206 // Wait for SOGO interrupt
2207 wait_for_ctrl_irq (ctrl);
2209 // Get ready for next iteration
2210 long_delay((2*HZ)/10);
2212 for (loop = 0; loop < num_of_slots; loop++) {
2213 work_LED = work_LED << 1;
2214 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2218 // Wait for SOGO interrupt
2219 wait_for_ctrl_irq (ctrl);
2221 // Get ready for next iteration
2222 long_delay((2*HZ)/10);
2224 for (loop = 0; loop < num_of_slots; loop++) {
2225 work_LED = work_LED >> 1;
2226 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2230 // Wait for SOGO interrupt
2231 wait_for_ctrl_irq (ctrl);
2233 // Get ready for next iteration
2234 long_delay((2*HZ)/10);
2237 work_LED = 0x01010000;
2238 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2239 for (loop = 0; loop < num_of_slots; loop++) {
2242 // Wait for SOGO interrupt
2243 wait_for_ctrl_irq (ctrl);
2245 // Get ready for next iteration
2246 work_LED = work_LED << 1;
2247 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2248 long_delay((2*HZ)/10);
2250 for (loop = 0; loop < num_of_slots; loop++) {
2251 work_LED = work_LED >> 1;
2252 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2256 // Wait for SOGO interrupt
2257 wait_for_ctrl_irq (ctrl);
2259 // Get ready for next iteration
2260 long_delay((2*HZ)/10);
2262 work_LED = 0x00000101;
2263 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2264 for (loop = 0; loop < num_of_slots; loop++) {
2265 work_LED = work_LED << 1;
2266 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2270 // Wait for SOGO interrupt
2271 wait_for_ctrl_irq (ctrl);
2273 // Get ready for next iteration
2274 long_delay((2*HZ)/10);
2276 for (loop = 0; loop < num_of_slots; loop++) {
2277 work_LED = work_LED >> 1;
2278 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2282 // Wait for SOGO interrupt
2283 wait_for_ctrl_irq (ctrl);
2285 // Get ready for next iteration
2286 long_delay((2*HZ)/10);
2290 work_LED = 0x01010000;
2291 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2292 for (loop = 0; loop < num_of_slots; loop++) {
2295 // Wait for SOGO interrupt
2296 wait_for_ctrl_irq (ctrl);
2298 // Get ready for next iteration
2299 long_delay((3*HZ)/10);
2300 work_LED = work_LED >> 16;
2301 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2305 // Wait for SOGO interrupt
2306 wait_for_ctrl_irq (ctrl);
2308 // Get ready for next iteration
2309 long_delay((3*HZ)/10);
2310 work_LED = work_LED << 16;
2311 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2312 work_LED = work_LED << 1;
2313 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2316 writel (save_LED, ctrl->hpc_reg + LED_CONTROL); // put it back the way it was
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) {
2410 new_slot->bus = func->bus;
2411 new_slot->device = func->device;
2412 new_slot->function = function;
2413 new_slot->is_a_board = 1;
2414 new_slot->status = 0;
2420 } while (function < max_functions);
2421 dbg("returning from configure_new_device\n");
2428 Configuration logic that involves the hotplug data structures and
2434 * configure_new_function - Configures the PCI header information of one device
2436 * @ctrl: pointer to controller structure
2437 * @func: pointer to function structure
2438 * @behind_bridge: 1 if this is a recursive call, 0 if not
2439 * @resources: pointer to set of resource lists
2441 * Calls itself recursively for bridged devices.
2442 * Returns 0 if success
2445 static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2446 u8 behind_bridge, struct resource_lists * resources)
2461 struct pci_resource *mem_node;
2462 struct pci_resource *p_mem_node;
2463 struct pci_resource *io_node;
2464 struct pci_resource *bus_node;
2465 struct pci_resource *hold_mem_node;
2466 struct pci_resource *hold_p_mem_node;
2467 struct pci_resource *hold_IO_node;
2468 struct pci_resource *hold_bus_node;
2469 struct irq_mapping irqs;
2470 struct pci_func *new_slot;
2471 struct pci_bus *pci_bus;
2472 struct resource_lists temp_resources;
2474 pci_bus = ctrl->pci_bus;
2475 pci_bus->number = func->bus;
2476 devfn = PCI_DEVFN(func->device, func->function);
2479 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2483 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
2485 dbg("set Primary bus = %d\n", func->bus);
2486 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2490 // find range of busses to use
2491 dbg("find ranges of buses to use\n");
2492 bus_node = get_max_resource(&resources->bus_head, 1);
2494 // If we don't have any busses to allocate, we can't continue
2498 // set Secondary bus
2499 temp_byte = bus_node->base;
2500 dbg("set Secondary bus = %d\n", bus_node->base);
2501 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2505 // set subordinate bus
2506 temp_byte = bus_node->base + bus_node->length - 1;
2507 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2508 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2512 // set subordinate Latency Timer and base Latency Timer
2514 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2517 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2521 // set Cache Line size
2523 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2527 // Setup the IO, memory, and prefetchable windows
2529 io_node = get_max_resource(&(resources->io_head), 0x1000);
2532 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2535 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2538 dbg("Setup the IO, memory, and prefetchable windows\n");
2540 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2542 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base, 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, p_mem_node->length, p_mem_node->next);
2546 // set up the IRQ info
2547 if (!resources->irqs) {
2548 irqs.barber_pole = 0;
2549 irqs.interrupt[0] = 0;
2550 irqs.interrupt[1] = 0;
2551 irqs.interrupt[2] = 0;
2552 irqs.interrupt[3] = 0;
2555 irqs.barber_pole = resources->irqs->barber_pole;
2556 irqs.interrupt[0] = resources->irqs->interrupt[0];
2557 irqs.interrupt[1] = resources->irqs->interrupt[1];
2558 irqs.interrupt[2] = resources->irqs->interrupt[2];
2559 irqs.interrupt[3] = resources->irqs->interrupt[3];
2560 irqs.valid_INT = resources->irqs->valid_INT;
2563 // set up resource lists that are now aligned on top and bottom
2564 // for anything behind the bridge.
2565 temp_resources.bus_head = bus_node;
2566 temp_resources.io_head = io_node;
2567 temp_resources.mem_head = mem_node;
2568 temp_resources.p_mem_head = p_mem_node;
2569 temp_resources.irqs = &irqs;
2571 // Make copies of the nodes we are going to pass down so that
2572 // if there is a problem,we can just use these to free resources
2573 hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2574 hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2575 hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2576 hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2578 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2580 kfree(hold_bus_node);
2582 kfree(hold_IO_node);
2584 kfree(hold_mem_node);
2585 if (hold_p_mem_node)
2586 kfree(hold_p_mem_node);
2591 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2593 bus_node->base += 1;
2594 bus_node->length -= 1;
2595 bus_node->next = NULL;
2597 // If we have IO resources copy them and fill in the bridge's
2598 // IO range registers
2600 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2601 io_node->next = NULL;
2603 // set IO base and Limit registers
2604 temp_byte = io_node->base >> 8;
2605 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2607 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2608 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2610 kfree(hold_IO_node);
2611 hold_IO_node = NULL;
2614 // If we have memory resources copy them and fill in the bridge's
2615 // memory range registers. Otherwise, fill in the range
2616 // registers with values that disable them.
2618 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2619 mem_node->next = NULL;
2621 // set Mem base and Limit registers
2622 temp_word = mem_node->base >> 16;
2623 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2625 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2626 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2629 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2632 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2634 kfree(hold_mem_node);
2635 hold_mem_node = NULL;
2638 // If we have prefetchable memory resources copy them and
2639 // fill in the bridge's memory range registers. Otherwise,
2640 // fill in the range registers with values that disable them.
2642 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2643 p_mem_node->next = NULL;
2645 // set Pre Mem base and Limit registers
2646 temp_word = p_mem_node->base >> 16;
2647 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2649 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2650 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2653 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2656 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2658 kfree(hold_p_mem_node);
2659 hold_p_mem_node = NULL;
2662 // Adjust this to compensate for extra adjustment in first loop
2667 // Here we actually find the devices and configure them
2668 for (device = 0; (device <= 0x1F) && !rc; device++) {
2669 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2672 pci_bus->number = hold_bus_node->base;
2673 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2674 pci_bus->number = func->bus;
2676 if (ID != 0xFFFFFFFF) { // device Present
2677 // Setup slot structure.
2678 new_slot = cpqhp_slot_create(hold_bus_node->base);
2680 if (new_slot == NULL) {
2686 new_slot->bus = hold_bus_node->base;
2687 new_slot->device = device;
2688 new_slot->function = 0;
2689 new_slot->is_a_board = 1;
2690 new_slot->status = 0;
2692 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2693 dbg("configure_new_device rc=0x%x\n",rc);
2694 } // End of IF (device in slot?)
2695 } // End of FOR loop
2698 cpqhp_destroy_resource_list(&temp_resources);
2700 return_resource(&(resources->bus_head), hold_bus_node);
2701 return_resource(&(resources->io_head), hold_IO_node);
2702 return_resource(&(resources->mem_head), hold_mem_node);
2703 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2706 // save the interrupt routing information
2707 if (resources->irqs) {
2708 resources->irqs->interrupt[0] = irqs.interrupt[0];
2709 resources->irqs->interrupt[1] = irqs.interrupt[1];
2710 resources->irqs->interrupt[2] = irqs.interrupt[2];
2711 resources->irqs->interrupt[3] = irqs.interrupt[3];
2712 resources->irqs->valid_INT = irqs.valid_INT;
2713 } else if (!behind_bridge) {
2714 // We need to hook up the interrupts here
2715 for (cloop = 0; cloop < 4; cloop++) {
2716 if (irqs.valid_INT & (0x01 << cloop)) {
2717 rc = cpqhp_set_irq(func->bus, func->device,
2718 0x0A + cloop, irqs.interrupt[cloop]);
2720 cpqhp_destroy_resource_list (&temp_resources);
2722 return_resource(&(resources-> bus_head), hold_bus_node);
2723 return_resource(&(resources-> io_head), hold_IO_node);
2724 return_resource(&(resources-> mem_head), hold_mem_node);
2725 return_resource(&(resources-> p_mem_head), hold_p_mem_node);
2729 } // end of for loop
2731 // Return unused bus resources
2732 // First use the temporary node to store information for the board
2733 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2734 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2736 hold_bus_node->next = func->bus_head;
2737 func->bus_head = hold_bus_node;
2739 temp_byte = temp_resources.bus_head->base - 1;
2741 // set subordinate bus
2742 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2744 if (temp_resources.bus_head->length == 0) {
2745 kfree(temp_resources.bus_head);
2746 temp_resources.bus_head = NULL;
2748 return_resource(&(resources->bus_head), temp_resources.bus_head);
2752 // If we have IO space available and there is some left,
2753 // return the unused portion
2754 if (hold_IO_node && temp_resources.io_head) {
2755 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2756 &hold_IO_node, 0x1000);
2758 // Check if we were able to split something off
2760 hold_IO_node->base = io_node->base + io_node->length;
2762 temp_byte = (hold_IO_node->base) >> 8;
2763 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2765 return_resource(&(resources->io_head), io_node);
2768 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2770 // Check if we were able to split something off
2772 // First use the temporary node to store information for the board
2773 hold_IO_node->length = io_node->base - hold_IO_node->base;
2775 // If we used any, add it to the board's list
2776 if (hold_IO_node->length) {
2777 hold_IO_node->next = func->io_head;
2778 func->io_head = hold_IO_node;
2780 temp_byte = (io_node->base - 1) >> 8;
2781 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2783 return_resource(&(resources->io_head), io_node);
2785 // it doesn't need any IO
2787 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2789 return_resource(&(resources->io_head), io_node);
2790 kfree(hold_IO_node);
2793 // it used most of the range
2794 hold_IO_node->next = func->io_head;
2795 func->io_head = hold_IO_node;
2797 } else if (hold_IO_node) {
2798 // it used the whole range
2799 hold_IO_node->next = func->io_head;
2800 func->io_head = hold_IO_node;
2802 // If we have memory space available and there is some left,
2803 // return the unused portion
2804 if (hold_mem_node && temp_resources.mem_head) {
2805 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2806 &hold_mem_node, 0x100000);
2808 // Check if we were able to split something off
2810 hold_mem_node->base = mem_node->base + mem_node->length;
2812 temp_word = (hold_mem_node->base) >> 16;
2813 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2815 return_resource(&(resources->mem_head), mem_node);
2818 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2820 // Check if we were able to split something off
2822 // First use the temporary node to store information for the board
2823 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2825 if (hold_mem_node->length) {
2826 hold_mem_node->next = func->mem_head;
2827 func->mem_head = hold_mem_node;
2829 // configure end address
2830 temp_word = (mem_node->base - 1) >> 16;
2831 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2833 // Return unused resources to the pool
2834 return_resource(&(resources->mem_head), mem_node);
2836 // it doesn't need any Mem
2838 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2840 return_resource(&(resources->mem_head), mem_node);
2841 kfree(hold_mem_node);
2844 // it used most of the range
2845 hold_mem_node->next = func->mem_head;
2846 func->mem_head = hold_mem_node;
2848 } else if (hold_mem_node) {
2849 // it used the whole range
2850 hold_mem_node->next = func->mem_head;
2851 func->mem_head = hold_mem_node;
2853 // If we have prefetchable memory space available and there is some
2854 // left at the end, return the unused portion
2855 if (hold_p_mem_node && temp_resources.p_mem_head) {
2856 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2857 &hold_p_mem_node, 0x100000);
2859 // Check if we were able to split something off
2861 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2863 temp_word = (hold_p_mem_node->base) >> 16;
2864 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2866 return_resource(&(resources->p_mem_head), p_mem_node);
2869 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2871 // Check if we were able to split something off
2873 // First use the temporary node to store information for the board
2874 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2876 // If we used any, add it to the board's list
2877 if (hold_p_mem_node->length) {
2878 hold_p_mem_node->next = func->p_mem_head;
2879 func->p_mem_head = hold_p_mem_node;
2881 temp_word = (p_mem_node->base - 1) >> 16;
2882 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2884 return_resource(&(resources->p_mem_head), p_mem_node);
2886 // it doesn't need any PMem
2888 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2890 return_resource(&(resources->p_mem_head), p_mem_node);
2891 kfree(hold_p_mem_node);
2894 // it used the most of the range
2895 hold_p_mem_node->next = func->p_mem_head;
2896 func->p_mem_head = hold_p_mem_node;
2898 } else if (hold_p_mem_node) {
2899 // it used the whole range
2900 hold_p_mem_node->next = func->p_mem_head;
2901 func->p_mem_head = hold_p_mem_node;
2903 // We should be configuring an IRQ and the bridge's base address
2904 // registers if it needs them. Although we have never seen such
2908 command = 0x0157; // = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
2909 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2911 // set Bridge Control Register
2912 command = 0x07; // = PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR | PCI_BRIDGE_CTL_NO_ISA
2913 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2914 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2916 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2918 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2919 // Display (video) adapter (not supported)
2920 return(DEVICE_TYPE_NOT_SUPPORTED);
2922 // Figure out IO and memory needs
2923 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2924 temp_register = 0xFFFFFFFF;
2926 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2927 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2929 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2930 dbg("CND: base = 0x%x\n", temp_register);
2932 if (temp_register) { // If this register is implemented
2933 if ((temp_register & 0x03L) == 0x01) {
2936 // set base = amount of IO space
2937 base = temp_register & 0xFFFFFFFC;
2940 dbg("CND: length = 0x%x\n", base);
2941 io_node = get_io_resource(&(resources->io_head), base);
2942 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2943 io_node->base, io_node->length, io_node->next);
2944 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2946 // allocate the resource to the board
2948 base = io_node->base;
2950 io_node->next = func->io_head;
2951 func->io_head = io_node;
2954 } else if ((temp_register & 0x0BL) == 0x08) {
2955 // Map prefetchable memory
2956 base = temp_register & 0xFFFFFFF0;
2959 dbg("CND: length = 0x%x\n", base);
2960 p_mem_node = get_resource(&(resources->p_mem_head), base);
2962 // allocate the resource to the board
2964 base = p_mem_node->base;
2966 p_mem_node->next = func->p_mem_head;
2967 func->p_mem_head = p_mem_node;
2970 } else if ((temp_register & 0x0BL) == 0x00) {
2972 base = temp_register & 0xFFFFFFF0;
2975 dbg("CND: length = 0x%x\n", base);
2976 mem_node = get_resource(&(resources->mem_head), base);
2978 // allocate the resource to the board
2980 base = mem_node->base;
2982 mem_node->next = func->mem_head;
2983 func->mem_head = mem_node;
2986 } else if ((temp_register & 0x0BL) == 0x04) {
2988 base = temp_register & 0xFFFFFFF0;
2991 dbg("CND: length = 0x%x\n", base);
2992 mem_node = get_resource(&(resources->mem_head), base);
2994 // allocate the resource to the board
2996 base = mem_node->base;
2998 mem_node->next = func->mem_head;
2999 func->mem_head = mem_node;
3002 } else if ((temp_register & 0x0BL) == 0x06) {
3003 // Those bits are reserved, we can't handle this
3006 // Requesting space below 1M
3007 return(NOT_ENOUGH_RESOURCES);
3010 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, base);
3012 // Check for 64-bit base
3013 if ((temp_register & 0x07L) == 0x04) {
3016 // Upper 32 bits of address always zero on today's systems
3017 // FIXME this is probably not true on Alpha and ia64???
3019 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, base);
3022 } // End of base register loop
3023 if (cpqhp_legacy_mode) {
3024 // Figure out which interrupt pin this function uses
3025 rc = pci_bus_read_config_byte (pci_bus, devfn,
3026 PCI_INTERRUPT_PIN, &temp_byte);
3028 // If this function needs an interrupt and we are behind a bridge
3029 // and the pin is tied to something that's alread mapped,
3030 // set this one the same
3031 if (temp_byte && resources->irqs &&
3032 (resources->irqs->valid_INT &
3033 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3034 // We have to share with something already set up
3035 IRQ = resources->irqs->interrupt[(temp_byte +
3036 resources->irqs->barber_pole - 1) & 0x03];
3038 // Program IRQ based on card type
3039 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3041 if (class_code == PCI_BASE_CLASS_STORAGE) {
3042 IRQ = cpqhp_disk_irq;
3044 IRQ = cpqhp_nic_irq;
3049 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3052 if (!behind_bridge) {
3053 rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3057 //TBD - this code may also belong in the other clause of this If statement
3058 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3059 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3064 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
3068 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
3070 // disable ROM base Address
3072 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_dword);
3075 temp_word = 0x0157; // = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
3076 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, temp_word);
3077 } // End of Not-A-Bridge else
3079 // It's some strange type of PCI adapter (Cardbus?)
3080 return(DEVICE_TYPE_NOT_SUPPORTED);
3083 func->configured = 1;