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