xref: /freebsd/sys/dev/ath/if_ath_ioctl.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
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  *    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.
15  *
16  * NO WARRANTY
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.
28  */
29 
30 #include <sys/cdefs.h>
31 /*
32  * Driver for the Atheros Wireless LAN controller.
33  *
34  * This software is derived from work of Atsushi Onoe; his contribution
35  * is greatly appreciated.
36  */
37 
38 #include "opt_inet.h"
39 #include "opt_ath.h"
40 /*
41  * This is needed for register operations which are performed
42  * by the driver - eg, calls to ath_hal_gettsf32().
43  *
44  * It's also required for any AH_DEBUG checks in here, eg the
45  * module dependencies.
46  */
47 #include "opt_ah.h"
48 #include "opt_wlan.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/mbuf.h>
54 #include <sys/malloc.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/kernel.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/errno.h>
61 #include <sys/callout.h>
62 #include <sys/bus.h>
63 #include <sys/endian.h>
64 #include <sys/kthread.h>
65 #include <sys/taskqueue.h>
66 #include <sys/priv.h>
67 #include <sys/module.h>
68 #include <sys/ktr.h>
69 #include <sys/smp.h>	/* for mp_ncpus */
70 
71 #include <machine/bus.h>
72 
73 #include <net/if.h>
74 #include <net/if_var.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_llc.h>
81 
82 #include <net80211/ieee80211_var.h>
83 #include <net80211/ieee80211_regdomain.h>
84 #ifdef IEEE80211_SUPPORT_SUPERG
85 #include <net80211/ieee80211_superg.h>
86 #endif
87 #ifdef IEEE80211_SUPPORT_TDMA
88 #include <net80211/ieee80211_tdma.h>
89 #endif
90 
91 #include <net/bpf.h>
92 
93 #ifdef INET
94 #include <netinet/in.h>
95 #include <netinet/if_ether.h>
96 #endif
97 
98 #include <dev/ath/if_athvar.h>
99 #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
100 #include <dev/ath/ath_hal/ah_diagcodes.h>
101 
102 #include <dev/ath/if_ath_debug.h>
103 #include <dev/ath/if_ath_misc.h>
104 #include <dev/ath/if_ath_btcoex.h>
105 #include <dev/ath/if_ath_spectral.h>
106 #include <dev/ath/if_ath_lna_div.h>
107 #include <dev/ath/if_athdfs.h>
108 
109 #ifdef	IEEE80211_SUPPORT_TDMA
110 #include <dev/ath/if_ath_tdma.h>
111 #endif
112 
113 #include <dev/ath/if_ath_ioctl.h>
114 
115 /*
116  * ioctl() related pieces.
117  *
118  * Some subsystems (eg spectral, dfs) have their own ioctl method which
119  * we call.
120  */
121 
122 /*
123  * Fetch the rate control statistics for the given node.
124  */
125 static int
126 ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
127 {
128 	struct ath_node *an;
129 	struct ieee80211com *ic = &sc->sc_ic;
130 	struct ieee80211_node *ni;
131 	int error = 0;
132 
133 	/* Perform a lookup on the given node */
134 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
135 	if (ni == NULL) {
136 		error = EINVAL;
137 		goto bad;
138 	}
139 
140 	/* Lock the ath_node */
141 	an = ATH_NODE(ni);
142 	ATH_NODE_LOCK(an);
143 
144 	/* Fetch the rate control stats for this node */
145 	error = ath_rate_fetch_node_stats(sc, an, rs);
146 
147 	/* No matter what happens here, just drop through */
148 
149 	/* Unlock the ath_node */
150 	ATH_NODE_UNLOCK(an);
151 
152 	/* Unref the node */
153 	ieee80211_node_decref(ni);
154 
155 bad:
156 	return (error);
157 }
158 
159 #ifdef ATH_DIAGAPI
160 /*
161  * Diagnostic interface to the HAL.  This is used by various
162  * tools to do things like retrieve register contents for
163  * debugging.  The mechanism is intentionally opaque so that
164  * it can change frequently w/o concern for compatibility.
165  */
166 static int
167 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
168 {
169 	struct ath_hal *ah = sc->sc_ah;
170 	u_int id = ad->ad_id & ATH_DIAG_ID;
171 	void *indata = NULL;
172 	void *outdata = NULL;
173 	u_int32_t insize = ad->ad_in_size;
174 	u_int32_t outsize = ad->ad_out_size;
175 	int error = 0;
176 
177 	if (ad->ad_id & ATH_DIAG_IN) {
178 		/*
179 		 * Copy in data.
180 		 */
181 		indata = malloc(insize, M_TEMP, M_NOWAIT);
182 		if (indata == NULL) {
183 			error = ENOMEM;
184 			goto bad;
185 		}
186 		error = copyin(ad->ad_in_data, indata, insize);
187 		if (error)
188 			goto bad;
189 	}
190 	if (ad->ad_id & ATH_DIAG_DYN) {
191 		/*
192 		 * Allocate a buffer for the results (otherwise the HAL
193 		 * returns a pointer to a buffer where we can read the
194 		 * results).  Note that we depend on the HAL leaving this
195 		 * pointer for us to use below in reclaiming the buffer;
196 		 * may want to be more defensive.
197 		 */
198 		outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
199 		if (outdata == NULL) {
200 			error = ENOMEM;
201 			goto bad;
202 		}
203 	}
204 
205 	ATH_LOCK(sc);
206 	if (id != HAL_DIAG_REGS)
207 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
208 	ATH_UNLOCK(sc);
209 
210 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
211 		if (outsize < ad->ad_out_size)
212 			ad->ad_out_size = outsize;
213 		if (outdata != NULL)
214 			error = copyout(outdata, ad->ad_out_data,
215 					ad->ad_out_size);
216 	} else {
217 		error = EINVAL;
218 	}
219 
220 	ATH_LOCK(sc);
221 	if (id != HAL_DIAG_REGS)
222 		ath_power_restore_power_state(sc);
223 	ATH_UNLOCK(sc);
224 
225 bad:
226 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
227 		free(indata, M_TEMP);
228 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
229 		free(outdata, M_TEMP);
230 	return error;
231 }
232 #endif /* ATH_DIAGAPI */
233 
234 int
235 ath_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
236 {
237 	struct ifreq *ifr = data;
238 	struct ath_softc *sc = ic->ic_softc;
239 
240 	switch (cmd) {
241 	case SIOCGATHSTATS: {
242 		struct ieee80211vap *vap;
243 		if_t ifp;
244 		const HAL_RATE_TABLE *rt;
245 
246 		/* NB: embed these numbers to get a consistent view */
247 		sc->sc_stats.ast_tx_packets = 0;
248 		sc->sc_stats.ast_rx_packets = 0;
249 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
250 			ifp = vap->iv_ifp;
251 			sc->sc_stats.ast_tx_packets += if_getcounter(ifp,
252 			    IFCOUNTER_OPACKETS);
253 			sc->sc_stats.ast_rx_packets += if_getcounter(ifp,
254 			    IFCOUNTER_IPACKETS);
255 		}
256 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
257 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
258 #ifdef IEEE80211_SUPPORT_TDMA
259 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
260 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
261 #endif
262 		rt = sc->sc_currates;
263 		sc->sc_stats.ast_tx_rate =
264 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
265 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
266 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
267 		return copyout(&sc->sc_stats, ifr_data_get_ptr(ifr),
268 		    sizeof (sc->sc_stats));
269 	}
270 	case SIOCGATHAGSTATS:
271 		return copyout(&sc->sc_aggr_stats, ifr_data_get_ptr(ifr),
272 		    sizeof (sc->sc_aggr_stats));
273 	case SIOCZATHSTATS: {
274 		int error;
275 
276 		error = priv_check(curthread, PRIV_DRIVER);
277 		if (error == 0) {
278 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
279 			memset(&sc->sc_aggr_stats, 0,
280 			    sizeof(sc->sc_aggr_stats));
281 			memset(&sc->sc_intr_stats, 0,
282 			    sizeof(sc->sc_intr_stats));
283 		}
284 		return (error);
285 	}
286 #ifdef ATH_DIAGAPI
287 	case SIOCGATHDIAG:
288 		return (ath_ioctl_diag(sc, data));
289 	case SIOCGATHPHYERR:
290 		return (ath_ioctl_phyerr(sc, data));
291 #endif
292 	case SIOCGATHSPECTRAL:
293 		return (ath_ioctl_spectral(sc, data));
294 	case SIOCGATHNODERATESTATS:
295 		return (ath_ioctl_ratestats(sc, data));
296 	case SIOCGATHBTCOEX:
297 		return (ath_btcoex_ioctl(sc, data));
298 	default:
299 		/*
300 		 * This signals the net80211 layer that we didn't handle this
301 		 * ioctl.
302 		 */
303 		return (ENOTTY);
304 	}
305 }
306