2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
29 * $FreeBSD: src/tools/tools/ath/athregs/dumpregs.c,v 1.1 2008/12/07 19:17:33 sam Exp $
34 #include "ah_internal.h"
35 /* XXX cheat, 5212 has a superset of the key table defs */
36 #include "ar5212/ar5212reg.h"
47 u_int32_t regdata[0xffff / sizeof(u_int32_t)];
48 #define MAXREGS 5*1024
49 struct dumpreg *regs[MAXREGS];
54 static dumpregs_t state;
57 #define OS_REG_READ(ah, off) state.regdata[(off) >> 2]
59 static int ath_hal_anyregs(int what);
60 static int ath_hal_setupregs(struct ath_diag *atd, int what);
61 static u_int ath_hal_setupdiagregs(const HAL_REGRANGE regs[], u_int nr);
62 static void ath_hal_dumpregs(FILE *fd, int what);
63 static void ath_hal_dumprange(FILE *fd, u_int a, u_int b);
64 static void ath_hal_dumpkeycache(FILE *fd, int nkeys);
65 static void ath_hal_dumpint(FILE *fd, int what);
66 static void ath_hal_dumpqcu(FILE *fd, int what);
67 static void ath_hal_dumpdcu(FILE *fd, int what);
68 static void ath_hal_dumpbb(FILE *fd, int what);
73 fprintf(stderr, "usage: athregs [-i interface] [-abdkmqxz]\n");
74 fprintf(stderr, "-a display all registers\n");
75 fprintf(stderr, "-b display baseband registers\n");
76 fprintf(stderr, "-d display DCU registers\n");
77 fprintf(stderr, "-k display key cache registers\n");
78 fprintf(stderr, "-m display \"MAC\" registers (default)\n");
79 fprintf(stderr, "-q display QCU registers\n");
80 fprintf(stderr, "-x display XR registers\n");
81 fprintf(stderr, "-z display interrupt registers\n");
82 fprintf(stderr, "\n");
83 fprintf(stderr, "-A display register address\n");
84 fprintf(stderr, "-N suppress display of register name\n");
89 main(int argc, char *argv[])
97 s = socket(AF_INET, SOCK_DGRAM, 0);
100 ifname = getenv("ATH");
102 ifname = ATH_DEFAULT;
105 state.show_addrs = 0;
106 state.show_names = 1;
107 while ((c = getopt(argc, argv, "i:aAbdkmNqxz")) != -1)
113 state.show_addrs = 1;
116 what |= DUMP_BASEBAND;
122 what |= DUMP_KEYCACHE;
131 state.show_names = 0;
140 what |= DUMP_INTERRUPT;
146 strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
153 atd.ad_id = HAL_DIAG_REVS;
154 atd.ad_out_data = (caddr_t) &state.revs;
155 atd.ad_out_size = sizeof(state.revs);
156 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
159 if (ath_hal_setupregs(&atd, what) == 0)
160 errx(-1, "no registers are known for this part "
161 "(devid 0x%x mac %d.%d phy %d)", state.revs.ah_devid,
162 state.revs.ah_macVersion, state.revs.ah_macRev,
163 state.revs.ah_phyRev);
165 atd.ad_out_size = ath_hal_setupdiagregs((HAL_REGRANGE *) atd.ad_in_data,
166 atd.ad_in_size / sizeof(HAL_REGRANGE));
167 atd.ad_out_data = (caddr_t) malloc(atd.ad_out_size);
168 if (atd.ad_out_data == NULL) {
169 fprintf(stderr, "Cannot malloc output buffer, size %u\n",
173 atd.ad_id = HAL_DIAG_REGS | ATH_DIAG_IN | ATH_DIAG_DYN;
174 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
178 * Expand register data into global space that can be
179 * indexed directly by register offset.
181 dp = (u_int32_t *)atd.ad_out_data;
182 ep = (u_int32_t *)(atd.ad_out_data + atd.ad_out_size);
184 u_int r = dp[0] >> 16; /* start of range */
185 u_int e = dp[0] & 0xffff; /* end of range */
187 /* convert offsets to indices */
191 fprintf(stderr, "Warning, botched return data;"
192 "register at offset 0x%x not present\n",
196 state.regdata[r++] = *dp++;
200 if (what & DUMP_BASIC)
201 ath_hal_dumpregs(stdout, DUMP_BASIC);
202 if ((what & DUMP_INTERRUPT) && ath_hal_anyregs(DUMP_INTERRUPT)) {
203 if (what & DUMP_BASIC)
205 if (state.show_addrs)
206 ath_hal_dumpregs(stdout, DUMP_INTERRUPT);
208 ath_hal_dumpint(stdout, what);
210 if ((what & DUMP_QCU) && ath_hal_anyregs(DUMP_QCU)) {
211 if (what & (DUMP_BASIC|DUMP_INTERRUPT))
213 if (state.show_addrs)
214 ath_hal_dumpregs(stdout, DUMP_QCU);
216 ath_hal_dumpqcu(stdout, what);
218 if ((what & DUMP_DCU) && ath_hal_anyregs(DUMP_DCU)) {
219 if (what & (DUMP_BASIC|DUMP_INTERRUPT|DUMP_QCU))
221 if (state.show_addrs)
222 ath_hal_dumpregs(stdout, DUMP_DCU);
224 ath_hal_dumpdcu(stdout, what);
226 if (what & DUMP_KEYCACHE) {
227 if (state.show_addrs) {
228 if (what & (DUMP_BASIC|DUMP_INTERRUPT|DUMP_QCU|DUMP_DCU))
230 ath_hal_dumpregs(stdout, DUMP_KEYCACHE);
232 ath_hal_dumpkeycache(stdout, 128);
234 if (what & DUMP_BASEBAND) {
235 if (what &~ DUMP_BASEBAND)
236 fprintf(stdout, "\n");
237 ath_hal_dumpbb(stdout, what);
243 regcompar(const void *a, const void *b)
245 const struct dumpreg *ra = *(const struct dumpreg **)a;
246 const struct dumpreg *rb = *(const struct dumpreg **)b;
247 return ra->addr - rb->addr;
251 register_regs(struct dumpreg *chipregs, u_int nchipregs,
252 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
254 const int existing_regs = state.nregs;
257 for (i = 0; i < nchipregs; i++) {
258 struct dumpreg *nr = &chipregs[i];
259 if (nr->srevMin == 0)
260 nr->srevMin = def_srev_min;
261 if (nr->srevMax == 0)
262 nr->srevMax = def_srev_max;
264 nr->phyMin = def_phy_min;
266 nr->phyMax = def_phy_max;
267 for (j = 0; j < existing_regs; j++) {
268 struct dumpreg *r = state.regs[j];
270 * Check if we can just expand the mac+phy
271 * coverage for the existing entry.
273 if (nr->addr == r->addr &&
274 (nr->name == r->name ||
275 nr->name != NULL && r->name != NULL &&
276 strcmp(nr->name, r->name) == 0)) {
277 if (nr->srevMin < r->srevMin &&
278 (r->srevMin <= nr->srevMax &&
279 nr->srevMax+1 <= r->srevMax)) {
280 r->srevMin = nr->srevMin;
283 if (nr->srevMax > r->srevMax &&
284 (r->srevMin <= nr->srevMin &&
285 nr->srevMin <= r->srevMax)) {
286 r->srevMax = nr->srevMax;
290 if (r->addr > nr->addr)
294 * New item, add to the end, it'll be sorted below.
296 if (state.nregs == MAXREGS)
297 errx(-1, "too many registers; bump MAXREGS");
298 state.regs[state.nregs++] = nr;
302 qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar);
306 register_keycache(u_int nslots,
307 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
309 #define SET(r, a) do { \
310 r->addr = a; r->type = DUMP_KEYCACHE; r++; \
312 struct dumpreg *keyregs, *r;
315 keyregs = (struct dumpreg *) calloc(nslots, 8*sizeof(struct dumpreg));
317 errx(-1, "no space to %d keycache slots\n", nslots);
319 for (i = 0; i < nslots; i++) {
320 SET(r, AR_KEYTABLE_KEY0(i));
321 SET(r, AR_KEYTABLE_KEY1(i));
322 SET(r, AR_KEYTABLE_KEY2(i));
323 SET(r, AR_KEYTABLE_KEY3(i));
324 SET(r, AR_KEYTABLE_KEY4(i));
325 SET(r, AR_KEYTABLE_TYPE(i));
326 SET(r, AR_KEYTABLE_MAC0(i));
327 SET(r, AR_KEYTABLE_MAC1(i));
329 register_regs(keyregs, 8*nslots,
330 def_srev_min, def_srev_max, def_phy_min, def_phy_max);
335 register_range(u_int brange, u_int erange, int type,
336 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
338 struct dumpreg *bbregs, *r;
341 nregs = (erange - brange) / sizeof(uint32_t);
342 bbregs = (struct dumpreg *) calloc(nregs, sizeof(struct dumpreg));
344 errx(-1, "no space for %d register slots (type %d)\n",
347 for (i = 0; i < nregs; i++) {
348 r->addr = brange + (i<<2);
352 register_regs(bbregs, nregs,
353 def_srev_min, def_srev_max, def_phy_min, def_phy_max);
357 match(const struct dumpreg *dr, const HAL_REVS *revs)
359 if (!MAC_MATCH(dr, revs->ah_macVersion, revs->ah_macRev))
361 if ((dr->type & DUMP_BASEBAND) && !PHY_MATCH(dr, revs->ah_phyRev))
367 ath_hal_anyregs(int what)
369 const HAL_REVS *revs = &state.revs;
372 for (i = 0; i < state.nregs; i++) {
373 const struct dumpreg *dr = state.regs[i];
374 if ((what & dr->type) && match(dr, revs))
381 ath_hal_setupregs(struct ath_diag *atd, int what)
383 const HAL_REVS *revs = &state.revs;
390 for (i = 0; i < state.nregs; i++) {
391 const struct dumpreg *dr = state.regs[i];
392 if ((what & dr->type) && match(dr, revs)) {
393 if (erun + 4 != dr->addr) {
395 space += sizeof(HAL_REGRANGE);
396 brun = erun = dr->addr;
401 space += sizeof(HAL_REGRANGE);
403 atd->ad_in_data = (caddr_t) malloc(space);
404 if (atd->ad_in_data == NULL) {
405 fprintf(stderr, "Cannot malloc memory for registers!\n");
408 atd->ad_in_size = space;
409 cp = (u_int8_t *) atd->ad_in_data;
411 for (i = 0; i < state.nregs; i++) {
412 const struct dumpreg *dr = state.regs[i];
413 if ((what & dr->type) && match(dr, revs)) {
414 if (erun + 4 != dr->addr) {
416 r.start = brun, r.end = erun;
417 memcpy(cp, &r, sizeof(r));
420 brun = erun = dr->addr;
426 r.start = brun, r.end = erun;
427 memcpy(cp, &r, sizeof(r));
430 return space / sizeof(uint32_t);
434 ath_hal_dumpregs(FILE *fd, int what)
436 const HAL_REVS *revs = &state.revs;
437 const char *sep = "";
438 int i, count, itemsperline;
442 if (state.show_names && state.show_addrs)
444 for (i = 0; i < state.nregs; i++) {
445 const struct dumpreg *dr = state.regs[i];
446 if ((what & dr->type) && match(dr, revs)) {
447 if (state.show_names && dr->name != NULL) {
448 fprintf(fd, "%s%-8s", sep, dr->name);
449 if (state.show_addrs)
450 fprintf(fd, " [%04x]", dr->addr);
452 fprintf(fd, "%s%04x", sep, dr->addr);
453 fprintf(fd, " %08x", OS_REG_READ(ah, dr->addr));
455 if ((++count % itemsperline) == 0)
464 ath_hal_dumprange(FILE *fd, u_int a, u_int b)
468 for (r = a; r+16 <= b; r += 5*4)
470 "%04x %08x %04x %08x %04x %08x %04x %08x %04x %08x\n"
471 , r, OS_REG_READ(ah, r)
472 , r+4, OS_REG_READ(ah, r+4)
473 , r+8, OS_REG_READ(ah, r+8)
474 , r+12, OS_REG_READ(ah, r+12)
475 , r+16, OS_REG_READ(ah, r+16)
480 , "%04x %08x %04x %08x %04x %08x %04x %08x\n"
481 , r, OS_REG_READ(ah, r)
482 , r+4, OS_REG_READ(ah, r+4)
483 , r+8, OS_REG_READ(ah, r+8)
484 , r+12, OS_REG_READ(ah, r+12)
488 fprintf(fd, "%04x %08x %04x %08x %04x %08x\n"
489 , r, OS_REG_READ(ah, r)
490 , r+4, OS_REG_READ(ah, r+4)
491 , r+8, OS_REG_READ(ah, r+8)
495 fprintf(fd, "%04x %08x %04x %08x\n"
496 , r, OS_REG_READ(ah, r)
497 , r+4, OS_REG_READ(ah, r+4)
501 fprintf(fd, "%04x %08x\n"
502 , r, OS_REG_READ(ah, r)
509 ath_hal_dumpint(FILE *fd, int what)
513 /* Interrupt registers */
514 fprintf(fd, "IMR: %08x S0 %08x S1 %08x S2 %08x S3 %08x S4 %08x\n"
515 , OS_REG_READ(ah, AR_IMR)
516 , OS_REG_READ(ah, AR_IMR_S0)
517 , OS_REG_READ(ah, AR_IMR_S1)
518 , OS_REG_READ(ah, AR_IMR_S2)
519 , OS_REG_READ(ah, AR_IMR_S3)
520 , OS_REG_READ(ah, AR_IMR_S4)
522 fprintf(fd, "ISR: %08x S0 %08x S1 %08x S2 %08x S3 %08x S4 %08x\n"
523 , OS_REG_READ(ah, AR_ISR)
524 , OS_REG_READ(ah, AR_ISR_S0)
525 , OS_REG_READ(ah, AR_ISR_S1)
526 , OS_REG_READ(ah, AR_ISR_S2)
527 , OS_REG_READ(ah, AR_ISR_S3)
528 , OS_REG_READ(ah, AR_ISR_S4)
533 ath_hal_dumpqcu(FILE *fd, int what)
538 fprintf(fd, "%-8s %08x %-8s %08x %-8s %08x\n"
539 , "Q_TXE", OS_REG_READ(ah, AR_Q_TXE)
540 , "Q_TXD", OS_REG_READ(ah, AR_Q_TXD)
541 , "Q_RDYTIMSHD", OS_REG_READ(ah, AR_Q_RDYTIMESHDN)
543 fprintf(fd, "Q_ONESHOTARM_SC %08x Q_ONESHOTARM_CC %08x\n"
544 , OS_REG_READ(ah, AR_Q_ONESHOTARM_SC)
545 , OS_REG_READ(ah, AR_Q_ONESHOTARM_CC)
547 for (i = 0; i < 10; i++)
548 fprintf(fd, "Q[%u] TXDP %08x CBR %08x RDYT %08x MISC %08x STS %08x\n"
550 , OS_REG_READ(ah, AR_QTXDP(i))
551 , OS_REG_READ(ah, AR_QCBRCFG(i))
552 , OS_REG_READ(ah, AR_QRDYTIMECFG(i))
553 , OS_REG_READ(ah, AR_QMISC(i))
554 , OS_REG_READ(ah, AR_QSTS(i))
559 ath_hal_dumpdcu(FILE *fd, int what)
564 for (i = 0; i < 10; i++)
565 fprintf(fd, "D[%u] MASK %08x IFS %08x RTRY %08x CHNT %08x MISC %06x\n"
567 , OS_REG_READ(ah, AR_DQCUMASK(i))
568 , OS_REG_READ(ah, AR_DLCL_IFS(i))
569 , OS_REG_READ(ah, AR_DRETRY_LIMIT(i))
570 , OS_REG_READ(ah, AR_DCHNTIME(i))
571 , OS_REG_READ(ah, AR_DMISC(i))
576 ath_hal_dumpbb(FILE *fd, int what)
578 const HAL_REVS *revs = &state.revs;
582 for (i = 0; i < state.nregs; i++) {
583 const struct dumpreg *dr = state.regs[i];
584 if (!match(dr, revs))
586 if (dr->type & DUMP_BASEBAND) {
588 brun = erun = dr->addr;
589 } else if (dr->addr == erun + sizeof(uint32_t)) {
592 ath_hal_dumprange(fd, brun, erun);
593 brun = erun = dr->addr;
597 ath_hal_dumprange(fd, brun, erun);
602 ath_hal_dumprange(fd, brun, erun);
606 ath_hal_setupdiagregs(const HAL_REGRANGE regs[], u_int nr)
612 for (i = 0; i < nr; i++) {
613 u_int n = 2 * sizeof(u_int32_t); /* reg range + first */
615 if (regs[i].end < regs[i].start) {
616 fprintf(stderr, "%s: bad register range, "
617 "end 0x%x < start 0x%x\n",
618 __func__, regs[i].end, regs[i].end);
621 n += regs[i].end - regs[i].start;
629 * Format an Ethernet MAC for printing.
632 ether_sprintf(const u_int8_t *mac)
634 static char etherbuf[18];
635 snprintf(etherbuf, sizeof(etherbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
636 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
641 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
642 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
643 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
644 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
648 ath_hal_dumpkeycache(FILE *fd, int nkeys)
650 static const char *keytypenames[] = {
651 "WEP-40", /* AR_KEYTABLE_TYPE_40 */
652 "WEP-104", /* AR_KEYTABLE_TYPE_104 */
654 "WEP-128", /* AR_KEYTABLE_TYPE_128 */
655 "TKIP", /* AR_KEYTABLE_TYPE_TKIP */
656 "AES-OCB", /* AR_KEYTABLE_TYPE_AES */
657 "AES-CCM", /* AR_KEYTABLE_TYPE_CCM */
658 "CLR", /* AR_KEYTABLE_TYPE_CLR */
660 int micEnabled = SREV(state.revs.ah_macVersion, state.revs.ah_macRev) < SREV(4,8) ? 0 :
661 OS_REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_CRPT_MIC_ENABLE;
662 u_int8_t mac[IEEE80211_ADDR_LEN];
663 u_int8_t ismic[128/NBBY];
667 memset(ismic, 0, sizeof(ismic));
668 for (entry = 0; entry < nkeys; entry++) {
669 u_int32_t macLo, macHi, type;
670 u_int32_t key0, key1, key2, key3, key4;
672 macHi = OS_REG_READ(ah, AR_KEYTABLE_MAC1(entry));
673 if ((macHi & AR_KEYTABLE_VALID) == 0 && isclr(ismic, entry))
675 macLo = OS_REG_READ(ah, AR_KEYTABLE_MAC0(entry));
680 mac[4] = macHi & 0xff;
682 mac[0] = macLo & 0xff;
684 mac[2] = macLo >> 16;
685 mac[3] = macLo >> 24;
686 type = OS_REG_READ(ah, AR_KEYTABLE_TYPE(entry));
687 if ((type & 7) == AR_KEYTABLE_TYPE_TKIP && micEnabled)
688 setbit(ismic, entry+64);
689 key0 = OS_REG_READ(ah, AR_KEYTABLE_KEY0(entry));
690 key1 = OS_REG_READ(ah, AR_KEYTABLE_KEY1(entry));
691 key2 = OS_REG_READ(ah, AR_KEYTABLE_KEY2(entry));
692 key3 = OS_REG_READ(ah, AR_KEYTABLE_KEY3(entry));
693 key4 = OS_REG_READ(ah, AR_KEYTABLE_KEY4(entry));
698 fprintf(fd, "KEY[%03u] MAC %s %-7s %02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x\n"
701 , isset(ismic, entry) ? "MIC" : keytypenames[type & 7]
704 , (key0 >> 16) & 0xff
705 , (key0 >> 24) & 0xff
710 , (key2 >> 16) & 0xff
711 , (key2 >> 24) & 0xff
716 , (key4 >> 16) & 0xff
717 , (key4 >> 24) & 0xff