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