- Add noise floor to stats
[dragonfly.git] / sys / dev / netif / ath / hal / ah_osdep.c
1 /*-
2  * Copyright (c) 2002-2006 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  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD: src/sys/dev/ath/ah_osdep.c,v 1.1 2006/09/18 16:49:14 sam Exp $
37  * $DragonFly: src/sys/dev/netif/ath/hal/ah_osdep.c,v 1.1 2007/02/22 05:17:09 sephe Exp $
38  */
39 #include "opt_ah.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/bus.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49
50 #include <machine/stdarg.h>
51
52 #include <net/ethernet.h>               /* XXX for ether_sprintf */
53
54 #include <contrib/dev/ath/ah.h>
55
56 /*
57  * WiSoC boards overload the bus tag with information about the
58  * board layout.  We must extract the bus space tag from that
59  * indirect structure.  For everyone else the tag is passed in
60  * directly.
61  * XXX cache indirect ref privately
62  */
63 #ifdef AH_SUPPORT_AR5312
64 #define BUSTAG(ah) \
65         ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
66 #else
67 #define BUSTAG(ah)      ((bus_space_tag_t) (ah)->ah_st)
68 #endif
69
70 extern  void ath_hal_printf(struct ath_hal *, const char*, ...)
71                 __printflike(2,3);
72 extern  void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
73                 __printflike(2, 0);
74 extern  const char* ath_hal_ether_sprintf(const u_int8_t *mac);
75 extern  void *ath_hal_malloc(size_t);
76 extern  void ath_hal_free(void *);
77 #ifdef AH_ASSERT
78 extern  void ath_hal_assert_failed(const char* filename,
79                 int lineno, const char* msg);
80 #endif
81 #ifdef AH_DEBUG
82 extern  void HALDEBUG(struct ath_hal *ah, const char* fmt, ...);
83 extern  void HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...);
84 #endif /* AH_DEBUG */
85
86 /* NB: put this here instead of the driver to avoid circular references */
87 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
88 SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters");
89
90 #ifdef AH_DEBUG
91 static  int ath_hal_debug = 0;
92 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
93             0, "Atheros HAL debugging printfs");
94 TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
95 #endif /* AH_DEBUG */
96
97 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, version, CTLFLAG_RD, ath_hal_version, 0,
98         "Atheros HAL version");
99
100 /* NB: these are deprecated; they exist for now for compatibility */
101 int     ath_hal_dma_beacon_response_time = 2;   /* in TU's */
102 SYSCTL_INT(_hw_ath_hal, OID_AUTO, dma_brt, CTLFLAG_RW,
103            &ath_hal_dma_beacon_response_time, 0,
104            "Atheros HAL DMA beacon response time");
105 int     ath_hal_sw_beacon_response_time = 10;   /* in TU's */
106 SYSCTL_INT(_hw_ath_hal, OID_AUTO, sw_brt, CTLFLAG_RW,
107            &ath_hal_sw_beacon_response_time, 0,
108            "Atheros HAL software beacon response time");
109 int     ath_hal_additional_swba_backoff = 0;    /* in TU's */
110 SYSCTL_INT(_hw_ath_hal, OID_AUTO, swba_backoff, CTLFLAG_RW,
111            &ath_hal_additional_swba_backoff, 0,
112            "Atheros HAL additional SWBA backoff time");
113
114 MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
115
116 void*
117 ath_hal_malloc(size_t size)
118 {
119         return kmalloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
120 }
121
122 void
123 ath_hal_free(void* p)
124 {
125         return kfree(p, M_ATH_HAL);
126 }
127
128 void
129 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, __va_list ap)
130 {
131         kvprintf(fmt, ap);
132 }
133
134 void
135 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
136 {
137         __va_list ap;
138         __va_start(ap, fmt);
139         ath_hal_vprintf(ah, fmt, ap);
140         __va_end(ap);
141 }
142
143 const char*
144 ath_hal_ether_sprintf(const u_int8_t *mac)
145 {
146         static char etherbuf[18];
147
148         ksnprintf(etherbuf, sizeof(etherbuf), "%6D", mac, ":");
149         return etherbuf;
150 }
151
152 #ifdef AH_DEBUG
153 void
154 HALDEBUG(struct ath_hal *ah, const char* fmt, ...)
155 {
156         if (ath_hal_debug) {
157                 __va_list ap;
158                 __va_start(ap, fmt);
159                 ath_hal_vprintf(ah, fmt, ap);
160                 __va_end(ap);
161         }
162 }
163
164 void
165 HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...)
166 {
167         if (ath_hal_debug >= level) {
168                 __va_list ap;
169                 va_start(ap, fmt);
170                 ath_hal_vprintf(ah, fmt, ap);
171                 va_end(ap);
172         }
173 }
174 #endif /* AH_DEBUG */
175
176 #ifdef AH_DEBUG_ALQ
177 /*
178  * ALQ register tracing support.
179  *
180  * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
181  * writes to the file /tmp/ath_hal.log.  The file format is a simple
182  * fixed-size array of records.  When done logging set hw.ath.hal.alq=0
183  * and then decode the file with the arcode program (that is part of the
184  * HAL).  If you start+stop tracing the data will be appended to an
185  * existing file.
186  *
187  * NB: doesn't handle multiple devices properly; only one DEVICE record
188  *     is emitted and the different devices are not identified.
189  */
190 #include <sys/alq.h>
191 #include <sys/pcpu.h>
192 #include <contrib/dev/ath/ah_decode.h>
193
194 static  struct alq *ath_hal_alq;
195 static  int ath_hal_alq_emitdev;        /* need to emit DEVICE record */
196 static  u_int ath_hal_alq_lost;         /* count of lost records */
197 static  const char *ath_hal_logfile = "/tmp/ath_hal.log";
198 static  u_int ath_hal_alq_qsize = 64*1024;
199
200 static int
201 ath_hal_setlogging(int enable)
202 {
203         int error;
204
205         if (enable) {
206                 error = suser(curthread);
207                 if (error == 0) {
208                         error = alq_open(&ath_hal_alq, ath_hal_logfile,
209                                 curthread->td_ucred, ALQ_DEFAULT_CMODE,
210                                 sizeof (struct athregrec), ath_hal_alq_qsize);
211                         ath_hal_alq_lost = 0;
212                         ath_hal_alq_emitdev = 1;
213                         kprintf("ath_hal: logging to %s enabled\n",
214                                 ath_hal_logfile);
215                 }
216         } else {
217                 if (ath_hal_alq)
218                         alq_close(ath_hal_alq);
219                 ath_hal_alq = NULL;
220                 kprintf("ath_hal: logging disabled\n");
221                 error = 0;
222         }
223         return (error);
224 }
225
226 static int
227 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
228 {
229         int error, enable;
230
231         enable = (ath_hal_alq != NULL);
232         error = sysctl_handle_int(oidp, &enable, 0, req);
233         if (error || !req->newptr)
234                 return (error);
235         else
236                 return (ath_hal_setlogging(enable));
237 }
238 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
239         0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
240 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
241         &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
242 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
243         &ath_hal_alq_lost, 0, "Register operations not logged");
244
245 static struct ale *
246 ath_hal_alq_get(struct ath_hal *ah)
247 {
248         struct ale *ale;
249
250         if (ath_hal_alq_emitdev) {
251                 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
252                 if (ale) {
253                         struct athregrec *r =
254                                 (struct athregrec *) ale->ae_data;
255                         r->op = OP_DEVICE;
256                         r->reg = 0;
257                         r->val = ah->ah_devid;
258                         alq_post(ath_hal_alq, ale);
259                         ath_hal_alq_emitdev = 0;
260                 } else
261                         ath_hal_alq_lost++;
262         }
263         ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
264         if (!ale)
265                 ath_hal_alq_lost++;
266         return ale;
267 }
268
269 void
270 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
271 {
272         bus_space_tag_t tag = BUSTAG(ah);
273         bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh;
274
275         if (ath_hal_alq) {
276                 struct ale *ale = ath_hal_alq_get(ah);
277                 if (ale) {
278                         struct athregrec *r = (struct athregrec *) ale->ae_data;
279                         r->op = OP_WRITE;
280                         r->reg = reg;
281                         r->val = val;
282                         alq_post(ath_hal_alq, ale);
283                 }
284         }
285 #if _BYTE_ORDER == _BIG_ENDIAN
286         if (reg >= 0x4000 && reg < 0x5000)
287                 bus_space_write_4(tag, h, reg, val);
288         else
289 #endif
290                 bus_space_write_stream_4(tag, h, reg, val);
291 }
292
293 u_int32_t
294 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
295 {
296         bus_space_tag_t tag = BUSTAG(ah);
297         bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh;
298         u_int32_t val;
299
300 #if _BYTE_ORDER == _BIG_ENDIAN
301         if (reg >= 0x4000 && reg < 0x5000)
302                 val = bus_space_read_4(tag, h, reg);
303         else
304 #endif
305                 val = bus_space_read_stream_4(tag, h, reg);
306         if (ath_hal_alq) {
307                 struct ale *ale = ath_hal_alq_get(ah);
308                 if (ale) {
309                         struct athregrec *r = (struct athregrec *) ale->ae_data;
310                         r->op = OP_READ;
311                         r->reg = reg;
312                         r->val = val;
313                         alq_post(ath_hal_alq, ale);
314                 }
315         }
316         return val;
317 }
318
319 void
320 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
321 {
322         if (ath_hal_alq) {
323                 struct ale *ale = ath_hal_alq_get(ah);
324                 if (ale) {
325                         struct athregrec *r = (struct athregrec *) ale->ae_data;
326                         r->op = OP_MARK;
327                         r->reg = id;
328                         r->val = v;
329                         alq_post(ath_hal_alq, ale);
330                 }
331         }
332 }
333 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
334 /*
335  * Memory-mapped device register read/write.  These are here
336  * as routines when debugging support is enabled and/or when
337  * explicitly configured to use function calls.  The latter is
338  * for architectures that might need to do something before
339  * referencing memory (e.g. remap an i/o window).
340  *
341  * NB: see the comments in ah_osdep.h about byte-swapping register
342  *     reads and writes to understand what's going on below.
343  */
344
345 void
346 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
347 {
348         bus_space_tag_t tag = BUSTAG(ah);
349         bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh;
350
351 #if _BYTE_ORDER == _BIG_ENDIAN
352         if (reg >= 0x4000 && reg < 0x5000)
353                 bus_space_write_4(tag, h, reg, val);
354         else
355 #endif
356                 bus_space_write_stream_4(tag, h, reg, val);
357 }
358
359 u_int32_t
360 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
361 {
362         bus_space_tag_t tag = BUSTAG(ah);
363         bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh;
364         u_int32_t val;
365
366 #if _BYTE_ORDER == _BIG_ENDIAN
367         if (reg >= 0x4000 && reg < 0x5000)
368                 val = bus_space_read_4(tag, h, reg);
369         else
370 #endif
371                 val = bus_space_read_stream_4(tag, h, reg);
372         return val;
373 }
374 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
375
376 #ifdef AH_ASSERT
377 void
378 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
379 {
380         kprintf("Atheros HAL assertion failure: %s: line %u: %s\n",
381                 filename, lineno, msg);
382         panic("ath_hal_assert");
383 }
384 #endif /* AH_ASSERT */
385
386 /*
387  * Delay n microseconds.
388  */
389 void
390 ath_hal_delay(int n)
391 {
392         DELAY(n);
393 }
394
395 u_int32_t
396 ath_hal_getuptime(struct ath_hal *ah)
397 {
398         struct timeval tv;
399
400         getmicrouptime(&tv);
401         return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
402 }
403
404 void
405 ath_hal_memzero(void *dst, size_t n)
406 {
407         bzero(dst, n);
408 }
409
410 void *
411 ath_hal_memcpy(void *dst, const void *src, size_t n)
412 {
413         return memcpy(dst, src, n);
414 }
415
416 /*
417  * Module glue.
418  */
419
420 static int
421 ath_hal_modevent(module_t mod, int type, void *unused)
422 {
423         const char *sep;
424         int i;
425
426         switch (type) {
427         case MOD_LOAD:
428                 kprintf("ath_hal: %s (", ath_hal_version);
429                 sep = "";
430                 for (i = 0; ath_hal_buildopts[i] != NULL; i++) {
431                         kprintf("%s%s", sep, ath_hal_buildopts[i]);
432                         sep = ", ";
433                 }
434                 kprintf(")\n");
435                 return 0;
436         case MOD_UNLOAD:
437                 return 0;
438         }
439         return EINVAL;
440 }
441
442 static moduledata_t ath_hal_mod = {
443         "ath_hal",
444         ath_hal_modevent,
445         0
446 };
447 DECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
448 MODULE_VERSION(ath_hal, 1);