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/ar5211/ar5211desc.h>
32
33#include "ar5211_ds.h"
34
35#define	MS(_v, _f)	( ((_v) & (_f)) >> _f##_S )
36#define	MF(_v, _f) ( !! ((_v) & (_f)))
37
38static void
39ar5211_decode_txstatus(struct if_ath_alq_payload *a)
40{
41	struct ar5211_desc txs;
42
43	/* XXX assumes txs is smaller than PAYLOAD_LEN! */
44	memcpy(&txs, &a->payload, sizeof(struct ar5211_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, VCollCnt=%d\n",
58	    MS(txs.ds_status0, AR_LongRetryCnt),
59	    MS(txs.ds_status0, AR_ShortRetryCnt),
60	    MS(txs.ds_status0, AR_VirtCollCnt));
61	printf("    SndTimestamp=0x%04x\n",
62	    MS(txs.ds_status0, AR_SendTimestamp));
63
64	/* ds_txstatus1 */
65	printf("    Done=%d, SeqNum=0x%04x, AckRSSI=%d\n",
66	    MF(txs.ds_status1, AR_Done),
67	    MS(txs.ds_status1, AR_SeqNum),
68	    MS(txs.ds_status1, AR_AckSigStrength));
69
70	printf("\n ------\n");
71}
72
73static void
74ar5211_decode_txdesc(struct if_ath_alq_payload *a)
75{
76	struct ar5211_desc txc;
77
78	/* XXX assumes txs is smaller than PAYLOAD_LEN! */
79	memcpy(&txc, &a->payload, sizeof(struct ar5211_desc));
80
81	printf("[%u.%06u] [%llu] TXD\n",
82	    (unsigned int) be32toh(a->hdr.tstamp_sec),
83	    (unsigned int) be32toh(a->hdr.tstamp_usec),
84	    (unsigned long long) be64toh(a->hdr.threadid));
85
86	printf("  link=0x%08x, data=0x%08x\n",
87	    txc.ds_link,
88	    txc.ds_data);
89
90	/* ds_ctl0 */
91	printf("    Frame Len=%d\n", txc.ds_ctl0 & AR_FrameLen);
92	printf("    TX Rate=0x%02x, RtsEna=%d, Veol=%d, ClrDstMask=%d AntModeXmit=0x%02x\n",
93	    MS(txc.ds_ctl0, AR_XmitRate),
94	    MF(txc.ds_ctl0, AR_RTSCTSEnable),
95	    MF(txc.ds_ctl0, AR_VEOL),
96	    MF(txc.ds_ctl0, AR_ClearDestMask),
97	    MF(txc.ds_ctl0, AR_AntModeXmit));
98	printf("    TxIntrReq=%d\n",
99	    MF(txc.ds_ctl0, AR_TxInterReq));
100
101	/* ds_ctl1 */
102	printf("    BufLen=%d, TxMore=%d, EncryptKeyIdx=%d,FrType=0x%x\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_FrmType));
107	printf("    NoAck=%d\n", MF(txc.ds_ctl1, AR_NoAck));
108
109	printf("\n ------ \n");
110}
111
112static void
113ar5211_decode_rxstatus(struct if_ath_alq_payload *a)
114{
115	struct ar5211_desc rxs;
116
117	/* XXX assumes rxs is smaller than PAYLOAD_LEN! */
118	memcpy(&rxs, &a->payload, sizeof(struct ar5211_desc));
119
120	printf("[%u.%06u] [%llu] RXSTATUS\n",
121	    (unsigned int) be32toh(a->hdr.tstamp_sec),
122	    (unsigned int) be32toh(a->hdr.tstamp_usec),
123	    (unsigned long long) be64toh(a->hdr.threadid));
124
125	printf("  link=0x%08x, data=0x%08x\n",
126	    rxs.ds_link,
127	    rxs.ds_data);
128
129	/* ds_rxstatus0 */
130	printf("  DataLen=%d, ArMore=%d, RSSI=%d, RcvAntenna=0x%x\n",
131	    rxs.ds_status0 & AR_DataLen,
132	    MF(rxs.ds_status0, AR_More),
133	    MS(rxs.ds_status0, AR_RcvSigStrength),
134	    MS(rxs.ds_status0, AR_RcvAntenna));
135
136	/* ds_rxstatus1 */
137	printf("  RxDone=%d, RxFrameOk=%d, CrcErr=%d, DecryptCrcErr=%d\n",
138	    MF(rxs.ds_status1, AR_Done),
139	    MF(rxs.ds_status1, AR_FrmRcvOK),
140	    MF(rxs.ds_status1, AR_CRCErr),
141	    MF(rxs.ds_status1, AR_DecryptCRCErr));
142	printf("  KeyIdxValid=%d\n",
143	    MF(rxs.ds_status1, AR_KeyIdxValid));
144
145	printf("  PhyErrCode=0x%02x\n",
146	    MS(rxs.ds_status1, AR_PHYErr));
147
148	printf("  KeyMiss=%d\n",
149	    MF(rxs.ds_status1, AR_KeyCacheMiss));
150
151	printf("  Timetamp: 0x%05x\n",
152	    MS(rxs.ds_status1, AR_RcvTimestamp));
153
154	printf("\n ------\n");
155}
156
157void
158ar5211_alq_payload(struct if_ath_alq_payload *a)
159{
160
161		switch (be16toh(a->hdr.op)) {
162			case ATH_ALQ_EDMA_TXSTATUS:	/* TXSTATUS */
163				ar5211_decode_txstatus(a);
164				break;
165			case ATH_ALQ_EDMA_RXSTATUS:	/* RXSTATUS */
166				ar5211_decode_rxstatus(a);
167				break;
168			case ATH_ALQ_EDMA_TXDESC:	/* TXDESC */
169				ar5211_decode_txdesc(a);
170				break;
171			default:
172				printf("[%d.%06d] [%lld] op: %d; len %d\n",
173				    be32toh(a->hdr.tstamp_sec),
174				    be32toh(a->hdr.tstamp_usec),
175				    be64toh(a->hdr.threadid),
176				    be16toh(a->hdr.op), be16toh(a->hdr.len));
177		}
178}
179