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