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