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$
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>
41233887Sadrian#include <sys/pcpu.h>
42227410Sadrian#include <sys/lock.h>
43227410Sadrian#include <sys/mutex.h>
44162413Ssam
45162413Ssam#include <machine/stdarg.h>
46162413Ssam
47162413Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
48162413Ssam
49185522Ssam#include <dev/ath/ath_hal/ah.h>
50237864Sadrian#include <dev/ath/ath_hal/ah_debug.h>
51162413Ssam
52162413Ssam/*
53162413Ssam * WiSoC boards overload the bus tag with information about the
54162413Ssam * board layout.  We must extract the bus space tag from that
55162413Ssam * indirect structure.  For everyone else the tag is passed in
56162413Ssam * directly.
57162413Ssam * XXX cache indirect ref privately
58162413Ssam */
59162413Ssam#ifdef AH_SUPPORT_AR5312
60162413Ssam#define	BUSTAG(ah) \
61162413Ssam	((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
62162413Ssam#else
63185522Ssam#define	BUSTAG(ah)	((ah)->ah_st)
64162413Ssam#endif
65162413Ssam
66227410Sadrian/*
67227410Sadrian * This lock is used to seralise register access for chips which have
68227410Sadrian * problems w/ SMP CPUs issuing concurrent PCI transactions.
69227410Sadrian *
70227410Sadrian * XXX This is a global lock for now; it should be pushed to
71227410Sadrian * a per-device lock in some platform-independent fashion.
72227410Sadrian */
73227410Sadrianstruct mtx ah_regser_mtx;
74227410SadrianMTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex",
75227410Sadrian    MTX_SPIN);
76227410Sadrian
77162413Ssamextern	void ath_hal_printf(struct ath_hal *, const char*, ...)
78162413Ssam		__printflike(2,3);
79162413Ssamextern	void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
80162413Ssam		__printflike(2, 0);
81162413Ssamextern	const char* ath_hal_ether_sprintf(const u_int8_t *mac);
82162413Ssamextern	void *ath_hal_malloc(size_t);
83162413Ssamextern	void ath_hal_free(void *);
84162413Ssam#ifdef AH_ASSERT
85162413Ssamextern	void ath_hal_assert_failed(const char* filename,
86162413Ssam		int lineno, const char* msg);
87162413Ssam#endif
88162413Ssam#ifdef AH_DEBUG
89219315Sadrianextern	void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
90162413Ssam#endif /* AH_DEBUG */
91162413Ssam
92162413Ssam/* NB: put this here instead of the driver to avoid circular references */
93162413SsamSYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
94227309Sedstatic SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
95227309Sed    "Atheros HAL parameters");
96162413Ssam
97223525Sadrian#ifdef AH_DEBUG
98223525Sadrianint ath_hal_debug = 0;
99267992ShselaskySYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RWTUN, &ath_hal_debug,
100223525Sadrian    0, "Atheros HAL debugging printfs");
101223525Sadrian#endif /* AH_DEBUG */
102223525Sadrian
103227293Sedstatic MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
104162413Ssam
105162413Ssamvoid*
106162413Ssamath_hal_malloc(size_t size)
107162413Ssam{
108162413Ssam	return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
109162413Ssam}
110162413Ssam
111162413Ssamvoid
112162413Ssamath_hal_free(void* p)
113162413Ssam{
114196935Ssam	free(p, M_ATH_HAL);
115162413Ssam}
116162413Ssam
117162413Ssamvoid
118162413Ssamath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
119162413Ssam{
120162413Ssam	vprintf(fmt, ap);
121162413Ssam}
122162413Ssam
123162413Ssamvoid
124162413Ssamath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
125162413Ssam{
126162413Ssam	va_list ap;
127162413Ssam	va_start(ap, fmt);
128162413Ssam	ath_hal_vprintf(ah, fmt, ap);
129162413Ssam	va_end(ap);
130162413Ssam}
131162413Ssam
132162413Ssamconst char*
133162413Ssamath_hal_ether_sprintf(const u_int8_t *mac)
134162413Ssam{
135162413Ssam	return ether_sprintf(mac);
136162413Ssam}
137162413Ssam
138162413Ssam#ifdef AH_DEBUG
139222031Sadrian
140264292Sadrian/*
141264292Sadrian * XXX This is highly relevant only for the AR5416 and later
142264292Sadrian * PCI/PCIe NICs.  It'll need adjustment for other hardware
143264292Sadrian * variations.
144264292Sadrian */
145264292Sadrianstatic int
146264292Sadrianath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg)
147264292Sadrian{
148264292Sadrian
149264292Sadrian	if (reg >= 0x4000 && reg < 0x5000)
150264292Sadrian		return (1);
151264292Sadrian	if (reg >= 0x6000 && reg < 0x7000)
152264292Sadrian		return (1);
153264292Sadrian	if (reg >= 0x7000 && reg < 0x8000)
154264292Sadrian		return (1);
155264292Sadrian	return (0);
156264292Sadrian}
157264292Sadrian
158162413Ssamvoid
159219315SadrianDO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
160184369Ssam{
161224045Sadrian	if ((mask == HAL_DEBUG_UNMASKABLE) ||
162225883Sadrian	    (ah != NULL && ah->ah_config.ah_debug & mask) ||
163224045Sadrian	    (ath_hal_debug & mask)) {
164184369Ssam		__va_list ap;
165184369Ssam		va_start(ap, fmt);
166184369Ssam		ath_hal_vprintf(ah, fmt, ap);
167184369Ssam		va_end(ap);
168184369Ssam	}
169184369Ssam}
170222031Sadrian#undef	HAL_DEBUG_UNMASKABLE
171162413Ssam#endif /* AH_DEBUG */
172162413Ssam
173162413Ssam#ifdef AH_DEBUG_ALQ
174162413Ssam/*
175162413Ssam * ALQ register tracing support.
176162413Ssam *
177162413Ssam * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
178162413Ssam * writes to the file /tmp/ath_hal.log.  The file format is a simple
179162413Ssam * fixed-size array of records.  When done logging set hw.ath.hal.alq=0
180162413Ssam * and then decode the file with the arcode program (that is part of the
181162413Ssam * HAL).  If you start+stop tracing the data will be appended to an
182162413Ssam * existing file.
183162413Ssam *
184162413Ssam * NB: doesn't handle multiple devices properly; only one DEVICE record
185162413Ssam *     is emitted and the different devices are not identified.
186162413Ssam */
187162413Ssam#include <sys/alq.h>
188162413Ssam#include <sys/pcpu.h>
189185522Ssam#include <dev/ath/ath_hal/ah_decode.h>
190162413Ssam
191162413Ssamstatic	struct alq *ath_hal_alq;
192162413Ssamstatic	int ath_hal_alq_emitdev;	/* need to emit DEVICE record */
193162413Ssamstatic	u_int ath_hal_alq_lost;		/* count of lost records */
194220367Sadrianstatic	char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
195220367Sadrian
196220367SadrianSYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
197220367Sadrian    &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
198220367Sadrian
199162413Ssamstatic	u_int ath_hal_alq_qsize = 64*1024;
200162413Ssam
201162413Ssamstatic int
202162413Ssamath_hal_setlogging(int enable)
203162413Ssam{
204162413Ssam	int error;
205162413Ssam
206162413Ssam	if (enable) {
207168589Srwatson		error = alq_open(&ath_hal_alq, ath_hal_logfile,
208168589Srwatson			curthread->td_ucred, ALQ_DEFAULT_CMODE,
209168589Srwatson			sizeof (struct athregrec), ath_hal_alq_qsize);
210168589Srwatson		ath_hal_alq_lost = 0;
211168589Srwatson		ath_hal_alq_emitdev = 1;
212168589Srwatson		printf("ath_hal: logging to %s enabled\n",
213168589Srwatson			ath_hal_logfile);
214162413Ssam	} else {
215162413Ssam		if (ath_hal_alq)
216162413Ssam			alq_close(ath_hal_alq);
217162413Ssam		ath_hal_alq = NULL;
218162413Ssam		printf("ath_hal: logging disabled\n");
219162413Ssam		error = 0;
220162413Ssam	}
221162413Ssam	return (error);
222162413Ssam}
223162413Ssam
224162413Ssamstatic int
225162413Ssamsysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
226162413Ssam{
227162413Ssam	int error, enable;
228162413Ssam
229162413Ssam	enable = (ath_hal_alq != NULL);
230162413Ssam        error = sysctl_handle_int(oidp, &enable, 0, req);
231162413Ssam        if (error || !req->newptr)
232162413Ssam                return (error);
233162413Ssam	else
234162413Ssam		return (ath_hal_setlogging(enable));
235162413Ssam}
236162413SsamSYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
237162413Ssam	0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
238162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
239162413Ssam	&ath_hal_alq_qsize, 0, "In-memory log size (#records)");
240162413SsamSYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
241162413Ssam	&ath_hal_alq_lost, 0, "Register operations not logged");
242162413Ssam
243162413Ssamstatic struct ale *
244162413Ssamath_hal_alq_get(struct ath_hal *ah)
245162413Ssam{
246162413Ssam	struct ale *ale;
247162413Ssam
248162413Ssam	if (ath_hal_alq_emitdev) {
249162413Ssam		ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
250162413Ssam		if (ale) {
251162413Ssam			struct athregrec *r =
252162413Ssam				(struct athregrec *) ale->ae_data;
253162413Ssam			r->op = OP_DEVICE;
254162413Ssam			r->reg = 0;
255162413Ssam			r->val = ah->ah_devid;
256162413Ssam			alq_post(ath_hal_alq, ale);
257162413Ssam			ath_hal_alq_emitdev = 0;
258162413Ssam		} else
259162413Ssam			ath_hal_alq_lost++;
260162413Ssam	}
261162413Ssam	ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
262162413Ssam	if (!ale)
263162413Ssam		ath_hal_alq_lost++;
264162413Ssam	return ale;
265162413Ssam}
266162413Ssam
267162413Ssamvoid
268162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
269162413Ssam{
270162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
271185522Ssam	bus_space_handle_t h = ah->ah_sh;
272162413Ssam
273293111Sadrian#ifdef	AH_DEBUG
274263418Sadrian	/* Debug - complain if we haven't fully waken things up */
275264292Sadrian	if (! ath_hal_reg_whilst_asleep(ah, reg) &&
276264292Sadrian	    ah->ah_powerMode != HAL_PM_AWAKE) {
277263418Sadrian		ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
278263418Sadrian		    __func__, reg, val, ah->ah_powerMode);
279263418Sadrian	}
280293111Sadrian#endif
281263418Sadrian
282162413Ssam	if (ath_hal_alq) {
283162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
284162413Ssam		if (ale) {
285162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
286233887Sadrian			r->threadid = curthread->td_tid;
287162413Ssam			r->op = OP_WRITE;
288162413Ssam			r->reg = reg;
289162413Ssam			r->val = val;
290162413Ssam			alq_post(ath_hal_alq, ale);
291162413Ssam		}
292162413Ssam	}
293227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
294227410Sadrian		mtx_lock_spin(&ah_regser_mtx);
295234450Sadrian	bus_space_write_4(tag, h, reg, val);
296293050Sadrian	OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
297227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
298227410Sadrian		mtx_unlock_spin(&ah_regser_mtx);
299162413Ssam}
300162413Ssam
301162413Ssamu_int32_t
302162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
303162413Ssam{
304162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
305185522Ssam	bus_space_handle_t h = ah->ah_sh;
306162413Ssam	u_int32_t val;
307162413Ssam
308293111Sadrian#ifdef	AH_DEBUG
309263418Sadrian	/* Debug - complain if we haven't fully waken things up */
310264292Sadrian	if (! ath_hal_reg_whilst_asleep(ah, reg) &&
311264292Sadrian	    ah->ah_powerMode != HAL_PM_AWAKE) {
312263418Sadrian		ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
313263418Sadrian		    __func__, reg, ah->ah_powerMode);
314263418Sadrian	}
315293111Sadrian#endif
316263418Sadrian
317227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
318227410Sadrian		mtx_lock_spin(&ah_regser_mtx);
319293050Sadrian	OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
320234450Sadrian	val = bus_space_read_4(tag, h, reg);
321227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
322227410Sadrian		mtx_unlock_spin(&ah_regser_mtx);
323162413Ssam	if (ath_hal_alq) {
324162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
325162413Ssam		if (ale) {
326162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
327233887Sadrian			r->threadid = curthread->td_tid;
328162413Ssam			r->op = OP_READ;
329162413Ssam			r->reg = reg;
330162413Ssam			r->val = val;
331162413Ssam			alq_post(ath_hal_alq, ale);
332162413Ssam		}
333162413Ssam	}
334162413Ssam	return val;
335162413Ssam}
336162413Ssam
337162413Ssamvoid
338162413SsamOS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
339162413Ssam{
340162413Ssam	if (ath_hal_alq) {
341162413Ssam		struct ale *ale = ath_hal_alq_get(ah);
342162413Ssam		if (ale) {
343162413Ssam			struct athregrec *r = (struct athregrec *) ale->ae_data;
344233887Sadrian			r->threadid = curthread->td_tid;
345162413Ssam			r->op = OP_MARK;
346162413Ssam			r->reg = id;
347162413Ssam			r->val = v;
348162413Ssam			alq_post(ath_hal_alq, ale);
349162413Ssam		}
350162413Ssam	}
351162413Ssam}
352293111Sadrian#else /* AH_DEBUG_ALQ */
353293111Sadrian
354162413Ssam/*
355162413Ssam * Memory-mapped device register read/write.  These are here
356162413Ssam * as routines when debugging support is enabled and/or when
357162413Ssam * explicitly configured to use function calls.  The latter is
358162413Ssam * for architectures that might need to do something before
359162413Ssam * referencing memory (e.g. remap an i/o window).
360162413Ssam *
361162413Ssam * NB: see the comments in ah_osdep.h about byte-swapping register
362162413Ssam *     reads and writes to understand what's going on below.
363162413Ssam */
364162413Ssam
365162413Ssamvoid
366162413Ssamath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
367162413Ssam{
368162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
369185522Ssam	bus_space_handle_t h = ah->ah_sh;
370162413Ssam
371293111Sadrian#ifdef	AH_DEBUG
372263418Sadrian	/* Debug - complain if we haven't fully waken things up */
373264292Sadrian	if (! ath_hal_reg_whilst_asleep(ah, reg) &&
374264292Sadrian	    ah->ah_powerMode != HAL_PM_AWAKE) {
375263418Sadrian		ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
376263418Sadrian		    __func__, reg, val, ah->ah_powerMode);
377263418Sadrian	}
378293111Sadrian#endif
379263418Sadrian
380227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
381227410Sadrian		mtx_lock_spin(&ah_regser_mtx);
382234450Sadrian	bus_space_write_4(tag, h, reg, val);
383293050Sadrian	OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
384227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
385227410Sadrian		mtx_unlock_spin(&ah_regser_mtx);
386162413Ssam}
387162413Ssam
388162413Ssamu_int32_t
389162413Ssamath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
390162413Ssam{
391162413Ssam	bus_space_tag_t tag = BUSTAG(ah);
392185522Ssam	bus_space_handle_t h = ah->ah_sh;
393162413Ssam	u_int32_t val;
394162413Ssam
395293111Sadrian#ifdef	AH_DEBUG
396263418Sadrian	/* Debug - complain if we haven't fully waken things up */
397264292Sadrian	if (! ath_hal_reg_whilst_asleep(ah, reg) &&
398264292Sadrian	    ah->ah_powerMode != HAL_PM_AWAKE) {
399263418Sadrian		ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
400263418Sadrian		    __func__, reg, ah->ah_powerMode);
401263418Sadrian	}
402293111Sadrian#endif
403263418Sadrian
404227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
405227410Sadrian		mtx_lock_spin(&ah_regser_mtx);
406293050Sadrian	OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
407234450Sadrian	val = bus_space_read_4(tag, h, reg);
408227410Sadrian	if (ah->ah_config.ah_serialise_reg_war)
409227410Sadrian		mtx_unlock_spin(&ah_regser_mtx);
410162413Ssam	return val;
411162413Ssam}
412293111Sadrian#endif /* AH_DEBUG_ALQ */
413162413Ssam
414162413Ssam#ifdef AH_ASSERT
415162413Ssamvoid
416162413Ssamath_hal_assert_failed(const char* filename, int lineno, const char *msg)
417162413Ssam{
418162413Ssam	printf("Atheros HAL assertion failure: %s: line %u: %s\n",
419162413Ssam		filename, lineno, msg);
420162413Ssam	panic("ath_hal_assert");
421162413Ssam}
422162413Ssam#endif /* AH_ASSERT */
423