1 // SPDX-License-Identifier: GPL-2.0+
3 * IBM Hot Plug Controller Driver
5 * Written By: Irene Zubarev, IBM Corporation
7 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
8 * Copyright (C) 2001,2002 IBM Corp.
10 * All rights reserved.
12 * Send feedback to <gregkh@us.ibm.com>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/list.h>
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
);
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)
36 static void assign_alt_irq(struct pci_func
*cur_func
, u8 class_code
)
39 for (j
= 0; j
< 4; j
++) {
40 if (cur_func
->irq
[j
] == 0xff) {
42 case PCI_BASE_CLASS_STORAGE
:
43 cur_func
->irq
[j
] = SCSI_IRQ
;
45 case PCI_BASE_CLASS_NETWORK
:
46 cur_func
->irq
[j
] = LAN_IRQ
;
49 cur_func
->irq
[j
] = OTHER_IRQ
;
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
61 * Input: function to be added
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....
67 int ibmphp_configure_card(struct pci_func
*func
, u8 slotno
)
72 u8 hdr_type
, device
, sec_number
;
74 struct pci_func
*newfunc
; /* for multi devices */
75 struct pci_func
*cur_func
, *prev_func
;
79 u8 valid_device
= 0x00; /* to see if we are able to read from card any device info at all */
81 debug("inside configure_card, func->busno = %x\n", func
->busno
);
83 device
= func
->device
;
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
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
;
96 cur_func
->function
= function
;
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
);
101 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_VENDOR_ID
, &vendor_id
);
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
);
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
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);
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
);
125 } else if (class == PCI_CLASS_DISPLAY_VGA
) {
126 err("The device %x is not supported for hot plugging. Please choose another device.\n",
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
);
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
);
142 cur_func
->next
= NULL
;
145 case PCI_HEADER_TYPE_MULTIDEVICE
:
146 assign_alt_irq(cur_func
, class_code
);
147 rc
= configure_device(cur_func
);
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
);
155 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
159 newfunc
->busno
= cur_func
->busno
;
160 newfunc
->device
= device
;
161 cur_func
->next
= newfunc
;
163 for (j
= 0; j
< 4; j
++)
164 newfunc
->irq
[j
] = cur_func
->irq
[j
];
166 case PCI_HEADER_TYPE_MULTIBRIDGE
:
168 if (class != PCI_CLASS_BRIDGE_PCI
) {
169 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
173 assign_alt_irq(cur_func
, class_code
);
174 rc
= configure_bridge(&cur_func
, slotno
);
176 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
177 err("Bus %x, devfunc %x\n", cur_func
->busno
, cur_func
->device
);
181 /* We need to do this in case some other BARs were properly inserted */
182 err("was not able to hot-add PPB properly.\n");
183 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
188 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
190 for (i
= 0; i
< 32; i
++) {
191 if (func
->devices
[i
]) {
192 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
196 newfunc
->busno
= sec_number
;
197 newfunc
->device
= (u8
) i
;
198 for (j
= 0; j
< 4; j
++)
199 newfunc
->irq
[j
] = cur_func
->irq
[j
];
202 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
) ;
203 prev_func
->next
= newfunc
;
205 cur_func
->next
= newfunc
;
207 rc
= ibmphp_configure_card(newfunc
, slotno
);
208 /* This could only happen if kmalloc failed */
210 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
211 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
219 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
223 newfunc
->busno
= cur_func
->busno
;
224 newfunc
->device
= device
;
225 for (j
= 0; j
< 4; j
++)
226 newfunc
->irq
[j
] = cur_func
->irq
[j
];
227 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
);
228 prev_func
->next
= newfunc
;
231 case PCI_HEADER_TYPE_BRIDGE
:
233 debug("class now is %x\n", class);
234 if (class != PCI_CLASS_BRIDGE_PCI
) {
235 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
240 assign_alt_irq(cur_func
, class_code
);
242 debug("cur_func->busno b4 configure_bridge is %x\n", cur_func
->busno
);
243 rc
= configure_bridge(&cur_func
, slotno
);
245 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
246 err("Bus %x, devfunc %x\n", cur_func
->busno
, cur_func
->device
);
250 /* We need to do this in case some other BARs were properly inserted */
251 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
252 err("was not able to hot-add PPB properly.\n");
256 debug("cur_func->busno = %x, device = %x, function = %x\n",
257 cur_func
->busno
, device
, function
);
258 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
259 debug("after configuring bridge..., sec_number = %x\n", sec_number
);
261 for (i
= 0; i
< 32; i
++) {
262 if (func
->devices
[i
]) {
263 debug("inside for loop, device is %x\n", i
);
264 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
268 newfunc
->busno
= sec_number
;
269 newfunc
->device
= (u8
) i
;
270 for (j
= 0; j
< 4; j
++)
271 newfunc
->irq
[j
] = cur_func
->irq
[j
];
274 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
);
275 prev_func
->next
= newfunc
;
277 cur_func
->next
= newfunc
;
279 rc
= ibmphp_configure_card(newfunc
, slotno
);
281 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
283 /* We need to do this in case some other BARs were properly inserted */
284 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
295 err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type
);
298 } /* end of switch */
299 } /* end of valid device */
303 err("Cannot find any valid devices on the card. Or unable to read from card.\n");
310 for (i
= 0; i
< cleanup_count
; i
++) {
311 if (cur_func
->io
[i
]) {
312 ibmphp_remove_resource(cur_func
->io
[i
]);
313 cur_func
->io
[i
] = NULL
;
314 } else if (cur_func
->pfmem
[i
]) {
315 ibmphp_remove_resource(cur_func
->pfmem
[i
]);
316 cur_func
->pfmem
[i
] = NULL
;
317 } else if (cur_func
->mem
[i
]) {
318 ibmphp_remove_resource(cur_func
->mem
[i
]);
319 cur_func
->mem
[i
] = NULL
;
326 * This function configures the pci BARs of a single device.
327 * Input: pointer to the pci_func
328 * Output: configured PCI, 0, or error
330 static int configure_device(struct pci_func
*func
)
345 struct resource_node
*io
[6];
346 struct resource_node
*mem
[6];
347 struct resource_node
*mem_tmp
;
348 struct resource_node
*pfmem
[6];
351 debug("%s - inside\n", __func__
);
353 devfn
= PCI_DEVFN(func
->device
, func
->function
);
354 ibmphp_pci_bus
->number
= func
->busno
;
356 for (count
= 0; address
[count
]; count
++) { /* for 6 BARs */
358 /* not sure if i need this. per scott, said maybe need * something like this
359 if devices don't adhere 100% to the spec, so don't want to write
362 pcibios_read_config_byte(cur_func->busno, cur_func->device,
363 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
364 if (tmp & 0x01) // IO
365 pcibios_write_config_dword(cur_func->busno, cur_func->device,
366 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
368 pcibios_write_config_dword(cur_func->busno, cur_func->device,
369 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
371 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
372 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
374 if (!bar
[count
]) /* This BAR is not implemented */
377 debug("Device %x BAR %d wants %x\n", func
->device
, count
, bar
[count
]);
379 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
381 debug("inside IO SPACE\n");
383 len
[count
] = bar
[count
] & 0xFFFFFFFC;
384 len
[count
] = ~len
[count
] + 1;
386 debug("len[count] in IO %x, count %d\n", len
[count
], count
);
388 io
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
393 io
[count
]->type
= IO
;
394 io
[count
]->busno
= func
->busno
;
395 io
[count
]->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
396 io
[count
]->len
= len
[count
];
397 if (ibmphp_check_resource(io
[count
], 0) == 0) {
398 ibmphp_add_resource(io
[count
]);
399 func
->io
[count
] = io
[count
];
401 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
402 func
->busno
, func
->device
, func
->function
, len
[count
]);
406 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->io
[count
]->start
);
408 /* _______________This is for debugging purposes only_____________________ */
409 debug("b4 writing, the IO address is %x\n", func
->io
[count
]->start
);
410 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
411 debug("after writing.... the start address is %x\n", bar
[count
]);
412 /* _________________________________________________________________________*/
416 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
418 debug("PFMEM SPACE\n");
420 len
[count
] = bar
[count
] & 0xFFFFFFF0;
421 len
[count
] = ~len
[count
] + 1;
423 debug("len[count] in PFMEM %x, count %d\n", len
[count
], count
);
425 pfmem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
429 pfmem
[count
]->type
= PFMEM
;
430 pfmem
[count
]->busno
= func
->busno
;
431 pfmem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
433 pfmem
[count
]->len
= len
[count
];
434 pfmem
[count
]->fromMem
= 0;
435 if (ibmphp_check_resource(pfmem
[count
], 0) == 0) {
436 ibmphp_add_resource(pfmem
[count
]);
437 func
->pfmem
[count
] = pfmem
[count
];
439 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
445 mem_tmp
->busno
= pfmem
[count
]->busno
;
446 mem_tmp
->devfunc
= pfmem
[count
]->devfunc
;
447 mem_tmp
->len
= pfmem
[count
]->len
;
448 debug("there's no pfmem... going into mem.\n");
449 if (ibmphp_check_resource(mem_tmp
, 0) == 0) {
450 ibmphp_add_resource(mem_tmp
);
451 pfmem
[count
]->fromMem
= 1;
452 pfmem
[count
]->rangeno
= mem_tmp
->rangeno
;
453 pfmem
[count
]->start
= mem_tmp
->start
;
454 pfmem
[count
]->end
= mem_tmp
->end
;
455 ibmphp_add_pfmem_from_mem(pfmem
[count
]);
456 func
->pfmem
[count
] = pfmem
[count
];
458 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
459 func
->busno
, func
->device
, len
[count
]);
466 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->pfmem
[count
]->start
);
468 /*_______________This is for debugging purposes only______________________________*/
469 debug("b4 writing, start address is %x\n", func
->pfmem
[count
]->start
);
470 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
471 debug("after writing, start address is %x\n", bar
[count
]);
472 /*_________________________________________________________________________________*/
474 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) { /* takes up another dword */
475 debug("inside the mem 64 case, count %d\n", count
);
477 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
478 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
482 debug("REGULAR MEM SPACE\n");
484 len
[count
] = bar
[count
] & 0xFFFFFFF0;
485 len
[count
] = ~len
[count
] + 1;
487 debug("len[count] in Mem %x, count %d\n", len
[count
], count
);
489 mem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
493 mem
[count
]->type
= MEM
;
494 mem
[count
]->busno
= func
->busno
;
495 mem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
497 mem
[count
]->len
= len
[count
];
498 if (ibmphp_check_resource(mem
[count
], 0) == 0) {
499 ibmphp_add_resource(mem
[count
]);
500 func
->mem
[count
] = mem
[count
];
502 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
503 func
->busno
, func
->device
, len
[count
]);
507 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->mem
[count
]->start
);
508 /* _______________________This is for debugging purposes only _______________________*/
509 debug("b4 writing, start address is %x\n", func
->mem
[count
]->start
);
510 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
511 debug("after writing, the address is %x\n", bar
[count
]);
512 /* __________________________________________________________________________________*/
514 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
515 /* takes up another dword */
516 debug("inside mem 64 case, reg. mem, count %d\n", count
);
518 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
519 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
525 func
->bus
= 0; /* To indicate that this is not a PPB */
526 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_PIN
, &irq
);
527 if ((irq
> 0x00) && (irq
< 0x05))
528 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_LINE
, func
->irq
[irq
- 1]);
530 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, CACHE
);
531 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_LATENCY_TIMER
, LATENCY
);
533 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_ROM_ADDRESS
, 0x00L
);
534 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_COMMAND
, DEVICEENABLE
);
539 /******************************************************************************
540 * This routine configures a PCI-2-PCI bridge and the functions behind it
541 * Parameters: pci_func
543 ******************************************************************************/
544 static int configure_bridge(struct pci_func
**func_passed
, u8 slotno
)
557 u8 need_io_upper
= 0;
558 u8 need_pfmem_upper
= 0;
559 struct res_needed
*amount_needed
= NULL
;
560 struct resource_node
*io
= NULL
;
561 struct resource_node
*bus_io
[2] = {NULL
, NULL
};
562 struct resource_node
*mem
= NULL
;
563 struct resource_node
*bus_mem
[2] = {NULL
, NULL
};
564 struct resource_node
*mem_tmp
= NULL
;
565 struct resource_node
*pfmem
= NULL
;
566 struct resource_node
*bus_pfmem
[2] = {NULL
, NULL
};
567 struct bus_node
*bus
;
573 struct pci_func
*func
= *func_passed
;
578 debug("%s - enter\n", __func__
);
580 devfn
= PCI_DEVFN(func
->function
, func
->device
);
581 ibmphp_pci_bus
->number
= func
->busno
;
583 /* Configuring necessary info for the bridge so that we could see the devices
587 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_PRIMARY_BUS
, func
->busno
);
589 /* _____________________For debugging purposes only __________________________
590 pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
591 debug("primary # written into the bridge is %x\n", pri_number);
592 ___________________________________________________________________________*/
594 /* in EBDA, only get allocated 1 additional bus # per slot */
595 sec_number
= find_sec_number(func
->busno
, slotno
);
596 if (sec_number
== 0xff) {
597 err("cannot allocate secondary bus number for the bridged device\n");
601 debug("after find_sec_number, the number we got is %x\n", sec_number
);
602 debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func
->busno
);
604 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, sec_number
);
606 /* __________________For debugging purposes only __________________________________
607 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
608 debug("sec_number after write/read is %x\n", sec_number);
609 ________________________________________________________________________________*/
611 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, sec_number
);
613 /* __________________For debugging purposes only ____________________________________
614 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
615 debug("subordinate number after write/read is %x\n", sec_number);
616 __________________________________________________________________________________*/
618 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, CACHE
);
619 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_LATENCY_TIMER
, LATENCY
);
620 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SEC_LATENCY_TIMER
, LATENCY
);
622 debug("func->busno is %x\n", func
->busno
);
623 debug("sec_number after writing is %x\n", sec_number
);
626 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
627 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
628 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
631 /* First we need to allocate mem/io for the bridge itself in case it needs it */
632 for (count
= 0; address
[count
]; count
++) { /* for 2 BARs */
633 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
634 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
637 /* This BAR is not implemented */
638 debug("so we come here then, eh?, count = %d\n", count
);
641 // tmp_bar = bar[count];
643 debug("Bar %d wants %x\n", count
, bar
[count
]);
645 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
647 len
[count
] = bar
[count
] & 0xFFFFFFFC;
648 len
[count
] = ~len
[count
] + 1;
650 debug("len[count] in IO = %x\n", len
[count
]);
652 bus_io
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
654 if (!bus_io
[count
]) {
658 bus_io
[count
]->type
= IO
;
659 bus_io
[count
]->busno
= func
->busno
;
660 bus_io
[count
]->devfunc
= PCI_DEVFN(func
->device
,
662 bus_io
[count
]->len
= len
[count
];
663 if (ibmphp_check_resource(bus_io
[count
], 0) == 0) {
664 ibmphp_add_resource(bus_io
[count
]);
665 func
->io
[count
] = bus_io
[count
];
667 err("cannot allocate requested io for bus %x, device %x, len %x\n",
668 func
->busno
, func
->device
, len
[count
]);
669 kfree(bus_io
[count
]);
673 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->io
[count
]->start
);
677 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
679 len
[count
] = bar
[count
] & 0xFFFFFFF0;
680 len
[count
] = ~len
[count
] + 1;
682 debug("len[count] in PFMEM = %x\n", len
[count
]);
684 bus_pfmem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
685 if (!bus_pfmem
[count
]) {
689 bus_pfmem
[count
]->type
= PFMEM
;
690 bus_pfmem
[count
]->busno
= func
->busno
;
691 bus_pfmem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
693 bus_pfmem
[count
]->len
= len
[count
];
694 bus_pfmem
[count
]->fromMem
= 0;
695 if (ibmphp_check_resource(bus_pfmem
[count
], 0) == 0) {
696 ibmphp_add_resource(bus_pfmem
[count
]);
697 func
->pfmem
[count
] = bus_pfmem
[count
];
699 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
705 mem_tmp
->busno
= bus_pfmem
[count
]->busno
;
706 mem_tmp
->devfunc
= bus_pfmem
[count
]->devfunc
;
707 mem_tmp
->len
= bus_pfmem
[count
]->len
;
708 if (ibmphp_check_resource(mem_tmp
, 0) == 0) {
709 ibmphp_add_resource(mem_tmp
);
710 bus_pfmem
[count
]->fromMem
= 1;
711 bus_pfmem
[count
]->rangeno
= mem_tmp
->rangeno
;
712 ibmphp_add_pfmem_from_mem(bus_pfmem
[count
]);
713 func
->pfmem
[count
] = bus_pfmem
[count
];
715 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
716 func
->busno
, func
->device
, len
[count
]);
718 kfree(bus_pfmem
[count
]);
723 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->pfmem
[count
]->start
);
725 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
726 /* takes up another dword */
728 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
729 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
734 len
[count
] = bar
[count
] & 0xFFFFFFF0;
735 len
[count
] = ~len
[count
] + 1;
737 debug("len[count] in Memory is %x\n", len
[count
]);
739 bus_mem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
740 if (!bus_mem
[count
]) {
744 bus_mem
[count
]->type
= MEM
;
745 bus_mem
[count
]->busno
= func
->busno
;
746 bus_mem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
748 bus_mem
[count
]->len
= len
[count
];
749 if (ibmphp_check_resource(bus_mem
[count
], 0) == 0) {
750 ibmphp_add_resource(bus_mem
[count
]);
751 func
->mem
[count
] = bus_mem
[count
];
753 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
754 func
->busno
, func
->device
, len
[count
]);
755 kfree(bus_mem
[count
]);
759 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->mem
[count
]->start
);
761 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
762 /* takes up another dword */
764 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
765 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
772 /* Now need to see how much space the devices behind the bridge needed */
773 amount_needed
= scan_behind_bridge(func
, sec_number
);
774 if (amount_needed
== NULL
)
777 ibmphp_pci_bus
->number
= func
->busno
;
778 debug("after coming back from scan_behind_bridge\n");
779 debug("amount_needed->not_correct = %x\n", amount_needed
->not_correct
);
780 debug("amount_needed->io = %x\n", amount_needed
->io
);
781 debug("amount_needed->mem = %x\n", amount_needed
->mem
);
782 debug("amount_needed->pfmem = %x\n", amount_needed
->pfmem
);
784 if (amount_needed
->not_correct
) {
785 debug("amount_needed is not correct\n");
786 for (count
= 0; address
[count
]; count
++) {
789 ibmphp_remove_resource(bus_io
[count
]);
790 func
->io
[count
] = NULL
;
791 } else if (bus_pfmem
[count
]) {
792 ibmphp_remove_resource(bus_pfmem
[count
]);
793 func
->pfmem
[count
] = NULL
;
794 } else if (bus_mem
[count
]) {
795 ibmphp_remove_resource(bus_mem
[count
]);
796 func
->mem
[count
] = NULL
;
799 kfree(amount_needed
);
803 if (!amount_needed
->io
) {
804 debug("it doesn't want IO?\n");
807 debug("it wants %x IO behind the bridge\n", amount_needed
->io
);
808 io
= kzalloc(sizeof(*io
), GFP_KERNEL
);
815 io
->busno
= func
->busno
;
816 io
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
817 io
->len
= amount_needed
->io
;
818 if (ibmphp_check_resource(io
, 1) == 0) {
819 debug("were we able to add io\n");
820 ibmphp_add_resource(io
);
825 if (!amount_needed
->mem
) {
826 debug("it doesn't want n.e.memory?\n");
829 debug("it wants %x memory behind the bridge\n", amount_needed
->mem
);
830 mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
836 mem
->busno
= func
->busno
;
837 mem
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
838 mem
->len
= amount_needed
->mem
;
839 if (ibmphp_check_resource(mem
, 1) == 0) {
840 ibmphp_add_resource(mem
);
842 debug("were we able to add mem\n");
846 if (!amount_needed
->pfmem
) {
847 debug("it doesn't want n.e.pfmem mem?\n");
850 debug("it wants %x pfmemory behind the bridge\n", amount_needed
->pfmem
);
851 pfmem
= kzalloc(sizeof(*pfmem
), GFP_KERNEL
);
857 pfmem
->busno
= func
->busno
;
858 pfmem
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
859 pfmem
->len
= amount_needed
->pfmem
;
861 if (ibmphp_check_resource(pfmem
, 1) == 0) {
862 ibmphp_add_resource(pfmem
);
865 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
871 mem_tmp
->busno
= pfmem
->busno
;
872 mem_tmp
->devfunc
= pfmem
->devfunc
;
873 mem_tmp
->len
= pfmem
->len
;
874 if (ibmphp_check_resource(mem_tmp
, 1) == 0) {
875 ibmphp_add_resource(mem_tmp
);
877 pfmem
->rangeno
= mem_tmp
->rangeno
;
878 ibmphp_add_pfmem_from_mem(pfmem
);
884 debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
885 debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io
, flag_mem
, flag_pfmem
);
887 if (flag_io
&& flag_mem
&& flag_pfmem
) {
888 /* If on bootup, there was a bridged card in this slot,
889 * then card was removed and ibmphp got unloaded and loaded
890 * back again, there's no way for us to remove the bus
891 * struct, so no need to kmalloc, can use existing node
893 bus
= ibmphp_find_res_bus(sec_number
);
895 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
900 bus
->busno
= sec_number
;
901 debug("b4 adding new bus\n");
902 rc
= add_new_bus(bus
, io
, mem
, pfmem
, func
->busno
);
903 } else if (!(bus
->rangeIO
) && !(bus
->rangeMem
) && !(bus
->rangePFMem
))
904 rc
= add_new_bus(bus
, io
, mem
, pfmem
, 0xFF);
906 err("expected bus structure not empty?\n");
912 ibmphp_remove_bus(bus
, func
->busno
);
913 kfree(amount_needed
);
919 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, &io_base
);
920 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, &pfmem_base
);
922 if ((io_base
& PCI_IO_RANGE_TYPE_MASK
) == PCI_IO_RANGE_TYPE_32
) {
926 if ((pfmem_base
& PCI_PREF_RANGE_TYPE_MASK
) == PCI_PREF_RANGE_TYPE_64
) {
928 need_pfmem_upper
= 1;
931 if (bus
->noIORanges
) {
932 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, 0x00 | bus
->rangeIO
->start
>> 8);
933 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT
, 0x00 | bus
->rangeIO
->end
>> 8);
935 /* _______________This is for debugging purposes only ____________________
936 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
937 debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
938 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
939 debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
940 ________________________________________________________________________*/
942 if (need_io_upper
) { /* since can't support n.e.ways */
943 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_IO_BASE_UPPER16
, 0x0000);
944 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT_UPPER16
, 0x0000);
947 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, 0x00);
948 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT
, 0x00);
951 if (bus
->noMemRanges
) {
952 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_BASE
, 0x0000 | bus
->rangeMem
->start
>> 16);
953 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_LIMIT
, 0x0000 | bus
->rangeMem
->end
>> 16);
955 /* ____________________This is for debugging purposes only ________________________
956 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
957 debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
958 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
959 debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
960 __________________________________________________________________________________*/
963 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_BASE
, 0xffff);
964 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_LIMIT
, 0x0000);
966 if (bus
->noPFMemRanges
) {
967 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, 0x0000 | bus
->rangePFMem
->start
>> 16);
968 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, 0x0000 | bus
->rangePFMem
->end
>> 16);
970 /* __________________________This is for debugging purposes only _______________________
971 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
972 debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
973 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
974 debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
975 ______________________________________________________________________________________*/
977 if (need_pfmem_upper
) { /* since can't support n.e.ways */
978 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_PREF_BASE_UPPER32
, 0x00000000);
979 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_PREF_LIMIT_UPPER32
, 0x00000000);
982 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, 0xffff);
983 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, 0x0000);
986 debug("b4 writing control information\n");
988 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_PIN
, &irq
);
989 if ((irq
> 0x00) && (irq
< 0x05))
990 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_LINE
, func
->irq
[irq
- 1]);
992 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
993 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
994 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
997 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_COMMAND
, DEVICEENABLE
);
998 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, 0x07);
999 for (i
= 0; i
< 32; i
++) {
1000 if (amount_needed
->devices
[i
]) {
1001 debug("device where devices[i] is 1 = %x\n", i
);
1002 func
->devices
[i
] = 1;
1005 func
->bus
= 1; /* For unconfiguring, to indicate it's PPB */
1006 func_passed
= &func
;
1007 debug("func->busno b4 returning is %x\n", func
->busno
);
1008 debug("func->busno b4 returning in the other structure is %x\n", (*func_passed
)->busno
);
1009 kfree(amount_needed
);
1012 err("Configuring bridge was unsuccessful...\n");
1019 kfree(amount_needed
);
1021 ibmphp_remove_resource(pfmem
);
1023 ibmphp_remove_resource(io
);
1025 ibmphp_remove_resource(mem
);
1026 for (i
= 0; i
< 2; i
++) { /* for 2 BARs */
1028 ibmphp_remove_resource(bus_io
[i
]);
1030 } else if (bus_pfmem
[i
]) {
1031 ibmphp_remove_resource(bus_pfmem
[i
]);
1032 func
->pfmem
[i
] = NULL
;
1033 } else if (bus_mem
[i
]) {
1034 ibmphp_remove_resource(bus_mem
[i
]);
1035 func
->mem
[i
] = NULL
;
1041 /*****************************************************************************
1042 * This function adds up the amount of resources needed behind the PPB bridge
1043 * and passes it to the configure_bridge function
1044 * Input: bridge function
1045 * Output: amount of resources needed
1046 *****************************************************************************/
1047 static struct res_needed
*scan_behind_bridge(struct pci_func
*func
, u8 busno
)
1052 u8 device
, function
;
1054 int howmany
= 0; /*this is to see if there are any devices behind the bridge */
1066 struct res_needed
*amount
;
1068 amount
= kzalloc(sizeof(*amount
), GFP_KERNEL
);
1072 ibmphp_pci_bus
->number
= busno
;
1074 debug("the bus_no behind the bridge is %x\n", busno
);
1075 debug("scanning devices behind the bridge...\n");
1076 for (device
= 0; device
< 32; device
++) {
1077 amount
->devices
[device
] = 0;
1078 for (function
= 0; function
< 8; function
++) {
1079 devfn
= PCI_DEVFN(device
, function
);
1081 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_VENDOR_ID
, &vendor_id
);
1083 if (vendor_id
!= PCI_VENDOR_ID_NOTVALID
) {
1084 /* found correct device!!! */
1087 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_HEADER_TYPE
, &hdr_type
);
1088 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, PCI_CLASS_REVISION
, &class);
1090 debug("hdr_type behind the bridge is %x\n", hdr_type
);
1091 if ((hdr_type
& 0x7f) == PCI_HEADER_TYPE_BRIDGE
) {
1092 err("embedded bridges not supported for hot-plugging.\n");
1093 amount
->not_correct
= 1;
1097 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1098 if (class == PCI_CLASS_NOT_DEFINED_VGA
) {
1099 err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device
);
1100 amount
->not_correct
= 1;
1102 } else if (class == PCI_CLASS_DISPLAY_VGA
) {
1103 err("The device %x is not supported for hot plugging. Please choose another device.\n", device
);
1104 amount
->not_correct
= 1;
1108 amount
->devices
[device
] = 1;
1110 for (count
= 0; address
[count
]; count
++) {
1113 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1114 if (tmp & 0x01) // IO
1115 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1117 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1119 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
1120 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
1122 debug("what is bar[count]? %x, count = %d\n", bar
[count
], count
);
1124 if (!bar
[count
]) /* This BAR is not implemented */
1127 //tmp_bar = bar[count];
1129 debug("count %d device %x function %x wants %x resources\n", count
, device
, function
, bar
[count
]);
1131 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
1133 len
[count
] = bar
[count
] & 0xFFFFFFFC;
1134 len
[count
] = ~len
[count
] + 1;
1135 amount
->io
+= len
[count
];
1137 /* This is Memory */
1138 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1140 len
[count
] = bar
[count
] & 0xFFFFFFF0;
1141 len
[count
] = ~len
[count
] + 1;
1142 amount
->pfmem
+= len
[count
];
1143 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
)
1144 /* takes up another dword */
1148 /* regular memory */
1149 len
[count
] = bar
[count
] & 0xFFFFFFF0;
1150 len
[count
] = ~len
[count
] + 1;
1151 amount
->mem
+= len
[count
];
1152 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1153 /* takes up another dword */
1159 } /* end if (valid) */
1164 amount
->not_correct
= 1;
1166 amount
->not_correct
= 0;
1167 if ((amount
->io
) && (amount
->io
< IOBRIDGE
))
1168 amount
->io
= IOBRIDGE
;
1169 if ((amount
->mem
) && (amount
->mem
< MEMBRIDGE
))
1170 amount
->mem
= MEMBRIDGE
;
1171 if ((amount
->pfmem
) && (amount
->pfmem
< MEMBRIDGE
))
1172 amount
->pfmem
= MEMBRIDGE
;
1176 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1177 * upon bootup in the system, since we don't allocate func to such case, we need to read
1178 * the start addresses from pci config space and then find the corresponding entries in
1179 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1180 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1181 * the bootup case), since it should work same way
1183 static int unconfigure_boot_device(u8 busno
, u8 device
, u8 function
)
1196 struct resource_node
*io
;
1197 struct resource_node
*mem
;
1198 struct resource_node
*pfmem
;
1199 struct bus_node
*bus
;
1206 debug("%s - enter\n", __func__
);
1208 bus
= ibmphp_find_res_bus(busno
);
1210 debug("cannot find corresponding bus.\n");
1214 devfn
= PCI_DEVFN(device
, function
);
1215 ibmphp_pci_bus
->number
= busno
;
1216 for (count
= 0; address
[count
]; count
++) { /* for 6 BARs */
1217 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &start_address
);
1219 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1221 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
1222 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &size
);
1223 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], start_address
);
1225 debug("start_address is %x\n", start_address
);
1226 debug("busno, device, function %x %x %x\n", busno
, device
, function
);
1228 /* This BAR is not implemented */
1229 debug("is this bar no implemented?, count = %d\n", count
);
1232 tmp_address
= start_address
;
1233 if (start_address
& PCI_BASE_ADDRESS_SPACE_IO
) {
1235 start_address
&= PCI_BASE_ADDRESS_IO_MASK
;
1236 size
= size
& 0xFFFFFFFC;
1238 end_address
= start_address
+ size
- 1;
1239 if (ibmphp_find_resource(bus
, start_address
, &io
, IO
))
1240 goto report_search_failure
;
1242 debug("io->start = %x\n", io
->start
);
1244 start_address
= io
->end
+ 1;
1245 ibmphp_remove_resource(io
);
1246 /* This is needed b/c of the old I/O restrictions in the BIOS */
1247 while (temp_end
< end_address
) {
1248 if (ibmphp_find_resource(bus
, start_address
,
1250 goto report_search_failure
;
1252 debug("io->start = %x\n", io
->start
);
1254 start_address
= io
->end
+ 1;
1255 ibmphp_remove_resource(io
);
1258 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1260 /* This is Memory */
1261 if (start_address
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1263 debug("start address of pfmem is %x\n", start_address
);
1264 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1266 if (ibmphp_find_resource(bus
, start_address
, &pfmem
, PFMEM
) < 0) {
1267 err("cannot find corresponding PFMEM resource to remove\n");
1271 debug("pfmem->start = %x\n", pfmem
->start
);
1273 ibmphp_remove_resource(pfmem
);
1276 /* regular memory */
1277 debug("start address of mem is %x\n", start_address
);
1278 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1280 if (ibmphp_find_resource(bus
, start_address
, &mem
, MEM
) < 0) {
1281 err("cannot find corresponding MEM resource to remove\n");
1285 debug("mem->start = %x\n", mem
->start
);
1287 ibmphp_remove_resource(mem
);
1290 if (tmp_address
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1291 /* takes up another dword */
1299 report_search_failure
:
1300 err("cannot find corresponding IO resource to remove\n");
1304 static int unconfigure_boot_bridge(u8 busno
, u8 device
, u8 function
)
1307 int bus_no
, pri_no
, sub_no
, sec_no
= 0;
1308 u32 start_address
, tmp_address
;
1309 u8 sec_number
, sub_number
, pri_number
;
1310 struct resource_node
*io
= NULL
;
1311 struct resource_node
*mem
= NULL
;
1312 struct resource_node
*pfmem
= NULL
;
1313 struct bus_node
*bus
;
1321 devfn
= PCI_DEVFN(device
, function
);
1322 ibmphp_pci_bus
->number
= busno
;
1323 bus_no
= (int) busno
;
1324 debug("busno is %x\n", busno
);
1325 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_PRIMARY_BUS
, &pri_number
);
1326 debug("%s - busno = %x, primary_number = %x\n", __func__
, busno
, pri_number
);
1328 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
1329 debug("sec_number is %x\n", sec_number
);
1330 sec_no
= (int) sec_number
;
1331 pri_no
= (int) pri_number
;
1332 if (pri_no
!= bus_no
) {
1333 err("primary numbers in our structures and pci config space don't match.\n");
1337 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, &sub_number
);
1338 sub_no
= (int) sub_number
;
1339 debug("sub_no is %d, sec_no is %d\n", sub_no
, sec_no
);
1340 if (sec_no
!= sub_number
) {
1341 err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1345 bus
= ibmphp_find_res_bus(sec_number
);
1347 err("cannot find Bus structure for the bridged device\n");
1350 debug("bus->busno is %x\n", bus
->busno
);
1351 debug("sec_number is %x\n", sec_number
);
1353 ibmphp_remove_bus(bus
, busno
);
1355 for (count
= 0; address
[count
]; count
++) {
1357 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &start_address
);
1359 if (!start_address
) {
1360 /* This BAR is not implemented */
1364 tmp_address
= start_address
;
1366 if (start_address
& PCI_BASE_ADDRESS_SPACE_IO
) {
1368 start_address
&= PCI_BASE_ADDRESS_IO_MASK
;
1369 if (ibmphp_find_resource(bus
, start_address
, &io
, IO
) < 0) {
1370 err("cannot find corresponding IO resource to remove\n");
1374 debug("io->start = %x\n", io
->start
);
1376 ibmphp_remove_resource(io
);
1378 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1380 /* This is Memory */
1381 if (start_address
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1383 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1384 if (ibmphp_find_resource(bus
, start_address
, &pfmem
, PFMEM
) < 0) {
1385 err("cannot find corresponding PFMEM resource to remove\n");
1389 debug("pfmem->start = %x\n", pfmem
->start
);
1391 ibmphp_remove_resource(pfmem
);
1394 /* regular memory */
1395 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1396 if (ibmphp_find_resource(bus
, start_address
, &mem
, MEM
) < 0) {
1397 err("cannot find corresponding MEM resource to remove\n");
1401 debug("mem->start = %x\n", mem
->start
);
1403 ibmphp_remove_resource(mem
);
1406 if (tmp_address
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1407 /* takes up another dword */
1412 debug("%s - exiting, returning success\n", __func__
);
1416 static int unconfigure_boot_card(struct slot
*slot_cur
)
1426 u8 valid_device
= 0x00; /* To see if we are ever able to find valid device and read it */
1428 debug("%s - enter\n", __func__
);
1430 device
= slot_cur
->device
;
1431 busno
= slot_cur
->bus
;
1433 debug("b4 for loop, device is %x\n", device
);
1434 /* For every function on the card */
1435 for (function
= 0x0; function
< 0x08; function
++) {
1436 devfn
= PCI_DEVFN(device
, function
);
1437 ibmphp_pci_bus
->number
= busno
;
1439 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_VENDOR_ID
, &vendor_id
);
1441 if (vendor_id
!= PCI_VENDOR_ID_NOTVALID
) {
1442 /* found correct device!!! */
1445 debug("%s - found correct device\n", __func__
);
1447 /* header: x x x x x x x x
1448 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1449 * |_=> 0 = single function device, 1 = multi-function device
1452 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_HEADER_TYPE
, &hdr_type
);
1453 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, PCI_CLASS_REVISION
, &class);
1455 debug("hdr_type %x, class %x\n", hdr_type
, class);
1456 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1457 if (class == PCI_CLASS_NOT_DEFINED_VGA
) {
1458 err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device
, function
);
1460 } else if (class == PCI_CLASS_DISPLAY_VGA
) {
1461 err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device
, function
);
1466 case PCI_HEADER_TYPE_NORMAL
:
1467 rc
= unconfigure_boot_device(busno
, device
, function
);
1469 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1470 device
, function
, busno
);
1475 case PCI_HEADER_TYPE_MULTIDEVICE
:
1476 rc
= unconfigure_boot_device(busno
, device
, function
);
1478 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1479 device
, function
, busno
);
1483 case PCI_HEADER_TYPE_BRIDGE
:
1485 if (class != PCI_CLASS_BRIDGE_PCI
) {
1486 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
);
1489 rc
= unconfigure_boot_bridge(busno
, device
, function
);
1491 err("was not able to hot-remove PPB properly.\n");
1497 case PCI_HEADER_TYPE_MULTIBRIDGE
:
1499 if (class != PCI_CLASS_BRIDGE_PCI
) {
1500 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
);
1503 rc
= unconfigure_boot_bridge(busno
, device
, function
);
1505 err("was not able to hot-remove PPB properly.\n");
1510 err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1513 } /* end of switch */
1514 } /* end of valid device */
1517 if (!valid_device
) {
1518 err("Could not find device to unconfigure. Or could not read the card.\n");
1525 * free the resources of the card (multi, single, or bridged)
1526 * Parameters: slot, flag to say if this is for removing entire module or just
1527 * unconfiguring the device
1528 * TO DO: will probably need to add some code in case there was some resource,
1529 * to remove it... this is from when we have errors in the configure_card...
1530 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1531 * Returns: 0, -1, -ENODEV
1533 int ibmphp_unconfigure_card(struct slot
**slot_cur
, int the_end
)
1538 struct slot
*sl
= *slot_cur
;
1539 struct pci_func
*cur_func
= NULL
;
1540 struct pci_func
*temp_func
;
1542 debug("%s - enter\n", __func__
);
1545 /* Need to unconfigure the card */
1546 rc
= unconfigure_boot_card(sl
);
1547 if ((rc
== -ENODEV
) || (rc
== -EIO
) || (rc
== -EINVAL
)) {
1548 /* In all other cases, will still need to get rid of func structure if it exists */
1554 cur_func
= sl
->func
;
1556 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1557 if (cur_func
->bus
) {
1558 /* in other words, it's a PPB */
1564 for (i
= 0; i
< count
; i
++) {
1565 if (cur_func
->io
[i
]) {
1566 debug("io[%d] exists\n", i
);
1568 ibmphp_remove_resource(cur_func
->io
[i
]);
1569 cur_func
->io
[i
] = NULL
;
1571 if (cur_func
->mem
[i
]) {
1572 debug("mem[%d] exists\n", i
);
1574 ibmphp_remove_resource(cur_func
->mem
[i
]);
1575 cur_func
->mem
[i
] = NULL
;
1577 if (cur_func
->pfmem
[i
]) {
1578 debug("pfmem[%d] exists\n", i
);
1580 ibmphp_remove_resource(cur_func
->pfmem
[i
]);
1581 cur_func
->pfmem
[i
] = NULL
;
1585 temp_func
= cur_func
->next
;
1587 cur_func
= temp_func
;
1593 debug("%s - exit\n", __func__
);
1598 * add a new bus resulting from hot-plugging a PPB bridge with devices
1600 * Input: bus and the amount of resources needed (we know we can assign those,
1601 * since they've been checked already
1602 * Output: bus added to the correct spot
1605 static int add_new_bus(struct bus_node
*bus
, struct resource_node
*io
, struct resource_node
*mem
, struct resource_node
*pfmem
, u8 parent_busno
)
1607 struct range_node
*io_range
= NULL
;
1608 struct range_node
*mem_range
= NULL
;
1609 struct range_node
*pfmem_range
= NULL
;
1610 struct bus_node
*cur_bus
= NULL
;
1612 /* Trying to find the parent bus number */
1613 if (parent_busno
!= 0xFF) {
1614 cur_bus
= ibmphp_find_res_bus(parent_busno
);
1616 err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1620 list_add(&bus
->bus_list
, &cur_bus
->bus_list
);
1623 io_range
= kzalloc(sizeof(*io_range
), GFP_KERNEL
);
1627 io_range
->start
= io
->start
;
1628 io_range
->end
= io
->end
;
1629 io_range
->rangeno
= 1;
1630 bus
->noIORanges
= 1;
1631 bus
->rangeIO
= io_range
;
1634 mem_range
= kzalloc(sizeof(*mem_range
), GFP_KERNEL
);
1638 mem_range
->start
= mem
->start
;
1639 mem_range
->end
= mem
->end
;
1640 mem_range
->rangeno
= 1;
1641 bus
->noMemRanges
= 1;
1642 bus
->rangeMem
= mem_range
;
1645 pfmem_range
= kzalloc(sizeof(*pfmem_range
), GFP_KERNEL
);
1649 pfmem_range
->start
= pfmem
->start
;
1650 pfmem_range
->end
= pfmem
->end
;
1651 pfmem_range
->rangeno
= 1;
1652 bus
->noPFMemRanges
= 1;
1653 bus
->rangePFMem
= pfmem_range
;
1659 * find the 1st available bus number for PPB to set as its secondary bus
1660 * Parameters: bus_number of the primary bus
1661 * Returns: bus_number of the secondary bus or 0xff in case of failure
1663 static u8
find_sec_number(u8 primary_busno
, u8 slotno
)
1667 struct bus_info
*bus
;
1668 struct bus_node
*bus_cur
;
1670 bus
= ibmphp_find_same_bus_num(primary_busno
);
1672 err("cannot get slot range of the bus from the BIOS\n");
1675 max
= bus
->slot_max
;
1676 min
= bus
->slot_min
;
1677 if ((slotno
> max
) || (slotno
< min
)) {
1678 err("got the wrong range\n");
1681 busno
= (u8
) (slotno
- (u8
) min
);
1682 busno
+= primary_busno
+ 0x01;
1683 bus_cur
= ibmphp_find_res_bus(busno
);
1684 /* either there is no such bus number, or there are no ranges, which
1685 * can only happen if we removed the bridged device in previous load
1686 * of the driver, and now only have the skeleton bus struct
1688 if ((!bus_cur
) || (!(bus_cur
->rangeIO
) && !(bus_cur
->rangeMem
) && !(bus_cur
->rangePFMem
)))