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