xref: /netbsd-src/sys/dev/pci/pci_subr.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: pci_subr.c,v 1.74 2008/04/10 19:13:37 cegger 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.74 2008/04/10 19:13:37 cegger 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 #else
55 #include <pci.h>
56 #include <stdbool.h>
57 #include <stdio.h>
58 #endif
59 
60 #include <dev/pci/pcireg.h>
61 #ifdef _KERNEL
62 #include <dev/pci/pcivar.h>
63 #endif
64 #ifdef PCIVERBOSE
65 #include <dev/pci/pcidevs.h>
66 #endif
67 
68 /*
69  * Descriptions of known PCI classes and subclasses.
70  *
71  * Subclasses are described in the same way as classes, but have a
72  * NULL subclass pointer.
73  */
74 struct pci_class {
75 	const char	*name;
76 	int		val;		/* as wide as pci_{,sub}class_t */
77 	const struct pci_class *subclasses;
78 };
79 
80 static const struct pci_class pci_subclass_prehistoric[] = {
81 	{ "miscellaneous",	PCI_SUBCLASS_PREHISTORIC_MISC,	NULL,	},
82 	{ "VGA",		PCI_SUBCLASS_PREHISTORIC_VGA,	NULL,	},
83 	{ NULL,			0,				NULL,	},
84 };
85 
86 static const struct pci_class pci_subclass_mass_storage[] = {
87 	{ "SCSI",		PCI_SUBCLASS_MASS_STORAGE_SCSI,	NULL,	},
88 	{ "IDE",		PCI_SUBCLASS_MASS_STORAGE_IDE,	NULL,	},
89 	{ "floppy",		PCI_SUBCLASS_MASS_STORAGE_FLOPPY, NULL, },
90 	{ "IPI",		PCI_SUBCLASS_MASS_STORAGE_IPI,	NULL,	},
91 	{ "RAID",		PCI_SUBCLASS_MASS_STORAGE_RAID,	NULL,	},
92 	{ "ATA",		PCI_SUBCLASS_MASS_STORAGE_ATA,	NULL,	},
93 	{ "SATA",		PCI_SUBCLASS_MASS_STORAGE_SATA,	NULL,	},
94 	{ "SAS",		PCI_SUBCLASS_MASS_STORAGE_SAS,	NULL,	},
95 	{ "miscellaneous",	PCI_SUBCLASS_MASS_STORAGE_MISC,	NULL,	},
96 	{ NULL,			0,				NULL,	},
97 };
98 
99 static const struct pci_class pci_subclass_network[] = {
100 	{ "ethernet",		PCI_SUBCLASS_NETWORK_ETHERNET,	NULL,	},
101 	{ "token ring",		PCI_SUBCLASS_NETWORK_TOKENRING,	NULL,	},
102 	{ "FDDI",		PCI_SUBCLASS_NETWORK_FDDI,	NULL,	},
103 	{ "ATM",		PCI_SUBCLASS_NETWORK_ATM,	NULL,	},
104 	{ "ISDN",		PCI_SUBCLASS_NETWORK_ISDN,	NULL,	},
105 	{ "WorldFip",		PCI_SUBCLASS_NETWORK_WORLDFIP,	NULL,	},
106 	{ "PCMIG Multi Computing", PCI_SUBCLASS_NETWORK_PCIMGMULTICOMP, NULL, },
107 	{ "miscellaneous",	PCI_SUBCLASS_NETWORK_MISC,	NULL,	},
108 	{ NULL,			0,				NULL,	},
109 };
110 
111 static const struct pci_class pci_subclass_display[] = {
112 	{ "VGA",		PCI_SUBCLASS_DISPLAY_VGA,	NULL,	},
113 	{ "XGA",		PCI_SUBCLASS_DISPLAY_XGA,	NULL,	},
114 	{ "3D",			PCI_SUBCLASS_DISPLAY_3D,	NULL,	},
115 	{ "miscellaneous",	PCI_SUBCLASS_DISPLAY_MISC,	NULL,	},
116 	{ NULL,			0,				NULL,	},
117 };
118 
119 static const struct pci_class pci_subclass_multimedia[] = {
120 	{ "video",		PCI_SUBCLASS_MULTIMEDIA_VIDEO,	NULL,	},
121 	{ "audio",		PCI_SUBCLASS_MULTIMEDIA_AUDIO,	NULL,	},
122 	{ "telephony",		PCI_SUBCLASS_MULTIMEDIA_TELEPHONY, 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 #ifdef PCIVERBOSE
299 /*
300  * Descriptions of of known vendors and devices ("products").
301  */
302 struct pci_vendor {
303 	pci_vendor_id_t		vendor;
304 	const char		*vendorname;
305 };
306 struct pci_product {
307 	pci_vendor_id_t		vendor;
308 	pci_product_id_t	product;
309 	const char		*productname;
310 };
311 
312 #include <dev/pci/pcidevs_data.h>
313 #endif /* PCIVERBOSE */
314 
315 const char *
316 pci_findvendor(pcireg_t id_reg)
317 {
318 #ifdef PCIVERBOSE
319 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
320 	int n;
321 
322 	for (n = 0; n < pci_nvendors; n++)
323 		if (pci_vendors[n].vendor == vendor)
324 			return (pci_vendors[n].vendorname);
325 #endif
326 	return (NULL);
327 }
328 
329 const char *
330 pci_findproduct(pcireg_t id_reg)
331 {
332 #ifdef PCIVERBOSE
333 	pci_vendor_id_t vendor = PCI_VENDOR(id_reg);
334 	pci_product_id_t product = PCI_PRODUCT(id_reg);
335 	int n;
336 
337 	for (n = 0; n < pci_nproducts; n++)
338 		if (pci_products[n].vendor == vendor &&
339 		    pci_products[n].product == product)
340 			return (pci_products[n].productname);
341 #endif
342 	return (NULL);
343 }
344 
345 void
346 pci_devinfo(pcireg_t id_reg, pcireg_t class_reg, int showclass, char *cp,
347     size_t l)
348 {
349 	pci_vendor_id_t vendor;
350 	pci_product_id_t product;
351 	pci_class_t class;
352 	pci_subclass_t subclass;
353 	pci_interface_t interface;
354 	pci_revision_t revision;
355 	const char *vendor_namep, *product_namep;
356 	const struct pci_class *classp, *subclassp;
357 #ifdef PCIVERBOSE
358 	const char *unmatched = "unknown ";
359 #else
360 	const char *unmatched = "";
361 #endif
362 	char *ep;
363 
364 	ep = cp + l;
365 
366 	vendor = PCI_VENDOR(id_reg);
367 	product = PCI_PRODUCT(id_reg);
368 
369 	class = PCI_CLASS(class_reg);
370 	subclass = PCI_SUBCLASS(class_reg);
371 	interface = PCI_INTERFACE(class_reg);
372 	revision = PCI_REVISION(class_reg);
373 
374 	vendor_namep = pci_findvendor(id_reg);
375 	product_namep = pci_findproduct(id_reg);
376 
377 	classp = pci_class;
378 	while (classp->name != NULL) {
379 		if (class == classp->val)
380 			break;
381 		classp++;
382 	}
383 
384 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
385 	while (subclassp && subclassp->name != NULL) {
386 		if (subclass == subclassp->val)
387 			break;
388 		subclassp++;
389 	}
390 
391 	if (vendor_namep == NULL)
392 		cp += snprintf(cp, ep - cp, "%svendor 0x%04x product 0x%04x",
393 		    unmatched, vendor, product);
394 	else if (product_namep != NULL)
395 		cp += snprintf(cp, ep - cp, "%s %s", vendor_namep,
396 		    product_namep);
397 	else
398 		cp += snprintf(cp, ep - cp, "%s product 0x%04x",
399 		    vendor_namep, product);
400 	if (showclass) {
401 		cp += snprintf(cp, ep - cp, " (");
402 		if (classp->name == NULL)
403 			cp += snprintf(cp, ep - cp,
404 			    "class 0x%02x, subclass 0x%02x", class, subclass);
405 		else {
406 			if (subclassp == NULL || subclassp->name == NULL)
407 				cp += snprintf(cp, ep - cp,
408 				    "%s subclass 0x%02x",
409 				    classp->name, subclass);
410 			else
411 				cp += snprintf(cp, ep - cp, "%s %s",
412 				    subclassp->name, classp->name);
413 		}
414 		if (interface != 0)
415 			cp += snprintf(cp, ep - cp, ", interface 0x%02x",
416 			    interface);
417 		if (revision != 0)
418 			cp += snprintf(cp, ep - cp, ", revision 0x%02x",
419 			    revision);
420 		cp += snprintf(cp, ep - cp, ")");
421 	}
422 }
423 
424 /*
425  * Print out most of the PCI configuration registers.  Typically used
426  * in a device attach routine like this:
427  *
428  *	#ifdef MYDEV_DEBUG
429  *		printf("%s: ", device_xname(&sc->sc_dev));
430  *		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
431  *	#endif
432  */
433 
434 #define	i2o(i)	((i) * 4)
435 #define	o2i(o)	((o) / 4)
436 #define	onoff(str, bit)							\
437 	printf("      %s: %s\n", (str), (rval & (bit)) ? "on" : "off");
438 
439 static void
440 pci_conf_print_common(
441 #ifdef _KERNEL
442     pci_chipset_tag_t pc, pcitag_t tag,
443 #endif
444     const pcireg_t *regs)
445 {
446 	const char *name;
447 	const struct pci_class *classp, *subclassp;
448 	pcireg_t rval;
449 
450 	rval = regs[o2i(PCI_ID_REG)];
451 	name = pci_findvendor(rval);
452 	if (name)
453 		printf("    Vendor Name: %s (0x%04x)\n", name,
454 		    PCI_VENDOR(rval));
455 	else
456 		printf("    Vendor ID: 0x%04x\n", PCI_VENDOR(rval));
457 	name = pci_findproduct(rval);
458 	if (name)
459 		printf("    Device Name: %s (0x%04x)\n", name,
460 		    PCI_PRODUCT(rval));
461 	else
462 		printf("    Device ID: 0x%04x\n", PCI_PRODUCT(rval));
463 
464 	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];
465 
466 	printf("    Command register: 0x%04x\n", rval & 0xffff);
467 	onoff("I/O space accesses", PCI_COMMAND_IO_ENABLE);
468 	onoff("Memory space accesses", PCI_COMMAND_MEM_ENABLE);
469 	onoff("Bus mastering", PCI_COMMAND_MASTER_ENABLE);
470 	onoff("Special cycles", PCI_COMMAND_SPECIAL_ENABLE);
471 	onoff("MWI transactions", PCI_COMMAND_INVALIDATE_ENABLE);
472 	onoff("Palette snooping", PCI_COMMAND_PALETTE_ENABLE);
473 	onoff("Parity error checking", PCI_COMMAND_PARITY_ENABLE);
474 	onoff("Address/data stepping", PCI_COMMAND_STEPPING_ENABLE);
475 	onoff("System error (SERR)", PCI_COMMAND_SERR_ENABLE);
476 	onoff("Fast back-to-back transactions", PCI_COMMAND_BACKTOBACK_ENABLE);
477 	onoff("Interrupt disable", PCI_COMMAND_INTERRUPT_DISABLE);
478 
479 	printf("    Status register: 0x%04x\n", (rval >> 16) & 0xffff);
480 	onoff("Capability List support", PCI_STATUS_CAPLIST_SUPPORT);
481 	onoff("66 MHz capable", PCI_STATUS_66MHZ_SUPPORT);
482 	onoff("User Definable Features (UDF) support", PCI_STATUS_UDF_SUPPORT);
483 	onoff("Fast back-to-back capable", PCI_STATUS_BACKTOBACK_SUPPORT);
484 	onoff("Data parity error detected", PCI_STATUS_PARITY_ERROR);
485 
486 	printf("      DEVSEL timing: ");
487 	switch (rval & PCI_STATUS_DEVSEL_MASK) {
488 	case PCI_STATUS_DEVSEL_FAST:
489 		printf("fast");
490 		break;
491 	case PCI_STATUS_DEVSEL_MEDIUM:
492 		printf("medium");
493 		break;
494 	case PCI_STATUS_DEVSEL_SLOW:
495 		printf("slow");
496 		break;
497 	default:
498 		printf("unknown/reserved");	/* XXX */
499 		break;
500 	}
501 	printf(" (0x%x)\n", (rval & PCI_STATUS_DEVSEL_MASK) >> 25);
502 
503 	onoff("Slave signaled Target Abort", PCI_STATUS_TARGET_TARGET_ABORT);
504 	onoff("Master received Target Abort", PCI_STATUS_MASTER_TARGET_ABORT);
505 	onoff("Master received Master Abort", PCI_STATUS_MASTER_ABORT);
506 	onoff("Asserted System Error (SERR)", PCI_STATUS_SPECIAL_ERROR);
507 	onoff("Parity error detected", PCI_STATUS_PARITY_DETECT);
508 
509 	rval = regs[o2i(PCI_CLASS_REG)];
510 	for (classp = pci_class; classp->name != NULL; classp++) {
511 		if (PCI_CLASS(rval) == classp->val)
512 			break;
513 	}
514 	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
515 	while (subclassp && subclassp->name != NULL) {
516 		if (PCI_SUBCLASS(rval) == subclassp->val)
517 			break;
518 		subclassp++;
519 	}
520 	if (classp->name != NULL) {
521 		printf("    Class Name: %s (0x%02x)\n", classp->name,
522 		    PCI_CLASS(rval));
523 		if (subclassp != NULL && subclassp->name != NULL)
524 			printf("    Subclass Name: %s (0x%02x)\n",
525 			    subclassp->name, PCI_SUBCLASS(rval));
526 		else
527 			printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
528 	} else {
529 		printf("    Class ID: 0x%02x\n", PCI_CLASS(rval));
530 		printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
531 	}
532 	printf("    Interface: 0x%02x\n", PCI_INTERFACE(rval));
533 	printf("    Revision ID: 0x%02x\n", PCI_REVISION(rval));
534 
535 	rval = regs[o2i(PCI_BHLC_REG)];
536 	printf("    BIST: 0x%02x\n", PCI_BIST(rval));
537 	printf("    Header Type: 0x%02x%s (0x%02x)\n", PCI_HDRTYPE_TYPE(rval),
538 	    PCI_HDRTYPE_MULTIFN(rval) ? "+multifunction" : "",
539 	    PCI_HDRTYPE(rval));
540 	printf("    Latency Timer: 0x%02x\n", PCI_LATTIMER(rval));
541 	printf("    Cache Line Size: 0x%02x\n", PCI_CACHELINE(rval));
542 }
543 
544 static int
545 pci_conf_print_bar(
546 #ifdef _KERNEL
547     pci_chipset_tag_t pc, pcitag_t tag,
548 #endif
549     const pcireg_t *regs, int reg, const char *name
550 #ifdef _KERNEL
551     , int sizebar
552 #endif
553     )
554 {
555 	int width;
556 	pcireg_t rval, rval64h;
557 #ifdef _KERNEL
558 	int s;
559 	pcireg_t mask, mask64h;
560 #endif
561 
562 	width = 4;
563 
564 	/*
565 	 * Section 6.2.5.1, `Address Maps', tells us that:
566 	 *
567 	 * 1) The builtin software should have already mapped the
568 	 * device in a reasonable way.
569 	 *
570 	 * 2) A device which wants 2^n bytes of memory will hardwire
571 	 * the bottom n bits of the address to 0.  As recommended,
572 	 * we write all 1s and see what we get back.
573 	 */
574 
575 	rval = regs[o2i(reg)];
576 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
577 	    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
578 		rval64h = regs[o2i(reg + 4)];
579 		width = 8;
580 	} else
581 		rval64h = 0;
582 
583 #ifdef _KERNEL
584 	/* XXX don't size unknown memory type? */
585 	if (rval != 0 && sizebar) {
586 		/*
587 		 * The following sequence seems to make some devices
588 		 * (e.g. host bus bridges, which don't normally
589 		 * have their space mapped) very unhappy, to
590 		 * the point of crashing the system.
591 		 *
592 		 * Therefore, if the mapping register is zero to
593 		 * start out with, don't bother trying.
594 		 */
595 		s = splhigh();
596 		pci_conf_write(pc, tag, reg, 0xffffffff);
597 		mask = pci_conf_read(pc, tag, reg);
598 		pci_conf_write(pc, tag, reg, rval);
599 		if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
600 		    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
601 			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
602 			mask64h = pci_conf_read(pc, tag, reg + 4);
603 			pci_conf_write(pc, tag, reg + 4, rval64h);
604 		} else
605 			mask64h = 0;
606 		splx(s);
607 	} else
608 		mask = mask64h = 0;
609 #endif /* _KERNEL */
610 
611 	printf("    Base address register at 0x%02x", reg);
612 	if (name)
613 		printf(" (%s)", name);
614 	printf("\n      ");
615 	if (rval == 0) {
616 		printf("not implemented(?)\n");
617 		return width;
618 	}
619 	printf("type: ");
620 	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM) {
621 		const char *type, *prefetch;
622 
623 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
624 		case PCI_MAPREG_MEM_TYPE_32BIT:
625 			type = "32-bit";
626 			break;
627 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
628 			type = "32-bit-1M";
629 			break;
630 		case PCI_MAPREG_MEM_TYPE_64BIT:
631 			type = "64-bit";
632 			break;
633 		default:
634 			type = "unknown (XXX)";
635 			break;
636 		}
637 		if (PCI_MAPREG_MEM_PREFETCHABLE(rval))
638 			prefetch = "";
639 		else
640 			prefetch = "non";
641 		printf("%s %sprefetchable memory\n", type, prefetch);
642 		switch (PCI_MAPREG_MEM_TYPE(rval)) {
643 		case PCI_MAPREG_MEM_TYPE_64BIT:
644 			printf("      base: 0x%016llx, ",
645 			    PCI_MAPREG_MEM64_ADDR(
646 				((((long long) rval64h) << 32) | rval)));
647 #ifdef _KERNEL
648 			if (sizebar)
649 				printf("size: 0x%016llx",
650 				    PCI_MAPREG_MEM64_SIZE(
651 				      ((((long long) mask64h) << 32) | mask)));
652 			else
653 #endif /* _KERNEL */
654 				printf("not sized");
655 			printf("\n");
656 			break;
657 		case PCI_MAPREG_MEM_TYPE_32BIT:
658 		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
659 		default:
660 			printf("      base: 0x%08x, ",
661 			    PCI_MAPREG_MEM_ADDR(rval));
662 #ifdef _KERNEL
663 			if (sizebar)
664 				printf("size: 0x%08x",
665 				    PCI_MAPREG_MEM_SIZE(mask));
666 			else
667 #endif /* _KERNEL */
668 				printf("not sized");
669 			printf("\n");
670 			break;
671 		}
672 	} else {
673 #ifdef _KERNEL
674 		if (sizebar)
675 			printf("%d-bit ", mask & ~0x0000ffff ? 32 : 16);
676 #endif /* _KERNEL */
677 		printf("i/o\n");
678 		printf("      base: 0x%08x, ", PCI_MAPREG_IO_ADDR(rval));
679 #ifdef _KERNEL
680 		if (sizebar)
681 			printf("size: 0x%08x", PCI_MAPREG_IO_SIZE(mask));
682 		else
683 #endif /* _KERNEL */
684 			printf("not sized");
685 		printf("\n");
686 	}
687 
688 	return width;
689 }
690 
691 static void
692 pci_conf_print_regs(const pcireg_t *regs, int first, int pastlast)
693 {
694 	int off, needaddr, neednl;
695 
696 	needaddr = 1;
697 	neednl = 0;
698 	for (off = first; off < pastlast; off += 4) {
699 		if ((off % 16) == 0 || needaddr) {
700 			printf("    0x%02x:", off);
701 			needaddr = 0;
702 		}
703 		printf(" 0x%08x", regs[o2i(off)]);
704 		neednl = 1;
705 		if ((off % 16) == 12) {
706 			printf("\n");
707 			neednl = 0;
708 		}
709 	}
710 	if (neednl)
711 		printf("\n");
712 }
713 
714 static void
715 pci_conf_print_type0(
716 #ifdef _KERNEL
717     pci_chipset_tag_t pc, pcitag_t tag,
718 #endif
719     const pcireg_t *regs
720 #ifdef _KERNEL
721     , int sizebars
722 #endif
723     )
724 {
725 	int off, width;
726 	pcireg_t rval;
727 
728 	for (off = PCI_MAPREG_START; off < PCI_MAPREG_END; off += width) {
729 #ifdef _KERNEL
730 		width = pci_conf_print_bar(pc, tag, regs, off, NULL, sizebars);
731 #else
732 		width = pci_conf_print_bar(regs, off, NULL);
733 #endif
734 	}
735 
736 	printf("    Cardbus CIS Pointer: 0x%08x\n", regs[o2i(0x28)]);
737 
738 	rval = regs[o2i(PCI_SUBSYS_ID_REG)];
739 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
740 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
741 
742 	/* XXX */
743 	printf("    Expansion ROM Base Address: 0x%08x\n", regs[o2i(0x30)]);
744 
745 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
746 		printf("    Capability list pointer: 0x%02x\n",
747 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
748 	else
749 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
750 
751 	printf("    Reserved @ 0x38: 0x%08x\n", regs[o2i(0x38)]);
752 
753 	rval = regs[o2i(PCI_INTERRUPT_REG)];
754 	printf("    Maximum Latency: 0x%02x\n", (rval >> 24) & 0xff);
755 	printf("    Minimum Grant: 0x%02x\n", (rval >> 16) & 0xff);
756 	printf("    Interrupt pin: 0x%02x ", PCI_INTERRUPT_PIN(rval));
757 	switch (PCI_INTERRUPT_PIN(rval)) {
758 	case PCI_INTERRUPT_PIN_NONE:
759 		printf("(none)");
760 		break;
761 	case PCI_INTERRUPT_PIN_A:
762 		printf("(pin A)");
763 		break;
764 	case PCI_INTERRUPT_PIN_B:
765 		printf("(pin B)");
766 		break;
767 	case PCI_INTERRUPT_PIN_C:
768 		printf("(pin C)");
769 		break;
770 	case PCI_INTERRUPT_PIN_D:
771 		printf("(pin D)");
772 		break;
773 	default:
774 		printf("(? ? ?)");
775 		break;
776 	}
777 	printf("\n");
778 	printf("    Interrupt line: 0x%02x\n", PCI_INTERRUPT_LINE(rval));
779 }
780 
781 static void
782 pci_conf_print_pcie_cap(const pcireg_t *regs, int capoff)
783 {
784 	bool check_slot = false;
785 
786 	printf("\n  PCI Express Capabilities Register\n");
787 	printf("    Capability version: %x\n",
788 	    (unsigned int)((regs[o2i(capoff)] & 0x000f0000) >> 16));
789 	printf("    Device type: ");
790 	switch ((regs[o2i(capoff)] & 0x00f00000) >> 20) {
791 	case 0x0:
792 		printf("PCI Express Endpoint device\n");
793 		break;
794 	case 0x1:
795 		printf("Legcay PCI Express Endpoint device\n");
796 		break;
797 	case 0x4:
798 		printf("Root Port of PCI Express Root Complex\n");
799 		check_slot = true;
800 		break;
801 	case 0x5:
802 		printf("Upstream Port of PCI Express Switch\n");
803 		break;
804 	case 0x6:
805 		printf("Downstream Port of PCI Express Switch\n");
806 		check_slot = true;
807 		break;
808 	case 0x7:
809 		printf("PCI Express to PCI/PCI-X Bridge\n");
810 		break;
811 	case 0x8:
812 		printf("PCI/PCI-X to PCI Express Bridge\n");
813 		break;
814 	default:
815 		printf("unknown\n");
816 		break;
817 	}
818 	if (check_slot && (regs[o2i(capoff)] & 0x01000000) != 0)
819 		printf("    Slot implemented\n");
820 	printf("    Interrupt Message Number: %x\n",
821 	    (unsigned int)((regs[o2i(capoff)] & 0x4e000000) >> 27));
822 	if ((regs[o2i(capoff + 0x18)] & 0x07ff) != 0) {
823 		printf("    Slot Control Register:\n");
824 		if ((regs[o2i(capoff + 0x18)] & 0x0001) != 0)
825 			printf("      Attention Button Pressed Enabled\n");
826 		if ((regs[o2i(capoff + 0x18)] & 0x0002) != 0)
827 			printf("      Power Fault Detected Enabled\n");
828 		if ((regs[o2i(capoff + 0x18)] & 0x0004) != 0)
829 			printf("      MRL Sensor Changed Enabled\n");
830 		if ((regs[o2i(capoff + 0x18)] & 0x0008) != 0)
831 			printf("      Presense Detected Changed Enabled\n");
832 		if ((regs[o2i(capoff + 0x18)] & 0x0010) != 0)
833 			printf("      Command Completed Interrupt Enabled\n");
834 		if ((regs[o2i(capoff + 0x18)] & 0x0020) != 0)
835 			printf("      Hot-Plug Interrupt Enabled\n");
836 		printf("      Attention Indictor Control: ");
837 		switch ((regs[o2i(capoff + 0x18)] & 0x00a0) >> 6) {
838 		case 0x0:
839 			printf("reserved\n");
840 			break;
841 		case 0x1:
842 			printf("on\n");
843 			break;
844 		case 0x2:
845 			printf("blink\n");
846 			break;
847 		case 0x3:
848 			printf("off\n");
849 			break;
850 		}
851 		printf("      Power Indictor Control: ");
852 		switch ((regs[o2i(capoff + 0x18)] & 0x0300) >> 8) {
853 		case 0x0:
854 			printf("reserved\n");
855 			break;
856 		case 0x1:
857 			printf("on\n");
858 			break;
859 		case 0x2:
860 			printf("blink\n");
861 			break;
862 		case 0x3:
863 			printf("off\n");
864 			break;
865 		}
866 		printf("      Power Controller Control: ");
867 		if ((regs[o2i(capoff + 0x18)] & 0x0400) != 0)
868 			printf("off\n");
869 		else
870 			printf("on\n");
871 	}
872 }
873 
874 static void
875 pci_conf_print_caplist(
876 #ifdef _KERNEL
877     pci_chipset_tag_t pc, pcitag_t tag,
878 #endif
879     const pcireg_t *regs, int capoff)
880 {
881 	static const char unk[] = "unknown";
882 	static const char *pmrev[8] = {
883 		unk, "1.0", "1.1", "1.2", unk, unk, unk, unk
884 	};
885 	int off;
886 	pcireg_t rval;
887 	int pcie_off = -1;
888 
889 	for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
890 	     off != 0;
891 	     off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
892 		rval = regs[o2i(off)];
893 		printf("  Capability register at 0x%02x\n", off);
894 
895 		printf("    type: 0x%02x (", PCI_CAPLIST_CAP(rval));
896 		switch (PCI_CAPLIST_CAP(rval)) {
897 		case PCI_CAP_RESERVED0:
898 			printf("reserved");
899 			break;
900 		case PCI_CAP_PWRMGMT:
901 			printf("Power Management, rev. %s",
902 			       pmrev[(rval >> 0) & 0x07]);
903 			break;
904 		case PCI_CAP_AGP:
905 			printf("AGP, rev. %d.%d",
906 				PCI_CAP_AGP_MAJOR(rval),
907 				PCI_CAP_AGP_MINOR(rval));
908 			break;
909 		case PCI_CAP_VPD:
910 			printf("VPD");
911 			break;
912 		case PCI_CAP_SLOTID:
913 			printf("SlotID");
914 			break;
915 		case PCI_CAP_MSI:
916 			printf("MSI");
917 			break;
918 		case PCI_CAP_CPCI_HOTSWAP:
919 			printf("CompactPCI Hot-swapping");
920 			break;
921 		case PCI_CAP_PCIX:
922 			printf("PCI-X");
923 			break;
924 		case PCI_CAP_LDT:
925 			printf("LDT");
926 			break;
927 		case PCI_CAP_VENDSPEC:
928 			printf("Vendor-specific");
929 			break;
930 		case PCI_CAP_DEBUGPORT:
931 			printf("Debug Port");
932 			break;
933 		case PCI_CAP_CPCI_RSRCCTL:
934 			printf("CompactPCI Resource Control");
935 			break;
936 		case PCI_CAP_HOTPLUG:
937 			printf("Hot-Plug");
938 			break;
939 		case PCI_CAP_AGP8:
940 			printf("AGP 8x");
941 			break;
942 		case PCI_CAP_SECURE:
943 			printf("Secure Device");
944 			break;
945 		case PCI_CAP_PCIEXPRESS:
946 			printf("PCI Express");
947 			pcie_off = off;
948 			break;
949 		case PCI_CAP_MSIX:
950 			printf("MSI-X");
951 			break;
952 		default:
953 			printf("unknown");
954 		}
955 		printf(")\n");
956 	}
957 	if (pcie_off != -1)
958 		pci_conf_print_pcie_cap(regs, pcie_off);
959 }
960 
961 static void
962 pci_conf_print_type1(
963 #ifdef _KERNEL
964     pci_chipset_tag_t pc, pcitag_t tag,
965 #endif
966     const pcireg_t *regs
967 #ifdef _KERNEL
968     , int sizebars
969 #endif
970     )
971 {
972 	int off, width;
973 	pcireg_t rval;
974 
975 	/*
976 	 * XXX these need to be printed in more detail, need to be
977 	 * XXX checked against specs/docs, etc.
978 	 *
979 	 * This layout was cribbed from the TI PCI2030 PCI-to-PCI
980 	 * Bridge chip documentation, and may not be correct with
981 	 * respect to various standards. (XXX)
982 	 */
983 
984 	for (off = 0x10; off < 0x18; off += width) {
985 #ifdef _KERNEL
986 		width = pci_conf_print_bar(pc, tag, regs, off, NULL, sizebars);
987 #else
988 		width = pci_conf_print_bar(regs, off, NULL);
989 #endif
990 	}
991 
992 	printf("    Primary bus number: 0x%02x\n",
993 	    (regs[o2i(0x18)] >> 0) & 0xff);
994 	printf("    Secondary bus number: 0x%02x\n",
995 	    (regs[o2i(0x18)] >> 8) & 0xff);
996 	printf("    Subordinate bus number: 0x%02x\n",
997 	    (regs[o2i(0x18)] >> 16) & 0xff);
998 	printf("    Secondary bus latency timer: 0x%02x\n",
999 	    (regs[o2i(0x18)] >> 24) & 0xff);
1000 
1001 	rval = (regs[o2i(0x1c)] >> 16) & 0xffff;
1002 	printf("    Secondary status register: 0x%04x\n", rval); /* XXX bits */
1003 	onoff("66 MHz capable", 0x0020);
1004 	onoff("User Definable Features (UDF) support", 0x0040);
1005 	onoff("Fast back-to-back capable", 0x0080);
1006 	onoff("Data parity error detected", 0x0100);
1007 
1008 	printf("      DEVSEL timing: ");
1009 	switch (rval & 0x0600) {
1010 	case 0x0000:
1011 		printf("fast");
1012 		break;
1013 	case 0x0200:
1014 		printf("medium");
1015 		break;
1016 	case 0x0400:
1017 		printf("slow");
1018 		break;
1019 	default:
1020 		printf("unknown/reserved");	/* XXX */
1021 		break;
1022 	}
1023 	printf(" (0x%x)\n", (rval & 0x0600) >> 9);
1024 
1025 	onoff("Signaled Target Abort", 0x0800);
1026 	onoff("Received Target Abort", 0x1000);
1027 	onoff("Received Master Abort", 0x2000);
1028 	onoff("System Error", 0x4000);
1029 	onoff("Parity Error", 0x8000);
1030 
1031 	/* XXX Print more prettily */
1032 	printf("    I/O region:\n");
1033 	printf("      base register:  0x%02x\n", (regs[o2i(0x1c)] >> 0) & 0xff);
1034 	printf("      limit register: 0x%02x\n", (regs[o2i(0x1c)] >> 8) & 0xff);
1035 	printf("      base upper 16 bits register:  0x%04x\n",
1036 	    (regs[o2i(0x30)] >> 0) & 0xffff);
1037 	printf("      limit upper 16 bits register: 0x%04x\n",
1038 	    (regs[o2i(0x30)] >> 16) & 0xffff);
1039 
1040 	/* XXX Print more prettily */
1041 	printf("    Memory region:\n");
1042 	printf("      base register:  0x%04x\n",
1043 	    (regs[o2i(0x20)] >> 0) & 0xffff);
1044 	printf("      limit register: 0x%04x\n",
1045 	    (regs[o2i(0x20)] >> 16) & 0xffff);
1046 
1047 	/* XXX Print more prettily */
1048 	printf("    Prefetchable memory region:\n");
1049 	printf("      base register:  0x%04x\n",
1050 	    (regs[o2i(0x24)] >> 0) & 0xffff);
1051 	printf("      limit register: 0x%04x\n",
1052 	    (regs[o2i(0x24)] >> 16) & 0xffff);
1053 	printf("      base upper 32 bits register:  0x%08x\n", regs[o2i(0x28)]);
1054 	printf("      limit upper 32 bits register: 0x%08x\n", regs[o2i(0x2c)]);
1055 
1056 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
1057 		printf("    Capability list pointer: 0x%02x\n",
1058 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
1059 	else
1060 		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);
1061 
1062 	/* XXX */
1063 	printf("    Expansion ROM Base Address: 0x%08x\n", regs[o2i(0x38)]);
1064 
1065 	printf("    Interrupt line: 0x%02x\n",
1066 	    (regs[o2i(0x3c)] >> 0) & 0xff);
1067 	printf("    Interrupt pin: 0x%02x ",
1068 	    (regs[o2i(0x3c)] >> 8) & 0xff);
1069 	switch ((regs[o2i(0x3c)] >> 8) & 0xff) {
1070 	case PCI_INTERRUPT_PIN_NONE:
1071 		printf("(none)");
1072 		break;
1073 	case PCI_INTERRUPT_PIN_A:
1074 		printf("(pin A)");
1075 		break;
1076 	case PCI_INTERRUPT_PIN_B:
1077 		printf("(pin B)");
1078 		break;
1079 	case PCI_INTERRUPT_PIN_C:
1080 		printf("(pin C)");
1081 		break;
1082 	case PCI_INTERRUPT_PIN_D:
1083 		printf("(pin D)");
1084 		break;
1085 	default:
1086 		printf("(? ? ?)");
1087 		break;
1088 	}
1089 	printf("\n");
1090 	rval = (regs[o2i(0x3c)] >> 16) & 0xffff;
1091 	printf("    Bridge control register: 0x%04x\n", rval); /* XXX bits */
1092 	onoff("Parity error response", 0x0001);
1093 	onoff("Secondary SERR forwarding", 0x0002);
1094 	onoff("ISA enable", 0x0004);
1095 	onoff("VGA enable", 0x0008);
1096 	onoff("Master abort reporting", 0x0020);
1097 	onoff("Secondary bus reset", 0x0040);
1098 	onoff("Fast back-to-back capable", 0x0080);
1099 }
1100 
1101 static void
1102 pci_conf_print_type2(
1103 #ifdef _KERNEL
1104     pci_chipset_tag_t pc, pcitag_t tag,
1105 #endif
1106     const pcireg_t *regs
1107 #ifdef _KERNEL
1108     , int sizebars
1109 #endif
1110     )
1111 {
1112 	pcireg_t rval;
1113 
1114 	/*
1115 	 * XXX these need to be printed in more detail, need to be
1116 	 * XXX checked against specs/docs, etc.
1117 	 *
1118 	 * This layout was cribbed from the TI PCI1130 PCI-to-CardBus
1119 	 * controller chip documentation, and may not be correct with
1120 	 * respect to various standards. (XXX)
1121 	 */
1122 
1123 #ifdef _KERNEL
1124 	pci_conf_print_bar(pc, tag, regs, 0x10,
1125 	    "CardBus socket/ExCA registers", sizebars);
1126 #else
1127 	pci_conf_print_bar(regs, 0x10, "CardBus socket/ExCA registers");
1128 #endif
1129 
1130 	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
1131 		printf("    Capability list pointer: 0x%02x\n",
1132 		    PCI_CAPLIST_PTR(regs[o2i(PCI_CARDBUS_CAPLISTPTR_REG)]));
1133 	else
1134 		printf("    Reserved @ 0x14: 0x%04x\n",
1135 		       (regs[o2i(0x14)] >> 0) & 0xffff);
1136 	rval = (regs[o2i(0x14)] >> 16) & 0xffff;
1137 	printf("    Secondary status register: 0x%04x\n", rval);
1138 	onoff("66 MHz capable", 0x0020);
1139 	onoff("User Definable Features (UDF) support", 0x0040);
1140 	onoff("Fast back-to-back capable", 0x0080);
1141 	onoff("Data parity error detection", 0x0100);
1142 
1143 	printf("      DEVSEL timing: ");
1144 	switch (rval & 0x0600) {
1145 	case 0x0000:
1146 		printf("fast");
1147 		break;
1148 	case 0x0200:
1149 		printf("medium");
1150 		break;
1151 	case 0x0400:
1152 		printf("slow");
1153 		break;
1154 	default:
1155 		printf("unknown/reserved");	/* XXX */
1156 		break;
1157 	}
1158 	printf(" (0x%x)\n", (rval & 0x0600) >> 9);
1159 	onoff("PCI target aborts terminate CardBus bus master transactions",
1160 	    0x0800);
1161 	onoff("CardBus target aborts terminate PCI bus master transactions",
1162 	    0x1000);
1163 	onoff("Bus initiator aborts terminate initiator transactions",
1164 	    0x2000);
1165 	onoff("System error", 0x4000);
1166 	onoff("Parity error", 0x8000);
1167 
1168 	printf("    PCI bus number: 0x%02x\n",
1169 	    (regs[o2i(0x18)] >> 0) & 0xff);
1170 	printf("    CardBus bus number: 0x%02x\n",
1171 	    (regs[o2i(0x18)] >> 8) & 0xff);
1172 	printf("    Subordinate bus number: 0x%02x\n",
1173 	    (regs[o2i(0x18)] >> 16) & 0xff);
1174 	printf("    CardBus latency timer: 0x%02x\n",
1175 	    (regs[o2i(0x18)] >> 24) & 0xff);
1176 
1177 	/* XXX Print more prettily */
1178 	printf("    CardBus memory region 0:\n");
1179 	printf("      base register:  0x%08x\n", regs[o2i(0x1c)]);
1180 	printf("      limit register: 0x%08x\n", regs[o2i(0x20)]);
1181 	printf("    CardBus memory region 1:\n");
1182 	printf("      base register:  0x%08x\n", regs[o2i(0x24)]);
1183 	printf("      limit register: 0x%08x\n", regs[o2i(0x28)]);
1184 	printf("    CardBus I/O region 0:\n");
1185 	printf("      base register:  0x%08x\n", regs[o2i(0x2c)]);
1186 	printf("      limit register: 0x%08x\n", regs[o2i(0x30)]);
1187 	printf("    CardBus I/O region 1:\n");
1188 	printf("      base register:  0x%08x\n", regs[o2i(0x34)]);
1189 	printf("      limit register: 0x%08x\n", regs[o2i(0x38)]);
1190 
1191 	printf("    Interrupt line: 0x%02x\n",
1192 	    (regs[o2i(0x3c)] >> 0) & 0xff);
1193 	printf("    Interrupt pin: 0x%02x ",
1194 	    (regs[o2i(0x3c)] >> 8) & 0xff);
1195 	switch ((regs[o2i(0x3c)] >> 8) & 0xff) {
1196 	case PCI_INTERRUPT_PIN_NONE:
1197 		printf("(none)");
1198 		break;
1199 	case PCI_INTERRUPT_PIN_A:
1200 		printf("(pin A)");
1201 		break;
1202 	case PCI_INTERRUPT_PIN_B:
1203 		printf("(pin B)");
1204 		break;
1205 	case PCI_INTERRUPT_PIN_C:
1206 		printf("(pin C)");
1207 		break;
1208 	case PCI_INTERRUPT_PIN_D:
1209 		printf("(pin D)");
1210 		break;
1211 	default:
1212 		printf("(? ? ?)");
1213 		break;
1214 	}
1215 	printf("\n");
1216 	rval = (regs[o2i(0x3c)] >> 16) & 0xffff;
1217 	printf("    Bridge control register: 0x%04x\n", rval);
1218 	onoff("Parity error response", 0x0001);
1219 	onoff("CardBus SERR forwarding", 0x0002);
1220 	onoff("ISA enable", 0x0004);
1221 	onoff("VGA enable", 0x0008);
1222 	onoff("CardBus master abort reporting", 0x0020);
1223 	onoff("CardBus reset", 0x0040);
1224 	onoff("Functional interrupts routed by ExCA registers", 0x0080);
1225 	onoff("Memory window 0 prefetchable", 0x0100);
1226 	onoff("Memory window 1 prefetchable", 0x0200);
1227 	onoff("Write posting enable", 0x0400);
1228 
1229 	rval = regs[o2i(0x40)];
1230 	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
1231 	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));
1232 
1233 #ifdef _KERNEL
1234 	pci_conf_print_bar(pc, tag, regs, 0x44, "legacy-mode registers",
1235 	    sizebars);
1236 #else
1237 	pci_conf_print_bar(regs, 0x44, "legacy-mode registers");
1238 #endif
1239 }
1240 
1241 void
1242 pci_conf_print(
1243 #ifdef _KERNEL
1244     pci_chipset_tag_t pc, pcitag_t tag,
1245     void (*printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *)
1246 #else
1247     int pcifd, u_int bus, u_int dev, u_int func
1248 #endif
1249     )
1250 {
1251 	pcireg_t regs[o2i(256)];
1252 	int off, capoff, endoff, hdrtype;
1253 	const char *typename;
1254 #ifdef _KERNEL
1255 	void (*typeprintfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *, int);
1256 	int sizebars;
1257 #else
1258 	void (*typeprintfn)(const pcireg_t *);
1259 #endif
1260 
1261 	printf("PCI configuration registers:\n");
1262 
1263 	for (off = 0; off < 256; off += 4) {
1264 #ifdef _KERNEL
1265 		regs[o2i(off)] = pci_conf_read(pc, tag, off);
1266 #else
1267 		if (pcibus_conf_read(pcifd, bus, dev, func, off,
1268 		    &regs[o2i(off)]) == -1)
1269 			regs[o2i(off)] = 0;
1270 #endif
1271 	}
1272 
1273 #ifdef _KERNEL
1274 	sizebars = 1;
1275 	if (PCI_CLASS(regs[o2i(PCI_CLASS_REG)]) == PCI_CLASS_BRIDGE &&
1276 	    PCI_SUBCLASS(regs[o2i(PCI_CLASS_REG)]) == PCI_SUBCLASS_BRIDGE_HOST)
1277 		sizebars = 0;
1278 #endif
1279 
1280 	/* common header */
1281 	printf("  Common header:\n");
1282 	pci_conf_print_regs(regs, 0, 16);
1283 
1284 	printf("\n");
1285 #ifdef _KERNEL
1286 	pci_conf_print_common(pc, tag, regs);
1287 #else
1288 	pci_conf_print_common(regs);
1289 #endif
1290 	printf("\n");
1291 
1292 	/* type-dependent header */
1293 	hdrtype = PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]);
1294 	switch (hdrtype) {		/* XXX make a table, eventually */
1295 	case 0:
1296 		/* Standard device header */
1297 		typename = "\"normal\" device";
1298 		typeprintfn = &pci_conf_print_type0;
1299 		capoff = PCI_CAPLISTPTR_REG;
1300 		endoff = 64;
1301 		break;
1302 	case 1:
1303 		/* PCI-PCI bridge header */
1304 		typename = "PCI-PCI bridge";
1305 		typeprintfn = &pci_conf_print_type1;
1306 		capoff = PCI_CAPLISTPTR_REG;
1307 		endoff = 64;
1308 		break;
1309 	case 2:
1310 		/* PCI-CardBus bridge header */
1311 		typename = "PCI-CardBus bridge";
1312 		typeprintfn = &pci_conf_print_type2;
1313 		capoff = PCI_CARDBUS_CAPLISTPTR_REG;
1314 		endoff = 72;
1315 		break;
1316 	default:
1317 		typename = NULL;
1318 		typeprintfn = 0;
1319 		capoff = -1;
1320 		endoff = 64;
1321 		break;
1322 	}
1323 	printf("  Type %d ", hdrtype);
1324 	if (typename != NULL)
1325 		printf("(%s) ", typename);
1326 	printf("header:\n");
1327 	pci_conf_print_regs(regs, 16, endoff);
1328 	printf("\n");
1329 	if (typeprintfn) {
1330 #ifdef _KERNEL
1331 		(*typeprintfn)(pc, tag, regs, sizebars);
1332 #else
1333 		(*typeprintfn)(regs);
1334 #endif
1335 	} else
1336 		printf("    Don't know how to pretty-print type %d header.\n",
1337 		    hdrtype);
1338 	printf("\n");
1339 
1340 	/* capability list, if present */
1341 	if ((regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
1342 		&& (capoff > 0)) {
1343 #ifdef _KERNEL
1344 		pci_conf_print_caplist(pc, tag, regs, capoff);
1345 #else
1346 		pci_conf_print_caplist(regs, capoff);
1347 #endif
1348 		printf("\n");
1349 	}
1350 
1351 	/* device-dependent header */
1352 	printf("  Device-dependent header:\n");
1353 	pci_conf_print_regs(regs, endoff, 256);
1354 	printf("\n");
1355 #ifdef _KERNEL
1356 	if (printfn)
1357 		(*printfn)(pc, tag, regs);
1358 	else
1359 		printf("    Don't know how to pretty-print device-dependent header.\n");
1360 	printf("\n");
1361 #endif /* _KERNEL */
1362 }
1363