xref: /freebsd/sys/net80211/ieee80211.c (revision d78a9076652a3ad7c93e674284b08911f9c42582)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
321a1e1d21SSam Leffler /*
331a1e1d21SSam Leffler  * IEEE 802.11 generic handler
341a1e1d21SSam Leffler  */
35b032f27cSSam Leffler #include "opt_wlan.h"
361a1e1d21SSam Leffler 
371a1e1d21SSam Leffler #include <sys/param.h>
381a1e1d21SSam Leffler #include <sys/systm.h>
391a1e1d21SSam Leffler #include <sys/kernel.h>
408ec07310SGleb Smirnoff #include <sys/malloc.h>
418a1b9b6aSSam Leffler #include <sys/socket.h>
427a79cebfSGleb Smirnoff #include <sys/sbuf.h>
431a1e1d21SSam Leffler 
44c8f5794eSGleb Smirnoff #include <machine/stdarg.h>
45c8f5794eSGleb Smirnoff 
461a1e1d21SSam Leffler #include <net/if.h>
4776039bc8SGleb Smirnoff #include <net/if_var.h>
48b032f27cSSam Leffler #include <net/if_dl.h>
491a1e1d21SSam Leffler #include <net/if_media.h>
50b032f27cSSam Leffler #include <net/if_types.h>
511a1e1d21SSam Leffler #include <net/ethernet.h>
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
54b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
55616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
56616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
57616190d0SSam Leffler #endif
58b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
5967f4aa38SAdrian Chadd #include <net80211/ieee80211_vht.h>
601a1e1d21SSam Leffler 
611a1e1d21SSam Leffler #include <net/bpf.h>
621a1e1d21SSam Leffler 
63bb77492fSSam Leffler const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
64bb77492fSSam Leffler 	[IEEE80211_MODE_AUTO]	  = "auto",
65bb77492fSSam Leffler 	[IEEE80211_MODE_11A]	  = "11a",
66bb77492fSSam Leffler 	[IEEE80211_MODE_11B]	  = "11b",
67bb77492fSSam Leffler 	[IEEE80211_MODE_11G]	  = "11g",
68bb77492fSSam Leffler 	[IEEE80211_MODE_FH]	  = "FH",
69bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_A]  = "turboA",
70bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_G]  = "turboG",
71bb77492fSSam Leffler 	[IEEE80211_MODE_STURBO_A] = "sturboA",
726a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	  = "half",
736a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]  = "quarter",
74bb77492fSSam Leffler 	[IEEE80211_MODE_11NA]	  = "11na",
75bb77492fSSam Leffler 	[IEEE80211_MODE_11NG]	  = "11ng",
760c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	  = "11acg",
770c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	  = "11ac",
781a1e1d21SSam Leffler };
79c43feedeSSam Leffler /* map ieee80211_opmode to the corresponding capability bit */
80c43feedeSSam Leffler const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
81c43feedeSSam Leffler 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
82c43feedeSSam Leffler 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
83c43feedeSSam Leffler 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
84c43feedeSSam Leffler 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
85c43feedeSSam Leffler 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
86c43feedeSSam Leffler 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
8759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
8859aa14a9SRui Paulo 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
8959aa14a9SRui Paulo #endif
90c43feedeSSam Leffler };
91c43feedeSSam Leffler 
9292002144SGleb Smirnoff const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
93b032f27cSSam Leffler 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94b032f27cSSam Leffler 
95b032f27cSSam Leffler static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
962bfc8a91SSam Leffler static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
97b032f27cSSam Leffler static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
988e71a4aaSAdrian Chadd static	void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
99b032f27cSSam Leffler static	int ieee80211_media_setup(struct ieee80211com *ic,
100b032f27cSSam Leffler 		struct ifmedia *media, int caps, int addsta,
101b032f27cSSam Leffler 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
102b032f27cSSam Leffler static	int media_status(enum ieee80211_opmode,
103b032f27cSSam Leffler 		const struct ieee80211_channel *);
10428da1b56SGleb Smirnoff static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
105b032f27cSSam Leffler 
106b032f27cSSam Leffler MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
1071a1e1d21SSam Leffler 
108aadecb1aSSam Leffler /*
109aadecb1aSSam Leffler  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110aadecb1aSSam Leffler  */
111aadecb1aSSam Leffler #define	B(r)	((r) | IEEE80211_RATE_BASIC)
112aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11a =
113aadecb1aSSam Leffler 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
11441b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_half =
11541b3c790SSam Leffler 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
11641b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_quarter =
11741b3c790SSam Leffler 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
118aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11b =
119aadecb1aSSam Leffler 	{ 4, { B(2), B(4), B(11), B(22) } };
120aadecb1aSSam Leffler /* NB: OFDM rates are handled specially based on mode */
121aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11g =
122aadecb1aSSam Leffler 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
123aadecb1aSSam Leffler #undef B
124aadecb1aSSam Leffler 
12567f4aa38SAdrian Chadd static int set_vht_extchan(struct ieee80211_channel *c);
12667f4aa38SAdrian Chadd 
1271a1e1d21SSam Leffler /*
1281a1e1d21SSam Leffler  * Fill in 802.11 available channel set, mark
1291a1e1d21SSam Leffler  * all available channels as active, and pick
1301a1e1d21SSam Leffler  * a default channel if not already specified.
1311a1e1d21SSam Leffler  */
1327a79cebfSGleb Smirnoff void
13341b3c790SSam Leffler ieee80211_chan_init(struct ieee80211com *ic)
13441b3c790SSam Leffler {
13541b3c790SSam Leffler #define	DEFAULTRATES(m, def) do { \
1366a76ae21SSam Leffler 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
13745fa8b0eSSam Leffler 		ic->ic_sup_rates[m] = def; \
13841b3c790SSam Leffler } while (0)
13941b3c790SSam Leffler 	struct ieee80211_channel *c;
14041b3c790SSam Leffler 	int i;
14141b3c790SSam Leffler 
14231378b1cSSam Leffler 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
14368e8e04eSSam Leffler 		("invalid number of channels specified: %u", ic->ic_nchans));
1441a1e1d21SSam Leffler 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
145b032f27cSSam Leffler 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
1466dbd16f1SSam Leffler 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
14768e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1481a1e1d21SSam Leffler 		c = &ic->ic_channels[i];
14968e8e04eSSam Leffler 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
1509c2c544dSSam Leffler 		/*
1519c2c544dSSam Leffler 		 * Help drivers that work only with frequencies by filling
1529c2c544dSSam Leffler 		 * in IEEE channel #'s if not already calculated.  Note this
1539c2c544dSSam Leffler 		 * mimics similar work done in ieee80211_setregdomain when
1549c2c544dSSam Leffler 		 * changing regulatory state.
1559c2c544dSSam Leffler 		 */
1569c2c544dSSam Leffler 		if (c->ic_ieee == 0)
1579c2c544dSSam Leffler 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
15867f4aa38SAdrian Chadd 
15967f4aa38SAdrian Chadd 		/*
16067f4aa38SAdrian Chadd 		 * Setup the HT40/VHT40 upper/lower bits.
1611e375f3aSBjoern A. Zeeb 		 * The VHT80/... math is done elsewhere.
16267f4aa38SAdrian Chadd 		 */
1639c2c544dSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
1649c2c544dSSam Leffler 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
1659c2c544dSSam Leffler 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
1669c2c544dSSam Leffler 			    c->ic_flags);
16767f4aa38SAdrian Chadd 
16867f4aa38SAdrian Chadd 		/* Update VHT math */
16967f4aa38SAdrian Chadd 		/*
1701e375f3aSBjoern A. Zeeb 		 * XXX VHT again, note that this assumes VHT80/... channels
1711e375f3aSBjoern A. Zeeb 		 * are legit already.
17267f4aa38SAdrian Chadd 		 */
17367f4aa38SAdrian Chadd 		set_vht_extchan(c);
17467f4aa38SAdrian Chadd 
1759c2c544dSSam Leffler 		/* default max tx power to max regulatory */
1769c2c544dSSam Leffler 		if (c->ic_maxpower == 0)
1779c2c544dSSam Leffler 			c->ic_maxpower = 2*c->ic_maxregpower;
17868e8e04eSSam Leffler 		setbit(ic->ic_chan_avail, c->ic_ieee);
1791a1e1d21SSam Leffler 		/*
1801a1e1d21SSam Leffler 		 * Identify mode capabilities.
1811a1e1d21SSam Leffler 		 */
1821a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_A(c))
1836dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
1841a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_B(c))
1856dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
18645fa8b0eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(c))
1876dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
1884844aa7dSAtsushi Onoe 		if (IEEE80211_IS_CHAN_FHSS(c))
1896dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
19068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_108A(c))
1916dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
1928a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_108G(c))
1936dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
19468e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
19568e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
1966a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_HALF(c))
1976a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
1986a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_QUARTER(c))
1996a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
20068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTA(c))
20168e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
20268e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTG(c))
20368e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
2040c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTA(c))
2050c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
2060c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTG(c))
2070c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
20868e8e04eSSam Leffler 	}
20968e8e04eSSam Leffler 	/* initialize candidate channels to all available */
21068e8e04eSSam Leffler 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
21168e8e04eSSam Leffler 		sizeof(ic->ic_chan_avail));
21268e8e04eSSam Leffler 
213b032f27cSSam Leffler 	/* sort channel table to allow lookup optimizations */
214b032f27cSSam Leffler 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
215b032f27cSSam Leffler 
216b032f27cSSam Leffler 	/* invalidate any previous state */
21768e8e04eSSam Leffler 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
218ab562eefSSam Leffler 	ic->ic_prevchan = NULL;
219b032f27cSSam Leffler 	ic->ic_csa_newchan = NULL;
220b5c99415SSam Leffler 	/* arbitrarily pick the first channel */
22168e8e04eSSam Leffler 	ic->ic_curchan = &ic->ic_channels[0];
22226d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
223aadecb1aSSam Leffler 
224aadecb1aSSam Leffler 	/* fillin well-known rate sets if driver has not specified */
22541b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
22641b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
22741b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
22841b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
22941b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
2308500d65dSSam Leffler 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
2316a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
2326a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
23340432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
23440432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
2350c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,	 ieee80211_rateset_11g);
2360c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,	 ieee80211_rateset_11a);
23741b3c790SSam Leffler 
23841b3c790SSam Leffler 	/*
239fbbe47a9SBernhard Schmidt 	 * Setup required information to fill the mcsset field, if driver did
240fbbe47a9SBernhard Schmidt 	 * not. Assume a 2T2R setup for historic reasons.
241fbbe47a9SBernhard Schmidt 	 */
242fbbe47a9SBernhard Schmidt 	if (ic->ic_rxstream == 0)
243fbbe47a9SBernhard Schmidt 		ic->ic_rxstream = 2;
244fbbe47a9SBernhard Schmidt 	if (ic->ic_txstream == 0)
245fbbe47a9SBernhard Schmidt 		ic->ic_txstream = 2;
246fbbe47a9SBernhard Schmidt 
247dfabbaa0SAndriy Voskoboinyk 	ieee80211_init_suphtrates(ic);
248dfabbaa0SAndriy Voskoboinyk 
249fbbe47a9SBernhard Schmidt 	/*
25041b3c790SSam Leffler 	 * Set auto mode to reset active channel state and any desired channel.
25141b3c790SSam Leffler 	 */
25241b3c790SSam Leffler 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
25341b3c790SSam Leffler #undef DEFAULTRATES
25441b3c790SSam Leffler }
25541b3c790SSam Leffler 
256b032f27cSSam Leffler static void
257272f6adeSGleb Smirnoff null_update_mcast(struct ieee80211com *ic)
258b032f27cSSam Leffler {
259272f6adeSGleb Smirnoff 
260272f6adeSGleb Smirnoff 	ic_printf(ic, "need multicast update callback\n");
261b032f27cSSam Leffler }
262b032f27cSSam Leffler 
263b032f27cSSam Leffler static void
264272f6adeSGleb Smirnoff null_update_promisc(struct ieee80211com *ic)
265b032f27cSSam Leffler {
266272f6adeSGleb Smirnoff 
267272f6adeSGleb Smirnoff 	ic_printf(ic, "need promiscuous mode update callback\n");
268b032f27cSSam Leffler }
269b032f27cSSam Leffler 
270b94299c4SAdrian Chadd static void
271b94299c4SAdrian Chadd null_update_chw(struct ieee80211com *ic)
272b94299c4SAdrian Chadd {
273b94299c4SAdrian Chadd 
274c8f5794eSGleb Smirnoff 	ic_printf(ic, "%s: need callback\n", __func__);
275c8f5794eSGleb Smirnoff }
276c8f5794eSGleb Smirnoff 
277c8f5794eSGleb Smirnoff int
278c8f5794eSGleb Smirnoff ic_printf(struct ieee80211com *ic, const char * fmt, ...)
279c8f5794eSGleb Smirnoff {
280c8f5794eSGleb Smirnoff 	va_list ap;
281c8f5794eSGleb Smirnoff 	int retval;
282c8f5794eSGleb Smirnoff 
283c8f5794eSGleb Smirnoff 	retval = printf("%s: ", ic->ic_name);
284c8f5794eSGleb Smirnoff 	va_start(ap, fmt);
285c8f5794eSGleb Smirnoff 	retval += vprintf(fmt, ap);
286c8f5794eSGleb Smirnoff 	va_end(ap);
287c8f5794eSGleb Smirnoff 	return (retval);
288b94299c4SAdrian Chadd }
289b94299c4SAdrian Chadd 
2907a79cebfSGleb Smirnoff static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
2917a79cebfSGleb Smirnoff static struct mtx ic_list_mtx;
2927a79cebfSGleb Smirnoff MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
2937a79cebfSGleb Smirnoff 
2947a79cebfSGleb Smirnoff static int
2957a79cebfSGleb Smirnoff sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
2967a79cebfSGleb Smirnoff {
2977a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
298f09a089eSAndriy Voskoboinyk 	struct sbuf sb;
2997a79cebfSGleb Smirnoff 	char *sp;
3007a79cebfSGleb Smirnoff 	int error;
3017a79cebfSGleb Smirnoff 
302f09a089eSAndriy Voskoboinyk 	error = sysctl_wire_old_buffer(req, 0);
303f09a089eSAndriy Voskoboinyk 	if (error)
304f09a089eSAndriy Voskoboinyk 		return (error);
305f09a089eSAndriy Voskoboinyk 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
306f09a089eSAndriy Voskoboinyk 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
3077a79cebfSGleb Smirnoff 	sp = "";
3087a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3097a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next) {
310f09a089eSAndriy Voskoboinyk 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
3117a79cebfSGleb Smirnoff 		sp = " ";
3127a79cebfSGleb Smirnoff 	}
3137a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
314f09a089eSAndriy Voskoboinyk 	error = sbuf_finish(&sb);
315f09a089eSAndriy Voskoboinyk 	sbuf_delete(&sb);
3167a79cebfSGleb Smirnoff 	return (error);
3177a79cebfSGleb Smirnoff }
3187a79cebfSGleb Smirnoff 
3197a79cebfSGleb Smirnoff SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
3207a79cebfSGleb Smirnoff     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3217a79cebfSGleb Smirnoff     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
3227a79cebfSGleb Smirnoff 
323b032f27cSSam Leffler /*
324b032f27cSSam Leffler  * Attach/setup the common net80211 state.  Called by
325b032f27cSSam Leffler  * the driver on attach to prior to creating any vap's.
326b032f27cSSam Leffler  */
32741b3c790SSam Leffler void
3287a79cebfSGleb Smirnoff ieee80211_ifattach(struct ieee80211com *ic)
32941b3c790SSam Leffler {
33041b3c790SSam Leffler 
331c8f5794eSGleb Smirnoff 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
332c8f5794eSGleb Smirnoff 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
333b032f27cSSam Leffler 	TAILQ_INIT(&ic->ic_vaps);
3345efea30fSAndrew Thompson 
3355efea30fSAndrew Thompson 	/* Create a taskqueue for all state changes */
336bd29f817SBjoern A. Zeeb 	ic->ic_tq = taskqueue_create("ic_taskq",
337bd29f817SBjoern A. Zeeb 	    IEEE80211_M_WAITOK | IEEE80211_M_ZERO,
3385efea30fSAndrew Thompson 	    taskqueue_thread_enqueue, &ic->ic_tq);
3397b2b15ebSAdrian Chadd 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
3407fc10b6bSGleb Smirnoff 	    ic->ic_name);
341bd29f817SBjoern A. Zeeb 	ic->ic_ierrors = counter_u64_alloc(IEEE80211_M_WAITOK);
342bd29f817SBjoern A. Zeeb 	ic->ic_oerrors = counter_u64_alloc(IEEE80211_M_WAITOK);
34341b3c790SSam Leffler 	/*
34441b3c790SSam Leffler 	 * Fill in 802.11 available channel set, mark all
34541b3c790SSam Leffler 	 * available channels as active, and pick a default
34641b3c790SSam Leffler 	 * channel if not already specified.
34741b3c790SSam Leffler 	 */
3487a79cebfSGleb Smirnoff 	ieee80211_chan_init(ic);
34968e8e04eSSam Leffler 
350b032f27cSSam Leffler 	ic->ic_update_mcast = null_update_mcast;
351b032f27cSSam Leffler 	ic->ic_update_promisc = null_update_promisc;
352b94299c4SAdrian Chadd 	ic->ic_update_chw = null_update_chw;
3531a1e1d21SSam Leffler 
3545b16c28cSSam Leffler 	ic->ic_hash_key = arc4random();
355d365f9c7SSam Leffler 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
356d365f9c7SSam Leffler 	ic->ic_lintval = ic->ic_bintval;
3578a1b9b6aSSam Leffler 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
3588a1b9b6aSSam Leffler 
35968e8e04eSSam Leffler 	ieee80211_crypto_attach(ic);
3608a1b9b6aSSam Leffler 	ieee80211_node_attach(ic);
36168e8e04eSSam Leffler 	ieee80211_power_attach(ic);
3628a1b9b6aSSam Leffler 	ieee80211_proto_attach(ic);
363616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
364616190d0SSam Leffler 	ieee80211_superg_attach(ic);
365616190d0SSam Leffler #endif
36668e8e04eSSam Leffler 	ieee80211_ht_attach(ic);
36767f4aa38SAdrian Chadd 	ieee80211_vht_attach(ic);
36868e8e04eSSam Leffler 	ieee80211_scan_attach(ic);
369b032f27cSSam Leffler 	ieee80211_regdomain_attach(ic);
370e95e0edbSSam Leffler 	ieee80211_dfs_attach(ic);
3718a1b9b6aSSam Leffler 
372b032f27cSSam Leffler 	ieee80211_sysctl_attach(ic);
3738a1b9b6aSSam Leffler 
3747a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3757a79cebfSGleb Smirnoff 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
3767a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
3771a1e1d21SSam Leffler }
3781a1e1d21SSam Leffler 
379b032f27cSSam Leffler /*
380b032f27cSSam Leffler  * Detach net80211 state on device detach.  Tear down
381b032f27cSSam Leffler  * all vap's and reclaim all common state prior to the
382b032f27cSSam Leffler  * device state going away.  Note we may call back into
383b032f27cSSam Leffler  * driver; it must be prepared for this.
384b032f27cSSam Leffler  */
3851a1e1d21SSam Leffler void
3868a1b9b6aSSam Leffler ieee80211_ifdetach(struct ieee80211com *ic)
3871a1e1d21SSam Leffler {
388b032f27cSSam Leffler 	struct ieee80211vap *vap;
3891a1e1d21SSam Leffler 
390a84a458cSKyle Evans 	/*
391a84a458cSKyle Evans 	 * We use this as an indicator that ifattach never had a chance to be
392a84a458cSKyle Evans 	 * called, e.g. early driver attach failed and ifdetach was called
393a84a458cSKyle Evans 	 * during subsequent detach.  Never fear, for we have nothing to do
394a84a458cSKyle Evans 	 * here.
395a84a458cSKyle Evans 	 */
396a84a458cSKyle Evans 	if (ic->ic_tq == NULL)
397a84a458cSKyle Evans 		return;
398a84a458cSKyle Evans 
3997a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
4007a79cebfSGleb Smirnoff 	LIST_REMOVE(ic, ic_next);
4017a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
4025c600a90SSam Leffler 
4034061c639SAndriy Voskoboinyk 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
4044061c639SAndriy Voskoboinyk 
40530e4856aSAdrian Chadd 	/*
40630e4856aSAdrian Chadd 	 * The VAP is responsible for setting and clearing
40730e4856aSAdrian Chadd 	 * the VIMAGE context.
40830e4856aSAdrian Chadd 	 */
409dab61567SAndriy Voskoboinyk 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) {
410dab61567SAndriy Voskoboinyk 		ieee80211_com_vdetach(vap);
411b032f27cSSam Leffler 		ieee80211_vap_destroy(vap);
412dab61567SAndriy Voskoboinyk 	}
413ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
4148a1b9b6aSSam Leffler 
4158a1b9b6aSSam Leffler 	ieee80211_sysctl_detach(ic);
416e95e0edbSSam Leffler 	ieee80211_dfs_detach(ic);
417b032f27cSSam Leffler 	ieee80211_regdomain_detach(ic);
41868e8e04eSSam Leffler 	ieee80211_scan_detach(ic);
419616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
420616190d0SSam Leffler 	ieee80211_superg_detach(ic);
421616190d0SSam Leffler #endif
42267f4aa38SAdrian Chadd 	ieee80211_vht_detach(ic);
42368e8e04eSSam Leffler 	ieee80211_ht_detach(ic);
424ca4ac7aeSSam Leffler 	/* NB: must be called before ieee80211_node_detach */
4258a1b9b6aSSam Leffler 	ieee80211_proto_detach(ic);
4268a1b9b6aSSam Leffler 	ieee80211_crypto_detach(ic);
42768e8e04eSSam Leffler 	ieee80211_power_detach(ic);
4288a1b9b6aSSam Leffler 	ieee80211_node_detach(ic);
4298a1b9b6aSSam Leffler 
43028da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_ierrors);
43128da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_oerrors);
43230e4856aSAdrian Chadd 
4335efea30fSAndrew Thompson 	taskqueue_free(ic->ic_tq);
4345cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_DESTROY(ic);
43568e8e04eSSam Leffler 	IEEE80211_LOCK_DESTROY(ic);
436b032f27cSSam Leffler }
4378a1b9b6aSSam Leffler 
4387a79cebfSGleb Smirnoff struct ieee80211com *
4397a79cebfSGleb Smirnoff ieee80211_find_com(const char *name)
4407a79cebfSGleb Smirnoff {
4417a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
4427a79cebfSGleb Smirnoff 
4437a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
4447a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next)
4457a79cebfSGleb Smirnoff 		if (strcmp(ic->ic_name, name) == 0)
4467a79cebfSGleb Smirnoff 			break;
4477a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
4487a79cebfSGleb Smirnoff 
4497a79cebfSGleb Smirnoff 	return (ic);
4507a79cebfSGleb Smirnoff }
4517a79cebfSGleb Smirnoff 
4527cde0202SAndriy Voskoboinyk void
4537cde0202SAndriy Voskoboinyk ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
4547cde0202SAndriy Voskoboinyk {
4557cde0202SAndriy Voskoboinyk 	struct ieee80211com *ic;
4567cde0202SAndriy Voskoboinyk 
4577cde0202SAndriy Voskoboinyk 	mtx_lock(&ic_list_mtx);
4587cde0202SAndriy Voskoboinyk 	LIST_FOREACH(ic, &ic_head, ic_next)
4597cde0202SAndriy Voskoboinyk 		(*f)(arg, ic);
4607cde0202SAndriy Voskoboinyk 	mtx_unlock(&ic_list_mtx);
4617cde0202SAndriy Voskoboinyk }
4627cde0202SAndriy Voskoboinyk 
463b032f27cSSam Leffler /*
464b032f27cSSam Leffler  * Default reset method for use with the ioctl support.  This
465b032f27cSSam Leffler  * method is invoked after any state change in the 802.11
466b032f27cSSam Leffler  * layer that should be propagated to the hardware but not
467b032f27cSSam Leffler  * require re-initialization of the 802.11 state machine (e.g
468b032f27cSSam Leffler  * rescanning for an ap).  We always return ENETRESET which
469b032f27cSSam Leffler  * should cause the driver to re-initialize the device. Drivers
470b032f27cSSam Leffler  * can override this method to implement more optimized support.
471b032f27cSSam Leffler  */
472b032f27cSSam Leffler static int
473b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd)
474b032f27cSSam Leffler {
475b032f27cSSam Leffler 	return ENETRESET;
476b032f27cSSam Leffler }
477b032f27cSSam Leffler 
478b032f27cSSam Leffler /*
479781487cfSAdrian Chadd  * Default for updating the VAP default TX key index.
480781487cfSAdrian Chadd  *
481781487cfSAdrian Chadd  * Drivers that support TX offload as well as hardware encryption offload
482781487cfSAdrian Chadd  * may need to be informed of key index changes separate from the key
483781487cfSAdrian Chadd  * update.
484781487cfSAdrian Chadd  */
485781487cfSAdrian Chadd static void
486781487cfSAdrian Chadd default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
487781487cfSAdrian Chadd {
488781487cfSAdrian Chadd 
489781487cfSAdrian Chadd 	/* XXX assert validity */
490781487cfSAdrian Chadd 	/* XXX assert we're in a key update block */
491781487cfSAdrian Chadd 	vap->iv_def_txkey = kid;
492781487cfSAdrian Chadd }
493781487cfSAdrian Chadd 
494781487cfSAdrian Chadd /*
49528da1b56SGleb Smirnoff  * Add underlying device errors to vap errors.
49628da1b56SGleb Smirnoff  */
49728da1b56SGleb Smirnoff static uint64_t
49828da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
49928da1b56SGleb Smirnoff {
50028da1b56SGleb Smirnoff 	struct ieee80211vap *vap = ifp->if_softc;
50128da1b56SGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
50228da1b56SGleb Smirnoff 	uint64_t rv;
50328da1b56SGleb Smirnoff 
50428da1b56SGleb Smirnoff 	rv = if_get_counter_default(ifp, cnt);
50528da1b56SGleb Smirnoff 	switch (cnt) {
50628da1b56SGleb Smirnoff 	case IFCOUNTER_OERRORS:
50728da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_oerrors);
50828da1b56SGleb Smirnoff 		break;
50928da1b56SGleb Smirnoff 	case IFCOUNTER_IERRORS:
51028da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_ierrors);
51128da1b56SGleb Smirnoff 		break;
51228da1b56SGleb Smirnoff 	default:
51328da1b56SGleb Smirnoff 		break;
51428da1b56SGleb Smirnoff 	}
51528da1b56SGleb Smirnoff 
51628da1b56SGleb Smirnoff 	return (rv);
51728da1b56SGleb Smirnoff }
51828da1b56SGleb Smirnoff 
51928da1b56SGleb Smirnoff /*
520b032f27cSSam Leffler  * Prepare a vap for use.  Drivers use this call to
521b032f27cSSam Leffler  * setup net80211 state in new vap's prior attaching
522b032f27cSSam Leffler  * them with ieee80211_vap_attach (below).
523b032f27cSSam Leffler  */
524b032f27cSSam Leffler int
525b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
526fcd9500fSBernhard Schmidt     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
5277a79cebfSGleb Smirnoff     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
528b032f27cSSam Leffler {
529b032f27cSSam Leffler 	struct ifnet *ifp;
530b032f27cSSam Leffler 
531b032f27cSSam Leffler 	ifp = if_alloc(IFT_ETHER);
532b032f27cSSam Leffler 	if (ifp == NULL) {
533372c7b95SBjoern A. Zeeb 		ic_printf(ic, "%s: unable to allocate ifnet\n", __func__);
534b032f27cSSam Leffler 		return ENOMEM;
535b032f27cSSam Leffler 	}
536b032f27cSSam Leffler 	if_initname(ifp, name, unit);
537b032f27cSSam Leffler 	ifp->if_softc = vap;			/* back pointer */
538b032f27cSSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
539e7495198SAdrian Chadd 	ifp->if_transmit = ieee80211_vap_transmit;
540e7495198SAdrian Chadd 	ifp->if_qflush = ieee80211_vap_qflush;
541b032f27cSSam Leffler 	ifp->if_ioctl = ieee80211_ioctl;
542b032f27cSSam Leffler 	ifp->if_init = ieee80211_init;
54328da1b56SGleb Smirnoff 	ifp->if_get_counter = ieee80211_get_counter;
544b032f27cSSam Leffler 
545b032f27cSSam Leffler 	vap->iv_ifp = ifp;
546b032f27cSSam Leffler 	vap->iv_ic = ic;
547b032f27cSSam Leffler 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
548b032f27cSSam Leffler 	vap->iv_flags_ext = ic->ic_flags_ext;
549b032f27cSSam Leffler 	vap->iv_flags_ven = ic->ic_flags_ven;
550b032f27cSSam Leffler 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
55167f4aa38SAdrian Chadd 
55267f4aa38SAdrian Chadd 	/* 11n capabilities - XXX methodize */
553b032f27cSSam Leffler 	vap->iv_htcaps = ic->ic_htcaps;
554e1d36f83SRui Paulo 	vap->iv_htextcaps = ic->ic_htextcaps;
55567f4aa38SAdrian Chadd 
55667f4aa38SAdrian Chadd 	/* 11ac capabilities - XXX methodize */
55767f4aa38SAdrian Chadd 	vap->iv_vhtcaps = ic->ic_vhtcaps;
55867f4aa38SAdrian Chadd 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
55967f4aa38SAdrian Chadd 
560b032f27cSSam Leffler 	vap->iv_opmode = opmode;
561c43feedeSSam Leffler 	vap->iv_caps |= ieee80211_opcap[opmode];
5621d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
563b032f27cSSam Leffler 	switch (opmode) {
564b032f27cSSam Leffler 	case IEEE80211_M_WDS:
565b032f27cSSam Leffler 		/*
566b032f27cSSam Leffler 		 * WDS links must specify the bssid of the far end.
567b032f27cSSam Leffler 		 * For legacy operation this is a static relationship.
568b032f27cSSam Leffler 		 * For non-legacy operation the station must associate
569b032f27cSSam Leffler 		 * and be authorized to pass traffic.  Plumbing the
570b032f27cSSam Leffler 		 * vap to the proper node happens when the vap
571b032f27cSSam Leffler 		 * transitions to RUN state.
572b032f27cSSam Leffler 		 */
573b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
574b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_DESBSSID;
575b032f27cSSam Leffler 		if (flags & IEEE80211_CLONE_WDSLEGACY)
576b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
577b032f27cSSam Leffler 		break;
57810ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
57910ad9a77SSam Leffler 	case IEEE80211_M_AHDEMO:
58010ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
58110ad9a77SSam Leffler 			/* NB: checked before clone operation allowed */
58210ad9a77SSam Leffler 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
58310ad9a77SSam Leffler 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
58410ad9a77SSam Leffler 			/*
58510ad9a77SSam Leffler 			 * Propagate TDMA capability to mark vap; this
58610ad9a77SSam Leffler 			 * cannot be removed and is used to distinguish
58710ad9a77SSam Leffler 			 * regular ahdemo operation from ahdemo+tdma.
58810ad9a77SSam Leffler 			 */
58910ad9a77SSam Leffler 			vap->iv_caps |= IEEE80211_C_TDMA;
59010ad9a77SSam Leffler 		}
59110ad9a77SSam Leffler 		break;
59210ad9a77SSam Leffler #endif
593fcd9500fSBernhard Schmidt 	default:
594fcd9500fSBernhard Schmidt 		break;
595b032f27cSSam Leffler 	}
596ae3f00bbSSam Leffler 	/* auto-enable s/w beacon miss support */
597ae3f00bbSSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS)
598ae3f00bbSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
59983fcb812SAndrew Thompson 	/* auto-generated or user supplied MAC address */
60083fcb812SAndrew Thompson 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
60183fcb812SAndrew Thompson 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
602b032f27cSSam Leffler 	/*
603b032f27cSSam Leffler 	 * Enable various functionality by default if we're
604b032f27cSSam Leffler 	 * capable; the driver can override us if it knows better.
605b032f27cSSam Leffler 	 */
606b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_WME)
607b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_WME;
608b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_BURST)
609b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BURST;
610b032f27cSSam Leffler 	/* NB: bg scanning only makes sense for station mode right now */
611b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
612b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
613b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BGSCAN;
614c43feedeSSam Leffler 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
61582fd2577SSam Leffler 	/* NB: DFS support only makes sense for ap mode right now */
61682fd2577SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
61782fd2577SSam Leffler 	    (vap->iv_caps & IEEE80211_C_DFS))
618b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
6198379e8dbSAdrian Chadd 	/* NB: only flip on U-APSD for hostap/sta for now */
6208379e8dbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_STA)
6218379e8dbSAdrian Chadd 	    || (vap->iv_opmode == IEEE80211_M_HOSTAP)) {
6228379e8dbSAdrian Chadd 		if (vap->iv_caps & IEEE80211_C_UAPSD)
6238379e8dbSAdrian Chadd 			vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
6248379e8dbSAdrian Chadd 	}
625b032f27cSSam Leffler 
626b032f27cSSam Leffler 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
627b032f27cSSam Leffler 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
628b032f27cSSam Leffler 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
629b032f27cSSam Leffler 	/*
630b032f27cSSam Leffler 	 * Install a default reset method for the ioctl support;
631b032f27cSSam Leffler 	 * the driver can override this.
632b032f27cSSam Leffler 	 */
633b032f27cSSam Leffler 	vap->iv_reset = default_reset;
634b032f27cSSam Leffler 
635781487cfSAdrian Chadd 	/*
636781487cfSAdrian Chadd 	 * Install a default crypto key update method, the driver
637781487cfSAdrian Chadd 	 * can override this.
638781487cfSAdrian Chadd 	 */
639781487cfSAdrian Chadd 	vap->iv_update_deftxkey = default_update_deftxkey;
640781487cfSAdrian Chadd 
641b032f27cSSam Leffler 	ieee80211_sysctl_vattach(vap);
642b032f27cSSam Leffler 	ieee80211_crypto_vattach(vap);
643b032f27cSSam Leffler 	ieee80211_node_vattach(vap);
644b032f27cSSam Leffler 	ieee80211_power_vattach(vap);
645b032f27cSSam Leffler 	ieee80211_proto_vattach(vap);
646616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
647616190d0SSam Leffler 	ieee80211_superg_vattach(vap);
648616190d0SSam Leffler #endif
649b032f27cSSam Leffler 	ieee80211_ht_vattach(vap);
65067f4aa38SAdrian Chadd 	ieee80211_vht_vattach(vap);
651b032f27cSSam Leffler 	ieee80211_scan_vattach(vap);
652b032f27cSSam Leffler 	ieee80211_regdomain_vattach(vap);
6535463c4a4SSam Leffler 	ieee80211_radiotap_vattach(vap);
654d20ff6e6SAdrian Chadd 	ieee80211_vap_reset_erp(vap);
655a7c6aabdSBernhard Schmidt 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
656b6108616SRui Paulo 
657b032f27cSSam Leffler 	return 0;
658b032f27cSSam Leffler }
659b032f27cSSam Leffler 
660b032f27cSSam Leffler /*
661b032f27cSSam Leffler  * Activate a vap.  State should have been prepared with a
662b032f27cSSam Leffler  * call to ieee80211_vap_setup and by the driver.  On return
663b032f27cSSam Leffler  * from this call the vap is ready for use.
664b032f27cSSam Leffler  */
665b032f27cSSam Leffler int
6667a79cebfSGleb Smirnoff ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
6677a79cebfSGleb Smirnoff     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
668b032f27cSSam Leffler {
669b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
670b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
671b032f27cSSam Leffler 	struct ifmediareq imr;
672b032f27cSSam Leffler 	int maxrate;
673b032f27cSSam Leffler 
674b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
675b032f27cSSam Leffler 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
676b032f27cSSam Leffler 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
677c8f5794eSGleb Smirnoff 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
678b032f27cSSam Leffler 
679b032f27cSSam Leffler 	/*
680b032f27cSSam Leffler 	 * Do late attach work that cannot happen until after
681b032f27cSSam Leffler 	 * the driver has had a chance to override defaults.
682b032f27cSSam Leffler 	 */
683b032f27cSSam Leffler 	ieee80211_node_latevattach(vap);
684b032f27cSSam Leffler 	ieee80211_power_latevattach(vap);
685b032f27cSSam Leffler 
686b032f27cSSam Leffler 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
687b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
688b032f27cSSam Leffler 	ieee80211_media_status(ifp, &imr);
689b032f27cSSam Leffler 	/* NB: strip explicit mode; we're actually in autoselect */
690c3f10abdSSam Leffler 	ifmedia_set(&vap->iv_media,
691c3f10abdSSam Leffler 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
692b032f27cSSam Leffler 	if (maxrate)
693b032f27cSSam Leffler 		ifp->if_baudrate = IF_Mbps(maxrate);
694b032f27cSSam Leffler 
6957a79cebfSGleb Smirnoff 	ether_ifattach(ifp, macaddr);
6961d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
697b032f27cSSam Leffler 	/* hook output method setup by ether_ifattach */
698b032f27cSSam Leffler 	vap->iv_output = ifp->if_output;
699b032f27cSSam Leffler 	ifp->if_output = ieee80211_output;
700b032f27cSSam Leffler 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
701b032f27cSSam Leffler 
702b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
703b032f27cSSam Leffler 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
704b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
705616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
706b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
707616190d0SSam Leffler #endif
708b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
709b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
7102bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
7112bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
7128e71a4aaSAdrian Chadd 
7138e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
7148e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
7158e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
7168e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
7171e375f3aSBjoern A. Zeeb 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
718b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
719b032f27cSSam Leffler 
720b032f27cSSam Leffler 	return 1;
721b032f27cSSam Leffler }
722b032f27cSSam Leffler 
723b032f27cSSam Leffler /*
724b032f27cSSam Leffler  * Tear down vap state and reclaim the ifnet.
725b032f27cSSam Leffler  * The driver is assumed to have prepared for
726b032f27cSSam Leffler  * this; e.g. by turning off interrupts for the
727b032f27cSSam Leffler  * underlying device.
728b032f27cSSam Leffler  */
729b032f27cSSam Leffler void
730b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap)
731b032f27cSSam Leffler {
732b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
733b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
734b032f27cSSam Leffler 
73530e4856aSAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
73630e4856aSAdrian Chadd 
737b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
7387fc10b6bSGleb Smirnoff 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
739b032f27cSSam Leffler 
7401da89db5SSam Leffler 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
7411da89db5SSam Leffler 	ether_ifdetach(ifp);
7421da89db5SSam Leffler 
7431da89db5SSam Leffler 	ieee80211_stop(vap);
744b032f27cSSam Leffler 
7455efea30fSAndrew Thompson 	/*
7465efea30fSAndrew Thompson 	 * Flush any deferred vap tasks.
7475efea30fSAndrew Thompson 	 */
7485efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_nstate_task);
7495efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
750e3e94c96SAdrian Chadd 	ieee80211_draintask(ic, &vap->iv_wme_task);
751e2db307eSAndriy Voskoboinyk 	ieee80211_draintask(ic, &ic->ic_parent_task);
7525efea30fSAndrew Thompson 
753ab501dd6SSam Leffler 	/* XXX band-aid until ifnet handles this for us */
754ab501dd6SSam Leffler 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
755ab501dd6SSam Leffler 
7565efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
7575efea30fSAndrew Thompson 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
758b032f27cSSam Leffler 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
759b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
760616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
761b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
762616190d0SSam Leffler #endif
763b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
764b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
7652bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
7662bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
7678e71a4aaSAdrian Chadd 
7688e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
7698e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
7708e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
7718e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
7721e375f3aSBjoern A. Zeeb 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
7738e71a4aaSAdrian Chadd 
7745463c4a4SSam Leffler 	/* NB: this handles the bpfdetach done below */
7755463c4a4SSam Leffler 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
7767a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_PROMISC)
7777a79cebfSGleb Smirnoff 		ieee80211_promisc(vap, false);
7787a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_ALLMULTI)
7797a79cebfSGleb Smirnoff 		ieee80211_allmulti(vap, false);
780b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
781b032f27cSSam Leffler 
782b032f27cSSam Leffler 	ifmedia_removeall(&vap->iv_media);
783b032f27cSSam Leffler 
7845463c4a4SSam Leffler 	ieee80211_radiotap_vdetach(vap);
785b032f27cSSam Leffler 	ieee80211_regdomain_vdetach(vap);
786b032f27cSSam Leffler 	ieee80211_scan_vdetach(vap);
787616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
788616190d0SSam Leffler 	ieee80211_superg_vdetach(vap);
789616190d0SSam Leffler #endif
79067f4aa38SAdrian Chadd 	ieee80211_vht_vdetach(vap);
791b032f27cSSam Leffler 	ieee80211_ht_vdetach(vap);
792b032f27cSSam Leffler 	/* NB: must be before ieee80211_node_vdetach */
793b032f27cSSam Leffler 	ieee80211_proto_vdetach(vap);
794b032f27cSSam Leffler 	ieee80211_crypto_vdetach(vap);
795b032f27cSSam Leffler 	ieee80211_power_vdetach(vap);
796b032f27cSSam Leffler 	ieee80211_node_vdetach(vap);
797b032f27cSSam Leffler 	ieee80211_sysctl_vdetach(vap);
798b20f0ed1SWeongyo Jeong 
799b20f0ed1SWeongyo Jeong 	if_free(ifp);
80030e4856aSAdrian Chadd 
80130e4856aSAdrian Chadd 	CURVNET_RESTORE();
802b032f27cSSam Leffler }
803b032f27cSSam Leffler 
804b032f27cSSam Leffler /*
8057a79cebfSGleb Smirnoff  * Count number of vaps in promisc, and issue promisc on
8067a79cebfSGleb Smirnoff  * parent respectively.
807b032f27cSSam Leffler  */
808b032f27cSSam Leffler void
8097a79cebfSGleb Smirnoff ieee80211_promisc(struct ieee80211vap *vap, bool on)
810b032f27cSSam Leffler {
8117a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
812b032f27cSSam Leffler 
813c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
814c6427be9SAndriy Voskoboinyk 
8157a79cebfSGleb Smirnoff 	if (on) {
8167a79cebfSGleb Smirnoff 		if (++ic->ic_promisc == 1)
817ba2c1fbcSAdrian Chadd 			ieee80211_runtask(ic, &ic->ic_promisc_task);
8187a79cebfSGleb Smirnoff 	} else {
8197a79cebfSGleb Smirnoff 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
8207a79cebfSGleb Smirnoff 		    __func__, ic));
8217a79cebfSGleb Smirnoff 		if (--ic->ic_promisc == 0)
8227a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_promisc_task);
8237a79cebfSGleb Smirnoff 	}
8247a79cebfSGleb Smirnoff }
8257a79cebfSGleb Smirnoff 
8267a79cebfSGleb Smirnoff /*
8277a79cebfSGleb Smirnoff  * Count number of vaps in allmulti, and issue allmulti on
8287a79cebfSGleb Smirnoff  * parent respectively.
8297a79cebfSGleb Smirnoff  */
8307a79cebfSGleb Smirnoff void
8317a79cebfSGleb Smirnoff ieee80211_allmulti(struct ieee80211vap *vap, bool on)
8327a79cebfSGleb Smirnoff {
8337a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
8347a79cebfSGleb Smirnoff 
835c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
836c6427be9SAndriy Voskoboinyk 
8377a79cebfSGleb Smirnoff 	if (on) {
8387a79cebfSGleb Smirnoff 		if (++ic->ic_allmulti == 1)
8397a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_mcast_task);
8407a79cebfSGleb Smirnoff 	} else {
8417a79cebfSGleb Smirnoff 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
8427a79cebfSGleb Smirnoff 		    __func__, ic));
8437a79cebfSGleb Smirnoff 		if (--ic->ic_allmulti == 0)
8445efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_mcast_task);
845b032f27cSSam Leffler 	}
846b032f27cSSam Leffler }
847b032f27cSSam Leffler 
848b032f27cSSam Leffler /*
849b032f27cSSam Leffler  * Synchronize flag bit state in the com structure
850b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
851b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
852b032f27cSSam Leffler  */
853b032f27cSSam Leffler static void
854b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
855b032f27cSSam Leffler {
856b032f27cSSam Leffler 	struct ieee80211vap *vap;
857b032f27cSSam Leffler 	int bit;
858b032f27cSSam Leffler 
859b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
860b032f27cSSam Leffler 
861b032f27cSSam Leffler 	bit = 0;
862b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
863b032f27cSSam Leffler 		if (vap->iv_flags & flag) {
864b032f27cSSam Leffler 			bit = 1;
865b032f27cSSam Leffler 			break;
866b032f27cSSam Leffler 		}
867b032f27cSSam Leffler 	if (bit)
868b032f27cSSam Leffler 		ic->ic_flags |= flag;
869b032f27cSSam Leffler 	else
870b032f27cSSam Leffler 		ic->ic_flags &= ~flag;
871b032f27cSSam Leffler }
872b032f27cSSam Leffler 
873b032f27cSSam Leffler void
874b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag)
875b032f27cSSam Leffler {
876b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
877b032f27cSSam Leffler 
878b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
879b032f27cSSam Leffler 	if (flag < 0) {
880b032f27cSSam Leffler 		flag = -flag;
881b032f27cSSam Leffler 		vap->iv_flags &= ~flag;
882b032f27cSSam Leffler 	} else
883b032f27cSSam Leffler 		vap->iv_flags |= flag;
884b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, flag);
885b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
886b032f27cSSam Leffler }
887b032f27cSSam Leffler 
888b032f27cSSam Leffler /*
8892bfc8a91SSam Leffler  * Synchronize flags_ht bit state in the com structure
8902bfc8a91SSam Leffler  * according to the state of all vap's.  This is used,
8912bfc8a91SSam Leffler  * for example, to handle state changes via ioctls.
8922bfc8a91SSam Leffler  */
8932bfc8a91SSam Leffler static void
8942bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
8952bfc8a91SSam Leffler {
8962bfc8a91SSam Leffler 	struct ieee80211vap *vap;
8972bfc8a91SSam Leffler 	int bit;
8982bfc8a91SSam Leffler 
8992bfc8a91SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
9002bfc8a91SSam Leffler 
9012bfc8a91SSam Leffler 	bit = 0;
9022bfc8a91SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9032bfc8a91SSam Leffler 		if (vap->iv_flags_ht & flag) {
9042bfc8a91SSam Leffler 			bit = 1;
9052bfc8a91SSam Leffler 			break;
9062bfc8a91SSam Leffler 		}
9072bfc8a91SSam Leffler 	if (bit)
9082bfc8a91SSam Leffler 		ic->ic_flags_ht |= flag;
9092bfc8a91SSam Leffler 	else
9102bfc8a91SSam Leffler 		ic->ic_flags_ht &= ~flag;
9112bfc8a91SSam Leffler }
9122bfc8a91SSam Leffler 
9132bfc8a91SSam Leffler void
9142bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
9152bfc8a91SSam Leffler {
9162bfc8a91SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9172bfc8a91SSam Leffler 
9182bfc8a91SSam Leffler 	IEEE80211_LOCK(ic);
9192bfc8a91SSam Leffler 	if (flag < 0) {
9202bfc8a91SSam Leffler 		flag = -flag;
9212bfc8a91SSam Leffler 		vap->iv_flags_ht &= ~flag;
9222bfc8a91SSam Leffler 	} else
9232bfc8a91SSam Leffler 		vap->iv_flags_ht |= flag;
9242bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, flag);
9252bfc8a91SSam Leffler 	IEEE80211_UNLOCK(ic);
9262bfc8a91SSam Leffler }
9272bfc8a91SSam Leffler 
9282bfc8a91SSam Leffler /*
9298e71a4aaSAdrian Chadd  * Synchronize flags_vht bit state in the com structure
9308e71a4aaSAdrian Chadd  * according to the state of all vap's.  This is used,
9318e71a4aaSAdrian Chadd  * for example, to handle state changes via ioctls.
9328e71a4aaSAdrian Chadd  */
9338e71a4aaSAdrian Chadd static void
9348e71a4aaSAdrian Chadd ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
9358e71a4aaSAdrian Chadd {
9368e71a4aaSAdrian Chadd 	struct ieee80211vap *vap;
9378e71a4aaSAdrian Chadd 	int bit;
9388e71a4aaSAdrian Chadd 
9398e71a4aaSAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
9408e71a4aaSAdrian Chadd 
9418e71a4aaSAdrian Chadd 	bit = 0;
9428e71a4aaSAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9438e71a4aaSAdrian Chadd 		if (vap->iv_flags_vht & flag) {
9448e71a4aaSAdrian Chadd 			bit = 1;
9458e71a4aaSAdrian Chadd 			break;
9468e71a4aaSAdrian Chadd 		}
9478e71a4aaSAdrian Chadd 	if (bit)
9488e71a4aaSAdrian Chadd 		ic->ic_flags_vht |= flag;
9498e71a4aaSAdrian Chadd 	else
9508e71a4aaSAdrian Chadd 		ic->ic_flags_vht &= ~flag;
9518e71a4aaSAdrian Chadd }
9528e71a4aaSAdrian Chadd 
9538e71a4aaSAdrian Chadd void
9548e71a4aaSAdrian Chadd ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
9558e71a4aaSAdrian Chadd {
9568e71a4aaSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
9578e71a4aaSAdrian Chadd 
9588e71a4aaSAdrian Chadd 	IEEE80211_LOCK(ic);
9598e71a4aaSAdrian Chadd 	if (flag < 0) {
9608e71a4aaSAdrian Chadd 		flag = -flag;
9618e71a4aaSAdrian Chadd 		vap->iv_flags_vht &= ~flag;
9628e71a4aaSAdrian Chadd 	} else
9638e71a4aaSAdrian Chadd 		vap->iv_flags_vht |= flag;
9648e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, flag);
9658e71a4aaSAdrian Chadd 	IEEE80211_UNLOCK(ic);
9668e71a4aaSAdrian Chadd }
9678e71a4aaSAdrian Chadd 
9688e71a4aaSAdrian Chadd /*
9692bfc8a91SSam Leffler  * Synchronize flags_ext bit state in the com structure
970b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
971b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
972b032f27cSSam Leffler  */
973b032f27cSSam Leffler static void
974b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
975b032f27cSSam Leffler {
976b032f27cSSam Leffler 	struct ieee80211vap *vap;
977b032f27cSSam Leffler 	int bit;
978b032f27cSSam Leffler 
979b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
980b032f27cSSam Leffler 
981b032f27cSSam Leffler 	bit = 0;
982b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
983b032f27cSSam Leffler 		if (vap->iv_flags_ext & flag) {
984b032f27cSSam Leffler 			bit = 1;
985b032f27cSSam Leffler 			break;
986b032f27cSSam Leffler 		}
987b032f27cSSam Leffler 	if (bit)
988b032f27cSSam Leffler 		ic->ic_flags_ext |= flag;
989b032f27cSSam Leffler 	else
990b032f27cSSam Leffler 		ic->ic_flags_ext &= ~flag;
991b032f27cSSam Leffler }
992b032f27cSSam Leffler 
993b032f27cSSam Leffler void
994b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
995b032f27cSSam Leffler {
996b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
997b032f27cSSam Leffler 
998b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
999b032f27cSSam Leffler 	if (flag < 0) {
1000b032f27cSSam Leffler 		flag = -flag;
1001b032f27cSSam Leffler 		vap->iv_flags_ext &= ~flag;
1002b032f27cSSam Leffler 	} else
1003b032f27cSSam Leffler 		vap->iv_flags_ext |= flag;
1004b032f27cSSam Leffler 	ieee80211_syncflag_ext_locked(ic, flag);
1005b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
10061a1e1d21SSam Leffler }
10071a1e1d21SSam Leffler 
1008ca4ac7aeSSam Leffler static __inline int
1009ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags)
1010ca4ac7aeSSam Leffler {
1011ca4ac7aeSSam Leffler 	freq *= 10;
1012ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_QUARTER)
1013ca4ac7aeSSam Leffler 		freq += 5;
1014ca4ac7aeSSam Leffler 	else if (flags & IEEE80211_CHAN_HALF)
1015ca4ac7aeSSam Leffler 		freq += 10;
1016ca4ac7aeSSam Leffler 	else
1017ca4ac7aeSSam Leffler 		freq += 20;
1018ca4ac7aeSSam Leffler 	/* NB: there is no 907/20 wide but leave room */
1019ca4ac7aeSSam Leffler 	return (freq - 906*10) / 5;
1020ca4ac7aeSSam Leffler }
1021ca4ac7aeSSam Leffler 
1022ca4ac7aeSSam Leffler static __inline int
1023ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags)
1024ca4ac7aeSSam Leffler {
1025ca4ac7aeSSam Leffler 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1026ca4ac7aeSSam Leffler }
1027ca4ac7aeSSam Leffler 
10281a1e1d21SSam Leffler /*
10291a1e1d21SSam Leffler  * Convert MHz frequency to IEEE channel number.
10301a1e1d21SSam Leffler  */
10316f322b78SSam Leffler int
10321a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags)
10331a1e1d21SSam Leffler {
103411df4239SSam Leffler #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1035ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1036ca4ac7aeSSam Leffler 		return mapgsm(freq, flags);
10371a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10381a1e1d21SSam Leffler 		if (freq == 2484)
10391a1e1d21SSam Leffler 			return 14;
10401a1e1d21SSam Leffler 		if (freq < 2484)
10416f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
10421a1e1d21SSam Leffler 		else
10431a1e1d21SSam Leffler 			return 15 + ((freq - 2512) / 20);
1044c032abb5SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
104541b3c790SSam Leffler 		if (freq <= 5000) {
104668e8e04eSSam Leffler 			/* XXX check regdomain? */
104711df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1048ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
10496f322b78SSam Leffler 			return (freq - 4000) / 5;
105041b3c790SSam Leffler 		} else
10511a1e1d21SSam Leffler 			return (freq - 5000) / 5;
10521a1e1d21SSam Leffler 	} else {				/* either, guess */
10531a1e1d21SSam Leffler 		if (freq == 2484)
10541a1e1d21SSam Leffler 			return 14;
1055ca4ac7aeSSam Leffler 		if (freq < 2484) {
1056ca4ac7aeSSam Leffler 			if (907 <= freq && freq <= 922)
1057ca4ac7aeSSam Leffler 				return mapgsm(freq, flags);
10586f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
1059ca4ac7aeSSam Leffler 		}
10606f322b78SSam Leffler 		if (freq < 5000) {
106111df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1062ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
106341b3c790SSam Leffler 			else if (freq > 4900)
10646f322b78SSam Leffler 				return (freq - 4000) / 5;
10656f322b78SSam Leffler 			else
10661a1e1d21SSam Leffler 				return 15 + ((freq - 2512) / 20);
10676f322b78SSam Leffler 		}
10681a1e1d21SSam Leffler 		return (freq - 5000) / 5;
10691a1e1d21SSam Leffler 	}
107011df4239SSam Leffler #undef IS_FREQ_IN_PSB
10711a1e1d21SSam Leffler }
10721a1e1d21SSam Leffler 
10731a1e1d21SSam Leffler /*
10741a1e1d21SSam Leffler  * Convert channel to IEEE channel number.
10751a1e1d21SSam Leffler  */
10766f322b78SSam Leffler int
107738da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
10781a1e1d21SSam Leffler {
107968e8e04eSSam Leffler 	if (c == NULL) {
1080c8f5794eSGleb Smirnoff 		ic_printf(ic, "invalid channel (NULL)\n");
10818be0d570SSam Leffler 		return 0;		/* XXX */
10821a1e1d21SSam Leffler 	}
108368e8e04eSSam Leffler 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
10841a1e1d21SSam Leffler }
10851a1e1d21SSam Leffler 
10861a1e1d21SSam Leffler /*
10871a1e1d21SSam Leffler  * Convert IEEE channel number to MHz frequency.
10881a1e1d21SSam Leffler  */
10891a1e1d21SSam Leffler u_int
10901a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags)
10911a1e1d21SSam Leffler {
1092ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1093ca4ac7aeSSam Leffler 		return 907 + 5 * (chan / 10);
10941a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10951a1e1d21SSam Leffler 		if (chan == 14)
10961a1e1d21SSam Leffler 			return 2484;
10971a1e1d21SSam Leffler 		if (chan < 14)
10981a1e1d21SSam Leffler 			return 2407 + chan*5;
10991a1e1d21SSam Leffler 		else
11001a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
11011a1e1d21SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
110241b3c790SSam Leffler 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
110341b3c790SSam Leffler 			chan -= 37;
110441b3c790SSam Leffler 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
110541b3c790SSam Leffler 		}
11061a1e1d21SSam Leffler 		return 5000 + (chan*5);
11071a1e1d21SSam Leffler 	} else {				/* either, guess */
1108ca4ac7aeSSam Leffler 		/* XXX can't distinguish PSB+GSM channels */
11091a1e1d21SSam Leffler 		if (chan == 14)
11101a1e1d21SSam Leffler 			return 2484;
11111a1e1d21SSam Leffler 		if (chan < 14)			/* 0-13 */
11121a1e1d21SSam Leffler 			return 2407 + chan*5;
11131a1e1d21SSam Leffler 		if (chan < 27)			/* 15-26 */
11141a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
11151a1e1d21SSam Leffler 		return 5000 + (chan*5);
11161a1e1d21SSam Leffler 	}
11171a1e1d21SSam Leffler }
11181a1e1d21SSam Leffler 
1119355fec48SAndriy Voskoboinyk static __inline void
1120355fec48SAndriy Voskoboinyk set_extchan(struct ieee80211_channel *c)
1121355fec48SAndriy Voskoboinyk {
1122355fec48SAndriy Voskoboinyk 
1123355fec48SAndriy Voskoboinyk 	/*
1124355fec48SAndriy Voskoboinyk 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1125355fec48SAndriy Voskoboinyk 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
1126355fec48SAndriy Voskoboinyk 	 */
1127355fec48SAndriy Voskoboinyk 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
1128355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee + 4;
1129355fec48SAndriy Voskoboinyk 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1130355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee - 4;
1131355fec48SAndriy Voskoboinyk 	else
1132355fec48SAndriy Voskoboinyk 		c->ic_extieee = 0;
1133355fec48SAndriy Voskoboinyk }
1134355fec48SAndriy Voskoboinyk 
113567f4aa38SAdrian Chadd /*
113667f4aa38SAdrian Chadd  * Populate the freq1/freq2 fields as appropriate for VHT channels.
113767f4aa38SAdrian Chadd  *
113867f4aa38SAdrian Chadd  * This for now uses a hard-coded list of 80MHz wide channels.
113967f4aa38SAdrian Chadd  *
114067f4aa38SAdrian Chadd  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
114167f4aa38SAdrian Chadd  * wide channel we've already decided upon.
114267f4aa38SAdrian Chadd  *
114367f4aa38SAdrian Chadd  * For VHT80 and VHT160, there are only a small number of fixed
114467f4aa38SAdrian Chadd  * 80/160MHz wide channels, so we just use those.
114567f4aa38SAdrian Chadd  *
114667f4aa38SAdrian Chadd  * This is all likely very very wrong - both the regulatory code
114767f4aa38SAdrian Chadd  * and this code needs to ensure that all four channels are
114867f4aa38SAdrian Chadd  * available and valid before the VHT80 (and eight for VHT160) channel
114967f4aa38SAdrian Chadd  * is created.
115067f4aa38SAdrian Chadd  */
115167f4aa38SAdrian Chadd 
115267f4aa38SAdrian Chadd struct vht_chan_range {
115367f4aa38SAdrian Chadd 	uint16_t freq_start;
115467f4aa38SAdrian Chadd 	uint16_t freq_end;
115567f4aa38SAdrian Chadd };
115667f4aa38SAdrian Chadd 
115767f4aa38SAdrian Chadd struct vht_chan_range vht80_chan_ranges[] = {
115867f4aa38SAdrian Chadd 	{ 5170, 5250 },
115967f4aa38SAdrian Chadd 	{ 5250, 5330 },
116067f4aa38SAdrian Chadd 	{ 5490, 5570 },
116167f4aa38SAdrian Chadd 	{ 5570, 5650 },
116267f4aa38SAdrian Chadd 	{ 5650, 5730 },
116367f4aa38SAdrian Chadd 	{ 5735, 5815 },
11641e375f3aSBjoern A. Zeeb 	{ 0, 0 }
116567f4aa38SAdrian Chadd };
116667f4aa38SAdrian Chadd 
116704e7bb08SBjoern A. Zeeb struct vht_chan_range vht160_chan_ranges[] = {
116804e7bb08SBjoern A. Zeeb 	{ 5170, 5330 },
116904e7bb08SBjoern A. Zeeb 	{ 5490, 5650 },
117004e7bb08SBjoern A. Zeeb 	{ 0, 0 }
117104e7bb08SBjoern A. Zeeb };
117204e7bb08SBjoern A. Zeeb 
117367f4aa38SAdrian Chadd static int
117467f4aa38SAdrian Chadd set_vht_extchan(struct ieee80211_channel *c)
117567f4aa38SAdrian Chadd {
117667f4aa38SAdrian Chadd 	int i;
117767f4aa38SAdrian Chadd 
117830fdd33cSBjoern A. Zeeb 	if (! IEEE80211_IS_CHAN_VHT(c))
117967f4aa38SAdrian Chadd 		return (0);
118030fdd33cSBjoern A. Zeeb 
118130fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT80P80(c)) {
118230fdd33cSBjoern A. Zeeb 		printf("%s: TODO VHT80+80 channel (ieee=%d, flags=0x%08x)\n",
118330fdd33cSBjoern A. Zeeb 		    __func__, c->ic_ieee, c->ic_flags);
118467f4aa38SAdrian Chadd 	}
118567f4aa38SAdrian Chadd 
118630fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT160(c)) {
118704e7bb08SBjoern A. Zeeb 		for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
118804e7bb08SBjoern A. Zeeb 			if (c->ic_freq >= vht160_chan_ranges[i].freq_start &&
118904e7bb08SBjoern A. Zeeb 			    c->ic_freq < vht160_chan_ranges[i].freq_end) {
119004e7bb08SBjoern A. Zeeb 				int midpoint;
119104e7bb08SBjoern A. Zeeb 
119204e7bb08SBjoern A. Zeeb 				midpoint = vht160_chan_ranges[i].freq_start + 80;
119304e7bb08SBjoern A. Zeeb 				c->ic_vht_ch_freq1 =
119404e7bb08SBjoern A. Zeeb 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
119504e7bb08SBjoern A. Zeeb 				c->ic_vht_ch_freq2 = 0;
119604e7bb08SBjoern A. Zeeb #if 0
119704e7bb08SBjoern A. Zeeb 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
119804e7bb08SBjoern A. Zeeb 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
119904e7bb08SBjoern A. Zeeb 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
120004e7bb08SBjoern A. Zeeb #endif
120104e7bb08SBjoern A. Zeeb 				return (1);
120204e7bb08SBjoern A. Zeeb 			}
120304e7bb08SBjoern A. Zeeb 		}
120404e7bb08SBjoern A. Zeeb 		return (0);
120567f4aa38SAdrian Chadd 	}
120667f4aa38SAdrian Chadd 
120767f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT80(c)) {
120867f4aa38SAdrian Chadd 		for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
120967f4aa38SAdrian Chadd 			if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
121067f4aa38SAdrian Chadd 			    c->ic_freq < vht80_chan_ranges[i].freq_end) {
121167f4aa38SAdrian Chadd 				int midpoint;
121267f4aa38SAdrian Chadd 
121367f4aa38SAdrian Chadd 				midpoint = vht80_chan_ranges[i].freq_start + 40;
121467f4aa38SAdrian Chadd 				c->ic_vht_ch_freq1 =
121567f4aa38SAdrian Chadd 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
121667f4aa38SAdrian Chadd 				c->ic_vht_ch_freq2 = 0;
121767f4aa38SAdrian Chadd #if 0
121867f4aa38SAdrian Chadd 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
121967f4aa38SAdrian Chadd 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
122067f4aa38SAdrian Chadd 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
122167f4aa38SAdrian Chadd #endif
122267f4aa38SAdrian Chadd 				return (1);
122367f4aa38SAdrian Chadd 			}
122467f4aa38SAdrian Chadd 		}
122567f4aa38SAdrian Chadd 		return (0);
122667f4aa38SAdrian Chadd 	}
122767f4aa38SAdrian Chadd 
122830fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT40(c)) {
122930fdd33cSBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40U(c))
123030fdd33cSBjoern A. Zeeb 			c->ic_vht_ch_freq1 = c->ic_ieee + 2;
123130fdd33cSBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_HT40D(c))
123230fdd33cSBjoern A. Zeeb 			c->ic_vht_ch_freq1 = c->ic_ieee - 2;
123330fdd33cSBjoern A. Zeeb 		else
123430fdd33cSBjoern A. Zeeb 			return (0);
123530fdd33cSBjoern A. Zeeb 		return (1);
123630fdd33cSBjoern A. Zeeb 	}
123730fdd33cSBjoern A. Zeeb 
123830fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT20(c)) {
123930fdd33cSBjoern A. Zeeb 		c->ic_vht_ch_freq1 = c->ic_ieee;
124030fdd33cSBjoern A. Zeeb 		return (1);
124130fdd33cSBjoern A. Zeeb 	}
124230fdd33cSBjoern A. Zeeb 
124367f4aa38SAdrian Chadd 	printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1244372c7b95SBjoern A. Zeeb 	    __func__, c->ic_ieee, c->ic_flags);
124567f4aa38SAdrian Chadd 
124667f4aa38SAdrian Chadd 	return (0);
124767f4aa38SAdrian Chadd }
124867f4aa38SAdrian Chadd 
124967f4aa38SAdrian Chadd /*
125067f4aa38SAdrian Chadd  * Return whether the current channel could possibly be a part of
125104e7bb08SBjoern A. Zeeb  * a VHT80/VHT160 channel.
125267f4aa38SAdrian Chadd  *
125367f4aa38SAdrian Chadd  * This doesn't check that the whole range is in the allowed list
125467f4aa38SAdrian Chadd  * according to regulatory.
125567f4aa38SAdrian Chadd  */
125604e7bb08SBjoern A. Zeeb static bool
125704e7bb08SBjoern A. Zeeb is_vht160_valid_freq(uint16_t freq)
125804e7bb08SBjoern A. Zeeb {
125904e7bb08SBjoern A. Zeeb 	int i;
126004e7bb08SBjoern A. Zeeb 
126104e7bb08SBjoern A. Zeeb 	for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
126204e7bb08SBjoern A. Zeeb 		if (freq >= vht160_chan_ranges[i].freq_start &&
126304e7bb08SBjoern A. Zeeb 		    freq < vht160_chan_ranges[i].freq_end)
126404e7bb08SBjoern A. Zeeb 			return (true);
126504e7bb08SBjoern A. Zeeb 	}
126604e7bb08SBjoern A. Zeeb 	return (false);
126704e7bb08SBjoern A. Zeeb }
126804e7bb08SBjoern A. Zeeb 
126967f4aa38SAdrian Chadd static int
127067f4aa38SAdrian Chadd is_vht80_valid_freq(uint16_t freq)
127167f4aa38SAdrian Chadd {
127267f4aa38SAdrian Chadd 	int i;
127367f4aa38SAdrian Chadd 	for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
127467f4aa38SAdrian Chadd 		if (freq >= vht80_chan_ranges[i].freq_start &&
127567f4aa38SAdrian Chadd 		    freq < vht80_chan_ranges[i].freq_end)
127667f4aa38SAdrian Chadd 			return (1);
127767f4aa38SAdrian Chadd 	}
127867f4aa38SAdrian Chadd 	return (0);
127967f4aa38SAdrian Chadd }
128067f4aa38SAdrian Chadd 
1281355fec48SAndriy Voskoboinyk static int
1282355fec48SAndriy Voskoboinyk addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1283355fec48SAndriy Voskoboinyk     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1284355fec48SAndriy Voskoboinyk {
1285355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1286355fec48SAndriy Voskoboinyk 
1287355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1288355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1289355fec48SAndriy Voskoboinyk 
129067f4aa38SAdrian Chadd #if 0
129132cf376aSBjoern A. Zeeb 	printf("%s: %d of %d: ieee=%d, freq=%d, flags=0x%08x\n",
129232cf376aSBjoern A. Zeeb 	    __func__, *nchans, maxchans, ieee, freq, flags);
129367f4aa38SAdrian Chadd #endif
129467f4aa38SAdrian Chadd 
1295355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1296355fec48SAndriy Voskoboinyk 	c->ic_ieee = ieee;
1297355fec48SAndriy Voskoboinyk 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1298355fec48SAndriy Voskoboinyk 	c->ic_maxregpower = maxregpower;
1299355fec48SAndriy Voskoboinyk 	c->ic_maxpower = 2 * maxregpower;
1300355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
130167f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
130267f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1303355fec48SAndriy Voskoboinyk 	set_extchan(c);
130467f4aa38SAdrian Chadd 	set_vht_extchan(c);
1305355fec48SAndriy Voskoboinyk 
1306355fec48SAndriy Voskoboinyk 	return (0);
1307355fec48SAndriy Voskoboinyk }
1308355fec48SAndriy Voskoboinyk 
1309355fec48SAndriy Voskoboinyk static int
1310355fec48SAndriy Voskoboinyk copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1311355fec48SAndriy Voskoboinyk     uint32_t flags)
1312355fec48SAndriy Voskoboinyk {
1313355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1314355fec48SAndriy Voskoboinyk 
1315355fec48SAndriy Voskoboinyk 	KASSERT(*nchans > 0, ("channel list is empty\n"));
1316355fec48SAndriy Voskoboinyk 
1317355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1318355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1319355fec48SAndriy Voskoboinyk 
132067f4aa38SAdrian Chadd #if 0
132132cf376aSBjoern A. Zeeb 	printf("%s: %d of %d: flags=0x%08x\n",
132232cf376aSBjoern A. Zeeb 	    __func__, *nchans, maxchans, flags);
132367f4aa38SAdrian Chadd #endif
132467f4aa38SAdrian Chadd 
1325355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1326355fec48SAndriy Voskoboinyk 	c[0] = c[-1];
1327355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
132867f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
132967f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1330355fec48SAndriy Voskoboinyk 	set_extchan(c);
133167f4aa38SAdrian Chadd 	set_vht_extchan(c);
1332355fec48SAndriy Voskoboinyk 
1333355fec48SAndriy Voskoboinyk 	return (0);
1334355fec48SAndriy Voskoboinyk }
1335355fec48SAndriy Voskoboinyk 
133667f4aa38SAdrian Chadd /*
133767f4aa38SAdrian Chadd  * XXX VHT-2GHz
133867f4aa38SAdrian Chadd  */
1339355fec48SAndriy Voskoboinyk static void
13402b9f12f6SBjoern A. Zeeb getflags_2ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1341355fec48SAndriy Voskoboinyk {
1342355fec48SAndriy Voskoboinyk 	int nmodes;
1343355fec48SAndriy Voskoboinyk 
1344355fec48SAndriy Voskoboinyk 	nmodes = 0;
1345355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11B))
1346355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_B;
1347355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11G))
1348355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G;
1349355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NG))
1350355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
13512b9f12f6SBjoern A. Zeeb 	if (cbw_flags & NET80211_CBW_FLAG_HT40) {
1352355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1353355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1354355fec48SAndriy Voskoboinyk 	}
1355355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1356355fec48SAndriy Voskoboinyk }
1357355fec48SAndriy Voskoboinyk 
1358355fec48SAndriy Voskoboinyk static void
13592b9f12f6SBjoern A. Zeeb getflags_5ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1360355fec48SAndriy Voskoboinyk {
1361355fec48SAndriy Voskoboinyk 	int nmodes;
1362355fec48SAndriy Voskoboinyk 
136367f4aa38SAdrian Chadd 	/*
13642b9f12f6SBjoern A. Zeeb 	 * The addchan_list() function seems to expect the flags array to
136567f4aa38SAdrian Chadd 	 * be in channel width order, so the VHT bits are interspersed
136667f4aa38SAdrian Chadd 	 * as appropriate to maintain said order.
136767f4aa38SAdrian Chadd 	 *
136867f4aa38SAdrian Chadd 	 * It also assumes HT40U is before HT40D.
136967f4aa38SAdrian Chadd 	 */
1370355fec48SAndriy Voskoboinyk 	nmodes = 0;
137167f4aa38SAdrian Chadd 
137267f4aa38SAdrian Chadd 	/* 20MHz */
1373355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A))
1374355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A;
1375355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NA))
1376355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
137767f4aa38SAdrian Chadd 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
137867f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
137967f4aa38SAdrian Chadd 		    IEEE80211_CHAN_VHT20;
138098ff1f7cSAndriy Voskoboinyk 	}
138167f4aa38SAdrian Chadd 
138267f4aa38SAdrian Chadd 	/* 40MHz */
13832b9f12f6SBjoern A. Zeeb 	if (cbw_flags & NET80211_CBW_FLAG_HT40)
1384355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
13852b9f12f6SBjoern A. Zeeb 	if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
13862b9f12f6SBjoern A. Zeeb 	    isset(bands, IEEE80211_MODE_VHT_5GHZ))
13872b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
13882b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT40U;
13892b9f12f6SBjoern A. Zeeb 	if (cbw_flags & NET80211_CBW_FLAG_HT40)
1390355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
13912b9f12f6SBjoern A. Zeeb 	if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
13922b9f12f6SBjoern A. Zeeb 	    isset(bands, IEEE80211_MODE_VHT_5GHZ))
13932b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
13942b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT40D;
139567f4aa38SAdrian Chadd 
139667f4aa38SAdrian Chadd 	/* 80MHz */
13972b9f12f6SBjoern A. Zeeb 	if ((cbw_flags & NET80211_CBW_FLAG_VHT80) &&
13982b9f12f6SBjoern A. Zeeb 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
13992b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
14002b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT80;
14012b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
14022b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT80;
140367f4aa38SAdrian Chadd 	}
140467f4aa38SAdrian Chadd 
14052b9f12f6SBjoern A. Zeeb 	/* VHT160 */
14062b9f12f6SBjoern A. Zeeb 	if ((cbw_flags & NET80211_CBW_FLAG_VHT160) &&
14072b9f12f6SBjoern A. Zeeb 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
14082b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
14092b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT160;
14102b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
14112b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT160;
14122b9f12f6SBjoern A. Zeeb 	}
14132b9f12f6SBjoern A. Zeeb 
14142b9f12f6SBjoern A. Zeeb 	/* VHT80+80 */
14152b9f12f6SBjoern A. Zeeb 	if ((cbw_flags & NET80211_CBW_FLAG_VHT80P80) &&
14162b9f12f6SBjoern A. Zeeb 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
14172b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
14182b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT80P80;
14192b9f12f6SBjoern A. Zeeb 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
14202b9f12f6SBjoern A. Zeeb 		    IEEE80211_CHAN_VHT80P80;
14212b9f12f6SBjoern A. Zeeb 	}
14222b9f12f6SBjoern A. Zeeb 
1423355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1424355fec48SAndriy Voskoboinyk }
1425355fec48SAndriy Voskoboinyk 
1426355fec48SAndriy Voskoboinyk static void
14272b9f12f6SBjoern A. Zeeb getflags(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1428355fec48SAndriy Voskoboinyk {
1429355fec48SAndriy Voskoboinyk 
1430355fec48SAndriy Voskoboinyk 	flags[0] = 0;
1431355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A) ||
143267f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_11NA) ||
143367f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1434355fec48SAndriy Voskoboinyk 		if (isset(bands, IEEE80211_MODE_11B) ||
1435355fec48SAndriy Voskoboinyk 		    isset(bands, IEEE80211_MODE_11G) ||
143667f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_11NG) ||
143767f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_VHT_2GHZ))
1438355fec48SAndriy Voskoboinyk 			return;
1439355fec48SAndriy Voskoboinyk 
14402b9f12f6SBjoern A. Zeeb 		getflags_5ghz(bands, flags, cbw_flags);
1441355fec48SAndriy Voskoboinyk 	} else
14422b9f12f6SBjoern A. Zeeb 		getflags_2ghz(bands, flags, cbw_flags);
1443355fec48SAndriy Voskoboinyk }
1444355fec48SAndriy Voskoboinyk 
1445355fec48SAndriy Voskoboinyk /*
1446355fec48SAndriy Voskoboinyk  * Add one 20 MHz channel into specified channel list.
1447cd02c6b1SBjoern A. Zeeb  * You MUST NOT mix bands when calling this.  It will not add 5ghz
1448cd02c6b1SBjoern A. Zeeb  * channels if you have any B/G/N band bit set.
144904e7bb08SBjoern A. Zeeb  * The _cbw() variant does also support HT40/VHT80/160/80+80.
1450355fec48SAndriy Voskoboinyk  */
1451355fec48SAndriy Voskoboinyk int
145204e7bb08SBjoern A. Zeeb ieee80211_add_channel_cbw(struct ieee80211_channel chans[], int maxchans,
1453355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
145404e7bb08SBjoern A. Zeeb     uint32_t chan_flags, const uint8_t bands[], int cbw_flags)
1455355fec48SAndriy Voskoboinyk {
1456355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1457355fec48SAndriy Voskoboinyk 	int i, error;
1458355fec48SAndriy Voskoboinyk 
145904e7bb08SBjoern A. Zeeb 	getflags(bands, flags, cbw_flags);
1460355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1461355fec48SAndriy Voskoboinyk 
1462355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1463355fec48SAndriy Voskoboinyk 	    flags[0] | chan_flags);
1464355fec48SAndriy Voskoboinyk 	for (i = 1; flags[i] != 0 && error == 0; i++) {
1465355fec48SAndriy Voskoboinyk 		error = copychan_prev(chans, maxchans, nchans,
1466355fec48SAndriy Voskoboinyk 		    flags[i] | chan_flags);
1467355fec48SAndriy Voskoboinyk 	}
1468355fec48SAndriy Voskoboinyk 
1469355fec48SAndriy Voskoboinyk 	return (error);
1470355fec48SAndriy Voskoboinyk }
1471355fec48SAndriy Voskoboinyk 
147204e7bb08SBjoern A. Zeeb int
147304e7bb08SBjoern A. Zeeb ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
147404e7bb08SBjoern A. Zeeb     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
147504e7bb08SBjoern A. Zeeb     uint32_t chan_flags, const uint8_t bands[])
147604e7bb08SBjoern A. Zeeb {
147704e7bb08SBjoern A. Zeeb 
147804e7bb08SBjoern A. Zeeb 	return (ieee80211_add_channel_cbw(chans, maxchans, nchans, ieee, freq,
147904e7bb08SBjoern A. Zeeb 	    maxregpower, chan_flags, bands, 0));
148004e7bb08SBjoern A. Zeeb }
148104e7bb08SBjoern A. Zeeb 
1482355fec48SAndriy Voskoboinyk static struct ieee80211_channel *
1483355fec48SAndriy Voskoboinyk findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1484355fec48SAndriy Voskoboinyk     uint32_t flags)
1485355fec48SAndriy Voskoboinyk {
1486355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1487355fec48SAndriy Voskoboinyk 	int i;
1488355fec48SAndriy Voskoboinyk 
1489355fec48SAndriy Voskoboinyk 	flags &= IEEE80211_CHAN_ALLTURBO;
1490355fec48SAndriy Voskoboinyk 	/* brute force search */
1491355fec48SAndriy Voskoboinyk 	for (i = 0; i < nchans; i++) {
1492355fec48SAndriy Voskoboinyk 		c = &chans[i];
1493355fec48SAndriy Voskoboinyk 		if (c->ic_freq == freq &&
1494355fec48SAndriy Voskoboinyk 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1495355fec48SAndriy Voskoboinyk 			return c;
1496355fec48SAndriy Voskoboinyk 	}
1497355fec48SAndriy Voskoboinyk 	return NULL;
1498355fec48SAndriy Voskoboinyk }
1499355fec48SAndriy Voskoboinyk 
1500355fec48SAndriy Voskoboinyk /*
1501355fec48SAndriy Voskoboinyk  * Add 40 MHz channel pair into specified channel list.
1502355fec48SAndriy Voskoboinyk  */
150367f4aa38SAdrian Chadd /* XXX VHT */
1504355fec48SAndriy Voskoboinyk int
1505355fec48SAndriy Voskoboinyk ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1506355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1507355fec48SAndriy Voskoboinyk {
1508355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *cent, *extc;
1509355fec48SAndriy Voskoboinyk 	uint16_t freq;
1510355fec48SAndriy Voskoboinyk 	int error;
1511355fec48SAndriy Voskoboinyk 
1512355fec48SAndriy Voskoboinyk 	freq = ieee80211_ieee2mhz(ieee, flags);
1513355fec48SAndriy Voskoboinyk 
1514355fec48SAndriy Voskoboinyk 	/*
1515355fec48SAndriy Voskoboinyk 	 * Each entry defines an HT40 channel pair; find the
1516355fec48SAndriy Voskoboinyk 	 * center channel, then the extension channel above.
1517355fec48SAndriy Voskoboinyk 	 */
1518355fec48SAndriy Voskoboinyk 	flags |= IEEE80211_CHAN_HT20;
1519355fec48SAndriy Voskoboinyk 	cent = findchannel(chans, *nchans, freq, flags);
1520355fec48SAndriy Voskoboinyk 	if (cent == NULL)
1521355fec48SAndriy Voskoboinyk 		return (EINVAL);
1522355fec48SAndriy Voskoboinyk 
1523355fec48SAndriy Voskoboinyk 	extc = findchannel(chans, *nchans, freq + 20, flags);
1524355fec48SAndriy Voskoboinyk 	if (extc == NULL)
1525355fec48SAndriy Voskoboinyk 		return (ENOENT);
1526355fec48SAndriy Voskoboinyk 
1527355fec48SAndriy Voskoboinyk 	flags &= ~IEEE80211_CHAN_HT;
1528355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1529355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1530355fec48SAndriy Voskoboinyk 	if (error != 0)
1531355fec48SAndriy Voskoboinyk 		return (error);
1532355fec48SAndriy Voskoboinyk 
1533355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1534355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1535355fec48SAndriy Voskoboinyk 
1536355fec48SAndriy Voskoboinyk 	return (error);
1537355fec48SAndriy Voskoboinyk }
1538355fec48SAndriy Voskoboinyk 
1539355fec48SAndriy Voskoboinyk /*
15404774b999SAdrian Chadd  * Fetch the center frequency for the primary channel.
15414774b999SAdrian Chadd  */
15424774b999SAdrian Chadd uint32_t
15434774b999SAdrian Chadd ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
15444774b999SAdrian Chadd {
15454774b999SAdrian Chadd 
15464774b999SAdrian Chadd 	return (c->ic_freq);
15474774b999SAdrian Chadd }
15484774b999SAdrian Chadd 
15494774b999SAdrian Chadd /*
15504774b999SAdrian Chadd  * Fetch the center frequency for the primary BAND channel.
15514774b999SAdrian Chadd  *
15524774b999SAdrian Chadd  * For 5, 10, 20MHz channels it'll be the normally configured channel
15534774b999SAdrian Chadd  * frequency.
15544774b999SAdrian Chadd  *
1555*d78a9076SGordon Bergling  * For 40MHz, 80MHz, 160MHz channels it will be the centre of the
15564774b999SAdrian Chadd  * wide channel, not the centre of the primary channel (that's ic_freq).
15574774b999SAdrian Chadd  *
15584774b999SAdrian Chadd  * For 80+80MHz channels this will be the centre of the primary
15594774b999SAdrian Chadd  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
15604774b999SAdrian Chadd  */
15614774b999SAdrian Chadd uint32_t
15624774b999SAdrian Chadd ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
15634774b999SAdrian Chadd {
15644774b999SAdrian Chadd 
156567f4aa38SAdrian Chadd 	/*
156667f4aa38SAdrian Chadd 	 * VHT - use the pre-calculated centre frequency
156767f4aa38SAdrian Chadd 	 * of the given channel.
156867f4aa38SAdrian Chadd 	 */
156967f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
157067f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
157167f4aa38SAdrian Chadd 
15724774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40U(c)) {
15734774b999SAdrian Chadd 		return (c->ic_freq + 10);
15744774b999SAdrian Chadd 	}
15754774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40D(c)) {
15764774b999SAdrian Chadd 		return (c->ic_freq - 10);
15774774b999SAdrian Chadd 	}
15784774b999SAdrian Chadd 
15794774b999SAdrian Chadd 	return (c->ic_freq);
15804774b999SAdrian Chadd }
15814774b999SAdrian Chadd 
15824774b999SAdrian Chadd /*
158367f4aa38SAdrian Chadd  * For now, no 80+80 support; it will likely always return 0.
15844774b999SAdrian Chadd  */
15854774b999SAdrian Chadd uint32_t
15864774b999SAdrian Chadd ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
15874774b999SAdrian Chadd {
15884774b999SAdrian Chadd 
158967f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
159067f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
159167f4aa38SAdrian Chadd 
15924774b999SAdrian Chadd 	return (0);
15934774b999SAdrian Chadd }
15944774b999SAdrian Chadd 
15954774b999SAdrian Chadd /*
1596355fec48SAndriy Voskoboinyk  * Adds channels into specified channel list (ieee[] array must be sorted).
1597355fec48SAndriy Voskoboinyk  * Channels are already sorted.
1598355fec48SAndriy Voskoboinyk  */
1599355fec48SAndriy Voskoboinyk static int
1600355fec48SAndriy Voskoboinyk add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1601355fec48SAndriy Voskoboinyk     const uint8_t ieee[], int nieee, uint32_t flags[])
1602355fec48SAndriy Voskoboinyk {
1603355fec48SAndriy Voskoboinyk 	uint16_t freq;
1604355fec48SAndriy Voskoboinyk 	int i, j, error;
160567f4aa38SAdrian Chadd 	int is_vht;
1606355fec48SAndriy Voskoboinyk 
1607355fec48SAndriy Voskoboinyk 	for (i = 0; i < nieee; i++) {
1608355fec48SAndriy Voskoboinyk 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1609355fec48SAndriy Voskoboinyk 		for (j = 0; flags[j] != 0; j++) {
161067f4aa38SAdrian Chadd 			/*
161167f4aa38SAdrian Chadd 			 * Notes:
161267f4aa38SAdrian Chadd 			 * + HT40 and VHT40 channels occur together, so
161367f4aa38SAdrian Chadd 			 *   we need to be careful that we actually allow that.
161467f4aa38SAdrian Chadd 			 * + VHT80, VHT160 will coexist with HT40/VHT40, so
161567f4aa38SAdrian Chadd 			 *   make sure it's not skipped because of the overlap
161667f4aa38SAdrian Chadd 			 *   check used for (V)HT40.
161767f4aa38SAdrian Chadd 			 */
161867f4aa38SAdrian Chadd 			is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
161967f4aa38SAdrian Chadd 
16204b1c2487SBjoern A. Zeeb 			/* XXX TODO FIXME VHT80P80. */
162104e7bb08SBjoern A. Zeeb 
162204e7bb08SBjoern A. Zeeb 			/* Test for VHT160 analogue to the VHT80 below. */
162304e7bb08SBjoern A. Zeeb 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT160)
162404e7bb08SBjoern A. Zeeb 				if (! is_vht160_valid_freq(freq))
162504e7bb08SBjoern A. Zeeb 					continue;
16264b1c2487SBjoern A. Zeeb 
162767f4aa38SAdrian Chadd 			/*
162867f4aa38SAdrian Chadd 			 * Test for VHT80.
162967f4aa38SAdrian Chadd 			 * XXX This is all very broken right now.
163067f4aa38SAdrian Chadd 			 * What we /should/ do is:
163167f4aa38SAdrian Chadd 			 *
163267f4aa38SAdrian Chadd 			 * + check that the frequency is in the list of
163367f4aa38SAdrian Chadd 			 *   allowed VHT80 ranges; and
163467f4aa38SAdrian Chadd 			 * + the other 3 channels in the list are actually
163567f4aa38SAdrian Chadd 			 *   also available.
163667f4aa38SAdrian Chadd 			 */
163767f4aa38SAdrian Chadd 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
163867f4aa38SAdrian Chadd 				if (! is_vht80_valid_freq(freq))
163967f4aa38SAdrian Chadd 					continue;
164067f4aa38SAdrian Chadd 
164167f4aa38SAdrian Chadd 			/*
164267f4aa38SAdrian Chadd 			 * Test for (V)HT40.
164367f4aa38SAdrian Chadd 			 *
164467f4aa38SAdrian Chadd 			 * This is also a fall through from VHT80; as we only
164567f4aa38SAdrian Chadd 			 * allow a VHT80 channel if the VHT40 combination is
164667f4aa38SAdrian Chadd 			 * also valid.  If the VHT40 form is not valid then
164767f4aa38SAdrian Chadd 			 * we certainly can't do VHT80..
164867f4aa38SAdrian Chadd 			 */
1649355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40D)
165067f4aa38SAdrian Chadd 				/*
165167f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if we are the
165267f4aa38SAdrian Chadd 				 * first channel.
165367f4aa38SAdrian Chadd 				 *
165467f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if it's below/
165567f4aa38SAdrian Chadd 				 * within 20MHz of the first channel.
165667f4aa38SAdrian Chadd 				 *
165767f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if the channel
165867f4aa38SAdrian Chadd 				 * below it is not 20MHz away.
165967f4aa38SAdrian Chadd 				 */
1660355fec48SAndriy Voskoboinyk 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1661355fec48SAndriy Voskoboinyk 				    freq - 20 !=
1662355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1663355fec48SAndriy Voskoboinyk 					continue;
1664355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40U)
166567f4aa38SAdrian Chadd 				/*
166667f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if we are
166767f4aa38SAdrian Chadd 				 * the last channel.
166867f4aa38SAdrian Chadd 				 *
166967f4aa38SAdrian Chadd 				 * Can't have an "upper" channel be above the
167067f4aa38SAdrian Chadd 				 * last channel in the list.
167167f4aa38SAdrian Chadd 				 *
167267f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if the next
167367f4aa38SAdrian Chadd 				 * channel according to the math isn't 20MHz
167467f4aa38SAdrian Chadd 				 * away.  (Likely for channel 13/14.)
167567f4aa38SAdrian Chadd 				 */
1676355fec48SAndriy Voskoboinyk 				if (i == nieee - 1 ||
1677355fec48SAndriy Voskoboinyk 				    ieee[i] + 4 > ieee[nieee - 1] ||
1678355fec48SAndriy Voskoboinyk 				    freq + 20 !=
1679355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1680355fec48SAndriy Voskoboinyk 					continue;
1681355fec48SAndriy Voskoboinyk 
1682355fec48SAndriy Voskoboinyk 			if (j == 0) {
1683355fec48SAndriy Voskoboinyk 				error = addchan(chans, maxchans, nchans,
1684355fec48SAndriy Voskoboinyk 				    ieee[i], freq, 0, flags[j]);
1685355fec48SAndriy Voskoboinyk 			} else {
1686355fec48SAndriy Voskoboinyk 				error = copychan_prev(chans, maxchans, nchans,
1687355fec48SAndriy Voskoboinyk 				    flags[j]);
1688355fec48SAndriy Voskoboinyk 			}
1689355fec48SAndriy Voskoboinyk 			if (error != 0)
1690355fec48SAndriy Voskoboinyk 				return (error);
1691355fec48SAndriy Voskoboinyk 		}
1692355fec48SAndriy Voskoboinyk 	}
1693355fec48SAndriy Voskoboinyk 
16946dbbec93SAndriy Voskoboinyk 	return (0);
1695355fec48SAndriy Voskoboinyk }
1696355fec48SAndriy Voskoboinyk 
1697355fec48SAndriy Voskoboinyk int
1698355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1699355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
17002b9f12f6SBjoern A. Zeeb     int cbw_flags)
1701355fec48SAndriy Voskoboinyk {
1702355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1703355fec48SAndriy Voskoboinyk 
170467f4aa38SAdrian Chadd 	/* XXX no VHT for now */
17052b9f12f6SBjoern A. Zeeb 	getflags_2ghz(bands, flags, cbw_flags);
1706355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1707355fec48SAndriy Voskoboinyk 
1708355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1709355fec48SAndriy Voskoboinyk }
1710355fec48SAndriy Voskoboinyk 
1711355fec48SAndriy Voskoboinyk int
1712b84b3638SAndriy Voskoboinyk ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[],
17132b9f12f6SBjoern A. Zeeb     int maxchans, int *nchans, const uint8_t bands[], int cbw_flags)
1714b84b3638SAndriy Voskoboinyk {
1715b84b3638SAndriy Voskoboinyk 	const uint8_t default_chan_list[] =
1716b84b3638SAndriy Voskoboinyk 	    { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
1717b84b3638SAndriy Voskoboinyk 
1718b84b3638SAndriy Voskoboinyk 	return (ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
17192b9f12f6SBjoern A. Zeeb 	    default_chan_list, nitems(default_chan_list), bands, cbw_flags));
1720b84b3638SAndriy Voskoboinyk }
1721b84b3638SAndriy Voskoboinyk 
1722b84b3638SAndriy Voskoboinyk int
1723355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1724355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
17252b9f12f6SBjoern A. Zeeb     int cbw_flags)
1726355fec48SAndriy Voskoboinyk {
172767f4aa38SAdrian Chadd 	/*
17282b9f12f6SBjoern A. Zeeb 	 * XXX-BZ with HT and VHT there is no 1:1 mapping anymore.  Review all
17292b9f12f6SBjoern A. Zeeb 	 * uses of IEEE80211_MODE_MAX and add a new #define name for array size.
173067f4aa38SAdrian Chadd 	 */
17312b9f12f6SBjoern A. Zeeb 	uint32_t flags[2 * IEEE80211_MODE_MAX];
173267f4aa38SAdrian Chadd 
17332b9f12f6SBjoern A. Zeeb 	getflags_5ghz(bands, flags, cbw_flags);
1734355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1735355fec48SAndriy Voskoboinyk 
1736355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1737355fec48SAndriy Voskoboinyk }
1738355fec48SAndriy Voskoboinyk 
17391a1e1d21SSam Leffler /*
174068e8e04eSSam Leffler  * Locate a channel given a frequency+flags.  We cache
1741b032f27cSSam Leffler  * the previous lookup to optimize switching between two
174268e8e04eSSam Leffler  * channels--as happens with dynamic turbo.
174368e8e04eSSam Leffler  */
174468e8e04eSSam Leffler struct ieee80211_channel *
174568e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
174668e8e04eSSam Leffler {
174768e8e04eSSam Leffler 	struct ieee80211_channel *c;
174868e8e04eSSam Leffler 
174968e8e04eSSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
175068e8e04eSSam Leffler 	c = ic->ic_prevchan;
175168e8e04eSSam Leffler 	if (c != NULL && c->ic_freq == freq &&
175268e8e04eSSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
175368e8e04eSSam Leffler 		return c;
175468e8e04eSSam Leffler 	/* brute force search */
1755355fec48SAndriy Voskoboinyk 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
175668e8e04eSSam Leffler }
175768e8e04eSSam Leffler 
1758a557c018SSam Leffler /*
1759a557c018SSam Leffler  * Locate a channel given a channel number+flags.  We cache
1760a557c018SSam Leffler  * the previous lookup to optimize switching between two
1761a557c018SSam Leffler  * channels--as happens with dynamic turbo.
1762a557c018SSam Leffler  */
1763a557c018SSam Leffler struct ieee80211_channel *
1764a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1765a557c018SSam Leffler {
1766a557c018SSam Leffler 	struct ieee80211_channel *c;
1767a557c018SSam Leffler 	int i;
1768a557c018SSam Leffler 
1769a557c018SSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
1770a557c018SSam Leffler 	c = ic->ic_prevchan;
1771a557c018SSam Leffler 	if (c != NULL && c->ic_ieee == ieee &&
1772a557c018SSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1773a557c018SSam Leffler 		return c;
1774a557c018SSam Leffler 	/* brute force search */
1775a557c018SSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1776a557c018SSam Leffler 		c = &ic->ic_channels[i];
1777a557c018SSam Leffler 		if (c->ic_ieee == ieee &&
1778a557c018SSam Leffler 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1779a557c018SSam Leffler 			return c;
1780a557c018SSam Leffler 	}
1781a557c018SSam Leffler 	return NULL;
1782a557c018SSam Leffler }
1783a557c018SSam Leffler 
1784c79f192cSAdrian Chadd /*
1785c79f192cSAdrian Chadd  * Lookup a channel suitable for the given rx status.
1786c79f192cSAdrian Chadd  *
1787c79f192cSAdrian Chadd  * This is used to find a channel for a frame (eg beacon, probe
1788c79f192cSAdrian Chadd  * response) based purely on the received PHY information.
1789c79f192cSAdrian Chadd  *
1790c79f192cSAdrian Chadd  * For now it tries to do it based on R_FREQ / R_IEEE.
1791c79f192cSAdrian Chadd  * This is enough for 11bg and 11a (and thus 11ng/11na)
1792c79f192cSAdrian Chadd  * but it will not be enough for GSM, PSB channels and the
1793c79f192cSAdrian Chadd  * like.  It also doesn't know about legacy-turbog and
1794c79f192cSAdrian Chadd  * legacy-turbo modes, which some offload NICs actually
1795c79f192cSAdrian Chadd  * support in weird ways.
1796c79f192cSAdrian Chadd  *
1797c79f192cSAdrian Chadd  * Takes the ic and rxstatus; returns the channel or NULL
1798c79f192cSAdrian Chadd  * if not found.
1799c79f192cSAdrian Chadd  *
1800c79f192cSAdrian Chadd  * XXX TODO: Add support for that when the need arises.
1801c79f192cSAdrian Chadd  */
1802c79f192cSAdrian Chadd struct ieee80211_channel *
1803c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1804c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs)
1805c79f192cSAdrian Chadd {
1806c79f192cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1807c79f192cSAdrian Chadd 	uint32_t flags;
1808c79f192cSAdrian Chadd 	struct ieee80211_channel *c;
1809c79f192cSAdrian Chadd 
1810c79f192cSAdrian Chadd 	if (rxs == NULL)
1811c79f192cSAdrian Chadd 		return (NULL);
1812c79f192cSAdrian Chadd 
1813c79f192cSAdrian Chadd 	/*
1814c79f192cSAdrian Chadd 	 * Strictly speaking we only use freq for now,
1815c79f192cSAdrian Chadd 	 * however later on we may wish to just store
1816c79f192cSAdrian Chadd 	 * the ieee for verification.
1817c79f192cSAdrian Chadd 	 */
1818c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1819c79f192cSAdrian Chadd 		return (NULL);
1820c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1821c79f192cSAdrian Chadd 		return (NULL);
18229a669553SBjoern A. Zeeb 	if ((rxs->r_flags & IEEE80211_R_BAND) == 0)
18239a669553SBjoern A. Zeeb 		return (NULL);
1824c79f192cSAdrian Chadd 
1825c79f192cSAdrian Chadd 	/*
1826c79f192cSAdrian Chadd 	 * If the rx status contains a valid ieee/freq, then
1827c79f192cSAdrian Chadd 	 * ensure we populate the correct channel information
1828c79f192cSAdrian Chadd 	 * in rxchan before passing it up to the scan infrastructure.
1829c79f192cSAdrian Chadd 	 * Offload NICs will pass up beacons from all channels
1830c79f192cSAdrian Chadd 	 * during background scans.
1831c79f192cSAdrian Chadd 	 */
1832c79f192cSAdrian Chadd 
1833c79f192cSAdrian Chadd 	/* Determine a band */
18349a669553SBjoern A. Zeeb 	switch (rxs->c_band) {
18359a669553SBjoern A. Zeeb 	case IEEE80211_CHAN_2GHZ:
18369a669553SBjoern A. Zeeb 		flags = IEEE80211_CHAN_G;
18379a669553SBjoern A. Zeeb 		break;
18389a669553SBjoern A. Zeeb 	case IEEE80211_CHAN_5GHZ:
18399a669553SBjoern A. Zeeb 		flags = IEEE80211_CHAN_A;
18409a669553SBjoern A. Zeeb 		break;
18419a669553SBjoern A. Zeeb 	default:
1842c79f192cSAdrian Chadd 		if (rxs->c_freq < 3000) {
18432108f2a8SAdrian Chadd 			flags = IEEE80211_CHAN_G;
1844c79f192cSAdrian Chadd 		} else {
1845c79f192cSAdrian Chadd 			flags = IEEE80211_CHAN_A;
1846c79f192cSAdrian Chadd 		}
18479a669553SBjoern A. Zeeb 		break;
18489a669553SBjoern A. Zeeb 	}
1849c79f192cSAdrian Chadd 
1850c79f192cSAdrian Chadd 	/* Channel lookup */
1851c79f192cSAdrian Chadd 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1852c79f192cSAdrian Chadd 
1853c79f192cSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1854c79f192cSAdrian Chadd 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1855372c7b95SBjoern A. Zeeb 	    __func__, (int) rxs->c_freq, (int) rxs->c_ieee, flags, c);
1856c79f192cSAdrian Chadd 
1857c79f192cSAdrian Chadd 	return (c);
1858c79f192cSAdrian Chadd }
1859c79f192cSAdrian Chadd 
186068e8e04eSSam Leffler static void
1861b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
186268e8e04eSSam Leffler {
186368e8e04eSSam Leffler #define	ADD(_ic, _s, _o) \
1864b032f27cSSam Leffler 	ifmedia_add(media, \
186568e8e04eSSam Leffler 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
186668e8e04eSSam Leffler 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1867c3f10abdSSam Leffler 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1868c3f10abdSSam Leffler 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1869c3f10abdSSam Leffler 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1870c3f10abdSSam Leffler 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1871c3f10abdSSam Leffler 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1872c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1873c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1874c3f10abdSSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
18756a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
18766a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1877c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1878c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
18790c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IFM_IEEE80211_VHT2G,
18800c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IFM_IEEE80211_VHT5G,
188168e8e04eSSam Leffler 	};
188268e8e04eSSam Leffler 	u_int mopt;
188368e8e04eSSam Leffler 
188468e8e04eSSam Leffler 	mopt = mopts[mode];
1885b032f27cSSam Leffler 	if (addsta)
1886b032f27cSSam Leffler 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1887b032f27cSSam Leffler 	if (caps & IEEE80211_C_IBSS)
1888b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1889b032f27cSSam Leffler 	if (caps & IEEE80211_C_HOSTAP)
1890b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1891b032f27cSSam Leffler 	if (caps & IEEE80211_C_AHDEMO)
1892b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1893b032f27cSSam Leffler 	if (caps & IEEE80211_C_MONITOR)
1894b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1895b032f27cSSam Leffler 	if (caps & IEEE80211_C_WDS)
1896b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
189759aa14a9SRui Paulo 	if (caps & IEEE80211_C_MBSS)
189859aa14a9SRui Paulo 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
189968e8e04eSSam Leffler #undef ADD
190068e8e04eSSam Leffler }
190168e8e04eSSam Leffler 
190268e8e04eSSam Leffler /*
19031a1e1d21SSam Leffler  * Setup the media data structures according to the channel and
1904b032f27cSSam Leffler  * rate tables.
19051a1e1d21SSam Leffler  */
1906b032f27cSSam Leffler static int
1907b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic,
1908b032f27cSSam Leffler 	struct ifmedia *media, int caps, int addsta,
19091a1e1d21SSam Leffler 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
19101a1e1d21SSam Leffler {
1911fcd9500fSBernhard Schmidt 	int i, j, rate, maxrate, mword, r;
1912fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
191368e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
19141a1e1d21SSam Leffler 	struct ieee80211_rateset allrates;
19151a1e1d21SSam Leffler 
19162692bb26SSam Leffler 	/*
19171a1e1d21SSam Leffler 	 * Fill in media characteristics.
19181a1e1d21SSam Leffler 	 */
1919b032f27cSSam Leffler 	ifmedia_init(media, 0, media_change, media_stat);
19201a1e1d21SSam Leffler 	maxrate = 0;
192168e8e04eSSam Leffler 	/*
192268e8e04eSSam Leffler 	 * Add media for legacy operating modes.
192368e8e04eSSam Leffler 	 */
19241a1e1d21SSam Leffler 	memset(&allrates, 0, sizeof(allrates));
192568e8e04eSSam Leffler 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
19266dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
19271a1e1d21SSam Leffler 			continue;
1928b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
19291a1e1d21SSam Leffler 		if (mode == IEEE80211_MODE_AUTO)
19301a1e1d21SSam Leffler 			continue;
19311a1e1d21SSam Leffler 		rs = &ic->ic_sup_rates[mode];
19321a1e1d21SSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
19331a1e1d21SSam Leffler 			rate = rs->rs_rates[i];
19341a1e1d21SSam Leffler 			mword = ieee80211_rate2media(ic, rate, mode);
19351a1e1d21SSam Leffler 			if (mword == 0)
19361a1e1d21SSam Leffler 				continue;
1937b032f27cSSam Leffler 			addmedia(media, caps, addsta, mode, mword);
19381a1e1d21SSam Leffler 			/*
193968e8e04eSSam Leffler 			 * Add legacy rate to the collection of all rates.
19401a1e1d21SSam Leffler 			 */
19411a1e1d21SSam Leffler 			r = rate & IEEE80211_RATE_VAL;
19421a1e1d21SSam Leffler 			for (j = 0; j < allrates.rs_nrates; j++)
19431a1e1d21SSam Leffler 				if (allrates.rs_rates[j] == r)
19441a1e1d21SSam Leffler 					break;
19451a1e1d21SSam Leffler 			if (j == allrates.rs_nrates) {
19461a1e1d21SSam Leffler 				/* unique, add to the set */
19471a1e1d21SSam Leffler 				allrates.rs_rates[j] = r;
19481a1e1d21SSam Leffler 				allrates.rs_nrates++;
19491a1e1d21SSam Leffler 			}
19501a1e1d21SSam Leffler 			rate = (rate & IEEE80211_RATE_VAL) / 2;
19511a1e1d21SSam Leffler 			if (rate > maxrate)
19521a1e1d21SSam Leffler 				maxrate = rate;
19531a1e1d21SSam Leffler 		}
19541a1e1d21SSam Leffler 	}
19551a1e1d21SSam Leffler 	for (i = 0; i < allrates.rs_nrates; i++) {
19561a1e1d21SSam Leffler 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
19571a1e1d21SSam Leffler 				IEEE80211_MODE_AUTO);
19581a1e1d21SSam Leffler 		if (mword == 0)
19591a1e1d21SSam Leffler 			continue;
196068e8e04eSSam Leffler 		/* NB: remove media options from mword */
1961b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1962b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
19631a1e1d21SSam Leffler 	}
196468e8e04eSSam Leffler 	/*
196568e8e04eSSam Leffler 	 * Add HT/11n media.  Note that we do not have enough
196668e8e04eSSam Leffler 	 * bits in the media subtype to express the MCS so we
196768e8e04eSSam Leffler 	 * use a "placeholder" media subtype and any fixed MCS
196868e8e04eSSam Leffler 	 * must be specified with a different mechanism.
196968e8e04eSSam Leffler 	 */
19706a76ae21SSam Leffler 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
197168e8e04eSSam Leffler 		if (isclr(ic->ic_modecaps, mode))
197268e8e04eSSam Leffler 			continue;
1973b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1974b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
197568e8e04eSSam Leffler 	}
197668e8e04eSSam Leffler 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
197768e8e04eSSam Leffler 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1978b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1979b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
19806f897ba9SBernhard Schmidt 		i = ic->ic_txstream * 8 - 1;
19816f897ba9SBernhard Schmidt 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
19826f897ba9SBernhard Schmidt 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
19836f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_400ns;
19846f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
19856f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_800ns;
19866f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
19876f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_400ns;
19886f897ba9SBernhard Schmidt 		else
19896f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_800ns;
19906f897ba9SBernhard Schmidt 		if (rate > maxrate)
19916f897ba9SBernhard Schmidt 			maxrate = rate;
1992b032f27cSSam Leffler 	}
19930c67d389SAdrian Chadd 
19940c67d389SAdrian Chadd 	/*
19950c67d389SAdrian Chadd 	 * Add VHT media.
19968f32e493SBjoern A. Zeeb 	 * XXX-BZ skip "VHT_2GHZ" for now.
19970c67d389SAdrian Chadd 	 */
19988f32e493SBjoern A. Zeeb 	for (mode = IEEE80211_MODE_VHT_5GHZ; mode <= IEEE80211_MODE_VHT_5GHZ;
19998f32e493SBjoern A. Zeeb 	    mode++) {
20000c67d389SAdrian Chadd 		if (isclr(ic->ic_modecaps, mode))
20010c67d389SAdrian Chadd 			continue;
20020c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_AUTO);
20030c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
20048f32e493SBjoern A. Zeeb 	}
20058f32e493SBjoern A. Zeeb 	if (isset(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ)) {
20068f32e493SBjoern A. Zeeb 	       addmedia(media, caps, addsta,
20078f32e493SBjoern A. Zeeb 		   IEEE80211_MODE_AUTO, IFM_IEEE80211_VHT);
20080c67d389SAdrian Chadd 
20090c67d389SAdrian Chadd 		/* XXX TODO: VHT maxrate */
20100c67d389SAdrian Chadd 	}
20110c67d389SAdrian Chadd 
2012b032f27cSSam Leffler 	return maxrate;
201368e8e04eSSam Leffler }
201468e8e04eSSam Leffler 
20156a76ae21SSam Leffler /* XXX inline or eliminate? */
201641b3c790SSam Leffler const struct ieee80211_rateset *
201741b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
201841b3c790SSam Leffler {
201940432d36SSam Leffler 	/* XXX does this work for 11ng basic rates? */
202068e8e04eSSam Leffler 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
202141b3c790SSam Leffler }
202241b3c790SSam Leffler 
2023dfabbaa0SAndriy Voskoboinyk /* XXX inline or eliminate? */
2024dfabbaa0SAndriy Voskoboinyk const struct ieee80211_htrateset *
2025dfabbaa0SAndriy Voskoboinyk ieee80211_get_suphtrates(struct ieee80211com *ic,
2026dfabbaa0SAndriy Voskoboinyk     const struct ieee80211_channel *c)
2027dfabbaa0SAndriy Voskoboinyk {
2028dfabbaa0SAndriy Voskoboinyk 	return &ic->ic_sup_htrates;
2029dfabbaa0SAndriy Voskoboinyk }
2030dfabbaa0SAndriy Voskoboinyk 
20318a1b9b6aSSam Leffler void
20328a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic)
20338a1b9b6aSSam Leffler {
2034fcd9500fSBernhard Schmidt 	int i, rate, mword;
2035fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
203668e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
20378a1b9b6aSSam Leffler 
20387edb9e0aSSam Leffler 	/* NB: skip AUTO since it has no rates */
20397edb9e0aSSam Leffler 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
20406dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
20418a1b9b6aSSam Leffler 			continue;
2042c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
20438a1b9b6aSSam Leffler 		rs = &ic->ic_sup_rates[mode];
20448a1b9b6aSSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
204568e8e04eSSam Leffler 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
20468a1b9b6aSSam Leffler 			if (mword == 0)
20478a1b9b6aSSam Leffler 				continue;
204868e8e04eSSam Leffler 			rate = ieee80211_media2rate(mword);
20498a1b9b6aSSam Leffler 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
205068e8e04eSSam Leffler 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
20518a1b9b6aSSam Leffler 		}
20528a1b9b6aSSam Leffler 		printf("\n");
20538a1b9b6aSSam Leffler 	}
205468e8e04eSSam Leffler 	ieee80211_ht_announce(ic);
205567f4aa38SAdrian Chadd 	ieee80211_vht_announce(ic);
20568a1b9b6aSSam Leffler }
20578a1b9b6aSSam Leffler 
205868e8e04eSSam Leffler void
205968e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic)
20601a1e1d21SSam Leffler {
206168e8e04eSSam Leffler 	const struct ieee80211_channel *c;
206268e8e04eSSam Leffler 	char type;
206368e8e04eSSam Leffler 	int i, cw;
206468e8e04eSSam Leffler 
206568e8e04eSSam Leffler 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
206668e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
206768e8e04eSSam Leffler 		c = &ic->ic_channels[i];
206868e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
206968e8e04eSSam Leffler 			type = 'S';
207068e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108A(c))
207168e8e04eSSam Leffler 			type = 'T';
207268e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108G(c))
207368e8e04eSSam Leffler 			type = 'G';
207468e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HT(c))
207568e8e04eSSam Leffler 			type = 'n';
207668e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_A(c))
207768e8e04eSSam Leffler 			type = 'a';
207868e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_ANYG(c))
207968e8e04eSSam Leffler 			type = 'g';
208068e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_B(c))
208168e8e04eSSam Leffler 			type = 'b';
208268e8e04eSSam Leffler 		else
208368e8e04eSSam Leffler 			type = 'f';
208468e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
208568e8e04eSSam Leffler 			cw = 40;
208668e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(c))
208768e8e04eSSam Leffler 			cw = 10;
208868e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(c))
208968e8e04eSSam Leffler 			cw = 5;
209068e8e04eSSam Leffler 		else
209168e8e04eSSam Leffler 			cw = 20;
209268e8e04eSSam Leffler 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
209368e8e04eSSam Leffler 			, c->ic_ieee, c->ic_freq, type
209468e8e04eSSam Leffler 			, cw
209568e8e04eSSam Leffler 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
209668e8e04eSSam Leffler 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
209768e8e04eSSam Leffler 			, c->ic_maxregpower
209868e8e04eSSam Leffler 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
209968e8e04eSSam Leffler 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
210068e8e04eSSam Leffler 		);
210168e8e04eSSam Leffler 	}
21021a1e1d21SSam Leffler }
21031a1e1d21SSam Leffler 
210468e8e04eSSam Leffler static int
2105f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
210668e8e04eSSam Leffler {
21071a1e1d21SSam Leffler 	switch (IFM_MODE(ime->ifm_media)) {
21081a1e1d21SSam Leffler 	case IFM_IEEE80211_11A:
2109b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11A;
21101a1e1d21SSam Leffler 		break;
21111a1e1d21SSam Leffler 	case IFM_IEEE80211_11B:
2112b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11B;
21131a1e1d21SSam Leffler 		break;
21141a1e1d21SSam Leffler 	case IFM_IEEE80211_11G:
2115b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11G;
21161a1e1d21SSam Leffler 		break;
21174844aa7dSAtsushi Onoe 	case IFM_IEEE80211_FH:
2118b032f27cSSam Leffler 		*mode = IEEE80211_MODE_FH;
21194844aa7dSAtsushi Onoe 		break;
212068e8e04eSSam Leffler 	case IFM_IEEE80211_11NA:
2121b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NA;
212268e8e04eSSam Leffler 		break;
212368e8e04eSSam Leffler 	case IFM_IEEE80211_11NG:
2124b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NG;
212568e8e04eSSam Leffler 		break;
21268f32e493SBjoern A. Zeeb 	case IFM_IEEE80211_VHT2G:
21278f32e493SBjoern A. Zeeb 		*mode = IEEE80211_MODE_VHT_2GHZ;
21288f32e493SBjoern A. Zeeb 		break;
21298f32e493SBjoern A. Zeeb 	case IFM_IEEE80211_VHT5G:
21308f32e493SBjoern A. Zeeb 		*mode = IEEE80211_MODE_VHT_5GHZ;
21318f32e493SBjoern A. Zeeb 		break;
21321a1e1d21SSam Leffler 	case IFM_AUTO:
2133b032f27cSSam Leffler 		*mode = IEEE80211_MODE_AUTO;
21341a1e1d21SSam Leffler 		break;
21351a1e1d21SSam Leffler 	default:
2136b032f27cSSam Leffler 		return 0;
21371a1e1d21SSam Leffler 	}
21381a1e1d21SSam Leffler 	/*
21398a1b9b6aSSam Leffler 	 * Turbo mode is an ``option''.
21408a1b9b6aSSam Leffler 	 * XXX does not apply to AUTO
21411a1e1d21SSam Leffler 	 */
21421a1e1d21SSam Leffler 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2143b032f27cSSam Leffler 		if (*mode == IEEE80211_MODE_11A) {
2144f945bd7aSSam Leffler 			if (flags & IEEE80211_F_TURBOP)
2145b032f27cSSam Leffler 				*mode = IEEE80211_MODE_TURBO_A;
214668e8e04eSSam Leffler 			else
2147b032f27cSSam Leffler 				*mode = IEEE80211_MODE_STURBO_A;
2148b032f27cSSam Leffler 		} else if (*mode == IEEE80211_MODE_11G)
2149b032f27cSSam Leffler 			*mode = IEEE80211_MODE_TURBO_G;
21508a1b9b6aSSam Leffler 		else
2151b032f27cSSam Leffler 			return 0;
21521a1e1d21SSam Leffler 	}
215368e8e04eSSam Leffler 	/* XXX HT40 +/- */
2154b032f27cSSam Leffler 	return 1;
2155b032f27cSSam Leffler }
21561a1e1d21SSam Leffler 
21571a1e1d21SSam Leffler /*
2158b032f27cSSam Leffler  * Handle a media change request on the vap interface.
2159b032f27cSSam Leffler  */
2160b032f27cSSam Leffler int
2161b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp)
2162b032f27cSSam Leffler {
2163b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2164b032f27cSSam Leffler 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2165f945bd7aSSam Leffler 	uint16_t newmode;
2166b032f27cSSam Leffler 
2167f945bd7aSSam Leffler 	if (!media2mode(ime, vap->iv_flags, &newmode))
2168b032f27cSSam Leffler 		return EINVAL;
2169f945bd7aSSam Leffler 	if (vap->iv_des_mode != newmode) {
2170f945bd7aSSam Leffler 		vap->iv_des_mode = newmode;
21710a310468SSam Leffler 		/* XXX kick state machine if up+running */
2172b032f27cSSam Leffler 	}
2173b032f27cSSam Leffler 	return 0;
2174b032f27cSSam Leffler }
2175b032f27cSSam Leffler 
217668e8e04eSSam Leffler /*
217768e8e04eSSam Leffler  * Common code to calculate the media status word
217868e8e04eSSam Leffler  * from the operating mode and channel state.
217968e8e04eSSam Leffler  */
218068e8e04eSSam Leffler static int
218168e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
218268e8e04eSSam Leffler {
218368e8e04eSSam Leffler 	int status;
218468e8e04eSSam Leffler 
218568e8e04eSSam Leffler 	status = IFM_IEEE80211;
218668e8e04eSSam Leffler 	switch (opmode) {
218768e8e04eSSam Leffler 	case IEEE80211_M_STA:
218868e8e04eSSam Leffler 		break;
218968e8e04eSSam Leffler 	case IEEE80211_M_IBSS:
219068e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC;
219168e8e04eSSam Leffler 		break;
219268e8e04eSSam Leffler 	case IEEE80211_M_HOSTAP:
219368e8e04eSSam Leffler 		status |= IFM_IEEE80211_HOSTAP;
219468e8e04eSSam Leffler 		break;
219568e8e04eSSam Leffler 	case IEEE80211_M_MONITOR:
219668e8e04eSSam Leffler 		status |= IFM_IEEE80211_MONITOR;
219768e8e04eSSam Leffler 		break;
219868e8e04eSSam Leffler 	case IEEE80211_M_AHDEMO:
219968e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
220068e8e04eSSam Leffler 		break;
220168e8e04eSSam Leffler 	case IEEE80211_M_WDS:
2202b032f27cSSam Leffler 		status |= IFM_IEEE80211_WDS;
220368e8e04eSSam Leffler 		break;
220459aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
220559aa14a9SRui Paulo 		status |= IFM_IEEE80211_MBSS;
220659aa14a9SRui Paulo 		break;
220768e8e04eSSam Leffler 	}
2208656d0e8fSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) {
2209656d0e8fSBjoern A. Zeeb 		status |= IFM_IEEE80211_VHT5G;
2210656d0e8fSBjoern A. Zeeb 	} else if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) {
2211656d0e8fSBjoern A. Zeeb 		status |= IFM_IEEE80211_VHT2G;
2212656d0e8fSBjoern A. Zeeb 	} else if (IEEE80211_IS_CHAN_HTA(chan)) {
221368e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NA;
221468e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
221568e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NG;
221668e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_A(chan)) {
221768e8e04eSSam Leffler 		status |= IFM_IEEE80211_11A;
221868e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_B(chan)) {
221968e8e04eSSam Leffler 		status |= IFM_IEEE80211_11B;
222068e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
222168e8e04eSSam Leffler 		status |= IFM_IEEE80211_11G;
222268e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
222368e8e04eSSam Leffler 		status |= IFM_IEEE80211_FH;
222468e8e04eSSam Leffler 	}
222568e8e04eSSam Leffler 	/* XXX else complain? */
222668e8e04eSSam Leffler 
222768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_TURBO(chan))
222868e8e04eSSam Leffler 		status |= IFM_IEEE80211_TURBO;
2229b032f27cSSam Leffler #if 0
2230b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT20(chan))
2231b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT20;
2232b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT40(chan))
2233b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT40;
2234b032f27cSSam Leffler #endif
223568e8e04eSSam Leffler 	return status;
223668e8e04eSSam Leffler }
223768e8e04eSSam Leffler 
22381a1e1d21SSam Leffler void
22391a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
22401a1e1d21SSam Leffler {
2241b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2242b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
224368e8e04eSSam Leffler 	enum ieee80211_phymode mode;
22441a1e1d21SSam Leffler 
22451a1e1d21SSam Leffler 	imr->ifm_status = IFM_AVALID;
224668e8e04eSSam Leffler 	/*
224768e8e04eSSam Leffler 	 * NB: use the current channel's mode to lock down a xmit
224868e8e04eSSam Leffler 	 * rate only when running; otherwise we may have a mismatch
224968e8e04eSSam Leffler 	 * in which case the rate will not be convertible.
225068e8e04eSSam Leffler 	 */
22519f098ac7SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN ||
22529f098ac7SAdrian Chadd 	    vap->iv_state == IEEE80211_S_SLEEP) {
22531a1e1d21SSam Leffler 		imr->ifm_status |= IFM_ACTIVE;
225468e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_curchan);
225568e8e04eSSam Leffler 	} else
225668e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
2257b032f27cSSam Leffler 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
22588a1b9b6aSSam Leffler 	/*
22598a1b9b6aSSam Leffler 	 * Calculate a current rate if possible.
22608a1b9b6aSSam Leffler 	 */
2261b032f27cSSam Leffler 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
22628a1b9b6aSSam Leffler 		/*
22638a1b9b6aSSam Leffler 		 * A fixed rate is set, report that.
22648a1b9b6aSSam Leffler 		 */
22658a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2266b032f27cSSam Leffler 			vap->iv_txparms[mode].ucastrate, mode);
2267b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
22688a1b9b6aSSam Leffler 		/*
22698a1b9b6aSSam Leffler 		 * In station mode report the current transmit rate.
22708a1b9b6aSSam Leffler 		 */
22718a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2272b032f27cSSam Leffler 			vap->iv_bss->ni_txrate, mode);
2273ba99a9b1SAndre Oppermann 	} else
22741a1e1d21SSam Leffler 		imr->ifm_active |= IFM_AUTO;
2275b032f27cSSam Leffler 	if (imr->ifm_status & IFM_ACTIVE)
2276b032f27cSSam Leffler 		imr->ifm_current = imr->ifm_active;
22771a1e1d21SSam Leffler }
22781a1e1d21SSam Leffler 
22791a1e1d21SSam Leffler /*
22801a1e1d21SSam Leffler  * Set the current phy mode and recalculate the active channel
22811a1e1d21SSam Leffler  * set based on the available channels for this mode.  Also
22821a1e1d21SSam Leffler  * select a new default/current channel if the current one is
22831a1e1d21SSam Leffler  * inappropriate for this mode.
22841a1e1d21SSam Leffler  */
22851a1e1d21SSam Leffler int
22861a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
22871a1e1d21SSam Leffler {
22881a1e1d21SSam Leffler 	/*
2289ca4ac7aeSSam Leffler 	 * Adjust basic rates in 11b/11g supported rate set.
2290ca4ac7aeSSam Leffler 	 * Note that if operating on a hal/quarter rate channel
2291ca4ac7aeSSam Leffler 	 * this is a noop as those rates sets are different
2292ca4ac7aeSSam Leffler 	 * and used instead.
22931a1e1d21SSam Leffler 	 */
2294ca4ac7aeSSam Leffler 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2295b032f27cSSam Leffler 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2296ca4ac7aeSSam Leffler 
22971a1e1d21SSam Leffler 	ic->ic_curmode = mode;
2298d20ff6e6SAdrian Chadd 	ieee80211_reset_erp(ic);	/* reset global ERP state */
22998a1b9b6aSSam Leffler 
23001a1e1d21SSam Leffler 	return 0;
23011a1e1d21SSam Leffler }
23021a1e1d21SSam Leffler 
23031a1e1d21SSam Leffler /*
230468e8e04eSSam Leffler  * Return the phy mode for with the specified channel.
23051a1e1d21SSam Leffler  */
23061a1e1d21SSam Leffler enum ieee80211_phymode
230768e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan)
23081a1e1d21SSam Leffler {
230968e8e04eSSam Leffler 
23100c67d389SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
23110c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_2GHZ;
23120c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
23130c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_5GHZ;
23140c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_HTA(chan))
231568e8e04eSSam Leffler 		return IEEE80211_MODE_11NA;
231668e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_HTG(chan))
231768e8e04eSSam Leffler 		return IEEE80211_MODE_11NG;
231868e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_108G(chan))
23198a1b9b6aSSam Leffler 		return IEEE80211_MODE_TURBO_G;
232068e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ST(chan))
232168e8e04eSSam Leffler 		return IEEE80211_MODE_STURBO_A;
232268e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_TURBO(chan))
232368e8e04eSSam Leffler 		return IEEE80211_MODE_TURBO_A;
23246a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_HALF(chan))
23256a76ae21SSam Leffler 		return IEEE80211_MODE_HALF;
23266a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
23276a76ae21SSam Leffler 		return IEEE80211_MODE_QUARTER;
232868e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_A(chan))
232968e8e04eSSam Leffler 		return IEEE80211_MODE_11A;
233068e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(chan))
23311a1e1d21SSam Leffler 		return IEEE80211_MODE_11G;
233268e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_B(chan))
233368e8e04eSSam Leffler 		return IEEE80211_MODE_11B;
233468e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_FHSS(chan))
233568e8e04eSSam Leffler 		return IEEE80211_MODE_FH;
233668e8e04eSSam Leffler 
233768e8e04eSSam Leffler 	/* NB: should not get here */
233868e8e04eSSam Leffler 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
233968e8e04eSSam Leffler 		__func__, chan->ic_freq, chan->ic_flags);
23401a1e1d21SSam Leffler 	return IEEE80211_MODE_11B;
23411a1e1d21SSam Leffler }
23421a1e1d21SSam Leffler 
234368e8e04eSSam Leffler struct ratemedia {
234468e8e04eSSam Leffler 	u_int	match;	/* rate + mode */
234568e8e04eSSam Leffler 	u_int	media;	/* if_media rate */
234668e8e04eSSam Leffler };
234768e8e04eSSam Leffler 
234868e8e04eSSam Leffler static int
234968e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match)
235068e8e04eSSam Leffler {
235168e8e04eSSam Leffler 	int i;
235268e8e04eSSam Leffler 
235368e8e04eSSam Leffler 	for (i = 0; i < n; i++)
235468e8e04eSSam Leffler 		if (rates[i].match == match)
235568e8e04eSSam Leffler 			return rates[i].media;
235668e8e04eSSam Leffler 	return IFM_AUTO;
235768e8e04eSSam Leffler }
235868e8e04eSSam Leffler 
23591a1e1d21SSam Leffler /*
236068e8e04eSSam Leffler  * Convert IEEE80211 rate value to ifmedia subtype.
236168e8e04eSSam Leffler  * Rate is either a legacy rate in units of 0.5Mbps
236268e8e04eSSam Leffler  * or an MCS index.
23631a1e1d21SSam Leffler  */
23641a1e1d21SSam Leffler int
23651a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
23661a1e1d21SSam Leffler {
236768e8e04eSSam Leffler 	static const struct ratemedia rates[] = {
23684844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
23694844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
23704844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
23714844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
23724844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
23734844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
23744844aa7dSAtsushi Onoe 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
23754844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
23764844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
23774844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
23784844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
23794844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
23804844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
23814844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
23824844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
23834844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
23844844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
23854844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
23864844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
23874844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
23884844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
23894844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
23904844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
23914844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
23924844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
23934844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
23944844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
239541b3c790SSam Leffler 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
239641b3c790SSam Leffler 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
239741b3c790SSam Leffler 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2398a4641f4eSPedro F. Giffuni 		/* NB: OFDM72 doesn't really exist so we don't handle it */
23991a1e1d21SSam Leffler 	};
240068e8e04eSSam Leffler 	static const struct ratemedia htrates[] = {
240168e8e04eSSam Leffler 		{   0, IFM_IEEE80211_MCS },
240268e8e04eSSam Leffler 		{   1, IFM_IEEE80211_MCS },
240368e8e04eSSam Leffler 		{   2, IFM_IEEE80211_MCS },
240468e8e04eSSam Leffler 		{   3, IFM_IEEE80211_MCS },
240568e8e04eSSam Leffler 		{   4, IFM_IEEE80211_MCS },
240668e8e04eSSam Leffler 		{   5, IFM_IEEE80211_MCS },
240768e8e04eSSam Leffler 		{   6, IFM_IEEE80211_MCS },
240868e8e04eSSam Leffler 		{   7, IFM_IEEE80211_MCS },
240968e8e04eSSam Leffler 		{   8, IFM_IEEE80211_MCS },
241068e8e04eSSam Leffler 		{   9, IFM_IEEE80211_MCS },
241168e8e04eSSam Leffler 		{  10, IFM_IEEE80211_MCS },
241268e8e04eSSam Leffler 		{  11, IFM_IEEE80211_MCS },
241368e8e04eSSam Leffler 		{  12, IFM_IEEE80211_MCS },
241468e8e04eSSam Leffler 		{  13, IFM_IEEE80211_MCS },
241568e8e04eSSam Leffler 		{  14, IFM_IEEE80211_MCS },
241668e8e04eSSam Leffler 		{  15, IFM_IEEE80211_MCS },
2417f136f45fSBernhard Schmidt 		{  16, IFM_IEEE80211_MCS },
2418f136f45fSBernhard Schmidt 		{  17, IFM_IEEE80211_MCS },
2419f136f45fSBernhard Schmidt 		{  18, IFM_IEEE80211_MCS },
2420f136f45fSBernhard Schmidt 		{  19, IFM_IEEE80211_MCS },
2421f136f45fSBernhard Schmidt 		{  20, IFM_IEEE80211_MCS },
2422f136f45fSBernhard Schmidt 		{  21, IFM_IEEE80211_MCS },
2423f136f45fSBernhard Schmidt 		{  22, IFM_IEEE80211_MCS },
2424f136f45fSBernhard Schmidt 		{  23, IFM_IEEE80211_MCS },
2425f136f45fSBernhard Schmidt 		{  24, IFM_IEEE80211_MCS },
2426f136f45fSBernhard Schmidt 		{  25, IFM_IEEE80211_MCS },
2427f136f45fSBernhard Schmidt 		{  26, IFM_IEEE80211_MCS },
2428f136f45fSBernhard Schmidt 		{  27, IFM_IEEE80211_MCS },
2429f136f45fSBernhard Schmidt 		{  28, IFM_IEEE80211_MCS },
2430f136f45fSBernhard Schmidt 		{  29, IFM_IEEE80211_MCS },
2431f136f45fSBernhard Schmidt 		{  30, IFM_IEEE80211_MCS },
2432f136f45fSBernhard Schmidt 		{  31, IFM_IEEE80211_MCS },
2433f136f45fSBernhard Schmidt 		{  32, IFM_IEEE80211_MCS },
2434f136f45fSBernhard Schmidt 		{  33, IFM_IEEE80211_MCS },
2435f136f45fSBernhard Schmidt 		{  34, IFM_IEEE80211_MCS },
2436f136f45fSBernhard Schmidt 		{  35, IFM_IEEE80211_MCS },
2437f136f45fSBernhard Schmidt 		{  36, IFM_IEEE80211_MCS },
2438f136f45fSBernhard Schmidt 		{  37, IFM_IEEE80211_MCS },
2439f136f45fSBernhard Schmidt 		{  38, IFM_IEEE80211_MCS },
2440f136f45fSBernhard Schmidt 		{  39, IFM_IEEE80211_MCS },
2441f136f45fSBernhard Schmidt 		{  40, IFM_IEEE80211_MCS },
2442f136f45fSBernhard Schmidt 		{  41, IFM_IEEE80211_MCS },
2443f136f45fSBernhard Schmidt 		{  42, IFM_IEEE80211_MCS },
2444f136f45fSBernhard Schmidt 		{  43, IFM_IEEE80211_MCS },
2445f136f45fSBernhard Schmidt 		{  44, IFM_IEEE80211_MCS },
2446f136f45fSBernhard Schmidt 		{  45, IFM_IEEE80211_MCS },
2447f136f45fSBernhard Schmidt 		{  46, IFM_IEEE80211_MCS },
2448f136f45fSBernhard Schmidt 		{  47, IFM_IEEE80211_MCS },
2449f136f45fSBernhard Schmidt 		{  48, IFM_IEEE80211_MCS },
2450f136f45fSBernhard Schmidt 		{  49, IFM_IEEE80211_MCS },
2451f136f45fSBernhard Schmidt 		{  50, IFM_IEEE80211_MCS },
2452f136f45fSBernhard Schmidt 		{  51, IFM_IEEE80211_MCS },
2453f136f45fSBernhard Schmidt 		{  52, IFM_IEEE80211_MCS },
2454f136f45fSBernhard Schmidt 		{  53, IFM_IEEE80211_MCS },
2455f136f45fSBernhard Schmidt 		{  54, IFM_IEEE80211_MCS },
2456f136f45fSBernhard Schmidt 		{  55, IFM_IEEE80211_MCS },
2457f136f45fSBernhard Schmidt 		{  56, IFM_IEEE80211_MCS },
2458f136f45fSBernhard Schmidt 		{  57, IFM_IEEE80211_MCS },
2459f136f45fSBernhard Schmidt 		{  58, IFM_IEEE80211_MCS },
2460f136f45fSBernhard Schmidt 		{  59, IFM_IEEE80211_MCS },
2461f136f45fSBernhard Schmidt 		{  60, IFM_IEEE80211_MCS },
2462f136f45fSBernhard Schmidt 		{  61, IFM_IEEE80211_MCS },
2463f136f45fSBernhard Schmidt 		{  62, IFM_IEEE80211_MCS },
2464f136f45fSBernhard Schmidt 		{  63, IFM_IEEE80211_MCS },
2465f136f45fSBernhard Schmidt 		{  64, IFM_IEEE80211_MCS },
2466f136f45fSBernhard Schmidt 		{  65, IFM_IEEE80211_MCS },
2467f136f45fSBernhard Schmidt 		{  66, IFM_IEEE80211_MCS },
2468f136f45fSBernhard Schmidt 		{  67, IFM_IEEE80211_MCS },
2469f136f45fSBernhard Schmidt 		{  68, IFM_IEEE80211_MCS },
2470f136f45fSBernhard Schmidt 		{  69, IFM_IEEE80211_MCS },
2471f136f45fSBernhard Schmidt 		{  70, IFM_IEEE80211_MCS },
2472f136f45fSBernhard Schmidt 		{  71, IFM_IEEE80211_MCS },
2473f136f45fSBernhard Schmidt 		{  72, IFM_IEEE80211_MCS },
2474f136f45fSBernhard Schmidt 		{  73, IFM_IEEE80211_MCS },
2475f136f45fSBernhard Schmidt 		{  74, IFM_IEEE80211_MCS },
2476f136f45fSBernhard Schmidt 		{  75, IFM_IEEE80211_MCS },
2477f136f45fSBernhard Schmidt 		{  76, IFM_IEEE80211_MCS },
247868e8e04eSSam Leffler 	};
24798f32e493SBjoern A. Zeeb 	static const struct ratemedia vhtrates[] = {
24808f32e493SBjoern A. Zeeb 		{   0, IFM_IEEE80211_VHT },
24818f32e493SBjoern A. Zeeb 		{   1, IFM_IEEE80211_VHT },
24828f32e493SBjoern A. Zeeb 		{   2, IFM_IEEE80211_VHT },
24838f32e493SBjoern A. Zeeb 		{   3, IFM_IEEE80211_VHT },
24848f32e493SBjoern A. Zeeb 		{   4, IFM_IEEE80211_VHT },
24858f32e493SBjoern A. Zeeb 		{   5, IFM_IEEE80211_VHT },
24868f32e493SBjoern A. Zeeb 		{   6, IFM_IEEE80211_VHT },
24878f32e493SBjoern A. Zeeb 		{   7, IFM_IEEE80211_VHT },
24888f32e493SBjoern A. Zeeb 		{   8, IFM_IEEE80211_VHT },	/* Optional. */
24898f32e493SBjoern A. Zeeb 		{   9, IFM_IEEE80211_VHT },	/* Optional. */
24908f32e493SBjoern A. Zeeb #if 0
24918f32e493SBjoern A. Zeeb 		/* Some QCA and BRCM seem to support this; offspec. */
24928f32e493SBjoern A. Zeeb 		{  10, IFM_IEEE80211_VHT },
24938f32e493SBjoern A. Zeeb 		{  11, IFM_IEEE80211_VHT },
24948f32e493SBjoern A. Zeeb #endif
24958f32e493SBjoern A. Zeeb 	};
249668e8e04eSSam Leffler 	int m;
24971a1e1d21SSam Leffler 
249868e8e04eSSam Leffler 	/*
24998f32e493SBjoern A. Zeeb 	 * Check 11ac/11n rates first for match as an MCS.
250068e8e04eSSam Leffler 	 */
25018f32e493SBjoern A. Zeeb 	if (mode == IEEE80211_MODE_VHT_5GHZ) {
25028f32e493SBjoern A. Zeeb 		if (rate & IFM_IEEE80211_VHT) {
25038f32e493SBjoern A. Zeeb 			rate &= ~IFM_IEEE80211_VHT;
25048f32e493SBjoern A. Zeeb 			m = findmedia(vhtrates, nitems(vhtrates), rate);
25058f32e493SBjoern A. Zeeb 			if (m != IFM_AUTO)
25068f32e493SBjoern A. Zeeb 				return (m | IFM_IEEE80211_VHT);
25078f32e493SBjoern A. Zeeb 		}
25088f32e493SBjoern A. Zeeb 	} else if (mode == IEEE80211_MODE_11NA) {
2509f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2510f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2511a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
251268e8e04eSSam Leffler 			if (m != IFM_AUTO)
251368e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NA;
251468e8e04eSSam Leffler 		}
251568e8e04eSSam Leffler 	} else if (mode == IEEE80211_MODE_11NG) {
251668e8e04eSSam Leffler 		/* NB: 12 is ambiguous, it will be treated as an MCS */
2517f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2518f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2519a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
252068e8e04eSSam Leffler 			if (m != IFM_AUTO)
252168e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NG;
252268e8e04eSSam Leffler 		}
252368e8e04eSSam Leffler 	}
252468e8e04eSSam Leffler 	rate &= IEEE80211_RATE_VAL;
25251a1e1d21SSam Leffler 	switch (mode) {
25261a1e1d21SSam Leffler 	case IEEE80211_MODE_11A:
25276a76ae21SSam Leffler 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
25286a76ae21SSam Leffler 	case IEEE80211_MODE_QUARTER:
252968e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
25308a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_A:
253168e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
2532a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2533a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11A);
25341a1e1d21SSam Leffler 	case IEEE80211_MODE_11B:
2535a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2536a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11B);
25374844aa7dSAtsushi Onoe 	case IEEE80211_MODE_FH:
2538a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2539a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_FH);
25401a1e1d21SSam Leffler 	case IEEE80211_MODE_AUTO:
25411a1e1d21SSam Leffler 		/* NB: ic may be NULL for some drivers */
2542566d825bSSam Leffler 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2543a3e08d6fSRui Paulo 			return findmedia(rates, nitems(rates),
254468e8e04eSSam Leffler 			    rate | IFM_IEEE80211_FH);
25451a1e1d21SSam Leffler 		/* NB: hack, 11g matches both 11b+11a rates */
25461a1e1d21SSam Leffler 		/* fall thru... */
25471a1e1d21SSam Leffler 	case IEEE80211_MODE_11G:
254868e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
25498a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_G:
2550a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
25517aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_2GHZ:
25527aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_5GHZ:
25537aebd3e5SAdrian Chadd 		/* XXX TODO: need to figure out mapping for VHT rates */
25547aebd3e5SAdrian Chadd 		return IFM_AUTO;
25551a1e1d21SSam Leffler 	}
25561a1e1d21SSam Leffler 	return IFM_AUTO;
25571a1e1d21SSam Leffler }
25581a1e1d21SSam Leffler 
25591a1e1d21SSam Leffler int
25601a1e1d21SSam Leffler ieee80211_media2rate(int mword)
25611a1e1d21SSam Leffler {
25621a1e1d21SSam Leffler 	static const int ieeerates[] = {
25631a1e1d21SSam Leffler 		-1,		/* IFM_AUTO */
25641a1e1d21SSam Leffler 		0,		/* IFM_MANUAL */
25651a1e1d21SSam Leffler 		0,		/* IFM_NONE */
25661a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_FH1 */
25671a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_FH2 */
25681a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_DS1 */
25691a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_DS2 */
25701a1e1d21SSam Leffler 		11,		/* IFM_IEEE80211_DS5 */
25711a1e1d21SSam Leffler 		22,		/* IFM_IEEE80211_DS11 */
25721a1e1d21SSam Leffler 		44,		/* IFM_IEEE80211_DS22 */
25731a1e1d21SSam Leffler 		12,		/* IFM_IEEE80211_OFDM6 */
25741a1e1d21SSam Leffler 		18,		/* IFM_IEEE80211_OFDM9 */
25751a1e1d21SSam Leffler 		24,		/* IFM_IEEE80211_OFDM12 */
25761a1e1d21SSam Leffler 		36,		/* IFM_IEEE80211_OFDM18 */
25771a1e1d21SSam Leffler 		48,		/* IFM_IEEE80211_OFDM24 */
25781a1e1d21SSam Leffler 		72,		/* IFM_IEEE80211_OFDM36 */
25791a1e1d21SSam Leffler 		96,		/* IFM_IEEE80211_OFDM48 */
25801a1e1d21SSam Leffler 		108,		/* IFM_IEEE80211_OFDM54 */
25811a1e1d21SSam Leffler 		144,		/* IFM_IEEE80211_OFDM72 */
258241b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS354k */
258341b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS512k */
258441b3c790SSam Leffler 		6,		/* IFM_IEEE80211_OFDM3 */
258541b3c790SSam Leffler 		9,		/* IFM_IEEE80211_OFDM4 */
258641b3c790SSam Leffler 		54,		/* IFM_IEEE80211_OFDM27 */
258768e8e04eSSam Leffler 		-1,		/* IFM_IEEE80211_MCS */
25887aebd3e5SAdrian Chadd 		-1,		/* IFM_IEEE80211_VHT */
25891a1e1d21SSam Leffler 	};
2590a3e08d6fSRui Paulo 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
25911a1e1d21SSam Leffler 		ieeerates[IFM_SUBTYPE(mword)] : 0;
25921a1e1d21SSam Leffler }
25935b16c28cSSam Leffler 
25945b16c28cSSam Leffler /*
25955b16c28cSSam Leffler  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
25965b16c28cSSam Leffler  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
25975b16c28cSSam Leffler  */
25985b16c28cSSam Leffler #define	mix(a, b, c)							\
25995b16c28cSSam Leffler do {									\
26005b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 13);					\
26015b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 8);					\
26025b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 13);					\
26035b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 12);					\
26045b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 16);					\
26055b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 5);					\
26065b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 3);					\
26075b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 10);					\
26085b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 15);					\
26095b16c28cSSam Leffler } while (/*CONSTCOND*/0)
26105b16c28cSSam Leffler 
26115b16c28cSSam Leffler uint32_t
26125b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic,
26135b16c28cSSam Leffler 	const uint8_t addr[IEEE80211_ADDR_LEN])
26145b16c28cSSam Leffler {
26155b16c28cSSam Leffler 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
26165b16c28cSSam Leffler 
26175b16c28cSSam Leffler 	b += addr[5] << 8;
26185b16c28cSSam Leffler 	b += addr[4];
26195b16c28cSSam Leffler 	a += addr[3] << 24;
26205b16c28cSSam Leffler 	a += addr[2] << 16;
26215b16c28cSSam Leffler 	a += addr[1] << 8;
26225b16c28cSSam Leffler 	a += addr[0];
26235b16c28cSSam Leffler 
26245b16c28cSSam Leffler 	mix(a, b, c);
26255b16c28cSSam Leffler 
26265b16c28cSSam Leffler 	return c;
26275b16c28cSSam Leffler }
26285b16c28cSSam Leffler #undef mix
2629a1cbd043SAdrian Chadd 
2630a1cbd043SAdrian Chadd char
2631a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c)
2632a1cbd043SAdrian Chadd {
2633a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ST(c))
2634a1cbd043SAdrian Chadd 		return 'S';
2635a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108A(c))
2636a1cbd043SAdrian Chadd 		return 'T';
2637a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108G(c))
2638a1cbd043SAdrian Chadd 		return 'G';
26397aebd3e5SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
26407aebd3e5SAdrian Chadd 		return 'v';
2641a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(c))
2642a1cbd043SAdrian Chadd 		return 'n';
2643a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_A(c))
2644a1cbd043SAdrian Chadd 		return 'a';
2645a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ANYG(c))
2646a1cbd043SAdrian Chadd 		return 'g';
2647a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_B(c))
2648a1cbd043SAdrian Chadd 		return 'b';
2649a1cbd043SAdrian Chadd 	return 'f';
2650a1cbd043SAdrian Chadd }
2651