RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / pci / hotplug / cpqphp_ctrl.c
blob12f0fbe3c8aaecb153ee717559c47c4f6c66c842
1 /*
2 * Compaq Hot Plug Controller Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
8 * All rights reserved.
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.
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.
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.
25 * Send feedback to <greg@kroah.com>
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"
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);
49 static struct task_struct *cpqhp_event_thread;
50 static unsigned long pushbutton_pending; /* = 0 */
52 /* delay is in jiffies to wait for */
53 static void long_delay(int delay)
55 msleep_interruptible(jiffies_to_msecs(delay));
59 #define WRONG_BUS_FREQUENCY 0x07
60 static u8 handle_switch_change(u8 change, struct controller * ctrl)
62 int hp_slot;
63 u8 rc = 0;
64 u16 temp_word;
65 struct pci_func *func;
66 struct event_info *taskInfo;
68 if (!change)
69 return 0;
71 /* Switch Change */
72 dbg("cpqsbd: Switch interrupt received.\n");
74 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
75 if (change & (0x1L << hp_slot)) {
77 * this one changed.
79 func = cpqhp_slot_find(ctrl->bus,
80 (hp_slot + ctrl->slot_device_offset), 0);
82 /* this is the structure that tells the worker thread
83 * what to do
85 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
86 ctrl->next_event = (ctrl->next_event + 1) % 10;
87 taskInfo->hp_slot = hp_slot;
89 rc++;
91 temp_word = ctrl->ctrl_int_comp >> 16;
92 func->presence_save = (temp_word >> hp_slot) & 0x01;
93 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
95 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
97 * Switch opened
100 func->switch_save = 0;
102 taskInfo->event_type = INT_SWITCH_OPEN;
103 } else {
105 * Switch closed
108 func->switch_save = 0x10;
110 taskInfo->event_type = INT_SWITCH_CLOSE;
115 return rc;
119 * cpqhp_find_slot - find the struct slot of given device
120 * @ctrl: scan lots of this controller
121 * @device: the device id to find
123 static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
125 struct slot *slot = ctrl->slot;
127 while (slot && (slot->device != device))
128 slot = slot->next;
130 return slot;
134 static u8 handle_presence_change(u16 change, struct controller * ctrl)
136 int hp_slot;
137 u8 rc = 0;
138 u8 temp_byte;
139 u16 temp_word;
140 struct pci_func *func;
141 struct event_info *taskInfo;
142 struct slot *p_slot;
144 if (!change)
145 return 0;
148 * Presence Change
150 dbg("cpqsbd: Presence/Notify input change.\n");
151 dbg(" Changed bits are 0x%4.4x\n", change );
153 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
154 if (change & (0x0101 << hp_slot)) {
156 * this one changed.
158 func = cpqhp_slot_find(ctrl->bus,
159 (hp_slot + ctrl->slot_device_offset), 0);
161 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
162 ctrl->next_event = (ctrl->next_event + 1) % 10;
163 taskInfo->hp_slot = hp_slot;
165 rc++;
167 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
168 if (!p_slot)
169 return 0;
171 /* If the switch closed, must be a button
172 * If not in button mode, nevermind
174 if (func->switch_save && (ctrl->push_button == 1)) {
175 temp_word = ctrl->ctrl_int_comp >> 16;
176 temp_byte = (temp_word >> hp_slot) & 0x01;
177 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
179 if (temp_byte != func->presence_save) {
181 * button Pressed (doesn't do anything)
183 dbg("hp_slot %d button pressed\n", hp_slot);
184 taskInfo->event_type = INT_BUTTON_PRESS;
185 } else {
187 * button Released - TAKE ACTION!!!!
189 dbg("hp_slot %d button released\n", hp_slot);
190 taskInfo->event_type = INT_BUTTON_RELEASE;
192 /* Cancel if we are still blinking */
193 if ((p_slot->state == BLINKINGON_STATE)
194 || (p_slot->state == BLINKINGOFF_STATE)) {
195 taskInfo->event_type = INT_BUTTON_CANCEL;
196 dbg("hp_slot %d button cancel\n", hp_slot);
197 } else if ((p_slot->state == POWERON_STATE)
198 || (p_slot->state == POWEROFF_STATE)) {
199 /* info(msg_button_ignore, p_slot->number); */
200 taskInfo->event_type = INT_BUTTON_IGNORE;
201 dbg("hp_slot %d button ignore\n", hp_slot);
204 } else {
205 /* Switch is open, assume a presence change
206 * Save the presence state
208 temp_word = ctrl->ctrl_int_comp >> 16;
209 func->presence_save = (temp_word >> hp_slot) & 0x01;
210 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
212 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
213 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
214 /* Present */
215 taskInfo->event_type = INT_PRESENCE_ON;
216 } else {
217 /* Not Present */
218 taskInfo->event_type = INT_PRESENCE_OFF;
224 return rc;
228 static u8 handle_power_fault(u8 change, struct controller * ctrl)
230 int hp_slot;
231 u8 rc = 0;
232 struct pci_func *func;
233 struct event_info *taskInfo;
235 if (!change)
236 return 0;
239 * power fault
242 info("power fault interrupt\n");
244 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
245 if (change & (0x01 << hp_slot)) {
247 * this one changed.
249 func = cpqhp_slot_find(ctrl->bus,
250 (hp_slot + ctrl->slot_device_offset), 0);
252 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
253 ctrl->next_event = (ctrl->next_event + 1) % 10;
254 taskInfo->hp_slot = hp_slot;
256 rc++;
258 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
260 * power fault Cleared
262 func->status = 0x00;
264 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
265 } else {
267 * power fault
269 taskInfo->event_type = INT_POWER_FAULT;
271 if (ctrl->rev < 4) {
272 amber_LED_on (ctrl, hp_slot);
273 green_LED_off (ctrl, hp_slot);
274 set_SOGO (ctrl);
276 /* this is a fatal condition, we want
277 * to crash the machine to protect from
278 * data corruption. simulated_NMI
279 * shouldn't ever return */
281 /* The following code causes a software
282 * crash just in case simulated_NMI did
283 * return */
284 } else {
285 /* set power fault status for this board */
286 func->status = 0xFF;
287 info("power fault bit %x set\n", hp_slot);
293 return rc;
298 * sort_by_size - sort nodes on the list by their length, smallest first.
299 * @head: list to sort
301 static int sort_by_size(struct pci_resource **head)
303 struct pci_resource *current_res;
304 struct pci_resource *next_res;
305 int out_of_order = 1;
307 if (!(*head))
308 return 1;
310 if (!((*head)->next))
311 return 0;
313 while (out_of_order) {
314 out_of_order = 0;
316 /* Special case for swapping list head */
317 if (((*head)->next) &&
318 ((*head)->length > (*head)->next->length)) {
319 out_of_order++;
320 current_res = *head;
321 *head = (*head)->next;
322 current_res->next = (*head)->next;
323 (*head)->next = current_res;
326 current_res = *head;
328 while (current_res->next && current_res->next->next) {
329 if (current_res->next->length > current_res->next->next->length) {
330 out_of_order++;
331 next_res = current_res->next;
332 current_res->next = current_res->next->next;
333 current_res = current_res->next;
334 next_res->next = current_res->next;
335 current_res->next = next_res;
336 } else
337 current_res = current_res->next;
339 } /* End of out_of_order loop */
341 return 0;
346 * sort_by_max_size - sort nodes on the list by their length, largest first.
347 * @head: list to sort
349 static int sort_by_max_size(struct pci_resource **head)
351 struct pci_resource *current_res;
352 struct pci_resource *next_res;
353 int out_of_order = 1;
355 if (!(*head))
356 return 1;
358 if (!((*head)->next))
359 return 0;
361 while (out_of_order) {
362 out_of_order = 0;
364 /* Special case for swapping list head */
365 if (((*head)->next) &&
366 ((*head)->length < (*head)->next->length)) {
367 out_of_order++;
368 current_res = *head;
369 *head = (*head)->next;
370 current_res->next = (*head)->next;
371 (*head)->next = current_res;
374 current_res = *head;
376 while (current_res->next && current_res->next->next) {
377 if (current_res->next->length < current_res->next->next->length) {
378 out_of_order++;
379 next_res = current_res->next;
380 current_res->next = current_res->next->next;
381 current_res = current_res->next;
382 next_res->next = current_res->next;
383 current_res->next = next_res;
384 } else
385 current_res = current_res->next;
387 } /* End of out_of_order loop */
389 return 0;
394 * do_pre_bridge_resource_split - find node of resources that are unused
395 * @head: new list head
396 * @orig_head: original list head
397 * @alignment: max node size (?)
399 static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
400 struct pci_resource **orig_head, u32 alignment)
402 struct pci_resource *prevnode = NULL;
403 struct pci_resource *node;
404 struct pci_resource *split_node;
405 u32 rc;
406 u32 temp_dword;
407 dbg("do_pre_bridge_resource_split\n");
409 if (!(*head) || !(*orig_head))
410 return NULL;
412 rc = cpqhp_resource_sort_and_combine(head);
414 if (rc)
415 return NULL;
417 if ((*head)->base != (*orig_head)->base)
418 return NULL;
420 if ((*head)->length == (*orig_head)->length)
421 return NULL;
424 /* If we got here, there the bridge requires some of the resource, but
425 * we may be able to split some off of the front
428 node = *head;
430 if (node->length & (alignment -1)) {
431 /* this one isn't an aligned length, so we'll make a new entry
432 * and split it up.
434 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
436 if (!split_node)
437 return NULL;
439 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
441 split_node->base = node->base;
442 split_node->length = temp_dword;
444 node->length -= temp_dword;
445 node->base += split_node->length;
447 /* Put it in the list */
448 *head = split_node;
449 split_node->next = node;
452 if (node->length < alignment)
453 return NULL;
455 /* Now unlink it */
456 if (*head == node) {
457 *head = node->next;
458 } else {
459 prevnode = *head;
460 while (prevnode->next != node)
461 prevnode = prevnode->next;
463 prevnode->next = node->next;
465 node->next = NULL;
467 return node;
472 * do_bridge_resource_split - find one node of resources that aren't in use
473 * @head: list head
474 * @alignment: max node size (?)
476 static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
478 struct pci_resource *prevnode = NULL;
479 struct pci_resource *node;
480 u32 rc;
481 u32 temp_dword;
483 rc = cpqhp_resource_sort_and_combine(head);
485 if (rc)
486 return NULL;
488 node = *head;
490 while (node->next) {
491 prevnode = node;
492 node = node->next;
493 kfree(prevnode);
496 if (node->length < alignment)
497 goto error;
499 if (node->base & (alignment - 1)) {
500 /* Short circuit if adjusted size is too small */
501 temp_dword = (node->base | (alignment-1)) + 1;
502 if ((node->length - (temp_dword - node->base)) < alignment)
503 goto error;
505 node->length -= (temp_dword - node->base);
506 node->base = temp_dword;
509 if (node->length & (alignment - 1))
510 /* There's stuff in use after this node */
511 goto error;
513 return node;
514 error:
515 kfree(node);
516 return NULL;
521 * get_io_resource - find first node of given size not in ISA aliasing window.
522 * @head: list to search
523 * @size: size of node to find, must be a power of two.
525 * Description: This function sorts the resource list by size and then returns
526 * returns the first node of "size" length that is not in the ISA aliasing
527 * window. If it finds a node larger than "size" it will split it up.
529 static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
531 struct pci_resource *prevnode;
532 struct pci_resource *node;
533 struct pci_resource *split_node;
534 u32 temp_dword;
536 if (!(*head))
537 return NULL;
539 if (cpqhp_resource_sort_and_combine(head))
540 return NULL;
542 if (sort_by_size(head))
543 return NULL;
545 for (node = *head; node; node = node->next) {
546 if (node->length < size)
547 continue;
549 if (node->base & (size - 1)) {
550 /* this one isn't base aligned properly
551 * so we'll make a new entry and split it up
553 temp_dword = (node->base | (size-1)) + 1;
555 /* Short circuit if adjusted size is too small */
556 if ((node->length - (temp_dword - node->base)) < size)
557 continue;
559 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
561 if (!split_node)
562 return NULL;
564 split_node->base = node->base;
565 split_node->length = temp_dword - node->base;
566 node->base = temp_dword;
567 node->length -= split_node->length;
569 /* Put it in the list */
570 split_node->next = node->next;
571 node->next = split_node;
572 } /* End of non-aligned base */
574 /* Don't need to check if too small since we already did */
575 if (node->length > size) {
576 /* this one is longer than we need
577 * so we'll make a new entry and split it up
579 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
581 if (!split_node)
582 return NULL;
584 split_node->base = node->base + size;
585 split_node->length = node->length - size;
586 node->length = size;
588 /* Put it in the list */
589 split_node->next = node->next;
590 node->next = split_node;
591 } /* End of too big on top end */
593 /* For IO make sure it's not in the ISA aliasing space */
594 if (node->base & 0x300L)
595 continue;
597 /* If we got here, then it is the right size
598 * Now take it out of the list and break
600 if (*head == node) {
601 *head = node->next;
602 } else {
603 prevnode = *head;
604 while (prevnode->next != node)
605 prevnode = prevnode->next;
607 prevnode->next = node->next;
609 node->next = NULL;
610 break;
613 return node;
618 * get_max_resource - get largest node which has at least the given size.
619 * @head: the list to search the node in
620 * @size: the minimum size of the node to find
622 * Description: Gets the largest node that is at least "size" big from the
623 * list pointed to by head. It aligns the node on top and bottom
624 * to "size" alignment before returning it.
626 static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
628 struct pci_resource *max;
629 struct pci_resource *temp;
630 struct pci_resource *split_node;
631 u32 temp_dword;
633 if (cpqhp_resource_sort_and_combine(head))
634 return NULL;
636 if (sort_by_max_size(head))
637 return NULL;
639 for (max = *head; max; max = max->next) {
640 /* If not big enough we could probably just bail,
641 * instead we'll continue to the next.
643 if (max->length < size)
644 continue;
646 if (max->base & (size - 1)) {
647 /* this one isn't base aligned properly
648 * so we'll make a new entry and split it up
650 temp_dword = (max->base | (size-1)) + 1;
652 /* Short circuit if adjusted size is too small */
653 if ((max->length - (temp_dword - max->base)) < size)
654 continue;
656 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
658 if (!split_node)
659 return NULL;
661 split_node->base = max->base;
662 split_node->length = temp_dword - max->base;
663 max->base = temp_dword;
664 max->length -= split_node->length;
666 split_node->next = max->next;
667 max->next = split_node;
670 if ((max->base + max->length) & (size - 1)) {
671 /* this one isn't end aligned properly at the top
672 * so we'll make a new entry and split it up
674 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
676 if (!split_node)
677 return NULL;
678 temp_dword = ((max->base + max->length) & ~(size - 1));
679 split_node->base = temp_dword;
680 split_node->length = max->length + max->base
681 - split_node->base;
682 max->length -= split_node->length;
684 split_node->next = max->next;
685 max->next = split_node;
688 /* Make sure it didn't shrink too much when we aligned it */
689 if (max->length < size)
690 continue;
692 /* Now take it out of the list */
693 temp = *head;
694 if (temp == max) {
695 *head = max->next;
696 } else {
697 while (temp && temp->next != max) {
698 temp = temp->next;
701 temp->next = max->next;
704 max->next = NULL;
705 break;
708 return max;
713 * get_resource - find resource of given size and split up larger ones.
714 * @head: the list to search for resources
715 * @size: the size limit to use
717 * Description: This function sorts the resource list by size and then
718 * returns the first node of "size" length. If it finds a node
719 * larger than "size" it will split it up.
721 * size must be a power of two.
723 static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
725 struct pci_resource *prevnode;
726 struct pci_resource *node;
727 struct pci_resource *split_node;
728 u32 temp_dword;
730 if (cpqhp_resource_sort_and_combine(head))
731 return NULL;
733 if (sort_by_size(head))
734 return NULL;
736 for (node = *head; node; node = node->next) {
737 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
738 __func__, size, node, node->base, node->length);
739 if (node->length < size)
740 continue;
742 if (node->base & (size - 1)) {
743 dbg("%s: not aligned\n", __func__);
744 /* this one isn't base aligned properly
745 * so we'll make a new entry and split it up
747 temp_dword = (node->base | (size-1)) + 1;
749 /* Short circuit if adjusted size is too small */
750 if ((node->length - (temp_dword - node->base)) < size)
751 continue;
753 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
755 if (!split_node)
756 return NULL;
758 split_node->base = node->base;
759 split_node->length = temp_dword - node->base;
760 node->base = temp_dword;
761 node->length -= split_node->length;
763 split_node->next = node->next;
764 node->next = split_node;
765 } /* End of non-aligned base */
767 /* Don't need to check if too small since we already did */
768 if (node->length > size) {
769 dbg("%s: too big\n", __func__);
770 /* this one is longer than we need
771 * so we'll make a new entry and split it up
773 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
775 if (!split_node)
776 return NULL;
778 split_node->base = node->base + size;
779 split_node->length = node->length - size;
780 node->length = size;
782 /* Put it in the list */
783 split_node->next = node->next;
784 node->next = split_node;
785 } /* End of too big on top end */
787 dbg("%s: got one!!!\n", __func__);
788 /* If we got here, then it is the right size
789 * Now take it out of the list */
790 if (*head == node) {
791 *head = node->next;
792 } else {
793 prevnode = *head;
794 while (prevnode->next != node)
795 prevnode = prevnode->next;
797 prevnode->next = node->next;
799 node->next = NULL;
800 break;
802 return node;
807 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
808 * @head: the list to sort and clean up
810 * Description: Sorts all of the nodes in the list in ascending order by
811 * their base addresses. Also does garbage collection by
812 * combining adjacent nodes.
814 * Returns %0 if success.
816 int cpqhp_resource_sort_and_combine(struct pci_resource **head)
818 struct pci_resource *node1;
819 struct pci_resource *node2;
820 int out_of_order = 1;
822 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
824 if (!(*head))
825 return 1;
827 dbg("*head->next = %p\n",(*head)->next);
829 if (!(*head)->next)
830 return 0; /* only one item on the list, already sorted! */
832 dbg("*head->base = 0x%x\n",(*head)->base);
833 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
834 while (out_of_order) {
835 out_of_order = 0;
837 /* Special case for swapping list head */
838 if (((*head)->next) &&
839 ((*head)->base > (*head)->next->base)) {
840 node1 = *head;
841 (*head) = (*head)->next;
842 node1->next = (*head)->next;
843 (*head)->next = node1;
844 out_of_order++;
847 node1 = (*head);
849 while (node1->next && node1->next->next) {
850 if (node1->next->base > node1->next->next->base) {
851 out_of_order++;
852 node2 = node1->next;
853 node1->next = node1->next->next;
854 node1 = node1->next;
855 node2->next = node1->next;
856 node1->next = node2;
857 } else
858 node1 = node1->next;
860 } /* End of out_of_order loop */
862 node1 = *head;
864 while (node1 && node1->next) {
865 if ((node1->base + node1->length) == node1->next->base) {
866 /* Combine */
867 dbg("8..\n");
868 node1->length += node1->next->length;
869 node2 = node1->next;
870 node1->next = node1->next->next;
871 kfree(node2);
872 } else
873 node1 = node1->next;
876 return 0;
880 irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
882 struct controller *ctrl = data;
883 u8 schedule_flag = 0;
884 u8 reset;
885 u16 misc;
886 u32 Diff;
887 u32 temp_dword;
890 misc = readw(ctrl->hpc_reg + MISC);
892 * Check to see if it was our interrupt
894 if (!(misc & 0x000C)) {
895 return IRQ_NONE;
898 if (misc & 0x0004) {
900 * Serial Output interrupt Pending
903 /* Clear the interrupt */
904 misc |= 0x0004;
905 writew(misc, ctrl->hpc_reg + MISC);
907 /* Read to clear posted writes */
908 misc = readw(ctrl->hpc_reg + MISC);
910 dbg ("%s - waking up\n", __func__);
911 wake_up_interruptible(&ctrl->queue);
914 if (misc & 0x0008) {
915 /* General-interrupt-input interrupt Pending */
916 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
918 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
920 /* Clear the interrupt */
921 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
923 /* Read it back to clear any posted writes */
924 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
926 if (!Diff)
927 /* Clear all interrupts */
928 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
930 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
931 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
932 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
935 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
936 if (reset & 0x40) {
937 /* Bus reset has completed */
938 reset &= 0xCF;
939 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
940 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
941 wake_up_interruptible(&ctrl->queue);
944 if (schedule_flag) {
945 wake_up_process(cpqhp_event_thread);
946 dbg("Waking even thread");
948 return IRQ_HANDLED;
953 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
954 * @busnumber: bus where new node is to be located
956 * Returns pointer to the new node or %NULL if unsuccessful.
958 struct pci_func *cpqhp_slot_create(u8 busnumber)
960 struct pci_func *new_slot;
961 struct pci_func *next;
963 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
964 if (new_slot == NULL)
965 return new_slot;
967 new_slot->next = NULL;
968 new_slot->configured = 1;
970 if (cpqhp_slot_list[busnumber] == NULL) {
971 cpqhp_slot_list[busnumber] = new_slot;
972 } else {
973 next = cpqhp_slot_list[busnumber];
974 while (next->next != NULL)
975 next = next->next;
976 next->next = new_slot;
978 return new_slot;
983 * slot_remove - Removes a node from the linked list of slots.
984 * @old_slot: slot to remove
986 * Returns %0 if successful, !0 otherwise.
988 static int slot_remove(struct pci_func * old_slot)
990 struct pci_func *next;
992 if (old_slot == NULL)
993 return 1;
995 next = cpqhp_slot_list[old_slot->bus];
996 if (next == NULL)
997 return 1;
999 if (next == old_slot) {
1000 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1001 cpqhp_destroy_board_resources(old_slot);
1002 kfree(old_slot);
1003 return 0;
1006 while ((next->next != old_slot) && (next->next != NULL))
1007 next = next->next;
1009 if (next->next == old_slot) {
1010 next->next = old_slot->next;
1011 cpqhp_destroy_board_resources(old_slot);
1012 kfree(old_slot);
1013 return 0;
1014 } else
1015 return 2;
1020 * bridge_slot_remove - Removes a node from the linked list of slots.
1021 * @bridge: bridge to remove
1023 * Returns %0 if successful, !0 otherwise.
1025 static int bridge_slot_remove(struct pci_func *bridge)
1027 u8 subordinateBus, secondaryBus;
1028 u8 tempBus;
1029 struct pci_func *next;
1031 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1032 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1034 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1035 next = cpqhp_slot_list[tempBus];
1037 while (!slot_remove(next))
1038 next = cpqhp_slot_list[tempBus];
1041 next = cpqhp_slot_list[bridge->bus];
1043 if (next == NULL)
1044 return 1;
1046 if (next == bridge) {
1047 cpqhp_slot_list[bridge->bus] = bridge->next;
1048 goto out;
1051 while ((next->next != bridge) && (next->next != NULL))
1052 next = next->next;
1054 if (next->next != bridge)
1055 return 2;
1056 next->next = bridge->next;
1057 out:
1058 kfree(bridge);
1059 return 0;
1064 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1065 * @bus: bus to find
1066 * @device: device to find
1067 * @index: is %0 for first function found, %1 for the second...
1069 * Returns pointer to the node if successful, %NULL otherwise.
1071 struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1073 int found = -1;
1074 struct pci_func *func;
1076 func = cpqhp_slot_list[bus];
1078 if ((func == NULL) || ((func->device == device) && (index == 0)))
1079 return func;
1081 if (func->device == device)
1082 found++;
1084 while (func->next != NULL) {
1085 func = func->next;
1087 if (func->device == device)
1088 found++;
1090 if (found == index)
1091 return func;
1094 return NULL;
1098 static int is_bridge(struct pci_func * func)
1100 /* Check the header type */
1101 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1102 return 1;
1103 else
1104 return 0;
1109 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
1110 * @ctrl: controller to change frequency/mode for.
1111 * @adapter_speed: the speed of the adapter we want to match.
1112 * @hp_slot: the slot number where the adapter is installed.
1114 * Returns %0 if we successfully change frequency and/or mode to match the
1115 * adapter speed.
1117 static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1119 struct slot *slot;
1120 struct pci_bus *bus = ctrl->pci_bus;
1121 u8 reg;
1122 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1123 u16 reg16;
1124 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
1126 if (bus->cur_bus_speed == adapter_speed)
1127 return 0;
1129 /* We don't allow freq/mode changes if we find another adapter running
1130 * in another slot on this controller
1132 for(slot = ctrl->slot; slot; slot = slot->next) {
1133 if (slot->device == (hp_slot + ctrl->slot_device_offset))
1134 continue;
1135 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
1136 continue;
1137 if (slot->hotplug_slot->info->adapter_status == 0)
1138 continue;
1139 /* If another adapter is running on the same segment but at a
1140 * lower speed/mode, we allow the new adapter to function at
1141 * this rate if supported
1143 if (bus->cur_bus_speed < adapter_speed)
1144 return 0;
1146 return 1;
1149 /* If the controller doesn't support freq/mode changes and the
1150 * controller is running at a higher mode, we bail
1152 if ((bus->cur_bus_speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1153 return 1;
1155 /* But we allow the adapter to run at a lower rate if possible */
1156 if ((bus->cur_bus_speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1157 return 0;
1159 /* We try to set the max speed supported by both the adapter and
1160 * controller
1162 if (bus->max_bus_speed < adapter_speed) {
1163 if (bus->cur_bus_speed == bus->max_bus_speed)
1164 return 0;
1165 adapter_speed = bus->max_bus_speed;
1168 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1169 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
1171 set_SOGO(ctrl);
1172 wait_for_ctrl_irq(ctrl);
1174 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1175 reg = 0xF5;
1176 else
1177 reg = 0xF4;
1178 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1180 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1181 reg16 &= ~0x000F;
1182 switch(adapter_speed) {
1183 case(PCI_SPEED_133MHz_PCIX):
1184 reg = 0x75;
1185 reg16 |= 0xB;
1186 break;
1187 case(PCI_SPEED_100MHz_PCIX):
1188 reg = 0x74;
1189 reg16 |= 0xA;
1190 break;
1191 case(PCI_SPEED_66MHz_PCIX):
1192 reg = 0x73;
1193 reg16 |= 0x9;
1194 break;
1195 case(PCI_SPEED_66MHz):
1196 reg = 0x73;
1197 reg16 |= 0x1;
1198 break;
1199 default: /* 33MHz PCI 2.2 */
1200 reg = 0x71;
1201 break;
1204 reg16 |= 0xB << 12;
1205 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
1207 mdelay(5);
1209 /* Reenable interrupts */
1210 writel(0, ctrl->hpc_reg + INT_MASK);
1212 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1214 /* Restart state machine */
1215 reg = ~0xF;
1216 pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
1217 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
1219 /* Only if mode change...*/
1220 if (((bus->cur_bus_speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1221 ((bus->cur_bus_speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1222 set_SOGO(ctrl);
1224 wait_for_ctrl_irq(ctrl);
1225 mdelay(1100);
1227 /* Restore LED/Slot state */
1228 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1229 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
1231 set_SOGO(ctrl);
1232 wait_for_ctrl_irq(ctrl);
1234 bus->cur_bus_speed = adapter_speed;
1235 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1237 info("Successfully changed frequency/mode for adapter in slot %d\n",
1238 slot->number);
1239 return 0;
1242 /* the following routines constitute the bulk of the
1243 * hotplug controller logic
1248 * board_replaced - Called after a board has been replaced in the system.
1249 * @func: PCI device/function information
1250 * @ctrl: hotplug controller
1252 * This is only used if we don't have resources for hot add.
1253 * Turns power on for the board.
1254 * Checks to see if board is the same.
1255 * If board is same, reconfigures it.
1256 * If board isn't same, turns it back off.
1258 static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1260 struct pci_bus *bus = ctrl->pci_bus;
1261 u8 hp_slot;
1262 u8 temp_byte;
1263 u8 adapter_speed;
1264 u32 rc = 0;
1266 hp_slot = func->device - ctrl->slot_device_offset;
1269 * The switch is open.
1271 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot))
1272 rc = INTERLOCK_OPEN;
1274 * The board is already on
1276 else if (is_slot_enabled (ctrl, hp_slot))
1277 rc = CARD_FUNCTIONING;
1278 else {
1279 mutex_lock(&ctrl->crit_sect);
1281 /* turn on board without attaching to the bus */
1282 enable_slot_power (ctrl, hp_slot);
1284 set_SOGO(ctrl);
1286 /* Wait for SOBS to be unset */
1287 wait_for_ctrl_irq (ctrl);
1289 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1290 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1291 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1293 set_SOGO(ctrl);
1295 /* Wait for SOBS to be unset */
1296 wait_for_ctrl_irq (ctrl);
1298 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1299 if (bus->cur_bus_speed != adapter_speed)
1300 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1301 rc = WRONG_BUS_FREQUENCY;
1303 /* turn off board without attaching to the bus */
1304 disable_slot_power (ctrl, hp_slot);
1306 set_SOGO(ctrl);
1308 /* Wait for SOBS to be unset */
1309 wait_for_ctrl_irq (ctrl);
1311 mutex_unlock(&ctrl->crit_sect);
1313 if (rc)
1314 return rc;
1316 mutex_lock(&ctrl->crit_sect);
1318 slot_enable (ctrl, hp_slot);
1319 green_LED_blink (ctrl, hp_slot);
1321 amber_LED_off (ctrl, hp_slot);
1323 set_SOGO(ctrl);
1325 /* Wait for SOBS to be unset */
1326 wait_for_ctrl_irq (ctrl);
1328 mutex_unlock(&ctrl->crit_sect);
1330 /* Wait for ~1 second because of hot plug spec */
1331 long_delay(1*HZ);
1333 /* Check for a power fault */
1334 if (func->status == 0xFF) {
1335 /* power fault occurred, but it was benign */
1336 rc = POWER_FAILURE;
1337 func->status = 0;
1338 } else
1339 rc = cpqhp_valid_replace(ctrl, func);
1341 if (!rc) {
1342 /* It must be the same board */
1344 rc = cpqhp_configure_board(ctrl, func);
1346 /* If configuration fails, turn it off
1347 * Get slot won't work for devices behind
1348 * bridges, but in this case it will always be
1349 * called for the "base" bus/dev/func of an
1350 * adapter.
1353 mutex_lock(&ctrl->crit_sect);
1355 amber_LED_on (ctrl, hp_slot);
1356 green_LED_off (ctrl, hp_slot);
1357 slot_disable (ctrl, hp_slot);
1359 set_SOGO(ctrl);
1361 /* Wait for SOBS to be unset */
1362 wait_for_ctrl_irq (ctrl);
1364 mutex_unlock(&ctrl->crit_sect);
1366 if (rc)
1367 return rc;
1368 else
1369 return 1;
1371 } else {
1372 /* Something is wrong
1374 * Get slot won't work for devices behind bridges, but
1375 * in this case it will always be called for the "base"
1376 * bus/dev/func of an adapter.
1379 mutex_lock(&ctrl->crit_sect);
1381 amber_LED_on (ctrl, hp_slot);
1382 green_LED_off (ctrl, hp_slot);
1383 slot_disable (ctrl, hp_slot);
1385 set_SOGO(ctrl);
1387 /* Wait for SOBS to be unset */
1388 wait_for_ctrl_irq (ctrl);
1390 mutex_unlock(&ctrl->crit_sect);
1394 return rc;
1400 * board_added - Called after a board has been added to the system.
1401 * @func: PCI device/function info
1402 * @ctrl: hotplug controller
1404 * Turns power on for the board.
1405 * Configures board.
1407 static u32 board_added(struct pci_func *func, struct controller *ctrl)
1409 u8 hp_slot;
1410 u8 temp_byte;
1411 u8 adapter_speed;
1412 int index;
1413 u32 temp_register = 0xFFFFFFFF;
1414 u32 rc = 0;
1415 struct pci_func *new_slot = NULL;
1416 struct pci_bus *bus = ctrl->pci_bus;
1417 struct slot *p_slot;
1418 struct resource_lists res_lists;
1420 hp_slot = func->device - ctrl->slot_device_offset;
1421 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
1422 __func__, func->device, ctrl->slot_device_offset, hp_slot);
1424 mutex_lock(&ctrl->crit_sect);
1426 /* turn on board without attaching to the bus */
1427 enable_slot_power(ctrl, hp_slot);
1429 set_SOGO(ctrl);
1431 /* Wait for SOBS to be unset */
1432 wait_for_ctrl_irq (ctrl);
1434 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1435 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1436 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1438 set_SOGO(ctrl);
1440 /* Wait for SOBS to be unset */
1441 wait_for_ctrl_irq (ctrl);
1443 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1444 if (bus->cur_bus_speed != adapter_speed)
1445 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1446 rc = WRONG_BUS_FREQUENCY;
1448 /* turn off board without attaching to the bus */
1449 disable_slot_power (ctrl, hp_slot);
1451 set_SOGO(ctrl);
1453 /* Wait for SOBS to be unset */
1454 wait_for_ctrl_irq(ctrl);
1456 mutex_unlock(&ctrl->crit_sect);
1458 if (rc)
1459 return rc;
1461 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1463 /* turn on board and blink green LED */
1465 dbg("%s: before down\n", __func__);
1466 mutex_lock(&ctrl->crit_sect);
1467 dbg("%s: after down\n", __func__);
1469 dbg("%s: before slot_enable\n", __func__);
1470 slot_enable (ctrl, hp_slot);
1472 dbg("%s: before green_LED_blink\n", __func__);
1473 green_LED_blink (ctrl, hp_slot);
1475 dbg("%s: before amber_LED_blink\n", __func__);
1476 amber_LED_off (ctrl, hp_slot);
1478 dbg("%s: before set_SOGO\n", __func__);
1479 set_SOGO(ctrl);
1481 /* Wait for SOBS to be unset */
1482 dbg("%s: before wait_for_ctrl_irq\n", __func__);
1483 wait_for_ctrl_irq (ctrl);
1484 dbg("%s: after wait_for_ctrl_irq\n", __func__);
1486 dbg("%s: before up\n", __func__);
1487 mutex_unlock(&ctrl->crit_sect);
1488 dbg("%s: after up\n", __func__);
1490 /* Wait for ~1 second because of hot plug spec */
1491 dbg("%s: before long_delay\n", __func__);
1492 long_delay(1*HZ);
1493 dbg("%s: after long_delay\n", __func__);
1495 dbg("%s: func status = %x\n", __func__, func->status);
1496 /* Check for a power fault */
1497 if (func->status == 0xFF) {
1498 /* power fault occurred, but it was benign */
1499 temp_register = 0xFFFFFFFF;
1500 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
1501 rc = POWER_FAILURE;
1502 func->status = 0;
1503 } else {
1504 /* Get vendor/device ID u32 */
1505 ctrl->pci_bus->number = func->bus;
1506 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
1507 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1508 dbg("%s: temp_register is %x\n", __func__, temp_register);
1510 if (rc != 0) {
1511 /* Something's wrong here */
1512 temp_register = 0xFFFFFFFF;
1513 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
1515 /* Preset return code. It will be changed later if things go okay. */
1516 rc = NO_ADAPTER_PRESENT;
1519 /* All F's is an empty slot or an invalid board */
1520 if (temp_register != 0xFFFFFFFF) {
1521 res_lists.io_head = ctrl->io_head;
1522 res_lists.mem_head = ctrl->mem_head;
1523 res_lists.p_mem_head = ctrl->p_mem_head;
1524 res_lists.bus_head = ctrl->bus_head;
1525 res_lists.irqs = NULL;
1527 rc = configure_new_device(ctrl, func, 0, &res_lists);
1529 dbg("%s: back from configure_new_device\n", __func__);
1530 ctrl->io_head = res_lists.io_head;
1531 ctrl->mem_head = res_lists.mem_head;
1532 ctrl->p_mem_head = res_lists.p_mem_head;
1533 ctrl->bus_head = res_lists.bus_head;
1535 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1536 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1537 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1538 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1540 if (rc) {
1541 mutex_lock(&ctrl->crit_sect);
1543 amber_LED_on (ctrl, hp_slot);
1544 green_LED_off (ctrl, hp_slot);
1545 slot_disable (ctrl, hp_slot);
1547 set_SOGO(ctrl);
1549 /* Wait for SOBS to be unset */
1550 wait_for_ctrl_irq (ctrl);
1552 mutex_unlock(&ctrl->crit_sect);
1553 return rc;
1554 } else {
1555 cpqhp_save_slot_config(ctrl, func);
1559 func->status = 0;
1560 func->switch_save = 0x10;
1561 func->is_a_board = 0x01;
1563 /* next, we will instantiate the linux pci_dev structures (with
1564 * appropriate driver notification, if already present) */
1565 dbg("%s: configure linux pci_dev structure\n", __func__);
1566 index = 0;
1567 do {
1568 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1569 if (new_slot && !new_slot->pci_dev)
1570 cpqhp_configure_device(ctrl, new_slot);
1571 } while (new_slot);
1573 mutex_lock(&ctrl->crit_sect);
1575 green_LED_on (ctrl, hp_slot);
1577 set_SOGO(ctrl);
1579 /* Wait for SOBS to be unset */
1580 wait_for_ctrl_irq (ctrl);
1582 mutex_unlock(&ctrl->crit_sect);
1583 } else {
1584 mutex_lock(&ctrl->crit_sect);
1586 amber_LED_on (ctrl, hp_slot);
1587 green_LED_off (ctrl, hp_slot);
1588 slot_disable (ctrl, hp_slot);
1590 set_SOGO(ctrl);
1592 /* Wait for SOBS to be unset */
1593 wait_for_ctrl_irq (ctrl);
1595 mutex_unlock(&ctrl->crit_sect);
1597 return rc;
1599 return 0;
1604 * remove_board - Turns off slot and LEDs
1605 * @func: PCI device/function info
1606 * @replace_flag: whether replacing or adding a new device
1607 * @ctrl: target controller
1609 static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1611 int index;
1612 u8 skip = 0;
1613 u8 device;
1614 u8 hp_slot;
1615 u8 temp_byte;
1616 u32 rc;
1617 struct resource_lists res_lists;
1618 struct pci_func *temp_func;
1620 if (cpqhp_unconfigure_device(func))
1621 return 1;
1623 device = func->device;
1625 hp_slot = func->device - ctrl->slot_device_offset;
1626 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
1628 /* When we get here, it is safe to change base address registers.
1629 * We will attempt to save the base address register lengths */
1630 if (replace_flag || !ctrl->add_support)
1631 rc = cpqhp_save_base_addr_length(ctrl, func);
1632 else if (!func->bus_head && !func->mem_head &&
1633 !func->p_mem_head && !func->io_head) {
1634 /* Here we check to see if we've saved any of the board's
1635 * resources already. If so, we'll skip the attempt to
1636 * determine what's being used. */
1637 index = 0;
1638 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1639 while (temp_func) {
1640 if (temp_func->bus_head || temp_func->mem_head
1641 || temp_func->p_mem_head || temp_func->io_head) {
1642 skip = 1;
1643 break;
1645 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1648 if (!skip)
1649 rc = cpqhp_save_used_resources(ctrl, func);
1651 /* Change status to shutdown */
1652 if (func->is_a_board)
1653 func->status = 0x01;
1654 func->configured = 0;
1656 mutex_lock(&ctrl->crit_sect);
1658 green_LED_off (ctrl, hp_slot);
1659 slot_disable (ctrl, hp_slot);
1661 set_SOGO(ctrl);
1663 /* turn off SERR for slot */
1664 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1665 temp_byte &= ~(0x01 << hp_slot);
1666 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1668 /* Wait for SOBS to be unset */
1669 wait_for_ctrl_irq (ctrl);
1671 mutex_unlock(&ctrl->crit_sect);
1673 if (!replace_flag && ctrl->add_support) {
1674 while (func) {
1675 res_lists.io_head = ctrl->io_head;
1676 res_lists.mem_head = ctrl->mem_head;
1677 res_lists.p_mem_head = ctrl->p_mem_head;
1678 res_lists.bus_head = ctrl->bus_head;
1680 cpqhp_return_board_resources(func, &res_lists);
1682 ctrl->io_head = res_lists.io_head;
1683 ctrl->mem_head = res_lists.mem_head;
1684 ctrl->p_mem_head = res_lists.p_mem_head;
1685 ctrl->bus_head = res_lists.bus_head;
1687 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1688 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1689 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1690 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1692 if (is_bridge(func)) {
1693 bridge_slot_remove(func);
1694 } else
1695 slot_remove(func);
1697 func = cpqhp_slot_find(ctrl->bus, device, 0);
1700 /* Setup slot structure with entry for empty slot */
1701 func = cpqhp_slot_create(ctrl->bus);
1703 if (func == NULL)
1704 return 1;
1706 func->bus = ctrl->bus;
1707 func->device = device;
1708 func->function = 0;
1709 func->configured = 0;
1710 func->switch_save = 0x10;
1711 func->is_a_board = 0;
1712 func->p_task_event = NULL;
1715 return 0;
1718 static void pushbutton_helper_thread(unsigned long data)
1720 pushbutton_pending = data;
1721 wake_up_process(cpqhp_event_thread);
1725 /* this is the main worker thread */
1726 static int event_thread(void* data)
1728 struct controller *ctrl;
1730 while (1) {
1731 dbg("!!!!event_thread sleeping\n");
1732 set_current_state(TASK_INTERRUPTIBLE);
1733 schedule();
1735 if (kthread_should_stop())
1736 break;
1737 /* Do stuff here */
1738 if (pushbutton_pending)
1739 cpqhp_pushbutton_thread(pushbutton_pending);
1740 else
1741 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1742 interrupt_event_handler(ctrl);
1744 dbg("event_thread signals exit\n");
1745 return 0;
1748 int cpqhp_event_start_thread(void)
1750 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1751 if (IS_ERR(cpqhp_event_thread)) {
1752 err ("Can't start up our event thread\n");
1753 return PTR_ERR(cpqhp_event_thread);
1756 return 0;
1760 void cpqhp_event_stop_thread(void)
1762 kthread_stop(cpqhp_event_thread);
1766 static int update_slot_info(struct controller *ctrl, struct slot *slot)
1768 struct hotplug_slot_info *info;
1769 int result;
1771 info = kmalloc(sizeof(*info), GFP_KERNEL);
1772 if (!info)
1773 return -ENOMEM;
1775 info->power_status = get_slot_enabled(ctrl, slot);
1776 info->attention_status = cpq_get_attention_status(ctrl, slot);
1777 info->latch_status = cpq_get_latch_status(ctrl, slot);
1778 info->adapter_status = get_presence_status(ctrl, slot);
1779 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1780 kfree (info);
1781 return result;
1784 static void interrupt_event_handler(struct controller *ctrl)
1786 int loop = 0;
1787 int change = 1;
1788 struct pci_func *func;
1789 u8 hp_slot;
1790 struct slot *p_slot;
1792 while (change) {
1793 change = 0;
1795 for (loop = 0; loop < 10; loop++) {
1796 /* dbg("loop %d\n", loop); */
1797 if (ctrl->event_queue[loop].event_type != 0) {
1798 hp_slot = ctrl->event_queue[loop].hp_slot;
1800 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1801 if (!func)
1802 return;
1804 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1805 if (!p_slot)
1806 return;
1808 dbg("hp_slot %d, func %p, p_slot %p\n",
1809 hp_slot, func, p_slot);
1811 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1812 dbg("button pressed\n");
1813 } else if (ctrl->event_queue[loop].event_type ==
1814 INT_BUTTON_CANCEL) {
1815 dbg("button cancel\n");
1816 del_timer(&p_slot->task_event);
1818 mutex_lock(&ctrl->crit_sect);
1820 if (p_slot->state == BLINKINGOFF_STATE) {
1821 /* slot is on */
1822 dbg("turn on green LED\n");
1823 green_LED_on (ctrl, hp_slot);
1824 } else if (p_slot->state == BLINKINGON_STATE) {
1825 /* slot is off */
1826 dbg("turn off green LED\n");
1827 green_LED_off (ctrl, hp_slot);
1830 info(msg_button_cancel, p_slot->number);
1832 p_slot->state = STATIC_STATE;
1834 amber_LED_off (ctrl, hp_slot);
1836 set_SOGO(ctrl);
1838 /* Wait for SOBS to be unset */
1839 wait_for_ctrl_irq (ctrl);
1841 mutex_unlock(&ctrl->crit_sect);
1843 /*** button Released (No action on press...) */
1844 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1845 dbg("button release\n");
1847 if (is_slot_enabled (ctrl, hp_slot)) {
1848 dbg("slot is on\n");
1849 p_slot->state = BLINKINGOFF_STATE;
1850 info(msg_button_off, p_slot->number);
1851 } else {
1852 dbg("slot is off\n");
1853 p_slot->state = BLINKINGON_STATE;
1854 info(msg_button_on, p_slot->number);
1856 mutex_lock(&ctrl->crit_sect);
1858 dbg("blink green LED and turn off amber\n");
1860 amber_LED_off (ctrl, hp_slot);
1861 green_LED_blink (ctrl, hp_slot);
1863 set_SOGO(ctrl);
1865 /* Wait for SOBS to be unset */
1866 wait_for_ctrl_irq (ctrl);
1868 mutex_unlock(&ctrl->crit_sect);
1869 init_timer(&p_slot->task_event);
1870 p_slot->hp_slot = hp_slot;
1871 p_slot->ctrl = ctrl;
1872 /* p_slot->physical_slot = physical_slot; */
1873 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1874 p_slot->task_event.function = pushbutton_helper_thread;
1875 p_slot->task_event.data = (u32) p_slot;
1877 dbg("add_timer p_slot = %p\n", p_slot);
1878 add_timer(&p_slot->task_event);
1880 /***********POWER FAULT */
1881 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1882 dbg("power fault\n");
1883 } else {
1884 /* refresh notification */
1885 if (p_slot)
1886 update_slot_info(ctrl, p_slot);
1889 ctrl->event_queue[loop].event_type = 0;
1891 change = 1;
1893 } /* End of FOR loop */
1896 return;
1901 * cpqhp_pushbutton_thread - handle pushbutton events
1902 * @slot: target slot (struct)
1904 * Scheduled procedure to handle blocking stuff for the pushbuttons.
1905 * Handles all pending events and exits.
1907 void cpqhp_pushbutton_thread(unsigned long slot)
1909 u8 hp_slot;
1910 u8 device;
1911 struct pci_func *func;
1912 struct slot *p_slot = (struct slot *) slot;
1913 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1915 pushbutton_pending = 0;
1916 hp_slot = p_slot->hp_slot;
1918 device = p_slot->device;
1920 if (is_slot_enabled(ctrl, hp_slot)) {
1921 p_slot->state = POWEROFF_STATE;
1922 /* power Down board */
1923 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1924 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1925 if (!func) {
1926 dbg("Error! func NULL in %s\n", __func__);
1927 return ;
1930 if (cpqhp_process_SS(ctrl, func) != 0) {
1931 amber_LED_on(ctrl, hp_slot);
1932 green_LED_on(ctrl, hp_slot);
1934 set_SOGO(ctrl);
1936 /* Wait for SOBS to be unset */
1937 wait_for_ctrl_irq(ctrl);
1940 p_slot->state = STATIC_STATE;
1941 } else {
1942 p_slot->state = POWERON_STATE;
1943 /* slot is off */
1945 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1946 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1947 if (!func) {
1948 dbg("Error! func NULL in %s\n", __func__);
1949 return ;
1952 if (ctrl != NULL) {
1953 if (cpqhp_process_SI(ctrl, func) != 0) {
1954 amber_LED_on(ctrl, hp_slot);
1955 green_LED_off(ctrl, hp_slot);
1957 set_SOGO(ctrl);
1959 /* Wait for SOBS to be unset */
1960 wait_for_ctrl_irq (ctrl);
1964 p_slot->state = STATIC_STATE;
1967 return;
1971 int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1973 u8 device, hp_slot;
1974 u16 temp_word;
1975 u32 tempdword;
1976 int rc;
1977 struct slot* p_slot;
1978 int physical_slot = 0;
1980 tempdword = 0;
1982 device = func->device;
1983 hp_slot = device - ctrl->slot_device_offset;
1984 p_slot = cpqhp_find_slot(ctrl, device);
1985 if (p_slot)
1986 physical_slot = p_slot->number;
1988 /* Check to see if the interlock is closed */
1989 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
1991 if (tempdword & (0x01 << hp_slot)) {
1992 return 1;
1995 if (func->is_a_board) {
1996 rc = board_replaced(func, ctrl);
1997 } else {
1998 /* add board */
1999 slot_remove(func);
2001 func = cpqhp_slot_create(ctrl->bus);
2002 if (func == NULL)
2003 return 1;
2005 func->bus = ctrl->bus;
2006 func->device = device;
2007 func->function = 0;
2008 func->configured = 0;
2009 func->is_a_board = 1;
2011 /* We have to save the presence info for these slots */
2012 temp_word = ctrl->ctrl_int_comp >> 16;
2013 func->presence_save = (temp_word >> hp_slot) & 0x01;
2014 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2016 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2017 func->switch_save = 0;
2018 } else {
2019 func->switch_save = 0x10;
2022 rc = board_added(func, ctrl);
2023 if (rc) {
2024 if (is_bridge(func)) {
2025 bridge_slot_remove(func);
2026 } else
2027 slot_remove(func);
2029 /* Setup slot structure with entry for empty slot */
2030 func = cpqhp_slot_create(ctrl->bus);
2032 if (func == NULL)
2033 return 1;
2035 func->bus = ctrl->bus;
2036 func->device = device;
2037 func->function = 0;
2038 func->configured = 0;
2039 func->is_a_board = 0;
2041 /* We have to save the presence info for these slots */
2042 temp_word = ctrl->ctrl_int_comp >> 16;
2043 func->presence_save = (temp_word >> hp_slot) & 0x01;
2044 func->presence_save |=
2045 (temp_word >> (hp_slot + 7)) & 0x02;
2047 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2048 func->switch_save = 0;
2049 } else {
2050 func->switch_save = 0x10;
2055 if (rc) {
2056 dbg("%s: rc = %d\n", __func__, rc);
2059 if (p_slot)
2060 update_slot_info(ctrl, p_slot);
2062 return rc;
2066 int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2068 u8 device, class_code, header_type, BCR;
2069 u8 index = 0;
2070 u8 replace_flag;
2071 u32 rc = 0;
2072 unsigned int devfn;
2073 struct slot* p_slot;
2074 struct pci_bus *pci_bus = ctrl->pci_bus;
2075 int physical_slot=0;
2077 device = func->device;
2078 func = cpqhp_slot_find(ctrl->bus, device, index++);
2079 p_slot = cpqhp_find_slot(ctrl, device);
2080 if (p_slot) {
2081 physical_slot = p_slot->number;
2084 /* Make sure there are no video controllers here */
2085 while (func && !rc) {
2086 pci_bus->number = func->bus;
2087 devfn = PCI_DEVFN(func->device, func->function);
2089 /* Check the Class Code */
2090 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2091 if (rc)
2092 return rc;
2094 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2095 /* Display/Video adapter (not supported) */
2096 rc = REMOVE_NOT_SUPPORTED;
2097 } else {
2098 /* See if it's a bridge */
2099 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2100 if (rc)
2101 return rc;
2103 /* If it's a bridge, check the VGA Enable bit */
2104 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2105 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2106 if (rc)
2107 return rc;
2109 /* If the VGA Enable bit is set, remove isn't
2110 * supported */
2111 if (BCR & PCI_BRIDGE_CTL_VGA)
2112 rc = REMOVE_NOT_SUPPORTED;
2116 func = cpqhp_slot_find(ctrl->bus, device, index++);
2119 func = cpqhp_slot_find(ctrl->bus, device, 0);
2120 if ((func != NULL) && !rc) {
2121 replace_flag = !(ctrl->add_support);
2122 rc = remove_board(func, replace_flag, ctrl);
2123 } else if (!rc) {
2124 rc = 1;
2127 if (p_slot)
2128 update_slot_info(ctrl, p_slot);
2130 return rc;
2134 * switch_leds - switch the leds, go from one site to the other.
2135 * @ctrl: controller to use
2136 * @num_of_slots: number of slots to use
2137 * @work_LED: LED control value
2138 * @direction: 1 to start from the left side, 0 to start right.
2140 static void switch_leds(struct controller *ctrl, const int num_of_slots,
2141 u32 *work_LED, const int direction)
2143 int loop;
2145 for (loop = 0; loop < num_of_slots; loop++) {
2146 if (direction)
2147 *work_LED = *work_LED >> 1;
2148 else
2149 *work_LED = *work_LED << 1;
2150 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2152 set_SOGO(ctrl);
2154 /* Wait for SOGO interrupt */
2155 wait_for_ctrl_irq(ctrl);
2157 /* Get ready for next iteration */
2158 long_delay((2*HZ)/10);
2163 * cpqhp_hardware_test - runs hardware tests
2164 * @ctrl: target controller
2165 * @test_num: the number written to the "test" file in sysfs.
2167 * For hot plug ctrl folks to play with.
2169 int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2171 u32 save_LED;
2172 u32 work_LED;
2173 int loop;
2174 int num_of_slots;
2176 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2178 switch (test_num) {
2179 case 1:
2180 /* Do stuff here! */
2182 /* Do that funky LED thing */
2183 /* so we can restore them later */
2184 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2185 work_LED = 0x01010101;
2186 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2187 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2188 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2189 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2191 work_LED = 0x01010000;
2192 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2193 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2194 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2195 work_LED = 0x00000101;
2196 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2197 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2198 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2200 work_LED = 0x01010000;
2201 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2202 for (loop = 0; loop < num_of_slots; loop++) {
2203 set_SOGO(ctrl);
2205 /* Wait for SOGO interrupt */
2206 wait_for_ctrl_irq (ctrl);
2208 /* Get ready for next iteration */
2209 long_delay((3*HZ)/10);
2210 work_LED = work_LED >> 16;
2211 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2213 set_SOGO(ctrl);
2215 /* Wait for SOGO interrupt */
2216 wait_for_ctrl_irq (ctrl);
2218 /* Get ready for next iteration */
2219 long_delay((3*HZ)/10);
2220 work_LED = work_LED << 16;
2221 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2222 work_LED = work_LED << 1;
2223 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2226 /* put it back the way it was */
2227 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2229 set_SOGO(ctrl);
2231 /* Wait for SOBS to be unset */
2232 wait_for_ctrl_irq (ctrl);
2233 break;
2234 case 2:
2235 /* Do other stuff here! */
2236 break;
2237 case 3:
2238 /* and more... */
2239 break;
2241 return 0;
2246 * configure_new_device - Configures the PCI header information of one board.
2247 * @ctrl: pointer to controller structure
2248 * @func: pointer to function structure
2249 * @behind_bridge: 1 if this is a recursive call, 0 if not
2250 * @resources: pointer to set of resource lists
2252 * Returns 0 if success.
2254 static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2255 u8 behind_bridge, struct resource_lists * resources)
2257 u8 temp_byte, function, max_functions, stop_it;
2258 int rc;
2259 u32 ID;
2260 struct pci_func *new_slot;
2261 int index;
2263 new_slot = func;
2265 dbg("%s\n", __func__);
2266 /* Check for Multi-function device */
2267 ctrl->pci_bus->number = func->bus;
2268 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2269 if (rc) {
2270 dbg("%s: rc = %d\n", __func__, rc);
2271 return rc;
2274 if (temp_byte & 0x80) /* Multi-function device */
2275 max_functions = 8;
2276 else
2277 max_functions = 1;
2279 function = 0;
2281 do {
2282 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2284 if (rc) {
2285 dbg("configure_new_function failed %d\n",rc);
2286 index = 0;
2288 while (new_slot) {
2289 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2291 if (new_slot)
2292 cpqhp_return_board_resources(new_slot, resources);
2295 return rc;
2298 function++;
2300 stop_it = 0;
2302 /* The following loop skips to the next present function
2303 * and creates a board structure */
2305 while ((function < max_functions) && (!stop_it)) {
2306 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2308 if (ID == 0xFFFFFFFF) {
2309 function++;
2310 } else {
2311 /* Setup slot structure. */
2312 new_slot = cpqhp_slot_create(func->bus);
2314 if (new_slot == NULL)
2315 return 1;
2317 new_slot->bus = func->bus;
2318 new_slot->device = func->device;
2319 new_slot->function = function;
2320 new_slot->is_a_board = 1;
2321 new_slot->status = 0;
2323 stop_it++;
2327 } while (function < max_functions);
2328 dbg("returning from configure_new_device\n");
2330 return 0;
2335 * Configuration logic that involves the hotplug data structures and
2336 * their bookkeeping
2341 * configure_new_function - Configures the PCI header information of one device
2342 * @ctrl: pointer to controller structure
2343 * @func: pointer to function structure
2344 * @behind_bridge: 1 if this is a recursive call, 0 if not
2345 * @resources: pointer to set of resource lists
2347 * Calls itself recursively for bridged devices.
2348 * Returns 0 if success.
2350 static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2351 u8 behind_bridge,
2352 struct resource_lists *resources)
2354 int cloop;
2355 u8 IRQ = 0;
2356 u8 temp_byte;
2357 u8 device;
2358 u8 class_code;
2359 u16 command;
2360 u16 temp_word;
2361 u32 temp_dword;
2362 u32 rc;
2363 u32 temp_register;
2364 u32 base;
2365 u32 ID;
2366 unsigned int devfn;
2367 struct pci_resource *mem_node;
2368 struct pci_resource *p_mem_node;
2369 struct pci_resource *io_node;
2370 struct pci_resource *bus_node;
2371 struct pci_resource *hold_mem_node;
2372 struct pci_resource *hold_p_mem_node;
2373 struct pci_resource *hold_IO_node;
2374 struct pci_resource *hold_bus_node;
2375 struct irq_mapping irqs;
2376 struct pci_func *new_slot;
2377 struct pci_bus *pci_bus;
2378 struct resource_lists temp_resources;
2380 pci_bus = ctrl->pci_bus;
2381 pci_bus->number = func->bus;
2382 devfn = PCI_DEVFN(func->device, func->function);
2384 /* Check for Bridge */
2385 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2386 if (rc)
2387 return rc;
2389 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2390 /* set Primary bus */
2391 dbg("set Primary bus = %d\n", func->bus);
2392 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2393 if (rc)
2394 return rc;
2396 /* find range of busses to use */
2397 dbg("find ranges of buses to use\n");
2398 bus_node = get_max_resource(&(resources->bus_head), 1);
2400 /* If we don't have any busses to allocate, we can't continue */
2401 if (!bus_node)
2402 return -ENOMEM;
2404 /* set Secondary bus */
2405 temp_byte = bus_node->base;
2406 dbg("set Secondary bus = %d\n", bus_node->base);
2407 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2408 if (rc)
2409 return rc;
2411 /* set subordinate bus */
2412 temp_byte = bus_node->base + bus_node->length - 1;
2413 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2414 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2415 if (rc)
2416 return rc;
2418 /* set subordinate Latency Timer and base Latency Timer */
2419 temp_byte = 0x40;
2420 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2421 if (rc)
2422 return rc;
2423 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2424 if (rc)
2425 return rc;
2427 /* set Cache Line size */
2428 temp_byte = 0x08;
2429 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2430 if (rc)
2431 return rc;
2433 /* Setup the IO, memory, and prefetchable windows */
2434 io_node = get_max_resource(&(resources->io_head), 0x1000);
2435 if (!io_node)
2436 return -ENOMEM;
2437 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2438 if (!mem_node)
2439 return -ENOMEM;
2440 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2441 if (!p_mem_node)
2442 return -ENOMEM;
2443 dbg("Setup the IO, memory, and prefetchable windows\n");
2444 dbg("io_node\n");
2445 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2446 io_node->length, io_node->next);
2447 dbg("mem_node\n");
2448 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2449 mem_node->length, mem_node->next);
2450 dbg("p_mem_node\n");
2451 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2452 p_mem_node->length, p_mem_node->next);
2454 /* set up the IRQ info */
2455 if (!resources->irqs) {
2456 irqs.barber_pole = 0;
2457 irqs.interrupt[0] = 0;
2458 irqs.interrupt[1] = 0;
2459 irqs.interrupt[2] = 0;
2460 irqs.interrupt[3] = 0;
2461 irqs.valid_INT = 0;
2462 } else {
2463 irqs.barber_pole = resources->irqs->barber_pole;
2464 irqs.interrupt[0] = resources->irqs->interrupt[0];
2465 irqs.interrupt[1] = resources->irqs->interrupt[1];
2466 irqs.interrupt[2] = resources->irqs->interrupt[2];
2467 irqs.interrupt[3] = resources->irqs->interrupt[3];
2468 irqs.valid_INT = resources->irqs->valid_INT;
2471 /* set up resource lists that are now aligned on top and bottom
2472 * for anything behind the bridge. */
2473 temp_resources.bus_head = bus_node;
2474 temp_resources.io_head = io_node;
2475 temp_resources.mem_head = mem_node;
2476 temp_resources.p_mem_head = p_mem_node;
2477 temp_resources.irqs = &irqs;
2479 /* Make copies of the nodes we are going to pass down so that
2480 * if there is a problem,we can just use these to free resources
2482 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2483 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2484 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2485 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2487 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2488 kfree(hold_bus_node);
2489 kfree(hold_IO_node);
2490 kfree(hold_mem_node);
2491 kfree(hold_p_mem_node);
2493 return 1;
2496 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2498 bus_node->base += 1;
2499 bus_node->length -= 1;
2500 bus_node->next = NULL;
2502 /* If we have IO resources copy them and fill in the bridge's
2503 * IO range registers */
2504 if (io_node) {
2505 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2506 io_node->next = NULL;
2508 /* set IO base and Limit registers */
2509 temp_byte = io_node->base >> 8;
2510 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2512 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2513 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2514 } else {
2515 kfree(hold_IO_node);
2516 hold_IO_node = NULL;
2519 /* If we have memory resources copy them and fill in the
2520 * bridge's memory range registers. Otherwise, fill in the
2521 * range registers with values that disable them. */
2522 if (mem_node) {
2523 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2524 mem_node->next = NULL;
2526 /* set Mem base and Limit registers */
2527 temp_word = mem_node->base >> 16;
2528 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2530 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2531 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2532 } else {
2533 temp_word = 0xFFFF;
2534 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2536 temp_word = 0x0000;
2537 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2539 kfree(hold_mem_node);
2540 hold_mem_node = NULL;
2543 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2544 p_mem_node->next = NULL;
2546 /* set Pre Mem base and Limit registers */
2547 temp_word = p_mem_node->base >> 16;
2548 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2550 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2551 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2553 /* Adjust this to compensate for extra adjustment in first loop
2555 irqs.barber_pole--;
2557 rc = 0;
2559 /* Here we actually find the devices and configure them */
2560 for (device = 0; (device <= 0x1F) && !rc; device++) {
2561 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2563 ID = 0xFFFFFFFF;
2564 pci_bus->number = hold_bus_node->base;
2565 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2566 pci_bus->number = func->bus;
2568 if (ID != 0xFFFFFFFF) { /* device present */
2569 /* Setup slot structure. */
2570 new_slot = cpqhp_slot_create(hold_bus_node->base);
2572 if (new_slot == NULL) {
2573 rc = -ENOMEM;
2574 continue;
2577 new_slot->bus = hold_bus_node->base;
2578 new_slot->device = device;
2579 new_slot->function = 0;
2580 new_slot->is_a_board = 1;
2581 new_slot->status = 0;
2583 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2584 dbg("configure_new_device rc=0x%x\n",rc);
2585 } /* End of IF (device in slot?) */
2586 } /* End of FOR loop */
2588 if (rc)
2589 goto free_and_out;
2590 /* save the interrupt routing information */
2591 if (resources->irqs) {
2592 resources->irqs->interrupt[0] = irqs.interrupt[0];
2593 resources->irqs->interrupt[1] = irqs.interrupt[1];
2594 resources->irqs->interrupt[2] = irqs.interrupt[2];
2595 resources->irqs->interrupt[3] = irqs.interrupt[3];
2596 resources->irqs->valid_INT = irqs.valid_INT;
2597 } else if (!behind_bridge) {
2598 /* We need to hook up the interrupts here */
2599 for (cloop = 0; cloop < 4; cloop++) {
2600 if (irqs.valid_INT & (0x01 << cloop)) {
2601 rc = cpqhp_set_irq(func->bus, func->device,
2602 cloop + 1, irqs.interrupt[cloop]);
2603 if (rc)
2604 goto free_and_out;
2606 } /* end of for loop */
2608 /* Return unused bus resources
2609 * First use the temporary node to store information for
2610 * the board */
2611 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2612 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2614 hold_bus_node->next = func->bus_head;
2615 func->bus_head = hold_bus_node;
2617 temp_byte = temp_resources.bus_head->base - 1;
2619 /* set subordinate bus */
2620 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2622 if (temp_resources.bus_head->length == 0) {
2623 kfree(temp_resources.bus_head);
2624 temp_resources.bus_head = NULL;
2625 } else {
2626 return_resource(&(resources->bus_head), temp_resources.bus_head);
2630 /* If we have IO space available and there is some left,
2631 * return the unused portion */
2632 if (hold_IO_node && temp_resources.io_head) {
2633 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2634 &hold_IO_node, 0x1000);
2636 /* Check if we were able to split something off */
2637 if (io_node) {
2638 hold_IO_node->base = io_node->base + io_node->length;
2640 temp_byte = (hold_IO_node->base) >> 8;
2641 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2643 return_resource(&(resources->io_head), io_node);
2646 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2648 /* Check if we were able to split something off */
2649 if (io_node) {
2650 /* First use the temporary node to store
2651 * information for the board */
2652 hold_IO_node->length = io_node->base - hold_IO_node->base;
2654 /* If we used any, add it to the board's list */
2655 if (hold_IO_node->length) {
2656 hold_IO_node->next = func->io_head;
2657 func->io_head = hold_IO_node;
2659 temp_byte = (io_node->base - 1) >> 8;
2660 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2662 return_resource(&(resources->io_head), io_node);
2663 } else {
2664 /* it doesn't need any IO */
2665 temp_word = 0x0000;
2666 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2668 return_resource(&(resources->io_head), io_node);
2669 kfree(hold_IO_node);
2671 } else {
2672 /* it used most of the range */
2673 hold_IO_node->next = func->io_head;
2674 func->io_head = hold_IO_node;
2676 } else if (hold_IO_node) {
2677 /* it used the whole range */
2678 hold_IO_node->next = func->io_head;
2679 func->io_head = hold_IO_node;
2681 /* If we have memory space available and there is some left,
2682 * return the unused portion */
2683 if (hold_mem_node && temp_resources.mem_head) {
2684 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2685 &hold_mem_node, 0x100000);
2687 /* Check if we were able to split something off */
2688 if (mem_node) {
2689 hold_mem_node->base = mem_node->base + mem_node->length;
2691 temp_word = (hold_mem_node->base) >> 16;
2692 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2694 return_resource(&(resources->mem_head), mem_node);
2697 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2699 /* Check if we were able to split something off */
2700 if (mem_node) {
2701 /* First use the temporary node to store
2702 * information for the board */
2703 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2705 if (hold_mem_node->length) {
2706 hold_mem_node->next = func->mem_head;
2707 func->mem_head = hold_mem_node;
2709 /* configure end address */
2710 temp_word = (mem_node->base - 1) >> 16;
2711 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2713 /* Return unused resources to the pool */
2714 return_resource(&(resources->mem_head), mem_node);
2715 } else {
2716 /* it doesn't need any Mem */
2717 temp_word = 0x0000;
2718 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2720 return_resource(&(resources->mem_head), mem_node);
2721 kfree(hold_mem_node);
2723 } else {
2724 /* it used most of the range */
2725 hold_mem_node->next = func->mem_head;
2726 func->mem_head = hold_mem_node;
2728 } else if (hold_mem_node) {
2729 /* it used the whole range */
2730 hold_mem_node->next = func->mem_head;
2731 func->mem_head = hold_mem_node;
2733 /* If we have prefetchable memory space available and there
2734 * is some left at the end, return the unused portion */
2735 if (hold_p_mem_node && temp_resources.p_mem_head) {
2736 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2737 &hold_p_mem_node, 0x100000);
2739 /* Check if we were able to split something off */
2740 if (p_mem_node) {
2741 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2743 temp_word = (hold_p_mem_node->base) >> 16;
2744 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2746 return_resource(&(resources->p_mem_head), p_mem_node);
2749 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2751 /* Check if we were able to split something off */
2752 if (p_mem_node) {
2753 /* First use the temporary node to store
2754 * information for the board */
2755 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2757 /* If we used any, add it to the board's list */
2758 if (hold_p_mem_node->length) {
2759 hold_p_mem_node->next = func->p_mem_head;
2760 func->p_mem_head = hold_p_mem_node;
2762 temp_word = (p_mem_node->base - 1) >> 16;
2763 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2765 return_resource(&(resources->p_mem_head), p_mem_node);
2766 } else {
2767 /* it doesn't need any PMem */
2768 temp_word = 0x0000;
2769 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2771 return_resource(&(resources->p_mem_head), p_mem_node);
2772 kfree(hold_p_mem_node);
2774 } else {
2775 /* it used the most of the range */
2776 hold_p_mem_node->next = func->p_mem_head;
2777 func->p_mem_head = hold_p_mem_node;
2779 } else if (hold_p_mem_node) {
2780 /* it used the whole range */
2781 hold_p_mem_node->next = func->p_mem_head;
2782 func->p_mem_head = hold_p_mem_node;
2784 /* We should be configuring an IRQ and the bridge's base address
2785 * registers if it needs them. Although we have never seen such
2786 * a device */
2788 /* enable card */
2789 command = 0x0157; /* = PCI_COMMAND_IO |
2790 * PCI_COMMAND_MEMORY |
2791 * PCI_COMMAND_MASTER |
2792 * PCI_COMMAND_INVALIDATE |
2793 * PCI_COMMAND_PARITY |
2794 * PCI_COMMAND_SERR */
2795 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2797 /* set Bridge Control Register */
2798 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2799 * PCI_BRIDGE_CTL_SERR |
2800 * PCI_BRIDGE_CTL_NO_ISA */
2801 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2802 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2803 /* Standard device */
2804 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2806 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2807 /* Display (video) adapter (not supported) */
2808 return DEVICE_TYPE_NOT_SUPPORTED;
2810 /* Figure out IO and memory needs */
2811 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2812 temp_register = 0xFFFFFFFF;
2814 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2815 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2817 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2818 dbg("CND: base = 0x%x\n", temp_register);
2820 if (temp_register) { /* If this register is implemented */
2821 if ((temp_register & 0x03L) == 0x01) {
2822 /* Map IO */
2824 /* set base = amount of IO space */
2825 base = temp_register & 0xFFFFFFFC;
2826 base = ~base + 1;
2828 dbg("CND: length = 0x%x\n", base);
2829 io_node = get_io_resource(&(resources->io_head), base);
2830 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2831 io_node->base, io_node->length, io_node->next);
2832 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2834 /* allocate the resource to the board */
2835 if (io_node) {
2836 base = io_node->base;
2838 io_node->next = func->io_head;
2839 func->io_head = io_node;
2840 } else
2841 return -ENOMEM;
2842 } else if ((temp_register & 0x0BL) == 0x08) {
2843 /* Map prefetchable memory */
2844 base = temp_register & 0xFFFFFFF0;
2845 base = ~base + 1;
2847 dbg("CND: length = 0x%x\n", base);
2848 p_mem_node = get_resource(&(resources->p_mem_head), base);
2850 /* allocate the resource to the board */
2851 if (p_mem_node) {
2852 base = p_mem_node->base;
2854 p_mem_node->next = func->p_mem_head;
2855 func->p_mem_head = p_mem_node;
2856 } else
2857 return -ENOMEM;
2858 } else if ((temp_register & 0x0BL) == 0x00) {
2859 /* Map memory */
2860 base = temp_register & 0xFFFFFFF0;
2861 base = ~base + 1;
2863 dbg("CND: length = 0x%x\n", base);
2864 mem_node = get_resource(&(resources->mem_head), base);
2866 /* allocate the resource to the board */
2867 if (mem_node) {
2868 base = mem_node->base;
2870 mem_node->next = func->mem_head;
2871 func->mem_head = mem_node;
2872 } else
2873 return -ENOMEM;
2874 } else if ((temp_register & 0x0BL) == 0x04) {
2875 /* Map memory */
2876 base = temp_register & 0xFFFFFFF0;
2877 base = ~base + 1;
2879 dbg("CND: length = 0x%x\n", base);
2880 mem_node = get_resource(&(resources->mem_head), base);
2882 /* allocate the resource to the board */
2883 if (mem_node) {
2884 base = mem_node->base;
2886 mem_node->next = func->mem_head;
2887 func->mem_head = mem_node;
2888 } else
2889 return -ENOMEM;
2890 } else if ((temp_register & 0x0BL) == 0x06) {
2891 /* Those bits are reserved, we can't handle this */
2892 return 1;
2893 } else {
2894 /* Requesting space below 1M */
2895 return NOT_ENOUGH_RESOURCES;
2898 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2900 /* Check for 64-bit base */
2901 if ((temp_register & 0x07L) == 0x04) {
2902 cloop += 4;
2904 /* Upper 32 bits of address always zero
2905 * on today's systems */
2906 base = 0;
2907 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2910 } /* End of base register loop */
2911 if (cpqhp_legacy_mode) {
2912 /* Figure out which interrupt pin this function uses */
2913 rc = pci_bus_read_config_byte (pci_bus, devfn,
2914 PCI_INTERRUPT_PIN, &temp_byte);
2916 /* If this function needs an interrupt and we are behind
2917 * a bridge and the pin is tied to something that's
2918 * alread mapped, set this one the same */
2919 if (temp_byte && resources->irqs &&
2920 (resources->irqs->valid_INT &
2921 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2922 /* We have to share with something already set up */
2923 IRQ = resources->irqs->interrupt[(temp_byte +
2924 resources->irqs->barber_pole - 1) & 0x03];
2925 } else {
2926 /* Program IRQ based on card type */
2927 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2929 if (class_code == PCI_BASE_CLASS_STORAGE)
2930 IRQ = cpqhp_disk_irq;
2931 else
2932 IRQ = cpqhp_nic_irq;
2935 /* IRQ Line */
2936 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2939 if (!behind_bridge) {
2940 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
2941 if (rc)
2942 return 1;
2943 } else {
2944 /* TBD - this code may also belong in the other clause
2945 * of this If statement */
2946 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2947 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2950 /* Latency Timer */
2951 temp_byte = 0x40;
2952 rc = pci_bus_write_config_byte(pci_bus, devfn,
2953 PCI_LATENCY_TIMER, temp_byte);
2955 /* Cache Line size */
2956 temp_byte = 0x08;
2957 rc = pci_bus_write_config_byte(pci_bus, devfn,
2958 PCI_CACHE_LINE_SIZE, temp_byte);
2960 /* disable ROM base Address */
2961 temp_dword = 0x00L;
2962 rc = pci_bus_write_config_word(pci_bus, devfn,
2963 PCI_ROM_ADDRESS, temp_dword);
2965 /* enable card */
2966 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2967 * PCI_COMMAND_MEMORY |
2968 * PCI_COMMAND_MASTER |
2969 * PCI_COMMAND_INVALIDATE |
2970 * PCI_COMMAND_PARITY |
2971 * PCI_COMMAND_SERR */
2972 rc = pci_bus_write_config_word (pci_bus, devfn,
2973 PCI_COMMAND, temp_word);
2974 } else { /* End of Not-A-Bridge else */
2975 /* It's some strange type of PCI adapter (Cardbus?) */
2976 return DEVICE_TYPE_NOT_SUPPORTED;
2979 func->configured = 1;
2981 return 0;
2982 free_and_out:
2983 cpqhp_destroy_resource_list (&temp_resources);
2985 return_resource(&(resources-> bus_head), hold_bus_node);
2986 return_resource(&(resources-> io_head), hold_IO_node);
2987 return_resource(&(resources-> mem_head), hold_mem_node);
2988 return_resource(&(resources-> p_mem_head), hold_p_mem_node);
2989 return rc;