ah_osdep.c revision 223525
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 223525 2011-06-25 00:34:40Z 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>
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
74219315Sadrianextern	void DO_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");
79223525SadrianSYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0, "Atheros HAL parameters");
80162413Ssam
81223525Sadrian#ifdef AH_DEBUG
82223525Sadrianint ath_hal_debug = 0;
83223525SadrianSYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
84223525Sadrian    0, "Atheros HAL debugging printfs");
85223525SadrianTUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
86223525Sadrian#endif /* AH_DEBUG */
87223525Sadrian
88162413SsamMALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
89162413Ssam
90162413Ssamvoid*
91162413Ssamath_hal_malloc(size_t size)
92162413Ssam{
93162413Ssam	return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
94162413Ssam}
95162413Ssam
96162413Ssamvoid
97162413Ssamath_hal_free(void* p)
98162413Ssam{
99196935Ssam	free(p, M_ATH_HAL);
100162413Ssam}
101162413Ssam
102162413Ssamvoid
103162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
104162413Ssam{
105162413Ssam	vprintf(fmt, ap);
106162413Ssam}
107162413Ssam
108162413Ssamvoid
109162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
110162413Ssam{
111162413Ssam	va_list ap;
112162413Ssam	va_start(ap, fmt);
113162413Ssam	ath_hal_vprintf(ah, fmt, ap);
114162413Ssam	va_end(ap);
115162413Ssam}
116162413Ssam
117162413Ssamconst char*
118162413Ssamath_hal_ether_sprintf(const u_int8_t *mac)
119162413Ssam{
120162413Ssam	return ether_sprintf(mac);
121162413Ssam}
122162413Ssam
123162413Ssam#ifdef AH_DEBUG
124222031Sadrian
125222031Sadrian/* This must match the definition in ath_hal/ah_debug.h */
126222031Sadrian#define	HAL_DEBUG_UNMASKABLE	0xf0000000
127162413Ssamvoid
128219315SadrianDO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
129184369Ssam{
130223459Sadrian	if ((mask == HAL_DEBUG_UNMASKABLE) || (ah->ah_config.ah_debug & mask)) {
131184369Ssam		__va_list ap;
132184369Ssam		va_start(ap, fmt);
133184369Ssam		ath_hal_vprintf(ah, fmt, ap);
134184369Ssam		va_end(ap);
135184369Ssam	}
136184369Ssam}
137222031Sadrian#undef	HAL_DEBUG_UNMASKABLE
138162413Ssam#endif /* AH_DEBUG */
139162413Ssam
140162413Ssam#ifdef AH_DEBUG_ALQ
141162413Ssam/*
142162413Ssam * ALQ register tracing support.
143162413Ssam *
144162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
145162413Ssam * writes to the file /tmp/ath_hal.log.  The file format is a simple
146162413Ssam * fixed-size array of records.  When done logging set hw.ath.hal.alq=0
147162413Ssam * and then decode the file with the arcode program (that is part of the
148162413Ssam * HAL).  If you start+stop tracing the data will be appended to an
149162413Ssam * existing file.
150162413Ssam *
151162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record
152162413Ssam *     is emitted and the different devices are not identified.
153162413Ssam */
154162413Ssam#include <sys/alq.h>
155162413Ssam#include <sys/pcpu.h>
156185522Ssam#include <dev/ath/ath_hal/ah_decode.h>
157162413Ssam
158162413Ssamstatic	struct alq *ath_hal_alq;
159162413Ssamstatic	int ath_hal_alq_emitdev;	/* need to emit DEVICE record */
160162413Ssamstatic	u_int ath_hal_alq_lost;		/* count of lost records */
161220367Sadrianstatic	char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
162220367Sadrian
163220367SadrianSYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
164220367Sadrian    &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
165220367Sadrian
166162413Ssamstatic	u_int ath_hal_alq_qsize = 64*1024;
167162413Ssam
168162413Ssamstatic int
169162413Ssamath_hal_setlogging(int enable)
170162413Ssam{
171162413Ssam	int error;
172162413Ssam
173162413Ssam	if (enable) {
174168589Srwatson		error = alq_open(&ath_hal_alq, ath_hal_logfile,
175168589Srwatson			curthread->td_ucred, ALQ_DEFAULT_CMODE,
176168589Srwatson			sizeof (struct athregrec), ath_hal_alq_qsize);
177168589Srwatson		ath_hal_alq_lost = 0;
178168589Srwatson		ath_hal_alq_emitdev = 1;
179168589Srwatson		printf("ath_hal: logging to %s enabled\n",
180168589Srwatson			ath_hal_logfile);
181162413Ssam	} else {
182162413Ssam		if (ath_hal_alq)
183162413Ssam			alq_close(ath_hal_alq);
184162413Ssam		ath_hal_alq = NULL;
185162413Ssam		printf("ath_hal: logging disabled\n");
186162413Ssam		error = 0;
187162413Ssam	}
188162413Ssam	return (error);
189162413Ssam}
190162413Ssam
191162413Ssamstatic int
192162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
193162413Ssam{
194162413Ssam	int error, enable;
195162413Ssam
196162413Ssam	enable = (ath_hal_alq != NULL);
197162413Ssam        error = sysctl_handle_int(oidp, &enable, 0, req);
198162413Ssam        if (error || !req->newptr)
199162413Ssam                return (error);
200162413Ssam	else
201162413Ssam		return (ath_hal_setlogging(enable));
202162413Ssam}
203162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
204162413Ssam	0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
205162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
206162413Ssam	&ath_hal_alq_qsize, 0, "In-memory log size (#records)");
207162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
208162413Ssam	&ath_hal_alq_lost, 0, "Register operations not logged");
209162413Ssam
210162413Ssamstatic struct ale *
211162413Ssamath_hal_alq_get(struct ath_hal *ah)
212162413Ssam{
213162413Ssam	struct ale *ale;
214162413Ssam
215162413Ssam	if (ath_hal_alq_emitdev) {
216162413Ssam		ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
217162413Ssam		if (ale) {
218162413Ssam			struct athregrec *r =
219162413Ssam				(struct athregrec *) ale->ae_data;
220162413Ssam			r->op = OP_DEVICE;
221162413Ssam			r->reg = 0;
222162413Ssam			r->val = ah->ah_devid;
223162413Ssam			alq_post(ath_hal_alq, ale);
224162413Ssam			ath_hal_alq_emitdev = 0;
225162413Ssam		} else
226162413Ssam			ath_hal_alq_lost++;
227162413Ssam	}
228162413Ssam	ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
229162413Ssam	if (!ale)
230162413Ssam		ath_hal_alq_lost++;
231162413Ssam	return ale;
232162413Ssam}
233162413Ssam
234162413Ssamvoid
235162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
236162413Ssam{
237162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
238185522Ssam	bus_space_handle_t h = ah->ah_sh;
239162413Ssam
240162413Ssam	if (ath_hal_alq) {
241162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
242162413Ssam		if (ale) {
243162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
244162413Ssam			r->op = OP_WRITE;
245162413Ssam			r->reg = reg;
246162413Ssam			r->val = val;
247162413Ssam			alq_post(ath_hal_alq, ale);
248162413Ssam		}
249162413Ssam	}
250162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN
251195418Ssam	if (OS_REG_UNSWAPPED(reg))
252162413Ssam		bus_space_write_4(tag, h, reg, val);
253162413Ssam	else
254162413Ssam#endif
255162413Ssam		bus_space_write_stream_4(tag, h, reg, val);
256162413Ssam}
257162413Ssam
258162413Ssamu_int32_t
259162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
260162413Ssam{
261162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
262185522Ssam	bus_space_handle_t h = ah->ah_sh;
263162413Ssam	u_int32_t val;
264162413Ssam
265162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN
266195418Ssam	if (OS_REG_UNSWAPPED(reg))
267162413Ssam		val = bus_space_read_4(tag, h, reg);
268162413Ssam	else
269162413Ssam#endif
270162413Ssam		val = bus_space_read_stream_4(tag, h, reg);
271162413Ssam	if (ath_hal_alq) {
272162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
273162413Ssam		if (ale) {
274162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
275162413Ssam			r->op = OP_READ;
276162413Ssam			r->reg = reg;
277162413Ssam			r->val = val;
278162413Ssam			alq_post(ath_hal_alq, ale);
279162413Ssam		}
280162413Ssam	}
281162413Ssam	return val;
282162413Ssam}
283162413Ssam
284162413Ssamvoid
285162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
286162413Ssam{
287162413Ssam	if (ath_hal_alq) {
288162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
289162413Ssam		if (ale) {
290162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
291162413Ssam			r->op = OP_MARK;
292162413Ssam			r->reg = id;
293162413Ssam			r->val = v;
294162413Ssam			alq_post(ath_hal_alq, ale);
295162413Ssam		}
296162413Ssam	}
297162413Ssam}
298162413Ssam#elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
299162413Ssam/*
300162413Ssam * Memory-mapped device register read/write.  These are here
301162413Ssam * as routines when debugging support is enabled and/or when
302162413Ssam * explicitly configured to use function calls.  The latter is
303162413Ssam * for architectures that might need to do something before
304162413Ssam * referencing memory (e.g. remap an i/o window).
305162413Ssam *
306162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register
307162413Ssam *     reads and writes to understand what's going on below.
308162413Ssam */
309162413Ssam
310162413Ssamvoid
311162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
312162413Ssam{
313162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
314185522Ssam	bus_space_handle_t h = ah->ah_sh;
315162413Ssam
316162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN
317195418Ssam	if (OS_REG_UNSWAPPED(reg))
318162413Ssam		bus_space_write_4(tag, h, reg, val);
319162413Ssam	else
320162413Ssam#endif
321162413Ssam		bus_space_write_stream_4(tag, h, reg, val);
322162413Ssam}
323162413Ssam
324162413Ssamu_int32_t
325162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
326162413Ssam{
327162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
328185522Ssam	bus_space_handle_t h = ah->ah_sh;
329162413Ssam	u_int32_t val;
330162413Ssam
331162413Ssam#if _BYTE_ORDER == _BIG_ENDIAN
332195418Ssam	if (OS_REG_UNSWAPPED(reg))
333162413Ssam		val = bus_space_read_4(tag, h, reg);
334162413Ssam	else
335162413Ssam#endif
336162413Ssam		val = bus_space_read_stream_4(tag, h, reg);
337162413Ssam	return val;
338162413Ssam}
339162413Ssam#endif /* AH_DEBUG || AH_REGOPS_FUNC */
340162413Ssam
341162413Ssam#ifdef AH_ASSERT
342162413Ssamvoid
343162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg)
344162413Ssam{
345162413Ssam	printf("Atheros HAL assertion failure: %s: line %u: %s\n",
346162413Ssam		filename, lineno, msg);
347162413Ssam	panic("ath_hal_assert");
348162413Ssam}
349162413Ssam#endif /* AH_ASSERT */
350