5c074242b7123d17f917700ccb9bf1290cf161a8
[dragonfly.git] / sys / dev / netif / ath / hal / ah_osdep.c
1 /*-
2  * Copyright (c) 2002-2008 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: head/sys/dev/ath/ah_osdep.c 196970 2009-09-08 13:19:05Z phk $
30  */
31 #include "opt_ah.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41
42 #include <machine/stdarg.h>
43
44 #include <dev/netif/ath/hal/ath_hal/ah.h>
45
46 /*
47  * WiSoC boards overload the bus tag with information about the
48  * board layout.  We must extract the bus space tag from that
49  * indirect structure.  For everyone else the tag is passed in
50  * directly.
51  * XXX cache indirect ref privately
52  */
53 #ifdef AH_SUPPORT_AR5312
54 #define BUSTAG(ah) \
55         ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
56 #else
57 #define BUSTAG(ah)      ((ah)->ah_st)
58 #endif
59
60 extern  void ath_hal_printf(struct ath_hal *, const char*, ...)
61                 __printflike(2,3);
62 extern  void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
63                 __printflike(2, 0);
64 extern  const char* ath_hal_ether_sprintf(const u_int8_t *mac);
65 extern  void *ath_hal_malloc(size_t);
66 extern  void ath_hal_free(void *);
67 #ifdef AH_ASSERT
68 extern  void ath_hal_assert_failed(const char* filename,
69                 int lineno, const char* msg);
70 #endif
71 #ifdef AH_DEBUG
72 extern  void HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
73 #endif /* AH_DEBUG */
74
75 /* NB: put this here instead of the driver to avoid circular references */
76 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
77 SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters");
78
79 #ifdef AH_DEBUG
80 static  int ath_hal_debug = 0xffffffff;
81 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
82             0, "Atheros HAL debugging printfs");
83 TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
84 #endif /* AH_DEBUG */
85
86 /* NB: these are deprecated; they exist for now for compatibility */
87 int     ath_hal_dma_beacon_response_time = 2;   /* in TU's */
88 SYSCTL_INT(_hw_ath_hal, OID_AUTO, dma_brt, CTLFLAG_RW,
89            &ath_hal_dma_beacon_response_time, 0,
90            "Atheros HAL DMA beacon response time");
91 int     ath_hal_sw_beacon_response_time = 10;   /* in TU's */
92 SYSCTL_INT(_hw_ath_hal, OID_AUTO, sw_brt, CTLFLAG_RW,
93            &ath_hal_sw_beacon_response_time, 0,
94            "Atheros HAL software beacon response time");
95 int     ath_hal_additional_swba_backoff = 0;    /* in TU's */
96 SYSCTL_INT(_hw_ath_hal, OID_AUTO, swba_backoff, CTLFLAG_RW,
97            &ath_hal_additional_swba_backoff, 0,
98            "Atheros HAL additional SWBA backoff time");
99
100 MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
101
102 void*
103 ath_hal_malloc(size_t size)
104 {
105         return kmalloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
106 }
107
108 void
109 ath_hal_free(void* p)
110 {
111         kfree(p, M_ATH_HAL);
112 }
113
114 void
115 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, __va_list ap)
116 {
117         kvprintf(fmt, ap);
118 }
119
120 void
121 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
122 {
123         __va_list ap;
124         __va_start(ap, fmt);
125         ath_hal_vprintf(ah, fmt, ap);
126         __va_end(ap);
127 }
128
129 const char*
130 ath_hal_ether_sprintf(const u_int8_t *mac)
131 {
132         static char etherbuf[18];
133
134         ksnprintf(etherbuf, sizeof(etherbuf), "%6D", mac, ":");
135         return etherbuf;
136 }
137
138 #ifdef AH_DEBUG
139 void
140 HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
141 {
142         if (ath_hal_debug & mask) {
143                 __va_list ap;
144                 __va_start(ap, fmt);
145                 ath_hal_vprintf(ah, fmt, ap);
146                 __va_end(ap);
147         }
148 }
149 #endif /* AH_DEBUG */
150
151 #ifdef AH_DEBUG_ALQ
152 /*
153  * ALQ register tracing support.
154  *
155  * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
156  * writes to the file /tmp/ath_hal.log.  The file format is a simple
157  * fixed-size array of records.  When done logging set hw.ath.hal.alq=0
158  * and then decode the file with the arcode program (that is part of the
159  * HAL).  If you start+stop tracing the data will be appended to an
160  * existing file.
161  *
162  * NB: doesn't handle multiple devices properly; only one DEVICE record
163  *     is emitted and the different devices are not identified.
164  */
165 #include <sys/alq.h>
166 #include <sys/pcpu.h>
167 #include <dev/netif/ath/hal/ath_hal/ah_decode.h>
168
169 static  struct alq *ath_hal_alq;
170 static  int ath_hal_alq_emitdev;        /* need to emit DEVICE record */
171 static  u_int ath_hal_alq_lost;         /* count of lost records */
172 static  const char *ath_hal_logfile = "/tmp/ath_hal.log";
173 static  u_int ath_hal_alq_qsize = 64*1024;
174
175 static int
176 ath_hal_setlogging(int enable)
177 {
178         int error;
179
180         if (enable) {
181                 error = alq_open(&ath_hal_alq, ath_hal_logfile,
182                         curthread->td_ucred, ALQ_DEFAULT_CMODE,
183                         sizeof (struct athregrec), ath_hal_alq_qsize);
184                 ath_hal_alq_lost = 0;
185                 ath_hal_alq_emitdev = 1;
186                 printf("ath_hal: logging to %s enabled\n",
187                         ath_hal_logfile);
188         } else {
189                 if (ath_hal_alq)
190                         alq_close(ath_hal_alq);
191                 ath_hal_alq = NULL;
192                 printf("ath_hal: logging disabled\n");
193                 error = 0;
194         }
195         return (error);
196 }
197
198 static int
199 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
200 {
201         int error, enable;
202
203         enable = (ath_hal_alq != NULL);
204         error = sysctl_handle_int(oidp, &enable, 0, req);
205         if (error || !req->newptr)
206                 return (error);
207         else
208                 return (ath_hal_setlogging(enable));
209 }
210 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
211         0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
212 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
213         &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
214 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
215         &ath_hal_alq_lost, 0, "Register operations not logged");
216
217 static struct ale *
218 ath_hal_alq_get(struct ath_hal *ah)
219 {
220         struct ale *ale;
221
222         if (ath_hal_alq_emitdev) {
223                 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
224                 if (ale) {
225                         struct athregrec *r =
226                                 (struct athregrec *) ale->ae_data;
227                         r->op = OP_DEVICE;
228                         r->reg = 0;
229                         r->val = ah->ah_devid;
230                         alq_post(ath_hal_alq, ale);
231                         ath_hal_alq_emitdev = 0;
232                 } else
233                         ath_hal_alq_lost++;
234         }
235         ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
236         if (!ale)
237                 ath_hal_alq_lost++;
238         return ale;
239 }
240
241 void
242 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
243 {
244         bus_space_tag_t tag = BUSTAG(ah);
245         bus_space_handle_t h = ah->ah_sh;
246
247         if (ath_hal_alq) {
248                 struct ale *ale = ath_hal_alq_get(ah);
249                 if (ale) {
250                         struct athregrec *r = (struct athregrec *) ale->ae_data;
251                         r->op = OP_WRITE;
252                         r->reg = reg;
253                         r->val = val;
254                         alq_post(ath_hal_alq, ale);
255                 }
256         }
257 #if _BYTE_ORDER == _BIG_ENDIAN
258         if (OS_REG_UNSWAPPED(reg))
259                 bus_space_write_4(tag, h, reg, val);
260         else
261 #endif
262                 bus_space_write_stream_4(tag, h, reg, val);
263 }
264
265 u_int32_t
266 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
267 {
268         bus_space_tag_t tag = BUSTAG(ah);
269         bus_space_handle_t h = ah->ah_sh;
270         u_int32_t val;
271
272 #if _BYTE_ORDER == _BIG_ENDIAN
273         if (OS_REG_UNSWAPPED(reg))
274                 val = bus_space_read_4(tag, h, reg);
275         else
276 #endif
277                 val = bus_space_read_stream_4(tag, h, reg);
278         if (ath_hal_alq) {
279                 struct ale *ale = ath_hal_alq_get(ah);
280                 if (ale) {
281                         struct athregrec *r = (struct athregrec *) ale->ae_data;
282                         r->op = OP_READ;
283                         r->reg = reg;
284                         r->val = val;
285                         alq_post(ath_hal_alq, ale);
286                 }
287         }
288         return val;
289 }
290
291 void
292 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
293 {
294         if (ath_hal_alq) {
295                 struct ale *ale = ath_hal_alq_get(ah);
296                 if (ale) {
297                         struct athregrec *r = (struct athregrec *) ale->ae_data;
298                         r->op = OP_MARK;
299                         r->reg = id;
300                         r->val = v;
301                         alq_post(ath_hal_alq, ale);
302                 }
303         }
304 }
305 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
306 /*
307  * Memory-mapped device register read/write.  These are here
308  * as routines when debugging support is enabled and/or when
309  * explicitly configured to use function calls.  The latter is
310  * for architectures that might need to do something before
311  * referencing memory (e.g. remap an i/o window).
312  *
313  * NB: see the comments in ah_osdep.h about byte-swapping register
314  *     reads and writes to understand what's going on below.
315  */
316
317 void
318 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
319 {
320         bus_space_tag_t tag = BUSTAG(ah);
321         bus_space_handle_t h = ah->ah_sh;
322
323 #if _BYTE_ORDER == _BIG_ENDIAN
324         if (OS_REG_UNSWAPPED(reg))
325                 bus_space_write_4(tag, h, reg, val);
326         else
327 #endif
328                 bus_space_write_stream_4(tag, h, reg, val);
329 }
330
331 u_int32_t
332 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
333 {
334         bus_space_tag_t tag = BUSTAG(ah);
335         bus_space_handle_t h = ah->ah_sh;
336         u_int32_t val;
337
338 #if _BYTE_ORDER == _BIG_ENDIAN
339         if (OS_REG_UNSWAPPED(reg))
340                 val = bus_space_read_4(tag, h, reg);
341         else
342 #endif
343                 val = bus_space_read_stream_4(tag, h, reg);
344         return val;
345 }
346 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
347
348 #ifdef AH_ASSERT
349 void
350 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
351 {
352         printf("Atheros HAL assertion failure: %s: line %u: %s\n",
353                 filename, lineno, msg);
354         panic("ath_hal_assert");
355 }
356 #endif /* AH_ASSERT */
357
358 /*
359  * Module glue.
360  */
361 static int
362 ath_hal_modevent(module_t mod, int type, void *unused)
363 {
364         switch (type) {
365         case MOD_LOAD:
366                 return 0;
367         case MOD_UNLOAD:
368                 return 0;
369         }
370
371         return EINVAL;
372 }
373
374 static moduledata_t ath_hal_mod = {
375         "ath_hal",
376         ath_hal_modevent,
377         0
378 };
379
380 DECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
381 MODULE_VERSION(ath_hal, 1);