ath_netbsd.c revision 1.8
1/*	$NetBSD: ath_netbsd.c,v 1.8 2006/05/11 22:26:54 mrg Exp $ */
2
3/*-
4 * Copyright (c) 2003, 2004 David Young
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 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__KERNEL_RCSID(0, "$NetBSD: ath_netbsd.c,v 1.8 2006/05/11 22:26:54 mrg Exp $");
32
33#include <sys/param.h>
34#include <sys/types.h>
35#include <sys/errno.h>
36#include <sys/systm.h>
37#include <sys/sysctl.h>
38#include <sys/mbuf.h>
39#include <sys/malloc.h>
40#include <sys/lock.h>
41#include <sys/kernel.h>
42#include <sys/socket.h>
43#include <sys/sockio.h>
44#include <sys/sysctl.h>
45#include <sys/callout.h>
46#include <machine/bus.h>
47#include <machine/stdarg.h>
48#include <sys/endian.h>
49#include <sys/device.h>
50
51#include <net/if.h>
52#include <net/if_dl.h>
53#include <net/if_media.h>
54#include <net/if_arp.h>
55#include <net/if_ether.h>
56#include <net/if_llc.h>
57
58#include <net80211/ieee80211_netbsd.h>
59#include <net80211/ieee80211_var.h>
60#include <dev/ic/ath_netbsd.h>
61#include <dev/ic/athvar.h>
62
63void
64device_printf(struct device dv, const char *fmt, ...)
65{
66        va_list ap;
67
68        va_start(ap, fmt);
69        printf("%s: ", dv.dv_xname);
70        vprintf(fmt, ap);
71        va_end(ap);
72	return;
73}
74
75struct mbuf *
76m_defrag(struct mbuf *m0, int flags)
77{
78	struct mbuf *m;
79	MGETHDR(m, flags, MT_DATA);
80	if (m == NULL)
81		return NULL;
82
83	M_COPY_PKTHDR(m, m0);
84	MCLGET(m, flags);
85	if ((m->m_flags & M_EXT) == 0) {
86		m_free(m);
87		return NULL;
88	}
89	m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
90	m->m_len = m->m_pkthdr.len;
91	return m;
92}
93
94/*
95 * Setup sysctl(3) MIB, hw.ath.*.
96 *
97 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
98 */
99SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
100{
101	int rc;
102	const struct sysctlnode *cnode, *rnode;
103
104	if ((rnode = ath_sysctl_treetop(clog)) == NULL)
105		return;
106
107	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "dwell",
108	    "channel dwell time (ms) for AP/station scanning",
109	    dwelltime)) != 0)
110		goto err;
111
112	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "calibrate",
113	    "chip calibration interval (secs)", calinterval)) != 0)
114		goto err;
115
116	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "outdoor",
117	    "outdoor operation", outdoor)) != 0)
118		goto err;
119
120	/* country code */
121	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE,
122	    "countrycode", "country code", countrycode)) != 0)
123		goto err;
124
125	/* regulatory domain */
126	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "regdomain",
127	    "EEPROM regdomain code", regdomain)) != 0)
128		goto err;
129
130	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "debug",
131	    "control debugging printfs", debug)) != 0)
132		goto err;
133
134	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "rxbuf",
135	    "rx buffers allocated", rxbuf)) != 0)
136		goto err;
137	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "txbuf",
138	    "tx buffers allocated", txbuf)) != 0)
139		goto err;
140
141	return;
142err:
143	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
144}
145
146static int
147ath_sysctl_slottime(SYSCTLFN_ARGS)
148{
149	struct ath_softc *sc;
150	struct sysctlnode node;
151	u_int slottime;
152	int error;
153
154	node = *rnode;
155	sc = (struct ath_softc *)node.sysctl_data;
156	slottime = ath_hal_getslottime(sc->sc_ah);
157	node.sysctl_data = &slottime;
158	error = sysctl_lookup(SYSCTLFN_CALL(&node));
159	if (error || newp == NULL)
160		return error;
161	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
162}
163
164static int
165ath_sysctl_acktimeout(SYSCTLFN_ARGS)
166{
167	struct ath_softc *sc;
168	struct sysctlnode node;
169	u_int acktimeout;
170	int error;
171
172	node = *rnode;
173	sc = (struct ath_softc *)node.sysctl_data;
174	acktimeout = ath_hal_getacktimeout(sc->sc_ah);
175	node.sysctl_data = &acktimeout;
176	error = sysctl_lookup(SYSCTLFN_CALL(&node));
177	if (error || newp == NULL)
178		return error;
179	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
180}
181
182static int
183ath_sysctl_ctstimeout(SYSCTLFN_ARGS)
184{
185	struct ath_softc *sc;
186	struct sysctlnode node;
187	u_int ctstimeout;
188	int error;
189
190	node = *rnode;
191	sc = (struct ath_softc *)node.sysctl_data;
192	ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
193	node.sysctl_data = &ctstimeout;
194	error = sysctl_lookup(SYSCTLFN_CALL(&node));
195	if (error || newp == NULL)
196		return error;
197	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
198}
199
200static int
201ath_sysctl_softled(SYSCTLFN_ARGS)
202{
203	struct ath_softc *sc;
204	struct sysctlnode node;
205	int softled;
206	int error;
207
208	node = *rnode;
209	sc = (struct ath_softc *)node.sysctl_data;
210	softled = sc->sc_softled;
211	node.sysctl_data = &softled;
212	error = sysctl_lookup(SYSCTLFN_CALL(&node));
213	if (error || newp == NULL)
214		return error;
215	softled = (softled != 0);
216	if (softled != sc->sc_softled) {
217		if (softled) {
218			/* NB: handle any sc_ledpin change */
219			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
220			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
221				!sc->sc_ledon);
222		}
223		sc->sc_softled = softled;
224	}
225	return 0;
226}
227
228static int
229ath_sysctl_rxantenna(SYSCTLFN_ARGS)
230{
231	struct ath_softc *sc;
232	struct sysctlnode node;
233	u_int defantenna;
234	int error;
235
236	node = *rnode;
237	sc = (struct ath_softc *)node.sysctl_data;
238	defantenna = ath_hal_getdefantenna(sc->sc_ah);
239	node.sysctl_data = &defantenna;
240	error = sysctl_lookup(SYSCTLFN_CALL(&node));
241	if (error || newp == NULL)
242		return error;
243	ath_hal_setdefantenna(sc->sc_ah, defantenna);
244	return 0;
245}
246
247static int
248ath_sysctl_diversity(SYSCTLFN_ARGS)
249{
250	struct ath_softc *sc;
251	struct sysctlnode node;
252	u_int diversity;
253	int error;
254
255	node = *rnode;
256	sc = (struct ath_softc *)node.sysctl_data;
257	diversity = sc->sc_diversity;
258	node.sysctl_data = &diversity;
259	error = sysctl_lookup(SYSCTLFN_CALL(&node));
260	if (error || newp == NULL)
261		return error;
262	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
263		return EINVAL;
264	sc->sc_diversity = diversity;
265	return 0;
266}
267
268static int
269ath_sysctl_diag(SYSCTLFN_ARGS)
270{
271	struct ath_softc *sc;
272	struct sysctlnode node;
273	u_int32_t diag;
274	int error;
275
276	node = *rnode;
277	sc = (struct ath_softc *)node.sysctl_data;
278	if (!ath_hal_getdiag(sc->sc_ah, &diag))
279		return EINVAL;
280	node.sysctl_data = &diag;
281	error = sysctl_lookup(SYSCTLFN_CALL(&node));
282	if (error || newp == NULL)
283		return error;
284	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
285}
286
287static int
288ath_sysctl_tpscale(SYSCTLFN_ARGS)
289{
290	struct ath_softc *sc;
291	struct sysctlnode node;
292	u_int32_t scale;
293	int error;
294
295	node = *rnode;
296	sc = (struct ath_softc *)node.sysctl_data;
297	(void)ath_hal_gettpscale(sc->sc_ah, &scale);
298	node.sysctl_data = &scale;
299	error = sysctl_lookup(SYSCTLFN_CALL(&node));
300	if (error || newp == NULL)
301		return error;
302	return !ath_hal_settpscale(sc->sc_ah, scale)
303	    ? EINVAL
304	    : ath_reset(&sc->sc_if);
305}
306
307static int
308ath_sysctl_tpc(SYSCTLFN_ARGS)
309{
310	struct ath_softc *sc;
311	struct sysctlnode node;
312	u_int tpc;
313	int error;
314
315	node = *rnode;
316	sc = (struct ath_softc *)node.sysctl_data;
317	tpc = ath_hal_gettpc(sc->sc_ah);
318	node.sysctl_data = &tpc;
319	error = sysctl_lookup(SYSCTLFN_CALL(&node));
320	if (error || newp == NULL)
321		return error;
322	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
323}
324
325static int
326ath_sysctl_rfkill(SYSCTLFN_ARGS)
327{
328	struct ath_softc *sc;
329	struct sysctlnode node;
330	u_int rfkill;
331	int error;
332
333	node = *rnode;
334	sc = (struct ath_softc *)node.sysctl_data;
335	rfkill = ath_hal_getrfkill(sc->sc_ah);
336	node.sysctl_data = &rfkill;
337	error = sysctl_lookup(SYSCTLFN_CALL(&node));
338	if (error || newp == NULL)
339		return error;
340	return !ath_hal_setrfkill(sc->sc_ah, rfkill) ? EINVAL : 0;
341}
342
343static int
344ath_sysctl_rfsilent(SYSCTLFN_ARGS)
345{
346	struct ath_softc *sc;
347	struct sysctlnode node;
348	u_int rfsilent;
349	int error;
350
351	node = *rnode;
352	sc = (struct ath_softc *)node.sysctl_data;
353	(void)ath_hal_getrfsilent(sc->sc_ah, &rfsilent);
354	node.sysctl_data = &rfsilent;
355	error = sysctl_lookup(SYSCTLFN_CALL(&node));
356	if (error || newp == NULL)
357		return error;
358	return !ath_hal_setrfsilent(sc->sc_ah, rfsilent) ? EINVAL : 0;
359}
360
361static int
362ath_sysctl_regdomain(SYSCTLFN_ARGS)
363{
364	struct ath_softc *sc;
365	struct sysctlnode node;
366	u_int32_t rd;
367	int error;
368
369	node = *rnode;
370	sc = (struct ath_softc *)node.sysctl_data;
371	if (!ath_hal_getregdomain(sc->sc_ah, &rd))
372		return EINVAL;
373	node.sysctl_data = &rd;
374	error = sysctl_lookup(SYSCTLFN_CALL(&node));
375	if (error || newp == NULL)
376		return error;
377	return !ath_hal_setregdomain(sc->sc_ah, rd) ? EINVAL : 0;
378}
379
380static int
381ath_sysctl_tpack(SYSCTLFN_ARGS)
382{
383	struct ath_softc *sc;
384	struct sysctlnode node;
385	u_int32_t tpack;
386	int error;
387
388	node = *rnode;
389	sc = (struct ath_softc *)node.sysctl_data;
390	(void)ath_hal_gettpack(sc->sc_ah, &tpack);
391	node.sysctl_data = &tpack;
392	error = sysctl_lookup(SYSCTLFN_CALL(&node));
393	if (error || newp == NULL)
394		return error;
395	return !ath_hal_settpack(sc->sc_ah, tpack) ? EINVAL : 0;
396}
397
398static int
399ath_sysctl_tpcts(SYSCTLFN_ARGS)
400{
401	struct ath_softc *sc;
402	struct sysctlnode node;
403	u_int32_t tpcts;
404	int error;
405
406	node = *rnode;
407	sc = (struct ath_softc *)node.sysctl_data;
408	(void)ath_hal_gettpcts(sc->sc_ah, &tpcts);
409	node.sysctl_data = &tpcts;
410	error = sysctl_lookup(SYSCTLFN_CALL(&node));
411	if (error || newp == NULL)
412		return error;
413	return !ath_hal_settpcts(sc->sc_ah, tpcts) ? EINVAL : 0;
414}
415
416const struct sysctlnode *
417ath_sysctl_instance(const char *dvname, struct sysctllog **log)
418{
419	int rc;
420	const struct sysctlnode *rnode;
421
422	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
423	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
424	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
425		goto err;
426
427	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
428	    CTLFLAG_PERMANENT, CTLTYPE_NODE, dvname,
429	    SYSCTL_DESCR("ath information and options"),
430	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
431		goto err;
432
433	return rnode;
434err:
435	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
436	return NULL;
437}
438
439const struct sysctlnode *
440ath_sysctl_treetop(struct sysctllog **log)
441{
442	int rc;
443	const struct sysctlnode *rnode;
444
445	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
446	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
447	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
448		goto err;
449
450	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
451	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
452	    SYSCTL_DESCR("ath information and options"),
453	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
454		goto err;
455
456	return rnode;
457err:
458	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
459	return NULL;
460}
461
462void
463ath_sysctlattach(struct ath_softc *sc)
464{
465	int rc;
466	struct sysctllog **log = &sc->sc_sysctllog;
467	const struct sysctlnode *cnode, *rnode;
468
469	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
470	(void)ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
471	sc->sc_debug = ath_debug;
472	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
473
474	if ((rnode = ath_sysctl_instance(sc->sc_dev.dv_xname, log)) == NULL)
475		return;
476
477	if ((rc = SYSCTL_INT(0, countrycode, "EEPROM country code")) != 0)
478		goto err;
479
480	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, debug,
481	    "control debugging printfs")) != 0)
482		goto err;
483
484#if 0
485	/* channel dwell time (ms) for AP/station scanning */
486	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, dwell,
487	    "Channel dwell time (ms) for scanning")) != 0)
488		goto err;
489#endif
490
491	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, slottime,
492	    "802.11 slot time (us)")) != 0)
493		goto err;
494	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, acktimeout,
495	    "802.11 ACK timeout (us)")) != 0)
496		goto err;
497	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, ctstimeout,
498	    "802.11 CTS timeout (us)")) != 0)
499		goto err;
500	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, softled,
501	    "enable/disable software LED support")) != 0)
502		goto err;
503	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledpin,
504	    "GPIO pin connected to LED")) != 0)
505		goto err;
506	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledon,
507	    "setting to turn LED on")) != 0)
508		goto err;
509	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledidle,
510	    "idle time for inactivity LED (ticks)")) != 0)
511		goto err;
512	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txantenna,
513	    "tx antenna (0=auto)")) != 0)
514		goto err;
515	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rxantenna,
516	    "default/rx antenna")) != 0)
517		goto err;
518	if (ath_hal_hasdiversity(sc->sc_ah)) {
519		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diversity,
520		    "antenna diversity")) != 0)
521			goto err;
522	}
523	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txintrperiod,
524	    "tx descriptor batching")) != 0)
525		goto err;
526	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diag,
527	    "h/w diagnostic control")) != 0)
528		goto err;
529	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpscale,
530	    "tx power scaling")) != 0)
531		goto err;
532	if (ath_hal_hastpc(sc->sc_ah)) {
533		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpc,
534		    "enable/disable per-packet TPC")) != 0)
535			goto err;
536		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpack,
537		    "tx power for ack frames")) != 0)
538			goto err;
539		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpcts,
540		    "tx power for cts frames")) != 0)
541			goto err;
542	}
543	if (ath_hal_hasrfsilent(sc->sc_ah)) {
544		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rfsilent,
545		    "h/w RF silent config")) != 0)
546			goto err;
547		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rfkill,
548		    "enable/disable RF kill switch")) != 0)
549			goto err;
550	}
551	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, regdomain,
552	    "EEPROM regdomain code")) != 0)
553		goto err;
554	return;
555err:
556	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
557}
558