[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / pci / hotplug / shpchp_ctrl.c
blob490a9553a0625175d7ba02783988f6915fbc57b7
1 /*
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
9 * All rights reserved.
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
20 * details.
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>
41 #include "shpchp.h"
42 #include "shpchprm.h"
44 static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
45 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
46 static int configure_new_function( struct controller *ctrl, struct pci_func *func,
47 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
48 static void interrupt_event_handler(struct controller *ctrl);
50 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
51 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
52 static int event_finished;
53 static unsigned long pushbutton_pending; /* = 0 */
55 u8 shpchp_disk_irq;
56 u8 shpchp_nic_irq;
58 u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
60 struct controller *ctrl = (struct controller *) inst_id;
61 struct slot *p_slot;
62 u8 rc = 0;
63 u8 getstatus;
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;
82 rc++;
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
94 * or hot-remove
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);
108 if (rc)
109 up(&event_semaphore); /* signal event thread that new event is posted */
111 return 0;
115 u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
117 struct controller *ctrl = (struct controller *) inst_id;
118 struct slot *p_slot;
119 u8 rc = 0;
120 u8 getstatus;
121 struct pci_func *func;
122 struct event_info *taskInfo;
124 /* Switch Change */
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
130 * what to do
132 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
133 ctrl->next_event = (ctrl->next_event + 1) % 10;
134 taskInfo->hp_slot = hp_slot;
136 rc++;
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);
143 if (getstatus) {
145 * Switch opened
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");
154 } else {
156 * Switch closed
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;
163 if (rc)
164 up(&event_semaphore); /* signal event thread that new event is posted */
166 return rc;
169 u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
171 struct controller *ctrl = (struct controller *) inst_id;
172 struct slot *p_slot;
173 u8 rc = 0;
174 /*u8 temp_byte;*/
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
184 * what to do
186 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
187 ctrl->next_event = (ctrl->next_event + 1) % 10;
188 taskInfo->hp_slot = hp_slot;
190 rc++;
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) {
199 * Card Present
201 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
202 taskInfo->event_type = INT_PRESENCE_ON;
203 } else {
205 * Not Present
207 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
208 taskInfo->event_type = INT_PRESENCE_OFF;
211 if (rc)
212 up(&event_semaphore); /* signal event thread that new event is posted */
214 return rc;
217 u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
219 struct controller *ctrl = (struct controller *) inst_id;
220 struct slot *p_slot;
221 u8 rc = 0;
222 struct pci_func *func;
223 struct event_info *taskInfo;
225 /* Power fault */
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
231 * what to do
233 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
234 ctrl->next_event = (ctrl->next_event + 1) % 10;
235 taskInfo->hp_slot = hp_slot;
237 rc++;
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);
245 func->status = 0x00;
246 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
247 } else {
249 * Power fault
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 */
254 func->status = 0xFF;
255 info("power fault bit %x set\n", hp_slot);
257 if (rc)
258 up(&event_semaphore); /* signal event thread that new event is posted */
260 return rc;
265 * sort_by_size
267 * Sorts nodes on the list by their length.
268 * Smallest first.
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;
277 if (!(*head))
278 return(1);
280 if (!((*head)->next))
281 return(0);
283 while (out_of_order) {
284 out_of_order = 0;
286 /* Special case for swapping list head */
287 if (((*head)->next) &&
288 ((*head)->length > (*head)->next->length)) {
289 out_of_order++;
290 current_res = *head;
291 *head = (*head)->next;
292 current_res->next = (*head)->next;
293 (*head)->next = current_res;
296 current_res = *head;
298 while (current_res->next && current_res->next->next) {
299 if (current_res->next->length > current_res->next->next->length) {
300 out_of_order++;
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;
306 } else
307 current_res = current_res->next;
309 } /* End of out_of_order loop */
311 return(0);
316 * sort_by_max_size
318 * Sorts nodes on the list by their length.
319 * Largest first.
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;
328 if (!(*head))
329 return(1);
331 if (!((*head)->next))
332 return(0);
334 while (out_of_order) {
335 out_of_order = 0;
337 /* Special case for swapping list head */
338 if (((*head)->next) &&
339 ((*head)->length < (*head)->next->length)) {
340 out_of_order++;
341 current_res = *head;
342 *head = (*head)->next;
343 current_res->next = (*head)->next;
344 (*head)->next = current_res;
347 current_res = *head;
349 while (current_res->next && current_res->next->next) {
350 if (current_res->next->length < current_res->next->next->length) {
351 out_of_order++;
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;
357 } else
358 current_res = current_res->next;
360 } /* End of out_of_order loop */
362 return(0);
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;
377 u32 rc;
378 u32 temp_dword;
379 dbg("do_pre_bridge_resource_split\n");
381 if (!(*head) || !(*orig_head))
382 return(NULL);
384 rc = shpchp_resource_sort_and_combine(head);
386 if (rc)
387 return(NULL);
389 if ((*head)->base != (*orig_head)->base)
390 return(NULL);
392 if ((*head)->length == (*orig_head)->length)
393 return(NULL);
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
399 node = *head;
401 if (node->length & (alignment -1)) {
402 /* This one isn't an aligned length, so we'll make a new entry
403 * and split it up.
405 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
407 if (!split_node)
408 return(NULL);
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 */
419 *head = split_node;
420 split_node->next = node;
423 if (node->length < alignment) {
424 return(NULL);
427 /* Now unlink it */
428 if (*head == node) {
429 *head = node->next;
430 node->next = NULL;
431 } else {
432 prevnode = *head;
433 while (prevnode->next != node)
434 prevnode = prevnode->next;
436 prevnode->next = node->next;
437 node->next = NULL;
440 return(node);
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;
454 u32 rc;
455 u32 temp_dword;
457 if (!(*head))
458 return(NULL);
460 rc = shpchp_resource_sort_and_combine(head);
462 if (rc)
463 return(NULL);
465 node = *head;
467 while (node->next) {
468 prevnode = node;
469 node = node->next;
470 kfree(prevnode);
473 if (node->length < alignment) {
474 kfree(node);
475 return(NULL);
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) {
482 kfree(node);
483 return(NULL);
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 */
492 kfree(node);
493 return(NULL);
496 return(node);
501 * get_io_resource
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;
515 u32 temp_dword;
517 if (!(*head))
518 return(NULL);
520 if ( shpchp_resource_sort_and_combine(head) )
521 return(NULL);
523 if ( sort_by_size(head) )
524 return(NULL);
526 for (node = *head; node; node = node->next) {
527 if (node->length < size)
528 continue;
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)
537 continue;
539 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
541 if (!split_node)
542 return(NULL);
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);
560 if (!split_node)
561 return(NULL);
563 split_node->base = node->base + size;
564 split_node->length = node->length - size;
565 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)
574 continue;
576 /* If we got here, then it is the right size
577 Now take it out of the list */
578 if (*head == node) {
579 *head = node->next;
580 } else {
581 prevnode = *head;
582 while (prevnode->next != node)
583 prevnode = prevnode->next;
585 prevnode->next = node->next;
587 node->next = NULL;
588 /* Stop looping */
589 break;
592 return(node);
597 * get_max_resource
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;
610 u32 temp_dword;
611 u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
612 int i;
614 if (!(*head))
615 return(NULL);
617 if (shpchp_resource_sort_and_combine(head))
618 return(NULL);
620 if (sort_by_max_size(head))
621 return(NULL);
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)
628 continue;
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)
637 continue;
639 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
641 if (!split_node)
642 return(NULL);
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);
659 if (!split_node)
660 return(NULL);
661 temp_dword = ((max->base + max->length) & ~(size - 1));
662 split_node->base = temp_dword;
663 split_node->length = max->length + max->base
664 - split_node->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)
674 continue;
676 for ( i = 0; max_size[i] > size; i++) {
677 if (max->length > max_size[i]) {
678 split_node = kmalloc(sizeof(*split_node),
679 GFP_KERNEL);
680 if (!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;
688 break;
692 /* Now take it out of the list */
693 temp = (struct pci_resource*) *head;
694 if (temp == max) {
695 *head = max->next;
696 } else {
697 while (temp && temp->next != max) {
698 temp = temp->next;
701 temp->next = max->next;
704 max->next = NULL;
705 return(max);
708 /* If we get here, we couldn't find one */
709 return(NULL);
714 * get_resource
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;
727 u32 temp_dword;
729 if (!(*head))
730 return(NULL);
732 if ( shpchp_resource_sort_and_combine(head) )
733 return(NULL);
735 if ( sort_by_size(head) )
736 return(NULL);
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)
742 continue;
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)
752 continue;
754 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
756 if (!split_node)
757 return(NULL);
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);
776 if (!split_node)
777 return(NULL);
779 split_node->base = node->base + size;
780 split_node->length = node->length - size;
781 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 */
791 if (*head == node) {
792 *head = node->next;
793 } else {
794 prevnode = *head;
795 while (prevnode->next != node)
796 prevnode = prevnode->next;
798 prevnode->next = node->next;
800 node->next = NULL;
801 /* Stop looping */
802 break;
804 return(node);
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);
825 if (!(*head))
826 return(1);
828 dbg("*head->next = %p\n",(*head)->next);
830 if (!(*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) {
836 out_of_order = 0;
838 /* Special case for swapping list head */
839 if (((*head)->next) &&
840 ((*head)->base > (*head)->next->base)) {
841 node1 = *head;
842 (*head) = (*head)->next;
843 node1->next = (*head)->next;
844 (*head)->next = node1;
845 out_of_order++;
848 node1 = (*head);
850 while (node1->next && node1->next->next) {
851 if (node1->next->base > node1->next->next->base) {
852 out_of_order++;
853 node2 = node1->next;
854 node1->next = node1->next->next;
855 node1 = node1->next;
856 node2->next = node1->next;
857 node1->next = node2;
858 } else
859 node1 = node1->next;
861 } /* End of out_of_order loop */
863 node1 = *head;
865 while (node1 && node1->next) {
866 if ((node1->base + node1->length) == node1->next->base) {
867 /* Combine */
868 dbg("8..\n");
869 node1->length += node1->next->length;
870 node2 = node1->next;
871 node1->next = node1->next->next;
872 kfree(node2);
873 } else
874 node1 = node1->next;
877 return(0);
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) {
895 return(new_slot);
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;
905 } else {
906 next = shpchp_slot_list[busnumber];
907 while (next->next != NULL)
908 next = next->next;
909 next->next = new_slot;
911 return(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)
926 return(1);
928 next = shpchp_slot_list[old_slot->bus];
930 if (next == NULL) {
931 return(1);
934 if (next == old_slot) {
935 shpchp_slot_list[old_slot->bus] = old_slot->next;
936 shpchp_destroy_board_resources(old_slot);
937 kfree(old_slot);
938 return(0);
941 while ((next->next != old_slot) && (next->next != NULL)) {
942 next = next->next;
945 if (next->next == old_slot) {
946 next->next = old_slot->next;
947 shpchp_destroy_board_resources(old_slot);
948 kfree(old_slot);
949 return(0);
950 } else
951 return(2);
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;
964 u8 tempBus;
965 struct pci_func *next;
967 if (bridge == NULL)
968 return(1);
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];
983 if (next == NULL) {
984 return(1);
987 if (next == bridge) {
988 shpchp_slot_list[bridge->bus] = bridge->next;
989 kfree(bridge);
990 return(0);
993 while ((next->next != bridge) && (next->next != NULL)) {
994 next = next->next;
997 if (next->next == bridge) {
998 next->next = bridge->next;
999 kfree(bridge);
1000 return(0);
1001 } else
1002 return(2);
1007 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1008 * @bus: bus to find
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)
1016 int found = -1;
1017 struct pci_func *func;
1019 func = shpchp_slot_list[bus];
1021 if ((func == NULL) || ((func->device == device) && (index == 0)))
1022 return(func);
1024 if (func->device == device)
1025 found++;
1027 while (func->next != NULL) {
1028 func = func->next;
1030 if (func->device == device)
1031 found++;
1033 if (found == index)
1034 return(func);
1037 return(NULL);
1040 static int is_bridge(struct pci_func * func)
1042 /* Check the header type */
1043 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1044 return 1;
1045 else
1046 return 0;
1050 /* The following routines constitute the bulk of the
1051 hotplug controller logic
1053 static u32 change_bus_speed(struct controller *ctrl, struct slot *p_slot, enum pci_bus_speed speed)
1055 u32 rc = 0;
1057 dbg("%s: change to speed %d\n", __FUNCTION__, speed);
1058 down(&ctrl->crit_sect);
1059 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
1060 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1061 up(&ctrl->crit_sect);
1062 return WRONG_BUS_FREQUENCY;
1064 wait_for_ctrl_irq (ctrl);
1066 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
1067 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1068 __FUNCTION__);
1069 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1070 up(&ctrl->crit_sect);
1071 return WRONG_BUS_FREQUENCY;
1073 up(&ctrl->crit_sect);
1074 return rc;
1077 static u32 fix_bus_speed(struct controller *ctrl, struct slot *pslot, u8 flag,
1078 enum pci_bus_speed asp, enum pci_bus_speed bsp, enum pci_bus_speed msp)
1080 u32 rc = 0;
1082 if (flag != 0) { /* Other slots on the same bus are occupied */
1083 if ( asp < bsp ) {
1084 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
1085 return WRONG_BUS_FREQUENCY;
1087 } else {
1088 /* Other slots on the same bus are empty */
1089 if (msp == bsp) {
1090 /* if adapter_speed >= bus_speed, do nothing */
1091 if (asp < bsp) {
1093 * Try to lower bus speed to accommodate the adapter if other slots
1094 * on the same controller are empty
1096 if ((rc = change_bus_speed(ctrl, pslot, asp)))
1097 return rc;
1099 } else {
1100 if (asp < msp) {
1101 if ((rc = change_bus_speed(ctrl, pslot, asp)))
1102 return rc;
1103 } else {
1104 if ((rc = change_bus_speed(ctrl, pslot, msp)))
1105 return rc;
1109 return rc;
1113 * board_added - Called after a board has been added to the system.
1115 * Turns power on for the board
1116 * Configures board
1119 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1121 u8 hp_slot;
1122 u8 slots_not_empty = 0;
1123 int index;
1124 u32 temp_register = 0xFFFFFFFF;
1125 u32 retval, rc = 0;
1126 struct pci_func *new_func = NULL;
1127 struct slot *p_slot;
1128 struct resource_lists res_lists;
1129 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
1130 u8 pi, mode;
1132 p_slot = shpchp_find_slot(ctrl, func->device);
1133 hp_slot = func->device - ctrl->slot_device_offset;
1135 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1137 /* Wait for exclusive access to hardware */
1138 down(&ctrl->crit_sect);
1140 /* Power on slot without connecting to bus */
1141 rc = p_slot->hpc_ops->power_on_slot(p_slot);
1142 if (rc) {
1143 err("%s: Failed to power on slot\n", __FUNCTION__);
1144 /* Done with exclusive hardware access */
1145 up(&ctrl->crit_sect);
1146 return -1;
1149 /* Wait for the command to complete */
1150 wait_for_ctrl_irq (ctrl);
1152 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1153 if (rc) {
1154 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
1155 /* Done with exclusive hardware access */
1156 up(&ctrl->crit_sect);
1157 return -1;
1161 if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
1162 if (slots_not_empty)
1163 return WRONG_BUS_FREQUENCY;
1165 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
1166 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
1167 up(&ctrl->crit_sect);
1168 return WRONG_BUS_FREQUENCY;
1170 wait_for_ctrl_irq (ctrl);
1172 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
1173 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
1174 __FUNCTION__);
1175 err("%s: Error code (%d)\n", __FUNCTION__, rc);
1176 up(&ctrl->crit_sect);
1177 return WRONG_BUS_FREQUENCY;
1179 /* turn on board, blink green LED, turn off Amber LED */
1180 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
1181 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1182 up(&ctrl->crit_sect);
1183 return rc;
1185 wait_for_ctrl_irq (ctrl);
1187 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
1188 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1189 up(&ctrl->crit_sect);
1190 return rc;
1194 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
1195 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
1196 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
1197 /* 0xd = PCI-X 133 Mhz 533 */
1198 /* This encoding is different from the one used in cur_bus_speed & */
1199 /* max_bus_speed */
1201 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
1202 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
1203 /* Done with exclusive hardware access */
1204 up(&ctrl->crit_sect);
1205 return WRONG_BUS_FREQUENCY;
1208 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
1209 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
1210 err("%s: Can't get bus operation speed\n", __FUNCTION__);
1211 /* Done with exclusive hardware access */
1212 up(&ctrl->crit_sect);
1213 return WRONG_BUS_FREQUENCY;
1216 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
1217 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
1218 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
1219 max_bus_speed = bus_speed;
1222 /* Done with exclusive hardware access */
1223 up(&ctrl->crit_sect);
1225 if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
1226 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
1227 pi = 1;
1230 /* Check if there are other slots or devices on the same bus */
1231 if (!list_empty(&ctrl->pci_dev->subordinate->devices))
1232 slots_not_empty = 1;
1234 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
1235 slots_not_empty, pi);
1236 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
1237 adapter_speed, bus_speed, max_bus_speed);
1239 if (pi == 2) {
1240 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
1241 if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
1242 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
1243 mode = 0;
1246 switch (adapter_speed) {
1247 case PCI_SPEED_133MHz_PCIX_533:
1248 case PCI_SPEED_133MHz_PCIX_266:
1249 if ((bus_speed != adapter_speed) &&
1250 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1251 return rc;
1252 break;
1253 case PCI_SPEED_133MHz_PCIX_ECC:
1254 case PCI_SPEED_133MHz_PCIX:
1255 if (mode) { /* Bus - Mode 1 ECC */
1256 if ((bus_speed != 0x7) &&
1257 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1258 return rc;
1259 } else {
1260 if ((bus_speed != 0x4) &&
1261 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1262 return rc;
1264 break;
1265 case PCI_SPEED_66MHz_PCIX_ECC:
1266 case PCI_SPEED_66MHz_PCIX:
1267 if (mode) { /* Bus - Mode 1 ECC */
1268 if ((bus_speed != 0x5) &&
1269 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1270 return rc;
1271 } else {
1272 if ((bus_speed != 0x2) &&
1273 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1274 return rc;
1276 break;
1277 case PCI_SPEED_66MHz:
1278 if ((bus_speed != 0x1) &&
1279 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1280 return rc;
1281 break;
1282 case PCI_SPEED_33MHz:
1283 if (bus_speed > 0x0) {
1284 if (slots_not_empty == 0) {
1285 if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
1286 return rc;
1287 } else {
1288 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1289 return WRONG_BUS_FREQUENCY;
1292 break;
1293 default:
1294 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
1295 return WRONG_BUS_FREQUENCY;
1297 } else {
1298 /* If adpater_speed == bus_speed, nothing to do here */
1299 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
1300 if ((adapter_speed != bus_speed) &&
1301 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
1302 return rc;
1305 down(&ctrl->crit_sect);
1306 /* turn on board, blink green LED, turn off Amber LED */
1307 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
1308 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1309 up(&ctrl->crit_sect);
1310 return rc;
1312 wait_for_ctrl_irq (ctrl);
1314 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
1315 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
1316 up(&ctrl->crit_sect);
1317 return rc;
1320 up(&ctrl->crit_sect);
1322 /* Wait for ~1 second */
1323 dbg("%s: before long_delay\n", __FUNCTION__);
1324 wait_for_ctrl_irq (ctrl);
1325 dbg("%s: after long_delay\n", __FUNCTION__);
1327 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1328 /* Check for a power fault */
1329 if (func->status == 0xFF) {
1330 /* power fault occurred, but it was benign */
1331 temp_register = 0xFFFFFFFF;
1332 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1333 rc = POWER_FAILURE;
1334 func->status = 0;
1335 } else {
1336 /* Get vendor/device ID u32 */
1337 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
1338 PCI_VENDOR_ID, &temp_register);
1339 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1340 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1342 if (rc != 0) {
1343 /* Something's wrong here */
1344 temp_register = 0xFFFFFFFF;
1345 dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1347 /* Preset return code. It will be changed later if things go okay. */
1348 rc = NO_ADAPTER_PRESENT;
1351 /* All F's is an empty slot or an invalid board */
1352 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1353 res_lists.io_head = ctrl->io_head;
1354 res_lists.mem_head = ctrl->mem_head;
1355 res_lists.p_mem_head = ctrl->p_mem_head;
1356 res_lists.bus_head = ctrl->bus_head;
1357 res_lists.irqs = NULL;
1359 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1360 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1362 ctrl->io_head = res_lists.io_head;
1363 ctrl->mem_head = res_lists.mem_head;
1364 ctrl->p_mem_head = res_lists.p_mem_head;
1365 ctrl->bus_head = res_lists.bus_head;
1367 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1368 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1369 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1370 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1372 if (rc) {
1373 /* Wait for exclusive access to hardware */
1374 down(&ctrl->crit_sect);
1376 /* turn off slot, turn on Amber LED, turn off Green LED */
1377 retval = p_slot->hpc_ops->slot_disable(p_slot);
1378 if (retval) {
1379 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
1380 /* Done with exclusive hardware access */
1381 up(&ctrl->crit_sect);
1382 return retval;
1384 /* Wait for the command to complete */
1385 wait_for_ctrl_irq (ctrl);
1387 retval = p_slot->hpc_ops->check_cmd_status(ctrl);
1388 if (retval) {
1389 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, retval);
1390 /* Done with exclusive hardware access */
1391 up(&ctrl->crit_sect);
1392 return retval;
1395 /* Done with exclusive hardware access */
1396 up(&ctrl->crit_sect);
1398 return(rc);
1400 shpchp_save_slot_config(ctrl, func);
1402 func->status = 0;
1403 func->switch_save = 0x10;
1404 func->is_a_board = 0x01;
1405 func->pwr_save = 1;
1407 /* Next, we will instantiate the linux pci_dev structures
1408 * (with appropriate driver notification, if already present)
1410 index = 0;
1411 do {
1412 new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
1413 if (new_func && !new_func->pci_dev) {
1414 dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
1415 shpchp_configure_device(ctrl, new_func);
1417 } while (new_func);
1419 /* Wait for exclusive access to hardware */
1420 down(&ctrl->crit_sect);
1422 p_slot->hpc_ops->green_led_on(p_slot);
1424 /* Wait for the command to complete */
1425 wait_for_ctrl_irq (ctrl);
1428 /* Done with exclusive hardware access */
1429 up(&ctrl->crit_sect);
1431 } else {
1432 /* Wait for exclusive access to hardware */
1433 down(&ctrl->crit_sect);
1435 /* turn off slot, turn on Amber LED, turn off Green LED */
1436 rc = p_slot->hpc_ops->slot_disable(p_slot);
1437 if (rc) {
1438 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1439 /* Done with exclusive hardware access */
1440 up(&ctrl->crit_sect);
1441 return rc;
1443 /* Wait for the command to complete */
1444 wait_for_ctrl_irq (ctrl);
1446 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1447 if (rc) {
1448 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1449 /* Done with exclusive hardware access */
1450 up(&ctrl->crit_sect);
1451 return rc;
1454 /* Done with exclusive hardware access */
1455 up(&ctrl->crit_sect);
1457 return(rc);
1459 return 0;
1464 * remove_board - Turns off slot and LED's
1467 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1469 int index;
1470 u8 skip = 0;
1471 u8 device;
1472 u8 hp_slot;
1473 u32 rc;
1474 struct resource_lists res_lists;
1475 struct pci_func *temp_func;
1476 struct slot *p_slot;
1478 if (func == NULL)
1479 return(1);
1481 if (shpchp_unconfigure_device(func))
1482 return(1);
1484 device = func->device;
1486 hp_slot = func->device - ctrl->slot_device_offset;
1487 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1489 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1491 if ((ctrl->add_support) &&
1492 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1493 /* Here we check to see if we've saved any of the board's
1494 * resources already. If so, we'll skip the attempt to
1495 * determine what's being used.
1497 index = 0;
1499 temp_func = func;
1501 while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
1502 if (temp_func->bus_head || temp_func->mem_head
1503 || temp_func->p_mem_head || temp_func->io_head) {
1504 skip = 1;
1505 break;
1509 if (!skip)
1510 rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
1512 /* Change status to shutdown */
1513 if (func->is_a_board)
1514 func->status = 0x01;
1515 func->configured = 0;
1517 /* Wait for exclusive access to hardware */
1518 down(&ctrl->crit_sect);
1520 /* turn off slot, turn on Amber LED, turn off Green LED */
1521 rc = p_slot->hpc_ops->slot_disable(p_slot);
1522 if (rc) {
1523 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1524 /* Done with exclusive hardware access */
1525 up(&ctrl->crit_sect);
1526 return rc;
1528 /* Wait for the command to complete */
1529 wait_for_ctrl_irq (ctrl);
1531 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
1532 if (rc) {
1533 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
1534 /* Done with exclusive hardware access */
1535 up(&ctrl->crit_sect);
1536 return rc;
1539 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
1540 if (rc) {
1541 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
1542 /* Done with exclusive hardware access */
1543 up(&ctrl->crit_sect);
1544 return rc;
1546 /* Wait for the command to complete */
1547 wait_for_ctrl_irq (ctrl);
1549 /* Done with exclusive hardware access */
1550 up(&ctrl->crit_sect);
1552 if (ctrl->add_support) {
1553 while (func) {
1554 res_lists.io_head = ctrl->io_head;
1555 res_lists.mem_head = ctrl->mem_head;
1556 res_lists.p_mem_head = ctrl->p_mem_head;
1557 res_lists.bus_head = ctrl->bus_head;
1559 dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", func->bus,
1560 func->device, func->function);
1562 shpchp_return_board_resources(func, &res_lists);
1564 ctrl->io_head = res_lists.io_head;
1565 ctrl->mem_head = res_lists.mem_head;
1566 ctrl->p_mem_head = res_lists.p_mem_head;
1567 ctrl->bus_head = res_lists.bus_head;
1569 shpchp_resource_sort_and_combine(&(ctrl->mem_head));
1570 shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
1571 shpchp_resource_sort_and_combine(&(ctrl->io_head));
1572 shpchp_resource_sort_and_combine(&(ctrl->bus_head));
1574 if (is_bridge(func)) {
1575 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1576 func->device, func->function);
1577 bridge_slot_remove(func);
1578 } else
1579 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
1580 func->device, func->function);
1581 slot_remove(func);
1583 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
1586 /* Setup slot structure with entry for empty slot */
1587 func = shpchp_slot_create(ctrl->slot_bus);
1589 if (func == NULL) {
1590 return(1);
1593 func->bus = ctrl->slot_bus;
1594 func->device = device;
1595 func->function = 0;
1596 func->configured = 0;
1597 func->switch_save = 0x10;
1598 func->pwr_save = 0;
1599 func->is_a_board = 0;
1602 return 0;
1606 static void pushbutton_helper_thread (unsigned long data)
1608 pushbutton_pending = data;
1610 up(&event_semaphore);
1615 * shpchp_pushbutton_thread
1617 * Scheduled procedure to handle blocking stuff for the pushbuttons
1618 * Handles all pending events and exits.
1621 static void shpchp_pushbutton_thread (unsigned long slot)
1623 struct slot *p_slot = (struct slot *) slot;
1624 u8 getstatus;
1626 pushbutton_pending = 0;
1628 if (!p_slot) {
1629 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1630 return;
1633 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1634 if (getstatus) {
1635 p_slot->state = POWEROFF_STATE;
1636 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1638 shpchp_disable_slot(p_slot);
1639 p_slot->state = STATIC_STATE;
1640 } else {
1641 p_slot->state = POWERON_STATE;
1642 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1644 if (shpchp_enable_slot(p_slot)) {
1645 /* Wait for exclusive access to hardware */
1646 down(&p_slot->ctrl->crit_sect);
1648 p_slot->hpc_ops->green_led_off(p_slot);
1650 /* Wait for the command to complete */
1651 wait_for_ctrl_irq (p_slot->ctrl);
1653 /* Done with exclusive hardware access */
1654 up(&p_slot->ctrl->crit_sect);
1656 p_slot->state = STATIC_STATE;
1659 return;
1663 /* this is the main worker thread */
1664 static int event_thread(void* data)
1666 struct controller *ctrl;
1667 lock_kernel();
1668 daemonize("shpchpd_event");
1669 unlock_kernel();
1671 while (1) {
1672 dbg("!!!!event_thread sleeping\n");
1673 down_interruptible (&event_semaphore);
1674 dbg("event_thread woken finished = %d\n", event_finished);
1675 if (event_finished || signal_pending(current))
1676 break;
1677 /* Do stuff here */
1678 if (pushbutton_pending)
1679 shpchp_pushbutton_thread(pushbutton_pending);
1680 else
1681 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
1682 interrupt_event_handler(ctrl);
1684 dbg("event_thread signals exit\n");
1685 up(&event_exit);
1686 return 0;
1689 int shpchp_event_start_thread (void)
1691 int pid;
1693 /* initialize our semaphores */
1694 init_MUTEX_LOCKED(&event_exit);
1695 event_finished=0;
1697 init_MUTEX_LOCKED(&event_semaphore);
1698 pid = kernel_thread(event_thread, NULL, 0);
1700 if (pid < 0) {
1701 err ("Can't start up our event thread\n");
1702 return -1;
1704 dbg("Our event thread pid = %d\n", pid);
1705 return 0;
1709 void shpchp_event_stop_thread (void)
1711 event_finished = 1;
1712 dbg("event_thread finish command given\n");
1713 up(&event_semaphore);
1714 dbg("wait for event_thread to exit\n");
1715 down(&event_exit);
1719 static int update_slot_info (struct slot *slot)
1721 struct hotplug_slot_info *info;
1722 int result;
1724 info = kmalloc(sizeof(*info), GFP_KERNEL);
1725 if (!info)
1726 return -ENOMEM;
1728 slot->hpc_ops->get_power_status(slot, &(info->power_status));
1729 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1730 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1731 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1733 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1734 kfree (info);
1735 return result;
1738 static void interrupt_event_handler(struct controller *ctrl)
1740 int loop = 0;
1741 int change = 1;
1742 struct pci_func *func;
1743 u8 hp_slot;
1744 u8 getstatus;
1745 struct slot *p_slot;
1747 dbg("%s:\n", __FUNCTION__);
1748 while (change) {
1749 change = 0;
1751 for (loop = 0; loop < 10; loop++) {
1752 if (ctrl->event_queue[loop].event_type != 0) {
1753 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1754 ctrl->event_queue[loop].event_type);
1755 hp_slot = ctrl->event_queue[loop].hp_slot;
1757 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1759 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1761 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1763 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1764 dbg("%s: button cancel\n", __FUNCTION__);
1765 del_timer(&p_slot->task_event);
1767 switch (p_slot->state) {
1768 case BLINKINGOFF_STATE:
1769 /* Wait for exclusive access to hardware */
1770 down(&ctrl->crit_sect);
1772 p_slot->hpc_ops->green_led_on(p_slot);
1773 /* Wait for the command to complete */
1774 wait_for_ctrl_irq (ctrl);
1776 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1778 /* Wait for the command to complete */
1779 wait_for_ctrl_irq (ctrl);
1781 /* Done with exclusive hardware access */
1782 up(&ctrl->crit_sect);
1783 break;
1784 case BLINKINGON_STATE:
1785 /* Wait for exclusive access to hardware */
1786 down(&ctrl->crit_sect);
1788 p_slot->hpc_ops->green_led_off(p_slot);
1789 /* Wait for the command to complete */
1790 wait_for_ctrl_irq (ctrl);
1792 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1793 /* Wait for the command to complete */
1794 wait_for_ctrl_irq (ctrl);
1796 /* Done with exclusive hardware access */
1797 up(&ctrl->crit_sect);
1799 break;
1800 default:
1801 warn("Not a valid state\n");
1802 return;
1804 info(msg_button_cancel, p_slot->number);
1805 p_slot->state = STATIC_STATE;
1806 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1807 /* Button Pressed (No action on 1st press...) */
1808 dbg("%s: Button pressed\n", __FUNCTION__);
1810 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1811 if (getstatus) {
1812 /* slot is on */
1813 dbg("%s: slot is on\n", __FUNCTION__);
1814 p_slot->state = BLINKINGOFF_STATE;
1815 info(msg_button_off, p_slot->number);
1816 } else {
1817 /* slot is off */
1818 dbg("%s: slot is off\n", __FUNCTION__);
1819 p_slot->state = BLINKINGON_STATE;
1820 info(msg_button_on, p_slot->number);
1823 /* Wait for exclusive access to hardware */
1824 down(&ctrl->crit_sect);
1826 /* blink green LED and turn off amber */
1827 p_slot->hpc_ops->green_led_blink(p_slot);
1828 /* Wait for the command to complete */
1829 wait_for_ctrl_irq (ctrl);
1831 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1833 /* Wait for the command to complete */
1834 wait_for_ctrl_irq (ctrl);
1836 /* Done with exclusive hardware access */
1837 up(&ctrl->crit_sect);
1839 init_timer(&p_slot->task_event);
1840 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1841 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1842 p_slot->task_event.data = (unsigned long) p_slot;
1844 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
1845 add_timer(&p_slot->task_event);
1846 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1847 /***********POWER FAULT********************/
1848 dbg("%s: power fault\n", __FUNCTION__);
1849 /* Wait for exclusive access to hardware */
1850 down(&ctrl->crit_sect);
1852 p_slot->hpc_ops->set_attention_status(p_slot, 1);
1853 /* Wait for the command to complete */
1854 wait_for_ctrl_irq (ctrl);
1856 p_slot->hpc_ops->green_led_off(p_slot);
1857 /* Wait for the command to complete */
1858 wait_for_ctrl_irq (ctrl);
1860 /* Done with exclusive hardware access */
1861 up(&ctrl->crit_sect);
1862 } else {
1863 /* refresh notification */
1864 if (p_slot)
1865 update_slot_info(p_slot);
1868 ctrl->event_queue[loop].event_type = 0;
1870 change = 1;
1872 } /* End of FOR loop */
1875 return;
1879 int shpchp_enable_slot (struct slot *p_slot)
1881 u8 getstatus = 0;
1882 int rc;
1883 struct pci_func *func;
1885 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
1886 if (!func) {
1887 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1888 return -ENODEV;
1891 /* Check to see if (latch closed, card present, power off) */
1892 down(&p_slot->ctrl->crit_sect);
1893 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1894 if (rc || !getstatus) {
1895 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1896 up(&p_slot->ctrl->crit_sect);
1897 return -ENODEV;
1899 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1900 if (rc || getstatus) {
1901 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1902 up(&p_slot->ctrl->crit_sect);
1903 return -ENODEV;
1905 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1906 if (rc || getstatus) {
1907 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
1908 up(&p_slot->ctrl->crit_sect);
1909 return -ENODEV;
1911 up(&p_slot->ctrl->crit_sect);
1913 slot_remove(func);
1915 func = shpchp_slot_create(p_slot->bus);
1916 if (func == NULL)
1917 return -ENOMEM;
1919 func->bus = p_slot->bus;
1920 func->device = p_slot->device;
1921 func->function = 0;
1922 func->configured = 0;
1923 func->is_a_board = 1;
1925 /* We have to save the presence info for these slots */
1926 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1927 p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
1928 dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
1929 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1930 func->switch_save = !getstatus? 0x10:0;
1932 rc = board_added(func, p_slot->ctrl);
1933 if (rc) {
1934 if (is_bridge(func))
1935 bridge_slot_remove(func);
1936 else
1937 slot_remove(func);
1939 /* Setup slot structure with entry for empty slot */
1940 func = shpchp_slot_create(p_slot->bus);
1941 if (func == NULL)
1942 return -ENOMEM; /* Out of memory */
1944 func->bus = p_slot->bus;
1945 func->device = p_slot->device;
1946 func->function = 0;
1947 func->configured = 0;
1948 func->is_a_board = 1;
1950 /* We have to save the presence info for these slots */
1951 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1952 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1953 func->switch_save = !getstatus? 0x10:0;
1956 if (p_slot)
1957 update_slot_info(p_slot);
1959 return rc;
1963 int shpchp_disable_slot (struct slot *p_slot)
1965 u8 class_code, header_type, BCR;
1966 u8 index = 0;
1967 u8 getstatus = 0;
1968 u32 rc = 0;
1969 int ret = 0;
1970 unsigned int devfn;
1971 struct pci_bus *pci_bus;
1972 struct pci_func *func;
1974 if (!p_slot->ctrl)
1975 return -ENODEV;
1977 pci_bus = p_slot->ctrl->pci_dev->subordinate;
1979 /* Check to see if (latch closed, card present, power on) */
1980 down(&p_slot->ctrl->crit_sect);
1982 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1983 if (ret || !getstatus) {
1984 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1985 up(&p_slot->ctrl->crit_sect);
1986 return -ENODEV;
1988 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1989 if (ret || getstatus) {
1990 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1991 up(&p_slot->ctrl->crit_sect);
1992 return -ENODEV;
1994 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1995 if (ret || !getstatus) {
1996 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
1997 up(&p_slot->ctrl->crit_sect);
1998 return -ENODEV;
2000 up(&p_slot->ctrl->crit_sect);
2002 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2004 /* Make sure there are no video controllers here
2005 * for all func of p_slot
2007 while (func && !rc) {
2008 pci_bus->number = func->bus;
2009 devfn = PCI_DEVFN(func->device, func->function);
2011 /* Check the Class Code */
2012 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2013 if (rc)
2014 return -ENODEV;
2016 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2017 /* Display/Video adapter (not supported) */
2018 rc = REMOVE_NOT_SUPPORTED;
2019 } else {
2020 /* See if it's a bridge */
2021 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2022 if (rc)
2023 return -ENODEV;
2025 /* If it's a bridge, check the VGA Enable bit */
2026 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2027 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2028 if (rc)
2029 return -ENODEV;
2031 /* If the VGA Enable bit is set, remove isn't supported */
2032 if (BCR & PCI_BRIDGE_CTL_VGA) {
2033 rc = REMOVE_NOT_SUPPORTED;
2038 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
2041 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
2042 if ((func != NULL) && !rc) {
2043 rc = remove_board(func, p_slot->ctrl);
2044 } else if (!rc)
2045 rc = -ENODEV;
2047 if (p_slot)
2048 update_slot_info(p_slot);
2050 return rc;
2055 * configure_new_device - Configures the PCI header information of one board.
2057 * @ctrl: pointer to controller structure
2058 * @func: pointer to function structure
2059 * @behind_bridge: 1 if this is a recursive call, 0 if not
2060 * @resources: pointer to set of resource lists
2062 * Returns 0 if success
2065 static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
2066 u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
2068 u8 temp_byte, function, max_functions, stop_it;
2069 int rc;
2070 u32 ID;
2071 struct pci_func *new_slot;
2072 struct pci_bus lpci_bus, *pci_bus;
2073 int index;
2075 new_slot = func;
2077 dbg("%s\n", __FUNCTION__);
2078 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2079 pci_bus = &lpci_bus;
2080 pci_bus->number = func->bus;
2082 /* Check for Multi-function device */
2083 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2084 if (rc) {
2085 dbg("%s: rc = %d\n", __FUNCTION__, rc);
2086 return rc;
2089 if (temp_byte & 0x80) /* Multi-function device */
2090 max_functions = 8;
2091 else
2092 max_functions = 1;
2094 function = 0;
2096 do {
2097 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
2099 if (rc) {
2100 dbg("configure_new_function failed %d\n",rc);
2101 index = 0;
2103 while (new_slot) {
2104 new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
2106 if (new_slot)
2107 shpchp_return_board_resources(new_slot, resources);
2110 return(rc);
2113 function++;
2115 stop_it = 0;
2117 /* The following loop skips to the next present function
2118 * and creates a board structure
2121 while ((function < max_functions) && (!stop_it)) {
2122 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2124 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2125 function++;
2126 } else { /* There's something there */
2127 /* Setup slot structure. */
2128 new_slot = shpchp_slot_create(func->bus);
2130 if (new_slot == NULL) {
2131 /* Out of memory */
2132 return(1);
2135 new_slot->bus = func->bus;
2136 new_slot->device = func->device;
2137 new_slot->function = function;
2138 new_slot->is_a_board = 1;
2139 new_slot->status = 0;
2141 stop_it++;
2145 } while (function < max_functions);
2146 dbg("returning from configure_new_device\n");
2148 return 0;
2153 * Configuration logic that involves the hotplug data structures and
2154 * their bookkeeping
2159 * configure_new_function - Configures the PCI header information of one device
2161 * @ctrl: pointer to controller structure
2162 * @func: pointer to function structure
2163 * @behind_bridge: 1 if this is a recursive call, 0 if not
2164 * @resources: pointer to set of resource lists
2166 * Calls itself recursively for bridged devices.
2167 * Returns 0 if success
2170 static int configure_new_function (struct controller * ctrl, struct pci_func * func,
2171 u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
2173 int cloop;
2174 u8 temp_byte;
2175 u8 device;
2176 u8 class_code;
2177 u16 temp_word;
2178 u32 rc;
2179 u32 temp_register;
2180 u32 base;
2181 u32 ID;
2182 unsigned int devfn;
2183 struct pci_resource *mem_node;
2184 struct pci_resource *p_mem_node;
2185 struct pci_resource *io_node;
2186 struct pci_resource *bus_node;
2187 struct pci_resource *hold_mem_node;
2188 struct pci_resource *hold_p_mem_node;
2189 struct pci_resource *hold_IO_node;
2190 struct pci_resource *hold_bus_node;
2191 struct irq_mapping irqs;
2192 struct pci_func *new_slot;
2193 struct pci_bus lpci_bus, *pci_bus;
2194 struct resource_lists temp_resources;
2195 #if defined(CONFIG_X86_64)
2196 u8 IRQ=0;
2197 #endif
2199 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2200 pci_bus = &lpci_bus;
2201 pci_bus->number = func->bus;
2202 devfn = PCI_DEVFN(func->device, func->function);
2204 /* Check for Bridge */
2205 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2206 if (rc)
2207 return rc;
2209 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2210 /* set Primary bus */
2211 dbg("set Primary bus = 0x%x\n", func->bus);
2212 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2213 if (rc)
2214 return rc;
2216 /* find range of busses to use */
2217 bus_node = get_max_resource(&resources->bus_head, 1L);
2219 /* If we don't have any busses to allocate, we can't continue */
2220 if (!bus_node) {
2221 err("Got NO bus resource to use\n");
2222 return -ENOMEM;
2224 dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2226 /* set Secondary bus */
2227 temp_byte = (u8)bus_node->base;
2228 dbg("set Secondary bus = 0x%x\n", temp_byte);
2229 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2230 if (rc)
2231 return rc;
2233 /* set subordinate bus */
2234 temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2235 dbg("set subordinate bus = 0x%x\n", temp_byte);
2236 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2237 if (rc)
2238 return rc;
2240 /* Set HP parameters (Cache Line Size, Latency Timer) */
2241 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2242 if (rc)
2243 return rc;
2245 /* Setup the IO, memory, and prefetchable windows */
2247 io_node = get_max_resource(&(resources->io_head), 0x1000L);
2248 if (io_node) {
2249 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
2252 mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2253 if (mem_node) {
2254 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
2257 if (resources->p_mem_head)
2258 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2259 else {
2261 * In some platform implementation, MEM and PMEM are not
2262 * distinguished, and hence ACPI _CRS has only MEM entries
2263 * for both MEM and PMEM.
2265 dbg("using MEM for PMEM\n");
2266 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2268 if (p_mem_node) {
2269 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
2272 /* set up the IRQ info */
2273 if (!resources->irqs) {
2274 irqs.barber_pole = 0;
2275 irqs.interrupt[0] = 0;
2276 irqs.interrupt[1] = 0;
2277 irqs.interrupt[2] = 0;
2278 irqs.interrupt[3] = 0;
2279 irqs.valid_INT = 0;
2280 } else {
2281 irqs.barber_pole = resources->irqs->barber_pole;
2282 irqs.interrupt[0] = resources->irqs->interrupt[0];
2283 irqs.interrupt[1] = resources->irqs->interrupt[1];
2284 irqs.interrupt[2] = resources->irqs->interrupt[2];
2285 irqs.interrupt[3] = resources->irqs->interrupt[3];
2286 irqs.valid_INT = resources->irqs->valid_INT;
2289 /* set up resource lists that are now aligned on top and bottom
2290 * for anything behind the bridge.
2292 temp_resources.bus_head = bus_node;
2293 temp_resources.io_head = io_node;
2294 temp_resources.mem_head = mem_node;
2295 temp_resources.p_mem_head = p_mem_node;
2296 temp_resources.irqs = &irqs;
2298 /* Make copies of the nodes we are going to pass down so that
2299 * if there is a problem,we can just use these to free resources
2301 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2302 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2303 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2304 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2306 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2307 kfree(hold_bus_node);
2308 kfree(hold_IO_node);
2309 kfree(hold_mem_node);
2310 kfree(hold_p_mem_node);
2312 return 1;
2315 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2317 bus_node->base += 1;
2318 bus_node->length -= 1;
2319 bus_node->next = NULL;
2321 /* If we have IO resources copy them and fill in the bridge's
2322 * IO range registers
2324 if (io_node) {
2325 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2326 io_node->next = NULL;
2328 /* set IO base and Limit registers */
2329 RES_CHECK(io_node->base, 8);
2330 temp_byte = (u8)(io_node->base >> 8);
2331 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2333 RES_CHECK(io_node->base + io_node->length - 1, 8);
2334 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2335 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2336 } else {
2337 kfree(hold_IO_node);
2338 hold_IO_node = NULL;
2341 /* If we have memory resources copy them and fill in the bridge's
2342 * memory range registers. Otherwise, fill in the range
2343 * registers with values that disable them.
2345 if (mem_node) {
2346 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2347 mem_node->next = NULL;
2349 /* set Mem base and Limit registers */
2350 RES_CHECK(mem_node->base, 16);
2351 temp_word = (u32)(mem_node->base >> 16);
2352 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2354 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
2355 temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
2356 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2357 } else {
2358 temp_word = 0xFFFF;
2359 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2361 temp_word = 0x0000;
2362 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2364 kfree(hold_mem_node);
2365 hold_mem_node = NULL;
2368 /* If we have prefetchable memory resources copy them and
2369 * fill in the bridge's memory range registers. Otherwise,
2370 * fill in the range registers with values that disable them.
2372 if (p_mem_node) {
2373 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2374 p_mem_node->next = NULL;
2376 /* set Pre Mem base and Limit registers */
2377 RES_CHECK(p_mem_node->base, 16);
2378 temp_word = (u32)(p_mem_node->base >> 16);
2379 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2381 RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
2382 temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
2383 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2384 } else {
2385 temp_word = 0xFFFF;
2386 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2388 temp_word = 0x0000;
2389 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2391 kfree(hold_p_mem_node);
2392 hold_p_mem_node = NULL;
2395 /* Adjust this to compensate for extra adjustment in first loop */
2396 irqs.barber_pole--;
2398 rc = 0;
2400 /* Here we actually find the devices and configure them */
2401 for (device = 0; (device <= 0x1F) && !rc; device++) {
2402 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2404 ID = 0xFFFFFFFF;
2405 pci_bus->number = hold_bus_node->base;
2406 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
2407 PCI_VENDOR_ID, &ID);
2408 pci_bus->number = func->bus;
2410 if (ID != 0xFFFFFFFF) { /* device Present */
2411 /* Setup slot structure. */
2412 new_slot = shpchp_slot_create(hold_bus_node->base);
2414 if (new_slot == NULL) {
2415 /* Out of memory */
2416 rc = -ENOMEM;
2417 continue;
2420 new_slot->bus = hold_bus_node->base;
2421 new_slot->device = device;
2422 new_slot->function = 0;
2423 new_slot->is_a_board = 1;
2424 new_slot->status = 0;
2426 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
2427 dbg("configure_new_device rc=0x%x\n",rc);
2428 } /* End of IF (device in slot?) */
2429 } /* End of FOR loop */
2431 if (rc) {
2432 shpchp_destroy_resource_list(&temp_resources);
2434 return_resource(&(resources->bus_head), hold_bus_node);
2435 return_resource(&(resources->io_head), hold_IO_node);
2436 return_resource(&(resources->mem_head), hold_mem_node);
2437 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2438 return(rc);
2441 /* save the interrupt routing information */
2442 if (resources->irqs) {
2443 resources->irqs->interrupt[0] = irqs.interrupt[0];
2444 resources->irqs->interrupt[1] = irqs.interrupt[1];
2445 resources->irqs->interrupt[2] = irqs.interrupt[2];
2446 resources->irqs->interrupt[3] = irqs.interrupt[3];
2447 resources->irqs->valid_INT = irqs.valid_INT;
2448 } else if (!behind_bridge) {
2449 /* We need to hook up the interrupts here */
2450 for (cloop = 0; cloop < 4; cloop++) {
2451 if (irqs.valid_INT & (0x01 << cloop)) {
2452 rc = shpchp_set_irq(func->bus, func->device,
2453 0x0A + cloop, irqs.interrupt[cloop]);
2454 if (rc) {
2455 shpchp_destroy_resource_list (&temp_resources);
2456 return_resource(&(resources->bus_head), hold_bus_node);
2457 return_resource(&(resources->io_head), hold_IO_node);
2458 return_resource(&(resources->mem_head), hold_mem_node);
2459 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2460 return rc;
2463 } /* end of for loop */
2466 /* Return unused bus resources
2467 * First use the temporary node to store information for the board
2469 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2470 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2472 hold_bus_node->next = func->bus_head;
2473 func->bus_head = hold_bus_node;
2475 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2477 /* set subordinate bus */
2478 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2479 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2481 if (temp_resources.bus_head->length == 0) {
2482 kfree(temp_resources.bus_head);
2483 temp_resources.bus_head = NULL;
2484 } else {
2485 dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2486 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2487 return_resource(&(resources->bus_head), temp_resources.bus_head);
2491 /* If we have IO space available and there is some left,
2492 * return the unused portion
2494 if (hold_IO_node && temp_resources.io_head) {
2495 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2496 &hold_IO_node, 0x1000);
2498 /* Check if we were able to split something off */
2499 if (io_node) {
2500 hold_IO_node->base = io_node->base + io_node->length;
2502 RES_CHECK(hold_IO_node->base, 8);
2503 temp_byte = (u8)((hold_IO_node->base) >> 8);
2504 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2506 return_resource(&(resources->io_head), io_node);
2509 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2511 /* Check if we were able to split something off */
2512 if (io_node) {
2513 /* First use the temporary node to store information for the board */
2514 hold_IO_node->length = io_node->base - hold_IO_node->base;
2516 /* If we used any, add it to the board's list */
2517 if (hold_IO_node->length) {
2518 hold_IO_node->next = func->io_head;
2519 func->io_head = hold_IO_node;
2521 RES_CHECK(io_node->base - 1, 8);
2522 temp_byte = (u8)((io_node->base - 1) >> 8);
2523 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2525 return_resource(&(resources->io_head), io_node);
2526 } else {
2527 /* it doesn't need any IO */
2528 temp_byte = 0x00;
2529 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2531 return_resource(&(resources->io_head), io_node);
2532 kfree(hold_IO_node);
2534 } else {
2535 /* it used most of the range */
2536 hold_IO_node->next = func->io_head;
2537 func->io_head = hold_IO_node;
2539 } else if (hold_IO_node) {
2540 /* it used the whole range */
2541 hold_IO_node->next = func->io_head;
2542 func->io_head = hold_IO_node;
2545 /* If we have memory space available and there is some left,
2546 * return the unused portion
2548 if (hold_mem_node && temp_resources.mem_head) {
2549 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2551 /* Check if we were able to split something off */
2552 if (mem_node) {
2553 hold_mem_node->base = mem_node->base + mem_node->length;
2555 RES_CHECK(hold_mem_node->base, 16);
2556 temp_word = (u32)((hold_mem_node->base) >> 16);
2557 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2559 return_resource(&(resources->mem_head), mem_node);
2562 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2564 /* Check if we were able to split something off */
2565 if (mem_node) {
2566 /* First use the temporary node to store information for the board */
2567 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2569 if (hold_mem_node->length) {
2570 hold_mem_node->next = func->mem_head;
2571 func->mem_head = hold_mem_node;
2573 /* configure end address */
2574 RES_CHECK(mem_node->base - 1, 16);
2575 temp_word = (u32)((mem_node->base - 1) >> 16);
2576 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2578 /* Return unused resources to the pool */
2579 return_resource(&(resources->mem_head), mem_node);
2580 } else {
2581 /* it doesn't need any Mem */
2582 temp_word = 0x0000;
2583 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2585 return_resource(&(resources->mem_head), mem_node);
2586 kfree(hold_mem_node);
2588 } else {
2589 /* it used most of the range */
2590 hold_mem_node->next = func->mem_head;
2591 func->mem_head = hold_mem_node;
2593 } else if (hold_mem_node) {
2594 /* it used the whole range */
2595 hold_mem_node->next = func->mem_head;
2596 func->mem_head = hold_mem_node;
2599 /* If we have prefetchable memory space available and there is some
2600 * left at the end, return the unused portion
2602 if (hold_p_mem_node && temp_resources.p_mem_head) {
2603 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2604 &hold_p_mem_node, 0x100000L);
2606 /* Check if we were able to split something off */
2607 if (p_mem_node) {
2608 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2610 RES_CHECK(hold_p_mem_node->base, 16);
2611 temp_word = (u32)((hold_p_mem_node->base) >> 16);
2612 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2614 return_resource(&(resources->p_mem_head), p_mem_node);
2617 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2619 /* Check if we were able to split something off */
2620 if (p_mem_node) {
2621 /* First use the temporary node to store information for the board */
2622 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2624 /* If we used any, add it to the board's list */
2625 if (hold_p_mem_node->length) {
2626 hold_p_mem_node->next = func->p_mem_head;
2627 func->p_mem_head = hold_p_mem_node;
2629 RES_CHECK(p_mem_node->base - 1, 16);
2630 temp_word = (u32)((p_mem_node->base - 1) >> 16);
2631 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2633 return_resource(&(resources->p_mem_head), p_mem_node);
2634 } else {
2635 /* it doesn't need any PMem */
2636 temp_word = 0x0000;
2637 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2639 return_resource(&(resources->p_mem_head), p_mem_node);
2640 kfree(hold_p_mem_node);
2642 } else {
2643 /* it used the most of the range */
2644 hold_p_mem_node->next = func->p_mem_head;
2645 func->p_mem_head = hold_p_mem_node;
2647 } else if (hold_p_mem_node) {
2648 /* it used the whole range */
2649 hold_p_mem_node->next = func->p_mem_head;
2650 func->p_mem_head = hold_p_mem_node;
2653 /* We should be configuring an IRQ and the bridge's base address
2654 * registers if it needs them. Although we have never seen such
2655 * a device
2658 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2660 dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2661 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2662 /* Standard device */
2663 u64 base64;
2664 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2666 if (class_code == PCI_BASE_CLASS_DISPLAY)
2667 return (DEVICE_TYPE_NOT_SUPPORTED);
2669 /* Figure out IO and memory needs */
2670 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2671 temp_register = 0xFFFFFFFF;
2673 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2674 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2675 dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, func->bus, func->device,
2676 func->function);
2678 if (!temp_register)
2679 continue;
2681 base64 = 0L;
2682 if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2683 /* Map IO */
2685 /* set base = amount of IO space */
2686 base = temp_register & 0xFFFFFFFC;
2687 base = ~base + 1;
2689 dbg("NEED IO length(0x%x)\n", base);
2690 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2692 /* allocate the resource to the board */
2693 if (io_node) {
2694 dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2695 base = (u32)io_node->base;
2696 io_node->next = func->io_head;
2697 func->io_head = io_node;
2698 } else {
2699 err("Got NO IO resource(length=0x%x)\n", base);
2700 return -ENOMEM;
2702 } else { /* map MEM */
2703 int prefetchable = 1;
2704 struct pci_resource **res_node = &func->p_mem_head;
2705 char *res_type_str = "PMEM";
2706 u32 temp_register2;
2708 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2709 prefetchable = 0;
2710 res_node = &func->mem_head;
2711 res_type_str++;
2714 base = temp_register & 0xFFFFFFF0;
2715 base = ~base + 1;
2717 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2718 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2719 dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2721 if (prefetchable && resources->p_mem_head)
2722 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2723 else {
2724 if (prefetchable)
2725 dbg("using MEM for PMEM\n");
2726 mem_node=get_resource(&(resources->mem_head), (ulong)base);
2729 /* allocate the resource to the board */
2730 if (mem_node) {
2731 base = (u32)mem_node->base;
2732 mem_node->next = *res_node;
2733 *res_node = mem_node;
2734 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
2735 mem_node->length);
2736 } else {
2737 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2738 return -ENOMEM;
2740 break;
2741 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2742 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2743 dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
2744 temp_register, base);
2746 if (prefetchable && resources->p_mem_head)
2747 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2748 else {
2749 if (prefetchable)
2750 dbg("using MEM for PMEM\n");
2751 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2754 /* allocate the resource to the board */
2755 if (mem_node) {
2756 base64 = mem_node->base;
2757 mem_node->next = *res_node;
2758 *res_node = mem_node;
2759 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
2760 (u32)base64, mem_node->length);
2761 } else {
2762 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2763 return -ENOMEM;
2765 break;
2766 default:
2767 dbg("reserved BAR type=0x%x\n", temp_register);
2768 break;
2773 if (base64) {
2774 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2775 cloop += 4;
2776 base64 >>= 32;
2778 if (base64) {
2779 dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2780 base64 = 0x0L;
2783 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2784 } else {
2785 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2787 } /* End of base register loop */
2789 #if defined(CONFIG_X86_64)
2790 /* Figure out which interrupt pin this function uses */
2791 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
2793 /* If this function needs an interrupt and we are behind a bridge
2794 and the pin is tied to something that's alread mapped,
2795 set this one the same
2797 if (temp_byte && resources->irqs &&
2798 (resources->irqs->valid_INT &
2799 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2800 /* We have to share with something already set up */
2801 IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
2802 } else {
2803 /* Program IRQ based on card type */
2804 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2806 if (class_code == PCI_BASE_CLASS_STORAGE) {
2807 IRQ = shpchp_disk_irq;
2808 } else {
2809 IRQ = shpchp_nic_irq;
2813 /* IRQ Line */
2814 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2816 if (!behind_bridge) {
2817 rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
2818 if (rc)
2819 return(1);
2820 } else {
2821 /* TBD - this code may also belong in the other clause of this If statement */
2822 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2823 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2825 #endif
2826 /* Disable ROM base Address */
2827 temp_word = 0x00L;
2828 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
2830 /* Set HP parameters (Cache Line Size, Latency Timer) */
2831 rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2832 if (rc)
2833 return rc;
2835 shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2837 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2838 } /* End of Not-A-Bridge else */
2839 else {
2840 /* It's some strange type of PCI adapter (Cardbus?) */
2841 return(DEVICE_TYPE_NOT_SUPPORTED);
2844 func->configured = 1;
2846 return 0;