2 * Compaq Hot Plug Controller Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <greg@kroah.com>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/workqueue.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/wait.h>
37 #include <linux/smp_lock.h>
38 #include <linux/pci.h>
39 #include <linux/pci_hotplug.h>
40 #include <linux/kthread.h>
43 static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,
44 u8 behind_bridge, struct resource_lists *resources);
45 static int configure_new_function(struct controller* ctrl, struct pci_func *func,
46 u8 behind_bridge, struct resource_lists *resources);
47 static void interrupt_event_handler(struct controller *ctrl);
50 static struct task_struct *cpqhp_event_thread;
51 static unsigned long pushbutton_pending; /* = 0 */
53 /* delay is in jiffies to wait for */
54 static void long_delay(int delay)
57 * XXX(hch): if someone is bored please convert all callers
58 * to call msleep_interruptible directly. They really want
59 * to specify timeouts in natural units and spend a lot of
60 * effort converting them to jiffies..
62 msleep_interruptible(jiffies_to_msecs(delay));
66 /* FIXME: The following line needs to be somewhere else... */
67 #define WRONG_BUS_FREQUENCY 0x07
68 static u8 handle_switch_change(u8 change, struct controller * ctrl)
73 struct pci_func *func;
74 struct event_info *taskInfo;
80 dbg("cpqsbd: Switch interrupt received.\n");
82 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
83 if (change & (0x1L << hp_slot)) {
84 /**********************************
86 **********************************/
87 func = cpqhp_slot_find(ctrl->bus,
88 (hp_slot + ctrl->slot_device_offset), 0);
90 /* this is the structure that tells the worker thread
92 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
93 ctrl->next_event = (ctrl->next_event + 1) % 10;
94 taskInfo->hp_slot = hp_slot;
98 temp_word = ctrl->ctrl_int_comp >> 16;
99 func->presence_save = (temp_word >> hp_slot) & 0x01;
100 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
102 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
103 /**********************************
105 **********************************/
107 func->switch_save = 0;
109 taskInfo->event_type = INT_SWITCH_OPEN;
111 /**********************************
113 **********************************/
115 func->switch_save = 0x10;
117 taskInfo->event_type = INT_SWITCH_CLOSE;
126 * cpqhp_find_slot: find the struct slot of given device
127 * @ctrl: scan lots of this controller
128 * @device: the device id to find
130 static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
132 struct slot *slot = ctrl->slot;
134 while (slot && (slot->device != device)) {
142 static u8 handle_presence_change(u16 change, struct controller * ctrl)
148 struct pci_func *func;
149 struct event_info *taskInfo;
155 /**********************************
157 **********************************/
158 dbg("cpqsbd: Presence/Notify input change.\n");
159 dbg(" Changed bits are 0x%4.4x\n", change );
161 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
162 if (change & (0x0101 << hp_slot)) {
163 /**********************************
165 **********************************/
166 func = cpqhp_slot_find(ctrl->bus,
167 (hp_slot + ctrl->slot_device_offset), 0);
169 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
170 ctrl->next_event = (ctrl->next_event + 1) % 10;
171 taskInfo->hp_slot = hp_slot;
175 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
179 /* If the switch closed, must be a button
180 * If not in button mode, nevermind */
181 if (func->switch_save && (ctrl->push_button == 1)) {
182 temp_word = ctrl->ctrl_int_comp >> 16;
183 temp_byte = (temp_word >> hp_slot) & 0x01;
184 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
186 if (temp_byte != func->presence_save) {
187 /**************************************
188 * button Pressed (doesn't do anything)
189 **************************************/
190 dbg("hp_slot %d button pressed\n", hp_slot);
191 taskInfo->event_type = INT_BUTTON_PRESS;
193 /**********************************
194 * button Released - TAKE ACTION!!!!
195 **********************************/
196 dbg("hp_slot %d button released\n", hp_slot);
197 taskInfo->event_type = INT_BUTTON_RELEASE;
199 /* Cancel if we are still blinking */
200 if ((p_slot->state == BLINKINGON_STATE)
201 || (p_slot->state == BLINKINGOFF_STATE)) {
202 taskInfo->event_type = INT_BUTTON_CANCEL;
203 dbg("hp_slot %d button cancel\n", hp_slot);
204 } else if ((p_slot->state == POWERON_STATE)
205 || (p_slot->state == POWEROFF_STATE)) {
206 /* info(msg_button_ignore, p_slot->number); */
207 taskInfo->event_type = INT_BUTTON_IGNORE;
208 dbg("hp_slot %d button ignore\n", hp_slot);
212 /* Switch is open, assume a presence change
213 * Save the presence state */
214 temp_word = ctrl->ctrl_int_comp >> 16;
215 func->presence_save = (temp_word >> hp_slot) & 0x01;
216 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
218 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
219 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
221 taskInfo->event_type = INT_PRESENCE_ON;
224 taskInfo->event_type = INT_PRESENCE_OFF;
234 static u8 handle_power_fault(u8 change, struct controller * ctrl)
238 struct pci_func *func;
239 struct event_info *taskInfo;
244 /**********************************
246 **********************************/
248 info("power fault interrupt\n");
250 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
251 if (change & (0x01 << hp_slot)) {
252 /**********************************
254 **********************************/
255 func = cpqhp_slot_find(ctrl->bus,
256 (hp_slot + ctrl->slot_device_offset), 0);
258 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
259 ctrl->next_event = (ctrl->next_event + 1) % 10;
260 taskInfo->hp_slot = hp_slot;
264 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
265 /**********************************
266 * power fault Cleared
267 **********************************/
270 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
272 /**********************************
274 **********************************/
275 taskInfo->event_type = INT_POWER_FAULT;
278 amber_LED_on (ctrl, hp_slot);
279 green_LED_off (ctrl, hp_slot);
282 /* this is a fatal condition, we want
283 * to crash the machine to protect from
284 * data corruption. simulated_NMI
285 * shouldn't ever return */
287 simulated_NMI(hp_slot, ctrl); */
289 /* The following code causes a software
290 * crash just in case simulated_NMI did
293 panic(msg_power_fault); */
295 /* set power fault status for this board */
297 info("power fault bit %x set\n", hp_slot);
308 * sort_by_size: sort nodes on the list by their length, smallest first.
309 * @head: list to sort
312 static int sort_by_size(struct pci_resource **head)
314 struct pci_resource *current_res;
315 struct pci_resource *next_res;
316 int out_of_order = 1;
321 if (!((*head)->next))
324 while (out_of_order) {
327 /* Special case for swapping list head */
328 if (((*head)->next) &&
329 ((*head)->length > (*head)->next->length)) {
332 *head = (*head)->next;
333 current_res->next = (*head)->next;
334 (*head)->next = current_res;
339 while (current_res->next && current_res->next->next) {
340 if (current_res->next->length > current_res->next->next->length) {
342 next_res = current_res->next;
343 current_res->next = current_res->next->next;
344 current_res = current_res->next;
345 next_res->next = current_res->next;
346 current_res->next = next_res;
348 current_res = current_res->next;
350 } /* End of out_of_order loop */
357 * sort_by_max_size: sort nodes on the list by their length, largest first.
358 * @head: list to sort
361 static int sort_by_max_size(struct pci_resource **head)
363 struct pci_resource *current_res;
364 struct pci_resource *next_res;
365 int out_of_order = 1;
370 if (!((*head)->next))
373 while (out_of_order) {
376 /* Special case for swapping list head */
377 if (((*head)->next) &&
378 ((*head)->length < (*head)->next->length)) {
381 *head = (*head)->next;
382 current_res->next = (*head)->next;
383 (*head)->next = current_res;
388 while (current_res->next && current_res->next->next) {
389 if (current_res->next->length < current_res->next->next->length) {
391 next_res = current_res->next;
392 current_res->next = current_res->next->next;
393 current_res = current_res->next;
394 next_res->next = current_res->next;
395 current_res->next = next_res;
397 current_res = current_res->next;
399 } /* End of out_of_order loop */
406 * do_pre_bridge_resource_split: find node of resources that are unused
409 static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
410 struct pci_resource **orig_head, u32 alignment)
412 struct pci_resource *prevnode = NULL;
413 struct pci_resource *node;
414 struct pci_resource *split_node;
417 dbg("do_pre_bridge_resource_split\n");
419 if (!(*head) || !(*orig_head))
422 rc = cpqhp_resource_sort_and_combine(head);
427 if ((*head)->base != (*orig_head)->base)
430 if ((*head)->length == (*orig_head)->length)
434 /* If we got here, there the bridge requires some of the resource, but
435 * we may be able to split some off of the front */
439 if (node->length & (alignment -1)) {
440 /* this one isn't an aligned length, so we'll make a new entry
441 * and split it up. */
442 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
447 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
449 split_node->base = node->base;
450 split_node->length = temp_dword;
452 node->length -= temp_dword;
453 node->base += split_node->length;
455 /* Put it in the list */
457 split_node->next = node;
460 if (node->length < alignment)
468 while (prevnode->next != node)
469 prevnode = prevnode->next;
471 prevnode->next = node->next;
480 * do_bridge_resource_split: find one node of resources that aren't in use
483 static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
485 struct pci_resource *prevnode = NULL;
486 struct pci_resource *node;
490 rc = cpqhp_resource_sort_and_combine(head);
503 if (node->length < alignment)
506 if (node->base & (alignment - 1)) {
507 /* Short circuit if adjusted size is too small */
508 temp_dword = (node->base | (alignment-1)) + 1;
509 if ((node->length - (temp_dword - node->base)) < alignment)
512 node->length -= (temp_dword - node->base);
513 node->base = temp_dword;
516 if (node->length & (alignment - 1))
517 /* There's stuff in use after this node */
528 * get_io_resource: find first node of given size not in ISA aliasing window.
529 * @head: list to search
530 * @size: size of node to find, must be a power of two.
532 * Description: this function sorts the resource list by size and then returns
533 * returns the first node of "size" length that is not in the ISA aliasing
534 * window. If it finds a node larger than "size" it will split it up.
537 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
539 struct pci_resource *prevnode;
540 struct pci_resource *node;
541 struct pci_resource *split_node;
547 if ( cpqhp_resource_sort_and_combine(head) )
550 if ( sort_by_size(head) )
553 for (node = *head; node; node = node->next) {
554 if (node->length < size)
557 if (node->base & (size - 1)) {
558 /* this one isn't base aligned properly
559 * so we'll make a new entry and split it up */
560 temp_dword = (node->base | (size-1)) + 1;
562 /* Short circuit if adjusted size is too small */
563 if ((node->length - (temp_dword - node->base)) < size)
566 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
571 split_node->base = node->base;
572 split_node->length = temp_dword - node->base;
573 node->base = temp_dword;
574 node->length -= split_node->length;
576 /* Put it in the list */
577 split_node->next = node->next;
578 node->next = split_node;
579 } /* End of non-aligned base */
581 /* Don't need to check if too small since we already did */
582 if (node->length > size) {
583 /* this one is longer than we need
584 * so we'll make a new entry and split it up */
585 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
590 split_node->base = node->base + size;
591 split_node->length = node->length - size;
594 /* Put it in the list */
595 split_node->next = node->next;
596 node->next = split_node;
597 } /* End of too big on top end */
599 /* For IO make sure it's not in the ISA aliasing space */
600 if (node->base & 0x300L)
603 /* If we got here, then it is the right size
604 * Now take it out of the list and break */
609 while (prevnode->next != node)
610 prevnode = prevnode->next;
612 prevnode->next = node->next;
623 * get_max_resource: get largest node which has at least the given size.
624 * @head: the list to search the node in
625 * @size: the minimum size of the node to find
627 * Description: Gets the largest node that is at least "size" big from the
628 * list pointed to by head. It aligns the node on top and bottom
629 * to "size" alignment before returning it.
631 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
633 struct pci_resource *max;
634 struct pci_resource *temp;
635 struct pci_resource *split_node;
638 if (cpqhp_resource_sort_and_combine(head))
641 if (sort_by_max_size(head))
644 for (max = *head; max; max = max->next) {
645 /* If not big enough we could probably just bail,
646 * instead we'll continue to the next. */
647 if (max->length < size)
650 if (max->base & (size - 1)) {
651 /* this one isn't base aligned properly
652 * so we'll make a new entry and split it up */
653 temp_dword = (max->base | (size-1)) + 1;
655 /* Short circuit if adjusted size is too small */
656 if ((max->length - (temp_dword - max->base)) < size)
659 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
664 split_node->base = max->base;
665 split_node->length = temp_dword - max->base;
666 max->base = temp_dword;
667 max->length -= split_node->length;
669 split_node->next = max->next;
670 max->next = split_node;
673 if ((max->base + max->length) & (size - 1)) {
674 /* this one isn't end aligned properly at the top
675 * so we'll make a new entry and split it up */
676 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
680 temp_dword = ((max->base + max->length) & ~(size - 1));
681 split_node->base = temp_dword;
682 split_node->length = max->length + max->base
684 max->length -= split_node->length;
686 split_node->next = max->next;
687 max->next = split_node;
690 /* Make sure it didn't shrink too much when we aligned it */
691 if (max->length < size)
694 /* Now take it out of the list */
699 while (temp && temp->next != max) {
703 temp->next = max->next;
715 * get_resource: find resource of given size and split up larger ones.
716 * @head: the list to search for resources
717 * @size: the size limit to use
719 * Description: This function sorts the resource list by size and then
720 * returns the first node of "size" length. If it finds a node
721 * larger than "size" it will split it up.
723 * size must be a power of two.
725 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
727 struct pci_resource *prevnode;
728 struct pci_resource *node;
729 struct pci_resource *split_node;
732 if (cpqhp_resource_sort_and_combine(head))
735 if (sort_by_size(head))
738 for (node = *head; node; node = node->next) {
739 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
740 __FUNCTION__, size, node, node->base, node->length);
741 if (node->length < size)
744 if (node->base & (size - 1)) {
745 dbg("%s: not aligned\n", __FUNCTION__);
746 /* this one isn't base aligned properly
747 * so we'll make a new entry and split it up */
748 temp_dword = (node->base | (size-1)) + 1;
750 /* Short circuit if adjusted size is too small */
751 if ((node->length - (temp_dword - node->base)) < size)
754 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
759 split_node->base = node->base;
760 split_node->length = temp_dword - node->base;
761 node->base = temp_dword;
762 node->length -= split_node->length;
764 split_node->next = node->next;
765 node->next = split_node;
766 } /* End of non-aligned base */
768 /* Don't need to check if too small since we already did */
769 if (node->length > size) {
770 dbg("%s: too big\n", __FUNCTION__);
771 /* this one is longer than we need
772 * so we'll make a new entry and split it up */
773 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
778 split_node->base = node->base + size;
779 split_node->length = node->length - size;
782 /* Put it in the list */
783 split_node->next = node->next;
784 node->next = split_node;
785 } /* End of too big on top end */
787 dbg("%s: got one!!!\n", __FUNCTION__);
788 /* If we got here, then it is the right size
789 * Now take it out of the list */
794 while (prevnode->next != node)
795 prevnode = prevnode->next;
797 prevnode->next = node->next;
807 * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
808 * @head: the list to sort and clean up
810 * Description: Sorts all of the nodes in the list in ascending order by
811 * their base addresses. Also does garbage collection by
812 * combining adjacent nodes.
814 * returns 0 if success
816 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
818 struct pci_resource *node1;
819 struct pci_resource *node2;
820 int out_of_order = 1;
822 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
827 dbg("*head->next = %p\n",(*head)->next);
830 return 0; /* only one item on the list, already sorted! */
832 dbg("*head->base = 0x%x\n",(*head)->base);
833 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
834 while (out_of_order) {
837 /* Special case for swapping list head */
838 if (((*head)->next) &&
839 ((*head)->base > (*head)->next->base)) {
841 (*head) = (*head)->next;
842 node1->next = (*head)->next;
843 (*head)->next = node1;
849 while (node1->next && node1->next->next) {
850 if (node1->next->base > node1->next->next->base) {
853 node1->next = node1->next->next;
855 node2->next = node1->next;
860 } /* End of out_of_order loop */
864 while (node1 && node1->next) {
865 if ((node1->base + node1->length) == node1->next->base) {
868 node1->length += node1->next->length;
870 node1->next = node1->next->next;
880 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
882 struct controller *ctrl = data;
883 u8 schedule_flag = 0;
890 misc = readw(ctrl->hpc_reg + MISC);
891 /***************************************
892 * Check to see if it was our interrupt
893 ***************************************/
894 if (!(misc & 0x000C)) {
899 /**********************************
900 * Serial Output interrupt Pending
901 **********************************/
903 /* Clear the interrupt */
905 writew(misc, ctrl->hpc_reg + MISC);
907 /* Read to clear posted writes */
908 misc = readw(ctrl->hpc_reg + MISC);
910 dbg ("%s - waking up\n", __FUNCTION__);
911 wake_up_interruptible(&ctrl->queue);
915 /* General-interrupt-input interrupt Pending */
916 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
918 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
920 /* Clear the interrupt */
921 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
923 /* Read it back to clear any posted writes */
924 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
927 /* Clear all interrupts */
928 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
930 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
931 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
932 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
935 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
937 /* Bus reset has completed */
939 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
940 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
941 wake_up_interruptible(&ctrl->queue);
945 wake_up_process(cpqhp_event_thread);
946 dbg("Waking even thread");
953 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
954 * @busnumber - bus where new node is to be located
956 * Returns pointer to the new node or NULL if unsuccessful
958 struct pci_func *cpqhp_slot_create(u8 busnumber)
960 struct pci_func *new_slot;
961 struct pci_func *next;
963 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
964 if (new_slot == NULL) {
970 new_slot->next = NULL;
971 new_slot->configured = 1;
973 if (cpqhp_slot_list[busnumber] == NULL) {
974 cpqhp_slot_list[busnumber] = new_slot;
976 next = cpqhp_slot_list[busnumber];
977 while (next->next != NULL)
979 next->next = new_slot;
986 * slot_remove - Removes a node from the linked list of slots.
987 * @old_slot: slot to remove
989 * Returns 0 if successful, !0 otherwise.
991 static int slot_remove(struct pci_func * old_slot)
993 struct pci_func *next;
995 if (old_slot == NULL)
998 next = cpqhp_slot_list[old_slot->bus];
1004 if (next == old_slot) {
1005 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1006 cpqhp_destroy_board_resources(old_slot);
1011 while ((next->next != old_slot) && (next->next != NULL)) {
1015 if (next->next == old_slot) {
1016 next->next = old_slot->next;
1017 cpqhp_destroy_board_resources(old_slot);
1026 * bridge_slot_remove - Removes a node from the linked list of slots.
1027 * @bridge: bridge to remove
1029 * Returns 0 if successful, !0 otherwise.
1031 static int bridge_slot_remove(struct pci_func *bridge)
1033 u8 subordinateBus, secondaryBus;
1035 struct pci_func *next;
1037 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1038 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1040 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1041 next = cpqhp_slot_list[tempBus];
1043 while (!slot_remove(next)) {
1044 next = cpqhp_slot_list[tempBus];
1048 next = cpqhp_slot_list[bridge->bus];
1053 if (next == bridge) {
1054 cpqhp_slot_list[bridge->bus] = bridge->next;
1058 while ((next->next != bridge) && (next->next != NULL))
1061 if (next->next != bridge)
1063 next->next = bridge->next;
1071 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1073 * @device: device to find
1074 * @index: is 0 for first function found, 1 for the second...
1076 * Returns pointer to the node if successful, %NULL otherwise.
1078 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1081 struct pci_func *func;
1083 func = cpqhp_slot_list[bus];
1085 if ((func == NULL) || ((func->device == device) && (index == 0)))
1088 if (func->device == device)
1091 while (func->next != NULL) {
1094 if (func->device == device)
1105 /* DJZ: I don't think is_bridge will work as is.
1107 static int is_bridge(struct pci_func * func)
1109 /* Check the header type */
1110 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1118 * set_controller_speed - set the frequency and/or mode of a specific
1119 * controller segment.
1121 * @ctrl: controller to change frequency/mode for.
1122 * @adapter_speed: the speed of the adapter we want to match.
1123 * @hp_slot: the slot number where the adapter is installed.
1125 * Returns 0 if we successfully change frequency and/or mode to match the
1129 static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1133 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1135 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
1137 if (ctrl->speed == adapter_speed)
1140 /* We don't allow freq/mode changes if we find another adapter running
1141 * in another slot on this controller */
1142 for(slot = ctrl->slot; slot; slot = slot->next) {
1143 if (slot->device == (hp_slot + ctrl->slot_device_offset))
1145 if (!slot->hotplug_slot && !slot->hotplug_slot->info)
1147 if (slot->hotplug_slot->info->adapter_status == 0)
1149 /* If another adapter is running on the same segment but at a
1150 * lower speed/mode, we allow the new adapter to function at
1151 * this rate if supported */
1152 if (ctrl->speed < adapter_speed)
1158 /* If the controller doesn't support freq/mode changes and the
1159 * controller is running at a higher mode, we bail */
1160 if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1163 /* But we allow the adapter to run at a lower rate if possible */
1164 if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1167 /* We try to set the max speed supported by both the adapter and
1169 if (ctrl->speed_capability < adapter_speed) {
1170 if (ctrl->speed == ctrl->speed_capability)
1172 adapter_speed = ctrl->speed_capability;
1175 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1176 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
1179 wait_for_ctrl_irq(ctrl);
1181 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1185 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1187 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1189 switch(adapter_speed) {
1190 case(PCI_SPEED_133MHz_PCIX):
1194 case(PCI_SPEED_100MHz_PCIX):
1198 case(PCI_SPEED_66MHz_PCIX):
1202 case(PCI_SPEED_66MHz):
1206 default: /* 33MHz PCI 2.2 */
1212 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
1216 /* Reenable interrupts */
1217 writel(0, ctrl->hpc_reg + INT_MASK);
1219 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1221 /* Restart state machine */
1223 pci_read_config_byte(ctrl->pci_dev, 0x43, ®);
1224 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
1226 /* Only if mode change...*/
1227 if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1228 ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1231 wait_for_ctrl_irq(ctrl);
1234 /* Restore LED/Slot state */
1235 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1236 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
1239 wait_for_ctrl_irq(ctrl);
1241 ctrl->speed = adapter_speed;
1242 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1244 info("Successfully changed frequency/mode for adapter in slot %d\n",
1249 /* the following routines constitute the bulk of the
1250 hotplug controller logic
1255 * board_replaced - Called after a board has been replaced in the system.
1257 * This is only used if we don't have resources for hot add
1258 * Turns power on for the board
1259 * Checks to see if board is the same
1260 * If board is same, reconfigures it
1261 * If board isn't same, turns it back off.
1264 static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1271 hp_slot = func->device - ctrl->slot_device_offset;
1273 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
1274 /**********************************
1275 * The switch is open.
1276 **********************************/
1277 rc = INTERLOCK_OPEN;
1278 } else if (is_slot_enabled (ctrl, hp_slot)) {
1279 /**********************************
1280 * The board is already on
1281 **********************************/
1282 rc = CARD_FUNCTIONING;
1284 mutex_lock(&ctrl->crit_sect);
1286 /* turn on board without attaching to the bus */
1287 enable_slot_power (ctrl, hp_slot);
1291 /* Wait for SOBS to be unset */
1292 wait_for_ctrl_irq (ctrl);
1294 /* Change bits in slot power register to force another shift out
1295 * NOTE: this is to work around the timer bug */
1296 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1297 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1298 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1302 /* Wait for SOBS to be unset */
1303 wait_for_ctrl_irq (ctrl);
1305 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1306 if (ctrl->speed != adapter_speed)
1307 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1308 rc = WRONG_BUS_FREQUENCY;
1310 /* turn off board without attaching to the bus */
1311 disable_slot_power (ctrl, hp_slot);
1315 /* Wait for SOBS to be unset */
1316 wait_for_ctrl_irq (ctrl);
1318 mutex_unlock(&ctrl->crit_sect);
1323 mutex_lock(&ctrl->crit_sect);
1325 slot_enable (ctrl, hp_slot);
1326 green_LED_blink (ctrl, hp_slot);
1328 amber_LED_off (ctrl, hp_slot);
1332 /* Wait for SOBS to be unset */
1333 wait_for_ctrl_irq (ctrl);
1335 mutex_unlock(&ctrl->crit_sect);
1337 /* Wait for ~1 second because of hot plug spec */
1340 /* Check for a power fault */
1341 if (func->status == 0xFF) {
1342 /* power fault occurred, but it was benign */
1346 rc = cpqhp_valid_replace(ctrl, func);
1349 /* It must be the same board */
1351 rc = cpqhp_configure_board(ctrl, func);
1353 /* If configuration fails, turn it off
1354 * Get slot won't work for devices behind
1355 * bridges, but in this case it will always be
1356 * called for the "base" bus/dev/func of an
1359 mutex_lock(&ctrl->crit_sect);
1361 amber_LED_on (ctrl, hp_slot);
1362 green_LED_off (ctrl, hp_slot);
1363 slot_disable (ctrl, hp_slot);
1367 /* Wait for SOBS to be unset */
1368 wait_for_ctrl_irq (ctrl);
1370 mutex_unlock(&ctrl->crit_sect);
1378 /* Something is wrong
1380 * Get slot won't work for devices behind bridges, but
1381 * in this case it will always be called for the "base"
1382 * bus/dev/func of an adapter. */
1384 mutex_lock(&ctrl->crit_sect);
1386 amber_LED_on (ctrl, hp_slot);
1387 green_LED_off (ctrl, hp_slot);
1388 slot_disable (ctrl, hp_slot);
1392 /* Wait for SOBS to be unset */
1393 wait_for_ctrl_irq (ctrl);
1395 mutex_unlock(&ctrl->crit_sect);
1405 * board_added - Called after a board has been added to the system.
1407 * Turns power on for the board
1411 static u32 board_added(struct pci_func *func, struct controller *ctrl)
1417 u32 temp_register = 0xFFFFFFFF;
1419 struct pci_func *new_slot = NULL;
1420 struct slot *p_slot;
1421 struct resource_lists res_lists;
1423 hp_slot = func->device - ctrl->slot_device_offset;
1424 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1425 __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1427 mutex_lock(&ctrl->crit_sect);
1429 /* turn on board without attaching to the bus */
1430 enable_slot_power(ctrl, hp_slot);
1434 /* Wait for SOBS to be unset */
1435 wait_for_ctrl_irq (ctrl);
1437 /* Change bits in slot power register to force another shift out
1438 * NOTE: this is to work around the timer bug */
1439 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1440 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1441 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1445 /* Wait for SOBS to be unset */
1446 wait_for_ctrl_irq (ctrl);
1448 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1449 if (ctrl->speed != adapter_speed)
1450 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1451 rc = WRONG_BUS_FREQUENCY;
1453 /* turn off board without attaching to the bus */
1454 disable_slot_power (ctrl, hp_slot);
1458 /* Wait for SOBS to be unset */
1459 wait_for_ctrl_irq(ctrl);
1461 mutex_unlock(&ctrl->crit_sect);
1466 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1468 /* turn on board and blink green LED */
1470 dbg("%s: before down\n", __FUNCTION__);
1471 mutex_lock(&ctrl->crit_sect);
1472 dbg("%s: after down\n", __FUNCTION__);
1474 dbg("%s: before slot_enable\n", __FUNCTION__);
1475 slot_enable (ctrl, hp_slot);
1477 dbg("%s: before green_LED_blink\n", __FUNCTION__);
1478 green_LED_blink (ctrl, hp_slot);
1480 dbg("%s: before amber_LED_blink\n", __FUNCTION__);
1481 amber_LED_off (ctrl, hp_slot);
1483 dbg("%s: before set_SOGO\n", __FUNCTION__);
1486 /* Wait for SOBS to be unset */
1487 dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
1488 wait_for_ctrl_irq (ctrl);
1489 dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
1491 dbg("%s: before up\n", __FUNCTION__);
1492 mutex_unlock(&ctrl->crit_sect);
1493 dbg("%s: after up\n", __FUNCTION__);
1495 /* Wait for ~1 second because of hot plug spec */
1496 dbg("%s: before long_delay\n", __FUNCTION__);
1498 dbg("%s: after long_delay\n", __FUNCTION__);
1500 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1501 /* Check for a power fault */
1502 if (func->status == 0xFF) {
1503 /* power fault occurred, but it was benign */
1504 temp_register = 0xFFFFFFFF;
1505 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1509 /* Get vendor/device ID u32 */
1510 ctrl->pci_bus->number = func->bus;
1511 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1512 dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
1513 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1516 /* Something's wrong here */
1517 temp_register = 0xFFFFFFFF;
1518 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1520 /* Preset return code. It will be changed later if things go okay. */
1521 rc = NO_ADAPTER_PRESENT;
1524 /* All F's is an empty slot or an invalid board */
1525 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1526 res_lists.io_head = ctrl->io_head;
1527 res_lists.mem_head = ctrl->mem_head;
1528 res_lists.p_mem_head = ctrl->p_mem_head;
1529 res_lists.bus_head = ctrl->bus_head;
1530 res_lists.irqs = NULL;
1532 rc = configure_new_device(ctrl, func, 0, &res_lists);
1534 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1535 ctrl->io_head = res_lists.io_head;
1536 ctrl->mem_head = res_lists.mem_head;
1537 ctrl->p_mem_head = res_lists.p_mem_head;
1538 ctrl->bus_head = res_lists.bus_head;
1540 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1541 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1542 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1543 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1546 mutex_lock(&ctrl->crit_sect);
1548 amber_LED_on (ctrl, hp_slot);
1549 green_LED_off (ctrl, hp_slot);
1550 slot_disable (ctrl, hp_slot);
1554 /* Wait for SOBS to be unset */
1555 wait_for_ctrl_irq (ctrl);
1557 mutex_unlock(&ctrl->crit_sect);
1560 cpqhp_save_slot_config(ctrl, func);
1565 func->switch_save = 0x10;
1566 func->is_a_board = 0x01;
1568 /* next, we will instantiate the linux pci_dev structures (with
1569 * appropriate driver notification, if already present) */
1570 dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
1573 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1574 if (new_slot && !new_slot->pci_dev) {
1575 cpqhp_configure_device(ctrl, new_slot);
1579 mutex_lock(&ctrl->crit_sect);
1581 green_LED_on (ctrl, hp_slot);
1585 /* Wait for SOBS to be unset */
1586 wait_for_ctrl_irq (ctrl);
1588 mutex_unlock(&ctrl->crit_sect);
1590 mutex_lock(&ctrl->crit_sect);
1592 amber_LED_on (ctrl, hp_slot);
1593 green_LED_off (ctrl, hp_slot);
1594 slot_disable (ctrl, hp_slot);
1598 /* Wait for SOBS to be unset */
1599 wait_for_ctrl_irq (ctrl);
1601 mutex_unlock(&ctrl->crit_sect);
1610 * remove_board - Turns off slot and LED's
1613 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1621 struct resource_lists res_lists;
1622 struct pci_func *temp_func;
1624 if (cpqhp_unconfigure_device(func))
1627 device = func->device;
1629 hp_slot = func->device - ctrl->slot_device_offset;
1630 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1632 /* When we get here, it is safe to change base address registers.
1633 * We will attempt to save the base address register lengths */
1634 if (replace_flag || !ctrl->add_support)
1635 rc = cpqhp_save_base_addr_length(ctrl, func);
1636 else if (!func->bus_head && !func->mem_head &&
1637 !func->p_mem_head && !func->io_head) {
1638 /* Here we check to see if we've saved any of the board's
1639 * resources already. If so, we'll skip the attempt to
1640 * determine what's being used. */
1642 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1644 if (temp_func->bus_head || temp_func->mem_head
1645 || temp_func->p_mem_head || temp_func->io_head) {
1649 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1653 rc = cpqhp_save_used_resources(ctrl, func);
1655 /* Change status to shutdown */
1656 if (func->is_a_board)
1657 func->status = 0x01;
1658 func->configured = 0;
1660 mutex_lock(&ctrl->crit_sect);
1662 green_LED_off (ctrl, hp_slot);
1663 slot_disable (ctrl, hp_slot);
1667 /* turn off SERR for slot */
1668 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1669 temp_byte &= ~(0x01 << hp_slot);
1670 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1672 /* Wait for SOBS to be unset */
1673 wait_for_ctrl_irq (ctrl);
1675 mutex_unlock(&ctrl->crit_sect);
1677 if (!replace_flag && ctrl->add_support) {
1679 res_lists.io_head = ctrl->io_head;
1680 res_lists.mem_head = ctrl->mem_head;
1681 res_lists.p_mem_head = ctrl->p_mem_head;
1682 res_lists.bus_head = ctrl->bus_head;
1684 cpqhp_return_board_resources(func, &res_lists);
1686 ctrl->io_head = res_lists.io_head;
1687 ctrl->mem_head = res_lists.mem_head;
1688 ctrl->p_mem_head = res_lists.p_mem_head;
1689 ctrl->bus_head = res_lists.bus_head;
1691 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1692 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1693 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1694 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1696 if (is_bridge(func)) {
1697 bridge_slot_remove(func);
1701 func = cpqhp_slot_find(ctrl->bus, device, 0);
1704 /* Setup slot structure with entry for empty slot */
1705 func = cpqhp_slot_create(ctrl->bus);
1710 func->bus = ctrl->bus;
1711 func->device = device;
1713 func->configured = 0;
1714 func->switch_save = 0x10;
1715 func->is_a_board = 0;
1716 func->p_task_event = NULL;
1722 static void pushbutton_helper_thread(unsigned long data)
1724 pushbutton_pending = data;
1725 wake_up_process(cpqhp_event_thread);
1729 /* this is the main worker thread */
1730 static int event_thread(void* data)
1732 struct controller *ctrl;
1735 dbg("!!!!event_thread sleeping\n");
1736 set_current_state(TASK_INTERRUPTIBLE);
1739 if (kthread_should_stop())
1742 if (pushbutton_pending)
1743 cpqhp_pushbutton_thread(pushbutton_pending);
1745 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1746 interrupt_event_handler(ctrl);
1748 dbg("event_thread signals exit\n");
1752 int cpqhp_event_start_thread(void)
1754 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1755 if (IS_ERR(cpqhp_event_thread)) {
1756 err ("Can't start up our event thread\n");
1757 return PTR_ERR(cpqhp_event_thread);
1764 void cpqhp_event_stop_thread(void)
1766 kthread_stop(cpqhp_event_thread);
1770 static int update_slot_info(struct controller *ctrl, struct slot *slot)
1772 struct hotplug_slot_info *info;
1775 info = kmalloc(sizeof(*info), GFP_KERNEL);
1779 info->power_status = get_slot_enabled(ctrl, slot);
1780 info->attention_status = cpq_get_attention_status(ctrl, slot);
1781 info->latch_status = cpq_get_latch_status(ctrl, slot);
1782 info->adapter_status = get_presence_status(ctrl, slot);
1783 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1788 static void interrupt_event_handler(struct controller *ctrl)
1792 struct pci_func *func;
1794 struct slot *p_slot;
1799 for (loop = 0; loop < 10; loop++) {
1800 /* dbg("loop %d\n", loop); */
1801 if (ctrl->event_queue[loop].event_type != 0) {
1802 hp_slot = ctrl->event_queue[loop].hp_slot;
1804 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1808 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1812 dbg("hp_slot %d, func %p, p_slot %p\n",
1813 hp_slot, func, p_slot);
1815 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1816 dbg("button pressed\n");
1817 } else if (ctrl->event_queue[loop].event_type ==
1818 INT_BUTTON_CANCEL) {
1819 dbg("button cancel\n");
1820 del_timer(&p_slot->task_event);
1822 mutex_lock(&ctrl->crit_sect);
1824 if (p_slot->state == BLINKINGOFF_STATE) {
1826 dbg("turn on green LED\n");
1827 green_LED_on (ctrl, hp_slot);
1828 } else if (p_slot->state == BLINKINGON_STATE) {
1830 dbg("turn off green LED\n");
1831 green_LED_off (ctrl, hp_slot);
1834 info(msg_button_cancel, p_slot->number);
1836 p_slot->state = STATIC_STATE;
1838 amber_LED_off (ctrl, hp_slot);
1842 /* Wait for SOBS to be unset */
1843 wait_for_ctrl_irq (ctrl);
1845 mutex_unlock(&ctrl->crit_sect);
1847 /*** button Released (No action on press...) */
1848 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1849 dbg("button release\n");
1851 if (is_slot_enabled (ctrl, hp_slot)) {
1852 dbg("slot is on\n");
1853 p_slot->state = BLINKINGOFF_STATE;
1854 info(msg_button_off, p_slot->number);
1856 dbg("slot is off\n");
1857 p_slot->state = BLINKINGON_STATE;
1858 info(msg_button_on, p_slot->number);
1860 mutex_lock(&ctrl->crit_sect);
1862 dbg("blink green LED and turn off amber\n");
1864 amber_LED_off (ctrl, hp_slot);
1865 green_LED_blink (ctrl, hp_slot);
1869 /* Wait for SOBS to be unset */
1870 wait_for_ctrl_irq (ctrl);
1872 mutex_unlock(&ctrl->crit_sect);
1873 init_timer(&p_slot->task_event);
1874 p_slot->hp_slot = hp_slot;
1875 p_slot->ctrl = ctrl;
1876 /* p_slot->physical_slot = physical_slot; */
1877 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1878 p_slot->task_event.function = pushbutton_helper_thread;
1879 p_slot->task_event.data = (u32) p_slot;
1881 dbg("add_timer p_slot = %p\n", p_slot);
1882 add_timer(&p_slot->task_event);
1884 /***********POWER FAULT */
1885 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1886 dbg("power fault\n");
1888 /* refresh notification */
1890 update_slot_info(ctrl, p_slot);
1893 ctrl->event_queue[loop].event_type = 0;
1897 } /* End of FOR loop */
1905 * cpqhp_pushbutton_thread
1907 * Scheduled procedure to handle blocking stuff for the pushbuttons
1908 * Handles all pending events and exits.
1911 void cpqhp_pushbutton_thread(unsigned long slot)
1915 struct pci_func *func;
1916 struct slot *p_slot = (struct slot *) slot;
1917 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1919 pushbutton_pending = 0;
1920 hp_slot = p_slot->hp_slot;
1922 device = p_slot->device;
1924 if (is_slot_enabled(ctrl, hp_slot)) {
1925 p_slot->state = POWEROFF_STATE;
1926 /* power Down board */
1927 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1928 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1930 dbg("Error! func NULL in %s\n", __FUNCTION__);
1934 if (func != NULL && ctrl != NULL) {
1935 if (cpqhp_process_SS(ctrl, func) != 0) {
1936 amber_LED_on (ctrl, hp_slot);
1937 green_LED_on (ctrl, hp_slot);
1941 /* Wait for SOBS to be unset */
1942 wait_for_ctrl_irq (ctrl);
1946 p_slot->state = STATIC_STATE;
1948 p_slot->state = POWERON_STATE;
1951 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1952 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1954 dbg("Error! func NULL in %s\n", __FUNCTION__);
1958 if (func != NULL && ctrl != NULL) {
1959 if (cpqhp_process_SI(ctrl, func) != 0) {
1960 amber_LED_on(ctrl, hp_slot);
1961 green_LED_off(ctrl, hp_slot);
1965 /* Wait for SOBS to be unset */
1966 wait_for_ctrl_irq (ctrl);
1970 p_slot->state = STATIC_STATE;
1977 int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1983 struct slot* p_slot;
1984 int physical_slot = 0;
1988 device = func->device;
1989 hp_slot = device - ctrl->slot_device_offset;
1990 p_slot = cpqhp_find_slot(ctrl, device);
1992 physical_slot = p_slot->number;
1994 /* Check to see if the interlock is closed */
1995 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
1997 if (tempdword & (0x01 << hp_slot)) {
2001 if (func->is_a_board) {
2002 rc = board_replaced(func, ctrl);
2007 func = cpqhp_slot_create(ctrl->bus);
2011 func->bus = ctrl->bus;
2012 func->device = device;
2014 func->configured = 0;
2015 func->is_a_board = 1;
2017 /* We have to save the presence info for these slots */
2018 temp_word = ctrl->ctrl_int_comp >> 16;
2019 func->presence_save = (temp_word >> hp_slot) & 0x01;
2020 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2022 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2023 func->switch_save = 0;
2025 func->switch_save = 0x10;
2028 rc = board_added(func, ctrl);
2030 if (is_bridge(func)) {
2031 bridge_slot_remove(func);
2035 /* Setup slot structure with entry for empty slot */
2036 func = cpqhp_slot_create(ctrl->bus);
2041 func->bus = ctrl->bus;
2042 func->device = device;
2044 func->configured = 0;
2045 func->is_a_board = 0;
2047 /* We have to save the presence info for these slots */
2048 temp_word = ctrl->ctrl_int_comp >> 16;
2049 func->presence_save = (temp_word >> hp_slot) & 0x01;
2050 func->presence_save |=
2051 (temp_word >> (hp_slot + 7)) & 0x02;
2053 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2054 func->switch_save = 0;
2056 func->switch_save = 0x10;
2062 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2066 update_slot_info(ctrl, p_slot);
2072 int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2074 u8 device, class_code, header_type, BCR;
2079 struct slot* p_slot;
2080 struct pci_bus *pci_bus = ctrl->pci_bus;
2081 int physical_slot=0;
2083 device = func->device;
2084 func = cpqhp_slot_find(ctrl->bus, device, index++);
2085 p_slot = cpqhp_find_slot(ctrl, device);
2087 physical_slot = p_slot->number;
2090 /* Make sure there are no video controllers here */
2091 while (func && !rc) {
2092 pci_bus->number = func->bus;
2093 devfn = PCI_DEVFN(func->device, func->function);
2095 /* Check the Class Code */
2096 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2100 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2101 /* Display/Video adapter (not supported) */
2102 rc = REMOVE_NOT_SUPPORTED;
2104 /* See if it's a bridge */
2105 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2109 /* If it's a bridge, check the VGA Enable bit */
2110 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2111 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2115 /* If the VGA Enable bit is set, remove isn't
2117 if (BCR & PCI_BRIDGE_CTL_VGA) {
2118 rc = REMOVE_NOT_SUPPORTED;
2123 func = cpqhp_slot_find(ctrl->bus, device, index++);
2126 func = cpqhp_slot_find(ctrl->bus, device, 0);
2127 if ((func != NULL) && !rc) {
2128 /* FIXME: Replace flag should be passed into process_SS */
2129 replace_flag = !(ctrl->add_support);
2130 rc = remove_board(func, replace_flag, ctrl);
2136 update_slot_info(ctrl, p_slot);
2142 * switch_leds: switch the leds, go from one site to the other.
2143 * @ctrl: controller to use
2144 * @num_of_slots: number of slots to use
2145 * @direction: 1 to start from the left side, 0 to start right.
2147 static void switch_leds(struct controller *ctrl, const int num_of_slots,
2148 u32 *work_LED, const int direction)
2152 for (loop = 0; loop < num_of_slots; loop++) {
2154 *work_LED = *work_LED >> 1;
2156 *work_LED = *work_LED << 1;
2157 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2161 /* Wait for SOGO interrupt */
2162 wait_for_ctrl_irq(ctrl);
2164 /* Get ready for next iteration */
2165 long_delay((2*HZ)/10);
2170 * hardware_test - runs hardware tests
2172 * For hot plug ctrl folks to play with.
2173 * test_num is the number written to the "test" file in sysfs
2176 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2183 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2187 /* Do stuff here! */
2189 /* Do that funky LED thing */
2190 /* so we can restore them later */
2191 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2192 work_LED = 0x01010101;
2193 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2194 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2195 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2196 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2198 work_LED = 0x01010000;
2199 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2200 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2201 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2202 work_LED = 0x00000101;
2203 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2204 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2205 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2207 work_LED = 0x01010000;
2208 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2209 for (loop = 0; loop < num_of_slots; loop++) {
2212 /* Wait for SOGO interrupt */
2213 wait_for_ctrl_irq (ctrl);
2215 /* Get ready for next iteration */
2216 long_delay((3*HZ)/10);
2217 work_LED = work_LED >> 16;
2218 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2222 /* Wait for SOGO interrupt */
2223 wait_for_ctrl_irq (ctrl);
2225 /* Get ready for next iteration */
2226 long_delay((3*HZ)/10);
2227 work_LED = work_LED << 16;
2228 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2229 work_LED = work_LED << 1;
2230 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2233 /* put it back the way it was */
2234 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2238 /* Wait for SOBS to be unset */
2239 wait_for_ctrl_irq (ctrl);
2242 /* Do other stuff here! */
2253 * configure_new_device - Configures the PCI header information of one board.
2255 * @ctrl: pointer to controller structure
2256 * @func: pointer to function structure
2257 * @behind_bridge: 1 if this is a recursive call, 0 if not
2258 * @resources: pointer to set of resource lists
2260 * Returns 0 if success
2263 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2264 u8 behind_bridge, struct resource_lists * resources)
2266 u8 temp_byte, function, max_functions, stop_it;
2269 struct pci_func *new_slot;
2274 dbg("%s\n", __FUNCTION__);
2275 /* Check for Multi-function device */
2276 ctrl->pci_bus->number = func->bus;
2277 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2279 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2283 if (temp_byte & 0x80) /* Multi-function device */
2291 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2294 dbg("configure_new_function failed %d\n",rc);
2298 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2301 cpqhp_return_board_resources(new_slot, resources);
2311 /* The following loop skips to the next present function
2312 * and creates a board structure */
2314 while ((function < max_functions) && (!stop_it)) {
2315 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2317 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2319 } else { /* There's something there */
2320 /* Setup slot structure. */
2321 new_slot = cpqhp_slot_create(func->bus);
2323 if (new_slot == NULL)
2326 new_slot->bus = func->bus;
2327 new_slot->device = func->device;
2328 new_slot->function = function;
2329 new_slot->is_a_board = 1;
2330 new_slot->status = 0;
2336 } while (function < max_functions);
2337 dbg("returning from configure_new_device\n");
2344 Configuration logic that involves the hotplug data structures and
2350 * configure_new_function - Configures the PCI header information of one device
2352 * @ctrl: pointer to controller structure
2353 * @func: pointer to function structure
2354 * @behind_bridge: 1 if this is a recursive call, 0 if not
2355 * @resources: pointer to set of resource lists
2357 * Calls itself recursively for bridged devices.
2358 * Returns 0 if success
2361 static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2363 struct resource_lists *resources)
2378 struct pci_resource *mem_node;
2379 struct pci_resource *p_mem_node;
2380 struct pci_resource *io_node;
2381 struct pci_resource *bus_node;
2382 struct pci_resource *hold_mem_node;
2383 struct pci_resource *hold_p_mem_node;
2384 struct pci_resource *hold_IO_node;
2385 struct pci_resource *hold_bus_node;
2386 struct irq_mapping irqs;
2387 struct pci_func *new_slot;
2388 struct pci_bus *pci_bus;
2389 struct resource_lists temp_resources;
2391 pci_bus = ctrl->pci_bus;
2392 pci_bus->number = func->bus;
2393 devfn = PCI_DEVFN(func->device, func->function);
2395 /* Check for Bridge */
2396 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2400 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2401 /* set Primary bus */
2402 dbg("set Primary bus = %d\n", func->bus);
2403 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2407 /* find range of busses to use */
2408 dbg("find ranges of buses to use\n");
2409 bus_node = get_max_resource(&(resources->bus_head), 1);
2411 /* If we don't have any busses to allocate, we can't continue */
2415 /* set Secondary bus */
2416 temp_byte = bus_node->base;
2417 dbg("set Secondary bus = %d\n", bus_node->base);
2418 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2422 /* set subordinate bus */
2423 temp_byte = bus_node->base + bus_node->length - 1;
2424 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2425 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2429 /* set subordinate Latency Timer and base Latency Timer */
2431 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2434 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2438 /* set Cache Line size */
2440 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2444 /* Setup the IO, memory, and prefetchable windows */
2445 io_node = get_max_resource(&(resources->io_head), 0x1000);
2448 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2451 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2454 dbg("Setup the IO, memory, and prefetchable windows\n");
2456 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2457 io_node->length, io_node->next);
2459 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2460 mem_node->length, mem_node->next);
2461 dbg("p_mem_node\n");
2462 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2463 p_mem_node->length, p_mem_node->next);
2465 /* set up the IRQ info */
2466 if (!resources->irqs) {
2467 irqs.barber_pole = 0;
2468 irqs.interrupt[0] = 0;
2469 irqs.interrupt[1] = 0;
2470 irqs.interrupt[2] = 0;
2471 irqs.interrupt[3] = 0;
2474 irqs.barber_pole = resources->irqs->barber_pole;
2475 irqs.interrupt[0] = resources->irqs->interrupt[0];
2476 irqs.interrupt[1] = resources->irqs->interrupt[1];
2477 irqs.interrupt[2] = resources->irqs->interrupt[2];
2478 irqs.interrupt[3] = resources->irqs->interrupt[3];
2479 irqs.valid_INT = resources->irqs->valid_INT;
2482 /* set up resource lists that are now aligned on top and bottom
2483 * for anything behind the bridge. */
2484 temp_resources.bus_head = bus_node;
2485 temp_resources.io_head = io_node;
2486 temp_resources.mem_head = mem_node;
2487 temp_resources.p_mem_head = p_mem_node;
2488 temp_resources.irqs = &irqs;
2490 /* Make copies of the nodes we are going to pass down so that
2491 * if there is a problem,we can just use these to free resources */
2492 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2493 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2494 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2495 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2497 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2498 kfree(hold_bus_node);
2499 kfree(hold_IO_node);
2500 kfree(hold_mem_node);
2501 kfree(hold_p_mem_node);
2506 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2508 bus_node->base += 1;
2509 bus_node->length -= 1;
2510 bus_node->next = NULL;
2512 /* If we have IO resources copy them and fill in the bridge's
2513 * IO range registers */
2515 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2516 io_node->next = NULL;
2518 /* set IO base and Limit registers */
2519 temp_byte = io_node->base >> 8;
2520 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2522 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2523 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2525 kfree(hold_IO_node);
2526 hold_IO_node = NULL;
2529 /* If we have memory resources copy them and fill in the
2530 * bridge's memory range registers. Otherwise, fill in the
2531 * range registers with values that disable them. */
2533 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2534 mem_node->next = NULL;
2536 /* set Mem base and Limit registers */
2537 temp_word = mem_node->base >> 16;
2538 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2540 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2541 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2544 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2547 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2549 kfree(hold_mem_node);
2550 hold_mem_node = NULL;
2553 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2554 p_mem_node->next = NULL;
2556 /* set Pre Mem base and Limit registers */
2557 temp_word = p_mem_node->base >> 16;
2558 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2560 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2561 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2563 /* Adjust this to compensate for extra adjustment in first loop */
2568 /* Here we actually find the devices and configure them */
2569 for (device = 0; (device <= 0x1F) && !rc; device++) {
2570 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2573 pci_bus->number = hold_bus_node->base;
2574 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2575 pci_bus->number = func->bus;
2577 if (ID != 0xFFFFFFFF) { /* device present */
2578 /* Setup slot structure. */
2579 new_slot = cpqhp_slot_create(hold_bus_node->base);
2581 if (new_slot == NULL) {
2586 new_slot->bus = hold_bus_node->base;
2587 new_slot->device = device;
2588 new_slot->function = 0;
2589 new_slot->is_a_board = 1;
2590 new_slot->status = 0;
2592 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2593 dbg("configure_new_device rc=0x%x\n",rc);
2594 } /* End of IF (device in slot?) */
2595 } /* End of FOR loop */
2599 /* save the interrupt routing information */
2600 if (resources->irqs) {
2601 resources->irqs->interrupt[0] = irqs.interrupt[0];
2602 resources->irqs->interrupt[1] = irqs.interrupt[1];
2603 resources->irqs->interrupt[2] = irqs.interrupt[2];
2604 resources->irqs->interrupt[3] = irqs.interrupt[3];
2605 resources->irqs->valid_INT = irqs.valid_INT;
2606 } else if (!behind_bridge) {
2607 /* We need to hook up the interrupts here */
2608 for (cloop = 0; cloop < 4; cloop++) {
2609 if (irqs.valid_INT & (0x01 << cloop)) {
2610 rc = cpqhp_set_irq(func->bus, func->device,
2611 0x0A + cloop, irqs.interrupt[cloop]);
2615 } /* end of for loop */
2617 /* Return unused bus resources
2618 * First use the temporary node to store information for
2620 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2621 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2623 hold_bus_node->next = func->bus_head;
2624 func->bus_head = hold_bus_node;
2626 temp_byte = temp_resources.bus_head->base - 1;
2628 /* set subordinate bus */
2629 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2631 if (temp_resources.bus_head->length == 0) {
2632 kfree(temp_resources.bus_head);
2633 temp_resources.bus_head = NULL;
2635 return_resource(&(resources->bus_head), temp_resources.bus_head);
2639 /* If we have IO space available and there is some left,
2640 * return the unused portion */
2641 if (hold_IO_node && temp_resources.io_head) {
2642 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2643 &hold_IO_node, 0x1000);
2645 /* Check if we were able to split something off */
2647 hold_IO_node->base = io_node->base + io_node->length;
2649 temp_byte = (hold_IO_node->base) >> 8;
2650 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2652 return_resource(&(resources->io_head), io_node);
2655 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2657 /* Check if we were able to split something off */
2659 /* First use the temporary node to store
2660 * information for the board */
2661 hold_IO_node->length = io_node->base - hold_IO_node->base;
2663 /* If we used any, add it to the board's list */
2664 if (hold_IO_node->length) {
2665 hold_IO_node->next = func->io_head;
2666 func->io_head = hold_IO_node;
2668 temp_byte = (io_node->base - 1) >> 8;
2669 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2671 return_resource(&(resources->io_head), io_node);
2673 /* it doesn't need any IO */
2675 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2677 return_resource(&(resources->io_head), io_node);
2678 kfree(hold_IO_node);
2681 /* it used most of the range */
2682 hold_IO_node->next = func->io_head;
2683 func->io_head = hold_IO_node;
2685 } else if (hold_IO_node) {
2686 /* it used the whole range */
2687 hold_IO_node->next = func->io_head;
2688 func->io_head = hold_IO_node;
2690 /* If we have memory space available and there is some left,
2691 * return the unused portion */
2692 if (hold_mem_node && temp_resources.mem_head) {
2693 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2694 &hold_mem_node, 0x100000);
2696 /* Check if we were able to split something off */
2698 hold_mem_node->base = mem_node->base + mem_node->length;
2700 temp_word = (hold_mem_node->base) >> 16;
2701 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2703 return_resource(&(resources->mem_head), mem_node);
2706 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2708 /* Check if we were able to split something off */
2710 /* First use the temporary node to store
2711 * information for the board */
2712 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2714 if (hold_mem_node->length) {
2715 hold_mem_node->next = func->mem_head;
2716 func->mem_head = hold_mem_node;
2718 /* configure end address */
2719 temp_word = (mem_node->base - 1) >> 16;
2720 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2722 /* Return unused resources to the pool */
2723 return_resource(&(resources->mem_head), mem_node);
2725 /* it doesn't need any Mem */
2727 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2729 return_resource(&(resources->mem_head), mem_node);
2730 kfree(hold_mem_node);
2733 /* it used most of the range */
2734 hold_mem_node->next = func->mem_head;
2735 func->mem_head = hold_mem_node;
2737 } else if (hold_mem_node) {
2738 /* it used the whole range */
2739 hold_mem_node->next = func->mem_head;
2740 func->mem_head = hold_mem_node;
2742 /* If we have prefetchable memory space available and there
2743 * is some left at the end, return the unused portion */
2744 if (hold_p_mem_node && temp_resources.p_mem_head) {
2745 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2746 &hold_p_mem_node, 0x100000);
2748 /* Check if we were able to split something off */
2750 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2752 temp_word = (hold_p_mem_node->base) >> 16;
2753 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2755 return_resource(&(resources->p_mem_head), p_mem_node);
2758 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2760 /* Check if we were able to split something off */
2762 /* First use the temporary node to store
2763 * information for the board */
2764 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2766 /* If we used any, add it to the board's list */
2767 if (hold_p_mem_node->length) {
2768 hold_p_mem_node->next = func->p_mem_head;
2769 func->p_mem_head = hold_p_mem_node;
2771 temp_word = (p_mem_node->base - 1) >> 16;
2772 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2774 return_resource(&(resources->p_mem_head), p_mem_node);
2776 /* it doesn't need any PMem */
2778 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2780 return_resource(&(resources->p_mem_head), p_mem_node);
2781 kfree(hold_p_mem_node);
2784 /* it used the most of the range */
2785 hold_p_mem_node->next = func->p_mem_head;
2786 func->p_mem_head = hold_p_mem_node;
2788 } else if (hold_p_mem_node) {
2789 /* it used the whole range */
2790 hold_p_mem_node->next = func->p_mem_head;
2791 func->p_mem_head = hold_p_mem_node;
2793 /* We should be configuring an IRQ and the bridge's base address
2794 * registers if it needs them. Although we have never seen such
2798 command = 0x0157; /* = PCI_COMMAND_IO |
2799 * PCI_COMMAND_MEMORY |
2800 * PCI_COMMAND_MASTER |
2801 * PCI_COMMAND_INVALIDATE |
2802 * PCI_COMMAND_PARITY |
2803 * PCI_COMMAND_SERR */
2804 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2806 /* set Bridge Control Register */
2807 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2808 * PCI_BRIDGE_CTL_SERR |
2809 * PCI_BRIDGE_CTL_NO_ISA */
2810 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2811 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2812 /* Standard device */
2813 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2815 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2816 /* Display (video) adapter (not supported) */
2817 return DEVICE_TYPE_NOT_SUPPORTED;
2819 /* Figure out IO and memory needs */
2820 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2821 temp_register = 0xFFFFFFFF;
2823 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2824 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2826 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2827 dbg("CND: base = 0x%x\n", temp_register);
2829 if (temp_register) { /* If this register is implemented */
2830 if ((temp_register & 0x03L) == 0x01) {
2833 /* set base = amount of IO space */
2834 base = temp_register & 0xFFFFFFFC;
2837 dbg("CND: length = 0x%x\n", base);
2838 io_node = get_io_resource(&(resources->io_head), base);
2839 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2840 io_node->base, io_node->length, io_node->next);
2841 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2843 /* allocate the resource to the board */
2845 base = io_node->base;
2847 io_node->next = func->io_head;
2848 func->io_head = io_node;
2851 } else if ((temp_register & 0x0BL) == 0x08) {
2852 /* Map prefetchable memory */
2853 base = temp_register & 0xFFFFFFF0;
2856 dbg("CND: length = 0x%x\n", base);
2857 p_mem_node = get_resource(&(resources->p_mem_head), base);
2859 /* allocate the resource to the board */
2861 base = p_mem_node->base;
2863 p_mem_node->next = func->p_mem_head;
2864 func->p_mem_head = p_mem_node;
2867 } else if ((temp_register & 0x0BL) == 0x00) {
2869 base = temp_register & 0xFFFFFFF0;
2872 dbg("CND: length = 0x%x\n", base);
2873 mem_node = get_resource(&(resources->mem_head), base);
2875 /* allocate the resource to the board */
2877 base = mem_node->base;
2879 mem_node->next = func->mem_head;
2880 func->mem_head = mem_node;
2883 } else if ((temp_register & 0x0BL) == 0x04) {
2885 base = temp_register & 0xFFFFFFF0;
2888 dbg("CND: length = 0x%x\n", base);
2889 mem_node = get_resource(&(resources->mem_head), base);
2891 /* allocate the resource to the board */
2893 base = mem_node->base;
2895 mem_node->next = func->mem_head;
2896 func->mem_head = mem_node;
2899 } else if ((temp_register & 0x0BL) == 0x06) {
2900 /* Those bits are reserved, we can't handle this */
2903 /* Requesting space below 1M */
2904 return NOT_ENOUGH_RESOURCES;
2907 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2909 /* Check for 64-bit base */
2910 if ((temp_register & 0x07L) == 0x04) {
2913 /* Upper 32 bits of address always zero
2914 * on today's systems */
2915 /* FIXME this is probably not true on
2916 * Alpha and ia64??? */
2918 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2921 } /* End of base register loop */
2922 if (cpqhp_legacy_mode) {
2923 /* Figure out which interrupt pin this function uses */
2924 rc = pci_bus_read_config_byte (pci_bus, devfn,
2925 PCI_INTERRUPT_PIN, &temp_byte);
2927 /* If this function needs an interrupt and we are behind
2928 * a bridge and the pin is tied to something that's
2929 * alread mapped, set this one the same */
2930 if (temp_byte && resources->irqs &&
2931 (resources->irqs->valid_INT &
2932 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2933 /* We have to share with something already set up */
2934 IRQ = resources->irqs->interrupt[(temp_byte +
2935 resources->irqs->barber_pole - 1) & 0x03];
2937 /* Program IRQ based on card type */
2938 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2940 if (class_code == PCI_BASE_CLASS_STORAGE) {
2941 IRQ = cpqhp_disk_irq;
2943 IRQ = cpqhp_nic_irq;
2948 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2951 if (!behind_bridge) {
2952 rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
2956 /* TBD - this code may also belong in the other clause
2957 * of this If statement */
2958 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2959 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2964 rc = pci_bus_write_config_byte(pci_bus, devfn,
2965 PCI_LATENCY_TIMER, temp_byte);
2967 /* Cache Line size */
2969 rc = pci_bus_write_config_byte(pci_bus, devfn,
2970 PCI_CACHE_LINE_SIZE, temp_byte);
2972 /* disable ROM base Address */
2974 rc = pci_bus_write_config_word(pci_bus, devfn,
2975 PCI_ROM_ADDRESS, temp_dword);
2978 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2979 * PCI_COMMAND_MEMORY |
2980 * PCI_COMMAND_MASTER |
2981 * PCI_COMMAND_INVALIDATE |
2982 * PCI_COMMAND_PARITY |
2983 * PCI_COMMAND_SERR */
2984 rc = pci_bus_write_config_word (pci_bus, devfn,
2985 PCI_COMMAND, temp_word);
2986 } else { /* End of Not-A-Bridge else */
2987 /* It's some strange type of PCI adapter (Cardbus?) */
2988 return DEVICE_TYPE_NOT_SUPPORTED;
2991 func->configured = 1;
2995 cpqhp_destroy_resource_list (&temp_resources);
2997 return_resource(&(resources-> bus_head), hold_bus_node);
2998 return_resource(&(resources-> io_head), hold_IO_node);
2999 return_resource(&(resources-> mem_head), hold_mem_node);
3000 return_resource(&(resources-> p_mem_head), hold_p_mem_node);