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 = kmalloc(sizeof(*split_node), 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 = kmalloc(sizeof(*split_node), 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 = kmalloc(sizeof(*split_node), 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 = kmalloc(sizeof(*split_node), 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 = kmalloc(sizeof(*split_node), 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 = kmalloc(sizeof(*split_node),
675 break; /* return (NULL); */
676 split_node->base = max->base + max_size[i];
677 split_node->length = max->length - max_size[i];
678 max->length = max_size[i];
679 /* Put it next in the list */
680 split_node->next = max->next;
681 max->next = split_node;
686 /* Now take it out of the list */
687 temp = (struct pci_resource*) *head;
691 while (temp && temp->next != max) {
695 temp->next = max->next;
702 /* If we get here, we couldn't find one */
710 * this function sorts the resource list by size and then
711 * returns the first node of "size" length. If it finds a node
712 * larger than "size" it will split it up.
714 * size must be a power of two.
716 static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
718 struct pci_resource *prevnode;
719 struct pci_resource *node;
720 struct pci_resource *split_node;
726 if ( shpchp_resource_sort_and_combine(head) )
729 if ( sort_by_size(head) )
732 for (node = *head; node; node = node->next) {
733 dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
734 __FUNCTION__, size, node, node->base, node->length);
735 if (node->length < size)
738 if (node->base & (size - 1)) {
739 dbg("%s: not aligned\n", __FUNCTION__);
740 /* this one isn't base aligned properly
741 so we'll make a new entry and split it up */
742 temp_dword = (node->base | (size-1)) + 1;
744 /* Short circuit if adjusted size is too small */
745 if ((node->length - (temp_dword - node->base)) < size)
748 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
753 split_node->base = node->base;
754 split_node->length = temp_dword - node->base;
755 node->base = temp_dword;
756 node->length -= split_node->length;
758 /* Put it in the list */
759 split_node->next = node->next;
760 node->next = split_node;
761 } /* End of non-aligned base */
763 /* Don't need to check if too small since we already did */
764 if (node->length > size) {
765 dbg("%s: too big\n", __FUNCTION__);
766 /* this one is longer than we need
767 so we'll make a new entry and split it up */
768 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
773 split_node->base = node->base + size;
774 split_node->length = node->length - size;
777 /* Put it in the list */
778 split_node->next = node->next;
779 node->next = split_node;
780 } /* End of too big on top end */
782 dbg("%s: got one!!!\n", __FUNCTION__);
783 /* If we got here, then it is the right size
784 Now take it out of the list */
789 while (prevnode->next != node)
790 prevnode = prevnode->next;
792 prevnode->next = node->next;
803 * shpchp_resource_sort_and_combine
805 * Sorts all of the nodes in the list in ascending order by
806 * their base addresses. Also does garbage collection by
807 * combining adjacent nodes.
809 * returns 0 if success
811 int shpchp_resource_sort_and_combine(struct pci_resource **head)
813 struct pci_resource *node1;
814 struct pci_resource *node2;
815 int out_of_order = 1;
817 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
822 dbg("*head->next = %p\n",(*head)->next);
825 return(0); /* only one item on the list, already sorted! */
827 dbg("*head->base = 0x%x\n",(*head)->base);
828 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
829 while (out_of_order) {
832 /* Special case for swapping list head */
833 if (((*head)->next) &&
834 ((*head)->base > (*head)->next->base)) {
836 (*head) = (*head)->next;
837 node1->next = (*head)->next;
838 (*head)->next = node1;
844 while (node1->next && node1->next->next) {
845 if (node1->next->base > node1->next->next->base) {
848 node1->next = node1->next->next;
850 node2->next = node1->next;
855 } /* End of out_of_order loop */
859 while (node1 && node1->next) {
860 if ((node1->base + node1->length) == node1->next->base) {
863 node1->length += node1->next->length;
865 node1->next = node1->next->next;
876 * shpchp_slot_create - Creates a node and adds it to the proper bus.
877 * @busnumber - bus where new node is to be located
879 * Returns pointer to the new node or NULL if unsuccessful
881 struct pci_func *shpchp_slot_create(u8 busnumber)
883 struct pci_func *new_slot;
884 struct pci_func *next;
886 new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
888 if (new_slot == NULL) {
892 memset(new_slot, 0, sizeof(struct pci_func));
894 new_slot->next = NULL;
895 new_slot->configured = 1;
897 if (shpchp_slot_list[busnumber] == NULL) {
898 shpchp_slot_list[busnumber] = new_slot;
900 next = shpchp_slot_list[busnumber];
901 while (next->next != NULL)
903 next->next = new_slot;
910 * slot_remove - Removes a node from the linked list of slots.
911 * @old_slot: slot to remove
913 * Returns 0 if successful, !0 otherwise.
915 static int slot_remove(struct pci_func * old_slot)
917 struct pci_func *next;
919 if (old_slot == NULL)
922 next = shpchp_slot_list[old_slot->bus];
928 if (next == old_slot) {
929 shpchp_slot_list[old_slot->bus] = old_slot->next;
930 shpchp_destroy_board_resources(old_slot);
935 while ((next->next != old_slot) && (next->next != NULL)) {
939 if (next->next == old_slot) {
940 next->next = old_slot->next;
941 shpchp_destroy_board_resources(old_slot);
950 * bridge_slot_remove - Removes a node from the linked list of slots.
951 * @bridge: bridge to remove
953 * Returns 0 if successful, !0 otherwise.
955 static int bridge_slot_remove(struct pci_func *bridge)
957 u8 subordinateBus, secondaryBus;
959 struct pci_func *next;
964 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
965 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
967 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
968 next = shpchp_slot_list[tempBus];
970 while (!slot_remove(next)) {
971 next = shpchp_slot_list[tempBus];
975 next = shpchp_slot_list[bridge->bus];
981 if (next == bridge) {
982 shpchp_slot_list[bridge->bus] = bridge->next;
987 while ((next->next != bridge) && (next->next != NULL)) {
991 if (next->next == bridge) {
992 next->next = bridge->next;
1001 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1003 * @device: device to find
1004 * @index: is 0 for first function found, 1 for the second...
1006 * Returns pointer to the node if successful, %NULL otherwise.
1008 struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
1011 struct pci_func *func;
1013 func = shpchp_slot_list[bus];
1015 if ((func == NULL) || ((func->device == device) && (index == 0)))
1018 if (func->device == device)
1021 while (func->next != NULL) {
1024 if (func->device == device)
1034 static int is_bridge(struct pci_func * func)
1036 /* Check the header type */
1037 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1044 /* The following routines constitute the bulk of the
1045 hotplug controller logic
1050 * board_added - Called after a board has been added to the system.
1052 * Turns power on for the board
1056 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1059 u8 slots_not_empty = 0;
1061 u32 temp_register = 0xFFFFFFFF;
1063 struct pci_func *new_func = NULL;
1064 struct pci_func *t_func = NULL;
1065 struct slot *p_slot, *pslot;
1066 struct resource_lists res_lists;
1067 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
1070 p_slot = shpchp_find_slot(ctrl, func->device);
1071 hp_slot = func->device - ctrl->slot_device_offset;
1073 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1075 /* Wait for exclusive access to hardware */
1076 down(&ctrl->crit_sect);
1078 /* Power on slot without connecting to bus */
1079 rc = p_slot->hpc_ops->power_on_slot(p_slot);
1081 err("%s: Failed to power on slot\n", __FUNCTION__);
1082 /* Done with exclusive hardware access */
1083 up(&ctrl->crit_sect);
1087 /* Wait for the command to complete */
1088 wait_for_ctrl_irq (ctrl);
1090 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1092 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
1093 /* Done with exclusive hardware access */
1094 up(&ctrl->crit_sect);
1098 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
1099 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
1100 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
1101 /* 0xd = PCI-X 133 Mhz 533 */
1102 /* This encoding is different from the one used in cur_bus_speed & */
1105 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
1106 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
1107 /* Done with exclusive hardware access */
1108 up(&ctrl->crit_sect);
1109 return WRONG_BUS_FREQUENCY;
1112 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
1113 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
1114 err("%s: Can't get bus operation speed\n", __FUNCTION__);
1115 /* Done with exclusive hardware access */
1116 up(&ctrl->crit_sect);
1117 return WRONG_BUS_FREQUENCY;
1120 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1121 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1122 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1123 max_bus_speed = bus_speed;
1126 /* Done with exclusive hardware access */
1127 up(&ctrl->crit_sect);
1129 rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi);
1131 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
1135 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1136 if (slot != hp_slot) {
1137 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1138 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1139 slots_not_empty |= t_func->is_a_board;
1143 switch (adapter_speed) {
1144 case PCI_SPEED_133MHz_PCIX_533:
1145 case PCI_SPEED_133MHz_PCIX_266:
1146 if ((( bus_speed < 0xa ) || (bus_speed < 0xd)) && (max_bus_speed > bus_speed) &&
1147 ((max_bus_speed <= 0xa) || (max_bus_speed <= 0xd)) && (!slots_not_empty)) {
1149 /* Wait for exclusive access to hardware */
1150 down(&ctrl->crit_sect);
1152 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1154 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1155 /* Done with exclusive hardware access */
1156 up(&ctrl->crit_sect);
1157 return WRONG_BUS_FREQUENCY;
1160 /* Wait for the command to complete */
1161 wait_for_ctrl_irq (ctrl);
1163 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1165 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1167 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1168 /* Done with exclusive hardware access */
1169 up(&ctrl->crit_sect);
1170 return WRONG_BUS_FREQUENCY;
1172 /* Done with exclusive hardware access */
1173 up(&ctrl->crit_sect);
1176 case PCI_SPEED_133MHz_PCIX_ECC:
1177 case PCI_SPEED_133MHz_PCIX:
1179 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1182 err("%s: PI is 1 \n", __FUNCTION__);
1183 return WRONG_BUS_FREQUENCY;
1186 if (mode) { /* Bus - Mode 1 ECC */
1188 if (bus_speed > 0x7) {
1189 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1190 return WRONG_BUS_FREQUENCY;
1193 if ((bus_speed < 0x7) && (max_bus_speed <= 0x7) &&
1194 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1196 /* Wait for exclusive access to hardware */
1197 down(&ctrl->crit_sect);
1199 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1201 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1202 /* Done with exclusive hardware access */
1203 up(&ctrl->crit_sect);
1204 return WRONG_BUS_FREQUENCY;
1207 /* Wait for the command to complete */
1208 wait_for_ctrl_irq (ctrl);
1210 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1212 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1214 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1215 /* Done with exclusive hardware access */
1216 up(&ctrl->crit_sect);
1217 return WRONG_BUS_FREQUENCY;
1219 /* Done with exclusive hardware access */
1220 up(&ctrl->crit_sect);
1223 if (bus_speed > 0x4) {
1224 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1225 return WRONG_BUS_FREQUENCY;
1228 if ((bus_speed < 0x4) && (max_bus_speed <= 0x4) &&
1229 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1231 /* Wait for exclusive access to hardware */
1232 down(&ctrl->crit_sect);
1234 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1236 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1237 /* Done with exclusive hardware access */
1238 up(&ctrl->crit_sect);
1239 return WRONG_BUS_FREQUENCY;
1242 /* Wait for the command to complete */
1243 wait_for_ctrl_irq (ctrl);
1245 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1247 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1249 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1250 /* Done with exclusive hardware access */
1251 up(&ctrl->crit_sect);
1252 return WRONG_BUS_FREQUENCY;
1254 /* Done with exclusive hardware access */
1255 up(&ctrl->crit_sect);
1259 case PCI_SPEED_66MHz_PCIX_ECC:
1260 case PCI_SPEED_66MHz_PCIX:
1262 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1265 err("%s: PI is 1 \n", __FUNCTION__);
1266 return WRONG_BUS_FREQUENCY;
1269 if (mode) { /* Bus - Mode 1 ECC */
1271 if (bus_speed > 0x5) {
1272 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1273 return WRONG_BUS_FREQUENCY;
1276 if ((bus_speed < 0x5) && (max_bus_speed <= 0x5) &&
1277 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1279 /* Wait for exclusive access to hardware */
1280 down(&ctrl->crit_sect);
1282 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1284 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1285 /* Done with exclusive hardware access */
1286 up(&ctrl->crit_sect);
1287 return WRONG_BUS_FREQUENCY;
1290 /* Wait for the command to complete */
1291 wait_for_ctrl_irq (ctrl);
1293 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1295 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1297 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1298 /* Done with exclusive hardware access */
1299 up(&ctrl->crit_sect);
1300 return WRONG_BUS_FREQUENCY;
1302 /* Done with exclusive hardware access */
1303 up(&ctrl->crit_sect);
1306 if (bus_speed > 0x2) {
1307 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1308 return WRONG_BUS_FREQUENCY;
1311 if ((bus_speed < 0x2) && (max_bus_speed <= 0x2) &&
1312 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1314 /* Wait for exclusive access to hardware */
1315 down(&ctrl->crit_sect);
1317 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1319 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1320 /* Done with exclusive hardware access */
1321 up(&ctrl->crit_sect);
1322 return WRONG_BUS_FREQUENCY;
1325 /* Wait for the command to complete */
1326 wait_for_ctrl_irq (ctrl);
1328 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1330 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1332 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1333 /* Done with exclusive hardware access */
1334 up(&ctrl->crit_sect);
1335 return WRONG_BUS_FREQUENCY;
1337 /* Done with exclusive hardware access */
1338 up(&ctrl->crit_sect);
1342 case PCI_SPEED_66MHz:
1343 if (bus_speed > 0x1) {
1344 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1345 return WRONG_BUS_FREQUENCY;
1347 if (bus_speed == 0x1)
1349 if ((bus_speed == 0x0) && ( max_bus_speed == 0x1)) {
1350 /* Wait for exclusive access to hardware */
1351 down(&ctrl->crit_sect);
1353 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1355 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1356 /* Done with exclusive hardware access */
1357 up(&ctrl->crit_sect);
1358 return WRONG_BUS_FREQUENCY;
1361 /* Wait for the command to complete */
1362 wait_for_ctrl_irq (ctrl);
1364 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1366 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1368 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1369 /* Done with exclusive hardware access */
1370 up(&ctrl->crit_sect);
1371 return WRONG_BUS_FREQUENCY;
1373 /* Done with exclusive hardware access */
1374 up(&ctrl->crit_sect);
1377 case PCI_SPEED_33MHz:
1378 if (bus_speed > 0x0) {
1379 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1380 return WRONG_BUS_FREQUENCY;
1384 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1385 return WRONG_BUS_FREQUENCY;
1388 /* if adpater_speed == bus_speed, nothing to do here */
1389 if (adapter_speed != bus_speed) {
1390 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1391 if (slot != hp_slot) {
1392 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1393 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1394 slots_not_empty |= t_func->is_a_board;
1398 if (slots_not_empty != 0) { /* Other slots on the same bus are occupied */
1399 if ( adapter_speed < bus_speed ) {
1400 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1401 return WRONG_BUS_FREQUENCY;
1403 /* Do nothing if adapter_speed >= bus_speed */
1407 if ((adapter_speed != bus_speed) && (slots_not_empty == 0)) {
1408 /* Other slots on the same bus are empty */
1410 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1411 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1412 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1413 max_bus_speed = bus_speed;
1416 if (max_bus_speed == bus_speed) {
1417 /* if adapter_speed >= bus_speed, do nothing */
1418 if (adapter_speed < bus_speed) {
1420 * Try to lower bus speed to accommodate the adapter if other slots
1421 * on the same controller are empty
1424 /* Wait for exclusive access to hardware */
1425 down(&ctrl->crit_sect);
1427 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1429 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1430 return WRONG_BUS_FREQUENCY;
1433 /* Wait for the command to complete */
1434 wait_for_ctrl_irq (ctrl);
1436 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1438 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1440 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1441 return WRONG_BUS_FREQUENCY;
1443 /* Done with exclusive hardware access */
1444 up(&ctrl->crit_sect);
1448 /* Wait for exclusive access to hardware */
1449 down(&ctrl->crit_sect);
1451 /* max_bus_speed != bus_speed. Note: max_bus_speed should be > than bus_speed */
1452 if (adapter_speed < max_bus_speed)
1453 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1455 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1458 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1459 /* Done with exclusive hardware access */
1460 up(&ctrl->crit_sect);
1461 return WRONG_BUS_FREQUENCY;
1464 /* Wait for the command to complete */
1465 wait_for_ctrl_irq (ctrl);
1467 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1469 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1471 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1472 /* Done with exclusive hardware access */
1473 up(&ctrl->crit_sect);
1474 return WRONG_BUS_FREQUENCY;
1476 /* Done with exclusive hardware access */
1477 up(&ctrl->crit_sect);
1483 /* Wait for exclusive access to hardware */
1484 down(&ctrl->crit_sect);
1486 /* turn on board, blink green LED, turn off Amber LED */
1487 rc = p_slot->hpc_ops->slot_enable(p_slot);
1490 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1491 /* Done with exclusive hardware access */
1492 up(&ctrl->crit_sect);
1495 /* Wait for the command to complete */
1496 wait_for_ctrl_irq (ctrl);
1498 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1500 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1501 /* Done with exclusive hardware access */
1502 up(&ctrl->crit_sect);
1506 /* Done with exclusive hardware access */
1507 up(&ctrl->crit_sect);
1509 /* Wait for ~1 second */
1510 dbg("%s: before long_delay\n", __FUNCTION__);
1511 wait_for_ctrl_irq (ctrl);
1512 dbg("%s: afterlong_delay\n", __FUNCTION__);
1514 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1515 /* Check for a power fault */
1516 if (func->status == 0xFF) {
1517 /* power fault occurred, but it was benign */
1518 temp_register = 0xFFFFFFFF;
1519 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1523 /* Get vendor/device ID u32 */
1524 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
1525 PCI_VENDOR_ID, &temp_register);
1526 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1527 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1530 /* Something's wrong here */
1531 temp_register = 0xFFFFFFFF;
1532 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1534 /* Preset return code. It will be changed later if things go okay. */
1535 rc = NO_ADAPTER_PRESENT;
1538 /* All F's is an empty slot or an invalid board */
1539 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1540 res_lists.io_head = ctrl->io_head;
1541 res_lists.mem_head = ctrl->mem_head;
1542 res_lists.p_mem_head = ctrl->p_mem_head;
1543 res_lists.bus_head = ctrl->bus_head;
1544 res_lists.irqs = NULL;
1546 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1547 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1549 ctrl->io_head = res_lists.io_head;
1550 ctrl->mem_head = res_lists.mem_head;
1551 ctrl->p_mem_head = res_lists.p_mem_head;
1552 ctrl->bus_head = res_lists.bus_head;
1554 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1555 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1556 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1557 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1560 /* Wait for exclusive access to hardware */
1561 down(&ctrl->crit_sect);
1563 /* turn off slot, turn on Amber LED, turn off Green LED */
1564 retval = p_slot->hpc_ops->slot_disable(p_slot);
1566 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1567 /* Done with exclusive hardware access */
1568 up(&ctrl->crit_sect);
1571 /* Wait for the command to complete */
1572 wait_for_ctrl_irq (ctrl);
1574 retval = p_slot->hpc_ops->check_cmd_status(ctrl);
1576 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, retval);
1577 /* Done with exclusive hardware access */
1578 up(&ctrl->crit_sect);
1582 /* Done with exclusive hardware access */
1583 up(&ctrl->crit_sect);
1587 shpchp_save_slot_config(ctrl, func);
1590 func->switch_save = 0x10;
1591 func->is_a_board = 0x01;
1593 /* next, we will instantiate the linux pci_dev structures
1594 * (with appropriate driver notification, if already present)
1598 new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
1599 if (new_func && !new_func->pci_dev) {
1600 dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
1601 shpchp_configure_device(ctrl, new_func);
1605 /* Wait for exclusive access to hardware */
1606 down(&ctrl->crit_sect);
1608 p_slot->hpc_ops->green_led_on(p_slot);
1610 /* Wait for the command to complete */
1611 wait_for_ctrl_irq (ctrl);
1614 /* Done with exclusive hardware access */
1615 up(&ctrl->crit_sect);
1618 /* Wait for exclusive access to hardware */
1619 down(&ctrl->crit_sect);
1621 /* turn off slot, turn on Amber LED, turn off Green LED */
1622 rc = p_slot->hpc_ops->slot_disable(p_slot);
1624 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1625 /* Done with exclusive hardware access */
1626 up(&ctrl->crit_sect);
1629 /* Wait for the command to complete */
1630 wait_for_ctrl_irq (ctrl);
1632 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1634 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1635 /* Done with exclusive hardware access */
1636 up(&ctrl->crit_sect);
1640 /* Done with exclusive hardware access */
1641 up(&ctrl->crit_sect);
1650 * remove_board - Turns off slot and LED's
1653 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1660 struct resource_lists res_lists;
1661 struct pci_func *temp_func;
1662 struct slot *p_slot;
1667 if (shpchp_unconfigure_device(func))
1670 device = func->device;
1672 hp_slot = func->device - ctrl->slot_device_offset;
1673 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1675 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1677 if ((ctrl->add_support) &&
1678 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1679 /* Here we check to see if we've saved any of the board's
1680 * resources already. If so, we'll skip the attempt to
1681 * determine what's being used.
1687 while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
1688 if (temp_func->bus_head || temp_func->mem_head
1689 || temp_func->p_mem_head || temp_func->io_head) {
1696 rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
1698 /* Change status to shutdown */
1699 if (func->is_a_board)
1700 func->status = 0x01;
1701 func->configured = 0;
1703 /* Wait for exclusive access to hardware */
1704 down(&ctrl->crit_sect);
1706 /* turn off slot, turn on Amber LED, turn off Green LED */
1707 rc = p_slot->hpc_ops->slot_disable(p_slot);
1709 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1710 /* Done with exclusive hardware access */
1711 up(&ctrl->crit_sect);
1714 /* Wait for the command to complete */
1715 wait_for_ctrl_irq (ctrl);
1717 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1719 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1720 /* Done with exclusive hardware access */
1721 up(&ctrl->crit_sect);
1725 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
1727 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
1728 /* Done with exclusive hardware access */
1729 up(&ctrl->crit_sect);
1732 /* Wait for the command to complete */
1733 wait_for_ctrl_irq (ctrl);
1735 /* Done with exclusive hardware access */
1736 up(&ctrl->crit_sect);
1738 if (ctrl->add_support) {
1740 res_lists.io_head = ctrl->io_head;
1741 res_lists.mem_head = ctrl->mem_head;
1742 res_lists.p_mem_head = ctrl->p_mem_head;
1743 res_lists.bus_head = ctrl->bus_head;
1745 dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", func->bus,
1746 func->device, func->function);
1748 shpchp_return_board_resources(func, &res_lists);
1750 ctrl->io_head = res_lists.io_head;
1751 ctrl->mem_head = res_lists.mem_head;
1752 ctrl->p_mem_head = res_lists.p_mem_head;
1753 ctrl->bus_head = res_lists.bus_head;
1755 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1756 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1757 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1758 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1760 if (is_bridge(func)) {
1761 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1762 func->device, func->function);
1763 bridge_slot_remove(func);
1765 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1766 func->device, func->function);
1769 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
1772 /* Setup slot structure with entry for empty slot */
1773 func = shpchp_slot_create(ctrl->slot_bus);
1779 func->bus = ctrl->slot_bus;
1780 func->device = device;
1782 func->configured = 0;
1783 func->switch_save = 0x10;
1784 func->is_a_board = 0;
1791 static void pushbutton_helper_thread (unsigned long data)
1793 pushbutton_pending = data;
1795 up(&event_semaphore);
1800 * shpchp_pushbutton_thread
1802 * Scheduled procedure to handle blocking stuff for the pushbuttons
1803 * Handles all pending events and exits.
1806 static void shpchp_pushbutton_thread (unsigned long slot)
1808 struct slot *p_slot = (struct slot *) slot;
1812 pushbutton_pending = 0;
1815 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1819 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1821 p_slot->state = POWEROFF_STATE;
1822 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1824 if (shpchp_disable_slot(p_slot)) {
1825 /* Wait for exclusive access to hardware */
1826 down(&p_slot->ctrl->crit_sect);
1828 /* Turn on the Attention LED */
1829 rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
1831 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
1835 /* Wait for the command to complete */
1836 wait_for_ctrl_irq (p_slot->ctrl);
1838 /* Done with exclusive hardware access */
1839 up(&p_slot->ctrl->crit_sect);
1841 p_slot->state = STATIC_STATE;
1843 p_slot->state = POWERON_STATE;
1844 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1846 if (shpchp_enable_slot(p_slot)) {
1847 /* Wait for exclusive access to hardware */
1848 down(&p_slot->ctrl->crit_sect);
1850 /* Turn off the green LED */
1851 rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
1853 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
1856 /* Wait for the command to complete */
1857 wait_for_ctrl_irq (p_slot->ctrl);
1859 p_slot->hpc_ops->green_led_off(p_slot);
1861 /* Wait for the command to complete */
1862 wait_for_ctrl_irq (p_slot->ctrl);
1864 /* Done with exclusive hardware access */
1865 up(&p_slot->ctrl->crit_sect);
1867 p_slot->state = STATIC_STATE;
1874 /* this is the main worker thread */
1875 static int event_thread(void* data)
1877 struct controller *ctrl;
1879 daemonize("shpchpd_event");
1883 dbg("!!!!event_thread sleeping\n");
1884 down_interruptible (&event_semaphore);
1885 dbg("event_thread woken finished = %d\n", event_finished);
1886 if (event_finished || signal_pending(current))
1889 if (pushbutton_pending)
1890 shpchp_pushbutton_thread(pushbutton_pending);
1892 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
1893 interrupt_event_handler(ctrl);
1895 dbg("event_thread signals exit\n");
1900 int shpchp_event_start_thread (void)
1904 /* initialize our semaphores */
1905 init_MUTEX_LOCKED(&event_exit);
1908 init_MUTEX_LOCKED(&event_semaphore);
1909 pid = kernel_thread(event_thread, 0, 0);
1912 err ("Can't start up our event thread\n");
1915 dbg("Our event thread pid = %d\n", pid);
1920 void shpchp_event_stop_thread (void)
1923 dbg("event_thread finish command given\n");
1924 up(&event_semaphore);
1925 dbg("wait for event_thread to exit\n");
1930 static int update_slot_info (struct slot *slot)
1932 struct hotplug_slot_info *info;
1935 info = kmalloc(sizeof(*info), GFP_KERNEL);
1939 slot->hpc_ops->get_power_status(slot, &(info->power_status));
1940 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1941 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1942 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1944 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1949 static void interrupt_event_handler(struct controller *ctrl)
1953 struct pci_func *func;
1956 struct slot *p_slot;
1958 dbg("%s:\n", __FUNCTION__);
1962 for (loop = 0; loop < 10; loop++) {
1963 if (ctrl->event_queue[loop].event_type != 0) {
1964 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1965 ctrl->event_queue[loop].event_type);
1966 hp_slot = ctrl->event_queue[loop].hp_slot;
1968 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1970 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1972 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1974 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1975 dbg("%s: button cancel\n", __FUNCTION__);
1976 del_timer(&p_slot->task_event);
1978 switch (p_slot->state) {
1979 case BLINKINGOFF_STATE:
1980 /* Wait for exclusive access to hardware */
1981 down(&ctrl->crit_sect);
1983 p_slot->hpc_ops->green_led_on(p_slot);
1984 /* Wait for the command to complete */
1985 wait_for_ctrl_irq (ctrl);
1987 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1989 /* Wait for the command to complete */
1990 wait_for_ctrl_irq (ctrl);
1992 /* Done with exclusive hardware access */
1993 up(&ctrl->crit_sect);
1995 case BLINKINGON_STATE:
1996 /* Wait for exclusive access to hardware */
1997 down(&ctrl->crit_sect);
1999 p_slot->hpc_ops->green_led_off(p_slot);
2000 /* Wait for the command to complete */
2001 wait_for_ctrl_irq (ctrl);
2003 p_slot->hpc_ops->set_attention_status(p_slot, 0);
2004 /* Wait for the command to complete */
2005 wait_for_ctrl_irq (ctrl);
2007 /* Done with exclusive hardware access */
2008 up(&ctrl->crit_sect);
2012 warn("Not a valid state\n");
2015 info(msg_button_cancel, p_slot->number);
2016 p_slot->state = STATIC_STATE;
2017 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
2018 /* Button Pressed (No action on 1st press...) */
2019 dbg("%s: Button pressed\n", __FUNCTION__);
2021 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2024 dbg("%s: slot is on\n", __FUNCTION__);
2025 p_slot->state = BLINKINGOFF_STATE;
2026 info(msg_button_off, p_slot->number);
2029 dbg("%s: slot is off\n", __FUNCTION__);
2030 p_slot->state = BLINKINGON_STATE;
2031 info(msg_button_on, p_slot->number);
2034 /* Wait for exclusive access to hardware */
2035 down(&ctrl->crit_sect);
2037 /* blink green LED and turn off amber */
2038 p_slot->hpc_ops->green_led_blink(p_slot);
2039 /* Wait for the command to complete */
2040 wait_for_ctrl_irq (ctrl);
2042 p_slot->hpc_ops->set_attention_status(p_slot, 0);
2044 /* Wait for the command to complete */
2045 wait_for_ctrl_irq (ctrl);
2047 /* Done with exclusive hardware access */
2048 up(&ctrl->crit_sect);
2050 init_timer(&p_slot->task_event);
2051 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
2052 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
2053 p_slot->task_event.data = (unsigned long) p_slot;
2055 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
2056 add_timer(&p_slot->task_event);
2057 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
2058 /***********POWER FAULT********************/
2059 dbg("%s: power fault\n", __FUNCTION__);
2060 /* Wait for exclusive access to hardware */
2061 down(&ctrl->crit_sect);
2063 p_slot->hpc_ops->set_attention_status(p_slot, 1);
2064 /* Wait for the command to complete */
2065 wait_for_ctrl_irq (ctrl);
2067 p_slot->hpc_ops->green_led_off(p_slot);
2068 /* Wait for the command to complete */
2069 wait_for_ctrl_irq (ctrl);
2071 /* Done with exclusive hardware access */
2072 up(&ctrl->crit_sect);
2074 /* refresh notification */
2076 update_slot_info(p_slot);
2079 ctrl->event_queue[loop].event_type = 0;
2083 } /* End of FOR loop */
2090 int shpchp_enable_slot (struct slot *p_slot)
2094 struct pci_func *func;
2096 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2098 dbg("%s: Error! slot NULL\n", __FUNCTION__);
2102 /* Check to see if (latch closed, card present, power off) */
2103 down(&p_slot->ctrl->crit_sect);
2104 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2105 if (rc || !getstatus) {
2106 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2107 up(&p_slot->ctrl->crit_sect);
2110 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2111 if (rc || getstatus) {
2112 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2113 up(&p_slot->ctrl->crit_sect);
2116 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2117 if (rc || getstatus) {
2118 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
2119 up(&p_slot->ctrl->crit_sect);
2122 up(&p_slot->ctrl->crit_sect);
2126 func = shpchp_slot_create(p_slot->bus);
2130 func->bus = p_slot->bus;
2131 func->device = p_slot->device;
2133 func->configured = 0;
2134 func->is_a_board = 1;
2136 /* We have to save the presence info for these slots */
2137 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2138 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2139 func->switch_save = !getstatus? 0x10:0;
2141 rc = board_added(func, p_slot->ctrl);
2143 if (is_bridge(func))
2144 bridge_slot_remove(func);
2148 /* Setup slot structure with entry for empty slot */
2149 func = shpchp_slot_create(p_slot->bus);
2151 return (1); /* Out of memory */
2153 func->bus = p_slot->bus;
2154 func->device = p_slot->device;
2156 func->configured = 0;
2157 func->is_a_board = 1;
2159 /* We have to save the presence info for these slots */
2160 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2161 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2162 func->switch_save = !getstatus? 0x10:0;
2166 update_slot_info(p_slot);
2172 int shpchp_disable_slot (struct slot *p_slot)
2174 u8 class_code, header_type, BCR;
2180 struct pci_bus *pci_bus = p_slot->ctrl->pci_dev->subordinate;
2181 struct pci_func *func;
2186 /* Check to see if (latch closed, card present, power on) */
2187 down(&p_slot->ctrl->crit_sect);
2189 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2190 if (ret || !getstatus) {
2191 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2192 up(&p_slot->ctrl->crit_sect);
2195 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2196 if (ret || getstatus) {
2197 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2198 up(&p_slot->ctrl->crit_sect);
2201 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2202 if (ret || !getstatus) {
2203 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
2204 up(&p_slot->ctrl->crit_sect);
2207 up(&p_slot->ctrl->crit_sect);
2209 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2211 /* Make sure there are no video controllers here
2212 * for all func of p_slot
2214 while (func && !rc) {
2215 pci_bus->number = func->bus;
2216 devfn = PCI_DEVFN(func->device, func->function);
2218 /* Check the Class Code */
2219 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2223 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2224 /* Display/Video adapter (not supported) */
2225 rc = REMOVE_NOT_SUPPORTED;
2227 /* See if it's a bridge */
2228 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2232 /* If it's a bridge, check the VGA Enable bit */
2233 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2234 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2238 /* If the VGA Enable bit is set, remove isn't supported */
2239 if (BCR & PCI_BRIDGE_CTL_VGA) {
2240 rc = REMOVE_NOT_SUPPORTED;
2245 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2248 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2249 if ((func != NULL) && !rc) {
2250 rc = remove_board(func, p_slot->ctrl);
2255 update_slot_info(p_slot);
2262 * configure_new_device - Configures the PCI header information of one board.
2264 * @ctrl: pointer to controller structure
2265 * @func: pointer to function structure
2266 * @behind_bridge: 1 if this is a recursive call, 0 if not
2267 * @resources: pointer to set of resource lists
2269 * Returns 0 if success
2272 static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
2273 u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
2275 u8 temp_byte, function, max_functions, stop_it;
2278 struct pci_func *new_slot;
2279 struct pci_bus lpci_bus, *pci_bus;
2284 dbg("%s\n", __FUNCTION__);
2285 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2286 pci_bus = &lpci_bus;
2287 pci_bus->number = func->bus;
2289 /* Check for Multi-function device */
2290 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2292 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2296 if (temp_byte & 0x80) /* Multi-function device */
2304 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
2307 dbg("configure_new_function failed %d\n",rc);
2311 new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
2314 shpchp_return_board_resources(new_slot, resources);
2324 /* The following loop skips to the next present function
2325 * and creates a board structure
2328 while ((function < max_functions) && (!stop_it)) {
2329 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2331 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2333 } else { /* There's something there */
2334 /* Setup slot structure. */
2335 new_slot = shpchp_slot_create(func->bus);
2337 if (new_slot == NULL) {
2342 new_slot->bus = func->bus;
2343 new_slot->device = func->device;
2344 new_slot->function = function;
2345 new_slot->is_a_board = 1;
2346 new_slot->status = 0;
2352 } while (function < max_functions);
2353 dbg("returning from configure_new_device\n");
2360 * Configuration logic that involves the hotplug data structures and
2366 * configure_new_function - Configures the PCI header information of one device
2368 * @ctrl: pointer to controller structure
2369 * @func: pointer to function structure
2370 * @behind_bridge: 1 if this is a recursive call, 0 if not
2371 * @resources: pointer to set of resource lists
2373 * Calls itself recursively for bridged devices.
2374 * Returns 0 if success
2377 static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2378 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
2390 struct pci_resource *mem_node;
2391 struct pci_resource *p_mem_node;
2392 struct pci_resource *io_node;
2393 struct pci_resource *bus_node;
2394 struct pci_resource *hold_mem_node;
2395 struct pci_resource *hold_p_mem_node;
2396 struct pci_resource *hold_IO_node;
2397 struct pci_resource *hold_bus_node;
2398 struct irq_mapping irqs;
2399 struct pci_func *new_slot;
2400 struct pci_bus lpci_bus, *pci_bus;
2401 struct resource_lists temp_resources;
2402 #if defined(CONFIG_X86_64)
2406 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2407 pci_bus = &lpci_bus;
2408 pci_bus->number = func->bus;
2409 devfn = PCI_DEVFN(func->device, func->function);
2411 /* Check for Bridge */
2412 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2416 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2417 /* set Primary bus */
2418 dbg("set Primary bus = 0x%x\n", func->bus);
2419 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2423 /* find range of busses to use */
2424 bus_node = get_max_resource(&resources->bus_head, 1L);
2426 /* If we don't have any busses to allocate, we can't continue */
2428 err("Got NO bus resource to use\n");
2431 dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2433 /* set Secondary bus */
2434 temp_byte = (u8)bus_node->base;
2435 dbg("set Secondary bus = 0x%x\n", temp_byte);
2436 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2440 /* set subordinate bus */
2441 temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2442 dbg("set subordinate bus = 0x%x\n", temp_byte);
2443 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2447 /* Set HP parameters (Cache Line Size, Latency Timer) */
2448 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2452 /* Setup the IO, memory, and prefetchable windows */
2454 io_node = get_max_resource(&(resources->io_head), 0x1000L);
2456 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2459 mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2461 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2464 if (resources->p_mem_head)
2465 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2468 * In some platform implementation, MEM and PMEM are not
2469 * distinguished, and hence ACPI _CRS has only MEM entries
2470 * for both MEM and PMEM.
2472 dbg("using MEM for PMEM\n");
2473 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2476 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2479 /* set up the IRQ info */
2480 if (!resources->irqs) {
2481 irqs.barber_pole = 0;
2482 irqs.interrupt[0] = 0;
2483 irqs.interrupt[1] = 0;
2484 irqs.interrupt[2] = 0;
2485 irqs.interrupt[3] = 0;
2488 irqs.barber_pole = resources->irqs->barber_pole;
2489 irqs.interrupt[0] = resources->irqs->interrupt[0];
2490 irqs.interrupt[1] = resources->irqs->interrupt[1];
2491 irqs.interrupt[2] = resources->irqs->interrupt[2];
2492 irqs.interrupt[3] = resources->irqs->interrupt[3];
2493 irqs.valid_INT = resources->irqs->valid_INT;
2496 /* set up resource lists that are now aligned on top and bottom
2497 * for anything behind the bridge.
2499 temp_resources.bus_head = bus_node;
2500 temp_resources.io_head = io_node;
2501 temp_resources.mem_head = mem_node;
2502 temp_resources.p_mem_head = p_mem_node;
2503 temp_resources.irqs = &irqs;
2505 /* Make copies of the nodes we are going to pass down so that
2506 * if there is a problem,we can just use these to free resources
2508 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2509 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2510 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2511 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2513 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2514 kfree(hold_bus_node);
2515 kfree(hold_IO_node);
2516 kfree(hold_mem_node);
2517 kfree(hold_p_mem_node);
2522 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2524 bus_node->base += 1;
2525 bus_node->length -= 1;
2526 bus_node->next = NULL;
2528 /* If we have IO resources copy them and fill in the bridge's
2529 * IO range registers
2532 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2533 io_node->next = NULL;
2535 /* set IO base and Limit registers */
2536 RES_CHECK(io_node->base, 8);
2537 temp_byte = (u8)(io_node->base >> 8);
2538 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2540 RES_CHECK(io_node->base + io_node->length - 1, 8);
2541 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2542 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2544 kfree(hold_IO_node);
2545 hold_IO_node = NULL;
2548 /* If we have memory resources copy them and fill in the bridge's
2549 * memory range registers. Otherwise, fill in the range
2550 * registers with values that disable them.
2553 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2554 mem_node->next = NULL;
2556 /* set Mem base and Limit registers */
2557 RES_CHECK(mem_node->base, 16);
2558 temp_word = (u32)(mem_node->base >> 16);
2559 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2561 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
2562 temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
2563 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2566 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2569 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2571 kfree(hold_mem_node);
2572 hold_mem_node = NULL;
2575 /* If we have prefetchable memory resources copy them and
2576 * fill in the bridge's memory range registers. Otherwise,
2577 * fill in the range registers with values that disable them.
2580 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2581 p_mem_node->next = NULL;
2583 /* set Pre Mem base and Limit registers */
2584 RES_CHECK(p_mem_node->base, 16);
2585 temp_word = (u32)(p_mem_node->base >> 16);
2586 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2588 RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
2589 temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
2590 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2593 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2596 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2598 kfree(hold_p_mem_node);
2599 hold_p_mem_node = NULL;
2602 /* Adjust this to compensate for extra adjustment in first loop */
2607 /* Here we actually find the devices and configure them */
2608 for (device = 0; (device <= 0x1F) && !rc; device++) {
2609 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2612 pci_bus->number = hold_bus_node->base;
2613 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
2614 PCI_VENDOR_ID, &ID);
2615 pci_bus->number = func->bus;
2617 if (ID != 0xFFFFFFFF) { /* device Present */
2618 /* Setup slot structure. */
2619 new_slot = shpchp_slot_create(hold_bus_node->base);
2621 if (new_slot == NULL) {
2627 new_slot->bus = hold_bus_node->base;
2628 new_slot->device = device;
2629 new_slot->function = 0;
2630 new_slot->is_a_board = 1;
2631 new_slot->status = 0;
2633 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
2634 dbg("configure_new_device rc=0x%x\n",rc);
2635 } /* End of IF (device in slot?) */
2636 } /* End of FOR loop */
2639 shpchp_destroy_resource_list(&temp_resources);
2641 return_resource(&(resources->bus_head), hold_bus_node);
2642 return_resource(&(resources->io_head), hold_IO_node);
2643 return_resource(&(resources->mem_head), hold_mem_node);
2644 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2648 /* save the interrupt routing information */
2649 if (resources->irqs) {
2650 resources->irqs->interrupt[0] = irqs.interrupt[0];
2651 resources->irqs->interrupt[1] = irqs.interrupt[1];
2652 resources->irqs->interrupt[2] = irqs.interrupt[2];
2653 resources->irqs->interrupt[3] = irqs.interrupt[3];
2654 resources->irqs->valid_INT = irqs.valid_INT;
2655 } else if (!behind_bridge) {
2656 /* We need to hook up the interrupts here */
2657 for (cloop = 0; cloop < 4; cloop++) {
2658 if (irqs.valid_INT & (0x01 << cloop)) {
2659 rc = shpchp_set_irq(func->bus, func->device,
2660 0x0A + cloop, irqs.interrupt[cloop]);
2662 shpchp_destroy_resource_list (&temp_resources);
2663 return_resource(&(resources->bus_head), hold_bus_node);
2664 return_resource(&(resources->io_head), hold_IO_node);
2665 return_resource(&(resources->mem_head), hold_mem_node);
2666 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2670 } /* end of for loop */
2673 /* Return unused bus resources
2674 * First use the temporary node to store information for the board
2676 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2677 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2679 hold_bus_node->next = func->bus_head;
2680 func->bus_head = hold_bus_node;
2682 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2684 /* set subordinate bus */
2685 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2686 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2688 if (temp_resources.bus_head->length == 0) {
2689 kfree(temp_resources.bus_head);
2690 temp_resources.bus_head = NULL;
2692 dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2693 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2694 return_resource(&(resources->bus_head), temp_resources.bus_head);
2698 /* If we have IO space available and there is some left,
2699 * return the unused portion
2701 if (hold_IO_node && temp_resources.io_head) {
2702 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2703 &hold_IO_node, 0x1000);
2705 /* Check if we were able to split something off */
2707 hold_IO_node->base = io_node->base + io_node->length;
2709 RES_CHECK(hold_IO_node->base, 8);
2710 temp_byte = (u8)((hold_IO_node->base) >> 8);
2711 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2713 return_resource(&(resources->io_head), io_node);
2716 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2718 /* Check if we were able to split something off */
2720 /* First use the temporary node to store information for the board */
2721 hold_IO_node->length = io_node->base - hold_IO_node->base;
2723 /* If we used any, add it to the board's list */
2724 if (hold_IO_node->length) {
2725 hold_IO_node->next = func->io_head;
2726 func->io_head = hold_IO_node;
2728 RES_CHECK(io_node->base - 1, 8);
2729 temp_byte = (u8)((io_node->base - 1) >> 8);
2730 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2732 return_resource(&(resources->io_head), io_node);
2734 /* it doesn't need any IO */
2736 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2738 return_resource(&(resources->io_head), io_node);
2739 kfree(hold_IO_node);
2742 /* it used most of the range */
2743 hold_IO_node->next = func->io_head;
2744 func->io_head = hold_IO_node;
2746 } else if (hold_IO_node) {
2747 /* it used the whole range */
2748 hold_IO_node->next = func->io_head;
2749 func->io_head = hold_IO_node;
2752 /* If we have memory space available and there is some left,
2753 * return the unused portion
2755 if (hold_mem_node && temp_resources.mem_head) {
2756 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2758 /* Check if we were able to split something off */
2760 hold_mem_node->base = mem_node->base + mem_node->length;
2762 RES_CHECK(hold_mem_node->base, 16);
2763 temp_word = (u32)((hold_mem_node->base) >> 16);
2764 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2766 return_resource(&(resources->mem_head), mem_node);
2769 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2771 /* Check if we were able to split something off */
2773 /* First use the temporary node to store information for the board */
2774 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2776 if (hold_mem_node->length) {
2777 hold_mem_node->next = func->mem_head;
2778 func->mem_head = hold_mem_node;
2780 /* configure end address */
2781 RES_CHECK(mem_node->base - 1, 16);
2782 temp_word = (u32)((mem_node->base - 1) >> 16);
2783 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2785 /* Return unused resources to the pool */
2786 return_resource(&(resources->mem_head), mem_node);
2788 /* it doesn't need any Mem */
2790 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2792 return_resource(&(resources->mem_head), mem_node);
2793 kfree(hold_mem_node);
2796 /* it used most of the range */
2797 hold_mem_node->next = func->mem_head;
2798 func->mem_head = hold_mem_node;
2800 } else if (hold_mem_node) {
2801 /* it used the whole range */
2802 hold_mem_node->next = func->mem_head;
2803 func->mem_head = hold_mem_node;
2806 /* If we have prefetchable memory space available and there is some
2807 * left at the end, return the unused portion
2809 if (hold_p_mem_node && temp_resources.p_mem_head) {
2810 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2811 &hold_p_mem_node, 0x100000L);
2813 /* Check if we were able to split something off */
2815 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2817 RES_CHECK(hold_p_mem_node->base, 16);
2818 temp_word = (u32)((hold_p_mem_node->base) >> 16);
2819 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2821 return_resource(&(resources->p_mem_head), p_mem_node);
2824 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2826 /* Check if we were able to split something off */
2828 /* First use the temporary node to store information for the board */
2829 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2831 /* If we used any, add it to the board's list */
2832 if (hold_p_mem_node->length) {
2833 hold_p_mem_node->next = func->p_mem_head;
2834 func->p_mem_head = hold_p_mem_node;
2836 RES_CHECK(p_mem_node->base - 1, 16);
2837 temp_word = (u32)((p_mem_node->base - 1) >> 16);
2838 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2840 return_resource(&(resources->p_mem_head), p_mem_node);
2842 /* it doesn't need any PMem */
2844 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2846 return_resource(&(resources->p_mem_head), p_mem_node);
2847 kfree(hold_p_mem_node);
2850 /* it used the most of the range */
2851 hold_p_mem_node->next = func->p_mem_head;
2852 func->p_mem_head = hold_p_mem_node;
2854 } else if (hold_p_mem_node) {
2855 /* it used the whole range */
2856 hold_p_mem_node->next = func->p_mem_head;
2857 func->p_mem_head = hold_p_mem_node;
2860 /* We should be configuring an IRQ and the bridge's base address
2861 * registers if it needs them. Although we have never seen such
2865 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2867 dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2868 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2869 /* Standard device */
2871 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2873 if (class_code == PCI_BASE_CLASS_DISPLAY)
2874 return (DEVICE_TYPE_NOT_SUPPORTED);
2876 /* Figure out IO and memory needs */
2877 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2878 temp_register = 0xFFFFFFFF;
2880 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2881 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2882 dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, func->bus, func->device,
2889 if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2892 /* set base = amount of IO space */
2893 base = temp_register & 0xFFFFFFFC;
2896 dbg("NEED IO length(0x%x)\n", base);
2897 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2899 /* allocate the resource to the board */
2901 dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2902 base = (u32)io_node->base;
2903 io_node->next = func->io_head;
2904 func->io_head = io_node;
2906 err("Got NO IO resource(length=0x%x)\n", base);
2909 } else { /* map MEM */
2910 int prefetchable = 1;
2911 struct pci_resource **res_node = &func->p_mem_head;
2912 char *res_type_str = "PMEM";
2915 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2917 res_node = &func->mem_head;
2921 base = temp_register & 0xFFFFFFF0;
2924 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2925 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2926 dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2928 if (prefetchable && resources->p_mem_head)
2929 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2932 dbg("using MEM for PMEM\n");
2933 mem_node=get_resource(&(resources->mem_head), (ulong)base);
2936 /* allocate the resource to the board */
2938 base = (u32)mem_node->base;
2939 mem_node->next = *res_node;
2940 *res_node = mem_node;
2941 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
2944 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2948 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2949 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2950 dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
2951 temp_register, base);
2953 if (prefetchable && resources->p_mem_head)
2954 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2957 dbg("using MEM for PMEM\n");
2958 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2961 /* allocate the resource to the board */
2963 base64 = mem_node->base;
2964 mem_node->next = *res_node;
2965 *res_node = mem_node;
2966 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
2967 (u32)base64, mem_node->length);
2969 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2974 dbg("reserved BAR type=0x%x\n", temp_register);
2981 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2986 dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2990 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2992 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2994 } /* End of base register loop */
2996 #if defined(CONFIG_X86_64)
2997 /* Figure out which interrupt pin this function uses */
2998 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
3000 /* If this function needs an interrupt and we are behind a bridge
3001 and the pin is tied to something that's alread mapped,
3002 set this one the same
3004 if (temp_byte && resources->irqs &&
3005 (resources->irqs->valid_INT &
3006 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
3007 /* We have to share with something already set up */
3008 IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
3010 /* Program IRQ based on card type */
3011 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3013 if (class_code == PCI_BASE_CLASS_STORAGE) {
3014 IRQ = shpchp_disk_irq;
3016 IRQ = shpchp_nic_irq;
3021 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3023 if (!behind_bridge) {
3024 rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3028 /* TBD - this code may also belong in the other clause of this If statement */
3029 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3030 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3033 /* Disable ROM base Address */
3035 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
3037 /* Set HP parameters (Cache Line Size, Latency Timer) */
3038 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3042 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3044 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
3045 } /* End of Not-A-Bridge else */
3047 /* It's some strange type of PCI adapter (Cardbus?) */
3048 return(DEVICE_TYPE_NOT_SUPPORTED);
3051 func->configured = 1;