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