ah_osdep.c revision 178354
1193323Sed/*- 2193323Sed * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3193323Sed * All rights reserved. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer, 10193323Sed * without modification. 11193323Sed * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12193323Sed * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13193323Sed * redistribution must be conditioned upon including a substantially 14193323Sed * similar Disclaimer requirement for further binary redistribution. 15193323Sed * 16193323Sed * NO WARRANTY 17193323Sed * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18193323Sed * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19193323Sed * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20193323Sed * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21193323Sed * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22193323Sed * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23193323Sed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24193323Sed * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25193323Sed * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27206274Srdivacky * THE POSSIBILITY OF SUCH DAMAGES. 28193323Sed * 29193323Sed * $FreeBSD: head/sys/dev/ath/ah_osdep.c 178354 2008-04-20 20:35:46Z sam $ 30193323Sed */ 31193323Sed#include "opt_ah.h" 32193323Sed 33193323Sed#include <sys/param.h> 34193323Sed#include <sys/systm.h> 35193323Sed#include <sys/kernel.h> 36198090Srdivacky#include <sys/module.h> 37198090Srdivacky#include <sys/sysctl.h> 38193323Sed#include <sys/bus.h> 39193323Sed#include <sys/malloc.h> 40193323Sed#include <sys/proc.h> 41193323Sed 42193323Sed#include <machine/stdarg.h> 43193323Sed 44193323Sed#include <net/ethernet.h> /* XXX for ether_sprintf */ 45193323Sed 46193323Sed#include <contrib/dev/ath/ah.h> 47 48/* 49 * WiSoC boards overload the bus tag with information about the 50 * board layout. We must extract the bus space tag from that 51 * indirect structure. For everyone else the tag is passed in 52 * directly. 53 * XXX cache indirect ref privately 54 */ 55#ifdef AH_SUPPORT_AR5312 56#define BUSTAG(ah) \ 57 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag) 58#else 59#define BUSTAG(ah) ((bus_space_tag_t) (ah)->ah_st) 60#endif 61 62extern void ath_hal_printf(struct ath_hal *, const char*, ...) 63 __printflike(2,3); 64extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list) 65 __printflike(2, 0); 66extern const char* ath_hal_ether_sprintf(const u_int8_t *mac); 67extern void *ath_hal_malloc(size_t); 68extern void ath_hal_free(void *); 69#ifdef AH_ASSERT 70extern void ath_hal_assert_failed(const char* filename, 71 int lineno, const char* msg); 72#endif 73#ifdef AH_DEBUG 74extern void HALDEBUG(struct ath_hal *ah, const char* fmt, ...); 75extern void HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...); 76#endif /* AH_DEBUG */ 77 78/* NB: put this here instead of the driver to avoid circular references */ 79SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 80SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters"); 81 82#ifdef AH_DEBUG 83static int ath_hal_debug = 0; 84SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 85 0, "Atheros HAL debugging printfs"); 86TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 87#endif /* AH_DEBUG */ 88 89SYSCTL_STRING(_hw_ath_hal, OID_AUTO, version, CTLFLAG_RD, ath_hal_version, 0, 90 "Atheros HAL version"); 91 92/* NB: these are deprecated; they exist for now for compatibility */ 93int ath_hal_dma_beacon_response_time = 2; /* in TU's */ 94SYSCTL_INT(_hw_ath_hal, OID_AUTO, dma_brt, CTLFLAG_RW, 95 &ath_hal_dma_beacon_response_time, 0, 96 "Atheros HAL DMA beacon response time"); 97int ath_hal_sw_beacon_response_time = 10; /* in TU's */ 98SYSCTL_INT(_hw_ath_hal, OID_AUTO, sw_brt, CTLFLAG_RW, 99 &ath_hal_sw_beacon_response_time, 0, 100 "Atheros HAL software beacon response time"); 101int ath_hal_additional_swba_backoff = 0; /* in TU's */ 102SYSCTL_INT(_hw_ath_hal, OID_AUTO, swba_backoff, CTLFLAG_RW, 103 &ath_hal_additional_swba_backoff, 0, 104 "Atheros HAL additional SWBA backoff time"); 105 106MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 107 108void* 109ath_hal_malloc(size_t size) 110{ 111 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 112} 113 114void 115ath_hal_free(void* p) 116{ 117 return free(p, M_ATH_HAL); 118} 119 120void 121ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 122{ 123 vprintf(fmt, ap); 124} 125 126void 127ath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 128{ 129 va_list ap; 130 va_start(ap, fmt); 131 ath_hal_vprintf(ah, fmt, ap); 132 va_end(ap); 133} 134 135const char* 136ath_hal_ether_sprintf(const u_int8_t *mac) 137{ 138 return ether_sprintf(mac); 139} 140 141#ifdef AH_DEBUG 142void 143HALDEBUG(struct ath_hal *ah, const char* fmt, ...) 144{ 145 if (ath_hal_debug) { 146 __va_list ap; 147 va_start(ap, fmt); 148 ath_hal_vprintf(ah, fmt, ap); 149 va_end(ap); 150 } 151} 152 153void 154HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...) 155{ 156 if (ath_hal_debug >= level) { 157 __va_list ap; 158 va_start(ap, fmt); 159 ath_hal_vprintf(ah, fmt, ap); 160 va_end(ap); 161 } 162} 163#endif /* AH_DEBUG */ 164 165#ifdef AH_DEBUG_ALQ 166/* 167 * ALQ register tracing support. 168 * 169 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 170 * writes to the file /tmp/ath_hal.log. The file format is a simple 171 * fixed-size array of records. When done logging set hw.ath.hal.alq=0 172 * and then decode the file with the arcode program (that is part of the 173 * HAL). If you start+stop tracing the data will be appended to an 174 * existing file. 175 * 176 * NB: doesn't handle multiple devices properly; only one DEVICE record 177 * is emitted and the different devices are not identified. 178 */ 179#include <sys/alq.h> 180#include <sys/pcpu.h> 181#include <contrib/dev/ath/ah_decode.h> 182 183static struct alq *ath_hal_alq; 184static int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 185static u_int ath_hal_alq_lost; /* count of lost records */ 186static const char *ath_hal_logfile = "/tmp/ath_hal.log"; 187static u_int ath_hal_alq_qsize = 64*1024; 188 189static int 190ath_hal_setlogging(int enable) 191{ 192 int error; 193 194 if (enable) { 195 error = alq_open(&ath_hal_alq, ath_hal_logfile, 196 curthread->td_ucred, ALQ_DEFAULT_CMODE, 197 sizeof (struct athregrec), ath_hal_alq_qsize); 198 ath_hal_alq_lost = 0; 199 ath_hal_alq_emitdev = 1; 200 printf("ath_hal: logging to %s enabled\n", 201 ath_hal_logfile); 202 } else { 203 if (ath_hal_alq) 204 alq_close(ath_hal_alq); 205 ath_hal_alq = NULL; 206 printf("ath_hal: logging disabled\n"); 207 error = 0; 208 } 209 return (error); 210} 211 212static int 213sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 214{ 215 int error, enable; 216 217 enable = (ath_hal_alq != NULL); 218 error = sysctl_handle_int(oidp, &enable, 0, req); 219 if (error || !req->newptr) 220 return (error); 221 else 222 return (ath_hal_setlogging(enable)); 223} 224SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 225 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 226SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 227 &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 228SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 229 &ath_hal_alq_lost, 0, "Register operations not logged"); 230 231static struct ale * 232ath_hal_alq_get(struct ath_hal *ah) 233{ 234 struct ale *ale; 235 236 if (ath_hal_alq_emitdev) { 237 ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 238 if (ale) { 239 struct athregrec *r = 240 (struct athregrec *) ale->ae_data; 241 r->op = OP_DEVICE; 242 r->reg = 0; 243 r->val = ah->ah_devid; 244 alq_post(ath_hal_alq, ale); 245 ath_hal_alq_emitdev = 0; 246 } else 247 ath_hal_alq_lost++; 248 } 249 ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 250 if (!ale) 251 ath_hal_alq_lost++; 252 return ale; 253} 254 255void 256ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 257{ 258 bus_space_tag_t tag = BUSTAG(ah); 259 bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 260 261 if (ath_hal_alq) { 262 struct ale *ale = ath_hal_alq_get(ah); 263 if (ale) { 264 struct athregrec *r = (struct athregrec *) ale->ae_data; 265 r->op = OP_WRITE; 266 r->reg = reg; 267 r->val = val; 268 alq_post(ath_hal_alq, ale); 269 } 270 } 271#if _BYTE_ORDER == _BIG_ENDIAN 272 if (reg >= 0x4000 && reg < 0x5000) 273 bus_space_write_4(tag, h, reg, val); 274 else 275#endif 276 bus_space_write_stream_4(tag, h, reg, val); 277} 278 279u_int32_t 280ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 281{ 282 bus_space_tag_t tag = BUSTAG(ah); 283 bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 284 u_int32_t val; 285 286#if _BYTE_ORDER == _BIG_ENDIAN 287 if (reg >= 0x4000 && reg < 0x5000) 288 val = bus_space_read_4(tag, h, reg); 289 else 290#endif 291 val = bus_space_read_stream_4(tag, h, reg); 292 if (ath_hal_alq) { 293 struct ale *ale = ath_hal_alq_get(ah); 294 if (ale) { 295 struct athregrec *r = (struct athregrec *) ale->ae_data; 296 r->op = OP_READ; 297 r->reg = reg; 298 r->val = val; 299 alq_post(ath_hal_alq, ale); 300 } 301 } 302 return val; 303} 304 305void 306OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 307{ 308 if (ath_hal_alq) { 309 struct ale *ale = ath_hal_alq_get(ah); 310 if (ale) { 311 struct athregrec *r = (struct athregrec *) ale->ae_data; 312 r->op = OP_MARK; 313 r->reg = id; 314 r->val = v; 315 alq_post(ath_hal_alq, ale); 316 } 317 } 318} 319#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 320/* 321 * Memory-mapped device register read/write. These are here 322 * as routines when debugging support is enabled and/or when 323 * explicitly configured to use function calls. The latter is 324 * for architectures that might need to do something before 325 * referencing memory (e.g. remap an i/o window). 326 * 327 * NB: see the comments in ah_osdep.h about byte-swapping register 328 * reads and writes to understand what's going on below. 329 */ 330 331void 332ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 333{ 334 bus_space_tag_t tag = BUSTAG(ah); 335 bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 336 337#if _BYTE_ORDER == _BIG_ENDIAN 338 if (reg >= 0x4000 && reg < 0x5000) 339 bus_space_write_4(tag, h, reg, val); 340 else 341#endif 342 bus_space_write_stream_4(tag, h, reg, val); 343} 344 345u_int32_t 346ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 347{ 348 bus_space_tag_t tag = BUSTAG(ah); 349 bus_space_handle_t h = (bus_space_handle_t) ah->ah_sh; 350 u_int32_t val; 351 352#if _BYTE_ORDER == _BIG_ENDIAN 353 if (reg >= 0x4000 && reg < 0x5000) 354 val = bus_space_read_4(tag, h, reg); 355 else 356#endif 357 val = bus_space_read_stream_4(tag, h, reg); 358 return val; 359} 360#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 361 362#ifdef AH_ASSERT 363void 364ath_hal_assert_failed(const char* filename, int lineno, const char *msg) 365{ 366 printf("Atheros HAL assertion failure: %s: line %u: %s\n", 367 filename, lineno, msg); 368 panic("ath_hal_assert"); 369} 370#endif /* AH_ASSERT */ 371 372/* 373 * Delay n microseconds. 374 */ 375void 376ath_hal_delay(int n) 377{ 378 DELAY(n); 379} 380 381u_int32_t 382ath_hal_getuptime(struct ath_hal *ah) 383{ 384 struct bintime bt; 385 getbinuptime(&bt); 386 return (bt.sec * 1000) + 387 (((uint64_t)1000 * (uint32_t)(bt.frac >> 32)) >> 32); 388} 389 390void 391ath_hal_memzero(void *dst, size_t n) 392{ 393 bzero(dst, n); 394} 395 396void * 397ath_hal_memcpy(void *dst, const void *src, size_t n) 398{ 399 return memcpy(dst, src, n); 400} 401 402/* 403 * Module glue. 404 */ 405 406static int 407ath_hal_modevent(module_t mod, int type, void *unused) 408{ 409 const char *sep; 410 int i; 411 412 switch (type) { 413 case MOD_LOAD: 414 printf("ath_hal: %s (", ath_hal_version); 415 sep = ""; 416 for (i = 0; ath_hal_buildopts[i] != NULL; i++) { 417 printf("%s%s", sep, ath_hal_buildopts[i]); 418 sep = ", "; 419 } 420 printf(")\n"); 421 return 0; 422 case MOD_UNLOAD: 423 return 0; 424 } 425 return EINVAL; 426} 427 428static moduledata_t ath_hal_mod = { 429 "ath_hal", 430 ath_hal_modevent, 431 0 432}; 433DECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 434MODULE_VERSION(ath_hal, 1); 435