2 * PCI Express 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.
7 * Copyright (C) 2003-2004 Intel Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38 #include <linux/wait.h>
39 #include <linux/smp_lock.h>
40 #include <linux/pci.h>
44 static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
45 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
46 static int configure_new_function( struct controller *ctrl, struct pci_func *func,
47 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
48 static void interrupt_event_handler(struct controller *ctrl);
50 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
51 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
52 static int event_finished;
53 static unsigned long pushbutton_pending; /* = 0 */
55 u8 pciehp_handle_attention_button(u8 hp_slot, void *inst_id)
57 struct controller *ctrl = (struct controller *) inst_id;
61 struct pci_func *func;
62 struct event_info *taskInfo;
64 /* Attention Button Change */
65 dbg("pciehp: Attention button interrupt received.\n");
67 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
69 /* This is the structure that tells the worker thread what to do */
70 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
71 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
73 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
74 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
76 ctrl->next_event = (ctrl->next_event + 1) % 10;
77 taskInfo->hp_slot = hp_slot;
82 * Button pressed - See if need to TAKE ACTION!!!
84 info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
85 taskInfo->event_type = INT_BUTTON_PRESS;
87 if ((p_slot->state == BLINKINGON_STATE)
88 || (p_slot->state == BLINKINGOFF_STATE)) {
89 /* Cancel if we are still blinking; this means that we press the
90 * attention again before the 5 sec. limit expires to cancel hot-add
93 taskInfo->event_type = INT_BUTTON_CANCEL;
94 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
95 } else if ((p_slot->state == POWERON_STATE)
96 || (p_slot->state == POWEROFF_STATE)) {
97 /* Ignore if the slot is on power-on or power-off state; this
98 * means that the previous attention button action to hot-add or
99 * hot-remove is undergoing
101 taskInfo->event_type = INT_BUTTON_IGNORE;
102 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
106 up(&event_semaphore); /* signal event thread that new event is posted */
112 u8 pciehp_handle_switch_change(u8 hp_slot, void *inst_id)
114 struct controller *ctrl = (struct controller *) inst_id;
118 struct pci_func *func;
119 struct event_info *taskInfo;
122 dbg("pciehp: Switch interrupt received.\n");
124 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
126 /* This is the structure that tells the worker thread
129 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
130 ctrl->next_event = (ctrl->next_event + 1) % 10;
131 taskInfo->hp_slot = hp_slot;
134 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
135 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
136 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
142 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
143 func->switch_save = 0;
144 taskInfo->event_type = INT_SWITCH_OPEN;
149 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
150 func->switch_save = 0x10;
151 taskInfo->event_type = INT_SWITCH_CLOSE;
155 up(&event_semaphore); /* signal event thread that new event is posted */
160 u8 pciehp_handle_presence_change(u8 hp_slot, void *inst_id)
162 struct controller *ctrl = (struct controller *) inst_id;
165 struct pci_func *func;
166 struct event_info *taskInfo;
168 /* Presence Change */
169 dbg("pciehp: Presence/Notify input change.\n");
171 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
173 /* This is the structure that tells the worker thread
176 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
177 ctrl->next_event = (ctrl->next_event + 1) % 10;
178 taskInfo->hp_slot = hp_slot;
181 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
183 /* Switch is open, assume a presence change
184 * Save the presence state
186 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
187 if (func->presence_save) {
191 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
192 taskInfo->event_type = INT_PRESENCE_ON;
197 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
198 taskInfo->event_type = INT_PRESENCE_OFF;
202 up(&event_semaphore); /* signal event thread that new event is posted */
207 u8 pciehp_handle_power_fault(u8 hp_slot, void *inst_id)
209 struct controller *ctrl = (struct controller *) inst_id;
212 struct pci_func *func;
213 struct event_info *taskInfo;
216 dbg("pciehp: Power fault interrupt received.\n");
218 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
220 /* this is the structure that tells the worker thread
223 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
224 ctrl->next_event = (ctrl->next_event + 1) % 10;
225 taskInfo->hp_slot = hp_slot;
228 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
230 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
232 * power fault Cleared
234 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
236 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
241 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
242 taskInfo->event_type = INT_POWER_FAULT;
243 /* set power fault status for this board */
245 info("power fault bit %x set\n", hp_slot);
248 up(&event_semaphore); /* signal event thread that new event is posted */
255 * sort_by_size: sort nodes by their length, smallest first.
257 * @head: list to sort
259 static int sort_by_size(struct pci_resource **head)
261 struct pci_resource *current_res;
262 struct pci_resource *next_res;
263 int out_of_order = 1;
268 if (!((*head)->next))
271 while (out_of_order) {
274 /* Special case for swapping list head */
275 if (((*head)->next) &&
276 ((*head)->length > (*head)->next->length)) {
279 *head = (*head)->next;
280 current_res->next = (*head)->next;
281 (*head)->next = current_res;
286 while (current_res->next && current_res->next->next) {
287 if (current_res->next->length > current_res->next->next->length) {
289 next_res = current_res->next;
290 current_res->next = current_res->next->next;
291 current_res = current_res->next;
292 next_res->next = current_res->next;
293 current_res->next = next_res;
295 current_res = current_res->next;
297 } /* End of out_of_order loop */
306 * Sorts nodes on the list by their length.
310 static int sort_by_max_size(struct pci_resource **head)
312 struct pci_resource *current_res;
313 struct pci_resource *next_res;
314 int out_of_order = 1;
319 if (!((*head)->next))
322 while (out_of_order) {
325 /* Special case for swapping list head */
326 if (((*head)->next) &&
327 ((*head)->length < (*head)->next->length)) {
330 *head = (*head)->next;
331 current_res->next = (*head)->next;
332 (*head)->next = current_res;
337 while (current_res->next && current_res->next->next) {
338 if (current_res->next->length < current_res->next->next->length) {
340 next_res = current_res->next;
341 current_res->next = current_res->next->next;
342 current_res = current_res->next;
343 next_res->next = current_res->next;
344 current_res->next = next_res;
346 current_res = current_res->next;
348 } /* End of out_of_order loop */
355 * do_pre_bridge_resource_split: return one unused resource node
356 * @head: list to scan
359 static struct pci_resource *
360 do_pre_bridge_resource_split(struct pci_resource **head,
361 struct pci_resource **orig_head, u32 alignment)
363 struct pci_resource *prevnode = NULL;
364 struct pci_resource *node;
365 struct pci_resource *split_node;
368 dbg("do_pre_bridge_resource_split\n");
370 if (!(*head) || !(*orig_head))
373 rc = pciehp_resource_sort_and_combine(head);
378 if ((*head)->base != (*orig_head)->base)
381 if ((*head)->length == (*orig_head)->length)
385 /* If we got here, there the bridge requires some of the resource, but
386 * we may be able to split some off of the front
390 if (node->length & (alignment -1)) {
391 /* this one isn't an aligned length, so we'll make a new entry
394 split_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
399 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
401 split_node->base = node->base;
402 split_node->length = temp_dword;
404 node->length -= temp_dword;
405 node->base += split_node->length;
407 /* Put it in the list */
409 split_node->next = node;
412 if (node->length < alignment)
420 while (prevnode->next != node)
421 prevnode = prevnode->next;
423 prevnode->next = node->next;
432 * do_bridge_resource_split: return one unused resource node
433 * @head: list to scan
436 static struct pci_resource *
437 do_bridge_resource_split(struct pci_resource **head, u32 alignment)
439 struct pci_resource *prevnode = NULL;
440 struct pci_resource *node;
447 rc = pciehp_resource_sort_and_combine(head);
460 if (node->length < alignment) {
465 if (node->base & (alignment - 1)) {
466 /* Short circuit if adjusted size is too small */
467 temp_dword = (node->base | (alignment-1)) + 1;
468 if ((node->length - (temp_dword - node->base)) < alignment) {
473 node->length -= (temp_dword - node->base);
474 node->base = temp_dword;
477 if (node->length & (alignment - 1)) {
478 /* There's stuff in use after this node */
490 * this function sorts the resource list by size and then
491 * returns the first node of "size" length that is not in the
492 * ISA aliasing window. If it finds a node larger than "size"
493 * it will split it up.
495 * size must be a power of two.
497 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
499 struct pci_resource *prevnode;
500 struct pci_resource *node;
501 struct pci_resource *split_node = NULL;
507 if ( pciehp_resource_sort_and_combine(head) )
510 if ( sort_by_size(head) )
513 for (node = *head; node; node = node->next) {
514 if (node->length < size)
517 if (node->base & (size - 1)) {
518 /* this one isn't base aligned properly
519 so we'll make a new entry and split it up */
520 temp_dword = (node->base | (size-1)) + 1;
522 /*/ Short circuit if adjusted size is too small */
523 if ((node->length - (temp_dword - node->base)) < size)
526 split_node = kmalloc(sizeof(struct pci_resource),
532 split_node->base = node->base;
533 split_node->length = temp_dword - node->base;
534 node->base = temp_dword;
535 node->length -= split_node->length;
537 /* Put it in the list */
538 split_node->next = node->next;
539 node->next = split_node;
540 } /* End of non-aligned base */
542 /* Don't need to check if too small since we already did */
543 if (node->length > size) {
544 /* this one is longer than we need
545 so we'll make a new entry and split it up */
546 split_node = kmalloc(sizeof(struct pci_resource),
552 split_node->base = node->base + size;
553 split_node->length = node->length - size;
556 /* Put it in the list */
557 split_node->next = node->next;
558 node->next = split_node;
559 } /* End of too big on top end */
561 /* For IO make sure it's not in the ISA aliasing space */
562 if (node->base & 0x300L)
565 /* If we got here, then it is the right size
566 Now take it out of the list */
571 while (prevnode->next != node)
572 prevnode = prevnode->next;
574 prevnode->next = node->next;
588 * Gets the largest node that is at least "size" big from the
589 * list pointed to by head. It aligns the node on top and bottom
590 * to "size" alignment before returning it.
591 * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
592 * This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
594 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
596 struct pci_resource *max;
597 struct pci_resource *temp;
598 struct pci_resource *split_node;
600 u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
606 if (pciehp_resource_sort_and_combine(head))
609 if (sort_by_max_size(head))
612 for (max = *head;max; max = max->next) {
614 /* If not big enough we could probably just bail,
615 instead we'll continue to the next. */
616 if (max->length < size)
619 if (max->base & (size - 1)) {
620 /* this one isn't base aligned properly
621 so we'll make a new entry and split it up */
622 temp_dword = (max->base | (size-1)) + 1;
624 /* Short circuit if adjusted size is too small */
625 if ((max->length - (temp_dword - max->base)) < size)
628 split_node = kmalloc(sizeof(struct pci_resource),
634 split_node->base = max->base;
635 split_node->length = temp_dword - max->base;
636 max->base = temp_dword;
637 max->length -= split_node->length;
639 /* Put it next in the list */
640 split_node->next = max->next;
641 max->next = split_node;
644 if ((max->base + max->length) & (size - 1)) {
645 /* this one isn't end aligned properly at the top
646 so we'll make a new entry and split it up */
647 split_node = kmalloc(sizeof(struct pci_resource),
652 temp_dword = ((max->base + max->length) & ~(size - 1));
653 split_node->base = temp_dword;
654 split_node->length = max->length + max->base
656 max->length -= split_node->length;
658 /* Put it in the list */
659 split_node->next = max->next;
660 max->next = split_node;
663 /* Make sure it didn't shrink too much when we aligned it */
664 if (max->length < size)
667 for ( i = 0; max_size[i] > size; i++) {
668 if (max->length > max_size[i]) {
669 split_node = kmalloc(sizeof(struct pci_resource),
672 break; /* return NULL; */
673 split_node->base = max->base + max_size[i];
674 split_node->length = max->length - max_size[i];
675 max->length = max_size[i];
676 /* Put it next in the list */
677 split_node->next = max->next;
678 max->next = split_node;
683 /* Now take it out of the list */
684 temp = (struct pci_resource*) *head;
688 while (temp && temp->next != max) {
692 temp->next = max->next;
699 /* If we get here, we couldn't find one */
707 * this function sorts the resource list by size and then
708 * returns the first node of "size" length. If it finds a node
709 * larger than "size" it will split it up.
711 * size must be a power of two.
713 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
715 struct pci_resource *prevnode;
716 struct pci_resource *node;
717 struct pci_resource *split_node;
723 if ( pciehp_resource_sort_and_combine(head) )
726 if ( sort_by_size(head) )
729 for (node = *head; node; node = node->next) {
730 dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
731 __FUNCTION__, size, node, node->base, node->length);
732 if (node->length < size)
735 if (node->base & (size - 1)) {
736 dbg("%s: not aligned\n", __FUNCTION__);
737 /* this one isn't base aligned properly
738 so we'll make a new entry and split it up */
739 temp_dword = (node->base | (size-1)) + 1;
741 /* Short circuit if adjusted size is too small */
742 if ((node->length - (temp_dword - node->base)) < size)
745 split_node = kmalloc(sizeof(struct pci_resource),
751 split_node->base = node->base;
752 split_node->length = temp_dword - node->base;
753 node->base = temp_dword;
754 node->length -= split_node->length;
756 /* Put it in the list */
757 split_node->next = node->next;
758 node->next = split_node;
759 } /* End of non-aligned base */
761 /* Don't need to check if too small since we already did */
762 if (node->length > size) {
763 dbg("%s: too big\n", __FUNCTION__);
764 /* this one is longer than we need
765 so we'll make a new entry and split it up */
766 split_node = kmalloc(sizeof(struct pci_resource),
772 split_node->base = node->base + size;
773 split_node->length = node->length - size;
776 /* Put it in the list */
777 split_node->next = node->next;
778 node->next = split_node;
779 } /* End of too big on top end */
781 dbg("%s: got one!!!\n", __FUNCTION__);
782 /* If we got here, then it is the right size
783 Now take it out of the list */
788 while (prevnode->next != node)
789 prevnode = prevnode->next;
791 prevnode->next = node->next;
802 * pciehp_resource_sort_and_combine
804 * Sorts all of the nodes in the list in ascending order by
805 * their base addresses. Also does garbage collection by
806 * combining adjacent nodes.
808 * returns 0 if success
810 int pciehp_resource_sort_and_combine(struct pci_resource **head)
812 struct pci_resource *node1;
813 struct pci_resource *node2;
814 int out_of_order = 1;
816 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
821 dbg("*head->next = %p\n",(*head)->next);
824 return 0; /* only one item on the list, already sorted! */
826 dbg("*head->base = 0x%x\n",(*head)->base);
827 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
828 while (out_of_order) {
831 /* Special case for swapping list head */
832 if (((*head)->next) &&
833 ((*head)->base > (*head)->next->base)) {
835 (*head) = (*head)->next;
836 node1->next = (*head)->next;
837 (*head)->next = node1;
843 while (node1->next && node1->next->next) {
844 if (node1->next->base > node1->next->next->base) {
847 node1->next = node1->next->next;
849 node2->next = node1->next;
854 } /* End of out_of_order loop */
858 while (node1 && node1->next) {
859 if ((node1->base + node1->length) == node1->next->base) {
862 node1->length += node1->next->length;
864 node1->next = node1->next->next;
875 * pciehp_slot_create - Creates a node and adds it to the proper bus.
876 * @busnumber - bus where new node is to be located
878 * Returns pointer to the new node or NULL if unsuccessful
880 struct pci_func *pciehp_slot_create(u8 busnumber)
882 struct pci_func *new_slot;
883 struct pci_func *next;
884 dbg("%s: busnumber %x\n", __FUNCTION__, busnumber);
885 new_slot = kmalloc(sizeof(struct pci_func), GFP_KERNEL);
887 if (new_slot == NULL)
890 memset(new_slot, 0, sizeof(struct pci_func));
892 new_slot->next = NULL;
893 new_slot->configured = 1;
895 if (pciehp_slot_list[busnumber] == NULL) {
896 pciehp_slot_list[busnumber] = new_slot;
898 next = pciehp_slot_list[busnumber];
899 while (next->next != NULL)
901 next->next = new_slot;
908 * slot_remove - Removes a node from the linked list of slots.
909 * @old_slot: slot to remove
911 * Returns 0 if successful, !0 otherwise.
913 static int slot_remove(struct pci_func * old_slot)
915 struct pci_func *next;
917 if (old_slot == NULL)
920 next = pciehp_slot_list[old_slot->bus];
925 if (next == old_slot) {
926 pciehp_slot_list[old_slot->bus] = old_slot->next;
927 pciehp_destroy_board_resources(old_slot);
932 while ((next->next != old_slot) && (next->next != NULL)) {
936 if (next->next == old_slot) {
937 next->next = old_slot->next;
938 pciehp_destroy_board_resources(old_slot);
947 * bridge_slot_remove - Removes a node from the linked list of slots.
948 * @bridge: bridge to remove
950 * Returns 0 if successful, !0 otherwise.
952 static int bridge_slot_remove(struct pci_func *bridge)
954 u8 subordinateBus, secondaryBus;
956 struct pci_func *next;
961 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
962 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
964 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
965 next = pciehp_slot_list[tempBus];
967 while (!slot_remove(next)) {
968 next = pciehp_slot_list[tempBus];
972 next = pciehp_slot_list[bridge->bus];
978 if (next == bridge) {
979 pciehp_slot_list[bridge->bus] = bridge->next;
984 while ((next->next != bridge) && (next->next != NULL)) {
988 if (next->next == bridge) {
989 next->next = bridge->next;
998 * pciehp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1000 * @device: device to find
1001 * @index: is 0 for first function found, 1 for the second...
1003 * Returns pointer to the node if successful, %NULL otherwise.
1005 struct pci_func *pciehp_slot_find(u8 bus, u8 device, u8 index)
1008 struct pci_func *func;
1010 func = pciehp_slot_list[bus];
1011 dbg("%s: bus %x device %x index %x\n",
1012 __FUNCTION__, bus, device, index);
1014 dbg("%s: func-> bus %x device %x function %x pci_dev %p\n",
1015 __FUNCTION__, func->bus, func->device, func->function,
1018 dbg("%s: func == NULL\n", __FUNCTION__);
1020 if ((func == NULL) || ((func->device == device) && (index == 0)))
1023 if (func->device == device)
1026 while (func->next != NULL) {
1029 dbg("%s: In while loop, func-> bus %x device %x function %x pci_dev %p\n",
1030 __FUNCTION__, func->bus, func->device, func->function,
1032 if (func->device == device)
1034 dbg("%s: while loop, found %d, index %d\n", __FUNCTION__,
1037 if ((found == index) || (func->function == index)) {
1038 dbg("%s: Found bus %x dev %x func %x\n", __FUNCTION__,
1039 func->bus, func->device, func->function);
1047 static int is_bridge(struct pci_func * func)
1049 /* Check the header type */
1050 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1057 /* The following routines constitute the bulk of the
1058 hotplug controller logic
1061 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
1063 /* Wait for exclusive access to hardware */
1064 down(&ctrl->crit_sect);
1066 /* turn off slot, turn on Amber LED, turn off Green LED */
1067 if (pslot->hpc_ops->power_off_slot(pslot)) {
1068 err("%s: Issue of Slot Power Off command failed\n", __FUNCTION__);
1069 up(&ctrl->crit_sect);
1072 wait_for_ctrl_irq (ctrl);
1074 pslot->hpc_ops->green_led_off(pslot);
1076 wait_for_ctrl_irq (ctrl);
1078 /* turn on Amber LED */
1079 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
1080 err("%s: Issue of Set Attention Led command failed\n", __FUNCTION__);
1081 up(&ctrl->crit_sect);
1084 wait_for_ctrl_irq (ctrl);
1086 /* Done with exclusive hardware access */
1087 up(&ctrl->crit_sect);
1091 * board_added - Called after a board has been added to the system.
1093 * Turns power on for the board
1097 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1101 u32 temp_register = 0xFFFFFFFF;
1103 struct pci_func *new_func = NULL;
1104 struct slot *p_slot;
1105 struct resource_lists res_lists;
1107 p_slot = pciehp_find_slot(ctrl, func->device);
1108 hp_slot = func->device - ctrl->slot_device_offset;
1110 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1112 /* Wait for exclusive access to hardware */
1113 down(&ctrl->crit_sect);
1116 rc = p_slot->hpc_ops->power_on_slot(p_slot);
1118 up(&ctrl->crit_sect);
1122 /* Wait for the command to complete */
1123 wait_for_ctrl_irq (ctrl);
1125 p_slot->hpc_ops->green_led_blink(p_slot);
1127 /* Wait for the command to complete */
1128 wait_for_ctrl_irq (ctrl);
1130 /* Done with exclusive hardware access */
1131 up(&ctrl->crit_sect);
1133 /* Wait for ~1 second */
1134 dbg("%s: before long_delay\n", __FUNCTION__);
1135 wait_for_ctrl_irq (ctrl);
1136 dbg("%s: afterlong_delay\n", __FUNCTION__);
1138 /* Check link training status */
1139 rc = p_slot->hpc_ops->check_lnk_status(ctrl);
1141 err("%s: Failed to check link status\n", __FUNCTION__);
1142 set_slot_off(ctrl, p_slot);
1146 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1148 /* Check for a power fault */
1149 if (func->status == 0xFF) {
1150 /* power fault occurred, but it was benign */
1151 temp_register = 0xFFFFFFFF;
1152 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1156 /* Get vendor/device ID u32 */
1157 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
1158 PCI_VENDOR_ID, &temp_register);
1159 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1160 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1163 /* Something's wrong here */
1164 temp_register = 0xFFFFFFFF;
1165 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1167 /* Preset return code. It will be changed later if things go okay. */
1168 rc = NO_ADAPTER_PRESENT;
1171 /* All F's is an empty slot or an invalid board */
1172 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1173 res_lists.io_head = ctrl->io_head;
1174 res_lists.mem_head = ctrl->mem_head;
1175 res_lists.p_mem_head = ctrl->p_mem_head;
1176 res_lists.bus_head = ctrl->bus_head;
1177 res_lists.irqs = NULL;
1179 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1180 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1182 ctrl->io_head = res_lists.io_head;
1183 ctrl->mem_head = res_lists.mem_head;
1184 ctrl->p_mem_head = res_lists.p_mem_head;
1185 ctrl->bus_head = res_lists.bus_head;
1187 pciehp_resource_sort_and_combine(&(ctrl->mem_head));
1188 pciehp_resource_sort_and_combine(&(ctrl->p_mem_head));
1189 pciehp_resource_sort_and_combine(&(ctrl->io_head));
1190 pciehp_resource_sort_and_combine(&(ctrl->bus_head));
1193 set_slot_off(ctrl, p_slot);
1196 pciehp_save_slot_config(ctrl, func);
1199 func->switch_save = 0x10;
1200 func->is_a_board = 0x01;
1202 /* next, we will instantiate the linux pci_dev structures
1203 * (with appropriate driver notification, if already present)
1207 new_func = pciehp_slot_find(ctrl->slot_bus, func->device, index++);
1208 if (new_func && !new_func->pci_dev) {
1209 dbg("%s:call pci_hp_configure_dev, func %x\n",
1210 __FUNCTION__, index);
1211 pciehp_configure_device(ctrl, new_func);
1215 /* Wait for exclusive access to hardware */
1216 down(&ctrl->crit_sect);
1218 p_slot->hpc_ops->green_led_on(p_slot);
1220 /* Wait for the command to complete */
1221 wait_for_ctrl_irq (ctrl);
1224 /* Done with exclusive hardware access */
1225 up(&ctrl->crit_sect);
1228 set_slot_off(ctrl, p_slot);
1236 * remove_board - Turns off slot and LED's
1239 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1246 struct resource_lists res_lists;
1247 struct pci_func *temp_func;
1248 struct slot *p_slot;
1253 if (pciehp_unconfigure_device(func))
1256 device = func->device;
1258 hp_slot = func->device - ctrl->slot_device_offset;
1259 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1261 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1263 if ((ctrl->add_support) &&
1264 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1265 /* Here we check to see if we've saved any of the board's
1266 * resources already. If so, we'll skip the attempt to
1267 * determine what's being used.
1273 while ((temp_func = pciehp_slot_find(temp_func->bus, temp_func->device, index++))) {
1274 if (temp_func->bus_head || temp_func->mem_head
1275 || temp_func->p_mem_head || temp_func->io_head) {
1282 rc = pciehp_save_used_resources(ctrl, func, DISABLE_CARD);
1284 /* Change status to shutdown */
1285 if (func->is_a_board)
1286 func->status = 0x01;
1287 func->configured = 0;
1289 /* Wait for exclusive access to hardware */
1290 down(&ctrl->crit_sect);
1292 /* power off slot */
1293 rc = p_slot->hpc_ops->power_off_slot(p_slot);
1295 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1296 up(&ctrl->crit_sect);
1299 /* Wait for the command to complete */
1300 wait_for_ctrl_irq (ctrl);
1302 /* turn off Green LED */
1303 p_slot->hpc_ops->green_led_off(p_slot);
1305 /* Wait for the command to complete */
1306 wait_for_ctrl_irq (ctrl);
1308 /* Done with exclusive hardware access */
1309 up(&ctrl->crit_sect);
1311 if (ctrl->add_support) {
1313 res_lists.io_head = ctrl->io_head;
1314 res_lists.mem_head = ctrl->mem_head;
1315 res_lists.p_mem_head = ctrl->p_mem_head;
1316 res_lists.bus_head = ctrl->bus_head;
1318 dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n",
1319 func->bus, func->device, func->function);
1321 pciehp_return_board_resources(func, &res_lists);
1323 ctrl->io_head = res_lists.io_head;
1324 ctrl->mem_head = res_lists.mem_head;
1325 ctrl->p_mem_head = res_lists.p_mem_head;
1326 ctrl->bus_head = res_lists.bus_head;
1328 pciehp_resource_sort_and_combine(&(ctrl->mem_head));
1329 pciehp_resource_sort_and_combine(&(ctrl->p_mem_head));
1330 pciehp_resource_sort_and_combine(&(ctrl->io_head));
1331 pciehp_resource_sort_and_combine(&(ctrl->bus_head));
1333 if (is_bridge(func)) {
1334 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n",
1335 ctrl->seg, func->bus, func->device, func->function);
1336 bridge_slot_remove(func);
1338 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n",
1339 ctrl->seg, func->bus, func->device, func->function);
1342 func = pciehp_slot_find(ctrl->slot_bus, device, 0);
1345 /* Setup slot structure with entry for empty slot */
1346 func = pciehp_slot_create(ctrl->slot_bus);
1352 func->bus = ctrl->slot_bus;
1353 func->device = device;
1355 func->configured = 0;
1356 func->switch_save = 0x10;
1357 func->is_a_board = 0;
1364 static void pushbutton_helper_thread(unsigned long data)
1366 pushbutton_pending = data;
1368 up(&event_semaphore);
1373 * pciehp_pushbutton_thread
1375 * Scheduled procedure to handle blocking stuff for the pushbuttons
1376 * Handles all pending events and exits.
1379 static void pciehp_pushbutton_thread(unsigned long slot)
1381 struct slot *p_slot = (struct slot *) slot;
1384 pushbutton_pending = 0;
1387 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1391 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1393 p_slot->state = POWEROFF_STATE;
1394 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1396 pciehp_disable_slot(p_slot);
1397 p_slot->state = STATIC_STATE;
1399 p_slot->state = POWERON_STATE;
1400 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1402 if (pciehp_enable_slot(p_slot)) {
1403 /* Wait for exclusive access to hardware */
1404 down(&p_slot->ctrl->crit_sect);
1406 p_slot->hpc_ops->green_led_off(p_slot);
1408 /* Wait for the command to complete */
1409 wait_for_ctrl_irq (p_slot->ctrl);
1411 /* Done with exclusive hardware access */
1412 up(&p_slot->ctrl->crit_sect);
1414 p_slot->state = STATIC_STATE;
1421 /* this is the main worker thread */
1422 static int event_thread(void* data)
1424 struct controller *ctrl;
1426 daemonize("pciehpd_event");
1431 dbg("!!!!event_thread sleeping\n");
1432 down_interruptible (&event_semaphore);
1433 dbg("event_thread woken finished = %d\n", event_finished);
1434 if (event_finished || signal_pending(current))
1437 if (pushbutton_pending)
1438 pciehp_pushbutton_thread(pushbutton_pending);
1440 for (ctrl = pciehp_ctrl_list; ctrl; ctrl=ctrl->next)
1441 interrupt_event_handler(ctrl);
1443 dbg("event_thread signals exit\n");
1448 int pciehp_event_start_thread(void)
1452 /* initialize our semaphores */
1453 init_MUTEX_LOCKED(&event_exit);
1456 init_MUTEX_LOCKED(&event_semaphore);
1457 pid = kernel_thread(event_thread, NULL, 0);
1460 err ("Can't start up our event thread\n");
1463 dbg("Our event thread pid = %d\n", pid);
1468 void pciehp_event_stop_thread(void)
1471 dbg("event_thread finish command given\n");
1472 up(&event_semaphore);
1473 dbg("wait for event_thread to exit\n");
1478 static int update_slot_info(struct slot *slot)
1480 struct hotplug_slot_info *info;
1481 /* char buffer[SLOT_NAME_SIZE]; */
1484 info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
1488 /* make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot); */
1490 slot->hpc_ops->get_power_status(slot, &(info->power_status));
1491 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1492 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1493 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1495 /* result = pci_hp_change_slot_info(buffer, info); */
1496 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1501 static void interrupt_event_handler(struct controller *ctrl)
1505 struct pci_func *func;
1508 struct slot *p_slot;
1513 for (loop = 0; loop < 10; loop++) {
1514 if (ctrl->event_queue[loop].event_type != 0) {
1515 hp_slot = ctrl->event_queue[loop].hp_slot;
1517 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1519 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1521 dbg("hp_slot %d, func %p, p_slot %p\n", hp_slot, func, p_slot);
1523 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1524 dbg("button cancel\n");
1525 del_timer(&p_slot->task_event);
1527 switch (p_slot->state) {
1528 case BLINKINGOFF_STATE:
1529 /* Wait for exclusive access to hardware */
1530 down(&ctrl->crit_sect);
1532 p_slot->hpc_ops->green_led_on(p_slot);
1533 /* Wait for the command to complete */
1534 wait_for_ctrl_irq (ctrl);
1536 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1538 /* Wait for the command to complete */
1539 wait_for_ctrl_irq (ctrl);
1541 /* Done with exclusive hardware access */
1542 up(&ctrl->crit_sect);
1544 case BLINKINGON_STATE:
1545 /* Wait for exclusive access to hardware */
1546 down(&ctrl->crit_sect);
1548 p_slot->hpc_ops->green_led_off(p_slot);
1549 /* Wait for the command to complete */
1550 wait_for_ctrl_irq (ctrl);
1552 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1553 /* Wait for the command to complete */
1554 wait_for_ctrl_irq (ctrl);
1556 /* Done with exclusive hardware access */
1557 up(&ctrl->crit_sect);
1561 warn("Not a valid state\n");
1564 info(msg_button_cancel, p_slot->number);
1565 p_slot->state = STATIC_STATE;
1567 /* ***********Button Pressed (No action on 1st press...) */
1568 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1569 dbg("Button pressed\n");
1571 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1574 dbg("slot is on\n");
1575 p_slot->state = BLINKINGOFF_STATE;
1576 info(msg_button_off, p_slot->number);
1579 dbg("slot is off\n");
1580 p_slot->state = BLINKINGON_STATE;
1581 info(msg_button_on, p_slot->number);
1584 /* Wait for exclusive access to hardware */
1585 down(&ctrl->crit_sect);
1587 /* blink green LED and turn off amber */
1588 p_slot->hpc_ops->green_led_blink(p_slot);
1589 /* Wait for the command to complete */
1590 wait_for_ctrl_irq (ctrl);
1592 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1594 /* Wait for the command to complete */
1595 wait_for_ctrl_irq (ctrl);
1597 /* Done with exclusive hardware access */
1598 up(&ctrl->crit_sect);
1600 init_timer(&p_slot->task_event);
1601 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1602 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1603 p_slot->task_event.data = (unsigned long) p_slot;
1605 dbg("add_timer p_slot = %p\n", (void *) p_slot);
1606 add_timer(&p_slot->task_event);
1608 /***********POWER FAULT********************/
1609 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1610 dbg("power fault\n");
1611 /* Wait for exclusive access to hardware */
1612 down(&ctrl->crit_sect);
1614 p_slot->hpc_ops->set_attention_status(p_slot, 1);
1615 wait_for_ctrl_irq (ctrl);
1617 p_slot->hpc_ops->green_led_off(p_slot);
1618 wait_for_ctrl_irq (ctrl);
1620 /* Done with exclusive hardware access */
1621 up(&ctrl->crit_sect);
1623 /* refresh notification */
1625 update_slot_info(p_slot);
1628 ctrl->event_queue[loop].event_type = 0;
1632 } /* End of FOR loop */
1637 int pciehp_enable_slot(struct slot *p_slot)
1641 struct pci_func *func;
1643 func = pciehp_slot_find(p_slot->bus, p_slot->device, 0);
1645 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1649 /* Check to see if (latch closed, card present, power off) */
1650 down(&p_slot->ctrl->crit_sect);
1651 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1652 if (rc || !getstatus) {
1653 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1654 up(&p_slot->ctrl->crit_sect);
1658 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1659 if (rc || getstatus) {
1660 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1661 up(&p_slot->ctrl->crit_sect);
1665 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1666 if (rc || getstatus) {
1667 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
1668 up(&p_slot->ctrl->crit_sect);
1671 up(&p_slot->ctrl->crit_sect);
1675 func = pciehp_slot_create(p_slot->bus);
1679 func->bus = p_slot->bus;
1680 func->device = p_slot->device;
1682 func->configured = 0;
1683 func->is_a_board = 1;
1685 /* We have to save the presence info for these slots */
1686 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1687 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1688 func->switch_save = !getstatus? 0x10:0;
1690 rc = board_added(func, p_slot->ctrl);
1692 if (is_bridge(func))
1693 bridge_slot_remove(func);
1697 /* Setup slot structure with entry for empty slot */
1698 func = pciehp_slot_create(p_slot->bus);
1700 return 1; /* Out of memory */
1702 func->bus = p_slot->bus;
1703 func->device = p_slot->device;
1705 func->configured = 0;
1706 func->is_a_board = 1;
1708 /* We have to save the presence info for these slots */
1709 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1710 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1711 func->switch_save = !getstatus? 0x10:0;
1715 update_slot_info(p_slot);
1721 int pciehp_disable_slot(struct slot *p_slot)
1723 u8 class_code, header_type, BCR;
1729 struct pci_bus *pci_bus = p_slot->ctrl->pci_dev->subordinate;
1730 struct pci_func *func;
1735 /* Check to see if (latch closed, card present, power on) */
1736 down(&p_slot->ctrl->crit_sect);
1738 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1739 if (ret || !getstatus) {
1740 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1741 up(&p_slot->ctrl->crit_sect);
1745 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1746 if (ret || getstatus) {
1747 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1748 up(&p_slot->ctrl->crit_sect);
1752 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1753 if (ret || !getstatus) {
1754 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
1755 up(&p_slot->ctrl->crit_sect);
1758 up(&p_slot->ctrl->crit_sect);
1760 func = pciehp_slot_find(p_slot->bus, p_slot->device, index++);
1762 /* Make sure there are no video controllers here
1763 * for all func of p_slot
1765 while (func && !rc) {
1766 pci_bus->number = func->bus;
1767 devfn = PCI_DEVFN(func->device, func->function);
1769 /* Check the Class Code */
1770 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
1774 if (class_code == PCI_BASE_CLASS_DISPLAY) {
1775 /* Display/Video adapter (not supported) */
1776 rc = REMOVE_NOT_SUPPORTED;
1778 /* See if it's a bridge */
1779 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1783 /* If it's a bridge, check the VGA Enable bit */
1784 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1785 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
1789 /* If the VGA Enable bit is set, remove isn't supported */
1790 if (BCR & PCI_BRIDGE_CTL_VGA) {
1791 rc = REMOVE_NOT_SUPPORTED;
1796 func = pciehp_slot_find(p_slot->bus, p_slot->device, index++);
1799 func = pciehp_slot_find(p_slot->bus, p_slot->device, 0);
1800 if ((func != NULL) && !rc) {
1801 rc = remove_board(func, p_slot->ctrl);
1806 update_slot_info(p_slot);
1813 * configure_new_device - Configures the PCI header information of one board.
1815 * @ctrl: pointer to controller structure
1816 * @func: pointer to function structure
1817 * @behind_bridge: 1 if this is a recursive call, 0 if not
1818 * @resources: pointer to set of resource lists
1820 * Returns 0 if success
1823 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
1824 u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
1826 u8 temp_byte, function, max_functions, stop_it;
1829 struct pci_func *new_slot;
1830 struct pci_bus lpci_bus, *pci_bus;
1835 dbg("%s\n", __FUNCTION__);
1836 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
1837 pci_bus = &lpci_bus;
1838 pci_bus->number = func->bus;
1840 /* Check for Multi-function device */
1841 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
1843 dbg("%s: rc = %d\n", __FUNCTION__, rc);
1847 if (temp_byte & 0x80) /* Multi-function device */
1855 rc = configure_new_function(ctrl, new_slot, behind_bridge,
1856 resources, bridge_bus, bridge_dev);
1859 dbg("configure_new_function failed: %d\n", rc);
1863 new_slot = pciehp_slot_find(new_slot->bus,
1864 new_slot->device, index++);
1867 pciehp_return_board_resources(new_slot,
1878 /* The following loop skips to the next present function
1879 * and creates a board structure
1882 while ((function < max_functions) && (!stop_it)) {
1883 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
1885 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
1887 } else { /* There's something there */
1888 /* Setup slot structure. */
1889 new_slot = pciehp_slot_create(func->bus);
1891 if (new_slot == NULL) {
1896 new_slot->bus = func->bus;
1897 new_slot->device = func->device;
1898 new_slot->function = function;
1899 new_slot->is_a_board = 1;
1900 new_slot->status = 0;
1906 } while (function < max_functions);
1907 dbg("returning from %s\n", __FUNCTION__);
1913 * Configuration logic that involves the hotplug data structures and
1918 * configure_bridge: fill bridge's registers, either configure or disable it.
1921 configure_bridge(struct pci_bus *pci_bus, unsigned int devfn,
1922 struct pci_resource *mem_node,
1923 struct pci_resource **hold_mem_node,
1924 int base_addr, int limit_addr)
1930 memcpy(*hold_mem_node, mem_node, sizeof(struct pci_resource));
1931 mem_node->next = NULL;
1933 /* set Mem base and Limit registers */
1934 RES_CHECK(mem_node->base, 16);
1935 temp_word = (u16)(mem_node->base >> 16);
1936 rc = pci_bus_write_config_word(pci_bus, devfn, base_addr, temp_word);
1938 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
1939 temp_word = (u16)((mem_node->base + mem_node->length - 1) >> 16);
1940 rc = pci_bus_write_config_word(pci_bus, devfn, limit_addr, temp_word);
1943 rc = pci_bus_write_config_word(pci_bus, devfn, base_addr, temp_word);
1946 rc = pci_bus_write_config_word(pci_bus, devfn, limit_addr, temp_word);
1948 kfree(*hold_mem_node);
1949 *hold_mem_node = NULL;
1955 configure_new_bridge(struct controller *ctrl, struct pci_func *func,
1956 u8 behind_bridge, struct resource_lists *resources,
1957 struct pci_bus *pci_bus)
1966 struct pci_resource *mem_node;
1967 struct pci_resource *p_mem_node;
1968 struct pci_resource *io_node;
1969 struct pci_resource *bus_node;
1970 struct pci_resource *hold_mem_node;
1971 struct pci_resource *hold_p_mem_node;
1972 struct pci_resource *hold_IO_node;
1973 struct pci_resource *hold_bus_node;
1974 struct irq_mapping irqs;
1975 struct pci_func *new_slot;
1976 struct resource_lists temp_resources;
1978 devfn = PCI_DEVFN(func->device, func->function);
1980 /* set Primary bus */
1981 dbg("set Primary bus = 0x%x\n", func->bus);
1982 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
1986 /* find range of busses to use */
1987 bus_node = get_max_resource(&resources->bus_head, 1L);
1989 /* If we don't have any busses to allocate, we can't continue */
1991 err("Got NO bus resource to use\n");
1994 dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
1996 /* set Secondary bus */
1997 temp_byte = (u8)bus_node->base;
1998 dbg("set Secondary bus = 0x%x\n", temp_byte);
1999 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2003 /* set subordinate bus */
2004 temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2005 dbg("set subordinate bus = 0x%x\n", temp_byte);
2006 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2010 /* Set HP parameters (Cache Line Size, Latency Timer) */
2011 rc = pciehprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2015 /* Setup the IO, memory, and prefetchable windows */
2017 io_node = get_max_resource(&(resources->io_head), 0x1000L);
2019 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base,
2020 io_node->length, io_node->next);
2023 mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2025 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base,
2026 mem_node->length, mem_node->next);
2029 if (resources->p_mem_head)
2030 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2033 * In some platform implementation, MEM and PMEM are not
2034 * distinguished, and hence ACPI _CRS has only MEM entries
2035 * for both MEM and PMEM.
2037 dbg("using MEM for PMEM\n");
2038 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2041 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2042 p_mem_node->length, p_mem_node->next);
2045 /* set up the IRQ info */
2046 if (!resources->irqs) {
2047 irqs.barber_pole = 0;
2048 irqs.interrupt[0] = 0;
2049 irqs.interrupt[1] = 0;
2050 irqs.interrupt[2] = 0;
2051 irqs.interrupt[3] = 0;
2054 irqs.barber_pole = resources->irqs->barber_pole;
2055 irqs.interrupt[0] = resources->irqs->interrupt[0];
2056 irqs.interrupt[1] = resources->irqs->interrupt[1];
2057 irqs.interrupt[2] = resources->irqs->interrupt[2];
2058 irqs.interrupt[3] = resources->irqs->interrupt[3];
2059 irqs.valid_INT = resources->irqs->valid_INT;
2062 /* set up resource lists that are now aligned on top and bottom
2063 * for anything behind the bridge.
2065 temp_resources.bus_head = bus_node;
2066 temp_resources.io_head = io_node;
2067 temp_resources.mem_head = mem_node;
2068 temp_resources.p_mem_head = p_mem_node;
2069 temp_resources.irqs = &irqs;
2071 /* Make copies of the nodes we are going to pass down so that
2072 * if there is a problem,we can just use these to free resources
2074 hold_bus_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2075 hold_IO_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2076 hold_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2077 hold_p_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2079 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2080 kfree(hold_bus_node);
2081 kfree(hold_IO_node);
2082 kfree(hold_mem_node);
2083 kfree(hold_p_mem_node);
2088 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2090 bus_node->base += 1;
2091 bus_node->length -= 1;
2092 bus_node->next = NULL;
2094 /* If we have IO resources copy them and fill in the bridge's
2095 * IO range registers
2098 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2099 io_node->next = NULL;
2101 /* set IO base and Limit registers */
2102 RES_CHECK(io_node->base, 8);
2103 temp_byte = (u8)(io_node->base >> 8);
2104 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2106 RES_CHECK(io_node->base + io_node->length - 1, 8);
2107 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2108 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2110 kfree(hold_IO_node);
2111 hold_IO_node = NULL;
2114 /* If we have memory resources copy them and fill in the bridge's
2115 * memory range registers. Otherwise, fill in the range
2116 * registers with values that disable them.
2118 rc = configure_bridge(pci_bus, devfn, mem_node, &hold_mem_node,
2119 PCI_MEMORY_BASE, PCI_MEMORY_LIMIT);
2121 /* If we have prefetchable memory resources copy them and
2122 * fill in the bridge's memory range registers. Otherwise,
2123 * fill in the range registers with values that disable them.
2125 rc = configure_bridge(pci_bus, devfn, p_mem_node, &hold_p_mem_node,
2126 PCI_PREF_MEMORY_BASE, PCI_PREF_MEMORY_LIMIT);
2128 /* Adjust this to compensate for extra adjustment in first loop */
2133 /* Here we actually find the devices and configure them */
2134 for (device = 0; (device <= 0x1F) && !rc; device++) {
2135 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2138 pci_bus->number = hold_bus_node->base;
2139 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
2140 pci_bus->number = func->bus;
2142 if (ID != 0xFFFFFFFF) { /* device Present */
2143 /* Setup slot structure. */
2144 new_slot = pciehp_slot_create(hold_bus_node->base);
2146 if (new_slot == NULL) {
2152 new_slot->bus = hold_bus_node->base;
2153 new_slot->device = device;
2154 new_slot->function = 0;
2155 new_slot->is_a_board = 1;
2156 new_slot->status = 0;
2158 rc = configure_new_device(ctrl, new_slot, 1,
2159 &temp_resources, func->bus,
2161 dbg("configure_new_device rc=0x%x\n",rc);
2162 } /* End of IF (device in slot?) */
2163 } /* End of FOR loop */
2166 pciehp_destroy_resource_list(&temp_resources);
2168 return_resource(&(resources->bus_head), hold_bus_node);
2169 return_resource(&(resources->io_head), hold_IO_node);
2170 return_resource(&(resources->mem_head), hold_mem_node);
2171 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2175 /* save the interrupt routing information */
2176 if (resources->irqs) {
2177 resources->irqs->interrupt[0] = irqs.interrupt[0];
2178 resources->irqs->interrupt[1] = irqs.interrupt[1];
2179 resources->irqs->interrupt[2] = irqs.interrupt[2];
2180 resources->irqs->interrupt[3] = irqs.interrupt[3];
2181 resources->irqs->valid_INT = irqs.valid_INT;
2182 } else if (!behind_bridge) {
2183 /* We need to hook up the interrupts here */
2184 for (cloop = 0; cloop < 4; cloop++) {
2185 if (irqs.valid_INT & (0x01 << cloop)) {
2186 rc = pciehp_set_irq(func->bus, func->device,
2187 0x0A + cloop, irqs.interrupt[cloop]);
2189 pciehp_destroy_resource_list (&temp_resources);
2190 return_resource(&(resources->bus_head), hold_bus_node);
2191 return_resource(&(resources->io_head), hold_IO_node);
2192 return_resource(&(resources->mem_head), hold_mem_node);
2193 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2197 } /* end of for loop */
2200 /* Return unused bus resources
2201 * First use the temporary node to store information for the board
2203 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2204 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2206 hold_bus_node->next = func->bus_head;
2207 func->bus_head = hold_bus_node;
2209 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2211 /* set subordinate bus */
2212 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2213 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2215 if (temp_resources.bus_head->length == 0) {
2216 kfree(temp_resources.bus_head);
2217 temp_resources.bus_head = NULL;
2219 dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2220 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2221 return_resource(&(resources->bus_head), temp_resources.bus_head);
2225 /* If we have IO space available and there is some left,
2226 * return the unused portion
2228 if (hold_IO_node && temp_resources.io_head) {
2229 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2230 &hold_IO_node, 0x1000);
2232 /* Check if we were able to split something off */
2234 hold_IO_node->base = io_node->base + io_node->length;
2236 RES_CHECK(hold_IO_node->base, 8);
2237 temp_byte = (u8)((hold_IO_node->base) >> 8);
2238 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2240 return_resource(&(resources->io_head), io_node);
2243 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2245 /* Check if we were able to split something off */
2247 /* First use the temporary node to store information for the board */
2248 hold_IO_node->length = io_node->base - hold_IO_node->base;
2250 /* If we used any, add it to the board's list */
2251 if (hold_IO_node->length) {
2252 hold_IO_node->next = func->io_head;
2253 func->io_head = hold_IO_node;
2255 RES_CHECK(io_node->base - 1, 8);
2256 temp_byte = (u8)((io_node->base - 1) >> 8);
2257 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2259 return_resource(&(resources->io_head), io_node);
2261 /* it doesn't need any IO */
2263 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2265 return_resource(&(resources->io_head), io_node);
2266 kfree(hold_IO_node);
2269 /* it used most of the range */
2270 hold_IO_node->next = func->io_head;
2271 func->io_head = hold_IO_node;
2273 } else if (hold_IO_node) {
2274 /* it used the whole range */
2275 hold_IO_node->next = func->io_head;
2276 func->io_head = hold_IO_node;
2279 /* If we have memory space available and there is some left,
2280 * return the unused portion
2282 if (hold_mem_node && temp_resources.mem_head) {
2283 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2285 /* Check if we were able to split something off */
2287 hold_mem_node->base = mem_node->base + mem_node->length;
2289 RES_CHECK(hold_mem_node->base, 16);
2290 temp_word = (u16)((hold_mem_node->base) >> 16);
2291 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2293 return_resource(&(resources->mem_head), mem_node);
2296 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2298 /* Check if we were able to split something off */
2300 /* First use the temporary node to store information for the board */
2301 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2303 if (hold_mem_node->length) {
2304 hold_mem_node->next = func->mem_head;
2305 func->mem_head = hold_mem_node;
2307 /* configure end address */
2308 RES_CHECK(mem_node->base - 1, 16);
2309 temp_word = (u16)((mem_node->base - 1) >> 16);
2310 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2312 /* Return unused resources to the pool */
2313 return_resource(&(resources->mem_head), mem_node);
2315 /* it doesn't need any Mem */
2317 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2319 return_resource(&(resources->mem_head), mem_node);
2320 kfree(hold_mem_node);
2323 /* it used most of the range */
2324 hold_mem_node->next = func->mem_head;
2325 func->mem_head = hold_mem_node;
2327 } else if (hold_mem_node) {
2328 /* it used the whole range */
2329 hold_mem_node->next = func->mem_head;
2330 func->mem_head = hold_mem_node;
2333 /* If we have prefetchable memory space available and there is some
2334 * left at the end, return the unused portion
2336 if (hold_p_mem_node && temp_resources.p_mem_head) {
2337 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2338 &hold_p_mem_node, 0x100000L);
2340 /* Check if we were able to split something off */
2342 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2344 RES_CHECK(hold_p_mem_node->base, 16);
2345 temp_word = (u16)((hold_p_mem_node->base) >> 16);
2346 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2348 return_resource(&(resources->p_mem_head), p_mem_node);
2351 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2353 /* Check if we were able to split something off */
2355 /* First use the temporary node to store information for the board */
2356 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2358 /* If we used any, add it to the board's list */
2359 if (hold_p_mem_node->length) {
2360 hold_p_mem_node->next = func->p_mem_head;
2361 func->p_mem_head = hold_p_mem_node;
2363 RES_CHECK(p_mem_node->base - 1, 16);
2364 temp_word = (u16)((p_mem_node->base - 1) >> 16);
2365 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2367 return_resource(&(resources->p_mem_head), p_mem_node);
2369 /* it doesn't need any PMem */
2371 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2373 return_resource(&(resources->p_mem_head), p_mem_node);
2374 kfree(hold_p_mem_node);
2377 /* it used the most of the range */
2378 hold_p_mem_node->next = func->p_mem_head;
2379 func->p_mem_head = hold_p_mem_node;
2381 } else if (hold_p_mem_node) {
2382 /* it used the whole range */
2383 hold_p_mem_node->next = func->p_mem_head;
2384 func->p_mem_head = hold_p_mem_node;
2387 /* We should be configuring an IRQ and the bridge's base address
2388 * registers if it needs them. Although we have never seen such
2392 pciehprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2394 dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2400 * configure_new_function - Configures the PCI header information of one device
2402 * @ctrl: pointer to controller structure
2403 * @func: pointer to function structure
2404 * @behind_bridge: 1 if this is a recursive call, 0 if not
2405 * @resources: pointer to set of resource lists
2407 * Calls itself recursively for bridged devices.
2408 * Returns 0 if success
2412 configure_new_function(struct controller *ctrl, struct pci_func *func,
2413 u8 behind_bridge, struct resource_lists *resources,
2414 u8 bridge_bus, u8 bridge_dev)
2424 struct pci_resource *mem_node;
2425 struct pci_resource *io_node;
2426 struct pci_bus lpci_bus, *pci_bus;
2428 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2429 pci_bus = &lpci_bus;
2430 pci_bus->number = func->bus;
2431 devfn = PCI_DEVFN(func->device, func->function);
2433 /* Check for Bridge */
2434 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2437 dbg("%s: bus %x dev %x func %x temp_byte = %x\n", __FUNCTION__,
2438 func->bus, func->device, func->function, temp_byte);
2440 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2441 rc = configure_new_bridge(ctrl, func, behind_bridge, resources,
2446 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2447 /* Standard device */
2449 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
2451 if (class_code == PCI_BASE_CLASS_DISPLAY)
2452 return DEVICE_TYPE_NOT_SUPPORTED;
2454 /* Figure out IO and memory needs */
2455 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2456 temp_register = 0xFFFFFFFF;
2458 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2459 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2460 dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register,
2461 func->bus, func->device, func->function);
2467 if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2470 /* set base = amount of IO space */
2471 base = temp_register & 0xFFFFFFFC;
2474 dbg("NEED IO length(0x%x)\n", base);
2475 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2477 /* allocate the resource to the board */
2479 dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2480 base = (u32)io_node->base;
2481 io_node->next = func->io_head;
2482 func->io_head = io_node;
2484 err("Got NO IO resource(length=0x%x)\n", base);
2487 } else { /* map MEM */
2488 int prefetchable = 1;
2489 struct pci_resource **res_node = &func->p_mem_head;
2490 char *res_type_str = "PMEM";
2493 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2495 res_node = &func->mem_head;
2499 base = temp_register & 0xFFFFFFF0;
2502 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2503 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2504 dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2506 if (prefetchable && resources->p_mem_head)
2507 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2510 dbg("using MEM for PMEM\n");
2511 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2514 /* allocate the resource to the board */
2516 base = (u32)mem_node->base;
2517 mem_node->next = *res_node;
2518 *res_node = mem_node;
2519 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
2522 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2526 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2527 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2528 dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
2529 temp_register, base);
2531 if (prefetchable && resources->p_mem_head)
2532 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2535 dbg("using MEM for PMEM\n");
2536 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2539 /* allocate the resource to the board */
2541 base64 = mem_node->base;
2542 mem_node->next = *res_node;
2543 *res_node = mem_node;
2544 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
2545 (u32)base64, mem_node->length);
2547 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2552 dbg("reserved BAR type=0x%x\n", temp_register);
2559 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2564 dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2568 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2570 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2572 } /* End of base register loop */
2574 /* disable ROM base Address */
2576 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
2578 /* Set HP parameters (Cache Line Size, Latency Timer) */
2579 rc = pciehprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2583 pciehprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2585 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device,
2587 } /* End of Not-A-Bridge else */
2589 /* It's some strange type of PCI adapter (Cardbus?) */
2590 return DEVICE_TYPE_NOT_SUPPORTED;
2593 func->configured = 1;