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