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