ah_osdep.c revision 178354
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 178354 2008-04-20 20:35:46Z sam $ 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> 41162413Ssam 42162413Ssam#include <machine/stdarg.h> 43162413Ssam 44162413Ssam#include <net/ethernet.h> /* XXX for ether_sprintf */ 45162413Ssam 46162413Ssam#include <contrib/dev/ath/ah.h> 47162413Ssam 48162413Ssam/* 49162413Ssam * WiSoC boards overload the bus tag with information about the 50162413Ssam * board layout. We must extract the bus space tag from that 51162413Ssam * indirect structure. For everyone else the tag is passed in 52162413Ssam * directly. 53162413Ssam * XXX cache indirect ref privately 54162413Ssam */ 55162413Ssam#ifdef AH_SUPPORT_AR5312 56162413Ssam#define BUSTAG(ah) \ 57162413Ssam ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag) 58162413Ssam#else 59162413Ssam#define BUSTAG(ah) ((bus_space_tag_t) (ah)->ah_st) 60162413Ssam#endif 61162413Ssam 62162413Ssamextern void ath_hal_printf(struct ath_hal *, const char*, ...) 63162413Ssam __printflike(2,3); 64162413Ssamextern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list) 65162413Ssam __printflike(2, 0); 66162413Ssamextern const char* ath_hal_ether_sprintf(const u_int8_t *mac); 67162413Ssamextern void *ath_hal_malloc(size_t); 68162413Ssamextern void ath_hal_free(void *); 69162413Ssam#ifdef AH_ASSERT 70162413Ssamextern void ath_hal_assert_failed(const char* filename, 71162413Ssam int lineno, const char* msg); 72162413Ssam#endif 73162413Ssam#ifdef AH_DEBUG 74162413Ssamextern void HALDEBUG(struct ath_hal *ah, const char* fmt, ...); 75162413Ssamextern void HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...); 76162413Ssam#endif /* AH_DEBUG */ 77162413Ssam 78162413Ssam/* NB: put this here instead of the driver to avoid circular references */ 79162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 80162413SsamSYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters"); 81162413Ssam 82162413Ssam#ifdef AH_DEBUG 83162413Ssamstatic int ath_hal_debug = 0; 84162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 85162413Ssam 0, "Atheros HAL debugging printfs"); 86162413SsamTUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 87162413Ssam#endif /* AH_DEBUG */ 88162413Ssam 89162413SsamSYSCTL_STRING(_hw_ath_hal, OID_AUTO, version, CTLFLAG_RD, ath_hal_version, 0, 90162413Ssam "Atheros HAL version"); 91162413Ssam 92162413Ssam/* NB: these are deprecated; they exist for now for compatibility */ 93162413Ssamint ath_hal_dma_beacon_response_time = 2; /* in TU's */ 94162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, dma_brt, CTLFLAG_RW, 95162413Ssam &ath_hal_dma_beacon_response_time, 0, 96162413Ssam "Atheros HAL DMA beacon response time"); 97162413Ssamint ath_hal_sw_beacon_response_time = 10; /* in TU's */ 98162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, sw_brt, CTLFLAG_RW, 99162413Ssam &ath_hal_sw_beacon_response_time, 0, 100162413Ssam "Atheros HAL software beacon response time"); 101162413Ssamint ath_hal_additional_swba_backoff = 0; /* in TU's */ 102162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, swba_backoff, CTLFLAG_RW, 103162413Ssam &ath_hal_additional_swba_backoff, 0, 104162413Ssam "Atheros HAL additional SWBA backoff time"); 105162413Ssam 106162413SsamMALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 107162413Ssam 108162413Ssamvoid* 109162413Ssamath_hal_malloc(size_t size) 110162413Ssam{ 111162413Ssam return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 112162413Ssam} 113162413Ssam 114162413Ssamvoid 115162413Ssamath_hal_free(void* p) 116162413Ssam{ 117162413Ssam return free(p, M_ATH_HAL); 118162413Ssam} 119162413Ssam 120162413Ssamvoid 121162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 122162413Ssam{ 123162413Ssam vprintf(fmt, ap); 124162413Ssam} 125162413Ssam 126162413Ssamvoid 127162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 128162413Ssam{ 129162413Ssam va_list ap; 130162413Ssam va_start(ap, fmt); 131162413Ssam ath_hal_vprintf(ah, fmt, ap); 132162413Ssam va_end(ap); 133162413Ssam} 134162413Ssam 135162413Ssamconst char* 136162413Ssamath_hal_ether_sprintf(const u_int8_t *mac) 137162413Ssam{ 138162413Ssam return ether_sprintf(mac); 139162413Ssam} 140162413Ssam 141162413Ssam#ifdef AH_DEBUG 142162413Ssamvoid 143162413SsamHALDEBUG(struct ath_hal *ah, const char* fmt, ...) 144162413Ssam{ 145162413Ssam if (ath_hal_debug) { 146162413Ssam __va_list ap; 147162413Ssam va_start(ap, fmt); 148162413Ssam ath_hal_vprintf(ah, fmt, ap); 149162413Ssam va_end(ap); 150162413Ssam } 151162413Ssam} 152162413Ssam 153162413Ssamvoid 154162413SsamHALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...) 155162413Ssam{ 156162413Ssam if (ath_hal_debug >= level) { 157162413Ssam __va_list ap; 158162413Ssam va_start(ap, fmt); 159162413Ssam ath_hal_vprintf(ah, fmt, ap); 160162413Ssam va_end(ap); 161162413Ssam } 162162413Ssam} 163162413Ssam#endif /* AH_DEBUG */ 164162413Ssam 165162413Ssam#ifdef AH_DEBUG_ALQ 166162413Ssam/* 167162413Ssam * ALQ register tracing support. 168162413Ssam * 169162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 170162413Ssam * writes to the file /tmp/ath_hal.log. The file format is a simple 171162413Ssam * fixed-size array of records. When done logging set hw.ath.hal.alq=0 172162413Ssam * and then decode the file with the arcode program (that is part of the 173162413Ssam * HAL). If you start+stop tracing the data will be appended to an 174162413Ssam * existing file. 175162413Ssam * 176162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record 177162413Ssam * is emitted and the different devices are not identified. 178162413Ssam */ 179162413Ssam#include <sys/alq.h> 180162413Ssam#include <sys/pcpu.h> 181162413Ssam#include <contrib/dev/ath/ah_decode.h> 182162413Ssam 183162413Ssamstatic struct alq *ath_hal_alq; 184162413Ssamstatic int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 185162413Ssamstatic u_int ath_hal_alq_lost; /* count of lost records */ 186162413Ssamstatic const char *ath_hal_logfile = "/tmp/ath_hal.log"; 187162413Ssamstatic u_int ath_hal_alq_qsize = 64*1024; 188162413Ssam 189162413Ssamstatic int 190162413Ssamath_hal_setlogging(int enable) 191162413Ssam{ 192162413Ssam int error; 193162413Ssam 194162413Ssam if (enable) { 195168589Srwatson error = alq_open(&ath_hal_alq, ath_hal_logfile, 196168589Srwatson curthread->td_ucred, ALQ_DEFAULT_CMODE, 197168589Srwatson sizeof (struct athregrec), ath_hal_alq_qsize); 198168589Srwatson ath_hal_alq_lost = 0; 199168589Srwatson ath_hal_alq_emitdev = 1; 200168589Srwatson printf("ath_hal: logging to %s enabled\n", 201168589Srwatson ath_hal_logfile); 202162413Ssam } else { 203162413Ssam if (ath_hal_alq) 204162413Ssam alq_close(ath_hal_alq); 205162413Ssam ath_hal_alq = NULL; 206162413Ssam printf("ath_hal: logging disabled\n"); 207162413Ssam error = 0; 208162413Ssam } 209162413Ssam return (error); 210162413Ssam} 211162413Ssam 212162413Ssamstatic int 213162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 214162413Ssam{ 215162413Ssam int error, enable; 216162413Ssam 217162413Ssam enable = (ath_hal_alq != NULL); 218162413Ssam error = sysctl_handle_int(oidp, &enable, 0, req); 219162413Ssam if (error || !req->newptr) 220162413Ssam return (error); 221162413Ssam else 222162413Ssam return (ath_hal_setlogging(enable)); 223162413Ssam} 224162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 225162413Ssam 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 226162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 227162413Ssam &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 228162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 229162413Ssam &ath_hal_alq_lost, 0, "Register operations not logged"); 230162413Ssam 231162413Ssamstatic struct ale * 232162413Ssamath_hal_alq_get(struct ath_hal *ah) 233162413Ssam{ 234162413Ssam struct ale *ale; 235162413Ssam 236162413Ssam if (ath_hal_alq_emitdev) { 237162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 238162413Ssam if (ale) { 239162413Ssam struct athregrec *r = 240162413Ssam (struct athregrec *) ale->ae_data; 241162413Ssam r->op = OP_DEVICE; 242162413Ssam r->reg = 0; 243162413Ssam r->val = ah->ah_devid; 244162413Ssam alq_post(ath_hal_alq, ale); 245162413Ssam ath_hal_alq_emitdev = 0; 246162413Ssam } else 247162413Ssam ath_hal_alq_lost++; 248162413Ssam } 249162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 250162413Ssam if (!ale) 251162413Ssam ath_hal_alq_lost++; 252162413Ssam return ale; 253162413Ssam} 254162413Ssam 255162413Ssamvoid 256162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 257162413Ssam{ 258162413Ssam bus_space_tag_t tag = BUSTAG(ah); 259162413Ssam bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 260162413Ssam 261162413Ssam if (ath_hal_alq) { 262162413Ssam struct ale *ale = ath_hal_alq_get(ah); 263162413Ssam if (ale) { 264162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 265162413Ssam r->op = OP_WRITE; 266162413Ssam r->reg = reg; 267162413Ssam r->val = val; 268162413Ssam alq_post(ath_hal_alq, ale); 269162413Ssam } 270162413Ssam } 271162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 272162413Ssam if (reg >= 0x4000 && reg < 0x5000) 273162413Ssam bus_space_write_4(tag, h, reg, val); 274162413Ssam else 275162413Ssam#endif 276162413Ssam bus_space_write_stream_4(tag, h, reg, val); 277162413Ssam} 278162413Ssam 279162413Ssamu_int32_t 280162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 281162413Ssam{ 282162413Ssam bus_space_tag_t tag = BUSTAG(ah); 283162413Ssam bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 284162413Ssam u_int32_t val; 285162413Ssam 286162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 287162413Ssam if (reg >= 0x4000 && reg < 0x5000) 288162413Ssam val = bus_space_read_4(tag, h, reg); 289162413Ssam else 290162413Ssam#endif 291162413Ssam val = bus_space_read_stream_4(tag, h, reg); 292162413Ssam if (ath_hal_alq) { 293162413Ssam struct ale *ale = ath_hal_alq_get(ah); 294162413Ssam if (ale) { 295162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 296162413Ssam r->op = OP_READ; 297162413Ssam r->reg = reg; 298162413Ssam r->val = val; 299162413Ssam alq_post(ath_hal_alq, ale); 300162413Ssam } 301162413Ssam } 302162413Ssam return val; 303162413Ssam} 304162413Ssam 305162413Ssamvoid 306162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 307162413Ssam{ 308162413Ssam if (ath_hal_alq) { 309162413Ssam struct ale *ale = ath_hal_alq_get(ah); 310162413Ssam if (ale) { 311162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 312162413Ssam r->op = OP_MARK; 313162413Ssam r->reg = id; 314162413Ssam r->val = v; 315162413Ssam alq_post(ath_hal_alq, ale); 316162413Ssam } 317162413Ssam } 318162413Ssam} 319162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 320162413Ssam/* 321162413Ssam * Memory-mapped device register read/write. These are here 322162413Ssam * as routines when debugging support is enabled and/or when 323162413Ssam * explicitly configured to use function calls. The latter is 324162413Ssam * for architectures that might need to do something before 325162413Ssam * referencing memory (e.g. remap an i/o window). 326162413Ssam * 327162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register 328162413Ssam * reads and writes to understand what's going on below. 329162413Ssam */ 330162413Ssam 331162413Ssamvoid 332162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 333162413Ssam{ 334162413Ssam bus_space_tag_t tag = BUSTAG(ah); 335162413Ssam bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 336162413Ssam 337162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 338162413Ssam if (reg >= 0x4000 && reg < 0x5000) 339162413Ssam bus_space_write_4(tag, h, reg, val); 340162413Ssam else 341162413Ssam#endif 342162413Ssam bus_space_write_stream_4(tag, h, reg, val); 343162413Ssam} 344162413Ssam 345162413Ssamu_int32_t 346162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 347162413Ssam{ 348162413Ssam bus_space_tag_t tag = BUSTAG(ah); 349162413Ssam bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 350162413Ssam u_int32_t val; 351162413Ssam 352162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 353162413Ssam if (reg >= 0x4000 && reg < 0x5000) 354162413Ssam val = bus_space_read_4(tag, h, reg); 355162413Ssam else 356162413Ssam#endif 357162413Ssam val = bus_space_read_stream_4(tag, h, reg); 358162413Ssam return val; 359162413Ssam} 360162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 361162413Ssam 362162413Ssam#ifdef AH_ASSERT 363162413Ssamvoid 364162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg) 365162413Ssam{ 366162413Ssam printf("Atheros HAL assertion failure: %s: line %u: %s\n", 367162413Ssam filename, lineno, msg); 368162413Ssam panic("ath_hal_assert"); 369162413Ssam} 370162413Ssam#endif /* AH_ASSERT */ 371162413Ssam 372162413Ssam/* 373162413Ssam * Delay n microseconds. 374162413Ssam */ 375162413Ssamvoid 376162413Ssamath_hal_delay(int n) 377162413Ssam{ 378162413Ssam DELAY(n); 379162413Ssam} 380162413Ssam 381162413Ssamu_int32_t 382162413Ssamath_hal_getuptime(struct ath_hal *ah) 383162413Ssam{ 384162413Ssam struct bintime bt; 385162413Ssam getbinuptime(&bt); 386162413Ssam return (bt.sec * 1000) + 387162413Ssam (((uint64_t)1000 * (uint32_t)(bt.frac >> 32)) >> 32); 388162413Ssam} 389162413Ssam 390162413Ssamvoid 391162413Ssamath_hal_memzero(void *dst, size_t n) 392162413Ssam{ 393162413Ssam bzero(dst, n); 394162413Ssam} 395162413Ssam 396162413Ssamvoid * 397162413Ssamath_hal_memcpy(void *dst, const void *src, size_t n) 398162413Ssam{ 399162413Ssam return memcpy(dst, src, n); 400162413Ssam} 401162413Ssam 402162413Ssam/* 403162413Ssam * Module glue. 404162413Ssam */ 405162413Ssam 406162413Ssamstatic int 407162413Ssamath_hal_modevent(module_t mod, int type, void *unused) 408162413Ssam{ 409162413Ssam const char *sep; 410162413Ssam int i; 411162413Ssam 412162413Ssam switch (type) { 413162413Ssam case MOD_LOAD: 414162413Ssam printf("ath_hal: %s (", ath_hal_version); 415162413Ssam sep = ""; 416162413Ssam for (i = 0; ath_hal_buildopts[i] != NULL; i++) { 417162413Ssam printf("%s%s", sep, ath_hal_buildopts[i]); 418162413Ssam sep = ", "; 419162413Ssam } 420162413Ssam printf(")\n"); 421162413Ssam return 0; 422162413Ssam case MOD_UNLOAD: 423162413Ssam return 0; 424162413Ssam } 425162413Ssam return EINVAL; 426162413Ssam} 427162413Ssam 428162413Ssamstatic moduledata_t ath_hal_mod = { 429162413Ssam "ath_hal", 430162413Ssam ath_hal_modevent, 431162413Ssam 0 432162413Ssam}; 433162413SsamDECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 434162413SsamMODULE_VERSION(ath_hal, 1); 435