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