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