xref: /netbsd-src/sys/dev/ic/ath_netbsd.c (revision 89c0a98ceb0938e74396d28fc9371d2ebf43eb45)
1 /*-
2  * Copyright (c) 2003, 2004 David Young
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
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 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/errno.h>
30 #include <sys/systm.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/malloc.h>
34 #include <sys/lock.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/callout.h>
40 #include <machine/bus.h>
41 #include <machine/stdarg.h>
42 #include <sys/endian.h>
43 #include <sys/device.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_arp.h>
49 #include <net/if_ether.h>
50 #include <net/if_llc.h>
51 
52 #include <net80211/ieee80211_netbsd.h>
53 #include <net80211/ieee80211_var.h>
54 #include <dev/ic/ath_netbsd.h>
55 #include <dev/ic/athvar.h>
56 
57 void
58 device_printf(struct device dv, const char *fmt, ...)
59 {
60         va_list ap;
61 
62         va_start(ap, fmt);
63         printf("%s: ", dv.dv_xname);
64         vprintf(fmt, ap);
65         va_end(ap);
66 	return;
67 }
68 
69 struct mbuf *
70 m_defrag(struct mbuf *m0, int flags)
71 {
72 	struct mbuf *m;
73 	MGETHDR(m, flags, MT_DATA);
74 	if (m == NULL)
75 		return NULL;
76 
77 	M_COPY_PKTHDR(m, m0);
78 	MCLGET(m, flags);
79 	if ((m->m_flags & M_EXT) == 0) {
80 		m_free(m);
81 		return NULL;
82 	}
83 	m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
84 	m->m_len = m->m_pkthdr.len;
85 	return m;
86 }
87 
88 /*
89  * Setup sysctl(3) MIB, ath.*.
90  *
91  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
92  */
93 SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
94 {
95 	int rc;
96 	const struct sysctlnode *cnode, *rnode;
97 
98 	if ((rnode = ath_sysctl_treetop(clog)) == NULL)
99 		return;
100 
101 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "dwell",
102 	    "channel dwell time (ms) for AP/station scanning",
103 	    dwelltime)) != 0)
104 		goto err;
105 
106 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "calibrate",
107 	    "chip calibration interval (secs)", calinterval)) != 0)
108 		goto err;
109 
110 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "outdoor",
111 	    "outdoor operation", outdoor)) != 0)
112 		goto err;
113 
114 	/* country code */
115 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE,
116 	    "countrycode", "country code", countrycode)) != 0)
117 		goto err;
118 
119 	/* regulatory domain */
120 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "regdomain",
121 	    "regulatory domain", regdomain)) != 0)
122 		goto err;
123 
124 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "debug",
125 	    "control debugging printfs", debug)) != 0)
126 		goto err;
127 
128 	return;
129 err:
130 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
131 }
132 
133 static int
134 ath_sysctl_slottime(SYSCTLFN_ARGS)
135 {
136 	struct ath_softc *sc;
137 	struct sysctlnode node;
138 	u_int slottime;
139 	int error;
140 
141 	node = *rnode;
142 	sc = (struct ath_softc *)node.sysctl_data;
143 	slottime = ath_hal_getslottime(sc->sc_ah);
144 	node.sysctl_data = &slottime;
145 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
146 	if (error || newp == NULL)
147 		return error;
148 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
149 }
150 
151 static int
152 ath_sysctl_acktimeout(SYSCTLFN_ARGS)
153 {
154 	struct ath_softc *sc;
155 	struct sysctlnode node;
156 	u_int acktimeout;
157 	int error;
158 
159 	node = *rnode;
160 	sc = (struct ath_softc *)node.sysctl_data;
161 	acktimeout = ath_hal_getacktimeout(sc->sc_ah);
162 	node.sysctl_data = &acktimeout;
163 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
164 	if (error || newp == NULL)
165 		return error;
166 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
167 }
168 
169 static int
170 ath_sysctl_ctstimeout(SYSCTLFN_ARGS)
171 {
172 	struct ath_softc *sc;
173 	struct sysctlnode node;
174 	u_int ctstimeout;
175 	int error;
176 
177 	node = *rnode;
178 	sc = (struct ath_softc *)node.sysctl_data;
179 	ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
180 	node.sysctl_data = &ctstimeout;
181 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
182 	if (error || newp == NULL)
183 		return error;
184 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
185 }
186 
187 static int
188 ath_sysctl_softled(SYSCTLFN_ARGS)
189 {
190 	struct ath_softc *sc;
191 	struct sysctlnode node;
192 	int softled;
193 	int error;
194 
195 	node = *rnode;
196 	sc = (struct ath_softc *)node.sysctl_data;
197 	softled = sc->sc_softled;
198 	node.sysctl_data = &softled;
199 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
200 	if (error || newp == NULL)
201 		return error;
202 	softled = (softled != 0);
203 	if (softled != sc->sc_softled) {
204 		if (softled) {
205 			/* NB: handle any sc_ledpin change */
206 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
207 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
208 				!sc->sc_ledon);
209 		}
210 		sc->sc_softled = softled;
211 	}
212 	return 0;
213 }
214 
215 static int
216 ath_sysctl_rxantenna(SYSCTLFN_ARGS)
217 {
218 	struct ath_softc *sc;
219 	struct sysctlnode node;
220 	u_int defantenna;
221 	int error;
222 
223 	node = *rnode;
224 	sc = (struct ath_softc *)node.sysctl_data;
225 	defantenna = ath_hal_getdefantenna(sc->sc_ah);
226 	node.sysctl_data = &defantenna;
227 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
228 	if (error || newp == NULL)
229 		return error;
230 	ath_hal_setdefantenna(sc->sc_ah, defantenna);
231 	return 0;
232 }
233 
234 static int
235 ath_sysctl_diversity(SYSCTLFN_ARGS)
236 {
237 	struct ath_softc *sc;
238 	struct sysctlnode node;
239 	u_int diversity;
240 	int error;
241 
242 	node = *rnode;
243 	sc = (struct ath_softc *)node.sysctl_data;
244 	diversity = sc->sc_diversity;
245 	node.sysctl_data = &diversity;
246 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
247 	if (error || newp == NULL)
248 		return error;
249 	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
250 		return EINVAL;
251 	sc->sc_diversity = diversity;
252 	return 0;
253 }
254 
255 static int
256 ath_sysctl_diag(SYSCTLFN_ARGS)
257 {
258 	struct ath_softc *sc;
259 	struct sysctlnode node;
260 	u_int32_t diag;
261 	int error;
262 
263 	node = *rnode;
264 	sc = (struct ath_softc *)node.sysctl_data;
265 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
266 		return EINVAL;
267 	node.sysctl_data = &diag;
268 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
269 	if (error || newp == NULL)
270 		return error;
271 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
272 }
273 
274 static int
275 ath_sysctl_tpscale(SYSCTLFN_ARGS)
276 {
277 	struct ath_softc *sc;
278 	struct sysctlnode node;
279 	u_int32_t scale;
280 	int error;
281 
282 	node = *rnode;
283 	sc = (struct ath_softc *)node.sysctl_data;
284 	ath_hal_gettpscale(sc->sc_ah, &scale);
285 	node.sysctl_data = &scale;
286 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
287 	if (error || newp == NULL)
288 		return error;
289 	return !ath_hal_settpscale(sc->sc_ah, scale)
290 	    ? EINVAL
291 	    : ath_reset(&sc->sc_if);
292 }
293 
294 static int
295 ath_sysctl_tpc(SYSCTLFN_ARGS)
296 {
297 	struct ath_softc *sc;
298 	struct sysctlnode node;
299 	u_int tpc;
300 	int error;
301 
302 	node = *rnode;
303 	sc = (struct ath_softc *)node.sysctl_data;
304 	tpc = ath_hal_gettpc(sc->sc_ah);
305 	node.sysctl_data = &tpc;
306 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
307 	if (error || newp == NULL)
308 		return error;
309 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
310 }
311 
312 const struct sysctlnode *
313 ath_sysctl_instance(const char *dvname, struct sysctllog **log)
314 {
315 	int rc;
316 	const struct sysctlnode *rnode;
317 
318 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
319 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
320 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
321 		goto err;
322 
323 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
324 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, dvname,
325 	    SYSCTL_DESCR("ath information and options"),
326 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
327 		goto err;
328 
329 	return rnode;
330 err:
331 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
332 	return NULL;
333 }
334 
335 const struct sysctlnode *
336 ath_sysctl_treetop(struct sysctllog **log)
337 {
338 	int rc;
339 	const struct sysctlnode *rnode;
340 
341 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
342 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
343 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
344 		goto err;
345 
346 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
347 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
348 	    SYSCTL_DESCR("ath information and options"),
349 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
350 		goto err;
351 
352 	return rnode;
353 err:
354 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
355 	return NULL;
356 }
357 
358 void
359 ath_sysctlattach(struct ath_softc *sc)
360 {
361 	int rc;
362 	struct sysctllog **log = &sc->sc_sysctllog;
363 	const struct sysctlnode *cnode, *rnode;
364 
365 	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
366 	ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
367 	sc->sc_debug = ath_debug;
368 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
369 
370 	if ((rnode = ath_sysctl_instance(sc->sc_dev.dv_xname, log)) == NULL)
371 		return;
372 
373 	if ((rc = SYSCTL_INT(0, countrycode, "EEPROM country code")) != 0)
374 		goto err;
375 
376 	if ((rc = SYSCTL_INT(0, regdomain, "EEPROM regdomain code")) != 0)
377 		goto err;
378 
379 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, debug,
380 	    "control debugging printfs")) != 0)
381 		goto err;
382 
383 #if 0
384 	/* channel dwell time (ms) for AP/station scanning */
385 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, dwell,
386 	    "Channel dwell time (ms) for scanning")) != 0)
387 		goto err;
388 #endif
389 
390 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, slottime,
391 	    "802.11 slot time (us)")) != 0)
392 		goto err;
393 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, acktimeout,
394 	    "802.11 ACK timeout (us)")) != 0)
395 		goto err;
396 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, ctstimeout,
397 	    "802.11 CTS timeout (us)")) != 0)
398 		goto err;
399 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, softled,
400 	    "enable/disable software LED support")) != 0)
401 		goto err;
402 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledpin,
403 	    "GPIO pin connected to LED")) != 0)
404 		goto err;
405 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledon,
406 	    "setting to turn LED on")) != 0)
407 		goto err;
408 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledidle,
409 	    "idle time for inactivity LED (ticks)")) != 0)
410 		goto err;
411 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txantenna,
412 	    "tx antenna (0=auto)")) != 0)
413 		goto err;
414 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rxantenna,
415 	    "default/rx antenna")) != 0)
416 		goto err;
417 	if (sc->sc_hasdiversity) {
418 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diversity,
419 		    "antenna diversity")) != 0)
420 			goto err;
421 	}
422 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txintrperiod,
423 	    "tx descriptor batching")) != 0)
424 		goto err;
425 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diag,
426 	    "h/w diagnostic control")) != 0)
427 		goto err;
428 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpscale,
429 	    "tx power scaling")) != 0)
430 		goto err;
431 	if (sc->sc_hastpc) {
432 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpc,
433 		    "enable/disable per-packet TPC")) != 0)
434 			goto err;
435 	}
436 	return;
437 err:
438 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
439 }
440