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