1 /* 2 * Copyright 1996 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char rcsid[] = 32 "$FreeBSD$"; 33 #endif /* not lint */ 34 35 #include <sys/types.h> 36 #include <sys/fcntl.h> 37 38 #include <assert.h> 39 #include <ctype.h> 40 #include <err.h> 41 #include <inttypes.h> 42 #include <stdlib.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <sys/pciio.h> 47 #include <sys/queue.h> 48 49 #include <dev/pci/pcireg.h> 50 51 #include "pathnames.h" 52 #include "pciconf.h" 53 54 struct pci_device_info 55 { 56 TAILQ_ENTRY(pci_device_info) link; 57 int id; 58 char *desc; 59 }; 60 61 struct pci_vendor_info 62 { 63 TAILQ_ENTRY(pci_vendor_info) link; 64 TAILQ_HEAD(,pci_device_info) devs; 65 int id; 66 char *desc; 67 }; 68 69 TAILQ_HEAD(,pci_vendor_info) pci_vendors; 70 71 static struct pcisel getsel(const char *str); 72 static void list_bars(int fd, struct pci_conf *p); 73 static void list_devs(const char *name, int verbose, int bars, int caps, 74 int errors, int vpd); 75 static void list_verbose(struct pci_conf *p); 76 static void list_vpd(int fd, struct pci_conf *p); 77 static const char *guess_class(struct pci_conf *p); 78 static const char *guess_subclass(struct pci_conf *p); 79 static int load_vendors(void); 80 static void readit(const char *, const char *, int); 81 static void writeit(const char *, const char *, const char *, int); 82 static void chkattached(const char *); 83 84 static int exitstatus = 0; 85 86 static void 87 usage(void) 88 { 89 fprintf(stderr, "%s\n%s\n%s\n%s\n", 90 "usage: pciconf -l [-bcevV] [device]", 91 " pciconf -a device", 92 " pciconf -r [-b | -h] device addr[:addr2]", 93 " pciconf -w [-b | -h] device addr value"); 94 exit (1); 95 } 96 97 int 98 main(int argc, char **argv) 99 { 100 int c; 101 int listmode, readmode, writemode, attachedmode; 102 int bars, caps, errors, verbose, vpd; 103 int byte, isshort; 104 105 listmode = readmode = writemode = attachedmode = 0; 106 bars = caps = errors = verbose = vpd = byte = isshort = 0; 107 108 while ((c = getopt(argc, argv, "abcehlrwvV")) != -1) { 109 switch(c) { 110 case 'a': 111 attachedmode = 1; 112 break; 113 114 case 'b': 115 bars = 1; 116 byte = 1; 117 break; 118 119 case 'c': 120 caps = 1; 121 break; 122 123 case 'e': 124 errors = 1; 125 break; 126 127 case 'h': 128 isshort = 1; 129 break; 130 131 case 'l': 132 listmode = 1; 133 break; 134 135 case 'r': 136 readmode = 1; 137 break; 138 139 case 'w': 140 writemode = 1; 141 break; 142 143 case 'v': 144 verbose = 1; 145 break; 146 147 case 'V': 148 vpd = 1; 149 break; 150 151 default: 152 usage(); 153 } 154 } 155 156 if ((listmode && optind >= argc + 1) 157 || (writemode && optind + 3 != argc) 158 || (readmode && optind + 2 != argc) 159 || (attachedmode && optind + 1 != argc)) 160 usage(); 161 162 if (listmode) { 163 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose, 164 bars, caps, errors, vpd); 165 } else if (attachedmode) { 166 chkattached(argv[optind]); 167 } else if (readmode) { 168 readit(argv[optind], argv[optind + 1], 169 byte ? 1 : isshort ? 2 : 4); 170 } else if (writemode) { 171 writeit(argv[optind], argv[optind + 1], argv[optind + 2], 172 byte ? 1 : isshort ? 2 : 4); 173 } else { 174 usage(); 175 } 176 177 return exitstatus; 178 } 179 180 static void 181 list_devs(const char *name, int verbose, int bars, int caps, int errors, 182 int vpd) 183 { 184 int fd; 185 struct pci_conf_io pc; 186 struct pci_conf conf[255], *p; 187 struct pci_match_conf patterns[1]; 188 int none_count = 0; 189 190 if (verbose) 191 load_vendors(); 192 193 fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0); 194 if (fd < 0) 195 err(1, "%s", _PATH_DEVPCI); 196 197 bzero(&pc, sizeof(struct pci_conf_io)); 198 pc.match_buf_len = sizeof(conf); 199 pc.matches = conf; 200 if (name != NULL) { 201 bzero(&patterns, sizeof(patterns)); 202 patterns[0].pc_sel = getsel(name); 203 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | 204 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV | 205 PCI_GETCONF_MATCH_FUNC; 206 pc.num_patterns = 1; 207 pc.pat_buf_len = sizeof(patterns); 208 pc.patterns = patterns; 209 } 210 211 do { 212 if (ioctl(fd, PCIOCGETCONF, &pc) == -1) 213 err(1, "ioctl(PCIOCGETCONF)"); 214 215 /* 216 * 255 entries should be more than enough for most people, 217 * but if someone has more devices, and then changes things 218 * around between ioctls, we'll do the cheesy thing and 219 * just bail. The alternative would be to go back to the 220 * beginning of the list, and print things twice, which may 221 * not be desirable. 222 */ 223 if (pc.status == PCI_GETCONF_LIST_CHANGED) { 224 warnx("PCI device list changed, please try again"); 225 exitstatus = 1; 226 close(fd); 227 return; 228 } else if (pc.status == PCI_GETCONF_ERROR) { 229 warnx("error returned from PCIOCGETCONF ioctl"); 230 exitstatus = 1; 231 close(fd); 232 return; 233 } 234 for (p = conf; p < &conf[pc.num_matches]; p++) { 235 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x " 236 "chip=0x%08x rev=0x%02x hdr=0x%02x\n", 237 (p->pd_name && *p->pd_name) ? p->pd_name : 238 "none", 239 (p->pd_name && *p->pd_name) ? (int)p->pd_unit : 240 none_count++, p->pc_sel.pc_domain, 241 p->pc_sel.pc_bus, p->pc_sel.pc_dev, 242 p->pc_sel.pc_func, (p->pc_class << 16) | 243 (p->pc_subclass << 8) | p->pc_progif, 244 (p->pc_subdevice << 16) | p->pc_subvendor, 245 (p->pc_device << 16) | p->pc_vendor, 246 p->pc_revid, p->pc_hdr); 247 if (verbose) 248 list_verbose(p); 249 if (bars) 250 list_bars(fd, p); 251 if (caps) 252 list_caps(fd, p); 253 if (errors) 254 list_errors(fd, p); 255 if (vpd) 256 list_vpd(fd, p); 257 } 258 } while (pc.status == PCI_GETCONF_MORE_DEVS); 259 260 close(fd); 261 } 262 263 static void 264 list_bars(int fd, struct pci_conf *p) 265 { 266 struct pci_bar_io bar; 267 uint64_t base; 268 const char *type; 269 int i, range, max; 270 271 switch (p->pc_hdr & PCIM_HDRTYPE) { 272 case PCIM_HDRTYPE_NORMAL: 273 max = PCIR_MAX_BAR_0; 274 break; 275 case PCIM_HDRTYPE_BRIDGE: 276 max = PCIR_MAX_BAR_1; 277 break; 278 case PCIM_HDRTYPE_CARDBUS: 279 max = PCIR_MAX_BAR_2; 280 break; 281 default: 282 return; 283 } 284 285 for (i = 0; i <= max; i++) { 286 bar.pbi_sel = p->pc_sel; 287 bar.pbi_reg = PCIR_BAR(i); 288 if (ioctl(fd, PCIOCGETBAR, &bar) < 0) 289 continue; 290 if (PCI_BAR_IO(bar.pbi_base)) { 291 type = "I/O Port"; 292 range = 32; 293 base = bar.pbi_base & PCIM_BAR_IO_BASE; 294 } else { 295 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH) 296 type = "Prefetchable Memory"; 297 else 298 type = "Memory"; 299 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) { 300 case PCIM_BAR_MEM_32: 301 range = 32; 302 break; 303 case PCIM_BAR_MEM_1MB: 304 range = 20; 305 break; 306 case PCIM_BAR_MEM_64: 307 range = 64; 308 break; 309 default: 310 range = -1; 311 } 312 base = bar.pbi_base & ~((uint64_t)0xf); 313 } 314 printf(" bar [%02x] = type %s, range %2d, base %#jx, ", 315 PCIR_BAR(i), type, range, (uintmax_t)base); 316 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length, 317 bar.pbi_enabled ? "enabled" : "disabled"); 318 } 319 } 320 321 static void 322 list_verbose(struct pci_conf *p) 323 { 324 struct pci_vendor_info *vi; 325 struct pci_device_info *di; 326 const char *dp; 327 328 TAILQ_FOREACH(vi, &pci_vendors, link) { 329 if (vi->id == p->pc_vendor) { 330 printf(" vendor = '%s'\n", vi->desc); 331 break; 332 } 333 } 334 if (vi == NULL) { 335 di = NULL; 336 } else { 337 TAILQ_FOREACH(di, &vi->devs, link) { 338 if (di->id == p->pc_device) { 339 printf(" device = '%s'\n", di->desc); 340 break; 341 } 342 } 343 } 344 if ((dp = guess_class(p)) != NULL) 345 printf(" class = %s\n", dp); 346 if ((dp = guess_subclass(p)) != NULL) 347 printf(" subclass = %s\n", dp); 348 } 349 350 static void 351 list_vpd(int fd, struct pci_conf *p) 352 { 353 struct pci_list_vpd_io list; 354 struct pci_vpd_element *vpd, *end; 355 356 list.plvi_sel = p->pc_sel; 357 list.plvi_len = 0; 358 list.plvi_data = NULL; 359 if (ioctl(fd, PCIOCLISTVPD, &list) < 0 || list.plvi_len == 0) 360 return; 361 362 list.plvi_data = malloc(list.plvi_len); 363 if (ioctl(fd, PCIOCLISTVPD, &list) < 0) { 364 free(list.plvi_data); 365 return; 366 } 367 368 vpd = list.plvi_data; 369 end = (struct pci_vpd_element *)((char *)vpd + list.plvi_len); 370 for (; vpd < end; vpd = PVE_NEXT(vpd)) { 371 if (vpd->pve_flags == PVE_FLAG_IDENT) { 372 printf(" VPD ident = '%.*s'\n", 373 (int)vpd->pve_datalen, vpd->pve_data); 374 continue; 375 } 376 377 /* Ignore the checksum keyword. */ 378 if (!(vpd->pve_flags & PVE_FLAG_RW) && 379 memcmp(vpd->pve_keyword, "RV", 2) == 0) 380 continue; 381 382 /* Ignore remaining read-write space. */ 383 if (vpd->pve_flags & PVE_FLAG_RW && 384 memcmp(vpd->pve_keyword, "RW", 2) == 0) 385 continue; 386 387 /* Handle extended capability keyword. */ 388 if (!(vpd->pve_flags & PVE_FLAG_RW) && 389 memcmp(vpd->pve_keyword, "CP", 2) == 0) { 390 printf(" VPD ro CP = ID %02x in map 0x%x[0x%x]\n", 391 (unsigned int)vpd->pve_data[0], 392 PCIR_BAR((unsigned int)vpd->pve_data[1]), 393 (unsigned int)vpd->pve_data[3] << 8 | 394 (unsigned int)vpd->pve_data[2]); 395 continue; 396 } 397 398 /* Remaining keywords should all have ASCII values. */ 399 printf(" VPD %s %c%c = '%.*s'\n", 400 vpd->pve_flags & PVE_FLAG_RW ? "rw" : "ro", 401 vpd->pve_keyword[0], vpd->pve_keyword[1], 402 (int)vpd->pve_datalen, vpd->pve_data); 403 } 404 free(list.plvi_data); 405 } 406 407 /* 408 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c. 409 */ 410 static struct 411 { 412 int class; 413 int subclass; 414 const char *desc; 415 } pci_nomatch_tab[] = { 416 {PCIC_OLD, -1, "old"}, 417 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"}, 418 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"}, 419 {PCIC_STORAGE, -1, "mass storage"}, 420 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"}, 421 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"}, 422 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"}, 423 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"}, 424 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"}, 425 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"}, 426 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"}, 427 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"}, 428 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"}, 429 {PCIC_NETWORK, -1, "network"}, 430 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"}, 431 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"}, 432 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"}, 433 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"}, 434 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"}, 435 {PCIC_DISPLAY, -1, "display"}, 436 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"}, 437 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"}, 438 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"}, 439 {PCIC_MULTIMEDIA, -1, "multimedia"}, 440 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"}, 441 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"}, 442 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"}, 443 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"}, 444 {PCIC_MEMORY, -1, "memory"}, 445 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"}, 446 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"}, 447 {PCIC_BRIDGE, -1, "bridge"}, 448 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"}, 449 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"}, 450 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"}, 451 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"}, 452 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"}, 453 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"}, 454 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"}, 455 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"}, 456 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"}, 457 {PCIC_SIMPLECOMM, -1, "simple comms"}, 458 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */ 459 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"}, 460 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"}, 461 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"}, 462 {PCIC_BASEPERIPH, -1, "base peripheral"}, 463 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"}, 464 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"}, 465 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"}, 466 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"}, 467 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"}, 468 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"}, 469 {PCIC_INPUTDEV, -1, "input device"}, 470 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"}, 471 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"}, 472 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"}, 473 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"}, 474 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"}, 475 {PCIC_DOCKING, -1, "docking station"}, 476 {PCIC_PROCESSOR, -1, "processor"}, 477 {PCIC_SERIALBUS, -1, "serial bus"}, 478 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"}, 479 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"}, 480 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"}, 481 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"}, 482 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"}, 483 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"}, 484 {PCIC_WIRELESS, -1, "wireless controller"}, 485 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"}, 486 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"}, 487 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"}, 488 {PCIC_INTELLIIO, -1, "intelligent I/O controller"}, 489 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"}, 490 {PCIC_SATCOM, -1, "satellite communication"}, 491 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"}, 492 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"}, 493 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"}, 494 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"}, 495 {PCIC_CRYPTO, -1, "encrypt/decrypt"}, 496 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"}, 497 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"}, 498 {PCIC_DASP, -1, "dasp"}, 499 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"}, 500 {0, 0, NULL} 501 }; 502 503 static const char * 504 guess_class(struct pci_conf *p) 505 { 506 int i; 507 508 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 509 if (pci_nomatch_tab[i].class == p->pc_class) 510 return(pci_nomatch_tab[i].desc); 511 } 512 return(NULL); 513 } 514 515 static const char * 516 guess_subclass(struct pci_conf *p) 517 { 518 int i; 519 520 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 521 if ((pci_nomatch_tab[i].class == p->pc_class) && 522 (pci_nomatch_tab[i].subclass == p->pc_subclass)) 523 return(pci_nomatch_tab[i].desc); 524 } 525 return(NULL); 526 } 527 528 static int 529 load_vendors(void) 530 { 531 const char *dbf; 532 FILE *db; 533 struct pci_vendor_info *cv; 534 struct pci_device_info *cd; 535 char buf[1024], str[1024]; 536 char *ch; 537 int id, error; 538 539 /* 540 * Locate the database and initialise. 541 */ 542 TAILQ_INIT(&pci_vendors); 543 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL) 544 dbf = _PATH_PCIVDB; 545 if ((db = fopen(dbf, "r")) == NULL) 546 return(1); 547 cv = NULL; 548 cd = NULL; 549 error = 0; 550 551 /* 552 * Scan input lines from the database 553 */ 554 for (;;) { 555 if (fgets(buf, sizeof(buf), db) == NULL) 556 break; 557 558 if ((ch = strchr(buf, '#')) != NULL) 559 *ch = '\0'; 560 ch = strchr(buf, '\0') - 1; 561 while (ch > buf && isspace(*ch)) 562 *ch-- = '\0'; 563 if (ch <= buf) 564 continue; 565 566 /* Can't handle subvendor / subdevice entries yet */ 567 if (buf[0] == '\t' && buf[1] == '\t') 568 continue; 569 570 /* Check for vendor entry */ 571 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) { 572 if ((id == 0) || (strlen(str) < 1)) 573 continue; 574 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) { 575 warn("allocating vendor entry"); 576 error = 1; 577 break; 578 } 579 if ((cv->desc = strdup(str)) == NULL) { 580 free(cv); 581 warn("allocating vendor description"); 582 error = 1; 583 break; 584 } 585 cv->id = id; 586 TAILQ_INIT(&cv->devs); 587 TAILQ_INSERT_TAIL(&pci_vendors, cv, link); 588 continue; 589 } 590 591 /* Check for device entry */ 592 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) { 593 if ((id == 0) || (strlen(str) < 1)) 594 continue; 595 if (cv == NULL) { 596 warnx("device entry with no vendor!"); 597 continue; 598 } 599 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) { 600 warn("allocating device entry"); 601 error = 1; 602 break; 603 } 604 if ((cd->desc = strdup(str)) == NULL) { 605 free(cd); 606 warn("allocating device description"); 607 error = 1; 608 break; 609 } 610 cd->id = id; 611 TAILQ_INSERT_TAIL(&cv->devs, cd, link); 612 continue; 613 } 614 615 /* It's a comment or junk, ignore it */ 616 } 617 if (ferror(db)) 618 error = 1; 619 fclose(db); 620 621 return(error); 622 } 623 624 uint32_t 625 read_config(int fd, struct pcisel *sel, long reg, int width) 626 { 627 struct pci_io pi; 628 629 pi.pi_sel = *sel; 630 pi.pi_reg = reg; 631 pi.pi_width = width; 632 633 if (ioctl(fd, PCIOCREAD, &pi) < 0) 634 err(1, "ioctl(PCIOCREAD)"); 635 636 return (pi.pi_data); 637 } 638 639 static struct pcisel 640 getdevice(const char *name) 641 { 642 struct pci_conf_io pc; 643 struct pci_conf conf[1]; 644 struct pci_match_conf patterns[1]; 645 char *cp; 646 int fd; 647 648 fd = open(_PATH_DEVPCI, O_RDONLY, 0); 649 if (fd < 0) 650 err(1, "%s", _PATH_DEVPCI); 651 652 bzero(&pc, sizeof(struct pci_conf_io)); 653 pc.match_buf_len = sizeof(conf); 654 pc.matches = conf; 655 656 bzero(&patterns, sizeof(patterns)); 657 658 /* 659 * The pattern structure requires the unit to be split out from 660 * the driver name. Walk backwards from the end of the name to 661 * find the start of the unit. 662 */ 663 if (name[0] == '\0') 664 err(1, "Empty device name"); 665 cp = strchr(name, '\0'); 666 assert(cp != NULL && cp != name); 667 cp--; 668 while (cp != name && isdigit(cp[-1])) 669 cp--; 670 if (cp == name) 671 errx(1, "Invalid device name"); 672 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name)) 673 errx(1, "Device name i2s too long"); 674 memcpy(patterns[0].pd_name, name, cp - name); 675 patterns[0].pd_unit = strtol(cp, &cp, 10); 676 assert(*cp == '\0'); 677 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT; 678 pc.num_patterns = 1; 679 pc.pat_buf_len = sizeof(patterns); 680 pc.patterns = patterns; 681 682 if (ioctl(fd, PCIOCGETCONF, &pc) == -1) 683 err(1, "ioctl(PCIOCGETCONF)"); 684 if (pc.status != PCI_GETCONF_LAST_DEVICE && 685 pc.status != PCI_GETCONF_MORE_DEVS) 686 errx(1, "error returned from PCIOCGETCONF ioctl"); 687 close(fd); 688 if (pc.num_matches == 0) 689 errx(1, "Device not found"); 690 return (conf[0].pc_sel); 691 } 692 693 static struct pcisel 694 parsesel(const char *str) 695 { 696 char *ep = strchr(str, '@'); 697 char *epbase; 698 struct pcisel sel; 699 unsigned long selarr[4]; 700 int i; 701 702 if (ep == NULL) 703 ep = (char *)str; 704 else 705 ep++; 706 707 epbase = ep; 708 709 if (strncmp(ep, "pci", 3) == 0) { 710 ep += 3; 711 i = 0; 712 do { 713 selarr[i++] = strtoul(ep, &ep, 10); 714 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4); 715 716 if (i > 2) 717 sel.pc_func = selarr[--i]; 718 else 719 sel.pc_func = 0; 720 sel.pc_dev = selarr[--i]; 721 sel.pc_bus = selarr[--i]; 722 if (i > 0) 723 sel.pc_domain = selarr[--i]; 724 else 725 sel.pc_domain = 0; 726 } 727 if (*ep != '\x0' || ep == epbase) 728 errx(1, "cannot parse selector %s", str); 729 return sel; 730 } 731 732 static struct pcisel 733 getsel(const char *str) 734 { 735 736 /* 737 * No device names contain colons and selectors always contain 738 * at least one colon. 739 */ 740 if (strchr(str, ':') == NULL) 741 return (getdevice(str)); 742 else 743 return (parsesel(str)); 744 } 745 746 static void 747 readone(int fd, struct pcisel *sel, long reg, int width) 748 { 749 750 printf("%0*x", width*2, read_config(fd, sel, reg, width)); 751 } 752 753 static void 754 readit(const char *name, const char *reg, int width) 755 { 756 long rstart; 757 long rend; 758 long r; 759 char *end; 760 int i; 761 int fd; 762 struct pcisel sel; 763 764 fd = open(_PATH_DEVPCI, O_RDWR, 0); 765 if (fd < 0) 766 err(1, "%s", _PATH_DEVPCI); 767 768 rend = rstart = strtol(reg, &end, 0); 769 if (end && *end == ':') { 770 end++; 771 rend = strtol(end, (char **) 0, 0); 772 } 773 sel = getsel(name); 774 for (i = 1, r = rstart; r <= rend; i++, r += width) { 775 readone(fd, &sel, r, width); 776 if (i && !(i % 8)) 777 putchar(' '); 778 putchar(i % (16/width) ? ' ' : '\n'); 779 } 780 if (i % (16/width) != 1) 781 putchar('\n'); 782 close(fd); 783 } 784 785 static void 786 writeit(const char *name, const char *reg, const char *data, int width) 787 { 788 int fd; 789 struct pci_io pi; 790 791 pi.pi_sel = getsel(name); 792 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */ 793 pi.pi_width = width; 794 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */ 795 796 fd = open(_PATH_DEVPCI, O_RDWR, 0); 797 if (fd < 0) 798 err(1, "%s", _PATH_DEVPCI); 799 800 if (ioctl(fd, PCIOCWRITE, &pi) < 0) 801 err(1, "ioctl(PCIOCWRITE)"); 802 } 803 804 static void 805 chkattached(const char *name) 806 { 807 int fd; 808 struct pci_io pi; 809 810 pi.pi_sel = getsel(name); 811 812 fd = open(_PATH_DEVPCI, O_RDWR, 0); 813 if (fd < 0) 814 err(1, "%s", _PATH_DEVPCI); 815 816 if (ioctl(fd, PCIOCATTACHED, &pi) < 0) 817 err(1, "ioctl(PCIOCATTACHED)"); 818 819 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */ 820 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached"); 821 } 822