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