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/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/wait.h>
37 #include <linux/smp_lock.h>
38 #include <linux/pci.h>
41 static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,u8 behind_bridge, struct resource_lists *resources);
42 static int configure_new_function(struct controller* ctrl, struct pci_func *func,u8 behind_bridge, struct resource_lists *resources);
43 static void interrupt_event_handler(struct controller *ctrl);
45 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
46 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
47 static int event_finished;
48 static unsigned long pushbutton_pending; /* = 0 */
50 /* things needed for the long_delay function */
51 static struct semaphore delay_sem;
52 static wait_queue_head_t delay_wait;
54 /* delay is in jiffies to wait for */
55 static void long_delay (int delay)
57 DECLARE_WAITQUEUE(wait, current);
59 /* only allow 1 customer into the delay queue at once
60 * yes this makes some people wait even longer, but who really cares?
61 * this is for _huge_ delays to make the hardware happy as the
62 * signals bounce around
66 init_waitqueue_head (&delay_wait);
68 add_wait_queue(&delay_wait, &wait);
69 set_current_state(TASK_INTERRUPTIBLE);
70 schedule_timeout(delay);
71 remove_wait_queue(&delay_wait, &wait);
72 set_current_state(TASK_RUNNING);
78 //FIXME: The following line needs to be somewhere else...
79 #define WRONG_BUS_FREQUENCY 0x07
80 static u8 handle_switch_change(u8 change, struct controller * ctrl)
85 struct pci_func *func;
86 struct event_info *taskInfo;
92 dbg("cpqsbd: Switch interrupt received.\n");
94 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
95 if (change & (0x1L << hp_slot)) {
96 //*********************************
98 //*********************************
99 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
101 //this is the structure that tells the worker thread
103 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
104 ctrl->next_event = (ctrl->next_event + 1) % 10;
105 taskInfo->hp_slot = hp_slot;
109 temp_word = ctrl->ctrl_int_comp >> 16;
110 func->presence_save = (temp_word >> hp_slot) & 0x01;
111 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
113 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
114 //*********************************
116 //*********************************
118 func->switch_save = 0;
120 taskInfo->event_type = INT_SWITCH_OPEN;
122 //*********************************
124 //*********************************
126 func->switch_save = 0x10;
128 taskInfo->event_type = INT_SWITCH_CLOSE;
140 struct slot *cpqhp_find_slot (struct controller * ctrl, u8 device)
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, (hp_slot + ctrl->slot_device_offset), 0);
183 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
184 ctrl->next_event = (ctrl->next_event + 1) % 10;
185 taskInfo->hp_slot = hp_slot;
189 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
191 // If the switch closed, must be a button
192 // If not in button mode, nevermind
193 if (func->switch_save && (ctrl->push_button == 1)) {
194 temp_word = ctrl->ctrl_int_comp >> 16;
195 temp_byte = (temp_word >> hp_slot) & 0x01;
196 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
198 if (temp_byte != func->presence_save) {
199 //*********************************
200 // button Pressed (doesn't do anything)
201 //*********************************
202 dbg("hp_slot %d button pressed\n", hp_slot);
203 taskInfo->event_type = INT_BUTTON_PRESS;
205 //*********************************
206 // button Released - TAKE ACTION!!!!
207 //*********************************
208 dbg("hp_slot %d button released\n", hp_slot);
209 taskInfo->event_type = INT_BUTTON_RELEASE;
211 // Cancel if we are still blinking
212 if ((p_slot->state == BLINKINGON_STATE)
213 || (p_slot->state == BLINKINGOFF_STATE)) {
214 taskInfo->event_type = INT_BUTTON_CANCEL;
215 dbg("hp_slot %d button cancel\n", hp_slot);
216 } else if ((p_slot->state == POWERON_STATE)
217 || (p_slot->state == POWEROFF_STATE)) {
218 //info(msg_button_ignore, p_slot->number);
219 taskInfo->event_type = INT_BUTTON_IGNORE;
220 dbg("hp_slot %d button ignore\n", hp_slot);
224 // Switch is open, assume a presence change
225 // Save the presence state
226 temp_word = ctrl->ctrl_int_comp >> 16;
227 func->presence_save = (temp_word >> hp_slot) & 0x01;
228 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
230 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
231 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
232 //*********************************
234 //*********************************
235 taskInfo->event_type = INT_PRESENCE_ON;
237 //*********************************
239 //*********************************
240 taskInfo->event_type = INT_PRESENCE_OFF;
250 static u8 handle_power_fault(u8 change, struct controller * ctrl)
254 struct pci_func *func;
255 struct event_info *taskInfo;
260 //*********************************
262 //*********************************
264 info("power fault interrupt\n");
266 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
267 if (change & (0x01 << hp_slot)) {
268 //*********************************
270 //*********************************
271 func = cpqhp_slot_find(ctrl->bus, (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 to crash the
298 // machine to protect from data corruption
299 // simulated_NMI shouldn't ever return
301 //simulated_NMI(hp_slot, ctrl);
303 //The following code causes a software crash just in
304 //case simulated_NMI did return
306 //panic(msg_power_fault);
308 // set power fault status for this board
310 info("power fault bit %x set\n", hp_slot);
323 * Sorts nodes on the list by their length.
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
374 * Sorts nodes on the list by their length.
378 static int sort_by_max_size(struct pci_resource **head)
380 struct pci_resource *current_res;
381 struct pci_resource *next_res;
382 int out_of_order = 1;
387 if (!((*head)->next))
390 while (out_of_order) {
393 // Special case for swapping list head
394 if (((*head)->next) &&
395 ((*head)->length < (*head)->next->length)) {
398 *head = (*head)->next;
399 current_res->next = (*head)->next;
400 (*head)->next = current_res;
405 while (current_res->next && current_res->next->next) {
406 if (current_res->next->length < current_res->next->next->length) {
408 next_res = current_res->next;
409 current_res->next = current_res->next->next;
410 current_res = current_res->next;
411 next_res->next = current_res->next;
412 current_res->next = next_res;
414 current_res = current_res->next;
416 } // End of out_of_order loop
423 * do_pre_bridge_resource_split
425 * Returns zero or one node of resources that aren't in use
428 static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
430 struct pci_resource *prevnode = NULL;
431 struct pci_resource *node;
432 struct pci_resource *split_node;
435 dbg("do_pre_bridge_resource_split\n");
437 if (!(*head) || !(*orig_head))
440 rc = cpqhp_resource_sort_and_combine(head);
445 if ((*head)->base != (*orig_head)->base)
448 if ((*head)->length == (*orig_head)->length)
452 // If we got here, there the bridge requires some of the resource, but
453 // we may be able to split some off of the front
457 if (node->length & (alignment -1)) {
458 // this one isn't an aligned length, so we'll make a new entry
460 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
465 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
467 split_node->base = node->base;
468 split_node->length = temp_dword;
470 node->length -= temp_dword;
471 node->base += split_node->length;
473 // Put it in the list
475 split_node->next = node;
478 if (node->length < alignment) {
488 while (prevnode->next != node)
489 prevnode = prevnode->next;
491 prevnode->next = node->next;
500 * do_bridge_resource_split
502 * Returns zero or one node of resources that aren't in use
505 static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
507 struct pci_resource *prevnode = NULL;
508 struct pci_resource *node;
515 rc = cpqhp_resource_sort_and_combine(head);
528 if (node->length < alignment) {
533 if (node->base & (alignment - 1)) {
534 // Short circuit if adjusted size is too small
535 temp_dword = (node->base | (alignment-1)) + 1;
536 if ((node->length - (temp_dword - node->base)) < alignment) {
541 node->length -= (temp_dword - node->base);
542 node->base = temp_dword;
545 if (node->length & (alignment - 1)) {
546 // There's stuff in use after this node
558 * this function sorts the resource list by size and then
559 * returns the first node of "size" length that is not in the
560 * ISA aliasing window. If it finds a node larger than "size"
561 * it will split it up.
563 * size must be a power of two.
565 static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
567 struct pci_resource *prevnode;
568 struct pci_resource *node;
569 struct pci_resource *split_node;
575 if ( cpqhp_resource_sort_and_combine(head) )
578 if ( sort_by_size(head) )
581 for (node = *head; node; node = node->next) {
582 if (node->length < size)
585 if (node->base & (size - 1)) {
586 // this one isn't base aligned properly
587 // so we'll make a new entry and split it up
588 temp_dword = (node->base | (size-1)) + 1;
590 // Short circuit if adjusted size is too small
591 if ((node->length - (temp_dword - node->base)) < size)
594 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
599 split_node->base = node->base;
600 split_node->length = temp_dword - node->base;
601 node->base = temp_dword;
602 node->length -= split_node->length;
604 // Put it in the list
605 split_node->next = node->next;
606 node->next = split_node;
607 } // End of non-aligned base
609 // Don't need to check if too small since we already did
610 if (node->length > size) {
611 // this one is longer than we need
612 // so we'll make a new entry and split it up
613 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
618 split_node->base = node->base + size;
619 split_node->length = node->length - size;
622 // Put it in the list
623 split_node->next = node->next;
624 node->next = split_node;
625 } // End of too big on top end
627 // For IO make sure it's not in the ISA aliasing space
628 if (node->base & 0x300L)
631 // If we got here, then it is the right size
632 // Now take it out of the list
637 while (prevnode->next != node)
638 prevnode = prevnode->next;
640 prevnode->next = node->next;
654 * Gets the largest node that is at least "size" big from the
655 * list pointed to by head. It aligns the node on top and bottom
656 * to "size" alignment before returning it.
658 static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
660 struct pci_resource *max;
661 struct pci_resource *temp;
662 struct pci_resource *split_node;
668 if (cpqhp_resource_sort_and_combine(head))
671 if (sort_by_max_size(head))
674 for (max = *head;max; max = max->next) {
676 // If not big enough we could probably just bail,
677 // instead we'll continue to the next.
678 if (max->length < size)
681 if (max->base & (size - 1)) {
682 // this one isn't base aligned properly
683 // so we'll make a new entry and split it up
684 temp_dword = (max->base | (size-1)) + 1;
686 // Short circuit if adjusted size is too small
687 if ((max->length - (temp_dword - max->base)) < size)
690 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
695 split_node->base = max->base;
696 split_node->length = temp_dword - max->base;
697 max->base = temp_dword;
698 max->length -= split_node->length;
700 // Put it next in the list
701 split_node->next = max->next;
702 max->next = split_node;
705 if ((max->base + max->length) & (size - 1)) {
706 // this one isn't end aligned properly at the top
707 // so we'll make a new entry and split it up
708 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
712 temp_dword = ((max->base + max->length) & ~(size - 1));
713 split_node->base = temp_dword;
714 split_node->length = max->length + max->base
716 max->length -= split_node->length;
718 // Put it in the list
719 split_node->next = max->next;
720 max->next = split_node;
723 // Make sure it didn't shrink too much when we aligned it
724 if (max->length < size)
727 // Now take it out of the list
728 temp = (struct pci_resource*) *head;
732 while (temp && temp->next != max) {
736 temp->next = max->next;
743 // If we get here, we couldn't find one
751 * this function sorts the resource list by size and then
752 * returns the first node of "size" length. If it finds a node
753 * larger than "size" it will split it up.
755 * size must be a power of two.
757 static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
759 struct pci_resource *prevnode;
760 struct pci_resource *node;
761 struct pci_resource *split_node;
767 if ( cpqhp_resource_sort_and_combine(head) )
770 if ( sort_by_size(head) )
773 for (node = *head; node; node = node->next) {
774 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
775 __FUNCTION__, size, node, node->base, node->length);
776 if (node->length < size)
779 if (node->base & (size - 1)) {
780 dbg("%s: not aligned\n", __FUNCTION__);
781 // this one isn't base aligned properly
782 // so we'll make a new entry and split it up
783 temp_dword = (node->base | (size-1)) + 1;
785 // Short circuit if adjusted size is too small
786 if ((node->length - (temp_dword - node->base)) < size)
789 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
794 split_node->base = node->base;
795 split_node->length = temp_dword - node->base;
796 node->base = temp_dword;
797 node->length -= split_node->length;
799 // Put it in the list
800 split_node->next = node->next;
801 node->next = split_node;
802 } // End of non-aligned base
804 // Don't need to check if too small since we already did
805 if (node->length > size) {
806 dbg("%s: too big\n", __FUNCTION__);
807 // this one is longer than we need
808 // so we'll make a new entry and split it up
809 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
814 split_node->base = node->base + size;
815 split_node->length = node->length - size;
818 // Put it in the list
819 split_node->next = node->next;
820 node->next = split_node;
821 } // End of too big on top end
823 dbg("%s: got one!!!\n", __FUNCTION__);
824 // If we got here, then it is the right size
825 // Now take it out of the list
830 while (prevnode->next != node)
831 prevnode = prevnode->next;
833 prevnode->next = node->next;
844 * cpqhp_resource_sort_and_combine
846 * Sorts all of the nodes in the list in ascending order by
847 * their base addresses. Also does garbage collection by
848 * combining adjacent nodes.
850 * returns 0 if success
852 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
854 struct pci_resource *node1;
855 struct pci_resource *node2;
856 int out_of_order = 1;
858 dbg("%s: head = %p, *head = %p\n",__FUNCTION__, head, *head);
863 dbg("*head->next = %p\n",(*head)->next);
866 return(0); /* only one item on the list, already sorted! */
868 dbg("*head->base = 0x%x\n",(*head)->base);
869 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
870 while (out_of_order) {
873 // Special case for swapping list head
874 if (((*head)->next) &&
875 ((*head)->base > (*head)->next->base)) {
877 (*head) = (*head)->next;
878 node1->next = (*head)->next;
879 (*head)->next = node1;
885 while (node1->next && node1->next->next) {
886 if (node1->next->base > node1->next->next->base) {
889 node1->next = node1->next->next;
891 node2->next = node1->next;
896 } // End of out_of_order loop
900 while (node1 && node1->next) {
901 if ((node1->base + node1->length) == node1->next->base) {
904 node1->length += node1->next->length;
906 node1->next = node1->next->next;
916 void cpqhp_ctrl_intr(int IRQ, struct controller * ctrl, struct pt_regs *regs)
918 u8 schedule_flag = 0;
925 misc = readw(ctrl->hpc_reg + MISC);
926 //*********************************
927 // Check to see if it was our interrupt
928 //*********************************
929 if (!(misc & 0x000C)) {
934 //*********************************
935 // Serial Output interrupt Pending
936 //*********************************
938 // Clear the interrupt
940 writew(misc, ctrl->hpc_reg + MISC);
942 // Read to clear posted writes
943 misc = readw(ctrl->hpc_reg + MISC);
945 dbg ("%s - waking up\n", __FUNCTION__);
946 wake_up_interruptible(&ctrl->queue);
950 // General-interrupt-input interrupt Pending
951 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
953 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
955 // Clear the interrupt
956 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
958 // Read it back to clear any posted writes
959 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
962 // Clear all interrupts
963 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
966 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
967 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
968 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
971 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
973 /* Bus Reset has completed */
975 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
976 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
977 wake_up_interruptible(&ctrl->queue);
981 up(&event_semaphore);
982 dbg("Signal event_semaphore\n");
983 mark_bh(IMMEDIATE_BH);
990 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
991 * @busnumber - bus where new node is to be located
993 * Returns pointer to the new node or NULL if unsuccessful
995 struct pci_func *cpqhp_slot_create(u8 busnumber)
997 struct pci_func *new_slot;
998 struct pci_func *next;
1000 new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL);
1002 if (new_slot == NULL) {
1003 // I'm not dead yet!
1008 memset(new_slot, 0, sizeof(struct pci_func));
1010 new_slot->next = NULL;
1011 new_slot->configured = 1;
1013 if (cpqhp_slot_list[busnumber] == NULL) {
1014 cpqhp_slot_list[busnumber] = new_slot;
1016 next = cpqhp_slot_list[busnumber];
1017 while (next->next != NULL)
1019 next->next = new_slot;
1026 * slot_remove - Removes a node from the linked list of slots.
1027 * @old_slot: slot to remove
1029 * Returns 0 if successful, !0 otherwise.
1031 static int slot_remove(struct pci_func * old_slot)
1033 struct pci_func *next;
1035 if (old_slot == NULL)
1038 next = cpqhp_slot_list[old_slot->bus];
1044 if (next == old_slot) {
1045 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1046 cpqhp_destroy_board_resources(old_slot);
1051 while ((next->next != old_slot) && (next->next != NULL)) {
1055 if (next->next == old_slot) {
1056 next->next = old_slot->next;
1057 cpqhp_destroy_board_resources(old_slot);
1066 * bridge_slot_remove - Removes a node from the linked list of slots.
1067 * @bridge: bridge to remove
1069 * Returns 0 if successful, !0 otherwise.
1071 static int bridge_slot_remove(struct pci_func *bridge)
1073 u8 subordinateBus, secondaryBus;
1075 struct pci_func *next;
1080 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1081 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1083 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1084 next = cpqhp_slot_list[tempBus];
1086 while (!slot_remove(next)) {
1087 next = cpqhp_slot_list[tempBus];
1091 next = cpqhp_slot_list[bridge->bus];
1097 if (next == bridge) {
1098 cpqhp_slot_list[bridge->bus] = bridge->next;
1103 while ((next->next != bridge) && (next->next != NULL)) {
1107 if (next->next == bridge) {
1108 next->next = bridge->next;
1117 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1119 * @device: device to find
1120 * @index: is 0 for first function found, 1 for the second...
1122 * Returns pointer to the node if successful, %NULL otherwise.
1124 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1127 struct pci_func *func;
1129 func = cpqhp_slot_list[bus];
1131 if ((func == NULL) || ((func->device == device) && (index == 0)))
1134 if (func->device == device)
1137 while (func->next != NULL) {
1140 if (func->device == device)
1151 // DJZ: I don't think is_bridge will work as is.
1153 static int is_bridge(struct pci_func * func)
1155 // Check the header type
1156 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1163 /* the following routines constitute the bulk of the
1164 hotplug controller logic
1169 * board_replaced - Called after a board has been replaced in the system.
1171 * This is only used if we don't have resources for hot add
1172 * Turns power on for the board
1173 * Checks to see if board is the same
1174 * If board is same, reconfigures it
1175 * If board isn't same, turns it back off.
1178 static u32 board_replaced(struct pci_func * func, struct controller * ctrl)
1187 hp_slot = func->device - ctrl->slot_device_offset;
1189 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
1190 //*********************************
1191 // The switch is open.
1192 //*********************************
1193 rc = INTERLOCK_OPEN;
1194 } else if (is_slot_enabled (ctrl, hp_slot)) {
1195 //*********************************
1196 // The board is already on
1197 //*********************************
1198 rc = CARD_FUNCTIONING;
1200 // Wait for exclusive access to hardware
1201 down(&ctrl->crit_sect);
1203 // turn on board without attaching to the bus
1204 enable_slot_power (ctrl, hp_slot);
1208 // Wait for SOBS to be unset
1209 wait_for_ctrl_irq (ctrl);
1211 // Change bits in slot power register to force another shift out
1212 // NOTE: this is to work around the timer bug
1213 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1214 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1215 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1219 // Wait for SOBS to be unset
1220 wait_for_ctrl_irq (ctrl);
1222 // 66MHz and/or PCI-X support check
1223 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1224 if (ctrl->speed != adapter_speed)
1225 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1226 rc = WRONG_BUS_FREQUENCY;
1228 // turn off board without attaching to the bus
1229 disable_slot_power (ctrl, hp_slot);
1233 // Wait for SOBS to be unset
1234 wait_for_ctrl_irq (ctrl);
1236 // Done with exclusive hardware access
1237 up(&ctrl->crit_sect);
1242 // Wait for exclusive access to hardware
1243 down(&ctrl->crit_sect);
1245 slot_enable (ctrl, hp_slot);
1246 green_LED_blink (ctrl, hp_slot);
1248 amber_LED_off (ctrl, hp_slot);
1252 // Wait for SOBS to be unset
1253 wait_for_ctrl_irq (ctrl);
1255 // Done with exclusive hardware access
1256 up(&ctrl->crit_sect);
1258 // Wait for ~1 second because of hot plug spec
1261 // Check for a power fault
1262 if (func->status == 0xFF) {
1263 // power fault occurred, but it was benign
1267 rc = cpqhp_valid_replace(ctrl, func);
1270 // It must be the same board
1272 rc = cpqhp_configure_board(ctrl, func);
1275 // If configuration fails, turn it off
1276 // Get slot won't work for devices behind bridges, but
1277 // in this case it will always be called for the "base"
1278 // bus/dev/func of an adapter.
1280 // Wait for exclusive access to hardware
1281 down(&ctrl->crit_sect);
1283 amber_LED_on (ctrl, hp_slot);
1284 green_LED_off (ctrl, hp_slot);
1285 slot_disable (ctrl, hp_slot);
1289 // Wait for SOBS to be unset
1290 wait_for_ctrl_irq (ctrl);
1292 // Done with exclusive hardware access
1293 up(&ctrl->crit_sect);
1302 func->switch_save = 0x10;
1305 while (((func = cpqhp_slot_find(func->bus, func->device, index)) != NULL) && !rc) {
1306 rc |= cpqhp_configure_board(ctrl, func);
1311 // If configuration fails, turn it off
1312 // Get slot won't work for devices behind bridges, but
1313 // in this case it will always be called for the "base"
1314 // bus/dev/func of an adapter.
1316 // Wait for exclusive access to hardware
1317 down(&ctrl->crit_sect);
1319 amber_LED_on (ctrl, hp_slot);
1320 green_LED_off (ctrl, hp_slot);
1321 slot_disable (ctrl, hp_slot);
1325 // Wait for SOBS to be unset
1326 wait_for_ctrl_irq (ctrl);
1328 // Done with exclusive hardware access
1329 up(&ctrl->crit_sect);
1333 // Done configuring so turn LED on full time
1335 // Wait for exclusive access to hardware
1336 down(&ctrl->crit_sect);
1338 green_LED_on (ctrl, hp_slot);
1342 // Wait for SOBS to be unset
1343 wait_for_ctrl_irq (ctrl);
1345 // Done with exclusive hardware access
1346 up(&ctrl->crit_sect);
1349 // Something is wrong
1351 // Get slot won't work for devices behind bridges, but
1352 // in this case it will always be called for the "base"
1353 // bus/dev/func of an adapter.
1355 // Wait for exclusive access to hardware
1356 down(&ctrl->crit_sect);
1358 amber_LED_on (ctrl, hp_slot);
1359 green_LED_off (ctrl, hp_slot);
1360 slot_disable (ctrl, hp_slot);
1364 // Wait for SOBS to be unset
1365 wait_for_ctrl_irq (ctrl);
1367 // Done with exclusive hardware access
1368 up(&ctrl->crit_sect);
1378 * board_added - Called after a board has been added to the system.
1380 * Turns power on for the board
1384 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1390 u32 temp_register = 0xFFFFFFFF;
1392 struct pci_func *new_slot = NULL;
1393 struct slot *p_slot;
1394 struct resource_lists res_lists;
1396 hp_slot = func->device - ctrl->slot_device_offset;
1397 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1398 __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1400 // Wait for exclusive access to hardware
1401 down(&ctrl->crit_sect);
1403 // turn on board without attaching to the bus
1404 enable_slot_power (ctrl, hp_slot);
1408 // Wait for SOBS to be unset
1409 wait_for_ctrl_irq (ctrl);
1411 // Change bits in slot power register to force another shift out
1412 // NOTE: this is to work around the timer bug
1413 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1414 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1415 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1419 // Wait for SOBS to be unset
1420 wait_for_ctrl_irq (ctrl);
1422 // 66MHz and/or PCI-X support check
1423 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1424 if (ctrl->speed != adapter_speed)
1425 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1426 rc = WRONG_BUS_FREQUENCY;
1428 // turn off board without attaching to the bus
1429 disable_slot_power (ctrl, hp_slot);
1433 // Wait for SOBS to be unset
1434 wait_for_ctrl_irq (ctrl);
1436 // Done with exclusive hardware access
1437 up(&ctrl->crit_sect);
1442 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1444 // turn on board and blink green LED
1446 // Wait for exclusive access to hardware
1447 dbg("%s: before down\n", __FUNCTION__);
1448 down(&ctrl->crit_sect);
1449 dbg("%s: after down\n", __FUNCTION__);
1451 dbg("%s: before slot_enable\n", __FUNCTION__);
1452 slot_enable (ctrl, hp_slot);
1454 dbg("%s: before green_LED_blink\n", __FUNCTION__);
1455 green_LED_blink (ctrl, hp_slot);
1457 dbg("%s: before amber_LED_blink\n", __FUNCTION__);
1458 amber_LED_off (ctrl, hp_slot);
1460 dbg("%s: before set_SOGO\n", __FUNCTION__);
1463 // Wait for SOBS to be unset
1464 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
1465 wait_for_ctrl_irq (ctrl);
1466 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
1468 // Done with exclusive hardware access
1469 dbg("%s: before up\n", __FUNCTION__);
1470 up(&ctrl->crit_sect);
1471 dbg("%s: after up\n", __FUNCTION__);
1473 // Wait for ~1 second because of hot plug spec
1474 dbg("%s: before long_delay\n", __FUNCTION__);
1476 dbg("%s: after long_delay\n", __FUNCTION__);
1478 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1479 // Check for a power fault
1480 if (func->status == 0xFF) {
1481 // power fault occurred, but it was benign
1482 temp_register = 0xFFFFFFFF;
1483 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1487 // Get vendor/device ID u32
1488 rc = pci_read_config_dword_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_VENDOR_ID, &temp_register);
1489 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
1490 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1493 // Something's wrong here
1494 temp_register = 0xFFFFFFFF;
1495 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1497 // Preset return code. It will be changed later if things go okay.
1498 rc = NO_ADAPTER_PRESENT;
1501 // All F's is an empty slot or an invalid board
1502 if (temp_register != 0xFFFFFFFF) { // Check for a board in the slot
1503 res_lists.io_head = ctrl->io_head;
1504 res_lists.mem_head = ctrl->mem_head;
1505 res_lists.p_mem_head = ctrl->p_mem_head;
1506 res_lists.bus_head = ctrl->bus_head;
1507 res_lists.irqs = NULL;
1509 rc = configure_new_device(ctrl, func, 0, &res_lists);
1511 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1512 ctrl->io_head = res_lists.io_head;
1513 ctrl->mem_head = res_lists.mem_head;
1514 ctrl->p_mem_head = res_lists.p_mem_head;
1515 ctrl->bus_head = res_lists.bus_head;
1517 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1518 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1519 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1520 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1523 // Wait for exclusive access to hardware
1524 down(&ctrl->crit_sect);
1526 amber_LED_on (ctrl, hp_slot);
1527 green_LED_off (ctrl, hp_slot);
1528 slot_disable (ctrl, hp_slot);
1532 // Wait for SOBS to be unset
1533 wait_for_ctrl_irq (ctrl);
1535 // Done with exclusive hardware access
1536 up(&ctrl->crit_sect);
1539 cpqhp_save_slot_config(ctrl, func);
1544 func->switch_save = 0x10;
1545 func->is_a_board = 0x01;
1547 //next, we will instantiate the linux pci_dev structures (with appropriate driver notification, if already present)
1548 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
1551 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1552 if (new_slot && !new_slot->pci_dev) {
1553 cpqhp_configure_device(ctrl, new_slot);
1557 // Wait for exclusive access to hardware
1558 down(&ctrl->crit_sect);
1560 green_LED_on (ctrl, hp_slot);
1564 // Wait for SOBS to be unset
1565 wait_for_ctrl_irq (ctrl);
1567 // Done with exclusive hardware access
1568 up(&ctrl->crit_sect);
1570 // Wait for exclusive access to hardware
1571 down(&ctrl->crit_sect);
1573 amber_LED_on (ctrl, hp_slot);
1574 green_LED_off (ctrl, hp_slot);
1575 slot_disable (ctrl, hp_slot);
1579 // Wait for SOBS to be unset
1580 wait_for_ctrl_irq (ctrl);
1582 // Done with exclusive hardware access
1583 up(&ctrl->crit_sect);
1592 * remove_board - Turns off slot and LED's
1595 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1603 struct resource_lists res_lists;
1604 struct pci_func *temp_func;
1609 if (cpqhp_unconfigure_device(func))
1612 device = func->device;
1614 hp_slot = func->device - ctrl->slot_device_offset;
1615 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1617 // When we get here, it is safe to change base Address Registers.
1618 // We will attempt to save the base Address Register Lengths
1619 if (replace_flag || !ctrl->add_support)
1620 rc = cpqhp_save_base_addr_length(ctrl, func);
1621 else if (!func->bus_head && !func->mem_head &&
1622 !func->p_mem_head && !func->io_head) {
1623 // Here we check to see if we've saved any of the board's
1624 // resources already. If so, we'll skip the attempt to
1625 // determine what's being used.
1627 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1629 if (temp_func->bus_head || temp_func->mem_head
1630 || temp_func->p_mem_head || temp_func->io_head) {
1634 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1638 rc = cpqhp_save_used_resources(ctrl, func);
1640 // Change status to shutdown
1641 if (func->is_a_board)
1642 func->status = 0x01;
1643 func->configured = 0;
1645 // Wait for exclusive access to hardware
1646 down(&ctrl->crit_sect);
1648 green_LED_off (ctrl, hp_slot);
1649 slot_disable (ctrl, hp_slot);
1653 // turn off SERR for slot
1654 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1655 temp_byte &= ~(0x01 << hp_slot);
1656 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1658 // Wait for SOBS to be unset
1659 wait_for_ctrl_irq (ctrl);
1661 // Done with exclusive hardware access
1662 up(&ctrl->crit_sect);
1664 if (!replace_flag && ctrl->add_support) {
1666 res_lists.io_head = ctrl->io_head;
1667 res_lists.mem_head = ctrl->mem_head;
1668 res_lists.p_mem_head = ctrl->p_mem_head;
1669 res_lists.bus_head = ctrl->bus_head;
1671 cpqhp_return_board_resources(func, &res_lists);
1673 ctrl->io_head = res_lists.io_head;
1674 ctrl->mem_head = res_lists.mem_head;
1675 ctrl->p_mem_head = res_lists.p_mem_head;
1676 ctrl->bus_head = res_lists.bus_head;
1678 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1679 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1680 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1681 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1683 if (is_bridge(func)) {
1684 bridge_slot_remove(func);
1688 func = cpqhp_slot_find(ctrl->bus, device, 0);
1691 // Setup slot structure with entry for empty slot
1692 func = cpqhp_slot_create(ctrl->bus);
1699 func->bus = ctrl->bus;
1700 func->device = device;
1702 func->configured = 0;
1703 func->switch_save = 0x10;
1704 func->is_a_board = 0;
1705 func->p_task_event = NULL;
1712 static void pushbutton_helper_thread (unsigned long data)
1714 pushbutton_pending = data;
1715 up(&event_semaphore);
1719 // this is the main worker thread
1720 static int event_thread(void* data)
1722 struct controller *ctrl;
1728 strcpy(current->comm, "phpd_event");
1730 /* avoid getting signals */
1731 spin_lock_irq(¤t->sigmask_lock);
1732 flush_signals(current);
1733 sigfillset(¤t->blocked);
1734 recalc_sigpending(current);
1735 spin_unlock_irq(¤t->sigmask_lock);
1740 dbg("!!!!event_thread sleeping\n");
1741 down_interruptible (&event_semaphore);
1742 dbg("event_thread woken finished = %d\n", event_finished);
1743 if (event_finished) break;
1745 if (pushbutton_pending)
1746 cpqhp_pushbutton_thread(pushbutton_pending);
1748 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1749 interrupt_event_handler(ctrl);
1751 dbg("event_thread signals exit\n");
1757 int cpqhp_event_start_thread (void)
1761 /* initialize our semaphores */
1762 init_MUTEX(&delay_sem);
1763 init_MUTEX_LOCKED(&event_semaphore);
1764 init_MUTEX_LOCKED(&event_exit);
1767 pid = kernel_thread(event_thread, 0, 0);
1769 err ("Can't start up our event thread\n");
1772 dbg("Our event thread pid = %d\n", pid);
1777 void cpqhp_event_stop_thread (void)
1780 dbg("event_thread finish command given\n");
1781 up(&event_semaphore);
1782 dbg("wait for event_thread to exit\n");
1787 static int update_slot_info (struct controller *ctrl, struct slot *slot)
1789 struct hotplug_slot_info *info;
1790 char buffer[SLOT_NAME_SIZE];
1793 info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
1797 make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot);
1798 info->power_status = get_slot_enabled(ctrl, slot);
1799 info->attention_status = cpq_get_attention_status(ctrl, slot);
1800 info->latch_status = cpq_get_latch_status(ctrl, slot);
1801 info->adapter_status = get_presence_status(ctrl, slot);
1802 result = pci_hp_change_slot_info(buffer, info);
1807 static void interrupt_event_handler(struct controller *ctrl)
1811 struct pci_func *func;
1813 struct slot *p_slot;
1818 for (loop = 0; loop < 10; loop++) {
1819 //dbg("loop %d\n", loop);
1820 if (ctrl->event_queue[loop].event_type != 0) {
1821 hp_slot = ctrl->event_queue[loop].hp_slot;
1823 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1825 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1827 dbg("hp_slot %d, func %p, p_slot %p\n",
1828 hp_slot, func, p_slot);
1830 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1831 dbg("button pressed\n");
1832 } else if (ctrl->event_queue[loop].event_type ==
1833 INT_BUTTON_CANCEL) {
1834 dbg("button cancel\n");
1835 del_timer(&p_slot->task_event);
1837 // Wait for exclusive access to hardware
1838 down(&ctrl->crit_sect);
1840 if (p_slot->state == BLINKINGOFF_STATE) {
1842 // turn on green LED
1843 dbg("turn on green LED\n");
1844 green_LED_on (ctrl, hp_slot);
1845 } else if (p_slot->state == BLINKINGON_STATE) {
1847 // turn off green LED
1848 dbg("turn off green LED\n");
1849 green_LED_off (ctrl, hp_slot);
1852 info(msg_button_cancel, p_slot->number);
1854 p_slot->state = STATIC_STATE;
1856 amber_LED_off (ctrl, hp_slot);
1860 // Wait for SOBS to be unset
1861 wait_for_ctrl_irq (ctrl);
1863 // Done with exclusive hardware access
1864 up(&ctrl->crit_sect);
1866 // ***********button Released (No action on press...)
1867 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1868 dbg("button release\n");
1870 if (is_slot_enabled (ctrl, hp_slot)) {
1872 dbg("slot is on\n");
1873 p_slot->state = BLINKINGOFF_STATE;
1874 info(msg_button_off, p_slot->number);
1877 dbg("slot is off\n");
1878 p_slot->state = BLINKINGON_STATE;
1879 info(msg_button_on, p_slot->number);
1881 // Wait for exclusive access to hardware
1882 down(&ctrl->crit_sect);
1884 dbg("blink green LED and turn off amber\n");
1885 amber_LED_off (ctrl, hp_slot);
1886 green_LED_blink (ctrl, hp_slot);
1890 // Wait for SOBS to be unset
1891 wait_for_ctrl_irq (ctrl);
1893 // Done with exclusive hardware access
1894 up(&ctrl->crit_sect);
1895 init_timer(&p_slot->task_event);
1896 p_slot->hp_slot = hp_slot;
1897 p_slot->ctrl = ctrl;
1898 // p_slot->physical_slot = physical_slot;
1899 p_slot->task_event.expires = jiffies + 5 * HZ; // 5 second delay
1900 p_slot->task_event.function = pushbutton_helper_thread;
1901 p_slot->task_event.data = (u32) p_slot;
1903 dbg("add_timer p_slot = %p\n", p_slot);
1904 add_timer(&p_slot->task_event);
1906 // ***********POWER FAULT
1907 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1908 dbg("power fault\n");
1910 /* refresh notification */
1912 update_slot_info(ctrl, p_slot);
1915 ctrl->event_queue[loop].event_type = 0;
1919 } // End of FOR loop
1927 * cpqhp_pushbutton_thread
1929 * Scheduled procedure to handle blocking stuff for the pushbuttons
1930 * Handles all pending events and exits.
1933 void cpqhp_pushbutton_thread (unsigned long slot)
1937 struct pci_func *func;
1938 struct slot *p_slot = (struct slot *) slot;
1939 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1941 pushbutton_pending = 0;
1942 hp_slot = p_slot->hp_slot;
1944 device = p_slot->device;
1946 if (is_slot_enabled (ctrl, hp_slot)) {
1947 p_slot->state = POWEROFF_STATE;
1949 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1950 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1952 dbg("Error! func NULL in %s\n", __FUNCTION__);
1956 if (func != NULL && ctrl != NULL) {
1957 if (cpqhp_process_SS(ctrl, func) != 0) {
1958 amber_LED_on (ctrl, hp_slot);
1959 green_LED_on (ctrl, hp_slot);
1963 // Wait for SOBS to be unset
1964 wait_for_ctrl_irq (ctrl);
1968 p_slot->state = STATIC_STATE;
1970 p_slot->state = POWERON_STATE;
1973 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1974 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1976 dbg("Error! func NULL in %s\n", __FUNCTION__);
1980 if (func != NULL && ctrl != NULL) {
1981 if (cpqhp_process_SI(ctrl, func) != 0) {
1982 amber_LED_on (ctrl, hp_slot);
1983 green_LED_off (ctrl, hp_slot);
1987 // Wait for SOBS to be unset
1988 wait_for_ctrl_irq (ctrl);
1992 p_slot->state = STATIC_STATE;
1999 int cpqhp_process_SI (struct controller *ctrl, struct pci_func *func)
2005 struct slot* p_slot;
2006 int physical_slot = 0;
2013 device = func->device;
2014 hp_slot = device - ctrl->slot_device_offset;
2015 p_slot = cpqhp_find_slot(ctrl, device);
2017 physical_slot = p_slot->number;
2020 // Check to see if the interlock is closed
2021 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2023 if (tempdword & (0x01 << hp_slot)) {
2027 if (func->is_a_board) {
2028 rc = board_replaced(func, ctrl);
2033 func = cpqhp_slot_create(ctrl->bus);
2038 func->bus = ctrl->bus;
2039 func->device = device;
2041 func->configured = 0;
2042 func->is_a_board = 1;
2044 // We have to save the presence info for these slots
2045 temp_word = ctrl->ctrl_int_comp >> 16;
2046 func->presence_save = (temp_word >> hp_slot) & 0x01;
2047 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2049 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2050 func->switch_save = 0;
2052 func->switch_save = 0x10;
2055 rc = board_added(func, ctrl);
2057 if (is_bridge(func)) {
2058 bridge_slot_remove(func);
2062 // Setup slot structure with entry for empty slot
2063 func = cpqhp_slot_create(ctrl->bus);
2070 func->bus = ctrl->bus;
2071 func->device = device;
2073 func->configured = 0;
2074 func->is_a_board = 0;
2076 // We have to save the presence info for these slots
2077 temp_word = ctrl->ctrl_int_comp >> 16;
2078 func->presence_save = (temp_word >> hp_slot) & 0x01;
2079 func->presence_save |=
2080 (temp_word >> (hp_slot + 7)) & 0x02;
2082 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2083 func->switch_save = 0;
2085 func->switch_save = 0x10;
2091 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2095 update_slot_info(ctrl, p_slot);
2101 int cpqhp_process_SS (struct controller *ctrl, struct pci_func *func)
2103 u8 device, class_code, header_type, BCR;
2107 struct slot* p_slot;
2108 int physical_slot=0;
2110 device = func->device;
2111 func = cpqhp_slot_find(ctrl->bus, device, index++);
2112 p_slot = cpqhp_find_slot(ctrl, device);
2114 physical_slot = p_slot->number;
2117 // Make sure there are no video controllers here
2118 while (func && !rc) {
2119 // Check the Class Code
2120 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
2124 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2125 /* Display/Video adapter (not supported) */
2126 rc = REMOVE_NOT_SUPPORTED;
2128 // See if it's a bridge
2129 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_HEADER_TYPE, &header_type);
2133 // If it's a bridge, check the VGA Enable bit
2134 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2135 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_BRIDGE_CONTROL, &BCR);
2139 // If the VGA Enable bit is set, remove isn't supported
2140 if (BCR & PCI_BRIDGE_CTL_VGA) {
2141 rc = REMOVE_NOT_SUPPORTED;
2146 func = cpqhp_slot_find(ctrl->bus, device, index++);
2149 func = cpqhp_slot_find(ctrl->bus, device, 0);
2150 if ((func != NULL) && !rc) {
2151 //FIXME: Replace flag should be passed into process_SS
2152 replace_flag = !(ctrl->add_support);
2153 rc = remove_board(func, replace_flag, ctrl);
2159 update_slot_info(ctrl, p_slot);
2167 * hardware_test - runs hardware tests
2169 * For hot plug ctrl folks to play with.
2170 * test_num is the number entered in the GUI
2173 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2180 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2186 // Do that funky LED thing
2187 save_LED = readl(ctrl->hpc_reg + LED_CONTROL); // so we can restore them later
2188 work_LED = 0x01010101;
2189 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2190 for (loop = 0; loop < num_of_slots; loop++) {
2193 // Wait for SOGO interrupt
2194 wait_for_ctrl_irq (ctrl);
2196 // Get ready for next iteration
2197 work_LED = work_LED << 1;
2198 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2199 long_delay((2*HZ)/10);
2201 for (loop = 0; loop < num_of_slots; loop++) {
2202 work_LED = work_LED >> 1;
2203 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2207 // Wait for SOGO interrupt
2208 wait_for_ctrl_irq (ctrl);
2210 // Get ready for next iteration
2211 long_delay((2*HZ)/10);
2213 for (loop = 0; loop < num_of_slots; loop++) {
2214 work_LED = work_LED << 1;
2215 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2219 // Wait for SOGO interrupt
2220 wait_for_ctrl_irq (ctrl);
2222 // Get ready for next iteration
2223 long_delay((2*HZ)/10);
2225 for (loop = 0; loop < num_of_slots; loop++) {
2226 work_LED = work_LED >> 1;
2227 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2231 // Wait for SOGO interrupt
2232 wait_for_ctrl_irq (ctrl);
2234 // Get ready for next iteration
2235 long_delay((2*HZ)/10);
2238 work_LED = 0x01010000;
2239 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2240 for (loop = 0; loop < num_of_slots; loop++) {
2243 // Wait for SOGO interrupt
2244 wait_for_ctrl_irq (ctrl);
2246 // Get ready for next iteration
2247 work_LED = work_LED << 1;
2248 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2249 long_delay((2*HZ)/10);
2251 for (loop = 0; loop < num_of_slots; loop++) {
2252 work_LED = work_LED >> 1;
2253 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2257 // Wait for SOGO interrupt
2258 wait_for_ctrl_irq (ctrl);
2260 // Get ready for next iteration
2261 long_delay((2*HZ)/10);
2263 work_LED = 0x00000101;
2264 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2265 for (loop = 0; loop < num_of_slots; loop++) {
2266 work_LED = work_LED << 1;
2267 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2271 // Wait for SOGO interrupt
2272 wait_for_ctrl_irq (ctrl);
2274 // Get ready for next iteration
2275 long_delay((2*HZ)/10);
2277 for (loop = 0; loop < num_of_slots; loop++) {
2278 work_LED = work_LED >> 1;
2279 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2283 // Wait for SOGO interrupt
2284 wait_for_ctrl_irq (ctrl);
2286 // Get ready for next iteration
2287 long_delay((2*HZ)/10);
2291 work_LED = 0x01010000;
2292 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2293 for (loop = 0; loop < num_of_slots; loop++) {
2296 // Wait for SOGO interrupt
2297 wait_for_ctrl_irq (ctrl);
2299 // Get ready for next iteration
2300 long_delay((3*HZ)/10);
2301 work_LED = work_LED >> 16;
2302 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2306 // Wait for SOGO interrupt
2307 wait_for_ctrl_irq (ctrl);
2309 // Get ready for next iteration
2310 long_delay((3*HZ)/10);
2311 work_LED = work_LED << 16;
2312 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2313 work_LED = work_LED << 1;
2314 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2317 writel (save_LED, ctrl->hpc_reg + LED_CONTROL); // put it back the way it was
2321 // Wait for SOBS to be unset
2322 wait_for_ctrl_irq (ctrl);
2325 // Do other stuff here!
2336 * configure_new_device - Configures the PCI header information of one board.
2338 * @ctrl: pointer to controller structure
2339 * @func: pointer to function structure
2340 * @behind_bridge: 1 if this is a recursive call, 0 if not
2341 * @resources: pointer to set of resource lists
2343 * Returns 0 if success
2346 static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
2347 u8 behind_bridge, struct resource_lists * resources)
2349 u8 temp_byte, function, max_functions, stop_it;
2352 struct pci_func *new_slot;
2357 dbg("%s\n", __FUNCTION__);
2358 // Check for Multi-function device
2359 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, 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_read_config_dword_nodev (ctrl->pci_ops, func->bus, 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)
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 resource_lists temp_resources;
2473 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_HEADER_TYPE, &temp_byte);
2477 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
2479 dbg("set Primary bus = %d\n", func->bus);
2480 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PRIMARY_BUS, func->bus);
2484 // find range of busses to use
2485 dbg("find ranges of buses to use\n");
2486 bus_node = get_max_resource(&resources->bus_head, 1);
2488 // If we don't have any busses to allocate, we can't continue
2492 // set Secondary bus
2493 temp_byte = bus_node->base;
2494 dbg("set Secondary bus = %d\n", bus_node->base);
2495 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SECONDARY_BUS, temp_byte);
2499 // set subordinate bus
2500 temp_byte = bus_node->base + bus_node->length - 1;
2501 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2502 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SUBORDINATE_BUS, temp_byte);
2506 // set subordinate Latency Timer and base Latency Timer
2508 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SEC_LATENCY_TIMER, temp_byte);
2511 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_LATENCY_TIMER, temp_byte);
2515 // set Cache Line size
2517 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_CACHE_LINE_SIZE, temp_byte);
2521 // Setup the IO, memory, and prefetchable windows
2523 io_node = get_max_resource(&(resources->io_head), 0x1000);
2524 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2525 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2526 dbg("Setup the IO, memory, and prefetchable windows\n");
2528 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2530 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2531 dbg("p_mem_node\n");
2532 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2534 // set up the IRQ info
2535 if (!resources->irqs) {
2536 irqs.barber_pole = 0;
2537 irqs.interrupt[0] = 0;
2538 irqs.interrupt[1] = 0;
2539 irqs.interrupt[2] = 0;
2540 irqs.interrupt[3] = 0;
2543 irqs.barber_pole = resources->irqs->barber_pole;
2544 irqs.interrupt[0] = resources->irqs->interrupt[0];
2545 irqs.interrupt[1] = resources->irqs->interrupt[1];
2546 irqs.interrupt[2] = resources->irqs->interrupt[2];
2547 irqs.interrupt[3] = resources->irqs->interrupt[3];
2548 irqs.valid_INT = resources->irqs->valid_INT;
2551 // set up resource lists that are now aligned on top and bottom
2552 // for anything behind the bridge.
2553 temp_resources.bus_head = bus_node;
2554 temp_resources.io_head = io_node;
2555 temp_resources.mem_head = mem_node;
2556 temp_resources.p_mem_head = p_mem_node;
2557 temp_resources.irqs = &irqs;
2559 // Make copies of the nodes we are going to pass down so that
2560 // if there is a problem,we can just use these to free resources
2561 hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2562 hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2563 hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2564 hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2566 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2568 kfree(hold_bus_node);
2570 kfree(hold_IO_node);
2572 kfree(hold_mem_node);
2573 if (hold_p_mem_node)
2574 kfree(hold_p_mem_node);
2579 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2581 bus_node->base += 1;
2582 bus_node->length -= 1;
2583 bus_node->next = NULL;
2585 // If we have IO resources copy them and fill in the bridge's
2586 // IO range registers
2588 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2589 io_node->next = NULL;
2591 // set IO base and Limit registers
2592 temp_byte = io_node->base >> 8;
2593 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_BASE, temp_byte);
2595 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2596 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_byte);
2598 kfree(hold_IO_node);
2599 hold_IO_node = NULL;
2602 // If we have memory resources copy them and fill in the bridge's
2603 // memory range registers. Otherwise, fill in the range
2604 // registers with values that disable them.
2606 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2607 mem_node->next = NULL;
2609 // set Mem base and Limit registers
2610 temp_word = mem_node->base >> 16;
2611 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
2613 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2614 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
2617 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
2620 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
2622 kfree(hold_mem_node);
2623 hold_mem_node = NULL;
2626 // If we have prefetchable memory resources copy them and
2627 // fill in the bridge's memory range registers. Otherwise,
2628 // fill in the range registers with values that disable them.
2630 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2631 p_mem_node->next = NULL;
2633 // set Pre Mem base and Limit registers
2634 temp_word = p_mem_node->base >> 16;
2635 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
2637 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2638 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
2641 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
2644 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
2646 kfree(hold_p_mem_node);
2647 hold_p_mem_node = NULL;
2650 // Adjust this to compensate for extra adjustment in first loop
2655 // Here we actually find the devices and configure them
2656 for (device = 0; (device <= 0x1F) && !rc; device++) {
2657 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2660 pci_read_config_dword_nodev (ctrl->pci_ops, hold_bus_node->base, device, 0, 0x00, &ID);
2662 if (ID != 0xFFFFFFFF) { // device Present
2663 // Setup slot structure.
2664 new_slot = cpqhp_slot_create(hold_bus_node->base);
2666 if (new_slot == NULL) {
2672 new_slot->bus = hold_bus_node->base;
2673 new_slot->device = device;
2674 new_slot->function = 0;
2675 new_slot->is_a_board = 1;
2676 new_slot->status = 0;
2678 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2679 dbg("configure_new_device rc=0x%x\n",rc);
2680 } // End of IF (device in slot?)
2681 } // End of FOR loop
2684 cpqhp_destroy_resource_list(&temp_resources);
2686 return_resource(&(resources->bus_head), hold_bus_node);
2687 return_resource(&(resources->io_head), hold_IO_node);
2688 return_resource(&(resources->mem_head), hold_mem_node);
2689 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2692 // save the interrupt routing information
2693 if (resources->irqs) {
2694 resources->irqs->interrupt[0] = irqs.interrupt[0];
2695 resources->irqs->interrupt[1] = irqs.interrupt[1];
2696 resources->irqs->interrupt[2] = irqs.interrupt[2];
2697 resources->irqs->interrupt[3] = irqs.interrupt[3];
2698 resources->irqs->valid_INT = irqs.valid_INT;
2699 } else if (!behind_bridge) {
2700 // We need to hook up the interrupts here
2701 for (cloop = 0; cloop < 4; cloop++) {
2702 if (irqs.valid_INT & (0x01 << cloop)) {
2703 rc = cpqhp_set_irq(func->bus, func->device,
2704 0x0A + cloop, irqs.interrupt[cloop]);
2706 cpqhp_destroy_resource_list (&temp_resources);
2708 return_resource(&(resources-> bus_head), hold_bus_node);
2709 return_resource(&(resources-> io_head), hold_IO_node);
2710 return_resource(&(resources-> mem_head), hold_mem_node);
2711 return_resource(&(resources-> p_mem_head), hold_p_mem_node);
2715 } // end of for loop
2717 // Return unused bus resources
2718 // First use the temporary node to store information for the board
2719 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2720 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2722 hold_bus_node->next = func->bus_head;
2723 func->bus_head = hold_bus_node;
2725 temp_byte = temp_resources.bus_head->base - 1;
2727 // set subordinate bus
2728 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SUBORDINATE_BUS, temp_byte);
2730 if (temp_resources.bus_head->length == 0) {
2731 kfree(temp_resources.bus_head);
2732 temp_resources.bus_head = NULL;
2734 return_resource(&(resources->bus_head), temp_resources.bus_head);
2738 // If we have IO space available and there is some left,
2739 // return the unused portion
2740 if (hold_IO_node && temp_resources.io_head) {
2741 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2742 &hold_IO_node, 0x1000);
2744 // Check if we were able to split something off
2746 hold_IO_node->base = io_node->base + io_node->length;
2748 temp_byte = (hold_IO_node->base) >> 8;
2749 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_BASE, temp_byte);
2751 return_resource(&(resources->io_head), io_node);
2754 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2756 // Check if we were able to split something off
2758 // First use the temporary node to store information for the board
2759 hold_IO_node->length = io_node->base - hold_IO_node->base;
2761 // If we used any, add it to the board's list
2762 if (hold_IO_node->length) {
2763 hold_IO_node->next = func->io_head;
2764 func->io_head = hold_IO_node;
2766 temp_byte = (io_node->base - 1) >> 8;
2767 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_byte);
2769 return_resource(&(resources->io_head), io_node);
2771 // it doesn't need any IO
2773 pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_word);
2775 return_resource(&(resources->io_head), io_node);
2776 kfree(hold_IO_node);
2779 // it used most of the range
2780 hold_IO_node->next = func->io_head;
2781 func->io_head = hold_IO_node;
2783 } else if (hold_IO_node) {
2784 // it used the whole range
2785 hold_IO_node->next = func->io_head;
2786 func->io_head = hold_IO_node;
2788 // If we have memory space available and there is some left,
2789 // return the unused portion
2790 if (hold_mem_node && temp_resources.mem_head) {
2791 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2792 &hold_mem_node, 0x100000);
2794 // Check if we were able to split something off
2796 hold_mem_node->base = mem_node->base + mem_node->length;
2798 temp_word = (hold_mem_node->base) >> 16;
2799 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
2801 return_resource(&(resources->mem_head), mem_node);
2804 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2806 // Check if we were able to split something off
2808 // First use the temporary node to store information for the board
2809 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2811 if (hold_mem_node->length) {
2812 hold_mem_node->next = func->mem_head;
2813 func->mem_head = hold_mem_node;
2815 // configure end address
2816 temp_word = (mem_node->base - 1) >> 16;
2817 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
2819 // Return unused resources to the pool
2820 return_resource(&(resources->mem_head), mem_node);
2822 // it doesn't need any Mem
2824 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
2826 return_resource(&(resources->mem_head), mem_node);
2827 kfree(hold_mem_node);
2830 // it used most of the range
2831 hold_mem_node->next = func->mem_head;
2832 func->mem_head = hold_mem_node;
2834 } else if (hold_mem_node) {
2835 // it used the whole range
2836 hold_mem_node->next = func->mem_head;
2837 func->mem_head = hold_mem_node;
2839 // If we have prefetchable memory space available and there is some
2840 // left at the end, return the unused portion
2841 if (hold_p_mem_node && temp_resources.p_mem_head) {
2842 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2843 &hold_p_mem_node, 0x100000);
2845 // Check if we were able to split something off
2847 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2849 temp_word = (hold_p_mem_node->base) >> 16;
2850 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
2852 return_resource(&(resources->p_mem_head), p_mem_node);
2855 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2857 // Check if we were able to split something off
2859 // First use the temporary node to store 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_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, 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_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, 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 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
2895 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_COMMAND, command);
2897 // set Bridge Control Register
2898 command = 0x07; // = PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR | PCI_BRIDGE_CTL_NO_ISA
2899 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_BRIDGE_CONTROL, command);
2900 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2902 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
2904 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2905 // Display (video) adapter (not supported)
2906 return(DEVICE_TYPE_NOT_SUPPORTED);
2908 // Figure out IO and memory needs
2909 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2910 temp_register = 0xFFFFFFFF;
2912 dbg("CND: bus=%d, device=%d, func=%d, offset=%d\n", func->bus, func->device, func->function, cloop);
2913 rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, temp_register);
2915 rc = pci_read_config_dword_nodev (ctrl->pci_ops, func->bus, func->device, func->function, cloop, &temp_register);
2916 dbg("CND: base = 0x%x\n", temp_register);
2918 if (temp_register) { // If this register is implemented
2919 if ((temp_register & 0x03L) == 0x01) {
2922 // set base = amount of IO space
2923 base = temp_register & 0xFFFFFFFC;
2926 dbg("CND: length = 0x%x\n", base);
2927 io_node = get_io_resource(&(resources->io_head), base);
2928 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2929 io_node->base, io_node->length, io_node->next);
2930 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2932 // allocate the resource to the board
2934 base = io_node->base;
2936 io_node->next = func->io_head;
2937 func->io_head = io_node;
2940 } else if ((temp_register & 0x0BL) == 0x08) {
2941 // Map prefetchable memory
2942 base = temp_register & 0xFFFFFFF0;
2945 dbg("CND: length = 0x%x\n", base);
2946 p_mem_node = get_resource(&(resources->p_mem_head), base);
2948 // allocate the resource to the board
2950 base = p_mem_node->base;
2952 p_mem_node->next = func->p_mem_head;
2953 func->p_mem_head = p_mem_node;
2956 } else if ((temp_register & 0x0BL) == 0x00) {
2958 base = temp_register & 0xFFFFFFF0;
2961 dbg("CND: length = 0x%x\n", base);
2962 mem_node = get_resource(&(resources->mem_head), base);
2964 // allocate the resource to the board
2966 base = mem_node->base;
2968 mem_node->next = func->mem_head;
2969 func->mem_head = mem_node;
2972 } else if ((temp_register & 0x0BL) == 0x04) {
2974 base = temp_register & 0xFFFFFFF0;
2977 dbg("CND: length = 0x%x\n", base);
2978 mem_node = get_resource(&(resources->mem_head), base);
2980 // allocate the resource to the board
2982 base = mem_node->base;
2984 mem_node->next = func->mem_head;
2985 func->mem_head = mem_node;
2988 } else if ((temp_register & 0x0BL) == 0x06) {
2989 // Those bits are reserved, we can't handle this
2992 // Requesting space below 1M
2993 return(NOT_ENOUGH_RESOURCES);
2996 rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, base);
2998 // Check for 64-bit base
2999 if ((temp_register & 0x07L) == 0x04) {
3002 // Upper 32 bits of address always zero on today's systems
3003 // FIXME this is probably not true on Alpha and ia64???
3005 rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, base);
3008 } // End of base register loop
3010 // Figure out which interrupt pin this function uses
3011 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_INTERRUPT_PIN, &temp_byte);
3013 // If this function needs an interrupt and we are behind a bridge
3014 // and the pin is tied to something that's alread mapped,
3015 // set this one the same
3016 if (temp_byte && resources->irqs &&
3017 (resources->irqs->valid_INT &
3018 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3019 // We have to share with something already set up
3020 IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
3022 // Program IRQ based on card type
3023 rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
3025 if (class_code == PCI_BASE_CLASS_STORAGE) {
3026 IRQ = cpqhp_disk_irq;
3028 IRQ = cpqhp_nic_irq;
3033 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_INTERRUPT_LINE, IRQ);
3035 if (!behind_bridge) {
3036 rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3040 //TBD - this code may also belong in the other clause of this If statement
3041 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3042 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3047 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_LATENCY_TIMER, temp_byte);
3051 rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_CACHE_LINE_SIZE, temp_byte);
3053 // disable ROM base Address
3055 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_ROM_ADDRESS, temp_dword);
3058 temp_word = 0x0157; // = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
3059 rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_COMMAND, temp_word);
3060 } // End of Not-A-Bridge else
3062 // It's some strange type of PCI adapter (Cardbus?)
3063 return(DEVICE_TYPE_NOT_SUPPORTED);
3066 func->configured = 1;