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