Home | History | Annotate | Line # | Download | only in pci
pci_subr.c revision 1.230
      1 /*	$NetBSD: pci_subr.c,v 1.230 2021/09/11 19:56:51 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Zubin D. Dittia.  All rights reserved.
      5  * Copyright (c) 1995, 1996, 1998, 2000
      6  *	Christopher G. Demetriou.  All rights reserved.
      7  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by Charles M. Hannum.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * PCI autoconfiguration support functions.
     37  *
     38  * Note: This file is also built into a userland library (libpci).
     39  * Pay attention to this when you make modifications.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: pci_subr.c,v 1.230 2021/09/11 19:56:51 mrg Exp $");
     44 
     45 #ifdef _KERNEL_OPT
     46 #include "opt_pci.h"
     47 #endif
     48 
     49 #include <sys/param.h>
     50 
     51 #ifdef _KERNEL
     52 #include <sys/systm.h>
     53 #include <sys/intr.h>
     54 #include <sys/module.h>
     55 #include <sys/kmem.h>
     56 
     57 #define MALLOC(sz)	kmem_alloc(sz, KM_SLEEP)
     58 #define FREE(p, sz)	kmem_free(p, sz)
     59 
     60 #else
     61 #include <pci.h>
     62 #include <stdarg.h>
     63 #include <stdbool.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 
     68 #define MALLOC(sz)	malloc(sz)
     69 #define FREE(p, sz)	free(p)
     70 
     71 #endif
     72 
     73 #include <dev/pci/pcireg.h>
     74 #include <dev/pci/pcidevs.h>
     75 #ifdef _KERNEL
     76 #include <dev/pci/pcivar.h>
     77 #else
     78 #include <dev/pci/pci_verbose.h>
     79 #include <dev/pci/pcidevs_data.h>
     80 #endif
     81 
     82 static int pci_conf_find_cap(const pcireg_t *, unsigned int, int *);
     83 static int pci_conf_find_extcap(const pcireg_t *, unsigned int, int *);
     84 static void pci_conf_print_pcie_power(uint8_t, unsigned int);
     85 #define PCIREG_SHIFTOUT(a, b) ((pcireg_t)__SHIFTOUT((a), (b)))
     86 
     87 #ifdef _KERNEL
     88 /*
     89  * Common routines used to match a compatible device by its PCI ID code.
     90  */
     91 
     92 const struct device_compatible_entry *
     93 pci_compatible_lookup_id(pcireg_t const id,
     94     const struct device_compatible_entry *dce)
     95 {
     96 	return device_compatible_lookup_id(id, PCI_COMPAT_EOL_VALUE, dce);
     97 }
     98 
     99 const struct device_compatible_entry *
    100 pci_compatible_lookup(const struct pci_attach_args * const pa,
    101     const struct device_compatible_entry * const dce)
    102 {
    103 	return pci_compatible_lookup_id(pa->pa_id, dce);
    104 }
    105 
    106 const struct device_compatible_entry *
    107 pci_compatible_lookup_subsys(const struct pci_attach_args * const pa,
    108     const struct device_compatible_entry * const dce)
    109 {
    110 	const pcireg_t subsysid =
    111 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    112 
    113 	return pci_compatible_lookup_id(subsysid, dce);
    114 }
    115 
    116 int
    117 pci_compatible_match(const struct pci_attach_args * const pa,
    118     const struct device_compatible_entry * const dce)
    119 {
    120 	return pci_compatible_lookup(pa, dce) != NULL;
    121 }
    122 
    123 int
    124 pci_compatible_match_subsys(const struct pci_attach_args * const pa,
    125     const struct device_compatible_entry * const dce)
    126 {
    127 	return pci_compatible_lookup_subsys(pa, dce) != NULL;
    128 }
    129 #endif /* _KERNEL */
    130 
    131 /*
    132  * Descriptions of known PCI classes and subclasses.
    133  *
    134  * Subclasses are described in the same way as classes, but have a
    135  * NULL subclass pointer.
    136  */
    137 struct pci_class {
    138 	const char	*name;
    139 	u_int		val;		/* as wide as pci_{,sub}class_t */
    140 	const struct pci_class *subclasses;
    141 };
    142 
    143 /*
    144  * Class 0x00.
    145  * Before rev. 2.0.
    146  */
    147 static const struct pci_class pci_subclass_prehistoric[] = {
    148 	{ "miscellaneous",	PCI_SUBCLASS_PREHISTORIC_MISC,	NULL,	},
    149 	{ "VGA",		PCI_SUBCLASS_PREHISTORIC_VGA,	NULL,	},
    150 	{ NULL,			0,				NULL,	},
    151 };
    152 
    153 /*
    154  * Class 0x01.
    155  * Mass storage controller
    156  */
    157 
    158 /* ATA programming interface */
    159 static const struct pci_class pci_interface_ata[] = {
    160 	{ "with single DMA",	PCI_INTERFACE_ATA_SINGLEDMA,	NULL,	},
    161 	{ "with chained DMA",	PCI_INTERFACE_ATA_CHAINEDDMA,	NULL,	},
    162 	{ NULL,			0,				NULL,	},
    163 };
    164 
    165 /* SATA programming interface */
    166 static const struct pci_class pci_interface_sata[] = {
    167 	{ "vendor specific",	PCI_INTERFACE_SATA_VND,		NULL,	},
    168 	{ "AHCI 1.0",		PCI_INTERFACE_SATA_AHCI10,	NULL,	},
    169 	{ "Serial Storage Bus Interface", PCI_INTERFACE_SATA_SSBI, NULL, },
    170 	{ NULL,			0,				NULL,	},
    171 };
    172 
    173 /* Flash programming interface */
    174 static const struct pci_class pci_interface_nvm[] = {
    175 	{ "vendor specific",	PCI_INTERFACE_NVM_VND,		NULL,	},
    176 	{ "NVMHCI 1.0",		PCI_INTERFACE_NVM_NVMHCI10,	NULL,	},
    177 	{ "NVMe",		PCI_INTERFACE_NVM_NVME,		NULL,	},
    178 	{ NULL,			0,				NULL,	},
    179 };
    180 
    181 /* Subclasses */
    182 static const struct pci_class pci_subclass_mass_storage[] = {
    183 	{ "SCSI",		PCI_SUBCLASS_MASS_STORAGE_SCSI,	NULL,	},
    184 	{ "IDE",		PCI_SUBCLASS_MASS_STORAGE_IDE,	NULL,	},
    185 	{ "floppy",		PCI_SUBCLASS_MASS_STORAGE_FLOPPY, NULL, },
    186 	{ "IPI",		PCI_SUBCLASS_MASS_STORAGE_IPI,	NULL,	},
    187 	{ "RAID",		PCI_SUBCLASS_MASS_STORAGE_RAID,	NULL,	},
    188 	{ "ATA",		PCI_SUBCLASS_MASS_STORAGE_ATA,
    189 	  pci_interface_ata, },
    190 	{ "SATA",		PCI_SUBCLASS_MASS_STORAGE_SATA,
    191 	  pci_interface_sata, },
    192 	{ "SAS",		PCI_SUBCLASS_MASS_STORAGE_SAS,	NULL,	},
    193 	{ "Flash",		PCI_SUBCLASS_MASS_STORAGE_NVM,
    194 	  pci_interface_nvm,	},
    195 	{ "miscellaneous",	PCI_SUBCLASS_MASS_STORAGE_MISC,	NULL,	},
    196 	{ NULL,			0,				NULL,	},
    197 };
    198 
    199 /*
    200  * Class 0x02.
    201  * Network controller.
    202  */
    203 static const struct pci_class pci_subclass_network[] = {
    204 	{ "ethernet",		PCI_SUBCLASS_NETWORK_ETHERNET,	NULL,	},
    205 	{ "token ring",		PCI_SUBCLASS_NETWORK_TOKENRING,	NULL,	},
    206 	{ "FDDI",		PCI_SUBCLASS_NETWORK_FDDI,	NULL,	},
    207 	{ "ATM",		PCI_SUBCLASS_NETWORK_ATM,	NULL,	},
    208 	{ "ISDN",		PCI_SUBCLASS_NETWORK_ISDN,	NULL,	},
    209 	{ "WorldFip",		PCI_SUBCLASS_NETWORK_WORLDFIP,	NULL,	},
    210 	{ "PCMIG Multi Computing", PCI_SUBCLASS_NETWORK_PCIMGMULTICOMP, NULL, },
    211 	{ "miscellaneous",	PCI_SUBCLASS_NETWORK_MISC,	NULL,	},
    212 	{ NULL,			0,				NULL,	},
    213 };
    214 
    215 /*
    216  * Class 0x03.
    217  * Display controller.
    218  */
    219 
    220 /* VGA programming interface */
    221 static const struct pci_class pci_interface_vga[] = {
    222 	{ "",			PCI_INTERFACE_VGA_VGA,		NULL,	},
    223 	{ "8514-compat",	PCI_INTERFACE_VGA_8514,		NULL,	},
    224 	{ NULL,			0,				NULL,	},
    225 };
    226 /* Subclasses */
    227 static const struct pci_class pci_subclass_display[] = {
    228 	{ "VGA",		PCI_SUBCLASS_DISPLAY_VGA,  pci_interface_vga,},
    229 	{ "XGA",		PCI_SUBCLASS_DISPLAY_XGA,	NULL,	},
    230 	{ "3D",			PCI_SUBCLASS_DISPLAY_3D,	NULL,	},
    231 	{ "miscellaneous",	PCI_SUBCLASS_DISPLAY_MISC,	NULL,	},
    232 	{ NULL,			0,				NULL,	},
    233 };
    234 
    235 /*
    236  * Class 0x04.
    237  * Multimedia device.
    238  */
    239 static const struct pci_class pci_subclass_multimedia[] = {
    240 	{ "video",		PCI_SUBCLASS_MULTIMEDIA_VIDEO,	NULL,	},
    241 	{ "audio",		PCI_SUBCLASS_MULTIMEDIA_AUDIO,	NULL,	},
    242 	{ "telephony",		PCI_SUBCLASS_MULTIMEDIA_TELEPHONY, NULL,},
    243 	{ "mixed mode",		PCI_SUBCLASS_MULTIMEDIA_HDAUDIO, NULL, },
    244 	{ "miscellaneous",	PCI_SUBCLASS_MULTIMEDIA_MISC,	NULL,	},
    245 	{ NULL,			0,				NULL,	},
    246 };
    247 
    248 /*
    249  * Class 0x05.
    250  * Memory controller.
    251  */
    252 static const struct pci_class pci_subclass_memory[] = {
    253 	{ "RAM",		PCI_SUBCLASS_MEMORY_RAM,	NULL,	},
    254 	{ "flash",		PCI_SUBCLASS_MEMORY_FLASH,	NULL,	},
    255 	{ "miscellaneous",	PCI_SUBCLASS_MEMORY_MISC,	NULL,	},
    256 	{ NULL,			0,				NULL,	},
    257 };
    258 
    259 /*
    260  * Class 0x06.
    261  * Bridge device.
    262  */
    263 
    264 /* PCI bridge programming interface */
    265 static const struct pci_class pci_interface_pcibridge[] = {
    266 	{ "",			PCI_INTERFACE_BRIDGE_PCI_PCI,	NULL,	},
    267 	{ "subtractive decode",	PCI_INTERFACE_BRIDGE_PCI_SUBDEC, NULL,	},
    268 	{ NULL,			0,				NULL,	},
    269 };
    270 
    271 /* Semi-transparent PCI-to-PCI bridge programming interface */
    272 static const struct pci_class pci_interface_stpci[] = {
    273 	{ "primary side facing host",	PCI_INTERFACE_STPCI_PRIMARY, NULL, },
    274 	{ "secondary side facing host",	PCI_INTERFACE_STPCI_SECONDARY, NULL, },
    275 	{ NULL,			0,				NULL,	},
    276 };
    277 
    278 /* Advanced Switching programming interface */
    279 static const struct pci_class pci_interface_advsw[] = {
    280 	{ "custom interface",	PCI_INTERFACE_ADVSW_CUSTOM,	NULL, },
    281 	{ "ASI-SIG",		PCI_INTERFACE_ADVSW_ASISIG,	NULL, },
    282 	{ NULL,			0,				NULL,	},
    283 };
    284 
    285 /* Subclasses */
    286 static const struct pci_class pci_subclass_bridge[] = {
    287 	{ "host",		PCI_SUBCLASS_BRIDGE_HOST,	NULL,	},
    288 	{ "ISA",		PCI_SUBCLASS_BRIDGE_ISA,	NULL,	},
    289 	{ "EISA",		PCI_SUBCLASS_BRIDGE_EISA,	NULL,	},
    290 	{ "MicroChannel",	PCI_SUBCLASS_BRIDGE_MC,		NULL,	},
    291 	{ "PCI",		PCI_SUBCLASS_BRIDGE_PCI,
    292 	  pci_interface_pcibridge,	},
    293 	{ "PCMCIA",		PCI_SUBCLASS_BRIDGE_PCMCIA,	NULL,	},
    294 	{ "NuBus",		PCI_SUBCLASS_BRIDGE_NUBUS,	NULL,	},
    295 	{ "CardBus",		PCI_SUBCLASS_BRIDGE_CARDBUS,	NULL,	},
    296 	{ "RACEway",		PCI_SUBCLASS_BRIDGE_RACEWAY,	NULL,	},
    297 	{ "Semi-transparent PCI", PCI_SUBCLASS_BRIDGE_STPCI,
    298 	  pci_interface_stpci,	},
    299 	{ "InfiniBand",		PCI_SUBCLASS_BRIDGE_INFINIBAND,	NULL,	},
    300 	{ "advanced switching",	PCI_SUBCLASS_BRIDGE_ADVSW,
    301 	  pci_interface_advsw,	},
    302 	{ "miscellaneous",	PCI_SUBCLASS_BRIDGE_MISC,	NULL,	},
    303 	{ NULL,			0,				NULL,	},
    304 };
    305 
    306 /*
    307  * Class 0x07.
    308  * Simple communications controller.
    309  */
    310 
    311 /* Serial controller programming interface */
    312 static const struct pci_class pci_interface_serial[] = {
    313 	{ "generic XT-compat",	PCI_INTERFACE_SERIAL_XT,	NULL,	},
    314 	{ "16450-compat",	PCI_INTERFACE_SERIAL_16450,	NULL,	},
    315 	{ "16550-compat",	PCI_INTERFACE_SERIAL_16550,	NULL,	},
    316 	{ "16650-compat",	PCI_INTERFACE_SERIAL_16650,	NULL,	},
    317 	{ "16750-compat",	PCI_INTERFACE_SERIAL_16750,	NULL,	},
    318 	{ "16850-compat",	PCI_INTERFACE_SERIAL_16850,	NULL,	},
    319 	{ "16950-compat",	PCI_INTERFACE_SERIAL_16950,	NULL,	},
    320 	{ NULL,			0,				NULL,	},
    321 };
    322 
    323 /* Parallel controller programming interface */
    324 static const struct pci_class pci_interface_parallel[] = {
    325 	{ "",			PCI_INTERFACE_PARALLEL,			NULL,},
    326 	{ "bi-directional",	PCI_INTERFACE_PARALLEL_BIDIRECTIONAL,	NULL,},
    327 	{ "ECP 1.X-compat",	PCI_INTERFACE_PARALLEL_ECP1X,		NULL,},
    328 	{ "IEEE1284 controller", PCI_INTERFACE_PARALLEL_IEEE1284_CNTRL,	NULL,},
    329 	{ "IEEE1284 target",	PCI_INTERFACE_PARALLEL_IEEE1284_TGT,	NULL,},
    330 	{ NULL,			0,					NULL,},
    331 };
    332 
    333 /* Modem programming interface */
    334 static const struct pci_class pci_interface_modem[] = {
    335 	{ "",			PCI_INTERFACE_MODEM,			NULL,},
    336 	{ "Hayes&16450-compat",	PCI_INTERFACE_MODEM_HAYES16450,		NULL,},
    337 	{ "Hayes&16550-compat",	PCI_INTERFACE_MODEM_HAYES16550,		NULL,},
    338 	{ "Hayes&16650-compat",	PCI_INTERFACE_MODEM_HAYES16650,		NULL,},
    339 	{ "Hayes&16750-compat",	PCI_INTERFACE_MODEM_HAYES16750,		NULL,},
    340 	{ NULL,			0,					NULL,},
    341 };
    342 
    343 /* Subclasses */
    344 static const struct pci_class pci_subclass_communications[] = {
    345 	{ "serial",		PCI_SUBCLASS_COMMUNICATIONS_SERIAL,
    346 	  pci_interface_serial, },
    347 	{ "parallel",		PCI_SUBCLASS_COMMUNICATIONS_PARALLEL,
    348 	  pci_interface_parallel, },
    349 	{ "multi-port serial",	PCI_SUBCLASS_COMMUNICATIONS_MPSERIAL,	NULL,},
    350 	{ "modem",		PCI_SUBCLASS_COMMUNICATIONS_MODEM,
    351 	  pci_interface_modem, },
    352 	{ "GPIB",		PCI_SUBCLASS_COMMUNICATIONS_GPIB,	NULL,},
    353 	{ "smartcard",		PCI_SUBCLASS_COMMUNICATIONS_SMARTCARD,	NULL,},
    354 	{ "miscellaneous",	PCI_SUBCLASS_COMMUNICATIONS_MISC,	NULL,},
    355 	{ NULL,			0,					NULL,},
    356 };
    357 
    358 /*
    359  * Class 0x08.
    360  * Base system peripheral.
    361  */
    362 
    363 /* PIC programming interface */
    364 static const struct pci_class pci_interface_pic[] = {
    365 	{ "generic 8259",	PCI_INTERFACE_PIC_8259,		NULL,	},
    366 	{ "ISA PIC",		PCI_INTERFACE_PIC_ISA,		NULL,	},
    367 	{ "EISA PIC",		PCI_INTERFACE_PIC_EISA,		NULL,	},
    368 	{ "IO APIC",		PCI_INTERFACE_PIC_IOAPIC,	NULL,	},
    369 	{ "IO(x) APIC",		PCI_INTERFACE_PIC_IOXAPIC,	NULL,	},
    370 	{ NULL,			0,				NULL,	},
    371 };
    372 
    373 /* DMA programming interface */
    374 static const struct pci_class pci_interface_dma[] = {
    375 	{ "generic 8237",	PCI_INTERFACE_DMA_8237,		NULL,	},
    376 	{ "ISA",		PCI_INTERFACE_DMA_ISA,		NULL,	},
    377 	{ "EISA",		PCI_INTERFACE_DMA_EISA,		NULL,	},
    378 	{ NULL,			0,				NULL,	},
    379 };
    380 
    381 /* Timer programming interface */
    382 static const struct pci_class pci_interface_tmr[] = {
    383 	{ "generic 8254",	PCI_INTERFACE_TIMER_8254,	NULL,	},
    384 	{ "ISA",		PCI_INTERFACE_TIMER_ISA,	NULL,	},
    385 	{ "EISA",		PCI_INTERFACE_TIMER_EISA,	NULL,	},
    386 	{ "HPET",		PCI_INTERFACE_TIMER_HPET,	NULL,	},
    387 	{ NULL,			0,				NULL,	},
    388 };
    389 
    390 /* RTC programming interface */
    391 static const struct pci_class pci_interface_rtc[] = {
    392 	{ "generic",		PCI_INTERFACE_RTC_GENERIC,	NULL,	},
    393 	{ "ISA",		PCI_INTERFACE_RTC_ISA,		NULL,	},
    394 	{ NULL,			0,				NULL,	},
    395 };
    396 
    397 /* Subclasses */
    398 static const struct pci_class pci_subclass_system[] = {
    399 	{ "interrupt",		PCI_SUBCLASS_SYSTEM_PIC,   pci_interface_pic,},
    400 	{ "DMA",		PCI_SUBCLASS_SYSTEM_DMA,   pci_interface_dma,},
    401 	{ "timer",		PCI_SUBCLASS_SYSTEM_TIMER, pci_interface_tmr,},
    402 	{ "RTC",		PCI_SUBCLASS_SYSTEM_RTC,   pci_interface_rtc,},
    403 	{ "PCI Hot-Plug",	PCI_SUBCLASS_SYSTEM_PCIHOTPLUG, NULL,	},
    404 	{ "SD Host Controller",	PCI_SUBCLASS_SYSTEM_SDHC,	NULL,	},
    405 	{ "IOMMU",		PCI_SUBCLASS_SYSTEM_IOMMU,	NULL,	},
    406 	{ "Root Complex Event Collector", PCI_SUBCLASS_SYSTEM_RCEC, NULL, },
    407 	{ "miscellaneous",	PCI_SUBCLASS_SYSTEM_MISC,	NULL,	},
    408 	{ NULL,			0,				NULL,	},
    409 };
    410 
    411 /*
    412  * Class 0x09.
    413  * Input device.
    414  */
    415 
    416 /* Gameport programming interface */
    417 static const struct pci_class pci_interface_game[] = {
    418 	{ "generic",		PCI_INTERFACE_GAMEPORT_GENERIC,	NULL,	},
    419 	{ "legacy",		PCI_INTERFACE_GAMEPORT_LEGACY,	NULL,	},
    420 	{ NULL,			0,				NULL,	},
    421 };
    422 
    423 /* Subclasses */
    424 static const struct pci_class pci_subclass_input[] = {
    425 	{ "keyboard",		PCI_SUBCLASS_INPUT_KEYBOARD,	NULL,	},
    426 	{ "digitizer",		PCI_SUBCLASS_INPUT_DIGITIZER,	NULL,	},
    427 	{ "mouse",		PCI_SUBCLASS_INPUT_MOUSE,	NULL,	},
    428 	{ "scanner",		PCI_SUBCLASS_INPUT_SCANNER,	NULL,	},
    429 	{ "game port",		PCI_SUBCLASS_INPUT_GAMEPORT,
    430 	  pci_interface_game, },
    431 	{ "miscellaneous",	PCI_SUBCLASS_INPUT_MISC,	NULL,	},
    432 	{ NULL,			0,				NULL,	},
    433 };
    434 
    435 /*
    436  * Class 0x0a.
    437  * Docking station.
    438  */
    439 static const struct pci_class pci_subclass_dock[] = {
    440 	{ "generic",		PCI_SUBCLASS_DOCK_GENERIC,	NULL,	},
    441 	{ "miscellaneous",	PCI_SUBCLASS_DOCK_MISC,		NULL,	},
    442 	{ NULL,			0,				NULL,	},
    443 };
    444 
    445 /*
    446  * Class 0x0b.
    447  * Processor.
    448  */
    449 static const struct pci_class pci_subclass_processor[] = {
    450 	{ "386",		PCI_SUBCLASS_PROCESSOR_386,	NULL,	},
    451 	{ "486",		PCI_SUBCLASS_PROCESSOR_486,	NULL,	},
    452 	{ "Pentium",		PCI_SUBCLASS_PROCESSOR_PENTIUM, NULL,	},
    453 	{ "Alpha",		PCI_SUBCLASS_PROCESSOR_ALPHA,	NULL,	},
    454 	{ "PowerPC",		PCI_SUBCLASS_PROCESSOR_POWERPC, NULL,	},
    455 	{ "MIPS",		PCI_SUBCLASS_PROCESSOR_MIPS,	NULL,	},
    456 	{ "Co-processor",	PCI_SUBCLASS_PROCESSOR_COPROC,	NULL,	},
    457 	{ "miscellaneous",	PCI_SUBCLASS_PROCESSOR_MISC,	NULL,	},
    458 	{ NULL,			0,				NULL,	},
    459 };
    460 
    461 /*
    462  * Class 0x0c.
    463  * Serial bus controller.
    464  */
    465 
    466 /* IEEE1394 programming interface */
    467 static const struct pci_class pci_interface_ieee1394[] = {
    468 	{ "Firewire",		PCI_INTERFACE_IEEE1394_FIREWIRE,	NULL,},
    469 	{ "OpenHCI",		PCI_INTERFACE_IEEE1394_OPENHCI,		NULL,},
    470 	{ NULL,			0,					NULL,},
    471 };
    472 
    473 /* USB programming interface */
    474 static const struct pci_class pci_interface_usb[] = {
    475 	{ "UHCI",		PCI_INTERFACE_USB_UHCI,		NULL,	},
    476 	{ "OHCI",		PCI_INTERFACE_USB_OHCI,		NULL,	},
    477 	{ "EHCI",		PCI_INTERFACE_USB_EHCI,		NULL,	},
    478 	{ "xHCI",		PCI_INTERFACE_USB_XHCI,		NULL,	},
    479 	{ "other HC",		PCI_INTERFACE_USB_OTHERHC,	NULL,	},
    480 	{ "device",		PCI_INTERFACE_USB_DEVICE,	NULL,	},
    481 	{ NULL,			0,				NULL,	},
    482 };
    483 
    484 /* IPMI programming interface */
    485 static const struct pci_class pci_interface_ipmi[] = {
    486 	{ "SMIC",		PCI_INTERFACE_IPMI_SMIC,	NULL,	},
    487 	{ "keyboard",		PCI_INTERFACE_IPMI_KBD,		NULL,	},
    488 	{ "block transfer",	PCI_INTERFACE_IPMI_BLOCKXFER,	NULL,	},
    489 	{ NULL,			0,				NULL,	},
    490 };
    491 
    492 /* Subclasses */
    493 static const struct pci_class pci_subclass_serialbus[] = {
    494 	{ "IEEE1394",		PCI_SUBCLASS_SERIALBUS_FIREWIRE,
    495 	  pci_interface_ieee1394, },
    496 	{ "ACCESS.bus",		PCI_SUBCLASS_SERIALBUS_ACCESS,	NULL,	},
    497 	{ "SSA",		PCI_SUBCLASS_SERIALBUS_SSA,	NULL,	},
    498 	{ "USB",		PCI_SUBCLASS_SERIALBUS_USB,
    499 	  pci_interface_usb, },
    500 	/* XXX Fiber Channel/_FIBRECHANNEL */
    501 	{ "Fiber Channel",	PCI_SUBCLASS_SERIALBUS_FIBER,	NULL,	},
    502 	{ "SMBus",		PCI_SUBCLASS_SERIALBUS_SMBUS,	NULL,	},
    503 	{ "InfiniBand",		PCI_SUBCLASS_SERIALBUS_INFINIBAND, NULL,},
    504 	{ "IPMI",		PCI_SUBCLASS_SERIALBUS_IPMI,
    505 	  pci_interface_ipmi, },
    506 	{ "SERCOS",		PCI_SUBCLASS_SERIALBUS_SERCOS,	NULL,	},
    507 	{ "CANbus",		PCI_SUBCLASS_SERIALBUS_CANBUS,	NULL,	},
    508 	{ "miscellaneous",	PCI_SUBCLASS_SERIALBUS_MISC,	NULL,	},
    509 	{ NULL,			0,				NULL,	},
    510 };
    511 
    512 /*
    513  * Class 0x0d.
    514  * Wireless Controller.
    515  */
    516 static const struct pci_class pci_subclass_wireless[] = {
    517 	{ "IrDA",		PCI_SUBCLASS_WIRELESS_IRDA,	NULL,	},
    518 	{ "Consumer IR",/*XXX*/	PCI_SUBCLASS_WIRELESS_CONSUMERIR, NULL,	},
    519 	{ "RF",			PCI_SUBCLASS_WIRELESS_RF,	NULL,	},
    520 	{ "bluetooth",		PCI_SUBCLASS_WIRELESS_BLUETOOTH, NULL,	},
    521 	{ "broadband",		PCI_SUBCLASS_WIRELESS_BROADBAND, NULL,	},
    522 	{ "802.11a (5 GHz)",	PCI_SUBCLASS_WIRELESS_802_11A,	NULL,	},
    523 	{ "802.11b (2.4 GHz)",	PCI_SUBCLASS_WIRELESS_802_11B,	NULL,	},
    524 	{ "miscellaneous",	PCI_SUBCLASS_WIRELESS_MISC,	NULL,	},
    525 	{ NULL,			0,				NULL,	},
    526 };
    527 
    528 /*
    529  * Class 0x0e.
    530  * Intelligent IO controller.
    531  */
    532 
    533 /* Intelligent IO programming interface */
    534 static const struct pci_class pci_interface_i2o[] = {
    535 	{ "FIFO at offset 0x40", PCI_INTERFACE_I2O_FIFOAT40,	NULL,	},
    536 	{ NULL,			0,				NULL,	},
    537 };
    538 
    539 /* Subclasses */
    540 static const struct pci_class pci_subclass_i2o[] = {
    541 	{ "standard",		PCI_SUBCLASS_I2O_STANDARD, pci_interface_i2o,},
    542 	{ "miscellaneous",	PCI_SUBCLASS_I2O_MISC,		NULL,	},
    543 	{ NULL,			0,				NULL,	},
    544 };
    545 
    546 /*
    547  * Class 0x0f.
    548  * Satellite communication controller.
    549  */
    550 static const struct pci_class pci_subclass_satcom[] = {
    551 	{ "TV",			PCI_SUBCLASS_SATCOM_TV,		NULL,	},
    552 	{ "audio",		PCI_SUBCLASS_SATCOM_AUDIO,	NULL,	},
    553 	{ "voice",		PCI_SUBCLASS_SATCOM_VOICE,	NULL,	},
    554 	{ "data",		PCI_SUBCLASS_SATCOM_DATA,	NULL,	},
    555 	{ "miscellaneous",	PCI_SUBCLASS_SATCOM_MISC,	NULL,	},
    556 	{ NULL,			0,				NULL,	},
    557 };
    558 
    559 /*
    560  * Class 0x10.
    561  * Encryption/Decryption controller.
    562  */
    563 static const struct pci_class pci_subclass_crypto[] = {
    564 	{ "network/computing",	PCI_SUBCLASS_CRYPTO_NETCOMP,	NULL,	},
    565 	{ "entertainment",	PCI_SUBCLASS_CRYPTO_ENTERTAINMENT, NULL,},
    566 	{ "miscellaneous",	PCI_SUBCLASS_CRYPTO_MISC,	NULL,	},
    567 	{ NULL,			0,				NULL,	},
    568 };
    569 
    570 /*
    571  * Class 0x11.
    572  * Data aquuisition and signal processing controller.
    573  */
    574 static const struct pci_class pci_subclass_dasp[] = {
    575 	{ "DPIO",		PCI_SUBCLASS_DASP_DPIO,		NULL,	},
    576 	{ "performance counters", PCI_SUBCLASS_DASP_TIMEFREQ,	NULL,	},
    577 	{ "synchronization",	PCI_SUBCLASS_DASP_SYNC,		NULL,	},
    578 	{ "management",		PCI_SUBCLASS_DASP_MGMT,		NULL,	},
    579 	{ "miscellaneous",	PCI_SUBCLASS_DASP_MISC,		NULL,	},
    580 	{ NULL,			0,				NULL,	},
    581 };
    582 
    583 /* List of classes */
    584 static const struct pci_class pci_classes[] = {
    585 	{ "prehistoric",	PCI_CLASS_PREHISTORIC,
    586 	    pci_subclass_prehistoric,				},
    587 	{ "mass storage",	PCI_CLASS_MASS_STORAGE,
    588 	    pci_subclass_mass_storage,				},
    589 	{ "network",		PCI_CLASS_NETWORK,
    590 	    pci_subclass_network,				},
    591 	{ "display",		PCI_CLASS_DISPLAY,
    592 	    pci_subclass_display,				},
    593 	{ "multimedia",		PCI_CLASS_MULTIMEDIA,
    594 	    pci_subclass_multimedia,				},
    595 	{ "memory",		PCI_CLASS_MEMORY,
    596 	    pci_subclass_memory,				},
    597 	{ "bridge",		PCI_CLASS_BRIDGE,
    598 	    pci_subclass_bridge,				},
    599 	{ "communications",	PCI_CLASS_COMMUNICATIONS,
    600 	    pci_subclass_communications,			},
    601 	{ "system",		PCI_CLASS_SYSTEM,
    602 	    pci_subclass_system,				},
    603 	{ "input",		PCI_CLASS_INPUT,
    604 	    pci_subclass_input,					},
    605 	{ "dock",		PCI_CLASS_DOCK,
    606 	    pci_subclass_dock,					},
    607 	{ "processor",		PCI_CLASS_PROCESSOR,
    608 	    pci_subclass_processor,				},
    609 	{ "serial bus",		PCI_CLASS_SERIALBUS,
    610 	    pci_subclass_serialbus,				},
    611 	{ "wireless",		PCI_CLASS_WIRELESS,
    612 	    pci_subclass_wireless,				},
    613 	{ "I2O",		PCI_CLASS_I2O,
    614 	    pci_subclass_i2o,					},
    615 	{ "satellite comm",	PCI_CLASS_SATCOM,
    616 	    pci_subclass_satcom,				},
    617 	{ "crypto",		PCI_CLASS_CRYPTO,
    618 	    pci_subclass_crypto,				},
    619 	{ "DASP",		PCI_CLASS_DASP,
    620 	    pci_subclass_dasp,					},
    621 	{ "processing accelerators", PCI_CLASS_ACCEL,
    622 	    NULL,						},
    623 	{ "non-essential instrumentation", PCI_CLASS_INSTRUMENT,
    624 	    NULL,						},
    625 	{ "undefined",		PCI_CLASS_UNDEFINED,
    626 	    NULL,						},
    627 	{ NULL,			0,
    628 	    NULL,						},
    629 };
    630 
    631 DEV_VERBOSE_DEFINE(pci);
    632 
    633 /*
    634  * Append a formatted string to dest without writing more than len
    635  * characters (including the trailing NUL character).  dest and len
    636  * are updated for use in subsequent calls to snappendf().
    637  *
    638  * Returns 0 on success, a negative value if vnsprintf() fails, or
    639  * a positive value if the dest buffer would have overflowed.
    640  */
    641 
    642 static int __printflike(3, 4)
    643 snappendf(char **dest, size_t *len, const char * restrict fmt, ...)
    644 {
    645 	va_list	ap;
    646 	int count;
    647 
    648 	va_start(ap, fmt);
    649 	count = vsnprintf(*dest, *len, fmt, ap);
    650 	va_end(ap);
    651 
    652 	/* Let vsnprintf() errors bubble up to caller */
    653 	if (count < 0 || *len == 0)
    654 		return count;
    655 
    656 	/* Handle overflow */
    657 	if ((size_t)count >= *len) {
    658 		*dest += *len - 1;
    659 		*len = 1;
    660 		return 1;
    661 	}
    662 
    663 	/* Update dest & len to point at trailing NUL */
    664 	*dest += count;
    665 	*len -= count;
    666 
    667 	return 0;
    668 }
    669 
    670 void
    671 pci_devinfo(pcireg_t id_reg, pcireg_t class_reg, int showclass, char *cp,
    672     size_t l)
    673 {
    674 	pci_class_t class;
    675 	pci_subclass_t subclass;
    676 	pci_interface_t interface;
    677 	pci_revision_t revision;
    678 	char vendor[PCI_VENDORSTR_LEN], product[PCI_PRODUCTSTR_LEN];
    679 	const struct pci_class *classp, *subclassp, *interfacep;
    680 
    681 	class = PCI_CLASS(class_reg);
    682 	subclass = PCI_SUBCLASS(class_reg);
    683 	interface = PCI_INTERFACE(class_reg);
    684 	revision = PCI_REVISION(class_reg);
    685 
    686 	pci_findvendor(vendor, sizeof(vendor), PCI_VENDOR(id_reg));
    687 	pci_findproduct(product, sizeof(product), PCI_VENDOR(id_reg),
    688 	    PCI_PRODUCT(id_reg));
    689 
    690 	classp = pci_classes;
    691 	while (classp->name != NULL) {
    692 		if (class == classp->val)
    693 			break;
    694 		classp++;
    695 	}
    696 
    697 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
    698 	while (subclassp && subclassp->name != NULL) {
    699 		if (subclass == subclassp->val)
    700 			break;
    701 		subclassp++;
    702 	}
    703 
    704 	interfacep = (subclassp && subclassp->name != NULL) ?
    705 	    subclassp->subclasses : NULL;
    706 	while (interfacep && interfacep->name != NULL) {
    707 		if (interface == interfacep->val)
    708 			break;
    709 		interfacep++;
    710 	}
    711 
    712 	(void)snappendf(&cp, &l, "%s %s", vendor, product);
    713 	if (showclass) {
    714 		(void)snappendf(&cp, &l, " (");
    715 		if (classp->name == NULL)
    716 			(void)snappendf(&cp, &l,
    717 			    "class 0x%02x, subclass 0x%02x",
    718 			    class, subclass);
    719 		else {
    720 			if (subclassp == NULL || subclassp->name == NULL)
    721 				(void)snappendf(&cp, &l,
    722 				    "%s, subclass 0x%02x",
    723 				    classp->name, subclass);
    724 			else
    725 				(void)snappendf(&cp, &l, "%s %s",
    726 				    subclassp->name, classp->name);
    727 		}
    728 		if ((interfacep == NULL) || (interfacep->name == NULL)) {
    729 			if (interface != 0)
    730 				(void)snappendf(&cp, &l, ", interface 0x%02x",
    731 				    interface);
    732 		} else if (strncmp(interfacep->name, "", 1) != 0)
    733 			(void)snappendf(&cp, &l, ", %s", interfacep->name);
    734 		if (revision != 0)
    735 			(void)snappendf(&cp, &l, ", revision 0x%02x", revision);
    736 		(void)snappendf(&cp, &l, ")");
    737 	}
    738 }
    739 
    740 #ifdef _KERNEL
    741 void
    742 pci_aprint_devinfo_fancy(const struct pci_attach_args *pa, const char *naive,
    743 			 const char *known, int addrev)
    744 {
    745 	char devinfo[256];
    746 
    747 	if (known) {
    748 		aprint_normal(": %s", known);
    749 		if (addrev)
    750 			aprint_normal(" (rev. 0x%02x)",
    751 				      PCI_REVISION(pa->pa_class));
    752 		aprint_normal("\n");
    753 	} else {
    754 		pci_devinfo(pa->pa_id, pa->pa_class, 0,
    755 			    devinfo, sizeof(devinfo));
    756 		aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    757 			      PCI_REVISION(pa->pa_class));
    758 	}
    759 	if (naive)
    760 		aprint_naive(": %s\n", naive);
    761 	else
    762 		aprint_naive("\n");
    763 }
    764 #endif
    765 
    766 /*
    767  * Print out most of the PCI configuration registers.  Typically used
    768  * in a device attach routine like this:
    769  *
    770  *	#ifdef MYDEV_DEBUG
    771  *		printf("%s: ", device_xname(sc->sc_dev));
    772  *		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    773  *	#endif
    774  */
    775 
    776 #define	i2o(i)	((i) * 4)
    777 #define	o2i(o)	((o) / 4)
    778 #define	onoff2(str, rval, bit, onstr, offstr)				\
    779 	/*CONSTCOND*/							\
    780 	printf("      %s: %s\n", (str), ((rval) & (bit)) ? onstr : offstr);
    781 #define	onoff(str, rval, bit)	onoff2(str, rval, bit, "on", "off")
    782 
    783 static void
    784 pci_conf_print_common(
    785 #ifdef _KERNEL
    786     pci_chipset_tag_t pc, pcitag_t tag,
    787 #endif
    788     const pcireg_t *regs)
    789 {
    790 	pci_class_t class;
    791 	pci_subclass_t subclass;
    792 	pci_interface_t interface;
    793 	pci_revision_t revision;
    794 	char vendor[PCI_VENDORSTR_LEN], product[PCI_PRODUCTSTR_LEN];
    795 	const struct pci_class *classp, *subclassp, *interfacep;
    796 	const char *name;
    797 	pcireg_t rval;
    798 	unsigned int num;
    799 
    800 	rval = regs[o2i(PCI_CLASS_REG)];
    801 	class = PCI_CLASS(rval);
    802 	subclass = PCI_SUBCLASS(rval);
    803 	interface = PCI_INTERFACE(rval);
    804 	revision = PCI_REVISION(rval);
    805 
    806 	rval = regs[o2i(PCI_ID_REG)];
    807 	name = pci_findvendor(vendor, sizeof(vendor), PCI_VENDOR(rval));
    808 	if (name)
    809 		printf("    Vendor Name: %s (0x%04x)\n", name,
    810 		    PCI_VENDOR(rval));
    811 	else
    812 		printf("    Vendor ID: 0x%04x\n", PCI_VENDOR(rval));
    813 	name = pci_findproduct(product, sizeof(product), PCI_VENDOR(rval),
    814 	    PCI_PRODUCT(rval));
    815 	if (name)
    816 		printf("    Device Name: %s (0x%04x)\n", name,
    817 		    PCI_PRODUCT(rval));
    818 	else
    819 		printf("    Device ID: 0x%04x\n", PCI_PRODUCT(rval));
    820 
    821 	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];
    822 
    823 	printf("    Command register: 0x%04x\n", rval & 0xffff);
    824 	onoff("I/O space accesses", rval, PCI_COMMAND_IO_ENABLE);
    825 	onoff("Memory space accesses", rval, PCI_COMMAND_MEM_ENABLE);
    826 	onoff("Bus mastering", rval, PCI_COMMAND_MASTER_ENABLE);
    827 	onoff("Special cycles", rval, PCI_COMMAND_SPECIAL_ENABLE);
    828 	onoff("MWI transactions", rval, PCI_COMMAND_INVALIDATE_ENABLE);
    829 	onoff("Palette snooping", rval, PCI_COMMAND_PALETTE_ENABLE);
    830 	onoff("Parity error checking", rval, PCI_COMMAND_PARITY_ENABLE);
    831 	onoff("Address/data stepping", rval, PCI_COMMAND_STEPPING_ENABLE);
    832 	onoff("System error (SERR)", rval, PCI_COMMAND_SERR_ENABLE);
    833 	onoff("Fast back-to-back transactions", rval,
    834 	    PCI_COMMAND_BACKTOBACK_ENABLE);
    835 	onoff("Interrupt disable", rval, PCI_COMMAND_INTERRUPT_DISABLE);
    836 
    837 	printf("    Status register: 0x%04x\n", (rval >> 16) & 0xffff);
    838 	onoff("Immediate Readiness", rval, PCI_STATUS_IMMD_READNESS);
    839 	onoff2("Interrupt status", rval, PCI_STATUS_INT_STATUS, "active",
    840 	    "inactive");
    841 	onoff("Capability List support", rval, PCI_STATUS_CAPLIST_SUPPORT);
    842 	onoff("66 MHz capable", rval, PCI_STATUS_66MHZ_SUPPORT);
    843 	onoff("User Definable Features (UDF) support", rval,
    844 	    PCI_STATUS_UDF_SUPPORT);
    845 	onoff("Fast back-to-back capable", rval,
    846 	    PCI_STATUS_BACKTOBACK_SUPPORT);
    847 	onoff("Data parity error detected", rval, PCI_STATUS_PARITY_ERROR);
    848 
    849 	printf("      DEVSEL timing: ");
    850 	switch (rval & PCI_STATUS_DEVSEL_MASK) {
    851 	case PCI_STATUS_DEVSEL_FAST:
    852 		printf("fast");
    853 		break;
    854 	case PCI_STATUS_DEVSEL_MEDIUM:
    855 		printf("medium");
    856 		break;
    857 	case PCI_STATUS_DEVSEL_SLOW:
    858 		printf("slow");
    859 		break;
    860 	default:
    861 		printf("unknown/reserved");	/* XXX */
    862 		break;
    863 	}
    864 	printf(" (0x%x)\n", PCIREG_SHIFTOUT(rval, PCI_STATUS_DEVSEL_MASK));
    865 
    866 	onoff("Slave signaled Target Abort", rval,
    867 	    PCI_STATUS_TARGET_TARGET_ABORT);
    868 	onoff("Master received Target Abort", rval,
    869 	    PCI_STATUS_MASTER_TARGET_ABORT);
    870 	onoff("Master received Master Abort", rval, PCI_STATUS_MASTER_ABORT);
    871 	onoff("Asserted System Error (SERR)", rval, PCI_STATUS_SPECIAL_ERROR);
    872 	onoff("Parity error detected", rval, PCI_STATUS_PARITY_DETECT);
    873 
    874 	rval = regs[o2i(PCI_CLASS_REG)];
    875 	for (classp = pci_classes; classp->name != NULL; classp++) {
    876 		if (class == classp->val)
    877 			break;
    878 	}
    879 
    880 	/*
    881 	 * ECN: Change Root Complex Event Collector Class Code
    882 	 * Old RCEC has subclass 0x06. It's the same as IOMMU. Read the type
    883 	 * in PCIe extend capability to know whether it's RCEC or IOMMU.
    884 	 */
    885 	if ((class == PCI_CLASS_SYSTEM)
    886 	    && (subclass == PCI_SUBCLASS_SYSTEM_IOMMU)) {
    887 		int pcie_capoff;
    888 		pcireg_t reg;
    889 
    890 		if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
    891 			reg = regs[o2i(pcie_capoff + PCIE_XCAP)];
    892 			if (PCIE_XCAP_TYPE(reg) == PCIE_XCAP_TYPE_ROOT_EVNTC)
    893 				subclass = PCI_SUBCLASS_SYSTEM_RCEC;
    894 		}
    895 	}
    896 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
    897 	while (subclassp && subclassp->name != NULL) {
    898 		if (subclass == subclassp->val)
    899 			break;
    900 		subclassp++;
    901 	}
    902 
    903 	interfacep = (subclassp && subclassp->name != NULL) ?
    904 	    subclassp->subclasses : NULL;
    905 	while (interfacep && interfacep->name != NULL) {
    906 		if (interface == interfacep->val)
    907 			break;
    908 		interfacep++;
    909 	}
    910 
    911 	if (classp->name != NULL)
    912 		printf("    Class Name: %s (0x%02x)\n", classp->name, class);
    913 	else
    914 		printf("    Class ID: 0x%02x\n", class);
    915 	if (subclassp != NULL && subclassp->name != NULL)
    916 		printf("    Subclass Name: %s (0x%02x)\n",
    917 		    subclassp->name, PCI_SUBCLASS(rval));
    918 	else
    919 		printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
    920 	if ((interfacep != NULL) && (interfacep->name != NULL)
    921 	    && (strncmp(interfacep->name, "", 1) != 0))
    922 		printf("    Interface Name: %s (0x%02x)\n",
    923 		    interfacep->name, interface);
    924 	else
    925 		printf("    Interface: 0x%02x\n", interface);
    926 	printf("    Revision ID: 0x%02x\n", revision);
    927 
    928 	rval = regs[o2i(PCI_BHLC_REG)];
    929 	printf("    BIST: 0x%02x\n", PCI_BIST(rval));
    930 	printf("    Header Type: 0x%02x%s (0x%02x)\n", PCI_HDRTYPE_TYPE(rval),
    931 	    PCI_HDRTYPE_MULTIFN(rval) ? "+multifunction" : "",
    932 	    PCI_HDRTYPE(rval));
    933 	printf("    Latency Timer: 0x%02x\n", PCI_LATTIMER(rval));
    934 	num = PCI_CACHELINE(rval);
    935 	printf("    Cache Line Size: %ubytes (0x%02x)\n", num * 4, num);
    936 }
    937 
    938 static int
    939 pci_conf_print_bar(
    940 #ifdef _KERNEL
    941     pci_chipset_tag_t pc, pcitag_t tag,
    942 #endif
    943     const pcireg_t *regs, int reg, const char *name)
    944 {
    945 	int width;
    946 	pcireg_t rval, rval64h;
    947 	bool ioen, memen;
    948 #ifdef _KERNEL
    949 	pcireg_t mask, mask64h = 0;
    950 #endif
    951 
    952 	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];
    953 	ioen = rval & PCI_COMMAND_IO_ENABLE;
    954 	memen = rval & PCI_COMMAND_MEM_ENABLE;
    955 
    956 	width = 4;
    957 	/*
    958 	 * Section 6.2.5.1, `Address Maps', tells us that:
    959 	 *
    960 	 * 1) The builtin software should have already mapped the
    961 	 * device in a reasonable way.
    962 	 *
    963 	 * 2) A device which wants 2^n bytes of memory will hardwire
    964 	 * the bottom n bits of the address to 0.  As recommended,
    965 	 * we write all 1s and see what we get back.
    966 	 */
    967 
    968 	rval = regs[o2i(reg)];
    969 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
    970 	    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
    971 		rval64h = regs[o2i(reg + 4)];
    972 		width = 8;
    973 	} else
    974 		rval64h = 0;
    975 
    976 #ifdef _KERNEL
    977 	if (rval != 0 && memen) {
    978 		int s;
    979 
    980 		/*
    981 		 * The following sequence seems to make some devices
    982 		 * (e.g. host bus bridges, which don't normally
    983 		 * have their space mapped) very unhappy, to
    984 		 * the point of crashing the system.
    985 		 *
    986 		 * Therefore, if the mapping register is zero to
    987 		 * start out with, don't bother trying.
    988 		 */
    989 		s = splhigh();
    990 		pci_conf_write(pc, tag, reg, 0xffffffff);
    991 		mask = pci_conf_read(pc, tag, reg);
    992 		pci_conf_write(pc, tag, reg, rval);
    993 		if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
    994 		    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
    995 			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    996 			mask64h = pci_conf_read(pc, tag, reg + 4);
    997 			pci_conf_write(pc, tag, reg + 4, rval64h);
    998 		}
    999 		splx(s);
   1000 	} else
   1001 		mask = mask64h = 0;
   1002 #endif /* _KERNEL */
   1003 
   1004 	printf("    Base address register at 0x%02x", reg);
   1005 	if (name)
   1006 		printf(" (%s)", name);
   1007 	printf("\n      ");
   1008 	if (rval == 0) {
   1009 		printf("not implemented\n");
   1010 		return width;
   1011 	}
   1012 	printf("type: ");
   1013 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM) {
   1014 		const char *type, *prefetch;
   1015 
   1016 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
   1017 		case PCI_MAPREG_MEM_TYPE_32BIT:
   1018 			type = "32-bit";
   1019 			break;
   1020 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
   1021 			type = "32-bit-1M";
   1022 			break;
   1023 		case PCI_MAPREG_MEM_TYPE_64BIT:
   1024 			type = "64-bit";
   1025 			break;
   1026 		default:
   1027 			type = "unknown (XXX)";
   1028 			break;
   1029 		}
   1030 		if (PCI_MAPREG_MEM_PREFETCHABLE(rval))
   1031 			prefetch = "";
   1032 		else
   1033 			prefetch = "non";
   1034 		printf("%s %sprefetchable memory\n", type, prefetch);
   1035 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
   1036 		case PCI_MAPREG_MEM_TYPE_64BIT:
   1037 			printf("      base: 0x%016llx",
   1038 			    PCI_MAPREG_MEM64_ADDR(
   1039 				((((long long) rval64h) << 32) | rval)));
   1040 			if (!memen)
   1041 				printf(", disabled");
   1042 			printf("\n");
   1043 #ifdef _KERNEL
   1044 			printf("      size: 0x%016llx\n",
   1045 			    PCI_MAPREG_MEM64_SIZE(
   1046 				    ((((long long) mask64h) << 32) | mask)));
   1047 #endif
   1048 			break;
   1049 		case PCI_MAPREG_MEM_TYPE_32BIT:
   1050 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
   1051 		default:
   1052 			printf("      base: 0x%08x",
   1053 			    PCI_MAPREG_MEM_ADDR(rval));
   1054 			if (!memen)
   1055 				printf(", disabled");
   1056 			printf("\n");
   1057 #ifdef _KERNEL
   1058 			printf("      size: 0x%08x\n",
   1059 			    PCI_MAPREG_MEM_SIZE(mask));
   1060 #endif
   1061 			break;
   1062 		}
   1063 	} else {
   1064 #ifdef _KERNEL
   1065 		if (ioen)
   1066 			printf("%d-bit ", mask & ~0x0000ffff ? 32 : 16);
   1067 #endif
   1068 		printf("I/O\n");
   1069 		printf("      base: 0x%08x", PCI_MAPREG_IO_ADDR(rval));
   1070 		if (!ioen)
   1071 			printf(", disabled");
   1072 		printf("\n");
   1073 #ifdef _KERNEL
   1074 		printf("      size: 0x%08x\n", PCI_MAPREG_IO_SIZE(mask));
   1075 #endif
   1076 	}
   1077 
   1078 	return width;
   1079 }
   1080 
   1081 static void
   1082 pci_conf_print_regs(const pcireg_t *regs, int first, int pastlast)
   1083 {
   1084 	int off, needaddr, neednl;
   1085 
   1086 	needaddr = 1;
   1087 	neednl = 0;
   1088 	for (off = first; off < pastlast; off += 4) {
   1089 		if ((off % 16) == 0 || needaddr) {
   1090 			printf("    0x%02x:", off);
   1091 			needaddr = 0;
   1092 		}
   1093 		printf(" 0x%08x", regs[o2i(off)]);
   1094 		neednl = 1;
   1095 		if ((off % 16) == 12) {
   1096 			printf("\n");
   1097 			neednl = 0;
   1098 		}
   1099 	}
   1100 	if (neednl)
   1101 		printf("\n");
   1102 }
   1103 
   1104 static const char *
   1105 pci_conf_print_agp_calcycle(uint8_t cal)
   1106 {
   1107 
   1108 	switch (cal) {
   1109 	case 0x0:
   1110 		return "4ms";
   1111 	case 0x1:
   1112 		return "16ms";
   1113 	case 0x2:
   1114 		return "64ms";
   1115 	case 0x3:
   1116 		return "256ms";
   1117 	case 0x7:
   1118 		return "Calibration Cycle Not Needed";
   1119 	default:
   1120 		return "(reserved)";
   1121 	}
   1122 }
   1123 
   1124 static void
   1125 pci_conf_print_agp_datarate(pcireg_t reg, bool isagp3)
   1126 {
   1127 	if (isagp3) {
   1128 		/* AGP 3.0 */
   1129 		if (reg & AGP_MODE_V3_RATE_4x)
   1130 			printf("x4");
   1131 		if (reg & AGP_MODE_V3_RATE_8x)
   1132 			printf("x8");
   1133 	} else {
   1134 		/* AGP 2.0 */
   1135 		if (reg & AGP_MODE_V2_RATE_1x)
   1136 			printf("x1");
   1137 		if (reg & AGP_MODE_V2_RATE_2x)
   1138 			printf("x2");
   1139 		if (reg & AGP_MODE_V2_RATE_4x)
   1140 			printf("x4");
   1141 	}
   1142 	printf("\n");
   1143 }
   1144 
   1145 static void
   1146 pci_conf_print_agp_cap(const pcireg_t *regs, int capoff)
   1147 {
   1148 	pcireg_t rval;
   1149 	bool isagp3;
   1150 
   1151 	printf("\n  AGP Capabilities Register\n");
   1152 
   1153 	rval = regs[o2i(capoff)];
   1154 	printf("    Revision: %d.%d\n",
   1155 	    PCI_CAP_AGP_MAJOR(rval), PCI_CAP_AGP_MINOR(rval));
   1156 
   1157 	rval = regs[o2i(capoff + PCI_AGP_STATUS)];
   1158 	printf("    Status register: 0x%04x\n", rval);
   1159 	printf("      RQ: %u\n",
   1160 	    PCIREG_SHIFTOUT(rval, AGP_MODE_RQ) + 1);
   1161 	printf("      ARQSZ: %u\n",
   1162 	    PCIREG_SHIFTOUT(rval, AGP_MODE_ARQSZ));
   1163 	printf("      CAL cycle: %s\n",
   1164 	       pci_conf_print_agp_calcycle(PCIREG_SHIFTOUT(rval, AGP_MODE_CAL)));
   1165 	onoff("SBA", rval, AGP_MODE_SBA);
   1166 	onoff("htrans#", rval, AGP_MODE_HTRANS);
   1167 	onoff("Over 4G", rval, AGP_MODE_4G);
   1168 	onoff("Fast Write", rval, AGP_MODE_FW);
   1169 	onoff("AGP 3.0 Mode", rval, AGP_MODE_MODE_3);
   1170 	isagp3 = rval & AGP_MODE_MODE_3;
   1171 	printf("      Data Rate Support: ");
   1172 	pci_conf_print_agp_datarate(rval, isagp3);
   1173 
   1174 	rval = regs[o2i(capoff + PCI_AGP_COMMAND)];
   1175 	printf("    Command register: 0x%08x\n", rval);
   1176 	printf("      PRQ: %u\n",
   1177 	    PCIREG_SHIFTOUT(rval, AGP_MODE_RQ) + 1);
   1178 	printf("      PARQSZ: %u\n",
   1179 	    PCIREG_SHIFTOUT(rval, AGP_MODE_ARQSZ));
   1180 	printf("      PCAL cycle: %s\n",
   1181 	       pci_conf_print_agp_calcycle(PCIREG_SHIFTOUT(rval, AGP_MODE_CAL)));
   1182 	onoff("SBA", rval, AGP_MODE_SBA);
   1183 	onoff("AGP", rval, AGP_MODE_AGP);
   1184 	onoff("Over 4G", rval, AGP_MODE_4G);
   1185 	onoff("Fast Write", rval, AGP_MODE_FW);
   1186 	if (isagp3) {
   1187 		printf("      Data Rate Enable: ");
   1188 		/*
   1189 		 * The Data Rate Enable bits are used only on 3.0 and the
   1190 		 * Command register has no AGP_MODE_MODE_3 bit, so pass the
   1191 		 * flag to print correctly.
   1192 		 */
   1193 		pci_conf_print_agp_datarate(rval, isagp3);
   1194 	}
   1195 }
   1196 
   1197 static const char *
   1198 pci_conf_print_pcipm_cap_aux(uint16_t caps)
   1199 {
   1200 
   1201 	switch ((caps >> 6) & 7) {
   1202 	case 0:	return "self-powered";
   1203 	case 1: return "55 mA";
   1204 	case 2: return "100 mA";
   1205 	case 3: return "160 mA";
   1206 	case 4: return "220 mA";
   1207 	case 5: return "270 mA";
   1208 	case 6: return "320 mA";
   1209 	case 7:
   1210 	default: return "375 mA";
   1211 	}
   1212 }
   1213 
   1214 static const char *
   1215 pci_conf_print_pcipm_cap_pmrev(uint8_t val)
   1216 {
   1217 	static const char unk[] = "unknown";
   1218 	static const char *pmrev[8] = {
   1219 		unk, "1.0", "1.1", "1.2", unk, unk, unk, unk
   1220 	};
   1221 	if (val > 7)
   1222 		return unk;
   1223 	return pmrev[val];
   1224 }
   1225 
   1226 static void
   1227 pci_conf_print_pcipm_cap(const pcireg_t *regs, int capoff)
   1228 {
   1229 	uint16_t caps, pmcsr;
   1230 
   1231 	caps = regs[o2i(capoff)] >> PCI_PMCR_SHIFT;
   1232 	pmcsr = regs[o2i(capoff + PCI_PMCSR)];
   1233 
   1234 	printf("\n  PCI Power Management Capabilities Register\n");
   1235 
   1236 	printf("    Capabilities register: 0x%04x\n", caps);
   1237 	printf("      Version: %s\n",
   1238 	    pci_conf_print_pcipm_cap_pmrev(caps & PCI_PMCR_VERSION_MASK));
   1239 	onoff("PME# clock", caps, PCI_PMCR_PME_CLOCK);
   1240 	onoff("Device specific initialization", caps, PCI_PMCR_DSI);
   1241 	printf("      3.3V auxiliary current: %s\n",
   1242 	    pci_conf_print_pcipm_cap_aux(caps));
   1243 	onoff("D1 power management state support", caps, PCI_PMCR_D1SUPP);
   1244 	onoff("D2 power management state support", caps, PCI_PMCR_D2SUPP);
   1245 	onoff("PME# support D0", caps, PCI_PMCR_PME_D0);
   1246 	onoff("PME# support D1", caps, PCI_PMCR_PME_D1);
   1247 	onoff("PME# support D2", caps, PCI_PMCR_PME_D2);
   1248 	onoff("PME# support D3 hot", caps, PCI_PMCR_PME_D3HOT);
   1249 	onoff("PME# support D3 cold", caps, PCI_PMCR_PME_D3COLD);
   1250 
   1251 	printf("    Control/status register: 0x%08x\n", pmcsr);
   1252 	printf("      Power state: D%d\n", pmcsr & PCI_PMCSR_STATE_MASK);
   1253 	onoff("PCI Express reserved", (pmcsr >> 2), 1);
   1254 	onoff("No soft reset", pmcsr, PCI_PMCSR_NO_SOFTRST);
   1255 	printf("      PME# assertion: %sabled\n",
   1256 	    (pmcsr & PCI_PMCSR_PME_EN) ? "en" : "dis");
   1257 	printf("      Data Select: %d\n",
   1258 	    PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATASEL_MASK));
   1259 	printf("      Data Scale: %d\n",
   1260 	    PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATASCL_MASK));
   1261 	onoff("PME# status", pmcsr, PCI_PMCSR_PME_STS);
   1262 	printf("    Bridge Support Extensions register: 0x%02x\n",
   1263 	    (pmcsr >> 16) & 0xff);
   1264 	onoff("B2/B3 support", pmcsr, PCI_PMCSR_B2B3_SUPPORT);
   1265 	onoff("Bus Power/Clock Control Enable", pmcsr, PCI_PMCSR_BPCC_EN);
   1266 	printf("    Data register: 0x%02x\n",
   1267 	       PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATA));
   1268 }
   1269 
   1270 /* XXX pci_conf_print_vpd_cap */
   1271 /* XXX pci_conf_print_slotid_cap */
   1272 
   1273 static void
   1274 pci_conf_print_msi_cap(const pcireg_t *regs, int capoff)
   1275 {
   1276 	uint32_t ctl, mmc, mme;
   1277 
   1278 	regs += o2i(capoff);
   1279 	ctl = *regs++;
   1280 	mmc = PCIREG_SHIFTOUT(ctl, PCI_MSI_CTL_MMC_MASK);
   1281 	mme = PCIREG_SHIFTOUT(ctl, PCI_MSI_CTL_MME_MASK);
   1282 
   1283 	printf("\n  PCI Message Signaled Interrupt\n");
   1284 
   1285 	printf("    Message Control register: 0x%04x\n", ctl >> 16);
   1286 	onoff("MSI Enabled", ctl, PCI_MSI_CTL_MSI_ENABLE);
   1287 	printf("      Multiple Message Capable: %s (%d vector%s)\n",
   1288 	    mmc > 0 ? "yes" : "no", 1 << mmc, mmc > 0 ? "s" : "");
   1289 	printf("      Multiple Message Enabled: %s (%d vector%s)\n",
   1290 	    mme > 0 ? "on" : "off", 1 << mme, mme > 0 ? "s" : "");
   1291 	onoff("64 Bit Address Capable", ctl, PCI_MSI_CTL_64BIT_ADDR);
   1292 	onoff("Per-Vector Masking Capable", ctl, PCI_MSI_CTL_PERVEC_MASK);
   1293 	onoff("Extended Message Data Capable", ctl, PCI_MSI_CTL_EXTMDATA_CAP);
   1294 	onoff("Extended Message Data Enable", ctl, PCI_MSI_CTL_EXTMDATA_EN);
   1295 	printf("    Message Address %sregister: 0x%08x\n",
   1296 	    ctl & PCI_MSI_CTL_64BIT_ADDR ? "(lower) " : "", *regs++);
   1297 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
   1298 		printf("    Message Address %sregister: 0x%08x\n",
   1299 		    "(upper) ", *regs++);
   1300 	}
   1301 	printf("    Message Data register: ");
   1302 	if (ctl & PCI_MSI_CTL_EXTMDATA_CAP)
   1303 		printf("0x%08x\n", *regs);
   1304 	else
   1305 		printf("0x%04x\n", *regs & 0xffff);
   1306 	regs++;
   1307 	if (ctl & PCI_MSI_CTL_PERVEC_MASK) {
   1308 		printf("    Vector Mask register: 0x%08x\n", *regs++);
   1309 		printf("    Vector Pending register: 0x%08x\n", *regs++);
   1310 	}
   1311 }
   1312 
   1313 /* XXX pci_conf_print_cpci_hostwap_cap */
   1314 
   1315 /*
   1316  * For both command register and status register.
   1317  * The argument "idx" is index number (0 to 7).
   1318  */
   1319 static int
   1320 pcix_split_trans(unsigned int idx)
   1321 {
   1322 	static int table[8] = {
   1323 		1, 2, 3, 4, 8, 12, 16, 32
   1324 	};
   1325 
   1326 	if (idx >= __arraycount(table))
   1327 		return -1;
   1328 	return table[idx];
   1329 }
   1330 
   1331 static void
   1332 pci_conf_print_pcix_cap_2ndbusmode(int num)
   1333 {
   1334 	const char *maxfreq, *maxperiod;
   1335 
   1336 	printf("      Mode: ");
   1337 	if (num <= 0x07)
   1338 		printf("PCI-X Mode 1\n");
   1339 	else if (num <= 0x0b)
   1340 		printf("PCI-X 266 (Mode 2)\n");
   1341 	else
   1342 		printf("PCI-X 533 (Mode 2)\n");
   1343 
   1344 	printf("      Error protection: %s\n", (num <= 3) ? "parity" : "ECC");
   1345 	switch (num & 0x03) {
   1346 	default:
   1347 	case 0:
   1348 		maxfreq = "N/A";
   1349 		maxperiod = "N/A";
   1350 		break;
   1351 	case 1:
   1352 		maxfreq = "66MHz";
   1353 		maxperiod = "15ns";
   1354 		break;
   1355 	case 2:
   1356 		maxfreq = "100MHz";
   1357 		maxperiod = "10ns";
   1358 		break;
   1359 	case 3:
   1360 		maxfreq = "133MHz";
   1361 		maxperiod = "7.5ns";
   1362 		break;
   1363 	}
   1364 	printf("      Max Clock Freq: %s\n", maxfreq);
   1365 	printf("      Min Clock Period: %s\n", maxperiod);
   1366 }
   1367 
   1368 static void
   1369 pci_conf_print_pcix_cap(const pcireg_t *regs, int capoff)
   1370 {
   1371 	pcireg_t reg;
   1372 	int isbridge;
   1373 	int i;
   1374 
   1375 	isbridge = (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)])
   1376 	    & PCI_HDRTYPE_PPB) != 0 ? 1 : 0;
   1377 	printf("\n  PCI-X %s Capabilities Register\n",
   1378 	    isbridge ? "Bridge" : "Non-bridge");
   1379 
   1380 	reg = regs[o2i(capoff)];
   1381 	if (isbridge != 0) {
   1382 		printf("    Secondary status register: 0x%04x\n",
   1383 		    (reg & 0xffff0000) >> 16);
   1384 		onoff("64bit device", reg, PCIX_STATUS_64BIT);
   1385 		onoff("133MHz capable", reg, PCIX_STATUS_133);
   1386 		onoff("Split completion discarded", reg, PCIX_STATUS_SPLDISC);
   1387 		onoff("Unexpected split completion", reg, PCIX_STATUS_SPLUNEX);
   1388 		onoff("Split completion overrun", reg, PCIX_BRIDGE_ST_SPLOVRN);
   1389 		onoff("Split request delayed", reg, PCIX_BRIDGE_ST_SPLRQDL);
   1390 		pci_conf_print_pcix_cap_2ndbusmode(
   1391 			PCIREG_SHIFTOUT(reg, PCIX_BRIDGE_2NDST_CLKF));
   1392 		printf("      Version: 0x%x\n",
   1393 		    (reg & PCIX_BRIDGE_2NDST_VER_MASK)
   1394 		    >> PCIX_BRIDGE_2NDST_VER_SHIFT);
   1395 		onoff("266MHz capable", reg, PCIX_BRIDGE_ST_266);
   1396 		onoff("533MHz capable", reg, PCIX_BRIDGE_ST_533);
   1397 	} else {
   1398 		printf("    Command register: 0x%04x\n",
   1399 		    (reg & 0xffff0000) >> 16);
   1400 		onoff("Data Parity Error Recovery", reg,
   1401 		    PCIX_CMD_PERR_RECOVER);
   1402 		onoff("Enable Relaxed Ordering", reg, PCIX_CMD_RELAXED_ORDER);
   1403 		printf("      Maximum Burst Read Count: %u\n",
   1404 		    PCIX_CMD_BYTECNT(reg));
   1405 		printf("      Maximum Split Transactions: %d\n",
   1406 		    pcix_split_trans((reg & PCIX_CMD_SPLTRANS_MASK)
   1407 			>> PCIX_CMD_SPLTRANS_SHIFT));
   1408 	}
   1409 	reg = regs[o2i(capoff+PCIX_STATUS)]; /* Or PCIX_BRIDGE_PRI_STATUS */
   1410 	printf("    %sStatus register: 0x%08x\n",
   1411 	    isbridge ? "Bridge " : "", reg);
   1412 	printf("      Function: %d\n", PCIX_STATUS_FN(reg));
   1413 	printf("      Device: %d\n", PCIX_STATUS_DEV(reg));
   1414 	printf("      Bus: %d\n", PCIX_STATUS_BUS(reg));
   1415 	onoff("64bit device", reg, PCIX_STATUS_64BIT);
   1416 	onoff("133MHz capable", reg, PCIX_STATUS_133);
   1417 	onoff("Split completion discarded", reg, PCIX_STATUS_SPLDISC);
   1418 	onoff("Unexpected split completion", reg, PCIX_STATUS_SPLUNEX);
   1419 	if (isbridge != 0) {
   1420 		onoff("Split completion overrun", reg, PCIX_BRIDGE_ST_SPLOVRN);
   1421 		onoff("Split request delayed", reg, PCIX_BRIDGE_ST_SPLRQDL);
   1422 	} else {
   1423 		onoff2("Device Complexity", reg, PCIX_STATUS_DEVCPLX,
   1424 		    "bridge device", "simple device");
   1425 		printf("      Designed max memory read byte count: %d\n",
   1426 		    512 << ((reg & PCIX_STATUS_MAXB_MASK)
   1427 			>> PCIX_STATUS_MAXB_SHIFT));
   1428 		printf("      Designed max outstanding split transaction: %d\n",
   1429 		    pcix_split_trans((reg & PCIX_STATUS_MAXST_MASK)
   1430 			>> PCIX_STATUS_MAXST_SHIFT));
   1431 		printf("      MAX cumulative Read Size: %u\n",
   1432 		    8 << ((reg & 0x1c000000) >> PCIX_STATUS_MAXRS_SHIFT));
   1433 		onoff("Received split completion error", reg,
   1434 		    PCIX_STATUS_SCERR);
   1435 	}
   1436 	onoff("266MHz capable", reg, PCIX_STATUS_266);
   1437 	onoff("533MHz capable", reg, PCIX_STATUS_533);
   1438 
   1439 	if (isbridge == 0)
   1440 		return;
   1441 
   1442 	/* Only for bridge */
   1443 	for (i = 0; i < 2; i++) {
   1444 		reg = regs[o2i(capoff + PCIX_BRIDGE_UP_STCR + (4 * i))];
   1445 		printf("    %s split transaction control register: 0x%08x\n",
   1446 		    (i == 0) ? "Upstream" : "Downstream", reg);
   1447 		printf("      Capacity: %d\n", reg & PCIX_BRIDGE_STCAP);
   1448 		printf("      Commitment Limit: %d\n",
   1449 		    (reg & PCIX_BRIDGE_STCLIM) >> PCIX_BRIDGE_STCLIM_SHIFT);
   1450 	}
   1451 }
   1452 
   1453 /* pci_conf_print_ht_slave_cap */
   1454 /* pci_conf_print_ht_host_cap */
   1455 /* pci_conf_print_ht_switch_cap */
   1456 /* pci_conf_print_ht_intr_cap */
   1457 /* pci_conf_print_ht_revid_cap */
   1458 /* pci_conf_print_ht_unitid_cap */
   1459 /* pci_conf_print_ht_extcnf_cap */
   1460 /* pci_conf_print_ht_addrmap_cap */
   1461 /* pci_conf_print_ht_msimap_cap */
   1462 
   1463 static void
   1464 pci_conf_print_ht_msimap_cap(const pcireg_t *regs, int capoff)
   1465 {
   1466 	pcireg_t val;
   1467 	uint32_t lo, hi;
   1468 
   1469 	/*
   1470 	 * Print the rest of the command register bits. Others are
   1471 	 * printed in pci_conf_print_ht_cap().
   1472 	 */
   1473 	val = regs[o2i(capoff + PCI_HT_CMD)];
   1474 	onoff("Enable", val, PCI_HT_MSI_ENABLED);
   1475 	onoff("Fixed", val, PCI_HT_MSI_FIXED);
   1476 
   1477 	lo = regs[o2i(capoff + PCI_HT_MSI_ADDR_LO)];
   1478 	hi = regs[o2i(capoff + PCI_HT_MSI_ADDR_HI)];
   1479 	printf("    Address Low register: 0x%08x\n", lo);
   1480 	printf("    Address high register: 0x%08x\n", hi);
   1481 	printf("      Address: 0x%016" PRIx64 "\n",
   1482 	    (uint64_t)hi << 32 | (lo & PCI_HT_MSI_ADDR_LO_MASK));
   1483 }
   1484 
   1485 /* pci_conf_print_ht_droute_cap */
   1486 /* pci_conf_print_ht_vcset_cap */
   1487 /* pci_conf_print_ht_retry_cap */
   1488 /* pci_conf_print_ht_x86enc_cap */
   1489 /* pci_conf_print_ht_gen3_cap */
   1490 /* pci_conf_print_ht_fle_cap */
   1491 /* pci_conf_print_ht_pm_cap */
   1492 /* pci_conf_print_ht_hnc_cap */
   1493 
   1494 static const struct ht_types {
   1495 	pcireg_t cap;
   1496 	const char *name;
   1497 	void (*printfunc)(const pcireg_t *, int);
   1498 } ht_captab[] = {
   1499 	{PCI_HT_CAP_SLAVE,	"Slave or Primary Interface", NULL },
   1500 	{PCI_HT_CAP_HOST,	"Host or Secondary Interface", NULL },
   1501 	{PCI_HT_CAP_SWITCH,	"Switch", NULL },
   1502 	{PCI_HT_CAP_INTERRUPT,	"Interrupt Discovery and Configuration", NULL},
   1503 	{PCI_HT_CAP_REVID,	"Revision ID",	NULL },
   1504 	{PCI_HT_CAP_UNITID_CLUMP, "UnitID Clumping",	NULL },
   1505 	{PCI_HT_CAP_EXTCNFSPACE, "Extended Configuration Space Access",	NULL },
   1506 	{PCI_HT_CAP_ADDRMAP,	"Address Mapping",	NULL },
   1507 	{PCI_HT_CAP_MSIMAP,	"MSI Mapping",	pci_conf_print_ht_msimap_cap },
   1508 	{PCI_HT_CAP_DIRECTROUTE, "Direct Route",	NULL },
   1509 	{PCI_HT_CAP_VCSET,	"VCSet",	NULL },
   1510 	{PCI_HT_CAP_RETRYMODE,	"Retry Mode",	NULL },
   1511 	{PCI_HT_CAP_X86ENCODE,	"X86 Encoding",	NULL },
   1512 	{PCI_HT_CAP_GEN3,	"Gen3",	NULL },
   1513 	{PCI_HT_CAP_FLE,	"Function-Level Extension",	NULL },
   1514 	{PCI_HT_CAP_PM,		"Power Management",	NULL },
   1515 	{PCI_HT_CAP_HIGHNODECNT, "High Node Count",	NULL },
   1516 };
   1517 
   1518 static void
   1519 pci_conf_print_ht_cap(const pcireg_t *regs, int capoff)
   1520 {
   1521 	pcireg_t val, foundcap;
   1522 	unsigned int off;
   1523 
   1524 	val = regs[o2i(capoff + PCI_HT_CMD)];
   1525 
   1526 	printf("\n  HyperTransport Capability Register at 0x%02x\n", capoff);
   1527 
   1528 	printf("    Command register: 0x%04x\n", val >> 16);
   1529 	foundcap = PCI_HT_CAP(val);
   1530 	for (off = 0; off < __arraycount(ht_captab); off++) {
   1531 		if (ht_captab[off].cap == foundcap)
   1532 			break;
   1533 	}
   1534 	printf("      Capability Type: 0x%02x ", foundcap);
   1535 	if (off >= __arraycount(ht_captab)) {
   1536 		printf("(unknown)\n");
   1537 		return;
   1538 	}
   1539 	printf("(%s)\n", ht_captab[off].name);
   1540 	if (ht_captab[off].printfunc != NULL)
   1541 		ht_captab[off].printfunc(regs, capoff);
   1542 }
   1543 
   1544 static void
   1545 pci_conf_print_vendspec_cap(const pcireg_t *regs, int capoff)
   1546 {
   1547 	uint16_t caps;
   1548 
   1549 	caps = regs[o2i(capoff)] >> PCI_VENDORSPECIFIC_SHIFT;
   1550 
   1551 	printf("\n  PCI Vendor Specific Capabilities Register\n");
   1552 	printf("    Capabilities length: 0x%02x\n", caps & 0xff);
   1553 }
   1554 
   1555 static void
   1556 pci_conf_print_debugport_cap(const pcireg_t *regs, int capoff)
   1557 {
   1558 	pcireg_t val;
   1559 
   1560 	val = regs[o2i(capoff + PCI_DEBUG_BASER)];
   1561 
   1562 	printf("\n  Debugport Capability Register\n");
   1563 	printf("    Debug base Register: 0x%04x\n",
   1564 	    val >> PCI_DEBUG_BASER_SHIFT);
   1565 	printf("      port offset: 0x%04x\n",
   1566 	    (val & PCI_DEBUG_PORTOFF_MASK) >> PCI_DEBUG_PORTOFF_SHIFT);
   1567 	printf("      BAR number: %u\n",
   1568 	    (val & PCI_DEBUG_BARNUM_MASK) >> PCI_DEBUG_BARNUM_SHIFT);
   1569 }
   1570 
   1571 /* XXX pci_conf_print_cpci_rsrcctl_cap */
   1572 /* XXX pci_conf_print_hotplug_cap */
   1573 
   1574 static void
   1575 pci_conf_print_subsystem_cap(const pcireg_t *regs, int capoff)
   1576 {
   1577 	pcireg_t reg;
   1578 
   1579 	reg = regs[o2i(capoff + PCI_CAP_SUBSYS_ID)];
   1580 
   1581 	printf("\n  Subsystem ID Capability Register\n");
   1582 	printf("    Subsystem ID: 0x%08x\n", reg);
   1583 }
   1584 
   1585 /* XXX pci_conf_print_agp8_cap */
   1586 static void
   1587 pci_conf_print_secure_cap(const pcireg_t *regs, int capoff)
   1588 {
   1589 	pcireg_t reg, reg2, val;
   1590 	bool havemisc1;
   1591 
   1592 	printf("\n  Secure Capability Register\n");
   1593 	reg = regs[o2i(capoff + PCI_SECURE_CAP)];
   1594 	printf("    Capability Register: 0x%04x\n", reg >> 16);
   1595 	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_CAP_TYPE);
   1596 	printf("      Capability block type: ");
   1597 	/* I know IOMMU Only */
   1598 	if (val == PCI_SECURE_CAP_TYPE_IOMMU)
   1599 		printf("IOMMU\n");
   1600 	else {
   1601 		printf("0x%x(unknown)\n", val);
   1602 		return;
   1603 	}
   1604 
   1605 	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_CAP_REV);
   1606 	printf("      Capability revision: 0x%02x ", val);
   1607 	if (val == PCI_SECURE_CAP_REV_IOMMU)
   1608 		printf("(IOMMU)\n");
   1609 	else {
   1610 		printf("(unknown)\n");
   1611 		return;
   1612 	}
   1613 	onoff("IOTLB support", reg, PCI_SECURE_CAP_IOTLBSUP);
   1614 	onoff("HyperTransport tunnel translation support", reg,
   1615 	    PCI_SECURE_CAP_HTTUNNEL);
   1616 	onoff("Not present table entries cached", reg, PCI_SECURE_CAP_NPCACHE);
   1617 	onoff("IOMMU Extended Feature Register support", reg,
   1618 	    PCI_SECURE_CAP_EFRSUP);
   1619 	onoff("IOMMU Miscellaneous Information Register 1", reg,
   1620 	    PCI_SECURE_CAP_EXT);
   1621 	havemisc1 = reg & PCI_SECURE_CAP_EXT;
   1622 
   1623 	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_BAL)];
   1624 	printf("    Base Address Low Register: 0x%08x\n", reg);
   1625 	onoff("Enable", reg, PCI_SECURE_IOMMU_BAL_EN);
   1626 	reg2 = regs[o2i(capoff + PCI_SECURE_IOMMU_BAH)];
   1627 	printf("    Base Address High Register: 0x%08x\n", reg2);
   1628 	printf("      Base Address: 0x%016" PRIx64 "\n",
   1629 	    ((uint64_t)reg2 << 32)
   1630 	    | (reg & (PCI_SECURE_IOMMU_BAL_H | PCI_SECURE_IOMMU_BAL_L)));
   1631 
   1632 	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_RANGE)];
   1633 	printf("    IOMMU Range Register: 0x%08x\n", reg);
   1634 	printf("      HyperTransport UnitID: 0x%02x\n",
   1635 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_UNITID));
   1636 	onoff("Range valid", reg, PCI_SECURE_IOMMU_RANGE_RNGVALID);
   1637 	printf("      Device range bus number: 0x%02x\n",
   1638 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_BUSNUM));
   1639 	printf("      First device: 0x%04x\n",
   1640 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_FIRSTDEV));
   1641 	printf("      Last device: 0x%04x\n",
   1642 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_LASTDEV));
   1643 
   1644 	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_MISC0)];
   1645 	printf("    Miscellaneous Information Register 0: 0x%08x\n", reg);
   1646 	printf("      MSI Message number: 0x%02x\n",
   1647 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_MSINUM));
   1648 	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_GVASIZE);
   1649 	printf("      Guest Virtual Address size: ");
   1650 	if (val == PCI_SECURE_IOMMU_MISC0_GVASIZE_48B)
   1651 		printf("48bits\n");
   1652 	else
   1653 		printf("0x%x(unknown)\n", val);
   1654 	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_PASIZE);
   1655 	printf("      Physical Address size: %dbits\n", val);
   1656 	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_VASIZE);
   1657 	printf("      Virtual Address size: %dbits\n", val);
   1658 	onoff("ATS response address range reserved", reg,
   1659 	    PCI_SECURE_IOMMU_MISC0_ATSRESV);
   1660 	printf("      Peripheral Page Request MSI Message number: 0x%02x\n",
   1661 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_MISNPPR));
   1662 
   1663 	if (!havemisc1)
   1664 		return;
   1665 
   1666 	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_MISC1)];
   1667 	printf("    Miscellaneous Information Register 1: 0x%08x\n", reg);
   1668 	printf("      MSI Message number (GA): 0x%02x\n",
   1669 	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC1_MSINUM));
   1670 }
   1671 
   1672 static void
   1673 pci_print_pcie_L0s_latency(uint32_t val)
   1674 {
   1675 
   1676 	switch (val) {
   1677 	case 0x0:
   1678 		printf("Less than 64ns\n");
   1679 		break;
   1680 	case 0x1:
   1681 	case 0x2:
   1682 	case 0x3:
   1683 		printf("%dns to less than %dns\n", 32 << val, 32 << (val + 1));
   1684 		break;
   1685 	case 0x4:
   1686 		printf("512ns to less than 1us\n");
   1687 		break;
   1688 	case 0x5:
   1689 		printf("1us to less than 2us\n");
   1690 		break;
   1691 	case 0x6:
   1692 		printf("2us - 4us\n");
   1693 		break;
   1694 	case 0x7:
   1695 		printf("More than 4us\n");
   1696 		break;
   1697 	}
   1698 }
   1699 
   1700 static void
   1701 pci_print_pcie_L1_latency(uint32_t val)
   1702 {
   1703 
   1704 	switch (val) {
   1705 	case 0x0:
   1706 		printf("Less than 1us\n");
   1707 		break;
   1708 	case 0x6:
   1709 		printf("32us - 64us\n");
   1710 		break;
   1711 	case 0x7:
   1712 		printf("More than 64us\n");
   1713 		break;
   1714 	default:
   1715 		printf("%dus to less than %dus\n", 1 << (val - 1), 1 << val);
   1716 		break;
   1717 	}
   1718 }
   1719 
   1720 static void
   1721 pci_print_pcie_compl_timeout(uint32_t val)
   1722 {
   1723 
   1724 	switch (val) {
   1725 	case 0x0:
   1726 		printf("50us to 50ms\n");
   1727 		break;
   1728 	case 0x5:
   1729 		printf("16ms to 55ms\n");
   1730 		break;
   1731 	case 0x6:
   1732 		printf("65ms to 210ms\n");
   1733 		break;
   1734 	case 0x9:
   1735 		printf("260ms to 900ms\n");
   1736 		break;
   1737 	case 0xa:
   1738 		printf("1s to 3.5s\n");
   1739 		break;
   1740 	default:
   1741 		printf("unknown %u value\n", val);
   1742 		break;
   1743 	}
   1744 }
   1745 
   1746 static const char * const pcie_linkspeeds[] = {
   1747 	"2.5", "5.0", "8.0", "16.0", "32.0"
   1748 };
   1749 
   1750 /*
   1751  * Print link speed. This function is used for the following register bits:
   1752  *   Maximum Link Speed in LCAP
   1753  *   Current Link Speed in LCSR
   1754  *   Target Link Speed in LCSR2
   1755  * All of above bitfield's values start from 1.
   1756  * For LCSR2, 0 is allowed for a device which supports 2.5GT/s only (and
   1757  * this check also works for devices which compliant to versions of the base
   1758  * specification prior to 3.0.
   1759  */
   1760 static void
   1761 pci_print_pcie_linkspeed(int regnum, pcireg_t val)
   1762 {
   1763 
   1764 	if ((regnum == PCIE_LCSR2) && (val == 0))
   1765 		printf("2.5GT/s\n");
   1766 	else if ((val < 1) || (val > __arraycount(pcie_linkspeeds)))
   1767 		printf("unknown value (%u)\n", val);
   1768 	else
   1769 		printf("%sGT/s\n", pcie_linkspeeds[val - 1]);
   1770 }
   1771 
   1772 /*
   1773  * Print link speed "vector".
   1774  * This function is used for the following register bits:
   1775  *   Supported Link Speeds Vector in LCAP2
   1776  *   Lower SKP OS Generation Supported Speed Vector  in LCAP2
   1777  *   Lower SKP OS Reception Supported Speed Vector in LCAP2
   1778  *   Enable Lower SKP OS Generation Vector in LCTL3
   1779  * All of above bitfield's values start from 0.
   1780  */
   1781 static void
   1782 pci_print_pcie_linkspeedvector(pcireg_t val)
   1783 {
   1784 	unsigned int i;
   1785 
   1786 	/* Start from 0 */
   1787 	for (i = 0; i < 16; i++)
   1788 		if (((val >> i) & 0x01) != 0) {
   1789 			if (i >= __arraycount(pcie_linkspeeds))
   1790 				printf(" unknown vector (0x%x)", 1 << i);
   1791 			else
   1792 				printf(" %sGT/s", pcie_linkspeeds[i]);
   1793 		}
   1794 }
   1795 
   1796 static void
   1797 pci_print_pcie_link_deemphasis(pcireg_t val)
   1798 {
   1799 	switch (val) {
   1800 	case 0:
   1801 		printf("-6dB");
   1802 		break;
   1803 	case 1:
   1804 		printf("-3.5dB");
   1805 		break;
   1806 	default:
   1807 		printf("(reserved value)");
   1808 	}
   1809 }
   1810 
   1811 static void
   1812 pci_conf_print_pcie_cap(const pcireg_t *regs, int capoff)
   1813 {
   1814 	pcireg_t reg; /* for each register */
   1815 	pcireg_t val; /* for each bitfield */
   1816 	bool check_slot = false;
   1817 	unsigned int pcie_devtype;
   1818 	bool check_upstreamport = false;
   1819 	unsigned int pciever;
   1820 	unsigned int i;
   1821 
   1822 	printf("\n  PCI Express Capabilities Register\n");
   1823 	/* Capability Register */
   1824 	reg = regs[o2i(capoff)];
   1825 	printf("    Capability register: 0x%04x\n", reg >> 16);
   1826 	pciever = (unsigned int)(PCIE_XCAP_VER(reg));
   1827 	printf("      Capability version: %u\n", pciever);
   1828 	printf("      Device type: ");
   1829 	pcie_devtype = PCIE_XCAP_TYPE(reg);
   1830 	switch (pcie_devtype) {
   1831 	case PCIE_XCAP_TYPE_PCIE_DEV:	/* 0x0 */
   1832 		printf("PCI Express Endpoint device\n");
   1833 		check_upstreamport = true;
   1834 		break;
   1835 	case PCIE_XCAP_TYPE_PCI_DEV:	/* 0x1 */
   1836 		printf("Legacy PCI Express Endpoint device\n");
   1837 		check_upstreamport = true;
   1838 		break;
   1839 	case PCIE_XCAP_TYPE_ROOT:	/* 0x4 */
   1840 		printf("Root Port of PCI Express Root Complex\n");
   1841 		check_slot = true;
   1842 		break;
   1843 	case PCIE_XCAP_TYPE_UP:		/* 0x5 */
   1844 		printf("Upstream Port of PCI Express Switch\n");
   1845 		check_upstreamport = true;
   1846 		break;
   1847 	case PCIE_XCAP_TYPE_DOWN:	/* 0x6 */
   1848 		printf("Downstream Port of PCI Express Switch\n");
   1849 		check_slot = true;
   1850 		break;
   1851 	case PCIE_XCAP_TYPE_PCIE2PCI:	/* 0x7 */
   1852 		printf("PCI Express to PCI/PCI-X Bridge\n");
   1853 		check_upstreamport = true;
   1854 		break;
   1855 	case PCIE_XCAP_TYPE_PCI2PCIE:	/* 0x8 */
   1856 		printf("PCI/PCI-X to PCI Express Bridge\n");
   1857 		/* Upstream port is not PCIe */
   1858 		check_slot = true;
   1859 		break;
   1860 	case PCIE_XCAP_TYPE_ROOT_INTEP:	/* 0x9 */
   1861 		printf("Root Complex Integrated Endpoint\n");
   1862 		break;
   1863 	case PCIE_XCAP_TYPE_ROOT_EVNTC:	/* 0xa */
   1864 		printf("Root Complex Event Collector\n");
   1865 		break;
   1866 	default:
   1867 		printf("unknown\n");
   1868 		break;
   1869 	}
   1870 	onoff("Slot implemented", reg, PCIE_XCAP_SI);
   1871 	printf("      Interrupt Message Number: 0x%02x\n",
   1872 	    PCIREG_SHIFTOUT(reg, PCIE_XCAP_IRQ));
   1873 
   1874 	/* Device Capability Register */
   1875 	reg = regs[o2i(capoff + PCIE_DCAP)];
   1876 	printf("    Device Capabilities Register: 0x%08x\n", reg);
   1877 	printf("      Max Payload Size Supported: %u bytes max\n",
   1878 	    128 << (unsigned int)(reg & PCIE_DCAP_MAX_PAYLOAD));
   1879 	printf("      Phantom Functions Supported: ");
   1880 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP_PHANTOM_FUNCS)) {
   1881 	case 0x0:
   1882 		printf("not available\n");
   1883 		break;
   1884 	case 0x1:
   1885 		printf("MSB\n");
   1886 		break;
   1887 	case 0x2:
   1888 		printf("two MSB\n");
   1889 		break;
   1890 	case 0x3:
   1891 		printf("All three bits\n");
   1892 		break;
   1893 	}
   1894 	printf("      Extended Tag Field Supported: %dbit\n",
   1895 	    (reg & PCIE_DCAP_EXT_TAG_FIELD) == 0 ? 5 : 8);
   1896 	printf("      Endpoint L0 Acceptable Latency: ");
   1897 	pci_print_pcie_L0s_latency(PCIREG_SHIFTOUT(reg, PCIE_DCAP_L0S_LATENCY));
   1898 	printf("      Endpoint L1 Acceptable Latency: ");
   1899 	pci_print_pcie_L1_latency(PCIREG_SHIFTOUT(reg, PCIE_DCAP_L1_LATENCY));
   1900 	onoff("Attention Button Present", reg, PCIE_DCAP_ATTN_BUTTON);
   1901 	onoff("Attention Indicator Present", reg, PCIE_DCAP_ATTN_IND);
   1902 	onoff("Power Indicator Present", reg, PCIE_DCAP_PWR_IND);
   1903 	onoff("Role-Based Error Report", reg, PCIE_DCAP_ROLE_ERR_RPT);
   1904 	if (check_upstreamport) {
   1905 		printf("      Captured Slot Power Limit: ");
   1906 		pci_conf_print_pcie_power(
   1907 			PCIREG_SHIFTOUT(reg, PCIE_DCAP_SLOT_PWR_LIM_VAL),
   1908 			PCIREG_SHIFTOUT(reg, PCIE_DCAP_SLOT_PWR_LIM_SCALE));
   1909 	}
   1910 	onoff("Function-Level Reset Capability", reg, PCIE_DCAP_FLR);
   1911 
   1912 	/* Device Control Register */
   1913 	reg = regs[o2i(capoff + PCIE_DCSR)];
   1914 	printf("    Device Control Register: 0x%04x\n", reg & 0xffff);
   1915 	onoff("Correctable Error Reporting Enable", reg,
   1916 	    PCIE_DCSR_ENA_COR_ERR);
   1917 	onoff("Non Fatal Error Reporting Enable", reg, PCIE_DCSR_ENA_NFER);
   1918 	onoff("Fatal Error Reporting Enable", reg, PCIE_DCSR_ENA_FER);
   1919 	onoff("Unsupported Request Reporting Enable", reg, PCIE_DCSR_ENA_URR);
   1920 	onoff("Enable Relaxed Ordering", reg, PCIE_DCSR_ENA_RELAX_ORD);
   1921 	printf("      Max Payload Size: %d byte\n",
   1922 	    128 << PCIREG_SHIFTOUT(reg, PCIE_DCSR_MAX_PAYLOAD));
   1923 	onoff("Extended Tag Field Enable", reg, PCIE_DCSR_EXT_TAG_FIELD);
   1924 	onoff("Phantom Functions Enable", reg, PCIE_DCSR_PHANTOM_FUNCS);
   1925 	onoff("Aux Power PM Enable", reg, PCIE_DCSR_AUX_POWER_PM);
   1926 	onoff("Enable No Snoop", reg, PCIE_DCSR_ENA_NO_SNOOP);
   1927 	printf("      Max Read Request Size: %d byte\n",
   1928 	    128 << PCIREG_SHIFTOUT(reg, PCIE_DCSR_MAX_READ_REQ));
   1929 	if (pcie_devtype == PCIE_XCAP_TYPE_PCIE2PCI)
   1930 		onoff("Bridge Config Retry Enable", reg,
   1931 		    PCIE_DCSR_BRDG_CFG_RETRY);
   1932 
   1933 	/* Device Status Register */
   1934 	reg = regs[o2i(capoff + PCIE_DCSR)];
   1935 	printf("    Device Status Register: 0x%04x\n", reg >> 16);
   1936 	onoff("Correctable Error Detected", reg, PCIE_DCSR_CED);
   1937 	onoff("Non Fatal Error Detected", reg, PCIE_DCSR_NFED);
   1938 	onoff("Fatal Error Detected", reg, PCIE_DCSR_FED);
   1939 	onoff("Unsupported Request Detected", reg, PCIE_DCSR_URD);
   1940 	onoff("Aux Power Detected", reg, PCIE_DCSR_AUX_PWR);
   1941 	onoff("Transaction Pending", reg, PCIE_DCSR_TRANSACTION_PND);
   1942 	onoff("Emergency Power Reduction Detected", reg, PCIE_DCSR_EMGPWRREDD);
   1943 
   1944 	if (PCIE_HAS_LINKREGS(pcie_devtype)) {
   1945 		/* Link Capability Register */
   1946 		reg = regs[o2i(capoff + PCIE_LCAP)];
   1947 		printf("    Link Capabilities Register: 0x%08x\n", reg);
   1948 		printf("      Maximum Link Speed: ");
   1949 		pci_print_pcie_linkspeed(PCIE_LCAP, reg & PCIE_LCAP_MAX_SPEED);
   1950 		printf("      Maximum Link Width: x%u lanes\n",
   1951 		    PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH));
   1952 		printf("      Active State PM Support: ");
   1953 		switch (PCIREG_SHIFTOUT(reg, PCIE_LCAP_ASPM)) {
   1954 		case 0x0:
   1955 			printf("No ASPM support\n");
   1956 			break;
   1957 		case 0x1:
   1958 			printf("L0s supported\n");
   1959 			break;
   1960 		case 0x2:
   1961 			printf("L1 supported\n");
   1962 			break;
   1963 		case 0x3:
   1964 			printf("L0s and L1 supported\n");
   1965 			break;
   1966 		}
   1967 		printf("      L0 Exit Latency: ");
   1968 		pci_print_pcie_L0s_latency(PCIREG_SHIFTOUT(reg,PCIE_LCAP_L0S_EXIT));
   1969 		printf("      L1 Exit Latency: ");
   1970 		pci_print_pcie_L1_latency(PCIREG_SHIFTOUT(reg, PCIE_LCAP_L1_EXIT));
   1971 		printf("      Port Number: %u\n",
   1972 		    PCIREG_SHIFTOUT(reg, PCIE_LCAP_PORT));
   1973 		onoff("Clock Power Management", reg, PCIE_LCAP_CLOCK_PM);
   1974 		onoff("Surprise Down Error Report", reg,
   1975 		    PCIE_LCAP_SURPRISE_DOWN);
   1976 		onoff("Data Link Layer Link Active", reg, PCIE_LCAP_DL_ACTIVE);
   1977 		onoff("Link BW Notification Capable", reg,
   1978 			PCIE_LCAP_LINK_BW_NOTIFY);
   1979 		onoff("ASPM Optionally Compliance", reg,
   1980 		    PCIE_LCAP_ASPM_COMPLIANCE);
   1981 
   1982 		/* Link Control Register */
   1983 		reg = regs[o2i(capoff + PCIE_LCSR)];
   1984 		printf("    Link Control Register: 0x%04x\n", reg & 0xffff);
   1985 		printf("      Active State PM Control: ");
   1986 		switch (reg & (PCIE_LCSR_ASPM_L1 | PCIE_LCSR_ASPM_L0S)) {
   1987 		case 0:
   1988 			printf("disabled\n");
   1989 			break;
   1990 		case 1:
   1991 			printf("L0s Entry Enabled\n");
   1992 			break;
   1993 		case 2:
   1994 			printf("L1 Entry Enabled\n");
   1995 			break;
   1996 		case 3:
   1997 			printf("L0s and L1 Entry Enabled\n");
   1998 			break;
   1999 		}
   2000 		onoff2("Read Completion Boundary Control", reg, PCIE_LCSR_RCB,
   2001 		    "128bytes", "64bytes");
   2002 		onoff("Link Disable", reg, PCIE_LCSR_LINK_DIS);
   2003 		onoff("Retrain Link", reg, PCIE_LCSR_RETRAIN);
   2004 		onoff("Common Clock Configuration", reg, PCIE_LCSR_COMCLKCFG);
   2005 		onoff("Extended Synch", reg, PCIE_LCSR_EXTNDSYNC);
   2006 		onoff("Enable Clock Power Management", reg, PCIE_LCSR_ENCLKPM);
   2007 		onoff("Hardware Autonomous Width Disable", reg,PCIE_LCSR_HAWD);
   2008 		onoff("Link Bandwidth Management Interrupt Enable", reg,
   2009 		    PCIE_LCSR_LBMIE);
   2010 		onoff("Link Autonomous Bandwidth Interrupt Enable", reg,
   2011 		    PCIE_LCSR_LABIE);
   2012 		printf("      DRS Signaling Control: ");
   2013 		switch (PCIREG_SHIFTOUT(reg, PCIE_LCSR_DRSSGNL)) {
   2014 		case 0:
   2015 			printf("not reported\n");
   2016 			break;
   2017 		case 1:
   2018 			printf("Interrupt Enabled\n");
   2019 			break;
   2020 		case 2:
   2021 			printf("DRS to FRS Signaling Enabled\n");
   2022 			break;
   2023 		default:
   2024 			printf("reserved\n");
   2025 			break;
   2026 		}
   2027 
   2028 		/* Link Status Register */
   2029 		reg = regs[o2i(capoff + PCIE_LCSR)];
   2030 		printf("    Link Status Register: 0x%04x\n", reg >> 16);
   2031 		printf("      Negotiated Link Speed: ");
   2032 		pci_print_pcie_linkspeed(PCIE_LCSR,
   2033 		    PCIREG_SHIFTOUT(reg, PCIE_LCSR_LINKSPEED));
   2034 		printf("      Negotiated Link Width: x%u lanes\n",
   2035 		    PCIREG_SHIFTOUT(reg, PCIE_LCSR_NLW));
   2036 		onoff("Training Error", reg, PCIE_LCSR_LINKTRAIN_ERR);
   2037 		onoff("Link Training", reg, PCIE_LCSR_LINKTRAIN);
   2038 		onoff("Slot Clock Configuration", reg, PCIE_LCSR_SLOTCLKCFG);
   2039 		onoff("Data Link Layer Link Active", reg, PCIE_LCSR_DLACTIVE);
   2040 		onoff("Link Bandwidth Management Status", reg,
   2041 		    PCIE_LCSR_LINK_BW_MGMT);
   2042 		onoff("Link Autonomous Bandwidth Status", reg,
   2043 		    PCIE_LCSR_LINK_AUTO_BW);
   2044 	}
   2045 
   2046 	if (check_slot == true) {
   2047 		pcireg_t slcap;
   2048 
   2049 		/* Slot Capability Register */
   2050 		slcap = reg = regs[o2i(capoff + PCIE_SLCAP)];
   2051 		printf("    Slot Capability Register: 0x%08x\n", reg);
   2052 		onoff("Attention Button Present", reg, PCIE_SLCAP_ABP);
   2053 		onoff("Power Controller Present", reg, PCIE_SLCAP_PCP);
   2054 		onoff("MRL Sensor Present", reg, PCIE_SLCAP_MSP);
   2055 		onoff("Attention Indicator Present", reg, PCIE_SLCAP_AIP);
   2056 		onoff("Power Indicator Present", reg, PCIE_SLCAP_PIP);
   2057 		onoff("Hot-Plug Surprise", reg, PCIE_SLCAP_HPS);
   2058 		onoff("Hot-Plug Capable", reg, PCIE_SLCAP_HPC);
   2059 		printf("      Slot Power Limit Value: ");
   2060 		pci_conf_print_pcie_power(PCIREG_SHIFTOUT(reg, PCIE_SLCAP_SPLV),
   2061 		    PCIREG_SHIFTOUT(reg, PCIE_SLCAP_SPLS));
   2062 		onoff("Electromechanical Interlock Present", reg,
   2063 		    PCIE_SLCAP_EIP);
   2064 		onoff("No Command Completed Support", reg, PCIE_SLCAP_NCCS);
   2065 		printf("      Physical Slot Number: %d\n",
   2066 		    (unsigned int)(reg & PCIE_SLCAP_PSN) >> 19);
   2067 
   2068 		/* Slot Control Register */
   2069 		reg = regs[o2i(capoff + PCIE_SLCSR)];
   2070 		printf("    Slot Control Register: 0x%04x\n", reg & 0xffff);
   2071 		onoff("Attention Button Pressed Enabled", reg, PCIE_SLCSR_ABE);
   2072 		onoff("Power Fault Detected Enabled", reg, PCIE_SLCSR_PFE);
   2073 		onoff("MRL Sensor Changed Enabled", reg, PCIE_SLCSR_MSE);
   2074 		onoff("Presence Detect Changed Enabled", reg, PCIE_SLCSR_PDE);
   2075 		onoff("Command Completed Interrupt Enabled", reg,
   2076 		    PCIE_SLCSR_CCE);
   2077 		onoff("Hot-Plug Interrupt Enabled", reg, PCIE_SLCSR_HPE);
   2078 		/*
   2079 		 * For Attention Indicator Control and Power Indicator Control,
   2080 		 * it's allowed to be a read only value 0 if corresponding
   2081 		 * capability register bit is 0.
   2082 		 */
   2083 		if (slcap & PCIE_SLCAP_AIP) {
   2084 			printf("      Attention Indicator Control: ");
   2085 			switch ((reg & PCIE_SLCSR_AIC) >> 6) {
   2086 			case 0x0:
   2087 				printf("reserved\n");
   2088 				break;
   2089 			case PCIE_SLCSR_IND_ON:
   2090 				printf("on\n");
   2091 				break;
   2092 			case PCIE_SLCSR_IND_BLINK:
   2093 				printf("blink\n");
   2094 				break;
   2095 			case PCIE_SLCSR_IND_OFF:
   2096 				printf("off\n");
   2097 				break;
   2098 			}
   2099 		}
   2100 		if (slcap & PCIE_SLCAP_PIP) {
   2101 			printf("      Power Indicator Control: ");
   2102 			switch ((reg & PCIE_SLCSR_PIC) >> 8) {
   2103 			case 0x0:
   2104 				printf("reserved\n");
   2105 				break;
   2106 			case PCIE_SLCSR_IND_ON:
   2107 				printf("on\n");
   2108 				break;
   2109 			case PCIE_SLCSR_IND_BLINK:
   2110 				printf("blink\n");
   2111 				break;
   2112 			case PCIE_SLCSR_IND_OFF:
   2113 				printf("off\n");
   2114 				break;
   2115 			}
   2116 		}
   2117 		printf("      Power Controller Control: Power %s\n",
   2118 		    reg & PCIE_SLCSR_PCC ? "off" : "on");
   2119 		onoff("Electromechanical Interlock Control",
   2120 		    reg, PCIE_SLCSR_EIC);
   2121 		onoff("Data Link Layer State Changed Enable", reg,
   2122 		    PCIE_SLCSR_DLLSCE);
   2123 		onoff("Auto Slot Power Limit Disable", reg,
   2124 		    PCIE_SLCSR_AUTOSPLDIS);
   2125 
   2126 		/* Slot Status Register */
   2127 		printf("    Slot Status Register: 0x%04x\n", reg >> 16);
   2128 		onoff("Attention Button Pressed", reg, PCIE_SLCSR_ABP);
   2129 		onoff("Power Fault Detected", reg, PCIE_SLCSR_PFD);
   2130 		onoff("MRL Sensor Changed", reg, PCIE_SLCSR_MSC);
   2131 		onoff("Presence Detect Changed", reg, PCIE_SLCSR_PDC);
   2132 		onoff("Command Completed", reg, PCIE_SLCSR_CC);
   2133 		onoff("MRL Open", reg, PCIE_SLCSR_MS);
   2134 		onoff("Card Present in slot", reg, PCIE_SLCSR_PDS);
   2135 		onoff("Electromechanical Interlock engaged", reg,
   2136 		    PCIE_SLCSR_EIS);
   2137 		onoff("Data Link Layer State Changed", reg, PCIE_SLCSR_LACS);
   2138 	}
   2139 
   2140 	if (PCIE_HAS_ROOTREGS(pcie_devtype)) {
   2141 		/* Root Control Register */
   2142 		reg = regs[o2i(capoff + PCIE_RCR)];
   2143 		printf("    Root Control Register: 0x%04x\n", reg & 0xffff);
   2144 		onoff("SERR on Correctable Error Enable", reg,
   2145 		    PCIE_RCR_SERR_CER);
   2146 		onoff("SERR on Non-Fatal Error Enable", reg,
   2147 		    PCIE_RCR_SERR_NFER);
   2148 		onoff("SERR on Fatal Error Enable", reg, PCIE_RCR_SERR_FER);
   2149 		onoff("PME Interrupt Enable", reg, PCIE_RCR_PME_IE);
   2150 		onoff("CRS Software Visibility Enable", reg, PCIE_RCR_CRS_SVE);
   2151 
   2152 		/* Root Capability Register */
   2153 		printf("    Root Capability Register: 0x%04x\n",
   2154 		    reg >> 16);
   2155 		onoff("CRS Software Visibility", reg, PCIE_RCR_CRS_SV);
   2156 
   2157 		/* Root Status Register */
   2158 		reg = regs[o2i(capoff + PCIE_RSR)];
   2159 		printf("    Root Status Register: 0x%08x\n", reg);
   2160 		printf("      PME Requester ID: 0x%04x\n",
   2161 		    (unsigned int)(reg & PCIE_RSR_PME_REQESTER));
   2162 		onoff("PME was asserted", reg, PCIE_RSR_PME_STAT);
   2163 		onoff("another PME is pending", reg, PCIE_RSR_PME_PEND);
   2164 	}
   2165 
   2166 	/* PCIe DW9 to DW14 is for PCIe 2.0 and newer */
   2167 	if (pciever < 2)
   2168 		return;
   2169 
   2170 	/* Device Capabilities 2 */
   2171 	reg = regs[o2i(capoff + PCIE_DCAP2)];
   2172 	printf("    Device Capabilities 2: 0x%08x\n", reg);
   2173 	printf("      Completion Timeout Ranges Supported: ");
   2174 	val = reg & PCIE_DCAP2_COMPT_RANGE;
   2175 	switch (val) {
   2176 	case 0:
   2177 		printf("not supported\n");
   2178 		break;
   2179 	default:
   2180 		for (i = 0; i <= 3; i++) {
   2181 			if (((val >> i) & 0x01) != 0)
   2182 				printf("%c", 'A' + i);
   2183 		}
   2184 		printf("\n");
   2185 	}
   2186 	onoff("Completion Timeout Disable Supported", reg,
   2187 	    PCIE_DCAP2_COMPT_DIS);
   2188 	onoff("ARI Forwarding Supported", reg, PCIE_DCAP2_ARI_FWD);
   2189 	onoff("AtomicOp Routing Supported", reg, PCIE_DCAP2_ATOM_ROUT);
   2190 	onoff("32bit AtomicOp Completer Supported", reg, PCIE_DCAP2_32ATOM);
   2191 	onoff("64bit AtomicOp Completer Supported", reg, PCIE_DCAP2_64ATOM);
   2192 	onoff("128-bit CAS Completer Supported", reg, PCIE_DCAP2_128CAS);
   2193 	onoff("No RO-enabled PR-PR passing", reg, PCIE_DCAP2_NO_ROPR_PASS);
   2194 	onoff("LTR Mechanism Supported", reg, PCIE_DCAP2_LTR_MEC);
   2195 	printf("      TPH Completer Supported: ");
   2196 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_TPH_COMP)) {
   2197 	case 0:
   2198 		printf("Not supported\n");
   2199 		break;
   2200 	case 1:
   2201 		printf("TPH\n");
   2202 		break;
   2203 	case 3:
   2204 		printf("TPH and Extended TPH\n");
   2205 		break;
   2206 	default:
   2207 		printf("(reserved value)\n");
   2208 		break;
   2209 	}
   2210 	printf("      LN System CLS: ");
   2211 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_LNSYSCLS)) {
   2212 	case 0x0:
   2213 		printf("Not supported or not in effect\n");
   2214 		break;
   2215 	case 0x1:
   2216 		printf("64byte cachelines in effect\n");
   2217 		break;
   2218 	case 0x2:
   2219 		printf("128byte cachelines in effect\n");
   2220 		break;
   2221 	case 0x3:
   2222 		printf("Reserved\n");
   2223 		break;
   2224 	}
   2225 	onoff("10-bit Tag Completer Supported", reg, PCIE_DCAP2_TBT_COMP);
   2226 	onoff("10-bit Tag Requester Supported", reg, PCIE_DCAP2_TBT_REQ);
   2227 	printf("      OBFF Supported: ");
   2228 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_OBFF)) {
   2229 	case 0x0:
   2230 		printf("Not supported\n");
   2231 		break;
   2232 	case 0x1:
   2233 		printf("Message only\n");
   2234 		break;
   2235 	case 0x2:
   2236 		printf("WAKE# only\n");
   2237 		break;
   2238 	case 0x3:
   2239 		printf("Both\n");
   2240 		break;
   2241 	}
   2242 	onoff("Extended Fmt Field Supported", reg, PCIE_DCAP2_EXTFMT_FLD);
   2243 	onoff("End-End TLP Prefix Supported", reg, PCIE_DCAP2_EETLP_PREF);
   2244 	val = PCIREG_SHIFTOUT(reg, PCIE_DCAP2_MAX_EETLP);
   2245 	printf("      Max End-End TLP Prefixes: %u\n", (val == 0) ? 4 : val);
   2246 	printf("      Emergency Power Reduction Supported: ");
   2247 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_EMGPWRRED)) {
   2248 	case 0x0:
   2249 		printf("Not supported\n");
   2250 		break;
   2251 	case 0x1:
   2252 		printf("Device Specific mechanism\n");
   2253 		break;
   2254 	case 0x2:
   2255 		printf("Form Factor spec or Device Specific mechanism\n");
   2256 		break;
   2257 	case 0x3:
   2258 		printf("Reserved\n");
   2259 		break;
   2260 	}
   2261 	onoff("Emergency Power Reduction Initialization Required", reg,
   2262 	    PCIE_DCAP2_EMGPWRRED_INI);
   2263 	onoff("FRS Supported", reg, PCIE_DCAP2_FRS);
   2264 
   2265 	/* Device Control 2 */
   2266 	reg = regs[o2i(capoff + PCIE_DCSR2)];
   2267 	printf("    Device Control 2: 0x%04x\n", reg & 0xffff);
   2268 	printf("      Completion Timeout Value: ");
   2269 	pci_print_pcie_compl_timeout(reg & PCIE_DCSR2_COMPT_VAL);
   2270 	onoff("Completion Timeout Disabled", reg, PCIE_DCSR2_COMPT_DIS);
   2271 	onoff("ARI Forwarding Enabled", reg, PCIE_DCSR2_ARI_FWD);
   2272 	onoff("AtomicOp Requester Enabled", reg, PCIE_DCSR2_ATOM_REQ);
   2273 	onoff("AtomicOp Egress Blocking", reg, PCIE_DCSR2_ATOM_EBLK);
   2274 	onoff("IDO Request Enabled", reg, PCIE_DCSR2_IDO_REQ);
   2275 	onoff("IDO Completion Enabled", reg, PCIE_DCSR2_IDO_COMP);
   2276 	onoff("LTR Mechanism Enabled", reg, PCIE_DCSR2_LTR_MEC);
   2277 	onoff("Emergency Power Reduction Request", reg,
   2278 	    PCIE_DCSR2_EMGPWRRED_REQ);
   2279 	onoff("10-bit Tag Requester Enabled", reg, PCIE_DCSR2_TBT_REQ);
   2280 	printf("      OBFF: ");
   2281 	switch (PCIREG_SHIFTOUT(reg, PCIE_DCSR2_OBFF_EN)) {
   2282 	case 0x0:
   2283 		printf("Disabled\n");
   2284 		break;
   2285 	case 0x1:
   2286 		printf("Enabled with Message Signaling Variation A\n");
   2287 		break;
   2288 	case 0x2:
   2289 		printf("Enabled with Message Signaling Variation B\n");
   2290 		break;
   2291 	case 0x3:
   2292 		printf("Enabled using WAKE# signaling\n");
   2293 		break;
   2294 	}
   2295 	onoff("End-End TLP Prefix Blocking on", reg, PCIE_DCSR2_EETLP);
   2296 
   2297 	if (PCIE_HAS_LINKREGS(pcie_devtype)) {
   2298 		bool drs_supported = false;
   2299 
   2300 		/* Link Capability 2 */
   2301 		reg = regs[o2i(capoff + PCIE_LCAP2)];
   2302 		/* If the vector is 0, LCAP2 is not implemented */
   2303 		if ((reg & PCIE_LCAP2_SUP_LNKSV) != 0) {
   2304 			printf("    Link Capabilities 2: 0x%08x\n", reg);
   2305 			printf("      Supported Link Speeds Vector:");
   2306 			pci_print_pcie_linkspeedvector(
   2307 				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_SUP_LNKSV));
   2308 			printf("\n");
   2309 			onoff("Crosslink Supported", reg, PCIE_LCAP2_CROSSLNK);
   2310 			printf("      "
   2311 			    "Lower SKP OS Generation Supported Speed Vector:");
   2312 			pci_print_pcie_linkspeedvector(
   2313 				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_LOWSKPOS_GENSUPPSV));
   2314 			printf("\n");
   2315 			printf("      "
   2316 			    "Lower SKP OS Reception Supported Speed Vector:");
   2317 			pci_print_pcie_linkspeedvector(
   2318 				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_LOWSKPOS_RECSUPPSV));
   2319 			printf("\n");
   2320 			onoff("Retimer Presence Detect Supported", reg,
   2321 			    PCIE_LCAP2_RETIMERPD);
   2322 			onoff("DRS Supported", reg, PCIE_LCAP2_DRS);
   2323 			drs_supported = (reg & PCIE_LCAP2_DRS) ? true : false;
   2324 		}
   2325 
   2326 		/* Link Control 2 */
   2327 		reg = regs[o2i(capoff + PCIE_LCSR2)];
   2328 		/* If the vector is 0, LCAP2 is not implemented */
   2329 		printf("    Link Control 2: 0x%04x\n", reg & 0xffff);
   2330 		printf("      Target Link Speed: ");
   2331 		pci_print_pcie_linkspeed(PCIE_LCSR2,
   2332 		    PCIREG_SHIFTOUT(reg, PCIE_LCSR2_TGT_LSPEED));
   2333 		onoff("Enter Compliance Enabled", reg, PCIE_LCSR2_ENT_COMPL);
   2334 		onoff("HW Autonomous Speed Disabled", reg,
   2335 		    PCIE_LCSR2_HW_AS_DIS);
   2336 		printf("      Selectable De-emphasis: ");
   2337 		pci_print_pcie_link_deemphasis(
   2338 			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_SEL_DEEMP));
   2339 		printf("\n");
   2340 		printf("      Transmit Margin: %u\n",
   2341 		    PCIREG_SHIFTOUT(reg,  PCIE_LCSR2_TX_MARGIN));
   2342 		onoff("Enter Modified Compliance", reg, PCIE_LCSR2_EN_MCOMP);
   2343 		onoff("Compliance SOS", reg, PCIE_LCSR2_COMP_SOS);
   2344 		printf("      Compliance Present/De-emphasis: ");
   2345 		pci_print_pcie_link_deemphasis(
   2346 			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_COMP_DEEMP));
   2347 		printf("\n");
   2348 
   2349 		/* Link Status 2 */
   2350 		printf("    Link Status 2: 0x%04x\n", (reg >> 16) & 0xffff);
   2351 		printf("      Current De-emphasis Level: ");
   2352 		pci_print_pcie_link_deemphasis(
   2353 			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_DEEMP_LVL));
   2354 		printf("\n");
   2355 		onoff("Equalization Complete", reg, PCIE_LCSR2_EQ_COMPL);
   2356 		onoff("Equalization Phase 1 Successful", reg,
   2357 		    PCIE_LCSR2_EQP1_SUC);
   2358 		onoff("Equalization Phase 2 Successful", reg,
   2359 		    PCIE_LCSR2_EQP2_SUC);
   2360 		onoff("Equalization Phase 3 Successful", reg,
   2361 		    PCIE_LCSR2_EQP3_SUC);
   2362 		onoff("Link Equalization Request", reg, PCIE_LCSR2_LNKEQ_REQ);
   2363 		onoff("Retimer Presence Detected", reg, PCIE_LCSR2_RETIMERPD);
   2364 		if (drs_supported) {
   2365 			printf("      Downstream Component Presence: ");
   2366 			switch (PCIREG_SHIFTOUT(reg, PCIE_LCSR2_DSCOMPN)) {
   2367 			case PCIE_DSCOMPN_DOWN_NOTDETERM:
   2368 				printf("Link Down - Presence Not"
   2369 				    " Determined\n");
   2370 				break;
   2371 			case PCIE_DSCOMPN_DOWN_NOTPRES:
   2372 				printf("Link Down - Component Not Present\n");
   2373 				break;
   2374 			case PCIE_DSCOMPN_DOWN_PRES:
   2375 				printf("Link Down - Component Present\n");
   2376 				break;
   2377 			case PCIE_DSCOMPN_UP_PRES:
   2378 				printf("Link Up - Component Present\n");
   2379 				break;
   2380 			case PCIE_DSCOMPN_UP_PRES_DRS:
   2381 				printf("Link Up - Component Present and DRS"
   2382 				    " received\n");
   2383 				break;
   2384 			default:
   2385 				printf("reserved\n");
   2386 				break;
   2387 			}
   2388 			onoff("DRS Message Received", reg, PCIE_LCSR2_DRSRCV);
   2389 		}
   2390 	}
   2391 
   2392 	/* Slot Capability 2 */
   2393 	/* Slot Control 2 */
   2394 	/* Slot Status 2 */
   2395 }
   2396 
   2397 static void
   2398 pci_conf_print_msix_cap(const pcireg_t *regs, int capoff)
   2399 {
   2400 	pcireg_t reg;
   2401 
   2402 	printf("\n  MSI-X Capability Register\n");
   2403 
   2404 	reg = regs[o2i(capoff + PCI_MSIX_CTL)];
   2405 	printf("    Message Control register: 0x%04x\n",
   2406 	    (reg >> 16) & 0xff);
   2407 	printf("      Table Size: %d\n", PCI_MSIX_CTL_TBLSIZE(reg));
   2408 	onoff("Function Mask", reg, PCI_MSIX_CTL_FUNCMASK);
   2409 	onoff("MSI-X Enable", reg, PCI_MSIX_CTL_ENABLE);
   2410 	reg = regs[o2i(capoff + PCI_MSIX_TBLOFFSET)];
   2411 	printf("    Table offset register: 0x%08x\n", reg);
   2412 	printf("      Table offset: 0x%08x\n",
   2413 	    (pcireg_t)(reg & PCI_MSIX_TBLOFFSET_MASK));
   2414 	printf("      BIR: 0x%x\n", (pcireg_t)(reg & PCI_MSIX_TBLBIR_MASK));
   2415 	reg = regs[o2i(capoff + PCI_MSIX_PBAOFFSET)];
   2416 	printf("    Pending bit array register: 0x%08x\n", reg);
   2417 	printf("      Pending bit array offset: 0x%08x\n",
   2418 	    (pcireg_t)(reg & PCI_MSIX_PBAOFFSET_MASK));
   2419 	printf("      BIR: 0x%x\n", (pcireg_t)(reg & PCI_MSIX_PBABIR_MASK));
   2420 }
   2421 
   2422 static void
   2423 pci_conf_print_sata_cap(const pcireg_t *regs, int capoff)
   2424 {
   2425 	pcireg_t reg;
   2426 
   2427 	printf("\n  Serial ATA Capability Register\n");
   2428 
   2429 	reg = regs[o2i(capoff + PCI_SATA_REV)];
   2430 	printf("    Revision register: 0x%04x\n", (reg >> 16) & 0xff);
   2431 	printf("      Revision: %u.%u\n",
   2432 	    PCIREG_SHIFTOUT(reg, PCI_SATA_REV_MAJOR),
   2433 	    PCIREG_SHIFTOUT(reg, PCI_SATA_REV_MINOR));
   2434 
   2435 	reg = regs[o2i(capoff + PCI_SATA_BAR)];
   2436 
   2437 	printf("    BAR Register: 0x%08x\n", reg);
   2438 	printf("      Register location: ");
   2439 	if ((reg & PCI_SATA_BAR_SPEC) == PCI_SATA_BAR_INCONF)
   2440 		printf("in config space\n");
   2441 	else {
   2442 		printf("BAR %d\n", (int)PCI_SATA_BAR_NUM(reg));
   2443 		printf("      BAR offset: 0x%08x\n",
   2444 		    PCIREG_SHIFTOUT(reg, PCI_SATA_BAR_OFFSET) * 4);
   2445 	}
   2446 }
   2447 
   2448 static void
   2449 pci_conf_print_pciaf_cap(const pcireg_t *regs, int capoff)
   2450 {
   2451 	pcireg_t reg;
   2452 
   2453 	printf("\n  Advanced Features Capability Register\n");
   2454 
   2455 	reg = regs[o2i(capoff + PCI_AFCAPR)];
   2456 	printf("    AF Capabilities register: 0x%02x\n", (reg >> 24) & 0xff);
   2457 	printf("    AF Structure Length: 0x%02x\n",
   2458 	    PCIREG_SHIFTOUT(reg, PCI_AF_LENGTH));
   2459 	onoff("Transaction Pending", reg, PCI_AF_TP_CAP);
   2460 	onoff("Function Level Reset", reg, PCI_AF_FLR_CAP);
   2461 	reg = regs[o2i(capoff + PCI_AFCSR)];
   2462 	printf("    AF Control register: 0x%02x\n", reg & 0xff);
   2463 	/*
   2464 	 * Only PCI_AFCR_INITIATE_FLR is a member of the AF control register
   2465 	 * and it's always 0 on read
   2466 	 */
   2467 	printf("    AF Status register: 0x%02x\n", (reg >> 8) & 0xff);
   2468 	onoff("Transaction Pending", reg, PCI_AFSR_TP);
   2469 }
   2470 
   2471 static void
   2472 pci_conf_print_ea_cap_prop(unsigned int prop)
   2473 {
   2474 
   2475 	switch (prop) {
   2476 	case PCI_EA_PROP_MEM_NONPREF:
   2477 		printf("Memory Space, Non-Prefetchable\n");
   2478 		break;
   2479 	case PCI_EA_PROP_MEM_PREF:
   2480 		printf("Memory Space, Prefetchable\n");
   2481 		break;
   2482 	case PCI_EA_PROP_IO:
   2483 		printf("I/O Space\n");
   2484 		break;
   2485 	case PCI_EA_PROP_VF_MEM_NONPREF:
   2486 		printf("Resorce for VF use, Memory Space, Non-Prefetchable\n");
   2487 		break;
   2488 	case PCI_EA_PROP_VF_MEM_PREF:
   2489 		printf("Resorce for VF use, Memory Space, Prefetch\n");
   2490 		break;
   2491 	case PCI_EA_PROP_BB_MEM_NONPREF:
   2492 		printf("Behind the Bridge, Memory Space, Non-Pref\n");
   2493 		break;
   2494 	case PCI_EA_PROP_BB_MEM_PREF:
   2495 		printf("Behind the Bridge, Memory Space. Prefetchable\n");
   2496 		break;
   2497 	case PCI_EA_PROP_BB_IO:
   2498 		printf("Behind Bridge, I/O Space\n");
   2499 		break;
   2500 	case PCI_EA_PROP_MEM_UNAVAIL:
   2501 		printf("Memory Space Unavailable\n");
   2502 		break;
   2503 	case PCI_EA_PROP_IO_UNAVAIL:
   2504 		printf("IO Space Unavailable\n");
   2505 		break;
   2506 	case PCI_EA_PROP_UNAVAIL:
   2507 		printf("Entry Unavailable for use\n");
   2508 		break;
   2509 	default:
   2510 		printf("Reserved\n");
   2511 		break;
   2512 	}
   2513 }
   2514 
   2515 static void
   2516 pci_conf_print_ea_cap(const pcireg_t *regs, int capoff)
   2517 {
   2518 	pcireg_t reg, reg2;
   2519 	unsigned int entries, entoff, i;
   2520 
   2521 	printf("\n  Enhanced Allocation Capability Register\n");
   2522 
   2523 	reg = regs[o2i(capoff + PCI_EA_CAP1)];
   2524 	printf("    EA Num Entries register: 0x%04x\n", reg >> 16);
   2525 	entries = PCIREG_SHIFTOUT(reg, PCI_EA_CAP1_NUMENTRIES);
   2526 	printf("      EA Num Entries: %u\n", entries);
   2527 
   2528 	/* Type 1 only */
   2529 	if (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]) == PCI_HDRTYPE_PPB) {
   2530 		reg = regs[o2i(capoff + PCI_EA_CAP2)];
   2531 		printf("    EA Capability Second register: 0x%08x\n", reg);
   2532 		printf("      Fixed Secondary Bus Number: %hhu\n",
   2533 		    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_EA_CAP2_SECONDARY));
   2534 		printf("      Fixed Subordinate Bus Number: %hhu\n",
   2535 		    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_EA_CAP2_SUBORDINATE));
   2536 		entoff = capoff + 8;
   2537 	} else
   2538 		entoff = capoff + 4;
   2539 
   2540 	for (i = 0; i < entries; i++) {
   2541 		uint64_t base, offset;
   2542 		bool baseis64, offsetis64;
   2543 		unsigned int bei, entry_size;
   2544 
   2545 		printf("    Entry %u:\n", i);
   2546 		/* The first DW */
   2547 		reg = regs[o2i(entoff)];
   2548 		printf("      The first register: 0x%08x\n", reg);
   2549 		entry_size = PCIREG_SHIFTOUT(reg, PCI_EA_ES);
   2550 		printf("        Entry size: %u\n", entry_size);
   2551 		printf("        BAR Equivalent Indicator: ");
   2552 		bei = PCIREG_SHIFTOUT(reg, PCI_EA_BEI);
   2553 		switch (bei) {
   2554 		case PCI_EA_BEI_BAR0:
   2555 		case PCI_EA_BEI_BAR1:
   2556 		case PCI_EA_BEI_BAR2:
   2557 		case PCI_EA_BEI_BAR3:
   2558 		case PCI_EA_BEI_BAR4:
   2559 		case PCI_EA_BEI_BAR5:
   2560 			printf("BAR %u\n", bei - PCI_EA_BEI_BAR0);
   2561 			break;
   2562 		case PCI_EA_BEI_BEHIND:
   2563 			printf("Behind the function\n");
   2564 			break;
   2565 		case PCI_EA_BEI_NOTIND:
   2566 			printf("Not Indicated\n");
   2567 			break;
   2568 		case PCI_EA_BEI_EXPROM:
   2569 			printf("Expansion ROM\n");
   2570 			break;
   2571 		case PCI_EA_BEI_VFBAR0:
   2572 		case PCI_EA_BEI_VFBAR1:
   2573 		case PCI_EA_BEI_VFBAR2:
   2574 		case PCI_EA_BEI_VFBAR3:
   2575 		case PCI_EA_BEI_VFBAR4:
   2576 		case PCI_EA_BEI_VFBAR5:
   2577 			printf("VF BAR %u\n", bei - PCI_EA_BEI_VFBAR0);
   2578 			break;
   2579 		case PCI_EA_BEI_RESERVED:
   2580 		default:
   2581 			printf("Reserved\n");
   2582 			break;
   2583 		}
   2584 
   2585 		printf("      Primary Properties: ");
   2586 		pci_conf_print_ea_cap_prop(PCIREG_SHIFTOUT(reg, PCI_EA_PP));
   2587 		printf("      Secondary Properties: ");
   2588 		pci_conf_print_ea_cap_prop(PCIREG_SHIFTOUT(reg, PCI_EA_SP));
   2589 		onoff("Writable", reg, PCI_EA_W);
   2590 		onoff("Enable for this entry", reg, PCI_EA_E);
   2591 
   2592 		if (entry_size == 0) {
   2593 			entoff += 4;
   2594 			continue;
   2595 		}
   2596 
   2597 		/* Base addr */
   2598 		reg = regs[o2i(entoff + 4)];
   2599 		base = reg & PCI_EA_LOWMASK;
   2600 		baseis64 = reg & PCI_EA_BASEMAXOFFSET_64BIT;
   2601 		printf("      Base Address Register Low: 0x%08x\n", reg);
   2602 		if (baseis64) {
   2603 			/* 64bit */
   2604 			reg2 = regs[o2i(entoff + 12)];
   2605 			printf("      Base Address Register high: 0x%08x\n",
   2606 			    reg2);
   2607 			base |= (uint64_t)reg2 << 32;
   2608 		}
   2609 
   2610 		/* Offset addr */
   2611 		reg = regs[o2i(entoff + 8)];
   2612 		offset = reg & PCI_EA_LOWMASK;
   2613 		offsetis64 = reg & PCI_EA_BASEMAXOFFSET_64BIT;
   2614 		printf("      Max Offset Register Low: 0x%08x\n", reg);
   2615 		if (offsetis64) {
   2616 			/* 64bit */
   2617 			reg2 = regs[o2i(entoff + (baseis64 ? 16 : 12))];
   2618 			printf("      Max Offset Register high: 0x%08x\n",
   2619 			    reg2);
   2620 			offset |= (uint64_t)reg2 << 32;
   2621 		}
   2622 
   2623 		printf("        range: 0x%016" PRIx64 "-0x%016" PRIx64
   2624 			    "\n", base, base + offset);
   2625 
   2626 		entoff += 4 + (4 * entry_size);
   2627 	}
   2628 }
   2629 
   2630 /* XXX pci_conf_print_fpb_cap */
   2631 
   2632 static struct {
   2633 	pcireg_t cap;
   2634 	const char *name;
   2635 	void (*printfunc)(const pcireg_t *, int);
   2636 } pci_captab[] = {
   2637 	{ PCI_CAP_RESERVED0,	"reserved",	NULL },
   2638 	{ PCI_CAP_PWRMGMT,	"Power Management", pci_conf_print_pcipm_cap },
   2639 	{ PCI_CAP_AGP,		"AGP",		pci_conf_print_agp_cap },
   2640 	{ PCI_CAP_VPD,		"VPD",		NULL },
   2641 	{ PCI_CAP_SLOTID,	"SlotID",	NULL },
   2642 	{ PCI_CAP_MSI,		"MSI",		pci_conf_print_msi_cap },
   2643 	{ PCI_CAP_CPCI_HOTSWAP,	"CompactPCI Hot-swapping", NULL },
   2644 	{ PCI_CAP_PCIX,		"PCI-X",	pci_conf_print_pcix_cap },
   2645 	{ PCI_CAP_LDT,		"HyperTransport", pci_conf_print_ht_cap },
   2646 	{ PCI_CAP_VENDSPEC,	"Vendor-specific",
   2647 	  pci_conf_print_vendspec_cap },
   2648 	{ PCI_CAP_DEBUGPORT,	"Debug Port",	pci_conf_print_debugport_cap },
   2649 	{ PCI_CAP_CPCI_RSRCCTL, "CompactPCI Resource Control", NULL },
   2650 	{ PCI_CAP_HOTPLUG,	"Hot-Plug",	NULL },
   2651 	{ PCI_CAP_SUBVENDOR,	"Subsystem vendor ID",
   2652 	  pci_conf_print_subsystem_cap },
   2653 	{ PCI_CAP_AGP8,		"AGP 8x",	NULL },
   2654 	{ PCI_CAP_SECURE,	"Secure Device", pci_conf_print_secure_cap },
   2655 	{ PCI_CAP_PCIEXPRESS,	"PCI Express",	pci_conf_print_pcie_cap },
   2656 	{ PCI_CAP_MSIX,		"MSI-X",	pci_conf_print_msix_cap },
   2657 	{ PCI_CAP_SATA,		"SATA",		pci_conf_print_sata_cap },
   2658 	{ PCI_CAP_PCIAF,	"Advanced Features", pci_conf_print_pciaf_cap},
   2659 	{ PCI_CAP_EA,		"Enhanced Allocation", pci_conf_print_ea_cap },
   2660 	{ PCI_CAP_FPB,		"Flattening Portal Bridge", NULL }
   2661 };
   2662 
   2663 static int
   2664 pci_conf_find_cap(const pcireg_t *regs, unsigned int capid, int *offsetp)
   2665 {
   2666 	pcireg_t rval;
   2667 	unsigned int capptr;
   2668 	int off;
   2669 
   2670 	if (!(regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT))
   2671 		return 0;
   2672 
   2673 	/* Determine the Capability List Pointer register to start with. */
   2674 	switch (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)])) {
   2675 	case 0:	/* standard device header */
   2676 	case 1: /* PCI-PCI bridge header */
   2677 		capptr = PCI_CAPLISTPTR_REG;
   2678 		break;
   2679 	case 2:	/* PCI-CardBus Bridge header */
   2680 		capptr = PCI_CARDBUS_CAPLISTPTR_REG;
   2681 		break;
   2682 	default:
   2683 		return 0;
   2684 	}
   2685 
   2686 	for (off = PCI_CAPLIST_PTR(regs[o2i(capptr)]);
   2687 	     off != 0; off = PCI_CAPLIST_NEXT(rval)) {
   2688 		rval = regs[o2i(off)];
   2689 		if (capid == PCI_CAPLIST_CAP(rval)) {
   2690 			if (offsetp != NULL)
   2691 				*offsetp = off;
   2692 			return 1;
   2693 		}
   2694 	}
   2695 	return 0;
   2696 }
   2697 
   2698 static void
   2699 pci_conf_print_caplist(
   2700 #ifdef _KERNEL
   2701     pci_chipset_tag_t pc, pcitag_t tag,
   2702 #endif
   2703     const pcireg_t *regs, int capoff)
   2704 {
   2705 	int off;
   2706 	pcireg_t foundcap;
   2707 	pcireg_t rval;
   2708 	bool foundtable[__arraycount(pci_captab)];
   2709 	unsigned int i;
   2710 
   2711 	/* Clear table */
   2712 	for (i = 0; i < __arraycount(pci_captab); i++)
   2713 		foundtable[i] = false;
   2714 
   2715 	/* Print capability register's offset and the type first */
   2716 	for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
   2717 	     off != 0; off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
   2718 		rval = regs[o2i(off)];
   2719 		printf("  Capability register at 0x%02x\n", off);
   2720 
   2721 		printf("    type: 0x%02x (", PCI_CAPLIST_CAP(rval));
   2722 		foundcap = PCI_CAPLIST_CAP(rval);
   2723 		if (foundcap < __arraycount(pci_captab)) {
   2724 			printf("%s)\n", pci_captab[foundcap].name);
   2725 			/* Mark as found */
   2726 			foundtable[foundcap] = true;
   2727 		} else
   2728 			printf("unknown)\n");
   2729 	}
   2730 
   2731 	/*
   2732 	 * And then, print the detail of each capability registers
   2733 	 * in capability value's order.
   2734 	 */
   2735 	for (i = 0; i < __arraycount(pci_captab); i++) {
   2736 		if (foundtable[i] == false)
   2737 			continue;
   2738 
   2739 		/*
   2740 		 * The type was found. Search capability list again and
   2741 		 * print all capabilities that the capability type is
   2742 		 * the same. This is required because some capabilities
   2743 		 * appear multiple times (e.g. HyperTransport capability).
   2744 		 */
   2745 		for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
   2746 		     off != 0; off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
   2747 			rval = regs[o2i(off)];
   2748 			if ((PCI_CAPLIST_CAP(rval) == i)
   2749 			    && (pci_captab[i].printfunc != NULL))
   2750 				pci_captab[i].printfunc(regs, off);
   2751 		}
   2752 	}
   2753 }
   2754 
   2755 /* Extended Capability */
   2756 
   2757 static void
   2758 pci_conf_print_aer_cap_uc(pcireg_t reg)
   2759 {
   2760 
   2761 	onoff("Undefined", reg, PCI_AER_UC_UNDEFINED);
   2762 	onoff("Data Link Protocol Error", reg, PCI_AER_UC_DL_PROTOCOL_ERROR);
   2763 	onoff("Surprise Down Error", reg, PCI_AER_UC_SURPRISE_DOWN_ERROR);
   2764 	onoff("Poisoned TLP Received", reg, PCI_AER_UC_POISONED_TLP);
   2765 	onoff("Flow Control Protocol Error", reg, PCI_AER_UC_FC_PROTOCOL_ERROR);
   2766 	onoff("Completion Timeout", reg, PCI_AER_UC_COMPLETION_TIMEOUT);
   2767 	onoff("Completer Abort", reg, PCI_AER_UC_COMPLETER_ABORT);
   2768 	onoff("Unexpected Completion", reg, PCI_AER_UC_UNEXPECTED_COMPLETION);
   2769 	onoff("Receiver Overflow", reg, PCI_AER_UC_RECEIVER_OVERFLOW);
   2770 	onoff("Malformed TLP", reg, PCI_AER_UC_MALFORMED_TLP);
   2771 	onoff("ECRC Error", reg, PCI_AER_UC_ECRC_ERROR);
   2772 	onoff("Unsupported Request Error", reg,
   2773 	    PCI_AER_UC_UNSUPPORTED_REQUEST_ERROR);
   2774 	onoff("ACS Violation", reg, PCI_AER_UC_ACS_VIOLATION);
   2775 	onoff("Uncorrectable Internal Error", reg, PCI_AER_UC_INTERNAL_ERROR);
   2776 	onoff("MC Blocked TLP", reg, PCI_AER_UC_MC_BLOCKED_TLP);
   2777 	onoff("AtomicOp Egress BLK", reg, PCI_AER_UC_ATOMIC_OP_EGRESS_BLOCKED);
   2778 	onoff("TLP Prefix Blocked Error", reg,
   2779 	    PCI_AER_UC_TLP_PREFIX_BLOCKED_ERROR);
   2780 	onoff("Poisoned TLP Egress Blocked", reg,
   2781 	    PCI_AER_UC_POISONTLP_EGRESS_BLOCKED);
   2782 }
   2783 
   2784 static void
   2785 pci_conf_print_aer_cap_cor(pcireg_t reg)
   2786 {
   2787 
   2788 	onoff("Receiver Error", reg, PCI_AER_COR_RECEIVER_ERROR);
   2789 	onoff("Bad TLP", reg, PCI_AER_COR_BAD_TLP);
   2790 	onoff("Bad DLLP", reg, PCI_AER_COR_BAD_DLLP);
   2791 	onoff("REPLAY_NUM Rollover", reg, PCI_AER_COR_REPLAY_NUM_ROLLOVER);
   2792 	onoff("Replay Timer Timeout", reg, PCI_AER_COR_REPLAY_TIMER_TIMEOUT);
   2793 	onoff("Advisory Non-Fatal Error", reg, PCI_AER_COR_ADVISORY_NF_ERROR);
   2794 	onoff("Corrected Internal Error", reg, PCI_AER_COR_INTERNAL_ERROR);
   2795 	onoff("Header Log Overflow", reg, PCI_AER_COR_HEADER_LOG_OVERFLOW);
   2796 }
   2797 
   2798 static void
   2799 pci_conf_print_aer_cap_control(pcireg_t reg, bool *tlp_prefix_log)
   2800 {
   2801 
   2802 	printf("      First Error Pointer: 0x%04x\n",
   2803 	    PCIREG_SHIFTOUT(reg, PCI_AER_FIRST_ERROR_PTR));
   2804 	onoff("ECRC Generation Capable", reg, PCI_AER_ECRC_GEN_CAPABLE);
   2805 	onoff("ECRC Generation Enable", reg, PCI_AER_ECRC_GEN_ENABLE);
   2806 	onoff("ECRC Check Capable", reg, PCI_AER_ECRC_CHECK_CAPABLE);
   2807 	onoff("ECRC Check Enable", reg, PCI_AER_ECRC_CHECK_ENABLE);
   2808 	onoff("Multiple Header Recording Capable", reg,
   2809 	    PCI_AER_MULT_HDR_CAPABLE);
   2810 	onoff("Multiple Header Recording Enable", reg,PCI_AER_MULT_HDR_ENABLE);
   2811 	onoff("Completion Timeout Prefix/Header Log Capable", reg,
   2812 	    PCI_AER_COMPTOUTPRFXHDRLOG_CAP);
   2813 
   2814 	/* This bit is RsvdP if the End-End TLP Prefix Supported bit is Clear */
   2815 	if (!tlp_prefix_log)
   2816 		return;
   2817 	onoff("TLP Prefix Log Present", reg, PCI_AER_TLP_PREFIX_LOG_PRESENT);
   2818 	*tlp_prefix_log = (reg & PCI_AER_TLP_PREFIX_LOG_PRESENT) ? true : false;
   2819 }
   2820 
   2821 static void
   2822 pci_conf_print_aer_cap_rooterr_cmd(pcireg_t reg)
   2823 {
   2824 
   2825 	onoff("Correctable Error Reporting Enable", reg,
   2826 	    PCI_AER_ROOTERR_COR_ENABLE);
   2827 	onoff("Non-Fatal Error Reporting Enable", reg,
   2828 	    PCI_AER_ROOTERR_NF_ENABLE);
   2829 	onoff("Fatal Error Reporting Enable", reg, PCI_AER_ROOTERR_F_ENABLE);
   2830 }
   2831 
   2832 static void
   2833 pci_conf_print_aer_cap_rooterr_status(pcireg_t reg)
   2834 {
   2835 
   2836 	onoff("ERR_COR Received", reg, PCI_AER_ROOTERR_COR_ERR);
   2837 	onoff("Multiple ERR_COR Received", reg, PCI_AER_ROOTERR_MULTI_COR_ERR);
   2838 	onoff("ERR_FATAL/NONFATAL_ERR Received", reg, PCI_AER_ROOTERR_UC_ERR);
   2839 	onoff("Multiple ERR_FATAL/NONFATAL_ERR Received", reg,
   2840 	    PCI_AER_ROOTERR_MULTI_UC_ERR);
   2841 	onoff("First Uncorrectable Fatal", reg,PCI_AER_ROOTERR_FIRST_UC_FATAL);
   2842 	onoff("Non-Fatal Error Messages Received", reg,PCI_AER_ROOTERR_NF_ERR);
   2843 	onoff("Fatal Error Messages Received", reg, PCI_AER_ROOTERR_F_ERR);
   2844 	printf("      Advanced Error Interrupt Message Number: 0x%02x\n",
   2845 	    PCIREG_SHIFTOUT(reg, PCI_AER_ROOTERR_INT_MESSAGE));
   2846 }
   2847 
   2848 static void
   2849 pci_conf_print_aer_cap_errsrc_id(pcireg_t reg)
   2850 {
   2851 
   2852 	printf("      Correctable Source ID: 0x%04x\n",
   2853 	    PCIREG_SHIFTOUT(reg, PCI_AER_ERRSRC_ID_ERR_COR));
   2854 	printf("      ERR_FATAL/NONFATAL Source ID: 0x%04x\n",
   2855 	    PCIREG_SHIFTOUT(reg, PCI_AER_ERRSRC_ID_ERR_UC));
   2856 }
   2857 
   2858 static void
   2859 pci_conf_print_aer_cap(const pcireg_t *regs, int extcapoff)
   2860 {
   2861 	pcireg_t reg;
   2862 	int pcie_capoff;
   2863 	int pcie_devtype = -1;
   2864 	bool tlp_prefix_log = false;
   2865 
   2866 	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
   2867 		reg = regs[o2i(pcie_capoff)];
   2868 		pcie_devtype = PCIE_XCAP_TYPE(reg);
   2869 		/* PCIe DW9 to DW14 is for PCIe 2.0 and newer */
   2870 		if (PCIREG_SHIFTOUT(reg, PCIE_XCAP_VER_MASK) >= 2) {
   2871 			reg = regs[o2i(pcie_capoff + PCIE_DCAP2)];
   2872 			/* End-End TLP Prefix Supported */
   2873 			if (reg & PCIE_DCAP2_EETLP_PREF) {
   2874 				tlp_prefix_log = true;
   2875 			}
   2876 		}
   2877 	}
   2878 
   2879 	printf("\n  Advanced Error Reporting Register\n");
   2880 
   2881 	reg = regs[o2i(extcapoff + PCI_AER_UC_STATUS)];
   2882 	printf("    Uncorrectable Error Status register: 0x%08x\n", reg);
   2883 	pci_conf_print_aer_cap_uc(reg);
   2884 	reg = regs[o2i(extcapoff + PCI_AER_UC_MASK)];
   2885 	printf("    Uncorrectable Error Mask register: 0x%08x\n", reg);
   2886 	pci_conf_print_aer_cap_uc(reg);
   2887 	reg = regs[o2i(extcapoff + PCI_AER_UC_SEVERITY)];
   2888 	printf("    Uncorrectable Error Severity register: 0x%08x\n", reg);
   2889 	pci_conf_print_aer_cap_uc(reg);
   2890 
   2891 	reg = regs[o2i(extcapoff + PCI_AER_COR_STATUS)];
   2892 	printf("    Correctable Error Status register: 0x%08x\n", reg);
   2893 	pci_conf_print_aer_cap_cor(reg);
   2894 	reg = regs[o2i(extcapoff + PCI_AER_COR_MASK)];
   2895 	printf("    Correctable Error Mask register: 0x%08x\n", reg);
   2896 	pci_conf_print_aer_cap_cor(reg);
   2897 
   2898 	reg = regs[o2i(extcapoff + PCI_AER_CAP_CONTROL)];
   2899 	printf("    Advanced Error Capabilities and Control register: 0x%08x\n",
   2900 	    reg);
   2901 	pci_conf_print_aer_cap_control(reg, &tlp_prefix_log);
   2902 	reg = regs[o2i(extcapoff + PCI_AER_HEADER_LOG)];
   2903 	printf("    Header Log register:\n");
   2904 	pci_conf_print_regs(regs, extcapoff + PCI_AER_HEADER_LOG,
   2905 	    extcapoff + PCI_AER_ROOTERR_CMD);
   2906 
   2907 	switch (pcie_devtype) {
   2908 	case PCIE_XCAP_TYPE_ROOT: /* Root Port of PCI Express Root Complex */
   2909 	case PCIE_XCAP_TYPE_ROOT_EVNTC:	/* Root Complex Event Collector */
   2910 		reg = regs[o2i(extcapoff + PCI_AER_ROOTERR_CMD)];
   2911 		printf("    Root Error Command register: 0x%08x\n", reg);
   2912 		pci_conf_print_aer_cap_rooterr_cmd(reg);
   2913 		reg = regs[o2i(extcapoff + PCI_AER_ROOTERR_STATUS)];
   2914 		printf("    Root Error Status register: 0x%08x\n", reg);
   2915 		pci_conf_print_aer_cap_rooterr_status(reg);
   2916 
   2917 		reg = regs[o2i(extcapoff + PCI_AER_ERRSRC_ID)];
   2918 		printf("    Error Source Identification register: 0x%08x\n",
   2919 		    reg);
   2920 		pci_conf_print_aer_cap_errsrc_id(reg);
   2921 		break;
   2922 	}
   2923 
   2924 	if (tlp_prefix_log) {
   2925 		reg = regs[o2i(extcapoff + PCI_AER_TLP_PREFIX_LOG)];
   2926 		printf("    TLP Prefix Log register: 0x%08x\n", reg);
   2927 	}
   2928 }
   2929 
   2930 /*
   2931  * Helper function to print the arbitration phase register.
   2932  *
   2933  * phases: Number of phases in the arbitration tables.
   2934  * arbsize: Number of bits in each phase.
   2935  * indent: Add more two spaces if it's true.
   2936  */
   2937 static void
   2938 pci_conf_print_vc_cap_arbtab(const pcireg_t *regs, int off, const char *name,
   2939     const int phases, int arbsize, bool indent)
   2940 {
   2941 	pcireg_t reg;
   2942 	int num_per_reg = 32 / arbsize;
   2943 	int i, j;
   2944 
   2945 	printf("%s    %s Arbitration Table:\n", indent ? "  " : "", name);
   2946 	for (i = 0; i < phases; i += num_per_reg) {
   2947 		reg = regs[o2i(off + (sizeof(uint32_t) * (i / num_per_reg)))];
   2948 		for (j = 0; j < num_per_reg; j++) {
   2949 			printf("%s      Phase[%d]: 0x%x\n", indent ? "  " : "",
   2950 			    i + j,
   2951 			    (uint32_t)(reg & __BITS(arbsize - 1, 0)));
   2952 			reg >>= arbsize;
   2953 		}
   2954 	}
   2955 }
   2956 
   2957 /* For VC, bit 4-7 are reserved. For Port, bit 6-7 are reserved */
   2958 static const int arb_phases[8] = {0, 32, 64, 128, 128, 256, 0, 0 };
   2959 
   2960 static void
   2961 pci_conf_print_vc_cap(const pcireg_t *regs, int extcapoff)
   2962 {
   2963 	pcireg_t reg, n;
   2964 	int arbtab, parbsize;
   2965 	pcireg_t arbsel;
   2966 	int i, count;
   2967 
   2968 	printf("\n  Virtual Channel Register\n");
   2969 	reg = regs[o2i(extcapoff + PCI_VC_CAP1)];
   2970 	printf("    Port VC Capability register 1: 0x%08x\n", reg);
   2971 	count = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_EXT_COUNT);
   2972 	printf("      Extended VC Count: %d\n", count);
   2973 	n = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_LOWPRI_EXT_COUNT);
   2974 	printf("      Low Priority Extended VC Count: %u\n", n);
   2975 	n = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_REFCLK);
   2976 	printf("      Reference Clock: %s\n",
   2977 	    (n == PCI_VC_CAP1_REFCLK_100NS) ? "100ns" : "unknown");
   2978 	parbsize = 1 << PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_PORT_ARB_TABLE_SIZE);
   2979 	printf("      Port Arbitration Table Entry Size: %dbit\n", parbsize);
   2980 
   2981 	reg = regs[o2i(extcapoff + PCI_VC_CAP2)];
   2982 	printf("    Port VC Capability register 2: 0x%08x\n", reg);
   2983 	onoff("Hardware fixed arbitration scheme",
   2984 	    reg, PCI_VC_CAP2_ARB_CAP_HW_FIXED_SCHEME);
   2985 	onoff("WRR arbitration with 32 phases",
   2986 	    reg, PCI_VC_CAP2_ARB_CAP_WRR_32);
   2987 	onoff("WRR arbitration with 64 phases",
   2988 	    reg, PCI_VC_CAP2_ARB_CAP_WRR_64);
   2989 	onoff("WRR arbitration with 128 phases",
   2990 	    reg, PCI_VC_CAP2_ARB_CAP_WRR_128);
   2991 	arbtab = PCIREG_SHIFTOUT(reg, PCI_VC_CAP2_ARB_TABLE_OFFSET);
   2992 	printf("      VC Arbitration Table Offset: 0x%x\n", arbtab);
   2993 
   2994 	reg = regs[o2i(extcapoff + PCI_VC_CONTROL)] & 0xffff;
   2995 	printf("    Port VC Control register: 0x%04x\n", reg);
   2996 	arbsel = PCIREG_SHIFTOUT(reg, PCI_VC_CONTROL_VC_ARB_SELECT);
   2997 	printf("      VC Arbitration Select: 0x%x\n", arbsel);
   2998 
   2999 	reg = regs[o2i(extcapoff + PCI_VC_STATUS)] >> 16;
   3000 	printf("    Port VC Status register: 0x%04x\n", reg);
   3001 	onoff("VC Arbitration Table Status",
   3002 	    reg, PCI_VC_STATUS_LOAD_VC_ARB_TABLE);
   3003 
   3004 	if ((arbtab != 0) && (arbsel != 0))
   3005 		pci_conf_print_vc_cap_arbtab(regs, extcapoff + (arbtab * 16),
   3006 		    "VC", arb_phases[arbsel], 4, false);
   3007 
   3008 	for (i = 0; i < count + 1; i++) {
   3009 		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_CAP(i))];
   3010 		printf("    VC number %d\n", i);
   3011 		printf("      VC Resource Capability Register: 0x%08x\n", reg);
   3012 		onoff("  Non-configurable Hardware fixed arbitration scheme",
   3013 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_HW_FIXED_SCHEME);
   3014 		onoff("  WRR arbitration with 32 phases",
   3015 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_32);
   3016 		onoff("  WRR arbitration with 64 phases",
   3017 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_64);
   3018 		onoff("  WRR arbitration with 128 phases",
   3019 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_128);
   3020 		onoff("  Time-based WRR arbitration with 128 phases",
   3021 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_TWRR_128);
   3022 		onoff("  WRR arbitration with 256 phases",
   3023 		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_256);
   3024 		onoff("  Advanced Packet Switching",
   3025 		    reg, PCI_VC_RESOURCE_CAP_ADV_PKT_SWITCH);
   3026 		onoff("  Reject Snoop Transaction",
   3027 		    reg, PCI_VC_RESOURCE_CAP_REJCT_SNOOP_TRANS);
   3028 		n = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CAP_MAX_TIME_SLOTS) + 1;
   3029 		printf("        Maximum Time Slots: %d\n", n);
   3030 		arbtab = PCIREG_SHIFTOUT(reg,
   3031 		    PCI_VC_RESOURCE_CAP_PORT_ARB_TABLE_OFFSET);
   3032 		printf("        Port Arbitration Table offset: 0x%02x\n",
   3033 		    arbtab);
   3034 
   3035 		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_CTL(i))];
   3036 		printf("      VC Resource Control Register: 0x%08x\n", reg);
   3037 		printf("        TC/VC Map: 0x%02x\n",
   3038 		    PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_TCVC_MAP));
   3039 		/*
   3040 		 * The load Port Arbitration Table bit is used to update
   3041 		 * the Port Arbitration logic and it's always 0 on read, so
   3042 		 * we don't print it.
   3043 		 */
   3044 		arbsel = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_PORT_ARB_SELECT);
   3045 		printf("        Port Arbitration Select: 0x%x\n", arbsel);
   3046 		n = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_VC_ID);
   3047 		printf("        VC ID: %d\n", n);
   3048 		onoff("  VC Enable", reg, PCI_VC_RESOURCE_CTL_VC_ENABLE);
   3049 
   3050 		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_STA(i))] >> 16;
   3051 		printf("      VC Resource Status Register: 0x%08x\n", reg);
   3052 		onoff("  Port Arbitration Table Status",
   3053 		    reg, PCI_VC_RESOURCE_STA_PORT_ARB_TABLE);
   3054 		onoff("  VC Negotiation Pending",
   3055 		    reg, PCI_VC_RESOURCE_STA_VC_NEG_PENDING);
   3056 
   3057 		if ((arbtab != 0) && (arbsel != 0))
   3058 			pci_conf_print_vc_cap_arbtab(regs,
   3059 			    extcapoff + (arbtab * 16),
   3060 			    "Port", arb_phases[arbsel], parbsize, true);
   3061 	}
   3062 }
   3063 
   3064 /*
   3065  * Print Power limit. This encoding is the same among the following registers:
   3066  *  - The Captured Slot Power Limit in the PCIe Device Capability Register.
   3067  *  - The Slot Power Limit in the PCIe Slot Capability Register.
   3068  *  - The Base Power in the Data register of Power Budgeting capability.
   3069  */
   3070 static void
   3071 pci_conf_print_pcie_power(uint8_t base, unsigned int scale)
   3072 {
   3073 	unsigned int sdiv = 1;
   3074 
   3075 	if ((scale == 0) && (base > 0xef)) {
   3076 		const char *s;
   3077 
   3078 		switch (base) {
   3079 		case 0xf0:
   3080 			s = "239W < x <= 250W";
   3081 			break;
   3082 		case 0xf1:
   3083 			s = "250W < x <= 275W";
   3084 			break;
   3085 		case 0xf2:
   3086 			s = "275W < x <= 300W";
   3087 			break;
   3088 		default:
   3089 			s = "reserved for greater than 300W";
   3090 			break;
   3091 		}
   3092 		printf("%s\n", s);
   3093 		return;
   3094 	}
   3095 
   3096 	for (unsigned int i = scale; i > 0; i--)
   3097 		sdiv *= 10;
   3098 
   3099 	printf("%u", base / sdiv);
   3100 
   3101 	if (scale != 0) {
   3102 		printf(".%u", base % sdiv);
   3103 	}
   3104 	printf ("W\n");
   3105 	return;
   3106 }
   3107 
   3108 static const char *
   3109 pci_conf_print_pwrbdgt_type(uint8_t reg)
   3110 {
   3111 
   3112 	switch (reg) {
   3113 	case 0x00:
   3114 		return "PME Aux";
   3115 	case 0x01:
   3116 		return "Auxilary";
   3117 	case 0x02:
   3118 		return "Idle";
   3119 	case 0x03:
   3120 		return "Sustained";
   3121 	case 0x04:
   3122 		return "Sustained (Emergency Power Reduction)";
   3123 	case 0x05:
   3124 		return "Maximum (Emergency Power Reduction)";
   3125 	case 0x07:
   3126 		return "Maximum";
   3127 	default:
   3128 		return "Unknown";
   3129 	}
   3130 }
   3131 
   3132 static const char *
   3133 pci_conf_print_pwrbdgt_pwrrail(uint8_t reg)
   3134 {
   3135 
   3136 	switch (reg) {
   3137 	case 0x00:
   3138 		return "Power(12V)";
   3139 	case 0x01:
   3140 		return "Power(3.3V)";
   3141 	case 0x02:
   3142 		return "Power(1.5V or 1.8V)";
   3143 	case 0x07:
   3144 		return "Thermal";
   3145 	default:
   3146 		return "Unknown";
   3147 	}
   3148 }
   3149 
   3150 static void
   3151 pci_conf_print_pwrbdgt_cap(const pcireg_t *regs, int extcapoff)
   3152 {
   3153 	pcireg_t reg;
   3154 
   3155 	printf("\n  Power Budgeting\n");
   3156 
   3157 	reg = regs[o2i(extcapoff + PCI_PWRBDGT_DSEL)];
   3158 	printf("    Data Select register: 0x%08x\n", reg);
   3159 
   3160 	reg = regs[o2i(extcapoff + PCI_PWRBDGT_DATA)];
   3161 	printf("    Data register: 0x%08x\n", reg);
   3162 	printf("      Base Power: ");
   3163 	pci_conf_print_pcie_power(
   3164 	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_DATA_BASEPWR),
   3165 	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_DATA_SCALE));
   3166 	printf("      PM Sub State: 0x%hhx\n",
   3167 	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PM_SUBSTAT));
   3168 	printf("      PM State: D%u\n",
   3169 	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PM_STAT));
   3170 	printf("      Type: %s\n",
   3171 	    pci_conf_print_pwrbdgt_type(
   3172 		    (uint8_t)(PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_TYPE))));
   3173 	printf("      Power Rail: %s\n",
   3174 	    pci_conf_print_pwrbdgt_pwrrail(
   3175 		    (uint8_t)(PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PWRRAIL))));
   3176 
   3177 	reg = regs[o2i(extcapoff + PCI_PWRBDGT_CAP)];
   3178 	printf("    Power Budget Capability register: 0x%08x\n", reg);
   3179 	onoff("System Allocated",
   3180 	    reg, PCI_PWRBDGT_CAP_SYSALLOC);
   3181 }
   3182 
   3183 static const char *
   3184 pci_conf_print_rclink_dcl_cap_elmtype(unsigned char type)
   3185 {
   3186 
   3187 	switch (type) {
   3188 	case 0x00:
   3189 		return "Configuration Space Element";
   3190 	case 0x01:
   3191 		return "System Egress Port or internal sink (memory)";
   3192 	case 0x02:
   3193 		return "Internal Root Complex Link";
   3194 	default:
   3195 		return "Unknown";
   3196 	}
   3197 }
   3198 
   3199 static void
   3200 pci_conf_print_rclink_dcl_cap(const pcireg_t *regs, int extcapoff)
   3201 {
   3202 	pcireg_t reg;
   3203 	unsigned char nent, linktype;
   3204 	int i;
   3205 
   3206 	printf("\n  Root Complex Link Declaration\n");
   3207 
   3208 	reg = regs[o2i(extcapoff + PCI_RCLINK_DCL_ESDESC)];
   3209 	printf("    Element Self Description Register: 0x%08x\n", reg);
   3210 	printf("      Element Type: %s\n",
   3211 	    pci_conf_print_rclink_dcl_cap_elmtype((unsigned char)reg));
   3212 	nent = PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_NUMLINKENT);
   3213 	printf("      Number of Link Entries: %hhu\n", nent);
   3214 	printf("      Component ID: %hhu\n",
   3215 	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_COMPID));
   3216 	printf("      Port Number: %hhu\n",
   3217 	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_PORTNUM));
   3218 	for (i = 0; i < nent; i++) {
   3219 		reg = regs[o2i(extcapoff + PCI_RCLINK_DCL_LINKDESC(i))];
   3220 		printf("    Link Entry %d:\n", i + 1);
   3221 		printf("      Link Description Register: 0x%08x\n", reg);
   3222 		onoff("  Link Valid", reg, PCI_RCLINK_DCL_LINKDESC_LVALID);
   3223 		linktype = reg & PCI_RCLINK_DCL_LINKDESC_LTYPE;
   3224 		onoff2("  Link Type", reg, PCI_RCLINK_DCL_LINKDESC_LTYPE,
   3225 		    "Configuration Space", "Memory-Mapped Space");
   3226 		onoff("  Associated RCRB Header", reg,
   3227 		    PCI_RCLINK_DCL_LINKDESC_ARCRBH);
   3228 		printf("        Target Component ID: %hhu\n",
   3229 		    (uint8_t)PCIREG_SHIFTOUT(reg,
   3230 		    PCI_RCLINK_DCL_LINKDESC_TCOMPID));
   3231 		printf("        Target Port Number: %hhu\n",
   3232 		    (uint8_t)PCIREG_SHIFTOUT(reg,
   3233 		    PCI_RCLINK_DCL_LINKDESC_TPNUM));
   3234 
   3235 		if (linktype == 0) {
   3236 			/* Memory-Mapped Space */
   3237 			reg = regs[o2i(extcapoff
   3238 				    + PCI_RCLINK_DCL_LINKADDR_LT0_LO(i))];
   3239 			printf("      Link Address Low Register: 0x%08x\n",
   3240 			    reg);
   3241 			reg = regs[o2i(extcapoff
   3242 				    + PCI_RCLINK_DCL_LINKADDR_LT0_HI(i))];
   3243 			printf("      Link Address High Register: 0x%08x\n",
   3244 			    reg);
   3245 		} else {
   3246 			unsigned int nb;
   3247 			pcireg_t lo, hi;
   3248 
   3249 			/* Configuration Space */
   3250 			lo = regs[o2i(extcapoff
   3251 				    + PCI_RCLINK_DCL_LINKADDR_LT1_LO(i))];
   3252 			printf("      Configuration Space Low Register: "
   3253 			    "0x%08x\n", lo);
   3254 			hi = regs[o2i(extcapoff
   3255 				    + PCI_RCLINK_DCL_LINKADDR_LT1_HI(i))];
   3256 			printf("      Configuration Space High Register: "
   3257 			    "0x%08x\n", hi);
   3258 			nb = PCIREG_SHIFTOUT(lo, PCI_RCLINK_DCL_LINKADDR_LT1_N);
   3259 			printf("        N: %u\n", nb);
   3260 			printf("        Func: %hhu\n",
   3261 			    (uint8_t)PCIREG_SHIFTOUT(lo,
   3262 			    PCI_RCLINK_DCL_LINKADDR_LT1_FUNC));
   3263 			printf("        Dev: %hhu\n",
   3264 			    (uint8_t)PCIREG_SHIFTOUT(lo,
   3265 			    PCI_RCLINK_DCL_LINKADDR_LT1_DEV));
   3266 			printf("        Bus: %hhu\n",
   3267 			    (uint8_t)PCIREG_SHIFTOUT(lo,
   3268 			    PCI_RCLINK_DCL_LINKADDR_LT1_BUS(nb)));
   3269 			lo &= PCI_RCLINK_DCL_LINKADDR_LT1_BAL(i);
   3270 			printf("        Configuration Space Base Address: "
   3271 			    "0x%016" PRIx64 "\n", ((uint64_t)hi << 32) + lo);
   3272 		}
   3273 	}
   3274 }
   3275 
   3276 /* XXX pci_conf_print_rclink_ctl_cap */
   3277 
   3278 static void
   3279 pci_conf_print_rcec_assoc_cap(const pcireg_t *regs, int extcapoff)
   3280 {
   3281 	pcireg_t reg;
   3282 
   3283 	printf("\n  Root Complex Event Collector Association\n");
   3284 
   3285 	reg = regs[o2i(extcapoff + PCI_RCEC_ASSOC_ASSOCBITMAP)];
   3286 	printf("    Association Bitmap for Root Complex Integrated Devices:"
   3287 	    " 0x%08x\n", reg);
   3288 
   3289 	if (PCI_EXTCAPLIST_VERSION(regs[o2i(extcapoff)]) >= 2) {
   3290 		reg = regs[o2i(extcapoff + PCI_RCEC_ASSOC_ASSOCBUSNUM)];
   3291 		printf("    RCEC Associated Bus Numbers register: 0x%08x\n",
   3292 		    reg);
   3293 		printf("      RCEC Next Bus: %u\n",
   3294 		    PCIREG_SHIFTOUT(reg,
   3295 			PCI_RCEC_ASSOCBUSNUM_RCECNEXT));
   3296 		printf("      RCEC Last Bus: %u\n",
   3297 		    PCIREG_SHIFTOUT(reg,
   3298 			PCI_RCEC_ASSOCBUSNUM_RCECLAST));
   3299 	}
   3300 }
   3301 
   3302 /* XXX pci_conf_print_mfvc_cap */
   3303 /* XXX pci_conf_print_vc2_cap */
   3304 /* XXX pci_conf_print_rcrb_cap */
   3305 /* XXX pci_conf_print_vendor_cap */
   3306 /* XXX pci_conf_print_cac_cap */
   3307 
   3308 static void
   3309 pci_conf_print_acs_cap(const pcireg_t *regs, int extcapoff)
   3310 {
   3311 	pcireg_t reg, cap, ctl;
   3312 	unsigned int size, i;
   3313 
   3314 	printf("\n  Access Control Services\n");
   3315 
   3316 	reg = regs[o2i(extcapoff + PCI_ACS_CAP)];
   3317 	cap = reg & 0xffff;
   3318 	ctl = reg >> 16;
   3319 	printf("    ACS Capability register: 0x%08x\n", cap);
   3320 	onoff("ACS Source Validation", cap, PCI_ACS_CAP_V);
   3321 	onoff("ACS Transaction Blocking", cap, PCI_ACS_CAP_B);
   3322 	onoff("ACS P2P Request Redirect", cap, PCI_ACS_CAP_R);
   3323 	onoff("ACS P2P Completion Redirect", cap, PCI_ACS_CAP_C);
   3324 	onoff("ACS Upstream Forwarding", cap, PCI_ACS_CAP_U);
   3325 	onoff("ACS Egress Control", cap, PCI_ACS_CAP_E);
   3326 	onoff("ACS Direct Translated P2P", cap, PCI_ACS_CAP_T);
   3327 	size = PCIREG_SHIFTOUT(cap, PCI_ACS_CAP_ECVSIZE);
   3328 	if (size == 0)
   3329 		size = 256;
   3330 	printf("      Egress Control Vector Size: %u\n", size);
   3331 	printf("    ACS Control register: 0x%08x\n", ctl);
   3332 	onoff("ACS Source Validation Enable", ctl, PCI_ACS_CTL_V);
   3333 	onoff("ACS Transaction Blocking Enable", ctl, PCI_ACS_CTL_B);
   3334 	onoff("ACS P2P Request Redirect Enable", ctl, PCI_ACS_CTL_R);
   3335 	onoff("ACS P2P Completion Redirect Enable", ctl, PCI_ACS_CTL_C);
   3336 	onoff("ACS Upstream Forwarding Enable", ctl, PCI_ACS_CTL_U);
   3337 	onoff("ACS Egress Control Enable", ctl, PCI_ACS_CTL_E);
   3338 	onoff("ACS Direct Translated P2P Enable", ctl, PCI_ACS_CTL_T);
   3339 
   3340 	/*
   3341 	 * If the P2P Egress Control Capability bit is 0, ignore the Egress
   3342 	 * Control vector.
   3343 	 */
   3344 	if ((cap & PCI_ACS_CAP_E) == 0)
   3345 		return;
   3346 	for (i = 0; i < size; i += 32)
   3347 		printf("    Egress Control Vector [%u..%u]: 0x%08x\n", i + 31,
   3348 		    i, regs[o2i(extcapoff + PCI_ACS_ECV + (i / 32) * 4 )]);
   3349 }
   3350 
   3351 static void
   3352 pci_conf_print_ari_cap(const pcireg_t *regs, int extcapoff)
   3353 {
   3354 	pcireg_t reg, cap, ctl;
   3355 
   3356 	printf("\n  Alternative Routing-ID Interpretation Register\n");
   3357 
   3358 	reg = regs[o2i(extcapoff + PCI_ARI_CAP)];
   3359 	cap = reg & 0xffff;
   3360 	ctl = reg >> 16;
   3361 	printf("    Capability register: 0x%08x\n", cap);
   3362 	onoff("MVFC Function Groups Capability", reg, PCI_ARI_CAP_M);
   3363 	onoff("ACS Function Groups Capability", reg, PCI_ARI_CAP_A);
   3364 	printf("      Next Function Number: %u\n",
   3365 	    PCIREG_SHIFTOUT(reg, PCI_ARI_CAP_NXTFN));
   3366 	printf("    Control register: 0x%08x\n", ctl);
   3367 	onoff("MVFC Function Groups Enable", reg, PCI_ARI_CTL_M);
   3368 	onoff("ACS Function Groups Enable", reg, PCI_ARI_CTL_A);
   3369 	printf("      Function Group: %u\n",
   3370 	    PCIREG_SHIFTOUT(reg, PCI_ARI_CTL_FUNCGRP));
   3371 }
   3372 
   3373 static void
   3374 pci_conf_print_ats_cap(const pcireg_t *regs, int extcapoff)
   3375 {
   3376 	pcireg_t reg, cap, ctl;
   3377 	unsigned int num;
   3378 
   3379 	printf("\n  Address Translation Services\n");
   3380 
   3381 	reg = regs[o2i(extcapoff + PCI_ARI_CAP)];
   3382 	cap = reg & 0xffff;
   3383 	ctl = reg >> 16;
   3384 	printf("    Capability register: 0x%04x\n", cap);
   3385 	num = PCIREG_SHIFTOUT(reg, PCI_ATS_CAP_INVQDEPTH);
   3386 	if (num == 0)
   3387 		num = 32;
   3388 	printf("      Invalidate Queue Depth: %u\n", num);
   3389 	onoff("Page Aligned Request", reg, PCI_ATS_CAP_PALIGNREQ);
   3390 	onoff("Global Invalidate", reg, PCI_ATS_CAP_GLOBALINVL);
   3391 	onoff("Relaxed Ordering", reg, PCI_ATS_CAP_RELAXORD);
   3392 
   3393 	printf("    Control register: 0x%04x\n", ctl);
   3394 	printf("      Smallest Translation Unit: %u\n",
   3395 	    PCIREG_SHIFTOUT(reg, PCI_ATS_CTL_STU));
   3396 	onoff("Enable", reg, PCI_ATS_CTL_EN);
   3397 }
   3398 
   3399 static void
   3400 pci_conf_print_sernum_cap(const pcireg_t *regs, int extcapoff)
   3401 {
   3402 	pcireg_t lo, hi;
   3403 
   3404 	printf("\n  Device Serial Number Register\n");
   3405 
   3406 	lo = regs[o2i(extcapoff + PCI_SERIAL_LOW)];
   3407 	hi = regs[o2i(extcapoff + PCI_SERIAL_HIGH)];
   3408 	printf("    Serial Number: %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
   3409 	    hi >> 24, (hi >> 16) & 0xff, (hi >> 8) & 0xff, hi & 0xff,
   3410 	    lo >> 24, (lo >> 16) & 0xff, (lo >> 8) & 0xff, lo & 0xff);
   3411 }
   3412 
   3413 static void
   3414 pci_conf_print_sriov_cap(const pcireg_t *regs, int extcapoff)
   3415 {
   3416 	char buf[sizeof("99999 MB")];
   3417 	pcireg_t reg;
   3418 	pcireg_t total_vfs;
   3419 	int i;
   3420 	bool first;
   3421 
   3422 	printf("\n  Single Root IO Virtualization Register\n");
   3423 
   3424 	reg = regs[o2i(extcapoff + PCI_SRIOV_CAP)];
   3425 	printf("    Capabilities register: 0x%08x\n", reg);
   3426 	onoff("VF Migration Capable", reg, PCI_SRIOV_CAP_VF_MIGRATION);
   3427 	onoff("ARI Capable Hierarchy Preserved", reg,
   3428 	    PCI_SRIOV_CAP_ARI_CAP_HIER_PRESERVED);
   3429 	if (reg & PCI_SRIOV_CAP_VF_MIGRATION) {
   3430 		printf("      VF Migration Interrupt Message Number: 0x%03x\n",
   3431 		    PCIREG_SHIFTOUT(reg, PCI_SRIOV_CAP_VF_MIGRATION_INTMSG_N));
   3432 	}
   3433 
   3434 	reg = regs[o2i(extcapoff + PCI_SRIOV_CTL)] & 0xffff;
   3435 	printf("    Control register: 0x%04x\n", reg);
   3436 	onoff("VF Enable", reg, PCI_SRIOV_CTL_VF_ENABLE);
   3437 	onoff("VF Migration Enable", reg, PCI_SRIOV_CTL_VF_MIGRATION_SUPPORT);
   3438 	onoff("VF Migration Interrupt Enable", reg,
   3439 	    PCI_SRIOV_CTL_VF_MIGRATION_INT_ENABLE);
   3440 	onoff("VF Memory Space Enable", reg, PCI_SRIOV_CTL_VF_MSE);
   3441 	onoff("ARI Capable Hierarchy", reg, PCI_SRIOV_CTL_ARI_CAP_HIER);
   3442 
   3443 	reg = regs[o2i(extcapoff + PCI_SRIOV_STA)] >> 16;
   3444 	printf("    Status register: 0x%04x\n", reg);
   3445 	onoff("VF Migration Status", reg, PCI_SRIOV_STA_VF_MIGRATION);
   3446 
   3447 	reg = regs[o2i(extcapoff + PCI_SRIOV_INITIAL_VFS)] & 0xffff;
   3448 	printf("    InitialVFs register: 0x%04x\n", reg);
   3449 	total_vfs = reg = regs[o2i(extcapoff + PCI_SRIOV_TOTAL_VFS)] >> 16;
   3450 	printf("    TotalVFs register: 0x%04x\n", reg);
   3451 	reg = regs[o2i(extcapoff + PCI_SRIOV_NUM_VFS)] & 0xffff;
   3452 	printf("    NumVFs register: 0x%04x\n", reg);
   3453 
   3454 	reg = regs[o2i(extcapoff + PCI_SRIOV_FUNC_DEP_LINK)] >> 16;
   3455 	printf("    Function Dependency Link register: 0x%04x\n", reg);
   3456 
   3457 	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_OFF)] & 0xffff;
   3458 	printf("    First VF Offset register: 0x%04x\n", reg);
   3459 	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_STRIDE)] >> 16;
   3460 	printf("    VF Stride register: 0x%04x\n", reg);
   3461 	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_DID)] >> 16;
   3462 	printf("    Device ID: 0x%04x\n", reg);
   3463 
   3464 	reg = regs[o2i(extcapoff + PCI_SRIOV_PAGE_CAP)];
   3465 	printf("    Supported Page Sizes register: 0x%08x\n", reg);
   3466 	printf("      Supported Page Size:");
   3467 	for (i = 0, first = true; i < 32; i++) {
   3468 		if (reg & __BIT(i)) {
   3469 #ifdef _KERNEL
   3470 			format_bytes(buf, sizeof(buf), 1LL << (i + 12));
   3471 #else
   3472 			humanize_number(buf, sizeof(buf), 1LL << (i + 12), "B",
   3473 			    HN_AUTOSCALE, 0);
   3474 #endif
   3475 			printf("%s %s", first ? "" : ",", buf);
   3476 			first = false;
   3477 		}
   3478 	}
   3479 	printf("\n");
   3480 
   3481 	reg = regs[o2i(extcapoff + PCI_SRIOV_PAGE_SIZE)];
   3482 	printf("    System Page Sizes register: 0x%08x\n", reg);
   3483 	printf("      Page Size: ");
   3484 	if (reg != 0) {
   3485 		int bitpos = ffs(reg) -1;
   3486 
   3487 		/* Assume only one bit is set. */
   3488 #ifdef _KERNEL
   3489 		format_bytes(buf, sizeof(buf), 1LL << (bitpos + 12));
   3490 #else
   3491 		humanize_number(buf, sizeof(buf), 1LL << (bitpos + 12),
   3492 		    "B", HN_AUTOSCALE, 0);
   3493 #endif
   3494 		printf("%s", buf);
   3495 	} else {
   3496 		printf("unknown");
   3497 	}
   3498 	printf("\n");
   3499 
   3500 	for (i = 0; i < 6; i++) {
   3501 		reg = regs[o2i(extcapoff + PCI_SRIOV_BAR(i))];
   3502 		printf("    VF BAR%d register: 0x%08x\n", i, reg);
   3503 	}
   3504 
   3505 	if (total_vfs > 0) {
   3506 		reg = regs[o2i(extcapoff + PCI_SRIOV_VF_MIG_STA_AR)];
   3507 		printf("    VF Migration State Array Offset register: 0x%08x\n",
   3508 		    reg);
   3509 		printf("      VF Migration State Offset: 0x%08x\n",
   3510 		    PCIREG_SHIFTOUT(reg, PCI_SRIOV_VF_MIG_STA_OFFSET));
   3511 		i = PCIREG_SHIFTOUT(reg, PCI_SRIOV_VF_MIG_STA_BIR);
   3512 		printf("      VF Migration State BIR: ");
   3513 		if (i >= 0 && i <= 5) {
   3514 			printf("BAR%d", i);
   3515 		} else {
   3516 			printf("unknown BAR (%d)", i);
   3517 		}
   3518 		printf("\n");
   3519 	}
   3520 }
   3521 
   3522 /* XXX pci_conf_print_mriov_cap */
   3523 
   3524 static void
   3525 pci_conf_print_multicast_cap(const pcireg_t *regs, int extcapoff)
   3526 {
   3527 	pcireg_t reg, cap, ctl;
   3528 	pcireg_t regl, regh;
   3529 	uint64_t addr;
   3530 	int n;
   3531 
   3532 	printf("\n  Multicast\n");
   3533 
   3534 	reg = regs[o2i(extcapoff + PCI_MCAST_CTL)];
   3535 	cap = reg & 0xffff;
   3536 	ctl = reg >> 16;
   3537 	printf("    Capability Register: 0x%04x\n", cap);
   3538 	printf("      Max Group: %u\n",
   3539 	    (pcireg_t)(reg & PCI_MCAST_CAP_MAXGRP) + 1);
   3540 
   3541 	/* Endpoint Only */
   3542 	n = PCIREG_SHIFTOUT(reg, PCI_MCAST_CAP_WINSIZEREQ);
   3543 	if (n > 0)
   3544 		printf("      Window Size Requested: %d\n", 1 << (n - 1));
   3545 
   3546 	onoff("ECRC Regeneration Supported", reg, PCI_MCAST_CAP_ECRCREGEN);
   3547 
   3548 	printf("    Control Register: 0x%04x\n", ctl);
   3549 	printf("      Num Group: %u\n",
   3550 	    PCIREG_SHIFTOUT(reg, PCI_MCAST_CTL_NUMGRP) + 1);
   3551 	onoff("Enable", reg, PCI_MCAST_CTL_ENA);
   3552 
   3553 	regl = regs[o2i(extcapoff + PCI_MCAST_BARL)];
   3554 	regh = regs[o2i(extcapoff + PCI_MCAST_BARH)];
   3555 	printf("    Base Address Register 0: 0x%08x\n", regl);
   3556 	printf("    Base Address Register 1: 0x%08x\n", regh);
   3557 	printf("      Index Position: %u\n",
   3558 	    (unsigned int)(regl & PCI_MCAST_BARL_INDPOS));
   3559 	addr = ((uint64_t)regh << 32) | (regl & PCI_MCAST_BARL_ADDR);
   3560 	printf("      Base Address: 0x%016" PRIx64 "\n", addr);
   3561 
   3562 	regl = regs[o2i(extcapoff + PCI_MCAST_RECVL)];
   3563 	regh = regs[o2i(extcapoff + PCI_MCAST_RECVH)];
   3564 	printf("    Receive Register 0: 0x%08x\n", regl);
   3565 	printf("    Receive Register 1: 0x%08x\n", regh);
   3566 
   3567 	regl = regs[o2i(extcapoff + PCI_MCAST_BLOCKALLL)];
   3568 	regh = regs[o2i(extcapoff + PCI_MCAST_BLOCKALLH)];
   3569 	printf("    Block All Register 0: 0x%08x\n", regl);
   3570 	printf("    Block All Register 1: 0x%08x\n", regh);
   3571 
   3572 	regl = regs[o2i(extcapoff + PCI_MCAST_BLOCKUNTRNSL)];
   3573 	regh = regs[o2i(extcapoff + PCI_MCAST_BLOCKUNTRNSH)];
   3574 	printf("    Block Untranslated Register 0: 0x%08x\n", regl);
   3575 	printf("    Block Untranslated Register 1: 0x%08x\n", regh);
   3576 
   3577 	regl = regs[o2i(extcapoff + PCI_MCAST_OVERLAYL)];
   3578 	regh = regs[o2i(extcapoff + PCI_MCAST_OVERLAYH)];
   3579 	printf("    Overlay BAR 0: 0x%08x\n", regl);
   3580 	printf("    Overlay BAR 1: 0x%08x\n", regh);
   3581 
   3582 	n = regl & PCI_MCAST_OVERLAYL_SIZE;
   3583 	printf("      Overlay Size: ");
   3584 	if (n >= 6)
   3585 		printf("%d\n", n);
   3586 	else
   3587 		printf("off\n");
   3588 	addr = ((uint64_t)regh << 32) | (regl & PCI_MCAST_OVERLAYL_ADDR);
   3589 	printf("      Overlay BAR: 0x%016" PRIx64 "\n", addr);
   3590 }
   3591 
   3592 static void
   3593 pci_conf_print_page_req_cap(const pcireg_t *regs, int extcapoff)
   3594 {
   3595 	pcireg_t reg, ctl, sta;
   3596 
   3597 	printf("\n  Page Request\n");
   3598 
   3599 	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_CTL)];
   3600 	ctl = reg & 0xffff;
   3601 	sta = reg >> 16;
   3602 	printf("    Control Register: 0x%04x\n", ctl);
   3603 	onoff("Enable", reg, PCI_PAGE_REQ_CTL_E);
   3604 	onoff("Reset", reg, PCI_PAGE_REQ_CTL_R);
   3605 
   3606 	printf("    Status Register: 0x%04x\n", sta);
   3607 	onoff("Response Failure", reg, PCI_PAGE_REQ_STA_RF);
   3608 	onoff("Unexpected Page Request Group Index", reg,
   3609 	    PCI_PAGE_REQ_STA_UPRGI);
   3610 	onoff("Stopped", reg, PCI_PAGE_REQ_STA_S);
   3611 	onoff("PRG Response PASID Required", reg, PCI_PAGE_REQ_STA_PASIDR);
   3612 
   3613 	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_OUTSTCAPA)];
   3614 	printf("    Outstanding Page Request Capacity: %u\n", reg);
   3615 	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_OUTSTALLOC)];
   3616 	printf("    Outstanding Page Request Allocation: %u\n", reg);
   3617 }
   3618 
   3619 /* XXX pci_conf_print_amd_cap */
   3620 
   3621 #define MEM_PBUFSIZE	sizeof("999GB")
   3622 
   3623 static void
   3624 pci_conf_print_resizbar_cap(const pcireg_t *regs, int extcapoff)
   3625 {
   3626 	pcireg_t cap, ctl;
   3627 	unsigned int bars, i, n;
   3628 	char pbuf[MEM_PBUFSIZE];
   3629 
   3630 	printf("\n  Resizable BAR\n");
   3631 
   3632 	/* Get Number of Resizable BARs */
   3633 	ctl = regs[o2i(extcapoff + PCI_RESIZBAR_CTL(0))];
   3634 	bars = PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_NUMBAR);
   3635 	printf("    Number of Resizable BARs: ");
   3636 	if (bars <= 6)
   3637 		printf("%u\n", bars);
   3638 	else {
   3639 		printf("incorrect (%u)\n", bars);
   3640 		return;
   3641 	}
   3642 
   3643 	for (n = 0; n < 6; n++) {
   3644 		cap = regs[o2i(extcapoff + PCI_RESIZBAR_CAP(n))];
   3645 		printf("    Capability register(%u): 0x%08x\n", n, cap);
   3646 		if ((cap & PCI_RESIZBAR_CAP_SIZEMASK) == 0)
   3647 			continue; /* Not Used */
   3648 		printf("      Acceptable BAR sizes:");
   3649 		for (i = 4; i <= 23; i++) {
   3650 			if ((cap & (1 << i)) != 0) {
   3651 				humanize_number(pbuf, MEM_PBUFSIZE,
   3652 				    (int64_t)1024 * 1024 << (i - 4), "B",
   3653 #ifdef _KERNEL
   3654 				    1);
   3655 #else
   3656 				    HN_AUTOSCALE, HN_NOSPACE);
   3657 #endif
   3658 				printf(" %s", pbuf);
   3659 			}
   3660 		}
   3661 		printf("\n");
   3662 
   3663 		ctl = regs[o2i(extcapoff + PCI_RESIZBAR_CTL(n))];
   3664 		printf("    Control register(%u): 0x%08x\n", n, ctl);
   3665 		printf("      BAR Index: %u\n",
   3666 		    PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_BARIDX));
   3667 		humanize_number(pbuf, MEM_PBUFSIZE,
   3668 		    (int64_t)1024 * 1024
   3669 		    << PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_BARSIZ),
   3670 		    "B",
   3671 #ifdef _KERNEL
   3672 		    1);
   3673 #else
   3674 		    HN_AUTOSCALE, HN_NOSPACE);
   3675 #endif
   3676 		printf("      BAR Size: %s\n", pbuf);
   3677 	}
   3678 }
   3679 
   3680 static void
   3681 pci_conf_print_dpa_cap(const pcireg_t *regs, int extcapoff)
   3682 {
   3683 	pcireg_t reg;
   3684 	unsigned int substmax, i;
   3685 
   3686 	printf("\n  Dynamic Power Allocation\n");
   3687 
   3688 	reg = regs[o2i(extcapoff + PCI_DPA_CAP)];
   3689 	printf("    Capability register: 0x%08x\n", reg);
   3690 	substmax = PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_SUBSTMAX);
   3691 	printf("      Substate Max: %u\n", substmax);
   3692 	printf("      Transition Latency Unit: ");
   3693 	switch (PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_TLUINT)) {
   3694 	case 0:
   3695 		printf("1ms\n");
   3696 		break;
   3697 	case 1:
   3698 		printf("10ms\n");
   3699 		break;
   3700 	case 2:
   3701 		printf("100ms\n");
   3702 		break;
   3703 	default:
   3704 		printf("reserved\n");
   3705 		break;
   3706 	}
   3707 	printf("      Power Allocation Scale: ");
   3708 	switch (PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_PAS)) {
   3709 	case 0:
   3710 		printf("10.0x\n");
   3711 		break;
   3712 	case 1:
   3713 		printf("1.0x\n");
   3714 		break;
   3715 	case 2:
   3716 		printf("0.1x\n");
   3717 		break;
   3718 	case 3:
   3719 		printf("0.01x\n");
   3720 		break;
   3721 	}
   3722 	printf("      Transition Latency Value 0: %u\n",
   3723 	    PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_XLCY0));
   3724 	printf("      Transition Latency Value 1: %u\n",
   3725 	    PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_XLCY1));
   3726 
   3727 	reg = regs[o2i(extcapoff + PCI_DPA_LATIND)];
   3728 	printf("    Latency Indicator register: 0x%08x\n", reg);
   3729 
   3730 	reg = regs[o2i(extcapoff + PCI_DPA_CS)];
   3731 	printf("    Status register: 0x%04x\n", reg & 0xffff);
   3732 	printf("      Substate Status: 0x%02x\n",
   3733 	    PCIREG_SHIFTOUT(reg, PCI_DPA_CS_SUBSTSTAT));
   3734 	onoff("Substate Control Enabled", reg, PCI_DPA_CS_SUBSTCTLEN);
   3735 	printf("    Control register: 0x%04x\n", reg >> 16);
   3736 	printf("      Substate Control: 0x%02x\n",
   3737 	    PCIREG_SHIFTOUT(reg, PCI_DPA_CS_SUBSTCTL));
   3738 
   3739 	for (i = 0; i <= substmax; i++)
   3740 		printf("    Substate Power Allocation register %d: 0x%02x\n",
   3741 		    i, (regs[PCI_DPA_PWRALLOC + (i / 4)] >> (i % 4) & 0xff));
   3742 }
   3743 
   3744 static const char *
   3745 pci_conf_print_tph_req_cap_sttabloc(uint8_t val)
   3746 {
   3747 
   3748 	switch (val) {
   3749 	case PCI_TPH_REQ_STTBLLOC_NONE:
   3750 		return "Not Present";
   3751 	case PCI_TPH_REQ_STTBLLOC_TPHREQ:
   3752 		return "in the TPH Requester Capability Structure";
   3753 	case PCI_TPH_REQ_STTBLLOC_MSIX:
   3754 		return "in the MSI-X Table";
   3755 	default:
   3756 		return "Unknown";
   3757 	}
   3758 }
   3759 
   3760 static void
   3761 pci_conf_print_tph_req_cap(const pcireg_t *regs, int extcapoff)
   3762 {
   3763 	pcireg_t reg;
   3764 	int size = 0, i, j;
   3765 	uint8_t sttbloc;
   3766 
   3767 	printf("\n  TPH Requester Extended Capability\n");
   3768 
   3769 	reg = regs[o2i(extcapoff + PCI_TPH_REQ_CAP)];
   3770 	printf("    TPH Requester Capabililty register: 0x%08x\n", reg);
   3771 	onoff("No ST Mode Supported", reg, PCI_TPH_REQ_CAP_NOST);
   3772 	onoff("Interrupt Vector Mode Supported", reg, PCI_TPH_REQ_CAP_INTVEC);
   3773 	onoff("Device Specific Mode Supported", reg, PCI_TPH_REQ_CAP_DEVSPEC);
   3774 	onoff("Extend TPH Requester Supported", reg, PCI_TPH_REQ_CAP_XTPHREQ);
   3775 	sttbloc = PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CAP_STTBLLOC);
   3776 	printf("      ST Table Location: %s\n",
   3777 	    pci_conf_print_tph_req_cap_sttabloc(sttbloc));
   3778 	if (sttbloc == PCI_TPH_REQ_STTBLLOC_TPHREQ) {
   3779 		size = PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CAP_STTBLSIZ) + 1;
   3780 		printf("      ST Table Size: %d\n", size);
   3781 	}
   3782 
   3783 	reg = regs[o2i(extcapoff + PCI_TPH_REQ_CTL)];
   3784 	printf("    TPH Requester Control register: 0x%08x\n", reg);
   3785 	printf("      ST Mode Select: ");
   3786 	switch (PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CTL_STSEL)) {
   3787 	case PCI_TPH_REQ_CTL_STSEL_NO:
   3788 		printf("No ST Mode\n");
   3789 		break;
   3790 	case PCI_TPH_REQ_CTL_STSEL_IV:
   3791 		printf("Interrupt Vector Mode\n");
   3792 		break;
   3793 	case PCI_TPH_REQ_CTL_STSEL_DS:
   3794 		printf("Device Specific Mode\n");
   3795 		break;
   3796 	default:
   3797 		printf("(reserved value)\n");
   3798 		break;
   3799 	}
   3800 	printf("      TPH Requester Enable: ");
   3801 	switch (PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CTL_TPHREQEN)) {
   3802 	case PCI_TPH_REQ_CTL_TPHREQEN_NO: /* 0x0 */
   3803 		printf("Not permitted\n");
   3804 		break;
   3805 	case PCI_TPH_REQ_CTL_TPHREQEN_TPH:
   3806 		printf("TPH and not Extended TPH\n");
   3807 		break;
   3808 	case PCI_TPH_REQ_CTL_TPHREQEN_ETPH:
   3809 		printf("TPH and Extended TPH");
   3810 		break;
   3811 	default:
   3812 		printf("(reserved value)\n");
   3813 		break;
   3814 	}
   3815 
   3816 	if (sttbloc != PCI_TPH_REQ_STTBLLOC_TPHREQ)
   3817 		return;
   3818 
   3819 	for (i = 0; i < size ; i += 2) {
   3820 		reg = regs[o2i(extcapoff + PCI_TPH_REQ_STTBL + i / 2)];
   3821 		for (j = 0; j < 2 ; j++) {
   3822 			uint32_t entry = reg;
   3823 
   3824 			if (j != 0)
   3825 				entry >>= 16;
   3826 			entry &= 0xffff;
   3827 			printf("    TPH ST Table Entry (%d): 0x%04"PRIx32"\n",
   3828 			    i + j, entry);
   3829 		}
   3830 	}
   3831 }
   3832 
   3833 static void
   3834 pci_conf_print_ltr_cap(const pcireg_t *regs, int extcapoff)
   3835 {
   3836 	pcireg_t reg;
   3837 
   3838 	printf("\n  Latency Tolerance Reporting\n");
   3839 	reg = regs[o2i(extcapoff + PCI_LTR_MAXSNOOPLAT)];
   3840 	printf("    Max Snoop Latency Register: 0x%04x\n", reg & 0xffff);
   3841 	printf("      Max Snoop Latency: %juns\n",
   3842 	    (uintmax_t)(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXSNOOPLAT_VAL)
   3843 	    * PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXSNOOPLAT_SCALE))));
   3844 	printf("    Max No-Snoop Latency Register: 0x%04x\n", reg >> 16);
   3845 	printf("      Max No-Snoop Latency: %juns\n",
   3846 	    (uintmax_t)(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXNOSNOOPLAT_VAL)
   3847 	    * PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXNOSNOOPLAT_SCALE))));
   3848 }
   3849 
   3850 static void
   3851 pci_conf_print_sec_pcie_cap(const pcireg_t *regs, int extcapoff)
   3852 {
   3853 	int pcie_capoff;
   3854 	pcireg_t reg;
   3855 	int i, maxlinkwidth;
   3856 
   3857 	printf("\n  Secondary PCI Express Register\n");
   3858 
   3859 	reg = regs[o2i(extcapoff + PCI_SECPCIE_LCTL3)];
   3860 	printf("    Link Control 3 register: 0x%08x\n", reg);
   3861 	onoff("Perform Equalization", reg, PCI_SECPCIE_LCTL3_PERFEQ);
   3862 	onoff("Link Equalization Request Interrupt Enable",
   3863 	    reg, PCI_SECPCIE_LCTL3_LINKEQREQ_IE);
   3864 	printf("      Enable Lower SKP OS Generation Vector:");
   3865 	pci_print_pcie_linkspeedvector(
   3866 		PCIREG_SHIFTOUT(reg, PCI_SECPCIE_LCTL3_ELSKPOSGENV));
   3867 	printf("\n");
   3868 
   3869 	reg = regs[o2i(extcapoff + PCI_SECPCIE_LANEERR_STA)];
   3870 	printf("    Lane Error Status register: 0x%08x\n", reg);
   3871 
   3872 	/* Get Max Link Width */
   3873 	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
   3874 		reg = regs[o2i(pcie_capoff + PCIE_LCAP)];
   3875 		maxlinkwidth = PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
   3876 	} else {
   3877 		printf("error: falied to get PCIe capablity\n");
   3878 		return;
   3879 	}
   3880 	for (i = 0; i < maxlinkwidth; i++) {
   3881 		reg = regs[o2i(extcapoff + PCI_SECPCIE_EQCTL(i))];
   3882 		if (i % 2 != 0)
   3883 			reg >>= 16;
   3884 		else
   3885 			reg &= 0xffff;
   3886 		printf("    Equalization Control Register (Link %d): 0x%04x\n",
   3887 		    i, reg);
   3888 		printf("      Downstream Port Transmit Preset: 0x%x\n",
   3889 		    PCIREG_SHIFTOUT(reg,
   3890 			PCI_SECPCIE_EQCTL_DP_XMIT_PRESET));
   3891 		printf("      Downstream Port Receive Hint: 0x%x\n",
   3892 		    PCIREG_SHIFTOUT(reg, PCI_SECPCIE_EQCTL_DP_RCV_HINT));
   3893 		printf("      Upstream Port Transmit Preset: 0x%x\n",
   3894 		    PCIREG_SHIFTOUT(reg,
   3895 			PCI_SECPCIE_EQCTL_UP_XMIT_PRESET));
   3896 		printf("      Upstream Port Receive Hint: 0x%x\n",
   3897 		    PCIREG_SHIFTOUT(reg, PCI_SECPCIE_EQCTL_UP_RCV_HINT));
   3898 	}
   3899 }
   3900 
   3901 /* XXX pci_conf_print_pmux_cap */
   3902 
   3903 static void
   3904 pci_conf_print_pasid_cap(const pcireg_t *regs, int extcapoff)
   3905 {
   3906 	pcireg_t reg, cap, ctl;
   3907 	unsigned int num;
   3908 
   3909 	printf("\n  Process Address Space ID\n");
   3910 
   3911 	reg = regs[o2i(extcapoff + PCI_PASID_CAP)];
   3912 	cap = reg & 0xffff;
   3913 	ctl = reg >> 16;
   3914 	printf("    PASID Capability Register: 0x%04x\n", cap);
   3915 	onoff("Execute Permission Supported", reg, PCI_PASID_CAP_XPERM);
   3916 	onoff("Privileged Mode Supported", reg, PCI_PASID_CAP_PRIVMODE);
   3917 	num = (1 << PCIREG_SHIFTOUT(reg, PCI_PASID_CAP_MAXPASIDW)) - 1;
   3918 	printf("      Max PASID Width: %u\n", num);
   3919 
   3920 	printf("    PASID Control Register: 0x%04x\n", ctl);
   3921 	onoff("PASID Enable", reg, PCI_PASID_CTL_PASID_EN);
   3922 	onoff("Execute Permission Enable", reg, PCI_PASID_CTL_XPERM_EN);
   3923 	onoff("Privileged Mode Enable", reg, PCI_PASID_CTL_PRIVMODE_EN);
   3924 }
   3925 
   3926 static void
   3927 pci_conf_print_lnr_cap(const pcireg_t *regs, int extcapoff)
   3928 {
   3929 	pcireg_t reg, cap, ctl;
   3930 	unsigned int num;
   3931 
   3932 	printf("\n  LN Requester\n");
   3933 
   3934 	reg = regs[o2i(extcapoff + PCI_LNR_CAP)];
   3935 	cap = reg & 0xffff;
   3936 	ctl = reg >> 16;
   3937 	printf("    LNR Capability register: 0x%04x\n", cap);
   3938 	onoff("LNR-64 Supported", reg, PCI_LNR_CAP_64);
   3939 	onoff("LNR-128 Supported", reg, PCI_LNR_CAP_128);
   3940 	num = 1 << PCIREG_SHIFTOUT(reg, PCI_LNR_CAP_REGISTMAX);
   3941 	printf("      LNR Registration MAX: %u\n", num);
   3942 
   3943 	printf("    LNR Control register: 0x%04x\n", ctl);
   3944 	onoff("LNR Enable", reg, PCI_LNR_CTL_EN);
   3945 	onoff("LNR CLS", reg, PCI_LNR_CTL_CLS);
   3946 	num = 1 << PCIREG_SHIFTOUT(reg, PCI_LNR_CTL_REGISTLIM);
   3947 	printf("      LNR Registration Limit: %u\n", num);
   3948 }
   3949 
   3950 static void
   3951 pci_conf_print_dpc_pio(pcireg_t r)
   3952 {
   3953 	onoff("Cfg Request received UR Completion", r,PCI_DPC_RPPIO_CFGUR_CPL);
   3954 	onoff("Cfg Request received CA Completion", r,PCI_DPC_RPPIO_CFGCA_CPL);
   3955 	onoff("Cfg Request Completion Timeout", r, PCI_DPC_RPPIO_CFG_CTO);
   3956 	onoff("I/O Request received UR Completion", r, PCI_DPC_RPPIO_IOUR_CPL);
   3957 	onoff("I/O Request received CA Completion", r, PCI_DPC_RPPIO_IOCA_CPL);
   3958 	onoff("I/O Request Completion Timeout", r, PCI_DPC_RPPIO_IO_CTO);
   3959 	onoff("Mem Request received UR Completion", r,PCI_DPC_RPPIO_MEMUR_CPL);
   3960 	onoff("Mem Request received CA Completion", r,PCI_DPC_RPPIO_MEMCA_CPL);
   3961 	onoff("Mem Request Completion Timeout", r, PCI_DPC_RPPIO_MEM_CTO);
   3962 }
   3963 
   3964 static void
   3965 pci_conf_print_dpc_cap(const pcireg_t *regs, int extcapoff)
   3966 {
   3967 	pcireg_t reg, cap, ctl, stat, errsrc;
   3968 	const char *trigstr;
   3969 	bool rpext;
   3970 
   3971 	printf("\n  Downstream Port Containment\n");
   3972 
   3973 	reg = regs[o2i(extcapoff + PCI_DPC_CCR)];
   3974 	cap = reg & 0xffff;
   3975 	ctl = reg >> 16;
   3976 	rpext = (reg & PCI_DPCCAP_RPEXT) ? true : false;
   3977 	printf("    DPC Capability register: 0x%04x\n", cap);
   3978 	printf("      DPC Interrupt Message Number: %02x\n",
   3979 	    (unsigned int)(cap & PCI_DPCCAP_IMSGN));
   3980 	onoff("RP Extensions for DPC", reg, PCI_DPCCAP_RPEXT);
   3981 	onoff("Poisoned TLP Egress Blocking Supported", reg,
   3982 	    PCI_DPCCAP_POISONTLPEB);
   3983 	onoff("DPC Software Triggering Supported", reg, PCI_DPCCAP_SWTRIG);
   3984 	printf("      RP PIO Log Size: %u\n",
   3985 	    PCIREG_SHIFTOUT(reg, PCI_DPCCAP_RPPIOLOGSZ));
   3986 	onoff("DL_Active ERR_COR Signaling Supported", reg,
   3987 	    PCI_DPCCAP_DLACTECORS);
   3988 	printf("    DPC Control register: 0x%04x\n", ctl);
   3989 	switch (PCIREG_SHIFTOUT(reg, PCI_DPCCTL_TIRGEN)) {
   3990 	case 0:
   3991 		trigstr = "disabled";
   3992 		break;
   3993 	case 1:
   3994 		trigstr = "enabled(ERR_FATAL)";
   3995 		break;
   3996 	case 2:
   3997 		trigstr = "enabled(ERR_NONFATAL or ERR_FATAL)";
   3998 		break;
   3999 	default:
   4000 		trigstr = "(reserverd)";
   4001 		break;
   4002 	}
   4003 	printf("      DPC Trigger Enable: %s\n", trigstr);
   4004 	printf("      DPC Completion Control: %s Completion Status\n",
   4005 	    (reg & PCI_DPCCTL_COMPCTL)
   4006 	    ? "Unsupported Request(UR)" : "Completer Abort(CA)");
   4007 	onoff("DPC Interrupt Enable", reg, PCI_DPCCTL_IE);
   4008 	onoff("DPC ERR_COR Enable", reg, PCI_DPCCTL_ERRCOREN);
   4009 	onoff("Poisoned TLP Egress Blocking Enable", reg,
   4010 	    PCI_DPCCTL_POISONTLPEB);
   4011 	onoff("DPC Software Trigger", reg, PCI_DPCCTL_SWTRIG);
   4012 	onoff("DL_Active ERR_COR Enable", reg, PCI_DPCCTL_DLACTECOR);
   4013 
   4014 	reg = regs[o2i(extcapoff + PCI_DPC_STATESID)];
   4015 	stat = reg & 0xffff;
   4016 	errsrc = reg >> 16;
   4017 	printf("    DPC Status register: 0x%04x\n", stat);
   4018 	onoff("DPC Trigger Status", reg, PCI_DPCSTAT_TSTAT);
   4019 	switch (PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_TREASON)) {
   4020 	case 0:
   4021 		trigstr = "an unmasked uncorrectable error";
   4022 		break;
   4023 	case 1:
   4024 		trigstr = "receiving an ERR_NONFATAL";
   4025 		break;
   4026 	case 2:
   4027 		trigstr = "receiving an ERR_FATAL";
   4028 		break;
   4029 	case 3:
   4030 		trigstr = "DPC Trigger Reason Extension field";
   4031 		break;
   4032 	}
   4033 	printf("      DPC Trigger Reason: Due to %s\n", trigstr);
   4034 	onoff("DPC Interrupt Status", reg, PCI_DPCSTAT_ISTAT);
   4035 	if (rpext)
   4036 		onoff("DPC RP Busy", reg, PCI_DPCSTAT_RPBUSY);
   4037 	switch (PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_TREASON)) {
   4038 	case 0:
   4039 		trigstr = "Due to RP PIO error";
   4040 		break;
   4041 	case 1:
   4042 		trigstr = "Due to the DPC Software trigger bit";
   4043 		break;
   4044 	default:
   4045 		trigstr = "(reserved)";
   4046 		break;
   4047 	}
   4048 	printf("      DPC Trigger Reason Extension: %s\n", trigstr);
   4049 	if (rpext)
   4050 		printf("      RP PIO First Error Pointer: 0x%02x\n",
   4051 		    PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_RPPIOFEP));
   4052 	printf("    DPC Error Source ID register: 0x%04x\n", errsrc);
   4053 
   4054 	if (!rpext)
   4055 		return;
   4056 	/*
   4057 	 * All of the following registers are implemented by a device which has
   4058 	 * RP Extensions for DPC
   4059 	 */
   4060 
   4061 	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_STAT)];
   4062 	printf("    RP PIO Status Register: 0x%08x\n", reg);
   4063 	pci_conf_print_dpc_pio(reg);
   4064 
   4065 	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_MASK)];
   4066 	printf("    RP PIO Mask Register: 0x%08x\n", reg);
   4067 	pci_conf_print_dpc_pio(reg);
   4068 
   4069 	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_SEVE)];
   4070 	printf("    RP PIO Severity Register: 0x%08x\n", reg);
   4071 	pci_conf_print_dpc_pio(reg);
   4072 
   4073 	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_SYSERR)];
   4074 	printf("    RP PIO SysError Register: 0x%08x\n", reg);
   4075 	pci_conf_print_dpc_pio(reg);
   4076 
   4077 	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_EXCPT)];
   4078 	printf("    RP PIO Exception Register: 0x%08x\n", reg);
   4079 	pci_conf_print_dpc_pio(reg);
   4080 
   4081 	printf("    RP PIO Header Log Register: start from 0x%03x\n",
   4082 	    extcapoff + PCI_DPC_RPPIO_HLOG);
   4083 	printf("    RP PIO ImpSpec Log Register: start from 0x%03x\n",
   4084 	    extcapoff + PCI_DPC_RPPIO_IMPSLOG);
   4085 	printf("    RP PIO TLP Prefix Log Register: start from 0x%03x\n",
   4086 	    extcapoff + PCI_DPC_RPPIO_TLPPLOG);
   4087 }
   4088 
   4089 
   4090 static int
   4091 pci_conf_l1pm_cap_tposcale(unsigned char scale)
   4092 {
   4093 
   4094 	/* Return scale in us */
   4095 	switch (scale) {
   4096 	case 0x0:
   4097 		return 2;
   4098 	case 0x1:
   4099 		return 10;
   4100 	case 0x2:
   4101 		return 100;
   4102 	default:
   4103 		return -1;
   4104 	}
   4105 }
   4106 
   4107 static void
   4108 pci_conf_print_l1pm_cap(const pcireg_t *regs, int extcapoff)
   4109 {
   4110 	pcireg_t reg;
   4111 	int scale, val;
   4112 	int pcie_capoff;
   4113 
   4114 	printf("\n  L1 PM Substates\n");
   4115 
   4116 	reg = regs[o2i(extcapoff + PCI_L1PM_CAP)];
   4117 	printf("    L1 PM Substates Capability register: 0x%08x\n", reg);
   4118 	onoff("PCI-PM L1.2 Supported", reg, PCI_L1PM_CAP_PCIPM12);
   4119 	onoff("PCI-PM L1.1 Supported", reg, PCI_L1PM_CAP_PCIPM11);
   4120 	onoff("ASPM L1.2 Supported", reg, PCI_L1PM_CAP_ASPM12);
   4121 	onoff("ASPM L1.1 Supported", reg, PCI_L1PM_CAP_ASPM11);
   4122 	onoff("L1 PM Substates Supported", reg, PCI_L1PM_CAP_L1PM);
   4123 	/* The Link Activation Supported bit is only for Downstream Port */
   4124 	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
   4125 		uint32_t t = regs[o2i(pcie_capoff)];
   4126 
   4127 		if ((t == PCIE_XCAP_TYPE_ROOT) || (t == PCIE_XCAP_TYPE_DOWN))
   4128 			onoff("Link Activation Supported", reg,
   4129 			    PCI_L1PM_CAP_LA);
   4130 	}
   4131 	printf("      Port Common Mode Restore Time: %uus\n",
   4132 	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PCMRT));
   4133 	scale = pci_conf_l1pm_cap_tposcale(
   4134 	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PTPOSCALE));
   4135 	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PTPOVAL);
   4136 	printf("      Port T_POWER_ON: ");
   4137 	if (scale == -1)
   4138 		printf("unknown\n");
   4139 	else
   4140 		printf("%dus\n", val * scale);
   4141 
   4142 	reg = regs[o2i(extcapoff + PCI_L1PM_CTL1)];
   4143 	printf("    L1 PM Substates Control register 1: 0x%08x\n", reg);
   4144 	onoff("PCI-PM L1.2 Enable", reg, PCI_L1PM_CTL1_PCIPM12_EN);
   4145 	onoff("PCI-PM L1.1 Enable", reg, PCI_L1PM_CTL1_PCIPM11_EN);
   4146 	onoff("ASPM L1.2 Enable", reg, PCI_L1PM_CTL1_ASPM12_EN);
   4147 	onoff("ASPM L1.1 Enable", reg, PCI_L1PM_CTL1_ASPM11_EN);
   4148 	onoff("Link Activation Interrupt Enable", reg, PCI_L1PM_CTL1_LAIE);
   4149 	onoff("Link Activation Control", reg, PCI_L1PM_CTL1_LA);
   4150 	printf("      Common Mode Restore Time: %uus\n",
   4151 	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_CMRT));
   4152 	scale = PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_LTRTHSCALE));
   4153 	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_LTRTHVAL);
   4154 	printf("      LTR L1.2 THRESHOLD: %dus\n", val * scale);
   4155 
   4156 	reg = regs[o2i(extcapoff + PCI_L1PM_CTL2)];
   4157 	printf("    L1 PM Substates Control register 2: 0x%08x\n", reg);
   4158 	scale = pci_conf_l1pm_cap_tposcale(
   4159 		PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL2_TPOSCALE));
   4160 	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL2_TPOVAL);
   4161 	printf("      T_POWER_ON: ");
   4162 	if (scale == -1)
   4163 		printf("unknown\n");
   4164 	else
   4165 		printf("%dus\n", val * scale);
   4166 
   4167 	if (PCI_EXTCAPLIST_VERSION(regs[o2i(extcapoff)]) >= 2) {
   4168 		reg = regs[o2i(extcapoff + PCI_L1PM_CTL2)];
   4169 		printf("    L1 PM Substates Status register: 0x%08x\n", reg);
   4170 		onoff("Link Activation Status", reg, PCI_L1PM_STAT_LA);
   4171 	}
   4172 }
   4173 
   4174 static void
   4175 pci_conf_print_ptm_cap(const pcireg_t *regs, int extcapoff)
   4176 {
   4177 	pcireg_t reg;
   4178 	uint32_t val;
   4179 
   4180 	printf("\n  Precision Time Measurement\n");
   4181 
   4182 	reg = regs[o2i(extcapoff + PCI_PTM_CAP)];
   4183 	printf("    PTM Capability register: 0x%08x\n", reg);
   4184 	onoff("PTM Requester Capable", reg, PCI_PTM_CAP_REQ);
   4185 	onoff("PTM Responder Capable", reg, PCI_PTM_CAP_RESP);
   4186 	onoff("PTM Root Capable", reg, PCI_PTM_CAP_ROOT);
   4187 	printf("      Local Clock Granularity: ");
   4188 	val = PCIREG_SHIFTOUT(reg, PCI_PTM_CAP_LCLCLKGRNL);
   4189 	switch (val) {
   4190 	case 0:
   4191 		printf("Not implemented\n");
   4192 		break;
   4193 	case 0xffff:
   4194 		printf("> 254ns\n");
   4195 		break;
   4196 	default:
   4197 		printf("%uns\n", val);
   4198 		break;
   4199 	}
   4200 
   4201 	reg = regs[o2i(extcapoff + PCI_PTM_CTL)];
   4202 	printf("    PTM Control register: 0x%08x\n", reg);
   4203 	onoff("PTM Enable", reg, PCI_PTM_CTL_EN);
   4204 	onoff("Root Select", reg, PCI_PTM_CTL_ROOTSEL);
   4205 	printf("      Effective Granularity: ");
   4206 	val = PCIREG_SHIFTOUT(reg, PCI_PTM_CTL_EFCTGRNL);
   4207 	switch (val) {
   4208 	case 0:
   4209 		printf("Unknown\n");
   4210 		break;
   4211 	case 0xffff:
   4212 		printf("> 254ns\n");
   4213 		break;
   4214 	default:
   4215 		printf("%uns\n", val);
   4216 		break;
   4217 	}
   4218 }
   4219 
   4220 /* XXX pci_conf_print_mpcie_cap */
   4221 /* XXX pci_conf_print_frsq_cap */
   4222 /* XXX pci_conf_print_rtr_cap */
   4223 /* XXX pci_conf_print_desigvndsp_cap */
   4224 /* XXX pci_conf_print_vf_resizbar_cap */
   4225 
   4226 static void
   4227 pci_conf_print_dlf_cap(const pcireg_t *regs, int extcapoff)
   4228 {
   4229 	pcireg_t reg;
   4230 
   4231 	printf("\n  Data link Feature Register\n");
   4232 	reg = regs[o2i(extcapoff + PCI_DLF_CAP)];
   4233 	printf("    Capability register: 0x%08x\n", reg);
   4234 	onoff("Scaled Flow Control", reg, PCI_DLF_LFEAT_SCLFCTL);
   4235 	onoff("DLF Exchange enable", reg, PCI_DLF_CAP_XCHG);
   4236 
   4237 	reg = regs[o2i(extcapoff + PCI_DLF_STAT)];
   4238 	printf("    Status register: 0x%08x\n", reg);
   4239 	onoff("Scaled Flow Control", reg, PCI_DLF_LFEAT_SCLFCTL);
   4240 	onoff("Remote DLF supported Valid", reg, PCI_DLF_STAT_RMTVALID);
   4241 }
   4242 
   4243 /* XXX pci_conf_print_hierarchyid_cap */
   4244 /* XXX pci_conf_print_npem_cap */
   4245 
   4246 #undef	MS
   4247 #undef	SM
   4248 #undef	RW
   4249 
   4250 static struct {
   4251 	pcireg_t cap;
   4252 	const char *name;
   4253 	void (*printfunc)(const pcireg_t *, int);
   4254 } pci_extcaptab[] = {
   4255 	{ 0,			"reserved",
   4256 	  NULL },
   4257 	{ PCI_EXTCAP_AER,	"Advanced Error Reporting",
   4258 	  pci_conf_print_aer_cap },
   4259 	{ PCI_EXTCAP_VC,	"Virtual Channel",
   4260 	  pci_conf_print_vc_cap },
   4261 	{ PCI_EXTCAP_SERNUM,	"Device Serial Number",
   4262 	  pci_conf_print_sernum_cap },
   4263 	{ PCI_EXTCAP_PWRBDGT,	"Power Budgeting",
   4264 	  pci_conf_print_pwrbdgt_cap },
   4265 	{ PCI_EXTCAP_RCLINK_DCL,"Root Complex Link Declaration",
   4266 	  pci_conf_print_rclink_dcl_cap },
   4267 	{ PCI_EXTCAP_RCLINK_CTL,"Root Complex Internal Link Control",
   4268 	  NULL },
   4269 	{ PCI_EXTCAP_RCEC_ASSOC,"Root Complex Event Collector Association",
   4270 	  pci_conf_print_rcec_assoc_cap },
   4271 	{ PCI_EXTCAP_MFVC,	"Multi-Function Virtual Channel",
   4272 	  NULL },
   4273 	{ PCI_EXTCAP_VC2,	"Virtual Channel",
   4274 	  NULL },
   4275 	{ PCI_EXTCAP_RCRB,	"RCRB Header",
   4276 	  NULL },
   4277 	{ PCI_EXTCAP_VENDOR,	"Vendor Unique",
   4278 	  NULL },
   4279 	{ PCI_EXTCAP_CAC,	"Configuration Access Correction",
   4280 	  NULL },
   4281 	{ PCI_EXTCAP_ACS,	"Access Control Services",
   4282 	  pci_conf_print_acs_cap },
   4283 	{ PCI_EXTCAP_ARI,	"Alternative Routing-ID Interpretation",
   4284 	  pci_conf_print_ari_cap },
   4285 	{ PCI_EXTCAP_ATS,	"Address Translation Services",
   4286 	  pci_conf_print_ats_cap },
   4287 	{ PCI_EXTCAP_SRIOV,	"Single Root IO Virtualization",
   4288 	  pci_conf_print_sriov_cap },
   4289 	{ PCI_EXTCAP_MRIOV,	"Multiple Root IO Virtualization",
   4290 	  NULL },
   4291 	{ PCI_EXTCAP_MCAST,	"Multicast",
   4292 	  pci_conf_print_multicast_cap },
   4293 	{ PCI_EXTCAP_PAGE_REQ,	"Page Request",
   4294 	  pci_conf_print_page_req_cap },
   4295 	{ PCI_EXTCAP_AMD,	"Reserved for AMD",
   4296 	  NULL },
   4297 	{ PCI_EXTCAP_RESIZBAR,	"Resizable BAR",
   4298 	  pci_conf_print_resizbar_cap },
   4299 	{ PCI_EXTCAP_DPA,	"Dynamic Power Allocation",
   4300 	  pci_conf_print_dpa_cap },
   4301 	{ PCI_EXTCAP_TPH_REQ,	"TPH Requester",
   4302 	  pci_conf_print_tph_req_cap },
   4303 	{ PCI_EXTCAP_LTR,	"Latency Tolerance Reporting",
   4304 	  pci_conf_print_ltr_cap },
   4305 	{ PCI_EXTCAP_SEC_PCIE,	"Secondary PCI Express",
   4306 	  pci_conf_print_sec_pcie_cap },
   4307 	{ PCI_EXTCAP_PMUX,	"Protocol Multiplexing",
   4308 	  NULL },
   4309 	{ PCI_EXTCAP_PASID,	"Process Address Space ID",
   4310 	  pci_conf_print_pasid_cap },
   4311 	{ PCI_EXTCAP_LNR,	"LN Requester",
   4312 	  pci_conf_print_lnr_cap },
   4313 	{ PCI_EXTCAP_DPC,	"Downstream Port Containment",
   4314 	  pci_conf_print_dpc_cap },
   4315 	{ PCI_EXTCAP_L1PM,	"L1 PM Substates",
   4316 	  pci_conf_print_l1pm_cap },
   4317 	{ PCI_EXTCAP_PTM,	"Precision Time Measurement",
   4318 	  pci_conf_print_ptm_cap },
   4319 	{ PCI_EXTCAP_MPCIE,	"M-PCIe",
   4320 	  NULL },
   4321 	{ PCI_EXTCAP_FRSQ,	"Function Reading Status Queueing",
   4322 	  NULL },
   4323 	{ PCI_EXTCAP_RTR,	"Readiness Time Reporting",
   4324 	  NULL },
   4325 	{ PCI_EXTCAP_DESIGVNDSP, "Designated Vendor-Specific",
   4326 	  NULL },
   4327 	{ PCI_EXTCAP_VF_RESIZBAR, "VF Resizable BARs",
   4328 	  NULL },
   4329 	{ PCI_EXTCAP_DLF, "Data link Feature", pci_conf_print_dlf_cap },
   4330 	{ PCI_EXTCAP_PYSLAY_16GT, "Physical Layer 16.0 GT/s", NULL },
   4331 	{ 0x27, "unknown", NULL },
   4332 	{ PCI_EXTCAP_HIERARCHYID, "Hierarchy ID",
   4333 	  NULL },
   4334 	{ PCI_EXTCAP_NPEM,	"Native PCIe Enclosure Management",
   4335 	  NULL },
   4336 };
   4337 
   4338 static int
   4339 pci_conf_find_extcap(const pcireg_t *regs, unsigned int capid, int *offsetp)
   4340 {
   4341 	int off;
   4342 	pcireg_t rval;
   4343 
   4344 	for (off = PCI_EXTCAPLIST_BASE;
   4345 	     off != 0;
   4346 	     off = PCI_EXTCAPLIST_NEXT(rval)) {
   4347 		rval = regs[o2i(off)];
   4348 		if (capid == PCI_EXTCAPLIST_CAP(rval)) {
   4349 			if (offsetp != NULL)
   4350 				*offsetp = off;
   4351 			return 1;
   4352 		}
   4353 	}
   4354 	return 0;
   4355 }
   4356 
   4357 static void
   4358 pci_conf_print_extcaplist(
   4359 #ifdef _KERNEL
   4360     pci_chipset_tag_t pc, pcitag_t tag,
   4361 #endif
   4362     const pcireg_t *regs)
   4363 {
   4364 	int off;
   4365 	pcireg_t foundcap;
   4366 	pcireg_t rval;
   4367 	bool foundtable[__arraycount(pci_extcaptab)];
   4368 	unsigned int i;
   4369 
   4370 	/* Check Extended capability structure */
   4371 	off = PCI_EXTCAPLIST_BASE;
   4372 	rval = regs[o2i(off)];
   4373 	if (rval == 0xffffffff || rval == 0)
   4374 		return;
   4375 
   4376 	/* Clear table */
   4377 	for (i = 0; i < __arraycount(pci_extcaptab); i++)
   4378 		foundtable[i] = false;
   4379 
   4380 	/* Print extended capability register's offset and the type first */
   4381 	for (;;) {
   4382 		printf("  Extended Capability Register at 0x%02x\n", off);
   4383 
   4384 		foundcap = PCI_EXTCAPLIST_CAP(rval);
   4385 		printf("    type: 0x%04x (", foundcap);
   4386 		if (foundcap < __arraycount(pci_extcaptab)) {
   4387 			printf("%s)\n", pci_extcaptab[foundcap].name);
   4388 			/* Mark as found */
   4389 			foundtable[foundcap] = true;
   4390 		} else
   4391 			printf("unknown)\n");
   4392 		printf("    version: %d\n", PCI_EXTCAPLIST_VERSION(rval));
   4393 
   4394 		off = PCI_EXTCAPLIST_NEXT(rval);
   4395 		if (off == 0)
   4396 			break;
   4397 		else if (off <= PCI_CONF_SIZE) {
   4398 			printf("    next pointer: 0x%03x (incorrect)\n", off);
   4399 			return;
   4400 		}
   4401 		rval = regs[o2i(off)];
   4402 	}
   4403 
   4404 	/*
   4405 	 * And then, print the detail of each capability registers
   4406 	 * in capability value's order.
   4407 	 */
   4408 	for (i = 0; i < __arraycount(pci_extcaptab); i++) {
   4409 		if (foundtable[i] == false)
   4410 			continue;
   4411 
   4412 		/*
   4413 		 * The type was found. Search capability list again and
   4414 		 * print all capabilities that the capability type is
   4415 		 * the same.
   4416 		 */
   4417 		if (pci_conf_find_extcap(regs, i, &off) == 0)
   4418 			continue;
   4419 		rval = regs[o2i(off)];
   4420 		if ((PCI_EXTCAPLIST_VERSION(rval) <= 0)
   4421 		    || (pci_extcaptab[i].printfunc == NULL))
   4422 			continue;
   4423 
   4424 		pci_extcaptab[i].printfunc(regs, off);
   4425 
   4426 	}
   4427 }
   4428 
   4429 /* Print the Secondary Status Register. */
   4430 static void
   4431 pci_conf_print_ssr(pcireg_t rval)
   4432 {
   4433 	pcireg_t devsel;
   4434 
   4435 	printf("    Secondary status register: 0x%04x\n", rval); /* XXX bits */
   4436 	onoff("66 MHz capable", rval, __BIT(5));
   4437 	onoff("User Definable Features (UDF) support", rval, __BIT(6));
   4438 	onoff("Fast back-to-back capable", rval, __BIT(7));
   4439 	onoff("Data parity error detected", rval, __BIT(8));
   4440 
   4441 	printf("      DEVSEL timing: ");
   4442 	devsel = PCIREG_SHIFTOUT(rval, __BITS(10, 9));
   4443 	switch (devsel) {
   4444 	case 0:
   4445 		printf("fast");
   4446 		break;
   4447 	case 1:
   4448 		printf("medium");
   4449 		break;
   4450 	case 2:
   4451 		printf("slow");
   4452 		break;
   4453 	default:
   4454 		printf("unknown/reserved");	/* XXX */
   4455 		break;
   4456 	}
   4457 	printf(" (0x%x)\n", devsel);
   4458 
   4459 	onoff("Signalled target abort", rval, __BIT(11));
   4460 	onoff("Received target abort", rval, __BIT(12));
   4461 	onoff("Received master abort", rval, __BIT(13));
   4462 	onoff("Received system error", rval, __BIT(14));
   4463 	onoff("Detected parity error", rval, __BIT(15));
   4464 }
   4465 
   4466 static void
   4467 pci_conf_print_type0(
   4468 #ifdef _KERNEL
   4469     pci_chipset_tag_t pc, pcitag_t tag,
   4470 #endif
   4471     const pcireg_t *regs)
   4472 {
   4473 	int off, width;
   4474 	pcireg_t rval;
   4475 	const char *str;
   4476 
   4477 	for (off = PCI_MAPREG_START; off < PCI_MAPREG_END; off += width) {
   4478 #ifdef _KERNEL
   4479 		width = pci_conf_print_bar(pc, tag, regs, off, NULL);
   4480 #else
   4481 		width = pci_conf_print_bar(regs, off, NULL);
   4482 #endif
   4483 	}
   4484 
   4485 	printf("    Cardbus CIS Pointer: 0x%08x\n",
   4486 	    regs[o2i(PCI_CARDBUS_CIS_REG)]);
   4487 
   4488 	rval = regs[o2i(PCI_SUBSYS_ID_REG)];
   4489 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
   4490 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
   4491 
   4492 	rval = regs[o2i(PCI_MAPREG_ROM)];
   4493 	printf("    Expansion ROM Base Address Register: 0x%08x\n", rval);
   4494 	printf("      base: 0x%08x\n", (uint32_t)PCI_MAPREG_ROM_ADDR(rval));
   4495 	onoff("Expansion ROM Enable", rval, PCI_MAPREG_ROM_ENABLE);
   4496 	printf("      Validation Status: ");
   4497 	switch (PCIREG_SHIFTOUT(rval, PCI_MAPREG_ROM_VALID_STAT)) {
   4498 	case PCI_MAPREG_ROM_VSTAT_NOTSUPP:
   4499 		str = "Validation not supported";
   4500 		break;
   4501 	case PCI_MAPREG_ROM_VSTAT_INPROG:
   4502 		str = "Validation in Progress";
   4503 		break;
   4504 	case PCI_MAPREG_ROM_VSTAT_VPASS:
   4505 		str = "Validation Pass. "
   4506 		    "Valid contents, trust test was not performed";
   4507 		break;
   4508 	case PCI_MAPREG_ROM_VSTAT_VPASSTRUST:
   4509 		str = "Validation Pass. Valid and trusted contents";
   4510 		break;
   4511 	case PCI_MAPREG_ROM_VSTAT_VFAIL:
   4512 		str = "Validation Fail. Invalid contents";
   4513 		break;
   4514 	case PCI_MAPREG_ROM_VSTAT_VFAILUNTRUST:
   4515 		str = "Validation Fail. Valid but untrusted contents";
   4516 		break;
   4517 	case PCI_MAPREG_ROM_VSTAT_WPASS:
   4518 		str = "Warning Pass. Validation passed with warning. "
   4519 		    "Valid contents, trust test was not performed";
   4520 		break;
   4521 	case PCI_MAPREG_ROM_VSTAT_WPASSTRUST:
   4522 		str = "Warning Pass. Validation passed with warning. "
   4523 		    "Valid and trusted contents";
   4524 		break;
   4525 	}
   4526 	printf("%s\n", str);
   4527 	printf("      Validation Details: 0x%x\n",
   4528 	    PCIREG_SHIFTOUT(rval, PCI_MAPREG_ROM_VALID_DETAIL));
   4529 
   4530 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   4531 		printf("    Capability list pointer: 0x%02x\n",
   4532 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
   4533 	else
   4534 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
   4535 
   4536 	printf("    Reserved @ 0x38: 0x%08x\n", regs[o2i(0x38)]);
   4537 
   4538 	rval = regs[o2i(PCI_INTERRUPT_REG)];
   4539 	printf("    Maximum Latency: 0x%02x\n", PCI_MAX_LAT(rval));
   4540 	printf("    Minimum Grant: 0x%02x\n", PCI_MIN_GNT(rval));
   4541 	printf("    Interrupt pin: 0x%02x ", PCI_INTERRUPT_PIN(rval));
   4542 	switch (PCI_INTERRUPT_PIN(rval)) {
   4543 	case PCI_INTERRUPT_PIN_NONE:
   4544 		printf("(none)");
   4545 		break;
   4546 	case PCI_INTERRUPT_PIN_A:
   4547 		printf("(pin A)");
   4548 		break;
   4549 	case PCI_INTERRUPT_PIN_B:
   4550 		printf("(pin B)");
   4551 		break;
   4552 	case PCI_INTERRUPT_PIN_C:
   4553 		printf("(pin C)");
   4554 		break;
   4555 	case PCI_INTERRUPT_PIN_D:
   4556 		printf("(pin D)");
   4557 		break;
   4558 	default:
   4559 		printf("(? ? ?)");
   4560 		break;
   4561 	}
   4562 	printf("\n");
   4563 	printf("    Interrupt line: 0x%02x\n", PCI_INTERRUPT_LINE(rval));
   4564 }
   4565 
   4566 static void
   4567 pci_conf_print_type1(
   4568 #ifdef _KERNEL
   4569     pci_chipset_tag_t pc, pcitag_t tag,
   4570 #endif
   4571     const pcireg_t *regs)
   4572 {
   4573 	int off, width;
   4574 	pcireg_t rval, csreg;
   4575 	uint32_t base, limit;
   4576 	uint32_t base_h, limit_h;
   4577 	uint64_t pbase, plimit;
   4578 	int use_upper;
   4579 
   4580 	/*
   4581 	 * This layout was cribbed from the TI PCI2030 PCI-to-PCI
   4582 	 * Bridge chip documentation, and may not be correct with
   4583 	 * respect to various standards. (XXX)
   4584 	 */
   4585 
   4586 	for (off = 0x10; off < 0x18; off += width) {
   4587 #ifdef _KERNEL
   4588 		width = pci_conf_print_bar(pc, tag, regs, off, NULL);
   4589 #else
   4590 		width = pci_conf_print_bar(regs, off, NULL);
   4591 #endif
   4592 	}
   4593 
   4594 	rval = regs[o2i(PCI_BRIDGE_BUS_REG)];
   4595 	printf("    Primary bus number: 0x%02x\n",
   4596 	    PCI_BRIDGE_BUS_NUM_PRIMARY(rval));
   4597 	printf("    Secondary bus number: 0x%02x\n",
   4598 	    PCI_BRIDGE_BUS_NUM_SECONDARY(rval));
   4599 	printf("    Subordinate bus number: 0x%02x\n",
   4600 	    PCI_BRIDGE_BUS_NUM_SUBORDINATE(rval));
   4601 	printf("    Secondary bus latency timer: 0x%02x\n",
   4602 	    PCI_BRIDGE_BUS_SEC_LATTIMER_VAL(rval));
   4603 
   4604 	rval = regs[o2i(PCI_BRIDGE_STATIO_REG)];
   4605 	pci_conf_print_ssr(PCIREG_SHIFTOUT(rval, __BITS(31, 16)));
   4606 
   4607 	/* I/O region */
   4608 	printf("    I/O region:\n");
   4609 	printf("      base register:  0x%02x\n", (rval >> 0) & 0xff);
   4610 	printf("      limit register: 0x%02x\n", (rval >> 8) & 0xff);
   4611 	if (PCI_BRIDGE_IO_32BITS(rval))
   4612 		use_upper = 1;
   4613 	else
   4614 		use_upper = 0;
   4615 	onoff("32bit I/O", rval, use_upper);
   4616 	base = PCI_BRIDGE_STATIO_IOBASE_ADDR(rval);
   4617 	limit = PCI_BRIDGE_STATIO_IOLIMIT_ADDR(rval);
   4618 
   4619 	rval = regs[o2i(PCI_BRIDGE_IOHIGH_REG)];
   4620 	base_h = PCIREG_SHIFTOUT(rval, PCI_BRIDGE_IOHIGH_BASE);
   4621 	limit_h = PCIREG_SHIFTOUT(rval, PCI_BRIDGE_IOHIGH_LIMIT);
   4622 	printf("      base upper 16 bits register:  0x%04x\n", base_h);
   4623 	printf("      limit upper 16 bits register: 0x%04x\n", limit_h);
   4624 
   4625 	if (use_upper == 1) {
   4626 		base |= base_h << 16;
   4627 		limit |= limit_h << 16;
   4628 	}
   4629 	if (base < limit) {
   4630 		if (use_upper == 1)
   4631 			printf("      range: 0x%08x-0x%08x\n", base, limit);
   4632 		else
   4633 			printf("      range: 0x%04x-0x%04x\n", base, limit);
   4634 	} else
   4635 		printf("      range:  not set\n");
   4636 
   4637 	/* Non-prefetchable memory region */
   4638 	rval = regs[o2i(PCI_BRIDGE_MEMORY_REG)];
   4639 	printf("    Memory region:\n");
   4640 	printf("      base register:  0x%04hx\n",
   4641 	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_MEMORY_BASE));
   4642 	printf("      limit register: 0x%04hx\n",
   4643 	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_MEMORY_LIMIT));
   4644 	base = PCI_BRIDGE_MEMORY_BASE_ADDR(rval);
   4645 	limit = PCI_BRIDGE_MEMORY_LIMIT_ADDR(rval);
   4646 	if (base < limit)
   4647 		printf("      range: 0x%08x-0x%08x\n", base, limit);
   4648 	else
   4649 		printf("      range: not set\n");
   4650 
   4651 	/* Prefetchable memory region */
   4652 	rval = regs[o2i(PCI_BRIDGE_PREFETCHMEM_REG)];
   4653 	printf("    Prefetchable memory region:\n");
   4654 	printf("      base register:  0x%04x\n",
   4655 	    (rval >> 0) & 0xffff);
   4656 	printf("      limit register: 0x%04x\n",
   4657 	    (rval >> 16) & 0xffff);
   4658 	base_h = regs[o2i(PCI_BRIDGE_PREFETCHBASEUP32_REG)];
   4659 	limit_h = regs[o2i(PCI_BRIDGE_PREFETCHLIMITUP32_REG)];
   4660 	printf("      base upper 32 bits register:  0x%08x\n",
   4661 	    base_h);
   4662 	printf("      limit upper 32 bits register: 0x%08x\n",
   4663 	    limit_h);
   4664 	if (PCI_BRIDGE_PREFETCHMEM_64BITS(rval))
   4665 		use_upper = 1;
   4666 	else
   4667 		use_upper = 0;
   4668 	onoff("64bit memory address", rval, use_upper);
   4669 	pbase = PCI_BRIDGE_PREFETCHMEM_BASE_ADDR(rval);
   4670 	plimit = PCI_BRIDGE_PREFETCHMEM_LIMIT_ADDR(rval);
   4671 	if (use_upper == 1) {
   4672 		pbase |= (uint64_t)base_h << 32;
   4673 		plimit |= (uint64_t)limit_h << 32;
   4674 	}
   4675 	if (pbase < plimit) {
   4676 		if (use_upper == 1)
   4677 			printf("      range: 0x%016" PRIx64 "-0x%016" PRIx64
   4678 			    "\n", pbase, plimit);
   4679 		else
   4680 			printf("      range: 0x%08x-0x%08x\n",
   4681 			    (uint32_t)pbase, (uint32_t)plimit);
   4682 	} else
   4683 		printf("      range: not set\n");
   4684 
   4685 	csreg = regs[o2i(PCI_COMMAND_STATUS_REG)];
   4686 	if (csreg & PCI_STATUS_CAPLIST_SUPPORT)
   4687 		printf("    Capability list pointer: 0x%02x\n",
   4688 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
   4689 	else
   4690 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
   4691 
   4692 	printf("    Expansion ROM Base Address: 0x%08x\n",
   4693 	    regs[o2i(PCI_BRIDGE_EXPROMADDR_REG)]);
   4694 
   4695 	rval = regs[o2i(PCI_INTERRUPT_REG)];
   4696 	printf("    Interrupt line: 0x%02x\n",
   4697 	    (rval >> 0) & 0xff);
   4698 	printf("    Interrupt pin: 0x%02x ",
   4699 	    (rval >> 8) & 0xff);
   4700 	switch ((rval >> 8) & 0xff) {
   4701 	case PCI_INTERRUPT_PIN_NONE:
   4702 		printf("(none)");
   4703 		break;
   4704 	case PCI_INTERRUPT_PIN_A:
   4705 		printf("(pin A)");
   4706 		break;
   4707 	case PCI_INTERRUPT_PIN_B:
   4708 		printf("(pin B)");
   4709 		break;
   4710 	case PCI_INTERRUPT_PIN_C:
   4711 		printf("(pin C)");
   4712 		break;
   4713 	case PCI_INTERRUPT_PIN_D:
   4714 		printf("(pin D)");
   4715 		break;
   4716 	default:
   4717 		printf("(? ? ?)");
   4718 		break;
   4719 	}
   4720 	printf("\n");
   4721 	rval = regs[o2i(PCI_BRIDGE_CONTROL_REG)];
   4722 	printf("    Bridge control register: 0x%04hx\n",
   4723 	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_CONTROL));
   4724 	onoff("Parity error response", rval, PCI_BRIDGE_CONTROL_PERE);
   4725 	onoff("Secondary SERR forwarding", rval, PCI_BRIDGE_CONTROL_SERR);
   4726 	onoff("ISA enable", rval, PCI_BRIDGE_CONTROL_ISA);
   4727 	onoff("VGA enable", rval, PCI_BRIDGE_CONTROL_VGA);
   4728 	/*
   4729 	 * VGA 16bit decode bit has meaning if the VGA enable bit or the
   4730 	 * VGA Palette Snoop Enable bit is set.
   4731 	 */
   4732 	if (((rval & PCI_BRIDGE_CONTROL_VGA) != 0)
   4733 	    || ((csreg & PCI_COMMAND_PALETTE_ENABLE) != 0))
   4734 		onoff("VGA 16bit enable", rval, PCI_BRIDGE_CONTROL_VGA16);
   4735 	onoff("Master abort reporting", rval, PCI_BRIDGE_CONTROL_MABRT);
   4736 	onoff("Secondary bus reset", rval, PCI_BRIDGE_CONTROL_SECBR);
   4737 	onoff("Fast back-to-back enable", rval, PCI_BRIDGE_CONTROL_SECFASTB2B);
   4738 	onoff("Primary Discard Timer", rval,
   4739 	    PCI_BRIDGE_CONTROL_PRI_DISC_TIMER);
   4740 	onoff("Secondary Discard Timer",
   4741 	    rval, PCI_BRIDGE_CONTROL_SEC_DISC_TIMER);
   4742 	onoff("Discard Timer Status", rval,
   4743 	    PCI_BRIDGE_CONTROL_DISC_TIMER_STAT);
   4744 	onoff("Discard Timer SERR# Enable", rval,
   4745 	    PCI_BRIDGE_CONTROL_DISC_TIMER_SERR);
   4746 }
   4747 
   4748 static void
   4749 pci_conf_print_type2(
   4750 #ifdef _KERNEL
   4751     pci_chipset_tag_t pc, pcitag_t tag,
   4752 #endif
   4753     const pcireg_t *regs)
   4754 {
   4755 	pcireg_t rval;
   4756 
   4757 	/*
   4758 	 * XXX these need to be printed in more detail, need to be
   4759 	 * XXX checked against specs/docs, etc.
   4760 	 *
   4761 	 * This layout was cribbed from the TI PCI1420 PCI-to-CardBus
   4762 	 * controller chip documentation, and may not be correct with
   4763 	 * respect to various standards. (XXX)
   4764 	 */
   4765 
   4766 #ifdef _KERNEL
   4767 	pci_conf_print_bar(pc, tag, regs, 0x10,
   4768 	    "CardBus socket/ExCA registers");
   4769 #else
   4770 	pci_conf_print_bar(regs, 0x10, "CardBus socket/ExCA registers");
   4771 #endif
   4772 
   4773 	/* Capability list pointer and secondary status register */
   4774 	rval = regs[o2i(PCI_CARDBUS_CAPLISTPTR_REG)];
   4775 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   4776 		printf("    Capability list pointer: 0x%02x\n",
   4777 		    PCI_CAPLIST_PTR(rval));
   4778 	else
   4779 		printf("    Reserved @ 0x14: 0x%04x\n",
   4780 		       PCIREG_SHIFTOUT(rval, __BITS(15, 0)));
   4781 	pci_conf_print_ssr(PCIREG_SHIFTOUT(rval, __BITS(31, 16)));
   4782 
   4783 	rval = regs[o2i(PCI_BRIDGE_BUS_REG)];
   4784 	printf("    PCI bus number: 0x%02x\n",
   4785 	    (rval >> 0) & 0xff);
   4786 	printf("    CardBus bus number: 0x%02x\n",
   4787 	    (rval >> 8) & 0xff);
   4788 	printf("    Subordinate bus number: 0x%02x\n",
   4789 	    (rval >> 16) & 0xff);
   4790 	printf("    CardBus latency timer: 0x%02x\n",
   4791 	    (rval >> 24) & 0xff);
   4792 
   4793 	/* XXX Print more prettily */
   4794 	printf("    CardBus memory region 0:\n");
   4795 	printf("      base register:  0x%08x\n", regs[o2i(0x1c)]);
   4796 	printf("      limit register: 0x%08x\n", regs[o2i(0x20)]);
   4797 	printf("    CardBus memory region 1:\n");
   4798 	printf("      base register:  0x%08x\n", regs[o2i(0x24)]);
   4799 	printf("      limit register: 0x%08x\n", regs[o2i(0x28)]);
   4800 	printf("    CardBus I/O region 0:\n");
   4801 	printf("      base register:  0x%08x\n", regs[o2i(0x2c)]);
   4802 	printf("      limit register: 0x%08x\n", regs[o2i(0x30)]);
   4803 	printf("    CardBus I/O region 1:\n");
   4804 	printf("      base register:  0x%08x\n", regs[o2i(0x34)]);
   4805 	printf("      limit register: 0x%08x\n", regs[o2i(0x38)]);
   4806 
   4807 	rval = regs[o2i(PCI_INTERRUPT_REG)];
   4808 	printf("    Interrupt line: 0x%02x\n",
   4809 	    (rval >> 0) & 0xff);
   4810 	printf("    Interrupt pin: 0x%02x ",
   4811 	    (rval >> 8) & 0xff);
   4812 	switch ((rval >> 8) & 0xff) {
   4813 	case PCI_INTERRUPT_PIN_NONE:
   4814 		printf("(none)");
   4815 		break;
   4816 	case PCI_INTERRUPT_PIN_A:
   4817 		printf("(pin A)");
   4818 		break;
   4819 	case PCI_INTERRUPT_PIN_B:
   4820 		printf("(pin B)");
   4821 		break;
   4822 	case PCI_INTERRUPT_PIN_C:
   4823 		printf("(pin C)");
   4824 		break;
   4825 	case PCI_INTERRUPT_PIN_D:
   4826 		printf("(pin D)");
   4827 		break;
   4828 	default:
   4829 		printf("(? ? ?)");
   4830 		break;
   4831 	}
   4832 	printf("\n");
   4833 	rval = (regs[o2i(PCI_BRIDGE_CONTROL_REG)] >> 16) & 0xffff;
   4834 	printf("    Bridge control register: 0x%04x\n", rval);
   4835 	onoff("Parity error response", rval, __BIT(0));
   4836 	onoff("SERR# enable", rval, __BIT(1));
   4837 	onoff("ISA enable", rval, __BIT(2));
   4838 	onoff("VGA enable", rval, __BIT(3));
   4839 	onoff("Master abort mode", rval, __BIT(5));
   4840 	onoff("Secondary (CardBus) bus reset", rval, __BIT(6));
   4841 	onoff("Functional interrupts routed by ExCA registers", rval,
   4842 	    __BIT(7));
   4843 	onoff("Memory window 0 prefetchable", rval, __BIT(8));
   4844 	onoff("Memory window 1 prefetchable", rval, __BIT(9));
   4845 	onoff("Write posting enable", rval, __BIT(10));
   4846 
   4847 	rval = regs[o2i(0x40)];
   4848 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
   4849 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
   4850 
   4851 #ifdef _KERNEL
   4852 	pci_conf_print_bar(pc, tag, regs, 0x44, "legacy-mode registers");
   4853 #else
   4854 	pci_conf_print_bar(regs, 0x44, "legacy-mode registers");
   4855 #endif
   4856 }
   4857 
   4858 void
   4859 pci_conf_print(
   4860 #ifdef _KERNEL
   4861     pci_chipset_tag_t pc, pcitag_t tag,
   4862     void (*printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *)
   4863 #else
   4864     int pcifd, u_int bus, u_int dev, u_int func
   4865 #endif
   4866     )
   4867 {
   4868 	pcireg_t *regs;
   4869 	int off, capoff, endoff, hdrtype;
   4870 	const char *type_name;
   4871 #ifdef _KERNEL
   4872 	void (*type_printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *);
   4873 #else
   4874 	void (*type_printfn)(const pcireg_t *);
   4875 #endif
   4876 
   4877 	regs = MALLOC(PCI_EXTCONF_SIZE);
   4878 
   4879 	printf("PCI configuration registers:\n");
   4880 
   4881 	for (off = 0; off < PCI_EXTCONF_SIZE; off += 4) {
   4882 #ifdef _KERNEL
   4883 		regs[o2i(off)] = pci_conf_read(pc, tag, off);
   4884 #else
   4885 		if (pcibus_conf_read(pcifd, bus, dev, func, off,
   4886 		    &regs[o2i(off)]) == -1)
   4887 			regs[o2i(off)] = 0;
   4888 #endif
   4889 	}
   4890 
   4891 	/* common header */
   4892 	printf("  Common header:\n");
   4893 	pci_conf_print_regs(regs, 0, 16);
   4894 
   4895 	printf("\n");
   4896 #ifdef _KERNEL
   4897 	pci_conf_print_common(pc, tag, regs);
   4898 #else
   4899 	pci_conf_print_common(regs);
   4900 #endif
   4901 	printf("\n");
   4902 
   4903 	/* type-dependent header */
   4904 	hdrtype = PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]);
   4905 	switch (hdrtype) {		/* XXX make a table, eventually */
   4906 	case 0:
   4907 		/* Standard device header */
   4908 		type_name = "\"normal\" device";
   4909 		type_printfn = &pci_conf_print_type0;
   4910 		capoff = PCI_CAPLISTPTR_REG;
   4911 		endoff = 64;
   4912 		break;
   4913 	case 1:
   4914 		/* PCI-PCI bridge header */
   4915 		type_name = "PCI-PCI bridge";
   4916 		type_printfn = &pci_conf_print_type1;
   4917 		capoff = PCI_CAPLISTPTR_REG;
   4918 		endoff = 64;
   4919 		break;
   4920 	case 2:
   4921 		/* PCI-CardBus bridge header */
   4922 		type_name = "PCI-CardBus bridge";
   4923 		type_printfn = &pci_conf_print_type2;
   4924 		capoff = PCI_CARDBUS_CAPLISTPTR_REG;
   4925 		endoff = 72;
   4926 		break;
   4927 	default:
   4928 		type_name = NULL;
   4929 		type_printfn = 0;
   4930 		capoff = -1;
   4931 		endoff = 64;
   4932 		break;
   4933 	}
   4934 	printf("  Type %d ", hdrtype);
   4935 	if (type_name != NULL)
   4936 		printf("(%s) ", type_name);
   4937 	printf("header:\n");
   4938 	pci_conf_print_regs(regs, 16, endoff);
   4939 	printf("\n");
   4940 	if (type_printfn) {
   4941 #ifdef _KERNEL
   4942 		(*type_printfn)(pc, tag, regs);
   4943 #else
   4944 		(*type_printfn)(regs);
   4945 #endif
   4946 	} else
   4947 		printf("    Don't know how to pretty-print type %d header.\n",
   4948 		    hdrtype);
   4949 	printf("\n");
   4950 
   4951 	/* capability list, if present */
   4952 	if ((regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   4953 		&& (capoff > 0)) {
   4954 #ifdef _KERNEL
   4955 		pci_conf_print_caplist(pc, tag, regs, capoff);
   4956 #else
   4957 		pci_conf_print_caplist(regs, capoff);
   4958 #endif
   4959 		printf("\n");
   4960 	}
   4961 
   4962 	/* device-dependent header */
   4963 	printf("  Device-dependent header:\n");
   4964 	pci_conf_print_regs(regs, endoff, PCI_CONF_SIZE);
   4965 #ifdef _KERNEL
   4966 	printf("\n");
   4967 	if (printfn)
   4968 		(*printfn)(pc, tag, regs);
   4969 	else
   4970 		printf("    Don't know how to pretty-print device-dependent header.\n");
   4971 #endif /* _KERNEL */
   4972 
   4973 	if (regs[o2i(PCI_EXTCAPLIST_BASE)] == 0xffffffff ||
   4974 	    regs[o2i(PCI_EXTCAPLIST_BASE)] == 0)
   4975 		goto out;
   4976 
   4977 	printf("\n");
   4978 #ifdef _KERNEL
   4979 	pci_conf_print_extcaplist(pc, tag, regs);
   4980 #else
   4981 	pci_conf_print_extcaplist(regs);
   4982 #endif
   4983 	printf("\n");
   4984 
   4985 	/* Extended Configuration Space, if present */
   4986 	printf("  Extended Configuration Space:\n");
   4987 	pci_conf_print_regs(regs, PCI_EXTCAPLIST_BASE, PCI_EXTCONF_SIZE);
   4988 
   4989 out:
   4990 	FREE(regs, PCI_EXTCONF_SIZE);
   4991 }
   4992