main.c revision 189702
1189702Ssam/*-
2189702Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3189702Ssam * All rights reserved.
4189702Ssam *
5189702Ssam * Redistribution and use in source and binary forms, with or without
6189702Ssam * modification, are permitted provided that the following conditions
7189702Ssam * are met:
8189702Ssam * 1. Redistributions of source code must retain the above copyright
9189702Ssam *    notice, this list of conditions and the following disclaimer,
10189702Ssam *    without modification.
11189702Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12189702Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13189702Ssam *    redistribution must be conditioned upon including a substantially
14189702Ssam *    similar Disclaimer requirement for further binary redistribution.
15189702Ssam *
16189702Ssam * NO WARRANTY
17189702Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18189702Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19189702Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20189702Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21189702Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22189702Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23189702Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24189702Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25189702Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26189702Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27189702Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28189702Ssam *
29189702Ssam * $FreeBSD: head/tools/tools/ath/athdecode/main.c 189702 2009-03-11 17:15:33Z sam $
30189702Ssam */
31189702Ssam#include "diag.h"
32189702Ssam
33189702Ssam#include "ah.h"
34189702Ssam#include "ah_internal.h"
35189702Ssam#include "ah_decode.h"
36189702Ssam
37189702Ssam#include "dumpregs.h"
38189702Ssam
39189702Ssam#include <stdlib.h>
40189702Ssam#include <sys/file.h>
41189702Ssam#include <sys/stat.h>
42189702Ssam#include <sys/mman.h>
43189702Ssam
44189702Ssamtypedef struct {
45189702Ssam	HAL_REVS revs;
46189702Ssam	int chipnum;
47189702Ssam#define	MAXREGS	5*1024
48189702Ssam	struct dumpreg *regs[MAXREGS];
49189702Ssam	u_int nregs;
50189702Ssam} dumpregs_t;
51189702Ssamstatic	dumpregs_t state;
52189702Ssam
53189702Ssamstatic void opdevice(const struct athregrec *r);
54189702Ssamstatic const char* opmark(FILE *, int, const struct athregrec *);
55189702Ssamstatic void oprw(FILE *fd, int recnum, struct athregrec *r);
56189702Ssam
57189702Ssamint
58189702Ssammain(int argc, char *argv[])
59189702Ssam{
60189702Ssam	int fd, i, nrecs, same;
61189702Ssam	struct stat sb;
62189702Ssam	void *addr;
63189702Ssam	const char *filename = "/tmp/ath_hal.log";
64189702Ssam	struct athregrec *rprev;
65189702Ssam
66189702Ssam	if (argc > 1)
67189702Ssam		filename = argv[1];
68189702Ssam	fd = open(filename, O_RDONLY);
69189702Ssam	if (fd < 0)
70189702Ssam		err(1, filename);
71189702Ssam	if (fstat(fd, &sb) < 0)
72189702Ssam		err(1, "fstat");
73189702Ssam	addr = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE|MAP_NOCORE, fd, 0);
74189702Ssam	if (addr == MAP_FAILED)
75189702Ssam		err(1, "mmap");
76189702Ssam	nrecs = sb.st_size / sizeof (struct athregrec);
77189702Ssam	printf("%u records", nrecs);
78189702Ssam	rprev = NULL;
79189702Ssam	same = 0;
80189702Ssam	state.chipnum = 5210;
81189702Ssam	for (i = 0; i < nrecs; i++) {
82189702Ssam		struct athregrec *r = &((struct athregrec *) addr)[i];
83189702Ssam		if (rprev && bcmp(r, rprev, sizeof (*r)) == 0) {
84189702Ssam			same++;
85189702Ssam			continue;
86189702Ssam		}
87189702Ssam		if (same)
88189702Ssam			printf("\t\t+%u time%s", same, same == 1 ? "" : "s");
89189702Ssam		switch (r->op) {
90189702Ssam		case OP_DEVICE:
91189702Ssam			opdevice(r);
92189702Ssam			break;
93189702Ssam		case OP_READ:
94189702Ssam		case OP_WRITE:
95189702Ssam			oprw(stdout, i, r);
96189702Ssam			break;
97189702Ssam		case OP_MARK:
98189702Ssam			opmark(stdout, i, r);
99189702Ssam			break;
100189702Ssam		}
101189702Ssam		rprev = r;
102189702Ssam		same = 0;
103189702Ssam	}
104189702Ssam	putchar('\n');
105189702Ssam	return 0;
106189702Ssam}
107189702Ssam
108189702Ssamstatic const char*
109189702Ssamopmark(FILE *fd, int i, const struct athregrec *r)
110189702Ssam{
111189702Ssam	fprintf(fd, "\n%05d: ", i);
112189702Ssam	switch (r->reg) {
113189702Ssam	case AH_MARK_RESET:
114189702Ssam		fprintf(fd, "ar%uReset %s", state.chipnum,
115189702Ssam			r->val ? "change channel" : "no channel change");
116189702Ssam		break;
117189702Ssam	case AH_MARK_RESET_LINE:
118189702Ssam		fprintf(fd, "ar%u_reset.c; line %u", state.chipnum, r->val);
119189702Ssam		break;
120189702Ssam	case AH_MARK_RESET_DONE:
121189702Ssam		if (r->val)
122189702Ssam			fprintf(fd, "ar%uReset (done), FAIL, error %u",
123189702Ssam				state.chipnum, r->val);
124189702Ssam		else
125189702Ssam			fprintf(fd, "ar%uReset (done), OK", state.chipnum);
126189702Ssam		break;
127189702Ssam	case AH_MARK_CHIPRESET:
128189702Ssam		fprintf(fd, "ar%uChipReset, channel %u Mhz", state.chipnum, r->val);
129189702Ssam		break;
130189702Ssam	case AH_MARK_PERCAL:
131189702Ssam		fprintf(fd, "ar%uPerCalibration, channel %u Mhz", state.chipnum, r->val);
132189702Ssam		break;
133189702Ssam	case AH_MARK_SETCHANNEL:
134189702Ssam		fprintf(fd, "ar%uSetChannel, channel %u Mhz", state.chipnum, r->val);
135189702Ssam		break;
136189702Ssam	case AH_MARK_ANI_RESET:
137189702Ssam		switch (r->val) {
138189702Ssam		case HAL_M_STA:
139189702Ssam			fprintf(fd, "ar%uAniReset, HAL_M_STA", state.chipnum);
140189702Ssam			break;
141189702Ssam		case HAL_M_IBSS:
142189702Ssam			fprintf(fd, "ar%uAniReset, HAL_M_IBSS", state.chipnum);
143189702Ssam			break;
144189702Ssam		case HAL_M_HOSTAP:
145189702Ssam			fprintf(fd, "ar%uAniReset, HAL_M_HOSTAP", state.chipnum);
146189702Ssam			break;
147189702Ssam		case HAL_M_MONITOR:
148189702Ssam			fprintf(fd, "ar%uAniReset, HAL_M_MONITOR", state.chipnum);
149189702Ssam			break;
150189702Ssam		default:
151189702Ssam			fprintf(fd, "ar%uAniReset, opmode %u", state.chipnum, r->val);
152189702Ssam			break;
153189702Ssam		}
154189702Ssam		break;
155189702Ssam	case AH_MARK_ANI_POLL:
156189702Ssam		fprintf(fd, "ar%uAniPoll, listenTime %u", state.chipnum, r->val);
157189702Ssam		break;
158189702Ssam	case AH_MARK_ANI_CONTROL:
159189702Ssam		switch (r->val) {
160189702Ssam		case HAL_ANI_PRESENT:
161189702Ssam			fprintf(fd, "ar%uAniControl, PRESENT", state.chipnum);
162189702Ssam			break;
163189702Ssam		case HAL_ANI_NOISE_IMMUNITY_LEVEL:
164189702Ssam			fprintf(fd, "ar%uAniControl, NOISE_IMMUNITY", state.chipnum);
165189702Ssam			break;
166189702Ssam		case HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION:
167189702Ssam			fprintf(fd, "ar%uAniControl, OFDM_WEAK_SIGNAL", state.chipnum);
168189702Ssam			break;
169189702Ssam		case HAL_ANI_CCK_WEAK_SIGNAL_THR:
170189702Ssam			fprintf(fd, "ar%uAniControl, CCK_WEAK_SIGNAL", state.chipnum);
171189702Ssam			break;
172189702Ssam		case HAL_ANI_FIRSTEP_LEVEL:
173189702Ssam			fprintf(fd, "ar%uAniControl, FIRSTEP_LEVEL", state.chipnum);
174189702Ssam			break;
175189702Ssam		case HAL_ANI_SPUR_IMMUNITY_LEVEL:
176189702Ssam			fprintf(fd, "ar%uAniControl, SPUR_IMMUNITY", state.chipnum);
177189702Ssam			break;
178189702Ssam		case HAL_ANI_MODE:
179189702Ssam			fprintf(fd, "ar%uAniControl, MODE", state.chipnum);
180189702Ssam			break;
181189702Ssam		case HAL_ANI_PHYERR_RESET:
182189702Ssam			fprintf(fd, "ar%uAniControl, PHYERR_RESET", state.chipnum);
183189702Ssam			break;
184189702Ssam		default:
185189702Ssam			fprintf(fd, "ar%uAniControl, cmd %u", state.chipnum, r->val);
186189702Ssam			break;
187189702Ssam		}
188189702Ssam		break;
189189702Ssam	default:
190189702Ssam		fprintf(fd, "mark #%u value %u/0x%x", r->reg, r->val, r->val);
191189702Ssam		break;
192189702Ssam	}
193189702Ssam}
194189702Ssam
195189702Ssam#include "ah_devid.h"
196189702Ssam
197189702Ssamstatic void
198189702Ssamopdevice(const struct athregrec *r)
199189702Ssam{
200189702Ssam	switch (r->val) {
201189702Ssam	case AR5210_PROD:
202189702Ssam	case AR5210_DEFAULT:
203189702Ssam		state.chipnum = 5210;
204189702Ssam		state.revs.ah_macVersion = 1;
205189702Ssam		state.revs.ah_macRev = 0;
206189702Ssam		break;
207189702Ssam	case AR5211_DEVID:
208189702Ssam	case AR5311_DEVID:
209189702Ssam	case AR5211_DEFAULT:
210189702Ssam	case AR5211_FPGA11B:
211189702Ssam		state.chipnum = 5211;
212189702Ssam		state.revs.ah_macVersion = 2;
213189702Ssam		state.revs.ah_macRev = 0;
214189702Ssam		break;
215189702Ssam	/* AR5212 */
216189702Ssam	case AR5212_DEFAULT:
217189702Ssam	case AR5212_DEVID:
218189702Ssam	case AR5212_FPGA:
219189702Ssam	case AR5212_DEVID_IBM:
220189702Ssam	case AR5212_AR5312_REV2:
221189702Ssam	case AR5212_AR5312_REV7:
222189702Ssam	case AR5212_AR2313_REV8:
223189702Ssam	case AR5212_AR2315_REV6:
224189702Ssam	case AR5212_AR2315_REV7:
225189702Ssam	case AR5212_AR2317_REV1:
226189702Ssam	case AR5212_AR2317_REV2:
227189702Ssam
228189702Ssam	/* AR5212 compatible devid's also attach to 5212 */
229189702Ssam	case AR5212_DEVID_0014:
230189702Ssam	case AR5212_DEVID_0015:
231189702Ssam	case AR5212_DEVID_0016:
232189702Ssam	case AR5212_DEVID_0017:
233189702Ssam	case AR5212_DEVID_0018:
234189702Ssam	case AR5212_DEVID_0019:
235189702Ssam	case AR5212_AR2413:
236189702Ssam	case AR5212_AR5413:
237189702Ssam	case AR5212_AR5424:
238189702Ssam	case AR5212_AR2417:
239189702Ssam	case AR5212_DEVID_FF19:
240189702Ssam		state.chipnum = 5212;
241189702Ssam		state.revs.ah_macVersion = 4;
242189702Ssam		state.revs.ah_macRev = 5;
243189702Ssam		break;
244189702Ssam
245189702Ssam	/* AR5213 */
246189702Ssam	case AR5213_SREV_1_0:
247189702Ssam	case AR5213_SREV_REG:
248189702Ssam		state.chipnum = 5213;
249189702Ssam		state.revs.ah_macVersion = 5;
250189702Ssam		state.revs.ah_macRev = 9;
251189702Ssam		break;
252189702Ssam
253189702Ssam	/* AR5416 compatible devid's  */
254189702Ssam	case AR5416_DEVID_PCI:
255189702Ssam	case AR5416_DEVID_PCIE:
256189702Ssam	case AR9160_DEVID_PCI:
257189702Ssam	case AR9280_DEVID_PCI:
258189702Ssam	case AR9280_DEVID_PCIE:
259189702Ssam	case AR9285_DEVID_PCIE:
260189702Ssam		state.chipnum = 5416;
261189702Ssam		state.revs.ah_macVersion = 13;
262189702Ssam		state.revs.ah_macRev = 8;
263189702Ssam		break;
264189702Ssam	default:
265189702Ssam		printf("Unknown device id 0x%x\n", r->val);
266189702Ssam		exit(-1);
267189702Ssam	}
268189702Ssam}
269189702Ssam
270189702Ssamstatic int
271189702Ssamregcompar(const void *a, const void *b)
272189702Ssam{
273189702Ssam	const struct dumpreg *ra = *(const struct dumpreg **)a;
274189702Ssam	const struct dumpreg *rb = *(const struct dumpreg **)b;
275189702Ssam	return ra->addr - rb->addr;
276189702Ssam}
277189702Ssam
278189702Ssamvoid
279189702Ssamregister_regs(struct dumpreg *chipregs, u_int nchipregs,
280189702Ssam	int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
281189702Ssam{
282189702Ssam	const int existing_regs = state.nregs;
283189702Ssam	int i, j;
284189702Ssam
285189702Ssam	for (i = 0; i < nchipregs; i++) {
286189702Ssam		struct dumpreg *nr = &chipregs[i];
287189702Ssam		if (nr->srevMin == 0)
288189702Ssam			nr->srevMin = def_srev_min;
289189702Ssam		if (nr->srevMax == 0)
290189702Ssam			nr->srevMax = def_srev_max;
291189702Ssam		if (nr->phyMin == 0)
292189702Ssam			nr->phyMin = def_phy_min;
293189702Ssam		if (nr->phyMax == 0)
294189702Ssam			nr->phyMax = def_phy_max;
295189702Ssam		for (j = 0; j < existing_regs; j++) {
296189702Ssam			struct dumpreg *r = state.regs[j];
297189702Ssam			/*
298189702Ssam			 * Check if we can just expand the mac+phy
299189702Ssam			 * coverage for the existing entry.
300189702Ssam			 */
301189702Ssam			if (nr->addr == r->addr &&
302189702Ssam			    (nr->name == r->name ||
303189702Ssam			     nr->name != NULL && r->name != NULL &&
304189702Ssam			     strcmp(nr->name, r->name) == 0)) {
305189702Ssam				if (nr->srevMin < r->srevMin &&
306189702Ssam				    (r->srevMin <= nr->srevMax &&
307189702Ssam				     nr->srevMax+1 <= r->srevMax)) {
308189702Ssam					r->srevMin = nr->srevMin;
309189702Ssam					goto skip;
310189702Ssam				}
311189702Ssam				if (nr->srevMax > r->srevMax &&
312189702Ssam				    (r->srevMin <= nr->srevMin &&
313189702Ssam				     nr->srevMin <= r->srevMax)) {
314189702Ssam					r->srevMax = nr->srevMax;
315189702Ssam					goto skip;
316189702Ssam				}
317189702Ssam			}
318189702Ssam			if (r->addr > nr->addr)
319189702Ssam				break;
320189702Ssam		}
321189702Ssam		/*
322189702Ssam		 * New item, add to the end, it'll be sorted below.
323189702Ssam		 */
324189702Ssam		if (state.nregs == MAXREGS)
325189702Ssam			errx(-1, "too many registers; bump MAXREGS");
326189702Ssam		state.regs[state.nregs++] = nr;
327189702Ssam	skip:
328189702Ssam		;
329189702Ssam	}
330189702Ssam	qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar);
331189702Ssam}
332189702Ssam
333189702Ssamvoid
334189702Ssamregister_keycache(u_int nslots,
335189702Ssam	int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
336189702Ssam{
337189702Ssam	/* discard, no use */
338189702Ssam}
339189702Ssam
340189702Ssamvoid
341189702Ssamregister_range(u_int brange, u_int erange, int type,
342189702Ssam	int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
343189702Ssam{
344189702Ssam	/* discard, no use */
345189702Ssam}
346189702Ssam
347189702Ssamstatic const struct dumpreg *
348189702Ssamfindreg(int reg)
349189702Ssam{
350189702Ssam	const HAL_REVS *revs = &state.revs;
351189702Ssam	int i;
352189702Ssam
353189702Ssam	for (i = 0; i < state.nregs; i++) {
354189702Ssam		const struct dumpreg *dr = state.regs[i];
355189702Ssam		if (dr->addr == reg &&
356189702Ssam		    MAC_MATCH(dr, revs->ah_macVersion, revs->ah_macRev))
357189702Ssam			return dr;
358189702Ssam	}
359189702Ssam	return NULL;
360189702Ssam}
361189702Ssam
362189702Ssam/* XXX cheat, 5212 has a superset of the key table defs */
363189702Ssam#include "ar5212/ar5212reg.h"
364189702Ssam#include "ar5212/ar5212phy.h"
365189702Ssam
366189702Ssam#define PWR_TABLE_SIZE	64
367189702Ssam
368189702Ssamstatic void
369189702Ssamoprw(FILE *fd, int recnum, struct athregrec *r)
370189702Ssam{
371189702Ssam	const struct dumpreg *dr;
372189702Ssam	char buf[64];
373189702Ssam	const char* bits;
374189702Ssam	int i;
375189702Ssam
376189702Ssam	fprintf(fd, "\n%05d: ", recnum);
377189702Ssam	dr = findreg(r->reg);
378189702Ssam	if (dr != NULL && dr->name != NULL) {
379189702Ssam		snprintf(buf, sizeof (buf), "AR_%s (0x%x)", dr->name, r->reg);
380189702Ssam		bits = dr->bits;
381189702Ssam	} else if (AR_KEYTABLE(0) <= r->reg && r->reg < AR_KEYTABLE(128)) {
382189702Ssam		snprintf(buf, sizeof (buf), "AR_KEYTABLE%u(%u) (0x%x)",
383189702Ssam			((r->reg - AR_KEYTABLE_0) >> 2) & 7,
384189702Ssam			(r->reg - AR_KEYTABLE_0) >> 5, r->reg);
385189702Ssam		bits = NULL;
386189702Ssam#if 0
387189702Ssam	} else if (AR_PHY_PCDAC_TX_POWER(0) <= r->reg && r->reg < AR_PHY_PCDAC_TX_POWER(PWR_TABLE_SIZE/2)) {
388189702Ssam		snprintf(buf, sizeof (buf), "AR_PHY_PCDAC_TX_POWER(%u) (0x%x)",
389189702Ssam			(r->reg - AR_PHY_PCDAC_TX_POWER_0) >> 2, r->reg);
390189702Ssam		bits = NULL;
391189702Ssam#endif
392189702Ssam	} else if (AR_RATE_DURATION(0) <= r->reg && r->reg < AR_RATE_DURATION(32)) {
393189702Ssam		snprintf(buf, sizeof (buf), "AR_RATE_DURATION(0x%x) (0x%x)",
394189702Ssam			(r->reg - AR_RATE_DURATION_0) >> 2, r->reg);
395189702Ssam		bits = NULL;
396189702Ssam	} else if (AR_PHY_BASE <= r->reg) {
397189702Ssam		snprintf(buf, sizeof (buf), "AR_PHY(%u) (0x%x)",
398189702Ssam			(r->reg - AR_PHY_BASE) >> 2, r->reg);
399189702Ssam		bits = NULL;
400189702Ssam	} else {
401189702Ssam		snprintf(buf, sizeof (buf), "0x%x", r->reg);
402189702Ssam		bits = NULL;
403189702Ssam	}
404189702Ssam	fprintf(fd, "%-30s %s 0x%x", buf, r->op ? "<=" : "=>", r->val);
405189702Ssam	if (bits) {
406189702Ssam		const char *p = bits;
407189702Ssam		int tmp, n;
408189702Ssam
409189702Ssam		for (tmp = 0, p++; *p;) {
410189702Ssam			n = *p++;
411189702Ssam			if (r->val & (1 << (n - 1))) {
412189702Ssam				putc(tmp ? ',' : '<', fd);
413189702Ssam				for (; (n = *p) > ' '; ++p)
414189702Ssam					putc(n, fd);
415189702Ssam				tmp = 1;
416189702Ssam			} else
417189702Ssam				for (; *p > ' '; ++p)
418189702Ssam					continue;
419189702Ssam		}
420189702Ssam		if (tmp)
421189702Ssam			putc('>', fd);
422189702Ssam	}
423189702Ssam}
424