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