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