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