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)) {
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
93 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
94 ctrl->next_event = (ctrl->next_event + 1) % 10;
95 taskInfo->hp_slot = hp_slot;
99 temp_word = ctrl->ctrl_int_comp >> 16;
100 func->presence_save = (temp_word >> hp_slot) & 0x01;
101 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
103 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
108 func->switch_save = 0;
110 taskInfo->event_type = INT_SWITCH_OPEN;
116 func->switch_save = 0x10;
118 taskInfo->event_type = INT_SWITCH_CLOSE;
127 * cpqhp_find_slot - find the struct slot of given device
128 * @ctrl: scan lots of this controller
129 * @device: the device id to find
131 static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
133 struct slot *slot = ctrl->slot;
135 while (slot && (slot->device != device)) {
143 static u8 handle_presence_change(u16 change, struct controller * ctrl)
149 struct pci_func *func;
150 struct event_info *taskInfo;
159 dbg("cpqsbd: Presence/Notify input change.\n");
160 dbg(" Changed bits are 0x%4.4x\n", change );
162 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
163 if (change & (0x0101 << hp_slot)) {
167 func = cpqhp_slot_find(ctrl->bus,
168 (hp_slot + ctrl->slot_device_offset), 0);
170 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
171 ctrl->next_event = (ctrl->next_event + 1) % 10;
172 taskInfo->hp_slot = hp_slot;
176 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
180 /* If the switch closed, must be a button
181 * If not in button mode, nevermind
183 if (func->switch_save && (ctrl->push_button == 1)) {
184 temp_word = ctrl->ctrl_int_comp >> 16;
185 temp_byte = (temp_word >> hp_slot) & 0x01;
186 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
188 if (temp_byte != func->presence_save) {
190 * button Pressed (doesn't do anything)
192 dbg("hp_slot %d button pressed\n", hp_slot);
193 taskInfo->event_type = INT_BUTTON_PRESS;
196 * button Released - TAKE ACTION!!!!
198 dbg("hp_slot %d button released\n", hp_slot);
199 taskInfo->event_type = INT_BUTTON_RELEASE;
201 /* Cancel if we are still blinking */
202 if ((p_slot->state == BLINKINGON_STATE)
203 || (p_slot->state == BLINKINGOFF_STATE)) {
204 taskInfo->event_type = INT_BUTTON_CANCEL;
205 dbg("hp_slot %d button cancel\n", hp_slot);
206 } else if ((p_slot->state == POWERON_STATE)
207 || (p_slot->state == POWEROFF_STATE)) {
208 /* info(msg_button_ignore, p_slot->number); */
209 taskInfo->event_type = INT_BUTTON_IGNORE;
210 dbg("hp_slot %d button ignore\n", hp_slot);
214 /* Switch is open, assume a presence change
215 * Save the presence state
217 temp_word = ctrl->ctrl_int_comp >> 16;
218 func->presence_save = (temp_word >> hp_slot) & 0x01;
219 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
221 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
222 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
224 taskInfo->event_type = INT_PRESENCE_ON;
227 taskInfo->event_type = INT_PRESENCE_OFF;
237 static u8 handle_power_fault(u8 change, struct controller * ctrl)
241 struct pci_func *func;
242 struct event_info *taskInfo;
251 info("power fault interrupt\n");
253 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
254 if (change & (0x01 << hp_slot)) {
258 func = cpqhp_slot_find(ctrl->bus,
259 (hp_slot + ctrl->slot_device_offset), 0);
261 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
262 ctrl->next_event = (ctrl->next_event + 1) % 10;
263 taskInfo->hp_slot = hp_slot;
267 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
269 * power fault Cleared
273 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
278 taskInfo->event_type = INT_POWER_FAULT;
281 amber_LED_on (ctrl, hp_slot);
282 green_LED_off (ctrl, hp_slot);
285 /* this is a fatal condition, we want
286 * to crash the machine to protect from
287 * data corruption. simulated_NMI
288 * shouldn't ever return */
290 simulated_NMI(hp_slot, ctrl); */
292 /* The following code causes a software
293 * crash just in case simulated_NMI did
296 panic(msg_power_fault); */
298 /* set power fault status for this board */
300 info("power fault bit %x set\n", hp_slot);
311 * sort_by_size - sort nodes on the list by their length, smallest first.
312 * @head: list to sort
314 static int sort_by_size(struct pci_resource **head)
316 struct pci_resource *current_res;
317 struct pci_resource *next_res;
318 int out_of_order = 1;
323 if (!((*head)->next))
326 while (out_of_order) {
329 /* Special case for swapping list head */
330 if (((*head)->next) &&
331 ((*head)->length > (*head)->next->length)) {
334 *head = (*head)->next;
335 current_res->next = (*head)->next;
336 (*head)->next = current_res;
341 while (current_res->next && current_res->next->next) {
342 if (current_res->next->length > current_res->next->next->length) {
344 next_res = current_res->next;
345 current_res->next = current_res->next->next;
346 current_res = current_res->next;
347 next_res->next = current_res->next;
348 current_res->next = next_res;
350 current_res = current_res->next;
352 } /* End of out_of_order loop */
359 * sort_by_max_size - sort nodes on the list by their length, largest first.
360 * @head: list to sort
362 static int sort_by_max_size(struct pci_resource **head)
364 struct pci_resource *current_res;
365 struct pci_resource *next_res;
366 int out_of_order = 1;
371 if (!((*head)->next))
374 while (out_of_order) {
377 /* Special case for swapping list head */
378 if (((*head)->next) &&
379 ((*head)->length < (*head)->next->length)) {
382 *head = (*head)->next;
383 current_res->next = (*head)->next;
384 (*head)->next = current_res;
389 while (current_res->next && current_res->next->next) {
390 if (current_res->next->length < current_res->next->next->length) {
392 next_res = current_res->next;
393 current_res->next = current_res->next->next;
394 current_res = current_res->next;
395 next_res->next = current_res->next;
396 current_res->next = next_res;
398 current_res = current_res->next;
400 } /* End of out_of_order loop */
407 * do_pre_bridge_resource_split - find node of resources that are unused
408 * @head: new list head
409 * @orig_head: original list head
410 * @alignment: max node size (?)
412 static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
413 struct pci_resource **orig_head, u32 alignment)
415 struct pci_resource *prevnode = NULL;
416 struct pci_resource *node;
417 struct pci_resource *split_node;
420 dbg("do_pre_bridge_resource_split\n");
422 if (!(*head) || !(*orig_head))
425 rc = cpqhp_resource_sort_and_combine(head);
430 if ((*head)->base != (*orig_head)->base)
433 if ((*head)->length == (*orig_head)->length)
437 /* If we got here, there the bridge requires some of the resource, but
438 * we may be able to split some off of the front
443 if (node->length & (alignment -1)) {
444 /* this one isn't an aligned length, so we'll make a new entry
447 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
452 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
454 split_node->base = node->base;
455 split_node->length = temp_dword;
457 node->length -= temp_dword;
458 node->base += split_node->length;
460 /* Put it in the list */
462 split_node->next = node;
465 if (node->length < alignment)
473 while (prevnode->next != node)
474 prevnode = prevnode->next;
476 prevnode->next = node->next;
485 * do_bridge_resource_split - find one node of resources that aren't in use
487 * @alignment: max node size (?)
489 static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
491 struct pci_resource *prevnode = NULL;
492 struct pci_resource *node;
496 rc = cpqhp_resource_sort_and_combine(head);
509 if (node->length < alignment)
512 if (node->base & (alignment - 1)) {
513 /* Short circuit if adjusted size is too small */
514 temp_dword = (node->base | (alignment-1)) + 1;
515 if ((node->length - (temp_dword - node->base)) < alignment)
518 node->length -= (temp_dword - node->base);
519 node->base = temp_dword;
522 if (node->length & (alignment - 1))
523 /* There's stuff in use after this node */
534 * get_io_resource - find first node of given size not in ISA aliasing window.
535 * @head: list to search
536 * @size: size of node to find, must be a power of two.
538 * Description: This function sorts the resource list by size and then returns
539 * returns the first node of "size" length that is not in the ISA aliasing
540 * window. If it finds a node larger than "size" it will split it up.
542 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
544 struct pci_resource *prevnode;
545 struct pci_resource *node;
546 struct pci_resource *split_node;
552 if ( cpqhp_resource_sort_and_combine(head) )
555 if ( sort_by_size(head) )
558 for (node = *head; node; node = node->next) {
559 if (node->length < size)
562 if (node->base & (size - 1)) {
563 /* this one isn't base aligned properly
564 * so we'll make a new entry and split it up
566 temp_dword = (node->base | (size-1)) + 1;
568 /* Short circuit if adjusted size is too small */
569 if ((node->length - (temp_dword - node->base)) < size)
572 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
577 split_node->base = node->base;
578 split_node->length = temp_dword - node->base;
579 node->base = temp_dword;
580 node->length -= split_node->length;
582 /* Put it in the list */
583 split_node->next = node->next;
584 node->next = split_node;
585 } /* End of non-aligned base */
587 /* Don't need to check if too small since we already did */
588 if (node->length > size) {
589 /* this one is longer than we need
590 * so we'll make a new entry and split it up
592 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
597 split_node->base = node->base + size;
598 split_node->length = node->length - size;
601 /* Put it in the list */
602 split_node->next = node->next;
603 node->next = split_node;
604 } /* End of too big on top end */
606 /* For IO make sure it's not in the ISA aliasing space */
607 if (node->base & 0x300L)
610 /* If we got here, then it is the right size
611 * Now take it out of the list and break
617 while (prevnode->next != node)
618 prevnode = prevnode->next;
620 prevnode->next = node->next;
631 * get_max_resource - get largest node which has at least the given size.
632 * @head: the list to search the node in
633 * @size: the minimum size of the node to find
635 * Description: Gets the largest node that is at least "size" big from the
636 * list pointed to by head. It aligns the node on top and bottom
637 * to "size" alignment before returning it.
639 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
641 struct pci_resource *max;
642 struct pci_resource *temp;
643 struct pci_resource *split_node;
646 if (cpqhp_resource_sort_and_combine(head))
649 if (sort_by_max_size(head))
652 for (max = *head; max; max = max->next) {
653 /* If not big enough we could probably just bail,
654 * instead we'll continue to the next.
656 if (max->length < size)
659 if (max->base & (size - 1)) {
660 /* this one isn't base aligned properly
661 * so we'll make a new entry and split it up
663 temp_dword = (max->base | (size-1)) + 1;
665 /* Short circuit if adjusted size is too small */
666 if ((max->length - (temp_dword - max->base)) < size)
669 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
674 split_node->base = max->base;
675 split_node->length = temp_dword - max->base;
676 max->base = temp_dword;
677 max->length -= split_node->length;
679 split_node->next = max->next;
680 max->next = split_node;
683 if ((max->base + max->length) & (size - 1)) {
684 /* this one isn't end aligned properly at the top
685 * so we'll make a new entry and split it up
687 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
691 temp_dword = ((max->base + max->length) & ~(size - 1));
692 split_node->base = temp_dword;
693 split_node->length = max->length + max->base
695 max->length -= split_node->length;
697 split_node->next = max->next;
698 max->next = split_node;
701 /* Make sure it didn't shrink too much when we aligned it */
702 if (max->length < size)
705 /* Now take it out of the list */
710 while (temp && temp->next != max) {
714 temp->next = max->next;
726 * get_resource - find resource of given size and split up larger ones.
727 * @head: the list to search for resources
728 * @size: the size limit to use
730 * Description: This function sorts the resource list by size and then
731 * returns the first node of "size" length. If it finds a node
732 * larger than "size" it will split it up.
734 * size must be a power of two.
736 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
738 struct pci_resource *prevnode;
739 struct pci_resource *node;
740 struct pci_resource *split_node;
743 if (cpqhp_resource_sort_and_combine(head))
746 if (sort_by_size(head))
749 for (node = *head; node; node = node->next) {
750 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
751 __func__, size, node, node->base, node->length);
752 if (node->length < size)
755 if (node->base & (size - 1)) {
756 dbg("%s: not aligned\n", __func__);
757 /* this one isn't base aligned properly
758 * so we'll make a new entry and split it up
760 temp_dword = (node->base | (size-1)) + 1;
762 /* Short circuit if adjusted size is too small */
763 if ((node->length - (temp_dword - node->base)) < size)
766 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
771 split_node->base = node->base;
772 split_node->length = temp_dword - node->base;
773 node->base = temp_dword;
774 node->length -= split_node->length;
776 split_node->next = node->next;
777 node->next = split_node;
778 } /* End of non-aligned base */
780 /* Don't need to check if too small since we already did */
781 if (node->length > size) {
782 dbg("%s: too big\n", __func__);
783 /* this one is longer than we need
784 * so we'll make a new entry and split it up
786 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
791 split_node->base = node->base + size;
792 split_node->length = node->length - size;
795 /* Put it in the list */
796 split_node->next = node->next;
797 node->next = split_node;
798 } /* End of too big on top end */
800 dbg("%s: got one!!!\n", __func__);
801 /* If we got here, then it is the right size
802 * Now take it out of the list */
807 while (prevnode->next != node)
808 prevnode = prevnode->next;
810 prevnode->next = node->next;
820 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
821 * @head: the list to sort and clean up
823 * Description: Sorts all of the nodes in the list in ascending order by
824 * their base addresses. Also does garbage collection by
825 * combining adjacent nodes.
827 * Returns %0 if success.
829 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
831 struct pci_resource *node1;
832 struct pci_resource *node2;
833 int out_of_order = 1;
835 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
840 dbg("*head->next = %p\n",(*head)->next);
843 return 0; /* only one item on the list, already sorted! */
845 dbg("*head->base = 0x%x\n",(*head)->base);
846 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
847 while (out_of_order) {
850 /* Special case for swapping list head */
851 if (((*head)->next) &&
852 ((*head)->base > (*head)->next->base)) {
854 (*head) = (*head)->next;
855 node1->next = (*head)->next;
856 (*head)->next = node1;
862 while (node1->next && node1->next->next) {
863 if (node1->next->base > node1->next->next->base) {
866 node1->next = node1->next->next;
868 node2->next = node1->next;
873 } /* End of out_of_order loop */
877 while (node1 && node1->next) {
878 if ((node1->base + node1->length) == node1->next->base) {
881 node1->length += node1->next->length;
883 node1->next = node1->next->next;
893 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
895 struct controller *ctrl = data;
896 u8 schedule_flag = 0;
903 misc = readw(ctrl->hpc_reg + MISC);
905 * Check to see if it was our interrupt
907 if (!(misc & 0x000C)) {
913 * Serial Output interrupt Pending
916 /* Clear the interrupt */
918 writew(misc, ctrl->hpc_reg + MISC);
920 /* Read to clear posted writes */
921 misc = readw(ctrl->hpc_reg + MISC);
923 dbg ("%s - waking up\n", __func__);
924 wake_up_interruptible(&ctrl->queue);
928 /* General-interrupt-input interrupt Pending */
929 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
931 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
933 /* Clear the interrupt */
934 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
936 /* Read it back to clear any posted writes */
937 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
940 /* Clear all interrupts */
941 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
943 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
944 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
945 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
948 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
950 /* Bus reset has completed */
952 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
953 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
954 wake_up_interruptible(&ctrl->queue);
958 wake_up_process(cpqhp_event_thread);
959 dbg("Waking even thread");
966 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
967 * @busnumber: bus where new node is to be located
969 * Returns pointer to the new node or %NULL if unsuccessful.
971 struct pci_func *cpqhp_slot_create(u8 busnumber)
973 struct pci_func *new_slot;
974 struct pci_func *next;
976 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
977 if (new_slot == NULL) {
984 new_slot->next = NULL;
985 new_slot->configured = 1;
987 if (cpqhp_slot_list[busnumber] == NULL) {
988 cpqhp_slot_list[busnumber] = new_slot;
990 next = cpqhp_slot_list[busnumber];
991 while (next->next != NULL)
993 next->next = new_slot;
1000 * slot_remove - Removes a node from the linked list of slots.
1001 * @old_slot: slot to remove
1003 * Returns %0 if successful, !0 otherwise.
1005 static int slot_remove(struct pci_func * old_slot)
1007 struct pci_func *next;
1009 if (old_slot == NULL)
1012 next = cpqhp_slot_list[old_slot->bus];
1018 if (next == old_slot) {
1019 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1020 cpqhp_destroy_board_resources(old_slot);
1025 while ((next->next != old_slot) && (next->next != NULL)) {
1029 if (next->next == old_slot) {
1030 next->next = old_slot->next;
1031 cpqhp_destroy_board_resources(old_slot);
1040 * bridge_slot_remove - Removes a node from the linked list of slots.
1041 * @bridge: bridge to remove
1043 * Returns %0 if successful, !0 otherwise.
1045 static int bridge_slot_remove(struct pci_func *bridge)
1047 u8 subordinateBus, secondaryBus;
1049 struct pci_func *next;
1051 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1052 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1054 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1055 next = cpqhp_slot_list[tempBus];
1057 while (!slot_remove(next)) {
1058 next = cpqhp_slot_list[tempBus];
1062 next = cpqhp_slot_list[bridge->bus];
1067 if (next == bridge) {
1068 cpqhp_slot_list[bridge->bus] = bridge->next;
1072 while ((next->next != bridge) && (next->next != NULL))
1075 if (next->next != bridge)
1077 next->next = bridge->next;
1085 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1087 * @device: device to find
1088 * @index: is %0 for first function found, %1 for the second...
1090 * Returns pointer to the node if successful, %NULL otherwise.
1092 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1095 struct pci_func *func;
1097 func = cpqhp_slot_list[bus];
1099 if ((func == NULL) || ((func->device == device) && (index == 0)))
1102 if (func->device == device)
1105 while (func->next != NULL) {
1108 if (func->device == device)
1119 /* DJZ: I don't think is_bridge will work as is.
1121 static int is_bridge(struct pci_func * func)
1123 /* Check the header type */
1124 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1132 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
1133 * @ctrl: controller to change frequency/mode for.
1134 * @adapter_speed: the speed of the adapter we want to match.
1135 * @hp_slot: the slot number where the adapter is installed.
1137 * Returns %0 if we successfully change frequency and/or mode to match the
1140 static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1144 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1146 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
1148 if (ctrl->speed == adapter_speed)
1151 /* We don't allow freq/mode changes if we find another adapter running
1152 * in another slot on this controller
1154 for(slot = ctrl->slot; slot; slot = slot->next) {
1155 if (slot->device == (hp_slot + ctrl->slot_device_offset))
1157 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
1159 if (slot->hotplug_slot->info->adapter_status == 0)
1161 /* If another adapter is running on the same segment but at a
1162 * lower speed/mode, we allow the new adapter to function at
1163 * this rate if supported
1165 if (ctrl->speed < adapter_speed)
1171 /* If the controller doesn't support freq/mode changes and the
1172 * controller is running at a higher mode, we bail
1174 if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1177 /* But we allow the adapter to run at a lower rate if possible */
1178 if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1181 /* We try to set the max speed supported by both the adapter and
1184 if (ctrl->speed_capability < adapter_speed) {
1185 if (ctrl->speed == ctrl->speed_capability)
1187 adapter_speed = ctrl->speed_capability;
1190 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1191 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
1194 wait_for_ctrl_irq(ctrl);
1196 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1200 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1202 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1204 switch(adapter_speed) {
1205 case(PCI_SPEED_133MHz_PCIX):
1209 case(PCI_SPEED_100MHz_PCIX):
1213 case(PCI_SPEED_66MHz_PCIX):
1217 case(PCI_SPEED_66MHz):
1221 default: /* 33MHz PCI 2.2 */
1227 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
1231 /* Reenable interrupts */
1232 writel(0, ctrl->hpc_reg + INT_MASK);
1234 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1236 /* Restart state machine */
1238 pci_read_config_byte(ctrl->pci_dev, 0x43, ®);
1239 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
1241 /* Only if mode change...*/
1242 if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1243 ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1246 wait_for_ctrl_irq(ctrl);
1249 /* Restore LED/Slot state */
1250 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1251 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
1254 wait_for_ctrl_irq(ctrl);
1256 ctrl->speed = adapter_speed;
1257 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1259 info("Successfully changed frequency/mode for adapter in slot %d\n",
1264 /* the following routines constitute the bulk of the
1265 * hotplug controller logic
1270 * board_replaced - Called after a board has been replaced in the system.
1271 * @func: PCI device/function information
1272 * @ctrl: hotplug controller
1274 * This is only used if we don't have resources for hot add.
1275 * Turns power on for the board.
1276 * Checks to see if board is the same.
1277 * If board is same, reconfigures it.
1278 * If board isn't same, turns it back off.
1280 static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1287 hp_slot = func->device - ctrl->slot_device_offset;
1289 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
1291 * The switch is open.
1293 rc = INTERLOCK_OPEN;
1294 } else if (is_slot_enabled (ctrl, hp_slot)) {
1296 * The board is already on
1298 rc = CARD_FUNCTIONING;
1300 mutex_lock(&ctrl->crit_sect);
1302 /* turn on board without attaching to the bus */
1303 enable_slot_power (ctrl, hp_slot);
1307 /* Wait for SOBS to be unset */
1308 wait_for_ctrl_irq (ctrl);
1310 /* Change bits in slot power register to force another shift out
1311 * NOTE: this is to work around the timer bug */
1312 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1313 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1314 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1318 /* Wait for SOBS to be unset */
1319 wait_for_ctrl_irq (ctrl);
1321 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1322 if (ctrl->speed != adapter_speed)
1323 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1324 rc = WRONG_BUS_FREQUENCY;
1326 /* turn off board without attaching to the bus */
1327 disable_slot_power (ctrl, hp_slot);
1331 /* Wait for SOBS to be unset */
1332 wait_for_ctrl_irq (ctrl);
1334 mutex_unlock(&ctrl->crit_sect);
1339 mutex_lock(&ctrl->crit_sect);
1341 slot_enable (ctrl, hp_slot);
1342 green_LED_blink (ctrl, hp_slot);
1344 amber_LED_off (ctrl, hp_slot);
1348 /* Wait for SOBS to be unset */
1349 wait_for_ctrl_irq (ctrl);
1351 mutex_unlock(&ctrl->crit_sect);
1353 /* Wait for ~1 second because of hot plug spec */
1356 /* Check for a power fault */
1357 if (func->status == 0xFF) {
1358 /* power fault occurred, but it was benign */
1362 rc = cpqhp_valid_replace(ctrl, func);
1365 /* It must be the same board */
1367 rc = cpqhp_configure_board(ctrl, func);
1369 /* If configuration fails, turn it off
1370 * Get slot won't work for devices behind
1371 * bridges, but in this case it will always be
1372 * called for the "base" bus/dev/func of an
1376 mutex_lock(&ctrl->crit_sect);
1378 amber_LED_on (ctrl, hp_slot);
1379 green_LED_off (ctrl, hp_slot);
1380 slot_disable (ctrl, hp_slot);
1384 /* Wait for SOBS to be unset */
1385 wait_for_ctrl_irq (ctrl);
1387 mutex_unlock(&ctrl->crit_sect);
1395 /* Something is wrong
1397 * Get slot won't work for devices behind bridges, but
1398 * in this case it will always be called for the "base"
1399 * bus/dev/func of an adapter.
1402 mutex_lock(&ctrl->crit_sect);
1404 amber_LED_on (ctrl, hp_slot);
1405 green_LED_off (ctrl, hp_slot);
1406 slot_disable (ctrl, hp_slot);
1410 /* Wait for SOBS to be unset */
1411 wait_for_ctrl_irq (ctrl);
1413 mutex_unlock(&ctrl->crit_sect);
1423 * board_added - Called after a board has been added to the system.
1424 * @func: PCI device/function info
1425 * @ctrl: hotplug controller
1427 * Turns power on for the board.
1430 static u32 board_added(struct pci_func *func, struct controller *ctrl)
1436 u32 temp_register = 0xFFFFFFFF;
1438 struct pci_func *new_slot = NULL;
1439 struct slot *p_slot;
1440 struct resource_lists res_lists;
1442 hp_slot = func->device - ctrl->slot_device_offset;
1443 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1444 __func__, func->device, ctrl->slot_device_offset, hp_slot);
1446 mutex_lock(&ctrl->crit_sect);
1448 /* turn on board without attaching to the bus */
1449 enable_slot_power(ctrl, hp_slot);
1453 /* Wait for SOBS to be unset */
1454 wait_for_ctrl_irq (ctrl);
1456 /* Change bits in slot power register to force another shift out
1457 * NOTE: this is to work around the timer bug
1459 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1460 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1461 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1465 /* Wait for SOBS to be unset */
1466 wait_for_ctrl_irq (ctrl);
1468 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1469 if (ctrl->speed != adapter_speed)
1470 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1471 rc = WRONG_BUS_FREQUENCY;
1473 /* turn off board without attaching to the bus */
1474 disable_slot_power (ctrl, hp_slot);
1478 /* Wait for SOBS to be unset */
1479 wait_for_ctrl_irq(ctrl);
1481 mutex_unlock(&ctrl->crit_sect);
1486 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1488 /* turn on board and blink green LED */
1490 dbg("%s: before down\n", __func__);
1491 mutex_lock(&ctrl->crit_sect);
1492 dbg("%s: after down\n", __func__);
1494 dbg("%s: before slot_enable\n", __func__);
1495 slot_enable (ctrl, hp_slot);
1497 dbg("%s: before green_LED_blink\n", __func__);
1498 green_LED_blink (ctrl, hp_slot);
1500 dbg("%s: before amber_LED_blink\n", __func__);
1501 amber_LED_off (ctrl, hp_slot);
1503 dbg("%s: before set_SOGO\n", __func__);
1506 /* Wait for SOBS to be unset */
1507 dbg("%s: before wait_for_ctrl_irq\n", __func__);
1508 wait_for_ctrl_irq (ctrl);
1509 dbg("%s: after wait_for_ctrl_irq\n", __func__);
1511 dbg("%s: before up\n", __func__);
1512 mutex_unlock(&ctrl->crit_sect);
1513 dbg("%s: after up\n", __func__);
1515 /* Wait for ~1 second because of hot plug spec */
1516 dbg("%s: before long_delay\n", __func__);
1518 dbg("%s: after long_delay\n", __func__);
1520 dbg("%s: func status = %x\n", __func__, func->status);
1521 /* Check for a power fault */
1522 if (func->status == 0xFF) {
1523 /* power fault occurred, but it was benign */
1524 temp_register = 0xFFFFFFFF;
1525 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
1529 /* Get vendor/device ID u32 */
1530 ctrl->pci_bus->number = func->bus;
1531 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1532 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1533 dbg("%s: temp_register is %x\n", __func__, temp_register);
1536 /* Something's wrong here */
1537 temp_register = 0xFFFFFFFF;
1538 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
1540 /* Preset return code. It will be changed later if things go okay. */
1541 rc = NO_ADAPTER_PRESENT;
1544 /* All F's is an empty slot or an invalid board */
1545 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1546 res_lists.io_head = ctrl->io_head;
1547 res_lists.mem_head = ctrl->mem_head;
1548 res_lists.p_mem_head = ctrl->p_mem_head;
1549 res_lists.bus_head = ctrl->bus_head;
1550 res_lists.irqs = NULL;
1552 rc = configure_new_device(ctrl, func, 0, &res_lists);
1554 dbg("%s: back from configure_new_device\n", __func__);
1555 ctrl->io_head = res_lists.io_head;
1556 ctrl->mem_head = res_lists.mem_head;
1557 ctrl->p_mem_head = res_lists.p_mem_head;
1558 ctrl->bus_head = res_lists.bus_head;
1560 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1561 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1562 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1563 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1566 mutex_lock(&ctrl->crit_sect);
1568 amber_LED_on (ctrl, hp_slot);
1569 green_LED_off (ctrl, hp_slot);
1570 slot_disable (ctrl, hp_slot);
1574 /* Wait for SOBS to be unset */
1575 wait_for_ctrl_irq (ctrl);
1577 mutex_unlock(&ctrl->crit_sect);
1580 cpqhp_save_slot_config(ctrl, func);
1585 func->switch_save = 0x10;
1586 func->is_a_board = 0x01;
1588 /* next, we will instantiate the linux pci_dev structures (with
1589 * appropriate driver notification, if already present) */
1590 dbg("%s: configure linux pci_dev structure\n", __func__);
1593 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1594 if (new_slot && !new_slot->pci_dev) {
1595 cpqhp_configure_device(ctrl, new_slot);
1599 mutex_lock(&ctrl->crit_sect);
1601 green_LED_on (ctrl, hp_slot);
1605 /* Wait for SOBS to be unset */
1606 wait_for_ctrl_irq (ctrl);
1608 mutex_unlock(&ctrl->crit_sect);
1610 mutex_lock(&ctrl->crit_sect);
1612 amber_LED_on (ctrl, hp_slot);
1613 green_LED_off (ctrl, hp_slot);
1614 slot_disable (ctrl, hp_slot);
1618 /* Wait for SOBS to be unset */
1619 wait_for_ctrl_irq (ctrl);
1621 mutex_unlock(&ctrl->crit_sect);
1630 * remove_board - Turns off slot and LEDs
1631 * @func: PCI device/function info
1632 * @replace_flag: whether replacing or adding a new device
1633 * @ctrl: target controller
1635 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1643 struct resource_lists res_lists;
1644 struct pci_func *temp_func;
1646 if (cpqhp_unconfigure_device(func))
1649 device = func->device;
1651 hp_slot = func->device - ctrl->slot_device_offset;
1652 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
1654 /* When we get here, it is safe to change base address registers.
1655 * We will attempt to save the base address register lengths */
1656 if (replace_flag || !ctrl->add_support)
1657 rc = cpqhp_save_base_addr_length(ctrl, func);
1658 else if (!func->bus_head && !func->mem_head &&
1659 !func->p_mem_head && !func->io_head) {
1660 /* Here we check to see if we've saved any of the board's
1661 * resources already. If so, we'll skip the attempt to
1662 * determine what's being used. */
1664 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1666 if (temp_func->bus_head || temp_func->mem_head
1667 || temp_func->p_mem_head || temp_func->io_head) {
1671 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1675 rc = cpqhp_save_used_resources(ctrl, func);
1677 /* Change status to shutdown */
1678 if (func->is_a_board)
1679 func->status = 0x01;
1680 func->configured = 0;
1682 mutex_lock(&ctrl->crit_sect);
1684 green_LED_off (ctrl, hp_slot);
1685 slot_disable (ctrl, hp_slot);
1689 /* turn off SERR for slot */
1690 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1691 temp_byte &= ~(0x01 << hp_slot);
1692 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1694 /* Wait for SOBS to be unset */
1695 wait_for_ctrl_irq (ctrl);
1697 mutex_unlock(&ctrl->crit_sect);
1699 if (!replace_flag && ctrl->add_support) {
1701 res_lists.io_head = ctrl->io_head;
1702 res_lists.mem_head = ctrl->mem_head;
1703 res_lists.p_mem_head = ctrl->p_mem_head;
1704 res_lists.bus_head = ctrl->bus_head;
1706 cpqhp_return_board_resources(func, &res_lists);
1708 ctrl->io_head = res_lists.io_head;
1709 ctrl->mem_head = res_lists.mem_head;
1710 ctrl->p_mem_head = res_lists.p_mem_head;
1711 ctrl->bus_head = res_lists.bus_head;
1713 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1714 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1715 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1716 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1718 if (is_bridge(func)) {
1719 bridge_slot_remove(func);
1723 func = cpqhp_slot_find(ctrl->bus, device, 0);
1726 /* Setup slot structure with entry for empty slot */
1727 func = cpqhp_slot_create(ctrl->bus);
1732 func->bus = ctrl->bus;
1733 func->device = device;
1735 func->configured = 0;
1736 func->switch_save = 0x10;
1737 func->is_a_board = 0;
1738 func->p_task_event = NULL;
1744 static void pushbutton_helper_thread(unsigned long data)
1746 pushbutton_pending = data;
1747 wake_up_process(cpqhp_event_thread);
1751 /* this is the main worker thread */
1752 static int event_thread(void* data)
1754 struct controller *ctrl;
1757 dbg("!!!!event_thread sleeping\n");
1758 set_current_state(TASK_INTERRUPTIBLE);
1761 if (kthread_should_stop())
1764 if (pushbutton_pending)
1765 cpqhp_pushbutton_thread(pushbutton_pending);
1767 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1768 interrupt_event_handler(ctrl);
1770 dbg("event_thread signals exit\n");
1774 int cpqhp_event_start_thread(void)
1776 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1777 if (IS_ERR(cpqhp_event_thread)) {
1778 err ("Can't start up our event thread\n");
1779 return PTR_ERR(cpqhp_event_thread);
1786 void cpqhp_event_stop_thread(void)
1788 kthread_stop(cpqhp_event_thread);
1792 static int update_slot_info(struct controller *ctrl, struct slot *slot)
1794 struct hotplug_slot_info *info;
1797 info = kmalloc(sizeof(*info), GFP_KERNEL);
1801 info->power_status = get_slot_enabled(ctrl, slot);
1802 info->attention_status = cpq_get_attention_status(ctrl, slot);
1803 info->latch_status = cpq_get_latch_status(ctrl, slot);
1804 info->adapter_status = get_presence_status(ctrl, slot);
1805 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1810 static void interrupt_event_handler(struct controller *ctrl)
1814 struct pci_func *func;
1816 struct slot *p_slot;
1821 for (loop = 0; loop < 10; loop++) {
1822 /* dbg("loop %d\n", loop); */
1823 if (ctrl->event_queue[loop].event_type != 0) {
1824 hp_slot = ctrl->event_queue[loop].hp_slot;
1826 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1830 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1834 dbg("hp_slot %d, func %p, p_slot %p\n",
1835 hp_slot, func, p_slot);
1837 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1838 dbg("button pressed\n");
1839 } else if (ctrl->event_queue[loop].event_type ==
1840 INT_BUTTON_CANCEL) {
1841 dbg("button cancel\n");
1842 del_timer(&p_slot->task_event);
1844 mutex_lock(&ctrl->crit_sect);
1846 if (p_slot->state == BLINKINGOFF_STATE) {
1848 dbg("turn on green LED\n");
1849 green_LED_on (ctrl, hp_slot);
1850 } else if (p_slot->state == BLINKINGON_STATE) {
1852 dbg("turn off green LED\n");
1853 green_LED_off (ctrl, hp_slot);
1856 info(msg_button_cancel, p_slot->number);
1858 p_slot->state = STATIC_STATE;
1860 amber_LED_off (ctrl, hp_slot);
1864 /* Wait for SOBS to be unset */
1865 wait_for_ctrl_irq (ctrl);
1867 mutex_unlock(&ctrl->crit_sect);
1869 /*** button Released (No action on press...) */
1870 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1871 dbg("button release\n");
1873 if (is_slot_enabled (ctrl, hp_slot)) {
1874 dbg("slot is on\n");
1875 p_slot->state = BLINKINGOFF_STATE;
1876 info(msg_button_off, p_slot->number);
1878 dbg("slot is off\n");
1879 p_slot->state = BLINKINGON_STATE;
1880 info(msg_button_on, p_slot->number);
1882 mutex_lock(&ctrl->crit_sect);
1884 dbg("blink green LED and turn off amber\n");
1886 amber_LED_off (ctrl, hp_slot);
1887 green_LED_blink (ctrl, hp_slot);
1891 /* Wait for SOBS to be unset */
1892 wait_for_ctrl_irq (ctrl);
1894 mutex_unlock(&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 - handle pushbutton events
1928 * @slot: target slot (struct)
1930 * Scheduled procedure to handle blocking stuff for the pushbuttons.
1931 * 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;
1948 /* power Down board */
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", __func__);
1956 if (cpqhp_process_SS(ctrl, func) != 0) {
1957 amber_LED_on(ctrl, hp_slot);
1958 green_LED_on(ctrl, hp_slot);
1962 /* Wait for SOBS to be unset */
1963 wait_for_ctrl_irq(ctrl);
1966 p_slot->state = STATIC_STATE;
1968 p_slot->state = POWERON_STATE;
1971 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1972 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1974 dbg("Error! func NULL in %s\n", __func__);
1979 if (cpqhp_process_SI(ctrl, func) != 0) {
1980 amber_LED_on(ctrl, hp_slot);
1981 green_LED_off(ctrl, hp_slot);
1985 /* Wait for SOBS to be unset */
1986 wait_for_ctrl_irq (ctrl);
1990 p_slot->state = STATIC_STATE;
1997 int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
2003 struct slot* p_slot;
2004 int physical_slot = 0;
2008 device = func->device;
2009 hp_slot = device - ctrl->slot_device_offset;
2010 p_slot = cpqhp_find_slot(ctrl, device);
2012 physical_slot = p_slot->number;
2014 /* Check to see if the interlock is closed */
2015 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2017 if (tempdword & (0x01 << hp_slot)) {
2021 if (func->is_a_board) {
2022 rc = board_replaced(func, ctrl);
2027 func = cpqhp_slot_create(ctrl->bus);
2031 func->bus = ctrl->bus;
2032 func->device = device;
2034 func->configured = 0;
2035 func->is_a_board = 1;
2037 /* We have to save the presence info for these slots */
2038 temp_word = ctrl->ctrl_int_comp >> 16;
2039 func->presence_save = (temp_word >> hp_slot) & 0x01;
2040 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2042 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2043 func->switch_save = 0;
2045 func->switch_save = 0x10;
2048 rc = board_added(func, ctrl);
2050 if (is_bridge(func)) {
2051 bridge_slot_remove(func);
2055 /* Setup slot structure with entry for empty slot */
2056 func = cpqhp_slot_create(ctrl->bus);
2061 func->bus = ctrl->bus;
2062 func->device = device;
2064 func->configured = 0;
2065 func->is_a_board = 0;
2067 /* We have to save the presence info for these slots */
2068 temp_word = ctrl->ctrl_int_comp >> 16;
2069 func->presence_save = (temp_word >> hp_slot) & 0x01;
2070 func->presence_save |=
2071 (temp_word >> (hp_slot + 7)) & 0x02;
2073 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2074 func->switch_save = 0;
2076 func->switch_save = 0x10;
2082 dbg("%s: rc = %d\n", __func__, rc);
2086 update_slot_info(ctrl, p_slot);
2092 int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2094 u8 device, class_code, header_type, BCR;
2099 struct slot* p_slot;
2100 struct pci_bus *pci_bus = ctrl->pci_bus;
2101 int physical_slot=0;
2103 device = func->device;
2104 func = cpqhp_slot_find(ctrl->bus, device, index++);
2105 p_slot = cpqhp_find_slot(ctrl, device);
2107 physical_slot = p_slot->number;
2110 /* Make sure there are no video controllers here */
2111 while (func && !rc) {
2112 pci_bus->number = func->bus;
2113 devfn = PCI_DEVFN(func->device, func->function);
2115 /* Check the Class Code */
2116 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2120 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2121 /* Display/Video adapter (not supported) */
2122 rc = REMOVE_NOT_SUPPORTED;
2124 /* See if it's a bridge */
2125 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2129 /* If it's a bridge, check the VGA Enable bit */
2130 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2131 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2135 /* If the VGA Enable bit is set, remove isn't
2137 if (BCR & PCI_BRIDGE_CTL_VGA) {
2138 rc = REMOVE_NOT_SUPPORTED;
2143 func = cpqhp_slot_find(ctrl->bus, device, index++);
2146 func = cpqhp_slot_find(ctrl->bus, device, 0);
2147 if ((func != NULL) && !rc) {
2148 /* FIXME: Replace flag should be passed into process_SS */
2149 replace_flag = !(ctrl->add_support);
2150 rc = remove_board(func, replace_flag, ctrl);
2156 update_slot_info(ctrl, p_slot);
2162 * switch_leds - switch the leds, go from one site to the other.
2163 * @ctrl: controller to use
2164 * @num_of_slots: number of slots to use
2165 * @work_LED: LED control value
2166 * @direction: 1 to start from the left side, 0 to start right.
2168 static void switch_leds(struct controller *ctrl, const int num_of_slots,
2169 u32 *work_LED, const int direction)
2173 for (loop = 0; loop < num_of_slots; loop++) {
2175 *work_LED = *work_LED >> 1;
2177 *work_LED = *work_LED << 1;
2178 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2182 /* Wait for SOGO interrupt */
2183 wait_for_ctrl_irq(ctrl);
2185 /* Get ready for next iteration */
2186 long_delay((2*HZ)/10);
2191 * cpqhp_hardware_test - runs hardware tests
2192 * @ctrl: target controller
2193 * @test_num: the number written to the "test" file in sysfs.
2195 * For hot plug ctrl folks to play with.
2197 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2204 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2208 /* Do stuff here! */
2210 /* Do that funky LED thing */
2211 /* so we can restore them later */
2212 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2213 work_LED = 0x01010101;
2214 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2215 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2216 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2217 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2219 work_LED = 0x01010000;
2220 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2221 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2222 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2223 work_LED = 0x00000101;
2224 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2225 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2226 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2228 work_LED = 0x01010000;
2229 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2230 for (loop = 0; loop < num_of_slots; loop++) {
2233 /* Wait for SOGO interrupt */
2234 wait_for_ctrl_irq (ctrl);
2236 /* Get ready for next iteration */
2237 long_delay((3*HZ)/10);
2238 work_LED = work_LED >> 16;
2239 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2243 /* Wait for SOGO interrupt */
2244 wait_for_ctrl_irq (ctrl);
2246 /* Get ready for next iteration */
2247 long_delay((3*HZ)/10);
2248 work_LED = work_LED << 16;
2249 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2250 work_LED = work_LED << 1;
2251 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2254 /* put it back the way it was */
2255 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2259 /* Wait for SOBS to be unset */
2260 wait_for_ctrl_irq (ctrl);
2263 /* Do other stuff here! */
2274 * configure_new_device - Configures the PCI header information of one board.
2275 * @ctrl: pointer to controller structure
2276 * @func: pointer to function structure
2277 * @behind_bridge: 1 if this is a recursive call, 0 if not
2278 * @resources: pointer to set of resource lists
2280 * Returns 0 if success.
2282 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2283 u8 behind_bridge, struct resource_lists * resources)
2285 u8 temp_byte, function, max_functions, stop_it;
2288 struct pci_func *new_slot;
2293 dbg("%s\n", __func__);
2294 /* Check for Multi-function device */
2295 ctrl->pci_bus->number = func->bus;
2296 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2298 dbg("%s: rc = %d\n", __func__, rc);
2302 if (temp_byte & 0x80) /* Multi-function device */
2310 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2313 dbg("configure_new_function failed %d\n",rc);
2317 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2320 cpqhp_return_board_resources(new_slot, resources);
2330 /* The following loop skips to the next present function
2331 * and creates a board structure */
2333 while ((function < max_functions) && (!stop_it)) {
2334 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2336 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2338 } else { /* There's something there */
2339 /* Setup slot structure. */
2340 new_slot = cpqhp_slot_create(func->bus);
2342 if (new_slot == NULL)
2345 new_slot->bus = func->bus;
2346 new_slot->device = func->device;
2347 new_slot->function = function;
2348 new_slot->is_a_board = 1;
2349 new_slot->status = 0;
2355 } while (function < max_functions);
2356 dbg("returning from configure_new_device\n");
2363 Configuration logic that involves the hotplug data structures and
2369 * configure_new_function - Configures the PCI header information of one device
2370 * @ctrl: pointer to controller structure
2371 * @func: pointer to function structure
2372 * @behind_bridge: 1 if this is a recursive call, 0 if not
2373 * @resources: pointer to set of resource lists
2375 * Calls itself recursively for bridged devices.
2376 * Returns 0 if success.
2378 static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2380 struct resource_lists *resources)
2395 struct pci_resource *mem_node;
2396 struct pci_resource *p_mem_node;
2397 struct pci_resource *io_node;
2398 struct pci_resource *bus_node;
2399 struct pci_resource *hold_mem_node;
2400 struct pci_resource *hold_p_mem_node;
2401 struct pci_resource *hold_IO_node;
2402 struct pci_resource *hold_bus_node;
2403 struct irq_mapping irqs;
2404 struct pci_func *new_slot;
2405 struct pci_bus *pci_bus;
2406 struct resource_lists temp_resources;
2408 pci_bus = ctrl->pci_bus;
2409 pci_bus->number = func->bus;
2410 devfn = PCI_DEVFN(func->device, func->function);
2412 /* Check for Bridge */
2413 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2417 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2418 /* set Primary bus */
2419 dbg("set Primary bus = %d\n", func->bus);
2420 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2424 /* find range of busses to use */
2425 dbg("find ranges of buses to use\n");
2426 bus_node = get_max_resource(&(resources->bus_head), 1);
2428 /* If we don't have any busses to allocate, we can't continue */
2432 /* set Secondary bus */
2433 temp_byte = bus_node->base;
2434 dbg("set Secondary bus = %d\n", bus_node->base);
2435 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2439 /* set subordinate bus */
2440 temp_byte = bus_node->base + bus_node->length - 1;
2441 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2442 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2446 /* set subordinate Latency Timer and base Latency Timer */
2448 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2451 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2455 /* set Cache Line size */
2457 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2461 /* Setup the IO, memory, and prefetchable windows */
2462 io_node = get_max_resource(&(resources->io_head), 0x1000);
2465 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2468 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2471 dbg("Setup the IO, memory, and prefetchable windows\n");
2473 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2474 io_node->length, io_node->next);
2476 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2477 mem_node->length, mem_node->next);
2478 dbg("p_mem_node\n");
2479 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2480 p_mem_node->length, p_mem_node->next);
2482 /* set up the IRQ info */
2483 if (!resources->irqs) {
2484 irqs.barber_pole = 0;
2485 irqs.interrupt[0] = 0;
2486 irqs.interrupt[1] = 0;
2487 irqs.interrupt[2] = 0;
2488 irqs.interrupt[3] = 0;
2491 irqs.barber_pole = resources->irqs->barber_pole;
2492 irqs.interrupt[0] = resources->irqs->interrupt[0];
2493 irqs.interrupt[1] = resources->irqs->interrupt[1];
2494 irqs.interrupt[2] = resources->irqs->interrupt[2];
2495 irqs.interrupt[3] = resources->irqs->interrupt[3];
2496 irqs.valid_INT = resources->irqs->valid_INT;
2499 /* set up resource lists that are now aligned on top and bottom
2500 * for anything behind the bridge. */
2501 temp_resources.bus_head = bus_node;
2502 temp_resources.io_head = io_node;
2503 temp_resources.mem_head = mem_node;
2504 temp_resources.p_mem_head = p_mem_node;
2505 temp_resources.irqs = &irqs;
2507 /* Make copies of the nodes we are going to pass down so that
2508 * if there is a problem,we can just use these to free resources
2510 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2511 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2512 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2513 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2515 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2516 kfree(hold_bus_node);
2517 kfree(hold_IO_node);
2518 kfree(hold_mem_node);
2519 kfree(hold_p_mem_node);
2524 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2526 bus_node->base += 1;
2527 bus_node->length -= 1;
2528 bus_node->next = NULL;
2530 /* If we have IO resources copy them and fill in the bridge's
2531 * IO range registers */
2533 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2534 io_node->next = NULL;
2536 /* set IO base and Limit registers */
2537 temp_byte = io_node->base >> 8;
2538 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2540 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2541 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2543 kfree(hold_IO_node);
2544 hold_IO_node = NULL;
2547 /* If we have memory resources copy them and fill in the
2548 * bridge's memory range registers. Otherwise, fill in the
2549 * range registers with values that disable them. */
2551 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2552 mem_node->next = NULL;
2554 /* set Mem base and Limit registers */
2555 temp_word = mem_node->base >> 16;
2556 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2558 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2559 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2562 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2565 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2567 kfree(hold_mem_node);
2568 hold_mem_node = NULL;
2571 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2572 p_mem_node->next = NULL;
2574 /* set Pre Mem base and Limit registers */
2575 temp_word = p_mem_node->base >> 16;
2576 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2578 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2579 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2581 /* Adjust this to compensate for extra adjustment in first loop
2587 /* Here we actually find the devices and configure them */
2588 for (device = 0; (device <= 0x1F) && !rc; device++) {
2589 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2592 pci_bus->number = hold_bus_node->base;
2593 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2594 pci_bus->number = func->bus;
2596 if (ID != 0xFFFFFFFF) { /* device present */
2597 /* Setup slot structure. */
2598 new_slot = cpqhp_slot_create(hold_bus_node->base);
2600 if (new_slot == NULL) {
2605 new_slot->bus = hold_bus_node->base;
2606 new_slot->device = device;
2607 new_slot->function = 0;
2608 new_slot->is_a_board = 1;
2609 new_slot->status = 0;
2611 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2612 dbg("configure_new_device rc=0x%x\n",rc);
2613 } /* End of IF (device in slot?) */
2614 } /* End of FOR loop */
2618 /* save the interrupt routing information */
2619 if (resources->irqs) {
2620 resources->irqs->interrupt[0] = irqs.interrupt[0];
2621 resources->irqs->interrupt[1] = irqs.interrupt[1];
2622 resources->irqs->interrupt[2] = irqs.interrupt[2];
2623 resources->irqs->interrupt[3] = irqs.interrupt[3];
2624 resources->irqs->valid_INT = irqs.valid_INT;
2625 } else if (!behind_bridge) {
2626 /* We need to hook up the interrupts here */
2627 for (cloop = 0; cloop < 4; cloop++) {
2628 if (irqs.valid_INT & (0x01 << cloop)) {
2629 rc = cpqhp_set_irq(func->bus, func->device,
2630 cloop + 1, irqs.interrupt[cloop]);
2634 } /* end of for loop */
2636 /* Return unused bus resources
2637 * First use the temporary node to store information for
2639 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2640 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2642 hold_bus_node->next = func->bus_head;
2643 func->bus_head = hold_bus_node;
2645 temp_byte = temp_resources.bus_head->base - 1;
2647 /* set subordinate bus */
2648 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2650 if (temp_resources.bus_head->length == 0) {
2651 kfree(temp_resources.bus_head);
2652 temp_resources.bus_head = NULL;
2654 return_resource(&(resources->bus_head), temp_resources.bus_head);
2658 /* If we have IO space available and there is some left,
2659 * return the unused portion */
2660 if (hold_IO_node && temp_resources.io_head) {
2661 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2662 &hold_IO_node, 0x1000);
2664 /* Check if we were able to split something off */
2666 hold_IO_node->base = io_node->base + io_node->length;
2668 temp_byte = (hold_IO_node->base) >> 8;
2669 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2671 return_resource(&(resources->io_head), io_node);
2674 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2676 /* Check if we were able to split something off */
2678 /* First use the temporary node to store
2679 * information for the board */
2680 hold_IO_node->length = io_node->base - hold_IO_node->base;
2682 /* If we used any, add it to the board's list */
2683 if (hold_IO_node->length) {
2684 hold_IO_node->next = func->io_head;
2685 func->io_head = hold_IO_node;
2687 temp_byte = (io_node->base - 1) >> 8;
2688 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2690 return_resource(&(resources->io_head), io_node);
2692 /* it doesn't need any IO */
2694 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2696 return_resource(&(resources->io_head), io_node);
2697 kfree(hold_IO_node);
2700 /* it used most of the range */
2701 hold_IO_node->next = func->io_head;
2702 func->io_head = hold_IO_node;
2704 } else if (hold_IO_node) {
2705 /* it used the whole range */
2706 hold_IO_node->next = func->io_head;
2707 func->io_head = hold_IO_node;
2709 /* If we have memory space available and there is some left,
2710 * return the unused portion */
2711 if (hold_mem_node && temp_resources.mem_head) {
2712 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2713 &hold_mem_node, 0x100000);
2715 /* Check if we were able to split something off */
2717 hold_mem_node->base = mem_node->base + mem_node->length;
2719 temp_word = (hold_mem_node->base) >> 16;
2720 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2722 return_resource(&(resources->mem_head), mem_node);
2725 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2727 /* Check if we were able to split something off */
2729 /* First use the temporary node to store
2730 * information for the board */
2731 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2733 if (hold_mem_node->length) {
2734 hold_mem_node->next = func->mem_head;
2735 func->mem_head = hold_mem_node;
2737 /* configure end address */
2738 temp_word = (mem_node->base - 1) >> 16;
2739 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2741 /* Return unused resources to the pool */
2742 return_resource(&(resources->mem_head), mem_node);
2744 /* it doesn't need any Mem */
2746 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2748 return_resource(&(resources->mem_head), mem_node);
2749 kfree(hold_mem_node);
2752 /* it used most of the range */
2753 hold_mem_node->next = func->mem_head;
2754 func->mem_head = hold_mem_node;
2756 } else if (hold_mem_node) {
2757 /* it used the whole range */
2758 hold_mem_node->next = func->mem_head;
2759 func->mem_head = hold_mem_node;
2761 /* If we have prefetchable memory space available and there
2762 * is some left at the end, return the unused portion */
2763 if (hold_p_mem_node && temp_resources.p_mem_head) {
2764 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2765 &hold_p_mem_node, 0x100000);
2767 /* Check if we were able to split something off */
2769 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2771 temp_word = (hold_p_mem_node->base) >> 16;
2772 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2774 return_resource(&(resources->p_mem_head), p_mem_node);
2777 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2779 /* Check if we were able to split something off */
2781 /* First use the temporary node to store
2782 * information for the board */
2783 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2785 /* If we used any, add it to the board's list */
2786 if (hold_p_mem_node->length) {
2787 hold_p_mem_node->next = func->p_mem_head;
2788 func->p_mem_head = hold_p_mem_node;
2790 temp_word = (p_mem_node->base - 1) >> 16;
2791 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2793 return_resource(&(resources->p_mem_head), p_mem_node);
2795 /* it doesn't need any PMem */
2797 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2799 return_resource(&(resources->p_mem_head), p_mem_node);
2800 kfree(hold_p_mem_node);
2803 /* it used the most of the range */
2804 hold_p_mem_node->next = func->p_mem_head;
2805 func->p_mem_head = hold_p_mem_node;
2807 } else if (hold_p_mem_node) {
2808 /* it used the whole range */
2809 hold_p_mem_node->next = func->p_mem_head;
2810 func->p_mem_head = hold_p_mem_node;
2812 /* We should be configuring an IRQ and the bridge's base address
2813 * registers if it needs them. Although we have never seen such
2817 command = 0x0157; /* = PCI_COMMAND_IO |
2818 * PCI_COMMAND_MEMORY |
2819 * PCI_COMMAND_MASTER |
2820 * PCI_COMMAND_INVALIDATE |
2821 * PCI_COMMAND_PARITY |
2822 * PCI_COMMAND_SERR */
2823 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2825 /* set Bridge Control Register */
2826 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2827 * PCI_BRIDGE_CTL_SERR |
2828 * PCI_BRIDGE_CTL_NO_ISA */
2829 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2830 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2831 /* Standard device */
2832 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2834 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2835 /* Display (video) adapter (not supported) */
2836 return DEVICE_TYPE_NOT_SUPPORTED;
2838 /* Figure out IO and memory needs */
2839 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2840 temp_register = 0xFFFFFFFF;
2842 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2843 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2845 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2846 dbg("CND: base = 0x%x\n", temp_register);
2848 if (temp_register) { /* If this register is implemented */
2849 if ((temp_register & 0x03L) == 0x01) {
2852 /* set base = amount of IO space */
2853 base = temp_register & 0xFFFFFFFC;
2856 dbg("CND: length = 0x%x\n", base);
2857 io_node = get_io_resource(&(resources->io_head), base);
2858 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2859 io_node->base, io_node->length, io_node->next);
2860 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2862 /* allocate the resource to the board */
2864 base = io_node->base;
2866 io_node->next = func->io_head;
2867 func->io_head = io_node;
2870 } else if ((temp_register & 0x0BL) == 0x08) {
2871 /* Map prefetchable memory */
2872 base = temp_register & 0xFFFFFFF0;
2875 dbg("CND: length = 0x%x\n", base);
2876 p_mem_node = get_resource(&(resources->p_mem_head), base);
2878 /* allocate the resource to the board */
2880 base = p_mem_node->base;
2882 p_mem_node->next = func->p_mem_head;
2883 func->p_mem_head = p_mem_node;
2886 } else if ((temp_register & 0x0BL) == 0x00) {
2888 base = temp_register & 0xFFFFFFF0;
2891 dbg("CND: length = 0x%x\n", base);
2892 mem_node = get_resource(&(resources->mem_head), base);
2894 /* allocate the resource to the board */
2896 base = mem_node->base;
2898 mem_node->next = func->mem_head;
2899 func->mem_head = mem_node;
2902 } else if ((temp_register & 0x0BL) == 0x04) {
2904 base = temp_register & 0xFFFFFFF0;
2907 dbg("CND: length = 0x%x\n", base);
2908 mem_node = get_resource(&(resources->mem_head), base);
2910 /* allocate the resource to the board */
2912 base = mem_node->base;
2914 mem_node->next = func->mem_head;
2915 func->mem_head = mem_node;
2918 } else if ((temp_register & 0x0BL) == 0x06) {
2919 /* Those bits are reserved, we can't handle this */
2922 /* Requesting space below 1M */
2923 return NOT_ENOUGH_RESOURCES;
2926 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2928 /* Check for 64-bit base */
2929 if ((temp_register & 0x07L) == 0x04) {
2932 /* Upper 32 bits of address always zero
2933 * on today's systems */
2934 /* FIXME this is probably not true on
2935 * Alpha and ia64??? */
2937 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2940 } /* End of base register loop */
2941 if (cpqhp_legacy_mode) {
2942 /* Figure out which interrupt pin this function uses */
2943 rc = pci_bus_read_config_byte (pci_bus, devfn,
2944 PCI_INTERRUPT_PIN, &temp_byte);
2946 /* If this function needs an interrupt and we are behind
2947 * a bridge and the pin is tied to something that's
2948 * alread mapped, set this one the same */
2949 if (temp_byte && resources->irqs &&
2950 (resources->irqs->valid_INT &
2951 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2952 /* We have to share with something already set up */
2953 IRQ = resources->irqs->interrupt[(temp_byte +
2954 resources->irqs->barber_pole - 1) & 0x03];
2956 /* Program IRQ based on card type */
2957 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2959 if (class_code == PCI_BASE_CLASS_STORAGE) {
2960 IRQ = cpqhp_disk_irq;
2962 IRQ = cpqhp_nic_irq;
2967 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2970 if (!behind_bridge) {
2971 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
2975 /* TBD - this code may also belong in the other clause
2976 * of this If statement */
2977 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2978 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2983 rc = pci_bus_write_config_byte(pci_bus, devfn,
2984 PCI_LATENCY_TIMER, temp_byte);
2986 /* Cache Line size */
2988 rc = pci_bus_write_config_byte(pci_bus, devfn,
2989 PCI_CACHE_LINE_SIZE, temp_byte);
2991 /* disable ROM base Address */
2993 rc = pci_bus_write_config_word(pci_bus, devfn,
2994 PCI_ROM_ADDRESS, temp_dword);
2997 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2998 * PCI_COMMAND_MEMORY |
2999 * PCI_COMMAND_MASTER |
3000 * PCI_COMMAND_INVALIDATE |
3001 * PCI_COMMAND_PARITY |
3002 * PCI_COMMAND_SERR */
3003 rc = pci_bus_write_config_word (pci_bus, devfn,
3004 PCI_COMMAND, temp_word);
3005 } else { /* End of Not-A-Bridge else */
3006 /* It's some strange type of PCI adapter (Cardbus?) */
3007 return DEVICE_TYPE_NOT_SUPPORTED;
3010 func->configured = 1;
3014 cpqhp_destroy_resource_list (&temp_resources);
3016 return_resource(&(resources-> bus_head), hold_bus_node);
3017 return_resource(&(resources-> io_head), hold_IO_node);
3018 return_resource(&(resources-> mem_head), hold_mem_node);
3019 return_resource(&(resources-> p_mem_head), hold_p_mem_node);