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))
142 static u8 handle_presence_change(u16 change, struct controller * ctrl)
148 struct pci_func *func;
149 struct event_info *taskInfo;
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)) {
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
182 if (func->switch_save && (ctrl->push_button == 1)) {
183 temp_word = ctrl->ctrl_int_comp >> 16;
184 temp_byte = (temp_word >> hp_slot) & 0x01;
185 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
187 if (temp_byte != func->presence_save) {
189 * button Pressed (doesn't do anything)
191 dbg("hp_slot %d button pressed\n", hp_slot);
192 taskInfo->event_type = INT_BUTTON_PRESS;
195 * button Released - TAKE ACTION!!!!
197 dbg("hp_slot %d button released\n", hp_slot);
198 taskInfo->event_type = INT_BUTTON_RELEASE;
200 /* Cancel if we are still blinking */
201 if ((p_slot->state == BLINKINGON_STATE)
202 || (p_slot->state == BLINKINGOFF_STATE)) {
203 taskInfo->event_type = INT_BUTTON_CANCEL;
204 dbg("hp_slot %d button cancel\n", hp_slot);
205 } else if ((p_slot->state == POWERON_STATE)
206 || (p_slot->state == POWEROFF_STATE)) {
207 /* info(msg_button_ignore, p_slot->number); */
208 taskInfo->event_type = INT_BUTTON_IGNORE;
209 dbg("hp_slot %d button ignore\n", hp_slot);
213 /* Switch is open, assume a presence change
214 * Save the presence state
216 temp_word = ctrl->ctrl_int_comp >> 16;
217 func->presence_save = (temp_word >> hp_slot) & 0x01;
218 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
220 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
221 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
223 taskInfo->event_type = INT_PRESENCE_ON;
226 taskInfo->event_type = INT_PRESENCE_OFF;
236 static u8 handle_power_fault(u8 change, struct controller * ctrl)
240 struct pci_func *func;
241 struct event_info *taskInfo;
250 info("power fault interrupt\n");
252 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
253 if (change & (0x01 << hp_slot)) {
257 func = cpqhp_slot_find(ctrl->bus,
258 (hp_slot + ctrl->slot_device_offset), 0);
260 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
261 ctrl->next_event = (ctrl->next_event + 1) % 10;
262 taskInfo->hp_slot = hp_slot;
266 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
268 * power fault Cleared
272 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
277 taskInfo->event_type = INT_POWER_FAULT;
280 amber_LED_on (ctrl, hp_slot);
281 green_LED_off (ctrl, hp_slot);
284 /* this is a fatal condition, we want
285 * to crash the machine to protect from
286 * data corruption. simulated_NMI
287 * shouldn't ever return */
289 simulated_NMI(hp_slot, ctrl); */
291 /* The following code causes a software
292 * crash just in case simulated_NMI did
295 panic(msg_power_fault); */
297 /* set power fault status for this board */
299 info("power fault bit %x set\n", hp_slot);
310 * sort_by_size - sort nodes on the list by their length, smallest first.
311 * @head: list to sort
313 static int sort_by_size(struct pci_resource **head)
315 struct pci_resource *current_res;
316 struct pci_resource *next_res;
317 int out_of_order = 1;
322 if (!((*head)->next))
325 while (out_of_order) {
328 /* Special case for swapping list head */
329 if (((*head)->next) &&
330 ((*head)->length > (*head)->next->length)) {
333 *head = (*head)->next;
334 current_res->next = (*head)->next;
335 (*head)->next = current_res;
340 while (current_res->next && current_res->next->next) {
341 if (current_res->next->length > current_res->next->next->length) {
343 next_res = current_res->next;
344 current_res->next = current_res->next->next;
345 current_res = current_res->next;
346 next_res->next = current_res->next;
347 current_res->next = next_res;
349 current_res = current_res->next;
351 } /* End of out_of_order loop */
358 * sort_by_max_size - sort nodes on the list by their length, largest first.
359 * @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
407 * @head: new list head
408 * @orig_head: original list head
409 * @alignment: max node size (?)
411 static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
412 struct pci_resource **orig_head, u32 alignment)
414 struct pci_resource *prevnode = NULL;
415 struct pci_resource *node;
416 struct pci_resource *split_node;
419 dbg("do_pre_bridge_resource_split\n");
421 if (!(*head) || !(*orig_head))
424 rc = cpqhp_resource_sort_and_combine(head);
429 if ((*head)->base != (*orig_head)->base)
432 if ((*head)->length == (*orig_head)->length)
436 /* If we got here, there the bridge requires some of the resource, but
437 * we may be able to split some off of the front
442 if (node->length & (alignment -1)) {
443 /* this one isn't an aligned length, so we'll make a new entry
446 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
451 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
453 split_node->base = node->base;
454 split_node->length = temp_dword;
456 node->length -= temp_dword;
457 node->base += split_node->length;
459 /* Put it in the list */
461 split_node->next = node;
464 if (node->length < alignment)
472 while (prevnode->next != node)
473 prevnode = prevnode->next;
475 prevnode->next = node->next;
484 * do_bridge_resource_split - find one node of resources that aren't in use
486 * @alignment: max node size (?)
488 static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
490 struct pci_resource *prevnode = NULL;
491 struct pci_resource *node;
495 rc = cpqhp_resource_sort_and_combine(head);
508 if (node->length < alignment)
511 if (node->base & (alignment - 1)) {
512 /* Short circuit if adjusted size is too small */
513 temp_dword = (node->base | (alignment-1)) + 1;
514 if ((node->length - (temp_dword - node->base)) < alignment)
517 node->length -= (temp_dword - node->base);
518 node->base = temp_dword;
521 if (node->length & (alignment - 1))
522 /* There's stuff in use after this node */
533 * get_io_resource - find first node of given size not in ISA aliasing window.
534 * @head: list to search
535 * @size: size of node to find, must be a power of two.
537 * Description: This function sorts the resource list by size and then returns
538 * returns the first node of "size" length that is not in the ISA aliasing
539 * window. If it finds a node larger than "size" it will split it up.
541 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
543 struct pci_resource *prevnode;
544 struct pci_resource *node;
545 struct pci_resource *split_node;
551 if (cpqhp_resource_sort_and_combine(head))
554 if (sort_by_size(head))
557 for (node = *head; node; node = node->next) {
558 if (node->length < size)
561 if (node->base & (size - 1)) {
562 /* this one isn't base aligned properly
563 * so we'll make a new entry and split it up
565 temp_dword = (node->base | (size-1)) + 1;
567 /* Short circuit if adjusted size is too small */
568 if ((node->length - (temp_dword - node->base)) < size)
571 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
576 split_node->base = node->base;
577 split_node->length = temp_dword - node->base;
578 node->base = temp_dword;
579 node->length -= split_node->length;
581 /* Put it in the list */
582 split_node->next = node->next;
583 node->next = split_node;
584 } /* End of non-aligned base */
586 /* Don't need to check if too small since we already did */
587 if (node->length > size) {
588 /* this one is longer than we need
589 * so we'll make a new entry and split it up
591 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
596 split_node->base = node->base + size;
597 split_node->length = node->length - size;
600 /* Put it in the list */
601 split_node->next = node->next;
602 node->next = split_node;
603 } /* End of too big on top end */
605 /* For IO make sure it's not in the ISA aliasing space */
606 if (node->base & 0x300L)
609 /* If we got here, then it is the right size
610 * Now take it out of the list and break
616 while (prevnode->next != node)
617 prevnode = prevnode->next;
619 prevnode->next = node->next;
630 * get_max_resource - get largest node which has at least the given size.
631 * @head: the list to search the node in
632 * @size: the minimum size of the node to find
634 * Description: Gets the largest node that is at least "size" big from the
635 * list pointed to by head. It aligns the node on top and bottom
636 * to "size" alignment before returning it.
638 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
640 struct pci_resource *max;
641 struct pci_resource *temp;
642 struct pci_resource *split_node;
645 if (cpqhp_resource_sort_and_combine(head))
648 if (sort_by_max_size(head))
651 for (max = *head; max; max = max->next) {
652 /* If not big enough we could probably just bail,
653 * instead we'll continue to the next.
655 if (max->length < size)
658 if (max->base & (size - 1)) {
659 /* this one isn't base aligned properly
660 * so we'll make a new entry and split it up
662 temp_dword = (max->base | (size-1)) + 1;
664 /* Short circuit if adjusted size is too small */
665 if ((max->length - (temp_dword - max->base)) < size)
668 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
673 split_node->base = max->base;
674 split_node->length = temp_dword - max->base;
675 max->base = temp_dword;
676 max->length -= split_node->length;
678 split_node->next = max->next;
679 max->next = split_node;
682 if ((max->base + max->length) & (size - 1)) {
683 /* this one isn't end aligned properly at the top
684 * so we'll make a new entry and split it up
686 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
690 temp_dword = ((max->base + max->length) & ~(size - 1));
691 split_node->base = temp_dword;
692 split_node->length = max->length + max->base
694 max->length -= split_node->length;
696 split_node->next = max->next;
697 max->next = split_node;
700 /* Make sure it didn't shrink too much when we aligned it */
701 if (max->length < size)
704 /* Now take it out of the list */
709 while (temp && temp->next != max) {
713 temp->next = max->next;
725 * get_resource - find resource of given size and split up larger ones.
726 * @head: the list to search for resources
727 * @size: the size limit to use
729 * Description: This function sorts the resource list by size and then
730 * returns the first node of "size" length. If it finds a node
731 * larger than "size" it will split it up.
733 * size must be a power of two.
735 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
737 struct pci_resource *prevnode;
738 struct pci_resource *node;
739 struct pci_resource *split_node;
742 if (cpqhp_resource_sort_and_combine(head))
745 if (sort_by_size(head))
748 for (node = *head; node; node = node->next) {
749 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
750 __func__, size, node, node->base, node->length);
751 if (node->length < size)
754 if (node->base & (size - 1)) {
755 dbg("%s: not aligned\n", __func__);
756 /* this one isn't base aligned properly
757 * so we'll make a new entry and split it up
759 temp_dword = (node->base | (size-1)) + 1;
761 /* Short circuit if adjusted size is too small */
762 if ((node->length - (temp_dword - node->base)) < size)
765 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
770 split_node->base = node->base;
771 split_node->length = temp_dword - node->base;
772 node->base = temp_dword;
773 node->length -= split_node->length;
775 split_node->next = node->next;
776 node->next = split_node;
777 } /* End of non-aligned base */
779 /* Don't need to check if too small since we already did */
780 if (node->length > size) {
781 dbg("%s: too big\n", __func__);
782 /* this one is longer than we need
783 * so we'll make a new entry and split it up
785 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
790 split_node->base = node->base + size;
791 split_node->length = node->length - size;
794 /* Put it in the list */
795 split_node->next = node->next;
796 node->next = split_node;
797 } /* End of too big on top end */
799 dbg("%s: got one!!!\n", __func__);
800 /* If we got here, then it is the right size
801 * Now take it out of the list */
806 while (prevnode->next != node)
807 prevnode = prevnode->next;
809 prevnode->next = node->next;
819 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
820 * @head: the list to sort and clean up
822 * Description: Sorts all of the nodes in the list in ascending order by
823 * their base addresses. Also does garbage collection by
824 * combining adjacent nodes.
826 * Returns %0 if success.
828 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
830 struct pci_resource *node1;
831 struct pci_resource *node2;
832 int out_of_order = 1;
834 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
839 dbg("*head->next = %p\n",(*head)->next);
842 return 0; /* only one item on the list, already sorted! */
844 dbg("*head->base = 0x%x\n",(*head)->base);
845 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
846 while (out_of_order) {
849 /* Special case for swapping list head */
850 if (((*head)->next) &&
851 ((*head)->base > (*head)->next->base)) {
853 (*head) = (*head)->next;
854 node1->next = (*head)->next;
855 (*head)->next = node1;
861 while (node1->next && node1->next->next) {
862 if (node1->next->base > node1->next->next->base) {
865 node1->next = node1->next->next;
867 node2->next = node1->next;
872 } /* End of out_of_order loop */
876 while (node1 && node1->next) {
877 if ((node1->base + node1->length) == node1->next->base) {
880 node1->length += node1->next->length;
882 node1->next = node1->next->next;
892 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
894 struct controller *ctrl = data;
895 u8 schedule_flag = 0;
902 misc = readw(ctrl->hpc_reg + MISC);
904 * Check to see if it was our interrupt
906 if (!(misc & 0x000C)) {
912 * Serial Output interrupt Pending
915 /* Clear the interrupt */
917 writew(misc, ctrl->hpc_reg + MISC);
919 /* Read to clear posted writes */
920 misc = readw(ctrl->hpc_reg + MISC);
922 dbg ("%s - waking up\n", __func__);
923 wake_up_interruptible(&ctrl->queue);
927 /* General-interrupt-input interrupt Pending */
928 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
930 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
932 /* Clear the interrupt */
933 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
935 /* Read it back to clear any posted writes */
936 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
939 /* Clear all interrupts */
940 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
942 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
943 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
944 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
947 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
949 /* Bus reset has completed */
951 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
952 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
953 wake_up_interruptible(&ctrl->queue);
957 wake_up_process(cpqhp_event_thread);
958 dbg("Waking even thread");
965 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
966 * @busnumber: bus where new node is to be located
968 * Returns pointer to the new node or %NULL if unsuccessful.
970 struct pci_func *cpqhp_slot_create(u8 busnumber)
972 struct pci_func *new_slot;
973 struct pci_func *next;
975 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
976 if (new_slot == NULL)
979 new_slot->next = NULL;
980 new_slot->configured = 1;
982 if (cpqhp_slot_list[busnumber] == NULL) {
983 cpqhp_slot_list[busnumber] = new_slot;
985 next = cpqhp_slot_list[busnumber];
986 while (next->next != NULL)
988 next->next = new_slot;
995 * slot_remove - Removes a node from the linked list of slots.
996 * @old_slot: slot to remove
998 * Returns %0 if successful, !0 otherwise.
1000 static int slot_remove(struct pci_func * old_slot)
1002 struct pci_func *next;
1004 if (old_slot == NULL)
1007 next = cpqhp_slot_list[old_slot->bus];
1011 if (next == old_slot) {
1012 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1013 cpqhp_destroy_board_resources(old_slot);
1018 while ((next->next != old_slot) && (next->next != NULL))
1021 if (next->next == old_slot) {
1022 next->next = old_slot->next;
1023 cpqhp_destroy_board_resources(old_slot);
1032 * bridge_slot_remove - Removes a node from the linked list of slots.
1033 * @bridge: bridge to remove
1035 * Returns %0 if successful, !0 otherwise.
1037 static int bridge_slot_remove(struct pci_func *bridge)
1039 u8 subordinateBus, secondaryBus;
1041 struct pci_func *next;
1043 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1044 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1046 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1047 next = cpqhp_slot_list[tempBus];
1049 while (!slot_remove(next))
1050 next = cpqhp_slot_list[tempBus];
1053 next = cpqhp_slot_list[bridge->bus];
1058 if (next == bridge) {
1059 cpqhp_slot_list[bridge->bus] = bridge->next;
1063 while ((next->next != bridge) && (next->next != NULL))
1066 if (next->next != bridge)
1068 next->next = bridge->next;
1076 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1078 * @device: device to find
1079 * @index: is %0 for first function found, %1 for the second...
1081 * Returns pointer to the node if successful, %NULL otherwise.
1083 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1086 struct pci_func *func;
1088 func = cpqhp_slot_list[bus];
1090 if ((func == NULL) || ((func->device == device) && (index == 0)))
1093 if (func->device == device)
1096 while (func->next != NULL) {
1099 if (func->device == device)
1110 /* DJZ: I don't think is_bridge will work as is.
1112 static int is_bridge(struct pci_func * func)
1114 /* Check the header type */
1115 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1123 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
1124 * @ctrl: controller to change frequency/mode for.
1125 * @adapter_speed: the speed of the adapter we want to match.
1126 * @hp_slot: the slot number where the adapter is installed.
1128 * Returns %0 if we successfully change frequency and/or mode to match the
1131 static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1135 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1137 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
1139 if (ctrl->speed == adapter_speed)
1142 /* We don't allow freq/mode changes if we find another adapter running
1143 * in another slot on this controller
1145 for(slot = ctrl->slot; slot; slot = slot->next) {
1146 if (slot->device == (hp_slot + ctrl->slot_device_offset))
1148 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
1150 if (slot->hotplug_slot->info->adapter_status == 0)
1152 /* If another adapter is running on the same segment but at a
1153 * lower speed/mode, we allow the new adapter to function at
1154 * this rate if supported
1156 if (ctrl->speed < adapter_speed)
1162 /* If the controller doesn't support freq/mode changes and the
1163 * controller is running at a higher mode, we bail
1165 if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1168 /* But we allow the adapter to run at a lower rate if possible */
1169 if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1172 /* We try to set the max speed supported by both the adapter and
1175 if (ctrl->speed_capability < adapter_speed) {
1176 if (ctrl->speed == ctrl->speed_capability)
1178 adapter_speed = ctrl->speed_capability;
1181 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1182 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
1185 wait_for_ctrl_irq(ctrl);
1187 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1191 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1193 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1195 switch(adapter_speed) {
1196 case(PCI_SPEED_133MHz_PCIX):
1200 case(PCI_SPEED_100MHz_PCIX):
1204 case(PCI_SPEED_66MHz_PCIX):
1208 case(PCI_SPEED_66MHz):
1212 default: /* 33MHz PCI 2.2 */
1218 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
1222 /* Reenable interrupts */
1223 writel(0, ctrl->hpc_reg + INT_MASK);
1225 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1227 /* Restart state machine */
1229 pci_read_config_byte(ctrl->pci_dev, 0x43, ®);
1230 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
1232 /* Only if mode change...*/
1233 if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1234 ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1237 wait_for_ctrl_irq(ctrl);
1240 /* Restore LED/Slot state */
1241 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1242 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
1245 wait_for_ctrl_irq(ctrl);
1247 ctrl->speed = adapter_speed;
1248 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1250 info("Successfully changed frequency/mode for adapter in slot %d\n",
1255 /* the following routines constitute the bulk of the
1256 * hotplug controller logic
1261 * board_replaced - Called after a board has been replaced in the system.
1262 * @func: PCI device/function information
1263 * @ctrl: hotplug controller
1265 * This is only used if we don't have resources for hot add.
1266 * Turns power on for the board.
1267 * Checks to see if board is the same.
1268 * If board is same, reconfigures it.
1269 * If board isn't same, turns it back off.
1271 static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1278 hp_slot = func->device - ctrl->slot_device_offset;
1281 * The switch is open.
1283 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot))
1284 rc = INTERLOCK_OPEN;
1286 * The board is already on
1288 else if (is_slot_enabled (ctrl, hp_slot))
1289 rc = CARD_FUNCTIONING;
1291 mutex_lock(&ctrl->crit_sect);
1293 /* turn on board without attaching to the bus */
1294 enable_slot_power (ctrl, hp_slot);
1298 /* Wait for SOBS to be unset */
1299 wait_for_ctrl_irq (ctrl);
1301 /* Change bits in slot power register to force another shift out
1302 * NOTE: this is to work around the timer bug */
1303 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1304 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1305 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1309 /* Wait for SOBS to be unset */
1310 wait_for_ctrl_irq (ctrl);
1312 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1313 if (ctrl->speed != adapter_speed)
1314 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1315 rc = WRONG_BUS_FREQUENCY;
1317 /* turn off board without attaching to the bus */
1318 disable_slot_power (ctrl, hp_slot);
1322 /* Wait for SOBS to be unset */
1323 wait_for_ctrl_irq (ctrl);
1325 mutex_unlock(&ctrl->crit_sect);
1330 mutex_lock(&ctrl->crit_sect);
1332 slot_enable (ctrl, hp_slot);
1333 green_LED_blink (ctrl, hp_slot);
1335 amber_LED_off (ctrl, hp_slot);
1339 /* Wait for SOBS to be unset */
1340 wait_for_ctrl_irq (ctrl);
1342 mutex_unlock(&ctrl->crit_sect);
1344 /* Wait for ~1 second because of hot plug spec */
1347 /* Check for a power fault */
1348 if (func->status == 0xFF) {
1349 /* power fault occurred, but it was benign */
1353 rc = cpqhp_valid_replace(ctrl, func);
1356 /* It must be the same board */
1358 rc = cpqhp_configure_board(ctrl, func);
1360 /* If configuration fails, turn it off
1361 * Get slot won't work for devices behind
1362 * bridges, but in this case it will always be
1363 * called for the "base" bus/dev/func of an
1367 mutex_lock(&ctrl->crit_sect);
1369 amber_LED_on (ctrl, hp_slot);
1370 green_LED_off (ctrl, hp_slot);
1371 slot_disable (ctrl, hp_slot);
1375 /* Wait for SOBS to be unset */
1376 wait_for_ctrl_irq (ctrl);
1378 mutex_unlock(&ctrl->crit_sect);
1386 /* Something is wrong
1388 * Get slot won't work for devices behind bridges, but
1389 * in this case it will always be called for the "base"
1390 * bus/dev/func of an adapter.
1393 mutex_lock(&ctrl->crit_sect);
1395 amber_LED_on (ctrl, hp_slot);
1396 green_LED_off (ctrl, hp_slot);
1397 slot_disable (ctrl, hp_slot);
1401 /* Wait for SOBS to be unset */
1402 wait_for_ctrl_irq (ctrl);
1404 mutex_unlock(&ctrl->crit_sect);
1414 * board_added - Called after a board has been added to the system.
1415 * @func: PCI device/function info
1416 * @ctrl: hotplug controller
1418 * Turns power on for the board.
1421 static u32 board_added(struct pci_func *func, struct controller *ctrl)
1427 u32 temp_register = 0xFFFFFFFF;
1429 struct pci_func *new_slot = NULL;
1430 struct slot *p_slot;
1431 struct resource_lists res_lists;
1433 hp_slot = func->device - ctrl->slot_device_offset;
1434 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1435 __func__, func->device, ctrl->slot_device_offset, hp_slot);
1437 mutex_lock(&ctrl->crit_sect);
1439 /* turn on board without attaching to the bus */
1440 enable_slot_power(ctrl, hp_slot);
1444 /* Wait for SOBS to be unset */
1445 wait_for_ctrl_irq (ctrl);
1447 /* Change bits in slot power register to force another shift out
1448 * NOTE: this is to work around the timer bug
1450 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1451 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1452 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1456 /* Wait for SOBS to be unset */
1457 wait_for_ctrl_irq (ctrl);
1459 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1460 if (ctrl->speed != adapter_speed)
1461 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1462 rc = WRONG_BUS_FREQUENCY;
1464 /* turn off board without attaching to the bus */
1465 disable_slot_power (ctrl, hp_slot);
1469 /* Wait for SOBS to be unset */
1470 wait_for_ctrl_irq(ctrl);
1472 mutex_unlock(&ctrl->crit_sect);
1477 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1479 /* turn on board and blink green LED */
1481 dbg("%s: before down\n", __func__);
1482 mutex_lock(&ctrl->crit_sect);
1483 dbg("%s: after down\n", __func__);
1485 dbg("%s: before slot_enable\n", __func__);
1486 slot_enable (ctrl, hp_slot);
1488 dbg("%s: before green_LED_blink\n", __func__);
1489 green_LED_blink (ctrl, hp_slot);
1491 dbg("%s: before amber_LED_blink\n", __func__);
1492 amber_LED_off (ctrl, hp_slot);
1494 dbg("%s: before set_SOGO\n", __func__);
1497 /* Wait for SOBS to be unset */
1498 dbg("%s: before wait_for_ctrl_irq\n", __func__);
1499 wait_for_ctrl_irq (ctrl);
1500 dbg("%s: after wait_for_ctrl_irq\n", __func__);
1502 dbg("%s: before up\n", __func__);
1503 mutex_unlock(&ctrl->crit_sect);
1504 dbg("%s: after up\n", __func__);
1506 /* Wait for ~1 second because of hot plug spec */
1507 dbg("%s: before long_delay\n", __func__);
1509 dbg("%s: after long_delay\n", __func__);
1511 dbg("%s: func status = %x\n", __func__, func->status);
1512 /* Check for a power fault */
1513 if (func->status == 0xFF) {
1514 /* power fault occurred, but it was benign */
1515 temp_register = 0xFFFFFFFF;
1516 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
1520 /* Get vendor/device ID u32 */
1521 ctrl->pci_bus->number = func->bus;
1522 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1523 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1524 dbg("%s: temp_register is %x\n", __func__, temp_register);
1527 /* Something's wrong here */
1528 temp_register = 0xFFFFFFFF;
1529 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
1531 /* Preset return code. It will be changed later if things go okay. */
1532 rc = NO_ADAPTER_PRESENT;
1535 /* All F's is an empty slot or an invalid board */
1536 if (temp_register != 0xFFFFFFFF) {
1537 res_lists.io_head = ctrl->io_head;
1538 res_lists.mem_head = ctrl->mem_head;
1539 res_lists.p_mem_head = ctrl->p_mem_head;
1540 res_lists.bus_head = ctrl->bus_head;
1541 res_lists.irqs = NULL;
1543 rc = configure_new_device(ctrl, func, 0, &res_lists);
1545 dbg("%s: back from configure_new_device\n", __func__);
1546 ctrl->io_head = res_lists.io_head;
1547 ctrl->mem_head = res_lists.mem_head;
1548 ctrl->p_mem_head = res_lists.p_mem_head;
1549 ctrl->bus_head = res_lists.bus_head;
1551 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1552 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1553 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1554 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1557 mutex_lock(&ctrl->crit_sect);
1559 amber_LED_on (ctrl, hp_slot);
1560 green_LED_off (ctrl, hp_slot);
1561 slot_disable (ctrl, hp_slot);
1565 /* Wait for SOBS to be unset */
1566 wait_for_ctrl_irq (ctrl);
1568 mutex_unlock(&ctrl->crit_sect);
1571 cpqhp_save_slot_config(ctrl, func);
1576 func->switch_save = 0x10;
1577 func->is_a_board = 0x01;
1579 /* next, we will instantiate the linux pci_dev structures (with
1580 * appropriate driver notification, if already present) */
1581 dbg("%s: configure linux pci_dev structure\n", __func__);
1584 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1585 if (new_slot && !new_slot->pci_dev)
1586 cpqhp_configure_device(ctrl, new_slot);
1589 mutex_lock(&ctrl->crit_sect);
1591 green_LED_on (ctrl, hp_slot);
1595 /* Wait for SOBS to be unset */
1596 wait_for_ctrl_irq (ctrl);
1598 mutex_unlock(&ctrl->crit_sect);
1600 mutex_lock(&ctrl->crit_sect);
1602 amber_LED_on (ctrl, hp_slot);
1603 green_LED_off (ctrl, hp_slot);
1604 slot_disable (ctrl, hp_slot);
1608 /* Wait for SOBS to be unset */
1609 wait_for_ctrl_irq (ctrl);
1611 mutex_unlock(&ctrl->crit_sect);
1620 * remove_board - Turns off slot and LEDs
1621 * @func: PCI device/function info
1622 * @replace_flag: whether replacing or adding a new device
1623 * @ctrl: target controller
1625 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1633 struct resource_lists res_lists;
1634 struct pci_func *temp_func;
1636 if (cpqhp_unconfigure_device(func))
1639 device = func->device;
1641 hp_slot = func->device - ctrl->slot_device_offset;
1642 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
1644 /* When we get here, it is safe to change base address registers.
1645 * We will attempt to save the base address register lengths */
1646 if (replace_flag || !ctrl->add_support)
1647 rc = cpqhp_save_base_addr_length(ctrl, func);
1648 else if (!func->bus_head && !func->mem_head &&
1649 !func->p_mem_head && !func->io_head) {
1650 /* Here we check to see if we've saved any of the board's
1651 * resources already. If so, we'll skip the attempt to
1652 * determine what's being used. */
1654 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1656 if (temp_func->bus_head || temp_func->mem_head
1657 || temp_func->p_mem_head || temp_func->io_head) {
1661 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1665 rc = cpqhp_save_used_resources(ctrl, func);
1667 /* Change status to shutdown */
1668 if (func->is_a_board)
1669 func->status = 0x01;
1670 func->configured = 0;
1672 mutex_lock(&ctrl->crit_sect);
1674 green_LED_off (ctrl, hp_slot);
1675 slot_disable (ctrl, hp_slot);
1679 /* turn off SERR for slot */
1680 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1681 temp_byte &= ~(0x01 << hp_slot);
1682 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1684 /* Wait for SOBS to be unset */
1685 wait_for_ctrl_irq (ctrl);
1687 mutex_unlock(&ctrl->crit_sect);
1689 if (!replace_flag && ctrl->add_support) {
1691 res_lists.io_head = ctrl->io_head;
1692 res_lists.mem_head = ctrl->mem_head;
1693 res_lists.p_mem_head = ctrl->p_mem_head;
1694 res_lists.bus_head = ctrl->bus_head;
1696 cpqhp_return_board_resources(func, &res_lists);
1698 ctrl->io_head = res_lists.io_head;
1699 ctrl->mem_head = res_lists.mem_head;
1700 ctrl->p_mem_head = res_lists.p_mem_head;
1701 ctrl->bus_head = res_lists.bus_head;
1703 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1704 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1705 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1706 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1708 if (is_bridge(func)) {
1709 bridge_slot_remove(func);
1713 func = cpqhp_slot_find(ctrl->bus, device, 0);
1716 /* Setup slot structure with entry for empty slot */
1717 func = cpqhp_slot_create(ctrl->bus);
1722 func->bus = ctrl->bus;
1723 func->device = device;
1725 func->configured = 0;
1726 func->switch_save = 0x10;
1727 func->is_a_board = 0;
1728 func->p_task_event = NULL;
1734 static void pushbutton_helper_thread(unsigned long data)
1736 pushbutton_pending = data;
1737 wake_up_process(cpqhp_event_thread);
1741 /* this is the main worker thread */
1742 static int event_thread(void* data)
1744 struct controller *ctrl;
1747 dbg("!!!!event_thread sleeping\n");
1748 set_current_state(TASK_INTERRUPTIBLE);
1751 if (kthread_should_stop())
1754 if (pushbutton_pending)
1755 cpqhp_pushbutton_thread(pushbutton_pending);
1757 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1758 interrupt_event_handler(ctrl);
1760 dbg("event_thread signals exit\n");
1764 int cpqhp_event_start_thread(void)
1766 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1767 if (IS_ERR(cpqhp_event_thread)) {
1768 err ("Can't start up our event thread\n");
1769 return PTR_ERR(cpqhp_event_thread);
1776 void cpqhp_event_stop_thread(void)
1778 kthread_stop(cpqhp_event_thread);
1782 static int update_slot_info(struct controller *ctrl, struct slot *slot)
1784 struct hotplug_slot_info *info;
1787 info = kmalloc(sizeof(*info), GFP_KERNEL);
1791 info->power_status = get_slot_enabled(ctrl, slot);
1792 info->attention_status = cpq_get_attention_status(ctrl, slot);
1793 info->latch_status = cpq_get_latch_status(ctrl, slot);
1794 info->adapter_status = get_presence_status(ctrl, slot);
1795 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1800 static void interrupt_event_handler(struct controller *ctrl)
1804 struct pci_func *func;
1806 struct slot *p_slot;
1811 for (loop = 0; loop < 10; loop++) {
1812 /* dbg("loop %d\n", loop); */
1813 if (ctrl->event_queue[loop].event_type != 0) {
1814 hp_slot = ctrl->event_queue[loop].hp_slot;
1816 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1820 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1824 dbg("hp_slot %d, func %p, p_slot %p\n",
1825 hp_slot, func, p_slot);
1827 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1828 dbg("button pressed\n");
1829 } else if (ctrl->event_queue[loop].event_type ==
1830 INT_BUTTON_CANCEL) {
1831 dbg("button cancel\n");
1832 del_timer(&p_slot->task_event);
1834 mutex_lock(&ctrl->crit_sect);
1836 if (p_slot->state == BLINKINGOFF_STATE) {
1838 dbg("turn on green LED\n");
1839 green_LED_on (ctrl, hp_slot);
1840 } else if (p_slot->state == BLINKINGON_STATE) {
1842 dbg("turn off green LED\n");
1843 green_LED_off (ctrl, hp_slot);
1846 info(msg_button_cancel, p_slot->number);
1848 p_slot->state = STATIC_STATE;
1850 amber_LED_off (ctrl, hp_slot);
1854 /* Wait for SOBS to be unset */
1855 wait_for_ctrl_irq (ctrl);
1857 mutex_unlock(&ctrl->crit_sect);
1859 /*** button Released (No action on press...) */
1860 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1861 dbg("button release\n");
1863 if (is_slot_enabled (ctrl, hp_slot)) {
1864 dbg("slot is on\n");
1865 p_slot->state = BLINKINGOFF_STATE;
1866 info(msg_button_off, p_slot->number);
1868 dbg("slot is off\n");
1869 p_slot->state = BLINKINGON_STATE;
1870 info(msg_button_on, p_slot->number);
1872 mutex_lock(&ctrl->crit_sect);
1874 dbg("blink green LED and turn off amber\n");
1876 amber_LED_off (ctrl, hp_slot);
1877 green_LED_blink (ctrl, hp_slot);
1881 /* Wait for SOBS to be unset */
1882 wait_for_ctrl_irq (ctrl);
1884 mutex_unlock(&ctrl->crit_sect);
1885 init_timer(&p_slot->task_event);
1886 p_slot->hp_slot = hp_slot;
1887 p_slot->ctrl = ctrl;
1888 /* p_slot->physical_slot = physical_slot; */
1889 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1890 p_slot->task_event.function = pushbutton_helper_thread;
1891 p_slot->task_event.data = (u32) p_slot;
1893 dbg("add_timer p_slot = %p\n", p_slot);
1894 add_timer(&p_slot->task_event);
1896 /***********POWER FAULT */
1897 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1898 dbg("power fault\n");
1900 /* refresh notification */
1902 update_slot_info(ctrl, p_slot);
1905 ctrl->event_queue[loop].event_type = 0;
1909 } /* End of FOR loop */
1917 * cpqhp_pushbutton_thread - handle pushbutton events
1918 * @slot: target slot (struct)
1920 * Scheduled procedure to handle blocking stuff for the pushbuttons.
1921 * Handles all pending events and exits.
1923 void cpqhp_pushbutton_thread(unsigned long slot)
1927 struct pci_func *func;
1928 struct slot *p_slot = (struct slot *) slot;
1929 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1931 pushbutton_pending = 0;
1932 hp_slot = p_slot->hp_slot;
1934 device = p_slot->device;
1936 if (is_slot_enabled(ctrl, hp_slot)) {
1937 p_slot->state = POWEROFF_STATE;
1938 /* power Down board */
1939 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1940 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1942 dbg("Error! func NULL in %s\n", __func__);
1946 if (cpqhp_process_SS(ctrl, func) != 0) {
1947 amber_LED_on(ctrl, hp_slot);
1948 green_LED_on(ctrl, hp_slot);
1952 /* Wait for SOBS to be unset */
1953 wait_for_ctrl_irq(ctrl);
1956 p_slot->state = STATIC_STATE;
1958 p_slot->state = POWERON_STATE;
1961 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1962 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1964 dbg("Error! func NULL in %s\n", __func__);
1969 if (cpqhp_process_SI(ctrl, func) != 0) {
1970 amber_LED_on(ctrl, hp_slot);
1971 green_LED_off(ctrl, hp_slot);
1975 /* Wait for SOBS to be unset */
1976 wait_for_ctrl_irq (ctrl);
1980 p_slot->state = STATIC_STATE;
1987 int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1993 struct slot* p_slot;
1994 int physical_slot = 0;
1998 device = func->device;
1999 hp_slot = device - ctrl->slot_device_offset;
2000 p_slot = cpqhp_find_slot(ctrl, device);
2002 physical_slot = p_slot->number;
2004 /* Check to see if the interlock is closed */
2005 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2007 if (tempdword & (0x01 << hp_slot)) {
2011 if (func->is_a_board) {
2012 rc = board_replaced(func, ctrl);
2017 func = cpqhp_slot_create(ctrl->bus);
2021 func->bus = ctrl->bus;
2022 func->device = device;
2024 func->configured = 0;
2025 func->is_a_board = 1;
2027 /* We have to save the presence info for these slots */
2028 temp_word = ctrl->ctrl_int_comp >> 16;
2029 func->presence_save = (temp_word >> hp_slot) & 0x01;
2030 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2032 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2033 func->switch_save = 0;
2035 func->switch_save = 0x10;
2038 rc = board_added(func, ctrl);
2040 if (is_bridge(func)) {
2041 bridge_slot_remove(func);
2045 /* Setup slot structure with entry for empty slot */
2046 func = cpqhp_slot_create(ctrl->bus);
2051 func->bus = ctrl->bus;
2052 func->device = device;
2054 func->configured = 0;
2055 func->is_a_board = 0;
2057 /* We have to save the presence info for these slots */
2058 temp_word = ctrl->ctrl_int_comp >> 16;
2059 func->presence_save = (temp_word >> hp_slot) & 0x01;
2060 func->presence_save |=
2061 (temp_word >> (hp_slot + 7)) & 0x02;
2063 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2064 func->switch_save = 0;
2066 func->switch_save = 0x10;
2072 dbg("%s: rc = %d\n", __func__, rc);
2076 update_slot_info(ctrl, p_slot);
2082 int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2084 u8 device, class_code, header_type, BCR;
2089 struct slot* p_slot;
2090 struct pci_bus *pci_bus = ctrl->pci_bus;
2091 int physical_slot=0;
2093 device = func->device;
2094 func = cpqhp_slot_find(ctrl->bus, device, index++);
2095 p_slot = cpqhp_find_slot(ctrl, device);
2097 physical_slot = p_slot->number;
2100 /* Make sure there are no video controllers here */
2101 while (func && !rc) {
2102 pci_bus->number = func->bus;
2103 devfn = PCI_DEVFN(func->device, func->function);
2105 /* Check the Class Code */
2106 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2110 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2111 /* Display/Video adapter (not supported) */
2112 rc = REMOVE_NOT_SUPPORTED;
2114 /* See if it's a bridge */
2115 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2119 /* If it's a bridge, check the VGA Enable bit */
2120 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2121 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2125 /* If the VGA Enable bit is set, remove isn't
2127 if (BCR & PCI_BRIDGE_CTL_VGA)
2128 rc = REMOVE_NOT_SUPPORTED;
2132 func = cpqhp_slot_find(ctrl->bus, device, index++);
2135 func = cpqhp_slot_find(ctrl->bus, device, 0);
2136 if ((func != NULL) && !rc) {
2137 /* FIXME: Replace flag should be passed into process_SS */
2138 replace_flag = !(ctrl->add_support);
2139 rc = remove_board(func, replace_flag, ctrl);
2145 update_slot_info(ctrl, p_slot);
2151 * switch_leds - switch the leds, go from one site to the other.
2152 * @ctrl: controller to use
2153 * @num_of_slots: number of slots to use
2154 * @work_LED: LED control value
2155 * @direction: 1 to start from the left side, 0 to start right.
2157 static void switch_leds(struct controller *ctrl, const int num_of_slots,
2158 u32 *work_LED, const int direction)
2162 for (loop = 0; loop < num_of_slots; loop++) {
2164 *work_LED = *work_LED >> 1;
2166 *work_LED = *work_LED << 1;
2167 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2171 /* Wait for SOGO interrupt */
2172 wait_for_ctrl_irq(ctrl);
2174 /* Get ready for next iteration */
2175 long_delay((2*HZ)/10);
2180 * cpqhp_hardware_test - runs hardware tests
2181 * @ctrl: target controller
2182 * @test_num: the number written to the "test" file in sysfs.
2184 * For hot plug ctrl folks to play with.
2186 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2193 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2197 /* Do stuff here! */
2199 /* Do that funky LED thing */
2200 /* so we can restore them later */
2201 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2202 work_LED = 0x01010101;
2203 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2204 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2205 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2206 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2208 work_LED = 0x01010000;
2209 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2210 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2211 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2212 work_LED = 0x00000101;
2213 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2214 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2215 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2217 work_LED = 0x01010000;
2218 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2219 for (loop = 0; loop < num_of_slots; loop++) {
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);
2232 /* Wait for SOGO interrupt */
2233 wait_for_ctrl_irq (ctrl);
2235 /* Get ready for next iteration */
2236 long_delay((3*HZ)/10);
2237 work_LED = work_LED << 16;
2238 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2239 work_LED = work_LED << 1;
2240 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2243 /* put it back the way it was */
2244 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2248 /* Wait for SOBS to be unset */
2249 wait_for_ctrl_irq (ctrl);
2252 /* Do other stuff here! */
2263 * configure_new_device - Configures the PCI header information of one board.
2264 * @ctrl: pointer to controller structure
2265 * @func: pointer to function structure
2266 * @behind_bridge: 1 if this is a recursive call, 0 if not
2267 * @resources: pointer to set of resource lists
2269 * Returns 0 if success.
2271 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2272 u8 behind_bridge, struct resource_lists * resources)
2274 u8 temp_byte, function, max_functions, stop_it;
2277 struct pci_func *new_slot;
2282 dbg("%s\n", __func__);
2283 /* Check for Multi-function device */
2284 ctrl->pci_bus->number = func->bus;
2285 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2287 dbg("%s: rc = %d\n", __func__, rc);
2291 if (temp_byte & 0x80) /* Multi-function device */
2299 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2302 dbg("configure_new_function failed %d\n",rc);
2306 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2309 cpqhp_return_board_resources(new_slot, resources);
2319 /* The following loop skips to the next present function
2320 * and creates a board structure */
2322 while ((function < max_functions) && (!stop_it)) {
2323 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2325 if (ID == 0xFFFFFFFF) {
2328 /* Setup slot structure. */
2329 new_slot = cpqhp_slot_create(func->bus);
2331 if (new_slot == NULL)
2334 new_slot->bus = func->bus;
2335 new_slot->device = func->device;
2336 new_slot->function = function;
2337 new_slot->is_a_board = 1;
2338 new_slot->status = 0;
2344 } while (function < max_functions);
2345 dbg("returning from configure_new_device\n");
2352 * Configuration logic that involves the hotplug data structures and
2358 * configure_new_function - Configures the PCI header information of one device
2359 * @ctrl: pointer to controller structure
2360 * @func: pointer to function structure
2361 * @behind_bridge: 1 if this is a recursive call, 0 if not
2362 * @resources: pointer to set of resource lists
2364 * Calls itself recursively for bridged devices.
2365 * Returns 0 if success.
2367 static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2369 struct resource_lists *resources)
2384 struct pci_resource *mem_node;
2385 struct pci_resource *p_mem_node;
2386 struct pci_resource *io_node;
2387 struct pci_resource *bus_node;
2388 struct pci_resource *hold_mem_node;
2389 struct pci_resource *hold_p_mem_node;
2390 struct pci_resource *hold_IO_node;
2391 struct pci_resource *hold_bus_node;
2392 struct irq_mapping irqs;
2393 struct pci_func *new_slot;
2394 struct pci_bus *pci_bus;
2395 struct resource_lists temp_resources;
2397 pci_bus = ctrl->pci_bus;
2398 pci_bus->number = func->bus;
2399 devfn = PCI_DEVFN(func->device, func->function);
2401 /* Check for Bridge */
2402 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2406 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2407 /* set Primary bus */
2408 dbg("set Primary bus = %d\n", func->bus);
2409 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2413 /* find range of busses to use */
2414 dbg("find ranges of buses to use\n");
2415 bus_node = get_max_resource(&(resources->bus_head), 1);
2417 /* If we don't have any busses to allocate, we can't continue */
2421 /* set Secondary bus */
2422 temp_byte = bus_node->base;
2423 dbg("set Secondary bus = %d\n", bus_node->base);
2424 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2428 /* set subordinate bus */
2429 temp_byte = bus_node->base + bus_node->length - 1;
2430 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2431 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2435 /* set subordinate Latency Timer and base Latency Timer */
2437 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2440 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2444 /* set Cache Line size */
2446 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2450 /* Setup the IO, memory, and prefetchable windows */
2451 io_node = get_max_resource(&(resources->io_head), 0x1000);
2454 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2457 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2460 dbg("Setup the IO, memory, and prefetchable windows\n");
2462 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2463 io_node->length, io_node->next);
2465 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2466 mem_node->length, mem_node->next);
2467 dbg("p_mem_node\n");
2468 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2469 p_mem_node->length, p_mem_node->next);
2471 /* set up the IRQ info */
2472 if (!resources->irqs) {
2473 irqs.barber_pole = 0;
2474 irqs.interrupt[0] = 0;
2475 irqs.interrupt[1] = 0;
2476 irqs.interrupt[2] = 0;
2477 irqs.interrupt[3] = 0;
2480 irqs.barber_pole = resources->irqs->barber_pole;
2481 irqs.interrupt[0] = resources->irqs->interrupt[0];
2482 irqs.interrupt[1] = resources->irqs->interrupt[1];
2483 irqs.interrupt[2] = resources->irqs->interrupt[2];
2484 irqs.interrupt[3] = resources->irqs->interrupt[3];
2485 irqs.valid_INT = resources->irqs->valid_INT;
2488 /* set up resource lists that are now aligned on top and bottom
2489 * for anything behind the bridge. */
2490 temp_resources.bus_head = bus_node;
2491 temp_resources.io_head = io_node;
2492 temp_resources.mem_head = mem_node;
2493 temp_resources.p_mem_head = p_mem_node;
2494 temp_resources.irqs = &irqs;
2496 /* Make copies of the nodes we are going to pass down so that
2497 * if there is a problem,we can just use these to free resources
2499 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2500 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2501 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2502 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2504 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2505 kfree(hold_bus_node);
2506 kfree(hold_IO_node);
2507 kfree(hold_mem_node);
2508 kfree(hold_p_mem_node);
2513 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2515 bus_node->base += 1;
2516 bus_node->length -= 1;
2517 bus_node->next = NULL;
2519 /* If we have IO resources copy them and fill in the bridge's
2520 * IO range registers */
2522 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2523 io_node->next = NULL;
2525 /* set IO base and Limit registers */
2526 temp_byte = io_node->base >> 8;
2527 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2529 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2530 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2532 kfree(hold_IO_node);
2533 hold_IO_node = NULL;
2536 /* If we have memory resources copy them and fill in the
2537 * bridge's memory range registers. Otherwise, fill in the
2538 * range registers with values that disable them. */
2540 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2541 mem_node->next = NULL;
2543 /* set Mem base and Limit registers */
2544 temp_word = mem_node->base >> 16;
2545 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2547 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2548 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2551 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2554 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2556 kfree(hold_mem_node);
2557 hold_mem_node = NULL;
2560 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2561 p_mem_node->next = NULL;
2563 /* set Pre Mem base and Limit registers */
2564 temp_word = p_mem_node->base >> 16;
2565 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2567 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2568 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2570 /* Adjust this to compensate for extra adjustment in first loop
2576 /* Here we actually find the devices and configure them */
2577 for (device = 0; (device <= 0x1F) && !rc; device++) {
2578 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2581 pci_bus->number = hold_bus_node->base;
2582 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2583 pci_bus->number = func->bus;
2585 if (ID != 0xFFFFFFFF) { /* device present */
2586 /* Setup slot structure. */
2587 new_slot = cpqhp_slot_create(hold_bus_node->base);
2589 if (new_slot == NULL) {
2594 new_slot->bus = hold_bus_node->base;
2595 new_slot->device = device;
2596 new_slot->function = 0;
2597 new_slot->is_a_board = 1;
2598 new_slot->status = 0;
2600 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2601 dbg("configure_new_device rc=0x%x\n",rc);
2602 } /* End of IF (device in slot?) */
2603 } /* End of FOR loop */
2607 /* save the interrupt routing information */
2608 if (resources->irqs) {
2609 resources->irqs->interrupt[0] = irqs.interrupt[0];
2610 resources->irqs->interrupt[1] = irqs.interrupt[1];
2611 resources->irqs->interrupt[2] = irqs.interrupt[2];
2612 resources->irqs->interrupt[3] = irqs.interrupt[3];
2613 resources->irqs->valid_INT = irqs.valid_INT;
2614 } else if (!behind_bridge) {
2615 /* We need to hook up the interrupts here */
2616 for (cloop = 0; cloop < 4; cloop++) {
2617 if (irqs.valid_INT & (0x01 << cloop)) {
2618 rc = cpqhp_set_irq(func->bus, func->device,
2619 cloop + 1, irqs.interrupt[cloop]);
2623 } /* end of for loop */
2625 /* Return unused bus resources
2626 * First use the temporary node to store information for
2628 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2629 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2631 hold_bus_node->next = func->bus_head;
2632 func->bus_head = hold_bus_node;
2634 temp_byte = temp_resources.bus_head->base - 1;
2636 /* set subordinate bus */
2637 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2639 if (temp_resources.bus_head->length == 0) {
2640 kfree(temp_resources.bus_head);
2641 temp_resources.bus_head = NULL;
2643 return_resource(&(resources->bus_head), temp_resources.bus_head);
2647 /* If we have IO space available and there is some left,
2648 * return the unused portion */
2649 if (hold_IO_node && temp_resources.io_head) {
2650 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2651 &hold_IO_node, 0x1000);
2653 /* Check if we were able to split something off */
2655 hold_IO_node->base = io_node->base + io_node->length;
2657 temp_byte = (hold_IO_node->base) >> 8;
2658 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2660 return_resource(&(resources->io_head), io_node);
2663 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2665 /* Check if we were able to split something off */
2667 /* First use the temporary node to store
2668 * information for the board */
2669 hold_IO_node->length = io_node->base - hold_IO_node->base;
2671 /* If we used any, add it to the board's list */
2672 if (hold_IO_node->length) {
2673 hold_IO_node->next = func->io_head;
2674 func->io_head = hold_IO_node;
2676 temp_byte = (io_node->base - 1) >> 8;
2677 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2679 return_resource(&(resources->io_head), io_node);
2681 /* it doesn't need any IO */
2683 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2685 return_resource(&(resources->io_head), io_node);
2686 kfree(hold_IO_node);
2689 /* it used most of the range */
2690 hold_IO_node->next = func->io_head;
2691 func->io_head = hold_IO_node;
2693 } else if (hold_IO_node) {
2694 /* it used the whole range */
2695 hold_IO_node->next = func->io_head;
2696 func->io_head = hold_IO_node;
2698 /* If we have memory space available and there is some left,
2699 * return the unused portion */
2700 if (hold_mem_node && temp_resources.mem_head) {
2701 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2702 &hold_mem_node, 0x100000);
2704 /* Check if we were able to split something off */
2706 hold_mem_node->base = mem_node->base + mem_node->length;
2708 temp_word = (hold_mem_node->base) >> 16;
2709 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2711 return_resource(&(resources->mem_head), mem_node);
2714 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2716 /* Check if we were able to split something off */
2718 /* First use the temporary node to store
2719 * information for the board */
2720 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2722 if (hold_mem_node->length) {
2723 hold_mem_node->next = func->mem_head;
2724 func->mem_head = hold_mem_node;
2726 /* configure end address */
2727 temp_word = (mem_node->base - 1) >> 16;
2728 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2730 /* Return unused resources to the pool */
2731 return_resource(&(resources->mem_head), mem_node);
2733 /* it doesn't need any Mem */
2735 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2737 return_resource(&(resources->mem_head), mem_node);
2738 kfree(hold_mem_node);
2741 /* it used most of the range */
2742 hold_mem_node->next = func->mem_head;
2743 func->mem_head = hold_mem_node;
2745 } else if (hold_mem_node) {
2746 /* it used the whole range */
2747 hold_mem_node->next = func->mem_head;
2748 func->mem_head = hold_mem_node;
2750 /* If we have prefetchable memory space available and there
2751 * is some left at the end, return the unused portion */
2752 if (hold_p_mem_node && temp_resources.p_mem_head) {
2753 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2754 &hold_p_mem_node, 0x100000);
2756 /* Check if we were able to split something off */
2758 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2760 temp_word = (hold_p_mem_node->base) >> 16;
2761 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2763 return_resource(&(resources->p_mem_head), p_mem_node);
2766 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2768 /* Check if we were able to split something off */
2770 /* First use the temporary node to store
2771 * information for the board */
2772 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2774 /* If we used any, add it to the board's list */
2775 if (hold_p_mem_node->length) {
2776 hold_p_mem_node->next = func->p_mem_head;
2777 func->p_mem_head = hold_p_mem_node;
2779 temp_word = (p_mem_node->base - 1) >> 16;
2780 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2782 return_resource(&(resources->p_mem_head), p_mem_node);
2784 /* it doesn't need any PMem */
2786 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2788 return_resource(&(resources->p_mem_head), p_mem_node);
2789 kfree(hold_p_mem_node);
2792 /* it used the most of the range */
2793 hold_p_mem_node->next = func->p_mem_head;
2794 func->p_mem_head = hold_p_mem_node;
2796 } else if (hold_p_mem_node) {
2797 /* it used the whole range */
2798 hold_p_mem_node->next = func->p_mem_head;
2799 func->p_mem_head = hold_p_mem_node;
2801 /* We should be configuring an IRQ and the bridge's base address
2802 * registers if it needs them. Although we have never seen such
2806 command = 0x0157; /* = PCI_COMMAND_IO |
2807 * PCI_COMMAND_MEMORY |
2808 * PCI_COMMAND_MASTER |
2809 * PCI_COMMAND_INVALIDATE |
2810 * PCI_COMMAND_PARITY |
2811 * PCI_COMMAND_SERR */
2812 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2814 /* set Bridge Control Register */
2815 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2816 * PCI_BRIDGE_CTL_SERR |
2817 * PCI_BRIDGE_CTL_NO_ISA */
2818 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2819 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2820 /* Standard device */
2821 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2823 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2824 /* Display (video) adapter (not supported) */
2825 return DEVICE_TYPE_NOT_SUPPORTED;
2827 /* Figure out IO and memory needs */
2828 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2829 temp_register = 0xFFFFFFFF;
2831 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2832 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2834 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2835 dbg("CND: base = 0x%x\n", temp_register);
2837 if (temp_register) { /* If this register is implemented */
2838 if ((temp_register & 0x03L) == 0x01) {
2841 /* set base = amount of IO space */
2842 base = temp_register & 0xFFFFFFFC;
2845 dbg("CND: length = 0x%x\n", base);
2846 io_node = get_io_resource(&(resources->io_head), base);
2847 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2848 io_node->base, io_node->length, io_node->next);
2849 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2851 /* allocate the resource to the board */
2853 base = io_node->base;
2855 io_node->next = func->io_head;
2856 func->io_head = io_node;
2859 } else if ((temp_register & 0x0BL) == 0x08) {
2860 /* Map prefetchable memory */
2861 base = temp_register & 0xFFFFFFF0;
2864 dbg("CND: length = 0x%x\n", base);
2865 p_mem_node = get_resource(&(resources->p_mem_head), base);
2867 /* allocate the resource to the board */
2869 base = p_mem_node->base;
2871 p_mem_node->next = func->p_mem_head;
2872 func->p_mem_head = p_mem_node;
2875 } else if ((temp_register & 0x0BL) == 0x00) {
2877 base = temp_register & 0xFFFFFFF0;
2880 dbg("CND: length = 0x%x\n", base);
2881 mem_node = get_resource(&(resources->mem_head), base);
2883 /* allocate the resource to the board */
2885 base = mem_node->base;
2887 mem_node->next = func->mem_head;
2888 func->mem_head = mem_node;
2891 } else if ((temp_register & 0x0BL) == 0x04) {
2893 base = temp_register & 0xFFFFFFF0;
2896 dbg("CND: length = 0x%x\n", base);
2897 mem_node = get_resource(&(resources->mem_head), base);
2899 /* allocate the resource to the board */
2901 base = mem_node->base;
2903 mem_node->next = func->mem_head;
2904 func->mem_head = mem_node;
2907 } else if ((temp_register & 0x0BL) == 0x06) {
2908 /* Those bits are reserved, we can't handle this */
2911 /* Requesting space below 1M */
2912 return NOT_ENOUGH_RESOURCES;
2915 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2917 /* Check for 64-bit base */
2918 if ((temp_register & 0x07L) == 0x04) {
2921 /* Upper 32 bits of address always zero
2922 * on today's systems */
2923 /* FIXME this is probably not true on
2924 * Alpha and ia64??? */
2926 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2929 } /* End of base register loop */
2930 if (cpqhp_legacy_mode) {
2931 /* Figure out which interrupt pin this function uses */
2932 rc = pci_bus_read_config_byte (pci_bus, devfn,
2933 PCI_INTERRUPT_PIN, &temp_byte);
2935 /* If this function needs an interrupt and we are behind
2936 * a bridge and the pin is tied to something that's
2937 * alread mapped, set this one the same */
2938 if (temp_byte && resources->irqs &&
2939 (resources->irqs->valid_INT &
2940 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2941 /* We have to share with something already set up */
2942 IRQ = resources->irqs->interrupt[(temp_byte +
2943 resources->irqs->barber_pole - 1) & 0x03];
2945 /* Program IRQ based on card type */
2946 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2948 if (class_code == PCI_BASE_CLASS_STORAGE)
2949 IRQ = cpqhp_disk_irq;
2951 IRQ = cpqhp_nic_irq;
2955 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2958 if (!behind_bridge) {
2959 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
2963 /* TBD - this code may also belong in the other clause
2964 * of this If statement */
2965 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2966 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2971 rc = pci_bus_write_config_byte(pci_bus, devfn,
2972 PCI_LATENCY_TIMER, temp_byte);
2974 /* Cache Line size */
2976 rc = pci_bus_write_config_byte(pci_bus, devfn,
2977 PCI_CACHE_LINE_SIZE, temp_byte);
2979 /* disable ROM base Address */
2981 rc = pci_bus_write_config_word(pci_bus, devfn,
2982 PCI_ROM_ADDRESS, temp_dword);
2985 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2986 * PCI_COMMAND_MEMORY |
2987 * PCI_COMMAND_MASTER |
2988 * PCI_COMMAND_INVALIDATE |
2989 * PCI_COMMAND_PARITY |
2990 * PCI_COMMAND_SERR */
2991 rc = pci_bus_write_config_word (pci_bus, devfn,
2992 PCI_COMMAND, temp_word);
2993 } else { /* End of Not-A-Bridge else */
2994 /* It's some strange type of PCI adapter (Cardbus?) */
2995 return DEVICE_TYPE_NOT_SUPPORTED;
2998 func->configured = 1;
3002 cpqhp_destroy_resource_list (&temp_resources);
3004 return_resource(&(resources-> bus_head), hold_bus_node);
3005 return_resource(&(resources-> io_head), hold_IO_node);
3006 return_resource(&(resources-> mem_head), hold_mem_node);
3007 return_resource(&(resources-> p_mem_head), hold_p_mem_node);