1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $FreeBSD$
32 */
33 #include "opt_ah.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/pcpu.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/conf.h>
47
48 #include <machine/stdarg.h>
49
50 #include <net/ethernet.h> /* XXX for ether_sprintf */
51
52 #include <dev/ath/ath_hal/ah.h>
53 #include <dev/ath/ath_hal/ah_debug.h>
54
55 /*
56 * WiSoC boards overload the bus tag with information about the
57 * board layout. We must extract the bus space tag from that
58 * indirect structure. For everyone else the tag is passed in
59 * directly.
60 * XXX cache indirect ref privately
61 */
62 #ifdef AH_SUPPORT_AR5312
63 #define BUSTAG(ah) \
64 ((bus_space_tag_t) ((struct ar531x_config *)((ah)->ah_st))->tag)
65 #else
66 #define BUSTAG(ah) ((ah)->ah_st)
67 #endif
68
69 /*
70 * This lock is used to seralise register access for chips which have
71 * problems w/ SMP CPUs issuing concurrent PCI transactions.
72 *
73 * XXX This is a global lock for now; it should be pushed to
74 * a per-device lock in some platform-independent fashion.
75 */
76 struct mtx ah_regser_mtx;
77 MTX_SYSINIT(ah_regser, &ah_regser_mtx, "Atheros register access mutex",
78 MTX_SPIN);
79
80 extern void ath_hal_printf(struct ath_hal *, const char*, ...)
81 __printflike(2,3);
82 extern void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
83 __printflike(2, 0);
84 extern const char* ath_hal_ether_sprintf(const u_int8_t *mac);
85 extern void *ath_hal_malloc(size_t);
86 extern void ath_hal_free(void *);
87 #ifdef AH_ASSERT
88 extern void ath_hal_assert_failed(const char* filename,
89 int lineno, const char* msg);
90 #endif
91 #ifdef AH_DEBUG
92 extern void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...);
93 #endif /* AH_DEBUG */
94
95 /* NB: put this here instead of the driver to avoid circular references */
96 SYSCTL_NODE(_hw, OID_AUTO, ath, CTLFLAG_RD, 0, "Atheros driver parameters");
97 static SYSCTL_NODE(_hw_ath, OID_AUTO, hal, CTLFLAG_RD, 0,
98 "Atheros HAL parameters");
99
100 #ifdef AH_DEBUG
101 int ath_hal_debug = 0;
102 SYSCTL_INT(_hw_ath_hal, OID_AUTO, debug, CTLFLAG_RWTUN, &ath_hal_debug,
103 0, "Atheros HAL debugging printfs");
104 #endif /* AH_DEBUG */
105
106 static MALLOC_DEFINE(M_ATH_HAL, "ath_hal", "ath hal data");
107
108 void*
ath_hal_malloc(size_t size)109 ath_hal_malloc(size_t size)
110 {
111 return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO);
112 }
113
114 void
ath_hal_free(void * p)115 ath_hal_free(void* p)
116 {
117 free(p, M_ATH_HAL);
118 }
119
120 void
ath_hal_vprintf(struct ath_hal * ah,const char * fmt,va_list ap)121 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
122 {
123 vprintf(fmt, ap);
124 }
125
126 void
ath_hal_printf(struct ath_hal * ah,const char * fmt,...)127 ath_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
135 const char*
ath_hal_ether_sprintf(const u_int8_t * mac)136 ath_hal_ether_sprintf(const u_int8_t *mac)
137 {
138 return ether_sprintf(mac);
139 }
140
141 #ifdef AH_DEBUG
142
143 /*
144 * XXX This is highly relevant only for the AR5416 and later
145 * PCI/PCIe NICs. It'll need adjustment for other hardware
146 * variations.
147 */
148 static int
ath_hal_reg_whilst_asleep(struct ath_hal * ah,uint32_t reg)149 ath_hal_reg_whilst_asleep(struct ath_hal *ah, uint32_t reg)
150 {
151
152 if (reg >= 0x4000 && reg < 0x5000)
153 return (1);
154 if (reg >= 0x6000 && reg < 0x7000)
155 return (1);
156 if (reg >= 0x7000 && reg < 0x8000)
157 return (1);
158 return (0);
159 }
160
161 void
DO_HALDEBUG(struct ath_hal * ah,u_int mask,const char * fmt,...)162 DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
163 {
164 if ((mask == HAL_DEBUG_UNMASKABLE) ||
165 (ah != NULL && ah->ah_config.ah_debug & mask) ||
166 (ath_hal_debug & mask)) {
167 __va_list ap;
168 va_start(ap, fmt);
169 ath_hal_vprintf(ah, fmt, ap);
170 va_end(ap);
171 }
172 }
173 #undef HAL_DEBUG_UNMASKABLE
174 #endif /* AH_DEBUG */
175
176 #ifdef AH_DEBUG_ALQ
177 /*
178 * ALQ register tracing support.
179 *
180 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
181 * writes to the file /tmp/ath_hal.log. The file format is a simple
182 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
183 * and then decode the file with the arcode program (that is part of the
184 * HAL). If you start+stop tracing the data will be appended to an
185 * existing file.
186 *
187 * NB: doesn't handle multiple devices properly; only one DEVICE record
188 * is emitted and the different devices are not identified.
189 */
190 #include <sys/alq.h>
191 #include <sys/pcpu.h>
192 #include <dev/ath/ath_hal/ah_decode.h>
193
194 static struct alq *ath_hal_alq;
195 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
196 static u_int ath_hal_alq_lost; /* count of lost records */
197 static char ath_hal_logfile[MAXPATHLEN] = "/tmp/ath_hal.log";
198
199 SYSCTL_STRING(_hw_ath_hal, OID_AUTO, alq_logfile, CTLFLAG_RW,
200 &ath_hal_logfile, sizeof(kernelname), "Name of ALQ logfile");
201
202 static u_int ath_hal_alq_qsize = 64*1024;
203
204 static int
ath_hal_setlogging(int enable)205 ath_hal_setlogging(int enable)
206 {
207 int error;
208
209 if (enable) {
210 error = alq_open(&ath_hal_alq, ath_hal_logfile,
211 curthread->td_ucred, ALQ_DEFAULT_CMODE,
212 sizeof (struct athregrec), ath_hal_alq_qsize);
213 ath_hal_alq_lost = 0;
214 ath_hal_alq_emitdev = 1;
215 printf("ath_hal: logging to %s enabled\n",
216 ath_hal_logfile);
217 } else {
218 if (ath_hal_alq)
219 alq_close(ath_hal_alq);
220 ath_hal_alq = NULL;
221 printf("ath_hal: logging disabled\n");
222 error = 0;
223 }
224 return (error);
225 }
226
227 static int
sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)228 sysctl_hw_ath_hal_log(SYSCTL_HANDLER_ARGS)
229 {
230 int error, enable;
231
232 enable = (ath_hal_alq != NULL);
233 error = sysctl_handle_int(oidp, &enable, 0, req);
234 if (error || !req->newptr)
235 return (error);
236 else
237 return (ath_hal_setlogging(enable));
238 }
239 SYSCTL_PROC(_hw_ath_hal, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
240 0, 0, sysctl_hw_ath_hal_log, "I", "Enable HAL register logging");
241 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_size, CTLFLAG_RW,
242 &ath_hal_alq_qsize, 0, "In-memory log size (#records)");
243 SYSCTL_INT(_hw_ath_hal, OID_AUTO, alq_lost, CTLFLAG_RW,
244 &ath_hal_alq_lost, 0, "Register operations not logged");
245
246 static struct ale *
ath_hal_alq_get(struct ath_hal * ah)247 ath_hal_alq_get(struct ath_hal *ah)
248 {
249 struct ale *ale;
250
251 if (ath_hal_alq_emitdev) {
252 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
253 if (ale) {
254 struct athregrec *r =
255 (struct athregrec *) ale->ae_data;
256 r->op = OP_DEVICE;
257 r->reg = 0;
258 r->val = ah->ah_devid;
259 alq_post(ath_hal_alq, ale);
260 ath_hal_alq_emitdev = 0;
261 } else
262 ath_hal_alq_lost++;
263 }
264 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
265 if (!ale)
266 ath_hal_alq_lost++;
267 return ale;
268 }
269
270 void
ath_hal_reg_write(struct ath_hal * ah,u_int32_t reg,u_int32_t val)271 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
272 {
273 bus_space_tag_t tag = BUSTAG(ah);
274 bus_space_handle_t h = ah->ah_sh;
275
276 #ifdef AH_DEBUG
277 /* Debug - complain if we haven't fully waken things up */
278 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
279 ah->ah_powerMode != HAL_PM_AWAKE) {
280 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
281 __func__, reg, val, ah->ah_powerMode);
282 }
283 #endif
284
285 if (ath_hal_alq) {
286 struct ale *ale = ath_hal_alq_get(ah);
287 if (ale) {
288 struct athregrec *r = (struct athregrec *) ale->ae_data;
289 r->threadid = curthread->td_tid;
290 r->op = OP_WRITE;
291 r->reg = reg;
292 r->val = val;
293 alq_post(ath_hal_alq, ale);
294 }
295 }
296 if (ah->ah_config.ah_serialise_reg_war)
297 mtx_lock_spin(&ah_regser_mtx);
298 bus_space_write_4(tag, h, reg, val);
299 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
300 if (ah->ah_config.ah_serialise_reg_war)
301 mtx_unlock_spin(&ah_regser_mtx);
302 }
303
304 u_int32_t
ath_hal_reg_read(struct ath_hal * ah,u_int32_t reg)305 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
306 {
307 bus_space_tag_t tag = BUSTAG(ah);
308 bus_space_handle_t h = ah->ah_sh;
309 u_int32_t val;
310
311 #ifdef AH_DEBUG
312 /* Debug - complain if we haven't fully waken things up */
313 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
314 ah->ah_powerMode != HAL_PM_AWAKE) {
315 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
316 __func__, reg, ah->ah_powerMode);
317 }
318 #endif
319
320 if (ah->ah_config.ah_serialise_reg_war)
321 mtx_lock_spin(&ah_regser_mtx);
322 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
323 val = bus_space_read_4(tag, h, reg);
324 if (ah->ah_config.ah_serialise_reg_war)
325 mtx_unlock_spin(&ah_regser_mtx);
326 if (ath_hal_alq) {
327 struct ale *ale = ath_hal_alq_get(ah);
328 if (ale) {
329 struct athregrec *r = (struct athregrec *) ale->ae_data;
330 r->threadid = curthread->td_tid;
331 r->op = OP_READ;
332 r->reg = reg;
333 r->val = val;
334 alq_post(ath_hal_alq, ale);
335 }
336 }
337 return val;
338 }
339
340 void
OS_MARK(struct ath_hal * ah,u_int id,u_int32_t v)341 OS_MARK(struct ath_hal *ah, u_int id, u_int32_t v)
342 {
343 if (ath_hal_alq) {
344 struct ale *ale = ath_hal_alq_get(ah);
345 if (ale) {
346 struct athregrec *r = (struct athregrec *) ale->ae_data;
347 r->threadid = curthread->td_tid;
348 r->op = OP_MARK;
349 r->reg = id;
350 r->val = v;
351 alq_post(ath_hal_alq, ale);
352 }
353 }
354 }
355 #else /* AH_DEBUG_ALQ */
356
357 /*
358 * Memory-mapped device register read/write. These are here
359 * as routines when debugging support is enabled and/or when
360 * explicitly configured to use function calls. The latter is
361 * for architectures that might need to do something before
362 * referencing memory (e.g. remap an i/o window).
363 *
364 * NB: see the comments in ah_osdep.h about byte-swapping register
365 * reads and writes to understand what's going on below.
366 */
367
368 void
ath_hal_reg_write(struct ath_hal * ah,u_int32_t reg,u_int32_t val)369 ath_hal_reg_write(struct ath_hal *ah, u_int32_t reg, u_int32_t val)
370 {
371 bus_space_tag_t tag = BUSTAG(ah);
372 bus_space_handle_t h = ah->ah_sh;
373
374 #ifdef AH_DEBUG
375 /* Debug - complain if we haven't fully waken things up */
376 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
377 ah->ah_powerMode != HAL_PM_AWAKE) {
378 ath_hal_printf(ah, "%s: reg=0x%08x, val=0x%08x, pm=%d\n",
379 __func__, reg, val, ah->ah_powerMode);
380 }
381 #endif
382
383 if (ah->ah_config.ah_serialise_reg_war)
384 mtx_lock_spin(&ah_regser_mtx);
385 bus_space_write_4(tag, h, reg, val);
386 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_WRITE);
387 if (ah->ah_config.ah_serialise_reg_war)
388 mtx_unlock_spin(&ah_regser_mtx);
389 }
390
391 u_int32_t
ath_hal_reg_read(struct ath_hal * ah,u_int32_t reg)392 ath_hal_reg_read(struct ath_hal *ah, u_int32_t reg)
393 {
394 bus_space_tag_t tag = BUSTAG(ah);
395 bus_space_handle_t h = ah->ah_sh;
396 u_int32_t val;
397
398 #ifdef AH_DEBUG
399 /* Debug - complain if we haven't fully waken things up */
400 if (! ath_hal_reg_whilst_asleep(ah, reg) &&
401 ah->ah_powerMode != HAL_PM_AWAKE) {
402 ath_hal_printf(ah, "%s: reg=0x%08x, pm=%d\n",
403 __func__, reg, ah->ah_powerMode);
404 }
405 #endif
406
407 if (ah->ah_config.ah_serialise_reg_war)
408 mtx_lock_spin(&ah_regser_mtx);
409 OS_BUS_BARRIER_REG(ah, reg, OS_BUS_BARRIER_READ);
410 val = bus_space_read_4(tag, h, reg);
411 if (ah->ah_config.ah_serialise_reg_war)
412 mtx_unlock_spin(&ah_regser_mtx);
413 return val;
414 }
415 #endif /* AH_DEBUG_ALQ */
416
417 #ifdef AH_ASSERT
418 void
ath_hal_assert_failed(const char * filename,int lineno,const char * msg)419 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
420 {
421 printf("Atheros HAL assertion failure: %s: line %u: %s\n",
422 filename, lineno, msg);
423 panic("ath_hal_assert");
424 }
425 #endif /* AH_ASSERT */
426
427 static int
ath_hal_modevent(module_t mod __unused,int type,void * data __unused)428 ath_hal_modevent(module_t mod __unused, int type, void *data __unused)
429 {
430 int error = 0;
431
432 switch (type) {
433 case MOD_LOAD:
434 printf("[ath_hal] loaded\n");
435 break;
436
437 case MOD_UNLOAD:
438 printf("[ath_hal] unloaded\n");
439 break;
440
441 case MOD_SHUTDOWN:
442 break;
443
444 default:
445 error = EOPNOTSUPP;
446 break;
447
448 }
449 return (error);
450 }
451
452 DEV_MODULE(ath_hal, ath_hal_modevent, NULL);
453 MODULE_VERSION(ath_hal, 1);
454 #if defined(AH_DEBUG_ALQ)
455 MODULE_DEPEND(ath_hal, alq, 1, 1, 1);
456 #endif
457