2 * Standard 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 */
58 u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
60 struct controller *ctrl = (struct controller *) inst_id;
64 struct pci_func *func;
65 struct event_info *taskInfo;
67 /* Attention Button Change */
68 dbg("shpchp: Attention button interrupt received.\n");
70 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
72 /* This is the structure that tells the worker thread what to do */
73 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
74 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
76 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
77 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
79 ctrl->next_event = (ctrl->next_event + 1) % 10;
80 taskInfo->hp_slot = hp_slot;
85 * Button pressed - See if need to TAKE ACTION!!!
87 info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
88 taskInfo->event_type = INT_BUTTON_PRESS;
90 if ((p_slot->state == BLINKINGON_STATE)
91 || (p_slot->state == BLINKINGOFF_STATE)) {
92 /* Cancel if we are still blinking; this means that we press the
93 * attention again before the 5 sec. limit expires to cancel hot-add
96 taskInfo->event_type = INT_BUTTON_CANCEL;
97 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
98 } else if ((p_slot->state == POWERON_STATE)
99 || (p_slot->state == POWEROFF_STATE)) {
100 /* Ignore if the slot is on power-on or power-off state; this
101 * means that the previous attention button action to hot-add or
102 * hot-remove is undergoing
104 taskInfo->event_type = INT_BUTTON_IGNORE;
105 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
109 up(&event_semaphore); /* signal event thread that new event is posted */
115 u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
117 struct controller *ctrl = (struct controller *) inst_id;
121 struct pci_func *func;
122 struct event_info *taskInfo;
125 dbg("shpchp: Switch interrupt received.\n");
127 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
129 /* This is the structure that tells the worker thread
132 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
133 ctrl->next_event = (ctrl->next_event + 1) % 10;
134 taskInfo->hp_slot = hp_slot;
137 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
138 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
139 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
145 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
146 func->switch_save = 0;
147 taskInfo->event_type = INT_SWITCH_OPEN;
152 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
153 func->switch_save = 0x10;
154 taskInfo->event_type = INT_SWITCH_CLOSE;
158 up(&event_semaphore); /* signal event thread that new event is posted */
163 u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
165 struct controller *ctrl = (struct controller *) inst_id;
169 struct pci_func *func;
170 struct event_info *taskInfo;
172 /* Presence Change */
173 dbg("shpchp: Presence/Notify input change.\n");
175 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
177 /* This is the structure that tells the worker thread
180 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
181 ctrl->next_event = (ctrl->next_event + 1) % 10;
182 taskInfo->hp_slot = hp_slot;
185 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
188 * Save the presence state
190 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
191 if (func->presence_save) {
195 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
196 taskInfo->event_type = INT_PRESENCE_ON;
201 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
202 taskInfo->event_type = INT_PRESENCE_OFF;
206 up(&event_semaphore); /* signal event thread that new event is posted */
211 u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
213 struct controller *ctrl = (struct controller *) inst_id;
216 struct pci_func *func;
217 struct event_info *taskInfo;
220 dbg("shpchp: Power fault interrupt received.\n");
222 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
224 /* This is the structure that tells the worker thread
227 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
228 ctrl->next_event = (ctrl->next_event + 1) % 10;
229 taskInfo->hp_slot = hp_slot;
232 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
234 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
236 * Power fault Cleared
238 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
240 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
245 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
246 taskInfo->event_type = INT_POWER_FAULT;
247 /* set power fault status for this board */
249 info("power fault bit %x set\n", hp_slot);
252 up(&event_semaphore); /* signal event thread that new event is posted */
261 * Sorts nodes on the list by their length.
265 static int sort_by_size(struct pci_resource **head)
267 struct pci_resource *current_res;
268 struct pci_resource *next_res;
269 int out_of_order = 1;
274 if (!((*head)->next))
277 while (out_of_order) {
280 /* Special case for swapping list head */
281 if (((*head)->next) &&
282 ((*head)->length > (*head)->next->length)) {
285 *head = (*head)->next;
286 current_res->next = (*head)->next;
287 (*head)->next = current_res;
292 while (current_res->next && current_res->next->next) {
293 if (current_res->next->length > current_res->next->next->length) {
295 next_res = current_res->next;
296 current_res->next = current_res->next->next;
297 current_res = current_res->next;
298 next_res->next = current_res->next;
299 current_res->next = next_res;
301 current_res = current_res->next;
303 } /* End of out_of_order loop */
312 * Sorts nodes on the list by their length.
316 static int sort_by_max_size(struct pci_resource **head)
318 struct pci_resource *current_res;
319 struct pci_resource *next_res;
320 int out_of_order = 1;
325 if (!((*head)->next))
328 while (out_of_order) {
331 /* Special case for swapping list head */
332 if (((*head)->next) &&
333 ((*head)->length < (*head)->next->length)) {
336 *head = (*head)->next;
337 current_res->next = (*head)->next;
338 (*head)->next = current_res;
343 while (current_res->next && current_res->next->next) {
344 if (current_res->next->length < current_res->next->next->length) {
346 next_res = current_res->next;
347 current_res->next = current_res->next->next;
348 current_res = current_res->next;
349 next_res->next = current_res->next;
350 current_res->next = next_res;
352 current_res = current_res->next;
354 } /* End of out_of_order loop */
361 * do_pre_bridge_resource_split
363 * Returns zero or one node of resources that aren't in use
366 static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
368 struct pci_resource *prevnode = NULL;
369 struct pci_resource *node;
370 struct pci_resource *split_node;
373 dbg("do_pre_bridge_resource_split\n");
375 if (!(*head) || !(*orig_head))
378 rc = shpchp_resource_sort_and_combine(head);
383 if ((*head)->base != (*orig_head)->base)
386 if ((*head)->length == (*orig_head)->length)
390 /* If we got here, there the bridge requires some of the resource, but
391 * we may be able to split some off of the front
395 if (node->length & (alignment -1)) {
396 /* This one isn't an aligned length, so we'll make a new entry
399 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
404 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
406 split_node->base = node->base;
407 split_node->length = temp_dword;
409 node->length -= temp_dword;
410 node->base += split_node->length;
412 /* Put it in the list */
414 split_node->next = node;
417 if (node->length < alignment) {
427 while (prevnode->next != node)
428 prevnode = prevnode->next;
430 prevnode->next = node->next;
439 * do_bridge_resource_split
441 * Returns zero or one node of resources that aren't in use
444 static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
446 struct pci_resource *prevnode = NULL;
447 struct pci_resource *node;
454 rc = shpchp_resource_sort_and_combine(head);
467 if (node->length < alignment) {
472 if (node->base & (alignment - 1)) {
473 /* Short circuit if adjusted size is too small */
474 temp_dword = (node->base | (alignment-1)) + 1;
475 if ((node->length - (temp_dword - node->base)) < alignment) {
480 node->length -= (temp_dword - node->base);
481 node->base = temp_dword;
484 if (node->length & (alignment - 1)) {
485 /* There's stuff in use after this node */
497 * this function sorts the resource list by size and then
498 * returns the first node of "size" length that is not in the
499 * ISA aliasing window. If it finds a node larger than "size"
500 * it will split it up.
502 * size must be a power of two.
504 static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
506 struct pci_resource *prevnode;
507 struct pci_resource *node;
508 struct pci_resource *split_node = NULL;
514 if ( shpchp_resource_sort_and_combine(head) )
517 if ( sort_by_size(head) )
520 for (node = *head; node; node = node->next) {
521 if (node->length < size)
524 if (node->base & (size - 1)) {
525 /* This one isn't base aligned properly
526 so we'll make a new entry and split it up */
527 temp_dword = (node->base | (size-1)) + 1;
529 /*/ Short circuit if adjusted size is too small */
530 if ((node->length - (temp_dword - node->base)) < size)
533 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
538 split_node->base = node->base;
539 split_node->length = temp_dword - node->base;
540 node->base = temp_dword;
541 node->length -= split_node->length;
543 /* Put it in the list */
544 split_node->next = node->next;
545 node->next = split_node;
546 } /* End of non-aligned base */
548 /* Don't need to check if too small since we already did */
549 if (node->length > size) {
550 /* This one is longer than we need
551 so we'll make a new entry and split it up */
552 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
557 split_node->base = node->base + size;
558 split_node->length = node->length - size;
561 /* Put it in the list */
562 split_node->next = node->next;
563 node->next = split_node;
564 } /* End of too big on top end */
566 /* For IO make sure it's not in the ISA aliasing space */
567 if (node->base & 0x300L)
570 /* If we got here, then it is the right size
571 Now take it out of the list */
576 while (prevnode->next != node)
577 prevnode = prevnode->next;
579 prevnode->next = node->next;
593 * Gets the largest node that is at least "size" big from the
594 * list pointed to by head. It aligns the node on top and bottom
595 * to "size" alignment before returning it.
596 * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
597 * This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
599 static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
601 struct pci_resource *max;
602 struct pci_resource *temp;
603 struct pci_resource *split_node;
605 u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
611 if (shpchp_resource_sort_and_combine(head))
614 if (sort_by_max_size(head))
617 for (max = *head;max; max = max->next) {
619 /* If not big enough we could probably just bail,
620 instead we'll continue to the next. */
621 if (max->length < size)
624 if (max->base & (size - 1)) {
625 /* This one isn't base aligned properly
626 so we'll make a new entry and split it up */
627 temp_dword = (max->base | (size-1)) + 1;
629 /* Short circuit if adjusted size is too small */
630 if ((max->length - (temp_dword - max->base)) < size)
633 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
638 split_node->base = max->base;
639 split_node->length = temp_dword - max->base;
640 max->base = temp_dword;
641 max->length -= split_node->length;
643 /* Put it next in the list */
644 split_node->next = max->next;
645 max->next = split_node;
648 if ((max->base + max->length) & (size - 1)) {
649 /* This one isn't end aligned properly at the top
650 so we'll make a new entry and split it up */
651 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
655 temp_dword = ((max->base + max->length) & ~(size - 1));
656 split_node->base = temp_dword;
657 split_node->length = max->length + max->base
659 max->length -= split_node->length;
661 /* Put it in the list */
662 split_node->next = max->next;
663 max->next = split_node;
666 /* Make sure it didn't shrink too much when we aligned it */
667 if (max->length < size)
670 for ( i = 0; max_size[i] > size; i++) {
671 if (max->length > max_size[i]) {
672 split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
674 break; /* return (NULL); */
675 split_node->base = max->base + max_size[i];
676 split_node->length = max->length - max_size[i];
677 max->length = max_size[i];
678 /* Put it next in the list */
679 split_node->next = max->next;
680 max->next = split_node;
685 /* Now take it out of the list */
686 temp = (struct pci_resource*) *head;
690 while (temp && temp->next != max) {
694 temp->next = max->next;
701 /* If we get here, we couldn't find one */
709 * this function sorts the resource list by size and then
710 * returns the first node of "size" length. If it finds a node
711 * larger than "size" it will split it up.
713 * size must be a power of two.
715 static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
717 struct pci_resource *prevnode;
718 struct pci_resource *node;
719 struct pci_resource *split_node;
725 if ( shpchp_resource_sort_and_combine(head) )
728 if ( sort_by_size(head) )
731 for (node = *head; node; node = node->next) {
732 dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
733 __FUNCTION__, size, node, node->base, node->length);
734 if (node->length < size)
737 if (node->base & (size - 1)) {
738 dbg("%s: not aligned\n", __FUNCTION__);
739 /* this one isn't base aligned properly
740 so we'll make a new entry and split it up */
741 temp_dword = (node->base | (size-1)) + 1;
743 /* Short circuit if adjusted size is too small */
744 if ((node->length - (temp_dword - node->base)) < size)
747 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
752 split_node->base = node->base;
753 split_node->length = temp_dword - node->base;
754 node->base = temp_dword;
755 node->length -= split_node->length;
757 /* Put it in the list */
758 split_node->next = node->next;
759 node->next = split_node;
760 } /* End of non-aligned base */
762 /* Don't need to check if too small since we already did */
763 if (node->length > size) {
764 dbg("%s: too big\n", __FUNCTION__);
765 /* this one is longer than we need
766 so we'll make a new entry and split it up */
767 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
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 * shpchp_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 shpchp_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 * shpchp_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 *shpchp_slot_create(u8 busnumber)
882 struct pci_func *new_slot;
883 struct pci_func *next;
885 new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL);
887 if (new_slot == NULL) {
891 memset(new_slot, 0, sizeof(struct pci_func));
893 new_slot->next = NULL;
894 new_slot->configured = 1;
896 if (shpchp_slot_list[busnumber] == NULL) {
897 shpchp_slot_list[busnumber] = new_slot;
899 next = shpchp_slot_list[busnumber];
900 while (next->next != NULL)
902 next->next = new_slot;
909 * slot_remove - Removes a node from the linked list of slots.
910 * @old_slot: slot to remove
912 * Returns 0 if successful, !0 otherwise.
914 static int slot_remove(struct pci_func * old_slot)
916 struct pci_func *next;
918 if (old_slot == NULL)
921 next = shpchp_slot_list[old_slot->bus];
927 if (next == old_slot) {
928 shpchp_slot_list[old_slot->bus] = old_slot->next;
929 shpchp_destroy_board_resources(old_slot);
934 while ((next->next != old_slot) && (next->next != NULL)) {
938 if (next->next == old_slot) {
939 next->next = old_slot->next;
940 shpchp_destroy_board_resources(old_slot);
949 * bridge_slot_remove - Removes a node from the linked list of slots.
950 * @bridge: bridge to remove
952 * Returns 0 if successful, !0 otherwise.
954 static int bridge_slot_remove(struct pci_func *bridge)
956 u8 subordinateBus, secondaryBus;
958 struct pci_func *next;
963 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
964 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
966 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
967 next = shpchp_slot_list[tempBus];
969 while (!slot_remove(next)) {
970 next = shpchp_slot_list[tempBus];
974 next = shpchp_slot_list[bridge->bus];
980 if (next == bridge) {
981 shpchp_slot_list[bridge->bus] = bridge->next;
986 while ((next->next != bridge) && (next->next != NULL)) {
990 if (next->next == bridge) {
991 next->next = bridge->next;
1000 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1002 * @device: device to find
1003 * @index: is 0 for first function found, 1 for the second...
1005 * Returns pointer to the node if successful, %NULL otherwise.
1007 struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
1010 struct pci_func *func;
1012 func = shpchp_slot_list[bus];
1014 if ((func == NULL) || ((func->device == device) && (index == 0)))
1017 if (func->device == device)
1020 while (func->next != NULL) {
1023 if (func->device == device)
1033 static int is_bridge(struct pci_func * func)
1035 /* Check the header type */
1036 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1043 /* The following routines constitute the bulk of the
1044 hotplug controller logic
1049 * board_added - Called after a board has been added to the system.
1051 * Turns power on for the board
1055 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1058 u8 slots_not_empty = 0;
1060 u32 temp_register = 0xFFFFFFFF;
1062 struct pci_func *new_func = NULL;
1063 struct pci_func *t_func = NULL;
1064 struct slot *p_slot, *pslot;
1065 struct resource_lists res_lists;
1066 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
1069 p_slot = shpchp_find_slot(ctrl, func->device);
1070 hp_slot = func->device - ctrl->slot_device_offset;
1072 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1074 /* Wait for exclusive access to hardware */
1075 down(&ctrl->crit_sect);
1077 /* Power on slot without connecting to bus */
1078 rc = p_slot->hpc_ops->power_on_slot(p_slot);
1080 err("%s: Failed to power on slot\n", __FUNCTION__);
1081 /* Done with exclusive hardware access */
1082 up(&ctrl->crit_sect);
1086 /* Wait for the command to complete */
1087 wait_for_ctrl_irq (ctrl);
1089 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1091 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
1092 /* Done with exclusive hardware access */
1093 up(&ctrl->crit_sect);
1097 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
1098 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
1099 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
1100 /* 0xd = PCI-X 133 Mhz 533 */
1101 /* This encoding is different from the one used in cur_bus_speed & */
1104 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
1105 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
1106 /* Done with exclusive hardware access */
1107 up(&ctrl->crit_sect);
1108 return WRONG_BUS_FREQUENCY;
1111 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
1112 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
1113 err("%s: Can't get bus operation speed\n", __FUNCTION__);
1114 /* Done with exclusive hardware access */
1115 up(&ctrl->crit_sect);
1116 return WRONG_BUS_FREQUENCY;
1119 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1120 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1121 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1122 max_bus_speed = bus_speed;
1125 /* Done with exclusive hardware access */
1126 up(&ctrl->crit_sect);
1128 rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi);
1130 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
1134 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1135 if (slot != hp_slot) {
1136 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1137 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1138 slots_not_empty |= t_func->is_a_board;
1142 switch (adapter_speed) {
1143 case PCI_SPEED_133MHz_PCIX_533:
1144 case PCI_SPEED_133MHz_PCIX_266:
1145 if ((( bus_speed < 0xa ) || (bus_speed < 0xd)) && (max_bus_speed > bus_speed) &&
1146 ((max_bus_speed <= 0xa) || (max_bus_speed <= 0xd)) && (!slots_not_empty)) {
1148 /* Wait for exclusive access to hardware */
1149 down(&ctrl->crit_sect);
1151 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1153 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1154 /* Done with exclusive hardware access */
1155 up(&ctrl->crit_sect);
1156 return WRONG_BUS_FREQUENCY;
1159 /* Wait for the command to complete */
1160 wait_for_ctrl_irq (ctrl);
1162 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1164 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1166 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1167 /* Done with exclusive hardware access */
1168 up(&ctrl->crit_sect);
1169 return WRONG_BUS_FREQUENCY;
1171 /* Done with exclusive hardware access */
1172 up(&ctrl->crit_sect);
1175 case PCI_SPEED_133MHz_PCIX_ECC:
1176 case PCI_SPEED_133MHz_PCIX:
1178 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1181 err("%s: PI is 1 \n", __FUNCTION__);
1182 return WRONG_BUS_FREQUENCY;
1185 if (mode) { /* Bus - Mode 1 ECC */
1187 if (bus_speed > 0x7) {
1188 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1189 return WRONG_BUS_FREQUENCY;
1192 if ((bus_speed < 0x7) && (max_bus_speed <= 0x7) &&
1193 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1195 /* Wait for exclusive access to hardware */
1196 down(&ctrl->crit_sect);
1198 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1200 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1201 /* Done with exclusive hardware access */
1202 up(&ctrl->crit_sect);
1203 return WRONG_BUS_FREQUENCY;
1206 /* Wait for the command to complete */
1207 wait_for_ctrl_irq (ctrl);
1209 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1211 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1213 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1214 /* Done with exclusive hardware access */
1215 up(&ctrl->crit_sect);
1216 return WRONG_BUS_FREQUENCY;
1218 /* Done with exclusive hardware access */
1219 up(&ctrl->crit_sect);
1222 if (bus_speed > 0x4) {
1223 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1224 return WRONG_BUS_FREQUENCY;
1227 if ((bus_speed < 0x4) && (max_bus_speed <= 0x4) &&
1228 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1230 /* Wait for exclusive access to hardware */
1231 down(&ctrl->crit_sect);
1233 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1235 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1236 /* Done with exclusive hardware access */
1237 up(&ctrl->crit_sect);
1238 return WRONG_BUS_FREQUENCY;
1241 /* Wait for the command to complete */
1242 wait_for_ctrl_irq (ctrl);
1244 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1246 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1248 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1249 /* Done with exclusive hardware access */
1250 up(&ctrl->crit_sect);
1251 return WRONG_BUS_FREQUENCY;
1253 /* Done with exclusive hardware access */
1254 up(&ctrl->crit_sect);
1258 case PCI_SPEED_66MHz_PCIX_ECC:
1259 case PCI_SPEED_66MHz_PCIX:
1261 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1264 err("%s: PI is 1 \n", __FUNCTION__);
1265 return WRONG_BUS_FREQUENCY;
1268 if (mode) { /* Bus - Mode 1 ECC */
1270 if (bus_speed > 0x5) {
1271 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1272 return WRONG_BUS_FREQUENCY;
1275 if ((bus_speed < 0x5) && (max_bus_speed <= 0x5) &&
1276 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1278 /* Wait for exclusive access to hardware */
1279 down(&ctrl->crit_sect);
1281 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1283 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1284 /* Done with exclusive hardware access */
1285 up(&ctrl->crit_sect);
1286 return WRONG_BUS_FREQUENCY;
1289 /* Wait for the command to complete */
1290 wait_for_ctrl_irq (ctrl);
1292 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1294 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1296 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1297 /* Done with exclusive hardware access */
1298 up(&ctrl->crit_sect);
1299 return WRONG_BUS_FREQUENCY;
1301 /* Done with exclusive hardware access */
1302 up(&ctrl->crit_sect);
1305 if (bus_speed > 0x2) {
1306 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1307 return WRONG_BUS_FREQUENCY;
1310 if ((bus_speed < 0x2) && (max_bus_speed <= 0x2) &&
1311 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1313 /* Wait for exclusive access to hardware */
1314 down(&ctrl->crit_sect);
1316 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1318 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1319 /* Done with exclusive hardware access */
1320 up(&ctrl->crit_sect);
1321 return WRONG_BUS_FREQUENCY;
1324 /* Wait for the command to complete */
1325 wait_for_ctrl_irq (ctrl);
1327 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1329 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1331 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1332 /* Done with exclusive hardware access */
1333 up(&ctrl->crit_sect);
1334 return WRONG_BUS_FREQUENCY;
1336 /* Done with exclusive hardware access */
1337 up(&ctrl->crit_sect);
1341 case PCI_SPEED_66MHz:
1342 if (bus_speed > 0x1) {
1343 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1344 return WRONG_BUS_FREQUENCY;
1346 if (bus_speed == 0x1)
1348 if ((bus_speed == 0x0) && ( max_bus_speed == 0x1)) {
1349 /* Wait for exclusive access to hardware */
1350 down(&ctrl->crit_sect);
1352 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1354 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1355 /* Done with exclusive hardware access */
1356 up(&ctrl->crit_sect);
1357 return WRONG_BUS_FREQUENCY;
1360 /* Wait for the command to complete */
1361 wait_for_ctrl_irq (ctrl);
1363 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1365 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1367 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1368 /* Done with exclusive hardware access */
1369 up(&ctrl->crit_sect);
1370 return WRONG_BUS_FREQUENCY;
1372 /* Done with exclusive hardware access */
1373 up(&ctrl->crit_sect);
1376 case PCI_SPEED_33MHz:
1377 if (bus_speed > 0x0) {
1378 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1379 return WRONG_BUS_FREQUENCY;
1383 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1384 return WRONG_BUS_FREQUENCY;
1387 /* if adpater_speed == bus_speed, nothing to do here */
1388 if (adapter_speed != bus_speed) {
1389 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1390 if (slot != hp_slot) {
1391 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1392 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1393 slots_not_empty |= t_func->is_a_board;
1397 if (slots_not_empty != 0) { /* Other slots on the same bus are occupied */
1398 if ( adapter_speed < bus_speed ) {
1399 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1400 return WRONG_BUS_FREQUENCY;
1402 /* Do nothing if adapter_speed >= bus_speed */
1406 if ((adapter_speed != bus_speed) && (slots_not_empty == 0)) {
1407 /* Other slots on the same bus are empty */
1409 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1410 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1411 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1412 max_bus_speed = bus_speed;
1415 if (max_bus_speed == bus_speed) {
1416 /* if adapter_speed >= bus_speed, do nothing */
1417 if (adapter_speed < bus_speed) {
1419 * Try to lower bus speed to accommodate the adapter if other slots
1420 * on the same controller are empty
1423 /* Wait for exclusive access to hardware */
1424 down(&ctrl->crit_sect);
1426 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1428 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1429 return WRONG_BUS_FREQUENCY;
1432 /* Wait for the command to complete */
1433 wait_for_ctrl_irq (ctrl);
1435 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1437 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1439 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1440 return WRONG_BUS_FREQUENCY;
1442 /* Done with exclusive hardware access */
1443 up(&ctrl->crit_sect);
1447 /* Wait for exclusive access to hardware */
1448 down(&ctrl->crit_sect);
1450 /* max_bus_speed != bus_speed. Note: max_bus_speed should be > than bus_speed */
1451 if (adapter_speed < max_bus_speed)
1452 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1454 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1457 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1458 /* Done with exclusive hardware access */
1459 up(&ctrl->crit_sect);
1460 return WRONG_BUS_FREQUENCY;
1463 /* Wait for the command to complete */
1464 wait_for_ctrl_irq (ctrl);
1466 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1468 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1470 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1471 /* Done with exclusive hardware access */
1472 up(&ctrl->crit_sect);
1473 return WRONG_BUS_FREQUENCY;
1475 /* Done with exclusive hardware access */
1476 up(&ctrl->crit_sect);
1482 /* Wait for exclusive access to hardware */
1483 down(&ctrl->crit_sect);
1485 /* turn on board, blink green LED, turn off Amber LED */
1486 rc = p_slot->hpc_ops->slot_enable(p_slot);
1489 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1490 /* Done with exclusive hardware access */
1491 up(&ctrl->crit_sect);
1494 /* Wait for the command to complete */
1495 wait_for_ctrl_irq (ctrl);
1497 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1499 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1500 /* Done with exclusive hardware access */
1501 up(&ctrl->crit_sect);
1505 /* Done with exclusive hardware access */
1506 up(&ctrl->crit_sect);
1508 /* Wait for ~1 second */
1509 dbg("%s: before long_delay\n", __FUNCTION__);
1510 wait_for_ctrl_irq (ctrl);
1511 dbg("%s: afterlong_delay\n", __FUNCTION__);
1513 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1514 /* Check for a power fault */
1515 if (func->status == 0xFF) {
1516 /* power fault occurred, but it was benign */
1517 temp_register = 0xFFFFFFFF;
1518 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1522 /* Get vendor/device ID u32 */
1523 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
1524 PCI_VENDOR_ID, &temp_register);
1525 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1526 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1529 /* Something's wrong here */
1530 temp_register = 0xFFFFFFFF;
1531 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1533 /* Preset return code. It will be changed later if things go okay. */
1534 rc = NO_ADAPTER_PRESENT;
1537 /* All F's is an empty slot or an invalid board */
1538 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1539 res_lists.io_head = ctrl->io_head;
1540 res_lists.mem_head = ctrl->mem_head;
1541 res_lists.p_mem_head = ctrl->p_mem_head;
1542 res_lists.bus_head = ctrl->bus_head;
1543 res_lists.irqs = NULL;
1545 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1546 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1548 ctrl->io_head = res_lists.io_head;
1549 ctrl->mem_head = res_lists.mem_head;
1550 ctrl->p_mem_head = res_lists.p_mem_head;
1551 ctrl->bus_head = res_lists.bus_head;
1553 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1554 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1555 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1556 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1559 /* Wait for exclusive access to hardware */
1560 down(&ctrl->crit_sect);
1562 /* turn off slot, turn on Amber LED, turn off Green LED */
1563 retval = p_slot->hpc_ops->slot_disable(p_slot);
1565 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1566 /* Done with exclusive hardware access */
1567 up(&ctrl->crit_sect);
1570 /* Wait for the command to complete */
1571 wait_for_ctrl_irq (ctrl);
1573 retval = p_slot->hpc_ops->check_cmd_status(ctrl);
1575 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1576 /* Done with exclusive hardware access */
1577 up(&ctrl->crit_sect);
1581 /* Done with exclusive hardware access */
1582 up(&ctrl->crit_sect);
1586 shpchp_save_slot_config(ctrl, func);
1589 func->switch_save = 0x10;
1590 func->is_a_board = 0x01;
1592 /* next, we will instantiate the linux pci_dev structures
1593 * (with appropriate driver notification, if already present)
1597 new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
1598 if (new_func && !new_func->pci_dev) {
1599 dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
1600 shpchp_configure_device(ctrl, new_func);
1604 /* Wait for exclusive access to hardware */
1605 down(&ctrl->crit_sect);
1607 p_slot->hpc_ops->green_led_on(p_slot);
1609 /* Wait for the command to complete */
1610 wait_for_ctrl_irq (ctrl);
1613 /* Done with exclusive hardware access */
1614 up(&ctrl->crit_sect);
1617 /* Wait for exclusive access to hardware */
1618 down(&ctrl->crit_sect);
1620 /* turn off slot, turn on Amber LED, turn off Green LED */
1621 rc = p_slot->hpc_ops->slot_disable(p_slot);
1623 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1624 /* Done with exclusive hardware access */
1625 up(&ctrl->crit_sect);
1628 /* Wait for the command to complete */
1629 wait_for_ctrl_irq (ctrl);
1631 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1633 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1634 /* Done with exclusive hardware access */
1635 up(&ctrl->crit_sect);
1639 /* Done with exclusive hardware access */
1640 up(&ctrl->crit_sect);
1649 * remove_board - Turns off slot and LED's
1652 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1659 struct resource_lists res_lists;
1660 struct pci_func *temp_func;
1661 struct slot *p_slot;
1666 if (shpchp_unconfigure_device(func))
1669 device = func->device;
1671 hp_slot = func->device - ctrl->slot_device_offset;
1672 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1674 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1676 if ((ctrl->add_support) &&
1677 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1678 /* Here we check to see if we've saved any of the board's
1679 * resources already. If so, we'll skip the attempt to
1680 * determine what's being used.
1686 while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
1687 if (temp_func->bus_head || temp_func->mem_head
1688 || temp_func->p_mem_head || temp_func->io_head) {
1695 rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
1697 /* Change status to shutdown */
1698 if (func->is_a_board)
1699 func->status = 0x01;
1700 func->configured = 0;
1702 /* Wait for exclusive access to hardware */
1703 down(&ctrl->crit_sect);
1705 /* turn off slot, turn on Amber LED, turn off Green LED */
1706 rc = p_slot->hpc_ops->slot_disable(p_slot);
1708 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1709 /* Done with exclusive hardware access */
1710 up(&ctrl->crit_sect);
1713 /* Wait for the command to complete */
1714 wait_for_ctrl_irq (ctrl);
1716 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1718 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1719 /* Done with exclusive hardware access */
1720 up(&ctrl->crit_sect);
1724 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
1726 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
1727 /* Done with exclusive hardware access */
1728 up(&ctrl->crit_sect);
1731 /* Wait for the command to complete */
1732 wait_for_ctrl_irq (ctrl);
1734 /* Done with exclusive hardware access */
1735 up(&ctrl->crit_sect);
1737 if (ctrl->add_support) {
1739 res_lists.io_head = ctrl->io_head;
1740 res_lists.mem_head = ctrl->mem_head;
1741 res_lists.p_mem_head = ctrl->p_mem_head;
1742 res_lists.bus_head = ctrl->bus_head;
1744 dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", func->bus,
1745 func->device, func->function);
1747 shpchp_return_board_resources(func, &res_lists);
1749 ctrl->io_head = res_lists.io_head;
1750 ctrl->mem_head = res_lists.mem_head;
1751 ctrl->p_mem_head = res_lists.p_mem_head;
1752 ctrl->bus_head = res_lists.bus_head;
1754 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1755 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1756 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1757 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1759 if (is_bridge(func)) {
1760 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1761 func->device, func->function);
1762 bridge_slot_remove(func);
1764 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1765 func->device, func->function);
1768 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
1771 /* Setup slot structure with entry for empty slot */
1772 func = shpchp_slot_create(ctrl->slot_bus);
1778 func->bus = ctrl->slot_bus;
1779 func->device = device;
1781 func->configured = 0;
1782 func->switch_save = 0x10;
1783 func->is_a_board = 0;
1790 static void pushbutton_helper_thread (unsigned long data)
1792 pushbutton_pending = data;
1794 up(&event_semaphore);
1798 /* this is the main worker thread */
1799 static int event_thread(void* data)
1801 struct controller *ctrl;
1803 daemonize("shpchpd_event");
1807 dbg("!!!!event_thread sleeping\n");
1808 down_interruptible (&event_semaphore);
1809 dbg("event_thread woken finished = %d\n", event_finished);
1810 if (event_finished || signal_pending(current))
1813 if (pushbutton_pending)
1814 shpchp_pushbutton_thread(pushbutton_pending);
1816 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
1817 interrupt_event_handler(ctrl);
1819 dbg("event_thread signals exit\n");
1824 int shpchp_event_start_thread (void)
1828 /* initialize our semaphores */
1829 init_MUTEX_LOCKED(&event_exit);
1832 init_MUTEX_LOCKED(&event_semaphore);
1833 pid = kernel_thread(event_thread, 0, 0);
1836 err ("Can't start up our event thread\n");
1839 dbg("Our event thread pid = %d\n", pid);
1844 void shpchp_event_stop_thread (void)
1847 dbg("event_thread finish command given\n");
1848 up(&event_semaphore);
1849 dbg("wait for event_thread to exit\n");
1854 static int update_slot_info (struct slot *slot)
1856 struct hotplug_slot_info *info;
1859 info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
1863 slot->hpc_ops->get_power_status(slot, &(info->power_status));
1864 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1865 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1866 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1868 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1873 static void interrupt_event_handler(struct controller *ctrl)
1877 struct pci_func *func;
1880 struct slot *p_slot;
1882 dbg("%s:\n", __FUNCTION__);
1886 for (loop = 0; loop < 10; loop++) {
1887 if (ctrl->event_queue[loop].event_type != 0) {
1888 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1889 ctrl->event_queue[loop].event_type);
1890 hp_slot = ctrl->event_queue[loop].hp_slot;
1892 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1894 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1896 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1898 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1899 dbg("%s: button cancel\n", __FUNCTION__);
1900 del_timer(&p_slot->task_event);
1902 switch (p_slot->state) {
1903 case BLINKINGOFF_STATE:
1904 /* Wait for exclusive access to hardware */
1905 down(&ctrl->crit_sect);
1907 p_slot->hpc_ops->green_led_on(p_slot);
1908 /* Wait for the command to complete */
1909 wait_for_ctrl_irq (ctrl);
1911 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1913 /* Wait for the command to complete */
1914 wait_for_ctrl_irq (ctrl);
1916 /* Done with exclusive hardware access */
1917 up(&ctrl->crit_sect);
1919 case BLINKINGON_STATE:
1920 /* Wait for exclusive access to hardware */
1921 down(&ctrl->crit_sect);
1923 p_slot->hpc_ops->green_led_off(p_slot);
1924 /* Wait for the command to complete */
1925 wait_for_ctrl_irq (ctrl);
1927 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1928 /* Wait for the command to complete */
1929 wait_for_ctrl_irq (ctrl);
1931 /* Done with exclusive hardware access */
1932 up(&ctrl->crit_sect);
1936 warn("Not a valid state\n");
1939 info(msg_button_cancel, p_slot->number);
1940 p_slot->state = STATIC_STATE;
1941 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1942 /* Button Pressed (No action on 1st press...) */
1943 dbg("%s: Button pressed\n", __FUNCTION__);
1945 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1948 dbg("%s: slot is on\n", __FUNCTION__);
1949 p_slot->state = BLINKINGOFF_STATE;
1950 info(msg_button_off, p_slot->number);
1953 dbg("%s: slot is off\n", __FUNCTION__);
1954 p_slot->state = BLINKINGON_STATE;
1955 info(msg_button_on, p_slot->number);
1958 /* Wait for exclusive access to hardware */
1959 down(&ctrl->crit_sect);
1961 /* blink green LED and turn off amber */
1962 p_slot->hpc_ops->green_led_blink(p_slot);
1963 /* Wait for the command to complete */
1964 wait_for_ctrl_irq (ctrl);
1966 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1968 /* Wait for the command to complete */
1969 wait_for_ctrl_irq (ctrl);
1971 /* Done with exclusive hardware access */
1972 up(&ctrl->crit_sect);
1974 init_timer(&p_slot->task_event);
1975 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1976 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1977 p_slot->task_event.data = (unsigned long) p_slot;
1979 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
1980 add_timer(&p_slot->task_event);
1981 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1982 /***********POWER FAULT********************/
1983 dbg("%s: power fault\n", __FUNCTION__);
1984 /* Wait for exclusive access to hardware */
1985 down(&ctrl->crit_sect);
1987 p_slot->hpc_ops->set_attention_status(p_slot, 1);
1988 /* Wait for the command to complete */
1989 wait_for_ctrl_irq (ctrl);
1991 p_slot->hpc_ops->green_led_off(p_slot);
1992 /* Wait for the command to complete */
1993 wait_for_ctrl_irq (ctrl);
1995 /* Done with exclusive hardware access */
1996 up(&ctrl->crit_sect);
1998 /* refresh notification */
2000 update_slot_info(p_slot);
2003 ctrl->event_queue[loop].event_type = 0;
2007 } /* End of FOR loop */
2015 * shpchp_pushbutton_thread
2017 * Scheduled procedure to handle blocking stuff for the pushbuttons
2018 * Handles all pending events and exits.
2021 void shpchp_pushbutton_thread (unsigned long slot)
2023 struct slot *p_slot = (struct slot *) slot;
2027 pushbutton_pending = 0;
2030 dbg("%s: Error! slot NULL\n", __FUNCTION__);
2034 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2036 p_slot->state = POWEROFF_STATE;
2037 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
2039 if (shpchp_disable_slot(p_slot)) {
2040 /* Wait for exclusive access to hardware */
2041 down(&p_slot->ctrl->crit_sect);
2043 /* Turn on the Attention LED */
2044 rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
2046 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
2050 /* Wait for the command to complete */
2051 wait_for_ctrl_irq (p_slot->ctrl);
2053 /* Done with exclusive hardware access */
2054 up(&p_slot->ctrl->crit_sect);
2056 p_slot->state = STATIC_STATE;
2058 p_slot->state = POWERON_STATE;
2059 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
2061 if (shpchp_enable_slot(p_slot)) {
2062 /* Wait for exclusive access to hardware */
2063 down(&p_slot->ctrl->crit_sect);
2065 /* Turn off the green LED */
2066 rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
2068 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
2071 /* Wait for the command to complete */
2072 wait_for_ctrl_irq (p_slot->ctrl);
2074 p_slot->hpc_ops->green_led_off(p_slot);
2076 /* Wait for the command to complete */
2077 wait_for_ctrl_irq (p_slot->ctrl);
2079 /* Done with exclusive hardware access */
2080 up(&p_slot->ctrl->crit_sect);
2082 p_slot->state = STATIC_STATE;
2089 int shpchp_enable_slot (struct slot *p_slot)
2093 struct pci_func *func;
2095 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2097 dbg("%s: Error! slot NULL\n", __FUNCTION__);
2101 /* Check to see if (latch closed, card present, power off) */
2102 down(&p_slot->ctrl->crit_sect);
2103 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2104 if (rc || !getstatus) {
2105 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2106 up(&p_slot->ctrl->crit_sect);
2109 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2110 if (rc || getstatus) {
2111 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2112 up(&p_slot->ctrl->crit_sect);
2115 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2116 if (rc || getstatus) {
2117 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
2118 up(&p_slot->ctrl->crit_sect);
2121 up(&p_slot->ctrl->crit_sect);
2125 func = shpchp_slot_create(p_slot->bus);
2129 func->bus = p_slot->bus;
2130 func->device = p_slot->device;
2132 func->configured = 0;
2133 func->is_a_board = 1;
2135 /* We have to save the presence info for these slots */
2136 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2137 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2138 func->switch_save = !getstatus? 0x10:0;
2140 rc = board_added(func, p_slot->ctrl);
2142 if (is_bridge(func))
2143 bridge_slot_remove(func);
2147 /* Setup slot structure with entry for empty slot */
2148 func = shpchp_slot_create(p_slot->bus);
2150 return (1); /* Out of memory */
2152 func->bus = p_slot->bus;
2153 func->device = p_slot->device;
2155 func->configured = 0;
2156 func->is_a_board = 1;
2158 /* We have to save the presence info for these slots */
2159 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2160 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2161 func->switch_save = !getstatus? 0x10:0;
2165 update_slot_info(p_slot);
2171 int shpchp_disable_slot (struct slot *p_slot)
2173 u8 class_code, header_type, BCR;
2179 struct pci_bus *pci_bus = p_slot->ctrl->pci_dev->subordinate;
2180 struct pci_func *func;
2185 /* Check to see if (latch closed, card present, power on) */
2186 down(&p_slot->ctrl->crit_sect);
2188 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2189 if (ret || !getstatus) {
2190 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2191 up(&p_slot->ctrl->crit_sect);
2194 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2195 if (ret || getstatus) {
2196 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2197 up(&p_slot->ctrl->crit_sect);
2200 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2201 if (ret || !getstatus) {
2202 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
2203 up(&p_slot->ctrl->crit_sect);
2206 up(&p_slot->ctrl->crit_sect);
2208 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2210 /* Make sure there are no video controllers here
2211 * for all func of p_slot
2213 while (func && !rc) {
2214 pci_bus->number = func->bus;
2215 devfn = PCI_DEVFN(func->device, func->function);
2217 /* Check the Class Code */
2218 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2222 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2223 /* Display/Video adapter (not supported) */
2224 rc = REMOVE_NOT_SUPPORTED;
2226 /* See if it's a bridge */
2227 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2231 /* If it's a bridge, check the VGA Enable bit */
2232 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2233 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2237 /* If the VGA Enable bit is set, remove isn't supported */
2238 if (BCR & PCI_BRIDGE_CTL_VGA) {
2239 rc = REMOVE_NOT_SUPPORTED;
2244 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2247 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2248 if ((func != NULL) && !rc) {
2249 rc = remove_board(func, p_slot->ctrl);
2254 update_slot_info(p_slot);
2261 * configure_new_device - Configures the PCI header information of one board.
2263 * @ctrl: pointer to controller structure
2264 * @func: pointer to function structure
2265 * @behind_bridge: 1 if this is a recursive call, 0 if not
2266 * @resources: pointer to set of resource lists
2268 * 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, u8 bridge_bus, u8 bridge_dev)
2274 u8 temp_byte, function, max_functions, stop_it;
2277 struct pci_func *new_slot;
2278 struct pci_bus lpci_bus, *pci_bus;
2283 dbg("%s\n", __FUNCTION__);
2284 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2285 pci_bus = &lpci_bus;
2286 pci_bus->number = func->bus;
2288 /* Check for Multi-function device */
2289 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2291 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2295 if (temp_byte & 0x80) /* Multi-function device */
2303 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
2306 dbg("configure_new_function failed %d\n",rc);
2310 new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
2313 shpchp_return_board_resources(new_slot, resources);
2323 /* The following loop skips to the next present function
2324 * and creates a board structure
2327 while ((function < max_functions) && (!stop_it)) {
2328 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2330 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2332 } else { /* There's something there */
2333 /* Setup slot structure. */
2334 new_slot = shpchp_slot_create(func->bus);
2336 if (new_slot == NULL) {
2341 new_slot->bus = func->bus;
2342 new_slot->device = func->device;
2343 new_slot->function = function;
2344 new_slot->is_a_board = 1;
2345 new_slot->status = 0;
2351 } while (function < max_functions);
2352 dbg("returning from configure_new_device\n");
2359 * Configuration logic that involves the hotplug data structures and
2365 * configure_new_function - Configures the PCI header information of one device
2367 * @ctrl: pointer to controller structure
2368 * @func: pointer to function structure
2369 * @behind_bridge: 1 if this is a recursive call, 0 if not
2370 * @resources: pointer to set of resource lists
2372 * Calls itself recursively for bridged devices.
2373 * Returns 0 if success
2376 static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2377 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
2389 struct pci_resource *mem_node;
2390 struct pci_resource *p_mem_node;
2391 struct pci_resource *io_node;
2392 struct pci_resource *bus_node;
2393 struct pci_resource *hold_mem_node;
2394 struct pci_resource *hold_p_mem_node;
2395 struct pci_resource *hold_IO_node;
2396 struct pci_resource *hold_bus_node;
2397 struct irq_mapping irqs;
2398 struct pci_func *new_slot;
2399 struct pci_bus lpci_bus, *pci_bus;
2400 struct resource_lists temp_resources;
2401 #if defined(CONFIG_X86_64)
2405 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2406 pci_bus = &lpci_bus;
2407 pci_bus->number = func->bus;
2408 devfn = PCI_DEVFN(func->device, func->function);
2410 /* Check for Bridge */
2411 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2415 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2416 /* set Primary bus */
2417 dbg("set Primary bus = 0x%x\n", func->bus);
2418 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2422 /* find range of busses to use */
2423 bus_node = get_max_resource(&resources->bus_head, 1L);
2425 /* If we don't have any busses to allocate, we can't continue */
2427 err("Got NO bus resource to use\n");
2430 dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2432 /* set Secondary bus */
2433 temp_byte = (u8)bus_node->base;
2434 dbg("set Secondary bus = 0x%x\n", temp_byte);
2435 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2439 /* set subordinate bus */
2440 temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2441 dbg("set subordinate bus = 0x%x\n", temp_byte);
2442 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2446 /* Set HP parameters (Cache Line Size, Latency Timer) */
2447 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2451 /* Setup the IO, memory, and prefetchable windows */
2453 io_node = get_max_resource(&(resources->io_head), 0x1000L);
2455 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2458 mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2460 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2463 if (resources->p_mem_head)
2464 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2467 * In some platform implementation, MEM and PMEM are not
2468 * distinguished, and hence ACPI _CRS has only MEM entries
2469 * for both MEM and PMEM.
2471 dbg("using MEM for PMEM\n");
2472 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2475 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2478 /* set up the IRQ info */
2479 if (!resources->irqs) {
2480 irqs.barber_pole = 0;
2481 irqs.interrupt[0] = 0;
2482 irqs.interrupt[1] = 0;
2483 irqs.interrupt[2] = 0;
2484 irqs.interrupt[3] = 0;
2487 irqs.barber_pole = resources->irqs->barber_pole;
2488 irqs.interrupt[0] = resources->irqs->interrupt[0];
2489 irqs.interrupt[1] = resources->irqs->interrupt[1];
2490 irqs.interrupt[2] = resources->irqs->interrupt[2];
2491 irqs.interrupt[3] = resources->irqs->interrupt[3];
2492 irqs.valid_INT = resources->irqs->valid_INT;
2495 /* set up resource lists that are now aligned on top and bottom
2496 * for anything behind the bridge.
2498 temp_resources.bus_head = bus_node;
2499 temp_resources.io_head = io_node;
2500 temp_resources.mem_head = mem_node;
2501 temp_resources.p_mem_head = p_mem_node;
2502 temp_resources.irqs = &irqs;
2504 /* Make copies of the nodes we are going to pass down so that
2505 * if there is a problem,we can just use these to free resources
2507 hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2508 hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2509 hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2510 hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2512 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2514 kfree(hold_bus_node);
2516 kfree(hold_IO_node);
2518 kfree(hold_mem_node);
2519 if (hold_p_mem_node)
2520 kfree(hold_p_mem_node);
2525 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2527 bus_node->base += 1;
2528 bus_node->length -= 1;
2529 bus_node->next = NULL;
2531 /* If we have IO resources copy them and fill in the bridge's
2532 * IO range registers
2535 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2536 io_node->next = NULL;
2538 /* set IO base and Limit registers */
2539 RES_CHECK(io_node->base, 8);
2540 temp_byte = (u8)(io_node->base >> 8);
2541 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2543 RES_CHECK(io_node->base + io_node->length - 1, 8);
2544 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2545 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2547 kfree(hold_IO_node);
2548 hold_IO_node = NULL;
2551 /* If we have memory resources copy them and fill in the bridge's
2552 * memory range registers. Otherwise, fill in the range
2553 * registers with values that disable them.
2556 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2557 mem_node->next = NULL;
2559 /* set Mem base and Limit registers */
2560 RES_CHECK(mem_node->base, 16);
2561 temp_word = (u32)(mem_node->base >> 16);
2562 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2564 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
2565 temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
2566 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2569 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2572 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2574 kfree(hold_mem_node);
2575 hold_mem_node = NULL;
2578 /* If we have prefetchable memory resources copy them and
2579 * fill in the bridge's memory range registers. Otherwise,
2580 * fill in the range registers with values that disable them.
2583 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2584 p_mem_node->next = NULL;
2586 /* set Pre Mem base and Limit registers */
2587 RES_CHECK(p_mem_node->base, 16);
2588 temp_word = (u32)(p_mem_node->base >> 16);
2589 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2591 RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
2592 temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
2593 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2596 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2599 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2601 kfree(hold_p_mem_node);
2602 hold_p_mem_node = NULL;
2605 /* Adjust this to compensate for extra adjustment in first loop */
2610 /* Here we actually find the devices and configure them */
2611 for (device = 0; (device <= 0x1F) && !rc; device++) {
2612 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2615 pci_bus->number = hold_bus_node->base;
2616 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
2617 pci_bus->number = func->bus;
2619 if (ID != 0xFFFFFFFF) { /* device Present */
2620 /* Setup slot structure. */
2621 new_slot = shpchp_slot_create(hold_bus_node->base);
2623 if (new_slot == NULL) {
2629 new_slot->bus = hold_bus_node->base;
2630 new_slot->device = device;
2631 new_slot->function = 0;
2632 new_slot->is_a_board = 1;
2633 new_slot->status = 0;
2635 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
2636 dbg("configure_new_device rc=0x%x\n",rc);
2637 } /* End of IF (device in slot?) */
2638 } /* End of FOR loop */
2641 shpchp_destroy_resource_list(&temp_resources);
2643 return_resource(&(resources->bus_head), hold_bus_node);
2644 return_resource(&(resources->io_head), hold_IO_node);
2645 return_resource(&(resources->mem_head), hold_mem_node);
2646 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2650 /* save the interrupt routing information */
2651 if (resources->irqs) {
2652 resources->irqs->interrupt[0] = irqs.interrupt[0];
2653 resources->irqs->interrupt[1] = irqs.interrupt[1];
2654 resources->irqs->interrupt[2] = irqs.interrupt[2];
2655 resources->irqs->interrupt[3] = irqs.interrupt[3];
2656 resources->irqs->valid_INT = irqs.valid_INT;
2657 } else if (!behind_bridge) {
2658 /* We need to hook up the interrupts here */
2659 for (cloop = 0; cloop < 4; cloop++) {
2660 if (irqs.valid_INT & (0x01 << cloop)) {
2661 rc = shpchp_set_irq(func->bus, func->device,
2662 0x0A + cloop, irqs.interrupt[cloop]);
2664 shpchp_destroy_resource_list (&temp_resources);
2665 return_resource(&(resources->bus_head), hold_bus_node);
2666 return_resource(&(resources->io_head), hold_IO_node);
2667 return_resource(&(resources->mem_head), hold_mem_node);
2668 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2672 } /* end of for loop */
2675 /* Return unused bus resources
2676 * First use the temporary node to store information for the board
2678 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2679 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2681 hold_bus_node->next = func->bus_head;
2682 func->bus_head = hold_bus_node;
2684 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2686 /* set subordinate bus */
2687 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2688 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2690 if (temp_resources.bus_head->length == 0) {
2691 kfree(temp_resources.bus_head);
2692 temp_resources.bus_head = NULL;
2694 dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2695 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2696 return_resource(&(resources->bus_head), temp_resources.bus_head);
2700 /* If we have IO space available and there is some left,
2701 * return the unused portion
2703 if (hold_IO_node && temp_resources.io_head) {
2704 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2705 &hold_IO_node, 0x1000);
2707 /* Check if we were able to split something off */
2709 hold_IO_node->base = io_node->base + io_node->length;
2711 RES_CHECK(hold_IO_node->base, 8);
2712 temp_byte = (u8)((hold_IO_node->base) >> 8);
2713 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2715 return_resource(&(resources->io_head), io_node);
2718 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2720 /* Check if we were able to split something off */
2722 /* First use the temporary node to store information for the board */
2723 hold_IO_node->length = io_node->base - hold_IO_node->base;
2725 /* If we used any, add it to the board's list */
2726 if (hold_IO_node->length) {
2727 hold_IO_node->next = func->io_head;
2728 func->io_head = hold_IO_node;
2730 RES_CHECK(io_node->base - 1, 8);
2731 temp_byte = (u8)((io_node->base - 1) >> 8);
2732 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2734 return_resource(&(resources->io_head), io_node);
2736 /* it doesn't need any IO */
2738 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2740 return_resource(&(resources->io_head), io_node);
2741 kfree(hold_IO_node);
2744 /* it used most of the range */
2745 hold_IO_node->next = func->io_head;
2746 func->io_head = hold_IO_node;
2748 } else if (hold_IO_node) {
2749 /* it used the whole range */
2750 hold_IO_node->next = func->io_head;
2751 func->io_head = hold_IO_node;
2754 /* If we have memory space available and there is some left,
2755 * return the unused portion
2757 if (hold_mem_node && temp_resources.mem_head) {
2758 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2760 /* Check if we were able to split something off */
2762 hold_mem_node->base = mem_node->base + mem_node->length;
2764 RES_CHECK(hold_mem_node->base, 16);
2765 temp_word = (u32)((hold_mem_node->base) >> 16);
2766 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2768 return_resource(&(resources->mem_head), mem_node);
2771 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2773 /* Check if we were able to split something off */
2775 /* First use the temporary node to store information for the board */
2776 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2778 if (hold_mem_node->length) {
2779 hold_mem_node->next = func->mem_head;
2780 func->mem_head = hold_mem_node;
2782 /* configure end address */
2783 RES_CHECK(mem_node->base - 1, 16);
2784 temp_word = (u32)((mem_node->base - 1) >> 16);
2785 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2787 /* Return unused resources to the pool */
2788 return_resource(&(resources->mem_head), mem_node);
2790 /* it doesn't need any Mem */
2792 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2794 return_resource(&(resources->mem_head), mem_node);
2795 kfree(hold_mem_node);
2798 /* it used most of the range */
2799 hold_mem_node->next = func->mem_head;
2800 func->mem_head = hold_mem_node;
2802 } else if (hold_mem_node) {
2803 /* it used the whole range */
2804 hold_mem_node->next = func->mem_head;
2805 func->mem_head = hold_mem_node;
2808 /* If we have prefetchable memory space available and there is some
2809 * left at the end, return the unused portion
2811 if (hold_p_mem_node && temp_resources.p_mem_head) {
2812 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2813 &hold_p_mem_node, 0x100000L);
2815 /* Check if we were able to split something off */
2817 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2819 RES_CHECK(hold_p_mem_node->base, 16);
2820 temp_word = (u32)((hold_p_mem_node->base) >> 16);
2821 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2823 return_resource(&(resources->p_mem_head), p_mem_node);
2826 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2828 /* Check if we were able to split something off */
2830 /* First use the temporary node to store information for the board */
2831 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2833 /* If we used any, add it to the board's list */
2834 if (hold_p_mem_node->length) {
2835 hold_p_mem_node->next = func->p_mem_head;
2836 func->p_mem_head = hold_p_mem_node;
2838 RES_CHECK(p_mem_node->base - 1, 16);
2839 temp_word = (u32)((p_mem_node->base - 1) >> 16);
2840 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2842 return_resource(&(resources->p_mem_head), p_mem_node);
2844 /* it doesn't need any PMem */
2846 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2848 return_resource(&(resources->p_mem_head), p_mem_node);
2849 kfree(hold_p_mem_node);
2852 /* it used the most of the range */
2853 hold_p_mem_node->next = func->p_mem_head;
2854 func->p_mem_head = hold_p_mem_node;
2856 } else if (hold_p_mem_node) {
2857 /* it used the whole range */
2858 hold_p_mem_node->next = func->p_mem_head;
2859 func->p_mem_head = hold_p_mem_node;
2862 /* We should be configuring an IRQ and the bridge's base address
2863 * registers if it needs them. Although we have never seen such
2867 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2869 dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2870 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2871 /* Standard device */
2873 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2875 if (class_code == PCI_BASE_CLASS_DISPLAY)
2876 return (DEVICE_TYPE_NOT_SUPPORTED);
2878 /* Figure out IO and memory needs */
2879 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2880 temp_register = 0xFFFFFFFF;
2882 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2883 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2884 dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, func->bus, func->device,
2891 if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2894 /* set base = amount of IO space */
2895 base = temp_register & 0xFFFFFFFC;
2898 dbg("NEED IO length(0x%x)\n", base);
2899 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2901 /* allocate the resource to the board */
2903 dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2904 base = (u32)io_node->base;
2905 io_node->next = func->io_head;
2906 func->io_head = io_node;
2908 err("Got NO IO resource(length=0x%x)\n", base);
2911 } else { /* map MEM */
2912 int prefetchable = 1;
2913 struct pci_resource **res_node = &func->p_mem_head;
2914 char *res_type_str = "PMEM";
2917 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2919 res_node = &func->mem_head;
2923 base = temp_register & 0xFFFFFFF0;
2926 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2927 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2928 dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2930 if (prefetchable && resources->p_mem_head)
2931 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2934 dbg("using MEM for PMEM\n");
2935 mem_node=get_resource(&(resources->mem_head), (ulong)base);
2938 /* allocate the resource to the board */
2940 base = (u32)mem_node->base;
2941 mem_node->next = *res_node;
2942 *res_node = mem_node;
2943 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
2946 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2950 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2951 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2952 dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
2953 temp_register, base);
2955 if (prefetchable && resources->p_mem_head)
2956 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2959 dbg("using MEM for PMEM\n");
2960 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2963 /* allocate the resource to the board */
2965 base64 = mem_node->base;
2966 mem_node->next = *res_node;
2967 *res_node = mem_node;
2968 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
2969 (u32)base64, mem_node->length);
2971 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2976 dbg("reserved BAR type=0x%x\n", temp_register);
2983 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2988 dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2992 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2994 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2996 } /* End of base register loop */
2998 #if defined(CONFIG_X86_64)
2999 /* Figure out which interrupt pin this function uses */
3000 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
3002 /* If this function needs an interrupt and we are behind a bridge
3003 and the pin is tied to something that's alread mapped,
3004 set this one the same
3006 if (temp_byte && resources->irqs &&
3007 (resources->irqs->valid_INT &
3008 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3009 /* We have to share with something already set up */
3010 IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
3012 /* Program IRQ based on card type */
3013 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3015 if (class_code == PCI_BASE_CLASS_STORAGE) {
3016 IRQ = shpchp_disk_irq;
3018 IRQ = shpchp_nic_irq;
3023 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3025 if (!behind_bridge) {
3026 rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3030 /* TBD - this code may also belong in the other clause of this If statement */
3031 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3032 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3035 /* Disable ROM base Address */
3037 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
3039 /* Set HP parameters (Cache Line Size, Latency Timer) */
3040 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3044 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3046 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
3047 } /* End of Not-A-Bridge else */
3049 /* It's some strange type of PCI adapter (Cardbus?) */
3050 return(DEVICE_TYPE_NOT_SUPPORTED);
3053 func->configured = 1;