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