ah_osdep.c revision 196935
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 196935 2009-09-07 16:18:16Z 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 46185522Ssam#include <dev/ath/ath_hal/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 59185522Ssam#define BUSTAG(ah) ((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 74184369Ssamextern void HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...); 75162413Ssam#endif /* AH_DEBUG */ 76162413Ssam 77162413Ssam/* NB: put this here instead of the driver to avoid circular references */ 78162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 79162413SsamSYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters"); 80162413Ssam 81162413Ssam#ifdef AH_DEBUG 82162413Ssamstatic int ath_hal_debug = 0; 83162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 84162413Ssam 0, "Atheros HAL debugging printfs"); 85162413SsamTUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 86162413Ssam#endif /* AH_DEBUG */ 87162413Ssam 88162413Ssam/* NB: these are deprecated; they exist for now for compatibility */ 89162413Ssamint ath_hal_dma_beacon_response_time = 2; /* in TU's */ 90162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, dma_brt, CTLFLAG_RW, 91162413Ssam &ath_hal_dma_beacon_response_time, 0, 92162413Ssam "Atheros HAL DMA beacon response time"); 93162413Ssamint ath_hal_sw_beacon_response_time = 10; /* in TU's */ 94162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, sw_brt, CTLFLAG_RW, 95162413Ssam &ath_hal_sw_beacon_response_time, 0, 96162413Ssam "Atheros HAL software beacon response time"); 97162413Ssamint ath_hal_additional_swba_backoff = 0; /* in TU's */ 98162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, swba_backoff, CTLFLAG_RW, 99162413Ssam &ath_hal_additional_swba_backoff, 0, 100162413Ssam "Atheros HAL additional SWBA backoff time"); 101162413Ssam 102162413SsamMALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 103162413Ssam 104162413Ssamvoid* 105162413Ssamath_hal_malloc(size_t size) 106162413Ssam{ 107162413Ssam return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 108162413Ssam} 109162413Ssam 110162413Ssamvoid 111162413Ssamath_hal_free(void* p) 112162413Ssam{ 113196935Ssam free(p, M_ATH_HAL); 114162413Ssam} 115162413Ssam 116162413Ssamvoid 117162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 118162413Ssam{ 119162413Ssam vprintf(fmt, ap); 120162413Ssam} 121162413Ssam 122162413Ssamvoid 123162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 124162413Ssam{ 125162413Ssam va_list ap; 126162413Ssam va_start(ap, fmt); 127162413Ssam ath_hal_vprintf(ah, fmt, ap); 128162413Ssam va_end(ap); 129162413Ssam} 130162413Ssam 131162413Ssamconst char* 132162413Ssamath_hal_ether_sprintf(const u_int8_t *mac) 133162413Ssam{ 134162413Ssam return ether_sprintf(mac); 135162413Ssam} 136162413Ssam 137162413Ssam#ifdef AH_DEBUG 138162413Ssamvoid 139184369SsamHALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...) 140184369Ssam{ 141184369Ssam if (ath_hal_debug & mask) { 142184369Ssam __va_list ap; 143184369Ssam va_start(ap, fmt); 144184369Ssam ath_hal_vprintf(ah, fmt, ap); 145184369Ssam va_end(ap); 146184369Ssam } 147184369Ssam} 148162413Ssam#endif /* AH_DEBUG */ 149162413Ssam 150162413Ssam#ifdef AH_DEBUG_ALQ 151162413Ssam/* 152162413Ssam * ALQ register tracing support. 153162413Ssam * 154162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 155162413Ssam * writes to the file /tmp/ath_hal.log. The file format is a simple 156162413Ssam * fixed-size array of records. When done logging set hw.ath.hal.alq=0 157162413Ssam * and then decode the file with the arcode program (that is part of the 158162413Ssam * HAL). If you start+stop tracing the data will be appended to an 159162413Ssam * existing file. 160162413Ssam * 161162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record 162162413Ssam * is emitted and the different devices are not identified. 163162413Ssam */ 164162413Ssam#include <sys/alq.h> 165162413Ssam#include <sys/pcpu.h> 166185522Ssam#include <dev/ath/ath_hal/ah_decode.h> 167162413Ssam 168162413Ssamstatic struct alq *ath_hal_alq; 169162413Ssamstatic int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 170162413Ssamstatic u_int ath_hal_alq_lost; /* count of lost records */ 171162413Ssamstatic const char *ath_hal_logfile = "/tmp/ath_hal.log"; 172162413Ssamstatic u_int ath_hal_alq_qsize = 64*1024; 173162413Ssam 174162413Ssamstatic int 175162413Ssamath_hal_setlogging(int enable) 176162413Ssam{ 177162413Ssam int error; 178162413Ssam 179162413Ssam if (enable) { 180168589Srwatson error = alq_open(&ath_hal_alq, ath_hal_logfile, 181168589Srwatson curthread->td_ucred, ALQ_DEFAULT_CMODE, 182168589Srwatson sizeof (struct athregrec), ath_hal_alq_qsize); 183168589Srwatson ath_hal_alq_lost = 0; 184168589Srwatson ath_hal_alq_emitdev = 1; 185168589Srwatson printf("ath_hal: logging to %s enabled\n", 186168589Srwatson ath_hal_logfile); 187162413Ssam } else { 188162413Ssam if (ath_hal_alq) 189162413Ssam alq_close(ath_hal_alq); 190162413Ssam ath_hal_alq = NULL; 191162413Ssam printf("ath_hal: logging disabled\n"); 192162413Ssam error = 0; 193162413Ssam } 194162413Ssam return (error); 195162413Ssam} 196162413Ssam 197162413Ssamstatic int 198162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 199162413Ssam{ 200162413Ssam int error, enable; 201162413Ssam 202162413Ssam enable = (ath_hal_alq != NULL); 203162413Ssam error = sysctl_handle_int(oidp, &enable, 0, req); 204162413Ssam if (error || !req->newptr) 205162413Ssam return (error); 206162413Ssam else 207162413Ssam return (ath_hal_setlogging(enable)); 208162413Ssam} 209162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 210162413Ssam 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 211162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 212162413Ssam &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 213162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 214162413Ssam &ath_hal_alq_lost, 0, "Register operations not logged"); 215162413Ssam 216162413Ssamstatic struct ale * 217162413Ssamath_hal_alq_get(struct ath_hal *ah) 218162413Ssam{ 219162413Ssam struct ale *ale; 220162413Ssam 221162413Ssam if (ath_hal_alq_emitdev) { 222162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 223162413Ssam if (ale) { 224162413Ssam struct athregrec *r = 225162413Ssam (struct athregrec *) ale->ae_data; 226162413Ssam r->op = OP_DEVICE; 227162413Ssam r->reg = 0; 228162413Ssam r->val = ah->ah_devid; 229162413Ssam alq_post(ath_hal_alq, ale); 230162413Ssam ath_hal_alq_emitdev = 0; 231162413Ssam } else 232162413Ssam ath_hal_alq_lost++; 233162413Ssam } 234162413Ssam ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 235162413Ssam if (!ale) 236162413Ssam ath_hal_alq_lost++; 237162413Ssam return ale; 238162413Ssam} 239162413Ssam 240162413Ssamvoid 241162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 242162413Ssam{ 243162413Ssam bus_space_tag_t tag = BUSTAG(ah); 244185522Ssam bus_space_handle_t h = ah->ah_sh; 245162413Ssam 246162413Ssam if (ath_hal_alq) { 247162413Ssam struct ale *ale = ath_hal_alq_get(ah); 248162413Ssam if (ale) { 249162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 250162413Ssam r->op = OP_WRITE; 251162413Ssam r->reg = reg; 252162413Ssam r->val = val; 253162413Ssam alq_post(ath_hal_alq, ale); 254162413Ssam } 255162413Ssam } 256162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 257195418Ssam if (OS_REG_UNSWAPPED(reg)) 258162413Ssam bus_space_write_4(tag, h, reg, val); 259162413Ssam else 260162413Ssam#endif 261162413Ssam bus_space_write_stream_4(tag, h, reg, val); 262162413Ssam} 263162413Ssam 264162413Ssamu_int32_t 265162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 266162413Ssam{ 267162413Ssam bus_space_tag_t tag = BUSTAG(ah); 268185522Ssam bus_space_handle_t h = ah->ah_sh; 269162413Ssam u_int32_t val; 270162413Ssam 271162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 272195418Ssam if (OS_REG_UNSWAPPED(reg)) 273162413Ssam val = bus_space_read_4(tag, h, reg); 274162413Ssam else 275162413Ssam#endif 276162413Ssam val = bus_space_read_stream_4(tag, h, reg); 277162413Ssam if (ath_hal_alq) { 278162413Ssam struct ale *ale = ath_hal_alq_get(ah); 279162413Ssam if (ale) { 280162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 281162413Ssam r->op = OP_READ; 282162413Ssam r->reg = reg; 283162413Ssam r->val = val; 284162413Ssam alq_post(ath_hal_alq, ale); 285162413Ssam } 286162413Ssam } 287162413Ssam return val; 288162413Ssam} 289162413Ssam 290162413Ssamvoid 291162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 292162413Ssam{ 293162413Ssam if (ath_hal_alq) { 294162413Ssam struct ale *ale = ath_hal_alq_get(ah); 295162413Ssam if (ale) { 296162413Ssam struct athregrec *r = (struct athregrec *) ale->ae_data; 297162413Ssam r->op = OP_MARK; 298162413Ssam r->reg = id; 299162413Ssam r->val = v; 300162413Ssam alq_post(ath_hal_alq, ale); 301162413Ssam } 302162413Ssam } 303162413Ssam} 304162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 305162413Ssam/* 306162413Ssam * Memory-mapped device register read/write. These are here 307162413Ssam * as routines when debugging support is enabled and/or when 308162413Ssam * explicitly configured to use function calls. The latter is 309162413Ssam * for architectures that might need to do something before 310162413Ssam * referencing memory (e.g. remap an i/o window). 311162413Ssam * 312162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register 313162413Ssam * reads and writes to understand what's going on below. 314162413Ssam */ 315162413Ssam 316162413Ssamvoid 317162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 318162413Ssam{ 319162413Ssam bus_space_tag_t tag = BUSTAG(ah); 320185522Ssam bus_space_handle_t h = ah->ah_sh; 321162413Ssam 322162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 323195418Ssam if (OS_REG_UNSWAPPED(reg)) 324162413Ssam bus_space_write_4(tag, h, reg, val); 325162413Ssam else 326162413Ssam#endif 327162413Ssam bus_space_write_stream_4(tag, h, reg, val); 328162413Ssam} 329162413Ssam 330162413Ssamu_int32_t 331162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 332162413Ssam{ 333162413Ssam bus_space_tag_t tag = BUSTAG(ah); 334185522Ssam bus_space_handle_t h = ah->ah_sh; 335162413Ssam u_int32_t val; 336162413Ssam 337162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN 338195418Ssam if (OS_REG_UNSWAPPED(reg)) 339162413Ssam val = bus_space_read_4(tag, h, reg); 340162413Ssam else 341162413Ssam#endif 342162413Ssam val = bus_space_read_stream_4(tag, h, reg); 343162413Ssam return val; 344162413Ssam} 345162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 346162413Ssam 347162413Ssam#ifdef AH_ASSERT 348162413Ssamvoid 349162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg) 350162413Ssam{ 351162413Ssam printf("Atheros HAL assertion failure: %s: line %u: %s\n", 352162413Ssam filename, lineno, msg); 353162413Ssam panic("ath_hal_assert"); 354162413Ssam} 355162413Ssam#endif /* AH_ASSERT */ 356