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