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