ah_osdep.c revision 264292
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 264292 2014-04-09 03:51:05Z 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> 50237864Sadrian#include <dev/ath/ath_hal/ah_debug.h> 51162413Ssam 52162413Ssam/* 53162413Ssam * WiSoC boards overload the bus tag with information about the 54162413Ssam * board layout. We must extract the bus space tag from that 55162413Ssam * indirect structure. For everyone else the tag is passed in 56162413Ssam * directly. 57162413Ssam * XXX cache indirect ref privately 58162413Ssam */ 59162413Ssam#ifdef AH_SUPPORT_AR5312 60162413Ssam#define BUSTAG(ah) \ 61162413Ssam ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag) 62162413Ssam#else 63185522Ssam#define BUSTAG(ah) ((ah)->ah_st) 64162413Ssam#endif 65162413Ssam 66227410Sadrian/* 67227410Sadrian * This lock is used to seralise register access for chips which have 68227410Sadrian * problems w/ SMP CPUs issuing concurrent PCI transactions. 69227410Sadrian * 70227410Sadrian * XXX This is a global lock for now; it should be pushed to 71227410Sadrian * a per-device lock in some platform-independent fashion. 72227410Sadrian */ 73227410Sadrianstruct mtx ah_regser_mtx; 74227410SadrianMTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex", 75227410Sadrian MTX_SPIN); 76227410Sadrian 77162413Ssamextern void ath_hal_printf(struct ath_hal *, const char*, ...) 78162413Ssam __printflike(2,3); 79162413Ssamextern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list) 80162413Ssam __printflike(2, 0); 81162413Ssamextern const char* ath_hal_ether_sprintf(const u_int8_t *mac); 82162413Ssamextern void *ath_hal_malloc(size_t); 83162413Ssamextern void ath_hal_free(void *); 84162413Ssam#ifdef AH_ASSERT 85162413Ssamextern void ath_hal_assert_failed(const char* filename, 86162413Ssam int lineno, const char* msg); 87162413Ssam#endif 88162413Ssam#ifdef AH_DEBUG 89219315Sadrianextern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...); 90162413Ssam#endif /* AH_DEBUG */ 91162413Ssam 92162413Ssam/* NB: put this here instead of the driver to avoid circular references */ 93162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 94227309Sedstatic SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, 95227309Sed "Atheros HAL parameters"); 96162413Ssam 97223525Sadrian#ifdef AH_DEBUG 98223525Sadrianint ath_hal_debug = 0; 99223525SadrianSYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 100223525Sadrian 0, "Atheros HAL debugging printfs"); 101223525SadrianTUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 102223525Sadrian#endif /* AH_DEBUG */ 103223525Sadrian 104227293Sedstatic MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 105162413Ssam 106162413Ssamvoid* 107162413Ssamath_hal_malloc(size_t size) 108162413Ssam{ 109162413Ssam return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 110162413Ssam} 111162413Ssam 112162413Ssamvoid 113162413Ssamath_hal_free(void* p) 114162413Ssam{ 115196935Ssam free(p, M_ATH_HAL); 116162413Ssam} 117162413Ssam 118162413Ssamvoid 119162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 120162413Ssam{ 121162413Ssam vprintf(fmt, ap); 122162413Ssam} 123162413Ssam 124162413Ssamvoid 125162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 126162413Ssam{ 127162413Ssam va_list ap; 128162413Ssam va_start(ap, fmt); 129162413Ssam ath_hal_vprintf(ah, fmt, ap); 130162413Ssam va_end(ap); 131162413Ssam} 132162413Ssam 133162413Ssamconst char* 134162413Ssamath_hal_ether_sprintf(const u_int8_t *mac) 135162413Ssam{ 136162413Ssam return ether_sprintf(mac); 137162413Ssam} 138162413Ssam 139162413Ssam#ifdef AH_DEBUG 140222031Sadrian 141264292Sadrian/* 142264292Sadrian * XXX This is highly relevant only for the AR5416 and later 143264292Sadrian * PCI/PCIe NICs. It'll need adjustment for other hardware 144264292Sadrian * variations. 145264292Sadrian */ 146264292Sadrianstatic int 147264292Sadrianath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg) 148264292Sadrian{ 149264292Sadrian 150264292Sadrian if (reg >= 0x4000 && reg < 0x5000) 151264292Sadrian return (1); 152264292Sadrian if (reg >= 0x6000 && reg < 0x7000) 153264292Sadrian return (1); 154264292Sadrian if (reg >= 0x7000 && reg < 0x8000) 155264292Sadrian return (1); 156264292Sadrian return (0); 157264292Sadrian} 158264292Sadrian 159162413Ssamvoid 160219315SadrianDO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...) 161184369Ssam{ 162224045Sadrian if ((mask == HAL_DEBUG_UNMASKABLE) || 163225883Sadrian (ah != NULL && ah->ah_config.ah_debug & mask) || 164224045Sadrian (ath_hal_debug & mask)) { 165184369Ssam __va_list ap; 166184369Ssam va_start(ap, fmt); 167184369Ssam ath_hal_vprintf(ah, fmt, ap); 168184369Ssam va_end(ap); 169184369Ssam } 170184369Ssam} 171222031Sadrian#undef HAL_DEBUG_UNMASKABLE 172162413Ssam#endif /* AH_DEBUG */ 173162413Ssam 174162413Ssam#ifdef AH_DEBUG_ALQ 175162413Ssam/* 176162413Ssam * ALQ register tracing support. 177162413Ssam * 178162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 179162413Ssam * writes to the file /tmp/ath_hal.log. The file format is a simple 180162413Ssam * fixed-size array of records. When done logging set hw.ath.hal.alq=0 181162413Ssam * and then decode the file with the arcode program (that is part of the 182162413Ssam * HAL). If you start+stop tracing the data will be appended to an 183162413Ssam * existing file. 184162413Ssam * 185162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record 186162413Ssam * is emitted and the different devices are not identified. 187162413Ssam */ 188162413Ssam#include <sys/alq.h> 189162413Ssam#include <sys/pcpu.h> 190185522Ssam#include <dev/ath/ath_hal/ah_decode.h> 191162413Ssam 192162413Ssamstatic struct alq *ath_hal_alq; 193162413Ssamstatic int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 194162413Ssamstatic u_int ath_hal_alq_lost; /* count of lost records */ 195220367Sadrianstatic char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log"; 196220367Sadrian 197220367SadrianSYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW, 198220367Sadrian &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile"); 199220367Sadrian 200162413Ssamstatic u_int ath_hal_alq_qsize = 64*1024; 201162413Ssam 202162413Ssamstatic int 203162413Ssamath_hal_setlogging(int enable) 204162413Ssam{ 205162413Ssam int error; 206162413Ssam 207162413Ssam if (enable) { 208168589Srwatson error = alq_open(&ath_hal_alq, ath_hal_logfile, 209168589Srwatson curthread->td_ucred, ALQ_DEFAULT_CMODE, 210168589Srwatson sizeof (struct athregrec), ath_hal_alq_qsize); 211168589Srwatson ath_hal_alq_lost = 0; 212168589Srwatson ath_hal_alq_emitdev = 1; 213168589Srwatson printf("ath_hal: logging to %s enabled\n", 214168589Srwatson ath_hal_logfile); 215162413Ssam } else { 216162413Ssam if (ath_hal_alq) 217162413Ssam alq_close(ath_hal_alq); 218162413Ssam ath_hal_alq = NULL; 219162413Ssam printf("ath_hal: logging disabled\n"); 220162413Ssam error = 0; 221162413Ssam } 222162413Ssam return (error); 223162413Ssam} 224162413Ssam 225162413Ssamstatic int 226162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 227162413Ssam{ 228162413Ssam int error, enable; 229162413Ssam 230162413Ssam enable = (ath_hal_alq != NULL); 231162413Ssam error = sysctl_handle_int(oidp, &enable, 0, req); 232162413Ssam if (error || !req->newptr) 233162413Ssam return (error); 234162413Ssam else 235162413Ssam return (ath_hal_setlogging(enable)); 236162413Ssam} 237162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 238162413Ssam 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 239162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 240162413Ssam &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 241162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 242162413Ssam &ath_hal_alq_lost, 0, "Register operations not logged"); 243162413Ssam 244162413Ssamstatic struct ale * 245162413Ssamath_hal_alq_get(struct ath_hal *ah) 246162413Ssam{ 247162413Ssam struct ale *ale; 248162413Ssam 249162413Ssam if (ath_hal_alq_emitdev) { 250162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 251162413Ssam if (ale) { 252162413Ssam struct athregrec *r = 253162413Ssam (struct athregrec *) ale->ae_data; 254162413Ssam r->op = OP_DEVICE; 255162413Ssam r->reg = 0; 256162413Ssam r->val = ah->ah_devid; 257162413Ssam alq_post(ath_hal_alq, ale); 258162413Ssam ath_hal_alq_emitdev = 0; 259162413Ssam } else 260162413Ssam ath_hal_alq_lost++; 261162413Ssam } 262162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 263162413Ssam if (!ale) 264162413Ssam ath_hal_alq_lost++; 265162413Ssam return ale; 266162413Ssam} 267162413Ssam 268162413Ssamvoid 269162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 270162413Ssam{ 271162413Ssam bus_space_tag_t tag = BUSTAG(ah); 272185522Ssam bus_space_handle_t h = ah->ah_sh; 273162413Ssam 274263418Sadrian /* Debug - complain if we haven't fully waken things up */ 275264292Sadrian if (! ath_hal_reg_whilst_asleep(ah, reg) && 276264292Sadrian ah->ah_powerMode != HAL_PM_AWAKE) { 277263418Sadrian ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n", 278263418Sadrian __func__, reg, val, ah->ah_powerMode); 279263418Sadrian } 280263418Sadrian 281162413Ssam if (ath_hal_alq) { 282162413Ssam struct ale *ale = ath_hal_alq_get(ah); 283162413Ssam if (ale) { 284162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 285233887Sadrian r->threadid = curthread->td_tid; 286162413Ssam r->op = OP_WRITE; 287162413Ssam r->reg = reg; 288162413Ssam r->val = val; 289162413Ssam alq_post(ath_hal_alq, ale); 290162413Ssam } 291162413Ssam } 292227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 293227410Sadrian mtx_lock_spin(&ah_regser_mtx); 294234450Sadrian bus_space_write_4(tag, h, reg, val); 295227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 296227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 297162413Ssam} 298162413Ssam 299162413Ssamu_int32_t 300162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 301162413Ssam{ 302162413Ssam bus_space_tag_t tag = BUSTAG(ah); 303185522Ssam bus_space_handle_t h = ah->ah_sh; 304162413Ssam u_int32_t val; 305162413Ssam 306263418Sadrian /* Debug - complain if we haven't fully waken things up */ 307264292Sadrian if (! ath_hal_reg_whilst_asleep(ah, reg) && 308264292Sadrian ah->ah_powerMode != HAL_PM_AWAKE) { 309263418Sadrian ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n", 310263418Sadrian __func__, reg, ah->ah_powerMode); 311263418Sadrian } 312263418Sadrian 313227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 314227410Sadrian mtx_lock_spin(&ah_regser_mtx); 315234450Sadrian val = bus_space_read_4(tag, h, reg); 316227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 317227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 318162413Ssam if (ath_hal_alq) { 319162413Ssam struct ale *ale = ath_hal_alq_get(ah); 320162413Ssam if (ale) { 321162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 322233887Sadrian r->threadid = curthread->td_tid; 323162413Ssam r->op = OP_READ; 324162413Ssam r->reg = reg; 325162413Ssam r->val = val; 326162413Ssam alq_post(ath_hal_alq, ale); 327162413Ssam } 328162413Ssam } 329162413Ssam return val; 330162413Ssam} 331162413Ssam 332162413Ssamvoid 333162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 334162413Ssam{ 335162413Ssam if (ath_hal_alq) { 336162413Ssam struct ale *ale = ath_hal_alq_get(ah); 337162413Ssam if (ale) { 338162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 339233887Sadrian r->threadid = curthread->td_tid; 340162413Ssam r->op = OP_MARK; 341162413Ssam r->reg = id; 342162413Ssam r->val = v; 343162413Ssam alq_post(ath_hal_alq, ale); 344162413Ssam } 345162413Ssam } 346162413Ssam} 347162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 348162413Ssam/* 349162413Ssam * Memory-mapped device register read/write. These are here 350162413Ssam * as routines when debugging support is enabled and/or when 351162413Ssam * explicitly configured to use function calls. The latter is 352162413Ssam * for architectures that might need to do something before 353162413Ssam * referencing memory (e.g. remap an i/o window). 354162413Ssam * 355162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register 356162413Ssam * reads and writes to understand what's going on below. 357162413Ssam */ 358162413Ssam 359162413Ssamvoid 360162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 361162413Ssam{ 362162413Ssam bus_space_tag_t tag = BUSTAG(ah); 363185522Ssam bus_space_handle_t h = ah->ah_sh; 364162413Ssam 365263418Sadrian /* Debug - complain if we haven't fully waken things up */ 366264292Sadrian if (! ath_hal_reg_whilst_asleep(ah, reg) && 367264292Sadrian ah->ah_powerMode != HAL_PM_AWAKE) { 368263418Sadrian ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n", 369263418Sadrian __func__, reg, val, ah->ah_powerMode); 370263418Sadrian } 371263418Sadrian 372227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 373227410Sadrian mtx_lock_spin(&ah_regser_mtx); 374234450Sadrian bus_space_write_4(tag, h, reg, val); 375227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 376227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 377162413Ssam} 378162413Ssam 379162413Ssamu_int32_t 380162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 381162413Ssam{ 382162413Ssam bus_space_tag_t tag = BUSTAG(ah); 383185522Ssam bus_space_handle_t h = ah->ah_sh; 384162413Ssam u_int32_t val; 385162413Ssam 386263418Sadrian /* Debug - complain if we haven't fully waken things up */ 387264292Sadrian if (! ath_hal_reg_whilst_asleep(ah, reg) && 388264292Sadrian ah->ah_powerMode != HAL_PM_AWAKE) { 389263418Sadrian ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n", 390263418Sadrian __func__, reg, ah->ah_powerMode); 391263418Sadrian } 392263418Sadrian 393227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 394227410Sadrian mtx_lock_spin(&ah_regser_mtx); 395234450Sadrian val = bus_space_read_4(tag, h, reg); 396227410Sadrian if (ah->ah_config.ah_serialise_reg_war) 397227410Sadrian mtx_unlock_spin(&ah_regser_mtx); 398162413Ssam return val; 399162413Ssam} 400162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 401162413Ssam 402162413Ssam#ifdef AH_ASSERT 403162413Ssamvoid 404162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg) 405162413Ssam{ 406162413Ssam printf("Atheros HAL assertion failure: %s: line %u: %s\n", 407162413Ssam filename, lineno, msg); 408162413Ssam panic("ath_hal_assert"); 409162413Ssam} 410162413Ssam#endif /* AH_ASSERT */ 411