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