pcictl.c revision 1.18.20.1 1 1.18.20.1 martin /* $NetBSD: pcictl.c,v 1.18.20.1 2014/09/10 08:38:31 martin Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * pcictl(8) -- a program to manipulate the PCI bus
40 1.1 thorpej */
41 1.1 thorpej
42 1.1 thorpej #include <sys/param.h>
43 1.1 thorpej #include <sys/ioctl.h>
44 1.1 thorpej #include <err.h>
45 1.1 thorpej #include <errno.h>
46 1.1 thorpej #include <fcntl.h>
47 1.5 tron #include <paths.h>
48 1.1 thorpej #include <pci.h>
49 1.1 thorpej #include <stdio.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <string.h>
52 1.1 thorpej #include <unistd.h>
53 1.1 thorpej #include <util.h>
54 1.1 thorpej
55 1.1 thorpej #include <dev/pci/pcireg.h>
56 1.1 thorpej #include <dev/pci/pcidevs.h>
57 1.1 thorpej #include <dev/pci/pciio.h>
58 1.1 thorpej
59 1.1 thorpej struct command {
60 1.1 thorpej const char *cmd_name;
61 1.1 thorpej const char *arg_names;
62 1.1 thorpej void (*cmd_func)(int, char *[]);
63 1.1 thorpej int open_flags;
64 1.1 thorpej };
65 1.1 thorpej
66 1.18 joerg __dead static void usage(void);
67 1.1 thorpej
68 1.18 joerg static int pcifd;
69 1.1 thorpej
70 1.18 joerg static struct pciio_businfo pci_businfo;
71 1.1 thorpej
72 1.18 joerg static const char *dvname;
73 1.18 joerg static char dvname_store[MAXPATHLEN];
74 1.18 joerg static const char *cmdname;
75 1.18 joerg static int print_numbers = 0;
76 1.1 thorpej
77 1.16 cegger static void cmd_list(int, char *[]);
78 1.16 cegger static void cmd_dump(int, char *[]);
79 1.18.20.1 martin static void cmd_read(int, char *[]);
80 1.18.20.1 martin static void cmd_write(int, char *[]);
81 1.1 thorpej
82 1.18 joerg static const struct command commands[] = {
83 1.1 thorpej { "list",
84 1.10 hubertf "[-n] [-b bus] [-d device] [-f function]",
85 1.1 thorpej cmd_list,
86 1.1 thorpej O_RDONLY },
87 1.1 thorpej
88 1.1 thorpej { "dump",
89 1.1 thorpej "[-b bus] -d device [-f function]",
90 1.1 thorpej cmd_dump,
91 1.1 thorpej O_RDONLY },
92 1.1 thorpej
93 1.18.20.1 martin { "read",
94 1.18.20.1 martin "[-b bus] -d device [-f function] reg",
95 1.18.20.1 martin cmd_read,
96 1.18.20.1 martin O_RDONLY },
97 1.18.20.1 martin
98 1.18.20.1 martin { "write",
99 1.18.20.1 martin "[-b bus] -d device [-f function] reg value",
100 1.18.20.1 martin cmd_write,
101 1.18.20.1 martin O_WRONLY },
102 1.18.20.1 martin
103 1.12 lukem { 0, 0, 0, 0 },
104 1.1 thorpej };
105 1.1 thorpej
106 1.16 cegger static int parse_bdf(const char *);
107 1.18.20.1 martin static u_int parse_reg(const char *);
108 1.1 thorpej
109 1.16 cegger static void scan_pci(int, int, int, void (*)(u_int, u_int, u_int));
110 1.1 thorpej
111 1.16 cegger static void scan_pci_list(u_int, u_int, u_int);
112 1.16 cegger static void scan_pci_dump(u_int, u_int, u_int);
113 1.1 thorpej
114 1.1 thorpej int
115 1.1 thorpej main(int argc, char *argv[])
116 1.1 thorpej {
117 1.1 thorpej int i;
118 1.1 thorpej
119 1.1 thorpej /* Must have at least: device command */
120 1.1 thorpej if (argc < 3)
121 1.1 thorpej usage();
122 1.1 thorpej
123 1.1 thorpej /* Skip program name, get and skip device name, get command. */
124 1.1 thorpej dvname = argv[1];
125 1.1 thorpej cmdname = argv[2];
126 1.1 thorpej argv += 2;
127 1.1 thorpej argc -= 2;
128 1.1 thorpej
129 1.1 thorpej /* Look up and call the command. */
130 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
131 1.1 thorpej if (strcmp(cmdname, commands[i].cmd_name) == 0)
132 1.1 thorpej break;
133 1.1 thorpej if (commands[i].cmd_name == NULL)
134 1.13 cegger errx(EXIT_FAILURE, "unknown command: %s", cmdname);
135 1.1 thorpej
136 1.1 thorpej /* Open the device. */
137 1.5 tron if ((strchr(dvname, '/') == NULL) &&
138 1.5 tron (snprintf(dvname_store, sizeof(dvname_store), _PATH_DEV "%s",
139 1.12 lukem dvname) < (int)sizeof(dvname_store)))
140 1.5 tron dvname = dvname_store;
141 1.5 tron pcifd = open(dvname, commands[i].open_flags);
142 1.5 tron if (pcifd < 0)
143 1.13 cegger err(EXIT_FAILURE, "%s", dvname);
144 1.1 thorpej
145 1.1 thorpej /* Make sure the device is a PCI bus. */
146 1.1 thorpej if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
147 1.13 cegger errx(EXIT_FAILURE, "%s: not a PCI bus device", dvname);
148 1.1 thorpej
149 1.1 thorpej (*commands[i].cmd_func)(argc, argv);
150 1.13 cegger exit(EXIT_SUCCESS);
151 1.1 thorpej }
152 1.1 thorpej
153 1.16 cegger static void
154 1.13 cegger usage(void)
155 1.1 thorpej {
156 1.1 thorpej int i;
157 1.1 thorpej
158 1.7 jmmv fprintf(stderr, "usage: %s device command [arg [...]]\n",
159 1.1 thorpej getprogname());
160 1.1 thorpej
161 1.1 thorpej fprintf(stderr, " Available commands:\n");
162 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
163 1.1 thorpej fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
164 1.1 thorpej commands[i].arg_names);
165 1.1 thorpej
166 1.13 cegger exit(EXIT_FAILURE);
167 1.1 thorpej }
168 1.1 thorpej
169 1.16 cegger static void
170 1.1 thorpej cmd_list(int argc, char *argv[])
171 1.1 thorpej {
172 1.1 thorpej int bus, dev, func;
173 1.1 thorpej int ch;
174 1.1 thorpej
175 1.17 jmcneill bus = -1;
176 1.3 thorpej dev = func = -1;
177 1.1 thorpej
178 1.10 hubertf while ((ch = getopt(argc, argv, "nb:d:f:")) != -1) {
179 1.1 thorpej switch (ch) {
180 1.1 thorpej case 'b':
181 1.1 thorpej bus = parse_bdf(optarg);
182 1.1 thorpej break;
183 1.1 thorpej case 'd':
184 1.1 thorpej dev = parse_bdf(optarg);
185 1.1 thorpej break;
186 1.1 thorpej case 'f':
187 1.1 thorpej func = parse_bdf(optarg);
188 1.1 thorpej break;
189 1.10 hubertf case 'n':
190 1.10 hubertf print_numbers = 1;
191 1.10 hubertf break;
192 1.1 thorpej default:
193 1.1 thorpej usage();
194 1.1 thorpej }
195 1.1 thorpej }
196 1.1 thorpej argv += optind;
197 1.1 thorpej argc -= optind;
198 1.1 thorpej
199 1.1 thorpej if (argc != 0)
200 1.1 thorpej usage();
201 1.1 thorpej
202 1.1 thorpej scan_pci(bus, dev, func, scan_pci_list);
203 1.1 thorpej }
204 1.1 thorpej
205 1.16 cegger static void
206 1.1 thorpej cmd_dump(int argc, char *argv[])
207 1.1 thorpej {
208 1.1 thorpej int bus, dev, func;
209 1.1 thorpej int ch;
210 1.1 thorpej
211 1.1 thorpej bus = pci_businfo.busno;
212 1.1 thorpej func = 0;
213 1.1 thorpej dev = -1;
214 1.1 thorpej
215 1.2 thorpej while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
216 1.1 thorpej switch (ch) {
217 1.1 thorpej case 'b':
218 1.1 thorpej bus = parse_bdf(optarg);
219 1.1 thorpej break;
220 1.1 thorpej case 'd':
221 1.1 thorpej dev = parse_bdf(optarg);
222 1.1 thorpej break;
223 1.1 thorpej case 'f':
224 1.1 thorpej func = parse_bdf(optarg);
225 1.1 thorpej break;
226 1.1 thorpej default:
227 1.1 thorpej usage();
228 1.1 thorpej }
229 1.1 thorpej }
230 1.1 thorpej argv += optind;
231 1.1 thorpej argc -= optind;
232 1.1 thorpej
233 1.1 thorpej if (argc != 0)
234 1.1 thorpej usage();
235 1.1 thorpej
236 1.1 thorpej if (bus == -1)
237 1.14 cegger errx(EXIT_FAILURE, "dump: wildcard bus number not permitted");
238 1.1 thorpej if (dev == -1)
239 1.14 cegger errx(EXIT_FAILURE, "dump: must specify a device number");
240 1.1 thorpej if (func == -1)
241 1.14 cegger errx(EXIT_FAILURE, "dump: wildcard function number not permitted");
242 1.1 thorpej
243 1.1 thorpej scan_pci(bus, dev, func, scan_pci_dump);
244 1.1 thorpej }
245 1.1 thorpej
246 1.18.20.1 martin static void
247 1.18.20.1 martin cmd_read(int argc, char *argv[])
248 1.18.20.1 martin {
249 1.18.20.1 martin int bus, dev, func;
250 1.18.20.1 martin u_int reg;
251 1.18.20.1 martin pcireg_t value;
252 1.18.20.1 martin int ch;
253 1.18.20.1 martin
254 1.18.20.1 martin bus = pci_businfo.busno;
255 1.18.20.1 martin func = 0;
256 1.18.20.1 martin dev = -1;
257 1.18.20.1 martin
258 1.18.20.1 martin while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
259 1.18.20.1 martin switch (ch) {
260 1.18.20.1 martin case 'b':
261 1.18.20.1 martin bus = parse_bdf(optarg);
262 1.18.20.1 martin break;
263 1.18.20.1 martin case 'd':
264 1.18.20.1 martin dev = parse_bdf(optarg);
265 1.18.20.1 martin break;
266 1.18.20.1 martin case 'f':
267 1.18.20.1 martin func = parse_bdf(optarg);
268 1.18.20.1 martin break;
269 1.18.20.1 martin default:
270 1.18.20.1 martin usage();
271 1.18.20.1 martin }
272 1.18.20.1 martin }
273 1.18.20.1 martin argv += optind;
274 1.18.20.1 martin argc -= optind;
275 1.18.20.1 martin
276 1.18.20.1 martin if (argc != 1)
277 1.18.20.1 martin usage();
278 1.18.20.1 martin reg = parse_reg(argv[0]);
279 1.18.20.1 martin if (pcibus_conf_read(pcifd, bus, dev, func, reg, &value) == -1)
280 1.18.20.1 martin err(EXIT_FAILURE, "pcibus_conf_read"
281 1.18.20.1 martin "(bus %d dev %d func %d reg %u)", bus, dev, func, reg);
282 1.18.20.1 martin if (printf("%08x\n", value) < 0)
283 1.18.20.1 martin err(EXIT_FAILURE, "printf");
284 1.18.20.1 martin }
285 1.18.20.1 martin
286 1.18.20.1 martin static void
287 1.18.20.1 martin cmd_write(int argc, char *argv[])
288 1.18.20.1 martin {
289 1.18.20.1 martin int bus, dev, func;
290 1.18.20.1 martin u_int reg;
291 1.18.20.1 martin pcireg_t value;
292 1.18.20.1 martin int ch;
293 1.18.20.1 martin
294 1.18.20.1 martin bus = pci_businfo.busno;
295 1.18.20.1 martin func = 0;
296 1.18.20.1 martin dev = -1;
297 1.18.20.1 martin
298 1.18.20.1 martin while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
299 1.18.20.1 martin switch (ch) {
300 1.18.20.1 martin case 'b':
301 1.18.20.1 martin bus = parse_bdf(optarg);
302 1.18.20.1 martin break;
303 1.18.20.1 martin case 'd':
304 1.18.20.1 martin dev = parse_bdf(optarg);
305 1.18.20.1 martin break;
306 1.18.20.1 martin case 'f':
307 1.18.20.1 martin func = parse_bdf(optarg);
308 1.18.20.1 martin break;
309 1.18.20.1 martin default:
310 1.18.20.1 martin usage();
311 1.18.20.1 martin }
312 1.18.20.1 martin }
313 1.18.20.1 martin argv += optind;
314 1.18.20.1 martin argc -= optind;
315 1.18.20.1 martin
316 1.18.20.1 martin if (argc != 2)
317 1.18.20.1 martin usage();
318 1.18.20.1 martin reg = parse_reg(argv[0]);
319 1.18.20.1 martin __CTASSERT(sizeof(value) == sizeof(u_int));
320 1.18.20.1 martin value = parse_reg(argv[1]);
321 1.18.20.1 martin if (pcibus_conf_write(pcifd, bus, dev, func, reg, value) == -1)
322 1.18.20.1 martin err(EXIT_FAILURE, "pcibus_conf_write"
323 1.18.20.1 martin "(bus %d dev %d func %d reg %u value 0x%x)",
324 1.18.20.1 martin bus, dev, func, reg, value);
325 1.18.20.1 martin }
326 1.18.20.1 martin
327 1.16 cegger static int
328 1.1 thorpej parse_bdf(const char *str)
329 1.1 thorpej {
330 1.4 joda long value;
331 1.4 joda char *end;
332 1.1 thorpej
333 1.1 thorpej if (strcmp(str, "all") == 0 ||
334 1.1 thorpej strcmp(str, "any") == 0)
335 1.1 thorpej return (-1);
336 1.1 thorpej
337 1.18.20.1 martin errno = 0;
338 1.4 joda value = strtol(str, &end, 0);
339 1.18.20.1 martin if ((str[0] == '\0') || (*end != '\0'))
340 1.18.20.1 martin errx(EXIT_FAILURE, "\"%s\" is not a number", str);
341 1.18.20.1 martin if ((errno == ERANGE) && ((value == LONG_MIN) || (value == LONG_MAX)))
342 1.18.20.1 martin errx(EXIT_FAILURE, "out of range: %s", str);
343 1.18.20.1 martin if ((value < INT_MIN) || (INT_MAX < value))
344 1.18.20.1 martin errx(EXIT_FAILURE, "out of range: %lu", value);
345 1.18.20.1 martin
346 1.18.20.1 martin return value;
347 1.18.20.1 martin }
348 1.18.20.1 martin
349 1.18.20.1 martin static u_int
350 1.18.20.1 martin parse_reg(const char *str)
351 1.18.20.1 martin {
352 1.18.20.1 martin unsigned long value;
353 1.18.20.1 martin char *end;
354 1.18.20.1 martin
355 1.18.20.1 martin errno = 0;
356 1.18.20.1 martin value = strtoul(str, &end, 0);
357 1.18.20.1 martin if (*end != '\0')
358 1.14 cegger errx(EXIT_FAILURE, "\"%s\" is not a number", str);
359 1.18.20.1 martin if ((errno == ERANGE) && (value == ULONG_MAX))
360 1.18.20.1 martin errx(EXIT_FAILURE, "out of range: %s", str);
361 1.18.20.1 martin if (UINT_MAX < value)
362 1.18.20.1 martin errx(EXIT_FAILURE, "out of range: %lu", value);
363 1.4 joda
364 1.4 joda return value;
365 1.1 thorpej }
366 1.1 thorpej
367 1.16 cegger static void
368 1.1 thorpej scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
369 1.1 thorpej {
370 1.1 thorpej u_int busmin, busmax;
371 1.1 thorpej u_int devmin, devmax;
372 1.1 thorpej u_int funcmin, funcmax;
373 1.1 thorpej u_int bus, dev, func;
374 1.1 thorpej pcireg_t id, bhlcr;
375 1.1 thorpej
376 1.1 thorpej if (busarg == -1) {
377 1.1 thorpej busmin = 0;
378 1.1 thorpej busmax = 255;
379 1.1 thorpej } else
380 1.1 thorpej busmin = busmax = busarg;
381 1.1 thorpej
382 1.1 thorpej if (devarg == -1) {
383 1.1 thorpej devmin = 0;
384 1.9 bsh if (pci_businfo.maxdevs <= 0)
385 1.9 bsh devmax = 0;
386 1.9 bsh else
387 1.9 bsh devmax = pci_businfo.maxdevs - 1;
388 1.1 thorpej } else
389 1.1 thorpej devmin = devmax = devarg;
390 1.1 thorpej
391 1.1 thorpej for (bus = busmin; bus <= busmax; bus++) {
392 1.1 thorpej for (dev = devmin; dev <= devmax; dev++) {
393 1.1 thorpej if (pcibus_conf_read(pcifd, bus, dev, 0,
394 1.1 thorpej PCI_BHLC_REG, &bhlcr) != 0)
395 1.1 thorpej continue;
396 1.1 thorpej if (funcarg == -1) {
397 1.1 thorpej funcmin = 0;
398 1.1 thorpej if (PCI_HDRTYPE_MULTIFN(bhlcr))
399 1.1 thorpej funcmax = 7;
400 1.1 thorpej else
401 1.1 thorpej funcmax = 0;
402 1.1 thorpej } else
403 1.1 thorpej funcmin = funcmax = funcarg;
404 1.1 thorpej for (func = funcmin; func <= funcmax; func++) {
405 1.1 thorpej if (pcibus_conf_read(pcifd, bus, dev,
406 1.1 thorpej func, PCI_ID_REG, &id) != 0)
407 1.1 thorpej continue;
408 1.1 thorpej
409 1.1 thorpej /* Invalid vendor ID value? */
410 1.1 thorpej if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
411 1.1 thorpej continue;
412 1.1 thorpej /*
413 1.1 thorpej * XXX Not invalid, but we've done this
414 1.1 thorpej * ~forever.
415 1.1 thorpej */
416 1.1 thorpej if (PCI_VENDOR(id) == 0)
417 1.1 thorpej continue;
418 1.1 thorpej
419 1.1 thorpej (*cb)(bus, dev, func);
420 1.1 thorpej }
421 1.1 thorpej }
422 1.1 thorpej }
423 1.1 thorpej }
424 1.1 thorpej
425 1.16 cegger static void
426 1.1 thorpej scan_pci_list(u_int bus, u_int dev, u_int func)
427 1.1 thorpej {
428 1.1 thorpej pcireg_t id, class;
429 1.1 thorpej char devinfo[256];
430 1.1 thorpej
431 1.1 thorpej if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
432 1.1 thorpej return;
433 1.1 thorpej if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
434 1.1 thorpej return;
435 1.1 thorpej
436 1.10 hubertf printf("%03u:%02u:%01u: ", bus, dev, func);
437 1.10 hubertf if (print_numbers) {
438 1.11 hubertf printf("0x%08x (0x%08x)\n", id, class);
439 1.10 hubertf } else {
440 1.10 hubertf pci_devinfo(id, class, 1, devinfo, sizeof(devinfo));
441 1.10 hubertf printf("%s\n", devinfo);
442 1.10 hubertf }
443 1.1 thorpej }
444 1.1 thorpej
445 1.16 cegger static void
446 1.1 thorpej scan_pci_dump(u_int bus, u_int dev, u_int func)
447 1.1 thorpej {
448 1.1 thorpej
449 1.1 thorpej pci_conf_print(pcifd, bus, dev, func);
450 1.1 thorpej }
451