📄 ibmphp_pci.c
字号:
if (!amount_needed->pfmem) { debug ("it doesn't want n.e.pfmem mem?\n"); flag_pfmem = TRUE; } else { debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem); pfmem = kmalloc (sizeof (struct resource_node), GFP_KERNEL); if (!pfmem) { err ("out of system memory\n"); retval = -ENOMEM; goto error; } memset (pfmem, 0, sizeof (struct resource_node)); pfmem->type = PFMEM; pfmem->busno = func->busno; pfmem->devfunc = ((func->device << 3) | (func->function & 0x7)); pfmem->len = amount_needed->pfmem; pfmem->fromMem = FALSE; if (ibmphp_check_resource (pfmem, 1) == 0) { ibmphp_add_resource (pfmem); flag_pfmem = TRUE; } else { mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL); if (!mem_tmp) { err ("out of system memory\n"); retval = -ENOMEM; goto error; } memset (mem_tmp, 0, sizeof (struct resource_node)); mem_tmp->type = MEM; mem_tmp->busno = pfmem->busno; mem_tmp->devfunc = pfmem->devfunc; mem_tmp->len = pfmem->len; if (ibmphp_check_resource (mem_tmp, 1) == 0) { ibmphp_add_resource (mem_tmp); pfmem->fromMem = TRUE; pfmem->rangeno = mem_tmp->rangeno; ibmphp_add_pfmem_from_mem (pfmem); flag_pfmem = TRUE; } } } debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n"); debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem); if (flag_io && flag_mem && flag_pfmem) { /* If on bootup, there was a bridged card in this slot, * then card was removed and ibmphp got unloaded and loaded * back again, there's no way for us to remove the bus * struct, so no need to kmalloc, can use existing node */ bus = ibmphp_find_res_bus (sec_number); if (!bus) { bus = kmalloc (sizeof (struct bus_node), GFP_KERNEL); if (!bus) { err ("out of system memory\n"); retval = -ENOMEM; goto error; } memset (bus, 0, sizeof (struct bus_node)); bus->busno = sec_number; debug ("b4 adding new bus\n"); rc = add_new_bus (bus, io, mem, pfmem, func->busno); } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem)) rc = add_new_bus (bus, io, mem, pfmem, 0xFF); else { err ("expected bus structure not empty?\n"); retval = -EIO; goto error; } if (rc) { if (rc == -ENOMEM) { ibmphp_remove_bus (bus, func->busno); kfree (amount_needed); return rc; } retval = rc; goto error; } pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base); pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base); if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { debug ("io 32\n"); need_io_upper = TRUE; } if ((io_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { debug ("pfmem 64\n"); need_pfmem_upper = TRUE; } if (bus->noIORanges) { pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8); pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8); /* _______________This is for debugging purposes only ____________________ pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp); debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8); pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp); debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8); ________________________________________________________________________*/ if (need_io_upper) { /* since can't support n.e.ways */ pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000); } } else { pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00); pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00); } if (bus->noMemRanges) { pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16); /* ____________________This is for debugging purposes only ________________________ pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp); debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16); pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp); debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16); __________________________________________________________________________________*/ } else { pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000); } if (bus->noPFMemRanges) { pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16); /* __________________________This is for debugging purposes only _______________________ pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp); debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16); pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp); debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16); ______________________________________________________________________________________*/ if (need_pfmem_upper) { /* since can't support n.e.ways */ pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000); pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000); } } else { pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000); } debug ("b4 writing control information\n"); pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq); if ((irq > 0x00) && (irq < 0x05)) pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]); /* pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl); pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY); pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR); */ pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE); pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07); for (i = 0; i < 32; i++) { if (amount_needed->devices[i]) { debug ("device where devices[i] is 1 = %x\n", i); func->devices[i] = 1; } } func->bus = 1; /* For unconfiguring, to indicate it's PPB */ func_passed = &func; debug ("func->busno b4 returning is %x\n", func->busno); debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno); kfree (amount_needed); return 0; } else { err ("Configuring bridge was unsuccessful...\n"); mem_tmp = NULL; retval = -EIO; goto error; }error: if (amount_needed) kfree (amount_needed); if (pfmem) ibmphp_remove_resource (pfmem); if (io) ibmphp_remove_resource (io); if (mem) ibmphp_remove_resource (mem); for (i = 0; i < 2; i++) { /* for 2 BARs */ if (bus_io[i]) { ibmphp_remove_resource (bus_io[i]); func->io[i] = NULL; } else if (bus_pfmem[i]) { ibmphp_remove_resource (bus_pfmem[i]); func->pfmem[i] = NULL; } else if (bus_mem[i]) { ibmphp_remove_resource (bus_mem[i]); func->mem[i] = NULL; } } return retval;}/***************************************************************************** * This function adds up the amount of resources needed behind the PPB bridge * and passes it to the configure_bridge function * Input: bridge function * Ouput: amount of resources needed *****************************************************************************/static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno){ int count, len[6]; u16 vendor_id; u8 hdr_type; u8 device, function; unsigned int devfn; int howmany = 0; /*this is to see if there are any devices behind the bridge */ u32 bar[6], class; u32 address[] = { PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_2, PCI_BASE_ADDRESS_3, PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5, 0 }; struct res_needed *amount; amount = kmalloc (sizeof (struct res_needed), GFP_KERNEL); if (amount == NULL) return NULL; memset (amount, 0, sizeof (struct res_needed)); ibmphp_pci_bus->number = busno; debug ("the bus_no behind the bridge is %x\n", busno); debug ("scanning devices behind the bridge...\n"); for (device = 0; device < 32; device++) { amount->devices[device] = 0; for (function = 0; function < 8; function++) { devfn = PCI_DEVFN(device, function); pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id); if (vendor_id != PCI_VENDOR_ID_NOTVALID) { /* found correct device!!! */ howmany++; pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type); pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class); debug ("hdr_type behind the bridge is %x\n", hdr_type); if (hdr_type & PCI_HEADER_TYPE_BRIDGE) { err ("embedded bridges not supported for hot-plugging.\n"); amount->not_correct = TRUE; return amount; } class >>= 8; /* to take revision out, class = class.subclass.prog i/f */ if (class == PCI_CLASS_NOT_DEFINED_VGA) { err ("The device %x is VGA compatible and as is not supported for hot plugging. " "Please choose another device.\n", device); amount->not_correct = TRUE; return amount; } else if (class == PCI_CLASS_DISPLAY_VGA) { err ("The device %x is not supported for hot plugging. " "Please choose another device.\n", device); amount->not_correct = TRUE; return amount; } amount->devices[device] = 1; for (count = 0; address[count]; count++) { /* for 6 BARs */ /* pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp); if (tmp & 0x01) // IO pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD); else // MEMORY pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF); */ pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF); pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]); debug ("what is bar[count]? %x, count = %d\n", bar[count], count); if (!bar[count]) /* This BAR is not implemented */ continue; //tmp_bar = bar[count]; debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]); if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) { /* This is IO */ len[count] = bar[count] & 0xFFFFFFFC; len[count] = ~len[count] + 1; amount->io += len[count]; } else { /* This is Memory */ if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) { /* pfmem */ len[count] = bar[count] & 0xFFFFFFF0; len[count] = ~len[count] + 1; amount->pfmem += len[count]; if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) /* takes up another dword */ count += 1; } else { /* regular memory */ len[count] = bar[count] & 0xFFFFFFF0; len[count] = ~len[count] + 1; amount->mem += len[count]; if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */ count += 1; } } } } /* end for */ } /* end if (valid) */ } /* end for */ } /* end for */ if (!howmany) amount->not_correct = TRUE; else amount->not_correct = FALSE; if ((amount->io) && (amount->io < IOBRIDGE)) amount->io = IOBRIDGE; if ((amount->mem) && (amount->mem < MEMBRIDGE)) amount->mem = MEMBRIDGE; if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE)) amount->pfmem = MEMBRIDGE; return amount;}/* The following 3 unconfigure_boot_ routines deal with the case when we had the card * upon bootup in the system, since we don't allocate func to such case, we need to read * the start addresses from pci config space and then find the corresponding entries in * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure) * Change: we also call these functions even if we configured the card ourselves (i.e., not * the bootup case), since it should work same way */static int unconfigure_boot_device (u8 busno, u8 device, u8 function){ u32 start_address; u32 address[] = { PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_2, PCI_BASE_ADDRESS_3, PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5, 0 }; int count; struct resource_node *io; struct resource_node *mem; struct resource_node *pfmem; struct bus_node *bus; u32 end_address; u32 temp_end; u32 size; u32 tmp_address; unsigned int devfn; debug ("%s - enter\n", __FUNCTION__); bus = ibmphp_find_res_bus (busno); if (!bus) { debug ("cannot find corresponding bus.\n"); return -EINVAL; } devfn = PCI_DEVFN(device, function); ibmphp_pci_bus->number = busno; for (count = 0; address[count]; count++) { /* for 6 BARs */ pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address); /* We can do this here, b/c by that time the device driver of the card has been stopped */ pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF); pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size); pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address); debug ("start_address is %x\n", start_address); debug ("busno, device, function %x %x %x\n", busno, device, function); if (!size) { /* This BAR is not implemented */ debug ("is this bar no implemented?, count = %d\n", count); continue; } tmp_address = start_address; if (start_address & PCI_BASE_ADDRESS_SPACE_IO) { /* This is IO */ start_address &= PCI_BASE_ADDRESS_IO_MASK; size = size & 0xFFFFFFFC; size = ~size + 1; end_address = start_address + size - 1; if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) { err ("cannot find corresponding IO resource to remove\n"); return -EIO; } debug ("io->start = %x\n", io->start); temp_end = io->end; start_address = io->end + 1; ibmphp_remove_resource (io); /* This is needed b/c of the old I/O restrictions in the BIOS */ while (temp_end < end_address) { if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) { err ("cannot find corresponding IO resource to remove\n"); return -EIO; } debug ("io->start = %x\n", io->start); temp_end = io->end; start_address = io->end + 1; ibmphp_remove_resource (io); } /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */ } else { /* This is Memory */ if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) { /* pfmem */ start_address &= PCI_BASE_ADDRESS_MEM_MASK; debug ("start address of pfmem is %x\n", start_address); if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) { err ("cannot find corresponding PFMEM resource to remove\n"); return -EIO; } if (pfmem) debug ("pfmem->start = %x\n", pfmem->start); ibmphp_remove_resource (pfmem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -