No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / pcictl / pcictl.c
blob61979697e88e60595746611bb0b63a0e70348556
1 /* $NetBSD: pcictl.c,v 1.15 2009/07/04 13:54:14 cegger Exp $ */
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * pcictl(8) -- a program to manipulate the PCI bus
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <paths.h>
48 #include <pci.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <util.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcidevs.h>
57 #include <dev/pci/pciio.h>
59 struct command {
60 const char *cmd_name;
61 const char *arg_names;
62 void (*cmd_func)(int, char *[]);
63 int open_flags;
66 static void usage(void);
68 int pcifd;
70 struct pciio_businfo pci_businfo;
72 const char *dvname;
73 char dvname_store[MAXPATHLEN];
74 const char *cmdname;
75 int print_numbers = 0;
77 static void cmd_list(int, char *[]);
78 static void cmd_dump(int, char *[]);
80 const struct command commands[] = {
81 { "list",
82 "[-n] [-b bus] [-d device] [-f function]",
83 cmd_list,
84 O_RDONLY },
86 { "dump",
87 "[-b bus] -d device [-f function]",
88 cmd_dump,
89 O_RDONLY },
91 { 0, 0, 0, 0 },
94 static int parse_bdf(const char *);
96 static void scan_pci(int, int, int, void (*)(u_int, u_int, u_int));
98 static void scan_pci_list(u_int, u_int, u_int);
99 static void scan_pci_dump(u_int, u_int, u_int);
102 main(int argc, char *argv[])
104 int i;
106 /* Must have at least: device command */
107 if (argc < 3)
108 usage();
110 /* Skip program name, get and skip device name, get command. */
111 dvname = argv[1];
112 cmdname = argv[2];
113 argv += 2;
114 argc -= 2;
116 /* Look up and call the command. */
117 for (i = 0; commands[i].cmd_name != NULL; i++)
118 if (strcmp(cmdname, commands[i].cmd_name) == 0)
119 break;
120 if (commands[i].cmd_name == NULL)
121 errx(EXIT_FAILURE, "unknown command: %s", cmdname);
123 /* Open the device. */
124 if ((strchr(dvname, '/') == NULL) &&
125 (snprintf(dvname_store, sizeof(dvname_store), _PATH_DEV "%s",
126 dvname) < (int)sizeof(dvname_store)))
127 dvname = dvname_store;
128 pcifd = open(dvname, commands[i].open_flags);
129 if (pcifd < 0)
130 err(EXIT_FAILURE, "%s", dvname);
132 /* Make sure the device is a PCI bus. */
133 if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
134 errx(EXIT_FAILURE, "%s: not a PCI bus device", dvname);
136 (*commands[i].cmd_func)(argc, argv);
137 exit(EXIT_SUCCESS);
140 static void
141 usage(void)
143 int i;
145 fprintf(stderr, "usage: %s device command [arg [...]]\n",
146 getprogname());
148 fprintf(stderr, " Available commands:\n");
149 for (i = 0; commands[i].cmd_name != NULL; i++)
150 fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
151 commands[i].arg_names);
153 exit(EXIT_FAILURE);
156 static void
157 cmd_list(int argc, char *argv[])
159 int bus, dev, func;
160 int ch;
162 bus = pci_businfo.busno;
163 dev = func = -1;
165 while ((ch = getopt(argc, argv, "nb:d:f:")) != -1) {
166 switch (ch) {
167 case 'b':
168 bus = parse_bdf(optarg);
169 break;
170 case 'd':
171 dev = parse_bdf(optarg);
172 break;
173 case 'f':
174 func = parse_bdf(optarg);
175 break;
176 case 'n':
177 print_numbers = 1;
178 break;
179 default:
180 usage();
183 argv += optind;
184 argc -= optind;
186 if (argc != 0)
187 usage();
189 scan_pci(bus, dev, func, scan_pci_list);
192 static void
193 cmd_dump(int argc, char *argv[])
195 int bus, dev, func;
196 int ch;
198 bus = pci_businfo.busno;
199 func = 0;
200 dev = -1;
202 while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
203 switch (ch) {
204 case 'b':
205 bus = parse_bdf(optarg);
206 break;
207 case 'd':
208 dev = parse_bdf(optarg);
209 break;
210 case 'f':
211 func = parse_bdf(optarg);
212 break;
213 default:
214 usage();
217 argv += optind;
218 argc -= optind;
220 if (argc != 0)
221 usage();
223 if (bus == -1)
224 errx(EXIT_FAILURE, "dump: wildcard bus number not permitted");
225 if (dev == -1)
226 errx(EXIT_FAILURE, "dump: must specify a device number");
227 if (func == -1)
228 errx(EXIT_FAILURE, "dump: wildcard function number not permitted");
230 scan_pci(bus, dev, func, scan_pci_dump);
233 static int
234 parse_bdf(const char *str)
236 long value;
237 char *end;
239 if (strcmp(str, "all") == 0 ||
240 strcmp(str, "any") == 0)
241 return (-1);
243 value = strtol(str, &end, 0);
244 if (*end != '\0')
245 errx(EXIT_FAILURE, "\"%s\" is not a number", str);
247 return value;
250 static void
251 scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
253 u_int busmin, busmax;
254 u_int devmin, devmax;
255 u_int funcmin, funcmax;
256 u_int bus, dev, func;
257 pcireg_t id, bhlcr;
259 if (busarg == -1) {
260 busmin = 0;
261 busmax = 255;
262 } else
263 busmin = busmax = busarg;
265 if (devarg == -1) {
266 devmin = 0;
267 if (pci_businfo.maxdevs <= 0)
268 devmax = 0;
269 else
270 devmax = pci_businfo.maxdevs - 1;
271 } else
272 devmin = devmax = devarg;
274 for (bus = busmin; bus <= busmax; bus++) {
275 for (dev = devmin; dev <= devmax; dev++) {
276 if (pcibus_conf_read(pcifd, bus, dev, 0,
277 PCI_BHLC_REG, &bhlcr) != 0)
278 continue;
279 if (funcarg == -1) {
280 funcmin = 0;
281 if (PCI_HDRTYPE_MULTIFN(bhlcr))
282 funcmax = 7;
283 else
284 funcmax = 0;
285 } else
286 funcmin = funcmax = funcarg;
287 for (func = funcmin; func <= funcmax; func++) {
288 if (pcibus_conf_read(pcifd, bus, dev,
289 func, PCI_ID_REG, &id) != 0)
290 continue;
292 /* Invalid vendor ID value? */
293 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
294 continue;
296 * XXX Not invalid, but we've done this
297 * ~forever.
299 if (PCI_VENDOR(id) == 0)
300 continue;
302 (*cb)(bus, dev, func);
308 static void
309 scan_pci_list(u_int bus, u_int dev, u_int func)
311 pcireg_t id, class;
312 char devinfo[256];
314 if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
315 return;
316 if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
317 return;
319 printf("%03u:%02u:%01u: ", bus, dev, func);
320 if (print_numbers) {
321 printf("0x%08x (0x%08x)\n", id, class);
322 } else {
323 pci_devinfo(id, class, 1, devinfo, sizeof(devinfo));
324 printf("%s\n", devinfo);
328 static void
329 scan_pci_dump(u_int bus, u_int dev, u_int func)
332 pci_conf_print(pcifd, bus, dev, func);