xref: /freebsd/tools/tools/ath/athstats/athstats.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
16  * NO WARRANTY
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.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "opt_ah.h"
33 
34 /*
35  * ath statistics class.
36  */
37 #include <sys/types.h>
38 #include <sys/file.h>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <net/if_media.h>
43 #include <net/if_var.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <signal.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <err.h>
51 
52 #include "ah.h"
53 #include "ah_desc.h"
54 #include "net80211/ieee80211_ioctl.h"
55 #include "net80211/ieee80211_radiotap.h"
56 #include "if_athioctl.h"
57 
58 #include "athstats.h"
59 
60 #ifdef ATH_SUPPORT_ANI
61 #define HAL_EP_RND(x,mul) \
62 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
63 #define HAL_RSSI(x)     HAL_EP_RND(x, HAL_RSSI_EP_MULTIPLIER)
64 #endif
65 
66 #define	NOTPRESENT	{ 0, "", "" }
67 
68 #define	AFTER(prev)	((prev)+1)
69 
70 static const struct fmt athstats[] = {
71 #define	S_INPUT		0
72 	{ 8,	"input",	"input",	"data frames received" },
73 #define	S_OUTPUT	AFTER(S_INPUT)
74 	{ 8,	"output",	"output",	"data frames transmit" },
75 #define	S_TX_ALTRATE	AFTER(S_OUTPUT)
76 	{ 7,	"altrate",	"altrate",	"tx frames with an alternate rate" },
77 #define	S_TX_SHORTRETRY	AFTER(S_TX_ALTRATE)
78 	{ 7,	"short",	"short",	"short on-chip tx retries" },
79 #define	S_TX_LONGRETRY	AFTER(S_TX_SHORTRETRY)
80 	{ 7,	"long",		"long",		"long on-chip tx retries" },
81 #define	S_TX_XRETRIES	AFTER(S_TX_LONGRETRY)
82 	{ 6,	"xretry",	"xretry",	"tx failed 'cuz too many retries" },
83 #define	S_MIB		AFTER(S_TX_XRETRIES)
84 	{ 5,	"mib",		"mib",		"mib overflow interrupts" },
85 #ifndef __linux__
86 #define	S_TX_LINEAR	AFTER(S_MIB)
87 	{ 5,	"txlinear",	"txlinear",	"tx linearized to cluster" },
88 #define	S_BSTUCK	AFTER(S_TX_LINEAR)
89 	{ 5,	"bstuck",	"bstuck",	"stuck beacon conditions" },
90 #define	S_INTRCOAL	AFTER(S_BSTUCK)
91 	{ 5,	"intrcoal",	"intrcoal",	"interrupts coalesced" },
92 #define	S_RATE		AFTER(S_INTRCOAL)
93 #else
94 #define	S_RATE		AFTER(S_MIB)
95 #endif
96 	{ 5,	"rate",		"rate",		"current transmit rate" },
97 #define	S_WATCHDOG	AFTER(S_RATE)
98 	{ 5,	"wdog",		"wdog",		"watchdog timeouts" },
99 #define	S_FATAL		AFTER(S_WATCHDOG)
100 	{ 5,	"fatal",	"fatal",	"hardware error interrupts" },
101 #define	S_BMISS		AFTER(S_FATAL)
102 	{ 5,	"bmiss",	"bmiss",	"beacon miss interrupts" },
103 #define	S_RXORN		AFTER(S_BMISS)
104 	{ 5,	"rxorn",	"rxorn",	"recv overrun interrupts" },
105 #define	S_RXEOL		AFTER(S_RXORN)
106 	{ 5,	"rxeol",	"rxeol",	"recv eol interrupts" },
107 #define	S_TXURN		AFTER(S_RXEOL)
108 	{ 5,	"txurn",	"txurn",	"txmit underrun interrupts" },
109 #define	S_TX_MGMT	AFTER(S_TXURN)
110 	{ 5,	"txmgt",	"txmgt",	"tx management frames" },
111 #define	S_TX_DISCARD	AFTER(S_TX_MGMT)
112 	{ 5,	"txdisc",	"txdisc",	"tx frames discarded prior to association" },
113 #define	S_TX_INVALID	AFTER(S_TX_DISCARD)
114 	{ 5,	"txinv",	"txinv",	"tx invalid (19)" },
115 #define	S_TX_QSTOP	AFTER(S_TX_INVALID)
116 	{ 5,	"qstop",	"qstop",	"tx stopped 'cuz no xmit buffer" },
117 #define	S_TX_ENCAP	AFTER(S_TX_QSTOP)
118 	{ 5,	"txencode",	"txencode",	"tx encapsulation failed" },
119 #define	S_TX_NONODE	AFTER(S_TX_ENCAP)
120 	{ 5,	"txnonode",	"txnonode",	"tx failed 'cuz no node" },
121 #define	S_TX_NOBUF	AFTER(S_TX_NONODE)
122 	{ 5,	"txnobuf",	"txnobuf",	"tx failed 'cuz dma buffer allocation failed" },
123 #define	S_TX_NOFRAG	AFTER(S_TX_NOBUF)
124 	{ 5,	"txnofrag",	"txnofrag",	"tx failed 'cuz frag buffer allocation(s) failed" },
125 #define	S_TX_NOMBUF	AFTER(S_TX_NOFRAG)
126 	{ 5,	"txnombuf",	"txnombuf",	"tx failed 'cuz mbuf allocation failed" },
127 #ifndef __linux__
128 #define	S_TX_NOMCL	AFTER(S_TX_NOMBUF)
129 	{ 5,	"txnomcl",	"txnomcl",	"tx failed 'cuz cluster allocation failed" },
130 #define	S_TX_FIFOERR	AFTER(S_TX_NOMCL)
131 #else
132 #define	S_TX_FIFOERR	AFTER(S_TX_NOMBUF)
133 #endif
134 	{ 5,	"efifo",	"efifo",	"tx failed 'cuz FIFO underrun" },
135 #define	S_TX_FILTERED	AFTER(S_TX_FIFOERR)
136 	{ 5,	"efilt",	"efilt",	"tx failed 'cuz destination filtered" },
137 #define	S_TX_BADRATE	AFTER(S_TX_FILTERED)
138 	{ 5,	"txbadrate",	"txbadrate",	"tx failed 'cuz bogus xmit rate" },
139 #define	S_TX_NOACK	AFTER(S_TX_BADRATE)
140 	{ 5,	"noack",	"noack",	"tx frames with no ack marked" },
141 #define	S_TX_RTS	AFTER(S_TX_NOACK)
142 	{ 5,	"rts",		"rts",		"tx frames with rts enabled" },
143 #define	S_TX_CTS	AFTER(S_TX_RTS)
144 	{ 5,	"cts",		"cts",		"tx frames with cts enabled" },
145 #define	S_TX_SHORTPRE	AFTER(S_TX_CTS)
146 	{ 5,	"shpre",	"shpre",	"tx frames with short preamble" },
147 #define	S_TX_PROTECT	AFTER(S_TX_SHORTPRE)
148 	{ 5,	"protect",	"protect",	"tx frames with 11g protection" },
149 #define	S_RX_ORN	AFTER(S_TX_PROTECT)
150 	{ 5,	"rxorn",	"rxorn",	"rx failed 'cuz of desc overrun" },
151 #define	S_RX_CRC_ERR	AFTER(S_RX_ORN)
152 	{ 6,	"crcerr",	"crcerr",	"rx failed 'cuz of bad CRC" },
153 #define	S_RX_FIFO_ERR	AFTER(S_RX_CRC_ERR)
154 	{ 5,	"rxfifo",	"rxfifo",	"rx failed 'cuz of FIFO overrun" },
155 #define	S_RX_CRYPTO_ERR	AFTER(S_RX_FIFO_ERR)
156 	{ 5,	"crypt",	"crypt",	"rx failed 'cuz decryption" },
157 #define	S_RX_MIC_ERR	AFTER(S_RX_CRYPTO_ERR)
158 	{ 4,	"mic",		"mic",		"rx failed 'cuz MIC failure" },
159 #define	S_RX_TOOSHORT	AFTER(S_RX_MIC_ERR)
160 	{ 5,	"rxshort",	"rxshort",	"rx failed 'cuz frame too short" },
161 #define	S_RX_NOMBUF	AFTER(S_RX_TOOSHORT)
162 	{ 5,	"rxnombuf",	"rxnombuf",	"rx setup failed 'cuz no mbuf" },
163 #define	S_RX_MGT	AFTER(S_RX_NOMBUF)
164 	{ 5,	"rxmgt",	"rxmgt",	"rx management frames" },
165 #define	S_RX_CTL	AFTER(S_RX_MGT)
166 	{ 5,	"rxctl",	"rxctl",	"rx control frames" },
167 #define	S_RX_PHY_ERR	AFTER(S_RX_CTL)
168 	{ 7,	"phyerr",	"phyerr",	"rx failed 'cuz of PHY err" },
169 #define	S_RX_PHY_UNDERRUN		AFTER(S_RX_PHY_ERR)
170 	{ 4,	"phyund",	"TUnd",	"transmit underrun" },
171 #define	S_RX_PHY_TIMING			AFTER(S_RX_PHY_UNDERRUN)
172 	{ 4,	"phytim",	"Tim",	"timing error" },
173 #define	S_RX_PHY_PARITY			AFTER(S_RX_PHY_TIMING)
174 	{ 4,	"phypar",	"IPar",	"illegal parity" },
175 #define	S_RX_PHY_RATE			AFTER(S_RX_PHY_PARITY)
176 	{ 4,	"phyrate",	"IRate",	"illegal rate" },
177 #define	S_RX_PHY_LENGTH			AFTER(S_RX_PHY_RATE)
178 	{ 4,	"phylen",	"ILen",		"illegal length" },
179 #define	S_RX_PHY_RADAR			AFTER(S_RX_PHY_LENGTH)
180 	{ 4,	"phyradar",	"Radar",	"radar detect" },
181 #define	S_RX_PHY_SERVICE		AFTER(S_RX_PHY_RADAR)
182 	{ 4,	"physervice",	"Service",	"illegal service" },
183 #define	S_RX_PHY_TOR			AFTER(S_RX_PHY_SERVICE)
184 	{ 4,	"phytor",	"TOR",		"transmit override receive" },
185 #define	S_RX_PHY_OFDM_TIMING		AFTER(S_RX_PHY_TOR)
186 	{ 6,	"ofdmtim",	"ofdmtim",	"OFDM timing" },
187 #define	S_RX_PHY_OFDM_SIGNAL_PARITY	AFTER(S_RX_PHY_OFDM_TIMING)
188 	{ 6,	"ofdmsig",	"ofdmsig",	"OFDM illegal parity" },
189 #define	S_RX_PHY_OFDM_RATE_ILLEGAL	AFTER(S_RX_PHY_OFDM_SIGNAL_PARITY)
190 	{ 6,	"ofdmrate",	"ofdmrate",	"OFDM illegal rate" },
191 #define	S_RX_PHY_OFDM_POWER_DROP	AFTER(S_RX_PHY_OFDM_RATE_ILLEGAL)
192 	{ 6,	"ofdmpow",	"ofdmpow",	"OFDM power drop" },
193 #define	S_RX_PHY_OFDM_SERVICE		AFTER(S_RX_PHY_OFDM_POWER_DROP)
194 	{ 6,	"ofdmservice",	"ofdmservice",	"OFDM illegal service" },
195 #define	S_RX_PHY_OFDM_RESTART		AFTER(S_RX_PHY_OFDM_SERVICE)
196 	{ 6,	"ofdmrestart",	"ofdmrestart",	"OFDM restart" },
197 #define	S_RX_PHY_CCK_TIMING		AFTER(S_RX_PHY_OFDM_RESTART)
198 	{ 6,	"ccktim",	"ccktim",	"CCK timing" },
199 #define	S_RX_PHY_CCK_HEADER_CRC		AFTER(S_RX_PHY_CCK_TIMING)
200 	{ 6,	"cckhead",	"cckhead",	"CCK header crc" },
201 #define	S_RX_PHY_CCK_RATE_ILLEGAL	AFTER(S_RX_PHY_CCK_HEADER_CRC)
202 	{ 6,	"cckrate",	"cckrate",	"CCK illegal rate" },
203 #define	S_RX_PHY_CCK_SERVICE		AFTER(S_RX_PHY_CCK_RATE_ILLEGAL)
204 	{ 6,	"cckservice",	"cckservice",	"CCK illegal service" },
205 #define	S_RX_PHY_CCK_RESTART		AFTER(S_RX_PHY_CCK_SERVICE)
206 	{ 6,	"cckrestar",	"cckrestar",	"CCK restart" },
207 #define	S_BE_NOMBUF	AFTER(S_RX_PHY_CCK_RESTART)
208 	{ 4,	"benombuf",	"benombuf",	"beacon setup failed 'cuz no mbuf" },
209 #define	S_BE_XMIT	AFTER(S_BE_NOMBUF)
210 	{ 7,	"bexmit",	"bexmit",	"beacons transmitted" },
211 #define	S_PER_CAL	AFTER(S_BE_XMIT)
212 	{ 4,	"pcal",		"pcal",		"periodic calibrations" },
213 #define	S_PER_CALFAIL	AFTER(S_PER_CAL)
214 	{ 4,	"pcalf",	"pcalf",	"periodic calibration failures" },
215 #define	S_PER_RFGAIN	AFTER(S_PER_CALFAIL)
216 	{ 4,	"prfga",	"prfga",	"rfgain value change" },
217 #if ATH_SUPPORT_TDMA
218 #define	S_TDMA_UPDATE	AFTER(S_PER_RFGAIN)
219 	{ 5,	"tdmau",	"tdmau",	"TDMA slot timing updates" },
220 #define	S_TDMA_TIMERS	AFTER(S_TDMA_UPDATE)
221 	{ 5,	"tdmab",	"tdmab",	"TDMA slot update set beacon timers" },
222 #define	S_TDMA_TSF	AFTER(S_TDMA_TIMERS)
223 	{ 5,	"tdmat",	"tdmat",	"TDMA slot update set TSF" },
224 #define	S_TDMA_TSFADJ	AFTER(S_TDMA_TSF)
225 	{ 8,	"tdmadj",	"tdmadj",	"TDMA slot adjust (usecs, smoothed)" },
226 #define	S_TDMA_ACK	AFTER(S_TDMA_TSFADJ)
227 	{ 5,	"tdmack",	"tdmack",	"TDMA tx failed 'cuz ACK required" },
228 #define	S_RATE_CALLS	AFTER(S_TDMA_ACK)
229 #else
230 #define	S_RATE_CALLS	AFTER(S_PER_RFGAIN)
231 #endif
232 	{ 5,	"ratec",	"ratec",	"rate control checks" },
233 #define	S_RATE_RAISE	AFTER(S_RATE_CALLS)
234 	{ 5,	"rate+",	"rate+",	"rate control raised xmit rate" },
235 #define	S_RATE_DROP	AFTER(S_RATE_RAISE)
236 	{ 5,	"rate-",	"rate-",	"rate control dropped xmit rate" },
237 #define	S_TX_RSSI	AFTER(S_RATE_DROP)
238 	{ 4,	"arssi",	"arssi",	"rssi of last ack" },
239 #define	S_RX_RSSI	AFTER(S_TX_RSSI)
240 	{ 4,	"rssi",		"rssi",		"avg recv rssi" },
241 #define	S_RX_NOISE	AFTER(S_RX_RSSI)
242 	{ 5,	"noise",	"noise",	"rx noise floor" },
243 #define	S_BMISS_PHANTOM	AFTER(S_RX_NOISE)
244 	{ 5,	"bmissphantom",	"bmissphantom",	"phantom beacon misses" },
245 #define	S_TX_RAW	AFTER(S_BMISS_PHANTOM)
246 	{ 5,	"txraw",	"txraw",	"tx frames through raw api" },
247 #define	S_TX_RAW_FAIL	AFTER(S_TX_RAW)
248 	{ 5,	"txrawfail",	"txrawfail",	"raw tx failed 'cuz interface/hw down" },
249 #define	S_RX_TOOBIG	AFTER(S_TX_RAW_FAIL)
250 	{ 5,	"rx2big",	"rx2big",	"rx failed 'cuz frame too large"  },
251 #define	S_RX_AGG	AFTER(S_RX_TOOBIG)
252 	{ 5,	"rxagg",	"rxagg",	"A-MPDU sub-frames received" },
253 #define	S_RX_HALFGI	AFTER(S_RX_AGG)
254 	{ 5,	"rxhalfgi",	"rxhgi",	"Half-GI frames received" },
255 #define	S_RX_2040	AFTER(S_RX_HALFGI)
256 	{ 6,	"rx2040",	"rx2040",	"40MHz frames received" },
257 #define	S_RX_PRE_CRC_ERR	AFTER(S_RX_2040)
258 	{ 11,	"rxprecrcerr",	"rxprecrcerr",	"CRC errors for non-last A-MPDU subframes" },
259 #define	S_RX_POST_CRC_ERR	AFTER(S_RX_PRE_CRC_ERR)
260 	{ 12,	"rxpostcrcerr",	"rxpostcrcerr",	"CRC errors for last subframe in an A-MPDU" },
261 #define	S_RX_DECRYPT_BUSY_ERR	AFTER(S_RX_POST_CRC_ERR)
262 	{ 10,	"rxdescbusy",	"rxdescbusy",	"Decryption engine busy" },
263 #define	S_RX_HI_CHAIN	AFTER(S_RX_DECRYPT_BUSY_ERR)
264 	{ 4,	"rxhi",	"rxhi",	"Frames received with RX chain in high power mode" },
265 #define	S_TX_HTPROTECT	AFTER(S_RX_HI_CHAIN)
266 	{ 7,	"txhtprot",	"txhtprot",	"Frames transmitted with HT Protection" },
267 #define	S_RX_QEND	AFTER(S_TX_HTPROTECT)
268 	{ 7,	"rxquend",	"rxquend",	"Hit end of RX descriptor queue" },
269 #define	S_TX_TIMEOUT	AFTER(S_RX_QEND)
270 	{ 4,	"txtimeout",	"TXTX",	"TX Timeout" },
271 #define	S_TX_CSTIMEOUT	AFTER(S_TX_TIMEOUT)
272 	{ 4,	"csttimeout",	"CSTX",	"Carrier Sense Timeout" },
273 #define	S_TX_XTXOP_ERR	AFTER(S_TX_CSTIMEOUT)
274 	{ 5,	"xtxoperr",	"TXOPX",	"TXOP exceed" },
275 #define	S_TX_TIMEREXPIRED_ERR	AFTER(S_TX_XTXOP_ERR)
276 	{ 7,	"texperr",	"texperr",	"TX Timer expired" },
277 #define	S_TX_DESCCFG_ERR	AFTER(S_TX_TIMEREXPIRED_ERR)
278 	{ 10,	"desccfgerr",	"desccfgerr",	"TX descriptor error" },
279 #define	S_TX_SWRETRIES	AFTER(S_TX_DESCCFG_ERR)
280 	{ 9,	"txswretry",	"txswretry",	"Number of frames retransmitted in software" },
281 #define	S_TX_SWRETRIES_MAX	AFTER(S_TX_SWRETRIES)
282 	{ 7,	"txswmax",	"txswmax",	"Number of frames exceeding software retry" },
283 #define	S_TX_DATA_UNDERRUN	AFTER(S_TX_SWRETRIES_MAX)
284 	{ 5,	"txdataunderrun",	"TXDAU",	"A-MPDU TX FIFO data underrun" },
285 #define	S_TX_DELIM_UNDERRUN	AFTER(S_TX_DATA_UNDERRUN)
286 	{ 5,	"txdelimunderrun",	"TXDEU",	"A-MPDU TX Delimiter underrun" },
287 #define	S_TX_AGGR_OK		AFTER(S_TX_DELIM_UNDERRUN)
288 	{ 5,	"txaggrok",	"TXAOK",	"A-MPDU sub-frame TX attempt success" },
289 #define	S_TX_AGGR_FAIL		AFTER(S_TX_AGGR_OK)
290 	{ 4,	"txaggrfail",	"TXAF",	"A-MPDU sub-frame TX attempt failures" },
291 #define	S_TX_AGGR_FAILALL	AFTER(S_TX_AGGR_FAIL)
292 	{ 7,	"txaggrfailall",	"TXAFALL",	"A-MPDU TX frame failures" },
293 #ifndef __linux__
294 #define	S_CABQ_XMIT	AFTER(S_TX_AGGR_FAILALL)
295 	{ 5,	"cabxmit",	"cabxmit",	"cabq frames transmitted" },
296 #define	S_CABQ_BUSY	AFTER(S_CABQ_XMIT)
297 	{ 5,	"cabqbusy",	"cabqbusy",	"cabq xmit overflowed beacon interval" },
298 #define	S_TX_NODATA	AFTER(S_CABQ_BUSY)
299 	{ 5,	"txnodata",	"txnodata",	"tx discarded empty frame" },
300 #define	S_TX_BUSDMA	AFTER(S_TX_NODATA)
301 	{ 5,	"txbusdma",	"txbusdma",	"tx failed for dma resrcs" },
302 #define	S_RX_BUSDMA	AFTER(S_TX_BUSDMA)
303 	{ 5,	"rxbusdma",	"rxbusdma",	"rx setup failed for dma resrcs" },
304 #define	S_FF_TXOK	AFTER(S_RX_BUSDMA)
305 #else
306 #define	S_FF_TXOK	AFTER(S_TX_AGGR_FAILALL)
307 #endif
308 	{ 5,	"fftxok",	"fftxok",	"fast frames xmit successfully" },
309 #define	S_FF_TXERR	AFTER(S_FF_TXOK)
310 	{ 5,	"fftxerr",	"fftxerr",	"fast frames not xmit due to error" },
311 #define	S_FF_RX		AFTER(S_FF_TXERR)
312 	{ 5,	"ffrx",		"ffrx",		"fast frames received" },
313 #define	S_FF_FLUSH	AFTER(S_FF_RX)
314 	{ 5,	"ffflush",	"ffflush",	"fast frames flushed from staging q" },
315 #define	S_TX_QFULL	AFTER(S_FF_FLUSH)
316 	{ 5,	"txqfull",	"txqfull",	"tx discarded 'cuz queue is full" },
317 #define	S_ANT_DEFSWITCH	AFTER(S_TX_QFULL)
318 	{ 5,	"defsw",	"defsw",	"switched default/rx antenna" },
319 #define	S_ANT_TXSWITCH	AFTER(S_ANT_DEFSWITCH)
320 	{ 5,	"txsw",		"txsw",		"tx used alternate antenna" },
321 #ifdef ATH_SUPPORT_ANI
322 #define	S_ANI_NOISE	AFTER(S_ANT_TXSWITCH)
323 	{ 2,	"ni",	"NI",		"noise immunity level" },
324 #define	S_ANI_SPUR	AFTER(S_ANI_NOISE)
325 	{ 2,	"si",	"SI",		"spur immunity level" },
326 #define	S_ANI_STEP	AFTER(S_ANI_SPUR)
327 	{ 2,	"step",	"ST",		"first step level" },
328 #define	S_ANI_OFDM	AFTER(S_ANI_STEP)
329 	{ 4,	"owsd",	"OWSD",		"OFDM weak signal detect" },
330 #define	S_ANI_CCK	AFTER(S_ANI_OFDM)
331 	{ 4,	"cwst",	"CWST",		"CCK weak signal threshold" },
332 #define	S_ANI_MAXSPUR	AFTER(S_ANI_CCK)
333 	{ 3,	"maxsi","MSI",		"max spur immunity level" },
334 #define	S_ANI_LISTEN	AFTER(S_ANI_MAXSPUR)
335 	{ 6,	"listen","LISTEN",	"listen time" },
336 #define	S_ANI_NIUP	AFTER(S_ANI_LISTEN)
337 	{ 4,	"ni+",	"NI-",		"ANI increased noise immunity" },
338 #define	S_ANI_NIDOWN	AFTER(S_ANI_NIUP)
339 	{ 4,	"ni-",	"NI-",		"ANI decrease noise immunity" },
340 #define	S_ANI_SIUP	AFTER(S_ANI_NIDOWN)
341 	{ 4,	"si+",	"SI+",		"ANI increased spur immunity" },
342 #define	S_ANI_SIDOWN	AFTER(S_ANI_SIUP)
343 	{ 4,	"si-",	"SI-",		"ANI decrease spur immunity" },
344 #define	S_ANI_OFDMON	AFTER(S_ANI_SIDOWN)
345 	{ 5,	"ofdm+","OFDM+",	"ANI enabled OFDM weak signal detect" },
346 #define	S_ANI_OFDMOFF	AFTER(S_ANI_OFDMON)
347 	{ 5,	"ofdm-","OFDM-",	"ANI disabled OFDM weak signal detect" },
348 #define	S_ANI_CCKHI	AFTER(S_ANI_OFDMOFF)
349 	{ 5,	"cck+",	"CCK+",		"ANI enabled CCK weak signal threshold" },
350 #define	S_ANI_CCKLO	AFTER(S_ANI_CCKHI)
351 	{ 5,	"cck-",	"CCK-",		"ANI disabled CCK weak signal threshold" },
352 #define	S_ANI_STEPUP	AFTER(S_ANI_CCKLO)
353 	{ 5,	"step+","STEP+",	"ANI increased first step level" },
354 #define	S_ANI_STEPDOWN	AFTER(S_ANI_STEPUP)
355 	{ 5,	"step-","STEP-",	"ANI decreased first step level" },
356 #define	S_ANI_OFDMERRS	AFTER(S_ANI_STEPDOWN)
357 	{ 8,	"ofdm",	"OFDM",		"cumulative OFDM phy error count" },
358 #define	S_ANI_CCKERRS	AFTER(S_ANI_OFDMERRS)
359 	{ 8,	"cck",	"CCK",		"cumulative CCK phy error count" },
360 #define	S_ANI_RESET	AFTER(S_ANI_CCKERRS)
361 	{ 5,	"reset","RESET",	"ANI parameters zero'd for non-STA operation" },
362 #define	S_ANI_LZERO	AFTER(S_ANI_RESET)
363 	{ 5,	"lzero","LZERO",	"ANI forced listen time to zero" },
364 #define	S_ANI_LNEG	AFTER(S_ANI_LZERO)
365 	{ 5,	"lneg",	"LNEG",		"ANI calculated listen time < 0" },
366 #define	S_MIB_ACKBAD	AFTER(S_ANI_LNEG)
367 	{ 5,	"ackbad","ACKBAD",	"missing ACK's" },
368 #define	S_MIB_RTSBAD	AFTER(S_MIB_ACKBAD)
369 	{ 5,	"rtsbad","RTSBAD",	"RTS without CTS" },
370 #define	S_MIB_RTSGOOD	AFTER(S_MIB_RTSBAD)
371 	{ 5,	"rtsgood","RTSGOOD",	"successful RTS" },
372 #define	S_MIB_FCSBAD	AFTER(S_MIB_RTSGOOD)
373 	{ 5,	"fcsbad","FCSBAD",	"bad FCS" },
374 #define	S_MIB_BEACONS	AFTER(S_MIB_FCSBAD)
375 	{ 5,	"beacons","beacons",	"beacons received" },
376 #define	S_NODE_AVGBRSSI	AFTER(S_MIB_BEACONS)
377 	{ 3,	"avgbrssi","BSI",	"average rssi (beacons only)" },
378 #define	S_NODE_AVGRSSI	AFTER(S_NODE_AVGBRSSI)
379 	{ 3,	"avgrssi","DSI",	"average rssi (all rx'd frames)" },
380 #define	S_NODE_AVGARSSI	AFTER(S_NODE_AVGRSSI)
381 	{ 3,	"avgtxrssi","TSI",	"average rssi (ACKs only)" },
382 #define	S_ANT_TX0	AFTER(S_NODE_AVGARSSI)
383 #else
384 #define	S_ANT_TX0	AFTER(S_ANT_TXSWITCH)
385 #endif /* ATH_SUPPORT_ANI */
386 	{ 8,	"tx0",	"ant0(tx)",	"frames tx on antenna 0" },
387 #define	S_ANT_TX1	AFTER(S_ANT_TX0)
388 	{ 8,	"tx1",	"ant1(tx)",	"frames tx on antenna 1"  },
389 #define	S_ANT_TX2	AFTER(S_ANT_TX1)
390 	{ 8,	"tx2",	"ant2(tx)",	"frames tx on antenna 2"  },
391 #define	S_ANT_TX3	AFTER(S_ANT_TX2)
392 	{ 8,	"tx3",	"ant3(tx)",	"frames tx on antenna 3"  },
393 #define	S_ANT_TX4	AFTER(S_ANT_TX3)
394 	{ 8,	"tx4",	"ant4(tx)",	"frames tx on antenna 4"  },
395 #define	S_ANT_TX5	AFTER(S_ANT_TX4)
396 	{ 8,	"tx5",	"ant5(tx)",	"frames tx on antenna 5"  },
397 #define	S_ANT_TX6	AFTER(S_ANT_TX5)
398 	{ 8,	"tx6",	"ant6(tx)",	"frames tx on antenna 6"  },
399 #define	S_ANT_TX7	AFTER(S_ANT_TX6)
400 	{ 8,	"tx7",	"ant7(tx)",	"frames tx on antenna 7"  },
401 #define	S_ANT_RX0	AFTER(S_ANT_TX7)
402 	{ 8,	"rx0",	"ant0(rx)",	"frames rx on antenna 0"  },
403 #define	S_ANT_RX1	AFTER(S_ANT_RX0)
404 	{ 8,	"rx1",	"ant1(rx)",	"frames rx on antenna 1"   },
405 #define	S_ANT_RX2	AFTER(S_ANT_RX1)
406 	{ 8,	"rx2",	"ant2(rx)",	"frames rx on antenna 2"   },
407 #define	S_ANT_RX3	AFTER(S_ANT_RX2)
408 	{ 8,	"rx3",	"ant3(rx)",	"frames rx on antenna 3"   },
409 #define	S_ANT_RX4	AFTER(S_ANT_RX3)
410 	{ 8,	"rx4",	"ant4(rx)",	"frames rx on antenna 4"   },
411 #define	S_ANT_RX5	AFTER(S_ANT_RX4)
412 	{ 8,	"rx5",	"ant5(rx)",	"frames rx on antenna 5"   },
413 #define	S_ANT_RX6	AFTER(S_ANT_RX5)
414 	{ 8,	"rx6",	"ant6(rx)",	"frames rx on antenna 6"   },
415 #define	S_ANT_RX7	AFTER(S_ANT_RX6)
416 	{ 8,	"rx7",	"ant7(rx)",	"frames rx on antenna 7"   },
417 #define	S_TX_SIGNAL	AFTER(S_ANT_RX7)
418 	{ 4,	"asignal",	"asig",	"signal of last ack (dBm)" },
419 #define	S_RX_SIGNAL	AFTER(S_TX_SIGNAL)
420 	{ 4,	"signal",	"sig",	"avg recv signal (dBm)" },
421 
422 };
423 #define	S_PHY_MIN	S_RX_PHY_UNDERRUN
424 #define	S_PHY_MAX	S_RX_PHY_CCK_RESTART
425 #define	S_LAST		S_ANT_TX0
426 #define	S_MAX	S_ANT_RX7+1
427 
428 /*
429  * XXX fold this into the external HAL definitions! -adrian
430  */
431 struct _athstats {
432 	struct ath_stats ath;
433 #ifdef ATH_SUPPORT_ANI
434 	struct {
435 		uint32_t ast_ani_niup;		/* increased noise immunity */
436 		uint32_t ast_ani_nidown;	/* decreased noise immunity */
437 		uint32_t ast_ani_spurup;	/* increased spur immunity */
438 		uint32_t ast_ani_spurdown;	/* descreased spur immunity */
439 		uint32_t ast_ani_ofdmon;	/* OFDM weak signal detect on */
440 		uint32_t ast_ani_ofdmoff;	/* OFDM weak signal detect off*/
441 		uint32_t ast_ani_cckhigh;	/* CCK weak signal thr high */
442 		uint32_t ast_ani_ccklow;	/* CCK weak signal thr low */
443 		uint32_t ast_ani_stepup;	/* increased first step level */
444 		uint32_t ast_ani_stepdown;	/* decreased first step level */
445 		uint32_t ast_ani_ofdmerrs;	/* cumulative ofdm phy err cnt*/
446 		uint32_t ast_ani_cckerrs;	/* cumulative cck phy err cnt */
447 		uint32_t ast_ani_reset;	/* params zero'd for non-STA */
448 		uint32_t ast_ani_lzero;	/* listen time forced to zero */
449 		uint32_t ast_ani_lneg;		/* listen time calculated < 0 */
450 		HAL_MIB_STATS ast_mibstats;	/* MIB counter stats */
451 		HAL_NODE_STATS ast_nodestats;	/* latest rssi stats */
452 	} ani_stats;
453 	struct {
454 		uint8_t	noiseImmunityLevel;
455 		uint8_t	spurImmunityLevel;
456 		uint8_t	firstepLevel;
457 		uint8_t	ofdmWeakSigDetectOff;
458 		uint8_t	cckWeakSigThreshold;
459 		uint32_t listenTime;
460 	} ani_state;
461 #endif
462 };
463 
464 struct athstatfoo_p {
465 	struct athstatfoo base;
466 	int s;
467 	int optstats;
468 #define	ATHSTATS_ANI	0x0001
469 	struct ifreq ifr;
470 	struct ath_diag atd;
471 	struct _athstats cur;
472 	struct _athstats total;
473 };
474 
475 static void
476 ath_setifname(struct athstatfoo *wf0, const char *ifname)
477 {
478 	struct athstatfoo_p *wf = (struct athstatfoo_p *) wf0;
479 
480 	strncpy(wf->ifr.ifr_name, ifname, sizeof (wf->ifr.ifr_name));
481 #ifdef ATH_SUPPORT_ANI
482 	strncpy(wf->atd.ad_name, ifname, sizeof (wf->atd.ad_name));
483 	wf->optstats |= ATHSTATS_ANI;
484 #endif
485 }
486 
487 static void
488 ath_zerostats(struct athstatfoo *wf0)
489 {
490 	struct athstatfoo_p *wf = (struct athstatfoo_p *) wf0;
491 
492 	if (ioctl(wf->s, SIOCZATHSTATS, &wf->ifr) < 0)
493 		err(-1, wf->ifr.ifr_name);
494 }
495 
496 static void
497 ath_collect(struct athstatfoo_p *wf, struct _athstats *stats)
498 {
499 	wf->ifr.ifr_data = (caddr_t) &stats->ath;
500 	if (ioctl(wf->s, SIOCGATHSTATS, &wf->ifr) < 0)
501 		err(1, wf->ifr.ifr_name);
502 #ifdef ATH_SUPPORT_ANI
503 	if (wf->optstats & ATHSTATS_ANI) {
504 		wf->atd.ad_id = 5;
505 		wf->atd.ad_out_data = (caddr_t) &stats->ani_state;
506 		wf->atd.ad_out_size = sizeof(stats->ani_state);
507 		if (ioctl(wf->s, SIOCGATHDIAG, &wf->atd) < 0) {
508 			warn(wf->atd.ad_name);
509 			wf->optstats &= ~ATHSTATS_ANI;
510 		}
511 		wf->atd.ad_id = 8;
512 		wf->atd.ad_out_data = (caddr_t) &stats->ani_stats;
513 		wf->atd.ad_out_size = sizeof(stats->ani_stats);
514 		if (ioctl(wf->s, SIOCGATHDIAG, &wf->atd) < 0)
515 			warn(wf->atd.ad_name);
516 	}
517 #endif /* ATH_SUPPORT_ANI */
518 }
519 
520 static void
521 ath_collect_cur(struct statfoo *sf)
522 {
523 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
524 
525 	ath_collect(wf, &wf->cur);
526 }
527 
528 static void
529 ath_collect_tot(struct statfoo *sf)
530 {
531 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
532 
533 	ath_collect(wf, &wf->total);
534 }
535 
536 static void
537 ath_update_tot(struct statfoo *sf)
538 {
539 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
540 
541 	wf->total = wf->cur;
542 }
543 
544 static void
545 snprintrate(char b[], size_t bs, int rate)
546 {
547 	if (rate & IEEE80211_RATE_MCS)
548 		snprintf(b, bs, "MCS%u", rate &~ IEEE80211_RATE_MCS);
549 	else if (rate & 1)
550 		snprintf(b, bs, "%u.5M", rate / 2);
551 	else
552 		snprintf(b, bs, "%uM", rate / 2);
553 }
554 
555 static int
556 ath_get_curstat(struct statfoo *sf, int s, char b[], size_t bs)
557 {
558 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
559 #define	STAT(x) \
560 	snprintf(b, bs, "%u", wf->cur.ath.ast_##x - wf->total.ath.ast_##x); return 1
561 #define	PHY(x) \
562 	snprintf(b, bs, "%u", wf->cur.ath.ast_rx_phy[x] - wf->total.ath.ast_rx_phy[x]); return 1
563 #define	ANI(x) \
564 	snprintf(b, bs, "%u", wf->cur.ani_state.x); return 1
565 #define	ANISTAT(x) \
566 	snprintf(b, bs, "%u", wf->cur.ani_stats.ast_ani_##x - wf->total.ani_stats.ast_ani_##x); return 1
567 #define	MIBSTAT(x) \
568 	snprintf(b, bs, "%u", wf->cur.ani_stats.ast_mibstats.x - wf->total.ani_stats.ast_mibstats.x); return 1
569 #define	TXANT(x) \
570 	snprintf(b, bs, "%u", wf->cur.ath.ast_ant_tx[x] - wf->total.ath.ast_ant_tx[x]); return 1
571 #define	RXANT(x) \
572 	snprintf(b, bs, "%u", wf->cur.ath.ast_ant_rx[x] - wf->total.ath.ast_ant_rx[x]); return 1
573 
574 	switch (s) {
575 	case S_INPUT:
576 		snprintf(b, bs, "%lu",
577 		    (wf->cur.ath.ast_rx_packets - wf->total.ath.ast_rx_packets) -
578 		    (wf->cur.ath.ast_rx_mgt - wf->total.ath.ast_rx_mgt));
579 		return 1;
580 	case S_OUTPUT:
581 		snprintf(b, bs, "%lu",
582 		    wf->cur.ath.ast_tx_packets - wf->total.ath.ast_tx_packets);
583 		return 1;
584 	case S_RATE:
585 		snprintrate(b, bs, wf->cur.ath.ast_tx_rate);
586 		return 1;
587 	case S_WATCHDOG:	STAT(watchdog);
588 	case S_FATAL:		STAT(hardware);
589 	case S_BMISS:		STAT(bmiss);
590 	case S_BMISS_PHANTOM:	STAT(bmiss_phantom);
591 #ifdef S_BSTUCK
592 	case S_BSTUCK:		STAT(bstuck);
593 #endif
594 	case S_RXORN:		STAT(rxorn);
595 	case S_RXEOL:		STAT(rxeol);
596 	case S_TXURN:		STAT(txurn);
597 	case S_MIB:		STAT(mib);
598 #ifdef S_INTRCOAL
599 	case S_INTRCOAL:	STAT(intrcoal);
600 #endif
601 	case S_TX_MGMT:		STAT(tx_mgmt);
602 	case S_TX_DISCARD:	STAT(tx_discard);
603 	case S_TX_QSTOP:	STAT(tx_qstop);
604 	case S_TX_ENCAP:	STAT(tx_encap);
605 	case S_TX_NONODE:	STAT(tx_nonode);
606 	case S_TX_NOBUF:	STAT(tx_nobuf);
607 	case S_TX_NOFRAG:	STAT(tx_nofrag);
608 	case S_TX_NOMBUF:	STAT(tx_nombuf);
609 #ifdef S_TX_NOMCL
610 	case S_TX_NOMCL:	STAT(tx_nomcl);
611 	case S_TX_LINEAR:	STAT(tx_linear);
612 	case S_TX_NODATA:	STAT(tx_nodata);
613 	case S_TX_BUSDMA:	STAT(tx_busdma);
614 #endif
615 	case S_TX_XRETRIES:	STAT(tx_xretries);
616 	case S_TX_FIFOERR:	STAT(tx_fifoerr);
617 	case S_TX_FILTERED:	STAT(tx_filtered);
618 	case S_TX_SHORTRETRY:	STAT(tx_shortretry);
619 	case S_TX_LONGRETRY:	STAT(tx_longretry);
620 	case S_TX_BADRATE:	STAT(tx_badrate);
621 	case S_TX_NOACK:	STAT(tx_noack);
622 	case S_TX_RTS:		STAT(tx_rts);
623 	case S_TX_CTS:		STAT(tx_cts);
624 	case S_TX_SHORTPRE:	STAT(tx_shortpre);
625 	case S_TX_ALTRATE:	STAT(tx_altrate);
626 	case S_TX_PROTECT:	STAT(tx_protect);
627 	case S_TX_RAW:		STAT(tx_raw);
628 	case S_TX_RAW_FAIL:	STAT(tx_raw_fail);
629 	case S_RX_NOMBUF:	STAT(rx_nombuf);
630 #ifdef S_RX_BUSDMA
631 	case S_RX_BUSDMA:	STAT(rx_busdma);
632 #endif
633 	case S_RX_ORN:		STAT(rx_orn);
634 	case S_RX_CRC_ERR:	STAT(rx_crcerr);
635 	case S_RX_FIFO_ERR: 	STAT(rx_fifoerr);
636 	case S_RX_CRYPTO_ERR: 	STAT(rx_badcrypt);
637 	case S_RX_MIC_ERR:	STAT(rx_badmic);
638 	case S_RX_PHY_ERR:	STAT(rx_phyerr);
639 	case S_RX_PHY_UNDERRUN:	PHY(HAL_PHYERR_UNDERRUN);
640 	case S_RX_PHY_TIMING:	PHY(HAL_PHYERR_TIMING);
641 	case S_RX_PHY_PARITY:	PHY(HAL_PHYERR_PARITY);
642 	case S_RX_PHY_RATE:	PHY(HAL_PHYERR_RATE);
643 	case S_RX_PHY_LENGTH:	PHY(HAL_PHYERR_LENGTH);
644 	case S_RX_PHY_RADAR:	PHY(HAL_PHYERR_RADAR);
645 	case S_RX_PHY_SERVICE:	PHY(HAL_PHYERR_SERVICE);
646 	case S_RX_PHY_TOR:	PHY(HAL_PHYERR_TOR);
647 	case S_RX_PHY_OFDM_TIMING:	  PHY(HAL_PHYERR_OFDM_TIMING);
648 	case S_RX_PHY_OFDM_SIGNAL_PARITY: PHY(HAL_PHYERR_OFDM_SIGNAL_PARITY);
649 	case S_RX_PHY_OFDM_RATE_ILLEGAL:  PHY(HAL_PHYERR_OFDM_RATE_ILLEGAL);
650 	case S_RX_PHY_OFDM_POWER_DROP:	  PHY(HAL_PHYERR_OFDM_POWER_DROP);
651 	case S_RX_PHY_OFDM_SERVICE:	  PHY(HAL_PHYERR_OFDM_SERVICE);
652 	case S_RX_PHY_OFDM_RESTART:	  PHY(HAL_PHYERR_OFDM_RESTART);
653 	case S_RX_PHY_CCK_TIMING:	  PHY(HAL_PHYERR_CCK_TIMING);
654 	case S_RX_PHY_CCK_HEADER_CRC:	  PHY(HAL_PHYERR_CCK_HEADER_CRC);
655 	case S_RX_PHY_CCK_RATE_ILLEGAL:	  PHY(HAL_PHYERR_CCK_RATE_ILLEGAL);
656 	case S_RX_PHY_CCK_SERVICE:	  PHY(HAL_PHYERR_CCK_SERVICE);
657 	case S_RX_PHY_CCK_RESTART:	  PHY(HAL_PHYERR_CCK_RESTART);
658 	case S_RX_TOOSHORT:	STAT(rx_tooshort);
659 	case S_RX_TOOBIG:	STAT(rx_toobig);
660 	case S_RX_MGT:		STAT(rx_mgt);
661 	case S_RX_CTL:		STAT(rx_ctl);
662 	case S_TX_RSSI:
663 		snprintf(b, bs, "%d", wf->cur.ath.ast_tx_rssi);
664 		return 1;
665 	case S_RX_RSSI:
666 		snprintf(b, bs, "%d", wf->cur.ath.ast_rx_rssi);
667 		return 1;
668 	case S_BE_XMIT:		STAT(be_xmit);
669 	case S_BE_NOMBUF:	STAT(be_nombuf);
670 	case S_PER_CAL:		STAT(per_cal);
671 	case S_PER_CALFAIL:	STAT(per_calfail);
672 	case S_PER_RFGAIN:	STAT(per_rfgain);
673 #ifdef S_TDMA_UPDATE
674 	case S_TDMA_UPDATE:	STAT(tdma_update);
675 	case S_TDMA_TIMERS:	STAT(tdma_timers);
676 	case S_TDMA_TSF:	STAT(tdma_tsf);
677 	case S_TDMA_TSFADJ:
678 		snprintf(b, bs, "-%d/+%d",
679 		    wf->cur.ath.ast_tdma_tsfadjm, wf->cur.ath.ast_tdma_tsfadjp);
680 		return 1;
681 	case S_TDMA_ACK:	STAT(tdma_ack);
682 #endif
683 	case S_RATE_CALLS:	STAT(rate_calls);
684 	case S_RATE_RAISE:	STAT(rate_raise);
685 	case S_RATE_DROP:	STAT(rate_drop);
686 	case S_ANT_DEFSWITCH:	STAT(ant_defswitch);
687 	case S_ANT_TXSWITCH:	STAT(ant_txswitch);
688 #ifdef S_ANI_NOISE
689 	case S_ANI_NOISE:	ANI(noiseImmunityLevel);
690 	case S_ANI_SPUR:	ANI(spurImmunityLevel);
691 	case S_ANI_STEP:	ANI(firstepLevel);
692 	case S_ANI_OFDM:	ANI(ofdmWeakSigDetectOff);
693 	case S_ANI_CCK:		ANI(cckWeakSigThreshold);
694 	case S_ANI_LISTEN:	ANI(listenTime);
695 	case S_ANI_NIUP:	ANISTAT(niup);
696 	case S_ANI_NIDOWN:	ANISTAT(nidown);
697 	case S_ANI_SIUP:	ANISTAT(spurup);
698 	case S_ANI_SIDOWN:	ANISTAT(spurdown);
699 	case S_ANI_OFDMON:	ANISTAT(ofdmon);
700 	case S_ANI_OFDMOFF:	ANISTAT(ofdmoff);
701 	case S_ANI_CCKHI:	ANISTAT(cckhigh);
702 	case S_ANI_CCKLO:	ANISTAT(ccklow);
703 	case S_ANI_STEPUP:	ANISTAT(stepup);
704 	case S_ANI_STEPDOWN:	ANISTAT(stepdown);
705 	case S_ANI_OFDMERRS:	ANISTAT(ofdmerrs);
706 	case S_ANI_CCKERRS:	ANISTAT(cckerrs);
707 	case S_ANI_RESET:	ANISTAT(reset);
708 	case S_ANI_LZERO:	ANISTAT(lzero);
709 	case S_ANI_LNEG:	ANISTAT(lneg);
710 	case S_MIB_ACKBAD:	MIBSTAT(ackrcv_bad);
711 	case S_MIB_RTSBAD:	MIBSTAT(rts_bad);
712 	case S_MIB_RTSGOOD:	MIBSTAT(rts_good);
713 	case S_MIB_FCSBAD:	MIBSTAT(fcs_bad);
714 	case S_MIB_BEACONS:	MIBSTAT(beacons);
715 	case S_NODE_AVGBRSSI:
716 		snprintf(b, bs, "%u",
717 		    HAL_RSSI(wf->cur.ani_stats.ast_nodestats.ns_avgbrssi));
718 		return 1;
719 	case S_NODE_AVGRSSI:
720 		snprintf(b, bs, "%u",
721 		    HAL_RSSI(wf->cur.ani_stats.ast_nodestats.ns_avgrssi));
722 		return 1;
723 	case S_NODE_AVGARSSI:
724 		snprintf(b, bs, "%u",
725 		    HAL_RSSI(wf->cur.ani_stats.ast_nodestats.ns_avgtxrssi));
726 		return 1;
727 #endif
728 	case S_ANT_TX0:		TXANT(0);
729 	case S_ANT_TX1:		TXANT(1);
730 	case S_ANT_TX2:		TXANT(2);
731 	case S_ANT_TX3:		TXANT(3);
732 	case S_ANT_TX4:		TXANT(4);
733 	case S_ANT_TX5:		TXANT(5);
734 	case S_ANT_TX6:		TXANT(6);
735 	case S_ANT_TX7:		TXANT(7);
736 	case S_ANT_RX0:		RXANT(0);
737 	case S_ANT_RX1:		RXANT(1);
738 	case S_ANT_RX2:		RXANT(2);
739 	case S_ANT_RX3:		RXANT(3);
740 	case S_ANT_RX4:		RXANT(4);
741 	case S_ANT_RX5:		RXANT(5);
742 	case S_ANT_RX6:		RXANT(6);
743 	case S_ANT_RX7:		RXANT(7);
744 #ifdef S_CABQ_XMIT
745 	case S_CABQ_XMIT:	STAT(cabq_xmit);
746 	case S_CABQ_BUSY:	STAT(cabq_busy);
747 #endif
748 	case S_FF_TXOK:		STAT(ff_txok);
749 	case S_FF_TXERR:	STAT(ff_txerr);
750 	case S_FF_RX:		STAT(ff_rx);
751 	case S_FF_FLUSH:	STAT(ff_flush);
752 	case S_TX_QFULL:	STAT(tx_qfull);
753 	case S_RX_NOISE:
754 		snprintf(b, bs, "%d", wf->cur.ath.ast_rx_noise);
755 		return 1;
756 	case S_TX_SIGNAL:
757 		snprintf(b, bs, "%d",
758 			wf->cur.ath.ast_tx_rssi + wf->cur.ath.ast_rx_noise);
759 		return 1;
760 	case S_RX_SIGNAL:
761 		snprintf(b, bs, "%d",
762 			wf->cur.ath.ast_rx_rssi + wf->cur.ath.ast_rx_noise);
763 		return 1;
764 	case S_RX_AGG:		STAT(rx_agg);
765 	case S_RX_HALFGI:	STAT(rx_halfgi);
766 	case S_RX_2040:		STAT(rx_2040);
767 	case S_RX_PRE_CRC_ERR:	STAT(rx_pre_crc_err);
768 	case S_RX_POST_CRC_ERR:	STAT(rx_post_crc_err);
769 	case S_RX_DECRYPT_BUSY_ERR:	STAT(rx_decrypt_busy_err);
770 	case S_RX_HI_CHAIN:	STAT(rx_hi_rx_chain);
771 	case S_TX_HTPROTECT:	STAT(tx_htprotect);
772 	case S_RX_QEND:		STAT(rx_hitqueueend);
773 	case S_TX_TIMEOUT:	STAT(tx_timeout);
774 	case S_TX_CSTIMEOUT:	STAT(tx_cst);
775 	case S_TX_XTXOP_ERR:	STAT(tx_xtxop);
776 	case S_TX_TIMEREXPIRED_ERR:	STAT(tx_timerexpired);
777 	case S_TX_DESCCFG_ERR:	STAT(tx_desccfgerr);
778 	case S_TX_SWRETRIES:	STAT(tx_swretries);
779 	case S_TX_SWRETRIES_MAX:	STAT(tx_swretrymax);
780 	case S_TX_DATA_UNDERRUN:	STAT(tx_data_underrun);
781 	case S_TX_DELIM_UNDERRUN:	STAT(tx_delim_underrun);
782 	case S_TX_AGGR_OK:		STAT(tx_aggr_ok);
783 	case S_TX_AGGR_FAIL:		STAT(tx_aggr_fail);
784 	case S_TX_AGGR_FAILALL:		STAT(tx_aggr_failall);
785 	}
786 	b[0] = '\0';
787 	return 0;
788 #undef RXANT
789 #undef TXANT
790 #undef ANI
791 #undef ANISTAT
792 #undef MIBSTAT
793 #undef PHY
794 #undef STAT
795 }
796 
797 static int
798 ath_get_totstat(struct statfoo *sf, int s, char b[], size_t bs)
799 {
800 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
801 #define	STAT(x) \
802 	snprintf(b, bs, "%u", wf->total.ath.ast_##x); return 1
803 #define	PHY(x) \
804 	snprintf(b, bs, "%u", wf->total.ath.ast_rx_phy[x]); return 1
805 #define	ANI(x) \
806 	snprintf(b, bs, "%u", wf->total.ani_state.x); return 1
807 #define	ANISTAT(x) \
808 	snprintf(b, bs, "%u", wf->total.ani_stats.ast_ani_##x); return 1
809 #define	MIBSTAT(x) \
810 	snprintf(b, bs, "%u", wf->total.ani_stats.ast_mibstats.x); return 1
811 #define	TXANT(x) \
812 	snprintf(b, bs, "%u", wf->total.ath.ast_ant_tx[x]); return 1
813 #define	RXANT(x) \
814 	snprintf(b, bs, "%u", wf->total.ath.ast_ant_rx[x]); return 1
815 
816 	switch (s) {
817 	case S_INPUT:
818 		snprintf(b, bs, "%lu",
819 		    wf->total.ath.ast_rx_packets - wf->total.ath.ast_rx_mgt);
820 		return 1;
821 	case S_OUTPUT:
822 		snprintf(b, bs, "%lu", wf->total.ath.ast_tx_packets);
823 		return 1;
824 	case S_RATE:
825 		snprintrate(b, bs, wf->total.ath.ast_tx_rate);
826 		return 1;
827 	case S_WATCHDOG:	STAT(watchdog);
828 	case S_FATAL:		STAT(hardware);
829 	case S_BMISS:		STAT(bmiss);
830 	case S_BMISS_PHANTOM:	STAT(bmiss_phantom);
831 #ifdef S_BSTUCK
832 	case S_BSTUCK:		STAT(bstuck);
833 #endif
834 	case S_RXORN:		STAT(rxorn);
835 	case S_RXEOL:		STAT(rxeol);
836 	case S_TXURN:		STAT(txurn);
837 	case S_MIB:		STAT(mib);
838 #ifdef S_INTRCOAL
839 	case S_INTRCOAL:	STAT(intrcoal);
840 #endif
841 	case S_TX_MGMT:		STAT(tx_mgmt);
842 	case S_TX_DISCARD:	STAT(tx_discard);
843 	case S_TX_QSTOP:	STAT(tx_qstop);
844 	case S_TX_ENCAP:	STAT(tx_encap);
845 	case S_TX_NONODE:	STAT(tx_nonode);
846 	case S_TX_NOBUF:	STAT(tx_nobuf);
847 	case S_TX_NOFRAG:	STAT(tx_nofrag);
848 	case S_TX_NOMBUF:	STAT(tx_nombuf);
849 #ifdef S_TX_NOMCL
850 	case S_TX_NOMCL:	STAT(tx_nomcl);
851 	case S_TX_LINEAR:	STAT(tx_linear);
852 	case S_TX_NODATA:	STAT(tx_nodata);
853 	case S_TX_BUSDMA:	STAT(tx_busdma);
854 #endif
855 	case S_TX_XRETRIES:	STAT(tx_xretries);
856 	case S_TX_FIFOERR:	STAT(tx_fifoerr);
857 	case S_TX_FILTERED:	STAT(tx_filtered);
858 	case S_TX_SHORTRETRY:	STAT(tx_shortretry);
859 	case S_TX_LONGRETRY:	STAT(tx_longretry);
860 	case S_TX_BADRATE:	STAT(tx_badrate);
861 	case S_TX_NOACK:	STAT(tx_noack);
862 	case S_TX_RTS:		STAT(tx_rts);
863 	case S_TX_CTS:		STAT(tx_cts);
864 	case S_TX_SHORTPRE:	STAT(tx_shortpre);
865 	case S_TX_ALTRATE:	STAT(tx_altrate);
866 	case S_TX_PROTECT:	STAT(tx_protect);
867 	case S_TX_RAW:		STAT(tx_raw);
868 	case S_TX_RAW_FAIL:	STAT(tx_raw_fail);
869 	case S_RX_NOMBUF:	STAT(rx_nombuf);
870 #ifdef S_RX_BUSDMA
871 	case S_RX_BUSDMA:	STAT(rx_busdma);
872 #endif
873 	case S_RX_ORN:		STAT(rx_orn);
874 	case S_RX_CRC_ERR:	STAT(rx_crcerr);
875 	case S_RX_FIFO_ERR: 	STAT(rx_fifoerr);
876 	case S_RX_CRYPTO_ERR: 	STAT(rx_badcrypt);
877 	case S_RX_MIC_ERR:	STAT(rx_badmic);
878 	case S_RX_PHY_ERR:	STAT(rx_phyerr);
879 	case S_RX_PHY_UNDERRUN:	PHY(HAL_PHYERR_UNDERRUN);
880 	case S_RX_PHY_TIMING:	PHY(HAL_PHYERR_TIMING);
881 	case S_RX_PHY_PARITY:	PHY(HAL_PHYERR_PARITY);
882 	case S_RX_PHY_RATE:	PHY(HAL_PHYERR_RATE);
883 	case S_RX_PHY_LENGTH:	PHY(HAL_PHYERR_LENGTH);
884 	case S_RX_PHY_RADAR:	PHY(HAL_PHYERR_RADAR);
885 	case S_RX_PHY_SERVICE:	PHY(HAL_PHYERR_SERVICE);
886 	case S_RX_PHY_TOR:	PHY(HAL_PHYERR_TOR);
887 	case S_RX_PHY_OFDM_TIMING:	  PHY(HAL_PHYERR_OFDM_TIMING);
888 	case S_RX_PHY_OFDM_SIGNAL_PARITY: PHY(HAL_PHYERR_OFDM_SIGNAL_PARITY);
889 	case S_RX_PHY_OFDM_RATE_ILLEGAL:  PHY(HAL_PHYERR_OFDM_RATE_ILLEGAL);
890 	case S_RX_PHY_OFDM_POWER_DROP:	  PHY(HAL_PHYERR_OFDM_POWER_DROP);
891 	case S_RX_PHY_OFDM_SERVICE:	  PHY(HAL_PHYERR_OFDM_SERVICE);
892 	case S_RX_PHY_OFDM_RESTART:	  PHY(HAL_PHYERR_OFDM_RESTART);
893 	case S_RX_PHY_CCK_TIMING:	  PHY(HAL_PHYERR_CCK_TIMING);
894 	case S_RX_PHY_CCK_HEADER_CRC:	  PHY(HAL_PHYERR_CCK_HEADER_CRC);
895 	case S_RX_PHY_CCK_RATE_ILLEGAL:	  PHY(HAL_PHYERR_CCK_RATE_ILLEGAL);
896 	case S_RX_PHY_CCK_SERVICE:	  PHY(HAL_PHYERR_CCK_SERVICE);
897 	case S_RX_PHY_CCK_RESTART:	  PHY(HAL_PHYERR_CCK_RESTART);
898 	case S_RX_TOOSHORT:	STAT(rx_tooshort);
899 	case S_RX_TOOBIG:	STAT(rx_toobig);
900 	case S_RX_MGT:		STAT(rx_mgt);
901 	case S_RX_CTL:		STAT(rx_ctl);
902 	case S_TX_RSSI:
903 		snprintf(b, bs, "%d", wf->total.ath.ast_tx_rssi);
904 		return 1;
905 	case S_RX_RSSI:
906 		snprintf(b, bs, "%d", wf->total.ath.ast_rx_rssi);
907 		return 1;
908 	case S_BE_XMIT:		STAT(be_xmit);
909 	case S_BE_NOMBUF:	STAT(be_nombuf);
910 	case S_PER_CAL:		STAT(per_cal);
911 	case S_PER_CALFAIL:	STAT(per_calfail);
912 	case S_PER_RFGAIN:	STAT(per_rfgain);
913 #ifdef S_TDMA_UPDATE
914 	case S_TDMA_UPDATE:	STAT(tdma_update);
915 	case S_TDMA_TIMERS:	STAT(tdma_timers);
916 	case S_TDMA_TSF:	STAT(tdma_tsf);
917 	case S_TDMA_TSFADJ:
918 		snprintf(b, bs, "-%d/+%d",
919 		    wf->total.ath.ast_tdma_tsfadjm,
920 		    wf->total.ath.ast_tdma_tsfadjp);
921 		return 1;
922 	case S_TDMA_ACK:	STAT(tdma_ack);
923 #endif
924 	case S_RATE_CALLS:	STAT(rate_calls);
925 	case S_RATE_RAISE:	STAT(rate_raise);
926 	case S_RATE_DROP:	STAT(rate_drop);
927 	case S_ANT_DEFSWITCH:	STAT(ant_defswitch);
928 	case S_ANT_TXSWITCH:	STAT(ant_txswitch);
929 #ifdef S_ANI_NOISE
930 	case S_ANI_NOISE:	ANI(noiseImmunityLevel);
931 	case S_ANI_SPUR:	ANI(spurImmunityLevel);
932 	case S_ANI_STEP:	ANI(firstepLevel);
933 	case S_ANI_OFDM:	ANI(ofdmWeakSigDetectOff);
934 	case S_ANI_CCK:		ANI(cckWeakSigThreshold);
935 	case S_ANI_LISTEN:	ANI(listenTime);
936 	case S_ANI_NIUP:	ANISTAT(niup);
937 	case S_ANI_NIDOWN:	ANISTAT(nidown);
938 	case S_ANI_SIUP:	ANISTAT(spurup);
939 	case S_ANI_SIDOWN:	ANISTAT(spurdown);
940 	case S_ANI_OFDMON:	ANISTAT(ofdmon);
941 	case S_ANI_OFDMOFF:	ANISTAT(ofdmoff);
942 	case S_ANI_CCKHI:	ANISTAT(cckhigh);
943 	case S_ANI_CCKLO:	ANISTAT(ccklow);
944 	case S_ANI_STEPUP:	ANISTAT(stepup);
945 	case S_ANI_STEPDOWN:	ANISTAT(stepdown);
946 	case S_ANI_OFDMERRS:	ANISTAT(ofdmerrs);
947 	case S_ANI_CCKERRS:	ANISTAT(cckerrs);
948 	case S_ANI_RESET:	ANISTAT(reset);
949 	case S_ANI_LZERO:	ANISTAT(lzero);
950 	case S_ANI_LNEG:	ANISTAT(lneg);
951 	case S_MIB_ACKBAD:	MIBSTAT(ackrcv_bad);
952 	case S_MIB_RTSBAD:	MIBSTAT(rts_bad);
953 	case S_MIB_RTSGOOD:	MIBSTAT(rts_good);
954 	case S_MIB_FCSBAD:	MIBSTAT(fcs_bad);
955 	case S_MIB_BEACONS:	MIBSTAT(beacons);
956 	case S_NODE_AVGBRSSI:
957 		snprintf(b, bs, "%u",
958 		    HAL_RSSI(wf->total.ani_stats.ast_nodestats.ns_avgbrssi));
959 		return 1;
960 	case S_NODE_AVGRSSI:
961 		snprintf(b, bs, "%u",
962 		    HAL_RSSI(wf->total.ani_stats.ast_nodestats.ns_avgrssi));
963 		return 1;
964 	case S_NODE_AVGARSSI:
965 		snprintf(b, bs, "%u",
966 		    HAL_RSSI(wf->total.ani_stats.ast_nodestats.ns_avgtxrssi));
967 		return 1;
968 #endif
969 	case S_ANT_TX0:		TXANT(0);
970 	case S_ANT_TX1:		TXANT(1);
971 	case S_ANT_TX2:		TXANT(2);
972 	case S_ANT_TX3:		TXANT(3);
973 	case S_ANT_TX4:		TXANT(4);
974 	case S_ANT_TX5:		TXANT(5);
975 	case S_ANT_TX6:		TXANT(6);
976 	case S_ANT_TX7:		TXANT(7);
977 	case S_ANT_RX0:		RXANT(0);
978 	case S_ANT_RX1:		RXANT(1);
979 	case S_ANT_RX2:		RXANT(2);
980 	case S_ANT_RX3:		RXANT(3);
981 	case S_ANT_RX4:		RXANT(4);
982 	case S_ANT_RX5:		RXANT(5);
983 	case S_ANT_RX6:		RXANT(6);
984 	case S_ANT_RX7:		RXANT(7);
985 #ifdef S_CABQ_XMIT
986 	case S_CABQ_XMIT:	STAT(cabq_xmit);
987 	case S_CABQ_BUSY:	STAT(cabq_busy);
988 #endif
989 	case S_FF_TXOK:		STAT(ff_txok);
990 	case S_FF_TXERR:	STAT(ff_txerr);
991 	case S_FF_RX:		STAT(ff_rx);
992 	case S_FF_FLUSH:	STAT(ff_flush);
993 	case S_TX_QFULL:	STAT(tx_qfull);
994 	case S_RX_NOISE:
995 		snprintf(b, bs, "%d", wf->total.ath.ast_rx_noise);
996 		return 1;
997 	case S_TX_SIGNAL:
998 		snprintf(b, bs, "%d",
999 			wf->total.ath.ast_tx_rssi + wf->total.ath.ast_rx_noise);
1000 		return 1;
1001 	case S_RX_SIGNAL:
1002 		snprintf(b, bs, "%d",
1003 			wf->total.ath.ast_rx_rssi + wf->total.ath.ast_rx_noise);
1004 		return 1;
1005 	case S_RX_AGG:		STAT(rx_agg);
1006 	case S_RX_HALFGI:	STAT(rx_halfgi);
1007 	case S_RX_2040:		STAT(rx_2040);
1008 	case S_RX_PRE_CRC_ERR:	STAT(rx_pre_crc_err);
1009 	case S_RX_POST_CRC_ERR:	STAT(rx_post_crc_err);
1010 	case S_RX_DECRYPT_BUSY_ERR:	STAT(rx_decrypt_busy_err);
1011 	case S_RX_HI_CHAIN:	STAT(rx_hi_rx_chain);
1012 	case S_TX_HTPROTECT:	STAT(tx_htprotect);
1013 	case S_RX_QEND:		STAT(rx_hitqueueend);
1014 	case S_TX_TIMEOUT:	STAT(tx_timeout);
1015 	case S_TX_CSTIMEOUT:	STAT(tx_cst);
1016 	case S_TX_XTXOP_ERR:	STAT(tx_xtxop);
1017 	case S_TX_TIMEREXPIRED_ERR:	STAT(tx_timerexpired);
1018 	case S_TX_DESCCFG_ERR:	STAT(tx_desccfgerr);
1019 	case S_TX_SWRETRIES:	STAT(tx_swretries);
1020 	case S_TX_SWRETRIES_MAX:	STAT(tx_swretrymax);
1021 	case S_TX_DATA_UNDERRUN:	STAT(tx_data_underrun);
1022 	case S_TX_DELIM_UNDERRUN:	STAT(tx_delim_underrun);
1023 	case S_TX_AGGR_OK:		STAT(tx_aggr_ok);
1024 	case S_TX_AGGR_FAIL:		STAT(tx_aggr_fail);
1025 	case S_TX_AGGR_FAILALL:		STAT(tx_aggr_failall);
1026 	}
1027 	b[0] = '\0';
1028 	return 0;
1029 #undef RXANT
1030 #undef TXANT
1031 #undef ANI
1032 #undef ANISTAT
1033 #undef MIBSTAT
1034 #undef PHY
1035 #undef STAT
1036 }
1037 
1038 static void
1039 ath_print_verbose(struct statfoo *sf, FILE *fd)
1040 {
1041 	struct athstatfoo_p *wf = (struct athstatfoo_p *) sf;
1042 #define	isphyerr(i)	(S_PHY_MIN <= i && i <= S_PHY_MAX)
1043 	const struct fmt *f;
1044 	char s[32];
1045 	const char *indent;
1046 	int i, width;
1047 
1048 	width = 0;
1049 	for (i = 0; i < S_LAST; i++) {
1050 		f = &sf->stats[i];
1051 		if (!isphyerr(i) && f->width > width)
1052 			width = f->width;
1053 	}
1054 	for (i = 0; i < S_LAST; i++) {
1055 		if (ath_get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0")) {
1056 			if (isphyerr(i))
1057 				indent = "    ";
1058 			else
1059 				indent = "";
1060 			fprintf(fd, "%s%-*s %s\n", indent, width, s, athstats[i].desc);
1061 		}
1062 	}
1063 	fprintf(fd, "Antenna profile:\n");
1064 	for (i = 0; i < 8; i++)
1065 		if (wf->total.ath.ast_ant_rx[i] || wf->total.ath.ast_ant_tx[i])
1066 			fprintf(fd, "[%u] tx %8u rx %8u\n", i,
1067 				wf->total.ath.ast_ant_tx[i],
1068 				wf->total.ath.ast_ant_rx[i]);
1069 #undef isphyerr
1070 }
1071 
1072 STATFOO_DEFINE_BOUNCE(athstatfoo)
1073 
1074 struct athstatfoo *
1075 athstats_new(const char *ifname, const char *fmtstring)
1076 {
1077 #define	N(a)	(sizeof(a) / sizeof(a[0]))
1078 	struct athstatfoo_p *wf;
1079 
1080 	wf = calloc(1, sizeof(struct athstatfoo_p));
1081 	if (wf != NULL) {
1082 		statfoo_init(&wf->base.base, "athstats", athstats, N(athstats));
1083 		/* override base methods */
1084 		wf->base.base.collect_cur = ath_collect_cur;
1085 		wf->base.base.collect_tot = ath_collect_tot;
1086 		wf->base.base.get_curstat = ath_get_curstat;
1087 		wf->base.base.get_totstat = ath_get_totstat;
1088 		wf->base.base.update_tot = ath_update_tot;
1089 		wf->base.base.print_verbose = ath_print_verbose;
1090 
1091 		/* setup bounce functions for public methods */
1092 		STATFOO_BOUNCE(wf, athstatfoo);
1093 
1094 		/* setup our public methods */
1095 		wf->base.setifname = ath_setifname;
1096 #if 0
1097 		wf->base.setstamac = wlan_setstamac;
1098 #endif
1099 		wf->base.zerostats = ath_zerostats;
1100 		wf->s = socket(AF_INET, SOCK_DGRAM, 0);
1101 		if (wf->s < 0)
1102 			err(1, "socket");
1103 
1104 		ath_setifname(&wf->base, ifname);
1105 		wf->base.setfmt(&wf->base, fmtstring);
1106 	}
1107 	return &wf->base;
1108 #undef N
1109 }
1110