const std::string& pci::getDescription(uint16_t vendor_id, uint16_t device_id) { char vendorbuf[128], devbuf[128]; pci_lookup_name(_pacc, vendorbuf, sizeof(vendorbuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_NO_NUMBERS, vendor_id, device_id); pci_lookup_name(_pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, vendor_id, device_id); return std::string(vendorbuf).append("|").append(devbuf); }
static void list_all_nvidia_devices(void) { size_t i; printf("\n*** These devices are supported by the current %3.2f NVIDIA " "driver %s ***\n\n", NVIDIA_VERSION, KMOD_NVIDIA); for (i = 0; i < ARRAY_SIZE(nv_current_pci_ids); i++) { name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, PCI_VENDOR_ID_NVIDIA, nv_current_pci_ids[i]); printf("[10de:%04x] %s\n", nv_current_pci_ids[i], name); } printf("\n*** These devices are supported by the legacy 340.xx NVIDIA " "driver %s ***\n\n", KMOD_NVIDIA_340XX); for (i = 0; i < ARRAY_SIZE(nv_340xx_pci_ids); i++) { name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, PCI_VENDOR_ID_NVIDIA, nv_340xx_pci_ids[i]); printf("[10de:%04x] %s\n", nv_340xx_pci_ids[i], name); } printf("\n*** These devices are supported by the legacy 304.xx NVIDIA " "driver %s ***\n\n", KMOD_NVIDIA_304XX); for (i = 0; i < ARRAY_SIZE(nv_304xx_pci_ids); i++) { name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, PCI_VENDOR_ID_NVIDIA, nv_304xx_pci_ids[i]); printf("[10de:%04x] %s\n", nv_304xx_pci_ids[i], name); } printf("\n*** These devices are supported by the legacy 173.xx NVIDIA " "driver %s ***\n\n", KMOD_NVIDIA_173XX); for (i = 0; i < ARRAY_SIZE(nv_173xx_pci_ids); i++) { name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, PCI_VENDOR_ID_NVIDIA, nv_173xx_pci_ids[i]); printf("[10de:%04x] %s\n", nv_173xx_pci_ids[i], name); } printf("\n*** These devices are supported by the legacy 96.xx NVIDIA " "driver %s ***\n\n", KMOD_NVIDIA_96XX); for (i = 0; i < ARRAY_SIZE(nv_96xx_pci_ids); i++) { name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, PCI_VENDOR_ID_NVIDIA, nv_96xx_pci_ids[i]); printf("[10de:%04x] %s\n", nv_96xx_pci_ids[i], name); } }
void CardsDB::addCards(struct card_info *card_infos, ChipSet chipset) { Cards *cards; struct Card card; struct pci_access *pacc; char devbuf[128]; pacc = pci_alloc(); for (int i = 0; card_infos[i].card_name != NULL; i++) { QString vendor = (card_infos[i].vendor_id == 0xffff ? i18n("Other") : pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_infos[i].vendor_id, 0)); card.card_id = card_infos[i].card_id; card.card_name = card_infos[i].card_name; card.chipset = chipset; if (!m_vendors.contains(vendor)) { cards = new Cards(); m_vendors[vendor] = cards; } else { cards = m_vendors[vendor]; } cards->push_back(card); } pci_cleanup(pacc); }
void TunersDB::initVendors() { Tuners *tuners; struct Tuner tuner; struct pci_access *pacc; char devbuf[128]; pacc = pci_alloc(); for (int i = 0; card_tuners[i].tuner_name != NULL; i++) { QString vendor = (card_tuners[i].vendor_id == 0xffff ? i18n("Other") : pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_tuners[i].vendor_id, 0)); tuner.tuner_id = card_tuners[i].tuner_id; tuner.tuner_name = card_tuners[i].tuner_name; if (!m_vendors.contains(vendor)) { tuners = new Tuners(); m_vendors[vendor] = tuners; } else { tuners = m_vendors[vendor]; } tuners->push_back(tuner); } pci_cleanup(pacc); }
static void print_device_name(struct pci_access * pci, struct pci_dev * dev) { char device_name[1024]; if (pci_lookup_name(pci, device_name, sizeof(device_name), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id)) fprintf(stdout, "Device: %s\n\n", device_name); }
int iface_desc(const char *iface, char *desc, int size) { struct ifreq ifr; int fd, err, len, device, vendor; struct ethtool_drvinfo drvinfo; char buf[512], path[PATH_MAX]; struct pci_access *pacc; memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, iface); fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { perror("Cannot get control socket"); return 1; } drvinfo.cmd = ETHTOOL_GDRVINFO; ifr.ifr_data = (caddr_t)&drvinfo; err = ioctl(fd, SIOCETHTOOL, &ifr); if (err < 0) { perror("Cannot get driver information"); printf("%d\n", errno); return 2; } close(fd); snprintf(path, PATH_MAX-1, "/sys/bus/pci/devices/%s/vendor", drvinfo.bus_info); fd = open(path, O_RDONLY); if (fd < 0) { perror("Cannot open the vendor file"); return 3; } len = read(fd, buf, sizeof(buf)); buf[len-1] = '\0'; close(fd); sscanf(buf,"%X", &vendor); snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/device", drvinfo.bus_info); fd = open(path, O_RDONLY); if (fd < 0) { perror("Cannot open the device file"); return 3; } len = read(fd, buf, sizeof(buf)); buf[len-1] = '\0'; close(fd); sscanf(buf,"%X", &device); pacc = pci_alloc(); pci_init(pacc); pci_lookup_name(pacc, desc, size, PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, vendor, device); pci_cleanup(pacc); return(0); }
int main (int argc, char** argv) { int vendor = (int)strtol(argv[1], NULL, 16); int device = (int)strtol(argv[2], NULL, 16); char buf[512]; pci_access* acc = pci_alloc(); acc->numeric_ids = 0; acc->debugging = 0; acc->writeable = 0; acc->id_file_name = PCI_PATH_IDS; pci_init (acc); printf ("%s\n", pci_lookup_name(acc, buf, 512, PCI_LOOKUP_DEVICE, vendor, device)); pci_cleanup(acc); return 0; }
nsresult nsSystemInfo::Init() { struct pci_access *pacc; struct pci_dev *p; pciaddr_t ram = 0; char buf[128]; int i, found, v, n; Display *dpy; char *display = NULL; Window root; XVisualInfo *info, templ; XWindowAttributes wts; XPixmapFormatValues *pf; XSetWindowAttributes attr; Window win; mWidth = 0; mHeight = 0; mDepth = 0; pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); for (p = pacc->devices; p; p=p->next) { pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES | PCI_FILL_SIZES); if (p->device_class == PCI_CLASS_DISPLAY_VGA) { pci_lookup_name(pacc, buf, sizeof(buf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id); mDeviceName.AssignLiteral(buf); sprintf(buf, "0x%04X", p->vendor_id); mVendorID.AssignLiteral(buf); sprintf(buf, "0x%04X", p->device_id); mDeviceID.AssignLiteral(buf); for (i=0; i<6; i++) { pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0; if (len > ram) ram = len; } vram = ram / 1024 / 1024; } else { Log("Grafx Bot: No PCI VGA device found"); } } pci_cleanup(pacc); if (NULL != (display = getenv("DISPLAY"))) { if (display[0] != ':') { display = strchr(display, ':'); if (NULL == display) { Log("Grafx Bot: unable to find display"); return NS_OK; } } if (NULL == (dpy = XOpenDisplay(display))) { Log("Grafx Bot: unable to find X display"); return NS_OK; } root = DefaultRootWindow(dpy); XGetWindowAttributes(dpy, root, &wts); mWidth = wts.width; mHeight = wts.height; templ.screen = XDefaultScreen(dpy); info = XGetVisualInfo(dpy, VisualScreenMask, &templ, &found); v = -1; for (i = 0; v == -1 && i < found; i++) { if (info[i].depth >= 15) v = i; } for (i = 0; v == -1 && i < found; i++) { if (info[i].depth == 8) v = i; } if (-1 == v) { Log("Grafx Bot: can't find visual"); return NS_OK; } pf = XListPixmapFormats(dpy, &n); for (i = 0; i < n; i++) { if (pf[i].depth == info[v].depth) { mDepth = pf[i].depth; } } if (gGLXWrap.OpenLibrary("libGL.so.1") && gGLXWrap.Init()) { attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(dpy, root, info[v].visual, AllocNone); attr.event_mask = StructureNotifyMask | ExposureMask; win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, info[v].depth, InputOutput, info[v].visual, CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr); GLXContext ctx = gGLXWrap.fCreateContext(dpy, info, NULL, true); if (ctx) { if (gGLXWrap.fMakeCurrent(dpy, win, ctx)) { mDriverVersion.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VERSION)); mDriver.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_RENDERER)); mDriver.AppendLiteral(" ("); mDriver.AppendLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VENDOR)); mDriver.AppendLiteral(")"); } else { Log("Grafx Bot: unable to make current"); } gGLXWrap.fDestroyContext(dpy, ctx); } else { Log("Grafx Bot: unable to create context"); } XDestroyWindow(dpy, win); } else { Log("Grafx Bot: can't init libGL.so.1"); } } return NS_OK; }
int main(int argc, char *argv[]) { bool has_intel = 0; bool has_nvidia = 0; bool abi_compat = 0; int ret = 0; int c = 0; while ((c = getopt_long(argc, argv, "lxVh", longopts, 0)) != EOF) switch (c) { case 'l': opt_list = true; break; case 'x': opt_xorg = true; break; case 'V': version(); exit(0); case 'h': usage(); exit(0); default: usage(); exit(0); } pacc = pci_alloc(); /* Get the pci_access structure */ pci_init(pacc); /* Initialize the PCI library */ if (opt_list) { list_all_nvidia_devices(); goto exit; } pci_scan_bus(pacc); /* Scan the bus for devices */ printf("Probing for supported NVIDIA devices...\n"); /* Iterate over all devices */ for (dev=pacc->devices; dev; dev=dev->next) { if (!dev->device_class) { fprintf(stderr, "Error getting device_class\n"); ret = -1; goto exit; } if ((dev->device_class & 0xff00) == 0x0300) { /* Get the name of the device */ name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); printf("[%04x:%04x] %s\n", dev->vendor_id, dev->device_id, name); /* Find NVIDIA device */ if (dev->vendor_id == PCI_VENDOR_ID_NVIDIA) { has_nvidia = true; ret = nv_lookup_device_id(dev->device_id); } /* * Find Intel device for simplistic detection * of Optimus hardware configurations */ if (dev->vendor_id == PCI_VENDOR_ID_INTEL) has_intel = true; } /* End of device_class */ } /* End iteration of devices */ /* Check Xorg ABI compatibility */ if (ret > 0) { if (opt_xorg) printf("\nChecking ABI compatibility with Xorg Server...\n"); abi_compat = check_xorg_abi_compat(ret); if (!abi_compat) printf("WARNING: The driver for this device " "does not support the current Xorg version\n"); else if (opt_xorg) printf("ABI compatibility check passed\n"); } /* Check for Optimus hardware */ if (has_intel && has_nvidia) has_optimus(); /* Catch cases where no NVIDIA devices were detected */ if (!has_nvidia) printf("No NVIDIA devices were found.\n"); exit: pci_cleanup(pacc); /* Close everything */ exit(ret); }
static int hwloc_look_pci(struct hwloc_backend *backend) { struct hwloc_topology *topology = backend->topology; struct hwloc_obj *first_obj = NULL, *last_obj = NULL; #ifdef HWLOC_HAVE_LIBPCIACCESS int ret; struct pci_device_iterator *iter; struct pci_device *pcidev; #else /* HWLOC_HAVE_PCIUTILS */ struct pci_access *pciaccess; struct pci_dev *pcidev; #endif if (!(hwloc_topology_get_flags(topology) & (HWLOC_TOPOLOGY_FLAG_IO_DEVICES|HWLOC_TOPOLOGY_FLAG_WHOLE_IO))) return 0; if (hwloc_get_next_pcidev(topology, NULL)) { hwloc_debug("%s", "PCI objects already added, ignoring pci backend.\n"); return 0; } if (!hwloc_topology_is_thissystem(topology)) { hwloc_debug("%s", "\nno PCI detection (not thissystem)\n"); return 0; } hwloc_debug("%s", "\nScanning PCI buses...\n"); /* initialize PCI scanning */ #ifdef HWLOC_HAVE_LIBPCIACCESS ret = pci_system_init(); if (ret) { hwloc_debug("%s", "Can not initialize libpciaccess\n"); return -1; } iter = pci_slot_match_iterator_create(NULL); #else /* HWLOC_HAVE_PCIUTILS */ pciaccess = pci_alloc(); pciaccess->error = hwloc_pci_error; pciaccess->warning = hwloc_pci_warning; if (setjmp(err_buf)) { pci_cleanup(pciaccess); return -1; } pci_init(pciaccess); pci_scan_bus(pciaccess); #endif /* iterate over devices */ #ifdef HWLOC_HAVE_LIBPCIACCESS for (pcidev = pci_device_next(iter); pcidev; pcidev = pci_device_next(iter)) #else /* HWLOC_HAVE_PCIUTILS */ for (pcidev = pciaccess->devices; pcidev; pcidev = pcidev->next) #endif { const char *vendorname, *devicename, *fullname; unsigned char config_space_cache[CONFIG_SPACE_CACHESIZE]; struct hwloc_obj *obj; unsigned os_index; unsigned domain; unsigned device_class; unsigned short tmp16; char name[128]; unsigned offset; #ifdef HWLOC_HAVE_PCI_FIND_CAP struct pci_cap *cap; #endif /* initialize the config space in case we fail to read it (missing permissions, etc). */ memset(config_space_cache, 0xff, CONFIG_SPACE_CACHESIZE); #ifdef HWLOC_HAVE_LIBPCIACCESS pci_device_probe(pcidev); pci_device_cfg_read(pcidev, config_space_cache, 0, CONFIG_SPACE_CACHESIZE, NULL); #else /* HWLOC_HAVE_PCIUTILS */ pci_read_block(pcidev, 0, config_space_cache, CONFIG_SPACE_CACHESIZE); /* doesn't even tell how much it actually reads */ #endif /* try to read the domain */ #if (defined HWLOC_HAVE_LIBPCIACCESS) || (defined HWLOC_HAVE_PCIDEV_DOMAIN) domain = pcidev->domain; #else domain = 0; /* default domain number */ #endif /* try to read the device_class */ #ifdef HWLOC_HAVE_LIBPCIACCESS device_class = pcidev->device_class >> 8; #else /* HWLOC_HAVE_PCIUTILS */ #ifdef HWLOC_HAVE_PCIDEV_DEVICE_CLASS device_class = pcidev->device_class; #else device_class = config_space_cache[PCI_CLASS_DEVICE] | (config_space_cache[PCI_CLASS_DEVICE+1] << 8); #endif #endif /* might be useful for debugging (note that domain might be truncated) */ os_index = (domain << 20) + (pcidev->bus << 12) + (pcidev->dev << 4) + pcidev->func; obj = hwloc_alloc_setup_object(HWLOC_OBJ_PCI_DEVICE, os_index); obj->attr->pcidev.domain = domain; obj->attr->pcidev.bus = pcidev->bus; obj->attr->pcidev.dev = pcidev->dev; obj->attr->pcidev.func = pcidev->func; obj->attr->pcidev.vendor_id = pcidev->vendor_id; obj->attr->pcidev.device_id = pcidev->device_id; obj->attr->pcidev.class_id = device_class; obj->attr->pcidev.revision = config_space_cache[PCI_REVISION_ID]; obj->attr->pcidev.linkspeed = 0; /* unknown */ #ifdef HWLOC_HAVE_PCI_FIND_CAP cap = pci_find_cap(pcidev, PCI_CAP_ID_EXP, PCI_CAP_NORMAL); offset = cap ? cap->addr : 0; #else offset = hwloc_pci_find_cap(config_space_cache, PCI_CAP_ID_EXP); #endif /* HWLOC_HAVE_PCI_FIND_CAP */ if (0xffff == pcidev->vendor_id && 0xffff == pcidev->device_id) { /* SR-IOV puts ffff:ffff in Virtual Function config space. * The actual VF device ID is stored at a special (dynamic) location in the Physical Function config space. * VF and PF have the same vendor ID. * * libpciaccess just returns ffff:ffff, needs to be fixed. * linuxpci is OK because sysfs files are already fixed the kernel. * pciutils is OK when it uses those Linux sysfs files. * * Reading these files is an easy way to work around the libpciaccess issue on Linux, * but we have no way to know if this is caused by SR-IOV or not. * * TODO: * If PF has CAP_ID_PCIX or CAP_ID_EXP (offset>0), * look for extended capability PCI_EXT_CAP_ID_SRIOV (need extended config space (more than 256 bytes)), * then read the VF device ID after it (PCI_IOV_DID bytes later). * Needs access to extended config space (needs root on Linux). * TODO: * Add string info attributes in VF and PF objects? */ #ifdef HWLOC_LINUX_SYS /* Workaround for Linux (the kernel returns the VF device/vendor IDs). */ char path[64]; char value[16]; FILE *file; snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/vendor", domain, pcidev->bus, pcidev->dev, pcidev->func); file = fopen(path, "r"); if (file) { fread(value, sizeof(value), 1, file); fclose(file); obj->attr->pcidev.vendor_id = strtoul(value, NULL, 16); } snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/device", domain, pcidev->bus, pcidev->dev, pcidev->func); file = fopen(path, "r"); if (file) { fread(value, sizeof(value), 1, file); fclose(file); obj->attr->pcidev.device_id = strtoul(value, NULL, 16); } #endif } if (offset > 0 && offset + 20 /* size of PCI express block up to link status */ <= CONFIG_SPACE_CACHESIZE) hwloc_pci_find_linkspeed(config_space_cache, offset, &obj->attr->pcidev.linkspeed); hwloc_pci_prepare_bridge(obj, config_space_cache); if (obj->type == HWLOC_OBJ_PCI_DEVICE) { memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_VENDOR_ID], sizeof(tmp16)); obj->attr->pcidev.subvendor_id = tmp16; memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_ID], sizeof(tmp16)); obj->attr->pcidev.subdevice_id = tmp16; } else { /* TODO: * bridge must lookup PCI_CAP_ID_SSVID and then look at offset+PCI_SSVID_VENDOR/DEVICE_ID * cardbus must look at PCI_CB_SUBSYSTEM_VENDOR_ID and PCI_CB_SUBSYSTEM_ID */ } /* starting from pciutils 2.2, pci_lookup_name() takes a variable number * of arguments, and supports the PCI_LOOKUP_NO_NUMBERS flag. */ /* get the vendor name */ #ifdef HWLOC_HAVE_LIBPCIACCESS vendorname = pci_device_get_vendor_name(pcidev); #else /* HWLOC_HAVE_PCIUTILS */ vendorname = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_VENDOR|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id #else PCI_LOOKUP_VENDOR, pcidev->vendor_id, 0, 0, 0 #endif ); #endif /* HWLOC_HAVE_PCIUTILS */ if (vendorname && *vendorname) hwloc_obj_add_info(obj, "PCIVendor", vendorname); /* get the device name */ #ifdef HWLOC_HAVE_LIBPCIACCESS devicename = pci_device_get_device_name(pcidev); #else /* HWLOC_HAVE_PCIUTILS */ devicename = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id, pcidev->device_id #else PCI_LOOKUP_DEVICE, pcidev->vendor_id, pcidev->device_id, 0, 0 #endif ); #endif /* HWLOC_HAVE_PCIUTILS */ if (devicename && *devicename) hwloc_obj_add_info(obj, "PCIDevice", devicename); /* generate or get the fullname */ #ifdef HWLOC_HAVE_LIBPCIACCESS snprintf(name, sizeof(name), "%s%s%s", vendorname ? vendorname : "", vendorname && devicename ? " " : "", devicename ? devicename : ""); fullname = name; if (*name) obj->name = strdup(name); #else /* HWLOC_HAVE_PCIUTILS */ fullname = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id, pcidev->device_id #else PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE, pcidev->vendor_id, pcidev->device_id, 0, 0 #endif ); if (fullname && *fullname) obj->name = strdup(fullname); #endif /* HWLOC_HAVE_PCIUTILS */ hwloc_debug(" %04x:%02x:%02x.%01x %04x %04x:%04x %s\n", domain, pcidev->bus, pcidev->dev, pcidev->func, device_class, pcidev->vendor_id, pcidev->device_id, fullname && *fullname ? fullname : "??"); /* queue the object for now */ if (first_obj) last_obj->next_sibling = obj; else first_obj = obj; last_obj = obj; } /* finalize device scanning */ #ifdef HWLOC_HAVE_LIBPCIACCESS pci_iterator_destroy(iter); pci_system_cleanup(); #else /* HWLOC_HAVE_PCIUTILS */ pci_cleanup(pciaccess); #endif return hwloc_insert_pci_device_list(backend, first_obj); }
static inline PdaDebugReturnCode PciDevice_invoke_libpci ( PciDevice *device, const char *bus_id ) { DEBUG_PRINTF(PDADEBUG_ENTER, ""); if(device == NULL) { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); } if(bus_id == NULL) { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); } if ( (strlen(bus_id) != 12) || (bus_id[4] != ':') || (bus_id[7] != ':') || (bus_id[10] != '.') ) { RETURN( ERROR( EINVAL, "Invalid bus ID string!\n") ); } struct pci_access *pci_access_handler = pci_alloc(); pci_init(pci_access_handler); pci_scan_bus(pci_access_handler); for ( struct pci_dev *pci_device = pci_access_handler->devices; pci_device; pci_device = pci_device->next ) { pci_fill_info(pci_device, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_CLASS | PCI_FILL_CAPS | PCI_FILL_EXT_CAPS | PCI_FILL_PHYS_SLOT ); char device_bus_id[] = "0000:00:00.0"; snprintf(device_bus_id, 13,"%04x:%02x:%02x.%d", pci_device->domain, pci_device->bus, pci_device->dev, pci_device->func); if(strcmp(device_bus_id, bus_id) == 0) { device->domain_id = pci_device->domain; device->bus_id = pci_device->bus; device->device_id = pci_device->dev; device->function_id = pci_device->func; for(uint32_t i = 0; i < PDA_MAX_PCI_32_BARS; i++) { uint32_t bar = pci_device->base_addr[i]; DEBUG_PRINTF(PDADEBUG_VALUE, "bar%d : 0x%x\n", i, bar); uint8_t config[64]; pci_read_block(pci_device, 0, config, 64); uint64_t addr = PCI_BASE_ADDRESS_0 + (4*i); uint32_t virt_flag = (config[addr+0] ) | (config[addr+1] << 8 ) | (config[addr+2] << 16 ) | (config[addr+3] << 24 ) ; if(bar && !virt_flag) { device->bar_types[i] = PCIBARTYPES_NOT_MAPPED; DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "bar%u is virtual\n", i); continue; } if(bar == 0x0) { if(device->bar_types[i] != PCIBARTYPES_NOT_MAPPED) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_NOT_MAPPED; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } if( (bar & 0x00000001) == 0x1) { if(device->bar_types[i] != PCIBARTYPES_IO) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_IO; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } uint32_t width_bit = bar & 0x00000006; if(width_bit == 0x00000000) { if(device->bar_types[i] != PCIBARTYPES_BAR32) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_BAR32; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } if(width_bit == 0x00000004) { if(device->bar_types[i] != PCIBARTYPES_BAR64) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_BAR64; device->bar_types[i + 1] = PCIBARTYPES_NOT_MAPPED; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } } /** Older versions of libpci leak memory when this function is called. **/ #if PCI_LIB_VERSION >= 0x030301 pci_lookup_name(pci_access_handler, device->description, PDA_STRING_LIMIT, PCI_LOOKUP_DEVICE, pci_device->vendor_id, pci_device->device_id); #else #warning "At least version 3.3.1 of libpci is needed. Otherwise the device description can't be returned." strcpy(device->description, ""); #endif DEBUG_PRINTF(PDADEBUG_VALUE, " (%s) %s\n", device->description, device_bus_id); } } pci_cleanup(pci_access_handler); RETURN(PDA_SUCCESS); }
void pci::probe(void) { pci_scan_bus(_pacc); uint8_t buf[CONFIG_SPACE_SIZE] = {0}; char classbuf[128] = {0}, vendorbuf[128] {0}, devbuf[128] = {0}; for (struct pci_dev *dev = _pacc->devices; dev && _entries.size() < MAX_DEVICES; dev = dev->next) { _entries.push_back(pciEntry()); pciEntry &e = _entries.back(); memset(buf, 0, sizeof(buf)); memset(classbuf, 0, sizeof(classbuf)); memset(vendorbuf, 0, sizeof(vendorbuf)); memset(devbuf, 0, sizeof(devbuf)); pci_setup_cache(dev, buf, CONFIG_SPACE_SIZE); pci_read_block(dev, 0, buf, CONFIG_SPACE_SIZE); pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_CAPS); pci_lookup_name(_pacc, vendorbuf, sizeof(vendorbuf), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id); pci_lookup_name(_pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); e.text.append(vendorbuf).append("|").append(devbuf); e.class_type += classbuf; e.vendor = dev->vendor_id; e.device = dev->device_id; e.pci_domain = dev->domain; e.bus = dev->bus; e.pciusb_device = dev->dev; e.pci_function = dev->func; e.class_id = dev->device_class; e.subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID); e.subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID); e.pci_revision = pci_read_byte(dev, PCI_REVISION_ID); if ((e.subvendor == 0 && e.subdevice == 0) || (e.subvendor == e.vendor && e.subdevice == e.device)) { e.subvendor = 0xffff; e.subdevice = 0xffff; } if (pci_find_cap(dev,PCI_CAP_ID_EXP, PCI_CAP_NORMAL)) e.is_pciexpress = true; /* special case for realtek 8139 that has two drivers */ if (e.vendor == 0x10ec && e.device == 0x8139) { if (e.pci_revision < 0x20) e.module = "8139too"; else e.module = "8139cp"; } } // fake two PCI controllers for xen struct stat sb; if (!stat("/sys/bus/xen", &sb)) { // FIXME: use C++ streams.. FILE *f; if ((f = fopen("/sys/hypervisor/uuid", "r"))) { char buf[38]; fgets(buf, sizeof(buf) - 1, f); fclose(f); if (strncmp(buf, "00000000-0000-0000-0000-000000000000", sizeof(buf))) { // We're now sure to be in a Xen guest: { _entries.push_back(pciEntry()); pciEntry &e = _entries.back(); e.text.append("XenSource, Inc.|Block Frontend"); e.class_id = 0x0106; // STORAGE_SATA e.vendor = 0x1a71; // XenSource e.device = 0xfffa; // fake e.subvendor = 0; e.subdevice = 0; e.class_id = 0x0106; e.module = "xen_blkfront"; } { _entries.push_back(pciEntry()); pciEntry &e = _entries.back(); e.text.append("XenSource, Inc.|Network Frontend"); e.class_id = 0x0200; // NETWORK_ETHERNET e.vendor = 0x1a71; // XenSource e.device = 0xfffb; // fake e.subvendor = 0; e.subdevice = 0; e.class_id = 0x0200; e.module = "xen_netfront"; } } } } findModules("pcitable", false); }