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 <stdbool.h> 43 #include <stdlib.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <sys/pciio.h> 48 #include <sys/queue.h> 49 50 #include <dev/pci/pcireg.h> 51 52 #include "pathnames.h" 53 #include "pciconf.h" 54 55 struct pci_device_info 56 { 57 TAILQ_ENTRY(pci_device_info) link; 58 int id; 59 char *desc; 60 }; 61 62 struct pci_vendor_info 63 { 64 TAILQ_ENTRY(pci_vendor_info) link; 65 TAILQ_HEAD(,pci_device_info) devs; 66 int id; 67 char *desc; 68 }; 69 70 static TAILQ_HEAD(,pci_vendor_info) pci_vendors; 71 72 static struct pcisel getsel(const char *str); 73 static void list_bridge(int fd, struct pci_conf *p); 74 static void list_bars(int fd, struct pci_conf *p); 75 static void list_devs(const char *name, int verbose, int bars, int bridge, 76 int caps, int errors, int vpd); 77 static void list_verbose(struct pci_conf *p); 78 static void list_vpd(int fd, struct pci_conf *p); 79 static const char *guess_class(struct pci_conf *p); 80 static const char *guess_subclass(struct pci_conf *p); 81 static int load_vendors(void); 82 static void readit(const char *, const char *, int); 83 static void writeit(const char *, const char *, const char *, int); 84 static void chkattached(const char *); 85 86 static int exitstatus = 0; 87 88 static void 89 usage(void) 90 { 91 fprintf(stderr, "%s\n%s\n%s\n%s\n", 92 "usage: pciconf -l [-BbcevV] [device]", 93 " pciconf -a device", 94 " pciconf -r [-b | -h] device addr[:addr2]", 95 " pciconf -w [-b | -h] device addr value"); 96 exit (1); 97 } 98 99 int 100 main(int argc, char **argv) 101 { 102 int c; 103 int listmode, readmode, writemode, attachedmode; 104 int bars, bridge, caps, errors, verbose, vpd; 105 int byte, isshort; 106 107 listmode = readmode = writemode = attachedmode = 0; 108 bars = bridge = caps = errors = verbose = vpd = byte = isshort = 0; 109 110 while ((c = getopt(argc, argv, "aBbcehlrwVv")) != -1) { 111 switch(c) { 112 case 'a': 113 attachedmode = 1; 114 break; 115 116 case 'B': 117 bridge = 1; 118 break; 119 120 case 'b': 121 bars = 1; 122 byte = 1; 123 break; 124 125 case 'c': 126 caps = 1; 127 break; 128 129 case 'e': 130 errors = 1; 131 break; 132 133 case 'h': 134 isshort = 1; 135 break; 136 137 case 'l': 138 listmode = 1; 139 break; 140 141 case 'r': 142 readmode = 1; 143 break; 144 145 case 'w': 146 writemode = 1; 147 break; 148 149 case 'v': 150 verbose = 1; 151 break; 152 153 case 'V': 154 vpd = 1; 155 break; 156 157 default: 158 usage(); 159 } 160 } 161 162 if ((listmode && optind >= argc + 1) 163 || (writemode && optind + 3 != argc) 164 || (readmode && optind + 2 != argc) 165 || (attachedmode && optind + 1 != argc)) 166 usage(); 167 168 if (listmode) { 169 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose, 170 bars, bridge, caps, errors, vpd); 171 } else if (attachedmode) { 172 chkattached(argv[optind]); 173 } else if (readmode) { 174 readit(argv[optind], argv[optind + 1], 175 byte ? 1 : isshort ? 2 : 4); 176 } else if (writemode) { 177 writeit(argv[optind], argv[optind + 1], argv[optind + 2], 178 byte ? 1 : isshort ? 2 : 4); 179 } else { 180 usage(); 181 } 182 183 return exitstatus; 184 } 185 186 static void 187 list_devs(const char *name, int verbose, int bars, int bridge, int caps, 188 int errors, int vpd) 189 { 190 int fd; 191 struct pci_conf_io pc; 192 struct pci_conf conf[255], *p; 193 struct pci_match_conf patterns[1]; 194 int none_count = 0; 195 196 if (verbose) 197 load_vendors(); 198 199 fd = open(_PATH_DEVPCI, (bridge || caps || errors) ? O_RDWR : O_RDONLY, 200 0); 201 if (fd < 0) 202 err(1, "%s", _PATH_DEVPCI); 203 204 bzero(&pc, sizeof(struct pci_conf_io)); 205 pc.match_buf_len = sizeof(conf); 206 pc.matches = conf; 207 if (name != NULL) { 208 bzero(&patterns, sizeof(patterns)); 209 patterns[0].pc_sel = getsel(name); 210 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | 211 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV | 212 PCI_GETCONF_MATCH_FUNC; 213 pc.num_patterns = 1; 214 pc.pat_buf_len = sizeof(patterns); 215 pc.patterns = patterns; 216 } 217 218 do { 219 if (ioctl(fd, PCIOCGETCONF, &pc) == -1) 220 err(1, "ioctl(PCIOCGETCONF)"); 221 222 /* 223 * 255 entries should be more than enough for most people, 224 * but if someone has more devices, and then changes things 225 * around between ioctls, we'll do the cheesy thing and 226 * just bail. The alternative would be to go back to the 227 * beginning of the list, and print things twice, which may 228 * not be desirable. 229 */ 230 if (pc.status == PCI_GETCONF_LIST_CHANGED) { 231 warnx("PCI device list changed, please try again"); 232 exitstatus = 1; 233 close(fd); 234 return; 235 } else if (pc.status == PCI_GETCONF_ERROR) { 236 warnx("error returned from PCIOCGETCONF ioctl"); 237 exitstatus = 1; 238 close(fd); 239 return; 240 } 241 for (p = conf; p < &conf[pc.num_matches]; p++) { 242 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x " 243 "chip=0x%08x rev=0x%02x hdr=0x%02x\n", 244 *p->pd_name ? p->pd_name : 245 "none", 246 *p->pd_name ? (int)p->pd_unit : 247 none_count++, p->pc_sel.pc_domain, 248 p->pc_sel.pc_bus, p->pc_sel.pc_dev, 249 p->pc_sel.pc_func, (p->pc_class << 16) | 250 (p->pc_subclass << 8) | p->pc_progif, 251 (p->pc_subdevice << 16) | p->pc_subvendor, 252 (p->pc_device << 16) | p->pc_vendor, 253 p->pc_revid, p->pc_hdr); 254 if (verbose) 255 list_verbose(p); 256 if (bars) 257 list_bars(fd, p); 258 if (bridge) 259 list_bridge(fd, p); 260 if (caps) 261 list_caps(fd, p); 262 if (errors) 263 list_errors(fd, p); 264 if (vpd) 265 list_vpd(fd, p); 266 } 267 } while (pc.status == PCI_GETCONF_MORE_DEVS); 268 269 close(fd); 270 } 271 272 static void 273 print_bus_range(int fd, struct pci_conf *p, int secreg, int subreg) 274 { 275 uint8_t secbus, subbus; 276 277 secbus = read_config(fd, &p->pc_sel, secreg, 1); 278 subbus = read_config(fd, &p->pc_sel, subreg, 1); 279 printf(" bus range = %u-%u\n", secbus, subbus); 280 } 281 282 static void 283 print_window(int reg, const char *type, int range, uint64_t base, 284 uint64_t limit) 285 { 286 287 printf(" window[%02x] = type %s, range %2d, addr %#jx-%#jx, %s\n", 288 reg, type, range, (uintmax_t)base, (uintmax_t)limit, 289 base < limit ? "enabled" : "disabled"); 290 } 291 292 static void 293 print_special_decode(bool isa, bool vga, bool subtractive) 294 { 295 bool comma; 296 297 if (isa || vga || subtractive) { 298 comma = false; 299 printf(" decode = "); 300 if (isa) { 301 printf("ISA"); 302 comma = true; 303 } 304 if (vga) { 305 printf("%sVGA", comma ? ", " : ""); 306 comma = true; 307 } 308 if (subtractive) 309 printf("%ssubtractive", comma ? ", " : ""); 310 printf("\n"); 311 } 312 } 313 314 static void 315 print_bridge_windows(int fd, struct pci_conf *p) 316 { 317 uint64_t base, limit; 318 uint32_t val; 319 uint16_t bctl; 320 bool subtractive; 321 int range; 322 323 /* 324 * XXX: This assumes that a window with a base and limit of 0 325 * is not implemented. In theory a window might be programmed 326 * at the smallest size with a base of 0, but those do not seem 327 * common in practice. 328 */ 329 val = read_config(fd, &p->pc_sel, PCIR_IOBASEL_1, 1); 330 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1) != 0) { 331 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 332 base = PCI_PPBIOBASE( 333 read_config(fd, &p->pc_sel, PCIR_IOBASEH_1, 2), 334 val); 335 limit = PCI_PPBIOLIMIT( 336 read_config(fd, &p->pc_sel, PCIR_IOLIMITH_1, 2), 337 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1)); 338 range = 32; 339 } else { 340 base = PCI_PPBIOBASE(0, val); 341 limit = PCI_PPBIOLIMIT(0, 342 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1)); 343 range = 16; 344 } 345 print_window(PCIR_IOBASEL_1, "I/O Port", range, base, limit); 346 } 347 348 base = PCI_PPBMEMBASE(0, 349 read_config(fd, &p->pc_sel, PCIR_MEMBASE_1, 2)); 350 limit = PCI_PPBMEMLIMIT(0, 351 read_config(fd, &p->pc_sel, PCIR_MEMLIMIT_1, 2)); 352 print_window(PCIR_MEMBASE_1, "Memory", 32, base, limit); 353 354 val = read_config(fd, &p->pc_sel, PCIR_PMBASEL_1, 2); 355 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2) != 0) { 356 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 357 base = PCI_PPBMEMBASE( 358 read_config(fd, &p->pc_sel, PCIR_PMBASEH_1, 4), 359 val); 360 limit = PCI_PPBMEMLIMIT( 361 read_config(fd, &p->pc_sel, PCIR_PMLIMITH_1, 4), 362 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2)); 363 range = 64; 364 } else { 365 base = PCI_PPBMEMBASE(0, val); 366 limit = PCI_PPBMEMLIMIT(0, 367 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2)); 368 range = 32; 369 } 370 print_window(PCIR_PMBASEL_1, "Prefetchable Memory", range, base, 371 limit); 372 } 373 374 /* 375 * XXX: This list of bridges that are subtractive but do not set 376 * progif to indicate it is copied from pci_pci.c. 377 */ 378 subtractive = p->pc_progif == PCIP_BRIDGE_PCI_SUBTRACTIVE; 379 switch (p->pc_device << 16 | p->pc_vendor) { 380 case 0xa002177d: /* Cavium ThunderX */ 381 case 0x124b8086: /* Intel 82380FB Mobile */ 382 case 0x060513d7: /* Toshiba ???? */ 383 subtractive = true; 384 } 385 if (p->pc_vendor == 0x8086 && (p->pc_device & 0xff00) == 0x2400) 386 subtractive = true; 387 388 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_1, 2); 389 print_special_decode(bctl & PCIB_BCR_ISA_ENABLE, 390 bctl & PCIB_BCR_VGA_ENABLE, subtractive); 391 } 392 393 static void 394 print_cardbus_mem_window(int fd, struct pci_conf *p, int basereg, int limitreg, 395 bool prefetch) 396 { 397 398 print_window(basereg, prefetch ? "Prefetchable Memory" : "Memory", 32, 399 PCI_CBBMEMBASE(read_config(fd, &p->pc_sel, basereg, 4)), 400 PCI_CBBMEMLIMIT(read_config(fd, &p->pc_sel, limitreg, 4))); 401 } 402 403 static void 404 print_cardbus_io_window(int fd, struct pci_conf *p, int basereg, int limitreg) 405 { 406 uint32_t base, limit; 407 uint32_t val; 408 int range; 409 410 val = read_config(fd, &p->pc_sel, basereg, 2); 411 if ((val & PCIM_CBBIO_MASK) == PCIM_CBBIO_32) { 412 base = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, basereg, 4)); 413 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 4)); 414 range = 32; 415 } else { 416 base = PCI_CBBIOBASE(val); 417 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 2)); 418 range = 16; 419 } 420 print_window(basereg, "I/O Port", range, base, limit); 421 } 422 423 static void 424 print_cardbus_windows(int fd, struct pci_conf *p) 425 { 426 uint16_t bctl; 427 428 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_2, 2); 429 print_cardbus_mem_window(fd, p, PCIR_MEMBASE0_2, PCIR_MEMLIMIT0_2, 430 bctl & CBB_BCR_PREFETCH_0_ENABLE); 431 print_cardbus_mem_window(fd, p, PCIR_MEMBASE1_2, PCIR_MEMLIMIT1_2, 432 bctl & CBB_BCR_PREFETCH_1_ENABLE); 433 print_cardbus_io_window(fd, p, PCIR_IOBASE0_2, PCIR_IOLIMIT0_2); 434 print_cardbus_io_window(fd, p, PCIR_IOBASE1_2, PCIR_IOLIMIT1_2); 435 print_special_decode(bctl & CBB_BCR_ISA_ENABLE, 436 bctl & CBB_BCR_VGA_ENABLE, false); 437 } 438 439 static void 440 list_bridge(int fd, struct pci_conf *p) 441 { 442 443 switch (p->pc_hdr & PCIM_HDRTYPE) { 444 case PCIM_HDRTYPE_BRIDGE: 445 print_bus_range(fd, p, PCIR_SECBUS_1, PCIR_SUBBUS_1); 446 print_bridge_windows(fd, p); 447 break; 448 case PCIM_HDRTYPE_CARDBUS: 449 print_bus_range(fd, p, PCIR_SECBUS_2, PCIR_SUBBUS_2); 450 print_cardbus_windows(fd, p); 451 break; 452 } 453 } 454 455 static void 456 list_bars(int fd, struct pci_conf *p) 457 { 458 int i, max; 459 460 switch (p->pc_hdr & PCIM_HDRTYPE) { 461 case PCIM_HDRTYPE_NORMAL: 462 max = PCIR_MAX_BAR_0; 463 break; 464 case PCIM_HDRTYPE_BRIDGE: 465 max = PCIR_MAX_BAR_1; 466 break; 467 case PCIM_HDRTYPE_CARDBUS: 468 max = PCIR_MAX_BAR_2; 469 break; 470 default: 471 return; 472 } 473 474 for (i = 0; i <= max; i++) 475 print_bar(fd, p, "bar ", PCIR_BAR(i)); 476 } 477 478 void 479 print_bar(int fd, struct pci_conf *p, const char *label, uint16_t bar_offset) 480 { 481 uint64_t base; 482 const char *type; 483 struct pci_bar_io bar; 484 int range; 485 486 bar.pbi_sel = p->pc_sel; 487 bar.pbi_reg = bar_offset; 488 if (ioctl(fd, PCIOCGETBAR, &bar) < 0) 489 return; 490 if (PCI_BAR_IO(bar.pbi_base)) { 491 type = "I/O Port"; 492 range = 32; 493 base = bar.pbi_base & PCIM_BAR_IO_BASE; 494 } else { 495 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH) 496 type = "Prefetchable Memory"; 497 else 498 type = "Memory"; 499 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) { 500 case PCIM_BAR_MEM_32: 501 range = 32; 502 break; 503 case PCIM_BAR_MEM_1MB: 504 range = 20; 505 break; 506 case PCIM_BAR_MEM_64: 507 range = 64; 508 break; 509 default: 510 range = -1; 511 } 512 base = bar.pbi_base & ~((uint64_t)0xf); 513 } 514 printf(" %s[%02x] = type %s, range %2d, base %#jx, ", 515 label, bar_offset, type, range, (uintmax_t)base); 516 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length, 517 bar.pbi_enabled ? "enabled" : "disabled"); 518 } 519 520 static void 521 list_verbose(struct pci_conf *p) 522 { 523 struct pci_vendor_info *vi; 524 struct pci_device_info *di; 525 const char *dp; 526 527 TAILQ_FOREACH(vi, &pci_vendors, link) { 528 if (vi->id == p->pc_vendor) { 529 printf(" vendor = '%s'\n", vi->desc); 530 break; 531 } 532 } 533 if (vi == NULL) { 534 di = NULL; 535 } else { 536 TAILQ_FOREACH(di, &vi->devs, link) { 537 if (di->id == p->pc_device) { 538 printf(" device = '%s'\n", di->desc); 539 break; 540 } 541 } 542 } 543 if ((dp = guess_class(p)) != NULL) 544 printf(" class = %s\n", dp); 545 if ((dp = guess_subclass(p)) != NULL) 546 printf(" subclass = %s\n", dp); 547 } 548 549 static void 550 list_vpd(int fd, struct pci_conf *p) 551 { 552 struct pci_list_vpd_io list; 553 struct pci_vpd_element *vpd, *end; 554 555 list.plvi_sel = p->pc_sel; 556 list.plvi_len = 0; 557 list.plvi_data = NULL; 558 if (ioctl(fd, PCIOCLISTVPD, &list) < 0 || list.plvi_len == 0) 559 return; 560 561 list.plvi_data = malloc(list.plvi_len); 562 if (ioctl(fd, PCIOCLISTVPD, &list) < 0) { 563 free(list.plvi_data); 564 return; 565 } 566 567 vpd = list.plvi_data; 568 end = (struct pci_vpd_element *)((char *)vpd + list.plvi_len); 569 for (; vpd < end; vpd = PVE_NEXT(vpd)) { 570 if (vpd->pve_flags == PVE_FLAG_IDENT) { 571 printf(" VPD ident = '%.*s'\n", 572 (int)vpd->pve_datalen, vpd->pve_data); 573 continue; 574 } 575 576 /* Ignore the checksum keyword. */ 577 if (!(vpd->pve_flags & PVE_FLAG_RW) && 578 memcmp(vpd->pve_keyword, "RV", 2) == 0) 579 continue; 580 581 /* Ignore remaining read-write space. */ 582 if (vpd->pve_flags & PVE_FLAG_RW && 583 memcmp(vpd->pve_keyword, "RW", 2) == 0) 584 continue; 585 586 /* Handle extended capability keyword. */ 587 if (!(vpd->pve_flags & PVE_FLAG_RW) && 588 memcmp(vpd->pve_keyword, "CP", 2) == 0) { 589 printf(" VPD ro CP = ID %02x in map 0x%x[0x%x]\n", 590 (unsigned int)vpd->pve_data[0], 591 PCIR_BAR((unsigned int)vpd->pve_data[1]), 592 (unsigned int)vpd->pve_data[3] << 8 | 593 (unsigned int)vpd->pve_data[2]); 594 continue; 595 } 596 597 /* Remaining keywords should all have ASCII values. */ 598 printf(" VPD %s %c%c = '%.*s'\n", 599 vpd->pve_flags & PVE_FLAG_RW ? "rw" : "ro", 600 vpd->pve_keyword[0], vpd->pve_keyword[1], 601 (int)vpd->pve_datalen, vpd->pve_data); 602 } 603 free(list.plvi_data); 604 } 605 606 /* 607 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c. 608 */ 609 static struct 610 { 611 int class; 612 int subclass; 613 const char *desc; 614 } pci_nomatch_tab[] = { 615 {PCIC_OLD, -1, "old"}, 616 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"}, 617 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"}, 618 {PCIC_STORAGE, -1, "mass storage"}, 619 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"}, 620 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"}, 621 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"}, 622 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"}, 623 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"}, 624 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"}, 625 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"}, 626 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"}, 627 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"}, 628 {PCIC_NETWORK, -1, "network"}, 629 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"}, 630 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"}, 631 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"}, 632 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"}, 633 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"}, 634 {PCIC_DISPLAY, -1, "display"}, 635 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"}, 636 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"}, 637 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"}, 638 {PCIC_MULTIMEDIA, -1, "multimedia"}, 639 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"}, 640 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"}, 641 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"}, 642 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"}, 643 {PCIC_MEMORY, -1, "memory"}, 644 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"}, 645 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"}, 646 {PCIC_BRIDGE, -1, "bridge"}, 647 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"}, 648 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"}, 649 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"}, 650 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"}, 651 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"}, 652 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"}, 653 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"}, 654 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"}, 655 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"}, 656 {PCIC_SIMPLECOMM, -1, "simple comms"}, 657 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */ 658 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"}, 659 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"}, 660 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"}, 661 {PCIC_BASEPERIPH, -1, "base peripheral"}, 662 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"}, 663 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"}, 664 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"}, 665 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"}, 666 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"}, 667 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"}, 668 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_IOMMU, "IOMMU"}, 669 {PCIC_INPUTDEV, -1, "input device"}, 670 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"}, 671 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"}, 672 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"}, 673 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"}, 674 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"}, 675 {PCIC_DOCKING, -1, "docking station"}, 676 {PCIC_PROCESSOR, -1, "processor"}, 677 {PCIC_SERIALBUS, -1, "serial bus"}, 678 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"}, 679 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"}, 680 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"}, 681 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"}, 682 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"}, 683 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"}, 684 {PCIC_WIRELESS, -1, "wireless controller"}, 685 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"}, 686 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"}, 687 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"}, 688 {PCIC_INTELLIIO, -1, "intelligent I/O controller"}, 689 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"}, 690 {PCIC_SATCOM, -1, "satellite communication"}, 691 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"}, 692 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"}, 693 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"}, 694 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"}, 695 {PCIC_CRYPTO, -1, "encrypt/decrypt"}, 696 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"}, 697 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"}, 698 {PCIC_DASP, -1, "dasp"}, 699 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"}, 700 {PCIC_DASP, PCIS_DASP_PERFCNTRS, "performance counters"}, 701 {PCIC_DASP, PCIS_DASP_COMM_SYNC, "communication synchronizer"}, 702 {PCIC_DASP, PCIS_DASP_MGMT_CARD, "signal processing management"}, 703 {PCIC_ACCEL, -1, "processing accelerators"}, 704 {PCIC_ACCEL, PCIS_ACCEL_PROCESSING, "processing accelerators"}, 705 {PCIC_INSTRUMENT, -1, "non-essential instrumentation"}, 706 {0, 0, NULL} 707 }; 708 709 static const char * 710 guess_class(struct pci_conf *p) 711 { 712 int i; 713 714 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 715 if (pci_nomatch_tab[i].class == p->pc_class) 716 return(pci_nomatch_tab[i].desc); 717 } 718 return(NULL); 719 } 720 721 static const char * 722 guess_subclass(struct pci_conf *p) 723 { 724 int i; 725 726 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 727 if ((pci_nomatch_tab[i].class == p->pc_class) && 728 (pci_nomatch_tab[i].subclass == p->pc_subclass)) 729 return(pci_nomatch_tab[i].desc); 730 } 731 return(NULL); 732 } 733 734 static int 735 load_vendors(void) 736 { 737 const char *dbf; 738 FILE *db; 739 struct pci_vendor_info *cv; 740 struct pci_device_info *cd; 741 char buf[1024], str[1024]; 742 char *ch; 743 int id, error; 744 745 /* 746 * Locate the database and initialise. 747 */ 748 TAILQ_INIT(&pci_vendors); 749 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL) 750 dbf = _PATH_LPCIVDB; 751 if ((db = fopen(dbf, "r")) == NULL) { 752 dbf = _PATH_PCIVDB; 753 if ((db = fopen(dbf, "r")) == NULL) 754 return(1); 755 } 756 cv = NULL; 757 cd = NULL; 758 error = 0; 759 760 /* 761 * Scan input lines from the database 762 */ 763 for (;;) { 764 if (fgets(buf, sizeof(buf), db) == NULL) 765 break; 766 767 if ((ch = strchr(buf, '#')) != NULL) 768 *ch = '\0'; 769 ch = strchr(buf, '\0') - 1; 770 while (ch > buf && isspace(*ch)) 771 *ch-- = '\0'; 772 if (ch <= buf) 773 continue; 774 775 /* Can't handle subvendor / subdevice entries yet */ 776 if (buf[0] == '\t' && buf[1] == '\t') 777 continue; 778 779 /* Check for vendor entry */ 780 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) { 781 if ((id == 0) || (strlen(str) < 1)) 782 continue; 783 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) { 784 warn("allocating vendor entry"); 785 error = 1; 786 break; 787 } 788 if ((cv->desc = strdup(str)) == NULL) { 789 free(cv); 790 warn("allocating vendor description"); 791 error = 1; 792 break; 793 } 794 cv->id = id; 795 TAILQ_INIT(&cv->devs); 796 TAILQ_INSERT_TAIL(&pci_vendors, cv, link); 797 continue; 798 } 799 800 /* Check for device entry */ 801 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) { 802 if ((id == 0) || (strlen(str) < 1)) 803 continue; 804 if (cv == NULL) { 805 warnx("device entry with no vendor!"); 806 continue; 807 } 808 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) { 809 warn("allocating device entry"); 810 error = 1; 811 break; 812 } 813 if ((cd->desc = strdup(str)) == NULL) { 814 free(cd); 815 warn("allocating device description"); 816 error = 1; 817 break; 818 } 819 cd->id = id; 820 TAILQ_INSERT_TAIL(&cv->devs, cd, link); 821 continue; 822 } 823 824 /* It's a comment or junk, ignore it */ 825 } 826 if (ferror(db)) 827 error = 1; 828 fclose(db); 829 830 return(error); 831 } 832 833 uint32_t 834 read_config(int fd, struct pcisel *sel, long reg, int width) 835 { 836 struct pci_io pi; 837 838 pi.pi_sel = *sel; 839 pi.pi_reg = reg; 840 pi.pi_width = width; 841 842 if (ioctl(fd, PCIOCREAD, &pi) < 0) 843 err(1, "ioctl(PCIOCREAD)"); 844 845 return (pi.pi_data); 846 } 847 848 static struct pcisel 849 getdevice(const char *name) 850 { 851 struct pci_conf_io pc; 852 struct pci_conf conf[1]; 853 struct pci_match_conf patterns[1]; 854 char *cp; 855 int fd; 856 857 fd = open(_PATH_DEVPCI, O_RDONLY, 0); 858 if (fd < 0) 859 err(1, "%s", _PATH_DEVPCI); 860 861 bzero(&pc, sizeof(struct pci_conf_io)); 862 pc.match_buf_len = sizeof(conf); 863 pc.matches = conf; 864 865 bzero(&patterns, sizeof(patterns)); 866 867 /* 868 * The pattern structure requires the unit to be split out from 869 * the driver name. Walk backwards from the end of the name to 870 * find the start of the unit. 871 */ 872 if (name[0] == '\0') 873 errx(1, "Empty device name"); 874 cp = strchr(name, '\0'); 875 assert(cp != NULL && cp != name); 876 cp--; 877 while (cp != name && isdigit(cp[-1])) 878 cp--; 879 if (cp == name || !isdigit(*cp)) 880 errx(1, "Invalid device name"); 881 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name)) 882 errx(1, "Device name is too long"); 883 memcpy(patterns[0].pd_name, name, cp - name); 884 patterns[0].pd_unit = strtol(cp, &cp, 10); 885 if (*cp != '\0') 886 errx(1, "Invalid device name"); 887 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT; 888 pc.num_patterns = 1; 889 pc.pat_buf_len = sizeof(patterns); 890 pc.patterns = patterns; 891 892 if (ioctl(fd, PCIOCGETCONF, &pc) == -1) 893 err(1, "ioctl(PCIOCGETCONF)"); 894 if (pc.status != PCI_GETCONF_LAST_DEVICE && 895 pc.status != PCI_GETCONF_MORE_DEVS) 896 errx(1, "error returned from PCIOCGETCONF ioctl"); 897 close(fd); 898 if (pc.num_matches == 0) 899 errx(1, "Device not found"); 900 return (conf[0].pc_sel); 901 } 902 903 static struct pcisel 904 parsesel(const char *str) 905 { 906 const char *ep; 907 char *eppos; 908 struct pcisel sel; 909 unsigned long selarr[4]; 910 int i; 911 912 ep = strchr(str, '@'); 913 if (ep != NULL) 914 ep++; 915 else 916 ep = str; 917 918 if (strncmp(ep, "pci", 3) == 0) { 919 ep += 3; 920 i = 0; 921 while (isdigit(*ep) && i < 4) { 922 selarr[i++] = strtoul(ep, &eppos, 10); 923 ep = eppos; 924 if (*ep == ':') 925 ep++; 926 } 927 if (i > 0 && *ep == '\0') { 928 sel.pc_func = (i > 2) ? selarr[--i] : 0; 929 sel.pc_dev = (i > 0) ? selarr[--i] : 0; 930 sel.pc_bus = (i > 0) ? selarr[--i] : 0; 931 sel.pc_domain = (i > 0) ? selarr[--i] : 0; 932 return (sel); 933 } 934 } 935 errx(1, "cannot parse selector %s", str); 936 } 937 938 static struct pcisel 939 getsel(const char *str) 940 { 941 942 /* 943 * No device names contain colons and selectors always contain 944 * at least one colon. 945 */ 946 if (strchr(str, ':') == NULL) 947 return (getdevice(str)); 948 else 949 return (parsesel(str)); 950 } 951 952 static void 953 readone(int fd, struct pcisel *sel, long reg, int width) 954 { 955 956 printf("%0*x", width*2, read_config(fd, sel, reg, width)); 957 } 958 959 static void 960 readit(const char *name, const char *reg, int width) 961 { 962 long rstart; 963 long rend; 964 long r; 965 char *end; 966 int i; 967 int fd; 968 struct pcisel sel; 969 970 fd = open(_PATH_DEVPCI, O_RDWR, 0); 971 if (fd < 0) 972 err(1, "%s", _PATH_DEVPCI); 973 974 rend = rstart = strtol(reg, &end, 0); 975 if (end && *end == ':') { 976 end++; 977 rend = strtol(end, (char **) 0, 0); 978 } 979 sel = getsel(name); 980 for (i = 1, r = rstart; r <= rend; i++, r += width) { 981 readone(fd, &sel, r, width); 982 if (i && !(i % 8)) 983 putchar(' '); 984 putchar(i % (16/width) ? ' ' : '\n'); 985 } 986 if (i % (16/width) != 1) 987 putchar('\n'); 988 close(fd); 989 } 990 991 static void 992 writeit(const char *name, const char *reg, const char *data, int width) 993 { 994 int fd; 995 struct pci_io pi; 996 997 pi.pi_sel = getsel(name); 998 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */ 999 pi.pi_width = width; 1000 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */ 1001 1002 fd = open(_PATH_DEVPCI, O_RDWR, 0); 1003 if (fd < 0) 1004 err(1, "%s", _PATH_DEVPCI); 1005 1006 if (ioctl(fd, PCIOCWRITE, &pi) < 0) 1007 err(1, "ioctl(PCIOCWRITE)"); 1008 } 1009 1010 static void 1011 chkattached(const char *name) 1012 { 1013 int fd; 1014 struct pci_io pi; 1015 1016 pi.pi_sel = getsel(name); 1017 1018 fd = open(_PATH_DEVPCI, O_RDWR, 0); 1019 if (fd < 0) 1020 err(1, "%s", _PATH_DEVPCI); 1021 1022 if (ioctl(fd, PCIOCATTACHED, &pi) < 0) 1023 err(1, "ioctl(PCIOCATTACHED)"); 1024 1025 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */ 1026 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached"); 1027 } 1028