30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/list.h>
37 static int configure_device(
struct pci_func *);
38 static int configure_bridge(
struct pci_func **,
u8);
41 static u8 find_sec_number (
u8 primary_busno,
u8 slotno);
50 static void assign_alt_irq (
struct pci_func * cur_func,
u8 class_code)
53 for (j = 0; j < 4; j++) {
54 if (cur_func->
irq[j] == 0xff) {
89 struct pci_func *cur_func, *prev_func;
93 u8 valid_device = 0x00;
95 debug (
"inside configure_card, func->busno = %x\n", func->
busno);
106 for (
function = 0x00;
function < 0x08;
function++) {
112 debug (
"inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
117 debug (
"vendor_id is %x\n", vendor_id);
120 debug (
"found valid device, vendor_id = %x\n", vendor_id);
132 class_code =
class >> 24;
133 debug (
"hrd_type = %x, class = %x, class_code %x\n", hdr_type,
class, class_code);
136 err (
"The device %x is VGA compatible and as is not supported for hot plugging. "
137 "Please choose another device.\n", cur_func->
device);
140 err (
"The device %x is not supported for hot plugging. "
141 "Please choose another device.\n", cur_func->
device);
146 debug (
"single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type,
class);
147 assign_alt_irq (cur_func, class_code);
148 if ((rc = configure_device (cur_func)) < 0) {
150 err (
"was not able to configure devfunc %x on bus %x.\n",
159 assign_alt_irq (cur_func, class_code);
160 if ((rc = configure_device (cur_func)) < 0) {
162 err (
"was not able to configure devfunc %x on bus %x...bailing out\n",
167 newfunc = kzalloc(
sizeof(*newfunc),
GFP_KERNEL);
169 err (
"out of system memory\n");
174 cur_func->
next = newfunc;
176 for (j = 0; j < 4; j++)
177 newfunc->
irq[j] = cur_func->
irq[j];
182 err (
"This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
183 "Please insert another card.\n", cur_func->
device);
186 assign_alt_irq (cur_func, class_code);
187 rc = configure_bridge (&cur_func, slotno);
189 err (
"You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
195 err (
"was not able to hot-add PPB properly.\n");
203 for (i = 0; i < 32; i++) {
205 newfunc = kzalloc(
sizeof(*newfunc),
GFP_KERNEL);
207 err (
"out of system memory\n");
210 newfunc->
busno = sec_number;
212 for (j = 0; j < 4; j++)
213 newfunc->
irq[j] = cur_func->
irq[j];
216 for (prev_func = cur_func; prev_func->
next; prev_func = prev_func->
next) ;
217 prev_func->
next = newfunc;
219 cur_func->
next = newfunc;
233 newfunc = kzalloc(
sizeof(*newfunc),
GFP_KERNEL);
235 err (
"out of system memory\n");
240 for (j = 0; j < 4; j++)
241 newfunc->
irq[j] = cur_func->
irq[j];
242 for (prev_func = cur_func; prev_func->
next; prev_func = prev_func->
next) ;
243 prev_func->
next = newfunc;
248 debug (
"class now is %x\n",
class);
250 err (
"This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
251 "Please insert another card.\n", cur_func->
device);
255 assign_alt_irq (cur_func, class_code);
257 debug (
"cur_func->busno b4 configure_bridge is %x\n", cur_func->
busno);
258 rc = configure_bridge (&cur_func, slotno);
260 err (
"You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
267 err (
"was not able to hot-add PPB properly.\n");
271 debug (
"cur_func->busno = %x, device = %x, function = %x\n",
272 cur_func->
busno, device,
function);
274 debug (
"after configuring bridge..., sec_number = %x\n", sec_number);
276 for (i = 0; i < 32; i++) {
278 debug (
"inside for loop, device is %x\n", i);
279 newfunc = kzalloc(
sizeof(*newfunc),
GFP_KERNEL);
281 err (
" out of system memory\n");
284 newfunc->
busno = sec_number;
286 for (j = 0; j < 4; j++)
287 newfunc->
irq[j] = cur_func->
irq[j];
290 for (prev_func = cur_func; prev_func->
next; prev_func = prev_func->
next) ;
291 prev_func->
next = newfunc;
293 cur_func->
next = newfunc;
311 err (
"MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
319 err (
"Cannot find any valid devices on the card. Or unable to read from card.\n");
326 for (i = 0; i < cleanup_count; i++) {
327 if (cur_func->
io[i]) {
330 }
else if (cur_func->
pfmem[i]) {
333 }
else if (cur_func->
mem[i]) {
367 debug (
"%s - inside\n", __func__);
372 for (count = 0; address[
count]; count++) {
387 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
388 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
393 debug (
"Device %x BAR %d wants %x\n", func->
device, count, bar[count]);
397 debug (
"inside IO SPACE\n");
402 debug (
"len[count] in IO %x, count %d\n", len[count], count);
407 err (
"out of system memory\n");
418 err (
"cannot allocate requested io for bus %x device %x function %x len %x\n",
423 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
io[count]->start);
426 debug (
"b4 writing, the IO address is %x\n", func->
io[count]->start);
427 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
428 debug (
"after writing.... the start address is %x\n", bar[count]);
435 debug (
"PFMEM SPACE\n");
440 debug (
"len[count] in PFMEM %x, count %d\n", len[count], count);
444 err (
"out of system memory\n");
457 mem_tmp = kzalloc(
sizeof(*mem_tmp),
GFP_KERNEL);
459 err (
"out of system memory\n");
460 kfree (pfmem[count]);
467 debug (
"there's no pfmem... going into mem.\n");
477 err (
"cannot allocate requested pfmem for bus %x, device %x, len %x\n",
480 kfree (pfmem[count]);
485 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
pfmem[count]->start);
488 debug (
"b4 writing, start address is %x\n", func->
pfmem[count]->start);
489 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
490 debug (
"after writing, start address is %x\n", bar[count]);
494 debug (
"inside the mem 64 case, count %d\n", count);
497 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0x00000000);
501 debug (
"REGULAR MEM SPACE\n");
506 debug (
"len[count] in Mem %x, count %d\n", len[count], count);
510 err (
"out of system memory\n");
522 err (
"cannot allocate requested mem for bus %x, device %x, len %x\n",
527 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
mem[count]->start);
529 debug (
"b4 writing, start address is %x\n", func->
mem[count]->start);
530 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
531 debug (
"after writing, the address is %x\n", bar[count]);
536 debug (
"inside mem 64 case, reg. mem, count %d\n", count);
539 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0x00000000);
547 if ((irq > 0x00) && (irq < 0x05))
564 static int configure_bridge (
struct pci_func **func_passed,
u8 slotno)
577 u8 need_io_upper = 0;
578 u8 need_pfmem_upper = 0;
593 struct pci_func *func = *func_passed;
598 debug (
"%s - enter\n", __func__);
615 sec_number = find_sec_number (func->
busno, slotno);
616 if (sec_number == 0xff) {
617 err (
"cannot allocate secondary bus number for the bridged device\n");
621 debug (
"after find_sec_number, the number we got is %x\n", sec_number);
622 debug (
"AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->
busno);
643 debug (
"sec_number after writing is %x\n", sec_number);
652 for (count = 0; address[
count]; count++) {
653 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
654 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
658 debug (
"so we come here then, eh?, count = %d\n", count);
663 debug (
"Bar %d wants %x\n", count, bar[count]);
665 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
670 debug (
"len[count] in IO = %x\n", len[count]);
674 if (!bus_io[count]) {
675 err (
"out of system memory\n");
688 err (
"cannot allocate requested io for bus %x, device %x, len %x\n",
690 kfree (bus_io[count]);
694 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
io[count]->start);
698 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
703 debug (
"len[count] in PFMEM = %x\n", len[count]);
706 if (!bus_pfmem[count]) {
707 err (
"out of system memory\n");
721 mem_tmp = kzalloc(
sizeof(*mem_tmp),
GFP_KERNEL);
723 err (
"out of system memory\n");
738 err (
"cannot allocate requested pfmem for bus %x, device %x, len %x\n",
741 kfree (bus_pfmem[count]);
746 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
pfmem[count]->start);
752 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0x00000000);
760 debug (
"len[count] in Memory is %x\n", len[count]);
763 if (!bus_mem[count]) {
764 err (
"out of system memory\n");
777 err (
"cannot allocate requested mem for bus %x, device %x, len %x\n",
779 kfree (bus_mem[count]);
783 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], func->
mem[count]->start);
789 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0x00000000);
797 amount_needed = scan_behind_bridge (func, sec_number);
798 if (amount_needed ==
NULL)
802 debug (
"after coming back from scan_behind_bridge\n");
804 debug (
"amount_needed->io = %x\n", amount_needed->
io);
805 debug (
"amount_needed->mem = %x\n", amount_needed->
mem);
806 debug (
"amount_needed->pfmem = %x\n", amount_needed->
pfmem);
809 debug (
"amount_needed is not correct\n");
810 for (count = 0; address[
count]; count++) {
815 }
else if (bus_pfmem[count]) {
818 }
else if (bus_mem[count]) {
823 kfree (amount_needed);
827 if (!amount_needed->
io) {
828 debug (
"it doesn't want IO?\n");
831 debug (
"it wants %x IO behind the bridge\n", amount_needed->
io);
835 err (
"out of system memory\n");
842 io->
len = amount_needed->
io;
844 debug (
"were we able to add io\n");
850 if (!amount_needed->
mem) {
851 debug (
"it doesn't want n.e.memory?\n");
854 debug (
"it wants %x memory behind the bridge\n", amount_needed->
mem);
857 err (
"out of system memory\n");
864 mem->
len = amount_needed->
mem;
868 debug (
"were we able to add mem\n");
872 if (!amount_needed->
pfmem) {
873 debug (
"it doesn't want n.e.pfmem mem?\n");
876 debug (
"it wants %x pfmemory behind the bridge\n", amount_needed->
pfmem);
879 err (
"out of system memory\n");
892 mem_tmp = kzalloc(
sizeof(*mem_tmp),
GFP_KERNEL);
894 err (
"out of system memory\n");
901 mem_tmp->
len = pfmem->
len;
912 debug (
"b4 if (flag_io && flag_mem && flag_pfmem)\n");
913 debug (
"flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
915 if (flag_io && flag_mem && flag_pfmem) {
925 err (
"out of system memory\n");
929 bus->
busno = sec_number;
930 debug (
"b4 adding new bus\n");
931 rc = add_new_bus (bus, io, mem, pfmem, func->
busno);
933 rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
935 err (
"expected bus structure not empty?\n");
942 kfree (amount_needed);
956 debug (
"pfmem 64\n");
957 need_pfmem_upper = 1;
1006 if (need_pfmem_upper) {
1015 debug (
"b4 writing control information\n");
1018 if ((irq > 0x00) && (irq < 0x05))
1028 for (i = 0; i < 32; i++) {
1029 if (amount_needed->
devices[i]) {
1030 debug (
"device where devices[i] is 1 = %x\n", i);
1035 func_passed = &
func;
1036 debug (
"func->busno b4 returning is %x\n", func->
busno);
1037 debug (
"func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1038 kfree (amount_needed);
1041 err (
"Configuring bridge was unsuccessful...\n");
1048 kfree(amount_needed);
1055 for (i = 0; i < 2; i++) {
1059 }
else if (bus_pfmem[i]) {
1062 }
else if (bus_mem[i]) {
1097 amount = kzalloc(
sizeof(*amount),
GFP_KERNEL);
1103 debug (
"the bus_no behind the bridge is %x\n", busno);
1104 debug (
"scanning devices behind the bridge...\n");
1105 for (device = 0; device < 32; device++) {
1107 for (
function = 0;
function < 8;
function++) {
1119 debug (
"hdr_type behind the bridge is %x\n", hdr_type);
1121 err (
"embedded bridges not supported for hot-plugging.\n");
1128 err (
"The device %x is VGA compatible and as is not supported for hot plugging. "
1129 "Please choose another device.\n", device);
1133 err (
"The device %x is not supported for hot plugging. "
1134 "Please choose another device.\n", device);
1141 for (count = 0; address[
count]; count++) {
1150 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1151 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &bar[count]);
1153 debug (
"what is bar[count]? %x, count = %d\n", bar[count], count);
1160 debug (
"count %d device %x function %x wants %x resources\n", count, device,
function, bar[count]);
1162 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1169 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1214 static int unconfigure_boot_device (
u8 busno,
u8 device,
u8 function)
1237 debug (
"%s - enter\n", __func__);
1241 debug (
"cannot find corresponding bus.\n");
1247 for (count = 0; address[
count]; count++) {
1248 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &start_address);
1252 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1253 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &size);
1254 pci_bus_write_config_dword (
ibmphp_pci_bus, devfn, address[count], start_address);
1256 debug (
"start_address is %x\n", start_address);
1257 debug (
"busno, device, function %x %x %x\n", busno, device,
function);
1260 debug (
"is this bar no implemented?, count = %d\n", count);
1263 tmp_address = start_address;
1264 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1267 size = size & 0xFFFFFFFC;
1269 end_address = start_address + size - 1;
1271 err (
"cannot find corresponding IO resource to remove\n");
1276 start_address = io->
end + 1;
1279 while (temp_end < end_address) {
1281 err (
"cannot find corresponding IO resource to remove\n");
1286 start_address = io->
end + 1;
1293 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1295 debug (
"start address of pfmem is %x\n", start_address);
1299 err (
"cannot find corresponding PFMEM resource to remove\n");
1309 debug (
"start address of mem is %x\n", start_address);
1313 err (
"cannot find corresponding MEM resource to remove\n");
1332 static int unconfigure_boot_bridge (
u8 busno,
u8 device,
u8 function)
1335 int bus_no, pri_no, sub_no, sec_no = 0;
1336 u32 start_address, tmp_address;
1337 u8 sec_number, sub_number, pri_number;
1351 bus_no = (
int) busno;
1352 debug (
"busno is %x\n", busno);
1354 debug (
"%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1357 debug (
"sec_number is %x\n", sec_number);
1358 sec_no = (
int) sec_number;
1359 pri_no = (
int) pri_number;
1360 if (pri_no != bus_no) {
1361 err (
"primary numbers in our structures and pci config space don't match.\n");
1366 sub_no = (
int) sub_number;
1367 debug (
"sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1368 if (sec_no != sub_number) {
1369 err (
"there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1375 err (
"cannot find Bus structure for the bridged device\n");
1379 debug(
"sec_number is %x\n", sec_number);
1383 for (count = 0; address[
count]; count++) {
1385 pci_bus_read_config_dword (
ibmphp_pci_bus, devfn, address[count], &start_address);
1387 if (!start_address) {
1392 tmp_address = start_address;
1394 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1398 err (
"cannot find corresponding IO resource to remove\n");
1409 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1413 err (
"cannot find corresponding PFMEM resource to remove\n");
1425 err (
"cannot find corresponding MEM resource to remove\n");
1440 debug (
"%s - exiting, returning success\n", __func__);
1444 static int unconfigure_boot_card (
struct slot *slot_cur)
1454 u8 valid_device = 0x00;
1456 debug (
"%s - enter\n", __func__);
1458 device = slot_cur->
device;
1459 busno = slot_cur->
bus;
1461 debug (
"b4 for loop, device is %x\n", device);
1463 for (
function = 0x0;
function < 0x08;
function++) {
1473 debug (
"%s - found correct device\n", __func__);
1483 debug (
"hdr_type %x, class %x\n", hdr_type,
class);
1486 err (
"The device %x function %x is VGA compatible and is not supported for hot removing. "
1487 "Please choose another device.\n", device,
function);
1490 err (
"The device %x function %x is not supported for hot removing. "
1491 "Please choose another device.\n", device,
function);
1497 rc = unconfigure_boot_device (busno, device,
function);
1499 err (
"was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1500 device,
function, busno);
1506 rc = unconfigure_boot_device (busno, device,
function);
1508 err (
"was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1509 device,
function, busno);
1513 case PCI_HEADER_TYPE_BRIDGE:
1516 err (
"This device %x function %x is not PCI-to-PCI bridge, "
1517 "and is not supported for hot-removing. "
1518 "Please try another card.\n", device,
function);
1521 rc = unconfigure_boot_bridge (busno, device,
function);
1523 err (
"was not able to hot-remove PPB properly.\n");
1532 err (
"This device %x function %x is not PCI-to-PCI bridge, "
1533 "and is not supported for hot-removing. "
1534 "Please try another card.\n", device,
function);
1537 rc = unconfigure_boot_bridge (busno, device,
function);
1539 err (
"was not able to hot-remove PPB properly.\n");
1544 err (
"MAJOR PROBLEM!!!! Cannot read device's header\n");
1551 if (!valid_device) {
1552 err (
"Could not find device to unconfigure. Or could not read the card.\n");
1572 struct slot *
sl = *slot_cur;
1576 debug (
"%s - enter\n", __func__);
1580 rc = unconfigure_boot_card (sl);
1588 cur_func = sl->
func;
1591 if (cur_func->
bus) {
1598 for (i = 0; i <
count; i++) {
1599 if (cur_func->
io[i]) {
1600 debug (
"io[%d] exists\n", i);
1605 if (cur_func->
mem[i]) {
1606 debug (
"mem[%d] exists\n", i);
1611 if (cur_func->
pfmem[i]) {
1612 debug (
"pfmem[%d] exists\n", i);
1619 temp_func = cur_func->
next;
1621 cur_func = temp_func;
1627 debug (
"%s - exit\n", __func__);
1647 if (parent_busno != 0xFF) {
1650 err (
"strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1657 io_range = kzalloc(
sizeof(*io_range),
GFP_KERNEL);
1659 err (
"out of system memory\n");
1669 mem_range = kzalloc(
sizeof(*mem_range),
GFP_KERNEL);
1671 err (
"out of system memory\n");
1675 mem_range->
end = mem->
end;
1681 pfmem_range = kzalloc(
sizeof(*pfmem_range),
GFP_KERNEL);
1683 err (
"out of system memory\n");
1687 pfmem_range->
end = pfmem->
end;
1700 static u8 find_sec_number (
u8 primary_busno,
u8 slotno)
1709 err (
"cannot get slot range of the bus from the BIOS\n");
1714 if ((slotno > max) || (slotno < min)) {
1715 err (
"got the wrong range\n");
1718 busno = (
u8) (slotno - (
u8)
min);
1719 busno += primary_busno + 0x01;