1 /*
2 * Copyright 1996 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/fcntl.h>
32 #include <sys/mman.h>
33 #include <sys/pciio.h>
34 #include <sys/queue.h>
35
36 #include <vm/vm.h>
37
38 #include <dev/pci/pcireg.h>
39
40 #include <assert.h>
41 #include <bitstring.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <inttypes.h>
45 #include <stdbool.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "pathnames.h"
52 #include "pciconf.h"
53
54 struct pci_device_info
55 {
56 TAILQ_ENTRY(pci_device_info) link;
57 int id;
58 char *desc;
59 };
60
61 struct pci_vendor_info
62 {
63 TAILQ_ENTRY(pci_vendor_info) link;
64 TAILQ_HEAD(,pci_device_info) devs;
65 int id;
66 char *desc;
67 };
68
69
70 struct pci_tree_entry {
71 TAILQ_ENTRY(pci_tree_entry) link;
72 TAILQ_HEAD(pci_tree_list, pci_tree_entry) children;
73 struct pci_conf *p;
74 };
75
76 static TAILQ_HEAD(,pci_vendor_info) pci_vendors;
77
78 static struct pcisel getsel(const char *str);
79 static void list_bridge(int fd, struct pci_conf *p);
80 static void list_bars(int fd, struct pci_conf *p);
81 static void list_devs(const char *name, int verbose, int bars, int bridge,
82 int caps, int errors, int vpd, int compact);
83 static void show_tree(int verbose);
84 static void list_verbose(struct pci_conf *p);
85 static void list_vpd(int fd, struct pci_conf *p);
86 static const char *guess_class(struct pci_conf *p);
87 static const char *guess_subclass(struct pci_conf *p);
88 static int load_vendors(void);
89 static void readit(const char *, const char *, int);
90 static void writeit(const char *, const char *, const char *, int);
91 static void chkattached(const char *);
92 static void dump_bar(const char *name, const char *reg, const char *bar_start,
93 const char *bar_count, int width, int verbose);
94
95 static int exitstatus = 0;
96
97 static void
usage(void)98 usage(void)
99 {
100
101 fprintf(stderr, "%s",
102 "usage: pciconf -l [-BbcevV] [device]\n"
103 " pciconf -t [-v]\n"
104 " pciconf -a device\n"
105 " pciconf -r [-b | -h] device addr[:addr2]\n"
106 " pciconf -w [-b | -h] device addr value\n"
107 " pciconf -D [-b | -h | -x] device bar [start [count]]"
108 "\n");
109 exit(1);
110 }
111
112 int
main(int argc,char ** argv)113 main(int argc, char **argv)
114 {
115 int c, width;
116 enum { NONE, LIST, TREE, READ, WRITE, ATTACHED, DUMPBAR } mode;
117 int compact, bars, bridge, caps, errors, verbose, vpd;
118
119 mode = NONE;
120 compact = bars = bridge = caps = errors = verbose = vpd = 0;
121 width = 4;
122
123 while ((c = getopt(argc, argv, "aBbcDehlrtwVvx")) != -1) {
124 switch(c) {
125 case 'a':
126 mode = ATTACHED;
127 break;
128
129 case 'B':
130 bridge = 1;
131 break;
132
133 case 'b':
134 bars = 1;
135 width = 1;
136 break;
137
138 case 'c':
139 caps++;
140 break;
141
142 case 'D':
143 mode = DUMPBAR;
144 break;
145
146 case 'e':
147 errors = 1;
148 break;
149
150 case 'h':
151 width = 2;
152 break;
153
154 case 'l':
155 if (mode == LIST)
156 compact = 1;
157 mode = LIST;
158 break;
159
160 case 'r':
161 mode = READ;
162 break;
163
164 case 't':
165 mode = TREE;
166 break;
167 case 'w':
168 mode = WRITE;
169 break;
170
171 case 'v':
172 verbose = 1;
173 break;
174
175 case 'V':
176 vpd = 1;
177 break;
178
179 case 'x':
180 width = 8;
181 break;
182
183 default:
184 usage();
185 }
186 }
187
188 switch (mode) {
189 case LIST:
190 if (optind >= argc + 1)
191 usage();
192 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose,
193 bars, bridge, caps, errors, vpd, compact);
194 break;
195 case TREE:
196 if (optind != argc)
197 usage();
198 show_tree(verbose);
199 break;
200 case ATTACHED:
201 if (optind + 1 != argc)
202 usage();
203 chkattached(argv[optind]);
204 break;
205 case READ:
206 if (optind + 2 != argc || width == 8)
207 usage();
208 readit(argv[optind], argv[optind + 1], width);
209 break;
210 case WRITE:
211 if (optind + 3 != argc || width == 8)
212 usage();
213 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
214 width);
215 break;
216 case DUMPBAR:
217 if (optind + 2 > argc || optind + 4 < argc)
218 usage();
219 dump_bar(argv[optind], argv[optind + 1],
220 optind + 2 < argc ? argv[optind + 2] : NULL,
221 optind + 3 < argc ? argv[optind + 3] : NULL,
222 width, verbose);
223 break;
224 default:
225 usage();
226 }
227
228 return (exitstatus);
229 }
230
231 static bool
fetch_devs(int fd,const char * name,struct pci_conf ** confp,size_t * countp)232 fetch_devs(int fd, const char *name, struct pci_conf **confp, size_t *countp)
233 {
234 struct pci_conf_io pc;
235 struct pci_conf conf[255], *p;
236 struct pci_match_conf patterns[1];
237 size_t count;
238
239 bzero(&pc, sizeof(struct pci_conf_io));
240 pc.match_buf_len = sizeof(conf);
241 pc.matches = conf;
242 if (name != NULL) {
243 bzero(&patterns, sizeof(patterns));
244 patterns[0].pc_sel = getsel(name);
245 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
246 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
247 PCI_GETCONF_MATCH_FUNC;
248 pc.num_patterns = 1;
249 pc.pat_buf_len = sizeof(patterns);
250 pc.patterns = patterns;
251 }
252
253 p = NULL;
254 count = 0;
255 do {
256 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
257 err(1, "ioctl(PCIOCGETCONF)");
258
259 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
260 free(p);
261 p = NULL;
262 count = 0;
263 pc.offset = 0;
264 continue;
265 }
266
267 if (pc.status == PCI_GETCONF_ERROR) {
268 warnx("error returned from PCIOCGETCONF ioctl");
269 return (false);
270 }
271
272 p = reallocf(p, (count + pc.num_matches) * sizeof(*p));
273 if (p == NULL) {
274 warnx("failed to allocate buffer for PCIOCGETCONF results");
275 return (false);
276 }
277
278 memcpy(p + count, conf, pc.num_matches * sizeof(*p));
279 count += pc.num_matches;
280 } while (pc.status == PCI_GETCONF_MORE_DEVS);
281
282 *confp = p;
283 *countp = count;
284 return (true);
285 }
286
287 static void
list_devs(const char * name,int verbose,int bars,int bridge,int caps,int errors,int vpd,int compact)288 list_devs(const char *name, int verbose, int bars, int bridge, int caps,
289 int errors, int vpd, int compact)
290 {
291 int fd;
292 struct pci_conf *conf, *p;
293 size_t count;
294 int none_count = 0;
295
296 if (verbose)
297 load_vendors();
298
299 fd = open(_PATH_DEVPCI, (bridge || caps || errors) ? O_RDWR : O_RDONLY,
300 0);
301 if (fd < 0)
302 err(1, "%s", _PATH_DEVPCI);
303
304 if (!fetch_devs(fd, name, &conf, &count)) {
305 exitstatus = 1;
306 close(fd);
307 return;
308 }
309
310 if (compact)
311 printf("drv\tselector\tclass rev hdr "
312 "vendor device subven subdev\n");
313 for (p = conf; p < conf + count; p++) {
314 if (compact)
315 printf("%s%d@pci%d:%d:%d:%d:"
316 "\t%06x %02x %02x "
317 "%04x %04x %04x %04x\n",
318 *p->pd_name ? p->pd_name : "none",
319 *p->pd_name ? (int)p->pd_unit :
320 none_count++, p->pc_sel.pc_domain,
321 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
322 p->pc_sel.pc_func, (p->pc_class << 16) |
323 (p->pc_subclass << 8) | p->pc_progif,
324 p->pc_revid, p->pc_hdr,
325 p->pc_vendor, p->pc_device,
326 p->pc_subvendor, p->pc_subdevice);
327 else
328 printf("%s%d@pci%d:%d:%d:%d:"
329 "\tclass=0x%06x rev=0x%02x hdr=0x%02x "
330 "vendor=0x%04x device=0x%04x "
331 "subvendor=0x%04x subdevice=0x%04x\n",
332 *p->pd_name ? p->pd_name : "none",
333 *p->pd_name ? (int)p->pd_unit :
334 none_count++, p->pc_sel.pc_domain,
335 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
336 p->pc_sel.pc_func, (p->pc_class << 16) |
337 (p->pc_subclass << 8) | p->pc_progif,
338 p->pc_revid, p->pc_hdr,
339 p->pc_vendor, p->pc_device,
340 p->pc_subvendor, p->pc_subdevice);
341 if (verbose)
342 list_verbose(p);
343 if (bars)
344 list_bars(fd, p);
345 if (bridge)
346 list_bridge(fd, p);
347 if (caps)
348 list_caps(fd, p, caps);
349 if (errors)
350 list_errors(fd, p);
351 if (vpd)
352 list_vpd(fd, p);
353 }
354
355 free(conf);
356 close(fd);
357 }
358
359 static int
pci_conf_compar(const void * lhs,const void * rhs)360 pci_conf_compar(const void *lhs, const void *rhs)
361 {
362 const struct pci_conf *l, *r;
363
364 l = lhs;
365 r = rhs;
366 if (l->pc_sel.pc_domain != r->pc_sel.pc_domain)
367 return (l->pc_sel.pc_domain - r->pc_sel.pc_domain);
368 if (l->pc_sel.pc_bus != r->pc_sel.pc_bus)
369 return (l->pc_sel.pc_bus - r->pc_sel.pc_bus);
370 if (l->pc_sel.pc_dev != r->pc_sel.pc_dev)
371 return (l->pc_sel.pc_dev - r->pc_sel.pc_dev);
372 return (l->pc_sel.pc_func - r->pc_sel.pc_func);
373 }
374
375 static void
tree_add_device(struct pci_tree_list * head,struct pci_conf * p,struct pci_conf * conf,size_t count,bitstr_t * added)376 tree_add_device(struct pci_tree_list *head, struct pci_conf *p,
377 struct pci_conf *conf, size_t count, bitstr_t *added)
378 {
379 struct pci_tree_entry *e;
380 struct pci_conf *child;
381 size_t i;
382
383 e = malloc(sizeof(*e));
384 TAILQ_INIT(&e->children);
385 e->p = p;
386 TAILQ_INSERT_TAIL(head, e, link);
387
388 switch (p->pc_hdr) {
389 case PCIM_HDRTYPE_BRIDGE:
390 case PCIM_HDRTYPE_CARDBUS:
391 break;
392 default:
393 return;
394 }
395 if (p->pc_secbus == 0)
396 return;
397
398 assert(p->pc_subbus >= p->pc_secbus);
399 for (i = 0; i < count; i++) {
400 child = conf + i;
401 if (child->pc_sel.pc_domain < p->pc_sel.pc_domain)
402 continue;
403 if (child->pc_sel.pc_domain > p->pc_sel.pc_domain)
404 break;
405 if (child->pc_sel.pc_bus < p->pc_secbus)
406 continue;
407 if (child->pc_sel.pc_bus > p->pc_subbus)
408 break;
409
410 if (child->pc_sel.pc_bus == p->pc_secbus) {
411 assert(bit_test(added, i) == 0);
412 bit_set(added, i);
413 tree_add_device(&e->children, conf + i, conf, count,
414 added);
415 continue;
416 }
417
418 if (bit_test(added, i) == 0) {
419 /*
420 * This really shouldn't happen, but if for
421 * some reason a child bridge doesn't claim
422 * this device, display it now rather than
423 * later.
424 */
425 bit_set(added, i);
426 tree_add_device(&e->children, conf + i, conf, count,
427 added);
428 continue;
429 }
430 }
431 }
432
433 static bool
build_tree(struct pci_tree_list * head,struct pci_conf * conf,size_t count)434 build_tree(struct pci_tree_list *head, struct pci_conf *conf, size_t count)
435 {
436 bitstr_t *added;
437 size_t i;
438
439 TAILQ_INIT(head);
440
441 /*
442 * Allocate a bitstring to track which devices have already
443 * been added to the tree.
444 */
445 added = bit_alloc(count);
446 if (added == NULL)
447 return (false);
448
449 for (i = 0; i < count; i++) {
450 if (bit_test(added, i))
451 continue;
452
453 bit_set(added, i);
454 tree_add_device(head, conf + i, conf, count, added);
455 }
456
457 free(added);
458 return (true);
459 }
460
461 static void
free_tree(struct pci_tree_list * head)462 free_tree(struct pci_tree_list *head)
463 {
464 struct pci_tree_entry *e, *n;
465
466 TAILQ_FOREACH_SAFE(e, head, link, n) {
467 free_tree(&e->children);
468 TAILQ_REMOVE(head, e, link);
469 free(e);
470 }
471 }
472
473 static void
print_tree_entry(struct pci_tree_entry * e,const char * indent,bool last,int verbose)474 print_tree_entry(struct pci_tree_entry *e, const char *indent, bool last,
475 int verbose)
476 {
477 struct pci_vendor_info *vi;
478 struct pci_device_info *di;
479 struct pci_tree_entry *child;
480 struct pci_conf *p = e->p;
481 char *indent_buf;
482
483 printf("%s%c--- ", indent, last ? '`' : '|');
484 if (p->pd_name[0] != '\0')
485 printf("%s%lu", p->pd_name, p->pd_unit);
486 else
487 printf("pci%d:%d:%d:%d", p->pc_sel.pc_domain, p->pc_sel.pc_bus,
488 p->pc_sel.pc_dev, p->pc_sel.pc_func);
489
490 if (verbose) {
491 di = NULL;
492 TAILQ_FOREACH(vi, &pci_vendors, link) {
493 if (vi->id == p->pc_vendor) {
494 printf(" %s", vi->desc);
495 TAILQ_FOREACH(di, &vi->devs, link) {
496 if (di->id == p->pc_device) {
497 printf(" %s", di->desc);
498 break;
499 }
500 }
501 break;
502 }
503 }
504 if (vi == NULL)
505 printf(" vendor=0x%04x device=0x%04x", p->pc_vendor,
506 p->pc_device);
507 else if (di == NULL)
508 printf(" device=0x%04x", p->pc_device);
509 }
510 printf("\n");
511
512 if (TAILQ_EMPTY(&e->children))
513 return;
514
515 asprintf(&indent_buf, "%s%c ", indent, last ? ' ' : '|');
516 TAILQ_FOREACH(child, &e->children, link) {
517 print_tree_entry(child, indent_buf, TAILQ_NEXT(child, link) ==
518 NULL, verbose);
519 }
520 free(indent_buf);
521 }
522
523 static void
show_tree(int verbose)524 show_tree(int verbose)
525 {
526 struct pci_tree_list head;
527 struct pci_tree_entry *e, *n;
528 struct pci_conf *conf;
529 size_t count;
530 int fd;
531 bool last, new_bus;
532
533 if (verbose)
534 load_vendors();
535
536 fd = open(_PATH_DEVPCI, O_RDONLY);
537 if (fd < 0)
538 err(1, "%s", _PATH_DEVPCI);
539
540 if (!fetch_devs(fd, NULL, &conf, &count)) {
541 exitstatus = 1;
542 goto close_fd;
543 }
544
545 if (count == 0)
546 goto close_fd;
547
548 if (conf[0].pc_reported_len < offsetof(struct pci_conf, pc_subbus)) {
549 warnx("kernel too old");
550 exitstatus = 1;
551 goto free_conf;
552 }
553
554 /* First, sort devices by DBSF. */
555 qsort(conf, count, sizeof(*conf), pci_conf_compar);
556
557 if (!build_tree(&head, conf, count)) {
558 warnx("failed to build tree of PCI devices");
559 exitstatus = 1;
560 goto free_conf;
561 }
562
563 new_bus = true;
564 TAILQ_FOREACH(e, &head, link) {
565 if (new_bus) {
566 printf("--- pci%d:%d\n", e->p->pc_sel.pc_domain,
567 e->p->pc_sel.pc_bus);
568 new_bus = false;
569 }
570
571 /* Is this the last entry for this bus? */
572 n = TAILQ_NEXT(e, link);
573 if (n == NULL ||
574 n->p->pc_sel.pc_domain != e->p->pc_sel.pc_domain ||
575 n->p->pc_sel.pc_bus != e->p->pc_sel.pc_bus) {
576 last = true;
577 new_bus = true;
578 } else
579 last = false;
580
581 print_tree_entry(e, " ", last, verbose);
582 }
583
584 free_tree(&head);
585 free_conf:
586 free(conf);
587 close_fd:
588 close(fd);
589 }
590
591 static void
print_bus_range(struct pci_conf * p)592 print_bus_range(struct pci_conf *p)
593 {
594 printf(" bus range = %u-%u\n", p->pc_secbus, p->pc_subbus);
595 }
596
597 static void
print_window(int reg,const char * type,int range,uint64_t base,uint64_t limit)598 print_window(int reg, const char *type, int range, uint64_t base,
599 uint64_t limit)
600 {
601
602 printf(" window[%02x] = type %s, range %2d, addr %#jx-%#jx, %s\n",
603 reg, type, range, (uintmax_t)base, (uintmax_t)limit,
604 base < limit ? "enabled" : "disabled");
605 }
606
607 static void
print_special_decode(bool isa,bool vga,bool subtractive)608 print_special_decode(bool isa, bool vga, bool subtractive)
609 {
610 bool comma;
611
612 if (isa || vga || subtractive) {
613 comma = false;
614 printf(" decode = ");
615 if (isa) {
616 printf("ISA");
617 comma = true;
618 }
619 if (vga) {
620 printf("%sVGA", comma ? ", " : "");
621 comma = true;
622 }
623 if (subtractive)
624 printf("%ssubtractive", comma ? ", " : "");
625 printf("\n");
626 }
627 }
628
629 static void
print_bridge_windows(int fd,struct pci_conf * p)630 print_bridge_windows(int fd, struct pci_conf *p)
631 {
632 uint64_t base, limit;
633 uint32_t val;
634 uint16_t bctl;
635 bool subtractive;
636 int range;
637
638 /*
639 * XXX: This assumes that a window with a base and limit of 0
640 * is not implemented. In theory a window might be programmed
641 * at the smallest size with a base of 0, but those do not seem
642 * common in practice.
643 */
644 val = read_config(fd, &p->pc_sel, PCIR_IOBASEL_1, 1);
645 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1) != 0) {
646 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
647 base = PCI_PPBIOBASE(
648 read_config(fd, &p->pc_sel, PCIR_IOBASEH_1, 2),
649 val);
650 limit = PCI_PPBIOLIMIT(
651 read_config(fd, &p->pc_sel, PCIR_IOLIMITH_1, 2),
652 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1));
653 range = 32;
654 } else {
655 base = PCI_PPBIOBASE(0, val);
656 limit = PCI_PPBIOLIMIT(0,
657 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1));
658 range = 16;
659 }
660 print_window(PCIR_IOBASEL_1, "I/O Port", range, base, limit);
661 }
662
663 base = PCI_PPBMEMBASE(0,
664 read_config(fd, &p->pc_sel, PCIR_MEMBASE_1, 2));
665 limit = PCI_PPBMEMLIMIT(0,
666 read_config(fd, &p->pc_sel, PCIR_MEMLIMIT_1, 2));
667 print_window(PCIR_MEMBASE_1, "Memory", 32, base, limit);
668
669 val = read_config(fd, &p->pc_sel, PCIR_PMBASEL_1, 2);
670 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2) != 0) {
671 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
672 base = PCI_PPBMEMBASE(
673 read_config(fd, &p->pc_sel, PCIR_PMBASEH_1, 4),
674 val);
675 limit = PCI_PPBMEMLIMIT(
676 read_config(fd, &p->pc_sel, PCIR_PMLIMITH_1, 4),
677 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2));
678 range = 64;
679 } else {
680 base = PCI_PPBMEMBASE(0, val);
681 limit = PCI_PPBMEMLIMIT(0,
682 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2));
683 range = 32;
684 }
685 print_window(PCIR_PMBASEL_1, "Prefetchable Memory", range, base,
686 limit);
687 }
688
689 /*
690 * XXX: This list of bridges that are subtractive but do not set
691 * progif to indicate it is copied from pci_pci.c.
692 */
693 subtractive = p->pc_progif == PCIP_BRIDGE_PCI_SUBTRACTIVE;
694 switch (p->pc_device << 16 | p->pc_vendor) {
695 case 0xa002177d: /* Cavium ThunderX */
696 case 0x124b8086: /* Intel 82380FB Mobile */
697 case 0x060513d7: /* Toshiba ???? */
698 subtractive = true;
699 }
700 if (p->pc_vendor == 0x8086 && (p->pc_device & 0xff00) == 0x2400)
701 subtractive = true;
702
703 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_1, 2);
704 print_special_decode(bctl & PCIB_BCR_ISA_ENABLE,
705 bctl & PCIB_BCR_VGA_ENABLE, subtractive);
706 }
707
708 static void
print_cardbus_mem_window(int fd,struct pci_conf * p,int basereg,int limitreg,bool prefetch)709 print_cardbus_mem_window(int fd, struct pci_conf *p, int basereg, int limitreg,
710 bool prefetch)
711 {
712
713 print_window(basereg, prefetch ? "Prefetchable Memory" : "Memory", 32,
714 PCI_CBBMEMBASE(read_config(fd, &p->pc_sel, basereg, 4)),
715 PCI_CBBMEMLIMIT(read_config(fd, &p->pc_sel, limitreg, 4)));
716 }
717
718 static void
print_cardbus_io_window(int fd,struct pci_conf * p,int basereg,int limitreg)719 print_cardbus_io_window(int fd, struct pci_conf *p, int basereg, int limitreg)
720 {
721 uint32_t base, limit;
722 uint32_t val;
723 int range;
724
725 val = read_config(fd, &p->pc_sel, basereg, 2);
726 if ((val & PCIM_CBBIO_MASK) == PCIM_CBBIO_32) {
727 base = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, basereg, 4));
728 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 4));
729 range = 32;
730 } else {
731 base = PCI_CBBIOBASE(val);
732 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 2));
733 range = 16;
734 }
735 print_window(basereg, "I/O Port", range, base, limit);
736 }
737
738 static void
print_cardbus_windows(int fd,struct pci_conf * p)739 print_cardbus_windows(int fd, struct pci_conf *p)
740 {
741 uint16_t bctl;
742
743 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_2, 2);
744 print_cardbus_mem_window(fd, p, PCIR_MEMBASE0_2, PCIR_MEMLIMIT0_2,
745 bctl & CBB_BCR_PREFETCH_0_ENABLE);
746 print_cardbus_mem_window(fd, p, PCIR_MEMBASE1_2, PCIR_MEMLIMIT1_2,
747 bctl & CBB_BCR_PREFETCH_1_ENABLE);
748 print_cardbus_io_window(fd, p, PCIR_IOBASE0_2, PCIR_IOLIMIT0_2);
749 print_cardbus_io_window(fd, p, PCIR_IOBASE1_2, PCIR_IOLIMIT1_2);
750 print_special_decode(bctl & CBB_BCR_ISA_ENABLE,
751 bctl & CBB_BCR_VGA_ENABLE, false);
752 }
753
754 static void
list_bridge(int fd,struct pci_conf * p)755 list_bridge(int fd, struct pci_conf *p)
756 {
757
758 switch (p->pc_hdr & PCIM_HDRTYPE) {
759 case PCIM_HDRTYPE_BRIDGE:
760 print_bus_range(p);
761 print_bridge_windows(fd, p);
762 break;
763 case PCIM_HDRTYPE_CARDBUS:
764 print_bus_range(p);
765 print_cardbus_windows(fd, p);
766 break;
767 }
768 }
769
770 static void
list_bars(int fd,struct pci_conf * p)771 list_bars(int fd, struct pci_conf *p)
772 {
773 int i, max;
774
775 switch (p->pc_hdr & PCIM_HDRTYPE) {
776 case PCIM_HDRTYPE_NORMAL:
777 max = PCIR_MAX_BAR_0;
778 break;
779 case PCIM_HDRTYPE_BRIDGE:
780 max = PCIR_MAX_BAR_1;
781 break;
782 case PCIM_HDRTYPE_CARDBUS:
783 max = PCIR_MAX_BAR_2;
784 break;
785 default:
786 return;
787 }
788
789 for (i = 0; i <= max; i++)
790 print_bar(fd, p, "bar ", PCIR_BAR(i));
791 }
792
793 void
print_bar(int fd,struct pci_conf * p,const char * label,uint16_t bar_offset)794 print_bar(int fd, struct pci_conf *p, const char *label, uint16_t bar_offset)
795 {
796 uint64_t base;
797 const char *type;
798 struct pci_bar_io bar;
799 int range;
800
801 bar.pbi_sel = p->pc_sel;
802 bar.pbi_reg = bar_offset;
803 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
804 return;
805 if (PCI_BAR_IO(bar.pbi_base)) {
806 type = "I/O Port";
807 range = 32;
808 base = bar.pbi_base & PCIM_BAR_IO_BASE;
809 } else {
810 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
811 type = "Prefetchable Memory";
812 else
813 type = "Memory";
814 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
815 case PCIM_BAR_MEM_32:
816 range = 32;
817 break;
818 case PCIM_BAR_MEM_1MB:
819 range = 20;
820 break;
821 case PCIM_BAR_MEM_64:
822 range = 64;
823 break;
824 default:
825 range = -1;
826 }
827 base = bar.pbi_base & ~((uint64_t)0xf);
828 }
829 printf(" %s[%02x] = type %s, range %2d, base %#jx, ",
830 label, bar_offset, type, range, (uintmax_t)base);
831 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length,
832 bar.pbi_enabled ? "enabled" : "disabled");
833 }
834
835 static void
list_verbose(struct pci_conf * p)836 list_verbose(struct pci_conf *p)
837 {
838 struct pci_vendor_info *vi;
839 struct pci_device_info *di;
840 const char *dp;
841
842 TAILQ_FOREACH(vi, &pci_vendors, link) {
843 if (vi->id == p->pc_vendor) {
844 printf(" vendor = '%s'\n", vi->desc);
845 break;
846 }
847 }
848 if (vi == NULL) {
849 di = NULL;
850 } else {
851 TAILQ_FOREACH(di, &vi->devs, link) {
852 if (di->id == p->pc_device) {
853 printf(" device = '%s'\n", di->desc);
854 break;
855 }
856 }
857 }
858 if ((dp = guess_class(p)) != NULL)
859 printf(" class = %s\n", dp);
860 if ((dp = guess_subclass(p)) != NULL)
861 printf(" subclass = %s\n", dp);
862 }
863
864 static void
list_vpd(int fd,struct pci_conf * p)865 list_vpd(int fd, struct pci_conf *p)
866 {
867 struct pci_list_vpd_io list;
868 struct pci_vpd_element *vpd, *end;
869
870 list.plvi_sel = p->pc_sel;
871 list.plvi_len = 0;
872 list.plvi_data = NULL;
873 if (ioctl(fd, PCIOCLISTVPD, &list) < 0 || list.plvi_len == 0)
874 return;
875
876 list.plvi_data = malloc(list.plvi_len);
877 if (ioctl(fd, PCIOCLISTVPD, &list) < 0) {
878 free(list.plvi_data);
879 return;
880 }
881
882 vpd = list.plvi_data;
883 end = (struct pci_vpd_element *)((char *)vpd + list.plvi_len);
884 for (; vpd < end; vpd = PVE_NEXT(vpd)) {
885 if (vpd->pve_flags == PVE_FLAG_IDENT) {
886 printf(" VPD ident = '%.*s'\n",
887 (int)vpd->pve_datalen, vpd->pve_data);
888 continue;
889 }
890
891 /* Ignore the checksum keyword. */
892 if (!(vpd->pve_flags & PVE_FLAG_RW) &&
893 memcmp(vpd->pve_keyword, "RV", 2) == 0)
894 continue;
895
896 /* Ignore remaining read-write space. */
897 if (vpd->pve_flags & PVE_FLAG_RW &&
898 memcmp(vpd->pve_keyword, "RW", 2) == 0)
899 continue;
900
901 /* Handle extended capability keyword. */
902 if (!(vpd->pve_flags & PVE_FLAG_RW) &&
903 memcmp(vpd->pve_keyword, "CP", 2) == 0) {
904 printf(" VPD ro CP = ID %02x in map 0x%x[0x%x]\n",
905 (unsigned int)vpd->pve_data[0],
906 PCIR_BAR((unsigned int)vpd->pve_data[1]),
907 (unsigned int)vpd->pve_data[3] << 8 |
908 (unsigned int)vpd->pve_data[2]);
909 continue;
910 }
911
912 /* Remaining keywords should all have ASCII values. */
913 printf(" VPD %s %c%c = '%.*s'\n",
914 vpd->pve_flags & PVE_FLAG_RW ? "rw" : "ro",
915 vpd->pve_keyword[0], vpd->pve_keyword[1],
916 (int)vpd->pve_datalen, vpd->pve_data);
917 }
918 free(list.plvi_data);
919 }
920
921 /*
922 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
923 */
924 static struct
925 {
926 int class;
927 int subclass;
928 const char *desc;
929 } pci_nomatch_tab[] = {
930 {PCIC_OLD, -1, "old"},
931 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
932 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
933 {PCIC_STORAGE, -1, "mass storage"},
934 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
935 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
936 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
937 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
938 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
939 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
940 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
941 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
942 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
943 {PCIC_STORAGE, PCIS_STORAGE_UFS, "UFS"},
944 {PCIC_NETWORK, -1, "network"},
945 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
946 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
947 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
948 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
949 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
950 {PCIC_NETWORK, PCIS_NETWORK_WORLDFIP, "WorldFip"},
951 {PCIC_NETWORK, PCIS_NETWORK_PICMG, "PICMG"},
952 {PCIC_NETWORK, PCIS_NETWORK_INFINIBAND, "InfiniBand"},
953 {PCIC_NETWORK, PCIS_NETWORK_HFC, "host fabric"},
954 {PCIC_DISPLAY, -1, "display"},
955 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
956 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
957 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
958 {PCIC_MULTIMEDIA, -1, "multimedia"},
959 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
960 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
961 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
962 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
963 {PCIC_MEMORY, -1, "memory"},
964 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
965 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
966 {PCIC_BRIDGE, -1, "bridge"},
967 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
968 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
969 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
970 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
971 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
972 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
973 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
974 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
975 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
976 {PCIC_BRIDGE, PCIS_BRIDGE_PCI_TRANSPARENT,
977 "Semi-transparent PCI-to-PCI"},
978 {PCIC_BRIDGE, PCIS_BRIDGE_INFINIBAND, "InfiniBand-PCI"},
979 {PCIC_BRIDGE, PCIS_BRIDGE_AS_PCI,
980 "AdvancedSwitching-PCI"},
981 {PCIC_SIMPLECOMM, -1, "simple comms"},
982 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
983 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
984 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
985 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
986 {PCIC_BASEPERIPH, -1, "base peripheral"},
987 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
988 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
989 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
990 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
991 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
992 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
993 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_IOMMU, "IOMMU"},
994 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RCEC,
995 "Root Complex Event Collector"},
996 {PCIC_INPUTDEV, -1, "input device"},
997 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
998 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
999 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
1000 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
1001 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
1002 {PCIC_DOCKING, -1, "docking station"},
1003 {PCIC_PROCESSOR, -1, "processor"},
1004 {PCIC_SERIALBUS, -1, "serial bus"},
1005 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
1006 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
1007 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
1008 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
1009 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
1010 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
1011 {PCIC_SERIALBUS, PCIS_SERIALBUS_INFINIBAND, "InfiniBand"},
1012 {PCIC_SERIALBUS, PCIS_SERIALBUS_IPMI, "IPMI"},
1013 {PCIC_SERIALBUS, PCIS_SERIALBUS_SERCOS, "SERCOS"},
1014 {PCIC_SERIALBUS, PCIS_SERIALBUS_CANBUS, "CANbus"},
1015 {PCIC_SERIALBUS, PCIS_SERIALBUS_MIPI_I3C, "MIPI I3C"},
1016 {PCIC_WIRELESS, -1, "wireless controller"},
1017 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
1018 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
1019 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
1020 {PCIC_WIRELESS, PCIS_WIRELESS_BLUETOOTH, "bluetooth"},
1021 {PCIC_WIRELESS, PCIS_WIRELESS_BROADBAND, "broadband"},
1022 {PCIC_WIRELESS, PCIS_WIRELESS_80211A, "ethernet 802.11a"},
1023 {PCIC_WIRELESS, PCIS_WIRELESS_80211B, "ethernet 802.11b"},
1024 {PCIC_WIRELESS, PCIS_WIRELESS_CELL,
1025 "cellular controller/modem"},
1026 {PCIC_WIRELESS, PCIS_WIRELESS_CELL_E,
1027 "cellular controller/modem plus ethernet"},
1028 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
1029 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
1030 {PCIC_SATCOM, -1, "satellite communication"},
1031 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
1032 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
1033 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
1034 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
1035 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
1036 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
1037 {PCIC_CRYPTO, PCIS_CRYPTO_ENTERTAIN, "entertainment crypto"},
1038 {PCIC_DASP, -1, "dasp"},
1039 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
1040 {PCIC_DASP, PCIS_DASP_PERFCNTRS, "performance counters"},
1041 {PCIC_DASP, PCIS_DASP_COMM_SYNC, "communication synchronizer"},
1042 {PCIC_DASP, PCIS_DASP_MGMT_CARD, "signal processing management"},
1043 {PCIC_ACCEL, -1, "processing accelerators"},
1044 {PCIC_ACCEL, PCIS_ACCEL_PROCESSING, "processing accelerators"},
1045 {PCIC_INSTRUMENT, -1, "non-essential instrumentation"},
1046 {0, 0, NULL}
1047 };
1048
1049 static const char *
guess_class(struct pci_conf * p)1050 guess_class(struct pci_conf *p)
1051 {
1052 int i;
1053
1054 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
1055 if (pci_nomatch_tab[i].class == p->pc_class)
1056 return(pci_nomatch_tab[i].desc);
1057 }
1058 return(NULL);
1059 }
1060
1061 static const char *
guess_subclass(struct pci_conf * p)1062 guess_subclass(struct pci_conf *p)
1063 {
1064 int i;
1065
1066 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
1067 if ((pci_nomatch_tab[i].class == p->pc_class) &&
1068 (pci_nomatch_tab[i].subclass == p->pc_subclass))
1069 return(pci_nomatch_tab[i].desc);
1070 }
1071 return(NULL);
1072 }
1073
1074 static int
load_vendors(void)1075 load_vendors(void)
1076 {
1077 const char *dbf;
1078 FILE *db;
1079 struct pci_vendor_info *cv;
1080 struct pci_device_info *cd;
1081 char buf[1024], str[1024];
1082 char *ch;
1083 int id, error;
1084
1085 /*
1086 * Locate the database and initialise.
1087 */
1088 TAILQ_INIT(&pci_vendors);
1089 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
1090 dbf = _PATH_LPCIVDB;
1091 if ((db = fopen(dbf, "r")) == NULL) {
1092 dbf = _PATH_PCIVDB;
1093 if ((db = fopen(dbf, "r")) == NULL)
1094 return(1);
1095 }
1096 cv = NULL;
1097 cd = NULL;
1098 error = 0;
1099
1100 /*
1101 * Scan input lines from the database
1102 */
1103 for (;;) {
1104 if (fgets(buf, sizeof(buf), db) == NULL)
1105 break;
1106
1107 if ((ch = strchr(buf, '#')) != NULL)
1108 *ch = '\0';
1109 ch = strchr(buf, '\0') - 1;
1110 while (ch > buf && isspace(*ch))
1111 *ch-- = '\0';
1112 if (ch <= buf)
1113 continue;
1114
1115 /* Can't handle subvendor / subdevice entries yet */
1116 if (buf[0] == '\t' && buf[1] == '\t')
1117 continue;
1118
1119 /* Check for vendor entry */
1120 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
1121 if ((id == 0) || (strlen(str) < 1))
1122 continue;
1123 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
1124 warn("allocating vendor entry");
1125 error = 1;
1126 break;
1127 }
1128 if ((cv->desc = strdup(str)) == NULL) {
1129 free(cv);
1130 warn("allocating vendor description");
1131 error = 1;
1132 break;
1133 }
1134 cv->id = id;
1135 TAILQ_INIT(&cv->devs);
1136 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
1137 continue;
1138 }
1139
1140 /* Check for device entry */
1141 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
1142 if ((id == 0) || (strlen(str) < 1))
1143 continue;
1144 if (cv == NULL) {
1145 warnx("device entry with no vendor!");
1146 continue;
1147 }
1148 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
1149 warn("allocating device entry");
1150 error = 1;
1151 break;
1152 }
1153 if ((cd->desc = strdup(str)) == NULL) {
1154 free(cd);
1155 warn("allocating device description");
1156 error = 1;
1157 break;
1158 }
1159 cd->id = id;
1160 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
1161 continue;
1162 }
1163
1164 /* It's a comment or junk, ignore it */
1165 }
1166 if (ferror(db))
1167 error = 1;
1168 fclose(db);
1169
1170 return(error);
1171 }
1172
1173 uint32_t
read_config(int fd,struct pcisel * sel,long reg,int width)1174 read_config(int fd, struct pcisel *sel, long reg, int width)
1175 {
1176 struct pci_io pi;
1177
1178 pi.pi_sel = *sel;
1179 pi.pi_reg = reg;
1180 pi.pi_width = width;
1181
1182 if (ioctl(fd, PCIOCREAD, &pi) < 0)
1183 err(1, "ioctl(PCIOCREAD)");
1184
1185 return (pi.pi_data);
1186 }
1187
1188 static struct pcisel
getdevice(const char * name)1189 getdevice(const char *name)
1190 {
1191 struct pci_conf_io pc;
1192 struct pci_conf conf[1];
1193 struct pci_match_conf patterns[1];
1194 char *cp;
1195 int fd;
1196
1197 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
1198 if (fd < 0)
1199 err(1, "%s", _PATH_DEVPCI);
1200
1201 bzero(&pc, sizeof(struct pci_conf_io));
1202 pc.match_buf_len = sizeof(conf);
1203 pc.matches = conf;
1204
1205 bzero(&patterns, sizeof(patterns));
1206
1207 /*
1208 * The pattern structure requires the unit to be split out from
1209 * the driver name. Walk backwards from the end of the name to
1210 * find the start of the unit.
1211 */
1212 if (name[0] == '\0')
1213 errx(1, "Empty device name");
1214 cp = strchr(name, '\0');
1215 assert(cp != NULL && cp != name);
1216 cp--;
1217 while (cp != name && isdigit(cp[-1]))
1218 cp--;
1219 if (cp == name || !isdigit(*cp))
1220 errx(1, "Invalid device name");
1221 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name))
1222 errx(1, "Device name is too long");
1223 memcpy(patterns[0].pd_name, name, cp - name);
1224 patterns[0].pd_unit = strtol(cp, &cp, 10);
1225 if (*cp != '\0')
1226 errx(1, "Invalid device name");
1227 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
1228 pc.num_patterns = 1;
1229 pc.pat_buf_len = sizeof(patterns);
1230 pc.patterns = patterns;
1231
1232 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
1233 err(1, "ioctl(PCIOCGETCONF)");
1234 if (pc.status != PCI_GETCONF_LAST_DEVICE &&
1235 pc.status != PCI_GETCONF_MORE_DEVS)
1236 errx(1, "error returned from PCIOCGETCONF ioctl");
1237 close(fd);
1238 if (pc.num_matches == 0)
1239 errx(1, "Device not found");
1240 return (conf[0].pc_sel);
1241 }
1242
1243 static struct pcisel
parsesel(const char * str)1244 parsesel(const char *str)
1245 {
1246 const char *ep;
1247 char *eppos;
1248 struct pcisel sel;
1249 unsigned long selarr[4];
1250 int i;
1251
1252 ep = strchr(str, '@');
1253 if (ep != NULL)
1254 ep++;
1255 else
1256 ep = str;
1257
1258 if (strncmp(ep, "pci", 3) == 0) {
1259 ep += 3;
1260 i = 0;
1261 while (isdigit(*ep) && i < 4) {
1262 selarr[i++] = strtoul(ep, &eppos, 10);
1263 ep = eppos;
1264 if (*ep == ':')
1265 ep++;
1266 }
1267 if (i > 0 && *ep == '\0') {
1268 sel.pc_func = (i > 2) ? selarr[--i] : 0;
1269 sel.pc_dev = (i > 0) ? selarr[--i] : 0;
1270 sel.pc_bus = (i > 0) ? selarr[--i] : 0;
1271 sel.pc_domain = (i > 0) ? selarr[--i] : 0;
1272 return (sel);
1273 }
1274 }
1275 errx(1, "cannot parse selector %s", str);
1276 }
1277
1278 static struct pcisel
getsel(const char * str)1279 getsel(const char *str)
1280 {
1281
1282 /*
1283 * No device names contain colons and selectors always contain
1284 * at least one colon.
1285 */
1286 if (strchr(str, ':') == NULL)
1287 return (getdevice(str));
1288 else
1289 return (parsesel(str));
1290 }
1291
1292 static void
readone(int fd,struct pcisel * sel,long reg,int width)1293 readone(int fd, struct pcisel *sel, long reg, int width)
1294 {
1295
1296 printf("%0*x", width*2, read_config(fd, sel, reg, width));
1297 }
1298
1299 static void
readit(const char * name,const char * reg,int width)1300 readit(const char *name, const char *reg, int width)
1301 {
1302 long rstart;
1303 long rend;
1304 long r;
1305 char *end;
1306 int i;
1307 int fd;
1308 struct pcisel sel;
1309
1310 fd = open(_PATH_DEVPCI, O_RDWR, 0);
1311 if (fd < 0)
1312 err(1, "%s", _PATH_DEVPCI);
1313
1314 rend = rstart = strtol(reg, &end, 0);
1315 if (end && *end == ':') {
1316 end++;
1317 rend = strtol(end, (char **) 0, 0);
1318 }
1319 sel = getsel(name);
1320 for (i = 1, r = rstart; r <= rend; i++, r += width) {
1321 readone(fd, &sel, r, width);
1322 if (i && !(i % 8))
1323 putchar(' ');
1324 putchar(i % (16/width) ? ' ' : '\n');
1325 }
1326 if (i % (16/width) != 1)
1327 putchar('\n');
1328 close(fd);
1329 }
1330
1331 static void
writeit(const char * name,const char * reg,const char * data,int width)1332 writeit(const char *name, const char *reg, const char *data, int width)
1333 {
1334 int fd;
1335 struct pci_io pi;
1336
1337 pi.pi_sel = getsel(name);
1338 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
1339 pi.pi_width = width;
1340 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
1341
1342 fd = open(_PATH_DEVPCI, O_RDWR, 0);
1343 if (fd < 0)
1344 err(1, "%s", _PATH_DEVPCI);
1345
1346 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
1347 err(1, "ioctl(PCIOCWRITE)");
1348 close(fd);
1349 }
1350
1351 static void
chkattached(const char * name)1352 chkattached(const char *name)
1353 {
1354 int fd;
1355 struct pci_io pi;
1356
1357 pi.pi_sel = getsel(name);
1358
1359 fd = open(_PATH_DEVPCI, O_RDWR, 0);
1360 if (fd < 0)
1361 err(1, "%s", _PATH_DEVPCI);
1362
1363 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
1364 err(1, "ioctl(PCIOCATTACHED)");
1365
1366 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
1367 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
1368 close(fd);
1369 }
1370
1371 static void
dump_bar(const char * name,const char * reg,const char * bar_start,const char * bar_count,int width,int verbose)1372 dump_bar(const char *name, const char *reg, const char *bar_start,
1373 const char *bar_count, int width, int verbose)
1374 {
1375 struct pci_bar_mmap pbm;
1376 uint32_t *dd;
1377 uint16_t *dh;
1378 uint8_t *db;
1379 uint64_t *dx, a, start, count;
1380 char *el;
1381 size_t res;
1382 int fd;
1383
1384 start = 0;
1385 if (bar_start != NULL) {
1386 start = strtoul(bar_start, &el, 0);
1387 if (*el != '\0')
1388 errx(1, "Invalid bar start specification %s",
1389 bar_start);
1390 }
1391 count = 0;
1392 if (bar_count != NULL) {
1393 count = strtoul(bar_count, &el, 0);
1394 if (*el != '\0')
1395 errx(1, "Invalid count specification %s",
1396 bar_count);
1397 }
1398
1399 pbm.pbm_sel = getsel(name);
1400 pbm.pbm_reg = strtoul(reg, &el, 0);
1401 if (*reg == '\0' || *el != '\0')
1402 errx(1, "Invalid bar specification %s", reg);
1403 pbm.pbm_flags = 0;
1404 pbm.pbm_memattr = VM_MEMATTR_DEVICE;
1405
1406 fd = open(_PATH_DEVPCI, O_RDWR, 0);
1407 if (fd < 0)
1408 err(1, "%s", _PATH_DEVPCI);
1409
1410 if (ioctl(fd, PCIOCBARMMAP, &pbm) < 0)
1411 err(1, "ioctl(PCIOCBARMMAP)");
1412
1413 if (count == 0)
1414 count = pbm.pbm_bar_length / width;
1415 if (start + count < start || (start + count) * width < (uint64_t)width)
1416 errx(1, "(start + count) x width overflow");
1417 if ((start + count) * width > pbm.pbm_bar_length) {
1418 if (start * width > pbm.pbm_bar_length)
1419 count = 0;
1420 else
1421 count = (pbm.pbm_bar_length - start * width) / width;
1422 }
1423 if (verbose) {
1424 fprintf(stderr,
1425 "Dumping pci%d:%d:%d:%d BAR %x mapped base %p "
1426 "off %#x length %#jx from %#jx count %#jx in %d-bytes\n",
1427 pbm.pbm_sel.pc_domain, pbm.pbm_sel.pc_bus,
1428 pbm.pbm_sel.pc_dev, pbm.pbm_sel.pc_func,
1429 pbm.pbm_reg, pbm.pbm_map_base, pbm.pbm_bar_off,
1430 pbm.pbm_bar_length, start, count, width);
1431 }
1432 switch (width) {
1433 case 1:
1434 db = (uint8_t *)(uintptr_t)((uintptr_t)pbm.pbm_map_base +
1435 pbm.pbm_bar_off + start * width);
1436 for (a = 0; a < count; a++, db++) {
1437 res = fwrite(db, width, 1, stdout);
1438 if (res != 1) {
1439 errx(1, "error writing to stdout");
1440 break;
1441 }
1442 }
1443 break;
1444 case 2:
1445 dh = (uint16_t *)(uintptr_t)((uintptr_t)pbm.pbm_map_base +
1446 pbm.pbm_bar_off + start * width);
1447 for (a = 0; a < count; a++, dh++) {
1448 res = fwrite(dh, width, 1, stdout);
1449 if (res != 1) {
1450 errx(1, "error writing to stdout");
1451 break;
1452 }
1453 }
1454 break;
1455 case 4:
1456 dd = (uint32_t *)(uintptr_t)((uintptr_t)pbm.pbm_map_base +
1457 pbm.pbm_bar_off + start * width);
1458 for (a = 0; a < count; a ++, dd++) {
1459 res = fwrite(dd, width, 1, stdout);
1460 if (res != 1) {
1461 errx(1, "error writing to stdout");
1462 break;
1463 }
1464 }
1465 break;
1466 case 8:
1467 dx = (uint64_t *)(uintptr_t)((uintptr_t)pbm.pbm_map_base +
1468 pbm.pbm_bar_off + start * width);
1469 for (a = 0; a < count; a++, dx++) {
1470 res = fwrite(dx, width, 1, stdout);
1471 if (res != 1) {
1472 errx(1, "error writing to stdout");
1473 break;
1474 }
1475 }
1476 break;
1477 default:
1478 errx(1, "invalid access width");
1479 }
1480
1481 munmap((void *)pbm.pbm_map_base, pbm.pbm_map_length);
1482 close(fd);
1483 }
1484