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 #define CTLFLAG_RWTUN CTLFLAG_RW
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <machine/stdarg.h>
48 #include <net/ethernet.h> /* XXX for ether_sprintf */
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
54 #include <dev/netif/ath/ath_hal/ah.h>
55 #include <dev/netif/ath/ath_hal/ah_debug.h>
57 #include <netproto/802_11/ieee80211_var.h>
60 * WiSoC boards overload the bus tag with information about the
61 * board layout. We must extract the bus space tag from that
62 * indirect structure. For everyone else the tag is passed in
64 * XXX cache indirect ref privately
66 #ifdef AH_SUPPORT_AR5312
68 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
70 #define BUSTAG(ah) ((ah)->ah_st)
74 * This lock is used to seralise register access for chips which have
75 * problems w/ SMP CPUs issuing concurrent PCI transactions.
77 * XXX This is a global lock for now; it should be pushed to
78 * a per-device lock in some platform-independent fashion.
80 struct lock ah_regser_mtx;
81 LOCK_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex", 0);
83 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
85 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
87 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
88 extern void *ath_hal_malloc(size_t);
89 extern void ath_hal_free(void *);
91 extern void ath_hal_assert_failed(const char* filename,
92 int lineno, const char* msg);
95 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
98 /* NB: put this here instead of the driver to avoid circular references */
99 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
100 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
101 "Atheros HAL parameters");
104 int ath_hal_debug = 0;
105 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RWTUN, &ath_hal_debug,
106 0, "Atheros HAL debugging printfs");
107 #endif /* AH_DEBUG */
109 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
112 ath_hal_malloc(size_t size)
114 return kmalloc(size, M_ATH_HAL, M_INTWAIT | M_ZERO);
118 ath_hal_free(void* p)
124 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, __va_list ap)
130 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
134 ath_hal_vprintf(ah, fmt, ap);
139 ath_hal_ether_sprintf(const u_int8_t *mac)
141 return ether_sprintf(mac);
147 * XXX This is highly relevant only for the AR5416 and later
148 * PCI/PCIe NICs. It'll need adjustment for other hardware
152 ath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg)
155 if (reg >= 0x4000 && reg < 0x5000)
157 if (reg >= 0x6000 && reg < 0x7000)
159 if (reg >= 0x7000 && reg < 0x8000)
165 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
167 if ((mask == HAL_DEBUG_UNMASKABLE) ||
168 (ah != NULL && ah->ah_config.ah_debug & mask) ||
169 (ath_hal_debug & mask)) {
172 ath_hal_vprintf(ah, fmt, ap);
176 #undef HAL_DEBUG_UNMASKABLE
177 #endif /* AH_DEBUG */
181 * ALQ register tracing support.
183 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
184 * writes to the file /tmp/ath_hal.log. The file format is a simple
185 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
186 * and then decode the file with the arcode program (that is part of the
187 * HAL). If you start+stop tracing the data will be appended to an
190 * NB: doesn't handle multiple devices properly; only one DEVICE record
191 * is emitted and the different devices are not identified.
193 /*#include <sys/alq.h> FreeBSD */
194 /*#include <sys/pcpu.h> FreeBSD */
195 #include <dev/netif/ath/ath_hal/ah_decode.h>
197 static struct alq *ath_hal_alq;
198 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
199 static u_int ath_hal_alq_lost; /* count of lost records */
200 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
202 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
203 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
205 static u_int ath_hal_alq_qsize = 64*1024;
208 ath_hal_setlogging(int enable)
213 error = alq_open(&ath_hal_alq, ath_hal_logfile,
214 curthread->td_ucred, ALQ_DEFAULT_CMODE,
215 sizeof (struct athregrec), ath_hal_alq_qsize);
216 ath_hal_alq_lost = 0;
217 ath_hal_alq_emitdev = 1;
218 kprintf("ath_hal: logging to %s enabled\n",
222 alq_close(ath_hal_alq);
224 kprintf("ath_hal: logging disabled\n");
231 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
235 enable = (ath_hal_alq != NULL);
236 error = sysctl_handle_int(oidp, &enable, 0, req);
237 if (error || !req->newptr)
240 return (ath_hal_setlogging(enable));
242 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
243 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
244 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
245 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
246 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
247 &ath_hal_alq_lost, 0, "Register operations not logged");
250 ath_hal_alq_get(struct ath_hal *ah)
254 if (ath_hal_alq_emitdev) {
255 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
257 struct athregrec *r =
258 (struct athregrec *) ale->ae_data;
261 r->val = ah->ah_devid;
262 alq_post(ath_hal_alq, ale);
263 ath_hal_alq_emitdev = 0;
267 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
274 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
276 bus_space_tag_t tag = BUSTAG(ah);
277 bus_space_handle_t h = ah->ah_sh;
279 /* Debug - complain if we haven't fully waken things up */
280 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
281 ah->ah_powerMode != HAL_PM_AWAKE) {
282 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
283 __func__, reg, val, ah->ah_powerMode);
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);
297 if (ah->ah_config.ah_serialise_reg_war)
298 lockmgr(&ah_regser_mtx, LK_EXCLUSIVE);
299 bus_space_write_4(tag, h, reg, val);
300 if (ah->ah_config.ah_serialise_reg_war)
301 lockmgr(&ah_regser_mtx, LK_RELEASE);
305 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
307 bus_space_tag_t tag = BUSTAG(ah);
308 bus_space_handle_t h = ah->ah_sh;
311 /* Debug - complain if we haven't fully waken things up */
312 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
313 ah->ah_powerMode != HAL_PM_AWAKE) {
314 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
315 __func__, reg, ah->ah_powerMode);
318 if (ah->ah_config.ah_serialise_reg_war)
319 lockmgr(&ah_regser_mtx, LK_EXCLUSIVE);
320 val = bus_space_read_4(tag, h, reg);
321 if (ah->ah_config.ah_serialise_reg_war)
322 lockmgr(&ah_regser_mtx, LK_RELEASE);
324 struct ale *ale = ath_hal_alq_get(ah);
326 struct athregrec *r = (struct athregrec *) ale->ae_data;
327 r->threadid = curthread->td_tid;
331 alq_post(ath_hal_alq, ale);
338 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
341 struct ale *ale = ath_hal_alq_get(ah);
343 struct athregrec *r = (struct athregrec *) ale->ae_data;
344 r->threadid = curthread->td_tid;
348 alq_post(ath_hal_alq, ale);
352 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
354 * Memory-mapped device register read/write. These are here
355 * as routines when debugging support is enabled and/or when
356 * explicitly configured to use function calls. The latter is
357 * for architectures that might need to do something before
358 * referencing memory (e.g. remap an i/o window).
360 * NB: see the comments in ah_osdep.h about byte-swapping register
361 * reads and writes to understand what's going on below.
365 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
367 bus_space_tag_t tag = BUSTAG(ah);
368 bus_space_handle_t h = ah->ah_sh;
370 /* Debug - complain if we haven't fully waken things up */
371 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
372 ah->ah_powerMode != HAL_PM_AWAKE) {
373 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
374 __func__, reg, val, ah->ah_powerMode);
377 if (ah->ah_config.ah_serialise_reg_war)
378 lockmgr(&ah_regser_mtx, LK_EXCLUSIVE);
379 bus_space_write_4(tag, h, reg, val);
380 if (ah->ah_config.ah_serialise_reg_war)
381 lockmgr(&ah_regser_mtx, LK_RELEASE);
385 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
387 bus_space_tag_t tag = BUSTAG(ah);
388 bus_space_handle_t h = ah->ah_sh;
391 /* Debug - complain if we haven't fully waken things up */
392 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
393 ah->ah_powerMode != HAL_PM_AWAKE) {
394 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
395 __func__, reg, ah->ah_powerMode);
398 if (ah->ah_config.ah_serialise_reg_war)
399 lockmgr(&ah_regser_mtx, LK_EXCLUSIVE);
400 val = bus_space_read_4(tag, h, reg);
401 if (ah->ah_config.ah_serialise_reg_war)
402 lockmgr(&ah_regser_mtx, LK_RELEASE);
405 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
409 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
411 kprintf("Atheros HAL assertion failure: %s: line %u: %s\n",
412 filename, lineno, msg);
413 panic("ath_hal_assert");
415 #endif /* AH_ASSERT */
421 ath_hal_modevent(module_t mod, int type, void *unused)
425 wlan_serialize_enter();
438 wlan_serialize_exit();
443 static moduledata_t ath_hal_mod = {
449 DECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
450 MODULE_VERSION(ath_hal, 1);