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