2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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>
39 #include <sys/malloc.h>
43 #include <sys/mutex.h>
45 #include <machine/stdarg.h>
47 #include <net/ethernet.h> /* XXX for ether_sprintf */
49 #include <dev/ath/ath_hal/ah.h>
50 #include <dev/ath/ath_hal/ah_debug.h>
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
57 * XXX cache indirect ref privately
59 #ifdef AH_SUPPORT_AR5312
61 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
63 #define BUSTAG(ah) ((ah)->ah_st)
67 * This lock is used to seralise register access for chips which have
68 * problems w/ SMP CPUs issuing concurrent PCI transactions.
70 * XXX This is a global lock for now; it should be pushed to
71 * a per-device lock in some platform-independent fashion.
73 struct mtx ah_regser_mtx;
74 MTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex",
77 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
79 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
81 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
82 extern void *ath_hal_malloc(size_t);
83 extern void ath_hal_free(void *);
85 extern void ath_hal_assert_failed(const char* filename,
86 int lineno, const char* msg);
89 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
92 /* NB: put this here instead of the driver to avoid circular references */
93 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
94 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
95 "Atheros HAL parameters");
98 int ath_hal_debug = 0;
99 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
100 0, "Atheros HAL debugging printfs");
101 TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
102 #endif /* AH_DEBUG */
104 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
107 ath_hal_malloc(size_t size)
109 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
113 ath_hal_free(void* p)
119 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
125 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
129 ath_hal_vprintf(ah, fmt, ap);
134 ath_hal_ether_sprintf(const u_int8_t *mac)
136 return ether_sprintf(mac);
142 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
144 if ((mask == HAL_DEBUG_UNMASKABLE) ||
145 (ah != NULL && ah->ah_config.ah_debug & mask) ||
146 (ath_hal_debug & mask)) {
149 ath_hal_vprintf(ah, fmt, ap);
153 #undef HAL_DEBUG_UNMASKABLE
154 #endif /* AH_DEBUG */
158 * ALQ register tracing support.
160 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
161 * writes to the file /tmp/ath_hal.log. The file format is a simple
162 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
163 * and then decode the file with the arcode program (that is part of the
164 * HAL). If you start+stop tracing the data will be appended to an
167 * NB: doesn't handle multiple devices properly; only one DEVICE record
168 * is emitted and the different devices are not identified.
171 #include <sys/pcpu.h>
172 #include <dev/ath/ath_hal/ah_decode.h>
174 static struct alq *ath_hal_alq;
175 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
176 static u_int ath_hal_alq_lost; /* count of lost records */
177 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
179 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
180 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
182 static u_int ath_hal_alq_qsize = 64*1024;
185 ath_hal_setlogging(int enable)
190 error = alq_open(&ath_hal_alq, ath_hal_logfile,
191 curthread->td_ucred, ALQ_DEFAULT_CMODE,
192 sizeof (struct athregrec), ath_hal_alq_qsize);
193 ath_hal_alq_lost = 0;
194 ath_hal_alq_emitdev = 1;
195 printf("ath_hal: logging to %s enabled\n",
199 alq_close(ath_hal_alq);
201 printf("ath_hal: logging disabled\n");
208 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
212 enable = (ath_hal_alq != NULL);
213 error = sysctl_handle_int(oidp, &enable, 0, req);
214 if (error || !req->newptr)
217 return (ath_hal_setlogging(enable));
219 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
220 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
221 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
222 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
223 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
224 &ath_hal_alq_lost, 0, "Register operations not logged");
227 ath_hal_alq_get(struct ath_hal *ah)
231 if (ath_hal_alq_emitdev) {
232 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
234 struct athregrec *r =
235 (struct athregrec *) ale->ae_data;
238 r->val = ah->ah_devid;
239 alq_post(ath_hal_alq, ale);
240 ath_hal_alq_emitdev = 0;
244 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
251 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
253 bus_space_tag_t tag = BUSTAG(ah);
254 bus_space_handle_t h = ah->ah_sh;
257 struct ale *ale = ath_hal_alq_get(ah);
259 struct athregrec *r = (struct athregrec *) ale->ae_data;
260 r->threadid = curthread->td_tid;
264 alq_post(ath_hal_alq, ale);
267 if (ah->ah_config.ah_serialise_reg_war)
268 mtx_lock_spin(&ah_regser_mtx);
269 bus_space_write_4(tag, h, reg, val);
270 if (ah->ah_config.ah_serialise_reg_war)
271 mtx_unlock_spin(&ah_regser_mtx);
275 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
277 bus_space_tag_t tag = BUSTAG(ah);
278 bus_space_handle_t h = ah->ah_sh;
281 if (ah->ah_config.ah_serialise_reg_war)
282 mtx_lock_spin(&ah_regser_mtx);
283 val = bus_space_read_4(tag, h, reg);
284 if (ah->ah_config.ah_serialise_reg_war)
285 mtx_unlock_spin(&ah_regser_mtx);
287 struct ale *ale = ath_hal_alq_get(ah);
289 struct athregrec *r = (struct athregrec *) ale->ae_data;
290 r->threadid = curthread->td_tid;
294 alq_post(ath_hal_alq, ale);
301 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
304 struct ale *ale = ath_hal_alq_get(ah);
306 struct athregrec *r = (struct athregrec *) ale->ae_data;
307 r->threadid = curthread->td_tid;
311 alq_post(ath_hal_alq, ale);
315 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
317 * Memory-mapped device register read/write. These are here
318 * as routines when debugging support is enabled and/or when
319 * explicitly configured to use function calls. The latter is
320 * for architectures that might need to do something before
321 * referencing memory (e.g. remap an i/o window).
323 * NB: see the comments in ah_osdep.h about byte-swapping register
324 * reads and writes to understand what's going on below.
328 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
330 bus_space_tag_t tag = BUSTAG(ah);
331 bus_space_handle_t h = ah->ah_sh;
333 if (ah->ah_config.ah_serialise_reg_war)
334 mtx_lock_spin(&ah_regser_mtx);
335 bus_space_write_4(tag, h, reg, val);
336 if (ah->ah_config.ah_serialise_reg_war)
337 mtx_unlock_spin(&ah_regser_mtx);
341 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
343 bus_space_tag_t tag = BUSTAG(ah);
344 bus_space_handle_t h = ah->ah_sh;
347 if (ah->ah_config.ah_serialise_reg_war)
348 mtx_lock_spin(&ah_regser_mtx);
349 val = bus_space_read_4(tag, h, reg);
350 if (ah->ah_config.ah_serialise_reg_war)
351 mtx_unlock_spin(&ah_regser_mtx);
354 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
358 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
360 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
361 filename, lineno, msg);
362 panic("ath_hal_assert");
364 #endif /* AH_ASSERT */