patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pci / hotplug / pciehp_ctrl.c
1 /*
2  * PCI Express Hot Plug Controller Driver
3  *
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
8  *
9  * All rights reserved.
10  *
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.
15  *
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.
21  *
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.
25  *
26  * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com>
27  *
28  */
29
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 "pciehp.h"
42 #include "pciehprm.h"
43
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);
49
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 */
54
55 u8 pciehp_handle_attention_button(u8 hp_slot, void *inst_id)
56 {
57         struct controller *ctrl = (struct controller *) inst_id;
58         struct slot *p_slot;
59         u8 rc = 0;
60         u8 getstatus;
61         struct pci_func *func;
62         struct event_info *taskInfo;
63
64         /* Attention Button Change */
65         dbg("pciehp:  Attention button interrupt received.\n");
66         
67         func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
68
69         /* This is the structure that tells the worker thread what to do */
70         taskInfo = &(ctrl->event_queue[ctrl->next_event]);
71         p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
72
73         p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
74         p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
75         
76         ctrl->next_event = (ctrl->next_event + 1) % 10;
77         taskInfo->hp_slot = hp_slot;
78
79         rc++;
80
81         /*
82          *  Button pressed - See if need to TAKE ACTION!!!
83          */
84         info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
85         taskInfo->event_type = INT_BUTTON_PRESS;
86
87         if ((p_slot->state == BLINKINGON_STATE)
88             || (p_slot->state == BLINKINGOFF_STATE)) {
89                 /* Cancel if we are still blinking; this means that we press the
90                  * attention again before the 5 sec. limit expires to cancel hot-add
91                  * or hot-remove
92                  */
93                 taskInfo->event_type = INT_BUTTON_CANCEL;
94                 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
95         } else if ((p_slot->state == POWERON_STATE)
96                    || (p_slot->state == POWEROFF_STATE)) {
97                 /* Ignore if the slot is on power-on or power-off state; this 
98                  * means that the previous attention button action to hot-add or
99                  * hot-remove is undergoing
100                  */
101                 taskInfo->event_type = INT_BUTTON_IGNORE;
102                 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
103         }
104
105         if (rc)
106                 up(&event_semaphore);   /* signal event thread that new event is posted */
107
108         return 0;
109
110 }
111
112 u8 pciehp_handle_switch_change(u8 hp_slot, void *inst_id)
113 {
114         struct controller *ctrl = (struct controller *) inst_id;
115         struct slot *p_slot;
116         u8 rc = 0;
117         u8 getstatus;
118         struct pci_func *func;
119         struct event_info *taskInfo;
120
121         /* Switch Change */
122         dbg("pciehp:  Switch interrupt received.\n");
123
124         func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
125
126         /* This is the structure that tells the worker thread
127          * what to do
128          */
129         taskInfo = &(ctrl->event_queue[ctrl->next_event]);
130         ctrl->next_event = (ctrl->next_event + 1) % 10;
131         taskInfo->hp_slot = hp_slot;
132
133         rc++;
134         p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
135         p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
136         p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
137
138         if (getstatus) {
139                 /*
140                  * Switch opened
141                  */
142                 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
143                 func->switch_save = 0;
144                 taskInfo->event_type = INT_SWITCH_OPEN;
145         } else {
146                 /*
147                  *  Switch closed
148                  */
149                 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
150                 func->switch_save = 0x10;
151                 taskInfo->event_type = INT_SWITCH_CLOSE;
152         }
153
154         if (rc)
155                 up(&event_semaphore);   /* signal event thread that new event is posted */
156
157         return rc;
158 }
159
160 u8 pciehp_handle_presence_change(u8 hp_slot, void *inst_id)
161 {
162         struct controller *ctrl = (struct controller *) inst_id;
163         struct slot *p_slot;
164         u8 rc = 0;
165         struct pci_func *func;
166         struct event_info *taskInfo;
167
168         /* Presence Change */
169         dbg("pciehp:  Presence/Notify input change.\n");
170
171         func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
172
173         /* This is the structure that tells the worker thread
174          * what to do
175          */
176         taskInfo = &(ctrl->event_queue[ctrl->next_event]);
177         ctrl->next_event = (ctrl->next_event + 1) % 10;
178         taskInfo->hp_slot = hp_slot;
179
180         rc++;
181         p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
182
183         /* Switch is open, assume a presence change
184          * Save the presence state
185          */
186         p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
187         if (func->presence_save) {
188                 /*
189                  * Card Present
190                  */
191                 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
192                 taskInfo->event_type = INT_PRESENCE_ON;
193         } else {
194                 /*
195                  * Not Present
196                  */
197                 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
198                 taskInfo->event_type = INT_PRESENCE_OFF;
199         }
200
201         if (rc)
202                 up(&event_semaphore);   /* signal event thread that new event is posted */
203
204         return rc;
205 }
206
207 u8 pciehp_handle_power_fault(u8 hp_slot, void *inst_id)
208 {
209         struct controller *ctrl = (struct controller *) inst_id;
210         struct slot *p_slot;
211         u8 rc = 0;
212         struct pci_func *func;
213         struct event_info *taskInfo;
214
215         /* power fault */
216         dbg("pciehp:  Power fault interrupt received.\n");
217
218         func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
219
220         /* this is the structure that tells the worker thread
221          * what to do
222          */
223         taskInfo = &(ctrl->event_queue[ctrl->next_event]);
224         ctrl->next_event = (ctrl->next_event + 1) % 10;
225         taskInfo->hp_slot = hp_slot;
226
227         rc++;
228         p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
229
230         if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
231                 /*
232                  * power fault Cleared
233                  */
234                 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
235                 func->status = 0x00;
236                 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
237         } else {
238                 /*
239                  *   power fault
240                  */
241                 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
242                 taskInfo->event_type = INT_POWER_FAULT;
243                 /* set power fault status for this board */
244                 func->status = 0xFF;
245                 info("power fault bit %x set\n", hp_slot);
246         }
247         if (rc)
248                 up(&event_semaphore);   /* signal event thread that new event is posted */
249
250         return rc;
251 }
252
253
254 /**
255  * sort_by_size: sort nodes by their length, smallest first.
256  *
257  * @head: list to sort
258  */
259 static int sort_by_size(struct pci_resource **head)
260 {
261         struct pci_resource *current_res;
262         struct pci_resource *next_res;
263         int out_of_order = 1;
264
265         if (!(*head))
266                 return 1;
267
268         if (!((*head)->next))
269                 return 0;
270
271         while (out_of_order) {
272                 out_of_order = 0;
273
274                 /* Special case for swapping list head */
275                 if (((*head)->next) &&
276                     ((*head)->length > (*head)->next->length)) {
277                         out_of_order++;
278                         current_res = *head;
279                         *head = (*head)->next;
280                         current_res->next = (*head)->next;
281                         (*head)->next = current_res;
282                 }
283
284                 current_res = *head;
285
286                 while (current_res->next && current_res->next->next) {
287                         if (current_res->next->length > current_res->next->next->length) {
288                                 out_of_order++;
289                                 next_res = current_res->next;
290                                 current_res->next = current_res->next->next;
291                                 current_res = current_res->next;
292                                 next_res->next = current_res->next;
293                                 current_res->next = next_res;
294                         } else
295                                 current_res = current_res->next;
296                 }
297         }  /* End of out_of_order loop */
298
299         return 0;
300 }
301
302
303 /*
304  * sort_by_max_size
305  *
306  * Sorts nodes on the list by their length.
307  * Largest first.
308  *
309  */
310 static int sort_by_max_size(struct pci_resource **head)
311 {
312         struct pci_resource *current_res;
313         struct pci_resource *next_res;
314         int out_of_order = 1;
315
316         if (!(*head))
317                 return 1;
318
319         if (!((*head)->next))
320                 return 0;
321
322         while (out_of_order) {
323                 out_of_order = 0;
324
325                 /* Special case for swapping list head */
326                 if (((*head)->next) &&
327                     ((*head)->length < (*head)->next->length)) {
328                         out_of_order++;
329                         current_res = *head;
330                         *head = (*head)->next;
331                         current_res->next = (*head)->next;
332                         (*head)->next = current_res;
333                 }
334
335                 current_res = *head;
336
337                 while (current_res->next && current_res->next->next) {
338                         if (current_res->next->length < current_res->next->next->length) {
339                                 out_of_order++;
340                                 next_res = current_res->next;
341                                 current_res->next = current_res->next->next;
342                                 current_res = current_res->next;
343                                 next_res->next = current_res->next;
344                                 current_res->next = next_res;
345                         } else
346                                 current_res = current_res->next;
347                 }
348         }  /* End of out_of_order loop */
349
350         return 0;
351 }
352
353
354 /**
355  * do_pre_bridge_resource_split: return one unused resource node
356  * @head: list to scan
357  *
358  */
359 static struct pci_resource *
360 do_pre_bridge_resource_split(struct pci_resource **head,
361                                 struct pci_resource **orig_head, u32 alignment)
362 {
363         struct pci_resource *prevnode = NULL;
364         struct pci_resource *node;
365         struct pci_resource *split_node;
366         u32 rc;
367         u32 temp_dword;
368         dbg("do_pre_bridge_resource_split\n");
369
370         if (!(*head) || !(*orig_head))
371                 return NULL;
372
373         rc = pciehp_resource_sort_and_combine(head);
374
375         if (rc)
376                 return NULL;
377
378         if ((*head)->base != (*orig_head)->base)
379                 return NULL;
380
381         if ((*head)->length == (*orig_head)->length)
382                 return NULL;
383
384
385         /* If we got here, there the bridge requires some of the resource, but
386          *  we may be able to split some off of the front
387          */     
388         node = *head;
389
390         if (node->length & (alignment -1)) {
391                 /* this one isn't an aligned length, so we'll make a new entry
392                  * and split it up.
393                  */
394                 split_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
395
396                 if (!split_node)
397                         return NULL;
398
399                 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
400
401                 split_node->base = node->base;
402                 split_node->length = temp_dword;
403
404                 node->length -= temp_dword;
405                 node->base += split_node->length;
406
407                 /* Put it in the list */
408                 *head = split_node;
409                 split_node->next = node;
410         }
411
412         if (node->length < alignment)
413                 return NULL;
414
415         /* Now unlink it */
416         if (*head == node) {
417                 *head = node->next;
418         } else {
419                 prevnode = *head;
420                 while (prevnode->next != node)
421                         prevnode = prevnode->next;
422
423                 prevnode->next = node->next;
424         }
425         node->next = NULL;
426
427         return node;
428 }
429
430
431 /**
432  * do_bridge_resource_split: return one unused resource node
433  * @head: list to scan
434  *
435  */
436 static struct pci_resource *
437 do_bridge_resource_split(struct pci_resource **head, u32 alignment)
438 {
439         struct pci_resource *prevnode = NULL;
440         struct pci_resource *node;
441         u32 rc;
442         u32 temp_dword;
443
444         if (!(*head))
445                 return NULL;
446
447         rc = pciehp_resource_sort_and_combine(head);
448
449         if (rc)
450                 return NULL;
451
452         node = *head;
453
454         while (node->next) {
455                 prevnode = node;
456                 node = node->next;
457                 kfree(prevnode);
458         }
459
460         if (node->length < alignment) {
461                 kfree(node);
462                 return NULL;
463         }
464
465         if (node->base & (alignment - 1)) {
466                 /* Short circuit if adjusted size is too small */
467                 temp_dword = (node->base | (alignment-1)) + 1;
468                 if ((node->length - (temp_dword - node->base)) < alignment) {
469                         kfree(node);
470                         return NULL;
471                 }
472
473                 node->length -= (temp_dword - node->base);
474                 node->base = temp_dword;
475         }
476
477         if (node->length & (alignment - 1)) {
478                 /* There's stuff in use after this node */
479                 kfree(node);
480                 return NULL;
481         }
482
483         return node;
484 }
485
486
487 /*
488  * get_io_resource
489  *
490  * this function sorts the resource list by size and then
491  * returns the first node of "size" length that is not in the
492  * ISA aliasing window.  If it finds a node larger than "size"
493  * it will split it up.
494  *
495  * size must be a power of two.
496  */
497 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
498 {
499         struct pci_resource *prevnode;
500         struct pci_resource *node;
501         struct pci_resource *split_node = NULL;
502         u32 temp_dword;
503
504         if (!(*head))
505                 return NULL;
506
507         if ( pciehp_resource_sort_and_combine(head) )
508                 return NULL;
509
510         if ( sort_by_size(head) )
511                 return NULL;
512
513         for (node = *head; node; node = node->next) {
514                 if (node->length < size)
515                         continue;
516
517                 if (node->base & (size - 1)) {
518                         /* this one isn't base aligned properly
519                            so we'll make a new entry and split it up */
520                         temp_dword = (node->base | (size-1)) + 1;
521
522                         /*/ Short circuit if adjusted size is too small */
523                         if ((node->length - (temp_dword - node->base)) < size)
524                                 continue;
525
526                         split_node = kmalloc(sizeof(struct pci_resource),
527                                                 GFP_KERNEL);
528
529                         if (!split_node)
530                                 return NULL;
531
532                         split_node->base = node->base;
533                         split_node->length = temp_dword - node->base;
534                         node->base = temp_dword;
535                         node->length -= split_node->length;
536
537                         /* Put it in the list */
538                         split_node->next = node->next;
539                         node->next = split_node;
540                 } /* End of non-aligned base */
541
542                 /* Don't need to check if too small since we already did */
543                 if (node->length > size) {
544                         /* this one is longer than we need
545                            so we'll make a new entry and split it up */
546                         split_node = kmalloc(sizeof(struct pci_resource),
547                                                 GFP_KERNEL);
548
549                         if (!split_node)
550                                 return NULL;
551
552                         split_node->base = node->base + size;
553                         split_node->length = node->length - size;
554                         node->length = size;
555
556                         /* Put it in the list */
557                         split_node->next = node->next;
558                         node->next = split_node;
559                 }  /* End of too big on top end */
560
561                 /* For IO make sure it's not in the ISA aliasing space */
562                 if (node->base & 0x300L)
563                         continue;
564
565                 /* If we got here, then it is the right size 
566                    Now take it out of the list */
567                 if (*head == node) {
568                         *head = node->next;
569                 } else {
570                         prevnode = *head;
571                         while (prevnode->next != node)
572                                 prevnode = prevnode->next;
573
574                         prevnode->next = node->next;
575                 }
576                 node->next = NULL;
577                 /* Stop looping */
578                 break;
579         }
580
581         return node;
582 }
583
584
585 /*
586  * get_max_resource
587  *
588  * Gets the largest node that is at least "size" big from the
589  * list pointed to by head.  It aligns the node on top and bottom
590  * to "size" alignment before returning it.
591  * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
592  *  This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
593  */
594 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
595 {
596         struct pci_resource *max;
597         struct pci_resource *temp;
598         struct pci_resource *split_node;
599         u32 temp_dword;
600         u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
601         int i;
602
603         if (!(*head))
604                 return NULL;
605
606         if (pciehp_resource_sort_and_combine(head))
607                 return NULL;
608
609         if (sort_by_max_size(head))
610                 return NULL;
611
612         for (max = *head;max; max = max->next) {
613
614                 /* If not big enough we could probably just bail, 
615                    instead we'll continue to the next. */
616                 if (max->length < size)
617                         continue;
618
619                 if (max->base & (size - 1)) {
620                         /* this one isn't base aligned properly
621                            so we'll make a new entry and split it up */
622                         temp_dword = (max->base | (size-1)) + 1;
623
624                         /* Short circuit if adjusted size is too small */
625                         if ((max->length - (temp_dword - max->base)) < size)
626                                 continue;
627
628                         split_node = kmalloc(sizeof(struct pci_resource),
629                                                 GFP_KERNEL);
630
631                         if (!split_node)
632                                 return NULL;
633
634                         split_node->base = max->base;
635                         split_node->length = temp_dword - max->base;
636                         max->base = temp_dword;
637                         max->length -= split_node->length;
638
639                         /* Put it next in the list */
640                         split_node->next = max->next;
641                         max->next = split_node;
642                 }
643
644                 if ((max->base + max->length) & (size - 1)) {
645                         /* this one isn't end aligned properly at the top
646                            so we'll make a new entry and split it up */
647                         split_node = kmalloc(sizeof(struct pci_resource),
648                                                 GFP_KERNEL);
649
650                         if (!split_node)
651                                 return NULL;
652                         temp_dword = ((max->base + max->length) & ~(size - 1));
653                         split_node->base = temp_dword;
654                         split_node->length = max->length + max->base
655                                              - split_node->base;
656                         max->length -= split_node->length;
657
658                         /* Put it in the list */
659                         split_node->next = max->next;
660                         max->next = split_node;
661                 }
662
663                 /* Make sure it didn't shrink too much when we aligned it */
664                 if (max->length < size)
665                         continue;
666
667                 for ( i = 0; max_size[i] > size; i++) {
668                         if (max->length > max_size[i]) {
669                                 split_node = kmalloc(sizeof(struct pci_resource),
670                                                         GFP_KERNEL);
671                                 if (!split_node)
672                                         break;  /* return NULL; */
673                                 split_node->base = max->base + max_size[i];
674                                 split_node->length = max->length - max_size[i];
675                                 max->length = max_size[i];
676                                 /* Put it next in the list */
677                                 split_node->next = max->next;
678                                 max->next = split_node;
679                                 break;
680                         }
681                 }
682
683                 /* Now take it out of the list */
684                 temp = (struct pci_resource*) *head;
685                 if (temp == max) {
686                         *head = max->next;
687                 } else {
688                         while (temp && temp->next != max) {
689                                 temp = temp->next;
690                         }
691
692                         temp->next = max->next;
693                 }
694
695                 max->next = NULL;
696                 return max;
697         }
698
699         /* If we get here, we couldn't find one */
700         return NULL;
701 }
702
703
704 /*
705  * get_resource
706  *
707  * this function sorts the resource list by size and then
708  * returns the first node of "size" length.  If it finds a node
709  * larger than "size" it will split it up.
710  *
711  * size must be a power of two.
712  */
713 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
714 {
715         struct pci_resource *prevnode;
716         struct pci_resource *node;
717         struct pci_resource *split_node;
718         u32 temp_dword;
719
720         if (!(*head))
721                 return NULL;
722
723         if ( pciehp_resource_sort_and_combine(head) )
724                 return NULL;
725
726         if ( sort_by_size(head) )
727                 return NULL;
728
729         for (node = *head; node; node = node->next) {
730                 dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
731                     __FUNCTION__, size, node, node->base, node->length);
732                 if (node->length < size)
733                         continue;
734
735                 if (node->base & (size - 1)) {
736                         dbg("%s: not aligned\n", __FUNCTION__);
737                         /* this one isn't base aligned properly
738                            so we'll make a new entry and split it up */
739                         temp_dword = (node->base | (size-1)) + 1;
740
741                         /* Short circuit if adjusted size is too small */
742                         if ((node->length - (temp_dword - node->base)) < size)
743                                 continue;
744
745                         split_node = kmalloc(sizeof(struct pci_resource),
746                                                 GFP_KERNEL);
747
748                         if (!split_node)
749                                 return NULL;
750
751                         split_node->base = node->base;
752                         split_node->length = temp_dword - node->base;
753                         node->base = temp_dword;
754                         node->length -= split_node->length;
755
756                         /* Put it in the list */
757                         split_node->next = node->next;
758                         node->next = split_node;
759                 } /* End of non-aligned base */
760
761                 /* Don't need to check if too small since we already did */
762                 if (node->length > size) {
763                         dbg("%s: too big\n", __FUNCTION__);
764                         /* this one is longer than we need
765                            so we'll make a new entry and split it up */
766                         split_node = kmalloc(sizeof(struct pci_resource),
767                                                 GFP_KERNEL);
768
769                         if (!split_node)
770                                 return NULL;
771
772                         split_node->base = node->base + size;
773                         split_node->length = node->length - size;
774                         node->length = size;
775
776                         /* Put it in the list */
777                         split_node->next = node->next;
778                         node->next = split_node;
779                 }  /* End of too big on top end */
780
781                 dbg("%s: got one!!!\n", __FUNCTION__);
782                 /* If we got here, then it is the right size
783                    Now take it out of the list */
784                 if (*head == node) {
785                         *head = node->next;
786                 } else {
787                         prevnode = *head;
788                         while (prevnode->next != node)
789                                 prevnode = prevnode->next;
790
791                         prevnode->next = node->next;
792                 }
793                 node->next = NULL;
794                 /* Stop looping */
795                 break;
796         }
797         return node;
798 }
799
800
801 /*
802  * pciehp_resource_sort_and_combine
803  *
804  * Sorts all of the nodes in the list in ascending order by
805  * their base addresses.  Also does garbage collection by
806  * combining adjacent nodes.
807  *
808  * returns 0 if success
809  */
810 int pciehp_resource_sort_and_combine(struct pci_resource **head)
811 {
812         struct pci_resource *node1;
813         struct pci_resource *node2;
814         int out_of_order = 1;
815
816         dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
817
818         if (!(*head))
819                 return 1;
820
821         dbg("*head->next = %p\n",(*head)->next);
822
823         if (!(*head)->next)
824                 return 0;       /* only one item on the list, already sorted! */
825
826         dbg("*head->base = 0x%x\n",(*head)->base);
827         dbg("*head->next->base = 0x%x\n",(*head)->next->base);
828         while (out_of_order) {
829                 out_of_order = 0;
830
831                 /* Special case for swapping list head */
832                 if (((*head)->next) &&
833                     ((*head)->base > (*head)->next->base)) {
834                         node1 = *head;
835                         (*head) = (*head)->next;
836                         node1->next = (*head)->next;
837                         (*head)->next = node1;
838                         out_of_order++;
839                 }
840
841                 node1 = (*head);
842
843                 while (node1->next && node1->next->next) {
844                         if (node1->next->base > node1->next->next->base) {
845                                 out_of_order++;
846                                 node2 = node1->next;
847                                 node1->next = node1->next->next;
848                                 node1 = node1->next;
849                                 node2->next = node1->next;
850                                 node1->next = node2;
851                         } else
852                                 node1 = node1->next;
853                 }
854         }  /* End of out_of_order loop */
855
856         node1 = *head;
857
858         while (node1 && node1->next) {
859                 if ((node1->base + node1->length) == node1->next->base) {
860                         /* Combine */
861                         dbg("8..\n");
862                         node1->length += node1->next->length;
863                         node2 = node1->next;
864                         node1->next = node1->next->next;
865                         kfree(node2);
866                 } else
867                         node1 = node1->next;
868         }
869
870         return 0;
871 }
872
873
874 /**
875  * pciehp_slot_create - Creates a node and adds it to the proper bus.
876  * @busnumber - bus where new node is to be located
877  *
878  * Returns pointer to the new node or NULL if unsuccessful
879  */
880 struct pci_func *pciehp_slot_create(u8 busnumber)
881 {
882         struct pci_func *new_slot;
883         struct pci_func *next;
884         dbg("%s: busnumber %x\n", __FUNCTION__, busnumber);
885         new_slot = kmalloc(sizeof(struct pci_func), GFP_KERNEL);
886
887         if (new_slot == NULL)
888                 return new_slot;
889
890         memset(new_slot, 0, sizeof(struct pci_func));
891
892         new_slot->next = NULL;
893         new_slot->configured = 1;
894
895         if (pciehp_slot_list[busnumber] == NULL) {
896                 pciehp_slot_list[busnumber] = new_slot;
897         } else {
898                 next = pciehp_slot_list[busnumber];
899                 while (next->next != NULL)
900                         next = next->next;
901                 next->next = new_slot;
902         }
903         return new_slot;
904 }
905
906
907 /**
908  * slot_remove - Removes a node from the linked list of slots.
909  * @old_slot: slot to remove
910  *
911  * Returns 0 if successful, !0 otherwise.
912  */
913 static int slot_remove(struct pci_func * old_slot)
914 {
915         struct pci_func *next;
916
917         if (old_slot == NULL)
918                 return 1;
919
920         next = pciehp_slot_list[old_slot->bus];
921
922         if (next == NULL)
923                 return 1;
924
925         if (next == old_slot) {
926                 pciehp_slot_list[old_slot->bus] = old_slot->next;
927                 pciehp_destroy_board_resources(old_slot);
928                 kfree(old_slot);
929                 return 0;
930         }
931
932         while ((next->next != old_slot) && (next->next != NULL)) {
933                 next = next->next;
934         }
935
936         if (next->next == old_slot) {
937                 next->next = old_slot->next;
938                 pciehp_destroy_board_resources(old_slot);
939                 kfree(old_slot);
940                 return 0;
941         } else
942                 return 2;
943 }
944
945
946 /**
947  * bridge_slot_remove - Removes a node from the linked list of slots.
948  * @bridge: bridge to remove
949  *
950  * Returns 0 if successful, !0 otherwise.
951  */
952 static int bridge_slot_remove(struct pci_func *bridge)
953 {
954         u8 subordinateBus, secondaryBus;
955         u8 tempBus;
956         struct pci_func *next;
957
958         if (bridge == NULL)
959                 return 1;
960
961         secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
962         subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
963
964         for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
965                 next = pciehp_slot_list[tempBus];
966
967                 while (!slot_remove(next)) {
968                         next = pciehp_slot_list[tempBus];
969                 }
970         }
971
972         next = pciehp_slot_list[bridge->bus];
973
974         if (next == NULL) {
975                 return 1;
976         }
977
978         if (next == bridge) {
979                 pciehp_slot_list[bridge->bus] = bridge->next;
980                 kfree(bridge);
981                 return 0;
982         }
983
984         while ((next->next != bridge) && (next->next != NULL)) {
985                 next = next->next;
986         }
987
988         if (next->next == bridge) {
989                 next->next = bridge->next;
990                 kfree(bridge);
991                 return 0;
992         } else
993                 return 2;
994 }
995
996
997 /**
998  * pciehp_slot_find - Looks for a node by bus, and device, multiple functions accessed
999  * @bus: bus to find
1000  * @device: device to find
1001  * @index: is 0 for first function found, 1 for the second...
1002  *
1003  * Returns pointer to the node if successful, %NULL otherwise.
1004  */
1005 struct pci_func *pciehp_slot_find(u8 bus, u8 device, u8 index)
1006 {
1007         int found = -1;
1008         struct pci_func *func;
1009
1010         func = pciehp_slot_list[bus];
1011         dbg("%s: bus %x device %x index %x\n",
1012                 __FUNCTION__, bus, device, index);
1013         if (func != NULL) {
1014                 dbg("%s: func-> bus %x device %x function %x pci_dev %p\n",
1015                         __FUNCTION__, func->bus, func->device, func->function,
1016                         func->pci_dev);
1017         } else
1018                 dbg("%s: func == NULL\n", __FUNCTION__);
1019
1020         if ((func == NULL) || ((func->device == device) && (index == 0)))
1021                 return func;
1022
1023         if (func->device == device)
1024                 found++;
1025
1026         while (func->next != NULL) {
1027                 func = func->next;
1028
1029                 dbg("%s: In while loop, func-> bus %x device %x function %x pci_dev %p\n",
1030                         __FUNCTION__, func->bus, func->device, func->function,
1031                         func->pci_dev);
1032                 if (func->device == device)
1033                         found++;
1034                 dbg("%s: while loop, found %d, index %d\n", __FUNCTION__,
1035                         found, index);
1036
1037                 if ((found == index) || (func->function == index)) {
1038                         dbg("%s: Found bus %x dev %x func %x\n", __FUNCTION__,
1039                                         func->bus, func->device, func->function);
1040                         return func;
1041                 }
1042         }
1043
1044         return NULL;
1045 }
1046
1047 static int is_bridge(struct pci_func * func)
1048 {
1049         /* Check the header type */
1050         if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1051                 return 1;
1052         else
1053                 return 0;
1054 }
1055
1056
1057 /* The following routines constitute the bulk of the 
1058    hotplug controller logic
1059  */
1060
1061
1062 /**
1063  * board_added - Called after a board has been added to the system.
1064  *
1065  * Turns power on for the board
1066  * Configures board
1067  *
1068  */
1069 static u32 board_added(struct pci_func * func, struct controller * ctrl)
1070 {
1071         u8 hp_slot;
1072         int index;
1073         u32 temp_register = 0xFFFFFFFF;
1074         u32 retval, rc = 0;
1075         struct pci_func *new_func = NULL;
1076         struct slot *p_slot;
1077         struct resource_lists res_lists;
1078
1079         p_slot = pciehp_find_slot(ctrl, func->device);
1080         hp_slot = func->device - ctrl->slot_device_offset;
1081
1082         dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
1083
1084         /* Wait for exclusive access to hardware */
1085         down(&ctrl->crit_sect);
1086
1087         /* Power on slot */
1088         rc = p_slot->hpc_ops->power_on_slot(p_slot);
1089         if (rc)
1090                 return -1;
1091
1092         /* Wait for the command to complete */
1093         wait_for_ctrl_irq (ctrl);
1094         
1095         p_slot->hpc_ops->green_led_blink(p_slot);
1096                         
1097         /* Wait for the command to complete */
1098         wait_for_ctrl_irq (ctrl);
1099
1100         /* Done with exclusive hardware access */
1101         up(&ctrl->crit_sect);
1102
1103         /* Wait for ~1 second */
1104         dbg("%s: before long_delay\n", __FUNCTION__);
1105         wait_for_ctrl_irq (ctrl);
1106         dbg("%s: afterlong_delay\n", __FUNCTION__);
1107
1108         /*  Make this to check for link training status */
1109         rc = p_slot->hpc_ops->check_lnk_status(ctrl);  
1110         if (rc) {
1111                 err("%s: Failed to check link status\n", __FUNCTION__);
1112                 return -1;
1113         }
1114
1115         dbg("%s: func status = %x\n", __FUNCTION__, func->status);
1116
1117         /* Check for a power fault */
1118         if (func->status == 0xFF) {
1119                 /* power fault occurred, but it was benign */
1120                 temp_register = 0xFFFFFFFF;
1121                 dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
1122                 rc = POWER_FAILURE;
1123                 func->status = 0;
1124         } else {
1125                 /* Get vendor/device ID u32 */
1126                 rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function), 
1127                         PCI_VENDOR_ID, &temp_register);
1128                 dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
1129                 dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
1130
1131                 if (rc != 0) {
1132                         /* Something's wrong here */
1133                         temp_register = 0xFFFFFFFF;
1134                         dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
1135                 }
1136                 /* Preset return code.  It will be changed later if things go okay. */
1137                 rc = NO_ADAPTER_PRESENT;
1138         }
1139
1140         /* All F's is an empty slot or an invalid board */
1141         if (temp_register != 0xFFFFFFFF) {        /* Check for a board in the slot */
1142                 res_lists.io_head = ctrl->io_head;
1143                 res_lists.mem_head = ctrl->mem_head;
1144                 res_lists.p_mem_head = ctrl->p_mem_head;
1145                 res_lists.bus_head = ctrl->bus_head;
1146                 res_lists.irqs = NULL;
1147
1148                 rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
1149                 dbg("%s: back from configure_new_device\n", __FUNCTION__);
1150
1151                 ctrl->io_head = res_lists.io_head;
1152                 ctrl->mem_head = res_lists.mem_head;
1153                 ctrl->p_mem_head = res_lists.p_mem_head;
1154                 ctrl->bus_head = res_lists.bus_head;
1155
1156                 pciehp_resource_sort_and_combine(&(ctrl->mem_head));
1157                 pciehp_resource_sort_and_combine(&(ctrl->p_mem_head));
1158                 pciehp_resource_sort_and_combine(&(ctrl->io_head));
1159                 pciehp_resource_sort_and_combine(&(ctrl->bus_head));
1160
1161                 if (rc) {
1162                         /* Wait for exclusive access to hardware */
1163                         down(&ctrl->crit_sect);
1164
1165                         /* turn off slot, turn on Amber LED, turn off Green LED */
1166                         retval = p_slot->hpc_ops->power_off_slot(p_slot);   
1167                         /* In PCI Express, just power off slot */
1168                         if (retval) {
1169                                 err("%s: Issue of Slot Power Off command failed\n", __FUNCTION__);
1170                                 return retval;
1171                         }
1172                         /* Wait for the command to complete */
1173                         wait_for_ctrl_irq (ctrl);
1174
1175                         p_slot->hpc_ops->green_led_off(p_slot);   
1176                         
1177                         /* Wait for the command to complete */
1178                         wait_for_ctrl_irq (ctrl);
1179
1180                         /* turn on Amber LED */
1181                         retval = p_slot->hpc_ops->set_attention_status(p_slot, 1);   
1182                         if (retval) {
1183                                 err("%s: Issue of Set Attention Led command failed\n", __FUNCTION__);
1184                                 return retval;
1185                         }
1186                         /* Wait for the command to complete */
1187                         wait_for_ctrl_irq (ctrl);
1188
1189                         /* Done with exclusive hardware access */
1190                         up(&ctrl->crit_sect);
1191
1192                         return rc;
1193                 }
1194                 pciehp_save_slot_config(ctrl, func);
1195
1196                 func->status = 0;
1197                 func->switch_save = 0x10;
1198                 func->is_a_board = 0x01;
1199
1200                 /* next, we will instantiate the linux pci_dev structures 
1201                  * (with appropriate driver notification, if already present) 
1202                  */
1203                 index = 0;
1204                 do {
1205                         new_func = pciehp_slot_find(ctrl->slot_bus, func->device, index++);
1206                         if (new_func && !new_func->pci_dev) {
1207                                 dbg("%s:call pci_hp_configure_dev, func %x\n", 
1208                                         __FUNCTION__, index);
1209                                 pciehp_configure_device(ctrl, new_func);
1210                         }
1211                 } while (new_func);
1212
1213                 /* Wait for exclusive access to hardware */
1214                 down(&ctrl->crit_sect);
1215
1216                 p_slot->hpc_ops->green_led_on(p_slot);
1217
1218                 /* Wait for the command to complete */
1219                 wait_for_ctrl_irq (ctrl);
1220
1221
1222                 /* Done with exclusive hardware access */
1223                 up(&ctrl->crit_sect);
1224
1225         } else {
1226                 /* Wait for exclusive access to hardware */
1227                 down(&ctrl->crit_sect);
1228
1229                 /* turn off slot, turn on Amber LED, turn off Green LED */
1230                 retval = p_slot->hpc_ops->power_off_slot(p_slot);   
1231                 /* In PCI Express, just power off slot */
1232                 if (retval) {
1233                         err("%s: Issue of Slot Power Off command failed\n", __FUNCTION__);
1234                         return retval;
1235                 }
1236                 /* Wait for the command to complete */
1237                 wait_for_ctrl_irq (ctrl);
1238
1239                 p_slot->hpc_ops->green_led_off(p_slot);   
1240                 
1241                 /* Wait for the command to complete */
1242                 wait_for_ctrl_irq (ctrl);
1243
1244                 /* turn on Amber LED */
1245                 retval = p_slot->hpc_ops->set_attention_status(p_slot, 1);   
1246                 if (retval) {
1247                         err("%s: Issue of Set Attention Led command failed\n", __FUNCTION__);
1248                         return retval;
1249                 }
1250                 /* Wait for the command to complete */
1251                 wait_for_ctrl_irq (ctrl);
1252
1253                 /* Done with exclusive hardware access */
1254                 up(&ctrl->crit_sect);
1255
1256                 return rc;
1257         }
1258         return 0;
1259 }
1260
1261
1262 /**
1263  * remove_board - Turns off slot and LED's
1264  *
1265  */
1266 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
1267 {
1268         int index;
1269         u8 skip = 0;
1270         u8 device;
1271         u8 hp_slot;
1272         u32 rc;
1273         struct resource_lists res_lists;
1274         struct pci_func *temp_func;
1275         struct slot *p_slot;
1276
1277         if (func == NULL)
1278                 return 1;
1279
1280         if (pciehp_unconfigure_device(func))
1281                 return 1;
1282
1283         device = func->device;
1284
1285         hp_slot = func->device - ctrl->slot_device_offset;
1286         p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1287
1288         dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
1289
1290         if ((ctrl->add_support) &&
1291                 !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
1292                 /* Here we check to see if we've saved any of the board's
1293                  * resources already.  If so, we'll skip the attempt to
1294                  * determine what's being used.
1295                  */
1296                 index = 0;
1297
1298                 temp_func = func;
1299
1300                 while ((temp_func = pciehp_slot_find(temp_func->bus, temp_func->device, index++))) {
1301                         if (temp_func->bus_head || temp_func->mem_head
1302                             || temp_func->p_mem_head || temp_func->io_head) {
1303                                 skip = 1;
1304                                 break;
1305                         }
1306                 }
1307
1308                 if (!skip)
1309                         rc = pciehp_save_used_resources(ctrl, func, DISABLE_CARD);
1310         }
1311         /* Change status to shutdown */
1312         if (func->is_a_board)
1313                 func->status = 0x01;
1314         func->configured = 0;
1315
1316         /* Wait for exclusive access to hardware */
1317         down(&ctrl->crit_sect);
1318
1319         /* power off slot */
1320         rc = p_slot->hpc_ops->power_off_slot(p_slot);
1321         if (rc) {
1322                 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
1323                 return rc;
1324         }
1325         /* Wait for the command to complete */
1326         wait_for_ctrl_irq (ctrl);
1327
1328         /* turn off Green LED */
1329         p_slot->hpc_ops->green_led_off(p_slot);
1330         
1331         /* Wait for the command to complete */
1332         wait_for_ctrl_irq (ctrl);
1333
1334         /* Done with exclusive hardware access */
1335         up(&ctrl->crit_sect);
1336
1337         if (ctrl->add_support) {
1338                 while (func) {
1339                         res_lists.io_head = ctrl->io_head;
1340                         res_lists.mem_head = ctrl->mem_head;
1341                         res_lists.p_mem_head = ctrl->p_mem_head;
1342                         res_lists.bus_head = ctrl->bus_head;
1343
1344                         dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", 
1345                                 func->bus, func->device, func->function);
1346
1347                         pciehp_return_board_resources(func, &res_lists);
1348
1349                         ctrl->io_head = res_lists.io_head;
1350                         ctrl->mem_head = res_lists.mem_head;
1351                         ctrl->p_mem_head = res_lists.p_mem_head;
1352                         ctrl->bus_head = res_lists.bus_head;
1353
1354                         pciehp_resource_sort_and_combine(&(ctrl->mem_head));
1355                         pciehp_resource_sort_and_combine(&(ctrl->p_mem_head));
1356                         pciehp_resource_sort_and_combine(&(ctrl->io_head));
1357                         pciehp_resource_sort_and_combine(&(ctrl->bus_head));
1358
1359                         if (is_bridge(func)) {
1360                                 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", 
1361                                         ctrl->seg, func->bus, func->device, func->function);
1362                                 bridge_slot_remove(func);
1363                         } else
1364                                 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", 
1365                                         ctrl->seg, func->bus, func->device, func->function);
1366                                 slot_remove(func);
1367
1368                         func = pciehp_slot_find(ctrl->slot_bus, device, 0);
1369                 }
1370
1371                 /* Setup slot structure with entry for empty slot */
1372                 func = pciehp_slot_create(ctrl->slot_bus);
1373
1374                 if (func == NULL) {
1375                         return 1;
1376                 }
1377
1378                 func->bus = ctrl->slot_bus;
1379                 func->device = device;
1380                 func->function = 0;
1381                 func->configured = 0;
1382                 func->switch_save = 0x10;
1383                 func->is_a_board = 0;
1384         }
1385
1386         return 0;
1387 }
1388
1389
1390 static void pushbutton_helper_thread(unsigned long data)
1391 {
1392         pushbutton_pending = data;
1393
1394         up(&event_semaphore);
1395 }
1396
1397
1398 /**
1399  * pciehp_pushbutton_thread
1400  *
1401  * Scheduled procedure to handle blocking stuff for the pushbuttons
1402  * Handles all pending events and exits.
1403  *
1404  */
1405 static void pciehp_pushbutton_thread(unsigned long slot)
1406 {
1407         struct slot *p_slot = (struct slot *) slot;
1408         u8 getstatus;
1409         int rc;
1410         
1411         pushbutton_pending = 0;
1412
1413         if (!p_slot) {
1414                 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1415                 return;
1416         }
1417
1418         p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1419         if (getstatus) {
1420                 p_slot->state = POWEROFF_STATE;
1421                 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1422
1423                 if (pciehp_disable_slot(p_slot)) {
1424                         /* Wait for exclusive access to hardware */
1425                         down(&p_slot->ctrl->crit_sect);
1426
1427                         /* Turn on the Attention LED */
1428                         rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
1429                         if (rc) {
1430                                 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
1431                                 return;
1432                         }
1433         
1434                         /* Wait for the command to complete */
1435                         wait_for_ctrl_irq (p_slot->ctrl);
1436
1437                         /* Done with exclusive hardware access */
1438                         up(&p_slot->ctrl->crit_sect);
1439                 }
1440                 p_slot->state = STATIC_STATE;
1441         } else {
1442                 p_slot->state = POWERON_STATE;
1443                 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
1444
1445                 if (pciehp_enable_slot(p_slot)) {
1446                         /* Wait for exclusive access to hardware */
1447                         down(&p_slot->ctrl->crit_sect);
1448
1449                         /* Turn off the green LED */
1450                         rc = p_slot->hpc_ops->set_attention_status(p_slot, 1);
1451                         if (rc) {
1452                                 err("%s: Issue of Set Atten Indicator On command failed\n", __FUNCTION__);
1453                                 return;
1454                         }
1455                         /* Wait for the command to complete */
1456                         wait_for_ctrl_irq (p_slot->ctrl);
1457                         
1458                         p_slot->hpc_ops->green_led_off(p_slot);
1459
1460                         /* Wait for the command to complete */
1461                         wait_for_ctrl_irq (p_slot->ctrl);
1462
1463                         /* Done with exclusive hardware access */
1464                         up(&p_slot->ctrl->crit_sect);
1465                 }
1466                 p_slot->state = STATIC_STATE;
1467         }
1468
1469         return;
1470 }
1471
1472
1473 /* this is the main worker thread */
1474 static int event_thread(void* data)
1475 {
1476         struct controller *ctrl;
1477         lock_kernel();
1478         daemonize("pciehpd_event");
1479
1480         unlock_kernel();
1481
1482         while (1) {
1483                 dbg("!!!!event_thread sleeping\n");
1484                 down_interruptible (&event_semaphore);
1485                 dbg("event_thread woken finished = %d\n", event_finished);
1486                 if (event_finished || signal_pending(current))
1487                         break;
1488                 /* Do stuff here */
1489                 if (pushbutton_pending)
1490                         pciehp_pushbutton_thread(pushbutton_pending);
1491                 else
1492                         for (ctrl = pciehp_ctrl_list; ctrl; ctrl=ctrl->next)
1493                                 interrupt_event_handler(ctrl);
1494         }
1495         dbg("event_thread signals exit\n");
1496         up(&event_exit);
1497         return 0;
1498 }
1499
1500 int pciehp_event_start_thread(void)
1501 {
1502         int pid;
1503
1504         /* initialize our semaphores */
1505         init_MUTEX_LOCKED(&event_exit);
1506         event_finished=0;
1507
1508         init_MUTEX_LOCKED(&event_semaphore);
1509         pid = kernel_thread(event_thread, 0, 0);
1510
1511         if (pid < 0) {
1512                 err ("Can't start up our event thread\n");
1513                 return -1;
1514         }
1515         dbg("Our event thread pid = %d\n", pid);
1516         return 0;
1517 }
1518
1519
1520 void pciehp_event_stop_thread(void)
1521 {
1522         event_finished = 1;
1523         dbg("event_thread finish command given\n");
1524         up(&event_semaphore);
1525         dbg("wait for event_thread to exit\n");
1526         down(&event_exit);
1527 }
1528
1529
1530 static int update_slot_info(struct slot *slot)
1531 {
1532         struct hotplug_slot_info *info;
1533         /* char buffer[SLOT_NAME_SIZE]; */
1534         int result;
1535
1536         info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
1537         if (!info)
1538                 return -ENOMEM;
1539
1540         /* make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot); */
1541
1542         slot->hpc_ops->get_power_status(slot, &(info->power_status));
1543         slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
1544         slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
1545         slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1546
1547         /* result = pci_hp_change_slot_info(buffer, info); */
1548         result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1549         kfree (info);
1550         return result;
1551 }
1552
1553 static void interrupt_event_handler(struct controller *ctrl)
1554 {
1555         int loop = 0;
1556         int change = 1;
1557         struct pci_func *func;
1558         u8 hp_slot;
1559         u8 getstatus;
1560         struct slot *p_slot;
1561
1562         while (change) {
1563                 change = 0;
1564
1565                 for (loop = 0; loop < 10; loop++) {
1566                         if (ctrl->event_queue[loop].event_type != 0) {
1567                                 hp_slot = ctrl->event_queue[loop].hp_slot;
1568
1569                                 func = pciehp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1570
1571                                 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1572
1573                                 dbg("hp_slot %d, func %p, p_slot %p\n", hp_slot, func, p_slot);
1574
1575                                 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1576                                         dbg("button cancel\n");
1577                                         del_timer(&p_slot->task_event);
1578
1579                                         switch (p_slot->state) {
1580                                         case BLINKINGOFF_STATE:
1581                                                 /* Wait for exclusive access to hardware */
1582                                                 down(&ctrl->crit_sect);
1583
1584                                                 p_slot->hpc_ops->green_led_on(p_slot);
1585                                                 /* Wait for the command to complete */
1586                                                 wait_for_ctrl_irq (ctrl);
1587
1588                                                 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1589
1590                                                 /* Wait for the command to complete */
1591                                                 wait_for_ctrl_irq (ctrl);
1592
1593                                                 /* Done with exclusive hardware access */
1594                                                 up(&ctrl->crit_sect);
1595                                                 break;
1596                                         case BLINKINGON_STATE:
1597                                                 /* Wait for exclusive access to hardware */
1598                                                 down(&ctrl->crit_sect);
1599
1600                                                 p_slot->hpc_ops->green_led_off(p_slot);
1601                                                 /* Wait for the command to complete */
1602                                                 wait_for_ctrl_irq (ctrl);
1603
1604                                                 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1605                                                 /* Wait for the command to complete */
1606                                                 wait_for_ctrl_irq (ctrl);
1607
1608                                                 /* Done with exclusive hardware access */
1609                                                 up(&ctrl->crit_sect);
1610
1611                                                 break;
1612                                         default:
1613                                                 warn("Not a valid state\n");
1614                                                 return;
1615                                         }
1616                                         info(msg_button_cancel, p_slot->number);
1617                                         p_slot->state = STATIC_STATE;
1618                                 }
1619                                 /* ***********Button Pressed (No action on 1st press...) */
1620                                 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1621                                         dbg("Button pressed\n");
1622
1623                                         p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1624                                         if (getstatus) {
1625                                                 /* slot is on */
1626                                                 dbg("slot is on\n");
1627                                                 p_slot->state = BLINKINGOFF_STATE;
1628                                                 info(msg_button_off, p_slot->number);
1629                                         } else {
1630                                                 /* slot is off */
1631                                                 dbg("slot is off\n");
1632                                                 p_slot->state = BLINKINGON_STATE;
1633                                                 info(msg_button_on, p_slot->number);
1634                                         }
1635
1636                                         /* Wait for exclusive access to hardware */
1637                                         down(&ctrl->crit_sect);
1638
1639                                         /* blink green LED and turn off amber */
1640                                         p_slot->hpc_ops->green_led_blink(p_slot);
1641                                         /* Wait for the command to complete */
1642                                         wait_for_ctrl_irq (ctrl);
1643                                         
1644                                         p_slot->hpc_ops->set_attention_status(p_slot, 0);
1645
1646                                         /* Wait for the command to complete */
1647                                         wait_for_ctrl_irq (ctrl);
1648
1649                                         /* Done with exclusive hardware access */
1650                                         up(&ctrl->crit_sect);
1651
1652                                         init_timer(&p_slot->task_event);
1653                                         p_slot->task_event.expires = jiffies + 5 * HZ;   /* 5 second delay */
1654                                         p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1655                                         p_slot->task_event.data = (unsigned long) p_slot;
1656
1657                                         dbg("add_timer p_slot = %p\n", (void *) p_slot);
1658                                         add_timer(&p_slot->task_event);
1659                                 }
1660                                 /***********POWER FAULT********************/
1661                                 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1662                                         dbg("power fault\n");
1663                                         /* Wait for exclusive access to hardware */
1664                                         down(&ctrl->crit_sect);
1665
1666                                         p_slot->hpc_ops->set_attention_status(p_slot, 1);
1667                                         p_slot->hpc_ops->green_led_off(p_slot);
1668
1669                                         /* Done with exclusive hardware access */
1670                                         up(&ctrl->crit_sect);
1671                                 } else {
1672                                         /* refresh notification */
1673                                         if (p_slot)
1674                                                 update_slot_info(p_slot);
1675                                 }
1676
1677                                 ctrl->event_queue[loop].event_type = 0;
1678
1679                                 change = 1;
1680                         }
1681                 }               /* End of FOR loop */
1682         }
1683 }
1684
1685
1686 int pciehp_enable_slot(struct slot *p_slot)
1687 {
1688         u8 getstatus = 0;
1689         int rc;
1690         struct pci_func *func;
1691
1692         func = pciehp_slot_find(p_slot->bus, p_slot->device, 0);
1693         if (!func) {
1694                 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1695                 return 1;
1696         }
1697
1698         /* Check to see if (latch closed, card present, power off) */
1699         down(&p_slot->ctrl->crit_sect);
1700         rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1701         if (rc || !getstatus) {
1702                 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1703                 up(&p_slot->ctrl->crit_sect);
1704                 return 0;
1705         }
1706         
1707         rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1708         if (rc || getstatus) {
1709                 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1710                 up(&p_slot->ctrl->crit_sect);
1711                 return 0;
1712         }
1713         
1714         rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1715         if (rc || getstatus) {
1716                 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
1717                 up(&p_slot->ctrl->crit_sect);
1718                 return 0;
1719         }
1720         up(&p_slot->ctrl->crit_sect);
1721
1722         slot_remove(func);
1723
1724         func = pciehp_slot_create(p_slot->bus);
1725         if (func == NULL)
1726                 return 1;
1727
1728         func->bus = p_slot->bus;
1729         func->device = p_slot->device;
1730         func->function = 0;
1731         func->configured = 0;
1732         func->is_a_board = 1;
1733
1734         /* We have to save the presence info for these slots */
1735         p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1736         p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1737         func->switch_save = !getstatus? 0x10:0;
1738
1739         rc = board_added(func, p_slot->ctrl);
1740         if (rc) {
1741                 if (is_bridge(func))
1742                         bridge_slot_remove(func);
1743                 else
1744                         slot_remove(func);
1745
1746                 /* Setup slot structure with entry for empty slot */
1747                 func = pciehp_slot_create(p_slot->bus);
1748                 if (func == NULL)
1749                         return 1;       /* Out of memory */
1750
1751                 func->bus = p_slot->bus;
1752                 func->device = p_slot->device;
1753                 func->function = 0;
1754                 func->configured = 0;
1755                 func->is_a_board = 1;
1756
1757                 /* We have to save the presence info for these slots */
1758                 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1759                 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1760                 func->switch_save = !getstatus? 0x10:0;
1761         }
1762
1763         if (p_slot)
1764                 update_slot_info(p_slot);
1765
1766         return rc;
1767 }
1768
1769
1770 int pciehp_disable_slot(struct slot *p_slot)
1771 {
1772         u8 class_code, header_type, BCR;
1773         u8 index = 0;
1774         u8 getstatus = 0;
1775         u32 rc = 0;
1776         int ret = 0;
1777         unsigned int devfn;
1778         struct pci_bus *pci_bus = p_slot->ctrl->pci_dev->subordinate;
1779         struct pci_func *func;
1780
1781         if (!p_slot->ctrl)
1782                 return 1;
1783
1784         /* Check to see if (latch closed, card present, power on) */
1785         down(&p_slot->ctrl->crit_sect);
1786
1787         ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1788         if (ret || !getstatus) {
1789                 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1790                 up(&p_slot->ctrl->crit_sect);
1791                 return 0;
1792         }
1793
1794         ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1795         if (ret || getstatus) {
1796                 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1797                 up(&p_slot->ctrl->crit_sect);
1798                 return 0;
1799         }
1800
1801         ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1802         if (ret || !getstatus) {
1803                 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
1804                 up(&p_slot->ctrl->crit_sect);
1805                 return 0;
1806         }
1807         up(&p_slot->ctrl->crit_sect);
1808
1809         func = pciehp_slot_find(p_slot->bus, p_slot->device, index++);
1810
1811         /* Make sure there are no video controllers here
1812          * for all func of p_slot
1813          */
1814         while (func && !rc) {
1815                 pci_bus->number = func->bus;
1816                 devfn = PCI_DEVFN(func->device, func->function);
1817
1818                 /* Check the Class Code */
1819                 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
1820                 if (rc)
1821                         return rc;
1822
1823                 if (class_code == PCI_BASE_CLASS_DISPLAY) {
1824                         /* Display/Video adapter (not supported) */
1825                         rc = REMOVE_NOT_SUPPORTED;
1826                 } else {
1827                         /* See if it's a bridge */
1828                         rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1829                         if (rc)
1830                                 return rc;
1831
1832                         /* If it's a bridge, check the VGA Enable bit */
1833                         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1834                                 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
1835                                 if (rc)
1836                                         return rc;
1837
1838                                 /* If the VGA Enable bit is set, remove isn't supported */
1839                                 if (BCR & PCI_BRIDGE_CTL_VGA) {
1840                                         rc = REMOVE_NOT_SUPPORTED;
1841                                 }
1842                         }
1843                 }
1844
1845                 func = pciehp_slot_find(p_slot->bus, p_slot->device, index++);
1846         }
1847
1848         func = pciehp_slot_find(p_slot->bus, p_slot->device, 0);
1849         if ((func != NULL) && !rc) {
1850                 rc = remove_board(func, p_slot->ctrl);
1851         } else if (!rc)
1852                 rc = 1;
1853
1854         if (p_slot)
1855                 update_slot_info(p_slot);
1856
1857         return rc;
1858 }
1859
1860
1861 /**
1862  * configure_new_device - Configures the PCI header information of one board.
1863  *
1864  * @ctrl: pointer to controller structure
1865  * @func: pointer to function structure
1866  * @behind_bridge: 1 if this is a recursive call, 0 if not
1867  * @resources: pointer to set of resource lists
1868  *
1869  * Returns 0 if success
1870  *
1871  */
1872 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
1873         u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
1874 {
1875         u8 temp_byte, function, max_functions, stop_it;
1876         int rc;
1877         u32 ID;
1878         struct pci_func *new_slot;
1879         struct pci_bus lpci_bus, *pci_bus;
1880         int index;
1881
1882         new_slot = func;
1883
1884         dbg("%s\n", __FUNCTION__);
1885         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
1886         pci_bus = &lpci_bus;
1887         pci_bus->number = func->bus;
1888
1889         /* Check for Multi-function device */
1890         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
1891         if (rc) {
1892                 dbg("%s: rc = %d\n", __FUNCTION__, rc);
1893                 return rc;
1894         }
1895
1896         if (temp_byte & 0x80)   /* Multi-function device */
1897                 max_functions = 8;
1898         else
1899                 max_functions = 1;
1900
1901         function = 0;
1902
1903         do {
1904                 rc = configure_new_function(ctrl, new_slot, behind_bridge,
1905                                         resources, bridge_bus, bridge_dev);
1906
1907                 if (rc) {
1908                         dbg("configure_new_function failed: %d\n", rc);
1909                         index = 0;
1910
1911                         while (new_slot) {
1912                                 new_slot = pciehp_slot_find(new_slot->bus,
1913                                                 new_slot->device, index++);
1914
1915                                 if (new_slot)
1916                                         pciehp_return_board_resources(new_slot,
1917                                                 resources);
1918                         }
1919
1920                         return rc;
1921                 }
1922
1923                 function++;
1924
1925                 stop_it = 0;
1926
1927                 /*  The following loop skips to the next present function
1928                  *  and creates a board structure
1929                  */
1930
1931                 while ((function < max_functions) && (!stop_it)) {
1932                         pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
1933
1934                         if (ID == 0xFFFFFFFF) {   /* There's nothing there. */
1935                                 function++;
1936                         } else {  /* There's something there */
1937                                 /* Setup slot structure. */
1938                                 new_slot = pciehp_slot_create(func->bus);
1939
1940                                 if (new_slot == NULL) {
1941                                         /* Out of memory */
1942                                         return 1;
1943                                 }
1944
1945                                 new_slot->bus = func->bus;
1946                                 new_slot->device = func->device;
1947                                 new_slot->function = function;
1948                                 new_slot->is_a_board = 1;
1949                                 new_slot->status = 0;
1950
1951                                 stop_it++;
1952                         }
1953                 }
1954
1955         } while (function < max_functions);
1956         dbg("returning from %s\n", __FUNCTION__);
1957
1958         return 0;
1959 }
1960
1961 /*
1962  * Configuration logic that involves the hotplug data structures and 
1963  * their bookkeeping
1964  */
1965
1966 /**
1967  * configure_bridge: fill bridge's registers, either configure or disable it.
1968  */
1969 static int
1970 configure_bridge(struct pci_bus *pci_bus, unsigned int devfn,
1971                         struct pci_resource *mem_node,
1972                         struct pci_resource **hold_mem_node,
1973                         int base_addr, int limit_addr)
1974 {
1975         u16 temp_word;
1976         u32 rc;
1977
1978         if (mem_node) {
1979                 memcpy(*hold_mem_node, mem_node, sizeof(struct pci_resource));
1980                 mem_node->next = NULL;
1981
1982                 /* set Mem base and Limit registers */
1983                 RES_CHECK(mem_node->base, 16);
1984                 temp_word = (u16)(mem_node->base >> 16);
1985                 rc = pci_bus_write_config_word(pci_bus, devfn, base_addr, temp_word);
1986
1987                 RES_CHECK(mem_node->base + mem_node->length - 1, 16);
1988                 temp_word = (u16)((mem_node->base + mem_node->length - 1) >> 16);
1989                 rc = pci_bus_write_config_word(pci_bus, devfn, limit_addr, temp_word);
1990         } else {
1991                 temp_word = 0xFFFF;
1992                 rc = pci_bus_write_config_word(pci_bus, devfn, base_addr, temp_word);
1993
1994                 temp_word = 0x0000;
1995                 rc = pci_bus_write_config_word(pci_bus, devfn, limit_addr, temp_word);
1996
1997                 kfree(*hold_mem_node);
1998                 *hold_mem_node = NULL;
1999         }
2000         return rc;
2001 }
2002
2003 static int
2004 configure_new_bridge(struct controller *ctrl, struct pci_func *func,
2005                 u8 behind_bridge, struct resource_lists *resources,
2006                 struct pci_bus *pci_bus)
2007 {
2008         int cloop;
2009         u8 temp_byte;
2010         u8 device;
2011         u16 temp_word;
2012         u32 rc;
2013         u32 ID;
2014         unsigned int devfn;
2015         struct pci_resource *mem_node;
2016         struct pci_resource *p_mem_node;
2017         struct pci_resource *io_node;
2018         struct pci_resource *bus_node;
2019         struct pci_resource *hold_mem_node;
2020         struct pci_resource *hold_p_mem_node;
2021         struct pci_resource *hold_IO_node;
2022         struct pci_resource *hold_bus_node;
2023         struct irq_mapping irqs;
2024         struct pci_func *new_slot;
2025         struct resource_lists temp_resources;
2026
2027         devfn = PCI_DEVFN(func->device, func->function);
2028
2029         /* set Primary bus */
2030         dbg("set Primary bus = 0x%x\n", func->bus);
2031         rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2032         if (rc)
2033                 return rc;
2034
2035         /* find range of busses to use */
2036         bus_node = get_max_resource(&resources->bus_head, 1L);
2037
2038         /* If we don't have any busses to allocate, we can't continue */
2039         if (!bus_node) {
2040                 err("Got NO bus resource to use\n");
2041                 return -ENOMEM;
2042         }
2043         dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
2044
2045         /* set Secondary bus */
2046         temp_byte = (u8)bus_node->base;
2047         dbg("set Secondary bus = 0x%x\n", temp_byte);
2048         rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2049         if (rc)
2050                 return rc;
2051
2052         /* set subordinate bus */
2053         temp_byte = (u8)(bus_node->base + bus_node->length - 1);
2054         dbg("set subordinate bus = 0x%x\n", temp_byte);
2055         rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2056         if (rc)
2057                 return rc;
2058
2059         /* Set HP parameters (Cache Line Size, Latency Timer) */
2060         rc = pciehprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2061         if (rc)
2062                 return rc;
2063
2064         /* Setup the IO, memory, and prefetchable windows */
2065
2066         io_node = get_max_resource(&(resources->io_head), 0x1000L);
2067         if (io_node) {
2068                 dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base,
2069                                 io_node->length, io_node->next);
2070         }
2071
2072         mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2073         if (mem_node) {
2074                 dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base,
2075                                 mem_node->length, mem_node->next);
2076         }
2077
2078         if (resources->p_mem_head)
2079                 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
2080         else {
2081                 /*
2082                  * In some platform implementation, MEM and PMEM are not
2083                  *  distinguished, and hence ACPI _CRS has only MEM entries
2084                  *  for both MEM and PMEM.
2085                  */
2086                 dbg("using MEM for PMEM\n");
2087                 p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
2088         }
2089         if (p_mem_node) {
2090                 dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2091                                 p_mem_node->length, p_mem_node->next);
2092         }
2093
2094         /* set up the IRQ info */
2095         if (!resources->irqs) {
2096                 irqs.barber_pole = 0;
2097                 irqs.interrupt[0] = 0;
2098                 irqs.interrupt[1] = 0;
2099                 irqs.interrupt[2] = 0;
2100                 irqs.interrupt[3] = 0;
2101                 irqs.valid_INT = 0;
2102         } else {
2103                 irqs.barber_pole = resources->irqs->barber_pole;
2104                 irqs.interrupt[0] = resources->irqs->interrupt[0];
2105                 irqs.interrupt[1] = resources->irqs->interrupt[1];
2106                 irqs.interrupt[2] = resources->irqs->interrupt[2];
2107                 irqs.interrupt[3] = resources->irqs->interrupt[3];
2108                 irqs.valid_INT = resources->irqs->valid_INT;
2109         }
2110
2111         /* set up resource lists that are now aligned on top and bottom
2112          * for anything behind the bridge.
2113          */
2114         temp_resources.bus_head = bus_node;
2115         temp_resources.io_head = io_node;
2116         temp_resources.mem_head = mem_node;
2117         temp_resources.p_mem_head = p_mem_node;
2118         temp_resources.irqs = &irqs;
2119
2120         /* Make copies of the nodes we are going to pass down so that
2121          * if there is a problem,we can just use these to free resources
2122          */
2123         hold_bus_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2124         hold_IO_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2125         hold_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2126         hold_p_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
2127
2128         if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2129                 kfree(hold_bus_node);
2130                 kfree(hold_IO_node);
2131                 kfree(hold_mem_node);
2132                 kfree(hold_p_mem_node);
2133
2134                 return 1;
2135         }
2136
2137         memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2138
2139         bus_node->base += 1;
2140         bus_node->length -= 1;
2141         bus_node->next = NULL;
2142
2143         /* If we have IO resources copy them and fill in the bridge's
2144          * IO range registers
2145          */
2146         if (io_node) {
2147                 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2148                 io_node->next = NULL;
2149
2150                 /* set IO base and Limit registers */
2151                 RES_CHECK(io_node->base, 8);
2152                 temp_byte = (u8)(io_node->base >> 8);
2153                 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2154
2155                 RES_CHECK(io_node->base + io_node->length - 1, 8);
2156                 temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
2157                 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2158         } else {
2159                 kfree(hold_IO_node);
2160                 hold_IO_node = NULL;
2161         }
2162
2163         /* If we have memory resources copy them and fill in the bridge's
2164          * memory range registers.  Otherwise, fill in the range
2165          * registers with values that disable them.
2166          */
2167         rc = configure_bridge(pci_bus, devfn, mem_node, &hold_mem_node,
2168                                 PCI_MEMORY_BASE, PCI_MEMORY_LIMIT);
2169
2170         /* If we have prefetchable memory resources copy them and 
2171          * fill in the bridge's memory range registers.  Otherwise,
2172          * fill in the range registers with values that disable them.
2173          */
2174         rc = configure_bridge(pci_bus, devfn, p_mem_node, &hold_p_mem_node,
2175                                 PCI_PREF_MEMORY_BASE, PCI_PREF_MEMORY_LIMIT);
2176
2177         /* Adjust this to compensate for extra adjustment in first loop */
2178         irqs.barber_pole--;
2179
2180         rc = 0;
2181
2182         /* Here we actually find the devices and configure them */
2183         for (device = 0; (device <= 0x1F) && !rc; device++) {
2184                 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2185
2186                 ID = 0xFFFFFFFF;
2187                 pci_bus->number = hold_bus_node->base;
2188                 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
2189                 pci_bus->number = func->bus;
2190
2191                 if (ID != 0xFFFFFFFF) {   /*  device Present */
2192                         /* Setup slot structure. */
2193                         new_slot = pciehp_slot_create(hold_bus_node->base);
2194
2195                         if (new_slot == NULL) {
2196                                 /* Out of memory */
2197                                 rc = -ENOMEM;
2198                                 continue;
2199                         }
2200
2201                         new_slot->bus = hold_bus_node->base;
2202                         new_slot->device = device;
2203                         new_slot->function = 0;
2204                         new_slot->is_a_board = 1;
2205                         new_slot->status = 0;
2206
2207                         rc = configure_new_device(ctrl, new_slot, 1,
2208                                         &temp_resources, func->bus,
2209                                         func->device);
2210                         dbg("configure_new_device rc=0x%x\n",rc);
2211                 }       /* End of IF (device in slot?) */
2212         }               /* End of FOR loop */
2213
2214         if (rc) {
2215                 pciehp_destroy_resource_list(&temp_resources);
2216
2217                 return_resource(&(resources->bus_head), hold_bus_node);
2218                 return_resource(&(resources->io_head), hold_IO_node);
2219                 return_resource(&(resources->mem_head), hold_mem_node);
2220                 return_resource(&(resources->p_mem_head), hold_p_mem_node);
2221                 return(rc);
2222         }
2223
2224         /* save the interrupt routing information */
2225         if (resources->irqs) {
2226                 resources->irqs->interrupt[0] = irqs.interrupt[0];
2227                 resources->irqs->interrupt[1] = irqs.interrupt[1];
2228                 resources->irqs->interrupt[2] = irqs.interrupt[2];
2229                 resources->irqs->interrupt[3] = irqs.interrupt[3];
2230                 resources->irqs->valid_INT = irqs.valid_INT;
2231         } else if (!behind_bridge) {
2232                 /* We need to hook up the interrupts here */
2233                 for (cloop = 0; cloop < 4; cloop++) {
2234                         if (irqs.valid_INT & (0x01 << cloop)) {
2235                                 rc = pciehp_set_irq(func->bus, func->device,
2236                                                         0x0A + cloop, irqs.interrupt[cloop]);
2237                                 if (rc) {
2238                                         pciehp_destroy_resource_list (&temp_resources);
2239                                         return_resource(&(resources->bus_head), hold_bus_node);
2240                                         return_resource(&(resources->io_head), hold_IO_node);
2241                                         return_resource(&(resources->mem_head), hold_mem_node);
2242                                         return_resource(&(resources->p_mem_head), hold_p_mem_node);
2243                                         return rc;
2244                                 }
2245                         }
2246                 }       /* end of for loop */
2247         }
2248
2249         /* Return unused bus resources
2250          * First use the temporary node to store information for the board
2251          */
2252         if (hold_bus_node && bus_node && temp_resources.bus_head) {
2253                 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2254
2255                 hold_bus_node->next = func->bus_head;
2256                 func->bus_head = hold_bus_node;
2257
2258                 temp_byte = (u8)(temp_resources.bus_head->base - 1);
2259
2260                 /* set subordinate bus */
2261                 dbg("re-set subordinate bus = 0x%x\n", temp_byte);
2262                 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2263
2264                 if (temp_resources.bus_head->length == 0) {
2265                         kfree(temp_resources.bus_head);
2266                         temp_resources.bus_head = NULL;
2267                 } else {
2268                         dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
2269                                 func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
2270                         return_resource(&(resources->bus_head), temp_resources.bus_head);
2271                 }
2272         }
2273
2274         /* If we have IO space available and there is some left,
2275          * return the unused portion
2276          */
2277         if (hold_IO_node && temp_resources.io_head) {
2278                 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2279                                                         &hold_IO_node, 0x1000);
2280
2281                 /* Check if we were able to split something off */
2282                 if (io_node) {
2283                         hold_IO_node->base = io_node->base + io_node->length;
2284
2285                         RES_CHECK(hold_IO_node->base, 8);
2286                         temp_byte = (u8)((hold_IO_node->base) >> 8);
2287                         rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2288
2289                         return_resource(&(resources->io_head), io_node);
2290                 }
2291
2292                 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2293
2294                 /*  Check if we were able to split something off */
2295                 if (io_node) {
2296                         /* First use the temporary node to store information for the board */
2297                         hold_IO_node->length = io_node->base - hold_IO_node->base;
2298
2299                         /* If we used any, add it to the board's list */
2300                         if (hold_IO_node->length) {
2301                                 hold_IO_node->next = func->io_head;
2302                                 func->io_head = hold_IO_node;
2303
2304                                 RES_CHECK(io_node->base - 1, 8);
2305                                 temp_byte = (u8)((io_node->base - 1) >> 8);
2306                                 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2307
2308                                 return_resource(&(resources->io_head), io_node);
2309                         } else {
2310                                 /* it doesn't need any IO */
2311                                 temp_byte = 0x00;
2312                                 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2313
2314                                 return_resource(&(resources->io_head), io_node);
2315                                 kfree(hold_IO_node);
2316                         }
2317                 } else {
2318                         /* it used most of the range */
2319                         hold_IO_node->next = func->io_head;
2320                         func->io_head = hold_IO_node;
2321                 }
2322         } else if (hold_IO_node) {
2323                 /* it used the whole range */
2324                 hold_IO_node->next = func->io_head;
2325                 func->io_head = hold_IO_node;
2326         }
2327
2328         /* If we have memory space available and there is some left,
2329          * return the unused portion
2330          */
2331         if (hold_mem_node && temp_resources.mem_head) {
2332                 mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
2333
2334                 /* Check if we were able to split something off */
2335                 if (mem_node) {
2336                         hold_mem_node->base = mem_node->base + mem_node->length;
2337
2338                         RES_CHECK(hold_mem_node->base, 16);
2339                         temp_word = (u16)((hold_mem_node->base) >> 16);
2340                         rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2341
2342                         return_resource(&(resources->mem_head), mem_node);
2343                 }
2344
2345                 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
2346
2347                 /* Check if we were able to split something off */
2348                 if (mem_node) {
2349                         /* First use the temporary node to store information for the board */
2350                         hold_mem_node->length = mem_node->base - hold_mem_node->base;
2351
2352                         if (hold_mem_node->length) {
2353                                 hold_mem_node->next = func->mem_head;
2354                                 func->mem_head = hold_mem_node;
2355
2356                                 /* configure end address */
2357                                 RES_CHECK(mem_node->base - 1, 16);
2358                                 temp_word = (u16)((mem_node->base - 1) >> 16);
2359                                 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2360
2361                                 /* Return unused resources to the pool */
2362                                 return_resource(&(resources->mem_head), mem_node);
2363                         } else {
2364                                 /* it doesn't need any Mem */
2365                                 temp_word = 0x0000;
2366                                 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2367
2368                                 return_resource(&(resources->mem_head), mem_node);
2369                                 kfree(hold_mem_node);
2370                         }
2371                 } else {
2372                         /* it used most of the range */
2373                         hold_mem_node->next = func->mem_head;
2374                         func->mem_head = hold_mem_node;
2375                 }
2376         } else if (hold_mem_node) {
2377                 /* it used the whole range */
2378                 hold_mem_node->next = func->mem_head;
2379                 func->mem_head = hold_mem_node;
2380         }
2381
2382         /* If we have prefetchable memory space available and there is some 
2383          * left at the end, return the unused portion
2384          */
2385         if (hold_p_mem_node && temp_resources.p_mem_head) {
2386                 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2387                                                                 &hold_p_mem_node, 0x100000L);
2388
2389                 /* Check if we were able to split something off */
2390                 if (p_mem_node) {
2391                         hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2392
2393                         RES_CHECK(hold_p_mem_node->base, 16);
2394                         temp_word = (u16)((hold_p_mem_node->base) >> 16);
2395                         rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2396
2397                         return_resource(&(resources->p_mem_head), p_mem_node);
2398                 }
2399
2400                 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
2401
2402                 /* Check if we were able to split something off */
2403                 if (p_mem_node) {
2404                         /* First use the temporary node to store information for the board */
2405                         hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2406
2407                         /* If we used any, add it to the board's list */
2408                         if (hold_p_mem_node->length) {
2409                                 hold_p_mem_node->next = func->p_mem_head;
2410                                 func->p_mem_head = hold_p_mem_node;
2411
2412                                 RES_CHECK(p_mem_node->base - 1, 16);
2413                                 temp_word = (u16)((p_mem_node->base - 1) >> 16);
2414                                 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2415
2416                                 return_resource(&(resources->p_mem_head), p_mem_node);
2417                         } else {
2418                                 /* it doesn't need any PMem */
2419                                 temp_word = 0x0000;
2420                                 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2421
2422                                 return_resource(&(resources->p_mem_head), p_mem_node);
2423                                 kfree(hold_p_mem_node);
2424                         }
2425                 } else {
2426                         /* it used the most of the range */
2427                         hold_p_mem_node->next = func->p_mem_head;
2428                         func->p_mem_head = hold_p_mem_node;
2429                 }
2430         } else if (hold_p_mem_node) {
2431                 /* it used the whole range */
2432                 hold_p_mem_node->next = func->p_mem_head;
2433                 func->p_mem_head = hold_p_mem_node;
2434         }
2435
2436         /* We should be configuring an IRQ and the bridge's base address
2437          * registers if it needs them.  Although we have never seen such
2438          * a device
2439          */
2440
2441         pciehprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
2442
2443         dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
2444
2445         return rc;
2446 }
2447
2448 /**
2449  * configure_new_function - Configures the PCI header information of one device
2450  *
2451  * @ctrl: pointer to controller structure
2452  * @func: pointer to function structure
2453  * @behind_bridge: 1 if this is a recursive call, 0 if not
2454  * @resources: pointer to set of resource lists
2455  *
2456  * Calls itself recursively for bridged devices.
2457  * Returns 0 if success
2458  *
2459  */
2460 static int
2461 configure_new_function(struct controller *ctrl, struct pci_func *func,
2462                 u8 behind_bridge, struct resource_lists *resources,
2463                 u8 bridge_bus, u8 bridge_dev)
2464 {
2465         int cloop;
2466         u8 temp_byte;
2467         u8 class_code;
2468         u16 temp_word;
2469         u32 rc;
2470         u32 temp_register;
2471         u32 base;
2472         unsigned int devfn;
2473         struct pci_resource *mem_node;
2474         struct pci_resource *io_node;
2475         struct pci_bus lpci_bus, *pci_bus;
2476
2477         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
2478         pci_bus = &lpci_bus;
2479         pci_bus->number = func->bus;
2480         devfn = PCI_DEVFN(func->device, func->function);
2481
2482         /* Check for Bridge */
2483         rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2484         if (rc)
2485                 return rc;
2486         dbg("%s: bus %x dev %x func %x temp_byte = %x\n", __FUNCTION__,
2487                 func->bus, func->device, func->function, temp_byte);
2488
2489         if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2490                 rc = configure_new_bridge(ctrl, func, behind_bridge, resources,
2491                                                 pci_bus);
2492
2493                 if (rc)
2494                         return rc;
2495         } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2496                 /* Standard device */
2497                 u64     base64;
2498                 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
2499
2500                 if (class_code == PCI_BASE_CLASS_DISPLAY)
2501                         return DEVICE_TYPE_NOT_SUPPORTED;
2502
2503                 /* Figure out IO and memory needs */
2504                 for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
2505                         temp_register = 0xFFFFFFFF;
2506
2507                         rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2508                         rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
2509                         dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, 
2510                                 func->bus, func->device, func->function);
2511
2512                         if (!temp_register)
2513                                 continue;
2514
2515                         base64 = 0L;
2516                         if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
2517                                 /* Map IO */
2518
2519                                 /* set base = amount of IO space */
2520                                 base = temp_register & 0xFFFFFFFC;
2521                                 base = ~base + 1;
2522
2523                                 dbg("NEED IO length(0x%x)\n", base);
2524                                 io_node = get_io_resource(&(resources->io_head),(ulong)base);
2525
2526                                 /* allocate the resource to the board */
2527                                 if (io_node) {
2528                                         dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
2529                                         base = (u32)io_node->base;
2530                                         io_node->next = func->io_head;
2531                                         func->io_head = io_node;
2532                                 } else {
2533                                         err("Got NO IO resource(length=0x%x)\n", base);
2534                                         return -ENOMEM;
2535                                 }
2536                         } else {        /* map MEM */
2537                                 int prefetchable = 1;
2538                                 struct pci_resource **res_node = &func->p_mem_head;
2539                                 char *res_type_str = "PMEM";
2540                                 u32     temp_register2;
2541
2542                                 if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
2543                                         prefetchable = 0;
2544                                         res_node = &func->mem_head;
2545                                         res_type_str++;
2546                                 }
2547
2548                                 base = temp_register & 0xFFFFFFF0;
2549                                 base = ~base + 1;
2550
2551                                 switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
2552                                 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2553                                         dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
2554
2555                                         if (prefetchable && resources->p_mem_head)
2556                                                 mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
2557                                         else {
2558                                                 if (prefetchable)
2559                                                         dbg("using MEM for PMEM\n");
2560                                                 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2561                                         }
2562
2563                                         /* allocate the resource to the board */
2564                                         if (mem_node) {
2565                                                 base = (u32)mem_node->base; 
2566                                                 mem_node->next = *res_node;
2567                                                 *res_node = mem_node;
2568                                                 dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base, 
2569                                                         mem_node->length);
2570                                         } else {
2571                                                 err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
2572                                                 return -ENOMEM;
2573                                         }
2574                                         break;
2575                                 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2576                                         rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
2577                                         dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2, 
2578                                                 temp_register, base);
2579
2580                                         if (prefetchable && resources->p_mem_head)
2581                                                 mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
2582                                         else {
2583                                                 if (prefetchable)
2584                                                         dbg("using MEM for PMEM\n");
2585                                                 mem_node = get_resource(&(resources->mem_head), (ulong)base);
2586                                         }
2587
2588                                         /* allocate the resource to the board */
2589                                         if (mem_node) {
2590                                                 base64 = mem_node->base; 
2591                                                 mem_node->next = *res_node;
2592                                                 *res_node = mem_node;
2593                                                 dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32), 
2594                                                         (u32)base64, mem_node->length);
2595                                         } else {
2596                                                 err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
2597                                                 return -ENOMEM;
2598                                         }
2599                                         break;
2600                                 default:
2601                                         dbg("reserved BAR type=0x%x\n", temp_register);
2602                                         break;
2603                                 }
2604
2605                         }
2606
2607                         if (base64) {
2608                                 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2609                                 cloop += 4;
2610                                 base64 >>= 32;
2611
2612                                 if (base64) {
2613                                         dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
2614                                         base64 = 0x0L;
2615                                 }
2616
2617                                 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
2618                         } else {
2619                                 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2620                         }
2621                 }               /* End of base register loop */
2622
2623                 /* disable ROM base Address */
2624                 temp_word = 0x00L;
2625                 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
2626
2627                 /* Set HP parameters (Cache Line Size, Latency Timer) */
2628                 rc = pciehprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2629                 if (rc)
2630                         return rc;
2631
2632                 pciehprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
2633
2634                 dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, 
2635                         func->function);
2636         }  /* End of Not-A-Bridge else */
2637         else {
2638                 /* It's some strange type of PCI adapter (Cardbus?) */
2639                 return DEVICE_TYPE_NOT_SUPPORTED;
2640         }
2641
2642         func->configured = 1;
2643
2644         return 0;
2645 }