Update comment to point to the right file (src/sys/bus/pci/pcireg.h).
[dragonfly.git] / usr.sbin / pciconf / pciconf.c
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  * $FreeBSD: src/usr.sbin/pciconf/pciconf.c,v 1.11.2.3 2002/09/17 22:09:15 jdp Exp $
30  * $DragonFly: src/usr.sbin/pciconf/pciconf.c,v 1.4 2004/07/19 09:28:17 asmodai Exp $
31  */
32
33 #include <sys/types.h>
34 #include <sys/fcntl.h>
35
36 #include <err.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/pciio.h>
42 #include <sys/queue.h>
43
44 #include <bus/pci/pcireg.h>
45
46 #include "pathnames.h"
47
48 struct pci_device_info 
49 {
50     TAILQ_ENTRY(pci_device_info)        link;
51     int                                 id;
52     char                                *desc;
53 };
54
55 struct pci_vendor_info
56 {
57     TAILQ_ENTRY(pci_vendor_info)        link;
58     TAILQ_HEAD(,pci_device_info)        devs;
59     int                                 id;
60     char                                *desc;
61 };
62
63 TAILQ_HEAD(,pci_vendor_info)    pci_vendors;
64
65 static void list_devs(int vendors);
66 static void list_verbose(struct pci_conf *p);
67 static char *guess_class(struct pci_conf *p);
68 static char *guess_subclass(struct pci_conf *p);
69 static int load_vendors(void);
70 static void readit(const char *, const char *, int);
71 static void writeit(const char *, const char *, const char *, int);
72 static void chkattached(const char *, int);
73
74 static int exitstatus = 0;
75
76 static void
77 usage()
78 {
79         fprintf(stderr, "%s\n%s\n%s\n%s\n",
80                 "usage: pciconf -l [-v]",
81                 "       pciconf -a selector",
82                 "       pciconf -r [-b | -h] selector addr[:addr2]",
83                 "       pciconf -w [-b | -h] selector addr value");
84         exit (1);
85 }
86
87 int
88 main(int argc, char **argv)
89 {
90         int c;
91         int listmode, readmode, writemode, attachedmode, verbose;
92         int byte, isshort;
93
94         listmode = readmode = writemode = attachedmode = verbose = byte = isshort = 0;
95
96         while ((c = getopt(argc, argv, "alrwbhv")) != -1) {
97                 switch(c) {
98                 case 'a':
99                         attachedmode = 1;
100                         break;
101
102                 case 'l':
103                         listmode = 1;
104                         break;
105
106                 case 'r':
107                         readmode = 1;
108                         break;
109                         
110                 case 'w':
111                         writemode = 1;
112                         break;
113
114                 case 'b':
115                         byte = 1;
116                         break;
117
118                 case 'h':
119                         isshort = 1;
120                         break;
121
122                 case 'v':
123                         verbose = 1;
124                         break;
125
126                 default:
127                         usage();
128                 }
129         }
130
131         if ((listmode && optind != argc)
132             || (writemode && optind + 3 != argc)
133             || (readmode && optind + 2 != argc)
134             || (attachedmode && optind + 1 != argc))
135                 usage();
136
137         if (listmode) {
138                 list_devs(verbose);
139         } else if(attachedmode) {
140                 chkattached(argv[optind], 
141                        byte ? 1 : isshort ? 2 : 4);
142         } else if(readmode) {
143                 readit(argv[optind], argv[optind + 1], 
144                        byte ? 1 : isshort ? 2 : 4);
145         } else if(writemode) {
146                 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
147                        byte ? 1 : isshort ? 2 : 4);
148         } else {
149                 usage();
150         }
151
152         return exitstatus;
153 }
154
155 static void
156 list_devs(int verbose)
157 {
158         int fd;
159         struct pci_conf_io pc;
160         struct pci_conf conf[255], *p;
161         int none_count = 0;
162
163         if (verbose)
164                 load_vendors();
165
166         fd = open(_PATH_DEVPCI, O_RDWR, 0);
167         if (fd < 0)
168                 err(1, "%s", _PATH_DEVPCI);
169
170         bzero(&pc, sizeof(struct pci_conf_io));
171         pc.match_buf_len = sizeof(conf);
172         pc.matches = conf;
173
174         do {
175                 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
176                         err(1, "ioctl(PCIOCGETCONF)");
177
178                 /*
179                  * 255 entries should be more than enough for most people,
180                  * but if someone has more devices, and then changes things
181                  * around between ioctls, we'll do the cheezy thing and
182                  * just bail.  The alternative would be to go back to the
183                  * beginning of the list, and print things twice, which may
184                  * not be desireable.
185                  */
186                 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
187                         warnx("PCI device list changed, please try again");
188                         exitstatus = 1;
189                         close(fd);
190                         return;
191                 } else if (pc.status ==  PCI_GETCONF_ERROR) {
192                         warnx("error returned from PCIOCGETCONF ioctl");
193                         exitstatus = 1;
194                         close(fd);
195                         return;
196                 }
197                 for (p = conf; p < &conf[pc.num_matches]; p++) {
198
199                         printf("%s%d@pci%d:%d:%d:\tclass=0x%06x card=0x%08x "
200                                "chip=0x%08x rev=0x%02x hdr=0x%02x\n", 
201                                (p->pd_name && *p->pd_name) ? p->pd_name :
202                                "none",
203                                (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
204                                none_count++,
205                                p->pc_sel.pc_bus, p->pc_sel.pc_dev, 
206                                p->pc_sel.pc_func, (p->pc_class << 16) |
207                                (p->pc_subclass << 8) | p->pc_progif,
208                                (p->pc_subdevice << 16) | p->pc_subvendor,
209                                (p->pc_device << 16) | p->pc_vendor,
210                                p->pc_revid, p->pc_hdr);
211                         if (verbose)
212                                 list_verbose(p);
213                 }
214         } while (pc.status == PCI_GETCONF_MORE_DEVS);
215
216         close(fd);
217 }
218
219 static void
220 list_verbose(struct pci_conf *p)
221 {
222         struct pci_vendor_info  *vi;
223         struct pci_device_info  *di;
224         char *dp;
225         
226         TAILQ_FOREACH(vi, &pci_vendors, link) {
227                 if (vi->id == p->pc_vendor) {
228                         printf("    vendor   = '%s'\n", vi->desc);
229                         break;
230                 }
231         }
232         if (vi == NULL) {
233                 di = NULL;
234         } else {
235                 TAILQ_FOREACH(di, &vi->devs, link) {
236                         if (di->id == p->pc_device) {
237                                 printf("    device   = '%s'\n", di->desc);
238                                 break;
239                         }
240                 }
241         }
242         if ((dp = guess_class(p)) != NULL)
243                 printf("    class    = %s\n", dp);
244         if ((dp = guess_subclass(p)) != NULL)
245                 printf("    subclass = %s\n", dp);
246 }
247
248 /*
249  * This is a direct cut-and-paste from the table in sys/bus/pci/pcireg.h.
250  */
251 static struct
252 {
253         int     class;
254         int     subclass;
255         char    *desc;
256 } pci_nomatch_tab[] = {
257         {PCIC_OLD,              -1,                     "old"},
258         {PCIC_OLD,              PCIS_OLD_NONVGA,        "non-VGA display device"},
259         {PCIC_OLD,              PCIS_OLD_VGA,           "VGA-compatible display device"},
260         {PCIC_STORAGE,          -1,                     "mass storage"},
261         {PCIC_STORAGE,          PCIS_STORAGE_SCSI,      "SCSI"},
262         {PCIC_STORAGE,          PCIS_STORAGE_IDE,       "ATA"},
263         {PCIC_STORAGE,          PCIS_STORAGE_FLOPPY,    "floppy disk"},
264         {PCIC_STORAGE,          PCIS_STORAGE_IPI,       "IPI"},
265         {PCIC_STORAGE,          PCIS_STORAGE_RAID,      "RAID"},
266         {PCIC_NETWORK,          -1,                     "network"},
267         {PCIC_NETWORK,          PCIS_NETWORK_ETHERNET,  "ethernet"},
268         {PCIC_NETWORK,          PCIS_NETWORK_TOKENRING, "token ring"},
269         {PCIC_NETWORK,          PCIS_NETWORK_FDDI,      "fddi"},
270         {PCIC_NETWORK,          PCIS_NETWORK_ATM,       "ATM"},
271         {PCIC_DISPLAY,          -1,                     "display"},
272         {PCIC_DISPLAY,          PCIS_DISPLAY_VGA,       "VGA"},
273         {PCIC_DISPLAY,          PCIS_DISPLAY_XGA,       "XGA"},
274         {PCIC_MULTIMEDIA,       -1,                     "multimedia"},
275         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_VIDEO,  "video"},
276         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_AUDIO,  "audio"},
277         {PCIC_MEMORY,           -1,                     "memory"},
278         {PCIC_MEMORY,           PCIS_MEMORY_RAM,        "RAM"},
279         {PCIC_MEMORY,           PCIS_MEMORY_FLASH,      "flash"},
280         {PCIC_BRIDGE,           -1,                     "bridge"},
281         {PCIC_BRIDGE,           PCIS_BRIDGE_HOST,       "HOST-PCI"},
282         {PCIC_BRIDGE,           PCIS_BRIDGE_ISA,        "PCI-ISA"},
283         {PCIC_BRIDGE,           PCIS_BRIDGE_EISA,       "PCI-EISA"},
284         {PCIC_BRIDGE,           PCIS_BRIDGE_MCA,        "PCI-MCA"},
285         {PCIC_BRIDGE,           PCIS_BRIDGE_PCI,        "PCI-PCI"},
286         {PCIC_BRIDGE,           PCIS_BRIDGE_PCMCIA,     "PCI-PCMCIA"},
287         {PCIC_BRIDGE,           PCIS_BRIDGE_NUBUS,      "PCI-NuBus"},
288         {PCIC_BRIDGE,           PCIS_BRIDGE_CARDBUS,    "PCI-CardBus"},
289         {PCIC_BRIDGE,           PCIS_BRIDGE_OTHER,      "PCI-unknown"},
290         {PCIC_SIMPLECOMM,       -1,                     "simple comms"},
291         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_UART,   "UART"},        /* could detect 16550 */
292         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_PAR,    "parallel port"},
293         {PCIC_BASEPERIPH,       -1,                     "base peripheral"},
294         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PIC,    "interrupt controller"},
295         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_DMA,    "DMA controller"},
296         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_TIMER,  "timer"},
297         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_RTC,    "realtime clock"},
298         {PCIC_INPUTDEV,         -1,                     "input device"},
299         {PCIC_INPUTDEV,         PCIS_INPUTDEV_KEYBOARD, "keyboard"},
300         {PCIC_INPUTDEV,         PCIS_INPUTDEV_DIGITIZER,"digitizer"},
301         {PCIC_INPUTDEV,         PCIS_INPUTDEV_MOUSE,    "mouse"},
302         {PCIC_DOCKING,          -1,                     "docking station"},
303         {PCIC_PROCESSOR,        -1,                     "processor"},
304         {PCIC_SERIALBUS,        -1,                     "serial bus"},
305         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FW,      "FireWire"},
306         {PCIC_SERIALBUS,        PCIS_SERIALBUS_ACCESS,  "AccessBus"},    
307         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SSA,     "SSA"},
308         {PCIC_SERIALBUS,        PCIS_SERIALBUS_USB,     "USB"},
309         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FC,      "Fibre Channel"},
310         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SMBUS,   "SMBus"},
311         {0, 0,          NULL}
312 };
313
314 static char *
315 guess_class(struct pci_conf *p)
316 {
317         int     i;
318
319         for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
320                 if (pci_nomatch_tab[i].class == p->pc_class)
321                         return(pci_nomatch_tab[i].desc);
322         }
323         return(NULL);
324 }
325
326 static char *
327 guess_subclass(struct pci_conf *p)
328 {
329         int     i;
330
331         for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
332                 if ((pci_nomatch_tab[i].class == p->pc_class) &&
333                     (pci_nomatch_tab[i].subclass == p->pc_subclass))
334                         return(pci_nomatch_tab[i].desc);
335         }
336         return(NULL);
337 }
338
339 static int
340 load_vendors(void)
341 {
342         char *dbf;
343         FILE *db;
344         struct pci_vendor_info *cv;
345         struct pci_device_info *cd;
346         char buf[100], str[100];
347         int id, error;
348
349         /*
350          * Locate the database and initialise.
351          */
352         TAILQ_INIT(&pci_vendors);
353         if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
354                 dbf = _PATH_PCIVDB;
355         if ((db = fopen(dbf, "r")) == NULL)
356                 return(1);
357         cv = NULL;
358         cd = NULL;
359         error = 0;
360
361         /*
362          * Scan input lines from the database
363          */
364         for (;;) {
365                 if (fgets(buf, sizeof(buf), db) == NULL)
366                         break;
367
368                 /* Check for vendor entry */
369                 if ((buf[0] != '\t') && (sscanf(buf, "%04x\t%[^\n]", &id, str) == 2)) {
370                         if ((id == 0) || (strlen(str) < 1))
371                                 continue;
372                         if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
373                                 warn("allocating vendor entry");
374                                 error = 1;
375                                 break;
376                         }
377                         if ((cv->desc = strdup(str)) == NULL) {
378                                 free(cv);
379                                 warn("allocating vendor description");
380                                 error = 1;
381                                 break;
382                         }
383                         cv->id = id;
384                         TAILQ_INIT(&cv->devs);
385                         TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
386                         continue;
387                 }
388                 
389                 /* Check for device entry */
390                 if ((buf[0] == '\t') && (sscanf(buf + 1, "%04x\t%[^\n]", &id, str) == 2)) {
391                         if ((id == 0) || (strlen(str) < 1))
392                                 continue;
393                         if (cv == NULL) {
394                                 warnx("device entry with no vendor!");
395                                 continue;
396                         }
397                         if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
398                                 warn("allocating device entry");
399                                 error = 1;
400                                 break;
401                         }
402                         if ((cd->desc = strdup(str)) == NULL) {
403                                 free(cd);
404                                 warn("allocating device description");
405                                 error = 1;
406                                 break;
407                         }
408                         cd->id = id;
409                         TAILQ_INSERT_TAIL(&cv->devs, cd, link);
410                         continue;
411                 }
412
413                 /* It's a comment or junk, ignore it */
414         }
415         if (ferror(db))
416                 error = 1;
417         fclose(db);
418         
419         return(error);
420 }
421
422
423 static struct pcisel
424 getsel(const char *str)
425 {
426         char *ep = (char*) str;
427         struct pcisel sel;
428         
429         if (strncmp(ep, "pci", 3) == 0) {
430                 ep += 3;
431                 sel.pc_bus = strtoul(ep, &ep, 0);
432                 if (!ep || *ep++ != ':')
433                         errx(1, "cannot parse selector %s", str);
434                 sel.pc_dev = strtoul(ep, &ep, 0);
435                 if (!ep || *ep != ':') {
436                         sel.pc_func = 0;
437                 } else {
438                         ep++;
439                         sel.pc_func = strtoul(ep, &ep, 0);
440                 }
441         }
442         if (*ep == ':')
443                 ep++;
444         if (*ep || ep == str)
445                 errx(1, "cannot parse selector %s", str);
446         return sel;
447 }
448
449 static void
450 readone(int fd, struct pcisel *sel, long reg, int width)
451 {
452         struct pci_io pi;
453
454         pi.pi_sel = *sel;
455         pi.pi_reg = reg;
456         pi.pi_width = width;
457
458         if (ioctl(fd, PCIOCREAD, &pi) < 0)
459                 err(1, "ioctl(PCIOCREAD)");
460
461         printf("0x%08x", pi.pi_data);
462 }
463
464 static void
465 readit(const char *name, const char *reg, int width)
466 {
467         long rstart;
468         long rend;
469         long r;
470         char *end;
471         int i;
472         int fd;
473         struct pcisel sel;
474
475         fd = open(_PATH_DEVPCI, O_RDWR, 0);
476         if (fd < 0)
477                 err(1, "%s", _PATH_DEVPCI);
478
479         rend = rstart = strtol(reg, &end, 0);
480         if (end && *end == ':') {
481                 end++;
482                 rend = strtol(end, (char **) 0, 0);
483         }
484         sel = getsel(name);
485         for (i = 1, r = rstart; r <= rend; i++, r += width) {   
486                 readone(fd, &sel, r, width);
487                 putchar(i % 4 ? ' ' : '\n');
488         }
489         if (i % 4 != 1)
490                 putchar('\n');
491         close(fd);
492 }
493
494 static void
495 writeit(const char *name, const char *reg, const char *data, int width)
496 {
497         int fd;
498         struct pci_io pi;
499
500         pi.pi_sel = getsel(name);
501         pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
502         pi.pi_width = width;
503         pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
504
505         fd = open(_PATH_DEVPCI, O_RDWR, 0);
506         if (fd < 0)
507                 err(1, "%s", _PATH_DEVPCI);
508
509         if (ioctl(fd, PCIOCWRITE, &pi) < 0)
510                 err(1, "ioctl(PCIOCWRITE)");
511 }
512
513 static void
514 chkattached (const char *name, int width)
515 {
516         int fd;
517         struct pci_io pi;
518
519         pi.pi_sel = getsel(name);
520         pi.pi_reg = 0;
521         pi.pi_width = width;
522         pi.pi_data = 0;
523
524         fd = open(_PATH_DEVPCI, O_RDWR, 0);
525         if (fd < 0)
526                 err(1, "%s", _PATH_DEVPCI);
527
528         if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
529                 err(1, "ioctl(PCIOCATTACHED)");
530
531         exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
532         printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
533 }