ah_osdep.c revision 234450
1162413Ssam/*- 2178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3162413Ssam * All rights reserved. 4162413Ssam * 5162413Ssam * Redistribution and use in source and binary forms, with or without 6162413Ssam * modification, are permitted provided that the following conditions 7162413Ssam * are met: 8162413Ssam * 1. Redistributions of source code must retain the above copyright 9162413Ssam * notice, this list of conditions and the following disclaimer, 10162413Ssam * without modification. 11162413Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12162413Ssam * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13162413Ssam * redistribution must be conditioned upon including a substantially 14162413Ssam * similar Disclaimer requirement for further binary redistribution. 15162413Ssam * 16162413Ssam * NO WARRANTY 17162413Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18162413Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19162413Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20162413Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21162413Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22162413Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23162413Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24162413Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25162413Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26162413Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27162413Ssam * THE POSSIBILITY OF SUCH DAMAGES. 28162413Ssam * 29162413Ssam * $FreeBSD: head/sys/dev/ath/ah_osdep.c 234450 2012-04-19 03:26:21Z adrian $ 30162413Ssam */ 31162413Ssam#include "opt_ah.h" 32162413Ssam 33162413Ssam#include <sys/param.h> 34162413Ssam#include <sys/systm.h> 35162413Ssam#include <sys/kernel.h> 36162413Ssam#include <sys/module.h> 37162413Ssam#include <sys/sysctl.h> 38162413Ssam#include <sys/bus.h> 39162413Ssam#include <sys/malloc.h> 40162413Ssam#include <sys/proc.h> 41233887Sadrian#include <sys/pcpu.h> 42227410Sadrian#include <sys/lock.h> 43227410Sadrian#include <sys/mutex.h> 44162413Ssam 45162413Ssam#include <machine/stdarg.h> 46162413Ssam 47162413Ssam#include <net/ethernet.h> /* XXX for ether_sprintf */ 48162413Ssam 49185522Ssam#include <dev/ath/ath_hal/ah.h> 50162413Ssam 51162413Ssam/* 52162413Ssam * WiSoC boards overload the bus tag with information about the 53162413Ssam * board layout. We must extract the bus space tag from that 54162413Ssam * indirect structure. For everyone else the tag is passed in 55162413Ssam * directly. 56162413Ssam * XXX cache indirect ref privately 57162413Ssam */ 58162413Ssam#ifdef AH_SUPPORT_AR5312 59162413Ssam#define BUSTAG(ah) \ 60162413Ssam ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag) 61162413Ssam#else 62185522Ssam#define BUSTAG(ah) ((ah)->ah_st) 63162413Ssam#endif 64162413Ssam 65227410Sadrian/* 66227410Sadrian * This lock is used to seralise register access for chips which have 67227410Sadrian * problems w/ SMP CPUs issuing concurrent PCI transactions. 68227410Sadrian * 69227410Sadrian * XXX This is a global lock for now; it should be pushed to 70227410Sadrian * a per-device lock in some platform-independent fashion. 71227410Sadrian */ 72227410Sadrianstruct mtx ah_regser_mtx; 73227410SadrianMTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex", 74227410Sadrian MTX_SPIN); 75227410Sadrian 76162413Ssamextern void ath_hal_printf(struct ath_hal *, const char*, ...) 77162413Ssam __printflike(2,3); 78162413Ssamextern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list) 79162413Ssam __printflike(2, 0); 80162413Ssamextern const char* ath_hal_ether_sprintf(const u_int8_t *mac); 81162413Ssamextern void *ath_hal_malloc(size_t); 82162413Ssamextern void ath_hal_free(void *); 83162413Ssam#ifdef AH_ASSERT 84162413Ssamextern void ath_hal_assert_failed(const char* filename, 85162413Ssam int lineno, const char* msg); 86162413Ssam#endif 87162413Ssam#ifdef AH_DEBUG 88219315Sadrianextern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...); 89162413Ssam#endif /* AH_DEBUG */ 90162413Ssam 91162413Ssam/* NB: put this here instead of the driver to avoid circular references */ 92162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 93227309Sedstatic SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, 94227309Sed "Atheros HAL parameters"); 95162413Ssam 96223525Sadrian#ifdef AH_DEBUG 97223525Sadrianint ath_hal_debug = 0; 98223525SadrianSYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 99223525Sadrian 0, "Atheros HAL debugging printfs"); 100223525SadrianTUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 101223525Sadrian#endif /* AH_DEBUG */ 102223525Sadrian 103227293Sedstatic MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 104162413Ssam 105162413Ssamvoid* 106162413Ssamath_hal_malloc(size_t size) 107162413Ssam{ 108162413Ssam return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 109162413Ssam} 110162413Ssam 111162413Ssamvoid 112162413Ssamath_hal_free(void* p) 113162413Ssam{ 114196935Ssam free(p, M_ATH_HAL); 115162413Ssam} 116162413Ssam 117162413Ssamvoid 118162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 119162413Ssam{ 120162413Ssam vprintf(fmt, ap); 121162413Ssam} 122162413Ssam 123162413Ssamvoid 124162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 125162413Ssam{ 126162413Ssam va_list ap; 127162413Ssam va_start(ap, fmt); 128162413Ssam ath_hal_vprintf(ah, fmt, ap); 129162413Ssam va_end(ap); 130162413Ssam} 131162413Ssam 132162413Ssamconst char* 133162413Ssamath_hal_ether_sprintf(const u_int8_t *mac) 134162413Ssam{ 135162413Ssam return ether_sprintf(mac); 136162413Ssam} 137162413Ssam 138162413Ssam#ifdef AH_DEBUG 139222031Sadrian 140222031Sadrian/* This must match the definition in ath_hal/ah_debug.h */ 141222031Sadrian#define HAL_DEBUG_UNMASKABLE 0xf0000000 142162413Ssamvoid 143219315SadrianDO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...) 144184369Ssam{ 145224045Sadrian if ((mask == HAL_DEBUG_UNMASKABLE) || 146225883Sadrian (ah != NULL && ah->ah_config.ah_debug & mask) || 147224045Sadrian (ath_hal_debug & mask)) { 148184369Ssam __va_list ap; 149184369Ssam va_start(ap, fmt); 150184369Ssam ath_hal_vprintf(ah, fmt, ap); 151184369Ssam va_end(ap); 152184369Ssam } 153184369Ssam} 154222031Sadrian#undef HAL_DEBUG_UNMASKABLE 155162413Ssam#endif /* AH_DEBUG */ 156162413Ssam 157162413Ssam#ifdef AH_DEBUG_ALQ 158162413Ssam/* 159162413Ssam * ALQ register tracing support. 160162413Ssam * 161162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 162162413Ssam * writes to the file /tmp/ath_hal.log. The file format is a simple 163162413Ssam * fixed-size array of records. When done logging set hw.ath.hal.alq=0 164162413Ssam * and then decode the file with the arcode program (that is part of the 165162413Ssam * HAL). If you start+stop tracing the data will be appended to an 166162413Ssam * existing file. 167162413Ssam * 168162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record 169162413Ssam * is emitted and the different devices are not identified. 170162413Ssam */ 171162413Ssam#include <sys/alq.h> 172162413Ssam#include <sys/pcpu.h> 173185522Ssam#include <dev/ath/ath_hal/ah_decode.h> 174162413Ssam 175162413Ssamstatic struct alq *ath_hal_alq; 176162413Ssamstatic int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 177162413Ssamstatic u_int ath_hal_alq_lost; /* count of lost records */ 178220367Sadrianstatic char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log"; 179220367Sadrian 180220367SadrianSYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW, 181220367Sadrian &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile"); 182220367Sadrian 183162413Ssamstatic u_int ath_hal_alq_qsize = 64*1024; 184162413Ssam 185162413Ssamstatic int 186162413Ssamath_hal_setlogging(int enable) 187162413Ssam{ 188162413Ssam int error; 189162413Ssam 190162413Ssam if (enable) { 191168589Srwatson error = alq_open(&ath_hal_alq, ath_hal_logfile, 192168589Srwatson curthread->td_ucred, ALQ_DEFAULT_CMODE, 193168589Srwatson sizeof (struct athregrec), ath_hal_alq_qsize); 194168589Srwatson ath_hal_alq_lost = 0; 195168589Srwatson ath_hal_alq_emitdev = 1; 196168589Srwatson printf("ath_hal: logging to %s enabled\n", 197168589Srwatson ath_hal_logfile); 198162413Ssam } else { 199162413Ssam if (ath_hal_alq) 200162413Ssam alq_close(ath_hal_alq); 201162413Ssam ath_hal_alq = NULL; 202162413Ssam printf("ath_hal: logging disabled\n"); 203162413Ssam error = 0; 204162413Ssam } 205162413Ssam return (error); 206162413Ssam} 207162413Ssam 208162413Ssamstatic int 209162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 210162413Ssam{ 211162413Ssam int error, enable; 212162413Ssam 213162413Ssam enable = (ath_hal_alq != NULL); 214162413Ssam error = sysctl_handle_int(oidp, &enable, 0, req); 215162413Ssam if (error || !req->newptr) 216162413Ssam return (error); 217162413Ssam else 218162413Ssam return (ath_hal_setlogging(enable)); 219162413Ssam} 220162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 221162413Ssam 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 222162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 223162413Ssam &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 224162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 225162413Ssam &ath_hal_alq_lost, 0, "Register operations not logged"); 226162413Ssam 227162413Ssamstatic struct ale * 228162413Ssamath_hal_alq_get(struct ath_hal *ah) 229162413Ssam{ 230162413Ssam struct ale *ale; 231162413Ssam 232162413Ssam if (ath_hal_alq_emitdev) { 233162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 234162413Ssam if (ale) { 235162413Ssam struct athregrec *r = 236162413Ssam (struct athregrec *) ale->ae_data; 237162413Ssam r->op = OP_DEVICE; 238162413Ssam r->reg = 0; 239162413Ssam r->val = ah->ah_devid; 240162413Ssam alq_post(ath_hal_alq, ale); 241162413Ssam ath_hal_alq_emitdev = 0; 242162413Ssam } else 243162413Ssam ath_hal_alq_lost++; 244162413Ssam } 245162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 246162413Ssam if (!ale) 247162413Ssam ath_hal_alq_lost++; 248162413Ssam return ale; 249162413Ssam} 250162413Ssam 251162413Ssamvoid 252162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 253162413Ssam{ 254162413Ssam bus_space_tag_t tag = BUSTAG(ah); 255185522Ssam bus_space_handle_t h = ah->ah_sh; 256162413Ssam 257162413Ssam if (ath_hal_alq) { 258162413Ssam struct ale *ale = ath_hal_alq_get(ah); 259162413Ssam if (ale) { 260162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 261233887Sadrian r->threadid = curthread->td_tid; 262162413Ssam r->op = OP_WRITE; 263162413Ssam r->reg = reg; 264162413Ssam r->val = val; 265162413Ssam alq_post(ath_hal_alq, ale); 266162413Ssam } 267162413Ssam } 268227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 269227410Sadrian mtx_lock_spin(&ah_regser_mtx); 270234450Sadrian bus_space_write_4(tag, h, reg, val); 271227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 272227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 273162413Ssam} 274162413Ssam 275162413Ssamu_int32_t 276162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 277162413Ssam{ 278162413Ssam bus_space_tag_t tag = BUSTAG(ah); 279185522Ssam bus_space_handle_t h = ah->ah_sh; 280162413Ssam u_int32_t val; 281162413Ssam 282227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 283227410Sadrian mtx_lock_spin(&ah_regser_mtx); 284234450Sadrian val = bus_space_read_4(tag, h, reg); 285227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 286227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 287162413Ssam if (ath_hal_alq) { 288162413Ssam struct ale *ale = ath_hal_alq_get(ah); 289162413Ssam if (ale) { 290162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 291233887Sadrian r->threadid = curthread->td_tid; 292162413Ssam r->op = OP_READ; 293162413Ssam r->reg = reg; 294162413Ssam r->val = val; 295162413Ssam alq_post(ath_hal_alq, ale); 296162413Ssam } 297162413Ssam } 298162413Ssam return val; 299162413Ssam} 300162413Ssam 301162413Ssamvoid 302162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 303162413Ssam{ 304162413Ssam if (ath_hal_alq) { 305162413Ssam struct ale *ale = ath_hal_alq_get(ah); 306162413Ssam if (ale) { 307162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 308233887Sadrian r->threadid = curthread->td_tid; 309162413Ssam r->op = OP_MARK; 310162413Ssam r->reg = id; 311162413Ssam r->val = v; 312162413Ssam alq_post(ath_hal_alq, ale); 313162413Ssam } 314162413Ssam } 315162413Ssam} 316162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 317162413Ssam/* 318162413Ssam * Memory-mapped device register read/write. These are here 319162413Ssam * as routines when debugging support is enabled and/or when 320162413Ssam * explicitly configured to use function calls. The latter is 321162413Ssam * for architectures that might need to do something before 322162413Ssam * referencing memory (e.g. remap an i/o window). 323162413Ssam * 324162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register 325162413Ssam * reads and writes to understand what's going on below. 326162413Ssam */ 327162413Ssam 328162413Ssamvoid 329162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 330162413Ssam{ 331162413Ssam bus_space_tag_t tag = BUSTAG(ah); 332185522Ssam bus_space_handle_t h = ah->ah_sh; 333162413Ssam 334227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 335227410Sadrian mtx_lock_spin(&ah_regser_mtx); 336234450Sadrian bus_space_write_4(tag, h, reg, val); 337227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 338227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 339162413Ssam} 340162413Ssam 341162413Ssamu_int32_t 342162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 343162413Ssam{ 344162413Ssam bus_space_tag_t tag = BUSTAG(ah); 345185522Ssam bus_space_handle_t h = ah->ah_sh; 346162413Ssam u_int32_t val; 347162413Ssam 348227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 349227410Sadrian mtx_lock_spin(&ah_regser_mtx); 350234450Sadrian val = bus_space_read_4(tag, h, reg); 351227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 352227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 353162413Ssam return val; 354162413Ssam} 355162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 356162413Ssam 357162413Ssam#ifdef AH_ASSERT 358162413Ssamvoid 359162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg) 360162413Ssam{ 361162413Ssam printf("Atheros HAL assertion failure: %s: line %u: %s\n", 362162413Ssam filename, lineno, msg); 363162413Ssam panic("ath_hal_assert"); 364162413Ssam} 365162413Ssam#endif /* AH_ASSERT */ 366