1/*
2 * Copyright (c) 2012 Adrian Chadd <adrian@FreeBSD.org>
3 * All Rights Reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/cdefs.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <string.h>
25
26#include <sys/types.h>
27#include <sys/alq.h>
28#include <sys/endian.h>
29
30#include <dev/ath/if_ath_alq.h>
31#include <dev/ath/ath_hal/ar5210/ar5210desc.h>
32
33#include "ar5210_ds.h"
34
35#define	MS(_v, _f)	( ((_v) & (_f)) >> _f##_S )
36#define	MF(_v, _f) ( !! ((_v) & (_f)))
37
38static void
39ar5210_decode_txstatus(struct if_ath_alq_payload *a)
40{
41	struct ar5210_desc txs;
42
43	/* XXX assumes txs is smaller than PAYLOAD_LEN! */
44	memcpy(&txs, &a->payload, sizeof(struct ar5210_desc));
45
46	printf("[%u.%06u] [%llu] TXSTATUS\n",
47	    (unsigned int) be32toh(a->hdr.tstamp_sec),
48	    (unsigned int) be32toh(a->hdr.tstamp_usec),
49	    (unsigned long long) be64toh(a->hdr.threadid));
50
51	/* ds_txstatus0 */
52	printf("    Frmok=%d, xretries=%d, fifounderrun=%d, filt=%d\n",
53	    MF(txs.ds_status0, AR_FrmXmitOK),
54	    MF(txs.ds_status0, AR_ExcessiveRetries),
55	    MF(txs.ds_status0, AR_FIFOUnderrun),
56	    MF(txs.ds_status0, AR_Filtered));
57	printf("    LongRetryCnt=%d, ShortRetryCnt=%d\n",
58	    MS(txs.ds_status0, AR_LongRetryCnt),
59	    MS(txs.ds_status0, AR_ShortRetryCnt));
60	printf("    SndTimestamp=0x%04x\n",
61	    MS(txs.ds_status0, AR_SendTimestamp));
62
63	/* ds_txstatus1 */
64	printf("    Done=%d, SeqNum=0x%04x, AckRSSI=%d\n",
65	    MF(txs.ds_status1, AR_Done),
66	    txs.ds_status1 & AR_SeqNum,
67	    MS(txs.ds_status1, AR_AckSigStrength));
68
69	printf("\n ------\n");
70}
71
72static void
73ar5210_decode_txdesc(struct if_ath_alq_payload *a)
74{
75	struct ar5210_desc txc;
76
77	/* XXX assumes txs is smaller than PAYLOAD_LEN! */
78	memcpy(&txc, &a->payload, sizeof(struct ar5210_desc));
79
80	printf("[%u.%06u] [%llu] TXD\n",
81	    (unsigned int) be32toh(a->hdr.tstamp_sec),
82	    (unsigned int) be32toh(a->hdr.tstamp_usec),
83	    (unsigned long long) be64toh(a->hdr.threadid));
84
85	printf("  link=0x%08x, data=0x%08x\n",
86	    txc.ds_link,
87	    txc.ds_data);
88
89	/* ds_ctl0 */
90	printf("    Frame Len=%d\n", txc.ds_ctl0 & AR_FrameLen);
91	printf("    TX Rate=0x%02x, RtsEna=%d, ClrDstMask=%d AntModeXmit=0x%02x\n",
92	    MS(txc.ds_ctl0, AR_XmitRate),
93	    MF(txc.ds_ctl0, AR_RTSCTSEnable),
94	    MF(txc.ds_ctl0, AR_ClearDestMask),
95	    MF(txc.ds_ctl0, AR_AntModeXmit));
96	printf("    FrmType=0x%02x, TxIntrReq=%d\n",
97	    MS(txc.ds_ctl0, AR_FrmType),
98	    MF(txc.ds_ctl0, AR_TxInterReq));
99	printf("    LongPkt=%d\n", MF(txc.ds_ctl0, AR_LongPkt));
100
101	/* ds_ctl1 */
102	printf("    BufLen=%d, TxMore=%d, EncryptKeyIdx=%d, RtsDuration=%d\n",
103	    txc.ds_ctl1 & AR_BufLen,
104	    MF(txc.ds_ctl1, AR_More),
105	    MS(txc.ds_ctl1, AR_EncryptKeyIdx),
106	    MS(txc.ds_ctl1, AR_RTSDuration));
107
108	printf("\n ------ \n");
109}
110
111static void
112ar5210_decode_rxstatus(struct if_ath_alq_payload *a)
113{
114	struct ar5210_desc rxs;
115
116	/* XXX assumes rxs is smaller than PAYLOAD_LEN! */
117	memcpy(&rxs, &a->payload, sizeof(struct ar5210_desc));
118
119	printf("[%u.%06u] [%llu] RXSTATUS\n",
120	    (unsigned int) be32toh(a->hdr.tstamp_sec),
121	    (unsigned int) be32toh(a->hdr.tstamp_usec),
122	    (unsigned long long) be64toh(a->hdr.threadid));
123
124	printf("  link=0x%08x, data=0x%08x\n",
125	    rxs.ds_link,
126	    rxs.ds_data);
127
128	/* ds_rxstatus0 */
129	printf("  DataLen=%d, ArMore=%d, RSSI=%d, RcvAntenna=0x%x\n",
130	    rxs.ds_status0 & AR_DataLen,
131	    MF(rxs.ds_status0, AR_More),
132	    MS(rxs.ds_status0, AR_RcvSigStrength),
133	    MF(rxs.ds_status0, AR_RcvAntenna));
134
135	/* ds_rxstatus1 */
136	printf("  RxDone=%d, RxFrameOk=%d, CrcErr=%d, DecryptCrcErr=%d\n",
137	    MF(rxs.ds_status1, AR_Done),
138	    MF(rxs.ds_status1, AR_FrmRcvOK),
139	    MF(rxs.ds_status1, AR_CRCErr),
140	    MF(rxs.ds_status1, AR_DecryptCRCErr));
141	printf("  KeyIdxValid=%d\n",
142	    MF(rxs.ds_status1, AR_KeyIdxValid));
143
144	printf("  PhyErrCode=0x%02x\n",
145	    MS(rxs.ds_status1, AR_PHYErr));
146
147	printf("  KeyMiss=%d\n",
148	    MF(rxs.ds_status1, AR_KeyCacheMiss));
149
150	printf("  Timetamp: 0x%05x\n",
151	    MS(rxs.ds_status1, AR_RcvTimestamp));
152
153	printf("\n ------\n");
154}
155
156void
157ar5210_alq_payload(struct if_ath_alq_payload *a)
158{
159
160		switch (be16toh(a->hdr.op)) {
161			case ATH_ALQ_EDMA_TXSTATUS:	/* TXSTATUS */
162				ar5210_decode_txstatus(a);
163				break;
164			case ATH_ALQ_EDMA_RXSTATUS:	/* RXSTATUS */
165				ar5210_decode_rxstatus(a);
166				break;
167			case ATH_ALQ_EDMA_TXDESC:	/* TXDESC */
168				ar5210_decode_txdesc(a);
169				break;
170			default:
171				printf("[%d.%06d] [%lld] op: %d; len %d\n",
172				    be32toh(a->hdr.tstamp_sec),
173				    be32toh(a->hdr.tstamp_usec),
174				    be64toh(a->hdr.threadid),
175				    be16toh(a->hdr.op), be16toh(a->hdr.len));
176		}
177}
178