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