pciconf.c revision 184936
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 31static const char rcsid[] = 32 "$FreeBSD: head/usr.sbin/pciconf/pciconf.c 184936 2008-11-13 19:49:16Z mav $"; 33#endif /* not lint */ 34 35#include <sys/types.h> 36#include <sys/fcntl.h> 37 38#include <ctype.h> 39#include <err.h> 40#include <stdlib.h> 41#include <stdio.h> 42#include <string.h> 43#include <unistd.h> 44#include <sys/pciio.h> 45#include <sys/queue.h> 46 47#include <dev/pci/pcireg.h> 48 49#include "pathnames.h" 50#include "pciconf.h" 51 52struct pci_device_info 53{ 54 TAILQ_ENTRY(pci_device_info) link; 55 int id; 56 char *desc; 57}; 58 59struct pci_vendor_info 60{ 61 TAILQ_ENTRY(pci_vendor_info) link; 62 TAILQ_HEAD(,pci_device_info) devs; 63 int id; 64 char *desc; 65}; 66 67TAILQ_HEAD(,pci_vendor_info) pci_vendors; 68 69static void list_devs(int verbose, int caps); 70static void list_verbose(struct pci_conf *p); 71static const char *guess_class(struct pci_conf *p); 72static const char *guess_subclass(struct pci_conf *p); 73static int load_vendors(void); 74static void readit(const char *, const char *, int); 75static void writeit(const char *, const char *, const char *, int); 76static void chkattached(const char *, int); 77 78static int exitstatus = 0; 79 80static void 81usage(void) 82{ 83 fprintf(stderr, "%s\n%s\n%s\n%s\n", 84 "usage: pciconf -l [-cv]", 85 " pciconf -a selector", 86 " pciconf -r [-b | -h] selector addr[:addr2]", 87 " pciconf -w [-b | -h] selector addr value"); 88 exit (1); 89} 90 91int 92main(int argc, char **argv) 93{ 94 int c; 95 int listmode, readmode, writemode, attachedmode, caps, verbose; 96 int byte, isshort; 97 98 listmode = readmode = writemode = attachedmode = caps = verbose = byte = isshort = 0; 99 100 while ((c = getopt(argc, argv, "abchlrwv")) != -1) { 101 switch(c) { 102 case 'a': 103 attachedmode = 1; 104 break; 105 106 case 'b': 107 byte = 1; 108 break; 109 110 case 'c': 111 caps = 1; 112 break; 113 114 case 'h': 115 isshort = 1; 116 break; 117 118 case 'l': 119 listmode = 1; 120 break; 121 122 case 'r': 123 readmode = 1; 124 break; 125 126 case 'w': 127 writemode = 1; 128 break; 129 130 case 'v': 131 verbose = 1; 132 break; 133 134 default: 135 usage(); 136 } 137 } 138 139 if ((listmode && optind != argc) 140 || (writemode && optind + 3 != argc) 141 || (readmode && optind + 2 != argc) 142 || (attachedmode && optind + 1 != argc)) 143 usage(); 144 145 if (listmode) { 146 list_devs(verbose, caps); 147 } else if (attachedmode) { 148 chkattached(argv[optind], 149 byte ? 1 : isshort ? 2 : 4); 150 } else if (readmode) { 151 readit(argv[optind], argv[optind + 1], 152 byte ? 1 : isshort ? 2 : 4); 153 } else if (writemode) { 154 writeit(argv[optind], argv[optind + 1], argv[optind + 2], 155 byte ? 1 : isshort ? 2 : 4); 156 } else { 157 usage(); 158 } 159 160 return exitstatus; 161} 162 163static void 164list_devs(int verbose, int caps) 165{ 166 int fd; 167 struct pci_conf_io pc; 168 struct pci_conf conf[255], *p; 169 int none_count = 0; 170 171 if (verbose) 172 load_vendors(); 173 174 fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0); 175 if (fd < 0) 176 err(1, "%s", _PATH_DEVPCI); 177 178 bzero(&pc, sizeof(struct pci_conf_io)); 179 pc.match_buf_len = sizeof(conf); 180 pc.matches = conf; 181 182 do { 183 if (ioctl(fd, PCIOCGETCONF, &pc) == -1) 184 err(1, "ioctl(PCIOCGETCONF)"); 185 186 /* 187 * 255 entries should be more than enough for most people, 188 * but if someone has more devices, and then changes things 189 * around between ioctls, we'll do the cheezy thing and 190 * just bail. The alternative would be to go back to the 191 * beginning of the list, and print things twice, which may 192 * not be desireable. 193 */ 194 if (pc.status == PCI_GETCONF_LIST_CHANGED) { 195 warnx("PCI device list changed, please try again"); 196 exitstatus = 1; 197 close(fd); 198 return; 199 } else if (pc.status == PCI_GETCONF_ERROR) { 200 warnx("error returned from PCIOCGETCONF ioctl"); 201 exitstatus = 1; 202 close(fd); 203 return; 204 } 205 for (p = conf; p < &conf[pc.num_matches]; p++) { 206 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x " 207 "chip=0x%08x rev=0x%02x hdr=0x%02x\n", 208 (p->pd_name && *p->pd_name) ? p->pd_name : 209 "none", 210 (p->pd_name && *p->pd_name) ? (int)p->pd_unit : 211 none_count++, p->pc_sel.pc_domain, 212 p->pc_sel.pc_bus, p->pc_sel.pc_dev, 213 p->pc_sel.pc_func, (p->pc_class << 16) | 214 (p->pc_subclass << 8) | p->pc_progif, 215 (p->pc_subdevice << 16) | p->pc_subvendor, 216 (p->pc_device << 16) | p->pc_vendor, 217 p->pc_revid, p->pc_hdr); 218 if (verbose) 219 list_verbose(p); 220 if (caps) 221 list_caps(fd, p); 222 } 223 } while (pc.status == PCI_GETCONF_MORE_DEVS); 224 225 close(fd); 226} 227 228static void 229list_verbose(struct pci_conf *p) 230{ 231 struct pci_vendor_info *vi; 232 struct pci_device_info *di; 233 const char *dp; 234 235 TAILQ_FOREACH(vi, &pci_vendors, link) { 236 if (vi->id == p->pc_vendor) { 237 printf(" vendor = '%s'\n", vi->desc); 238 break; 239 } 240 } 241 if (vi == NULL) { 242 di = NULL; 243 } else { 244 TAILQ_FOREACH(di, &vi->devs, link) { 245 if (di->id == p->pc_device) { 246 printf(" device = '%s'\n", di->desc); 247 break; 248 } 249 } 250 } 251 if ((dp = guess_class(p)) != NULL) 252 printf(" class = %s\n", dp); 253 if ((dp = guess_subclass(p)) != NULL) 254 printf(" subclass = %s\n", dp); 255} 256 257/* 258 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c. 259 */ 260static struct 261{ 262 int class; 263 int subclass; 264 const char *desc; 265} pci_nomatch_tab[] = { 266 {PCIC_OLD, -1, "old"}, 267 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"}, 268 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"}, 269 {PCIC_STORAGE, -1, "mass storage"}, 270 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"}, 271 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"}, 272 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"}, 273 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"}, 274 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"}, 275 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"}, 276 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"}, 277 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"}, 278 {PCIC_NETWORK, -1, "network"}, 279 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"}, 280 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"}, 281 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"}, 282 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"}, 283 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"}, 284 {PCIC_DISPLAY, -1, "display"}, 285 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"}, 286 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"}, 287 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"}, 288 {PCIC_MULTIMEDIA, -1, "multimedia"}, 289 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"}, 290 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"}, 291 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"}, 292 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"}, 293 {PCIC_MEMORY, -1, "memory"}, 294 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"}, 295 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"}, 296 {PCIC_BRIDGE, -1, "bridge"}, 297 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"}, 298 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"}, 299 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"}, 300 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"}, 301 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"}, 302 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"}, 303 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"}, 304 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"}, 305 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"}, 306 {PCIC_SIMPLECOMM, -1, "simple comms"}, 307 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */ 308 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"}, 309 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"}, 310 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"}, 311 {PCIC_BASEPERIPH, -1, "base peripheral"}, 312 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"}, 313 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"}, 314 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"}, 315 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"}, 316 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"}, 317 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"}, 318 {PCIC_INPUTDEV, -1, "input device"}, 319 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"}, 320 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"}, 321 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"}, 322 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"}, 323 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"}, 324 {PCIC_DOCKING, -1, "docking station"}, 325 {PCIC_PROCESSOR, -1, "processor"}, 326 {PCIC_SERIALBUS, -1, "serial bus"}, 327 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"}, 328 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"}, 329 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"}, 330 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"}, 331 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"}, 332 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"}, 333 {PCIC_WIRELESS, -1, "wireless controller"}, 334 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"}, 335 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"}, 336 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"}, 337 {PCIC_INTELLIIO, -1, "intelligent I/O controller"}, 338 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"}, 339 {PCIC_SATCOM, -1, "satellite communication"}, 340 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"}, 341 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"}, 342 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"}, 343 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"}, 344 {PCIC_CRYPTO, -1, "encrypt/decrypt"}, 345 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"}, 346 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"}, 347 {PCIC_DASP, -1, "dasp"}, 348 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"}, 349 {0, 0, NULL} 350}; 351 352static const char * 353guess_class(struct pci_conf *p) 354{ 355 int i; 356 357 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 358 if (pci_nomatch_tab[i].class == p->pc_class) 359 return(pci_nomatch_tab[i].desc); 360 } 361 return(NULL); 362} 363 364static const char * 365guess_subclass(struct pci_conf *p) 366{ 367 int i; 368 369 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { 370 if ((pci_nomatch_tab[i].class == p->pc_class) && 371 (pci_nomatch_tab[i].subclass == p->pc_subclass)) 372 return(pci_nomatch_tab[i].desc); 373 } 374 return(NULL); 375} 376 377static int 378load_vendors(void) 379{ 380 const char *dbf; 381 FILE *db; 382 struct pci_vendor_info *cv; 383 struct pci_device_info *cd; 384 char buf[1024], str[1024]; 385 char *ch; 386 int id, error; 387 388 /* 389 * Locate the database and initialise. 390 */ 391 TAILQ_INIT(&pci_vendors); 392 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL) 393 dbf = _PATH_PCIVDB; 394 if ((db = fopen(dbf, "r")) == NULL) 395 return(1); 396 cv = NULL; 397 cd = NULL; 398 error = 0; 399 400 /* 401 * Scan input lines from the database 402 */ 403 for (;;) { 404 if (fgets(buf, sizeof(buf), db) == NULL) 405 break; 406 407 if ((ch = strchr(buf, '#')) != NULL) 408 *ch = '\0'; 409 ch = strchr(buf, '\0') - 1; 410 while (ch > buf && isspace(*ch)) 411 *ch-- = '\0'; 412 if (ch <= buf) 413 continue; 414 415 /* Can't handle subvendor / subdevice entries yet */ 416 if (buf[0] == '\t' && buf[1] == '\t') 417 continue; 418 419 /* Check for vendor entry */ 420 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) { 421 if ((id == 0) || (strlen(str) < 1)) 422 continue; 423 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) { 424 warn("allocating vendor entry"); 425 error = 1; 426 break; 427 } 428 if ((cv->desc = strdup(str)) == NULL) { 429 free(cv); 430 warn("allocating vendor description"); 431 error = 1; 432 break; 433 } 434 cv->id = id; 435 TAILQ_INIT(&cv->devs); 436 TAILQ_INSERT_TAIL(&pci_vendors, cv, link); 437 continue; 438 } 439 440 /* Check for device entry */ 441 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) { 442 if ((id == 0) || (strlen(str) < 1)) 443 continue; 444 if (cv == NULL) { 445 warnx("device entry with no vendor!"); 446 continue; 447 } 448 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) { 449 warn("allocating device entry"); 450 error = 1; 451 break; 452 } 453 if ((cd->desc = strdup(str)) == NULL) { 454 free(cd); 455 warn("allocating device description"); 456 error = 1; 457 break; 458 } 459 cd->id = id; 460 TAILQ_INSERT_TAIL(&cv->devs, cd, link); 461 continue; 462 } 463 464 /* It's a comment or junk, ignore it */ 465 } 466 if (ferror(db)) 467 error = 1; 468 fclose(db); 469 470 return(error); 471} 472 473uint32_t 474read_config(int fd, struct pcisel *sel, long reg, int width) 475{ 476 struct pci_io pi; 477 478 pi.pi_sel = *sel; 479 pi.pi_reg = reg; 480 pi.pi_width = width; 481 482 if (ioctl(fd, PCIOCREAD, &pi) < 0) 483 err(1, "ioctl(PCIOCREAD)"); 484 485 return (pi.pi_data); 486} 487 488static struct pcisel 489getsel(const char *str) 490{ 491 char *ep = strchr(str, '@'); 492 char *epbase; 493 struct pcisel sel; 494 unsigned long selarr[4]; 495 int i; 496 497 if (ep == NULL) 498 ep = (char *)str; 499 else 500 ep++; 501 502 epbase = ep; 503 504 if (strncmp(ep, "pci", 3) == 0) { 505 ep += 3; 506 i = 0; 507 do { 508 selarr[i++] = strtoul(ep, &ep, 10); 509 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4); 510 511 if (i > 2) 512 sel.pc_func = selarr[--i]; 513 else 514 sel.pc_func = 0; 515 sel.pc_dev = selarr[--i]; 516 sel.pc_bus = selarr[--i]; 517 if (i > 0) 518 sel.pc_domain = selarr[--i]; 519 else 520 sel.pc_domain = 0; 521 } 522 if (*ep != '\x0' || ep == epbase) 523 errx(1, "cannot parse selector %s", str); 524 return sel; 525} 526 527static void 528readone(int fd, struct pcisel *sel, long reg, int width) 529{ 530 531 printf("%0*x", width*2, read_config(fd, sel, reg, width)); 532} 533 534static void 535readit(const char *name, const char *reg, int width) 536{ 537 long rstart; 538 long rend; 539 long r; 540 char *end; 541 int i; 542 int fd; 543 struct pcisel sel; 544 545 fd = open(_PATH_DEVPCI, O_RDWR, 0); 546 if (fd < 0) 547 err(1, "%s", _PATH_DEVPCI); 548 549 rend = rstart = strtol(reg, &end, 0); 550 if (end && *end == ':') { 551 end++; 552 rend = strtol(end, (char **) 0, 0); 553 } 554 sel = getsel(name); 555 for (i = 1, r = rstart; r <= rend; i++, r += width) { 556 readone(fd, &sel, r, width); 557 if (i && !(i % 8)) 558 putchar(' '); 559 putchar(i % (16/width) ? ' ' : '\n'); 560 } 561 if (i % (16/width) != 1) 562 putchar('\n'); 563 close(fd); 564} 565 566static void 567writeit(const char *name, const char *reg, const char *data, int width) 568{ 569 int fd; 570 struct pci_io pi; 571 572 pi.pi_sel = getsel(name); 573 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */ 574 pi.pi_width = width; 575 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */ 576 577 fd = open(_PATH_DEVPCI, O_RDWR, 0); 578 if (fd < 0) 579 err(1, "%s", _PATH_DEVPCI); 580 581 if (ioctl(fd, PCIOCWRITE, &pi) < 0) 582 err(1, "ioctl(PCIOCWRITE)"); 583} 584 585static void 586chkattached(const char *name, int width) 587{ 588 int fd; 589 struct pci_io pi; 590 591 pi.pi_sel = getsel(name); 592 pi.pi_reg = 0; 593 pi.pi_width = width; 594 pi.pi_data = 0; 595 596 fd = open(_PATH_DEVPCI, O_RDWR, 0); 597 if (fd < 0) 598 err(1, "%s", _PATH_DEVPCI); 599 600 if (ioctl(fd, PCIOCATTACHED, &pi) < 0) 601 err(1, "ioctl(PCIOCATTACHED)"); 602 603 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */ 604 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached"); 605} 606