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