Home | History | Annotate | Line # | Download | only in pci
pci_subr.c revision 1.100
      1 /*	$NetBSD: pci_subr.c,v 1.100 2013/04/17 04:36:27 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.100 2013/04/17 04:36:27 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 <stdbool.h>
     58 #include <stdio.h>
     59 #endif
     60 
     61 #include <dev/pci/pcireg.h>
     62 #ifdef _KERNEL
     63 #include <dev/pci/pcivar.h>
     64 #endif
     65 
     66 /*
     67  * Descriptions of known PCI classes and subclasses.
     68  *
     69  * Subclasses are described in the same way as classes, but have a
     70  * NULL subclass pointer.
     71  */
     72 struct pci_class {
     73 	const char	*name;
     74 	u_int		val;		/* as wide as pci_{,sub}class_t */
     75 	const struct pci_class *subclasses;
     76 };
     77 
     78 static const struct pci_class pci_subclass_prehistoric[] = {
     79 	{ "miscellaneous",	PCI_SUBCLASS_PREHISTORIC_MISC,	NULL,	},
     80 	{ "VGA",		PCI_SUBCLASS_PREHISTORIC_VGA,	NULL,	},
     81 	{ NULL,			0,				NULL,	},
     82 };
     83 
     84 static const struct pci_class pci_subclass_mass_storage[] = {
     85 	{ "SCSI",		PCI_SUBCLASS_MASS_STORAGE_SCSI,	NULL,	},
     86 	{ "IDE",		PCI_SUBCLASS_MASS_STORAGE_IDE,	NULL,	},
     87 	{ "floppy",		PCI_SUBCLASS_MASS_STORAGE_FLOPPY, NULL, },
     88 	{ "IPI",		PCI_SUBCLASS_MASS_STORAGE_IPI,	NULL,	},
     89 	{ "RAID",		PCI_SUBCLASS_MASS_STORAGE_RAID,	NULL,	},
     90 	{ "ATA",		PCI_SUBCLASS_MASS_STORAGE_ATA,	NULL,	},
     91 	{ "SATA",		PCI_SUBCLASS_MASS_STORAGE_SATA,	NULL,	},
     92 	{ "SAS",		PCI_SUBCLASS_MASS_STORAGE_SAS,	NULL,	},
     93 	{ "NVM",		PCI_SUBCLASS_MASS_STORAGE_NVM,	NULL,	},
     94 	{ "miscellaneous",	PCI_SUBCLASS_MASS_STORAGE_MISC,	NULL,	},
     95 	{ NULL,			0,				NULL,	},
     96 };
     97 
     98 static const struct pci_class pci_subclass_network[] = {
     99 	{ "ethernet",		PCI_SUBCLASS_NETWORK_ETHERNET,	NULL,	},
    100 	{ "token ring",		PCI_SUBCLASS_NETWORK_TOKENRING,	NULL,	},
    101 	{ "FDDI",		PCI_SUBCLASS_NETWORK_FDDI,	NULL,	},
    102 	{ "ATM",		PCI_SUBCLASS_NETWORK_ATM,	NULL,	},
    103 	{ "ISDN",		PCI_SUBCLASS_NETWORK_ISDN,	NULL,	},
    104 	{ "WorldFip",		PCI_SUBCLASS_NETWORK_WORLDFIP,	NULL,	},
    105 	{ "PCMIG Multi Computing", PCI_SUBCLASS_NETWORK_PCIMGMULTICOMP, NULL, },
    106 	{ "miscellaneous",	PCI_SUBCLASS_NETWORK_MISC,	NULL,	},
    107 	{ NULL,			0,				NULL,	},
    108 };
    109 
    110 static const struct pci_class pci_subclass_display[] = {
    111 	{ "VGA",		PCI_SUBCLASS_DISPLAY_VGA,	NULL,	},
    112 	{ "XGA",		PCI_SUBCLASS_DISPLAY_XGA,	NULL,	},
    113 	{ "3D",			PCI_SUBCLASS_DISPLAY_3D,	NULL,	},
    114 	{ "miscellaneous",	PCI_SUBCLASS_DISPLAY_MISC,	NULL,	},
    115 	{ NULL,			0,				NULL,	},
    116 };
    117 
    118 static const struct pci_class pci_subclass_multimedia[] = {
    119 	{ "video",		PCI_SUBCLASS_MULTIMEDIA_VIDEO,	NULL,	},
    120 	{ "audio",		PCI_SUBCLASS_MULTIMEDIA_AUDIO,	NULL,	},
    121 	{ "telephony",		PCI_SUBCLASS_MULTIMEDIA_TELEPHONY, NULL,},
    122 	{ "HD audio",		PCI_SUBCLASS_MULTIMEDIA_HDAUDIO, NULL,	},
    123 	{ "miscellaneous",	PCI_SUBCLASS_MULTIMEDIA_MISC,	NULL,	},
    124 	{ NULL,			0,				NULL,	},
    125 };
    126 
    127 static const struct pci_class pci_subclass_memory[] = {
    128 	{ "RAM",		PCI_SUBCLASS_MEMORY_RAM,	NULL,	},
    129 	{ "flash",		PCI_SUBCLASS_MEMORY_FLASH,	NULL,	},
    130 	{ "miscellaneous",	PCI_SUBCLASS_MEMORY_MISC,	NULL,	},
    131 	{ NULL,			0,				NULL,	},
    132 };
    133 
    134 static const struct pci_class pci_subclass_bridge[] = {
    135 	{ "host",		PCI_SUBCLASS_BRIDGE_HOST,	NULL,	},
    136 	{ "ISA",		PCI_SUBCLASS_BRIDGE_ISA,	NULL,	},
    137 	{ "EISA",		PCI_SUBCLASS_BRIDGE_EISA,	NULL,	},
    138 	{ "MicroChannel",	PCI_SUBCLASS_BRIDGE_MC,		NULL,	},
    139 	{ "PCI",		PCI_SUBCLASS_BRIDGE_PCI,	NULL,	},
    140 	{ "PCMCIA",		PCI_SUBCLASS_BRIDGE_PCMCIA,	NULL,	},
    141 	{ "NuBus",		PCI_SUBCLASS_BRIDGE_NUBUS,	NULL,	},
    142 	{ "CardBus",		PCI_SUBCLASS_BRIDGE_CARDBUS,	NULL,	},
    143 	{ "RACEway",		PCI_SUBCLASS_BRIDGE_RACEWAY,	NULL,	},
    144 	{ "Semi-transparent PCI", PCI_SUBCLASS_BRIDGE_STPCI,	NULL,	},
    145 	{ "InfiniBand",		PCI_SUBCLASS_BRIDGE_INFINIBAND,	NULL,	},
    146 	{ "miscellaneous",	PCI_SUBCLASS_BRIDGE_MISC,	NULL,	},
    147 	{ NULL,			0,				NULL,	},
    148 };
    149 
    150 static const struct pci_class pci_subclass_communications[] = {
    151 	{ "serial",		PCI_SUBCLASS_COMMUNICATIONS_SERIAL,	NULL, },
    152 	{ "parallel",		PCI_SUBCLASS_COMMUNICATIONS_PARALLEL,	NULL, },
    153 	{ "multi-port serial",	PCI_SUBCLASS_COMMUNICATIONS_MPSERIAL,	NULL, },
    154 	{ "modem",		PCI_SUBCLASS_COMMUNICATIONS_MODEM,	NULL, },
    155 	{ "GPIB",		PCI_SUBCLASS_COMMUNICATIONS_GPIB,	NULL, },
    156 	{ "smartcard",		PCI_SUBCLASS_COMMUNICATIONS_SMARTCARD,	NULL, },
    157 	{ "miscellaneous",	PCI_SUBCLASS_COMMUNICATIONS_MISC,	NULL, },
    158 	{ NULL,			0,					NULL, },
    159 };
    160 
    161 static const struct pci_class pci_subclass_system[] = {
    162 	{ "interrupt",		PCI_SUBCLASS_SYSTEM_PIC,	NULL,	},
    163 	{ "8237 DMA",		PCI_SUBCLASS_SYSTEM_DMA,	NULL,	},
    164 	{ "8254 timer",		PCI_SUBCLASS_SYSTEM_TIMER,	NULL,	},
    165 	{ "RTC",		PCI_SUBCLASS_SYSTEM_RTC,	NULL,	},
    166 	{ "PCI Hot-Plug",	PCI_SUBCLASS_SYSTEM_PCIHOTPLUG, NULL,	},
    167 	{ "SD Host Controller",	PCI_SUBCLASS_SYSTEM_SDHC,	NULL,	},
    168 	{ "miscellaneous",	PCI_SUBCLASS_SYSTEM_MISC,	NULL,	},
    169 	{ NULL,			0,				NULL,	},
    170 };
    171 
    172 static const struct pci_class pci_subclass_input[] = {
    173 	{ "keyboard",		PCI_SUBCLASS_INPUT_KEYBOARD,	NULL,	},
    174 	{ "digitizer",		PCI_SUBCLASS_INPUT_DIGITIZER,	NULL,	},
    175 	{ "mouse",		PCI_SUBCLASS_INPUT_MOUSE,	NULL,	},
    176 	{ "scanner",		PCI_SUBCLASS_INPUT_SCANNER,	NULL,	},
    177 	{ "game port",		PCI_SUBCLASS_INPUT_GAMEPORT,	NULL,	},
    178 	{ "miscellaneous",	PCI_SUBCLASS_INPUT_MISC,	NULL,	},
    179 	{ NULL,			0,				NULL,	},
    180 };
    181 
    182 static const struct pci_class pci_subclass_dock[] = {
    183 	{ "generic",		PCI_SUBCLASS_DOCK_GENERIC,	NULL,	},
    184 	{ "miscellaneous",	PCI_SUBCLASS_DOCK_MISC,		NULL,	},
    185 	{ NULL,			0,				NULL,	},
    186 };
    187 
    188 static const struct pci_class pci_subclass_processor[] = {
    189 	{ "386",		PCI_SUBCLASS_PROCESSOR_386,	NULL,	},
    190 	{ "486",		PCI_SUBCLASS_PROCESSOR_486,	NULL,	},
    191 	{ "Pentium",		PCI_SUBCLASS_PROCESSOR_PENTIUM, NULL,	},
    192 	{ "Alpha",		PCI_SUBCLASS_PROCESSOR_ALPHA,	NULL,	},
    193 	{ "PowerPC",		PCI_SUBCLASS_PROCESSOR_POWERPC, NULL,	},
    194 	{ "MIPS",		PCI_SUBCLASS_PROCESSOR_MIPS,	NULL,	},
    195 	{ "Co-processor",	PCI_SUBCLASS_PROCESSOR_COPROC,	NULL,	},
    196 	{ NULL,			0,				NULL,	},
    197 };
    198 
    199 static const struct pci_class pci_subclass_serialbus[] = {
    200 	{ "Firewire",		PCI_SUBCLASS_SERIALBUS_FIREWIRE, NULL,	},
    201 	{ "ACCESS.bus",		PCI_SUBCLASS_SERIALBUS_ACCESS,	NULL,	},
    202 	{ "SSA",		PCI_SUBCLASS_SERIALBUS_SSA,	NULL,	},
    203 	{ "USB",		PCI_SUBCLASS_SERIALBUS_USB,	NULL,	},
    204 	/* XXX Fiber Channel/_FIBRECHANNEL */
    205 	{ "Fiber Channel",	PCI_SUBCLASS_SERIALBUS_FIBER,	NULL,	},
    206 	{ "SMBus",		PCI_SUBCLASS_SERIALBUS_SMBUS,	NULL,	},
    207 	{ "InfiniBand",		PCI_SUBCLASS_SERIALBUS_INFINIBAND, NULL,},
    208 	{ "IPMI",		PCI_SUBCLASS_SERIALBUS_IPMI,	NULL,	},
    209 	{ "SERCOS",		PCI_SUBCLASS_SERIALBUS_SERCOS,	NULL,	},
    210 	{ "CANbus",		PCI_SUBCLASS_SERIALBUS_CANBUS,	NULL,	},
    211 	{ NULL,			0,				NULL,	},
    212 };
    213 
    214 static const struct pci_class pci_subclass_wireless[] = {
    215 	{ "IrDA",		PCI_SUBCLASS_WIRELESS_IRDA,	NULL,	},
    216 	{ "Consumer IR",	PCI_SUBCLASS_WIRELESS_CONSUMERIR, NULL,	},
    217 	{ "RF",			PCI_SUBCLASS_WIRELESS_RF,	NULL,	},
    218 	{ "bluetooth",		PCI_SUBCLASS_WIRELESS_BLUETOOTH, NULL,	},
    219 	{ "broadband",		PCI_SUBCLASS_WIRELESS_BROADBAND, NULL,	},
    220 	{ "802.11a (5 GHz)",	PCI_SUBCLASS_WIRELESS_802_11A,	NULL,	},
    221 	{ "802.11b (2.4 GHz)",	PCI_SUBCLASS_WIRELESS_802_11B,	NULL,	},
    222 	{ "miscellaneous",	PCI_SUBCLASS_WIRELESS_MISC,	NULL,	},
    223 	{ NULL,			0,				NULL,	},
    224 };
    225 
    226 static const struct pci_class pci_subclass_i2o[] = {
    227 	{ "standard",		PCI_SUBCLASS_I2O_STANDARD,	NULL,	},
    228 	{ NULL,			0,				NULL,	},
    229 };
    230 
    231 static const struct pci_class pci_subclass_satcom[] = {
    232 	{ "TV",			PCI_SUBCLASS_SATCOM_TV,	 	NULL,	},
    233 	{ "audio",		PCI_SUBCLASS_SATCOM_AUDIO, 	NULL,	},
    234 	{ "voice",		PCI_SUBCLASS_SATCOM_VOICE, 	NULL,	},
    235 	{ "data",		PCI_SUBCLASS_SATCOM_DATA,	NULL,	},
    236 	{ NULL,			0,				NULL,	},
    237 };
    238 
    239 static const struct pci_class pci_subclass_crypto[] = {
    240 	{ "network/computing",	PCI_SUBCLASS_CRYPTO_NETCOMP, 	NULL,	},
    241 	{ "entertainment",	PCI_SUBCLASS_CRYPTO_ENTERTAINMENT, NULL,},
    242 	{ "miscellaneous",	PCI_SUBCLASS_CRYPTO_MISC, 	NULL,	},
    243 	{ NULL,			0,				NULL,	},
    244 };
    245 
    246 static const struct pci_class pci_subclass_dasp[] = {
    247 	{ "DPIO",		PCI_SUBCLASS_DASP_DPIO,		NULL,	},
    248 	{ "Time and Frequency",	PCI_SUBCLASS_DASP_TIMEFREQ,	NULL,	},
    249 	{ "synchronization",	PCI_SUBCLASS_DASP_SYNC,		NULL,	},
    250 	{ "management",		PCI_SUBCLASS_DASP_MGMT,		NULL,	},
    251 	{ "miscellaneous",	PCI_SUBCLASS_DASP_MISC,		NULL,	},
    252 	{ NULL,			0,				NULL,	},
    253 };
    254 
    255 static const struct pci_class pci_class[] = {
    256 	{ "prehistoric",	PCI_CLASS_PREHISTORIC,
    257 	    pci_subclass_prehistoric,				},
    258 	{ "mass storage",	PCI_CLASS_MASS_STORAGE,
    259 	    pci_subclass_mass_storage,				},
    260 	{ "network",		PCI_CLASS_NETWORK,
    261 	    pci_subclass_network,				},
    262 	{ "display",		PCI_CLASS_DISPLAY,
    263 	    pci_subclass_display,				},
    264 	{ "multimedia",		PCI_CLASS_MULTIMEDIA,
    265 	    pci_subclass_multimedia,				},
    266 	{ "memory",		PCI_CLASS_MEMORY,
    267 	    pci_subclass_memory,				},
    268 	{ "bridge",		PCI_CLASS_BRIDGE,
    269 	    pci_subclass_bridge,				},
    270 	{ "communications",	PCI_CLASS_COMMUNICATIONS,
    271 	    pci_subclass_communications,			},
    272 	{ "system",		PCI_CLASS_SYSTEM,
    273 	    pci_subclass_system,				},
    274 	{ "input",		PCI_CLASS_INPUT,
    275 	    pci_subclass_input,					},
    276 	{ "dock",		PCI_CLASS_DOCK,
    277 	    pci_subclass_dock,					},
    278 	{ "processor",		PCI_CLASS_PROCESSOR,
    279 	    pci_subclass_processor,				},
    280 	{ "serial bus",		PCI_CLASS_SERIALBUS,
    281 	    pci_subclass_serialbus,				},
    282 	{ "wireless",		PCI_CLASS_WIRELESS,
    283 	    pci_subclass_wireless,				},
    284 	{ "I2O",		PCI_CLASS_I2O,
    285 	    pci_subclass_i2o,					},
    286 	{ "satellite comm",	PCI_CLASS_SATCOM,
    287 	    pci_subclass_satcom,				},
    288 	{ "crypto",		PCI_CLASS_CRYPTO,
    289 	    pci_subclass_crypto,				},
    290 	{ "DASP",		PCI_CLASS_DASP,
    291 	    pci_subclass_dasp,					},
    292 	{ "undefined",		PCI_CLASS_UNDEFINED,
    293 	    NULL,						},
    294 	{ NULL,			0,
    295 	    NULL,						},
    296 };
    297 
    298 void pci_load_verbose(void);
    299 
    300 #if defined(_KERNEL)
    301 /*
    302  * In kernel, these routines are provided and linked via the
    303  * pciverbose module.
    304  */
    305 const char *pci_findvendor_stub(pcireg_t);
    306 const char *pci_findproduct_stub(pcireg_t);
    307 
    308 const char *(*pci_findvendor)(pcireg_t) = pci_findvendor_stub;
    309 const char *(*pci_findproduct)(pcireg_t) = pci_findproduct_stub;
    310 const char *pci_unmatched = "";
    311 #else
    312 /*
    313  * For userland we just set the vectors here.
    314  */
    315 const char *(*pci_findvendor)(pcireg_t id_reg) = pci_findvendor_real;
    316 const char *(*pci_findproduct)(pcireg_t id_reg) = pci_findproduct_real;
    317 const char *pci_unmatched = "unmatched ";
    318 #endif
    319 
    320 int pciverbose_loaded = 0;
    321 
    322 #if defined(_KERNEL)
    323 /*
    324  * Routine to load the pciverbose kernel module as needed
    325  */
    326 void pci_load_verbose(void)
    327 {
    328 	if (pciverbose_loaded == 0)
    329 		module_autoload("pciverbose", MODULE_CLASS_MISC);
    330 }
    331 
    332 const char *pci_findvendor_stub(pcireg_t id_reg)
    333 {
    334 	pci_load_verbose();
    335 	if (pciverbose_loaded)
    336 		return pci_findvendor(id_reg);
    337 	else
    338 		return NULL;
    339 }
    340 
    341 const char *pci_findproduct_stub(pcireg_t id_reg)
    342 {
    343 	pci_load_verbose();
    344 	if (pciverbose_loaded)
    345 		return pci_findproduct(id_reg);
    346 	else
    347 		return NULL;
    348 }
    349 #endif
    350 
    351 void
    352 pci_devinfo(pcireg_t id_reg, pcireg_t class_reg, int showclass, char *cp,
    353     size_t l)
    354 {
    355 	pci_vendor_id_t vendor;
    356 	pci_product_id_t product;
    357 	pci_class_t class;
    358 	pci_subclass_t subclass;
    359 	pci_interface_t interface;
    360 	pci_revision_t revision;
    361 	const char *unmatched = pci_unmatched;
    362 	const char *vendor_namep, *product_namep;
    363 	const struct pci_class *classp, *subclassp;
    364 	char *ep;
    365 
    366 	ep = cp + l;
    367 
    368 	vendor = PCI_VENDOR(id_reg);
    369 	product = PCI_PRODUCT(id_reg);
    370 
    371 	class = PCI_CLASS(class_reg);
    372 	subclass = PCI_SUBCLASS(class_reg);
    373 	interface = PCI_INTERFACE(class_reg);
    374 	revision = PCI_REVISION(class_reg);
    375 
    376 	vendor_namep = pci_findvendor(id_reg);
    377 	product_namep = pci_findproduct(id_reg);
    378 
    379 	classp = pci_class;
    380 	while (classp->name != NULL) {
    381 		if (class == classp->val)
    382 			break;
    383 		classp++;
    384 	}
    385 
    386 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
    387 	while (subclassp && subclassp->name != NULL) {
    388 		if (subclass == subclassp->val)
    389 			break;
    390 		subclassp++;
    391 	}
    392 
    393 	if (vendor_namep == NULL)
    394 		cp += snprintf(cp, ep - cp, "%svendor 0x%04x product 0x%04x",
    395 		    unmatched, vendor, product);
    396 	else if (product_namep != NULL)
    397 		cp += snprintf(cp, ep - cp, "%s %s", vendor_namep,
    398 		    product_namep);
    399 	else
    400 		cp += snprintf(cp, ep - cp, "%s product 0x%04x",
    401 		    vendor_namep, product);
    402 	if (showclass) {
    403 		cp += snprintf(cp, ep - cp, " (");
    404 		if (classp->name == NULL)
    405 			cp += snprintf(cp, ep - cp,
    406 			    "class 0x%02x, subclass 0x%02x", class, subclass);
    407 		else {
    408 			if (subclassp == NULL || subclassp->name == NULL)
    409 				cp += snprintf(cp, ep - cp,
    410 				    "%s, subclass 0x%02x",
    411 				    classp->name, subclass);
    412 			else
    413 				cp += snprintf(cp, ep - cp, "%s %s",
    414 				    subclassp->name, classp->name);
    415 		}
    416 		if (interface != 0)
    417 			cp += snprintf(cp, ep - cp, ", interface 0x%02x",
    418 			    interface);
    419 		if (revision != 0)
    420 			cp += snprintf(cp, ep - cp, ", revision 0x%02x",
    421 			    revision);
    422 		cp += snprintf(cp, ep - cp, ")");
    423 	}
    424 }
    425 
    426 #ifdef _KERNEL
    427 void
    428 pci_aprint_devinfo_fancy(const struct pci_attach_args *pa, const char *naive,
    429 			 const char *known, int addrev)
    430 {
    431 	char devinfo[256];
    432 
    433 	if (known) {
    434 		aprint_normal(": %s", known);
    435 		if (addrev)
    436 			aprint_normal(" (rev. 0x%02x)",
    437 				      PCI_REVISION(pa->pa_class));
    438 		aprint_normal("\n");
    439 	} else {
    440 		pci_devinfo(pa->pa_id, pa->pa_class, 0,
    441 			    devinfo, sizeof(devinfo));
    442 		aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    443 			      PCI_REVISION(pa->pa_class));
    444 	}
    445 	if (naive)
    446 		aprint_naive(": %s\n", naive);
    447 	else
    448 		aprint_naive("\n");
    449 }
    450 #endif
    451 
    452 /*
    453  * Print out most of the PCI configuration registers.  Typically used
    454  * in a device attach routine like this:
    455  *
    456  *	#ifdef MYDEV_DEBUG
    457  *		printf("%s: ", device_xname(sc->sc_dev));
    458  *		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    459  *	#endif
    460  */
    461 
    462 #define	i2o(i)	((i) * 4)
    463 #define	o2i(o)	((o) / 4)
    464 #define	onoff2(str, bit, onstr, offstr)					\
    465 	printf("      %s: %s\n", (str), (rval & (bit)) ? onstr : offstr);
    466 #define	onoff(str, bit)	onoff2(str, bit, "on", "off")
    467 
    468 static void
    469 pci_conf_print_common(
    470 #ifdef _KERNEL
    471     pci_chipset_tag_t pc, pcitag_t tag,
    472 #endif
    473     const pcireg_t *regs)
    474 {
    475 	const char *name;
    476 	const struct pci_class *classp, *subclassp;
    477 	pcireg_t rval;
    478 
    479 	rval = regs[o2i(PCI_ID_REG)];
    480 	name = pci_findvendor(rval);
    481 	if (name)
    482 		printf("    Vendor Name: %s (0x%04x)\n", name,
    483 		    PCI_VENDOR(rval));
    484 	else
    485 		printf("    Vendor ID: 0x%04x\n", PCI_VENDOR(rval));
    486 	name = pci_findproduct(rval);
    487 	if (name)
    488 		printf("    Device Name: %s (0x%04x)\n", name,
    489 		    PCI_PRODUCT(rval));
    490 	else
    491 		printf("    Device ID: 0x%04x\n", PCI_PRODUCT(rval));
    492 
    493 	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];
    494 
    495 	printf("    Command register: 0x%04x\n", rval & 0xffff);
    496 	onoff("I/O space accesses", PCI_COMMAND_IO_ENABLE);
    497 	onoff("Memory space accesses", PCI_COMMAND_MEM_ENABLE);
    498 	onoff("Bus mastering", PCI_COMMAND_MASTER_ENABLE);
    499 	onoff("Special cycles", PCI_COMMAND_SPECIAL_ENABLE);
    500 	onoff("MWI transactions", PCI_COMMAND_INVALIDATE_ENABLE);
    501 	onoff("Palette snooping", PCI_COMMAND_PALETTE_ENABLE);
    502 	onoff("Parity error checking", PCI_COMMAND_PARITY_ENABLE);
    503 	onoff("Address/data stepping", PCI_COMMAND_STEPPING_ENABLE);
    504 	onoff("System error (SERR)", PCI_COMMAND_SERR_ENABLE);
    505 	onoff("Fast back-to-back transactions", PCI_COMMAND_BACKTOBACK_ENABLE);
    506 	onoff("Interrupt disable", PCI_COMMAND_INTERRUPT_DISABLE);
    507 
    508 	printf("    Status register: 0x%04x\n", (rval >> 16) & 0xffff);
    509 	onoff2("Interrupt status", PCI_STATUS_INT_STATUS, "active", "inactive");
    510 	onoff("Capability List support", PCI_STATUS_CAPLIST_SUPPORT);
    511 	onoff("66 MHz capable", PCI_STATUS_66MHZ_SUPPORT);
    512 	onoff("User Definable Features (UDF) support", PCI_STATUS_UDF_SUPPORT);
    513 	onoff("Fast back-to-back capable", PCI_STATUS_BACKTOBACK_SUPPORT);
    514 	onoff("Data parity error detected", PCI_STATUS_PARITY_ERROR);
    515 
    516 	printf("      DEVSEL timing: ");
    517 	switch (rval & PCI_STATUS_DEVSEL_MASK) {
    518 	case PCI_STATUS_DEVSEL_FAST:
    519 		printf("fast");
    520 		break;
    521 	case PCI_STATUS_DEVSEL_MEDIUM:
    522 		printf("medium");
    523 		break;
    524 	case PCI_STATUS_DEVSEL_SLOW:
    525 		printf("slow");
    526 		break;
    527 	default:
    528 		printf("unknown/reserved");	/* XXX */
    529 		break;
    530 	}
    531 	printf(" (0x%x)\n", (rval & PCI_STATUS_DEVSEL_MASK) >> 25);
    532 
    533 	onoff("Slave signaled Target Abort", PCI_STATUS_TARGET_TARGET_ABORT);
    534 	onoff("Master received Target Abort", PCI_STATUS_MASTER_TARGET_ABORT);
    535 	onoff("Master received Master Abort", PCI_STATUS_MASTER_ABORT);
    536 	onoff("Asserted System Error (SERR)", PCI_STATUS_SPECIAL_ERROR);
    537 	onoff("Parity error detected", PCI_STATUS_PARITY_DETECT);
    538 
    539 	rval = regs[o2i(PCI_CLASS_REG)];
    540 	for (classp = pci_class; classp->name != NULL; classp++) {
    541 		if (PCI_CLASS(rval) == classp->val)
    542 			break;
    543 	}
    544 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
    545 	while (subclassp && subclassp->name != NULL) {
    546 		if (PCI_SUBCLASS(rval) == subclassp->val)
    547 			break;
    548 		subclassp++;
    549 	}
    550 	if (classp->name != NULL) {
    551 		printf("    Class Name: %s (0x%02x)\n", classp->name,
    552 		    PCI_CLASS(rval));
    553 		if (subclassp != NULL && subclassp->name != NULL)
    554 			printf("    Subclass Name: %s (0x%02x)\n",
    555 			    subclassp->name, PCI_SUBCLASS(rval));
    556 		else
    557 			printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
    558 	} else {
    559 		printf("    Class ID: 0x%02x\n", PCI_CLASS(rval));
    560 		printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
    561 	}
    562 	printf("    Interface: 0x%02x\n", PCI_INTERFACE(rval));
    563 	printf("    Revision ID: 0x%02x\n", PCI_REVISION(rval));
    564 
    565 	rval = regs[o2i(PCI_BHLC_REG)];
    566 	printf("    BIST: 0x%02x\n", PCI_BIST(rval));
    567 	printf("    Header Type: 0x%02x%s (0x%02x)\n", PCI_HDRTYPE_TYPE(rval),
    568 	    PCI_HDRTYPE_MULTIFN(rval) ? "+multifunction" : "",
    569 	    PCI_HDRTYPE(rval));
    570 	printf("    Latency Timer: 0x%02x\n", PCI_LATTIMER(rval));
    571 	printf("    Cache Line Size: 0x%02x\n", PCI_CACHELINE(rval));
    572 }
    573 
    574 static int
    575 pci_conf_print_bar(
    576 #ifdef _KERNEL
    577     pci_chipset_tag_t pc, pcitag_t tag,
    578 #endif
    579     const pcireg_t *regs, int reg, const char *name
    580 #ifdef _KERNEL
    581     , int sizebar
    582 #endif
    583     )
    584 {
    585 	int width;
    586 	pcireg_t rval, rval64h;
    587 #ifdef _KERNEL
    588 	int s;
    589 	pcireg_t mask, mask64h;
    590 #endif
    591 
    592 	width = 4;
    593 
    594 	/*
    595 	 * Section 6.2.5.1, `Address Maps', tells us that:
    596 	 *
    597 	 * 1) The builtin software should have already mapped the
    598 	 * device in a reasonable way.
    599 	 *
    600 	 * 2) A device which wants 2^n bytes of memory will hardwire
    601 	 * the bottom n bits of the address to 0.  As recommended,
    602 	 * we write all 1s and see what we get back.
    603 	 */
    604 
    605 	rval = regs[o2i(reg)];
    606 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
    607 	    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
    608 		rval64h = regs[o2i(reg + 4)];
    609 		width = 8;
    610 	} else
    611 		rval64h = 0;
    612 
    613 #ifdef _KERNEL
    614 	/* XXX don't size unknown memory type? */
    615 	if (rval != 0 && sizebar) {
    616 		/*
    617 		 * The following sequence seems to make some devices
    618 		 * (e.g. host bus bridges, which don't normally
    619 		 * have their space mapped) very unhappy, to
    620 		 * the point of crashing the system.
    621 		 *
    622 		 * Therefore, if the mapping register is zero to
    623 		 * start out with, don't bother trying.
    624 		 */
    625 		s = splhigh();
    626 		pci_conf_write(pc, tag, reg, 0xffffffff);
    627 		mask = pci_conf_read(pc, tag, reg);
    628 		pci_conf_write(pc, tag, reg, rval);
    629 		if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
    630 		    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
    631 			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    632 			mask64h = pci_conf_read(pc, tag, reg + 4);
    633 			pci_conf_write(pc, tag, reg + 4, rval64h);
    634 		} else
    635 			mask64h = 0;
    636 		splx(s);
    637 	} else
    638 		mask = mask64h = 0;
    639 #endif /* _KERNEL */
    640 
    641 	printf("    Base address register at 0x%02x", reg);
    642 	if (name)
    643 		printf(" (%s)", name);
    644 	printf("\n      ");
    645 	if (rval == 0) {
    646 		printf("not implemented(?)\n");
    647 		return width;
    648 	}
    649 	printf("type: ");
    650 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM) {
    651 		const char *type, *prefetch;
    652 
    653 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
    654 		case PCI_MAPREG_MEM_TYPE_32BIT:
    655 			type = "32-bit";
    656 			break;
    657 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    658 			type = "32-bit-1M";
    659 			break;
    660 		case PCI_MAPREG_MEM_TYPE_64BIT:
    661 			type = "64-bit";
    662 			break;
    663 		default:
    664 			type = "unknown (XXX)";
    665 			break;
    666 		}
    667 		if (PCI_MAPREG_MEM_PREFETCHABLE(rval))
    668 			prefetch = "";
    669 		else
    670 			prefetch = "non";
    671 		printf("%s %sprefetchable memory\n", type, prefetch);
    672 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
    673 		case PCI_MAPREG_MEM_TYPE_64BIT:
    674 			printf("      base: 0x%016llx, ",
    675 			    PCI_MAPREG_MEM64_ADDR(
    676 				((((long long) rval64h) << 32) | rval)));
    677 #ifdef _KERNEL
    678 			if (sizebar)
    679 				printf("size: 0x%016llx",
    680 				    PCI_MAPREG_MEM64_SIZE(
    681 				      ((((long long) mask64h) << 32) | mask)));
    682 			else
    683 #endif /* _KERNEL */
    684 				printf("not sized");
    685 			printf("\n");
    686 			break;
    687 		case PCI_MAPREG_MEM_TYPE_32BIT:
    688 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    689 		default:
    690 			printf("      base: 0x%08x, ",
    691 			    PCI_MAPREG_MEM_ADDR(rval));
    692 #ifdef _KERNEL
    693 			if (sizebar)
    694 				printf("size: 0x%08x",
    695 				    PCI_MAPREG_MEM_SIZE(mask));
    696 			else
    697 #endif /* _KERNEL */
    698 				printf("not sized");
    699 			printf("\n");
    700 			break;
    701 		}
    702 	} else {
    703 #ifdef _KERNEL
    704 		if (sizebar)
    705 			printf("%d-bit ", mask & ~0x0000ffff ? 32 : 16);
    706 #endif /* _KERNEL */
    707 		printf("i/o\n");
    708 		printf("      base: 0x%08x, ", PCI_MAPREG_IO_ADDR(rval));
    709 #ifdef _KERNEL
    710 		if (sizebar)
    711 			printf("size: 0x%08x", PCI_MAPREG_IO_SIZE(mask));
    712 		else
    713 #endif /* _KERNEL */
    714 			printf("not sized");
    715 		printf("\n");
    716 	}
    717 
    718 	return width;
    719 }
    720 
    721 static void
    722 pci_conf_print_regs(const pcireg_t *regs, int first, int pastlast)
    723 {
    724 	int off, needaddr, neednl;
    725 
    726 	needaddr = 1;
    727 	neednl = 0;
    728 	for (off = first; off < pastlast; off += 4) {
    729 		if ((off % 16) == 0 || needaddr) {
    730 			printf("    0x%02x:", off);
    731 			needaddr = 0;
    732 		}
    733 		printf(" 0x%08x", regs[o2i(off)]);
    734 		neednl = 1;
    735 		if ((off % 16) == 12) {
    736 			printf("\n");
    737 			neednl = 0;
    738 		}
    739 	}
    740 	if (neednl)
    741 		printf("\n");
    742 }
    743 
    744 static void
    745 pci_conf_print_type0(
    746 #ifdef _KERNEL
    747     pci_chipset_tag_t pc, pcitag_t tag,
    748 #endif
    749     const pcireg_t *regs
    750 #ifdef _KERNEL
    751     , int sizebars
    752 #endif
    753     )
    754 {
    755 	int off, width;
    756 	pcireg_t rval;
    757 
    758 	for (off = PCI_MAPREG_START; off < PCI_MAPREG_END; off += width) {
    759 #ifdef _KERNEL
    760 		width = pci_conf_print_bar(pc, tag, regs, off, NULL, sizebars);
    761 #else
    762 		width = pci_conf_print_bar(regs, off, NULL);
    763 #endif
    764 	}
    765 
    766 	printf("    Cardbus CIS Pointer: 0x%08x\n", regs[o2i(0x28)]);
    767 
    768 	rval = regs[o2i(PCI_SUBSYS_ID_REG)];
    769 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
    770 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
    771 
    772 	/* XXX */
    773 	printf("    Expansion ROM Base Address: 0x%08x\n", regs[o2i(0x30)]);
    774 
    775 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
    776 		printf("    Capability list pointer: 0x%02x\n",
    777 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
    778 	else
    779 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
    780 
    781 	printf("    Reserved @ 0x38: 0x%08x\n", regs[o2i(0x38)]);
    782 
    783 	rval = regs[o2i(PCI_INTERRUPT_REG)];
    784 	printf("    Maximum Latency: 0x%02x\n", (rval >> 24) & 0xff);
    785 	printf("    Minimum Grant: 0x%02x\n", (rval >> 16) & 0xff);
    786 	printf("    Interrupt pin: 0x%02x ", PCI_INTERRUPT_PIN(rval));
    787 	switch (PCI_INTERRUPT_PIN(rval)) {
    788 	case PCI_INTERRUPT_PIN_NONE:
    789 		printf("(none)");
    790 		break;
    791 	case PCI_INTERRUPT_PIN_A:
    792 		printf("(pin A)");
    793 		break;
    794 	case PCI_INTERRUPT_PIN_B:
    795 		printf("(pin B)");
    796 		break;
    797 	case PCI_INTERRUPT_PIN_C:
    798 		printf("(pin C)");
    799 		break;
    800 	case PCI_INTERRUPT_PIN_D:
    801 		printf("(pin D)");
    802 		break;
    803 	default:
    804 		printf("(? ? ?)");
    805 		break;
    806 	}
    807 	printf("\n");
    808 	printf("    Interrupt line: 0x%02x\n", PCI_INTERRUPT_LINE(rval));
    809 }
    810 
    811 static void
    812 pci_print_pcie_L0s_latency(uint32_t val)
    813 {
    814 
    815 	switch (val) {
    816 	case 0x0:
    817 		printf("Less than 64ns\n");
    818 		break;
    819 	case 0x1:
    820 	case 0x2:
    821 	case 0x3:
    822 		printf("%dns to less than %dns\n", 32 << val, 32 << (val + 1));
    823 		break;
    824 	case 0x4:
    825 		printf("512ns to less than 1us\n");
    826 		break;
    827 	case 0x5:
    828 		printf("1us to less than 2us\n");
    829 		break;
    830 	case 0x6:
    831 		printf("2us - 4us\n");
    832 		break;
    833 	case 0x7:
    834 		printf("More than 4us\n");
    835 		break;
    836 	}
    837 }
    838 
    839 static void
    840 pci_print_pcie_L1_latency(uint32_t val)
    841 {
    842 
    843 	switch (val) {
    844 	case 0x0:
    845 		printf("Less than 1us\n");
    846 		break;
    847 	case 0x6:
    848 		printf("32us - 64us\n");
    849 		break;
    850 	case 0x7:
    851 		printf("More than 64us\n");
    852 		break;
    853 	default:
    854 		printf("%dus to less than %dus\n", 1 << (val - 1), 1 << val);
    855 		break;
    856 	}
    857 }
    858 
    859 static void
    860 pci_conf_print_pcie_cap(const pcireg_t *regs, int capoff)
    861 {
    862 	pcireg_t val;
    863 	bool check_slot = false;
    864 	static const char * const linkspeeds[] = {"2.5", "5.0", "8.0"};
    865 
    866 	printf("\n  PCI Express Capabilities Register\n");
    867 	/* Capability Register */
    868 	printf("    Capability register: %04x\n",
    869 	    regs[o2i(capoff)] >> 16);
    870 	printf("      Capability version: %x\n",
    871 	    (unsigned int)((regs[o2i(capoff)] & 0x000f0000) >> 16));
    872 	printf("      Device type: ");
    873 	switch ((regs[o2i(capoff)] & 0x00f00000) >> 20) {
    874 	case 0x0:
    875 		printf("PCI Express Endpoint device\n");
    876 		break;
    877 	case 0x1:
    878 		printf("Legacy PCI Express Endpoint device\n");
    879 		break;
    880 	case 0x4:
    881 		printf("Root Port of PCI Express Root Complex\n");
    882 		check_slot = true;
    883 		break;
    884 	case 0x5:
    885 		printf("Upstream Port of PCI Express Switch\n");
    886 		break;
    887 	case 0x6:
    888 		printf("Downstream Port of PCI Express Switch\n");
    889 		check_slot = true;
    890 		break;
    891 	case 0x7:
    892 		printf("PCI Express to PCI/PCI-X Bridge\n");
    893 		break;
    894 	case 0x8:
    895 		printf("PCI/PCI-X to PCI Express Bridge\n");
    896 		break;
    897 	case 0x9:
    898 		printf("Root Complex Integrated Endpoint\n");
    899 		break;
    900 	case 0xa:
    901 		printf("Root Complex Event Collector\n");
    902 		break;
    903 	default:
    904 		printf("unknown\n");
    905 		break;
    906 	}
    907 	if (check_slot && (regs[o2i(capoff)] & 0x01000000) != 0)
    908 		printf("      Slot implemented\n");
    909 	printf("      Interrupt Message Number: %x\n",
    910 	    (unsigned int)((regs[o2i(capoff)] & PCI_PCIE_XCAP_IRQ) >> 27));
    911 
    912 	/* Device Capability Register */
    913 	printf("    Device Capabilities Register: 0x%08x\n",
    914 	    regs[o2i(capoff + PCI_PCIE_DCAP)]);
    915 	printf("      Max Payload Size Supported: %u bytes max\n",
    916 	    (unsigned int)(regs[o2i(capoff + PCI_PCIE_DCAP)]
    917 		& PCI_PCIE_DCAP_MAX_PAYLOAD) * 256);
    918 	printf("      Phantom Functions Supported: ");
    919 	switch ((regs[o2i(capoff + PCI_PCIE_DCAP)]
    920 		& PCI_PCIE_DCAP_PHANTOM_FUNCS) >> 3) {
    921 	case 0x0:
    922 		printf("not available\n");
    923 		break;
    924 	case 0x1:
    925 		printf("MSB\n");
    926 		break;
    927 	case 0x2:
    928 		printf("two MSB\n");
    929 		break;
    930 	case 0x3:
    931 		printf("All three bits\n");
    932 		break;
    933 	}
    934 	printf("      Extended Tag Field Supported: %dbit\n",
    935 	    (regs[o2i(capoff + PCI_PCIE_DCAP)] & PCI_PCIE_DCAP_EXT_TAG_FIELD)
    936 	    == 0 ? 5 : 8);
    937 	printf("      Endpoint L0 Acceptable Latency: ");
    938 	pci_print_pcie_L0s_latency((regs[o2i(capoff + PCI_PCIE_DCAP)]
    939 		& PCI_PCIE_DCAP_L0S_LATENCY) >> 6);
    940 	printf("      Endpoint L1 Acceptable Latency: ");
    941 	pci_print_pcie_L1_latency((regs[o2i(capoff + PCI_PCIE_DCAP)]
    942 		& PCI_PCIE_DCAP_L1_LATENCY) >> 9);
    943 	printf("      Attention Button Present: %s\n",
    944 	    (regs[o2i(capoff + PCI_PCIE_DCAP)]
    945 		& PCI_PCIE_DCAP_ATTN_BUTTON) != 0 ? "yes" : "no");
    946 	printf("      Attention Indicator Present: %s\n",
    947 	    (regs[o2i(capoff + PCI_PCIE_DCAP)]
    948 		& PCI_PCIE_DCAP_ATTN_IND) != 0 ? "yes" : "no");
    949 	printf("      Power Indicator Present: %s\n",
    950 	    (regs[o2i(capoff + PCI_PCIE_DCAP)]
    951 		& PCI_PCIE_DCAP_PWR_IND) != 0 ? "yes" : "no");
    952 	printf("      Role-Based Error Report: %s\n",
    953 	    (regs[o2i(capoff + PCI_PCIE_DCAP)]
    954 		& PCI_PCIE_DCAP_ROLE_ERR_RPT) != 0 ? "yes" : "no");
    955 	printf("      Captured Slot Power Limit Value: %d\n",
    956 	    (unsigned int)(regs[o2i(capoff + PCI_PCIE_DCAP)]
    957 		& PCI_PCIE_DCAP_SLOT_PWR_LIM_VAL) >> 18);
    958 	printf("      Captured Slot Power Limit Scale: %d\n",
    959 	    (unsigned int)(regs[o2i(capoff + PCI_PCIE_DCAP)]
    960 		& PCI_PCIE_DCAP_SLOT_PWR_LIM_SCALE) >> 26);
    961 	printf("      Function-Level Reset Capability: %s\n",
    962 	    (regs[o2i(capoff + PCI_PCIE_DCAP)]
    963 		& PCI_PCIE_DCAP_FLR) != 0 ? "yes" : "no");
    964 
    965 	/* Device Control Register */
    966 	printf("    Device Control Register: 0x%04x\n",
    967 	    regs[o2i(capoff + PCI_PCIE_DCSR)] & 0xffff);
    968 	printf("      Correctable Error Reporting Enable: %s\n",
    969 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    970 		& PCI_PCIE_DCSR_ENA_COR_ERR) != 0 ? "on" : "off");
    971 	printf("      Non Fatal Error Reporting Enable: %s\n",
    972 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    973 		& PCI_PCIE_DCSR_ENA_NFER) != 0 ? "on" : "off");
    974 	printf("      Fatal Error Reporting Enable: %s\n",
    975 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    976 		& PCI_PCIE_DCSR_ENA_FER) != 0 ? "on" : "off");
    977 	printf("      Unsupported Request Reporting Enable: %s\n",
    978 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    979 		& PCI_PCIE_DCSR_ENA_URR) != 0 ? "on" : "off");
    980 	printf("      Enable Relaxed Ordering: %s\n",
    981 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    982 		& PCI_PCIE_DCSR_ENA_RELAX_ORD) != 0 ? "on" : "off");
    983 	printf("      Max Payload Size: %d byte\n",
    984 	    128 << (((unsigned int)(regs[o2i(capoff + PCI_PCIE_DCSR)]
    985 			& PCI_PCIE_DCSR_MAX_PAYLOAD) >> 5)));
    986 	printf("      Extended Tag Field Enable: %s\n",
    987 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    988 		& PCI_PCIE_DCSR_EXT_TAG_FIELD) != 0 ? "on" : "off");
    989 	printf("      Phantom Functions Enable: %s\n",
    990 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    991 		& PCI_PCIE_DCSR_PHANTOM_FUNCS) != 0 ? "on" : "off");
    992 	printf("      Aux Power PM Enable: %s\n",
    993 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    994 		& PCI_PCIE_DCSR_AUX_POWER_PM) != 0 ? "on" : "off");
    995 	printf("      Enable No Snoop: %s\n",
    996 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
    997 		& PCI_PCIE_DCSR_ENA_NO_SNOOP) != 0 ? "on" : "off");
    998 	printf("      Max Read Request Size: %d byte\n",
    999 	    128 << ((unsigned int)(regs[o2i(capoff + PCI_PCIE_DCSR)]
   1000 		    & PCI_PCIE_DCSR_MAX_READ_REQ) >> 12));
   1001 
   1002 	/* Device Status Register */
   1003 	printf("    Device Status Register: 0x%04x\n",
   1004 	    regs[o2i(capoff + PCI_PCIE_DCSR)] >> 16);
   1005 	printf("      Correctable Error Detected: %s\n",
   1006 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1007 		& PCI_PCIE_DCSR_CED) != 0 ? "on" : "off");
   1008 	printf("      Non Fatal Error Detected: %s\n",
   1009 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1010 		& PCI_PCIE_DCSR_NFED) != 0 ? "on" : "off");
   1011 	printf("      Fatal Error Detected: %s\n",
   1012 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1013 		& PCI_PCIE_DCSR_FED) != 0 ? "on" : "off");
   1014 	printf("      Unsupported Request Detected: %s\n",
   1015 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1016 		& PCI_PCIE_DCSR_URD) != 0 ? "on" : "off");
   1017 	printf("      Aux Power Detected: %s\n",
   1018 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1019 		& PCI_PCIE_DCSR_AUX_PWR) != 0 ? "on" : "off");
   1020 	printf("      Transaction Pending: %s\n",
   1021 	    (regs[o2i(capoff + PCI_PCIE_DCSR)]
   1022 		& PCI_PCIE_DCSR_TRANSACTION_PND) != 0 ? "on" : "off");
   1023 
   1024 	/* Link Capability Register */
   1025 	printf("    Link Capabilities Register: 0x%08x\n",
   1026 	    regs[o2i(capoff + PCI_PCIE_LCAP)]);
   1027 	printf("      Maximum Link Speed: ");
   1028 	if ((regs[o2i(capoff + PCI_PCIE_LCAP)] & 0x000f) < 1 ||
   1029 	    (regs[o2i(capoff + PCI_PCIE_LCAP)] & 0x000f) > 3) {
   1030 		printf("unknown %u value\n",
   1031 		    (regs[o2i(capoff + PCI_PCIE_LCAP)] & 0x000f));
   1032 	} else {
   1033 		printf("%sGb/s\n",
   1034 		    linkspeeds[(regs[o2i(capoff + PCI_PCIE_LCAP)] & 0x000f)
   1035 			- 1]);
   1036 	}
   1037 	printf("      Maximum Link Width: x%u lanes\n",
   1038 	    (regs[o2i(capoff + PCI_PCIE_LCAP)] & 0x03f0) >> 4);
   1039 	printf("      Active State PM Support: ");
   1040 	val = (regs[o2i(capoff + PCI_PCIE_LCAP)] & PCI_PCIE_LCAP_ASPM) >> 10;
   1041 	switch (val) {
   1042 	case 0x1:
   1043 		printf("L0s Entry supported\n");
   1044 		break;
   1045 	case 0x3:
   1046 		printf("L0s and L1 supported\n");
   1047 		break;
   1048 	default:
   1049 		printf("Reserved value\n");
   1050 		break;
   1051 	}
   1052 	printf("      L0 Exit Latency: ");
   1053 	pci_print_pcie_L0s_latency((regs[o2i(capoff + PCI_PCIE_LCAP)]
   1054 		& PCI_PCIE_LCAP_L0S_EXIT) >> 12);
   1055 	printf("      L1 Exit Latency: ");
   1056 	pci_print_pcie_L1_latency((regs[o2i(capoff + PCI_PCIE_LCAP)]
   1057 		& PCI_PCIE_LCAP_L1_EXIT) >> 15);
   1058 	printf("      Port Number: %u\n",
   1059 	    regs[o2i(capoff + PCI_PCIE_LCAP)] >> 24);
   1060 
   1061 	/* Link Control Register */
   1062 	printf("    Link Control Register: 0x%04x\n",
   1063 	    regs[o2i(capoff + PCI_PCIE_LCSR)] & 0xffff);
   1064 	printf("      Active State PM Control: ");
   1065 	val = regs[o2i(capoff + PCI_PCIE_LCSR)]
   1066 	    & (PCI_PCIE_LCSR_ASPM_L1 | PCI_PCIE_LCSR_ASPM_L0S);
   1067 	switch (val) {
   1068 	case 0:
   1069 		printf("disabled\n");
   1070 		break;
   1071 	case 1:
   1072 		printf("L0s Entry Enabled\n");
   1073 		break;
   1074 	case 2:
   1075 		printf("L1 Entry Enabled\n");
   1076 		break;
   1077 	case 3:
   1078 		printf("L0s and L1 Entry Enabled\n");
   1079 		break;
   1080 	}
   1081 	printf("      Read Completion Boundary Control: %dbyte\n",
   1082 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1083 		& PCI_PCIE_LCSR_RCB) != 0 ? 128 : 64);
   1084 	printf("      Link Disable: %s\n",
   1085 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1086 		& PCI_PCIE_LCSR_LINK_DIS) != 0 ? "on" : "off");
   1087 	printf("      Retrain Link: %s\n",
   1088 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1089 		& PCI_PCIE_LCSR_RETRAIN) != 0 ? "on" : "off");
   1090 	printf("      Common Clock Configuration: %s\n",
   1091 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1092 		& PCI_PCIE_LCSR_COMCLKCFG) != 0 ? "on" : "off");
   1093 	printf("      Extended Synch: %s\n",
   1094 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1095 		& PCI_PCIE_LCSR_EXTNDSYNC) != 0 ? "on" : "off");
   1096 	printf("      Enable Clock Power Management: %s\n",
   1097 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1098 		& PCI_PCIE_LCSR_ENCLKPM) != 0 ? "on" : "off");
   1099 	printf("      Hardware Autonomous Width Disable: %s\n",
   1100 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1101 		& PCI_PCIE_LCSR_HAWD) != 0 ? "on" : "off");
   1102 	printf("      Link Bandwidth Management Interrupt Enable: %s\n",
   1103 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1104 		& PCI_PCIE_LCSR_LBMIE) != 0 ? "on" : "off");
   1105 	printf("      Link Autonomous Bandwidth Interrupt Enable: %s\n",
   1106 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1107 		& PCI_PCIE_LCSR_LABIE) != 0 ? "on" : "off");
   1108 
   1109 	/* Link Status Register */
   1110 	printf("    Link Status Register: 0x%04x\n",
   1111 	    regs[o2i(capoff + PCI_PCIE_LCSR)] >> 16);
   1112 	printf("      Negotiated Link Speed: ");
   1113 	if (((regs[o2i(capoff + PCI_PCIE_LCSR)] >> 16) & 0x000f) < 1 ||
   1114 	    ((regs[o2i(capoff + PCI_PCIE_LCSR)] >> 16) & 0x000f) > 3) {
   1115 		printf("unknown %u value\n",
   1116 		    (regs[o2i(capoff + PCI_PCIE_LCSR)] >> 16) & 0x000f);
   1117 	} else {
   1118 		printf("%sGb/s\n",
   1119 		    linkspeeds[((regs[o2i(capoff + PCI_PCIE_LCSR)] >> 16)
   1120 				& 0x000f) - 1]);
   1121 	}
   1122 	printf("      Negotiated Link Width: x%u lanes\n",
   1123 	    (regs[o2i(capoff + PCI_PCIE_LCSR)] >> 20) & 0x003f);
   1124 	printf("      Training Error: %s\n",
   1125 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1126 		& PCI_PCIE_LCSR_LINKTRAIN_ERR) != 0 ? "on" : "off");
   1127 	printf("      Link Training: %s\n",
   1128 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1129 		& PCI_PCIE_LCSR_LINKTRAIN) != 0 ? "on" : "off");
   1130 	printf("      Slot Clock Configuration: %s\n",
   1131 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1132 		& PCI_PCIE_LCSR_SLOTCLKCFG) != 0 ? "on" : "off");
   1133 	printf("      Data Link Layer Link Active: %s\n",
   1134 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1135 		& PCI_PCIE_LCSR_DLACTIVE) != 0 ? "on" : "off");
   1136 	printf("      Link Bandwidth Management Status: %s\n",
   1137 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1138 		& PCI_PCIE_LCSR_LINK_BW_MGMT) != 0 ? "on" : "off");
   1139 	printf("      Link Autonomous Bandwidth Status: %s\n",
   1140 	    (regs[o2i(capoff + PCI_PCIE_LCSR)]
   1141 		& PCI_PCIE_LCSR_LINK_AUTO_BW) != 0 ? "on" : "off");
   1142 
   1143 	/* Slot Control Register */
   1144 	if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x07ff) != 0) {
   1145 		printf("    Slot Control Register:\n");
   1146 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0001) != 0)
   1147 			printf("      Attention Button Pressed Enabled\n");
   1148 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0002) != 0)
   1149 			printf("      Power Fault Detected Enabled\n");
   1150 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0004) != 0)
   1151 			printf("      MRL Sensor Changed Enabled\n");
   1152 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0008) != 0)
   1153 			printf("      Presense Detected Changed Enabled\n");
   1154 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0010) != 0)
   1155 			printf("      Command Completed Interrupt Enabled\n");
   1156 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0020) != 0)
   1157 			printf("      Hot-Plug Interrupt Enabled\n");
   1158 		printf("      Attention Indicator Control: ");
   1159 		switch ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x00c0) >> 6) {
   1160 		case 0x0:
   1161 			printf("reserved\n");
   1162 			break;
   1163 		case 0x1:
   1164 			printf("on\n");
   1165 			break;
   1166 		case 0x2:
   1167 			printf("blink\n");
   1168 			break;
   1169 		case 0x3:
   1170 			printf("off\n");
   1171 			break;
   1172 		}
   1173 		printf("      Power Indicator Control: ");
   1174 		switch ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0300) >> 8) {
   1175 		case 0x0:
   1176 			printf("reserved\n");
   1177 			break;
   1178 		case 0x1:
   1179 			printf("on\n");
   1180 			break;
   1181 		case 0x2:
   1182 			printf("blink\n");
   1183 			break;
   1184 		case 0x3:
   1185 			printf("off\n");
   1186 			break;
   1187 		}
   1188 		printf("      Power Controller Control: ");
   1189 		if ((regs[o2i(capoff + PCI_PCIE_SLCSR)] & 0x0400) != 0)
   1190 			printf("off\n");
   1191 		else
   1192 			printf("on\n");
   1193 	}
   1194 }
   1195 
   1196 static const char *
   1197 pci_conf_print_pcipm_cap_aux(uint16_t caps)
   1198 {
   1199 	switch ((caps >> 6) & 7) {
   1200 	case 0:	return "self-powered";
   1201 	case 1: return "55 mA";
   1202 	case 2: return "100 mA";
   1203 	case 3: return "160 mA";
   1204 	case 4: return "220 mA";
   1205 	case 5: return "270 mA";
   1206 	case 6: return "320 mA";
   1207 	case 7:
   1208 	default: return "375 mA";
   1209 	}
   1210 }
   1211 
   1212 static const char *
   1213 pci_conf_print_pcipm_cap_pmrev(uint8_t val)
   1214 {
   1215 	static const char unk[] = "unknown";
   1216 	static const char *pmrev[8] = {
   1217 		unk, "1.0", "1.1", "1.2", unk, unk, unk, unk
   1218 	};
   1219 	if (val > 7)
   1220 		return unk;
   1221 	return pmrev[val];
   1222 }
   1223 
   1224 static void
   1225 pci_conf_print_pcipm_cap(const pcireg_t *regs, int capoff)
   1226 {
   1227 	uint16_t caps, pmcsr;
   1228 
   1229 	caps = regs[o2i(capoff)] >> 16;
   1230 	pmcsr = regs[o2i(capoff + 0x04)] & 0xffff;
   1231 
   1232 	printf("\n  PCI Power Management Capabilities Register\n");
   1233 
   1234 	printf("    Capabilities register: 0x%04x\n", caps);
   1235 	printf("      Version: %s\n",
   1236 	    pci_conf_print_pcipm_cap_pmrev(caps & 0x3));
   1237 	printf("      PME# clock: %s\n", caps & 0x4 ? "on" : "off");
   1238 	printf("      Device specific initialization: %s\n",
   1239 	    caps & 0x20 ? "on" : "off");
   1240 	printf("      3.3V auxiliary current: %s\n",
   1241 	    pci_conf_print_pcipm_cap_aux(caps));
   1242 	printf("      D1 power management state support: %s\n",
   1243 	    (caps >> 9) & 1 ? "on" : "off");
   1244 	printf("      D2 power management state support: %s\n",
   1245 	    (caps >> 10) & 1 ? "on" : "off");
   1246 	printf("      PME# support: 0x%02x\n", caps >> 11);
   1247 
   1248 	printf("    Control/status register: 0x%04x\n", pmcsr);
   1249 	printf("      Power state: D%d\n", pmcsr & 3);
   1250 	printf("      PCI Express reserved: %s\n",
   1251 	    (pmcsr >> 2) & 1 ? "on" : "off");
   1252 	printf("      No soft reset: %s\n", (pmcsr >> 3) & 1 ? "on" : "off");
   1253 	printf("      PME# assertion %sabled\n",
   1254 	    (pmcsr >> 8) & 1 ? "en" : "dis");
   1255 	printf("      PME# status: %s\n", (pmcsr >> 15) ? "on" : "off");
   1256 }
   1257 
   1258 static void
   1259 pci_conf_print_msi_cap(const pcireg_t *regs, int capoff)
   1260 {
   1261 	uint32_t ctl, mmc, mme;
   1262 
   1263 	regs += o2i(capoff);
   1264 	ctl = *regs++;
   1265 	mmc = __SHIFTOUT(ctl, PCI_MSI_CTL_MMC_MASK);
   1266 	mme = __SHIFTOUT(ctl, PCI_MSI_CTL_MME_MASK);
   1267 
   1268 	printf("\n  PCI Message Signaled Interrupt\n");
   1269 
   1270 	printf("    Message Control register: 0x%04x\n", ctl >> 16);
   1271 	printf("      MSI Enabled: %s\n",
   1272 	    ctl & PCI_MSI_CTL_MSI_ENABLE ? "yes" : "no");
   1273 	printf("      Multiple Message Capable: %s (%d vector%s)\n",
   1274 	    mmc > 0 ? "yes" : "no", 1 << mmc, mmc > 0 ? "s" : "");
   1275 	printf("      Multiple Message Enabled: %s (%d vector%s)\n",
   1276 	    mme > 0 ? "on" : "off", 1 << mme, mme > 0 ? "s" : "");
   1277 	printf("      64 Bit Address Capable: %s\n",
   1278 	    ctl & PCI_MSI_CTL_64BIT_ADDR ? "yes" : "no");
   1279 	printf("      Per-Vector Masking Capable: %s\n",
   1280 	    ctl & PCI_MSI_CTL_PERVEC_MASK ? "yes" : "no");
   1281 	printf("    Message Address %sregister: 0x%08x\n",
   1282 	    ctl & PCI_MSI_CTL_64BIT_ADDR ? "(lower) " : "", *regs++);
   1283 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
   1284 		printf("    Message Address %sregister: 0x%08x\n",
   1285 		    "(upper) ", *regs++);
   1286 	}
   1287 	printf("    Message Data register: 0x%08x\n", *regs++);
   1288 	if (ctl & PCI_MSI_CTL_PERVEC_MASK) {
   1289 		printf("    Vector Mask register: 0x%08x\n", *regs++);
   1290 		printf("    Vector Pending register: 0x%08x\n", *regs++);
   1291 	}
   1292 }
   1293 static void
   1294 pci_conf_print_caplist(
   1295 #ifdef _KERNEL
   1296     pci_chipset_tag_t pc, pcitag_t tag,
   1297 #endif
   1298     const pcireg_t *regs, int capoff)
   1299 {
   1300 	int off;
   1301 	pcireg_t rval;
   1302 	int pcie_off = -1, pcipm_off = -1, msi_off = -1;
   1303 
   1304 	for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
   1305 	     off != 0;
   1306 	     off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
   1307 		rval = regs[o2i(off)];
   1308 		printf("  Capability register at 0x%02x\n", off);
   1309 
   1310 		printf("    type: 0x%02x (", PCI_CAPLIST_CAP(rval));
   1311 		switch (PCI_CAPLIST_CAP(rval)) {
   1312 		case PCI_CAP_RESERVED0:
   1313 			printf("reserved");
   1314 			break;
   1315 		case PCI_CAP_PWRMGMT:
   1316 			printf("Power Management, rev. %s",
   1317 			    pci_conf_print_pcipm_cap_pmrev((rval >> 0) & 0x07));
   1318 			pcipm_off = off;
   1319 			break;
   1320 		case PCI_CAP_AGP:
   1321 			printf("AGP, rev. %d.%d",
   1322 				PCI_CAP_AGP_MAJOR(rval),
   1323 				PCI_CAP_AGP_MINOR(rval));
   1324 			break;
   1325 		case PCI_CAP_VPD:
   1326 			printf("VPD");
   1327 			break;
   1328 		case PCI_CAP_SLOTID:
   1329 			printf("SlotID");
   1330 			break;
   1331 		case PCI_CAP_MSI:
   1332 			printf("MSI");
   1333 			msi_off = off;
   1334 			break;
   1335 		case PCI_CAP_CPCI_HOTSWAP:
   1336 			printf("CompactPCI Hot-swapping");
   1337 			break;
   1338 		case PCI_CAP_PCIX:
   1339 			printf("PCI-X");
   1340 			break;
   1341 		case PCI_CAP_LDT:
   1342 			printf("LDT");
   1343 			break;
   1344 		case PCI_CAP_VENDSPEC:
   1345 			printf("Vendor-specific");
   1346 			break;
   1347 		case PCI_CAP_DEBUGPORT:
   1348 			printf("Debug Port");
   1349 			break;
   1350 		case PCI_CAP_CPCI_RSRCCTL:
   1351 			printf("CompactPCI Resource Control");
   1352 			break;
   1353 		case PCI_CAP_HOTPLUG:
   1354 			printf("Hot-Plug");
   1355 			break;
   1356 		case PCI_CAP_SUBVENDOR:
   1357 			printf("Sub Vendor ID");
   1358 			break;
   1359 		case PCI_CAP_AGP8:
   1360 			printf("AGP 8x");
   1361 			break;
   1362 		case PCI_CAP_SECURE:
   1363 			printf("Secure Device");
   1364 			break;
   1365 		case PCI_CAP_PCIEXPRESS:
   1366 			printf("PCI Express");
   1367 			pcie_off = off;
   1368 			break;
   1369 		case PCI_CAP_MSIX:
   1370 			printf("MSI-X");
   1371 			break;
   1372 		case PCI_CAP_SATA:
   1373 			printf("SATA");
   1374 			break;
   1375 		case PCI_CAP_PCIAF:
   1376 			printf("Advanced Features");
   1377 			break;
   1378 		default:
   1379 			printf("unknown");
   1380 		}
   1381 		printf(")\n");
   1382 	}
   1383 	if (msi_off != -1)
   1384 		pci_conf_print_msi_cap(regs, msi_off);
   1385 	if (pcipm_off != -1)
   1386 		pci_conf_print_pcipm_cap(regs, pcipm_off);
   1387 	if (pcie_off != -1)
   1388 		pci_conf_print_pcie_cap(regs, pcie_off);
   1389 }
   1390 
   1391 /* Print the Secondary Status Register. */
   1392 static void
   1393 pci_conf_print_ssr(pcireg_t rval)
   1394 {
   1395 	pcireg_t devsel;
   1396 
   1397 	printf("    Secondary status register: 0x%04x\n", rval); /* XXX bits */
   1398 	onoff("66 MHz capable", __BIT(5));
   1399 	onoff("User Definable Features (UDF) support", __BIT(6));
   1400 	onoff("Fast back-to-back capable", __BIT(7));
   1401 	onoff("Data parity error detected", __BIT(8));
   1402 
   1403 	printf("      DEVSEL timing: ");
   1404 	devsel = __SHIFTOUT(rval, __BITS(10, 9));
   1405 	switch (devsel) {
   1406 	case 0:
   1407 		printf("fast");
   1408 		break;
   1409 	case 1:
   1410 		printf("medium");
   1411 		break;
   1412 	case 2:
   1413 		printf("slow");
   1414 		break;
   1415 	default:
   1416 		printf("unknown/reserved");	/* XXX */
   1417 		break;
   1418 	}
   1419 	printf(" (0x%x)\n", devsel);
   1420 
   1421 	onoff("Signalled target abort", __BIT(11));
   1422 	onoff("Received target abort", __BIT(12));
   1423 	onoff("Received master abort", __BIT(13));
   1424 	onoff("Received system error", __BIT(14));
   1425 	onoff("Detected parity error", __BIT(15));
   1426 }
   1427 
   1428 static void
   1429 pci_conf_print_type1(
   1430 #ifdef _KERNEL
   1431     pci_chipset_tag_t pc, pcitag_t tag,
   1432 #endif
   1433     const pcireg_t *regs
   1434 #ifdef _KERNEL
   1435     , int sizebars
   1436 #endif
   1437     )
   1438 {
   1439 	int off, width;
   1440 	pcireg_t rval;
   1441 
   1442 	/*
   1443 	 * XXX these need to be printed in more detail, need to be
   1444 	 * XXX checked against specs/docs, etc.
   1445 	 *
   1446 	 * This layout was cribbed from the TI PCI2030 PCI-to-PCI
   1447 	 * Bridge chip documentation, and may not be correct with
   1448 	 * respect to various standards. (XXX)
   1449 	 */
   1450 
   1451 	for (off = 0x10; off < 0x18; off += width) {
   1452 #ifdef _KERNEL
   1453 		width = pci_conf_print_bar(pc, tag, regs, off, NULL, sizebars);
   1454 #else
   1455 		width = pci_conf_print_bar(regs, off, NULL);
   1456 #endif
   1457 	}
   1458 
   1459 	printf("    Primary bus number: 0x%02x\n",
   1460 	    (regs[o2i(0x18)] >> 0) & 0xff);
   1461 	printf("    Secondary bus number: 0x%02x\n",
   1462 	    (regs[o2i(0x18)] >> 8) & 0xff);
   1463 	printf("    Subordinate bus number: 0x%02x\n",
   1464 	    (regs[o2i(0x18)] >> 16) & 0xff);
   1465 	printf("    Secondary bus latency timer: 0x%02x\n",
   1466 	    (regs[o2i(0x18)] >> 24) & 0xff);
   1467 
   1468 	pci_conf_print_ssr(__SHIFTOUT(regs[o2i(0x1c)], __BITS(31, 16)));
   1469 
   1470 	/* XXX Print more prettily */
   1471 	printf("    I/O region:\n");
   1472 	printf("      base register:  0x%02x\n", (regs[o2i(0x1c)] >> 0) & 0xff);
   1473 	printf("      limit register: 0x%02x\n", (regs[o2i(0x1c)] >> 8) & 0xff);
   1474 	printf("      base upper 16 bits register:  0x%04x\n",
   1475 	    (regs[o2i(0x30)] >> 0) & 0xffff);
   1476 	printf("      limit upper 16 bits register: 0x%04x\n",
   1477 	    (regs[o2i(0x30)] >> 16) & 0xffff);
   1478 
   1479 	/* XXX Print more prettily */
   1480 	printf("    Memory region:\n");
   1481 	printf("      base register:  0x%04x\n",
   1482 	    (regs[o2i(0x20)] >> 0) & 0xffff);
   1483 	printf("      limit register: 0x%04x\n",
   1484 	    (regs[o2i(0x20)] >> 16) & 0xffff);
   1485 
   1486 	/* XXX Print more prettily */
   1487 	printf("    Prefetchable memory region:\n");
   1488 	printf("      base register:  0x%04x\n",
   1489 	    (regs[o2i(0x24)] >> 0) & 0xffff);
   1490 	printf("      limit register: 0x%04x\n",
   1491 	    (regs[o2i(0x24)] >> 16) & 0xffff);
   1492 	printf("      base upper 32 bits register:  0x%08x\n", regs[o2i(0x28)]);
   1493 	printf("      limit upper 32 bits register: 0x%08x\n", regs[o2i(0x2c)]);
   1494 
   1495 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   1496 		printf("    Capability list pointer: 0x%02x\n",
   1497 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
   1498 	else
   1499 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
   1500 
   1501 	/* XXX */
   1502 	printf("    Expansion ROM Base Address: 0x%08x\n", regs[o2i(0x38)]);
   1503 
   1504 	printf("    Interrupt line: 0x%02x\n",
   1505 	    (regs[o2i(0x3c)] >> 0) & 0xff);
   1506 	printf("    Interrupt pin: 0x%02x ",
   1507 	    (regs[o2i(0x3c)] >> 8) & 0xff);
   1508 	switch ((regs[o2i(0x3c)] >> 8) & 0xff) {
   1509 	case PCI_INTERRUPT_PIN_NONE:
   1510 		printf("(none)");
   1511 		break;
   1512 	case PCI_INTERRUPT_PIN_A:
   1513 		printf("(pin A)");
   1514 		break;
   1515 	case PCI_INTERRUPT_PIN_B:
   1516 		printf("(pin B)");
   1517 		break;
   1518 	case PCI_INTERRUPT_PIN_C:
   1519 		printf("(pin C)");
   1520 		break;
   1521 	case PCI_INTERRUPT_PIN_D:
   1522 		printf("(pin D)");
   1523 		break;
   1524 	default:
   1525 		printf("(? ? ?)");
   1526 		break;
   1527 	}
   1528 	printf("\n");
   1529 	rval = (regs[o2i(0x3c)] >> 16) & 0xffff;
   1530 	printf("    Bridge control register: 0x%04x\n", rval); /* XXX bits */
   1531 	onoff("Parity error response", 0x0001);
   1532 	onoff("Secondary SERR forwarding", 0x0002);
   1533 	onoff("ISA enable", 0x0004);
   1534 	onoff("VGA enable", 0x0008);
   1535 	onoff("Master abort reporting", 0x0020);
   1536 	onoff("Secondary bus reset", 0x0040);
   1537 	onoff("Fast back-to-back capable", 0x0080);
   1538 }
   1539 
   1540 static void
   1541 pci_conf_print_type2(
   1542 #ifdef _KERNEL
   1543     pci_chipset_tag_t pc, pcitag_t tag,
   1544 #endif
   1545     const pcireg_t *regs
   1546 #ifdef _KERNEL
   1547     , int sizebars
   1548 #endif
   1549     )
   1550 {
   1551 	pcireg_t rval;
   1552 
   1553 	/*
   1554 	 * XXX these need to be printed in more detail, need to be
   1555 	 * XXX checked against specs/docs, etc.
   1556 	 *
   1557 	 * This layout was cribbed from the TI PCI1420 PCI-to-CardBus
   1558 	 * controller chip documentation, and may not be correct with
   1559 	 * respect to various standards. (XXX)
   1560 	 */
   1561 
   1562 #ifdef _KERNEL
   1563 	pci_conf_print_bar(pc, tag, regs, 0x10,
   1564 	    "CardBus socket/ExCA registers", sizebars);
   1565 #else
   1566 	pci_conf_print_bar(regs, 0x10, "CardBus socket/ExCA registers");
   1567 #endif
   1568 
   1569 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   1570 		printf("    Capability list pointer: 0x%02x\n",
   1571 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CARDBUS_CAPLISTPTR_REG)]));
   1572 	else
   1573 		printf("    Reserved @ 0x14: 0x%04" PRIxMAX "\n",
   1574 		       __SHIFTOUT(regs[o2i(0x14)], __BITS(15, 0)));
   1575 	pci_conf_print_ssr(__SHIFTOUT(regs[o2i(0x14)], __BITS(31, 16)));
   1576 
   1577 	printf("    PCI bus number: 0x%02x\n",
   1578 	    (regs[o2i(0x18)] >> 0) & 0xff);
   1579 	printf("    CardBus bus number: 0x%02x\n",
   1580 	    (regs[o2i(0x18)] >> 8) & 0xff);
   1581 	printf("    Subordinate bus number: 0x%02x\n",
   1582 	    (regs[o2i(0x18)] >> 16) & 0xff);
   1583 	printf("    CardBus latency timer: 0x%02x\n",
   1584 	    (regs[o2i(0x18)] >> 24) & 0xff);
   1585 
   1586 	/* XXX Print more prettily */
   1587 	printf("    CardBus memory region 0:\n");
   1588 	printf("      base register:  0x%08x\n", regs[o2i(0x1c)]);
   1589 	printf("      limit register: 0x%08x\n", regs[o2i(0x20)]);
   1590 	printf("    CardBus memory region 1:\n");
   1591 	printf("      base register:  0x%08x\n", regs[o2i(0x24)]);
   1592 	printf("      limit register: 0x%08x\n", regs[o2i(0x28)]);
   1593 	printf("    CardBus I/O region 0:\n");
   1594 	printf("      base register:  0x%08x\n", regs[o2i(0x2c)]);
   1595 	printf("      limit register: 0x%08x\n", regs[o2i(0x30)]);
   1596 	printf("    CardBus I/O region 1:\n");
   1597 	printf("      base register:  0x%08x\n", regs[o2i(0x34)]);
   1598 	printf("      limit register: 0x%08x\n", regs[o2i(0x38)]);
   1599 
   1600 	printf("    Interrupt line: 0x%02x\n",
   1601 	    (regs[o2i(0x3c)] >> 0) & 0xff);
   1602 	printf("    Interrupt pin: 0x%02x ",
   1603 	    (regs[o2i(0x3c)] >> 8) & 0xff);
   1604 	switch ((regs[o2i(0x3c)] >> 8) & 0xff) {
   1605 	case PCI_INTERRUPT_PIN_NONE:
   1606 		printf("(none)");
   1607 		break;
   1608 	case PCI_INTERRUPT_PIN_A:
   1609 		printf("(pin A)");
   1610 		break;
   1611 	case PCI_INTERRUPT_PIN_B:
   1612 		printf("(pin B)");
   1613 		break;
   1614 	case PCI_INTERRUPT_PIN_C:
   1615 		printf("(pin C)");
   1616 		break;
   1617 	case PCI_INTERRUPT_PIN_D:
   1618 		printf("(pin D)");
   1619 		break;
   1620 	default:
   1621 		printf("(? ? ?)");
   1622 		break;
   1623 	}
   1624 	printf("\n");
   1625 	rval = (regs[o2i(0x3c)] >> 16) & 0xffff;
   1626 	printf("    Bridge control register: 0x%04x\n", rval);
   1627 	onoff("Parity error response", __BIT(0));
   1628 	onoff("SERR# enable", __BIT(1));
   1629 	onoff("ISA enable", __BIT(2));
   1630 	onoff("VGA enable", __BIT(3));
   1631 	onoff("Master abort mode", __BIT(5));
   1632 	onoff("Secondary (CardBus) bus reset", __BIT(6));
   1633 	onoff("Functional interrupts routed by ExCA registers", __BIT(7));
   1634 	onoff("Memory window 0 prefetchable", __BIT(8));
   1635 	onoff("Memory window 1 prefetchable", __BIT(9));
   1636 	onoff("Write posting enable", __BIT(10));
   1637 
   1638 	rval = regs[o2i(0x40)];
   1639 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
   1640 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
   1641 
   1642 #ifdef _KERNEL
   1643 	pci_conf_print_bar(pc, tag, regs, 0x44, "legacy-mode registers",
   1644 	    sizebars);
   1645 #else
   1646 	pci_conf_print_bar(regs, 0x44, "legacy-mode registers");
   1647 #endif
   1648 }
   1649 
   1650 void
   1651 pci_conf_print(
   1652 #ifdef _KERNEL
   1653     pci_chipset_tag_t pc, pcitag_t tag,
   1654     void (*printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *)
   1655 #else
   1656     int pcifd, u_int bus, u_int dev, u_int func
   1657 #endif
   1658     )
   1659 {
   1660 	pcireg_t regs[o2i(256)];
   1661 	int off, capoff, endoff, hdrtype;
   1662 	const char *typename;
   1663 #ifdef _KERNEL
   1664 	void (*typeprintfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *, int);
   1665 	int sizebars;
   1666 #else
   1667 	void (*typeprintfn)(const pcireg_t *);
   1668 #endif
   1669 
   1670 	printf("PCI configuration registers:\n");
   1671 
   1672 	for (off = 0; off < 256; off += 4) {
   1673 #ifdef _KERNEL
   1674 		regs[o2i(off)] = pci_conf_read(pc, tag, off);
   1675 #else
   1676 		if (pcibus_conf_read(pcifd, bus, dev, func, off,
   1677 		    &regs[o2i(off)]) == -1)
   1678 			regs[o2i(off)] = 0;
   1679 #endif
   1680 	}
   1681 
   1682 #ifdef _KERNEL
   1683 	sizebars = 1;
   1684 	if (PCI_CLASS(regs[o2i(PCI_CLASS_REG)]) == PCI_CLASS_BRIDGE &&
   1685 	    PCI_SUBCLASS(regs[o2i(PCI_CLASS_REG)]) == PCI_SUBCLASS_BRIDGE_HOST)
   1686 		sizebars = 0;
   1687 #endif
   1688 
   1689 	/* common header */
   1690 	printf("  Common header:\n");
   1691 	pci_conf_print_regs(regs, 0, 16);
   1692 
   1693 	printf("\n");
   1694 #ifdef _KERNEL
   1695 	pci_conf_print_common(pc, tag, regs);
   1696 #else
   1697 	pci_conf_print_common(regs);
   1698 #endif
   1699 	printf("\n");
   1700 
   1701 	/* type-dependent header */
   1702 	hdrtype = PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]);
   1703 	switch (hdrtype) {		/* XXX make a table, eventually */
   1704 	case 0:
   1705 		/* Standard device header */
   1706 		typename = "\"normal\" device";
   1707 		typeprintfn = &pci_conf_print_type0;
   1708 		capoff = PCI_CAPLISTPTR_REG;
   1709 		endoff = 64;
   1710 		break;
   1711 	case 1:
   1712 		/* PCI-PCI bridge header */
   1713 		typename = "PCI-PCI bridge";
   1714 		typeprintfn = &pci_conf_print_type1;
   1715 		capoff = PCI_CAPLISTPTR_REG;
   1716 		endoff = 64;
   1717 		break;
   1718 	case 2:
   1719 		/* PCI-CardBus bridge header */
   1720 		typename = "PCI-CardBus bridge";
   1721 		typeprintfn = &pci_conf_print_type2;
   1722 		capoff = PCI_CARDBUS_CAPLISTPTR_REG;
   1723 		endoff = 72;
   1724 		break;
   1725 	default:
   1726 		typename = NULL;
   1727 		typeprintfn = 0;
   1728 		capoff = -1;
   1729 		endoff = 64;
   1730 		break;
   1731 	}
   1732 	printf("  Type %d ", hdrtype);
   1733 	if (typename != NULL)
   1734 		printf("(%s) ", typename);
   1735 	printf("header:\n");
   1736 	pci_conf_print_regs(regs, 16, endoff);
   1737 	printf("\n");
   1738 	if (typeprintfn) {
   1739 #ifdef _KERNEL
   1740 		(*typeprintfn)(pc, tag, regs, sizebars);
   1741 #else
   1742 		(*typeprintfn)(regs);
   1743 #endif
   1744 	} else
   1745 		printf("    Don't know how to pretty-print type %d header.\n",
   1746 		    hdrtype);
   1747 	printf("\n");
   1748 
   1749 	/* capability list, if present */
   1750 	if ((regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
   1751 		&& (capoff > 0)) {
   1752 #ifdef _KERNEL
   1753 		pci_conf_print_caplist(pc, tag, regs, capoff);
   1754 #else
   1755 		pci_conf_print_caplist(regs, capoff);
   1756 #endif
   1757 		printf("\n");
   1758 	}
   1759 
   1760 	/* device-dependent header */
   1761 	printf("  Device-dependent header:\n");
   1762 	pci_conf_print_regs(regs, endoff, 256);
   1763 	printf("\n");
   1764 #ifdef _KERNEL
   1765 	if (printfn)
   1766 		(*printfn)(pc, tag, regs);
   1767 	else
   1768 		printf("    Don't know how to pretty-print device-dependent header.\n");
   1769 	printf("\n");
   1770 #endif /* _KERNEL */
   1771 }
   1772