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