ah_osdep.c revision 223525
1/*- 2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD: head/sys/dev/ath/ah_osdep.c 223525 2011-06-25 00:34:40Z adrian $ 30 */ 31#include "opt_ah.h" 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/sysctl.h> 38#include <sys/bus.h> 39#include <sys/malloc.h> 40#include <sys/proc.h> 41 42#include <machine/stdarg.h> 43 44#include <net/ethernet.h> /* XXX for ether_sprintf */ 45 46#include <dev/ath/ath_hal/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) ((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 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...); 75#endif /* AH_DEBUG */ 76 77/* NB: put this here instead of the driver to avoid circular references */ 78SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters"); 79SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters"); 80 81#ifdef AH_DEBUG 82int ath_hal_debug = 0; 83SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug, 84 0, "Atheros HAL debugging printfs"); 85TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug); 86#endif /* AH_DEBUG */ 87 88MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data"); 89 90void* 91ath_hal_malloc(size_t size) 92{ 93 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); 94} 95 96void 97ath_hal_free(void* p) 98{ 99 free(p, M_ATH_HAL); 100} 101 102void 103ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap) 104{ 105 vprintf(fmt, ap); 106} 107 108void 109ath_hal_printf(struct ath_hal *ah, const char* fmt, ...) 110{ 111 va_list ap; 112 va_start(ap, fmt); 113 ath_hal_vprintf(ah, fmt, ap); 114 va_end(ap); 115} 116 117const char* 118ath_hal_ether_sprintf(const u_int8_t *mac) 119{ 120 return ether_sprintf(mac); 121} 122 123#ifdef AH_DEBUG 124 125/* This must match the definition in ath_hal/ah_debug.h */ 126#define HAL_DEBUG_UNMASKABLE 0xf0000000 127void 128DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...) 129{ 130 if ((mask == HAL_DEBUG_UNMASKABLE) || (ah->ah_config.ah_debug & mask)) { 131 __va_list ap; 132 va_start(ap, fmt); 133 ath_hal_vprintf(ah, fmt, ap); 134 va_end(ap); 135 } 136} 137#undef HAL_DEBUG_UNMASKABLE 138#endif /* AH_DEBUG */ 139 140#ifdef AH_DEBUG_ALQ 141/* 142 * ALQ register tracing support. 143 * 144 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and 145 * writes to the file /tmp/ath_hal.log. The file format is a simple 146 * fixed-size array of records. When done logging set hw.ath.hal.alq=0 147 * and then decode the file with the arcode program (that is part of the 148 * HAL). If you start+stop tracing the data will be appended to an 149 * existing file. 150 * 151 * NB: doesn't handle multiple devices properly; only one DEVICE record 152 * is emitted and the different devices are not identified. 153 */ 154#include <sys/alq.h> 155#include <sys/pcpu.h> 156#include <dev/ath/ath_hal/ah_decode.h> 157 158static struct alq *ath_hal_alq; 159static int ath_hal_alq_emitdev; /* need to emit DEVICE record */ 160static u_int ath_hal_alq_lost; /* count of lost records */ 161static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log"; 162 163SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW, 164 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile"); 165 166static u_int ath_hal_alq_qsize = 64*1024; 167 168static int 169ath_hal_setlogging(int enable) 170{ 171 int error; 172 173 if (enable) { 174 error = alq_open(&ath_hal_alq, ath_hal_logfile, 175 curthread->td_ucred, ALQ_DEFAULT_CMODE, 176 sizeof (struct athregrec), ath_hal_alq_qsize); 177 ath_hal_alq_lost = 0; 178 ath_hal_alq_emitdev = 1; 179 printf("ath_hal: logging to %s enabled\n", 180 ath_hal_logfile); 181 } else { 182 if (ath_hal_alq) 183 alq_close(ath_hal_alq); 184 ath_hal_alq = NULL; 185 printf("ath_hal: logging disabled\n"); 186 error = 0; 187 } 188 return (error); 189} 190 191static int 192sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS) 193{ 194 int error, enable; 195 196 enable = (ath_hal_alq != NULL); 197 error = sysctl_handle_int(oidp, &enable, 0, req); 198 if (error || !req->newptr) 199 return (error); 200 else 201 return (ath_hal_setlogging(enable)); 202} 203SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW, 204 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging"); 205SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW, 206 &ath_hal_alq_qsize, 0, "In-memory log size (#records)"); 207SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW, 208 &ath_hal_alq_lost, 0, "Register operations not logged"); 209 210static struct ale * 211ath_hal_alq_get(struct ath_hal *ah) 212{ 213 struct ale *ale; 214 215 if (ath_hal_alq_emitdev) { 216 ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 217 if (ale) { 218 struct athregrec *r = 219 (struct athregrec *) ale->ae_data; 220 r->op = OP_DEVICE; 221 r->reg = 0; 222 r->val = ah->ah_devid; 223 alq_post(ath_hal_alq, ale); 224 ath_hal_alq_emitdev = 0; 225 } else 226 ath_hal_alq_lost++; 227 } 228 ale = alq_get(ath_hal_alq, ALQ_NOWAIT); 229 if (!ale) 230 ath_hal_alq_lost++; 231 return ale; 232} 233 234void 235ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 236{ 237 bus_space_tag_t tag = BUSTAG(ah); 238 bus_space_handle_t h = ah->ah_sh; 239 240 if (ath_hal_alq) { 241 struct ale *ale = ath_hal_alq_get(ah); 242 if (ale) { 243 struct athregrec *r = (struct athregrec *) ale->ae_data; 244 r->op = OP_WRITE; 245 r->reg = reg; 246 r->val = val; 247 alq_post(ath_hal_alq, ale); 248 } 249 } 250#if _BYTE_ORDER == _BIG_ENDIAN 251 if (OS_REG_UNSWAPPED(reg)) 252 bus_space_write_4(tag, h, reg, val); 253 else 254#endif 255 bus_space_write_stream_4(tag, h, reg, val); 256} 257 258u_int32_t 259ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 260{ 261 bus_space_tag_t tag = BUSTAG(ah); 262 bus_space_handle_t h = ah->ah_sh; 263 u_int32_t val; 264 265#if _BYTE_ORDER == _BIG_ENDIAN 266 if (OS_REG_UNSWAPPED(reg)) 267 val = bus_space_read_4(tag, h, reg); 268 else 269#endif 270 val = bus_space_read_stream_4(tag, h, reg); 271 if (ath_hal_alq) { 272 struct ale *ale = ath_hal_alq_get(ah); 273 if (ale) { 274 struct athregrec *r = (struct athregrec *) ale->ae_data; 275 r->op = OP_READ; 276 r->reg = reg; 277 r->val = val; 278 alq_post(ath_hal_alq, ale); 279 } 280 } 281 return val; 282} 283 284void 285OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v) 286{ 287 if (ath_hal_alq) { 288 struct ale *ale = ath_hal_alq_get(ah); 289 if (ale) { 290 struct athregrec *r = (struct athregrec *) ale->ae_data; 291 r->op = OP_MARK; 292 r->reg = id; 293 r->val = v; 294 alq_post(ath_hal_alq, ale); 295 } 296 } 297} 298#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC) 299/* 300 * Memory-mapped device register read/write. These are here 301 * as routines when debugging support is enabled and/or when 302 * explicitly configured to use function calls. The latter is 303 * for architectures that might need to do something before 304 * referencing memory (e.g. remap an i/o window). 305 * 306 * NB: see the comments in ah_osdep.h about byte-swapping register 307 * reads and writes to understand what's going on below. 308 */ 309 310void 311ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val) 312{ 313 bus_space_tag_t tag = BUSTAG(ah); 314 bus_space_handle_t h = ah->ah_sh; 315 316#if _BYTE_ORDER == _BIG_ENDIAN 317 if (OS_REG_UNSWAPPED(reg)) 318 bus_space_write_4(tag, h, reg, val); 319 else 320#endif 321 bus_space_write_stream_4(tag, h, reg, val); 322} 323 324u_int32_t 325ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg) 326{ 327 bus_space_tag_t tag = BUSTAG(ah); 328 bus_space_handle_t h = ah->ah_sh; 329 u_int32_t val; 330 331#if _BYTE_ORDER == _BIG_ENDIAN 332 if (OS_REG_UNSWAPPED(reg)) 333 val = bus_space_read_4(tag, h, reg); 334 else 335#endif 336 val = bus_space_read_stream_4(tag, h, reg); 337 return val; 338} 339#endif /* AH_DEBUG || AH_REGOPS_FUNC */ 340 341#ifdef AH_ASSERT 342void 343ath_hal_assert_failed(const char* filename, int lineno, const char *msg) 344{ 345 printf("Atheros HAL assertion failure: %s: line %u: %s\n", 346 filename, lineno, msg); 347 panic("ath_hal_assert"); 348} 349#endif /* AH_ASSERT */ 350