xref: /freebsd/usr.sbin/pciconf/cap.c (revision 4133f23624058951a3b66e3ad735de980a485f36)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007 Yahoo!, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <strings.h>
43 #include <sys/agpio.h>
44 #include <sys/pciio.h>
45 
46 #include <dev/agp/agpreg.h>
47 #include <dev/pci/pcireg.h>
48 
49 #include "pciconf.h"
50 
51 static void	list_ecaps(int fd, struct pci_conf *p);
52 
53 static void
54 cap_power(int fd, struct pci_conf *p, uint8_t ptr)
55 {
56 	uint16_t cap, status;
57 
58 	cap = read_config(fd, &p->pc_sel, ptr + PCIR_POWER_CAP, 2);
59 	status = read_config(fd, &p->pc_sel, ptr + PCIR_POWER_STATUS, 2);
60 	printf("powerspec %d  supports D0%s%s D3  current D%d",
61 	    cap & PCIM_PCAP_SPEC,
62 	    cap & PCIM_PCAP_D1SUPP ? " D1" : "",
63 	    cap & PCIM_PCAP_D2SUPP ? " D2" : "",
64 	    status & PCIM_PSTAT_DMASK);
65 }
66 
67 static void
68 cap_agp(int fd, struct pci_conf *p, uint8_t ptr)
69 {
70 	uint32_t status, command;
71 
72 	status = read_config(fd, &p->pc_sel, ptr + AGP_STATUS, 4);
73 	command = read_config(fd, &p->pc_sel, ptr + AGP_CAPID, 4);
74 	printf("AGP ");
75 	if (AGP_MODE_GET_MODE_3(status)) {
76 		printf("v3 ");
77 		if (AGP_MODE_GET_RATE(status) & AGP_MODE_V3_RATE_8x)
78 			printf("8x ");
79 		if (AGP_MODE_GET_RATE(status) & AGP_MODE_V3_RATE_4x)
80 			printf("4x ");
81 	} else {
82 		if (AGP_MODE_GET_RATE(status) & AGP_MODE_V2_RATE_4x)
83 			printf("4x ");
84 		if (AGP_MODE_GET_RATE(status) & AGP_MODE_V2_RATE_2x)
85 			printf("2x ");
86 		if (AGP_MODE_GET_RATE(status) & AGP_MODE_V2_RATE_1x)
87 			printf("1x ");
88 	}
89 	if (AGP_MODE_GET_SBA(status))
90 		printf("SBA ");
91 	if (AGP_MODE_GET_AGP(command)) {
92 		printf("enabled at ");
93 		if (AGP_MODE_GET_MODE_3(command)) {
94 			printf("v3 ");
95 			switch (AGP_MODE_GET_RATE(command)) {
96 			case AGP_MODE_V3_RATE_8x:
97 				printf("8x ");
98 				break;
99 			case AGP_MODE_V3_RATE_4x:
100 				printf("4x ");
101 				break;
102 			}
103 		} else
104 			switch (AGP_MODE_GET_RATE(command)) {
105 			case AGP_MODE_V2_RATE_4x:
106 				printf("4x ");
107 				break;
108 			case AGP_MODE_V2_RATE_2x:
109 				printf("2x ");
110 				break;
111 			case AGP_MODE_V2_RATE_1x:
112 				printf("1x ");
113 				break;
114 			}
115 		if (AGP_MODE_GET_SBA(command))
116 			printf("SBA ");
117 	} else
118 		printf("disabled");
119 }
120 
121 static void
122 cap_vpd(int fd __unused, struct pci_conf *p __unused, uint8_t ptr __unused)
123 {
124 
125 	printf("VPD");
126 }
127 
128 static void
129 cap_msi(int fd, struct pci_conf *p, uint8_t ptr)
130 {
131 	uint16_t ctrl;
132 	int msgnum;
133 
134 	ctrl = read_config(fd, &p->pc_sel, ptr + PCIR_MSI_CTRL, 2);
135 	msgnum = 1 << ((ctrl & PCIM_MSICTRL_MMC_MASK) >> 1);
136 	printf("MSI supports %d message%s%s%s ", msgnum,
137 	    (msgnum == 1) ? "" : "s",
138 	    (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
139 	    (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks" : "");
140 	if (ctrl & PCIM_MSICTRL_MSI_ENABLE) {
141 		msgnum = 1 << ((ctrl & PCIM_MSICTRL_MME_MASK) >> 4);
142 		printf("enabled with %d message%s", msgnum,
143 		    (msgnum == 1) ? "" : "s");
144 	}
145 }
146 
147 static void
148 cap_pcix(int fd, struct pci_conf *p, uint8_t ptr)
149 {
150 	uint32_t status;
151 	int comma, max_splits, max_burst_read;
152 
153 	status = read_config(fd, &p->pc_sel, ptr + PCIXR_STATUS, 4);
154 	printf("PCI-X ");
155 	if (status & PCIXM_STATUS_64BIT)
156 		printf("64-bit ");
157 	if ((p->pc_hdr & PCIM_HDRTYPE) == 1)
158 		printf("bridge ");
159 	if ((p->pc_hdr & PCIM_HDRTYPE) != 1 || (status & (PCIXM_STATUS_133CAP |
160 	    PCIXM_STATUS_266CAP | PCIXM_STATUS_533CAP)) != 0)
161 		printf("supports");
162 	comma = 0;
163 	if (status & PCIXM_STATUS_133CAP) {
164 		printf(" 133MHz");
165 		comma = 1;
166 	}
167 	if (status & PCIXM_STATUS_266CAP) {
168 		printf("%s 266MHz", comma ? "," : "");
169 		comma = 1;
170 	}
171 	if (status & PCIXM_STATUS_533CAP) {
172 		printf("%s 533MHz", comma ? "," : "");
173 		comma = 1;
174 	}
175 	if ((p->pc_hdr & PCIM_HDRTYPE) == 1)
176 		return;
177 	max_burst_read = 0;
178 	switch (status & PCIXM_STATUS_MAX_READ) {
179 	case PCIXM_STATUS_MAX_READ_512:
180 		max_burst_read = 512;
181 		break;
182 	case PCIXM_STATUS_MAX_READ_1024:
183 		max_burst_read = 1024;
184 		break;
185 	case PCIXM_STATUS_MAX_READ_2048:
186 		max_burst_read = 2048;
187 		break;
188 	case PCIXM_STATUS_MAX_READ_4096:
189 		max_burst_read = 4096;
190 		break;
191 	}
192 	max_splits = 0;
193 	switch (status & PCIXM_STATUS_MAX_SPLITS) {
194 	case PCIXM_STATUS_MAX_SPLITS_1:
195 		max_splits = 1;
196 		break;
197 	case PCIXM_STATUS_MAX_SPLITS_2:
198 		max_splits = 2;
199 		break;
200 	case PCIXM_STATUS_MAX_SPLITS_3:
201 		max_splits = 3;
202 		break;
203 	case PCIXM_STATUS_MAX_SPLITS_4:
204 		max_splits = 4;
205 		break;
206 	case PCIXM_STATUS_MAX_SPLITS_8:
207 		max_splits = 8;
208 		break;
209 	case PCIXM_STATUS_MAX_SPLITS_12:
210 		max_splits = 12;
211 		break;
212 	case PCIXM_STATUS_MAX_SPLITS_16:
213 		max_splits = 16;
214 		break;
215 	case PCIXM_STATUS_MAX_SPLITS_32:
216 		max_splits = 32;
217 		break;
218 	}
219 	printf("%s %d burst read, %d split transaction%s", comma ? "," : "",
220 	    max_burst_read, max_splits, max_splits == 1 ? "" : "s");
221 }
222 
223 static void
224 cap_ht(int fd, struct pci_conf *p, uint8_t ptr)
225 {
226 	uint32_t reg;
227 	uint16_t command;
228 
229 	command = read_config(fd, &p->pc_sel, ptr + PCIR_HT_COMMAND, 2);
230 	printf("HT ");
231 	if ((command & 0xe000) == PCIM_HTCAP_SLAVE)
232 		printf("slave");
233 	else if ((command & 0xe000) == PCIM_HTCAP_HOST)
234 		printf("host");
235 	else
236 		switch (command & PCIM_HTCMD_CAP_MASK) {
237 		case PCIM_HTCAP_SWITCH:
238 			printf("switch");
239 			break;
240 		case PCIM_HTCAP_INTERRUPT:
241 			printf("interrupt");
242 			break;
243 		case PCIM_HTCAP_REVISION_ID:
244 			printf("revision ID");
245 			break;
246 		case PCIM_HTCAP_UNITID_CLUMPING:
247 			printf("unit ID clumping");
248 			break;
249 		case PCIM_HTCAP_EXT_CONFIG_SPACE:
250 			printf("extended config space");
251 			break;
252 		case PCIM_HTCAP_ADDRESS_MAPPING:
253 			printf("address mapping");
254 			break;
255 		case PCIM_HTCAP_MSI_MAPPING:
256 			printf("MSI %saddress window %s at 0x",
257 			    command & PCIM_HTCMD_MSI_FIXED ? "fixed " : "",
258 			    command & PCIM_HTCMD_MSI_ENABLE ? "enabled" :
259 			    "disabled");
260 			if (command & PCIM_HTCMD_MSI_FIXED)
261 				printf("fee00000");
262 			else {
263 				reg = read_config(fd, &p->pc_sel,
264 				    ptr + PCIR_HTMSI_ADDRESS_HI, 4);
265 				if (reg != 0)
266 					printf("%08x", reg);
267 				reg = read_config(fd, &p->pc_sel,
268 				    ptr + PCIR_HTMSI_ADDRESS_LO, 4);
269 				printf("%08x", reg);
270 			}
271 			break;
272 		case PCIM_HTCAP_DIRECT_ROUTE:
273 			printf("direct route");
274 			break;
275 		case PCIM_HTCAP_VCSET:
276 			printf("VC set");
277 			break;
278 		case PCIM_HTCAP_RETRY_MODE:
279 			printf("retry mode");
280 			break;
281 		case PCIM_HTCAP_X86_ENCODING:
282 			printf("X86 encoding");
283 			break;
284 		case PCIM_HTCAP_GEN3:
285 			printf("Gen3");
286 			break;
287 		case PCIM_HTCAP_FLE:
288 			printf("function-level extension");
289 			break;
290 		case PCIM_HTCAP_PM:
291 			printf("power management");
292 			break;
293 		case PCIM_HTCAP_HIGH_NODE_COUNT:
294 			printf("high node count");
295 			break;
296 		default:
297 			printf("unknown %02x", command);
298 			break;
299 		}
300 }
301 
302 static void
303 cap_vendor(int fd, struct pci_conf *p, uint8_t ptr)
304 {
305 	uint8_t length;
306 
307 	length = read_config(fd, &p->pc_sel, ptr + PCIR_VENDOR_LENGTH, 1);
308 	printf("vendor (length %d)", length);
309 	if (p->pc_vendor == 0x8086) {
310 		/* Intel */
311 		uint8_t version;
312 
313 		version = read_config(fd, &p->pc_sel, ptr + PCIR_VENDOR_DATA,
314 		    1);
315 		printf(" Intel cap %d version %d", version >> 4, version & 0xf);
316 		if (version >> 4 == 1 && length == 12) {
317 			/* Feature Detection */
318 			uint32_t fvec;
319 			int comma;
320 
321 			comma = 0;
322 			fvec = read_config(fd, &p->pc_sel, ptr +
323 			    PCIR_VENDOR_DATA + 5, 4);
324 			printf("\n\t\t features:");
325 			if (fvec & (1 << 0)) {
326 				printf(" AMT");
327 				comma = 1;
328 			}
329 			fvec = read_config(fd, &p->pc_sel, ptr +
330 			    PCIR_VENDOR_DATA + 1, 4);
331 			if (fvec & (1 << 21)) {
332 				printf("%s Quick Resume", comma ? "," : "");
333 				comma = 1;
334 			}
335 			if (fvec & (1 << 18)) {
336 				printf("%s SATA RAID-5", comma ? "," : "");
337 				comma = 1;
338 			}
339 			if (fvec & (1 << 9)) {
340 				printf("%s Mobile", comma ? "," : "");
341 				comma = 1;
342 			}
343 			if (fvec & (1 << 7)) {
344 				printf("%s 6 PCI-e x1 slots", comma ? "," : "");
345 				comma = 1;
346 			} else {
347 				printf("%s 4 PCI-e x1 slots", comma ? "," : "");
348 				comma = 1;
349 			}
350 			if (fvec & (1 << 5)) {
351 				printf("%s SATA RAID-0/1/10", comma ? "," : "");
352 				comma = 1;
353 			}
354 			if (fvec & (1 << 3))
355 				printf(", SATA AHCI");
356 		}
357 	}
358 }
359 
360 static void
361 cap_debug(int fd, struct pci_conf *p, uint8_t ptr)
362 {
363 	uint16_t debug_port;
364 
365 	debug_port = read_config(fd, &p->pc_sel, ptr + PCIR_DEBUG_PORT, 2);
366 	printf("EHCI Debug Port at offset 0x%x in map 0x%x", debug_port &
367 	    PCIM_DEBUG_PORT_OFFSET, PCIR_BAR(debug_port >> 13));
368 }
369 
370 static void
371 cap_subvendor(int fd, struct pci_conf *p, uint8_t ptr)
372 {
373 	uint32_t id;
374 	uint16_t ssid, ssvid;
375 
376 	id = read_config(fd, &p->pc_sel, ptr + PCIR_SUBVENDCAP_ID, 4);
377 	ssid = id >> 16;
378 	ssvid = id & 0xffff;
379 	printf("PCI Bridge subvendor=0x%04x subdevice=0x%04x", ssvid, ssid);
380 }
381 
382 #define	MAX_PAYLOAD(field)		(128 << (field))
383 
384 static const char *
385 link_speed_string(uint8_t speed)
386 {
387 
388 	switch (speed) {
389 	case 1:
390 		return ("2.5");
391 	case 2:
392 		return ("5.0");
393 	case 3:
394 		return ("8.0");
395 	case 4:
396 		return ("16.0");
397 	default:
398 		return ("undef");
399 	}
400 }
401 
402 static const char *
403 aspm_string(uint8_t aspm)
404 {
405 
406 	switch (aspm) {
407 	case 1:
408 		return ("L0s");
409 	case 2:
410 		return ("L1");
411 	case 3:
412 		return ("L0s/L1");
413 	default:
414 		return ("disabled");
415 	}
416 }
417 
418 static int
419 slot_power(uint32_t cap)
420 {
421 	int mwatts;
422 
423 	mwatts = (cap & PCIEM_SLOT_CAP_SPLV) >> 7;
424 	switch (cap & PCIEM_SLOT_CAP_SPLS) {
425 	case 0x0:
426 		mwatts *= 1000;
427 		break;
428 	case 0x1:
429 		mwatts *= 100;
430 		break;
431 	case 0x2:
432 		mwatts *= 10;
433 		break;
434 	default:
435 		break;
436 	}
437 	return (mwatts);
438 }
439 
440 static void
441 cap_express(int fd, struct pci_conf *p, uint8_t ptr)
442 {
443 	uint32_t cap;
444 	uint16_t ctl, flags, sta;
445 	unsigned int version;
446 
447 	flags = read_config(fd, &p->pc_sel, ptr + PCIER_FLAGS, 2);
448 	version = flags & PCIEM_FLAGS_VERSION;
449 	printf("PCI-Express %u ", version);
450 	switch (flags & PCIEM_FLAGS_TYPE) {
451 	case PCIEM_TYPE_ENDPOINT:
452 		printf("endpoint");
453 		break;
454 	case PCIEM_TYPE_LEGACY_ENDPOINT:
455 		printf("legacy endpoint");
456 		break;
457 	case PCIEM_TYPE_ROOT_PORT:
458 		printf("root port");
459 		break;
460 	case PCIEM_TYPE_UPSTREAM_PORT:
461 		printf("upstream port");
462 		break;
463 	case PCIEM_TYPE_DOWNSTREAM_PORT:
464 		printf("downstream port");
465 		break;
466 	case PCIEM_TYPE_PCI_BRIDGE:
467 		printf("PCI bridge");
468 		break;
469 	case PCIEM_TYPE_PCIE_BRIDGE:
470 		printf("PCI to PCIe bridge");
471 		break;
472 	case PCIEM_TYPE_ROOT_INT_EP:
473 		printf("root endpoint");
474 		break;
475 	case PCIEM_TYPE_ROOT_EC:
476 		printf("event collector");
477 		break;
478 	default:
479 		printf("type %d", (flags & PCIEM_FLAGS_TYPE) >> 4);
480 		break;
481 	}
482 	if (flags & PCIEM_FLAGS_IRQ)
483 		printf(" MSI %d", (flags & PCIEM_FLAGS_IRQ) >> 9);
484 	cap = read_config(fd, &p->pc_sel, ptr + PCIER_DEVICE_CAP, 4);
485 	ctl = read_config(fd, &p->pc_sel, ptr + PCIER_DEVICE_CTL, 2);
486 	printf(" max data %d(%d)",
487 	    MAX_PAYLOAD((ctl & PCIEM_CTL_MAX_PAYLOAD) >> 5),
488 	    MAX_PAYLOAD(cap & PCIEM_CAP_MAX_PAYLOAD));
489 	if ((cap & PCIEM_CAP_FLR) != 0)
490 		printf(" FLR");
491 	if (ctl & PCIEM_CTL_RELAXED_ORD_ENABLE)
492 		printf(" RO");
493 	if (ctl & PCIEM_CTL_NOSNOOP_ENABLE)
494 		printf(" NS");
495 	if (version >= 2) {
496 		cap = read_config(fd, &p->pc_sel, ptr + PCIER_DEVICE_CAP2, 4);
497 		if ((cap & PCIEM_CAP2_ARI) != 0) {
498 			ctl = read_config(fd, &p->pc_sel,
499 			    ptr + PCIER_DEVICE_CTL2, 4);
500 			printf(" ARI %s",
501 			    (ctl & PCIEM_CTL2_ARI) ? "enabled" : "disabled");
502 		}
503 	}
504 	cap = read_config(fd, &p->pc_sel, ptr + PCIER_LINK_CAP, 4);
505 	sta = read_config(fd, &p->pc_sel, ptr + PCIER_LINK_STA, 2);
506 	if (cap == 0 && sta == 0)
507 		return;
508 	printf("\n                ");
509 	printf(" link x%d(x%d)", (sta & PCIEM_LINK_STA_WIDTH) >> 4,
510 	    (cap & PCIEM_LINK_CAP_MAX_WIDTH) >> 4);
511 	if ((cap & PCIEM_LINK_CAP_MAX_WIDTH) != 0) {
512 		printf(" speed %s(%s)", (sta & PCIEM_LINK_STA_WIDTH) == 0 ?
513 		    "0.0" : link_speed_string(sta & PCIEM_LINK_STA_SPEED),
514 	    	    link_speed_string(cap & PCIEM_LINK_CAP_MAX_SPEED));
515 	}
516 	if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
517 		ctl = read_config(fd, &p->pc_sel, ptr + PCIER_LINK_CTL, 2);
518 		printf(" ASPM %s(%s)", aspm_string(ctl & PCIEM_LINK_CTL_ASPMC),
519 		    aspm_string((cap & PCIEM_LINK_CAP_ASPM) >> 10));
520 	}
521 	if ((cap & PCIEM_LINK_CAP_CLOCK_PM) != 0) {
522 		ctl = read_config(fd, &p->pc_sel, ptr + PCIER_LINK_CTL, 2);
523 		printf(" ClockPM %s", (ctl & PCIEM_LINK_CTL_ECPM) ?
524 		    "enabled" : "disabled");
525 	}
526 	if (!(flags & PCIEM_FLAGS_SLOT))
527 		return;
528 	cap = read_config(fd, &p->pc_sel, ptr + PCIER_SLOT_CAP, 4);
529 	sta = read_config(fd, &p->pc_sel, ptr + PCIER_SLOT_STA, 2);
530 	ctl = read_config(fd, &p->pc_sel, ptr + PCIER_SLOT_CTL, 2);
531 	printf("\n                ");
532 	printf(" slot %d", (cap & PCIEM_SLOT_CAP_PSN) >> 19);
533 	printf(" power limit %d mW", slot_power(cap));
534 	if (cap & PCIEM_SLOT_CAP_HPC)
535 		printf(" HotPlug(%s)", sta & PCIEM_SLOT_STA_PDS ? "present" :
536 		    "empty");
537 	if (cap & PCIEM_SLOT_CAP_HPS)
538 		printf(" surprise");
539 	if (cap & PCIEM_SLOT_CAP_APB)
540 		printf(" Attn Button");
541 	if (cap & PCIEM_SLOT_CAP_PCP)
542 		printf(" PC(%s)", ctl & PCIEM_SLOT_CTL_PCC ? "off" : "on");
543 	if (cap & PCIEM_SLOT_CAP_MRLSP)
544 		printf(" MRL(%s)", sta & PCIEM_SLOT_STA_MRLSS ? "open" :
545 		    "closed");
546 	if (cap & PCIEM_SLOT_CAP_EIP)
547 		printf(" EI(%s)", sta & PCIEM_SLOT_STA_EIS ? "engaged" :
548 		    "disengaged");
549 }
550 
551 static void
552 cap_msix(int fd, struct pci_conf *p, uint8_t ptr)
553 {
554 	uint32_t pba_offset, table_offset, val;
555 	int msgnum, pba_bar, table_bar;
556 	uint16_t ctrl;
557 
558 	ctrl = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_CTRL, 2);
559 	msgnum = (ctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1;
560 
561 	val = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_TABLE, 4);
562 	table_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
563 	table_offset = val & ~PCIM_MSIX_BIR_MASK;
564 
565 	val = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_PBA, 4);
566 	pba_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
567 	pba_offset = val & ~PCIM_MSIX_BIR_MASK;
568 
569 	printf("MSI-X supports %d message%s%s\n", msgnum,
570 	    (msgnum == 1) ? "" : "s",
571 	    (ctrl & PCIM_MSIXCTRL_MSIX_ENABLE) ? ", enabled" : "");
572 
573 	printf("                 ");
574 	printf("Table in map 0x%x[0x%x], PBA in map 0x%x[0x%x]",
575 	    table_bar, table_offset, pba_bar, pba_offset);
576 }
577 
578 static void
579 cap_sata(int fd __unused, struct pci_conf *p __unused, uint8_t ptr __unused)
580 {
581 
582 	printf("SATA Index-Data Pair");
583 }
584 
585 static void
586 cap_pciaf(int fd, struct pci_conf *p, uint8_t ptr)
587 {
588 	uint8_t cap;
589 
590 	cap = read_config(fd, &p->pc_sel, ptr + PCIR_PCIAF_CAP, 1);
591 	printf("PCI Advanced Features:%s%s",
592 	    cap & PCIM_PCIAFCAP_FLR ? " FLR" : "",
593 	    cap & PCIM_PCIAFCAP_TP  ? " TP"  : "");
594 }
595 
596 static const char *
597 ea_bei_to_name(int bei)
598 {
599 	static const char *barstr[] = {
600 		"BAR0", "BAR1", "BAR2", "BAR3", "BAR4", "BAR5"
601 	};
602 	static const char *vfbarstr[] = {
603 		"VFBAR0", "VFBAR1", "VFBAR2", "VFBAR3", "VFBAR4", "VFBAR5"
604 	};
605 
606 	if ((bei >= PCIM_EA_BEI_BAR_0) && (bei <= PCIM_EA_BEI_BAR_5))
607 		return (barstr[bei - PCIM_EA_BEI_BAR_0]);
608 	if ((bei >= PCIM_EA_BEI_VF_BAR_0) && (bei <= PCIM_EA_BEI_VF_BAR_5))
609 		return (vfbarstr[bei - PCIM_EA_BEI_VF_BAR_0]);
610 
611 	switch (bei) {
612 	case PCIM_EA_BEI_BRIDGE:
613 		return "BRIDGE";
614 	case PCIM_EA_BEI_ENI:
615 		return "ENI";
616 	case PCIM_EA_BEI_ROM:
617 		return "ROM";
618 	case PCIM_EA_BEI_RESERVED:
619 	default:
620 		return "RSVD";
621 	}
622 }
623 
624 static const char *
625 ea_prop_to_name(uint8_t prop)
626 {
627 
628 	switch (prop) {
629 	case PCIM_EA_P_MEM:
630 		return "Non-Prefetchable Memory";
631 	case PCIM_EA_P_MEM_PREFETCH:
632 		return "Prefetchable Memory";
633 	case PCIM_EA_P_IO:
634 		return "I/O Space";
635 	case PCIM_EA_P_VF_MEM_PREFETCH:
636 		return "VF Prefetchable Memory";
637 	case PCIM_EA_P_VF_MEM:
638 		return "VF Non-Prefetchable Memory";
639 	case PCIM_EA_P_BRIDGE_MEM:
640 		return "Bridge Non-Prefetchable Memory";
641 	case PCIM_EA_P_BRIDGE_MEM_PREFETCH:
642 		return "Bridge Prefetchable Memory";
643 	case PCIM_EA_P_BRIDGE_IO:
644 		return "Bridge I/O Space";
645 	case PCIM_EA_P_MEM_RESERVED:
646 		return "Reserved Memory";
647 	case PCIM_EA_P_IO_RESERVED:
648 		return "Reserved I/O Space";
649 	case PCIM_EA_P_UNAVAILABLE:
650 		return "Unavailable";
651 	default:
652 		return "Reserved";
653 	}
654 }
655 
656 static void
657 cap_ea(int fd, struct pci_conf *p, uint8_t ptr)
658 {
659 	int num_ent;
660 	int a, b;
661 	uint32_t bei;
662 	uint32_t val;
663 	int ent_size;
664 	uint32_t dw[4];
665 	uint32_t flags, flags_pp, flags_sp;
666 	uint64_t base, max_offset;
667 	uint8_t fixed_sub_bus_nr, fixed_sec_bus_nr;
668 
669 	/* Determine the number of entries */
670 	num_ent = read_config(fd, &p->pc_sel, ptr + PCIR_EA_NUM_ENT, 2);
671 	num_ent &= PCIM_EA_NUM_ENT_MASK;
672 
673 	printf("PCI Enhanced Allocation (%d entries)", num_ent);
674 
675 	/* Find the first entry to care of */
676 	ptr += PCIR_EA_FIRST_ENT;
677 
678 	/* Print BUS numbers for bridges */
679 	if ((p->pc_hdr & PCIM_HDRTYPE) == PCIM_HDRTYPE_BRIDGE) {
680 		val = read_config(fd, &p->pc_sel, ptr, 4);
681 
682 		fixed_sec_bus_nr = PCIM_EA_SEC_NR(val);
683 		fixed_sub_bus_nr = PCIM_EA_SUB_NR(val);
684 
685 		printf("\n\t\t BRIDGE, sec bus [%d], sub bus [%d]",
686 		    fixed_sec_bus_nr, fixed_sub_bus_nr);
687 		ptr += 4;
688 	}
689 
690 	for (a = 0; a < num_ent; a++) {
691 		/* Read a number of dwords in the entry */
692 		val = read_config(fd, &p->pc_sel, ptr, 4);
693 		ptr += 4;
694 		ent_size = (val & PCIM_EA_ES);
695 
696 		for (b = 0; b < ent_size; b++) {
697 			dw[b] = read_config(fd, &p->pc_sel, ptr, 4);
698 			ptr += 4;
699 		}
700 
701 		flags = val;
702 		flags_pp = (flags & PCIM_EA_PP) >> PCIM_EA_PP_OFFSET;
703 		flags_sp = (flags & PCIM_EA_SP) >> PCIM_EA_SP_OFFSET;
704 		bei = (PCIM_EA_BEI & val) >> PCIM_EA_BEI_OFFSET;
705 
706 		base = dw[0] & PCIM_EA_FIELD_MASK;
707 		max_offset = dw[1] | ~PCIM_EA_FIELD_MASK;
708 		b = 2;
709 		if (((dw[0] & PCIM_EA_IS_64) != 0) && (b < ent_size)) {
710 			base |= (uint64_t)dw[b] << 32UL;
711 			b++;
712 		}
713 		if (((dw[1] & PCIM_EA_IS_64) != 0)
714 			&& (b < ent_size)) {
715 			max_offset |= (uint64_t)dw[b] << 32UL;
716 			b++;
717 		}
718 
719 		printf("\n\t\t [%d] %s, %s, %s, base [0x%jx], size [0x%jx]"
720 		    "\n\t\t\tPrimary properties [0x%x] (%s)"
721 		    "\n\t\t\tSecondary properties [0x%x] (%s)",
722 		    bei, ea_bei_to_name(bei),
723 		    (flags & PCIM_EA_ENABLE ? "Enabled" : "Disabled"),
724 		    (flags & PCIM_EA_WRITABLE ? "Writable" : "Read-only"),
725 		    (uintmax_t)base, (uintmax_t)(max_offset + 1),
726 		    flags_pp, ea_prop_to_name(flags_pp),
727 		    flags_sp, ea_prop_to_name(flags_sp));
728 	}
729 }
730 
731 void
732 list_caps(int fd, struct pci_conf *p)
733 {
734 	int express;
735 	uint16_t sta;
736 	uint8_t ptr, cap;
737 
738 	/* Are capabilities present for this device? */
739 	sta = read_config(fd, &p->pc_sel, PCIR_STATUS, 2);
740 	if (!(sta & PCIM_STATUS_CAPPRESENT))
741 		return;
742 
743 	switch (p->pc_hdr & PCIM_HDRTYPE) {
744 	case PCIM_HDRTYPE_NORMAL:
745 	case PCIM_HDRTYPE_BRIDGE:
746 		ptr = PCIR_CAP_PTR;
747 		break;
748 	case PCIM_HDRTYPE_CARDBUS:
749 		ptr = PCIR_CAP_PTR_2;
750 		break;
751 	default:
752 		errx(1, "list_caps: bad header type");
753 	}
754 
755 	/* Walk the capability list. */
756 	express = 0;
757 	ptr = read_config(fd, &p->pc_sel, ptr, 1);
758 	while (ptr != 0 && ptr != 0xff) {
759 		cap = read_config(fd, &p->pc_sel, ptr + PCICAP_ID, 1);
760 		printf("    cap %02x[%02x] = ", cap, ptr);
761 		switch (cap) {
762 		case PCIY_PMG:
763 			cap_power(fd, p, ptr);
764 			break;
765 		case PCIY_AGP:
766 			cap_agp(fd, p, ptr);
767 			break;
768 		case PCIY_VPD:
769 			cap_vpd(fd, p, ptr);
770 			break;
771 		case PCIY_MSI:
772 			cap_msi(fd, p, ptr);
773 			break;
774 		case PCIY_PCIX:
775 			cap_pcix(fd, p, ptr);
776 			break;
777 		case PCIY_HT:
778 			cap_ht(fd, p, ptr);
779 			break;
780 		case PCIY_VENDOR:
781 			cap_vendor(fd, p, ptr);
782 			break;
783 		case PCIY_DEBUG:
784 			cap_debug(fd, p, ptr);
785 			break;
786 		case PCIY_SUBVENDOR:
787 			cap_subvendor(fd, p, ptr);
788 			break;
789 		case PCIY_EXPRESS:
790 			express = 1;
791 			cap_express(fd, p, ptr);
792 			break;
793 		case PCIY_MSIX:
794 			cap_msix(fd, p, ptr);
795 			break;
796 		case PCIY_SATA:
797 			cap_sata(fd, p, ptr);
798 			break;
799 		case PCIY_PCIAF:
800 			cap_pciaf(fd, p, ptr);
801 			break;
802 		case PCIY_EA:
803 			cap_ea(fd, p, ptr);
804 			break;
805 		default:
806 			printf("unknown");
807 			break;
808 		}
809 		printf("\n");
810 		ptr = read_config(fd, &p->pc_sel, ptr + PCICAP_NEXTPTR, 1);
811 	}
812 
813 	if (express)
814 		list_ecaps(fd, p);
815 }
816 
817 /* From <sys/systm.h>. */
818 static __inline uint32_t
819 bitcount32(uint32_t x)
820 {
821 
822 	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
823 	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
824 	x = (x + (x >> 4)) & 0x0f0f0f0f;
825 	x = (x + (x >> 8));
826 	x = (x + (x >> 16)) & 0x000000ff;
827 	return (x);
828 }
829 
830 static void
831 ecap_aer(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
832 {
833 	uint32_t sta, mask;
834 
835 	printf("AER %d", ver);
836 	if (ver < 1)
837 		return;
838 	sta = read_config(fd, &p->pc_sel, ptr + PCIR_AER_UC_STATUS, 4);
839 	mask = read_config(fd, &p->pc_sel, ptr + PCIR_AER_UC_SEVERITY, 4);
840 	printf(" %d fatal", bitcount32(sta & mask));
841 	printf(" %d non-fatal", bitcount32(sta & ~mask));
842 	sta = read_config(fd, &p->pc_sel, ptr + PCIR_AER_COR_STATUS, 4);
843 	printf(" %d corrected\n", bitcount32(sta));
844 }
845 
846 static void
847 ecap_vc(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
848 {
849 	uint32_t cap1;
850 
851 	printf("VC %d", ver);
852 	if (ver < 1)
853 		return;
854 	cap1 = read_config(fd, &p->pc_sel, ptr + PCIR_VC_CAP1, 4);
855 	printf(" max VC%d", cap1 & PCIM_VC_CAP1_EXT_COUNT);
856 	if ((cap1 & PCIM_VC_CAP1_LOWPRI_EXT_COUNT) != 0)
857 		printf(" lowpri VC0-VC%d",
858 		    (cap1 & PCIM_VC_CAP1_LOWPRI_EXT_COUNT) >> 4);
859 	printf("\n");
860 }
861 
862 static void
863 ecap_sernum(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
864 {
865 	uint32_t high, low;
866 
867 	printf("Serial %d", ver);
868 	if (ver < 1)
869 		return;
870 	low = read_config(fd, &p->pc_sel, ptr + PCIR_SERIAL_LOW, 4);
871 	high = read_config(fd, &p->pc_sel, ptr + PCIR_SERIAL_HIGH, 4);
872 	printf(" %08x%08x\n", high, low);
873 }
874 
875 static void
876 ecap_vendor(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
877 {
878 	uint32_t val;
879 
880 	printf("Vendor %d", ver);
881 	if (ver < 1)
882 		return;
883 	val = read_config(fd, &p->pc_sel, ptr + 4, 4);
884 	printf(" ID %d\n", val & 0xffff);
885 }
886 
887 static void
888 ecap_sec_pcie(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
889 {
890 	uint32_t val;
891 
892 	printf("PCIe Sec %d", ver);
893 	if (ver < 1)
894 		return;
895 	val = read_config(fd, &p->pc_sel, ptr + 8, 4);
896 	printf(" lane errors %#x\n", val);
897 }
898 
899 static const char *
900 check_enabled(int value)
901 {
902 
903 	return (value ? "enabled" : "disabled");
904 }
905 
906 static void
907 ecap_sriov(int fd, struct pci_conf *p, uint16_t ptr, uint8_t ver)
908 {
909 	const char *comma, *enabled;
910 	uint16_t iov_ctl, total_vfs, num_vfs, vf_offset, vf_stride, vf_did;
911 	uint32_t page_caps, page_size, page_shift, size;
912 	int i;
913 
914 	printf("SR-IOV %d ", ver);
915 
916 	iov_ctl = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_CTL, 2);
917 	printf("IOV %s, Memory Space %s, ARI %s\n",
918 	    check_enabled(iov_ctl & PCIM_SRIOV_VF_EN),
919 	    check_enabled(iov_ctl & PCIM_SRIOV_VF_MSE),
920 	    check_enabled(iov_ctl & PCIM_SRIOV_ARI_EN));
921 
922 	total_vfs = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_TOTAL_VFS, 2);
923 	num_vfs = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_NUM_VFS, 2);
924 	printf("                     ");
925 	printf("%d VFs configured out of %d supported\n", num_vfs, total_vfs);
926 
927 	vf_offset = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_VF_OFF, 2);
928 	vf_stride = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_VF_STRIDE, 2);
929 	printf("                     ");
930 	printf("First VF RID Offset 0x%04x, VF RID Stride 0x%04x\n", vf_offset,
931 	    vf_stride);
932 
933 	vf_did = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_VF_DID, 2);
934 	printf("                     VF Device ID 0x%04x\n", vf_did);
935 
936 	page_caps = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_PAGE_CAP, 4);
937 	page_size = read_config(fd, &p->pc_sel, ptr + PCIR_SRIOV_PAGE_SIZE, 4);
938 	printf("                     ");
939 	printf("Page Sizes: ");
940 	comma = "";
941 	while (page_caps != 0) {
942 		page_shift = ffs(page_caps) - 1;
943 
944 		if (page_caps & page_size)
945 			enabled = " (enabled)";
946 		else
947 			enabled = "";
948 
949 		size = (1 << (page_shift + PCI_SRIOV_BASE_PAGE_SHIFT));
950 		printf("%s%d%s", comma, size, enabled);
951 		comma = ", ";
952 
953 		page_caps &= ~(1 << page_shift);
954 	}
955 	printf("\n");
956 
957 	for (i = 0; i <= PCIR_MAX_BAR_0; i++)
958 		print_bar(fd, p, "iov bar  ", ptr + PCIR_SRIOV_BAR(i));
959 }
960 
961 static struct {
962 	uint16_t id;
963 	const char *name;
964 } ecap_names[] = {
965 	{ PCIZ_PWRBDGT, "Power Budgeting" },
966 	{ PCIZ_RCLINK_DCL, "Root Complex Link Declaration" },
967 	{ PCIZ_RCLINK_CTL, "Root Complex Internal Link Control" },
968 	{ PCIZ_RCEC_ASSOC, "Root Complex Event Collector ASsociation" },
969 	{ PCIZ_MFVC, "MFVC" },
970 	{ PCIZ_RCRB, "RCRB" },
971 	{ PCIZ_ACS, "ACS" },
972 	{ PCIZ_ARI, "ARI" },
973 	{ PCIZ_ATS, "ATS" },
974 	{ PCIZ_MULTICAST, "Multicast" },
975 	{ PCIZ_RESIZE_BAR, "Resizable BAR" },
976 	{ PCIZ_DPA, "DPA" },
977 	{ PCIZ_TPH_REQ, "TPH Requester" },
978 	{ PCIZ_LTR, "LTR" },
979 	{ 0, NULL }
980 };
981 
982 static void
983 list_ecaps(int fd, struct pci_conf *p)
984 {
985 	const char *name;
986 	uint32_t ecap;
987 	uint16_t ptr;
988 	int i;
989 
990 	ptr = PCIR_EXTCAP;
991 	ecap = read_config(fd, &p->pc_sel, ptr, 4);
992 	if (ecap == 0xffffffff || ecap == 0)
993 		return;
994 	for (;;) {
995 		printf("    ecap %04x[%03x] = ", PCI_EXTCAP_ID(ecap), ptr);
996 		switch (PCI_EXTCAP_ID(ecap)) {
997 		case PCIZ_AER:
998 			ecap_aer(fd, p, ptr, PCI_EXTCAP_VER(ecap));
999 			break;
1000 		case PCIZ_VC:
1001 			ecap_vc(fd, p, ptr, PCI_EXTCAP_VER(ecap));
1002 			break;
1003 		case PCIZ_SERNUM:
1004 			ecap_sernum(fd, p, ptr, PCI_EXTCAP_VER(ecap));
1005 			break;
1006 		case PCIZ_VENDOR:
1007 			ecap_vendor(fd, p, ptr, PCI_EXTCAP_VER(ecap));
1008 			break;
1009 		case PCIZ_SEC_PCIE:
1010 			ecap_sec_pcie(fd, p, ptr, PCI_EXTCAP_VER(ecap));
1011 			break;
1012 		case PCIZ_SRIOV:
1013 			ecap_sriov(fd, p, ptr, PCI_EXTCAP_VER(ecap));
1014 			break;
1015 		default:
1016 			name = "unknown";
1017 			for (i = 0; ecap_names[i].name != NULL; i++)
1018 				if (ecap_names[i].id == PCI_EXTCAP_ID(ecap)) {
1019 					name = ecap_names[i].name;
1020 					break;
1021 				}
1022 			printf("%s %d\n", name, PCI_EXTCAP_VER(ecap));
1023 			break;
1024 		}
1025 		ptr = PCI_EXTCAP_NEXTPTR(ecap);
1026 		if (ptr == 0)
1027 			break;
1028 		ecap = read_config(fd, &p->pc_sel, ptr, 4);
1029 	}
1030 }
1031 
1032 /* Find offset of a specific capability.  Returns 0 on failure. */
1033 uint8_t
1034 pci_find_cap(int fd, struct pci_conf *p, uint8_t id)
1035 {
1036 	uint16_t sta;
1037 	uint8_t ptr, cap;
1038 
1039 	/* Are capabilities present for this device? */
1040 	sta = read_config(fd, &p->pc_sel, PCIR_STATUS, 2);
1041 	if (!(sta & PCIM_STATUS_CAPPRESENT))
1042 		return (0);
1043 
1044 	switch (p->pc_hdr & PCIM_HDRTYPE) {
1045 	case PCIM_HDRTYPE_NORMAL:
1046 	case PCIM_HDRTYPE_BRIDGE:
1047 		ptr = PCIR_CAP_PTR;
1048 		break;
1049 	case PCIM_HDRTYPE_CARDBUS:
1050 		ptr = PCIR_CAP_PTR_2;
1051 		break;
1052 	default:
1053 		return (0);
1054 	}
1055 
1056 	ptr = read_config(fd, &p->pc_sel, ptr, 1);
1057 	while (ptr != 0 && ptr != 0xff) {
1058 		cap = read_config(fd, &p->pc_sel, ptr + PCICAP_ID, 1);
1059 		if (cap == id)
1060 			return (ptr);
1061 		ptr = read_config(fd, &p->pc_sel, ptr + PCICAP_NEXTPTR, 1);
1062 	}
1063 	return (0);
1064 }
1065 
1066 /* Find offset of a specific extended capability.  Returns 0 on failure. */
1067 uint16_t
1068 pcie_find_cap(int fd, struct pci_conf *p, uint16_t id)
1069 {
1070 	uint32_t ecap;
1071 	uint16_t ptr;
1072 
1073 	ptr = PCIR_EXTCAP;
1074 	ecap = read_config(fd, &p->pc_sel, ptr, 4);
1075 	if (ecap == 0xffffffff || ecap == 0)
1076 		return (0);
1077 	for (;;) {
1078 		if (PCI_EXTCAP_ID(ecap) == id)
1079 			return (ptr);
1080 		ptr = PCI_EXTCAP_NEXTPTR(ecap);
1081 		if (ptr == 0)
1082 			break;
1083 		ecap = read_config(fd, &p->pc_sel, ptr, 4);
1084 	}
1085 	return (0);
1086 }
1087