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