xref: /OK3568_Linux_fs/kernel/drivers/pci/hotplug/ibmphp_pci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * IBM Hot Plug Controller Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Written By: Irene Zubarev, IBM Corporation
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
8*4882a593Smuzhiyun  * Copyright (C) 2001,2002 IBM Corp.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * All rights reserved.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Send feedback to <gregkh@us.ibm.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/pci.h>
19*4882a593Smuzhiyun #include <linux/list.h>
20*4882a593Smuzhiyun #include "ibmphp.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static int configure_device(struct pci_func *);
24*4882a593Smuzhiyun static int configure_bridge(struct pci_func **, u8);
25*4882a593Smuzhiyun static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
26*4882a593Smuzhiyun static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
27*4882a593Smuzhiyun static u8 find_sec_number(u8 primary_busno, u8 slotno);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * NOTE..... If BIOS doesn't provide default routing, we assign:
31*4882a593Smuzhiyun  * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
32*4882a593Smuzhiyun  * If adapter is bridged, then we assign 11 to it and devices behind it.
33*4882a593Smuzhiyun  * We also assign the same irq numbers for multi function devices.
34*4882a593Smuzhiyun  * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
35*4882a593Smuzhiyun  */
assign_alt_irq(struct pci_func * cur_func,u8 class_code)36*4882a593Smuzhiyun static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	int j;
39*4882a593Smuzhiyun 	for (j = 0; j < 4; j++) {
40*4882a593Smuzhiyun 		if (cur_func->irq[j] == 0xff) {
41*4882a593Smuzhiyun 			switch (class_code) {
42*4882a593Smuzhiyun 				case PCI_BASE_CLASS_STORAGE:
43*4882a593Smuzhiyun 					cur_func->irq[j] = SCSI_IRQ;
44*4882a593Smuzhiyun 					break;
45*4882a593Smuzhiyun 				case PCI_BASE_CLASS_NETWORK:
46*4882a593Smuzhiyun 					cur_func->irq[j] = LAN_IRQ;
47*4882a593Smuzhiyun 					break;
48*4882a593Smuzhiyun 				default:
49*4882a593Smuzhiyun 					cur_func->irq[j] = OTHER_IRQ;
50*4882a593Smuzhiyun 					break;
51*4882a593Smuzhiyun 			}
52*4882a593Smuzhiyun 		}
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun  * Configures the device to be added (will allocate needed resources if it
58*4882a593Smuzhiyun  * can), the device can be a bridge or a regular pci device, can also be
59*4882a593Smuzhiyun  * multi-functional
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Input: function to be added
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * TO DO:  The error case with Multifunction device or multi function bridge,
64*4882a593Smuzhiyun  * if there is an error, will need to go through all previous functions and
65*4882a593Smuzhiyun  * unconfigure....or can add some code into unconfigure_card....
66*4882a593Smuzhiyun  */
ibmphp_configure_card(struct pci_func * func,u8 slotno)67*4882a593Smuzhiyun int ibmphp_configure_card(struct pci_func *func, u8 slotno)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	u16 vendor_id;
70*4882a593Smuzhiyun 	u32 class;
71*4882a593Smuzhiyun 	u8 class_code;
72*4882a593Smuzhiyun 	u8 hdr_type, device, sec_number;
73*4882a593Smuzhiyun 	u8 function;
74*4882a593Smuzhiyun 	struct pci_func *newfunc;	/* for multi devices */
75*4882a593Smuzhiyun 	struct pci_func *cur_func, *prev_func;
76*4882a593Smuzhiyun 	int rc, i, j;
77*4882a593Smuzhiyun 	int cleanup_count;
78*4882a593Smuzhiyun 	u8 flag;
79*4882a593Smuzhiyun 	u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	debug("inside configure_card, func->busno = %x\n", func->busno);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	device = func->device;
84*4882a593Smuzhiyun 	cur_func = func;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/* We only get bus and device from IRQ routing table.  So at this point,
87*4882a593Smuzhiyun 	 * func->busno is correct, and func->device contains only device (at the 5
88*4882a593Smuzhiyun 	 * highest bits)
89*4882a593Smuzhiyun 	 */
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/* For every function on the card */
92*4882a593Smuzhiyun 	for (function = 0x00; function < 0x08; function++) {
93*4882a593Smuzhiyun 		unsigned int devfn = PCI_DEVFN(device, function);
94*4882a593Smuzhiyun 		ibmphp_pci_bus->number = cur_func->busno;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 		cur_func->function = function;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
99*4882a593Smuzhiyun 			cur_func->busno, cur_func->device, cur_func->function);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		debug("vendor_id is %x\n", vendor_id);
104*4882a593Smuzhiyun 		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
105*4882a593Smuzhiyun 			/* found correct device!!! */
106*4882a593Smuzhiyun 			debug("found valid device, vendor_id = %x\n", vendor_id);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 			++valid_device;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 			/* header: x x x x x x x x
111*4882a593Smuzhiyun 			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
112*4882a593Smuzhiyun 			 *         |_=> 0 = single function device, 1 = multi-function device
113*4882a593Smuzhiyun 			 */
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
116*4882a593Smuzhiyun 			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 			class_code = class >> 24;
119*4882a593Smuzhiyun 			debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
120*4882a593Smuzhiyun 			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
121*4882a593Smuzhiyun 			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
122*4882a593Smuzhiyun 				err("The device %x is VGA compatible and as is not supported for hot plugging. "
123*4882a593Smuzhiyun 				     "Please choose another device.\n", cur_func->device);
124*4882a593Smuzhiyun 				return -ENODEV;
125*4882a593Smuzhiyun 			} else if (class == PCI_CLASS_DISPLAY_VGA) {
126*4882a593Smuzhiyun 				err("The device %x is not supported for hot plugging. Please choose another device.\n",
127*4882a593Smuzhiyun 				     cur_func->device);
128*4882a593Smuzhiyun 				return -ENODEV;
129*4882a593Smuzhiyun 			}
130*4882a593Smuzhiyun 			switch (hdr_type) {
131*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_NORMAL:
132*4882a593Smuzhiyun 					debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
133*4882a593Smuzhiyun 					assign_alt_irq(cur_func, class_code);
134*4882a593Smuzhiyun 					rc = configure_device(cur_func);
135*4882a593Smuzhiyun 					if (rc < 0) {
136*4882a593Smuzhiyun 						/* We need to do this in case some other BARs were properly inserted */
137*4882a593Smuzhiyun 						err("was not able to configure devfunc %x on bus %x.\n",
138*4882a593Smuzhiyun 						     cur_func->device, cur_func->busno);
139*4882a593Smuzhiyun 						cleanup_count = 6;
140*4882a593Smuzhiyun 						goto error;
141*4882a593Smuzhiyun 					}
142*4882a593Smuzhiyun 					cur_func->next = NULL;
143*4882a593Smuzhiyun 					function = 0x8;
144*4882a593Smuzhiyun 					break;
145*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_MULTIDEVICE:
146*4882a593Smuzhiyun 					assign_alt_irq(cur_func, class_code);
147*4882a593Smuzhiyun 					rc = configure_device(cur_func);
148*4882a593Smuzhiyun 					if (rc < 0) {
149*4882a593Smuzhiyun 						/* We need to do this in case some other BARs were properly inserted */
150*4882a593Smuzhiyun 						err("was not able to configure devfunc %x on bus %x...bailing out\n",
151*4882a593Smuzhiyun 						     cur_func->device, cur_func->busno);
152*4882a593Smuzhiyun 						cleanup_count = 6;
153*4882a593Smuzhiyun 						goto error;
154*4882a593Smuzhiyun 					}
155*4882a593Smuzhiyun 					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
156*4882a593Smuzhiyun 					if (!newfunc)
157*4882a593Smuzhiyun 						return -ENOMEM;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 					newfunc->busno = cur_func->busno;
160*4882a593Smuzhiyun 					newfunc->device = device;
161*4882a593Smuzhiyun 					cur_func->next = newfunc;
162*4882a593Smuzhiyun 					cur_func = newfunc;
163*4882a593Smuzhiyun 					for (j = 0; j < 4; j++)
164*4882a593Smuzhiyun 						newfunc->irq[j] = cur_func->irq[j];
165*4882a593Smuzhiyun 					break;
166*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_MULTIBRIDGE:
167*4882a593Smuzhiyun 					class >>= 8;
168*4882a593Smuzhiyun 					if (class != PCI_CLASS_BRIDGE_PCI) {
169*4882a593Smuzhiyun 						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
170*4882a593Smuzhiyun 						     cur_func->device);
171*4882a593Smuzhiyun 						return -ENODEV;
172*4882a593Smuzhiyun 					}
173*4882a593Smuzhiyun 					assign_alt_irq(cur_func, class_code);
174*4882a593Smuzhiyun 					rc = configure_bridge(&cur_func, slotno);
175*4882a593Smuzhiyun 					if (rc == -ENODEV) {
176*4882a593Smuzhiyun 						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
177*4882a593Smuzhiyun 						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
178*4882a593Smuzhiyun 						return rc;
179*4882a593Smuzhiyun 					}
180*4882a593Smuzhiyun 					if (rc) {
181*4882a593Smuzhiyun 						/* We need to do this in case some other BARs were properly inserted */
182*4882a593Smuzhiyun 						err("was not able to hot-add PPB properly.\n");
183*4882a593Smuzhiyun 						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
184*4882a593Smuzhiyun 						cleanup_count = 2;
185*4882a593Smuzhiyun 						goto error;
186*4882a593Smuzhiyun 					}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
189*4882a593Smuzhiyun 					flag = 0;
190*4882a593Smuzhiyun 					for (i = 0; i < 32; i++) {
191*4882a593Smuzhiyun 						if (func->devices[i]) {
192*4882a593Smuzhiyun 							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
193*4882a593Smuzhiyun 							if (!newfunc)
194*4882a593Smuzhiyun 								return -ENOMEM;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 							newfunc->busno = sec_number;
197*4882a593Smuzhiyun 							newfunc->device = (u8) i;
198*4882a593Smuzhiyun 							for (j = 0; j < 4; j++)
199*4882a593Smuzhiyun 								newfunc->irq[j] = cur_func->irq[j];
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 							if (flag) {
202*4882a593Smuzhiyun 								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
203*4882a593Smuzhiyun 								prev_func->next = newfunc;
204*4882a593Smuzhiyun 							} else
205*4882a593Smuzhiyun 								cur_func->next = newfunc;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 							rc = ibmphp_configure_card(newfunc, slotno);
208*4882a593Smuzhiyun 							/* This could only happen if kmalloc failed */
209*4882a593Smuzhiyun 							if (rc) {
210*4882a593Smuzhiyun 								/* We need to do this in case bridge itself got configured properly, but devices behind it failed */
211*4882a593Smuzhiyun 								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
212*4882a593Smuzhiyun 								cleanup_count = 2;
213*4882a593Smuzhiyun 								goto error;
214*4882a593Smuzhiyun 							}
215*4882a593Smuzhiyun 							flag = 1;
216*4882a593Smuzhiyun 						}
217*4882a593Smuzhiyun 					}
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
220*4882a593Smuzhiyun 					if (!newfunc)
221*4882a593Smuzhiyun 						return -ENOMEM;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 					newfunc->busno = cur_func->busno;
224*4882a593Smuzhiyun 					newfunc->device = device;
225*4882a593Smuzhiyun 					for (j = 0; j < 4; j++)
226*4882a593Smuzhiyun 						newfunc->irq[j] = cur_func->irq[j];
227*4882a593Smuzhiyun 					for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
228*4882a593Smuzhiyun 					prev_func->next = newfunc;
229*4882a593Smuzhiyun 					cur_func = newfunc;
230*4882a593Smuzhiyun 					break;
231*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_BRIDGE:
232*4882a593Smuzhiyun 					class >>= 8;
233*4882a593Smuzhiyun 					debug("class now is %x\n", class);
234*4882a593Smuzhiyun 					if (class != PCI_CLASS_BRIDGE_PCI) {
235*4882a593Smuzhiyun 						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
236*4882a593Smuzhiyun 						     cur_func->device);
237*4882a593Smuzhiyun 						return -ENODEV;
238*4882a593Smuzhiyun 					}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 					assign_alt_irq(cur_func, class_code);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 					debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
243*4882a593Smuzhiyun 					rc = configure_bridge(&cur_func, slotno);
244*4882a593Smuzhiyun 					if (rc == -ENODEV) {
245*4882a593Smuzhiyun 						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
246*4882a593Smuzhiyun 						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
247*4882a593Smuzhiyun 						return rc;
248*4882a593Smuzhiyun 					}
249*4882a593Smuzhiyun 					if (rc) {
250*4882a593Smuzhiyun 						/* We need to do this in case some other BARs were properly inserted */
251*4882a593Smuzhiyun 						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
252*4882a593Smuzhiyun 						err("was not able to hot-add PPB properly.\n");
253*4882a593Smuzhiyun 						cleanup_count = 2;
254*4882a593Smuzhiyun 						goto error;
255*4882a593Smuzhiyun 					}
256*4882a593Smuzhiyun 					debug("cur_func->busno = %x, device = %x, function = %x\n",
257*4882a593Smuzhiyun 						cur_func->busno, device, function);
258*4882a593Smuzhiyun 					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
259*4882a593Smuzhiyun 					debug("after configuring bridge..., sec_number = %x\n", sec_number);
260*4882a593Smuzhiyun 					flag = 0;
261*4882a593Smuzhiyun 					for (i = 0; i < 32; i++) {
262*4882a593Smuzhiyun 						if (func->devices[i]) {
263*4882a593Smuzhiyun 							debug("inside for loop, device is %x\n", i);
264*4882a593Smuzhiyun 							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
265*4882a593Smuzhiyun 							if (!newfunc)
266*4882a593Smuzhiyun 								return -ENOMEM;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 							newfunc->busno = sec_number;
269*4882a593Smuzhiyun 							newfunc->device = (u8) i;
270*4882a593Smuzhiyun 							for (j = 0; j < 4; j++)
271*4882a593Smuzhiyun 								newfunc->irq[j] = cur_func->irq[j];
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 							if (flag) {
274*4882a593Smuzhiyun 								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
275*4882a593Smuzhiyun 								prev_func->next = newfunc;
276*4882a593Smuzhiyun 							} else
277*4882a593Smuzhiyun 								cur_func->next = newfunc;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 							rc = ibmphp_configure_card(newfunc, slotno);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 							/* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
282*4882a593Smuzhiyun 							if (rc) {
283*4882a593Smuzhiyun 								/* We need to do this in case some other BARs were properly inserted */
284*4882a593Smuzhiyun 								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
285*4882a593Smuzhiyun 								cleanup_count = 2;
286*4882a593Smuzhiyun 								goto error;
287*4882a593Smuzhiyun 							}
288*4882a593Smuzhiyun 							flag = 1;
289*4882a593Smuzhiyun 						}
290*4882a593Smuzhiyun 					}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 					function = 0x8;
293*4882a593Smuzhiyun 					break;
294*4882a593Smuzhiyun 				default:
295*4882a593Smuzhiyun 					err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
296*4882a593Smuzhiyun 					return -ENXIO;
297*4882a593Smuzhiyun 					break;
298*4882a593Smuzhiyun 			}	/* end of switch */
299*4882a593Smuzhiyun 		}	/* end of valid device */
300*4882a593Smuzhiyun 	}	/* end of for */
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (!valid_device) {
303*4882a593Smuzhiyun 		err("Cannot find any valid devices on the card.  Or unable to read from card.\n");
304*4882a593Smuzhiyun 		return -ENODEV;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	return 0;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun error:
310*4882a593Smuzhiyun 	for (i = 0; i < cleanup_count; i++) {
311*4882a593Smuzhiyun 		if (cur_func->io[i]) {
312*4882a593Smuzhiyun 			ibmphp_remove_resource(cur_func->io[i]);
313*4882a593Smuzhiyun 			cur_func->io[i] = NULL;
314*4882a593Smuzhiyun 		} else if (cur_func->pfmem[i]) {
315*4882a593Smuzhiyun 			ibmphp_remove_resource(cur_func->pfmem[i]);
316*4882a593Smuzhiyun 			cur_func->pfmem[i] = NULL;
317*4882a593Smuzhiyun 		} else if (cur_func->mem[i]) {
318*4882a593Smuzhiyun 			ibmphp_remove_resource(cur_func->mem[i]);
319*4882a593Smuzhiyun 			cur_func->mem[i] = NULL;
320*4882a593Smuzhiyun 		}
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 	return rc;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun  * This function configures the pci BARs of a single device.
327*4882a593Smuzhiyun  * Input: pointer to the pci_func
328*4882a593Smuzhiyun  * Output: configured PCI, 0, or error
329*4882a593Smuzhiyun  */
configure_device(struct pci_func * func)330*4882a593Smuzhiyun static int configure_device(struct pci_func *func)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	u32 bar[6];
333*4882a593Smuzhiyun 	u32 address[] = {
334*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0,
335*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_1,
336*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_2,
337*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_3,
338*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_4,
339*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_5,
340*4882a593Smuzhiyun 		0
341*4882a593Smuzhiyun 	};
342*4882a593Smuzhiyun 	u8 irq;
343*4882a593Smuzhiyun 	int count;
344*4882a593Smuzhiyun 	int len[6];
345*4882a593Smuzhiyun 	struct resource_node *io[6];
346*4882a593Smuzhiyun 	struct resource_node *mem[6];
347*4882a593Smuzhiyun 	struct resource_node *mem_tmp;
348*4882a593Smuzhiyun 	struct resource_node *pfmem[6];
349*4882a593Smuzhiyun 	unsigned int devfn;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	debug("%s - inside\n", __func__);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	devfn = PCI_DEVFN(func->device, func->function);
354*4882a593Smuzhiyun 	ibmphp_pci_bus->number = func->busno;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	for (count = 0; address[count]; count++) {	/* for 6 BARs */
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/* not sure if i need this.  per scott, said maybe need * something like this
359*4882a593Smuzhiyun 		   if devices don't adhere 100% to the spec, so don't want to write
360*4882a593Smuzhiyun 		   to the reserved bits
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 		pcibios_read_config_byte(cur_func->busno, cur_func->device,
363*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
364*4882a593Smuzhiyun 		if (tmp & 0x01) // IO
365*4882a593Smuzhiyun 			pcibios_write_config_dword(cur_func->busno, cur_func->device,
366*4882a593Smuzhiyun 			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
367*4882a593Smuzhiyun 		else  // Memory
368*4882a593Smuzhiyun 			pcibios_write_config_dword(cur_func->busno, cur_func->device,
369*4882a593Smuzhiyun 			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
370*4882a593Smuzhiyun 		 */
371*4882a593Smuzhiyun 		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
372*4882a593Smuzhiyun 		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 		if (!bar[count])	/* This BAR is not implemented */
375*4882a593Smuzhiyun 			continue;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
380*4882a593Smuzhiyun 			/* This is IO */
381*4882a593Smuzhiyun 			debug("inside IO SPACE\n");
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 			len[count] = bar[count] & 0xFFFFFFFC;
384*4882a593Smuzhiyun 			len[count] = ~len[count] + 1;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 			debug("len[count] in IO %x, count %d\n", len[count], count);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 			io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 			if (!io[count])
391*4882a593Smuzhiyun 				return -ENOMEM;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 			io[count]->type = IO;
394*4882a593Smuzhiyun 			io[count]->busno = func->busno;
395*4882a593Smuzhiyun 			io[count]->devfunc = PCI_DEVFN(func->device, func->function);
396*4882a593Smuzhiyun 			io[count]->len = len[count];
397*4882a593Smuzhiyun 			if (ibmphp_check_resource(io[count], 0) == 0) {
398*4882a593Smuzhiyun 				ibmphp_add_resource(io[count]);
399*4882a593Smuzhiyun 				func->io[count] = io[count];
400*4882a593Smuzhiyun 			} else {
401*4882a593Smuzhiyun 				err("cannot allocate requested io for bus %x device %x function %x len %x\n",
402*4882a593Smuzhiyun 				     func->busno, func->device, func->function, len[count]);
403*4882a593Smuzhiyun 				kfree(io[count]);
404*4882a593Smuzhiyun 				return -EIO;
405*4882a593Smuzhiyun 			}
406*4882a593Smuzhiyun 			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 			/* _______________This is for debugging purposes only_____________________ */
409*4882a593Smuzhiyun 			debug("b4 writing, the IO address is %x\n", func->io[count]->start);
410*4882a593Smuzhiyun 			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
411*4882a593Smuzhiyun 			debug("after writing.... the start address is %x\n", bar[count]);
412*4882a593Smuzhiyun 			/* _________________________________________________________________________*/
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 		} else {
415*4882a593Smuzhiyun 			/* This is Memory */
416*4882a593Smuzhiyun 			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
417*4882a593Smuzhiyun 				/* pfmem */
418*4882a593Smuzhiyun 				debug("PFMEM SPACE\n");
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 				len[count] = bar[count] & 0xFFFFFFF0;
421*4882a593Smuzhiyun 				len[count] = ~len[count] + 1;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 				debug("len[count] in PFMEM %x, count %d\n", len[count], count);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 				pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
426*4882a593Smuzhiyun 				if (!pfmem[count])
427*4882a593Smuzhiyun 					return -ENOMEM;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 				pfmem[count]->type = PFMEM;
430*4882a593Smuzhiyun 				pfmem[count]->busno = func->busno;
431*4882a593Smuzhiyun 				pfmem[count]->devfunc = PCI_DEVFN(func->device,
432*4882a593Smuzhiyun 							func->function);
433*4882a593Smuzhiyun 				pfmem[count]->len = len[count];
434*4882a593Smuzhiyun 				pfmem[count]->fromMem = 0;
435*4882a593Smuzhiyun 				if (ibmphp_check_resource(pfmem[count], 0) == 0) {
436*4882a593Smuzhiyun 					ibmphp_add_resource(pfmem[count]);
437*4882a593Smuzhiyun 					func->pfmem[count] = pfmem[count];
438*4882a593Smuzhiyun 				} else {
439*4882a593Smuzhiyun 					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
440*4882a593Smuzhiyun 					if (!mem_tmp) {
441*4882a593Smuzhiyun 						kfree(pfmem[count]);
442*4882a593Smuzhiyun 						return -ENOMEM;
443*4882a593Smuzhiyun 					}
444*4882a593Smuzhiyun 					mem_tmp->type = MEM;
445*4882a593Smuzhiyun 					mem_tmp->busno = pfmem[count]->busno;
446*4882a593Smuzhiyun 					mem_tmp->devfunc = pfmem[count]->devfunc;
447*4882a593Smuzhiyun 					mem_tmp->len = pfmem[count]->len;
448*4882a593Smuzhiyun 					debug("there's no pfmem... going into mem.\n");
449*4882a593Smuzhiyun 					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
450*4882a593Smuzhiyun 						ibmphp_add_resource(mem_tmp);
451*4882a593Smuzhiyun 						pfmem[count]->fromMem = 1;
452*4882a593Smuzhiyun 						pfmem[count]->rangeno = mem_tmp->rangeno;
453*4882a593Smuzhiyun 						pfmem[count]->start = mem_tmp->start;
454*4882a593Smuzhiyun 						pfmem[count]->end = mem_tmp->end;
455*4882a593Smuzhiyun 						ibmphp_add_pfmem_from_mem(pfmem[count]);
456*4882a593Smuzhiyun 						func->pfmem[count] = pfmem[count];
457*4882a593Smuzhiyun 					} else {
458*4882a593Smuzhiyun 						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
459*4882a593Smuzhiyun 						     func->busno, func->device, len[count]);
460*4882a593Smuzhiyun 						kfree(mem_tmp);
461*4882a593Smuzhiyun 						kfree(pfmem[count]);
462*4882a593Smuzhiyun 						return -EIO;
463*4882a593Smuzhiyun 					}
464*4882a593Smuzhiyun 				}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 				/*_______________This is for debugging purposes only______________________________*/
469*4882a593Smuzhiyun 				debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
470*4882a593Smuzhiyun 				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
471*4882a593Smuzhiyun 				debug("after writing, start address is %x\n", bar[count]);
472*4882a593Smuzhiyun 				/*_________________________________________________________________________________*/
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {	/* takes up another dword */
475*4882a593Smuzhiyun 					debug("inside the mem 64 case, count %d\n", count);
476*4882a593Smuzhiyun 					count += 1;
477*4882a593Smuzhiyun 					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
478*4882a593Smuzhiyun 					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
479*4882a593Smuzhiyun 				}
480*4882a593Smuzhiyun 			} else {
481*4882a593Smuzhiyun 				/* regular memory */
482*4882a593Smuzhiyun 				debug("REGULAR MEM SPACE\n");
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 				len[count] = bar[count] & 0xFFFFFFF0;
485*4882a593Smuzhiyun 				len[count] = ~len[count] + 1;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 				debug("len[count] in Mem %x, count %d\n", len[count], count);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 				mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
490*4882a593Smuzhiyun 				if (!mem[count])
491*4882a593Smuzhiyun 					return -ENOMEM;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 				mem[count]->type = MEM;
494*4882a593Smuzhiyun 				mem[count]->busno = func->busno;
495*4882a593Smuzhiyun 				mem[count]->devfunc = PCI_DEVFN(func->device,
496*4882a593Smuzhiyun 							func->function);
497*4882a593Smuzhiyun 				mem[count]->len = len[count];
498*4882a593Smuzhiyun 				if (ibmphp_check_resource(mem[count], 0) == 0) {
499*4882a593Smuzhiyun 					ibmphp_add_resource(mem[count]);
500*4882a593Smuzhiyun 					func->mem[count] = mem[count];
501*4882a593Smuzhiyun 				} else {
502*4882a593Smuzhiyun 					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
503*4882a593Smuzhiyun 					     func->busno, func->device, len[count]);
504*4882a593Smuzhiyun 					kfree(mem[count]);
505*4882a593Smuzhiyun 					return -EIO;
506*4882a593Smuzhiyun 				}
507*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
508*4882a593Smuzhiyun 				/* _______________________This is for debugging purposes only _______________________*/
509*4882a593Smuzhiyun 				debug("b4 writing, start address is %x\n", func->mem[count]->start);
510*4882a593Smuzhiyun 				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
511*4882a593Smuzhiyun 				debug("after writing, the address is %x\n", bar[count]);
512*4882a593Smuzhiyun 				/* __________________________________________________________________________________*/
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
515*4882a593Smuzhiyun 					/* takes up another dword */
516*4882a593Smuzhiyun 					debug("inside mem 64 case, reg. mem, count %d\n", count);
517*4882a593Smuzhiyun 					count += 1;
518*4882a593Smuzhiyun 					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
519*4882a593Smuzhiyun 					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
520*4882a593Smuzhiyun 				}
521*4882a593Smuzhiyun 			}
522*4882a593Smuzhiyun 		}		/* end of mem */
523*4882a593Smuzhiyun 	}			/* end of for */
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	func->bus = 0;		/* To indicate that this is not a PPB */
526*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
527*4882a593Smuzhiyun 	if ((irq > 0x00) && (irq < 0x05))
528*4882a593Smuzhiyun 		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
531*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
534*4882a593Smuzhiyun 	pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	return 0;
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun /******************************************************************************
540*4882a593Smuzhiyun  * This routine configures a PCI-2-PCI bridge and the functions behind it
541*4882a593Smuzhiyun  * Parameters: pci_func
542*4882a593Smuzhiyun  * Returns:
543*4882a593Smuzhiyun  ******************************************************************************/
configure_bridge(struct pci_func ** func_passed,u8 slotno)544*4882a593Smuzhiyun static int configure_bridge(struct pci_func **func_passed, u8 slotno)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	int count;
547*4882a593Smuzhiyun 	int i;
548*4882a593Smuzhiyun 	int rc;
549*4882a593Smuzhiyun 	u8 sec_number;
550*4882a593Smuzhiyun 	u8 io_base;
551*4882a593Smuzhiyun 	u16 pfmem_base;
552*4882a593Smuzhiyun 	u32 bar[2];
553*4882a593Smuzhiyun 	u32 len[2];
554*4882a593Smuzhiyun 	u8 flag_io = 0;
555*4882a593Smuzhiyun 	u8 flag_mem = 0;
556*4882a593Smuzhiyun 	u8 flag_pfmem = 0;
557*4882a593Smuzhiyun 	u8 need_io_upper = 0;
558*4882a593Smuzhiyun 	u8 need_pfmem_upper = 0;
559*4882a593Smuzhiyun 	struct res_needed *amount_needed = NULL;
560*4882a593Smuzhiyun 	struct resource_node *io = NULL;
561*4882a593Smuzhiyun 	struct resource_node *bus_io[2] = {NULL, NULL};
562*4882a593Smuzhiyun 	struct resource_node *mem = NULL;
563*4882a593Smuzhiyun 	struct resource_node *bus_mem[2] = {NULL, NULL};
564*4882a593Smuzhiyun 	struct resource_node *mem_tmp = NULL;
565*4882a593Smuzhiyun 	struct resource_node *pfmem = NULL;
566*4882a593Smuzhiyun 	struct resource_node *bus_pfmem[2] = {NULL, NULL};
567*4882a593Smuzhiyun 	struct bus_node *bus;
568*4882a593Smuzhiyun 	u32 address[] = {
569*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0,
570*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_1,
571*4882a593Smuzhiyun 		0
572*4882a593Smuzhiyun 	};
573*4882a593Smuzhiyun 	struct pci_func *func = *func_passed;
574*4882a593Smuzhiyun 	unsigned int devfn;
575*4882a593Smuzhiyun 	u8 irq;
576*4882a593Smuzhiyun 	int retval;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	debug("%s - enter\n", __func__);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	devfn = PCI_DEVFN(func->function, func->device);
581*4882a593Smuzhiyun 	ibmphp_pci_bus->number = func->busno;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	/* Configuring necessary info for the bridge so that we could see the devices
584*4882a593Smuzhiyun 	 * behind it
585*4882a593Smuzhiyun 	 */
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* _____________________For debugging purposes only __________________________
590*4882a593Smuzhiyun 	pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
591*4882a593Smuzhiyun 	debug("primary # written into the bridge is %x\n", pri_number);
592*4882a593Smuzhiyun 	 ___________________________________________________________________________*/
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	/* in EBDA, only get allocated 1 additional bus # per slot */
595*4882a593Smuzhiyun 	sec_number = find_sec_number(func->busno, slotno);
596*4882a593Smuzhiyun 	if (sec_number == 0xff) {
597*4882a593Smuzhiyun 		err("cannot allocate secondary bus number for the bridged device\n");
598*4882a593Smuzhiyun 		return -EINVAL;
599*4882a593Smuzhiyun 	}
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	debug("after find_sec_number, the number we got is %x\n", sec_number);
602*4882a593Smuzhiyun 	debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/* __________________For debugging purposes only __________________________________
607*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
608*4882a593Smuzhiyun 	debug("sec_number after write/read is %x\n", sec_number);
609*4882a593Smuzhiyun 	 ________________________________________________________________________________*/
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	/* __________________For debugging purposes only ____________________________________
614*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
615*4882a593Smuzhiyun 	debug("subordinate number after write/read is %x\n", sec_number);
616*4882a593Smuzhiyun 	 __________________________________________________________________________________*/
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
619*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
620*4882a593Smuzhiyun 	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	debug("func->busno is %x\n", func->busno);
623*4882a593Smuzhiyun 	debug("sec_number after writing is %x\n", sec_number);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
627*4882a593Smuzhiyun 	   !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
628*4882a593Smuzhiyun 	   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	/* First we need to allocate mem/io for the bridge itself in case it needs it */
632*4882a593Smuzhiyun 	for (count = 0; address[count]; count++) {	/* for 2 BARs */
633*4882a593Smuzhiyun 		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
634*4882a593Smuzhiyun 		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 		if (!bar[count]) {
637*4882a593Smuzhiyun 			/* This BAR is not implemented */
638*4882a593Smuzhiyun 			debug("so we come here then, eh?, count = %d\n", count);
639*4882a593Smuzhiyun 			continue;
640*4882a593Smuzhiyun 		}
641*4882a593Smuzhiyun 		//  tmp_bar = bar[count];
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 		debug("Bar %d wants %x\n", count, bar[count]);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
646*4882a593Smuzhiyun 			/* This is IO */
647*4882a593Smuzhiyun 			len[count] = bar[count] & 0xFFFFFFFC;
648*4882a593Smuzhiyun 			len[count] = ~len[count] + 1;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 			debug("len[count] in IO = %x\n", len[count]);
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 			bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 			if (!bus_io[count]) {
655*4882a593Smuzhiyun 				retval = -ENOMEM;
656*4882a593Smuzhiyun 				goto error;
657*4882a593Smuzhiyun 			}
658*4882a593Smuzhiyun 			bus_io[count]->type = IO;
659*4882a593Smuzhiyun 			bus_io[count]->busno = func->busno;
660*4882a593Smuzhiyun 			bus_io[count]->devfunc = PCI_DEVFN(func->device,
661*4882a593Smuzhiyun 							func->function);
662*4882a593Smuzhiyun 			bus_io[count]->len = len[count];
663*4882a593Smuzhiyun 			if (ibmphp_check_resource(bus_io[count], 0) == 0) {
664*4882a593Smuzhiyun 				ibmphp_add_resource(bus_io[count]);
665*4882a593Smuzhiyun 				func->io[count] = bus_io[count];
666*4882a593Smuzhiyun 			} else {
667*4882a593Smuzhiyun 				err("cannot allocate requested io for bus %x, device %x, len %x\n",
668*4882a593Smuzhiyun 				     func->busno, func->device, len[count]);
669*4882a593Smuzhiyun 				kfree(bus_io[count]);
670*4882a593Smuzhiyun 				return -EIO;
671*4882a593Smuzhiyun 			}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 		} else {
676*4882a593Smuzhiyun 			/* This is Memory */
677*4882a593Smuzhiyun 			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
678*4882a593Smuzhiyun 				/* pfmem */
679*4882a593Smuzhiyun 				len[count] = bar[count] & 0xFFFFFFF0;
680*4882a593Smuzhiyun 				len[count] = ~len[count] + 1;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 				debug("len[count] in PFMEM = %x\n", len[count]);
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 				bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
685*4882a593Smuzhiyun 				if (!bus_pfmem[count]) {
686*4882a593Smuzhiyun 					retval = -ENOMEM;
687*4882a593Smuzhiyun 					goto error;
688*4882a593Smuzhiyun 				}
689*4882a593Smuzhiyun 				bus_pfmem[count]->type = PFMEM;
690*4882a593Smuzhiyun 				bus_pfmem[count]->busno = func->busno;
691*4882a593Smuzhiyun 				bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
692*4882a593Smuzhiyun 							func->function);
693*4882a593Smuzhiyun 				bus_pfmem[count]->len = len[count];
694*4882a593Smuzhiyun 				bus_pfmem[count]->fromMem = 0;
695*4882a593Smuzhiyun 				if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
696*4882a593Smuzhiyun 					ibmphp_add_resource(bus_pfmem[count]);
697*4882a593Smuzhiyun 					func->pfmem[count] = bus_pfmem[count];
698*4882a593Smuzhiyun 				} else {
699*4882a593Smuzhiyun 					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
700*4882a593Smuzhiyun 					if (!mem_tmp) {
701*4882a593Smuzhiyun 						retval = -ENOMEM;
702*4882a593Smuzhiyun 						goto error;
703*4882a593Smuzhiyun 					}
704*4882a593Smuzhiyun 					mem_tmp->type = MEM;
705*4882a593Smuzhiyun 					mem_tmp->busno = bus_pfmem[count]->busno;
706*4882a593Smuzhiyun 					mem_tmp->devfunc = bus_pfmem[count]->devfunc;
707*4882a593Smuzhiyun 					mem_tmp->len = bus_pfmem[count]->len;
708*4882a593Smuzhiyun 					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
709*4882a593Smuzhiyun 						ibmphp_add_resource(mem_tmp);
710*4882a593Smuzhiyun 						bus_pfmem[count]->fromMem = 1;
711*4882a593Smuzhiyun 						bus_pfmem[count]->rangeno = mem_tmp->rangeno;
712*4882a593Smuzhiyun 						ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
713*4882a593Smuzhiyun 						func->pfmem[count] = bus_pfmem[count];
714*4882a593Smuzhiyun 					} else {
715*4882a593Smuzhiyun 						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
716*4882a593Smuzhiyun 						     func->busno, func->device, len[count]);
717*4882a593Smuzhiyun 						kfree(mem_tmp);
718*4882a593Smuzhiyun 						kfree(bus_pfmem[count]);
719*4882a593Smuzhiyun 						return -EIO;
720*4882a593Smuzhiyun 					}
721*4882a593Smuzhiyun 				}
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
726*4882a593Smuzhiyun 					/* takes up another dword */
727*4882a593Smuzhiyun 					count += 1;
728*4882a593Smuzhiyun 					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
729*4882a593Smuzhiyun 					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 				}
732*4882a593Smuzhiyun 			} else {
733*4882a593Smuzhiyun 				/* regular memory */
734*4882a593Smuzhiyun 				len[count] = bar[count] & 0xFFFFFFF0;
735*4882a593Smuzhiyun 				len[count] = ~len[count] + 1;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 				debug("len[count] in Memory is %x\n", len[count]);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 				bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
740*4882a593Smuzhiyun 				if (!bus_mem[count]) {
741*4882a593Smuzhiyun 					retval = -ENOMEM;
742*4882a593Smuzhiyun 					goto error;
743*4882a593Smuzhiyun 				}
744*4882a593Smuzhiyun 				bus_mem[count]->type = MEM;
745*4882a593Smuzhiyun 				bus_mem[count]->busno = func->busno;
746*4882a593Smuzhiyun 				bus_mem[count]->devfunc = PCI_DEVFN(func->device,
747*4882a593Smuzhiyun 							func->function);
748*4882a593Smuzhiyun 				bus_mem[count]->len = len[count];
749*4882a593Smuzhiyun 				if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
750*4882a593Smuzhiyun 					ibmphp_add_resource(bus_mem[count]);
751*4882a593Smuzhiyun 					func->mem[count] = bus_mem[count];
752*4882a593Smuzhiyun 				} else {
753*4882a593Smuzhiyun 					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
754*4882a593Smuzhiyun 					     func->busno, func->device, len[count]);
755*4882a593Smuzhiyun 					kfree(bus_mem[count]);
756*4882a593Smuzhiyun 					return -EIO;
757*4882a593Smuzhiyun 				}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
762*4882a593Smuzhiyun 					/* takes up another dword */
763*4882a593Smuzhiyun 					count += 1;
764*4882a593Smuzhiyun 					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
765*4882a593Smuzhiyun 					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 				}
768*4882a593Smuzhiyun 			}
769*4882a593Smuzhiyun 		}		/* end of mem */
770*4882a593Smuzhiyun 	}			/* end of for  */
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	/* Now need to see how much space the devices behind the bridge needed */
773*4882a593Smuzhiyun 	amount_needed = scan_behind_bridge(func, sec_number);
774*4882a593Smuzhiyun 	if (amount_needed == NULL)
775*4882a593Smuzhiyun 		return -ENOMEM;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	ibmphp_pci_bus->number = func->busno;
778*4882a593Smuzhiyun 	debug("after coming back from scan_behind_bridge\n");
779*4882a593Smuzhiyun 	debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
780*4882a593Smuzhiyun 	debug("amount_needed->io = %x\n", amount_needed->io);
781*4882a593Smuzhiyun 	debug("amount_needed->mem = %x\n", amount_needed->mem);
782*4882a593Smuzhiyun 	debug("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	if (amount_needed->not_correct) {
785*4882a593Smuzhiyun 		debug("amount_needed is not correct\n");
786*4882a593Smuzhiyun 		for (count = 0; address[count]; count++) {
787*4882a593Smuzhiyun 			/* for 2 BARs */
788*4882a593Smuzhiyun 			if (bus_io[count]) {
789*4882a593Smuzhiyun 				ibmphp_remove_resource(bus_io[count]);
790*4882a593Smuzhiyun 				func->io[count] = NULL;
791*4882a593Smuzhiyun 			} else if (bus_pfmem[count]) {
792*4882a593Smuzhiyun 				ibmphp_remove_resource(bus_pfmem[count]);
793*4882a593Smuzhiyun 				func->pfmem[count] = NULL;
794*4882a593Smuzhiyun 			} else if (bus_mem[count]) {
795*4882a593Smuzhiyun 				ibmphp_remove_resource(bus_mem[count]);
796*4882a593Smuzhiyun 				func->mem[count] = NULL;
797*4882a593Smuzhiyun 			}
798*4882a593Smuzhiyun 		}
799*4882a593Smuzhiyun 		kfree(amount_needed);
800*4882a593Smuzhiyun 		return -ENODEV;
801*4882a593Smuzhiyun 	}
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	if (!amount_needed->io) {
804*4882a593Smuzhiyun 		debug("it doesn't want IO?\n");
805*4882a593Smuzhiyun 		flag_io = 1;
806*4882a593Smuzhiyun 	} else {
807*4882a593Smuzhiyun 		debug("it wants %x IO behind the bridge\n", amount_needed->io);
808*4882a593Smuzhiyun 		io = kzalloc(sizeof(*io), GFP_KERNEL);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		if (!io) {
811*4882a593Smuzhiyun 			retval = -ENOMEM;
812*4882a593Smuzhiyun 			goto error;
813*4882a593Smuzhiyun 		}
814*4882a593Smuzhiyun 		io->type = IO;
815*4882a593Smuzhiyun 		io->busno = func->busno;
816*4882a593Smuzhiyun 		io->devfunc = PCI_DEVFN(func->device, func->function);
817*4882a593Smuzhiyun 		io->len = amount_needed->io;
818*4882a593Smuzhiyun 		if (ibmphp_check_resource(io, 1) == 0) {
819*4882a593Smuzhiyun 			debug("were we able to add io\n");
820*4882a593Smuzhiyun 			ibmphp_add_resource(io);
821*4882a593Smuzhiyun 			flag_io = 1;
822*4882a593Smuzhiyun 		}
823*4882a593Smuzhiyun 	}
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	if (!amount_needed->mem) {
826*4882a593Smuzhiyun 		debug("it doesn't want n.e.memory?\n");
827*4882a593Smuzhiyun 		flag_mem = 1;
828*4882a593Smuzhiyun 	} else {
829*4882a593Smuzhiyun 		debug("it wants %x memory behind the bridge\n", amount_needed->mem);
830*4882a593Smuzhiyun 		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
831*4882a593Smuzhiyun 		if (!mem) {
832*4882a593Smuzhiyun 			retval = -ENOMEM;
833*4882a593Smuzhiyun 			goto error;
834*4882a593Smuzhiyun 		}
835*4882a593Smuzhiyun 		mem->type = MEM;
836*4882a593Smuzhiyun 		mem->busno = func->busno;
837*4882a593Smuzhiyun 		mem->devfunc = PCI_DEVFN(func->device, func->function);
838*4882a593Smuzhiyun 		mem->len = amount_needed->mem;
839*4882a593Smuzhiyun 		if (ibmphp_check_resource(mem, 1) == 0) {
840*4882a593Smuzhiyun 			ibmphp_add_resource(mem);
841*4882a593Smuzhiyun 			flag_mem = 1;
842*4882a593Smuzhiyun 			debug("were we able to add mem\n");
843*4882a593Smuzhiyun 		}
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	if (!amount_needed->pfmem) {
847*4882a593Smuzhiyun 		debug("it doesn't want n.e.pfmem mem?\n");
848*4882a593Smuzhiyun 		flag_pfmem = 1;
849*4882a593Smuzhiyun 	} else {
850*4882a593Smuzhiyun 		debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
851*4882a593Smuzhiyun 		pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
852*4882a593Smuzhiyun 		if (!pfmem) {
853*4882a593Smuzhiyun 			retval = -ENOMEM;
854*4882a593Smuzhiyun 			goto error;
855*4882a593Smuzhiyun 		}
856*4882a593Smuzhiyun 		pfmem->type = PFMEM;
857*4882a593Smuzhiyun 		pfmem->busno = func->busno;
858*4882a593Smuzhiyun 		pfmem->devfunc = PCI_DEVFN(func->device, func->function);
859*4882a593Smuzhiyun 		pfmem->len = amount_needed->pfmem;
860*4882a593Smuzhiyun 		pfmem->fromMem = 0;
861*4882a593Smuzhiyun 		if (ibmphp_check_resource(pfmem, 1) == 0) {
862*4882a593Smuzhiyun 			ibmphp_add_resource(pfmem);
863*4882a593Smuzhiyun 			flag_pfmem = 1;
864*4882a593Smuzhiyun 		} else {
865*4882a593Smuzhiyun 			mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
866*4882a593Smuzhiyun 			if (!mem_tmp) {
867*4882a593Smuzhiyun 				retval = -ENOMEM;
868*4882a593Smuzhiyun 				goto error;
869*4882a593Smuzhiyun 			}
870*4882a593Smuzhiyun 			mem_tmp->type = MEM;
871*4882a593Smuzhiyun 			mem_tmp->busno = pfmem->busno;
872*4882a593Smuzhiyun 			mem_tmp->devfunc = pfmem->devfunc;
873*4882a593Smuzhiyun 			mem_tmp->len = pfmem->len;
874*4882a593Smuzhiyun 			if (ibmphp_check_resource(mem_tmp, 1) == 0) {
875*4882a593Smuzhiyun 				ibmphp_add_resource(mem_tmp);
876*4882a593Smuzhiyun 				pfmem->fromMem = 1;
877*4882a593Smuzhiyun 				pfmem->rangeno = mem_tmp->rangeno;
878*4882a593Smuzhiyun 				ibmphp_add_pfmem_from_mem(pfmem);
879*4882a593Smuzhiyun 				flag_pfmem = 1;
880*4882a593Smuzhiyun 			}
881*4882a593Smuzhiyun 		}
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
885*4882a593Smuzhiyun 	debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	if (flag_io && flag_mem && flag_pfmem) {
888*4882a593Smuzhiyun 		/* If on bootup, there was a bridged card in this slot,
889*4882a593Smuzhiyun 		 * then card was removed and ibmphp got unloaded and loaded
890*4882a593Smuzhiyun 		 * back again, there's no way for us to remove the bus
891*4882a593Smuzhiyun 		 * struct, so no need to kmalloc, can use existing node
892*4882a593Smuzhiyun 		 */
893*4882a593Smuzhiyun 		bus = ibmphp_find_res_bus(sec_number);
894*4882a593Smuzhiyun 		if (!bus) {
895*4882a593Smuzhiyun 			bus = kzalloc(sizeof(*bus), GFP_KERNEL);
896*4882a593Smuzhiyun 			if (!bus) {
897*4882a593Smuzhiyun 				retval = -ENOMEM;
898*4882a593Smuzhiyun 				goto error;
899*4882a593Smuzhiyun 			}
900*4882a593Smuzhiyun 			bus->busno = sec_number;
901*4882a593Smuzhiyun 			debug("b4 adding new bus\n");
902*4882a593Smuzhiyun 			rc = add_new_bus(bus, io, mem, pfmem, func->busno);
903*4882a593Smuzhiyun 		} else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
904*4882a593Smuzhiyun 			rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
905*4882a593Smuzhiyun 		else {
906*4882a593Smuzhiyun 			err("expected bus structure not empty?\n");
907*4882a593Smuzhiyun 			retval = -EIO;
908*4882a593Smuzhiyun 			goto error;
909*4882a593Smuzhiyun 		}
910*4882a593Smuzhiyun 		if (rc) {
911*4882a593Smuzhiyun 			if (rc == -ENOMEM) {
912*4882a593Smuzhiyun 				ibmphp_remove_bus(bus, func->busno);
913*4882a593Smuzhiyun 				kfree(amount_needed);
914*4882a593Smuzhiyun 				return rc;
915*4882a593Smuzhiyun 			}
916*4882a593Smuzhiyun 			retval = rc;
917*4882a593Smuzhiyun 			goto error;
918*4882a593Smuzhiyun 		}
919*4882a593Smuzhiyun 		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
920*4882a593Smuzhiyun 		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 		if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
923*4882a593Smuzhiyun 			debug("io 32\n");
924*4882a593Smuzhiyun 			need_io_upper = 1;
925*4882a593Smuzhiyun 		}
926*4882a593Smuzhiyun 		if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
927*4882a593Smuzhiyun 			debug("pfmem 64\n");
928*4882a593Smuzhiyun 			need_pfmem_upper = 1;
929*4882a593Smuzhiyun 		}
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 		if (bus->noIORanges) {
932*4882a593Smuzhiyun 			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
933*4882a593Smuzhiyun 			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 			/* _______________This is for debugging purposes only ____________________
936*4882a593Smuzhiyun 			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
937*4882a593Smuzhiyun 			debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
938*4882a593Smuzhiyun 			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
939*4882a593Smuzhiyun 			debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
940*4882a593Smuzhiyun 			 ________________________________________________________________________*/
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 			if (need_io_upper) {	/* since can't support n.e.ways */
943*4882a593Smuzhiyun 				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
944*4882a593Smuzhiyun 				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
945*4882a593Smuzhiyun 			}
946*4882a593Smuzhiyun 		} else {
947*4882a593Smuzhiyun 			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
948*4882a593Smuzhiyun 			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
949*4882a593Smuzhiyun 		}
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 		if (bus->noMemRanges) {
952*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
953*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 			/* ____________________This is for debugging purposes only ________________________
956*4882a593Smuzhiyun 			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
957*4882a593Smuzhiyun 			debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
958*4882a593Smuzhiyun 			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
959*4882a593Smuzhiyun 			debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
960*4882a593Smuzhiyun 			 __________________________________________________________________________________*/
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		} else {
963*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
964*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
965*4882a593Smuzhiyun 		}
966*4882a593Smuzhiyun 		if (bus->noPFMemRanges) {
967*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
968*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 			/* __________________________This is for debugging purposes only _______________________
971*4882a593Smuzhiyun 			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
972*4882a593Smuzhiyun 			debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
973*4882a593Smuzhiyun 			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
974*4882a593Smuzhiyun 			debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
975*4882a593Smuzhiyun 			 ______________________________________________________________________________________*/
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 			if (need_pfmem_upper) {	/* since can't support n.e.ways */
978*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
979*4882a593Smuzhiyun 				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
980*4882a593Smuzhiyun 			}
981*4882a593Smuzhiyun 		} else {
982*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
983*4882a593Smuzhiyun 			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
984*4882a593Smuzhiyun 		}
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 		debug("b4 writing control information\n");
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
989*4882a593Smuzhiyun 		if ((irq > 0x00) && (irq < 0x05))
990*4882a593Smuzhiyun 			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
991*4882a593Smuzhiyun 		/*
992*4882a593Smuzhiyun 		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
993*4882a593Smuzhiyun 		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
994*4882a593Smuzhiyun 		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
995*4882a593Smuzhiyun 		 */
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
998*4882a593Smuzhiyun 		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
999*4882a593Smuzhiyun 		for (i = 0; i < 32; i++) {
1000*4882a593Smuzhiyun 			if (amount_needed->devices[i]) {
1001*4882a593Smuzhiyun 				debug("device where devices[i] is 1 = %x\n", i);
1002*4882a593Smuzhiyun 				func->devices[i] = 1;
1003*4882a593Smuzhiyun 			}
1004*4882a593Smuzhiyun 		}
1005*4882a593Smuzhiyun 		func->bus = 1;	/* For unconfiguring, to indicate it's PPB */
1006*4882a593Smuzhiyun 		func_passed = &func;
1007*4882a593Smuzhiyun 		debug("func->busno b4 returning is %x\n", func->busno);
1008*4882a593Smuzhiyun 		debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1009*4882a593Smuzhiyun 		kfree(amount_needed);
1010*4882a593Smuzhiyun 		return 0;
1011*4882a593Smuzhiyun 	} else {
1012*4882a593Smuzhiyun 		err("Configuring bridge was unsuccessful...\n");
1013*4882a593Smuzhiyun 		mem_tmp = NULL;
1014*4882a593Smuzhiyun 		retval = -EIO;
1015*4882a593Smuzhiyun 		goto error;
1016*4882a593Smuzhiyun 	}
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun error:
1019*4882a593Smuzhiyun 	kfree(amount_needed);
1020*4882a593Smuzhiyun 	if (pfmem)
1021*4882a593Smuzhiyun 		ibmphp_remove_resource(pfmem);
1022*4882a593Smuzhiyun 	if (io)
1023*4882a593Smuzhiyun 		ibmphp_remove_resource(io);
1024*4882a593Smuzhiyun 	if (mem)
1025*4882a593Smuzhiyun 		ibmphp_remove_resource(mem);
1026*4882a593Smuzhiyun 	for (i = 0; i < 2; i++) {	/* for 2 BARs */
1027*4882a593Smuzhiyun 		if (bus_io[i]) {
1028*4882a593Smuzhiyun 			ibmphp_remove_resource(bus_io[i]);
1029*4882a593Smuzhiyun 			func->io[i] = NULL;
1030*4882a593Smuzhiyun 		} else if (bus_pfmem[i]) {
1031*4882a593Smuzhiyun 			ibmphp_remove_resource(bus_pfmem[i]);
1032*4882a593Smuzhiyun 			func->pfmem[i] = NULL;
1033*4882a593Smuzhiyun 		} else if (bus_mem[i]) {
1034*4882a593Smuzhiyun 			ibmphp_remove_resource(bus_mem[i]);
1035*4882a593Smuzhiyun 			func->mem[i] = NULL;
1036*4882a593Smuzhiyun 		}
1037*4882a593Smuzhiyun 	}
1038*4882a593Smuzhiyun 	return retval;
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun /*****************************************************************************
1042*4882a593Smuzhiyun  * This function adds up the amount of resources needed behind the PPB bridge
1043*4882a593Smuzhiyun  * and passes it to the configure_bridge function
1044*4882a593Smuzhiyun  * Input: bridge function
1045*4882a593Smuzhiyun  * Output: amount of resources needed
1046*4882a593Smuzhiyun  *****************************************************************************/
scan_behind_bridge(struct pci_func * func,u8 busno)1047*4882a593Smuzhiyun static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	int count, len[6];
1050*4882a593Smuzhiyun 	u16 vendor_id;
1051*4882a593Smuzhiyun 	u8 hdr_type;
1052*4882a593Smuzhiyun 	u8 device, function;
1053*4882a593Smuzhiyun 	unsigned int devfn;
1054*4882a593Smuzhiyun 	int howmany = 0;	/*this is to see if there are any devices behind the bridge */
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	u32 bar[6], class;
1057*4882a593Smuzhiyun 	u32 address[] = {
1058*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0,
1059*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_1,
1060*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_2,
1061*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_3,
1062*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_4,
1063*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_5,
1064*4882a593Smuzhiyun 		0
1065*4882a593Smuzhiyun 	};
1066*4882a593Smuzhiyun 	struct res_needed *amount;
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1069*4882a593Smuzhiyun 	if (amount == NULL)
1070*4882a593Smuzhiyun 		return NULL;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	ibmphp_pci_bus->number = busno;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	debug("the bus_no behind the bridge is %x\n", busno);
1075*4882a593Smuzhiyun 	debug("scanning devices behind the bridge...\n");
1076*4882a593Smuzhiyun 	for (device = 0; device < 32; device++) {
1077*4882a593Smuzhiyun 		amount->devices[device] = 0;
1078*4882a593Smuzhiyun 		for (function = 0; function < 8; function++) {
1079*4882a593Smuzhiyun 			devfn = PCI_DEVFN(device, function);
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 			if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1084*4882a593Smuzhiyun 				/* found correct device!!! */
1085*4882a593Smuzhiyun 				howmany++;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 				pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1088*4882a593Smuzhiyun 				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 				debug("hdr_type behind the bridge is %x\n", hdr_type);
1091*4882a593Smuzhiyun 				if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1092*4882a593Smuzhiyun 					err("embedded bridges not supported for hot-plugging.\n");
1093*4882a593Smuzhiyun 					amount->not_correct = 1;
1094*4882a593Smuzhiyun 					return amount;
1095*4882a593Smuzhiyun 				}
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 				class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1098*4882a593Smuzhiyun 				if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1099*4882a593Smuzhiyun 					err("The device %x is VGA compatible and as is not supported for hot plugging.  Please choose another device.\n", device);
1100*4882a593Smuzhiyun 					amount->not_correct = 1;
1101*4882a593Smuzhiyun 					return amount;
1102*4882a593Smuzhiyun 				} else if (class == PCI_CLASS_DISPLAY_VGA) {
1103*4882a593Smuzhiyun 					err("The device %x is not supported for hot plugging.  Please choose another device.\n", device);
1104*4882a593Smuzhiyun 					amount->not_correct = 1;
1105*4882a593Smuzhiyun 					return amount;
1106*4882a593Smuzhiyun 				}
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 				amount->devices[device] = 1;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 				for (count = 0; address[count]; count++) {
1111*4882a593Smuzhiyun 					/* for 6 BARs */
1112*4882a593Smuzhiyun 					/*
1113*4882a593Smuzhiyun 					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1114*4882a593Smuzhiyun 					if (tmp & 0x01) // IO
1115*4882a593Smuzhiyun 						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1116*4882a593Smuzhiyun 					else // MEMORY
1117*4882a593Smuzhiyun 						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1118*4882a593Smuzhiyun 					*/
1119*4882a593Smuzhiyun 					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1120*4882a593Smuzhiyun 					pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 					debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 					if (!bar[count])	/* This BAR is not implemented */
1125*4882a593Smuzhiyun 						continue;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 					//tmp_bar = bar[count];
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 					debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 					if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1132*4882a593Smuzhiyun 						/* This is IO */
1133*4882a593Smuzhiyun 						len[count] = bar[count] & 0xFFFFFFFC;
1134*4882a593Smuzhiyun 						len[count] = ~len[count] + 1;
1135*4882a593Smuzhiyun 						amount->io += len[count];
1136*4882a593Smuzhiyun 					} else {
1137*4882a593Smuzhiyun 						/* This is Memory */
1138*4882a593Smuzhiyun 						if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1139*4882a593Smuzhiyun 							/* pfmem */
1140*4882a593Smuzhiyun 							len[count] = bar[count] & 0xFFFFFFF0;
1141*4882a593Smuzhiyun 							len[count] = ~len[count] + 1;
1142*4882a593Smuzhiyun 							amount->pfmem += len[count];
1143*4882a593Smuzhiyun 							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1144*4882a593Smuzhiyun 								/* takes up another dword */
1145*4882a593Smuzhiyun 								count += 1;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 						} else {
1148*4882a593Smuzhiyun 							/* regular memory */
1149*4882a593Smuzhiyun 							len[count] = bar[count] & 0xFFFFFFF0;
1150*4882a593Smuzhiyun 							len[count] = ~len[count] + 1;
1151*4882a593Smuzhiyun 							amount->mem += len[count];
1152*4882a593Smuzhiyun 							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1153*4882a593Smuzhiyun 								/* takes up another dword */
1154*4882a593Smuzhiyun 								count += 1;
1155*4882a593Smuzhiyun 							}
1156*4882a593Smuzhiyun 						}
1157*4882a593Smuzhiyun 					}
1158*4882a593Smuzhiyun 				}	/* end for */
1159*4882a593Smuzhiyun 			}	/* end if (valid) */
1160*4882a593Smuzhiyun 		}	/* end for */
1161*4882a593Smuzhiyun 	}	/* end for */
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	if (!howmany)
1164*4882a593Smuzhiyun 		amount->not_correct = 1;
1165*4882a593Smuzhiyun 	else
1166*4882a593Smuzhiyun 		amount->not_correct = 0;
1167*4882a593Smuzhiyun 	if ((amount->io) && (amount->io < IOBRIDGE))
1168*4882a593Smuzhiyun 		amount->io = IOBRIDGE;
1169*4882a593Smuzhiyun 	if ((amount->mem) && (amount->mem < MEMBRIDGE))
1170*4882a593Smuzhiyun 		amount->mem = MEMBRIDGE;
1171*4882a593Smuzhiyun 	if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1172*4882a593Smuzhiyun 		amount->pfmem = MEMBRIDGE;
1173*4882a593Smuzhiyun 	return amount;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1177*4882a593Smuzhiyun  * upon bootup in the system, since we don't allocate func to such case, we need to read
1178*4882a593Smuzhiyun  * the start addresses from pci config space and then find the corresponding entries in
1179*4882a593Smuzhiyun  * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1180*4882a593Smuzhiyun  * Change: we also call these functions even if we configured the card ourselves (i.e., not
1181*4882a593Smuzhiyun  * the bootup case), since it should work same way
1182*4882a593Smuzhiyun  */
unconfigure_boot_device(u8 busno,u8 device,u8 function)1183*4882a593Smuzhiyun static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun 	u32 start_address;
1186*4882a593Smuzhiyun 	u32 address[] = {
1187*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0,
1188*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_1,
1189*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_2,
1190*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_3,
1191*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_4,
1192*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_5,
1193*4882a593Smuzhiyun 		0
1194*4882a593Smuzhiyun 	};
1195*4882a593Smuzhiyun 	int count;
1196*4882a593Smuzhiyun 	struct resource_node *io;
1197*4882a593Smuzhiyun 	struct resource_node *mem;
1198*4882a593Smuzhiyun 	struct resource_node *pfmem;
1199*4882a593Smuzhiyun 	struct bus_node *bus;
1200*4882a593Smuzhiyun 	u32 end_address;
1201*4882a593Smuzhiyun 	u32 temp_end;
1202*4882a593Smuzhiyun 	u32 size;
1203*4882a593Smuzhiyun 	u32 tmp_address;
1204*4882a593Smuzhiyun 	unsigned int devfn;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	debug("%s - enter\n", __func__);
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	bus = ibmphp_find_res_bus(busno);
1209*4882a593Smuzhiyun 	if (!bus) {
1210*4882a593Smuzhiyun 		debug("cannot find corresponding bus.\n");
1211*4882a593Smuzhiyun 		return -EINVAL;
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	devfn = PCI_DEVFN(device, function);
1215*4882a593Smuzhiyun 	ibmphp_pci_bus->number = busno;
1216*4882a593Smuzhiyun 	for (count = 0; address[count]; count++) {	/* for 6 BARs */
1217*4882a593Smuzhiyun 		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		/* We can do this here, b/c by that time the device driver of the card has been stopped */
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1222*4882a593Smuzhiyun 		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1223*4882a593Smuzhiyun 		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		debug("start_address is %x\n", start_address);
1226*4882a593Smuzhiyun 		debug("busno, device, function %x %x %x\n", busno, device, function);
1227*4882a593Smuzhiyun 		if (!size) {
1228*4882a593Smuzhiyun 			/* This BAR is not implemented */
1229*4882a593Smuzhiyun 			debug("is this bar no implemented?, count = %d\n", count);
1230*4882a593Smuzhiyun 			continue;
1231*4882a593Smuzhiyun 		}
1232*4882a593Smuzhiyun 		tmp_address = start_address;
1233*4882a593Smuzhiyun 		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1234*4882a593Smuzhiyun 			/* This is IO */
1235*4882a593Smuzhiyun 			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1236*4882a593Smuzhiyun 			size = size & 0xFFFFFFFC;
1237*4882a593Smuzhiyun 			size = ~size + 1;
1238*4882a593Smuzhiyun 			end_address = start_address + size - 1;
1239*4882a593Smuzhiyun 			if (ibmphp_find_resource(bus, start_address, &io, IO))
1240*4882a593Smuzhiyun 				goto report_search_failure;
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 			debug("io->start = %x\n", io->start);
1243*4882a593Smuzhiyun 			temp_end = io->end;
1244*4882a593Smuzhiyun 			start_address = io->end + 1;
1245*4882a593Smuzhiyun 			ibmphp_remove_resource(io);
1246*4882a593Smuzhiyun 			/* This is needed b/c of the old I/O restrictions in the BIOS */
1247*4882a593Smuzhiyun 			while (temp_end < end_address) {
1248*4882a593Smuzhiyun 				if (ibmphp_find_resource(bus, start_address,
1249*4882a593Smuzhiyun 							 &io, IO))
1250*4882a593Smuzhiyun 					goto report_search_failure;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 				debug("io->start = %x\n", io->start);
1253*4882a593Smuzhiyun 				temp_end = io->end;
1254*4882a593Smuzhiyun 				start_address = io->end + 1;
1255*4882a593Smuzhiyun 				ibmphp_remove_resource(io);
1256*4882a593Smuzhiyun 			}
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1259*4882a593Smuzhiyun 		} else {
1260*4882a593Smuzhiyun 			/* This is Memory */
1261*4882a593Smuzhiyun 			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1262*4882a593Smuzhiyun 				/* pfmem */
1263*4882a593Smuzhiyun 				debug("start address of pfmem is %x\n", start_address);
1264*4882a593Smuzhiyun 				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1267*4882a593Smuzhiyun 					err("cannot find corresponding PFMEM resource to remove\n");
1268*4882a593Smuzhiyun 					return -EIO;
1269*4882a593Smuzhiyun 				}
1270*4882a593Smuzhiyun 				if (pfmem) {
1271*4882a593Smuzhiyun 					debug("pfmem->start = %x\n", pfmem->start);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 					ibmphp_remove_resource(pfmem);
1274*4882a593Smuzhiyun 				}
1275*4882a593Smuzhiyun 			} else {
1276*4882a593Smuzhiyun 				/* regular memory */
1277*4882a593Smuzhiyun 				debug("start address of mem is %x\n", start_address);
1278*4882a593Smuzhiyun 				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1281*4882a593Smuzhiyun 					err("cannot find corresponding MEM resource to remove\n");
1282*4882a593Smuzhiyun 					return -EIO;
1283*4882a593Smuzhiyun 				}
1284*4882a593Smuzhiyun 				if (mem) {
1285*4882a593Smuzhiyun 					debug("mem->start = %x\n", mem->start);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 					ibmphp_remove_resource(mem);
1288*4882a593Smuzhiyun 				}
1289*4882a593Smuzhiyun 			}
1290*4882a593Smuzhiyun 			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1291*4882a593Smuzhiyun 				/* takes up another dword */
1292*4882a593Smuzhiyun 				count += 1;
1293*4882a593Smuzhiyun 			}
1294*4882a593Smuzhiyun 		}	/* end of mem */
1295*4882a593Smuzhiyun 	}	/* end of for */
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	return 0;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun report_search_failure:
1300*4882a593Smuzhiyun 	err("cannot find corresponding IO resource to remove\n");
1301*4882a593Smuzhiyun 	return -EIO;
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
unconfigure_boot_bridge(u8 busno,u8 device,u8 function)1304*4882a593Smuzhiyun static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun 	int count;
1307*4882a593Smuzhiyun 	int bus_no, pri_no, sub_no, sec_no = 0;
1308*4882a593Smuzhiyun 	u32 start_address, tmp_address;
1309*4882a593Smuzhiyun 	u8 sec_number, sub_number, pri_number;
1310*4882a593Smuzhiyun 	struct resource_node *io = NULL;
1311*4882a593Smuzhiyun 	struct resource_node *mem = NULL;
1312*4882a593Smuzhiyun 	struct resource_node *pfmem = NULL;
1313*4882a593Smuzhiyun 	struct bus_node *bus;
1314*4882a593Smuzhiyun 	u32 address[] = {
1315*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_0,
1316*4882a593Smuzhiyun 		PCI_BASE_ADDRESS_1,
1317*4882a593Smuzhiyun 		0
1318*4882a593Smuzhiyun 	};
1319*4882a593Smuzhiyun 	unsigned int devfn;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	devfn = PCI_DEVFN(device, function);
1322*4882a593Smuzhiyun 	ibmphp_pci_bus->number = busno;
1323*4882a593Smuzhiyun 	bus_no = (int) busno;
1324*4882a593Smuzhiyun 	debug("busno is %x\n", busno);
1325*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1326*4882a593Smuzhiyun 	debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1329*4882a593Smuzhiyun 	debug("sec_number is %x\n", sec_number);
1330*4882a593Smuzhiyun 	sec_no = (int) sec_number;
1331*4882a593Smuzhiyun 	pri_no = (int) pri_number;
1332*4882a593Smuzhiyun 	if (pri_no != bus_no) {
1333*4882a593Smuzhiyun 		err("primary numbers in our structures and pci config space don't match.\n");
1334*4882a593Smuzhiyun 		return -EINVAL;
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1338*4882a593Smuzhiyun 	sub_no = (int) sub_number;
1339*4882a593Smuzhiyun 	debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1340*4882a593Smuzhiyun 	if (sec_no != sub_number) {
1341*4882a593Smuzhiyun 		err("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1342*4882a593Smuzhiyun 		return -ENODEV;
1343*4882a593Smuzhiyun 	}
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	bus = ibmphp_find_res_bus(sec_number);
1346*4882a593Smuzhiyun 	if (!bus) {
1347*4882a593Smuzhiyun 		err("cannot find Bus structure for the bridged device\n");
1348*4882a593Smuzhiyun 		return -EINVAL;
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 	debug("bus->busno is %x\n", bus->busno);
1351*4882a593Smuzhiyun 	debug("sec_number is %x\n", sec_number);
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	ibmphp_remove_bus(bus, busno);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	for (count = 0; address[count]; count++) {
1356*4882a593Smuzhiyun 		/* for 2 BARs */
1357*4882a593Smuzhiyun 		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 		if (!start_address) {
1360*4882a593Smuzhiyun 			/* This BAR is not implemented */
1361*4882a593Smuzhiyun 			continue;
1362*4882a593Smuzhiyun 		}
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 		tmp_address = start_address;
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1367*4882a593Smuzhiyun 			/* This is IO */
1368*4882a593Smuzhiyun 			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1369*4882a593Smuzhiyun 			if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1370*4882a593Smuzhiyun 				err("cannot find corresponding IO resource to remove\n");
1371*4882a593Smuzhiyun 				return -EIO;
1372*4882a593Smuzhiyun 			}
1373*4882a593Smuzhiyun 			if (io)
1374*4882a593Smuzhiyun 				debug("io->start = %x\n", io->start);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 			ibmphp_remove_resource(io);
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1379*4882a593Smuzhiyun 		} else {
1380*4882a593Smuzhiyun 			/* This is Memory */
1381*4882a593Smuzhiyun 			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1382*4882a593Smuzhiyun 				/* pfmem */
1383*4882a593Smuzhiyun 				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1384*4882a593Smuzhiyun 				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1385*4882a593Smuzhiyun 					err("cannot find corresponding PFMEM resource to remove\n");
1386*4882a593Smuzhiyun 					return -EINVAL;
1387*4882a593Smuzhiyun 				}
1388*4882a593Smuzhiyun 				if (pfmem) {
1389*4882a593Smuzhiyun 					debug("pfmem->start = %x\n", pfmem->start);
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 					ibmphp_remove_resource(pfmem);
1392*4882a593Smuzhiyun 				}
1393*4882a593Smuzhiyun 			} else {
1394*4882a593Smuzhiyun 				/* regular memory */
1395*4882a593Smuzhiyun 				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1396*4882a593Smuzhiyun 				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1397*4882a593Smuzhiyun 					err("cannot find corresponding MEM resource to remove\n");
1398*4882a593Smuzhiyun 					return -EINVAL;
1399*4882a593Smuzhiyun 				}
1400*4882a593Smuzhiyun 				if (mem) {
1401*4882a593Smuzhiyun 					debug("mem->start = %x\n", mem->start);
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 					ibmphp_remove_resource(mem);
1404*4882a593Smuzhiyun 				}
1405*4882a593Smuzhiyun 			}
1406*4882a593Smuzhiyun 			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1407*4882a593Smuzhiyun 				/* takes up another dword */
1408*4882a593Smuzhiyun 				count += 1;
1409*4882a593Smuzhiyun 			}
1410*4882a593Smuzhiyun 		}	/* end of mem */
1411*4882a593Smuzhiyun 	}	/* end of for */
1412*4882a593Smuzhiyun 	debug("%s - exiting, returning success\n", __func__);
1413*4882a593Smuzhiyun 	return 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
unconfigure_boot_card(struct slot * slot_cur)1416*4882a593Smuzhiyun static int unconfigure_boot_card(struct slot *slot_cur)
1417*4882a593Smuzhiyun {
1418*4882a593Smuzhiyun 	u16 vendor_id;
1419*4882a593Smuzhiyun 	u32 class;
1420*4882a593Smuzhiyun 	u8 hdr_type;
1421*4882a593Smuzhiyun 	u8 device;
1422*4882a593Smuzhiyun 	u8 busno;
1423*4882a593Smuzhiyun 	u8 function;
1424*4882a593Smuzhiyun 	int rc;
1425*4882a593Smuzhiyun 	unsigned int devfn;
1426*4882a593Smuzhiyun 	u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	debug("%s - enter\n", __func__);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	device = slot_cur->device;
1431*4882a593Smuzhiyun 	busno = slot_cur->bus;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	debug("b4 for loop, device is %x\n", device);
1434*4882a593Smuzhiyun 	/* For every function on the card */
1435*4882a593Smuzhiyun 	for (function = 0x0; function < 0x08; function++) {
1436*4882a593Smuzhiyun 		devfn = PCI_DEVFN(device, function);
1437*4882a593Smuzhiyun 		ibmphp_pci_bus->number = busno;
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun 		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1442*4882a593Smuzhiyun 			/* found correct device!!! */
1443*4882a593Smuzhiyun 			++valid_device;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 			debug("%s - found correct device\n", __func__);
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 			/* header: x x x x x x x x
1448*4882a593Smuzhiyun 			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1449*4882a593Smuzhiyun 			 *         |_=> 0 = single function device, 1 = multi-function device
1450*4882a593Smuzhiyun 			 */
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1453*4882a593Smuzhiyun 			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 			debug("hdr_type %x, class %x\n", hdr_type, class);
1456*4882a593Smuzhiyun 			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1457*4882a593Smuzhiyun 			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1458*4882a593Smuzhiyun 				err("The device %x function %x is VGA compatible and is not supported for hot removing.  Please choose another device.\n", device, function);
1459*4882a593Smuzhiyun 				return -ENODEV;
1460*4882a593Smuzhiyun 			} else if (class == PCI_CLASS_DISPLAY_VGA) {
1461*4882a593Smuzhiyun 				err("The device %x function %x is not supported for hot removing.  Please choose another device.\n", device, function);
1462*4882a593Smuzhiyun 				return -ENODEV;
1463*4882a593Smuzhiyun 			}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 			switch (hdr_type) {
1466*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_NORMAL:
1467*4882a593Smuzhiyun 					rc = unconfigure_boot_device(busno, device, function);
1468*4882a593Smuzhiyun 					if (rc) {
1469*4882a593Smuzhiyun 						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1470*4882a593Smuzhiyun 						     device, function, busno);
1471*4882a593Smuzhiyun 						return rc;
1472*4882a593Smuzhiyun 					}
1473*4882a593Smuzhiyun 					function = 0x8;
1474*4882a593Smuzhiyun 					break;
1475*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_MULTIDEVICE:
1476*4882a593Smuzhiyun 					rc = unconfigure_boot_device(busno, device, function);
1477*4882a593Smuzhiyun 					if (rc) {
1478*4882a593Smuzhiyun 						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1479*4882a593Smuzhiyun 						     device, function, busno);
1480*4882a593Smuzhiyun 						return rc;
1481*4882a593Smuzhiyun 					}
1482*4882a593Smuzhiyun 					break;
1483*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_BRIDGE:
1484*4882a593Smuzhiyun 					class >>= 8;
1485*4882a593Smuzhiyun 					if (class != PCI_CLASS_BRIDGE_PCI) {
1486*4882a593Smuzhiyun 						err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing.  Please try another card.\n", device, function);
1487*4882a593Smuzhiyun 						return -ENODEV;
1488*4882a593Smuzhiyun 					}
1489*4882a593Smuzhiyun 					rc = unconfigure_boot_bridge(busno, device, function);
1490*4882a593Smuzhiyun 					if (rc != 0) {
1491*4882a593Smuzhiyun 						err("was not able to hot-remove PPB properly.\n");
1492*4882a593Smuzhiyun 						return rc;
1493*4882a593Smuzhiyun 					}
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 					function = 0x8;
1496*4882a593Smuzhiyun 					break;
1497*4882a593Smuzhiyun 				case PCI_HEADER_TYPE_MULTIBRIDGE:
1498*4882a593Smuzhiyun 					class >>= 8;
1499*4882a593Smuzhiyun 					if (class != PCI_CLASS_BRIDGE_PCI) {
1500*4882a593Smuzhiyun 						err("This device %x function %x is not PCI-to-PCI bridge,  and is not supported for hot-removing.  Please try another card.\n", device, function);
1501*4882a593Smuzhiyun 						return -ENODEV;
1502*4882a593Smuzhiyun 					}
1503*4882a593Smuzhiyun 					rc = unconfigure_boot_bridge(busno, device, function);
1504*4882a593Smuzhiyun 					if (rc != 0) {
1505*4882a593Smuzhiyun 						err("was not able to hot-remove PPB properly.\n");
1506*4882a593Smuzhiyun 						return rc;
1507*4882a593Smuzhiyun 					}
1508*4882a593Smuzhiyun 					break;
1509*4882a593Smuzhiyun 				default:
1510*4882a593Smuzhiyun 					err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1511*4882a593Smuzhiyun 					return -1;
1512*4882a593Smuzhiyun 					break;
1513*4882a593Smuzhiyun 			}	/* end of switch */
1514*4882a593Smuzhiyun 		}	/* end of valid device */
1515*4882a593Smuzhiyun 	}	/* end of for */
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	if (!valid_device) {
1518*4882a593Smuzhiyun 		err("Could not find device to unconfigure.  Or could not read the card.\n");
1519*4882a593Smuzhiyun 		return -1;
1520*4882a593Smuzhiyun 	}
1521*4882a593Smuzhiyun 	return 0;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun /*
1525*4882a593Smuzhiyun  * free the resources of the card (multi, single, or bridged)
1526*4882a593Smuzhiyun  * Parameters: slot, flag to say if this is for removing entire module or just
1527*4882a593Smuzhiyun  * unconfiguring the device
1528*4882a593Smuzhiyun  * TO DO:  will probably need to add some code in case there was some resource,
1529*4882a593Smuzhiyun  * to remove it... this is from when we have errors in the configure_card...
1530*4882a593Smuzhiyun  *			!!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1531*4882a593Smuzhiyun  * Returns: 0, -1, -ENODEV
1532*4882a593Smuzhiyun  */
ibmphp_unconfigure_card(struct slot ** slot_cur,int the_end)1533*4882a593Smuzhiyun int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	int i;
1536*4882a593Smuzhiyun 	int count;
1537*4882a593Smuzhiyun 	int rc;
1538*4882a593Smuzhiyun 	struct slot *sl = *slot_cur;
1539*4882a593Smuzhiyun 	struct pci_func *cur_func = NULL;
1540*4882a593Smuzhiyun 	struct pci_func *temp_func;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	debug("%s - enter\n", __func__);
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	if (!the_end) {
1545*4882a593Smuzhiyun 		/* Need to unconfigure the card */
1546*4882a593Smuzhiyun 		rc = unconfigure_boot_card(sl);
1547*4882a593Smuzhiyun 		if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1548*4882a593Smuzhiyun 			/* In all other cases, will still need to get rid of func structure if it exists */
1549*4882a593Smuzhiyun 			return rc;
1550*4882a593Smuzhiyun 		}
1551*4882a593Smuzhiyun 	}
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	if (sl->func) {
1554*4882a593Smuzhiyun 		cur_func = sl->func;
1555*4882a593Smuzhiyun 		while (cur_func) {
1556*4882a593Smuzhiyun 			/* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1557*4882a593Smuzhiyun 			if (cur_func->bus) {
1558*4882a593Smuzhiyun 				/* in other words, it's a PPB */
1559*4882a593Smuzhiyun 				count = 2;
1560*4882a593Smuzhiyun 			} else {
1561*4882a593Smuzhiyun 				count = 6;
1562*4882a593Smuzhiyun 			}
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 			for (i = 0; i < count; i++) {
1565*4882a593Smuzhiyun 				if (cur_func->io[i]) {
1566*4882a593Smuzhiyun 					debug("io[%d] exists\n", i);
1567*4882a593Smuzhiyun 					if (the_end > 0)
1568*4882a593Smuzhiyun 						ibmphp_remove_resource(cur_func->io[i]);
1569*4882a593Smuzhiyun 					cur_func->io[i] = NULL;
1570*4882a593Smuzhiyun 				}
1571*4882a593Smuzhiyun 				if (cur_func->mem[i]) {
1572*4882a593Smuzhiyun 					debug("mem[%d] exists\n", i);
1573*4882a593Smuzhiyun 					if (the_end > 0)
1574*4882a593Smuzhiyun 						ibmphp_remove_resource(cur_func->mem[i]);
1575*4882a593Smuzhiyun 					cur_func->mem[i] = NULL;
1576*4882a593Smuzhiyun 				}
1577*4882a593Smuzhiyun 				if (cur_func->pfmem[i]) {
1578*4882a593Smuzhiyun 					debug("pfmem[%d] exists\n", i);
1579*4882a593Smuzhiyun 					if (the_end > 0)
1580*4882a593Smuzhiyun 						ibmphp_remove_resource(cur_func->pfmem[i]);
1581*4882a593Smuzhiyun 					cur_func->pfmem[i] = NULL;
1582*4882a593Smuzhiyun 				}
1583*4882a593Smuzhiyun 			}
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 			temp_func = cur_func->next;
1586*4882a593Smuzhiyun 			kfree(cur_func);
1587*4882a593Smuzhiyun 			cur_func = temp_func;
1588*4882a593Smuzhiyun 		}
1589*4882a593Smuzhiyun 	}
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	sl->func = NULL;
1592*4882a593Smuzhiyun 	*slot_cur = sl;
1593*4882a593Smuzhiyun 	debug("%s - exit\n", __func__);
1594*4882a593Smuzhiyun 	return 0;
1595*4882a593Smuzhiyun }
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun /*
1598*4882a593Smuzhiyun  * add a new bus resulting from hot-plugging a PPB bridge with devices
1599*4882a593Smuzhiyun  *
1600*4882a593Smuzhiyun  * Input: bus and the amount of resources needed (we know we can assign those,
1601*4882a593Smuzhiyun  *        since they've been checked already
1602*4882a593Smuzhiyun  * Output: bus added to the correct spot
1603*4882a593Smuzhiyun  *         0, -1, error
1604*4882a593Smuzhiyun  */
add_new_bus(struct bus_node * bus,struct resource_node * io,struct resource_node * mem,struct resource_node * pfmem,u8 parent_busno)1605*4882a593Smuzhiyun static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun 	struct range_node *io_range = NULL;
1608*4882a593Smuzhiyun 	struct range_node *mem_range = NULL;
1609*4882a593Smuzhiyun 	struct range_node *pfmem_range = NULL;
1610*4882a593Smuzhiyun 	struct bus_node *cur_bus = NULL;
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 	/* Trying to find the parent bus number */
1613*4882a593Smuzhiyun 	if (parent_busno != 0xFF) {
1614*4882a593Smuzhiyun 		cur_bus	= ibmphp_find_res_bus(parent_busno);
1615*4882a593Smuzhiyun 		if (!cur_bus) {
1616*4882a593Smuzhiyun 			err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1617*4882a593Smuzhiyun 			return -ENODEV;
1618*4882a593Smuzhiyun 		}
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 		list_add(&bus->bus_list, &cur_bus->bus_list);
1621*4882a593Smuzhiyun 	}
1622*4882a593Smuzhiyun 	if (io) {
1623*4882a593Smuzhiyun 		io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1624*4882a593Smuzhiyun 		if (!io_range)
1625*4882a593Smuzhiyun 			return -ENOMEM;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 		io_range->start = io->start;
1628*4882a593Smuzhiyun 		io_range->end = io->end;
1629*4882a593Smuzhiyun 		io_range->rangeno = 1;
1630*4882a593Smuzhiyun 		bus->noIORanges = 1;
1631*4882a593Smuzhiyun 		bus->rangeIO = io_range;
1632*4882a593Smuzhiyun 	}
1633*4882a593Smuzhiyun 	if (mem) {
1634*4882a593Smuzhiyun 		mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1635*4882a593Smuzhiyun 		if (!mem_range)
1636*4882a593Smuzhiyun 			return -ENOMEM;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 		mem_range->start = mem->start;
1639*4882a593Smuzhiyun 		mem_range->end = mem->end;
1640*4882a593Smuzhiyun 		mem_range->rangeno = 1;
1641*4882a593Smuzhiyun 		bus->noMemRanges = 1;
1642*4882a593Smuzhiyun 		bus->rangeMem = mem_range;
1643*4882a593Smuzhiyun 	}
1644*4882a593Smuzhiyun 	if (pfmem) {
1645*4882a593Smuzhiyun 		pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1646*4882a593Smuzhiyun 		if (!pfmem_range)
1647*4882a593Smuzhiyun 			return -ENOMEM;
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun 		pfmem_range->start = pfmem->start;
1650*4882a593Smuzhiyun 		pfmem_range->end = pfmem->end;
1651*4882a593Smuzhiyun 		pfmem_range->rangeno = 1;
1652*4882a593Smuzhiyun 		bus->noPFMemRanges = 1;
1653*4882a593Smuzhiyun 		bus->rangePFMem = pfmem_range;
1654*4882a593Smuzhiyun 	}
1655*4882a593Smuzhiyun 	return 0;
1656*4882a593Smuzhiyun }
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun /*
1659*4882a593Smuzhiyun  * find the 1st available bus number for PPB to set as its secondary bus
1660*4882a593Smuzhiyun  * Parameters: bus_number of the primary bus
1661*4882a593Smuzhiyun  * Returns: bus_number of the secondary bus or 0xff in case of failure
1662*4882a593Smuzhiyun  */
find_sec_number(u8 primary_busno,u8 slotno)1663*4882a593Smuzhiyun static u8 find_sec_number(u8 primary_busno, u8 slotno)
1664*4882a593Smuzhiyun {
1665*4882a593Smuzhiyun 	int min, max;
1666*4882a593Smuzhiyun 	u8 busno;
1667*4882a593Smuzhiyun 	struct bus_info *bus;
1668*4882a593Smuzhiyun 	struct bus_node *bus_cur;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	bus = ibmphp_find_same_bus_num(primary_busno);
1671*4882a593Smuzhiyun 	if (!bus) {
1672*4882a593Smuzhiyun 		err("cannot get slot range of the bus from the BIOS\n");
1673*4882a593Smuzhiyun 		return 0xff;
1674*4882a593Smuzhiyun 	}
1675*4882a593Smuzhiyun 	max = bus->slot_max;
1676*4882a593Smuzhiyun 	min = bus->slot_min;
1677*4882a593Smuzhiyun 	if ((slotno > max) || (slotno < min)) {
1678*4882a593Smuzhiyun 		err("got the wrong range\n");
1679*4882a593Smuzhiyun 		return 0xff;
1680*4882a593Smuzhiyun 	}
1681*4882a593Smuzhiyun 	busno = (u8) (slotno - (u8) min);
1682*4882a593Smuzhiyun 	busno += primary_busno + 0x01;
1683*4882a593Smuzhiyun 	bus_cur = ibmphp_find_res_bus(busno);
1684*4882a593Smuzhiyun 	/* either there is no such bus number, or there are no ranges, which
1685*4882a593Smuzhiyun 	 * can only happen if we removed the bridged device in previous load
1686*4882a593Smuzhiyun 	 * of the driver, and now only have the skeleton bus struct
1687*4882a593Smuzhiyun 	 */
1688*4882a593Smuzhiyun 	if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1689*4882a593Smuzhiyun 		return busno;
1690*4882a593Smuzhiyun 	return 0xff;
1691*4882a593Smuzhiyun }
1692