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);
140 dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
141 func->presence_save, func->pwr_save);
147 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
148 func->switch_save = 0;
149 taskInfo->event_type = INT_SWITCH_OPEN;
150 if (func->pwr_save && func->presence_save) {
151 taskInfo->event_type = INT_POWER_FAULT;
152 err("Surprise Removal of card\n");
158 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
159 func->switch_save = 0x10;
160 taskInfo->event_type = INT_SWITCH_CLOSE;
164 up(&event_semaphore); /* signal event thread that new event is posted */
169 u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
171 struct controller *ctrl = (struct controller *) inst_id;
175 struct pci_func *func;
176 struct event_info *taskInfo;
178 /* Presence Change */
179 dbg("shpchp: Presence/Notify input change.\n");
181 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
183 /* This is the structure that tells the worker thread
186 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
187 ctrl->next_event = (ctrl->next_event + 1) % 10;
188 taskInfo->hp_slot = hp_slot;
191 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
194 * Save the presence state
196 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
197 if (func->presence_save) {
201 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
202 taskInfo->event_type = INT_PRESENCE_ON;
207 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
208 taskInfo->event_type = INT_PRESENCE_OFF;
212 up(&event_semaphore); /* signal event thread that new event is posted */
217 u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
219 struct controller *ctrl = (struct controller *) inst_id;
222 struct pci_func *func;
223 struct event_info *taskInfo;
226 dbg("shpchp: Power fault interrupt received.\n");
228 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
230 /* This is the structure that tells the worker thread
233 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
234 ctrl->next_event = (ctrl->next_event + 1) % 10;
235 taskInfo->hp_slot = hp_slot;
238 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
240 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
242 * Power fault Cleared
244 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
246 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
251 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
252 taskInfo->event_type = INT_POWER_FAULT;
253 /* set power fault status for this board */
255 info("power fault bit %x set\n", hp_slot);
258 up(&event_semaphore); /* signal event thread that new event is posted */
267 * Sorts nodes on the list by their length.
271 static int sort_by_size(struct pci_resource **head)
273 struct pci_resource *current_res;
274 struct pci_resource *next_res;
275 int out_of_order = 1;
280 if (!((*head)->next))
283 while (out_of_order) {
286 /* Special case for swapping list head */
287 if (((*head)->next) &&
288 ((*head)->length > (*head)->next->length)) {
291 *head = (*head)->next;
292 current_res->next = (*head)->next;
293 (*head)->next = current_res;
298 while (current_res->next && current_res->next->next) {
299 if (current_res->next->length > current_res->next->next->length) {
301 next_res = current_res->next;
302 current_res->next = current_res->next->next;
303 current_res = current_res->next;
304 next_res->next = current_res->next;
305 current_res->next = next_res;
307 current_res = current_res->next;
309 } /* End of out_of_order loop */
318 * Sorts nodes on the list by their length.
322 static int sort_by_max_size(struct pci_resource **head)
324 struct pci_resource *current_res;
325 struct pci_resource *next_res;
326 int out_of_order = 1;
331 if (!((*head)->next))
334 while (out_of_order) {
337 /* Special case for swapping list head */
338 if (((*head)->next) &&
339 ((*head)->length < (*head)->next->length)) {
342 *head = (*head)->next;
343 current_res->next = (*head)->next;
344 (*head)->next = current_res;
349 while (current_res->next && current_res->next->next) {
350 if (current_res->next->length < current_res->next->next->length) {
352 next_res = current_res->next;
353 current_res->next = current_res->next->next;
354 current_res = current_res->next;
355 next_res->next = current_res->next;
356 current_res->next = next_res;
358 current_res = current_res->next;
360 } /* End of out_of_order loop */
367 * do_pre_bridge_resource_split
369 * Returns zero or one node of resources that aren't in use
372 static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
374 struct pci_resource *prevnode = NULL;
375 struct pci_resource *node;
376 struct pci_resource *split_node;
379 dbg("do_pre_bridge_resource_split\n");
381 if (!(*head) || !(*orig_head))
384 rc = shpchp_resource_sort_and_combine(head);
389 if ((*head)->base != (*orig_head)->base)
392 if ((*head)->length == (*orig_head)->length)
396 /* If we got here, there the bridge requires some of the resource, but
397 * we may be able to split some off of the front
401 if (node->length & (alignment -1)) {
402 /* This one isn't an aligned length, so we'll make a new entry
405 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
410 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
412 split_node->base = node->base;
413 split_node->length = temp_dword;
415 node->length -= temp_dword;
416 node->base += split_node->length;
418 /* Put it in the list */
420 split_node->next = node;
423 if (node->length < alignment) {
433 while (prevnode->next != node)
434 prevnode = prevnode->next;
436 prevnode->next = node->next;
445 * do_bridge_resource_split
447 * Returns zero or one node of resources that aren't in use
450 static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
452 struct pci_resource *prevnode = NULL;
453 struct pci_resource *node;
460 rc = shpchp_resource_sort_and_combine(head);
473 if (node->length < alignment) {
478 if (node->base & (alignment - 1)) {
479 /* Short circuit if adjusted size is too small */
480 temp_dword = (node->base | (alignment-1)) + 1;
481 if ((node->length - (temp_dword - node->base)) < alignment) {
486 node->length -= (temp_dword - node->base);
487 node->base = temp_dword;
490 if (node->length & (alignment - 1)) {
491 /* There's stuff in use after this node */
503 * this function sorts the resource list by size and then
504 * returns the first node of "size" length that is not in the
505 * ISA aliasing window. If it finds a node larger than "size"
506 * it will split it up.
508 * size must be a power of two.
510 static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
512 struct pci_resource *prevnode;
513 struct pci_resource *node;
514 struct pci_resource *split_node = NULL;
520 if ( shpchp_resource_sort_and_combine(head) )
523 if ( sort_by_size(head) )
526 for (node = *head; node; node = node->next) {
527 if (node->length < size)
530 if (node->base & (size - 1)) {
531 /* This one isn't base aligned properly
532 so we'll make a new entry and split it up */
533 temp_dword = (node->base | (size-1)) + 1;
535 /*/ Short circuit if adjusted size is too small */
536 if ((node->length - (temp_dword - node->base)) < size)
539 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
544 split_node->base = node->base;
545 split_node->length = temp_dword - node->base;
546 node->base = temp_dword;
547 node->length -= split_node->length;
549 /* Put it in the list */
550 split_node->next = node->next;
551 node->next = split_node;
552 } /* End of non-aligned base */
554 /* Don't need to check if too small since we already did */
555 if (node->length > size) {
556 /* This one is longer than we need
557 so we'll make a new entry and split it up */
558 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
563 split_node->base = node->base + size;
564 split_node->length = node->length - size;
567 /* Put it in the list */
568 split_node->next = node->next;
569 node->next = split_node;
570 } /* End of too big on top end */
572 /* For IO make sure it's not in the ISA aliasing space */
573 if (node->base & 0x300L)
576 /* If we got here, then it is the right size
577 Now take it out of the list */
582 while (prevnode->next != node)
583 prevnode = prevnode->next;
585 prevnode->next = node->next;
599 * Gets the largest node that is at least "size" big from the
600 * list pointed to by head. It aligns the node on top and bottom
601 * to "size" alignment before returning it.
602 * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
603 * This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
605 static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
607 struct pci_resource *max;
608 struct pci_resource *temp;
609 struct pci_resource *split_node;
611 u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
617 if (shpchp_resource_sort_and_combine(head))
620 if (sort_by_max_size(head))
623 for (max = *head;max; max = max->next) {
625 /* If not big enough we could probably just bail,
626 instead we'll continue to the next. */
627 if (max->length < size)
630 if (max->base & (size - 1)) {
631 /* This one isn't base aligned properly
632 so we'll make a new entry and split it up */
633 temp_dword = (max->base | (size-1)) + 1;
635 /* Short circuit if adjusted size is too small */
636 if ((max->length - (temp_dword - max->base)) < size)
639 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
644 split_node->base = max->base;
645 split_node->length = temp_dword - max->base;
646 max->base = temp_dword;
647 max->length -= split_node->length;
649 /* Put it next in the list */
650 split_node->next = max->next;
651 max->next = split_node;
654 if ((max->base + max->length) & (size - 1)) {
655 /* This one isn't end aligned properly at the top
656 so we'll make a new entry and split it up */
657 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
661 temp_dword = ((max->base + max->length) & ~(size - 1));
662 split_node->base = temp_dword;
663 split_node->length = max->length + max->base
665 max->length -= split_node->length;
667 /* Put it in the list */
668 split_node->next = max->next;
669 max->next = split_node;
672 /* Make sure it didn't shrink too much when we aligned it */
673 if (max->length < size)
676 for ( i = 0; max_size[i] > size; i++) {
677 if (max->length > max_size[i]) {
678 split_node = kmalloc(sizeof(*split_node),
681 break; /* return (NULL); */
682 split_node->base = max->base + max_size[i];
683 split_node->length = max->length - max_size[i];
684 max->length = max_size[i];
685 /* Put it next in the list */
686 split_node->next = max->next;
687 max->next = split_node;
692 /* Now take it out of the list */
693 temp = (struct pci_resource*) *head;
697 while (temp && temp->next != max) {
701 temp->next = max->next;
708 /* If we get here, we couldn't find one */
716 * this function sorts the resource list by size and then
717 * returns the first node of "size" length. If it finds a node
718 * larger than "size" it will split it up.
720 * size must be a power of two.
722 static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
724 struct pci_resource *prevnode;
725 struct pci_resource *node;
726 struct pci_resource *split_node;
732 if ( shpchp_resource_sort_and_combine(head) )
735 if ( sort_by_size(head) )
738 for (node = *head; node; node = node->next) {
739 dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
740 __FUNCTION__, size, node, node->base, node->length);
741 if (node->length < size)
744 if (node->base & (size - 1)) {
745 dbg("%s: not aligned\n", __FUNCTION__);
746 /* this one isn't base aligned properly
747 so we'll make a new entry and split it up */
748 temp_dword = (node->base | (size-1)) + 1;
750 /* Short circuit if adjusted size is too small */
751 if ((node->length - (temp_dword - node->base)) < size)
754 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
759 split_node->base = node->base;
760 split_node->length = temp_dword - node->base;
761 node->base = temp_dword;
762 node->length -= split_node->length;
764 /* Put it in the list */
765 split_node->next = node->next;
766 node->next = split_node;
767 } /* End of non-aligned base */
769 /* Don't need to check if too small since we already did */
770 if (node->length > size) {
771 dbg("%s: too big\n", __FUNCTION__);
772 /* this one is longer than we need
773 so we'll make a new entry and split it up */
774 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
779 split_node->base = node->base + size;
780 split_node->length = node->length - size;
783 /* Put it in the list */
784 split_node->next = node->next;
785 node->next = split_node;
786 } /* End of too big on top end */
788 dbg("%s: got one!!!\n", __FUNCTION__);
789 /* If we got here, then it is the right size
790 Now take it out of the list */
795 while (prevnode->next != node)
796 prevnode = prevnode->next;
798 prevnode->next = node->next;
809 * shpchp_resource_sort_and_combine
811 * Sorts all of the nodes in the list in ascending order by
812 * their base addresses. Also does garbage collection by
813 * combining adjacent nodes.
815 * returns 0 if success
817 int shpchp_resource_sort_and_combine(struct pci_resource **head)
819 struct pci_resource *node1;
820 struct pci_resource *node2;
821 int out_of_order = 1;
823 dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
828 dbg("*head->next = %p\n",(*head)->next);
831 return(0); /* only one item on the list, already sorted! */
833 dbg("*head->base = 0x%x\n",(*head)->base);
834 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
835 while (out_of_order) {
838 /* Special case for swapping list head */
839 if (((*head)->next) &&
840 ((*head)->base > (*head)->next->base)) {
842 (*head) = (*head)->next;
843 node1->next = (*head)->next;
844 (*head)->next = node1;
850 while (node1->next && node1->next->next) {
851 if (node1->next->base > node1->next->next->base) {
854 node1->next = node1->next->next;
856 node2->next = node1->next;
861 } /* End of out_of_order loop */
865 while (node1 && node1->next) {
866 if ((node1->base + node1->length) == node1->next->base) {
869 node1->length += node1->next->length;
871 node1->next = node1->next->next;
882 * shpchp_slot_create - Creates a node and adds it to the proper bus.
883 * @busnumber - bus where new node is to be located
885 * Returns pointer to the new node or NULL if unsuccessful
887 struct pci_func *shpchp_slot_create(u8 busnumber)
889 struct pci_func *new_slot;
890 struct pci_func *next;
892 new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
894 if (new_slot == NULL) {
898 memset(new_slot, 0, sizeof(struct pci_func));
900 new_slot->next = NULL;
901 new_slot->configured = 1;
903 if (shpchp_slot_list[busnumber] == NULL) {
904 shpchp_slot_list[busnumber] = new_slot;
906 next = shpchp_slot_list[busnumber];
907 while (next->next != NULL)
909 next->next = new_slot;
916 * slot_remove - Removes a node from the linked list of slots.
917 * @old_slot: slot to remove
919 * Returns 0 if successful, !0 otherwise.
921 static int slot_remove(struct pci_func * old_slot)
923 struct pci_func *next;
925 if (old_slot == NULL)
928 next = shpchp_slot_list[old_slot->bus];
934 if (next == old_slot) {
935 shpchp_slot_list[old_slot->bus] = old_slot->next;
936 shpchp_destroy_board_resources(old_slot);
941 while ((next->next != old_slot) && (next->next != NULL)) {
945 if (next->next == old_slot) {
946 next->next = old_slot->next;
947 shpchp_destroy_board_resources(old_slot);
956 * bridge_slot_remove - Removes a node from the linked list of slots.
957 * @bridge: bridge to remove
959 * Returns 0 if successful, !0 otherwise.
961 static int bridge_slot_remove(struct pci_func *bridge)
963 u8 subordinateBus, secondaryBus;
965 struct pci_func *next;
970 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
971 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
973 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
974 next = shpchp_slot_list[tempBus];
976 while (!slot_remove(next)) {
977 next = shpchp_slot_list[tempBus];
981 next = shpchp_slot_list[bridge->bus];
987 if (next == bridge) {
988 shpchp_slot_list[bridge->bus] = bridge->next;
993 while ((next->next != bridge) && (next->next != NULL)) {
997 if (next->next == bridge) {
998 next->next = bridge->next;
1007 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1009 * @device: device to find
1010 * @index: is 0 for first function found, 1 for the second...
1012 * Returns pointer to the node if successful, %NULL otherwise.
1014 struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
1017 struct pci_func *func;
1019 func = shpchp_slot_list[bus];
1021 if ((func == NULL) || ((func->device == device) && (index == 0)))
1024 if (func->device == device)
1027 while (func->next != NULL) {
1030 if (func->device == device)
1040 static int is_bridge(struct pci_func * func)
1042 /* Check the header type */
1043 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1050 /* The following routines constitute the bulk of the
1051 hotplug controller logic
1056 * board_added - Called after a board has been added to the system.
1058 * Turns power on for the board
1062 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1065 u8 slots_not_empty = 0;
1067 u32 temp_register = 0xFFFFFFFF;
1069 struct pci_func *new_func = NULL;
1070 struct pci_func *t_func = NULL;
1071 struct slot *p_slot, *pslot;
1072 struct resource_lists res_lists;
1073 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
1076 p_slot = shpchp_find_slot(ctrl, func->device);
1077 hp_slot = func->device - ctrl->slot_device_offset;
1079 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1081 /* Wait for exclusive access to hardware */
1082 down(&ctrl->crit_sect);
1084 /* Power on slot without connecting to bus */
1085 rc = p_slot->hpc_ops->power_on_slot(p_slot);
1087 err("%s: Failed to power on slot\n", __FUNCTION__);
1088 /* Done with exclusive hardware access */
1089 up(&ctrl->crit_sect);
1093 /* Wait for the command to complete */
1094 wait_for_ctrl_irq (ctrl);
1096 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1098 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
1099 /* Done with exclusive hardware access */
1100 up(&ctrl->crit_sect);
1104 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
1105 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
1106 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
1107 /* 0xd = PCI-X 133 Mhz 533 */
1108 /* This encoding is different from the one used in cur_bus_speed & */
1111 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
1112 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
1113 /* Done with exclusive hardware access */
1114 up(&ctrl->crit_sect);
1115 return WRONG_BUS_FREQUENCY;
1118 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
1119 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
1120 err("%s: Can't get bus operation speed\n", __FUNCTION__);
1121 /* Done with exclusive hardware access */
1122 up(&ctrl->crit_sect);
1123 return WRONG_BUS_FREQUENCY;
1126 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1127 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1128 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1129 max_bus_speed = bus_speed;
1132 /* Done with exclusive hardware access */
1133 up(&ctrl->crit_sect);
1135 rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi);
1137 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
1141 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1142 if (slot != hp_slot) {
1143 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1144 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1145 slots_not_empty |= t_func->is_a_board;
1149 switch (adapter_speed) {
1150 case PCI_SPEED_133MHz_PCIX_533:
1151 case PCI_SPEED_133MHz_PCIX_266:
1152 if ((( bus_speed < 0xa ) || (bus_speed < 0xd)) && (max_bus_speed > bus_speed) &&
1153 ((max_bus_speed <= 0xa) || (max_bus_speed <= 0xd)) && (!slots_not_empty)) {
1155 /* Wait for exclusive access to hardware */
1156 down(&ctrl->crit_sect);
1158 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1160 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1161 /* Done with exclusive hardware access */
1162 up(&ctrl->crit_sect);
1163 return WRONG_BUS_FREQUENCY;
1166 /* Wait for the command to complete */
1167 wait_for_ctrl_irq (ctrl);
1169 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1171 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1173 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1174 /* Done with exclusive hardware access */
1175 up(&ctrl->crit_sect);
1176 return WRONG_BUS_FREQUENCY;
1178 /* Done with exclusive hardware access */
1179 up(&ctrl->crit_sect);
1182 case PCI_SPEED_133MHz_PCIX_ECC:
1183 case PCI_SPEED_133MHz_PCIX:
1185 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1188 err("%s: PI is 1 \n", __FUNCTION__);
1189 return WRONG_BUS_FREQUENCY;
1192 if (mode) { /* Bus - Mode 1 ECC */
1194 if (bus_speed > 0x7) {
1195 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1196 return WRONG_BUS_FREQUENCY;
1199 if ((bus_speed < 0x7) && (max_bus_speed <= 0x7) &&
1200 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1202 /* Wait for exclusive access to hardware */
1203 down(&ctrl->crit_sect);
1205 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1207 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1208 /* Done with exclusive hardware access */
1209 up(&ctrl->crit_sect);
1210 return WRONG_BUS_FREQUENCY;
1213 /* Wait for the command to complete */
1214 wait_for_ctrl_irq (ctrl);
1216 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1218 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1220 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1221 /* Done with exclusive hardware access */
1222 up(&ctrl->crit_sect);
1223 return WRONG_BUS_FREQUENCY;
1225 /* Done with exclusive hardware access */
1226 up(&ctrl->crit_sect);
1229 if (bus_speed > 0x4) {
1230 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1231 return WRONG_BUS_FREQUENCY;
1234 if ((bus_speed < 0x4) && (max_bus_speed <= 0x4) &&
1235 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1237 /* Wait for exclusive access to hardware */
1238 down(&ctrl->crit_sect);
1240 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1242 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1243 /* Done with exclusive hardware access */
1244 up(&ctrl->crit_sect);
1245 return WRONG_BUS_FREQUENCY;
1248 /* Wait for the command to complete */
1249 wait_for_ctrl_irq (ctrl);
1251 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1253 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1255 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1256 /* Done with exclusive hardware access */
1257 up(&ctrl->crit_sect);
1258 return WRONG_BUS_FREQUENCY;
1260 /* Done with exclusive hardware access */
1261 up(&ctrl->crit_sect);
1265 case PCI_SPEED_66MHz_PCIX_ECC:
1266 case PCI_SPEED_66MHz_PCIX:
1268 rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode);
1271 err("%s: PI is 1 \n", __FUNCTION__);
1272 return WRONG_BUS_FREQUENCY;
1275 if (mode) { /* Bus - Mode 1 ECC */
1277 if (bus_speed > 0x5) {
1278 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1279 return WRONG_BUS_FREQUENCY;
1282 if ((bus_speed < 0x5) && (max_bus_speed <= 0x5) &&
1283 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1285 /* Wait for exclusive access to hardware */
1286 down(&ctrl->crit_sect);
1288 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1290 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1291 /* Done with exclusive hardware access */
1292 up(&ctrl->crit_sect);
1293 return WRONG_BUS_FREQUENCY;
1296 /* Wait for the command to complete */
1297 wait_for_ctrl_irq (ctrl);
1299 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1301 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1303 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1304 /* Done with exclusive hardware access */
1305 up(&ctrl->crit_sect);
1306 return WRONG_BUS_FREQUENCY;
1308 /* Done with exclusive hardware access */
1309 up(&ctrl->crit_sect);
1312 if (bus_speed > 0x2) {
1313 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1314 return WRONG_BUS_FREQUENCY;
1317 if ((bus_speed < 0x2) && (max_bus_speed <= 0x2) &&
1318 (bus_speed < max_bus_speed) && (!slots_not_empty)) {
1320 /* Wait for exclusive access to hardware */
1321 down(&ctrl->crit_sect);
1323 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1325 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1326 /* Done with exclusive hardware access */
1327 up(&ctrl->crit_sect);
1328 return WRONG_BUS_FREQUENCY;
1331 /* Wait for the command to complete */
1332 wait_for_ctrl_irq (ctrl);
1334 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1336 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1338 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1339 /* Done with exclusive hardware access */
1340 up(&ctrl->crit_sect);
1341 return WRONG_BUS_FREQUENCY;
1343 /* Done with exclusive hardware access */
1344 up(&ctrl->crit_sect);
1348 case PCI_SPEED_66MHz:
1349 if (bus_speed > 0x1) {
1350 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1351 return WRONG_BUS_FREQUENCY;
1353 if (bus_speed == 0x1)
1355 if ((bus_speed == 0x0) && ( max_bus_speed == 0x1)) {
1356 /* Wait for exclusive access to hardware */
1357 down(&ctrl->crit_sect);
1359 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1361 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1362 /* Done with exclusive hardware access */
1363 up(&ctrl->crit_sect);
1364 return WRONG_BUS_FREQUENCY;
1367 /* Wait for the command to complete */
1368 wait_for_ctrl_irq (ctrl);
1370 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1372 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1374 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1375 /* Done with exclusive hardware access */
1376 up(&ctrl->crit_sect);
1377 return WRONG_BUS_FREQUENCY;
1379 /* Done with exclusive hardware access */
1380 up(&ctrl->crit_sect);
1383 case PCI_SPEED_33MHz:
1384 if (bus_speed > 0x0) {
1385 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1386 return WRONG_BUS_FREQUENCY;
1390 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1391 return WRONG_BUS_FREQUENCY;
1394 /* if adpater_speed == bus_speed, nothing to do here */
1395 if (adapter_speed != bus_speed) {
1396 for ( slot = 0; slot < ctrl->num_slots; slot++) {
1397 if (slot != hp_slot) {
1398 pslot = shpchp_find_slot(ctrl, slot + ctrl->slot_device_offset);
1399 t_func = shpchp_slot_find(pslot->bus, pslot->device, 0);
1400 slots_not_empty |= t_func->is_a_board;
1404 if (slots_not_empty != 0) { /* Other slots on the same bus are occupied */
1405 if ( adapter_speed < bus_speed ) {
1406 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1407 return WRONG_BUS_FREQUENCY;
1409 /* Do nothing if adapter_speed >= bus_speed */
1413 if ((adapter_speed != bus_speed) && (slots_not_empty == 0)) {
1414 /* Other slots on the same bus are empty */
1416 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1417 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1418 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1419 max_bus_speed = bus_speed;
1422 if (max_bus_speed == bus_speed) {
1423 /* if adapter_speed >= bus_speed, do nothing */
1424 if (adapter_speed < bus_speed) {
1426 * Try to lower bus speed to accommodate the adapter if other slots
1427 * on the same controller are empty
1430 /* Wait for exclusive access to hardware */
1431 down(&ctrl->crit_sect);
1433 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1435 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1436 up(&ctrl->crit_sect);
1437 return WRONG_BUS_FREQUENCY;
1440 /* Wait for the command to complete */
1441 wait_for_ctrl_irq (ctrl);
1443 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1445 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1447 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1448 up(&ctrl->crit_sect);
1449 return WRONG_BUS_FREQUENCY;
1451 /* Done with exclusive hardware access */
1452 up(&ctrl->crit_sect);
1456 /* Wait for exclusive access to hardware */
1457 down(&ctrl->crit_sect);
1459 /* max_bus_speed != bus_speed. Note: max_bus_speed should be > than bus_speed */
1460 if (adapter_speed < max_bus_speed)
1461 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, adapter_speed);
1463 rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, max_bus_speed);
1466 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1467 /* Done with exclusive hardware access */
1468 up(&ctrl->crit_sect);
1469 return WRONG_BUS_FREQUENCY;
1472 /* Wait for the command to complete */
1473 wait_for_ctrl_irq (ctrl);
1475 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1477 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1479 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1480 /* Done with exclusive hardware access */
1481 up(&ctrl->crit_sect);
1482 return WRONG_BUS_FREQUENCY;
1484 /* Done with exclusive hardware access */
1485 up(&ctrl->crit_sect);
1491 /* Wait for exclusive access to hardware */
1492 down(&ctrl->crit_sect);
1494 /* turn on board, blink green LED, turn off Amber LED */
1495 rc = p_slot->hpc_ops->slot_enable(p_slot);
1498 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1499 /* Done with exclusive hardware access */
1500 up(&ctrl->crit_sect);
1503 /* Wait for the command to complete */
1504 wait_for_ctrl_irq (ctrl);
1506 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1508 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1509 /* Done with exclusive hardware access */
1510 up(&ctrl->crit_sect);
1514 /* Done with exclusive hardware access */
1515 up(&ctrl->crit_sect);
1517 /* Wait for ~1 second */
1518 dbg("%s: before long_delay\n", __FUNCTION__);
1519 wait_for_ctrl_irq (ctrl);
1520 dbg("%s: afterlong_delay\n", __FUNCTION__);
1522 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1523 /* Check for a power fault */
1524 if (func->status == 0xFF) {
1525 /* power fault occurred, but it was benign */
1526 temp_register = 0xFFFFFFFF;
1527 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1531 /* Get vendor/device ID u32 */
1532 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
1533 PCI_VENDOR_ID, &temp_register);
1534 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1535 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1538 /* Something's wrong here */
1539 temp_register = 0xFFFFFFFF;
1540 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1542 /* Preset return code. It will be changed later if things go okay. */
1543 rc = NO_ADAPTER_PRESENT;
1546 /* All F's is an empty slot or an invalid board */
1547 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1548 res_lists.io_head = ctrl->io_head;
1549 res_lists.mem_head = ctrl->mem_head;
1550 res_lists.p_mem_head = ctrl->p_mem_head;
1551 res_lists.bus_head = ctrl->bus_head;
1552 res_lists.irqs = NULL;
1554 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1555 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1557 ctrl->io_head = res_lists.io_head;
1558 ctrl->mem_head = res_lists.mem_head;
1559 ctrl->p_mem_head = res_lists.p_mem_head;
1560 ctrl->bus_head = res_lists.bus_head;
1562 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1563 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1564 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1565 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1568 /* Wait for exclusive access to hardware */
1569 down(&ctrl->crit_sect);
1571 /* turn off slot, turn on Amber LED, turn off Green LED */
1572 retval = p_slot->hpc_ops->slot_disable(p_slot);
1574 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1575 /* Done with exclusive hardware access */
1576 up(&ctrl->crit_sect);
1579 /* Wait for the command to complete */
1580 wait_for_ctrl_irq (ctrl);
1582 retval = p_slot->hpc_ops->check_cmd_status(ctrl);
1584 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, retval);
1585 /* Done with exclusive hardware access */
1586 up(&ctrl->crit_sect);
1590 /* Done with exclusive hardware access */
1591 up(&ctrl->crit_sect);
1595 shpchp_save_slot_config(ctrl, func);
1598 func->switch_save = 0x10;
1599 func->is_a_board = 0x01;
1602 /* Next, we will instantiate the linux pci_dev structures
1603 * (with appropriate driver notification, if already present)
1607 new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
1608 if (new_func && !new_func->pci_dev) {
1609 dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
1610 shpchp_configure_device(ctrl, new_func);
1614 /* Wait for exclusive access to hardware */
1615 down(&ctrl->crit_sect);
1617 p_slot->hpc_ops->green_led_on(p_slot);
1619 /* Wait for the command to complete */
1620 wait_for_ctrl_irq (ctrl);
1623 /* Done with exclusive hardware access */
1624 up(&ctrl->crit_sect);
1627 /* Wait for exclusive access to hardware */
1628 down(&ctrl->crit_sect);
1630 /* turn off slot, turn on Amber LED, turn off Green LED */
1631 rc = p_slot->hpc_ops->slot_disable(p_slot);
1633 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1634 /* Done with exclusive hardware access */
1635 up(&ctrl->crit_sect);
1638 /* Wait for the command to complete */
1639 wait_for_ctrl_irq (ctrl);
1641 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1643 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1644 /* Done with exclusive hardware access */
1645 up(&ctrl->crit_sect);
1649 /* Done with exclusive hardware access */
1650 up(&ctrl->crit_sect);
1659 * remove_board - Turns off slot and LED's
1662 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1669 struct resource_lists res_lists;
1670 struct pci_func *temp_func;
1671 struct slot *p_slot;
1676 if (shpchp_unconfigure_device(func))
1679 device = func->device;
1681 hp_slot = func->device - ctrl->slot_device_offset;
1682 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1684 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1686 if ((ctrl->add_support) &&
1687 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1688 /* Here we check to see if we've saved any of the board's
1689 * resources already. If so, we'll skip the attempt to
1690 * determine what's being used.
1696 while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
1697 if (temp_func->bus_head || temp_func->mem_head
1698 || temp_func->p_mem_head || temp_func->io_head) {
1705 rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
1707 /* Change status to shutdown */
1708 if (func->is_a_board)
1709 func->status = 0x01;
1710 func->configured = 0;
1712 /* Wait for exclusive access to hardware */
1713 down(&ctrl->crit_sect);
1715 /* turn off slot, turn on Amber LED, turn off Green LED */
1716 rc = p_slot->hpc_ops->slot_disable(p_slot);
1718 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1719 /* Done with exclusive hardware access */
1720 up(&ctrl->crit_sect);
1723 /* Wait for the command to complete */
1724 wait_for_ctrl_irq (ctrl);
1726 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1728 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1729 /* Done with exclusive hardware access */
1730 up(&ctrl->crit_sect);
1734 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
1736 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
1737 /* Done with exclusive hardware access */
1738 up(&ctrl->crit_sect);
1741 /* Wait for the command to complete */
1742 wait_for_ctrl_irq (ctrl);
1744 /* Done with exclusive hardware access */
1745 up(&ctrl->crit_sect);
1747 if (ctrl->add_support) {
1749 res_lists.io_head = ctrl->io_head;
1750 res_lists.mem_head = ctrl->mem_head;
1751 res_lists.p_mem_head = ctrl->p_mem_head;
1752 res_lists.bus_head = ctrl->bus_head;
1754 dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", func->bus,
1755 func->device, func->function);
1757 shpchp_return_board_resources(func, &res_lists);
1759 ctrl->io_head = res_lists.io_head;
1760 ctrl->mem_head = res_lists.mem_head;
1761 ctrl->p_mem_head = res_lists.p_mem_head;
1762 ctrl->bus_head = res_lists.bus_head;
1764 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1765 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1766 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1767 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1769 if (is_bridge(func)) {
1770 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1771 func->device, func->function);
1772 bridge_slot_remove(func);
1774 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1775 func->device, func->function);
1778 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
1781 /* Setup slot structure with entry for empty slot */
1782 func = shpchp_slot_create(ctrl->slot_bus);
1788 func->bus = ctrl->slot_bus;
1789 func->device = device;
1791 func->configured = 0;
1792 func->switch_save = 0x10;
1794 func->is_a_board = 0;
1801 static void pushbutton_helper_thread (unsigned long data)
1803 pushbutton_pending = data;
1805 up(&event_semaphore);
1810 * shpchp_pushbutton_thread
1812 * Scheduled procedure to handle blocking stuff for the pushbuttons
1813 * Handles all pending events and exits.
1816 static void shpchp_pushbutton_thread (unsigned long slot)
1818 struct slot *p_slot = (struct slot *) slot;
1821 pushbutton_pending = 0;
1824 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1828 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1830 p_slot->state = POWEROFF_STATE;
1831 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1833 shpchp_disable_slot(p_slot);
1834 p_slot->state = STATIC_STATE;
1836 p_slot->state = POWERON_STATE;
1837 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1839 if (shpchp_enable_slot(p_slot)) {
1840 /* Wait for exclusive access to hardware */
1841 down(&p_slot->ctrl->crit_sect);
1843 p_slot->hpc_ops->green_led_off(p_slot);
1845 /* Wait for the command to complete */
1846 wait_for_ctrl_irq (p_slot->ctrl);
1848 /* Done with exclusive hardware access */
1849 up(&p_slot->ctrl->crit_sect);
1851 p_slot->state = STATIC_STATE;
1858 /* this is the main worker thread */
1859 static int event_thread(void* data)
1861 struct controller *ctrl;
1863 daemonize("shpchpd_event");
1867 dbg("!!!!event_thread sleeping\n");
1868 down_interruptible (&event_semaphore);
1869 dbg("event_thread woken finished = %d\n", event_finished);
1870 if (event_finished || signal_pending(current))
1873 if (pushbutton_pending)
1874 shpchp_pushbutton_thread(pushbutton_pending);
1876 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
1877 interrupt_event_handler(ctrl);
1879 dbg("event_thread signals exit\n");
1884 int shpchp_event_start_thread (void)
1888 /* initialize our semaphores */
1889 init_MUTEX_LOCKED(&event_exit);
1892 init_MUTEX_LOCKED(&event_semaphore);
1893 pid = kernel_thread(event_thread, NULL, 0);
1896 err ("Can't start up our event thread\n");
1899 dbg("Our event thread pid = %d\n", pid);
1904 void shpchp_event_stop_thread (void)
1907 dbg("event_thread finish command given\n");
1908 up(&event_semaphore);
1909 dbg("wait for event_thread to exit\n");
1914 static int update_slot_info (struct slot *slot)
1916 struct hotplug_slot_info *info;
1919 info = kmalloc(sizeof(*info), GFP_KERNEL);
1923 slot->hpc_ops->get_power_status(slot, &(info->power_status));
1924 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1925 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1926 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1928 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1933 static void interrupt_event_handler(struct controller *ctrl)
1937 struct pci_func *func;
1940 struct slot *p_slot;
1942 dbg("%s:\n", __FUNCTION__);
1946 for (loop = 0; loop < 10; loop++) {
1947 if (ctrl->event_queue[loop].event_type != 0) {
1948 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1949 ctrl->event_queue[loop].event_type);
1950 hp_slot = ctrl->event_queue[loop].hp_slot;
1952 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1954 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1956 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1958 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1959 dbg("%s: button cancel\n", __FUNCTION__);
1960 del_timer(&p_slot->task_event);
1962 switch (p_slot->state) {
1963 case BLINKINGOFF_STATE:
1964 /* Wait for exclusive access to hardware */
1965 down(&ctrl->crit_sect);
1967 p_slot->hpc_ops->green_led_on(p_slot);
1968 /* Wait for the command to complete */
1969 wait_for_ctrl_irq (ctrl);
1971 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1973 /* Wait for the command to complete */
1974 wait_for_ctrl_irq (ctrl);
1976 /* Done with exclusive hardware access */
1977 up(&ctrl->crit_sect);
1979 case BLINKINGON_STATE:
1980 /* Wait for exclusive access to hardware */
1981 down(&ctrl->crit_sect);
1983 p_slot->hpc_ops->green_led_off(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);
1988 /* Wait for the command to complete */
1989 wait_for_ctrl_irq (ctrl);
1991 /* Done with exclusive hardware access */
1992 up(&ctrl->crit_sect);
1996 warn("Not a valid state\n");
1999 info(msg_button_cancel, p_slot->number);
2000 p_slot->state = STATIC_STATE;
2001 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
2002 /* Button Pressed (No action on 1st press...) */
2003 dbg("%s: Button pressed\n", __FUNCTION__);
2005 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2008 dbg("%s: slot is on\n", __FUNCTION__);
2009 p_slot->state = BLINKINGOFF_STATE;
2010 info(msg_button_off, p_slot->number);
2013 dbg("%s: slot is off\n", __FUNCTION__);
2014 p_slot->state = BLINKINGON_STATE;
2015 info(msg_button_on, p_slot->number);
2018 /* Wait for exclusive access to hardware */
2019 down(&ctrl->crit_sect);
2021 /* blink green LED and turn off amber */
2022 p_slot->hpc_ops->green_led_blink(p_slot);
2023 /* Wait for the command to complete */
2024 wait_for_ctrl_irq (ctrl);
2026 p_slot->hpc_ops->set_attention_status(p_slot, 0);
2028 /* Wait for the command to complete */
2029 wait_for_ctrl_irq (ctrl);
2031 /* Done with exclusive hardware access */
2032 up(&ctrl->crit_sect);
2034 init_timer(&p_slot->task_event);
2035 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
2036 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
2037 p_slot->task_event.data = (unsigned long) p_slot;
2039 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
2040 add_timer(&p_slot->task_event);
2041 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
2042 /***********POWER FAULT********************/
2043 dbg("%s: power fault\n", __FUNCTION__);
2044 /* Wait for exclusive access to hardware */
2045 down(&ctrl->crit_sect);
2047 p_slot->hpc_ops->set_attention_status(p_slot, 1);
2048 /* Wait for the command to complete */
2049 wait_for_ctrl_irq (ctrl);
2051 p_slot->hpc_ops->green_led_off(p_slot);
2052 /* Wait for the command to complete */
2053 wait_for_ctrl_irq (ctrl);
2055 /* Done with exclusive hardware access */
2056 up(&ctrl->crit_sect);
2058 /* refresh notification */
2060 update_slot_info(p_slot);
2063 ctrl->event_queue[loop].event_type = 0;
2067 } /* End of FOR loop */
2074 int shpchp_enable_slot (struct slot *p_slot)
2078 struct pci_func *func;
2080 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2082 dbg("%s: Error! slot NULL\n", __FUNCTION__);
2086 /* Check to see if (latch closed, card present, power off) */
2087 down(&p_slot->ctrl->crit_sect);
2088 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2089 if (rc || !getstatus) {
2090 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2091 up(&p_slot->ctrl->crit_sect);
2094 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2095 if (rc || getstatus) {
2096 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2097 up(&p_slot->ctrl->crit_sect);
2100 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2101 if (rc || getstatus) {
2102 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
2103 up(&p_slot->ctrl->crit_sect);
2106 up(&p_slot->ctrl->crit_sect);
2110 func = shpchp_slot_create(p_slot->bus);
2114 func->bus = p_slot->bus;
2115 func->device = p_slot->device;
2117 func->configured = 0;
2118 func->is_a_board = 1;
2120 /* We have to save the presence info for these slots */
2121 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2122 p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
2123 dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
2124 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2125 func->switch_save = !getstatus? 0x10:0;
2127 rc = board_added(func, p_slot->ctrl);
2129 if (is_bridge(func))
2130 bridge_slot_remove(func);
2134 /* Setup slot structure with entry for empty slot */
2135 func = shpchp_slot_create(p_slot->bus);
2137 return (1); /* Out of memory */
2139 func->bus = p_slot->bus;
2140 func->device = p_slot->device;
2142 func->configured = 0;
2143 func->is_a_board = 1;
2145 /* We have to save the presence info for these slots */
2146 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
2147 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2148 func->switch_save = !getstatus? 0x10:0;
2152 update_slot_info(p_slot);
2158 int shpchp_disable_slot (struct slot *p_slot)
2160 u8 class_code, header_type, BCR;
2166 struct pci_bus *pci_bus;
2167 struct pci_func *func;
2172 pci_bus = p_slot->ctrl->pci_dev->subordinate;
2174 /* Check to see if (latch closed, card present, power on) */
2175 down(&p_slot->ctrl->crit_sect);
2177 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
2178 if (ret || !getstatus) {
2179 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
2180 up(&p_slot->ctrl->crit_sect);
2183 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
2184 if (ret || getstatus) {
2185 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
2186 up(&p_slot->ctrl->crit_sect);
2189 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
2190 if (ret || !getstatus) {
2191 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
2192 up(&p_slot->ctrl->crit_sect);
2195 up(&p_slot->ctrl->crit_sect);
2197 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2199 /* Make sure there are no video controllers here
2200 * for all func of p_slot
2202 while (func && !rc) {
2203 pci_bus->number = func->bus;
2204 devfn = PCI_DEVFN(func->device, func->function);
2206 /* Check the Class Code */
2207 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2211 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2212 /* Display/Video adapter (not supported) */
2213 rc = REMOVE_NOT_SUPPORTED;
2215 /* See if it's a bridge */
2216 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2220 /* If it's a bridge, check the VGA Enable bit */
2221 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2222 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2226 /* If the VGA Enable bit is set, remove isn't supported */
2227 if (BCR & PCI_BRIDGE_CTL_VGA) {
2228 rc = REMOVE_NOT_SUPPORTED;
2233 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2236 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2237 if ((func != NULL) && !rc) {
2238 rc = remove_board(func, p_slot->ctrl);
2243 update_slot_info(p_slot);
2250 * configure_new_device - Configures the PCI header information of one board.
2252 * @ctrl: pointer to controller structure
2253 * @func: pointer to function structure
2254 * @behind_bridge: 1 if this is a recursive call, 0 if not
2255 * @resources: pointer to set of resource lists
2257 * Returns 0 if success
2260 static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
2261 u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
2263 u8 temp_byte, function, max_functions, stop_it;
2266 struct pci_func *new_slot;
2267 struct pci_bus lpci_bus, *pci_bus;
2272 dbg("%s\n", __FUNCTION__);
2273 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2274 pci_bus = &lpci_bus;
2275 pci_bus->number = func->bus;
2277 /* Check for Multi-function device */
2278 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2280 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2284 if (temp_byte & 0x80) /* Multi-function device */
2292 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
2295 dbg("configure_new_function failed %d\n",rc);
2299 new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
2302 shpchp_return_board_resources(new_slot, resources);
2312 /* The following loop skips to the next present function
2313 * and creates a board structure
2316 while ((function < max_functions) && (!stop_it)) {
2317 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2319 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2321 } else { /* There's something there */
2322 /* Setup slot structure. */
2323 new_slot = shpchp_slot_create(func->bus);
2325 if (new_slot == NULL) {
2330 new_slot->bus = func->bus;
2331 new_slot->device = func->device;
2332 new_slot->function = function;
2333 new_slot->is_a_board = 1;
2334 new_slot->status = 0;
2340 } while (function < max_functions);
2341 dbg("returning from configure_new_device\n");
2348 * Configuration logic that involves the hotplug data structures and
2354 * configure_new_function - Configures the PCI header information of one device
2356 * @ctrl: pointer to controller structure
2357 * @func: pointer to function structure
2358 * @behind_bridge: 1 if this is a recursive call, 0 if not
2359 * @resources: pointer to set of resource lists
2361 * Calls itself recursively for bridged devices.
2362 * Returns 0 if success
2365 static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2366 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
2378 struct pci_resource *mem_node;
2379 struct pci_resource *p_mem_node;
2380 struct pci_resource *io_node;
2381 struct pci_resource *bus_node;
2382 struct pci_resource *hold_mem_node;
2383 struct pci_resource *hold_p_mem_node;
2384 struct pci_resource *hold_IO_node;
2385 struct pci_resource *hold_bus_node;
2386 struct irq_mapping irqs;
2387 struct pci_func *new_slot;
2388 struct pci_bus lpci_bus, *pci_bus;
2389 struct resource_lists temp_resources;
2390 #if defined(CONFIG_X86_64)
2394 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2395 pci_bus = &lpci_bus;
2396 pci_bus->number = func->bus;
2397 devfn = PCI_DEVFN(func->device, func->function);
2399 /* Check for Bridge */
2400 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2404 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2405 /* set Primary bus */
2406 dbg("set Primary bus = 0x%x\n", func->bus);
2407 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2411 /* find range of busses to use */
2412 bus_node = get_max_resource(&resources->bus_head, 1L);
2414 /* If we don't have any busses to allocate, we can't continue */
2416 err("Got NO bus resource to use\n");
2419 dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2421 /* set Secondary bus */
2422 temp_byte = (u8)bus_node->base;
2423 dbg("set Secondary bus = 0x%x\n", temp_byte);
2424 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2428 /* set subordinate bus */
2429 temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2430 dbg("set subordinate bus = 0x%x\n", temp_byte);
2431 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2435 /* Set HP parameters (Cache Line Size, Latency Timer) */
2436 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2440 /* Setup the IO, memory, and prefetchable windows */
2442 io_node = get_max_resource(&(resources->io_head), 0x1000L);
2444 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2447 mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2449 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2452 if (resources->p_mem_head)
2453 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2456 * In some platform implementation, MEM and PMEM are not
2457 * distinguished, and hence ACPI _CRS has only MEM entries
2458 * for both MEM and PMEM.
2460 dbg("using MEM for PMEM\n");
2461 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2464 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2467 /* set up the IRQ info */
2468 if (!resources->irqs) {
2469 irqs.barber_pole = 0;
2470 irqs.interrupt[0] = 0;
2471 irqs.interrupt[1] = 0;
2472 irqs.interrupt[2] = 0;
2473 irqs.interrupt[3] = 0;
2476 irqs.barber_pole = resources->irqs->barber_pole;
2477 irqs.interrupt[0] = resources->irqs->interrupt[0];
2478 irqs.interrupt[1] = resources->irqs->interrupt[1];
2479 irqs.interrupt[2] = resources->irqs->interrupt[2];
2480 irqs.interrupt[3] = resources->irqs->interrupt[3];
2481 irqs.valid_INT = resources->irqs->valid_INT;
2484 /* set up resource lists that are now aligned on top and bottom
2485 * for anything behind the bridge.
2487 temp_resources.bus_head = bus_node;
2488 temp_resources.io_head = io_node;
2489 temp_resources.mem_head = mem_node;
2490 temp_resources.p_mem_head = p_mem_node;
2491 temp_resources.irqs = &irqs;
2493 /* Make copies of the nodes we are going to pass down so that
2494 * if there is a problem,we can just use these to free resources
2496 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2497 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2498 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2499 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2501 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2502 kfree(hold_bus_node);
2503 kfree(hold_IO_node);
2504 kfree(hold_mem_node);
2505 kfree(hold_p_mem_node);
2510 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2512 bus_node->base += 1;
2513 bus_node->length -= 1;
2514 bus_node->next = NULL;
2516 /* If we have IO resources copy them and fill in the bridge's
2517 * IO range registers
2520 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2521 io_node->next = NULL;
2523 /* set IO base and Limit registers */
2524 RES_CHECK(io_node->base, 8);
2525 temp_byte = (u8)(io_node->base >> 8);
2526 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2528 RES_CHECK(io_node->base + io_node->length - 1, 8);
2529 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2530 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2532 kfree(hold_IO_node);
2533 hold_IO_node = NULL;
2536 /* If we have memory resources copy them and fill in the bridge's
2537 * memory range registers. Otherwise, fill in the range
2538 * registers with values that disable them.
2541 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2542 mem_node->next = NULL;
2544 /* set Mem base and Limit registers */
2545 RES_CHECK(mem_node->base, 16);
2546 temp_word = (u32)(mem_node->base >> 16);
2547 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2549 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
2550 temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
2551 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2554 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2557 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2559 kfree(hold_mem_node);
2560 hold_mem_node = NULL;
2563 /* If we have prefetchable memory resources copy them and
2564 * fill in the bridge's memory range registers. Otherwise,
2565 * fill in the range registers with values that disable them.
2568 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2569 p_mem_node->next = NULL;
2571 /* set Pre Mem base and Limit registers */
2572 RES_CHECK(p_mem_node->base, 16);
2573 temp_word = (u32)(p_mem_node->base >> 16);
2574 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2576 RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
2577 temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
2578 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2581 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2584 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2586 kfree(hold_p_mem_node);
2587 hold_p_mem_node = NULL;
2590 /* Adjust this to compensate for extra adjustment in first loop */
2595 /* Here we actually find the devices and configure them */
2596 for (device = 0; (device <= 0x1F) && !rc; device++) {
2597 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2600 pci_bus->number = hold_bus_node->base;
2601 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
2602 PCI_VENDOR_ID, &ID);
2603 pci_bus->number = func->bus;
2605 if (ID != 0xFFFFFFFF) { /* device Present */
2606 /* Setup slot structure. */
2607 new_slot = shpchp_slot_create(hold_bus_node->base);
2609 if (new_slot == NULL) {
2615 new_slot->bus = hold_bus_node->base;
2616 new_slot->device = device;
2617 new_slot->function = 0;
2618 new_slot->is_a_board = 1;
2619 new_slot->status = 0;
2621 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
2622 dbg("configure_new_device rc=0x%x\n",rc);
2623 } /* End of IF (device in slot?) */
2624 } /* End of FOR loop */
2627 shpchp_destroy_resource_list(&temp_resources);
2629 return_resource(&(resources->bus_head), hold_bus_node);
2630 return_resource(&(resources->io_head), hold_IO_node);
2631 return_resource(&(resources->mem_head), hold_mem_node);
2632 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2636 /* save the interrupt routing information */
2637 if (resources->irqs) {
2638 resources->irqs->interrupt[0] = irqs.interrupt[0];
2639 resources->irqs->interrupt[1] = irqs.interrupt[1];
2640 resources->irqs->interrupt[2] = irqs.interrupt[2];
2641 resources->irqs->interrupt[3] = irqs.interrupt[3];
2642 resources->irqs->valid_INT = irqs.valid_INT;
2643 } else if (!behind_bridge) {
2644 /* We need to hook up the interrupts here */
2645 for (cloop = 0; cloop < 4; cloop++) {
2646 if (irqs.valid_INT & (0x01 << cloop)) {
2647 rc = shpchp_set_irq(func->bus, func->device,
2648 0x0A + cloop, irqs.interrupt[cloop]);
2650 shpchp_destroy_resource_list (&temp_resources);
2651 return_resource(&(resources->bus_head), hold_bus_node);
2652 return_resource(&(resources->io_head), hold_IO_node);
2653 return_resource(&(resources->mem_head), hold_mem_node);
2654 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2658 } /* end of for loop */
2661 /* Return unused bus resources
2662 * First use the temporary node to store information for the board
2664 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2665 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2667 hold_bus_node->next = func->bus_head;
2668 func->bus_head = hold_bus_node;
2670 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2672 /* set subordinate bus */
2673 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2674 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2676 if (temp_resources.bus_head->length == 0) {
2677 kfree(temp_resources.bus_head);
2678 temp_resources.bus_head = NULL;
2680 dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2681 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2682 return_resource(&(resources->bus_head), temp_resources.bus_head);
2686 /* If we have IO space available and there is some left,
2687 * return the unused portion
2689 if (hold_IO_node && temp_resources.io_head) {
2690 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2691 &hold_IO_node, 0x1000);
2693 /* Check if we were able to split something off */
2695 hold_IO_node->base = io_node->base + io_node->length;
2697 RES_CHECK(hold_IO_node->base, 8);
2698 temp_byte = (u8)((hold_IO_node->base) >> 8);
2699 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2701 return_resource(&(resources->io_head), io_node);
2704 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2706 /* Check if we were able to split something off */
2708 /* First use the temporary node to store information for the board */
2709 hold_IO_node->length = io_node->base - hold_IO_node->base;
2711 /* If we used any, add it to the board's list */
2712 if (hold_IO_node->length) {
2713 hold_IO_node->next = func->io_head;
2714 func->io_head = hold_IO_node;
2716 RES_CHECK(io_node->base - 1, 8);
2717 temp_byte = (u8)((io_node->base - 1) >> 8);
2718 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2720 return_resource(&(resources->io_head), io_node);
2722 /* it doesn't need any IO */
2724 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2726 return_resource(&(resources->io_head), io_node);
2727 kfree(hold_IO_node);
2730 /* it used most of the range */
2731 hold_IO_node->next = func->io_head;
2732 func->io_head = hold_IO_node;
2734 } else if (hold_IO_node) {
2735 /* it used the whole range */
2736 hold_IO_node->next = func->io_head;
2737 func->io_head = hold_IO_node;
2740 /* If we have memory space available and there is some left,
2741 * return the unused portion
2743 if (hold_mem_node && temp_resources.mem_head) {
2744 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2746 /* Check if we were able to split something off */
2748 hold_mem_node->base = mem_node->base + mem_node->length;
2750 RES_CHECK(hold_mem_node->base, 16);
2751 temp_word = (u32)((hold_mem_node->base) >> 16);
2752 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2754 return_resource(&(resources->mem_head), mem_node);
2757 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2759 /* Check if we were able to split something off */
2761 /* First use the temporary node to store information for the board */
2762 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2764 if (hold_mem_node->length) {
2765 hold_mem_node->next = func->mem_head;
2766 func->mem_head = hold_mem_node;
2768 /* configure end address */
2769 RES_CHECK(mem_node->base - 1, 16);
2770 temp_word = (u32)((mem_node->base - 1) >> 16);
2771 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2773 /* Return unused resources to the pool */
2774 return_resource(&(resources->mem_head), mem_node);
2776 /* it doesn't need any Mem */
2778 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2780 return_resource(&(resources->mem_head), mem_node);
2781 kfree(hold_mem_node);
2784 /* it used most of the range */
2785 hold_mem_node->next = func->mem_head;
2786 func->mem_head = hold_mem_node;
2788 } else if (hold_mem_node) {
2789 /* it used the whole range */
2790 hold_mem_node->next = func->mem_head;
2791 func->mem_head = hold_mem_node;
2794 /* If we have prefetchable memory space available and there is some
2795 * left at the end, return the unused portion
2797 if (hold_p_mem_node && temp_resources.p_mem_head) {
2798 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2799 &hold_p_mem_node, 0x100000L);
2801 /* Check if we were able to split something off */
2803 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2805 RES_CHECK(hold_p_mem_node->base, 16);
2806 temp_word = (u32)((hold_p_mem_node->base) >> 16);
2807 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2809 return_resource(&(resources->p_mem_head), p_mem_node);
2812 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2814 /* Check if we were able to split something off */
2816 /* First use the temporary node to store information for the board */
2817 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2819 /* If we used any, add it to the board's list */
2820 if (hold_p_mem_node->length) {
2821 hold_p_mem_node->next = func->p_mem_head;
2822 func->p_mem_head = hold_p_mem_node;
2824 RES_CHECK(p_mem_node->base - 1, 16);
2825 temp_word = (u32)((p_mem_node->base - 1) >> 16);
2826 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2828 return_resource(&(resources->p_mem_head), p_mem_node);
2830 /* it doesn't need any PMem */
2832 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2834 return_resource(&(resources->p_mem_head), p_mem_node);
2835 kfree(hold_p_mem_node);
2838 /* it used the most of the range */
2839 hold_p_mem_node->next = func->p_mem_head;
2840 func->p_mem_head = hold_p_mem_node;
2842 } else if (hold_p_mem_node) {
2843 /* it used the whole range */
2844 hold_p_mem_node->next = func->p_mem_head;
2845 func->p_mem_head = hold_p_mem_node;
2848 /* We should be configuring an IRQ and the bridge's base address
2849 * registers if it needs them. Although we have never seen such
2853 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2855 dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2856 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2857 /* Standard device */
2859 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2861 if (class_code == PCI_BASE_CLASS_DISPLAY)
2862 return (DEVICE_TYPE_NOT_SUPPORTED);
2864 /* Figure out IO and memory needs */
2865 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2866 temp_register = 0xFFFFFFFF;
2868 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2869 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2870 dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, func->bus, func->device,
2877 if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2880 /* set base = amount of IO space */
2881 base = temp_register & 0xFFFFFFFC;
2884 dbg("NEED IO length(0x%x)\n", base);
2885 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2887 /* allocate the resource to the board */
2889 dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2890 base = (u32)io_node->base;
2891 io_node->next = func->io_head;
2892 func->io_head = io_node;
2894 err("Got NO IO resource(length=0x%x)\n", base);
2897 } else { /* map MEM */
2898 int prefetchable = 1;
2899 struct pci_resource **res_node = &func->p_mem_head;
2900 char *res_type_str = "PMEM";
2903 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2905 res_node = &func->mem_head;
2909 base = temp_register & 0xFFFFFFF0;
2912 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2913 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2914 dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2916 if (prefetchable && resources->p_mem_head)
2917 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2920 dbg("using MEM for PMEM\n");
2921 mem_node=get_resource(&(resources->mem_head), (ulong)base);
2924 /* allocate the resource to the board */
2926 base = (u32)mem_node->base;
2927 mem_node->next = *res_node;
2928 *res_node = mem_node;
2929 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
2932 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2936 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2937 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2938 dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
2939 temp_register, base);
2941 if (prefetchable && resources->p_mem_head)
2942 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2945 dbg("using MEM for PMEM\n");
2946 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2949 /* allocate the resource to the board */
2951 base64 = mem_node->base;
2952 mem_node->next = *res_node;
2953 *res_node = mem_node;
2954 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
2955 (u32)base64, mem_node->length);
2957 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2962 dbg("reserved BAR type=0x%x\n", temp_register);
2969 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2974 dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2978 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2980 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2982 } /* End of base register loop */
2984 #if defined(CONFIG_X86_64)
2985 /* Figure out which interrupt pin this function uses */
2986 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
2988 /* If this function needs an interrupt and we are behind a bridge
2989 and the pin is tied to something that's alread mapped,
2990 set this one the same
2992 if (temp_byte && resources->irqs &&
2993 (resources->irqs->valid_INT &
2994 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2995 /* We have to share with something already set up */
2996 IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
2998 /* Program IRQ based on card type */
2999 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
3001 if (class_code == PCI_BASE_CLASS_STORAGE) {
3002 IRQ = shpchp_disk_irq;
3004 IRQ = shpchp_nic_irq;
3009 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
3011 if (!behind_bridge) {
3012 rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
3016 /* TBD - this code may also belong in the other clause of this If statement */
3017 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
3018 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
3021 /* Disable ROM base Address */
3023 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
3025 /* Set HP parameters (Cache Line Size, Latency Timer) */
3026 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3030 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
3032 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
3033 } /* End of Not-A-Bridge else */
3035 /* It's some strange type of PCI adapter (Cardbus?) */
3036 return(DEVICE_TYPE_NOT_SUPPORTED);
3039 func->configured = 1;