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