xref: /linux/drivers/pci/hotplug/cpqphp_pci.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Compaq Hot Plug Controller Driver
4  *
5  * Copyright (C) 1995,2001 Compaq Computer Corporation
6  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (C) 2001 IBM Corp.
8  *
9  * All rights reserved.
10  *
11  * Send feedback to <greg@kroah.com>
12  *
13  */
14 
15 #define pr_fmt(fmt) "cpqphp: " fmt
16 
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/printk.h>
20 #include <linux/types.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <linux/proc_fs.h>
24 #include <linux/pci.h>
25 #include <linux/pci_hotplug.h>
26 #include "../pci.h"
27 #include "cpqphp.h"
28 #include "cpqphp_nvram.h"
29 
30 
31 u8 cpqhp_nic_irq;
32 u8 cpqhp_disk_irq;
33 
34 static u16 unused_IRQ;
35 
36 /*
37  * detect_HRT_floating_pointer
38  *
39  * find the Hot Plug Resource Table in the specified region of memory.
40  *
41  */
42 static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
43 {
44 	void __iomem *fp;
45 	void __iomem *endp;
46 	u8 temp1, temp2, temp3, temp4;
47 	int status = 0;
48 
49 	endp = (end - sizeof(struct hrt) + 1);
50 
51 	for (fp = begin; fp <= endp; fp += 16) {
52 		temp1 = readb(fp + SIG0);
53 		temp2 = readb(fp + SIG1);
54 		temp3 = readb(fp + SIG2);
55 		temp4 = readb(fp + SIG3);
56 		if (temp1 == '$' &&
57 		    temp2 == 'H' &&
58 		    temp3 == 'R' &&
59 		    temp4 == 'T') {
60 			status = 1;
61 			break;
62 		}
63 	}
64 
65 	if (!status)
66 		fp = NULL;
67 
68 	dbg("Discovered Hotplug Resource Table at %p\n", fp);
69 	return fp;
70 }
71 
72 
73 int cpqhp_configure_device(struct controller *ctrl, struct pci_func *func)
74 {
75 	struct pci_bus *child;
76 	int num;
77 
78 	pci_lock_rescan_remove();
79 
80 	if (func->pci_dev == NULL)
81 		func->pci_dev = pci_get_domain_bus_and_slot(0, func->bus,
82 							PCI_DEVFN(func->device,
83 							func->function));
84 
85 	/* No pci device, we need to create it then */
86 	if (func->pci_dev == NULL) {
87 		dbg("INFO: pci_dev still null\n");
88 
89 		num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
90 		if (num)
91 			pci_bus_add_devices(ctrl->pci_dev->bus);
92 
93 		func->pci_dev = pci_get_domain_bus_and_slot(0, func->bus,
94 							PCI_DEVFN(func->device,
95 							func->function));
96 		if (func->pci_dev == NULL) {
97 			dbg("ERROR: pci_dev still null\n");
98 			goto out;
99 		}
100 	}
101 
102 	if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
103 		pci_hp_add_bridge(func->pci_dev);
104 		child = func->pci_dev->subordinate;
105 		if (child)
106 			pci_bus_add_devices(child);
107 	}
108 
109 	pci_dev_put(func->pci_dev);
110 
111  out:
112 	pci_unlock_rescan_remove();
113 	return 0;
114 }
115 
116 
117 int cpqhp_unconfigure_device(struct pci_func *func)
118 {
119 	int j;
120 
121 	dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
122 
123 	pci_lock_rescan_remove();
124 	for (j = 0; j < 8 ; j++) {
125 		struct pci_dev *temp = pci_get_domain_bus_and_slot(0,
126 							func->bus,
127 							PCI_DEVFN(func->device,
128 							j));
129 		if (temp) {
130 			pci_dev_put(temp);
131 			pci_stop_and_remove_bus_device(temp);
132 		}
133 	}
134 	pci_unlock_rescan_remove();
135 	return 0;
136 }
137 
138 /*
139  * cpqhp_set_irq
140  *
141  * @bus_num: bus number of PCI device
142  * @dev_num: device number of PCI device
143  * @slot: pointer to u8 where slot number will be returned
144  */
145 int cpqhp_set_irq(u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
146 {
147 	int rc = 0;
148 
149 	if (cpqhp_legacy_mode) {
150 		struct pci_dev *fakedev;
151 		struct pci_bus *fakebus;
152 		u16 temp_word;
153 
154 		fakedev = kmalloc_obj(*fakedev, GFP_KERNEL);
155 		fakebus = kmalloc_obj(*fakebus, GFP_KERNEL);
156 		if (!fakedev || !fakebus) {
157 			kfree(fakedev);
158 			kfree(fakebus);
159 			return -ENOMEM;
160 		}
161 
162 		fakedev->devfn = dev_num << 3;
163 		fakedev->bus = fakebus;
164 		fakebus->number = bus_num;
165 		dbg("%s: dev %d, bus %d, pin %d, num %d\n",
166 		    __func__, dev_num, bus_num, int_pin, irq_num);
167 		rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
168 		kfree(fakedev);
169 		kfree(fakebus);
170 		dbg("%s: rc %d\n", __func__, rc);
171 		if (!rc)
172 			return !rc;
173 
174 		/* set the Edge Level Control Register (ELCR) */
175 		temp_word = inb(0x4d0);
176 		temp_word |= inb(0x4d1) << 8;
177 
178 		temp_word |= 0x01 << irq_num;
179 
180 		/* This should only be for x86 as it sets the Edge Level
181 		 * Control Register
182 		 */
183 		outb((u8)(temp_word & 0xFF), 0x4d0);
184 		outb((u8)((temp_word & 0xFF00) >> 8), 0x4d1);
185 		rc = 0;
186 	}
187 
188 	return rc;
189 }
190 
191 
192 static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 *dev_num)
193 {
194 	u16 tdevice;
195 	u32 work;
196 	int ret = -1;
197 
198 	ctrl->pci_bus->number = bus_num;
199 
200 	for (tdevice = 0; tdevice < 0xFF; tdevice++) {
201 		/* Scan for access first */
202 		if (!pci_bus_read_dev_vendor_id(ctrl->pci_bus, tdevice, &work, 0))
203 			continue;
204 		ret = pci_bus_read_config_dword(ctrl->pci_bus, tdevice, PCI_CLASS_REVISION, &work);
205 		if (ret)
206 			continue;
207 		dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
208 		/* Yep we got one. Not a bridge ? */
209 		if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
210 			*dev_num = tdevice;
211 			dbg("found it !\n");
212 			return 0;
213 		} else {
214 			/*
215 			 * XXX: Code whose debug printout indicated
216 			 * recursion to buses underneath bridges might be
217 			 * necessary was removed because it never did
218 			 * any recursion.
219 			 */
220 			ret = 0;
221 			pr_warn("missing feature: bridge scan recursion not implemented\n");
222 		}
223 	}
224 
225 
226 	return ret;
227 }
228 
229 
230 static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
231 {
232 	int loop, len;
233 	u32 work;
234 	u8 tbus, tdevice, tslot;
235 
236 	len = cpqhp_routing_table_length();
237 	for (loop = 0; loop < len; ++loop) {
238 		tbus = cpqhp_routing_table->slots[loop].bus;
239 		tdevice = cpqhp_routing_table->slots[loop].devfn;
240 		tslot = cpqhp_routing_table->slots[loop].slot;
241 
242 		if (tslot == slot) {
243 			*bus_num = tbus;
244 			*dev_num = tdevice;
245 			ctrl->pci_bus->number = tbus;
246 			pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
247 			if (!nobridge || PCI_POSSIBLE_ERROR(work))
248 				return 0;
249 
250 			dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
251 			pci_bus_read_config_dword(ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
252 			dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
253 
254 			if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
255 				pci_bus_read_config_byte(ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
256 				dbg("Scan bus for Non Bridge: bus %d\n", tbus);
257 				if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
258 					*bus_num = tbus;
259 					return 0;
260 				}
261 			} else
262 				return 0;
263 		}
264 	}
265 	return -1;
266 }
267 
268 
269 int cpqhp_get_bus_dev(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot)
270 {
271 	/* plain (bridges allowed) */
272 	return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
273 }
274 
275 
276 /* More PCI configuration routines; this time centered around hotplug
277  * controller
278  */
279 
280 
281 /*
282  * cpqhp_save_config
283  *
284  * Reads configuration for all slots in a PCI bus and saves info.
285  *
286  * Note:  For non-hot plug buses, the slot # saved is the device #
287  *
288  * returns 0 if success
289  */
290 int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
291 {
292 	long rc;
293 	u8 class_code;
294 	u8 header_type;
295 	u32 ID;
296 	u8 secondary_bus;
297 	struct pci_func *new_slot;
298 	int sub_bus;
299 	int FirstSupported;
300 	int LastSupported;
301 	int max_functions;
302 	int function;
303 	u8 DevError;
304 	int device = 0;
305 	int cloop = 0;
306 	int stop_it;
307 	int index;
308 	u16 devfn;
309 
310 	/* Decide which slots are supported */
311 
312 	if (is_hot_plug) {
313 		/*
314 		 * is_hot_plug is the slot mask
315 		 */
316 		FirstSupported = is_hot_plug >> 4;
317 		LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
318 	} else {
319 		FirstSupported = 0;
320 		LastSupported = 0x1F;
321 	}
322 
323 	/* Save PCI configuration space for all devices in supported slots */
324 	ctrl->pci_bus->number = busnumber;
325 	for (device = FirstSupported; device <= LastSupported; device++) {
326 		ID = 0xFFFFFFFF;
327 		rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
328 
329 		if (ID == 0xFFFFFFFF) {
330 			if (is_hot_plug) {
331 				/* Setup slot structure with entry for empty
332 				 * slot
333 				 */
334 				new_slot = cpqhp_slot_create(busnumber);
335 				if (new_slot == NULL)
336 					return 1;
337 
338 				new_slot->bus = (u8) busnumber;
339 				new_slot->device = (u8) device;
340 				new_slot->function = 0;
341 				new_slot->is_a_board = 0;
342 				new_slot->presence_save = 0;
343 				new_slot->switch_save = 0;
344 			}
345 			continue;
346 		}
347 
348 		rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
349 		if (rc)
350 			return rc;
351 
352 		rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
353 		if (rc)
354 			return rc;
355 
356 		/* If multi-function device, set max_functions to 8 */
357 		if (header_type & PCI_HEADER_TYPE_MFD)
358 			max_functions = 8;
359 		else
360 			max_functions = 1;
361 
362 		function = 0;
363 
364 		do {
365 			DevError = 0;
366 			if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
367 				/* Recurse the subordinate bus
368 				 * get the subordinate bus number
369 				 */
370 				rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
371 				if (rc) {
372 					return rc;
373 				} else {
374 					sub_bus = (int) secondary_bus;
375 
376 					/* Save secondary bus cfg spc
377 					 * with this recursive call.
378 					 */
379 					rc = cpqhp_save_config(ctrl, sub_bus, 0);
380 					if (rc)
381 						return rc;
382 					ctrl->pci_bus->number = busnumber;
383 				}
384 			}
385 
386 			index = 0;
387 			new_slot = cpqhp_slot_find(busnumber, device, index++);
388 			while (new_slot &&
389 			       (new_slot->function != (u8) function))
390 				new_slot = cpqhp_slot_find(busnumber, device, index++);
391 
392 			if (!new_slot) {
393 				/* Setup slot structure. */
394 				new_slot = cpqhp_slot_create(busnumber);
395 				if (new_slot == NULL)
396 					return 1;
397 			}
398 
399 			new_slot->bus = (u8) busnumber;
400 			new_slot->device = (u8) device;
401 			new_slot->function = (u8) function;
402 			new_slot->is_a_board = 1;
403 			new_slot->switch_save = 0x10;
404 			/* In case of unsupported board */
405 			new_slot->status = DevError;
406 			devfn = (new_slot->device << 3) | new_slot->function;
407 			new_slot->pci_dev = pci_get_domain_bus_and_slot(0,
408 							new_slot->bus, devfn);
409 
410 			for (cloop = 0; cloop < 0x20; cloop++) {
411 				rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
412 				if (rc)
413 					return rc;
414 			}
415 
416 			pci_dev_put(new_slot->pci_dev);
417 
418 			function++;
419 
420 			stop_it = 0;
421 
422 			/* this loop skips to the next present function
423 			 * reading in Class Code and Header type.
424 			 */
425 			while ((function < max_functions) && (!stop_it)) {
426 				rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
427 				if (ID == 0xFFFFFFFF) {
428 					function++;
429 					continue;
430 				}
431 				rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
432 				if (rc)
433 					return rc;
434 
435 				rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
436 				if (rc)
437 					return rc;
438 
439 				stop_it++;
440 			}
441 
442 		} while (function < max_functions);
443 	}			/* End of FOR loop */
444 
445 	return 0;
446 }
447 
448 
449 /*
450  * cpqhp_save_slot_config
451  *
452  * Saves configuration info for all PCI devices in a given slot
453  * including subordinate buses.
454  *
455  * returns 0 if success
456  */
457 int cpqhp_save_slot_config(struct controller *ctrl, struct pci_func *new_slot)
458 {
459 	long rc;
460 	u8 class_code;
461 	u8 header_type;
462 	u32 ID;
463 	u8 secondary_bus;
464 	int sub_bus;
465 	int max_functions;
466 	int function = 0;
467 	int cloop;
468 	int stop_it;
469 
470 	ID = 0xFFFFFFFF;
471 
472 	ctrl->pci_bus->number = new_slot->bus;
473 	pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
474 
475 	if (ID == 0xFFFFFFFF)
476 		return 2;
477 
478 	pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
479 	pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
480 
481 	if (header_type & PCI_HEADER_TYPE_MFD)
482 		max_functions = 8;
483 	else
484 		max_functions = 1;
485 
486 	while (function < max_functions) {
487 		if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
488 			/*  Recurse the subordinate bus */
489 			pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
490 
491 			sub_bus = (int) secondary_bus;
492 
493 			/* Save the config headers for the secondary
494 			 * bus.
495 			 */
496 			rc = cpqhp_save_config(ctrl, sub_bus, 0);
497 			if (rc)
498 				return(rc);
499 			ctrl->pci_bus->number = new_slot->bus;
500 
501 		}
502 
503 		new_slot->status = 0;
504 
505 		for (cloop = 0; cloop < 0x20; cloop++)
506 			pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) &(new_slot->config_space[cloop]));
507 
508 		function++;
509 
510 		stop_it = 0;
511 
512 		/* this loop skips to the next present function
513 		 * reading in the Class Code and the Header type.
514 		 */
515 		while ((function < max_functions) && (!stop_it)) {
516 			pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
517 
518 			if (ID == 0xFFFFFFFF)
519 				function++;
520 			else {
521 				pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
522 				pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
523 				stop_it++;
524 			}
525 		}
526 
527 	}
528 
529 	return 0;
530 }
531 
532 
533 /*
534  * cpqhp_save_base_addr_length
535  *
536  * Saves the length of all base address registers for the
537  * specified slot.  this is for hot plug REPLACE
538  *
539  * returns 0 if success
540  */
541 int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func *func)
542 {
543 	u8 cloop;
544 	u8 header_type;
545 	u8 secondary_bus;
546 	u8 type;
547 	int sub_bus;
548 	u32 temp_register;
549 	u32 base;
550 	u32 rc;
551 	struct pci_func *next;
552 	int index = 0;
553 	struct pci_bus *pci_bus = ctrl->pci_bus;
554 	unsigned int devfn;
555 
556 	func = cpqhp_slot_find(func->bus, func->device, index++);
557 
558 	while (func != NULL) {
559 		pci_bus->number = func->bus;
560 		devfn = PCI_DEVFN(func->device, func->function);
561 
562 		/* Check for Bridge */
563 		pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
564 
565 		if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
566 			pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
567 
568 			sub_bus = (int) secondary_bus;
569 
570 			next = cpqhp_slot_list[sub_bus];
571 
572 			while (next != NULL) {
573 				rc = cpqhp_save_base_addr_length(ctrl, next);
574 				if (rc)
575 					return rc;
576 
577 				next = next->next;
578 			}
579 			pci_bus->number = func->bus;
580 
581 			/* FIXME: this loop is duplicated in the non-bridge
582 			 * case.  The two could be rolled together Figure out
583 			 * IO and memory base lengths
584 			 */
585 			for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
586 				temp_register = 0xFFFFFFFF;
587 				pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
588 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
589 				/* If this register is implemented */
590 				if (base) {
591 					if (base & 0x01L) {
592 						/* IO base
593 						 * set base = amount of IO space
594 						 * requested
595 						 */
596 						base = base & 0xFFFFFFFE;
597 						base = (~base) + 1;
598 
599 						type = 1;
600 					} else {
601 						/* memory base */
602 						base = base & 0xFFFFFFF0;
603 						base = (~base) + 1;
604 
605 						type = 0;
606 					}
607 				} else {
608 					base = 0x0L;
609 					type = 0;
610 				}
611 
612 				/* Save information in slot structure */
613 				func->base_length[(cloop - 0x10) >> 2] =
614 				base;
615 				func->base_type[(cloop - 0x10) >> 2] = type;
616 
617 			}	/* End of base register loop */
618 
619 		} else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
620 			/* Figure out IO and memory base lengths */
621 			for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
622 				temp_register = 0xFFFFFFFF;
623 				pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
624 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
625 
626 				/* If this register is implemented */
627 				if (base) {
628 					if (base & 0x01L) {
629 						/* IO base
630 						 * base = amount of IO space
631 						 * requested
632 						 */
633 						base = base & 0xFFFFFFFE;
634 						base = (~base) + 1;
635 
636 						type = 1;
637 					} else {
638 						/* memory base
639 						 * base = amount of memory
640 						 * space requested
641 						 */
642 						base = base & 0xFFFFFFF0;
643 						base = (~base) + 1;
644 
645 						type = 0;
646 					}
647 				} else {
648 					base = 0x0L;
649 					type = 0;
650 				}
651 
652 				/* Save information in slot structure */
653 				func->base_length[(cloop - 0x10) >> 2] = base;
654 				func->base_type[(cloop - 0x10) >> 2] = type;
655 
656 			}	/* End of base register loop */
657 
658 		} else {	  /* Some other unknown header type */
659 		}
660 
661 		/* find the next device in this slot */
662 		func = cpqhp_slot_find(func->bus, func->device, index++);
663 	}
664 
665 	return(0);
666 }
667 
668 
669 /*
670  * cpqhp_save_used_resources
671  *
672  * Stores used resource information for existing boards.  this is
673  * for boards that were in the system when this driver was loaded.
674  * this function is for hot plug ADD
675  *
676  * returns 0 if success
677  */
678 int cpqhp_save_used_resources(struct controller *ctrl, struct pci_func *func)
679 {
680 	u8 cloop;
681 	u8 header_type;
682 	u8 secondary_bus;
683 	u8 temp_byte;
684 	u8 b_base;
685 	u8 b_length;
686 	u16 command;
687 	u16 save_command;
688 	u16 w_base;
689 	u16 w_length;
690 	u32 temp_register;
691 	u32 save_base;
692 	u32 base;
693 	int index = 0;
694 	struct pci_resource *mem_node;
695 	struct pci_resource *p_mem_node;
696 	struct pci_resource *io_node;
697 	struct pci_resource *bus_node;
698 	struct pci_bus *pci_bus = ctrl->pci_bus;
699 	unsigned int devfn;
700 
701 	func = cpqhp_slot_find(func->bus, func->device, index++);
702 
703 	while ((func != NULL) && func->is_a_board) {
704 		pci_bus->number = func->bus;
705 		devfn = PCI_DEVFN(func->device, func->function);
706 
707 		/* Save the command register */
708 		pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
709 
710 		/* disable card */
711 		command = 0x00;
712 		pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
713 
714 		/* Check for Bridge */
715 		pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
716 
717 		if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
718 			/* Clear Bridge Control Register */
719 			command = 0x00;
720 			pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
721 			pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
722 			pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
723 
724 			bus_node = kmalloc_obj(*bus_node, GFP_KERNEL);
725 			if (!bus_node)
726 				return -ENOMEM;
727 
728 			bus_node->base = secondary_bus;
729 			bus_node->length = temp_byte - secondary_bus + 1;
730 
731 			bus_node->next = func->bus_head;
732 			func->bus_head = bus_node;
733 
734 			/* Save IO base and Limit registers */
735 			pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
736 			pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
737 
738 			if ((b_base <= b_length) && (save_command & 0x01)) {
739 				io_node = kmalloc_obj(*io_node, GFP_KERNEL);
740 				if (!io_node)
741 					return -ENOMEM;
742 
743 				io_node->base = (b_base & 0xF0) << 8;
744 				io_node->length = (b_length - b_base + 0x10) << 8;
745 
746 				io_node->next = func->io_head;
747 				func->io_head = io_node;
748 			}
749 
750 			/* Save memory base and Limit registers */
751 			pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
752 			pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
753 
754 			if ((w_base <= w_length) && (save_command & 0x02)) {
755 				mem_node = kmalloc_obj(*mem_node, GFP_KERNEL);
756 				if (!mem_node)
757 					return -ENOMEM;
758 
759 				mem_node->base = w_base << 16;
760 				mem_node->length = (w_length - w_base + 0x10) << 16;
761 
762 				mem_node->next = func->mem_head;
763 				func->mem_head = mem_node;
764 			}
765 
766 			/* Save prefetchable memory base and Limit registers */
767 			pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
768 			pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
769 
770 			if ((w_base <= w_length) && (save_command & 0x02)) {
771 				p_mem_node = kmalloc_obj(*p_mem_node,
772 							 GFP_KERNEL);
773 				if (!p_mem_node)
774 					return -ENOMEM;
775 
776 				p_mem_node->base = w_base << 16;
777 				p_mem_node->length = (w_length - w_base + 0x10) << 16;
778 
779 				p_mem_node->next = func->p_mem_head;
780 				func->p_mem_head = p_mem_node;
781 			}
782 			/* Figure out IO and memory base lengths */
783 			for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
784 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
785 
786 				temp_register = 0xFFFFFFFF;
787 				pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
788 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
789 
790 				temp_register = base;
791 
792 				/* If this register is implemented */
793 				if (base) {
794 					if (((base & 0x03L) == 0x01)
795 					    && (save_command & 0x01)) {
796 						/* IO base
797 						 * set temp_register = amount
798 						 * of IO space requested
799 						 */
800 						temp_register = base & 0xFFFFFFFE;
801 						temp_register = (~temp_register) + 1;
802 
803 						io_node = kmalloc_obj(*io_node,
804 								      GFP_KERNEL);
805 						if (!io_node)
806 							return -ENOMEM;
807 
808 						io_node->base =
809 						save_base & (~0x03L);
810 						io_node->length = temp_register;
811 
812 						io_node->next = func->io_head;
813 						func->io_head = io_node;
814 					} else
815 						if (((base & 0x0BL) == 0x08)
816 						    && (save_command & 0x02)) {
817 						/* prefetchable memory base */
818 						temp_register = base & 0xFFFFFFF0;
819 						temp_register = (~temp_register) + 1;
820 
821 						p_mem_node = kmalloc_obj(*p_mem_node,
822 									 GFP_KERNEL);
823 						if (!p_mem_node)
824 							return -ENOMEM;
825 
826 						p_mem_node->base = save_base & (~0x0FL);
827 						p_mem_node->length = temp_register;
828 
829 						p_mem_node->next = func->p_mem_head;
830 						func->p_mem_head = p_mem_node;
831 					} else
832 						if (((base & 0x0BL) == 0x00)
833 						    && (save_command & 0x02)) {
834 						/* prefetchable memory base */
835 						temp_register = base & 0xFFFFFFF0;
836 						temp_register = (~temp_register) + 1;
837 
838 						mem_node = kmalloc_obj(*mem_node,
839 								       GFP_KERNEL);
840 						if (!mem_node)
841 							return -ENOMEM;
842 
843 						mem_node->base = save_base & (~0x0FL);
844 						mem_node->length = temp_register;
845 
846 						mem_node->next = func->mem_head;
847 						func->mem_head = mem_node;
848 					} else
849 						return(1);
850 				}
851 			}	/* End of base register loop */
852 		/* Standard header */
853 		} else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
854 			/* Figure out IO and memory base lengths */
855 			for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
856 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
857 
858 				temp_register = 0xFFFFFFFF;
859 				pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
860 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
861 
862 				temp_register = base;
863 
864 				/* If this register is implemented */
865 				if (base) {
866 					if (((base & 0x03L) == 0x01)
867 					    && (save_command & 0x01)) {
868 						/* IO base
869 						 * set temp_register = amount
870 						 * of IO space requested
871 						 */
872 						temp_register = base & 0xFFFFFFFE;
873 						temp_register = (~temp_register) + 1;
874 
875 						io_node = kmalloc_obj(*io_node,
876 								      GFP_KERNEL);
877 						if (!io_node)
878 							return -ENOMEM;
879 
880 						io_node->base = save_base & (~0x01L);
881 						io_node->length = temp_register;
882 
883 						io_node->next = func->io_head;
884 						func->io_head = io_node;
885 					} else
886 						if (((base & 0x0BL) == 0x08)
887 						    && (save_command & 0x02)) {
888 						/* prefetchable memory base */
889 						temp_register = base & 0xFFFFFFF0;
890 						temp_register = (~temp_register) + 1;
891 
892 						p_mem_node = kmalloc_obj(*p_mem_node,
893 									 GFP_KERNEL);
894 						if (!p_mem_node)
895 							return -ENOMEM;
896 
897 						p_mem_node->base = save_base & (~0x0FL);
898 						p_mem_node->length = temp_register;
899 
900 						p_mem_node->next = func->p_mem_head;
901 						func->p_mem_head = p_mem_node;
902 					} else
903 						if (((base & 0x0BL) == 0x00)
904 						    && (save_command & 0x02)) {
905 						/* prefetchable memory base */
906 						temp_register = base & 0xFFFFFFF0;
907 						temp_register = (~temp_register) + 1;
908 
909 						mem_node = kmalloc_obj(*mem_node,
910 								       GFP_KERNEL);
911 						if (!mem_node)
912 							return -ENOMEM;
913 
914 						mem_node->base = save_base & (~0x0FL);
915 						mem_node->length = temp_register;
916 
917 						mem_node->next = func->mem_head;
918 						func->mem_head = mem_node;
919 					} else
920 						return(1);
921 				}
922 			}	/* End of base register loop */
923 		}
924 
925 		/* find the next device in this slot */
926 		func = cpqhp_slot_find(func->bus, func->device, index++);
927 	}
928 
929 	return 0;
930 }
931 
932 
933 /*
934  * cpqhp_configure_board
935  *
936  * Copies saved configuration information to one slot.
937  * this is called recursively for bridge devices.
938  * this is for hot plug REPLACE!
939  *
940  * returns 0 if success
941  */
942 int cpqhp_configure_board(struct controller *ctrl, struct pci_func *func)
943 {
944 	int cloop;
945 	u8 header_type;
946 	u8 secondary_bus;
947 	int sub_bus;
948 	struct pci_func *next;
949 	u32 temp;
950 	u32 rc;
951 	int index = 0;
952 	struct pci_bus *pci_bus = ctrl->pci_bus;
953 	unsigned int devfn;
954 
955 	func = cpqhp_slot_find(func->bus, func->device, index++);
956 
957 	while (func != NULL) {
958 		pci_bus->number = func->bus;
959 		devfn = PCI_DEVFN(func->device, func->function);
960 
961 		/* Start at the top of config space so that the control
962 		 * registers are programmed last
963 		 */
964 		for (cloop = 0x3C; cloop > 0; cloop -= 4)
965 			pci_bus_write_config_dword(pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
966 
967 		pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
968 
969 		/* If this is a bridge device, restore subordinate devices */
970 		if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
971 			pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
972 
973 			sub_bus = (int) secondary_bus;
974 
975 			next = cpqhp_slot_list[sub_bus];
976 
977 			while (next != NULL) {
978 				rc = cpqhp_configure_board(ctrl, next);
979 				if (rc)
980 					return rc;
981 
982 				next = next->next;
983 			}
984 		} else {
985 
986 			/* Check all the base Address Registers to make sure
987 			 * they are the same.  If not, the board is different.
988 			 */
989 
990 			for (cloop = 16; cloop < 40; cloop += 4) {
991 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp);
992 
993 				if (temp != func->config_space[cloop >> 2]) {
994 					dbg("Config space compare failure!!! offset = %x\n", cloop);
995 					dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
996 					dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
997 					return 1;
998 				}
999 			}
1000 		}
1001 
1002 		func->configured = 1;
1003 
1004 		func = cpqhp_slot_find(func->bus, func->device, index++);
1005 	}
1006 
1007 	return 0;
1008 }
1009 
1010 
1011 /*
1012  * cpqhp_valid_replace
1013  *
1014  * this function checks to see if a board is the same as the
1015  * one it is replacing.  this check will detect if the device's
1016  * vendor or device id's are the same
1017  *
1018  * returns 0 if the board is the same nonzero otherwise
1019  */
1020 int cpqhp_valid_replace(struct controller *ctrl, struct pci_func *func)
1021 {
1022 	u8 cloop;
1023 	u8 header_type;
1024 	u8 secondary_bus;
1025 	u8 type;
1026 	u32 temp_register = 0;
1027 	u32 base;
1028 	u32 rc;
1029 	struct pci_func *next;
1030 	int index = 0;
1031 	struct pci_bus *pci_bus = ctrl->pci_bus;
1032 	unsigned int devfn;
1033 
1034 	if (!func->is_a_board)
1035 		return(ADD_NOT_SUPPORTED);
1036 
1037 	func = cpqhp_slot_find(func->bus, func->device, index++);
1038 
1039 	while (func != NULL) {
1040 		pci_bus->number = func->bus;
1041 		devfn = PCI_DEVFN(func->device, func->function);
1042 
1043 		pci_bus_read_config_dword(pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1044 
1045 		/* No adapter present */
1046 		if (temp_register == 0xFFFFFFFF)
1047 			return(NO_ADAPTER_PRESENT);
1048 
1049 		if (temp_register != func->config_space[0])
1050 			return(ADAPTER_NOT_SAME);
1051 
1052 		/* Check for same revision number and class code */
1053 		pci_bus_read_config_dword(pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1054 
1055 		/* Adapter not the same */
1056 		if (temp_register != func->config_space[0x08 >> 2])
1057 			return(ADAPTER_NOT_SAME);
1058 
1059 		/* Check for Bridge */
1060 		pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1061 
1062 		if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE) {
1063 			/* In order to continue checking, we must program the
1064 			 * bus registers in the bridge to respond to accesses
1065 			 * for its subordinate bus(es)
1066 			 */
1067 
1068 			temp_register = func->config_space[0x18 >> 2];
1069 			pci_bus_write_config_dword(pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1070 
1071 			secondary_bus = (temp_register >> 8) & 0xFF;
1072 
1073 			next = cpqhp_slot_list[secondary_bus];
1074 
1075 			while (next != NULL) {
1076 				rc = cpqhp_valid_replace(ctrl, next);
1077 				if (rc)
1078 					return rc;
1079 
1080 				next = next->next;
1081 			}
1082 
1083 		}
1084 		/* Check to see if it is a standard config header */
1085 		else if ((header_type & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_NORMAL) {
1086 			/* Check subsystem vendor and ID */
1087 			pci_bus_read_config_dword(pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1088 
1089 			if (temp_register != func->config_space[0x2C >> 2]) {
1090 				/* If it's a SMART-2 and the register isn't
1091 				 * filled in, ignore the difference because
1092 				 * they just have an old rev of the firmware
1093 				 */
1094 				if (!((func->config_space[0] == 0xAE100E11)
1095 				      && (temp_register == 0x00L)))
1096 					return(ADAPTER_NOT_SAME);
1097 			}
1098 			/* Figure out IO and memory base lengths */
1099 			for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1100 				temp_register = 0xFFFFFFFF;
1101 				pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
1102 				pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
1103 
1104 				/* If this register is implemented */
1105 				if (base) {
1106 					if (base & 0x01L) {
1107 						/* IO base
1108 						 * set base = amount of IO
1109 						 * space requested
1110 						 */
1111 						base = base & 0xFFFFFFFE;
1112 						base = (~base) + 1;
1113 
1114 						type = 1;
1115 					} else {
1116 						/* memory base */
1117 						base = base & 0xFFFFFFF0;
1118 						base = (~base) + 1;
1119 
1120 						type = 0;
1121 					}
1122 				} else {
1123 					base = 0x0L;
1124 					type = 0;
1125 				}
1126 
1127 				/* Check information in slot structure */
1128 				if (func->base_length[(cloop - 0x10) >> 2] != base)
1129 					return(ADAPTER_NOT_SAME);
1130 
1131 				if (func->base_type[(cloop - 0x10) >> 2] != type)
1132 					return(ADAPTER_NOT_SAME);
1133 
1134 			}	/* End of base register loop */
1135 
1136 		}		/* End of (type 0 config space) else */
1137 		else {
1138 			/* this is not a type 0 or 1 config space header so
1139 			 * we don't know how to do it
1140 			 */
1141 			return(DEVICE_TYPE_NOT_SUPPORTED);
1142 		}
1143 
1144 		/* Get the next function */
1145 		func = cpqhp_slot_find(func->bus, func->device, index++);
1146 	}
1147 
1148 
1149 	return 0;
1150 }
1151 
1152 
1153 /*
1154  * cpqhp_find_available_resources
1155  *
1156  * Finds available memory, IO, and IRQ resources for programming
1157  * devices which may be added to the system
1158  * this function is for hot plug ADD!
1159  *
1160  * returns 0 if success
1161  */
1162 int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1163 {
1164 	u8 temp;
1165 	u8 populated_slot;
1166 	u8 bridged_slot;
1167 	void __iomem *one_slot;
1168 	void __iomem *rom_resource_table;
1169 	struct pci_func *func = NULL;
1170 	int i = 10, index;
1171 	u32 temp_dword, rc;
1172 	struct pci_resource *mem_node;
1173 	struct pci_resource *p_mem_node;
1174 	struct pci_resource *io_node;
1175 	struct pci_resource *bus_node;
1176 
1177 	rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1178 	dbg("rom_resource_table = %p\n", rom_resource_table);
1179 
1180 	if (rom_resource_table == NULL)
1181 		return -ENODEV;
1182 
1183 	/* Sum all resources and setup resource maps */
1184 	unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1185 	dbg("unused_IRQ = %x\n", unused_IRQ);
1186 
1187 	temp = 0;
1188 	while (unused_IRQ) {
1189 		if (unused_IRQ & 1) {
1190 			cpqhp_disk_irq = temp;
1191 			break;
1192 		}
1193 		unused_IRQ = unused_IRQ >> 1;
1194 		temp++;
1195 	}
1196 
1197 	dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1198 	unused_IRQ = unused_IRQ >> 1;
1199 	temp++;
1200 
1201 	while (unused_IRQ) {
1202 		if (unused_IRQ & 1) {
1203 			cpqhp_nic_irq = temp;
1204 			break;
1205 		}
1206 		unused_IRQ = unused_IRQ >> 1;
1207 		temp++;
1208 	}
1209 
1210 	dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1211 	unused_IRQ = readl(rom_resource_table + PCIIRQ);
1212 
1213 	temp = 0;
1214 
1215 	if (!cpqhp_nic_irq)
1216 		cpqhp_nic_irq = ctrl->cfgspc_irq;
1217 
1218 	if (!cpqhp_disk_irq)
1219 		cpqhp_disk_irq = ctrl->cfgspc_irq;
1220 
1221 	dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1222 
1223 	rc = compaq_nvram_load(rom_start, ctrl);
1224 	if (rc)
1225 		return rc;
1226 
1227 	one_slot = rom_resource_table + sizeof(struct hrt);
1228 
1229 	i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1230 	dbg("number_of_entries = %d\n", i);
1231 
1232 	if (!readb(one_slot + SECONDARY_BUS))
1233 		return 1;
1234 
1235 	dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1236 
1237 	while (i && readb(one_slot + SECONDARY_BUS)) {
1238 		u8 dev_func = readb(one_slot + DEV_FUNC);
1239 		u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1240 		u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1241 		u8 max_bus = readb(one_slot + MAX_BUS);
1242 		u16 io_base = readw(one_slot + IO_BASE);
1243 		u16 io_length = readw(one_slot + IO_LENGTH);
1244 		u16 mem_base = readw(one_slot + MEM_BASE);
1245 		u16 mem_length = readw(one_slot + MEM_LENGTH);
1246 		u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1247 		u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1248 
1249 		dbg("%2.2x | %4.4x  | %4.4x | %4.4x   | %4.4x | %4.4x   | %4.4x |%2.2x %2.2x %2.2x\n",
1250 		    dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1251 		    primary_bus, secondary_bus, max_bus);
1252 
1253 		/* If this entry isn't for our controller's bus, ignore it */
1254 		if (primary_bus != ctrl->bus) {
1255 			i--;
1256 			one_slot += sizeof(struct slot_rt);
1257 			continue;
1258 		}
1259 		/* find out if this entry is for an occupied slot */
1260 		ctrl->pci_bus->number = primary_bus;
1261 		pci_bus_read_config_dword(ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1262 		dbg("temp_D_word = %x\n", temp_dword);
1263 
1264 		if (temp_dword != 0xFFFFFFFF) {
1265 			index = 0;
1266 			func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1267 
1268 			while (func && (func->function != (dev_func & 0x07))) {
1269 				dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1270 				func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1271 			}
1272 
1273 			/* If we can't find a match, skip this table entry */
1274 			if (!func) {
1275 				i--;
1276 				one_slot += sizeof(struct slot_rt);
1277 				continue;
1278 			}
1279 			/* this may not work and shouldn't be used */
1280 			if (secondary_bus != primary_bus)
1281 				bridged_slot = 1;
1282 			else
1283 				bridged_slot = 0;
1284 
1285 			populated_slot = 1;
1286 		} else {
1287 			populated_slot = 0;
1288 			bridged_slot = 0;
1289 		}
1290 
1291 
1292 		/* If we've got a valid IO base, use it */
1293 
1294 		temp_dword = io_base + io_length;
1295 
1296 		if ((io_base) && (temp_dword < 0x10000)) {
1297 			io_node = kmalloc_obj(*io_node, GFP_KERNEL);
1298 			if (!io_node)
1299 				return -ENOMEM;
1300 
1301 			io_node->base = io_base;
1302 			io_node->length = io_length;
1303 
1304 			dbg("found io_node(base, length) = %x, %x\n",
1305 					io_node->base, io_node->length);
1306 			dbg("populated slot = %d\n", populated_slot);
1307 			if (!populated_slot) {
1308 				io_node->next = ctrl->io_head;
1309 				ctrl->io_head = io_node;
1310 			} else {
1311 				io_node->next = func->io_head;
1312 				func->io_head = io_node;
1313 			}
1314 		}
1315 
1316 		/* If we've got a valid memory base, use it */
1317 		temp_dword = mem_base + mem_length;
1318 		if ((mem_base) && (temp_dword < 0x10000)) {
1319 			mem_node = kmalloc_obj(*mem_node, GFP_KERNEL);
1320 			if (!mem_node)
1321 				return -ENOMEM;
1322 
1323 			mem_node->base = mem_base << 16;
1324 
1325 			mem_node->length = mem_length << 16;
1326 
1327 			dbg("found mem_node(base, length) = %x, %x\n",
1328 					mem_node->base, mem_node->length);
1329 			dbg("populated slot = %d\n", populated_slot);
1330 			if (!populated_slot) {
1331 				mem_node->next = ctrl->mem_head;
1332 				ctrl->mem_head = mem_node;
1333 			} else {
1334 				mem_node->next = func->mem_head;
1335 				func->mem_head = mem_node;
1336 			}
1337 		}
1338 
1339 		/* If we've got a valid prefetchable memory base, and
1340 		 * the base + length isn't greater than 0xFFFF
1341 		 */
1342 		temp_dword = pre_mem_base + pre_mem_length;
1343 		if ((pre_mem_base) && (temp_dword < 0x10000)) {
1344 			p_mem_node = kmalloc_obj(*p_mem_node, GFP_KERNEL);
1345 			if (!p_mem_node)
1346 				return -ENOMEM;
1347 
1348 			p_mem_node->base = pre_mem_base << 16;
1349 
1350 			p_mem_node->length = pre_mem_length << 16;
1351 			dbg("found p_mem_node(base, length) = %x, %x\n",
1352 					p_mem_node->base, p_mem_node->length);
1353 			dbg("populated slot = %d\n", populated_slot);
1354 
1355 			if (!populated_slot) {
1356 				p_mem_node->next = ctrl->p_mem_head;
1357 				ctrl->p_mem_head = p_mem_node;
1358 			} else {
1359 				p_mem_node->next = func->p_mem_head;
1360 				func->p_mem_head = p_mem_node;
1361 			}
1362 		}
1363 
1364 		/* If we've got a valid bus number, use it
1365 		 * The second condition is to ignore bus numbers on
1366 		 * populated slots that don't have PCI-PCI bridges
1367 		 */
1368 		if (secondary_bus && (secondary_bus != primary_bus)) {
1369 			bus_node = kmalloc_obj(*bus_node, GFP_KERNEL);
1370 			if (!bus_node)
1371 				return -ENOMEM;
1372 
1373 			bus_node->base = secondary_bus;
1374 			bus_node->length = max_bus - secondary_bus + 1;
1375 			dbg("found bus_node(base, length) = %x, %x\n",
1376 					bus_node->base, bus_node->length);
1377 			dbg("populated slot = %d\n", populated_slot);
1378 			if (!populated_slot) {
1379 				bus_node->next = ctrl->bus_head;
1380 				ctrl->bus_head = bus_node;
1381 			} else {
1382 				bus_node->next = func->bus_head;
1383 				func->bus_head = bus_node;
1384 			}
1385 		}
1386 
1387 		i--;
1388 		one_slot += sizeof(struct slot_rt);
1389 	}
1390 
1391 	/* If all of the following fail, we don't have any resources for
1392 	 * hot plug add
1393 	 */
1394 	rc = 1;
1395 	rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1396 	rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1397 	rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1398 	rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1399 
1400 	return rc;
1401 }
1402 
1403 
1404 /*
1405  * cpqhp_return_board_resources
1406  *
1407  * this routine returns all resources allocated to a board to
1408  * the available pool.
1409  *
1410  * returns 0 if success
1411  */
1412 int cpqhp_return_board_resources(struct pci_func *func, struct resource_lists *resources)
1413 {
1414 	int rc = 0;
1415 	struct pci_resource *node;
1416 	struct pci_resource *t_node;
1417 	dbg("%s\n", __func__);
1418 
1419 	if (!func)
1420 		return 1;
1421 
1422 	node = func->io_head;
1423 	func->io_head = NULL;
1424 	while (node) {
1425 		t_node = node->next;
1426 		return_resource(&(resources->io_head), node);
1427 		node = t_node;
1428 	}
1429 
1430 	node = func->mem_head;
1431 	func->mem_head = NULL;
1432 	while (node) {
1433 		t_node = node->next;
1434 		return_resource(&(resources->mem_head), node);
1435 		node = t_node;
1436 	}
1437 
1438 	node = func->p_mem_head;
1439 	func->p_mem_head = NULL;
1440 	while (node) {
1441 		t_node = node->next;
1442 		return_resource(&(resources->p_mem_head), node);
1443 		node = t_node;
1444 	}
1445 
1446 	node = func->bus_head;
1447 	func->bus_head = NULL;
1448 	while (node) {
1449 		t_node = node->next;
1450 		return_resource(&(resources->bus_head), node);
1451 		node = t_node;
1452 	}
1453 
1454 	rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1455 	rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1456 	rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1457 	rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1458 
1459 	return rc;
1460 }
1461 
1462 
1463 /*
1464  * cpqhp_destroy_resource_list
1465  *
1466  * Puts node back in the resource list pointed to by head
1467  */
1468 void cpqhp_destroy_resource_list(struct resource_lists *resources)
1469 {
1470 	struct pci_resource *res, *tres;
1471 
1472 	res = resources->io_head;
1473 	resources->io_head = NULL;
1474 
1475 	while (res) {
1476 		tres = res;
1477 		res = res->next;
1478 		kfree(tres);
1479 	}
1480 
1481 	res = resources->mem_head;
1482 	resources->mem_head = NULL;
1483 
1484 	while (res) {
1485 		tres = res;
1486 		res = res->next;
1487 		kfree(tres);
1488 	}
1489 
1490 	res = resources->p_mem_head;
1491 	resources->p_mem_head = NULL;
1492 
1493 	while (res) {
1494 		tres = res;
1495 		res = res->next;
1496 		kfree(tres);
1497 	}
1498 
1499 	res = resources->bus_head;
1500 	resources->bus_head = NULL;
1501 
1502 	while (res) {
1503 		tres = res;
1504 		res = res->next;
1505 		kfree(tres);
1506 	}
1507 }
1508 
1509 
1510 /*
1511  * cpqhp_destroy_board_resources
1512  *
1513  * Puts node back in the resource list pointed to by head
1514  */
1515 void cpqhp_destroy_board_resources(struct pci_func *func)
1516 {
1517 	struct pci_resource *res, *tres;
1518 
1519 	res = func->io_head;
1520 	func->io_head = NULL;
1521 
1522 	while (res) {
1523 		tres = res;
1524 		res = res->next;
1525 		kfree(tres);
1526 	}
1527 
1528 	res = func->mem_head;
1529 	func->mem_head = NULL;
1530 
1531 	while (res) {
1532 		tres = res;
1533 		res = res->next;
1534 		kfree(tres);
1535 	}
1536 
1537 	res = func->p_mem_head;
1538 	func->p_mem_head = NULL;
1539 
1540 	while (res) {
1541 		tres = res;
1542 		res = res->next;
1543 		kfree(tres);
1544 	}
1545 
1546 	res = func->bus_head;
1547 	func->bus_head = NULL;
1548 
1549 	while (res) {
1550 		tres = res;
1551 		res = res->next;
1552 		kfree(tres);
1553 	}
1554 }
1555