pcictl.c revision 1.20 1 /* $NetBSD: pcictl.c,v 1.20 2014/08/31 09:59:08 wiz Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
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.
24 *
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.
36 */
37
38 /*
39 * pcictl(8) -- a program to manipulate the PCI bus
40 */
41
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>
54
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcidevs.h>
57 #include <dev/pci/pciio.h>
58
59 struct command {
60 const char *cmd_name;
61 const char *arg_names;
62 void (*cmd_func)(int, char *[]);
63 int open_flags;
64 };
65
66 __dead static void usage(void);
67
68 static int pcifd;
69
70 static struct pciio_businfo pci_businfo;
71
72 static const char *dvname;
73 static char dvname_store[MAXPATHLEN];
74 static const char *cmdname;
75 static int print_numbers = 0;
76 static int print_names = 0;
77
78 static void cmd_list(int, char *[]);
79 static void cmd_dump(int, char *[]);
80
81 static const struct command commands[] = {
82 { "list",
83 "[-Nn] [-b bus] [-d device] [-f function]",
84 cmd_list,
85 O_RDONLY },
86
87 { "dump",
88 "[-b bus] -d device [-f function]",
89 cmd_dump,
90 O_RDONLY },
91
92 { 0, 0, 0, 0 },
93 };
94
95 static int parse_bdf(const char *);
96
97 static void scan_pci(int, int, int, void (*)(u_int, u_int, u_int));
98
99 static void scan_pci_list(u_int, u_int, u_int);
100 static void scan_pci_dump(u_int, u_int, u_int);
101
102 int
103 main(int argc, char *argv[])
104 {
105 int i;
106
107 /* Must have at least: device command */
108 if (argc < 3)
109 usage();
110
111 /* Skip program name, get and skip device name, get command. */
112 dvname = argv[1];
113 cmdname = argv[2];
114 argv += 2;
115 argc -= 2;
116
117 /* Look up and call the command. */
118 for (i = 0; commands[i].cmd_name != NULL; i++)
119 if (strcmp(cmdname, commands[i].cmd_name) == 0)
120 break;
121 if (commands[i].cmd_name == NULL)
122 errx(EXIT_FAILURE, "unknown command: %s", cmdname);
123
124 /* Open the device. */
125 if ((strchr(dvname, '/') == NULL) &&
126 (snprintf(dvname_store, sizeof(dvname_store), _PATH_DEV "%s",
127 dvname) < (int)sizeof(dvname_store)))
128 dvname = dvname_store;
129 pcifd = open(dvname, commands[i].open_flags);
130 if (pcifd < 0)
131 err(EXIT_FAILURE, "%s", dvname);
132
133 /* Make sure the device is a PCI bus. */
134 if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
135 errx(EXIT_FAILURE, "%s: not a PCI bus device", dvname);
136
137 (*commands[i].cmd_func)(argc, argv);
138 exit(EXIT_SUCCESS);
139 }
140
141 static void
142 usage(void)
143 {
144 int i;
145
146 fprintf(stderr, "usage: %s device command [arg [...]]\n",
147 getprogname());
148
149 fprintf(stderr, " Available commands:\n");
150 for (i = 0; commands[i].cmd_name != NULL; i++)
151 fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
152 commands[i].arg_names);
153
154 exit(EXIT_FAILURE);
155 }
156
157 static void
158 cmd_list(int argc, char *argv[])
159 {
160 int bus, dev, func;
161 int ch;
162
163 bus = -1;
164 dev = func = -1;
165
166 while ((ch = getopt(argc, argv, "b:d:f:Nn")) != -1) {
167 switch (ch) {
168 case 'b':
169 bus = parse_bdf(optarg);
170 break;
171 case 'd':
172 dev = parse_bdf(optarg);
173 break;
174 case 'f':
175 func = parse_bdf(optarg);
176 break;
177 case 'n':
178 print_numbers = 1;
179 break;
180 case 'N':
181 print_names = 1;
182 break;
183 default:
184 usage();
185 }
186 }
187 argv += optind;
188 argc -= optind;
189
190 if (argc != 0)
191 usage();
192
193 scan_pci(bus, dev, func, scan_pci_list);
194 }
195
196 static void
197 cmd_dump(int argc, char *argv[])
198 {
199 int bus, dev, func;
200 int ch;
201
202 bus = pci_businfo.busno;
203 func = 0;
204 dev = -1;
205
206 while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
207 switch (ch) {
208 case 'b':
209 bus = parse_bdf(optarg);
210 break;
211 case 'd':
212 dev = parse_bdf(optarg);
213 break;
214 case 'f':
215 func = parse_bdf(optarg);
216 break;
217 default:
218 usage();
219 }
220 }
221 argv += optind;
222 argc -= optind;
223
224 if (argc != 0)
225 usage();
226
227 if (bus == -1)
228 errx(EXIT_FAILURE, "dump: wildcard bus number not permitted");
229 if (dev == -1)
230 errx(EXIT_FAILURE, "dump: must specify a device number");
231 if (func == -1)
232 errx(EXIT_FAILURE, "dump: wildcard function number not permitted");
233
234 scan_pci(bus, dev, func, scan_pci_dump);
235 }
236
237 static int
238 parse_bdf(const char *str)
239 {
240 long value;
241 char *end;
242
243 if (strcmp(str, "all") == 0 ||
244 strcmp(str, "any") == 0)
245 return (-1);
246
247 value = strtol(str, &end, 0);
248 if (*end != '\0')
249 errx(EXIT_FAILURE, "\"%s\" is not a number", str);
250
251 return value;
252 }
253
254 static void
255 scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
256 {
257 u_int busmin, busmax;
258 u_int devmin, devmax;
259 u_int funcmin, funcmax;
260 u_int bus, dev, func;
261 pcireg_t id, bhlcr;
262
263 if (busarg == -1) {
264 busmin = 0;
265 busmax = 255;
266 } else
267 busmin = busmax = busarg;
268
269 if (devarg == -1) {
270 devmin = 0;
271 if (pci_businfo.maxdevs <= 0)
272 devmax = 0;
273 else
274 devmax = pci_businfo.maxdevs - 1;
275 } else
276 devmin = devmax = devarg;
277
278 for (bus = busmin; bus <= busmax; bus++) {
279 for (dev = devmin; dev <= devmax; dev++) {
280 if (pcibus_conf_read(pcifd, bus, dev, 0,
281 PCI_BHLC_REG, &bhlcr) != 0)
282 continue;
283 if (funcarg == -1) {
284 funcmin = 0;
285 if (PCI_HDRTYPE_MULTIFN(bhlcr))
286 funcmax = 7;
287 else
288 funcmax = 0;
289 } else
290 funcmin = funcmax = funcarg;
291 for (func = funcmin; func <= funcmax; func++) {
292 if (pcibus_conf_read(pcifd, bus, dev,
293 func, PCI_ID_REG, &id) != 0)
294 continue;
295
296 /* Invalid vendor ID value? */
297 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
298 continue;
299 /*
300 * XXX Not invalid, but we've done this
301 * ~forever.
302 */
303 if (PCI_VENDOR(id) == 0)
304 continue;
305
306 (*cb)(bus, dev, func);
307 }
308 }
309 }
310 }
311
312 static void
313 scan_pci_list(u_int bus, u_int dev, u_int func)
314 {
315 pcireg_t id, class;
316 char devinfo[256];
317
318 if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
319 return;
320 if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
321 return;
322
323 printf("%03u:%02u:%01u: ", bus, dev, func);
324 if (print_numbers) {
325 printf("0x%08x (0x%08x)", id, class);
326 } else {
327 pci_devinfo(id, class, 1, devinfo, sizeof(devinfo));
328 printf("%s", devinfo);
329 }
330 if (print_names) {
331 char drvname[16];
332 if (pci_drvname(pcifd, dev, func, drvname, sizeof drvname) == 0)
333 printf(" [%s]", drvname);
334 }
335 printf("\n");
336 }
337
338 static void
339 scan_pci_dump(u_int bus, u_int dev, u_int func)
340 {
341
342 pci_conf_print(pcifd, bus, dev, func);
343 }
344