ah_osdep.c revision 1.2 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 * $Id: ah_osdep.c,v 1.2 2009/03/24 21:36:46 martin Exp $
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ah_osdep.c,v 1.2 2009/03/24 21:36:46 martin Exp $");
34
35 #include "opt_athhal.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/kauth.h>
44
45 #include <machine/stdarg.h>
46
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_arp.h>
51 #include <net/if_ether.h>
52
53 #include <external/isc/atheros_hal/dist/ah.h>
54
55 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
56 __printflike(2,3);
57 extern void ath_hal_vprintf(struct ath_hal *, const char*, va_list)
58 __printflike(2, 0);
59 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
60 extern void *ath_hal_malloc(size_t);
61 extern void ath_hal_free(void *);
62 #ifdef ATHHAL_ASSERT
63 extern void ath_hal_assert_failed(const char* filename,
64 int lineno, const char* msg);
65 #endif
66 #ifdef ATHHAL_DEBUG
67 extern void HALDEBUG(struct ath_hal *ah, const char* fmt, ...);
68 extern void HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...);
69 #endif /* ATHHAL_DEBUG */
70
71 #ifdef ATHHAL_DEBUG
72 static int ath_hal_debug = 0;
73 #endif /* ATHHAL_DEBUG */
74
75 int ath_hal_dma_beacon_response_time = 2; /* in TU's */
76 int ath_hal_sw_beacon_response_time = 10; /* in TU's */
77 int ath_hal_additional_swba_backoff = 0; /* in TU's */
78
79 SYSCTL_SETUP(sysctl_ath_hal, "sysctl ath.hal subtree setup")
80 {
81 int rc;
82 const struct sysctlnode *cnode, *rnode;
83
84 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
85 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
86 goto err;
87
88 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode, CTLFLAG_PERMANENT,
89 CTLTYPE_NODE, "ath", SYSCTL_DESCR("Atheros driver parameters"),
90 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
91 goto err;
92
93 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode, CTLFLAG_PERMANENT,
94 CTLTYPE_NODE, "hal", SYSCTL_DESCR("Atheros HAL parameters"),
95 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
96 goto err;
97
98 #if 0
99 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
100 CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING, "version",
101 SYSCTL_DESCR("Atheros HAL version"), NULL, 0, &ath_hal_version, 0,
102 CTL_CREATE, CTL_EOL)) != 0)
103 goto err;
104 #endif
105
106 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
107 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "dma_brt",
108 SYSCTL_DESCR("Atheros HAL DMA beacon response time"), NULL, 0,
109 &ath_hal_dma_beacon_response_time, 0, CTL_CREATE, CTL_EOL)) != 0)
110 goto err;
111
112 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
113 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "sw_brt",
114 SYSCTL_DESCR("Atheros HAL software beacon response time"), NULL, 0,
115 &ath_hal_sw_beacon_response_time, 0, CTL_CREATE, CTL_EOL)) != 0)
116 goto err;
117
118 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
119 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "swba_backoff",
120 SYSCTL_DESCR("Atheros HAL additional SWBA backoff time"), NULL, 0,
121 &ath_hal_additional_swba_backoff, 0, CTL_CREATE, CTL_EOL)) != 0)
122 goto err;
123
124 #ifdef ATHHAL_DEBUG
125 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
126 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
127 SYSCTL_DESCR("Atheros HAL debugging printfs"), NULL, 0,
128 &ath_hal_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
129 goto err;
130 #endif /* ATHHAL_DEBUG */
131 return;
132 err:
133 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
134 }
135
136 MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
137
138 void*
139 ath_hal_malloc(size_t size)
140 {
141 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
142 }
143
144 void
145 ath_hal_free(void* p)
146 {
147 free(p, M_ATH_HAL);
148 }
149
150 void
151 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
152 {
153 vprintf(fmt, ap);
154 }
155
156 void
157 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
158 {
159 va_list ap;
160 va_start(ap, fmt);
161 ath_hal_vprintf(ah, fmt, ap);
162 va_end(ap);
163 }
164
165 const char*
166 ath_hal_ether_sprintf(const u_int8_t *mac)
167 {
168 return ether_sprintf(mac);
169 }
170
171 #ifdef ATHHAL_DEBUG
172 void
173 HALDEBUG(struct ath_hal *ah, const char* fmt, ...)
174 {
175 if (ath_hal_debug) {
176 va_list ap;
177 va_start(ap, fmt);
178 ath_hal_vprintf(ah, fmt, ap);
179 va_end(ap);
180 }
181 }
182
183 void
184 HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...)
185 {
186 if (ath_hal_debug >= level) {
187 va_list ap;
188 va_start(ap, fmt);
189 ath_hal_vprintf(ah, fmt, ap);
190 va_end(ap);
191 }
192 }
193 #endif /* ATHHAL_DEBUG */
194
195 #ifdef ATHHAL_DEBUG_ALQ
196 /*
197 * ALQ register tracing support.
198 *
199 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
200 * writes to the file /tmp/ath_hal.log. The file format is a simple
201 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
202 * and then decode the file with the arcode program (that is part of the
203 * HAL). If you start+stop tracing the data will be appended to an
204 * existing file.
205 *
206 * NB: doesn't handle multiple devices properly; only one DEVICE record
207 * is emitted and the different devices are not identified.
208 */
209 #include <sys/alq.h>
210 #include <sys/pcpu.h>
211 #include <contrib/dev/ath/ah_decode.h>
212
213 static struct alq *ath_hal_alq;
214 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
215 static u_int ath_hal_alq_lost; /* count of lost records */
216 static const char *ath_hal_logfile = "/tmp/ath_hal.log";
217 static u_int ath_hal_alq_qsize = 64*1024;
218
219 static int
220 ath_hal_setlogging(int enable)
221 {
222 int error;
223
224 if (enable) {
225 error = kauth_authorize_network(curlwp->l_cred,
226 KAUTH_NETWORK_INTERFACE,
227 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, NULL, NULL, NULL);
228 if (error == 0) {
229 error = alq_open(&ath_hal_alq, ath_hal_logfile,
230 curproc->p_ucred,
231 sizeof (struct athregrec), ath_hal_alq_qsize);
232 ath_hal_alq_lost = 0;
233 ath_hal_alq_emitdev = 1;
234 printf("ath_hal: logging to %s enabled\n",
235 ath_hal_logfile);
236 }
237 } else {
238 if (ath_hal_alq)
239 alq_close(ath_hal_alq);
240 ath_hal_alq = NULL;
241 printf("ath_hal: logging disabled\n");
242 error = 0;
243 }
244 return (error);
245 }
246
247 static int
248 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
249 {
250 int error, enable;
251
252 enable = (ath_hal_alq != NULL);
253 error = sysctl_handle_int(oidp, &enable, 0, req);
254 if (error || !req->newptr)
255 return (error);
256 else
257 return (ath_hal_setlogging(enable));
258 }
259 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
260 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
261 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
262 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
263 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
264 &ath_hal_alq_lost, 0, "Register operations not logged");
265
266 static struct ale *
267 ath_hal_alq_get(struct ath_hal *ah)
268 {
269 struct ale *ale;
270
271 if (ath_hal_alq_emitdev) {
272 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
273 if (ale) {
274 struct athregrec *r =
275 (struct athregrec *) ale->ae_data;
276 r->op = OP_DEVICE;
277 r->reg = 0;
278 r->val = ah->ah_devid;
279 alq_post(ath_hal_alq, ale);
280 ath_hal_alq_emitdev = 0;
281 } else
282 ath_hal_alq_lost++;
283 }
284 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
285 if (!ale)
286 ath_hal_alq_lost++;
287 return ale;
288 }
289
290 void
291 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
292 {
293 bus_space_tag_t t = BUSTAG(ah);
294
295 if (ath_hal_alq) {
296 struct ale *ale = ath_hal_alq_get(ah);
297 if (ale) {
298 struct athregrec *r = (struct athregrec *) ale->ae_data;
299 r->op = OP_WRITE;
300 r->reg = reg;
301 r->val = val;
302 alq_post(ath_hal_alq, ale);
303 }
304 }
305 #if _BYTE_ORDER == _BIG_ENDIAN
306 if (reg >= 0x4000 && reg < 0x5000)
307 bus_space_write_4(t, h, reg, val);
308 else
309 #endif
310 bus_space_write_stream_4(t, h, reg, val);
311 }
312
313 u_int32_t
314 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
315 {
316 u_int32_t val;
317 bus_space_handle_t h = BUSHANDLE(ah);
318 bus_space_tag_t t = BUSTAG(ah);
319
320 #if _BYTE_ORDER == _BIG_ENDIAN
321 if (reg >= 0x4000 && reg < 0x5000)
322 val = bus_space_read_4(t, h, reg);
323 else
324 #endif
325 val = bus_space_read_stream_4(t, h, reg);
326
327 if (ath_hal_alq) {
328 struct ale *ale = ath_hal_alq_get(ah);
329 if (ale) {
330 struct athregrec *r = (struct athregrec *) ale->ae_data;
331 r->op = OP_READ;
332 r->reg = reg;
333 r->val = val;
334 alq_post(ath_hal_alq, ale);
335 }
336 }
337
338 return val;
339 }
340
341 void
342 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
343 {
344 if (ath_hal_alq) {
345 struct ale *ale = ath_hal_alq_get(ah);
346
347 if (ale) {
348 struct athregrec *r = (struct athregrec *) ale->ae_data;
349 r->op = OP_MARK;
350 r->reg = id;
351 r->val = v;
352 alq_post(ath_hal_alq, ale);
353 }
354 }
355 }
356 #elif defined(ATHHAL_DEBUG) || defined(AH_REGOPS_FUNC)
357 /*
358 * Memory-mapped device register read/write. These are here
359 * as routines when debugging support is enabled and/or when
360 * explicitly configured to use function calls. The latter is
361 * for architectures that might need to do something before
362 * referencing memory (e.g. remap an i/o window).
363 *
364 * NB: see the comments in ah_osdep.h about byte-swapping register
365 * reads and writes to understand what's going on below.
366 */
367
368 void
369 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
370 {
371 bus_space_handle_t h = BUSHANDLE(ah);
372 bus_space_tag_t t = BUSTAG(ah);
373
374 #if _BYTE_ORDER == _BIG_ENDIAN
375 if (reg >= 0x4000 && reg < 0x5000)
376 bus_space_write_4(t, h, reg, val);
377 else
378 #endif
379 bus_space_write_stream_4(t, h, reg, val);
380 }
381
382 u_int32_t
383 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
384 {
385 bus_space_handle_t h = BUSHANDLE(ah);
386 bus_space_tag_t t = BUSTAG(ah);
387 uint32_t ret;
388
389 #if _BYTE_ORDER == _BIG_ENDIAN
390 if (reg >= 0x4000 && reg < 0x5000)
391 ret = bus_space_read_4(t, h, reg);
392 else
393 #endif
394 ret = bus_space_read_stream_4(t, h, reg);
395
396 return ret;
397 }
398 #endif /* ATHHAL_DEBUG || AH_REGOPS_FUNC */
399
400 #ifdef ATHHAL_ASSERT
401 void
402 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
403 {
404 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
405 filename, lineno, msg);
406 panic("ath_hal_assert");
407 }
408 #endif /* ATHHAL_ASSERT */
409
410 /*
411 * Delay n microseconds.
412 */
413 void
414 ath_hal_delay(int n)
415 {
416 DELAY(n);
417 }
418
419 u_int32_t
420 ath_hal_getuptime(struct ath_hal *ah)
421 {
422 struct bintime bt;
423 uint32_t ret;
424 getbinuptime(&bt);
425 ret = (bt.sec * 1000) +
426 (((uint64_t)1000 * (uint32_t)(bt.frac >> 32)) >> 32);
427 return ret;
428 }
429
430 void
431 ath_hal_memzero(void *dst, size_t n)
432 {
433 (void)memset(dst, 0, n);
434 }
435
436 void *
437 ath_hal_memcpy(void *dst, const void *src, size_t n)
438 {
439 return memcpy(dst, src, n);
440 }
441