2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <machine/stdarg.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h> /* XXX for ether_sprintf */
55 #include <netproto/802_11/ieee80211_var.h>
57 #include <dev/netif/ath/ath_hal/ah.h>
58 #include <dev/netif/ath/ath_hal/ah_debug.h>
61 * WiSoC boards overload the bus tag with information about the
62 * board layout. We must extract the bus space tag from that
63 * indirect structure. For everyone else the tag is passed in
65 * XXX cache indirect ref privately
67 #ifdef AH_SUPPORT_AR5312
69 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
71 #define BUSTAG(ah) ((ah)->ah_st)
75 * This lock is used to seralise register access for chips which have
76 * problems w/ SMP CPUs issuing concurrent PCI transactions.
78 * XXX This is a global lock for now; it should be pushed to
79 * a per-device lock in some platform-independent fashion.
81 struct spinlock ah_regser_spin = SPINLOCK_INITIALIZER(ah_regser_spin);
83 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
85 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
87 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
88 extern void *ath_hal_malloc(size_t);
89 extern void ath_hal_free(void *);
91 extern void ath_hal_assert_failed(const char* filename,
92 int lineno, const char* msg);
95 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
99 /* NB: put this here instead of the driver to avoid circular references */
100 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
101 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
102 "Atheros HAL parameters");
105 int ath_hal_debug = 0;
106 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RW, &ath_hal_debug,
107 0, "Atheros HAL debugging printfs");
108 TUNABLE_INT("hw.ath.hal.debug", &ath_hal_debug);
109 #endif /* AH_DEBUG */
111 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
114 ath_hal_malloc(size_t size)
116 return kmalloc(size, M_ATH_HAL, M_INTWAIT | M_ZERO);
120 ath_hal_free(void* p)
126 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, __va_list ap)
132 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
136 ath_hal_vprintf(ah, fmt, ap);
141 ath_hal_ether_sprintf(const u_int8_t *mac)
143 static char etherbuf[ETHER_ADDRSTRLEN + 1];
145 kether_ntoa(mac, etherbuf);
153 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
155 if ((mask == HAL_DEBUG_UNMASKABLE) ||
156 (ah != NULL && ah->ah_config.ah_debug & mask) ||
157 (ath_hal_debug & mask)) {
160 ath_hal_vprintf(ah, fmt, ap);
164 #undef HAL_DEBUG_UNMASKABLE
165 #endif /* AH_DEBUG */
169 * ALQ register tracing support.
171 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
172 * writes to the file /tmp/ath_hal.log. The file format is a simple
173 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
174 * and then decode the file with the arcode program (that is part of the
175 * HAL). If you start+stop tracing the data will be appended to an
178 * NB: doesn't handle multiple devices properly; only one DEVICE record
179 * is emitted and the different devices are not identified.
182 #include <dev/netif/ath/ath_hal/ah_decode.h>
184 static struct alq *ath_hal_alq;
185 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
186 static u_int ath_hal_alq_lost; /* count of lost records */
187 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
189 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
190 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
192 static u_int ath_hal_alq_qsize = 64*1024;
195 ath_hal_setlogging(int enable)
200 error = alq_open(&ath_hal_alq, ath_hal_logfile,
201 curthread->td_ucred, ALQ_DEFAULT_CMODE,
202 sizeof (struct athregrec), ath_hal_alq_qsize);
203 ath_hal_alq_lost = 0;
204 ath_hal_alq_emitdev = 1;
205 kprintf("ath_hal: logging to %s enabled\n",
209 alq_close(ath_hal_alq);
211 kprintf("ath_hal: logging disabled\n");
218 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
222 enable = (ath_hal_alq != NULL);
223 error = sysctl_handle_int(oidp, &enable, 0, req);
224 if (error || !req->newptr)
227 return (ath_hal_setlogging(enable));
229 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
230 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
231 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
232 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
233 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
234 &ath_hal_alq_lost, 0, "Register operations not logged");
237 ath_hal_alq_get(struct ath_hal *ah)
241 if (ath_hal_alq_emitdev) {
242 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
244 struct athregrec *r =
245 (struct athregrec *) ale->ae_data;
248 r->val = ah->ah_devid;
249 alq_post(ath_hal_alq, ale);
250 ath_hal_alq_emitdev = 0;
254 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
261 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
263 bus_space_tag_t tag = BUSTAG(ah);
264 bus_space_handle_t h = ah->ah_sh;
267 struct ale *ale = ath_hal_alq_get(ah);
269 struct athregrec *r = (struct athregrec *) ale->ae_data;
270 r->threadid = curthread->td_tid;
274 alq_post(ath_hal_alq, ale);
277 if (ah->ah_config.ah_serialise_reg_war)
278 spin_lock(&ah_regser_spin);
279 bus_space_write_4(tag, h, reg, val);
280 if (ah->ah_config.ah_serialise_reg_war)
281 spin_unlock(&ah_regser_spin);
285 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
287 bus_space_tag_t tag = BUSTAG(ah);
288 bus_space_handle_t h = ah->ah_sh;
291 if (ah->ah_config.ah_serialise_reg_war)
292 spin_lock(&ah_regser_spin);
293 val = bus_space_read_4(tag, h, reg);
294 if (ah->ah_config.ah_serialise_reg_war)
295 spin_unlock(&ah_regser_spin);
297 struct ale *ale = ath_hal_alq_get(ah);
299 struct athregrec *r = (struct athregrec *) ale->ae_data;
300 r->threadid = curthread->td_tid;
304 alq_post(ath_hal_alq, ale);
311 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
314 struct ale *ale = ath_hal_alq_get(ah);
316 struct athregrec *r = (struct athregrec *) ale->ae_data;
317 r->threadid = curthread->td_tid;
321 alq_post(ath_hal_alq, ale);
325 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
327 * Memory-mapped device register read/write. These are here
328 * as routines when debugging support is enabled and/or when
329 * explicitly configured to use function calls. The latter is
330 * for architectures that might need to do something before
331 * referencing memory (e.g. remap an i/o window).
333 * NB: see the comments in ah_osdep.h about byte-swapping register
334 * reads and writes to understand what's going on below.
338 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
340 bus_space_tag_t tag = BUSTAG(ah);
341 bus_space_handle_t h = ah->ah_sh;
343 if (ah->ah_config.ah_serialise_reg_war)
344 spin_lock(&ah_regser_spin);
345 bus_space_write_4(tag, h, reg, val);
346 if (ah->ah_config.ah_serialise_reg_war)
347 spin_unlock(&ah_regser_spin);
351 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
353 bus_space_tag_t tag = BUSTAG(ah);
354 bus_space_handle_t h = ah->ah_sh;
357 if (ah->ah_config.ah_serialise_reg_war)
358 spin_lock(&ah_regser_spin);
359 val = bus_space_read_4(tag, h, reg);
360 if (ah->ah_config.ah_serialise_reg_war)
361 spin_unlock(&ah_regser_spin);
364 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
368 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
370 kprintf("Atheros HAL assertion failure: %s: line %u: %s\n",
371 filename, lineno, msg);
372 panic("ath_hal_assert");
374 #endif /* AH_ASSERT */
380 ath_hal_modevent(module_t mod, int type, void *unused)
384 wlan_serialize_enter();
397 wlan_serialize_exit();
402 static moduledata_t ath_hal_mod = {
408 DECLARE_MODULE(ath_hal, ath_hal_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
409 MODULE_VERSION(ath_hal, 1);