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