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