Deleted Added
full compact
pciconf.c (256281) pciconf.c (261250)
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#ifndef lint
31static const char rcsid[] =
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#ifndef lint
31static const char rcsid[] =
32 "$FreeBSD: stable/10/usr.sbin/pciconf/pciconf.c 246632 2013-02-10 19:35:40Z neel $";
32 "$FreeBSD: stable/10/usr.sbin/pciconf/pciconf.c 261250 2014-01-28 21:56:18Z jhb $";
33#endif /* not lint */
34
35#include <sys/types.h>
36#include <sys/fcntl.h>
37
33#endif /* not lint */
34
35#include <sys/types.h>
36#include <sys/fcntl.h>
37
38#include <assert.h>
38#include <ctype.h>
39#include <err.h>
40#include <inttypes.h>
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44#include <unistd.h>
45#include <sys/pciio.h>
46#include <sys/queue.h>
47
48#include <dev/pci/pcireg.h>
49
50#include "pathnames.h"
51#include "pciconf.h"
52
53struct pci_device_info
54{
55 TAILQ_ENTRY(pci_device_info) link;
56 int id;
57 char *desc;
58};
59
60struct pci_vendor_info
61{
62 TAILQ_ENTRY(pci_vendor_info) link;
63 TAILQ_HEAD(,pci_device_info) devs;
64 int id;
65 char *desc;
66};
67
68TAILQ_HEAD(,pci_vendor_info) pci_vendors;
69
39#include <ctype.h>
40#include <err.h>
41#include <inttypes.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <string.h>
45#include <unistd.h>
46#include <sys/pciio.h>
47#include <sys/queue.h>
48
49#include <dev/pci/pcireg.h>
50
51#include "pathnames.h"
52#include "pciconf.h"
53
54struct pci_device_info
55{
56 TAILQ_ENTRY(pci_device_info) link;
57 int id;
58 char *desc;
59};
60
61struct 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
69TAILQ_HEAD(,pci_vendor_info) pci_vendors;
70
71static struct pcisel getsel(const char *str);
70static void list_bars(int fd, struct pci_conf *p);
72static void list_bars(int fd, struct pci_conf *p);
71static void list_devs(int verbose, int bars, int caps, int errors);
73static void list_devs(const char *name, int verbose, int bars, int caps,
74 int errors);
72static void list_verbose(struct pci_conf *p);
73static const char *guess_class(struct pci_conf *p);
74static const char *guess_subclass(struct pci_conf *p);
75static int load_vendors(void);
76static void readit(const char *, const char *, int);
77static void writeit(const char *, const char *, const char *, int);
78static void chkattached(const char *);
79
80static int exitstatus = 0;
81
82static void
83usage(void)
84{
85 fprintf(stderr, "%s\n%s\n%s\n%s\n",
75static void list_verbose(struct pci_conf *p);
76static const char *guess_class(struct pci_conf *p);
77static const char *guess_subclass(struct pci_conf *p);
78static int load_vendors(void);
79static void readit(const char *, const char *, int);
80static void writeit(const char *, const char *, const char *, int);
81static void chkattached(const char *);
82
83static int exitstatus = 0;
84
85static void
86usage(void)
87{
88 fprintf(stderr, "%s\n%s\n%s\n%s\n",
86 "usage: pciconf -l [-bcev]",
87 " pciconf -a selector",
88 " pciconf -r [-b | -h] selector addr[:addr2]",
89 " pciconf -w [-b | -h] selector addr value");
89 "usage: pciconf -l [-bcev] [device]",
90 " pciconf -a device",
91 " pciconf -r [-b | -h] device addr[:addr2]",
92 " pciconf -w [-b | -h] device addr value");
90 exit (1);
91}
92
93int
94main(int argc, char **argv)
95{
96 int c;
97 int listmode, readmode, writemode, attachedmode;
98 int bars, caps, errors, verbose;
99 int byte, isshort;
100
101 listmode = readmode = writemode = attachedmode = 0;
102 bars = caps = errors = verbose = byte = isshort = 0;
103
104 while ((c = getopt(argc, argv, "abcehlrwv")) != -1) {
105 switch(c) {
106 case 'a':
107 attachedmode = 1;
108 break;
109
110 case 'b':
111 bars = 1;
112 byte = 1;
113 break;
114
115 case 'c':
116 caps = 1;
117 break;
118
119 case 'e':
120 errors = 1;
121 break;
122
123 case 'h':
124 isshort = 1;
125 break;
126
127 case 'l':
128 listmode = 1;
129 break;
130
131 case 'r':
132 readmode = 1;
133 break;
134
135 case 'w':
136 writemode = 1;
137 break;
138
139 case 'v':
140 verbose = 1;
141 break;
142
143 default:
144 usage();
145 }
146 }
147
93 exit (1);
94}
95
96int
97main(int argc, char **argv)
98{
99 int c;
100 int listmode, readmode, writemode, attachedmode;
101 int bars, caps, errors, verbose;
102 int byte, isshort;
103
104 listmode = readmode = writemode = attachedmode = 0;
105 bars = caps = errors = verbose = byte = isshort = 0;
106
107 while ((c = getopt(argc, argv, "abcehlrwv")) != -1) {
108 switch(c) {
109 case 'a':
110 attachedmode = 1;
111 break;
112
113 case 'b':
114 bars = 1;
115 byte = 1;
116 break;
117
118 case 'c':
119 caps = 1;
120 break;
121
122 case 'e':
123 errors = 1;
124 break;
125
126 case 'h':
127 isshort = 1;
128 break;
129
130 case 'l':
131 listmode = 1;
132 break;
133
134 case 'r':
135 readmode = 1;
136 break;
137
138 case 'w':
139 writemode = 1;
140 break;
141
142 case 'v':
143 verbose = 1;
144 break;
145
146 default:
147 usage();
148 }
149 }
150
148 if ((listmode && optind != argc)
151 if ((listmode && optind >= argc + 1)
149 || (writemode && optind + 3 != argc)
150 || (readmode && optind + 2 != argc)
151 || (attachedmode && optind + 1 != argc))
152 usage();
153
154 if (listmode) {
152 || (writemode && optind + 3 != argc)
153 || (readmode && optind + 2 != argc)
154 || (attachedmode && optind + 1 != argc))
155 usage();
156
157 if (listmode) {
155 list_devs(verbose, bars, caps, errors);
158 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose,
159 bars, caps, errors);
156 } else if (attachedmode) {
157 chkattached(argv[optind]);
158 } else if (readmode) {
159 readit(argv[optind], argv[optind + 1],
160 byte ? 1 : isshort ? 2 : 4);
161 } else if (writemode) {
162 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
163 byte ? 1 : isshort ? 2 : 4);
164 } else {
165 usage();
166 }
167
168 return exitstatus;
169}
170
171static void
160 } else if (attachedmode) {
161 chkattached(argv[optind]);
162 } else if (readmode) {
163 readit(argv[optind], argv[optind + 1],
164 byte ? 1 : isshort ? 2 : 4);
165 } else if (writemode) {
166 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
167 byte ? 1 : isshort ? 2 : 4);
168 } else {
169 usage();
170 }
171
172 return exitstatus;
173}
174
175static void
172list_devs(int verbose, int bars, int caps, int errors)
176list_devs(const char *name, int verbose, int bars, int caps, int errors)
173{
174 int fd;
175 struct pci_conf_io pc;
176 struct pci_conf conf[255], *p;
177{
178 int fd;
179 struct pci_conf_io pc;
180 struct pci_conf conf[255], *p;
181 struct pci_match_conf patterns[1];
177 int none_count = 0;
178
179 if (verbose)
180 load_vendors();
181
182 fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0);
183 if (fd < 0)
184 err(1, "%s", _PATH_DEVPCI);
185
186 bzero(&pc, sizeof(struct pci_conf_io));
187 pc.match_buf_len = sizeof(conf);
188 pc.matches = conf;
182 int none_count = 0;
183
184 if (verbose)
185 load_vendors();
186
187 fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0);
188 if (fd < 0)
189 err(1, "%s", _PATH_DEVPCI);
190
191 bzero(&pc, sizeof(struct pci_conf_io));
192 pc.match_buf_len = sizeof(conf);
193 pc.matches = conf;
194 if (name != NULL) {
195 bzero(&patterns, sizeof(patterns));
196 patterns[0].pc_sel = getsel(name);
197 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
198 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
199 PCI_GETCONF_MATCH_FUNC;
200 pc.num_patterns = 1;
201 pc.pat_buf_len = sizeof(patterns);
202 pc.patterns = patterns;
203 }
189
190 do {
191 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
192 err(1, "ioctl(PCIOCGETCONF)");
193
194 /*
195 * 255 entries should be more than enough for most people,
196 * but if someone has more devices, and then changes things
197 * around between ioctls, we'll do the cheesy thing and
198 * just bail. The alternative would be to go back to the
199 * beginning of the list, and print things twice, which may
200 * not be desirable.
201 */
202 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
203 warnx("PCI device list changed, please try again");
204 exitstatus = 1;
205 close(fd);
206 return;
207 } else if (pc.status == PCI_GETCONF_ERROR) {
208 warnx("error returned from PCIOCGETCONF ioctl");
209 exitstatus = 1;
210 close(fd);
211 return;
212 }
213 for (p = conf; p < &conf[pc.num_matches]; p++) {
214 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
215 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
216 (p->pd_name && *p->pd_name) ? p->pd_name :
217 "none",
218 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
219 none_count++, p->pc_sel.pc_domain,
220 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
221 p->pc_sel.pc_func, (p->pc_class << 16) |
222 (p->pc_subclass << 8) | p->pc_progif,
223 (p->pc_subdevice << 16) | p->pc_subvendor,
224 (p->pc_device << 16) | p->pc_vendor,
225 p->pc_revid, p->pc_hdr);
226 if (verbose)
227 list_verbose(p);
228 if (bars)
229 list_bars(fd, p);
230 if (caps)
231 list_caps(fd, p);
232 if (errors)
233 list_errors(fd, p);
234 }
235 } while (pc.status == PCI_GETCONF_MORE_DEVS);
236
237 close(fd);
238}
239
240static void
241list_bars(int fd, struct pci_conf *p)
242{
243 struct pci_bar_io bar;
244 uint64_t base;
245 const char *type;
246 int i, range, max;
247
248 switch (p->pc_hdr & PCIM_HDRTYPE) {
249 case PCIM_HDRTYPE_NORMAL:
250 max = PCIR_MAX_BAR_0;
251 break;
252 case PCIM_HDRTYPE_BRIDGE:
253 max = PCIR_MAX_BAR_1;
254 break;
255 case PCIM_HDRTYPE_CARDBUS:
256 max = PCIR_MAX_BAR_2;
257 break;
258 default:
259 return;
260 }
261
262 for (i = 0; i <= max; i++) {
263 bar.pbi_sel = p->pc_sel;
264 bar.pbi_reg = PCIR_BAR(i);
265 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
266 continue;
267 if (PCI_BAR_IO(bar.pbi_base)) {
268 type = "I/O Port";
269 range = 32;
270 base = bar.pbi_base & PCIM_BAR_IO_BASE;
271 } else {
272 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
273 type = "Prefetchable Memory";
274 else
275 type = "Memory";
276 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
277 case PCIM_BAR_MEM_32:
278 range = 32;
279 break;
280 case PCIM_BAR_MEM_1MB:
281 range = 20;
282 break;
283 case PCIM_BAR_MEM_64:
284 range = 64;
285 break;
286 default:
287 range = -1;
288 }
289 base = bar.pbi_base & ~((uint64_t)0xf);
290 }
291 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
292 PCIR_BAR(i), type, range, (uintmax_t)base);
293 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length,
294 bar.pbi_enabled ? "enabled" : "disabled");
295 }
296}
297
298static void
299list_verbose(struct pci_conf *p)
300{
301 struct pci_vendor_info *vi;
302 struct pci_device_info *di;
303 const char *dp;
304
305 TAILQ_FOREACH(vi, &pci_vendors, link) {
306 if (vi->id == p->pc_vendor) {
307 printf(" vendor = '%s'\n", vi->desc);
308 break;
309 }
310 }
311 if (vi == NULL) {
312 di = NULL;
313 } else {
314 TAILQ_FOREACH(di, &vi->devs, link) {
315 if (di->id == p->pc_device) {
316 printf(" device = '%s'\n", di->desc);
317 break;
318 }
319 }
320 }
321 if ((dp = guess_class(p)) != NULL)
322 printf(" class = %s\n", dp);
323 if ((dp = guess_subclass(p)) != NULL)
324 printf(" subclass = %s\n", dp);
325}
326
327/*
328 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
329 */
330static struct
331{
332 int class;
333 int subclass;
334 const char *desc;
335} pci_nomatch_tab[] = {
336 {PCIC_OLD, -1, "old"},
337 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
338 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
339 {PCIC_STORAGE, -1, "mass storage"},
340 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
341 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
342 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
343 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
344 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
345 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
346 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
347 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
348 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
349 {PCIC_NETWORK, -1, "network"},
350 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
351 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
352 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
353 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
354 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
355 {PCIC_DISPLAY, -1, "display"},
356 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
357 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
358 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
359 {PCIC_MULTIMEDIA, -1, "multimedia"},
360 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
361 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
362 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
363 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
364 {PCIC_MEMORY, -1, "memory"},
365 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
366 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
367 {PCIC_BRIDGE, -1, "bridge"},
368 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
369 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
370 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
371 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
372 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
373 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
374 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
375 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
376 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
377 {PCIC_SIMPLECOMM, -1, "simple comms"},
378 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
379 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
380 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
381 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
382 {PCIC_BASEPERIPH, -1, "base peripheral"},
383 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
384 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
385 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
386 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
387 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
388 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
389 {PCIC_INPUTDEV, -1, "input device"},
390 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
391 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
392 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
393 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
394 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
395 {PCIC_DOCKING, -1, "docking station"},
396 {PCIC_PROCESSOR, -1, "processor"},
397 {PCIC_SERIALBUS, -1, "serial bus"},
398 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
399 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
400 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
401 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
402 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
403 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
404 {PCIC_WIRELESS, -1, "wireless controller"},
405 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
406 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
407 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
408 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
409 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
410 {PCIC_SATCOM, -1, "satellite communication"},
411 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
412 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
413 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
414 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
415 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
416 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
417 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
418 {PCIC_DASP, -1, "dasp"},
419 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
420 {0, 0, NULL}
421};
422
423static const char *
424guess_class(struct pci_conf *p)
425{
426 int i;
427
428 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
429 if (pci_nomatch_tab[i].class == p->pc_class)
430 return(pci_nomatch_tab[i].desc);
431 }
432 return(NULL);
433}
434
435static const char *
436guess_subclass(struct pci_conf *p)
437{
438 int i;
439
440 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
441 if ((pci_nomatch_tab[i].class == p->pc_class) &&
442 (pci_nomatch_tab[i].subclass == p->pc_subclass))
443 return(pci_nomatch_tab[i].desc);
444 }
445 return(NULL);
446}
447
448static int
449load_vendors(void)
450{
451 const char *dbf;
452 FILE *db;
453 struct pci_vendor_info *cv;
454 struct pci_device_info *cd;
455 char buf[1024], str[1024];
456 char *ch;
457 int id, error;
458
459 /*
460 * Locate the database and initialise.
461 */
462 TAILQ_INIT(&pci_vendors);
463 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
464 dbf = _PATH_PCIVDB;
465 if ((db = fopen(dbf, "r")) == NULL)
466 return(1);
467 cv = NULL;
468 cd = NULL;
469 error = 0;
470
471 /*
472 * Scan input lines from the database
473 */
474 for (;;) {
475 if (fgets(buf, sizeof(buf), db) == NULL)
476 break;
477
478 if ((ch = strchr(buf, '#')) != NULL)
479 *ch = '\0';
480 ch = strchr(buf, '\0') - 1;
481 while (ch > buf && isspace(*ch))
482 *ch-- = '\0';
483 if (ch <= buf)
484 continue;
485
486 /* Can't handle subvendor / subdevice entries yet */
487 if (buf[0] == '\t' && buf[1] == '\t')
488 continue;
489
490 /* Check for vendor entry */
491 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
492 if ((id == 0) || (strlen(str) < 1))
493 continue;
494 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
495 warn("allocating vendor entry");
496 error = 1;
497 break;
498 }
499 if ((cv->desc = strdup(str)) == NULL) {
500 free(cv);
501 warn("allocating vendor description");
502 error = 1;
503 break;
504 }
505 cv->id = id;
506 TAILQ_INIT(&cv->devs);
507 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
508 continue;
509 }
510
511 /* Check for device entry */
512 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
513 if ((id == 0) || (strlen(str) < 1))
514 continue;
515 if (cv == NULL) {
516 warnx("device entry with no vendor!");
517 continue;
518 }
519 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
520 warn("allocating device entry");
521 error = 1;
522 break;
523 }
524 if ((cd->desc = strdup(str)) == NULL) {
525 free(cd);
526 warn("allocating device description");
527 error = 1;
528 break;
529 }
530 cd->id = id;
531 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
532 continue;
533 }
534
535 /* It's a comment or junk, ignore it */
536 }
537 if (ferror(db))
538 error = 1;
539 fclose(db);
540
541 return(error);
542}
543
544uint32_t
545read_config(int fd, struct pcisel *sel, long reg, int width)
546{
547 struct pci_io pi;
548
549 pi.pi_sel = *sel;
550 pi.pi_reg = reg;
551 pi.pi_width = width;
552
553 if (ioctl(fd, PCIOCREAD, &pi) < 0)
554 err(1, "ioctl(PCIOCREAD)");
555
556 return (pi.pi_data);
557}
558
559static struct pcisel
204
205 do {
206 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
207 err(1, "ioctl(PCIOCGETCONF)");
208
209 /*
210 * 255 entries should be more than enough for most people,
211 * but if someone has more devices, and then changes things
212 * around between ioctls, we'll do the cheesy thing and
213 * just bail. The alternative would be to go back to the
214 * beginning of the list, and print things twice, which may
215 * not be desirable.
216 */
217 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
218 warnx("PCI device list changed, please try again");
219 exitstatus = 1;
220 close(fd);
221 return;
222 } else if (pc.status == PCI_GETCONF_ERROR) {
223 warnx("error returned from PCIOCGETCONF ioctl");
224 exitstatus = 1;
225 close(fd);
226 return;
227 }
228 for (p = conf; p < &conf[pc.num_matches]; p++) {
229 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
230 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
231 (p->pd_name && *p->pd_name) ? p->pd_name :
232 "none",
233 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
234 none_count++, p->pc_sel.pc_domain,
235 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
236 p->pc_sel.pc_func, (p->pc_class << 16) |
237 (p->pc_subclass << 8) | p->pc_progif,
238 (p->pc_subdevice << 16) | p->pc_subvendor,
239 (p->pc_device << 16) | p->pc_vendor,
240 p->pc_revid, p->pc_hdr);
241 if (verbose)
242 list_verbose(p);
243 if (bars)
244 list_bars(fd, p);
245 if (caps)
246 list_caps(fd, p);
247 if (errors)
248 list_errors(fd, p);
249 }
250 } while (pc.status == PCI_GETCONF_MORE_DEVS);
251
252 close(fd);
253}
254
255static void
256list_bars(int fd, struct pci_conf *p)
257{
258 struct pci_bar_io bar;
259 uint64_t base;
260 const char *type;
261 int i, range, max;
262
263 switch (p->pc_hdr & PCIM_HDRTYPE) {
264 case PCIM_HDRTYPE_NORMAL:
265 max = PCIR_MAX_BAR_0;
266 break;
267 case PCIM_HDRTYPE_BRIDGE:
268 max = PCIR_MAX_BAR_1;
269 break;
270 case PCIM_HDRTYPE_CARDBUS:
271 max = PCIR_MAX_BAR_2;
272 break;
273 default:
274 return;
275 }
276
277 for (i = 0; i <= max; i++) {
278 bar.pbi_sel = p->pc_sel;
279 bar.pbi_reg = PCIR_BAR(i);
280 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
281 continue;
282 if (PCI_BAR_IO(bar.pbi_base)) {
283 type = "I/O Port";
284 range = 32;
285 base = bar.pbi_base & PCIM_BAR_IO_BASE;
286 } else {
287 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
288 type = "Prefetchable Memory";
289 else
290 type = "Memory";
291 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
292 case PCIM_BAR_MEM_32:
293 range = 32;
294 break;
295 case PCIM_BAR_MEM_1MB:
296 range = 20;
297 break;
298 case PCIM_BAR_MEM_64:
299 range = 64;
300 break;
301 default:
302 range = -1;
303 }
304 base = bar.pbi_base & ~((uint64_t)0xf);
305 }
306 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
307 PCIR_BAR(i), type, range, (uintmax_t)base);
308 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length,
309 bar.pbi_enabled ? "enabled" : "disabled");
310 }
311}
312
313static void
314list_verbose(struct pci_conf *p)
315{
316 struct pci_vendor_info *vi;
317 struct pci_device_info *di;
318 const char *dp;
319
320 TAILQ_FOREACH(vi, &pci_vendors, link) {
321 if (vi->id == p->pc_vendor) {
322 printf(" vendor = '%s'\n", vi->desc);
323 break;
324 }
325 }
326 if (vi == NULL) {
327 di = NULL;
328 } else {
329 TAILQ_FOREACH(di, &vi->devs, link) {
330 if (di->id == p->pc_device) {
331 printf(" device = '%s'\n", di->desc);
332 break;
333 }
334 }
335 }
336 if ((dp = guess_class(p)) != NULL)
337 printf(" class = %s\n", dp);
338 if ((dp = guess_subclass(p)) != NULL)
339 printf(" subclass = %s\n", dp);
340}
341
342/*
343 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
344 */
345static struct
346{
347 int class;
348 int subclass;
349 const char *desc;
350} pci_nomatch_tab[] = {
351 {PCIC_OLD, -1, "old"},
352 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
353 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
354 {PCIC_STORAGE, -1, "mass storage"},
355 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
356 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
357 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
358 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
359 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
360 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
361 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
362 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
363 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
364 {PCIC_NETWORK, -1, "network"},
365 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
366 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
367 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
368 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
369 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
370 {PCIC_DISPLAY, -1, "display"},
371 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
372 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
373 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
374 {PCIC_MULTIMEDIA, -1, "multimedia"},
375 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
376 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
377 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
378 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
379 {PCIC_MEMORY, -1, "memory"},
380 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
381 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
382 {PCIC_BRIDGE, -1, "bridge"},
383 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
384 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
385 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
386 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
387 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
388 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
389 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
390 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
391 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
392 {PCIC_SIMPLECOMM, -1, "simple comms"},
393 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
394 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
395 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
396 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
397 {PCIC_BASEPERIPH, -1, "base peripheral"},
398 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
399 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
400 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
401 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
402 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
403 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
404 {PCIC_INPUTDEV, -1, "input device"},
405 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
406 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
407 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
408 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
409 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
410 {PCIC_DOCKING, -1, "docking station"},
411 {PCIC_PROCESSOR, -1, "processor"},
412 {PCIC_SERIALBUS, -1, "serial bus"},
413 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
414 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
415 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
416 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
417 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
418 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
419 {PCIC_WIRELESS, -1, "wireless controller"},
420 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
421 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
422 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
423 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
424 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
425 {PCIC_SATCOM, -1, "satellite communication"},
426 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
427 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
428 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
429 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
430 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
431 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
432 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
433 {PCIC_DASP, -1, "dasp"},
434 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
435 {0, 0, NULL}
436};
437
438static const char *
439guess_class(struct pci_conf *p)
440{
441 int i;
442
443 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
444 if (pci_nomatch_tab[i].class == p->pc_class)
445 return(pci_nomatch_tab[i].desc);
446 }
447 return(NULL);
448}
449
450static const char *
451guess_subclass(struct pci_conf *p)
452{
453 int i;
454
455 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
456 if ((pci_nomatch_tab[i].class == p->pc_class) &&
457 (pci_nomatch_tab[i].subclass == p->pc_subclass))
458 return(pci_nomatch_tab[i].desc);
459 }
460 return(NULL);
461}
462
463static int
464load_vendors(void)
465{
466 const char *dbf;
467 FILE *db;
468 struct pci_vendor_info *cv;
469 struct pci_device_info *cd;
470 char buf[1024], str[1024];
471 char *ch;
472 int id, error;
473
474 /*
475 * Locate the database and initialise.
476 */
477 TAILQ_INIT(&pci_vendors);
478 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
479 dbf = _PATH_PCIVDB;
480 if ((db = fopen(dbf, "r")) == NULL)
481 return(1);
482 cv = NULL;
483 cd = NULL;
484 error = 0;
485
486 /*
487 * Scan input lines from the database
488 */
489 for (;;) {
490 if (fgets(buf, sizeof(buf), db) == NULL)
491 break;
492
493 if ((ch = strchr(buf, '#')) != NULL)
494 *ch = '\0';
495 ch = strchr(buf, '\0') - 1;
496 while (ch > buf && isspace(*ch))
497 *ch-- = '\0';
498 if (ch <= buf)
499 continue;
500
501 /* Can't handle subvendor / subdevice entries yet */
502 if (buf[0] == '\t' && buf[1] == '\t')
503 continue;
504
505 /* Check for vendor entry */
506 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
507 if ((id == 0) || (strlen(str) < 1))
508 continue;
509 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
510 warn("allocating vendor entry");
511 error = 1;
512 break;
513 }
514 if ((cv->desc = strdup(str)) == NULL) {
515 free(cv);
516 warn("allocating vendor description");
517 error = 1;
518 break;
519 }
520 cv->id = id;
521 TAILQ_INIT(&cv->devs);
522 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
523 continue;
524 }
525
526 /* Check for device entry */
527 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
528 if ((id == 0) || (strlen(str) < 1))
529 continue;
530 if (cv == NULL) {
531 warnx("device entry with no vendor!");
532 continue;
533 }
534 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
535 warn("allocating device entry");
536 error = 1;
537 break;
538 }
539 if ((cd->desc = strdup(str)) == NULL) {
540 free(cd);
541 warn("allocating device description");
542 error = 1;
543 break;
544 }
545 cd->id = id;
546 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
547 continue;
548 }
549
550 /* It's a comment or junk, ignore it */
551 }
552 if (ferror(db))
553 error = 1;
554 fclose(db);
555
556 return(error);
557}
558
559uint32_t
560read_config(int fd, struct pcisel *sel, long reg, int width)
561{
562 struct pci_io pi;
563
564 pi.pi_sel = *sel;
565 pi.pi_reg = reg;
566 pi.pi_width = width;
567
568 if (ioctl(fd, PCIOCREAD, &pi) < 0)
569 err(1, "ioctl(PCIOCREAD)");
570
571 return (pi.pi_data);
572}
573
574static struct pcisel
560getsel(const char *str)
575getdevice(const char *name)
561{
576{
577 struct pci_conf_io pc;
578 struct pci_conf conf[1];
579 struct pci_match_conf patterns[1];
580 char *cp;
581 int fd;
582
583 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
584 if (fd < 0)
585 err(1, "%s", _PATH_DEVPCI);
586
587 bzero(&pc, sizeof(struct pci_conf_io));
588 pc.match_buf_len = sizeof(conf);
589 pc.matches = conf;
590
591 bzero(&patterns, sizeof(patterns));
592
593 /*
594 * The pattern structure requires the unit to be split out from
595 * the driver name. Walk backwards from the end of the name to
596 * find the start of the unit.
597 */
598 if (name[0] == '\0')
599 err(1, "Empty device name");
600 cp = strchr(name, '\0');
601 assert(cp != NULL && cp != name);
602 cp--;
603 while (cp != name && isdigit(cp[-1]))
604 cp--;
605 if (cp == name)
606 errx(1, "Invalid device name");
607 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name))
608 errx(1, "Device name i2s too long");
609 memcpy(patterns[0].pd_name, name, cp - name);
610 patterns[0].pd_unit = strtol(cp, &cp, 10);
611 assert(*cp == '\0');
612 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
613 pc.num_patterns = 1;
614 pc.pat_buf_len = sizeof(patterns);
615 pc.patterns = patterns;
616
617 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
618 err(1, "ioctl(PCIOCGETCONF)");
619 if (pc.status != PCI_GETCONF_LAST_DEVICE &&
620 pc.status != PCI_GETCONF_MORE_DEVS)
621 errx(1, "error returned from PCIOCGETCONF ioctl");
622 close(fd);
623 if (pc.num_matches == 0)
624 errx(1, "Device not found");
625 return (conf[0].pc_sel);
626}
627
628static struct pcisel
629parsesel(const char *str)
630{
562 char *ep = strchr(str, '@');
563 char *epbase;
564 struct pcisel sel;
565 unsigned long selarr[4];
566 int i;
567
568 if (ep == NULL)
569 ep = (char *)str;
570 else
571 ep++;
572
573 epbase = ep;
574
575 if (strncmp(ep, "pci", 3) == 0) {
576 ep += 3;
577 i = 0;
578 do {
579 selarr[i++] = strtoul(ep, &ep, 10);
580 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
581
582 if (i > 2)
583 sel.pc_func = selarr[--i];
584 else
585 sel.pc_func = 0;
586 sel.pc_dev = selarr[--i];
587 sel.pc_bus = selarr[--i];
588 if (i > 0)
589 sel.pc_domain = selarr[--i];
590 else
591 sel.pc_domain = 0;
592 }
593 if (*ep != '\x0' || ep == epbase)
594 errx(1, "cannot parse selector %s", str);
595 return sel;
596}
597
631 char *ep = strchr(str, '@');
632 char *epbase;
633 struct pcisel sel;
634 unsigned long selarr[4];
635 int i;
636
637 if (ep == NULL)
638 ep = (char *)str;
639 else
640 ep++;
641
642 epbase = ep;
643
644 if (strncmp(ep, "pci", 3) == 0) {
645 ep += 3;
646 i = 0;
647 do {
648 selarr[i++] = strtoul(ep, &ep, 10);
649 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
650
651 if (i > 2)
652 sel.pc_func = selarr[--i];
653 else
654 sel.pc_func = 0;
655 sel.pc_dev = selarr[--i];
656 sel.pc_bus = selarr[--i];
657 if (i > 0)
658 sel.pc_domain = selarr[--i];
659 else
660 sel.pc_domain = 0;
661 }
662 if (*ep != '\x0' || ep == epbase)
663 errx(1, "cannot parse selector %s", str);
664 return sel;
665}
666
667static struct pcisel
668getsel(const char *str)
669{
670
671 /*
672 * No device names contain colons and selectors always contain
673 * at least one colon.
674 */
675 if (strchr(str, ':') == NULL)
676 return (getdevice(str));
677 else
678 return (parsesel(str));
679}
680
598static void
599readone(int fd, struct pcisel *sel, long reg, int width)
600{
601
602 printf("%0*x", width*2, read_config(fd, sel, reg, width));
603}
604
605static void
606readit(const char *name, const char *reg, int width)
607{
608 long rstart;
609 long rend;
610 long r;
611 char *end;
612 int i;
613 int fd;
614 struct pcisel sel;
615
616 fd = open(_PATH_DEVPCI, O_RDWR, 0);
617 if (fd < 0)
618 err(1, "%s", _PATH_DEVPCI);
619
620 rend = rstart = strtol(reg, &end, 0);
621 if (end && *end == ':') {
622 end++;
623 rend = strtol(end, (char **) 0, 0);
624 }
625 sel = getsel(name);
626 for (i = 1, r = rstart; r <= rend; i++, r += width) {
627 readone(fd, &sel, r, width);
628 if (i && !(i % 8))
629 putchar(' ');
630 putchar(i % (16/width) ? ' ' : '\n');
631 }
632 if (i % (16/width) != 1)
633 putchar('\n');
634 close(fd);
635}
636
637static void
638writeit(const char *name, const char *reg, const char *data, int width)
639{
640 int fd;
641 struct pci_io pi;
642
643 pi.pi_sel = getsel(name);
644 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
645 pi.pi_width = width;
646 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
647
648 fd = open(_PATH_DEVPCI, O_RDWR, 0);
649 if (fd < 0)
650 err(1, "%s", _PATH_DEVPCI);
651
652 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
653 err(1, "ioctl(PCIOCWRITE)");
654}
655
656static void
657chkattached(const char *name)
658{
659 int fd;
660 struct pci_io pi;
661
662 pi.pi_sel = getsel(name);
663
664 fd = open(_PATH_DEVPCI, O_RDWR, 0);
665 if (fd < 0)
666 err(1, "%s", _PATH_DEVPCI);
667
668 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
669 err(1, "ioctl(PCIOCATTACHED)");
670
671 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
672 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
673}
681static void
682readone(int fd, struct pcisel *sel, long reg, int width)
683{
684
685 printf("%0*x", width*2, read_config(fd, sel, reg, width));
686}
687
688static void
689readit(const char *name, const char *reg, int width)
690{
691 long rstart;
692 long rend;
693 long r;
694 char *end;
695 int i;
696 int fd;
697 struct pcisel sel;
698
699 fd = open(_PATH_DEVPCI, O_RDWR, 0);
700 if (fd < 0)
701 err(1, "%s", _PATH_DEVPCI);
702
703 rend = rstart = strtol(reg, &end, 0);
704 if (end && *end == ':') {
705 end++;
706 rend = strtol(end, (char **) 0, 0);
707 }
708 sel = getsel(name);
709 for (i = 1, r = rstart; r <= rend; i++, r += width) {
710 readone(fd, &sel, r, width);
711 if (i && !(i % 8))
712 putchar(' ');
713 putchar(i % (16/width) ? ' ' : '\n');
714 }
715 if (i % (16/width) != 1)
716 putchar('\n');
717 close(fd);
718}
719
720static void
721writeit(const char *name, const char *reg, const char *data, int width)
722{
723 int fd;
724 struct pci_io pi;
725
726 pi.pi_sel = getsel(name);
727 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
728 pi.pi_width = width;
729 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
730
731 fd = open(_PATH_DEVPCI, O_RDWR, 0);
732 if (fd < 0)
733 err(1, "%s", _PATH_DEVPCI);
734
735 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
736 err(1, "ioctl(PCIOCWRITE)");
737}
738
739static void
740chkattached(const char *name)
741{
742 int fd;
743 struct pci_io pi;
744
745 pi.pi_sel = getsel(name);
746
747 fd = open(_PATH_DEVPCI, O_RDWR, 0);
748 if (fd < 0)
749 err(1, "%s", _PATH_DEVPCI);
750
751 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
752 err(1, "ioctl(PCIOCATTACHED)");
753
754 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
755 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
756}