2 * Copyright 1996 Massachusetts Institute of Technology
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
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
29 * $FreeBSD: src/usr.sbin/pciconf/pciconf.c,v 1.30.2.3.2.1 2009/04/15 03:14:26 kensmith Exp $
32 #include <sys/types.h>
33 #include <sys/fcntl.h>
41 #include <sys/pciio.h>
42 #include <sys/queue.h>
44 #include <bus/pci/pcireg.h>
46 #include "pathnames.h"
49 struct pci_device_info
51 TAILQ_ENTRY(pci_device_info
) link
;
56 struct pci_vendor_info
58 TAILQ_ENTRY(pci_vendor_info
) link
;
59 TAILQ_HEAD(,pci_device_info
) devs
;
64 TAILQ_HEAD(,pci_vendor_info
) pci_vendors
;
66 static void list_bars(int fd
, struct pci_conf
*p
);
67 static void list_devs(int verbose
, int bars
, int caps
);
68 static void list_verbose(struct pci_conf
*p
);
69 static const char *guess_class(struct pci_conf
*p
);
70 static const char *guess_subclass(struct pci_conf
*p
);
71 static int load_vendors(void);
72 static void readit(const char *, const char *, int);
73 static void writeit(const char *, const char *, const char *, int);
74 static void chkattached(const char *);
76 static int exitstatus
= 0;
81 fprintf(stderr
, "%s\n%s\n%s\n%s\n",
82 "usage: pciconf -l [-bcv]",
83 " pciconf -a selector",
84 " pciconf -r [-b | -h] selector addr[:addr2]",
85 " pciconf -w [-b | -h] selector addr value");
90 main(int argc
, char **argv
)
93 int listmode
, readmode
, writemode
, attachedmode
, bars
, caps
, verbose
;
96 listmode
= readmode
= writemode
= attachedmode
= bars
= caps
= verbose
= byte
= isshort
= 0;
98 while ((c
= getopt(argc
, argv
, "abchlrwv")) != -1) {
138 if ((listmode
&& optind
!= argc
)
139 || (writemode
&& optind
+ 3 != argc
)
140 || (readmode
&& optind
+ 2 != argc
)
141 || (attachedmode
&& optind
+ 1 != argc
))
145 list_devs(verbose
, bars
, caps
);
146 } else if (attachedmode
) {
147 chkattached(argv
[optind
]);
148 } else if (readmode
) {
149 readit(argv
[optind
], argv
[optind
+ 1],
150 byte
? 1 : isshort
? 2 : 4);
151 } else if (writemode
) {
152 writeit(argv
[optind
], argv
[optind
+ 1], argv
[optind
+ 2],
153 byte
? 1 : isshort
? 2 : 4);
162 list_devs(int verbose
, int bars
, int caps
)
165 struct pci_conf_io pc
;
166 struct pci_conf conf
[255], *p
;
172 fd
= open(_PATH_DEVPCI
, caps
? O_RDWR
: O_RDONLY
, 0);
174 err(1, "%s", _PATH_DEVPCI
);
176 bzero(&pc
, sizeof(struct pci_conf_io
));
177 pc
.match_buf_len
= sizeof(conf
);
181 if (ioctl(fd
, PCIOCGETCONF
, &pc
) == -1)
182 err(1, "ioctl(PCIOCGETCONF)");
185 * 255 entries should be more than enough for most people,
186 * but if someone has more devices, and then changes things
187 * around between ioctls, we'll do the cheesy thing and
188 * just bail. The alternative would be to go back to the
189 * beginning of the list, and print things twice, which may
192 if (pc
.status
== PCI_GETCONF_LIST_CHANGED
) {
193 warnx("PCI device list changed, please try again");
197 } else if (pc
.status
== PCI_GETCONF_ERROR
) {
198 warnx("error returned from PCIOCGETCONF ioctl");
203 for (p
= conf
; p
< &conf
[pc
.num_matches
]; p
++) {
204 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
205 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
206 (p
->pd_name
&& *p
->pd_name
) ? p
->pd_name
:
208 (p
->pd_name
&& *p
->pd_name
) ? (int)p
->pd_unit
:
209 none_count
++, p
->pc_sel
.pc_domain
,
210 p
->pc_sel
.pc_bus
, p
->pc_sel
.pc_dev
,
211 p
->pc_sel
.pc_func
, (p
->pc_class
<< 16) |
212 (p
->pc_subclass
<< 8) | p
->pc_progif
,
213 (p
->pc_subdevice
<< 16) | p
->pc_subvendor
,
214 (p
->pc_device
<< 16) | p
->pc_vendor
,
215 p
->pc_revid
, p
->pc_hdr
);
223 } while (pc
.status
== PCI_GETCONF_MORE_DEVS
);
229 list_bars(int fd
, struct pci_conf
*p
)
231 struct pci_bar_io bar
;
236 switch (p
->pc_hdr
& PCIM_HDRTYPE
) {
237 case PCIM_HDRTYPE_NORMAL
:
238 max
= PCIR_MAX_BAR_0
;
240 case PCIM_HDRTYPE_BRIDGE
:
241 max
= PCIR_MAX_BAR_1
;
243 case PCIM_HDRTYPE_CARDBUS
:
244 max
= PCIR_MAX_BAR_2
;
250 for (i
= 0; i
<= max
; i
++) {
251 bar
.pbi_sel
= p
->pc_sel
;
252 bar
.pbi_reg
= PCIR_BAR(i
);
253 if (ioctl(fd
, PCIOCGETBAR
, &bar
) < 0)
255 if (PCI_BAR_IO(bar
.pbi_base
)) {
258 base
= bar
.pbi_base
& PCIM_BAR_IO_BASE
;
260 if (bar
.pbi_base
& PCIM_BAR_MEM_PREFETCH
)
261 type
= "Prefetchable Memory";
264 switch (bar
.pbi_base
& PCIM_BAR_MEM_TYPE
) {
265 case PCIM_BAR_MEM_32
:
268 case PCIM_BAR_MEM_1MB
:
271 case PCIM_BAR_MEM_64
:
277 base
= bar
.pbi_base
& ~((uint64_t)0xf);
279 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
280 PCIR_BAR(i
), type
, range
, (uintmax_t)base
);
281 printf("size %2d, %s\n", (int)bar
.pbi_length
,
282 bar
.pbi_enabled
? "enabled" : "disabled");
287 list_verbose(struct pci_conf
*p
)
289 struct pci_vendor_info
*vi
;
290 struct pci_device_info
*di
;
293 TAILQ_FOREACH(vi
, &pci_vendors
, link
) {
294 if (vi
->id
== p
->pc_vendor
) {
295 printf(" vendor = '%s'\n", vi
->desc
);
302 TAILQ_FOREACH(di
, &vi
->devs
, link
) {
303 if (di
->id
== p
->pc_device
) {
304 printf(" device = '%s'\n", di
->desc
);
309 if ((dp
= guess_class(p
)) != NULL
)
310 printf(" class = %s\n", dp
);
311 if ((dp
= guess_subclass(p
)) != NULL
)
312 printf(" subclass = %s\n", dp
);
316 * This is a direct cut-and-paste from the table in sys/bus/pci/pcireg.h.
323 } pci_nomatch_tab
[] = {
324 {PCIC_OLD
, -1, "old"},
325 {PCIC_OLD
, PCIS_OLD_NONVGA
, "non-VGA display device"},
326 {PCIC_OLD
, PCIS_OLD_VGA
, "VGA-compatible display device"},
327 {PCIC_STORAGE
, -1, "mass storage"},
328 {PCIC_STORAGE
, PCIS_STORAGE_SCSI
, "SCSI"},
329 {PCIC_STORAGE
, PCIS_STORAGE_IDE
, "ATA"},
330 {PCIC_STORAGE
, PCIS_STORAGE_FLOPPY
, "floppy disk"},
331 {PCIC_STORAGE
, PCIS_STORAGE_IPI
, "IPI"},
332 {PCIC_STORAGE
, PCIS_STORAGE_RAID
, "RAID"},
333 {PCIC_STORAGE
, PCIS_STORAGE_ATA_ADMA
, "ATA (ADMA)"},
334 {PCIC_STORAGE
, PCIS_STORAGE_SATA
, "SATA"},
335 {PCIC_STORAGE
, PCIS_STORAGE_SAS
, "SAS"},
336 {PCIC_NETWORK
, -1, "network"},
337 {PCIC_NETWORK
, PCIS_NETWORK_ETHERNET
, "ethernet"},
338 {PCIC_NETWORK
, PCIS_NETWORK_TOKENRING
, "token ring"},
339 {PCIC_NETWORK
, PCIS_NETWORK_FDDI
, "fddi"},
340 {PCIC_NETWORK
, PCIS_NETWORK_ATM
, "ATM"},
341 {PCIC_NETWORK
, PCIS_NETWORK_ISDN
, "ISDN"},
342 {PCIC_DISPLAY
, -1, "display"},
343 {PCIC_DISPLAY
, PCIS_DISPLAY_VGA
, "VGA"},
344 {PCIC_DISPLAY
, PCIS_DISPLAY_XGA
, "XGA"},
345 {PCIC_DISPLAY
, PCIS_DISPLAY_3D
, "3D"},
346 {PCIC_MULTIMEDIA
, -1, "multimedia"},
347 {PCIC_MULTIMEDIA
, PCIS_MULTIMEDIA_VIDEO
, "video"},
348 {PCIC_MULTIMEDIA
, PCIS_MULTIMEDIA_AUDIO
, "audio"},
349 {PCIC_MULTIMEDIA
, PCIS_MULTIMEDIA_TELE
, "telephony"},
350 {PCIC_MULTIMEDIA
, PCIS_MULTIMEDIA_HDA
, "HDA"},
351 {PCIC_MEMORY
, -1, "memory"},
352 {PCIC_MEMORY
, PCIS_MEMORY_RAM
, "RAM"},
353 {PCIC_MEMORY
, PCIS_MEMORY_FLASH
, "flash"},
354 {PCIC_BRIDGE
, -1, "bridge"},
355 {PCIC_BRIDGE
, PCIS_BRIDGE_HOST
, "HOST-PCI"},
356 {PCIC_BRIDGE
, PCIS_BRIDGE_ISA
, "PCI-ISA"},
357 {PCIC_BRIDGE
, PCIS_BRIDGE_EISA
, "PCI-EISA"},
358 {PCIC_BRIDGE
, PCIS_BRIDGE_MCA
, "PCI-MCA"},
359 {PCIC_BRIDGE
, PCIS_BRIDGE_PCI
, "PCI-PCI"},
360 {PCIC_BRIDGE
, PCIS_BRIDGE_PCMCIA
, "PCI-PCMCIA"},
361 {PCIC_BRIDGE
, PCIS_BRIDGE_NUBUS
, "PCI-NuBus"},
362 {PCIC_BRIDGE
, PCIS_BRIDGE_CARDBUS
, "PCI-CardBus"},
363 {PCIC_BRIDGE
, PCIS_BRIDGE_RACEWAY
, "PCI-RACEway"},
364 {PCIC_SIMPLECOMM
, -1, "simple comms"},
365 {PCIC_SIMPLECOMM
, PCIS_SIMPLECOMM_UART
, "UART"}, /* could detect 16550 */
366 {PCIC_SIMPLECOMM
, PCIS_SIMPLECOMM_PAR
, "parallel port"},
367 {PCIC_SIMPLECOMM
, PCIS_SIMPLECOMM_MULSER
, "multiport serial"},
368 {PCIC_SIMPLECOMM
, PCIS_SIMPLECOMM_MODEM
, "generic modem"},
369 {PCIC_BASEPERIPH
, -1, "base peripheral"},
370 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_PIC
, "interrupt controller"},
371 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_DMA
, "DMA controller"},
372 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_TIMER
, "timer"},
373 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_RTC
, "realtime clock"},
374 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_PCIHOT
, "PCI hot-plug controller"},
375 {PCIC_BASEPERIPH
, PCIS_BASEPERIPH_SDHC
, "SD host controller"},
376 {PCIC_INPUTDEV
, -1, "input device"},
377 {PCIC_INPUTDEV
, PCIS_INPUTDEV_KEYBOARD
, "keyboard"},
378 {PCIC_INPUTDEV
, PCIS_INPUTDEV_DIGITIZER
,"digitizer"},
379 {PCIC_INPUTDEV
, PCIS_INPUTDEV_MOUSE
, "mouse"},
380 {PCIC_INPUTDEV
, PCIS_INPUTDEV_SCANNER
, "scanner"},
381 {PCIC_INPUTDEV
, PCIS_INPUTDEV_GAMEPORT
, "gameport"},
382 {PCIC_DOCKING
, -1, "docking station"},
383 {PCIC_PROCESSOR
, -1, "processor"},
384 {PCIC_SERIALBUS
, -1, "serial bus"},
385 {PCIC_SERIALBUS
, PCIS_SERIALBUS_FW
, "FireWire"},
386 {PCIC_SERIALBUS
, PCIS_SERIALBUS_ACCESS
, "AccessBus"},
387 {PCIC_SERIALBUS
, PCIS_SERIALBUS_SSA
, "SSA"},
388 {PCIC_SERIALBUS
, PCIS_SERIALBUS_USB
, "USB"},
389 {PCIC_SERIALBUS
, PCIS_SERIALBUS_FC
, "Fibre Channel"},
390 {PCIC_SERIALBUS
, PCIS_SERIALBUS_SMBUS
, "SMBus"},
391 {PCIC_WIRELESS
, -1, "wireless controller"},
392 {PCIC_WIRELESS
, PCIS_WIRELESS_IRDA
, "iRDA"},
393 {PCIC_WIRELESS
, PCIS_WIRELESS_IR
, "IR"},
394 {PCIC_WIRELESS
, PCIS_WIRELESS_RF
, "RF"},
395 {PCIC_INTELLIIO
, -1, "intelligent I/O controller"},
396 {PCIC_INTELLIIO
, PCIS_INTELLIIO_I2O
, "I2O"},
397 {PCIC_SATCOM
, -1, "satellite communication"},
398 {PCIC_SATCOM
, PCIS_SATCOM_TV
, "sat TV"},
399 {PCIC_SATCOM
, PCIS_SATCOM_AUDIO
, "sat audio"},
400 {PCIC_SATCOM
, PCIS_SATCOM_VOICE
, "sat voice"},
401 {PCIC_SATCOM
, PCIS_SATCOM_DATA
, "sat data"},
402 {PCIC_CRYPTO
, -1, "encrypt/decrypt"},
403 {PCIC_CRYPTO
, PCIS_CRYPTO_NETCOMP
, "network/computer crypto"},
404 {PCIC_CRYPTO
, PCIS_CRYPTO_NETCOMP
, "entertainment crypto"},
405 {PCIC_DASP
, -1, "dasp"},
406 {PCIC_DASP
, PCIS_DASP_DPIO
, "DPIO module"},
411 guess_class(struct pci_conf
*p
)
415 for (i
= 0; pci_nomatch_tab
[i
].desc
!= NULL
; i
++) {
416 if (pci_nomatch_tab
[i
].class == p
->pc_class
)
417 return(pci_nomatch_tab
[i
].desc
);
423 guess_subclass(struct pci_conf
*p
)
427 for (i
= 0; pci_nomatch_tab
[i
].desc
!= NULL
; i
++) {
428 if ((pci_nomatch_tab
[i
].class == p
->pc_class
) &&
429 (pci_nomatch_tab
[i
].subclass
== p
->pc_subclass
))
430 return(pci_nomatch_tab
[i
].desc
);
440 struct pci_vendor_info
*cv
;
441 struct pci_device_info
*cd
;
442 char buf
[1024], str
[1024];
447 * Locate the database and initialise.
449 TAILQ_INIT(&pci_vendors
);
450 if ((dbf
= getenv("PCICONF_VENDOR_DATABASE")) == NULL
)
452 if ((db
= fopen(dbf
, "r")) == NULL
)
459 * Scan input lines from the database
462 if (fgets(buf
, sizeof(buf
), db
) == NULL
)
465 if ((ch
= strchr(buf
, '#')) != NULL
)
467 ch
= strchr(buf
, '\0') - 1;
468 while (ch
> buf
&& isspace(*ch
))
473 /* Can't handle subvendor / subdevice entries yet */
474 if (buf
[0] == '\t' && buf
[1] == '\t')
477 /* Check for vendor entry */
478 if (buf
[0] != '\t' && sscanf(buf
, "%04x %[^\n]", &id
, str
) == 2) {
479 if ((id
== 0) || (strlen(str
) < 1))
481 if ((cv
= malloc(sizeof(struct pci_vendor_info
))) == NULL
) {
482 warn("allocating vendor entry");
486 if ((cv
->desc
= strdup(str
)) == NULL
) {
488 warn("allocating vendor description");
493 TAILQ_INIT(&cv
->devs
);
494 TAILQ_INSERT_TAIL(&pci_vendors
, cv
, link
);
498 /* Check for device entry */
499 if (buf
[0] == '\t' && sscanf(buf
+ 1, "%04x %[^\n]", &id
, str
) == 2) {
500 if ((id
== 0) || (strlen(str
) < 1))
503 warnx("device entry with no vendor!");
506 if ((cd
= malloc(sizeof(struct pci_device_info
))) == NULL
) {
507 warn("allocating device entry");
511 if ((cd
->desc
= strdup(str
)) == NULL
) {
513 warn("allocating device description");
518 TAILQ_INSERT_TAIL(&cv
->devs
, cd
, link
);
522 /* It's a comment or junk, ignore it */
532 read_config(int fd
, struct pcisel
*sel
, long reg
, int width
)
540 if (ioctl(fd
, PCIOCREAD
, &pi
) < 0)
541 err(1, "ioctl(PCIOCREAD)");
547 getsel(const char *str
)
549 char *ep
= strchr(str
, '@');
551 struct pcisel sel
= {0,0,0,0};
552 unsigned long selarr
[4];
556 ep
= __DECONST(char *, str
);
562 if (strncmp(ep
, "pci", 3) == 0) {
566 selarr
[i
++] = strtoul(ep
, &ep
, 10);
567 } while ((*ep
== ':' || *ep
== '.') && *++ep
!= '\0' && i
< 4);
570 sel
.pc_func
= selarr
[--i
];
573 sel
.pc_dev
= selarr
[--i
];
574 sel
.pc_bus
= selarr
[--i
];
576 sel
.pc_domain
= selarr
[--i
];
580 if (*ep
!= '\x0' || ep
== epbase
)
581 errx(1, "cannot parse selector %s", str
);
586 readone(int fd
, struct pcisel
*sel
, long reg
, int width
)
589 printf("%0*x", width
*2, read_config(fd
, sel
, reg
, width
));
593 readit(const char *name
, const char *reg
, int width
)
603 fd
= open(_PATH_DEVPCI
, O_RDWR
, 0);
605 err(1, "%s", _PATH_DEVPCI
);
607 rend
= rstart
= strtol(reg
, &end
, 0);
608 if (end
&& *end
== ':') {
610 rend
= strtol(end
, (char **) 0, 0);
613 for (i
= 1, r
= rstart
; r
<= rend
; i
++, r
+= width
) {
614 readone(fd
, &sel
, r
, width
);
617 putchar(i
% (16/width
) ? ' ' : '\n');
619 if (i
% (16/width
) != 1)
625 writeit(const char *name
, const char *reg
, const char *data
, int width
)
630 pi
.pi_sel
= getsel(name
);
631 pi
.pi_reg
= strtoul(reg
, (char **)0, 0); /* XXX error check */
633 pi
.pi_data
= strtoul(data
, (char **)0, 0); /* XXX error check */
635 fd
= open(_PATH_DEVPCI
, O_RDWR
, 0);
637 err(1, "%s", _PATH_DEVPCI
);
639 if (ioctl(fd
, PCIOCWRITE
, &pi
) < 0)
640 err(1, "ioctl(PCIOCWRITE)");
644 chkattached(const char *name
)
649 pi
.pi_sel
= getsel(name
);
651 fd
= open(_PATH_DEVPCI
, O_RDWR
, 0);
653 err(1, "%s", _PATH_DEVPCI
);
655 if (ioctl(fd
, PCIOCATTACHED
, &pi
) < 0)
656 err(1, "ioctl(PCIOCATTACHED)");
658 exitstatus
= pi
.pi_data
? 0 : 2; /* exit(2), if NOT attached */
659 printf("%s: %s%s\n", name
, pi
.pi_data
== 0 ? "not " : "", "attached");