xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 2ac8a2189ac6707f48f77ef2e36baf696a0d2f40)
16b4cac81SBjoern A. Zeeb /*-
28ac540d3SBjoern A. Zeeb  * Copyright (c) 2020-2023 The FreeBSD Foundation
3086be6a8SBjoern A. Zeeb  * Copyright (c) 2020-2022 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
426b4cac81SBjoern A. Zeeb #include <sys/param.h>
436b4cac81SBjoern A. Zeeb #include <sys/types.h>
446b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
456b4cac81SBjoern A. Zeeb #include <sys/errno.h>
466b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
476b4cac81SBjoern A. Zeeb #include <sys/module.h>
486b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
496b4cac81SBjoern A. Zeeb #include <sys/socket.h>
506b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
516b4cac81SBjoern A. Zeeb #include <sys/queue.h>
526b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
53527687a9SBjoern A. Zeeb #include <sys/libkern.h>
546b4cac81SBjoern A. Zeeb 
556b4cac81SBjoern A. Zeeb #include <net/if.h>
566b4cac81SBjoern A. Zeeb #include <net/if_var.h>
576b4cac81SBjoern A. Zeeb #include <net/if_media.h>
586b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
596b4cac81SBjoern A. Zeeb 
606b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
616b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
626b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
636b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
649fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
656b4cac81SBjoern A. Zeeb 
666b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
676b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
686b4cac81SBjoern A. Zeeb 
696b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
706b4cac81SBjoern A. Zeeb #include "linux_80211.h"
716b4cac81SBjoern A. Zeeb 
724a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
73b35f6cd0SBjoern A. Zeeb /* #define	LKPI_80211_HW_CRYPTO */
749fb91463SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
759fb91463SBjoern A. Zeeb /* #define	LKPI_80211_HT */
769fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
779fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
789fb91463SBjoern A. Zeeb #endif
79b35f6cd0SBjoern A. Zeeb 
806b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
816b4cac81SBjoern A. Zeeb 
825a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
835a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
845a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
855a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
865a9a0d78SBjoern A. Zeeb } while (0)
875a9a0d78SBjoern A. Zeeb 
886b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
896b4cac81SBjoern A. Zeeb 
909d9ba2b7SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
919d9ba2b7SBjoern A. Zeeb int linuxkpi_debug_80211;
926b4cac81SBjoern A. Zeeb 
936b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
949d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
959d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
969d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
979d9ba2b7SBjoern A. Zeeb 
989d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
999d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1009d9ba2b7SBjoern A. Zeeb 
1019d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1026b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1039d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1046b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1056b4cac81SBjoern A. Zeeb #else
1066b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1076b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1086b4cac81SBjoern A. Zeeb #endif
1096b4cac81SBjoern A. Zeeb 
1106b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1116b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1126b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1136b4cac81SBjoern A. Zeeb #endif
1146b4cac81SBjoern A. Zeeb 
1156b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1166b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1176b4cac81SBjoern A. Zeeb 
118b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
119b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
120b0f73768SBjoern A. Zeeb 
121fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
122fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1234f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1244f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1254f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1264f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1274f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1284f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1294f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1304f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1314f61ef8bSBjoern A. Zeeb #if 0
1324f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1334f61ef8bSBjoern A. Zeeb #endif
1344f61ef8bSBjoern A. Zeeb };
1354f61ef8bSBjoern A. Zeeb 
1366b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1376b4cac81SBjoern A. Zeeb 	/*
1386b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1396b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1406b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1416b4cac81SBjoern A. Zeeb 	 */
1426b4cac81SBjoern A. Zeeb };
1436b4cac81SBjoern A. Zeeb 
1446b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1456b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
1466b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
1476b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1484a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1494a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1504a67f1dfSBjoern A. Zeeb #endif
1516b4cac81SBjoern A. Zeeb 
1529fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
1539fb91463SBjoern A. Zeeb static void
1549fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
1559fb91463SBjoern A. Zeeb {
1569fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
1579fb91463SBjoern A. Zeeb 	uint8_t *ie;
1589fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
1599fb91463SBjoern A. Zeeb 	int i, rx_nss;
1609fb91463SBjoern A. Zeeb 
1619fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
1629fb91463SBjoern A. Zeeb 		return;
1639fb91463SBjoern A. Zeeb 
1649fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
1659fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1669fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
1679fb91463SBjoern A. Zeeb 
1689fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
1699fb91463SBjoern A. Zeeb 
1709fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
1719fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
1729fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1739fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
1749fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
1759fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1769fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
1779fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
1789fb91463SBjoern A. Zeeb 
1799fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
1809fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
1819fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1829fb91463SBjoern A. Zeeb 		ie += 4;
1839fb91463SBjoern A. Zeeb 	ie += 2;
1849fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
1859fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
1869fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
1879fb91463SBjoern A. Zeeb 
1889fb91463SBjoern A. Zeeb 	rx_nss = 0;
1899fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
1909fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
1919fb91463SBjoern A. Zeeb 			rx_nss++;
1929fb91463SBjoern A. Zeeb 	}
1939fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
1949fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
1959fb91463SBjoern A. Zeeb 
1969fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
1979fb91463SBjoern A. Zeeb }
1989fb91463SBjoern A. Zeeb #endif
1999fb91463SBjoern A. Zeeb 
2009fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
2019fb91463SBjoern A. Zeeb static void
2029fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
2039fb91463SBjoern A. Zeeb {
2049fb91463SBjoern A. Zeeb 
2059fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
2069fb91463SBjoern A. Zeeb 		return;
2079fb91463SBjoern A. Zeeb 
2089fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
2099fb91463SBjoern A. Zeeb #ifdef __notyet__
2109fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
2119fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
2129fb91463SBjoern A. Zeeb 		} else
2139fb91463SBjoern A. Zeeb #endif
2149fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2159fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
2169fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2179fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
2189fb91463SBjoern A. Zeeb 	}
2199fb91463SBjoern A. Zeeb 
2209fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
2219fb91463SBjoern A. Zeeb 	return;
2229fb91463SBjoern A. Zeeb }
2239fb91463SBjoern A. Zeeb #endif
2249fb91463SBjoern A. Zeeb 
225d9f59799SBjoern A. Zeeb static void
22667674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
22767674c1cSBjoern A. Zeeb     const char *_f, int _l)
228d9f59799SBjoern A. Zeeb {
229d9f59799SBjoern A. Zeeb 
2309d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2319d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
232d9f59799SBjoern A. Zeeb 		return;
233d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
234d9f59799SBjoern A. Zeeb 		return;
235d9f59799SBjoern A. Zeeb 
236d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
23767674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
23867674c1cSBjoern A. Zeeb 	if (ni != NULL)
23967674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
240d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
241d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
242d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
2439d9ba2b7SBjoern A. Zeeb #endif
244d9f59799SBjoern A. Zeeb }
245d9f59799SBjoern A. Zeeb 
246d9f59799SBjoern A. Zeeb static void
247d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
248d9f59799SBjoern A. Zeeb {
249d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
250d9f59799SBjoern A. Zeeb 
2513689f8aeSColin Percival 	IMPROVE("XXX-BZ remove tqe_prev check once ni-sta-state-sync is fixed");
2523689f8aeSColin Percival 
253d9f59799SBjoern A. Zeeb 	ni = lsta->ni;
254d9f59799SBjoern A. Zeeb 
255d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2563689f8aeSColin Percival 	if (lsta->lsta_entry.tqe_prev != NULL)
257d9f59799SBjoern A. Zeeb 		TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
258d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
259d9f59799SBjoern A. Zeeb 
260d9f59799SBjoern A. Zeeb 	lsta->ni = NULL;
261d9f59799SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
262e24e8103SBjoern A. Zeeb 	if (ni != NULL)
263d9f59799SBjoern A. Zeeb 		ieee80211_free_node(ni);
264d9f59799SBjoern A. Zeeb 
265e24e8103SBjoern A. Zeeb 	IMPROVE("more from lkpi_ic_node_free() should happen here.");
266e24e8103SBjoern A. Zeeb 
267e24e8103SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
268d9f59799SBjoern A. Zeeb }
269d9f59799SBjoern A. Zeeb 
2704f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
2714f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
2724f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
2734f61ef8bSBjoern A. Zeeb {
2744f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
2754f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
2764f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
2774f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
278b6b352e4SBjoern A. Zeeb 	int band, i, tid;
2799fb91463SBjoern A. Zeeb 	int ht_rx_nss;
2809fb91463SBjoern A. Zeeb 	int vht_rx_nss;
2814f61ef8bSBjoern A. Zeeb 
2824f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2834f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
2844f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2854f61ef8bSBjoern A. Zeeb 		return (NULL);
2864f61ef8bSBjoern A. Zeeb 
2874f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2884f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2894f61ef8bSBjoern A. Zeeb #if 0
2904f61ef8bSBjoern A. Zeeb 	/*
2914f61ef8bSBjoern A. Zeeb 	 * This needs to be done in node_init() as ieee80211_alloc_node()
2924f61ef8bSBjoern A. Zeeb 	 * will initialise the refcount after us.
2934f61ef8bSBjoern A. Zeeb 	 */
2944f61ef8bSBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
2954f61ef8bSBjoern A. Zeeb #endif
2964f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2974f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2984f61ef8bSBjoern A. Zeeb 
2994f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3004f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3014f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3024f61ef8bSBjoern A. Zeeb 
3034f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
304b6b352e4SBjoern A. Zeeb 
305b6b352e4SBjoern A. Zeeb 	/* TXQ */
3064f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
3074f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
3084f61ef8bSBjoern A. Zeeb 
309d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
3104f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
3114f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
3124f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
3134f61ef8bSBjoern A. Zeeb 			goto cleanup;
3144f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
3154f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
316d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
317d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
318d0d29110SBjoern A. Zeeb 				continue;
319d0d29110SBjoern A. Zeeb 			}
320d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
3214f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
3224f61ef8bSBjoern A. Zeeb 		} else {
323fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
3244f61ef8bSBjoern A. Zeeb 		}
325d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
3260cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
327d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
3284f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
3294f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
3300cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
331d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
332eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
3334f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
3344f61ef8bSBjoern A. Zeeb 	}
3354f61ef8bSBjoern A. Zeeb 
336b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
337b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
338b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
339b6b352e4SBjoern A. Zeeb 
340b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
341b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
342b6b352e4SBjoern A. Zeeb 			continue;
343b6b352e4SBjoern A. Zeeb 
344b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
345b6b352e4SBjoern A. Zeeb 
346b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
347b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
348b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
349b6b352e4SBjoern A. Zeeb 		}
350b6b352e4SBjoern A. Zeeb 	}
3519fb91463SBjoern A. Zeeb 
3526ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
3539fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
3549fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = 0;
3559fb91463SBjoern A. Zeeb 
3569fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
3579fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3589fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
3599fb91463SBjoern A. Zeeb #endif
3609fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
3619fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
3629fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
3639fb91463SBjoern A. Zeeb #endif
3649fb91463SBjoern A. Zeeb 
3659fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
3669fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
3679fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
368b6b352e4SBjoern A. Zeeb 
3696ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
3706ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
3716ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
3726ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
3736ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
3746ffb7bd4SBjoern A. Zeeb 	}
3756ffb7bd4SBjoern A. Zeeb 
3764f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
3774f61ef8bSBjoern A. Zeeb 	mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
3784f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
3794f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
3804f61ef8bSBjoern A. Zeeb 
3814f61ef8bSBjoern A. Zeeb 	return (lsta);
3824f61ef8bSBjoern A. Zeeb 
3834f61ef8bSBjoern A. Zeeb cleanup:
384eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
385eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
386eac3646fSBjoern A. Zeeb 
387eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
388eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
3894f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
390eac3646fSBjoern A. Zeeb 	}
3914f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
3924f61ef8bSBjoern A. Zeeb 	return (NULL);
3934f61ef8bSBjoern A. Zeeb }
3944f61ef8bSBjoern A. Zeeb 
3956b4cac81SBjoern A. Zeeb static enum nl80211_band
3966b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
3976b4cac81SBjoern A. Zeeb {
3986b4cac81SBjoern A. Zeeb 
3996b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
4006b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
4016b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
4026b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
4036b4cac81SBjoern A. Zeeb #ifdef __notyet__
4046b4cac81SBjoern A. Zeeb 	else if ()
4056b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
4066b4cac81SBjoern A. Zeeb 	else if ()
4076b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
4086b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
4096b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
4106b4cac81SBjoern A. Zeeb #endif
4116b4cac81SBjoern A. Zeeb 	else
4126b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
4136b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
4146b4cac81SBjoern A. Zeeb }
4156b4cac81SBjoern A. Zeeb 
4166b4cac81SBjoern A. Zeeb static uint32_t
4176b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
4186b4cac81SBjoern A. Zeeb {
4196b4cac81SBjoern A. Zeeb 
4206b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
4216b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
4226b4cac81SBjoern A. Zeeb 	switch (band) {
4236b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
4246b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
4256b4cac81SBjoern A. Zeeb 		break;
4266b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
4276b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
4286b4cac81SBjoern A. Zeeb 		break;
4296b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
4306b4cac81SBjoern A. Zeeb 		break;
4316b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
4326b4cac81SBjoern A. Zeeb 		break;
4336b4cac81SBjoern A. Zeeb 	default:
4346b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
4356b4cac81SBjoern A. Zeeb 		break;
4366b4cac81SBjoern A. Zeeb 	}
4376b4cac81SBjoern A. Zeeb 
4386b4cac81SBjoern A. Zeeb 	IMPROVE();
4396b4cac81SBjoern A. Zeeb 	return (0x00);
4406b4cac81SBjoern A. Zeeb }
4416b4cac81SBjoern A. Zeeb 
442e3a0b120SBjoern A. Zeeb #if 0
4436b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
4446b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
4456b4cac81SBjoern A. Zeeb {
4466b4cac81SBjoern A. Zeeb 
4476b4cac81SBjoern A. Zeeb 	switch (ac) {
4486b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
4496b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
4506b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
4516b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
4526b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
4536b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
4546b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
4556b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
4566b4cac81SBjoern A. Zeeb 	default:
4576b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
4586b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
4596b4cac81SBjoern A. Zeeb 	}
4606b4cac81SBjoern A. Zeeb }
461e3a0b120SBjoern A. Zeeb #endif
4626b4cac81SBjoern A. Zeeb 
4636b4cac81SBjoern A. Zeeb static enum nl80211_iftype
4646b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
4656b4cac81SBjoern A. Zeeb {
4666b4cac81SBjoern A. Zeeb 
4676b4cac81SBjoern A. Zeeb 	switch (opmode) {
4686b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
4696b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
4706b4cac81SBjoern A. Zeeb 		break;
4716b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
4726b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
4736b4cac81SBjoern A. Zeeb 		break;
4746b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
4756b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
4766b4cac81SBjoern A. Zeeb 		break;
4776b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
4786b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
4796b4cac81SBjoern A. Zeeb 		break;
4806b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
4816b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
4826b4cac81SBjoern A. Zeeb 		break;
4836b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
4846b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
4856b4cac81SBjoern A. Zeeb 		break;
4866b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
4876b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
4886b4cac81SBjoern A. Zeeb 	default:
4896b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
4906b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
4916b4cac81SBjoern A. Zeeb 	}
4926b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
4936b4cac81SBjoern A. Zeeb }
4946b4cac81SBjoern A. Zeeb 
495b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
4966b4cac81SBjoern A. Zeeb static uint32_t
4976b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
4986b4cac81SBjoern A. Zeeb {
4996b4cac81SBjoern A. Zeeb 
5006b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
5016b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
5026b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5036b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5046b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
5056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
5066b4cac81SBjoern A. Zeeb 		return (IEEE80211_CIPHER_AES_CCM);
5076b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
5086b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5096b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
5106b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
5116b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
5126b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
5136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5146b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5156b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5166b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
5176b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5186b4cac81SBjoern A. Zeeb 		break;
5196b4cac81SBjoern A. Zeeb 	default:
5206b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
5216b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5226b4cac81SBjoern A. Zeeb 	}
5236b4cac81SBjoern A. Zeeb 
5246b4cac81SBjoern A. Zeeb 	return (0);
5256b4cac81SBjoern A. Zeeb }
5266b4cac81SBjoern A. Zeeb 
5276b4cac81SBjoern A. Zeeb static uint32_t
5286b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
5296b4cac81SBjoern A. Zeeb {
5306b4cac81SBjoern A. Zeeb 
5316b4cac81SBjoern A. Zeeb 	switch (cipher) {
5326b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
5336b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
5346b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
5356b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
5366b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
5376b4cac81SBjoern A. Zeeb 		if (keylen < 8)
5386b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
5396b4cac81SBjoern A. Zeeb 		else
5406b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
5416b4cac81SBjoern A. Zeeb 		break;
5426b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
5436b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
5446b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
5456b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
5466b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
5476b4cac81SBjoern A. Zeeb 		break;
5486b4cac81SBjoern A. Zeeb 	default:
5496b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
5506b4cac81SBjoern A. Zeeb 	};
5516b4cac81SBjoern A. Zeeb 	return (0);
5526b4cac81SBjoern A. Zeeb }
5536b4cac81SBjoern A. Zeeb #endif
5546b4cac81SBjoern A. Zeeb 
5556b4cac81SBjoern A. Zeeb #ifdef __notyet__
5566b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
5576b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
5586b4cac81SBjoern A. Zeeb {
5596b4cac81SBjoern A. Zeeb 
5606b4cac81SBjoern A. Zeeb 	/*
5616b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
5626b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
5636b4cac81SBjoern A. Zeeb 	 */
5646b4cac81SBjoern A. Zeeb 	switch (state) {
5656b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
5666b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
5676b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
5686b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
5696b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
5706b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
5716b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
5726b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
5736b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
5746b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
5756b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
5766b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
5776b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
5786b4cac81SBjoern A. Zeeb 	default:
5796b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
5806b4cac81SBjoern A. Zeeb 	};
5816b4cac81SBjoern A. Zeeb 
5826b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
5836b4cac81SBjoern A. Zeeb }
5846b4cac81SBjoern A. Zeeb #endif
5856b4cac81SBjoern A. Zeeb 
5866b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
5876b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
5886b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
5896b4cac81SBjoern A. Zeeb {
5906b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5916b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
5926b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
5936b4cac81SBjoern A. Zeeb 	int i, nchans;
5946b4cac81SBjoern A. Zeeb 
5956b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5966b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
5976b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
5986b4cac81SBjoern A. Zeeb 		return (NULL);
5996b4cac81SBjoern A. Zeeb 
6006b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
6016b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
6026b4cac81SBjoern A. Zeeb 		return (NULL);
6036b4cac81SBjoern A. Zeeb 
6046b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
6056b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
6066b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
6076b4cac81SBjoern A. Zeeb 			return (&channels[i]);
6086b4cac81SBjoern A. Zeeb 	}
6096b4cac81SBjoern A. Zeeb 
6106b4cac81SBjoern A. Zeeb 	return (NULL);
6116b4cac81SBjoern A. Zeeb }
6126b4cac81SBjoern A. Zeeb 
613*2ac8a218SBjoern A. Zeeb #if 0
6146b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6156b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
6166b4cac81SBjoern A. Zeeb {
6176b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6186b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
6196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6206b4cac81SBjoern A. Zeeb 
6216b4cac81SBjoern A. Zeeb 	chan = NULL;
6226b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
6236b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
6246b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
6256b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
6266b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
6276b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
6286b4cac81SBjoern A. Zeeb 	else
6296b4cac81SBjoern A. Zeeb 		c = NULL;
6306b4cac81SBjoern A. Zeeb 
6316b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6326b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
6336b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
6346b4cac81SBjoern A. Zeeb 	}
6356b4cac81SBjoern A. Zeeb 
6366b4cac81SBjoern A. Zeeb 	return (chan);
6376b4cac81SBjoern A. Zeeb }
638*2ac8a218SBjoern A. Zeeb #endif
6396b4cac81SBjoern A. Zeeb 
6402e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
6412e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
6422e183d99SBjoern A. Zeeb {
6432e183d99SBjoern A. Zeeb 	enum nl80211_band band;
6442e183d99SBjoern A. Zeeb 
6452e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6462e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6472e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6482e183d99SBjoern A. Zeeb 		int i;
6492e183d99SBjoern A. Zeeb 
6502e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
6512e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6522e183d99SBjoern A. Zeeb 			continue;
6532e183d99SBjoern A. Zeeb 
6542e183d99SBjoern A. Zeeb 		channels = supband->channels;
6552e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6562e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
6572e183d99SBjoern A. Zeeb 				return (&channels[i]);
6582e183d99SBjoern A. Zeeb 		}
6592e183d99SBjoern A. Zeeb 	}
6602e183d99SBjoern A. Zeeb 
6612e183d99SBjoern A. Zeeb 	return (NULL);
6622e183d99SBjoern A. Zeeb }
6632e183d99SBjoern A. Zeeb 
664b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
6656b4cac81SBjoern A. Zeeb static int
6666b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
6676b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
6686b4cac81SBjoern A. Zeeb {
6696b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
6706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6716b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6726b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6736b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
6746b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
6756b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
6766b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
6776b4cac81SBjoern A. Zeeb 	int error;
6786b4cac81SBjoern A. Zeeb 
6796b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
6806b4cac81SBjoern A. Zeeb 
6816b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
6826b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
6836b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6846b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
6856b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
6866b4cac81SBjoern A. Zeeb 
6876b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
6886b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
6896b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
6906b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
6916b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
6926b4cac81SBjoern A. Zeeb #if 0
6936b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
6946b4cac81SBjoern A. Zeeb #endif
6956b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
6966b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
6976b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
6986b4cac81SBjoern A. Zeeb 
6996b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
7006b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
7016b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
7026b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
7036b4cac81SBjoern A. Zeeb 		break;
7046b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7056b4cac81SBjoern A. Zeeb 	default:
7066b4cac81SBjoern A. Zeeb 		IMPROVE();
7076b4cac81SBjoern A. Zeeb 		return (0);
7086b4cac81SBjoern A. Zeeb 	};
7096b4cac81SBjoern A. Zeeb 
7106b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
7116b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
7126b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
7136b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
7146b4cac81SBjoern A. Zeeb 
7156b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
7166b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
7176b4cac81SBjoern A. Zeeb 	}
7186b4cac81SBjoern A. Zeeb 
7196b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
7206b4cac81SBjoern A. Zeeb 	if (error != 0) {
7216b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
7226b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
7236b4cac81SBjoern A. Zeeb 		return (0);
7246b4cac81SBjoern A. Zeeb 	} else {
7256b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
7266b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
7276b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
7286b4cac81SBjoern A. Zeeb 		return (1);
7296b4cac81SBjoern A. Zeeb 	}
7306b4cac81SBjoern A. Zeeb }
7316b4cac81SBjoern A. Zeeb 
7326b4cac81SBjoern A. Zeeb static int
7336b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
7346b4cac81SBjoern A. Zeeb {
7356b4cac81SBjoern A. Zeeb 
7366b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
7376b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
7386b4cac81SBjoern A. Zeeb }
7396b4cac81SBjoern A. Zeeb static  int
7406b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
7416b4cac81SBjoern A. Zeeb {
7426b4cac81SBjoern A. Zeeb 
7436b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
7446b4cac81SBjoern A. Zeeb }
7456b4cac81SBjoern A. Zeeb #endif
7466b4cac81SBjoern A. Zeeb 
7476b4cac81SBjoern A. Zeeb static u_int
7486b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
7496b4cac81SBjoern A. Zeeb {
7506b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
7516b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7526b4cac81SBjoern A. Zeeb 
7536b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
7546b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
7556b4cac81SBjoern A. Zeeb 
7566b4cac81SBjoern A. Zeeb 	mc_list = arg;
7576b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
7586b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
7596b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
7606b4cac81SBjoern A. Zeeb 			return (0);
7616b4cac81SBjoern A. Zeeb 	}
7626b4cac81SBjoern A. Zeeb 
7636b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
7646b4cac81SBjoern A. Zeeb 	if (addr == NULL)
7656b4cac81SBjoern A. Zeeb 		return (0);
7666b4cac81SBjoern A. Zeeb 
7676b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
7686b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
7696b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
7706b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
7716b4cac81SBjoern A. Zeeb 	mc_list->count++;
7726b4cac81SBjoern A. Zeeb 
7739d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7749d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
7756b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
7766b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
7779d9ba2b7SBjoern A. Zeeb #endif
7786b4cac81SBjoern A. Zeeb 
7796b4cac81SBjoern A. Zeeb 	return (1);
7806b4cac81SBjoern A. Zeeb }
7816b4cac81SBjoern A. Zeeb 
7826b4cac81SBjoern A. Zeeb static void
7836b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
7846b4cac81SBjoern A. Zeeb {
7856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7866b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7876b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
7886b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
7896b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7906b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
7916b4cac81SBjoern A. Zeeb 	u64 mc;
7926b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
7936b4cac81SBjoern A. Zeeb 
7946b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
7956b4cac81SBjoern A. Zeeb 
7966b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
7976b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
7986b4cac81SBjoern A. Zeeb 		return;
7996b4cac81SBjoern A. Zeeb 
8006b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
8016b4cac81SBjoern A. Zeeb 		return;
8026b4cac81SBjoern A. Zeeb 
8036b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
8046b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
8056b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
8066b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
8076b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8086b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
8096b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
8106b4cac81SBjoern A. Zeeb 	} else {
8116b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
8126b4cac81SBjoern A. Zeeb 	}
8136b4cac81SBjoern A. Zeeb 
8146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8156b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
8166b4cac81SBjoern A. Zeeb 	/*
8176b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
8186b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
8196b4cac81SBjoern A. Zeeb 	 */
8206b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
8216b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
8226b4cac81SBjoern A. Zeeb 
8239d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8249d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8256b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
8266b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
8279d9ba2b7SBjoern A. Zeeb #endif
8286b4cac81SBjoern A. Zeeb 
8296b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
8306b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
8316b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
8326b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
8336b4cac81SBjoern A. Zeeb 			mc_list.count--;
8346b4cac81SBjoern A. Zeeb 		}
8356b4cac81SBjoern A. Zeeb 	}
8366b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
8376b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
8386b4cac81SBjoern A. Zeeb }
8396b4cac81SBjoern A. Zeeb 
840fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
841fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
842fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
843fa8f007dSBjoern A. Zeeb {
844fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
845fa8f007dSBjoern A. Zeeb 
846fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
847fa8f007dSBjoern A. Zeeb 
8489d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8499d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
850fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
851fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
852fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
853fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
854616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
855fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
856fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
857fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
858fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
859fa8f007dSBjoern A. Zeeb 			bss_changed);
8609d9ba2b7SBjoern A. Zeeb #endif
861fa8f007dSBjoern A. Zeeb 
862fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
863fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
864fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
865fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
866fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
867fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
868fa8f007dSBjoern A. Zeeb 	}
869fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
870fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
871fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
872fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
873fa8f007dSBjoern A. Zeeb 	}
874fa8f007dSBjoern A. Zeeb 
875fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
876fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
877fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
878fa8f007dSBjoern A. Zeeb 
8799d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
881fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
882fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
883fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
884fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
885616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
886fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
887fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
888fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
889fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
890fa8f007dSBjoern A. Zeeb 			bss_changed);
8919d9ba2b7SBjoern A. Zeeb #endif
892fa8f007dSBjoern A. Zeeb 
893fa8f007dSBjoern A. Zeeb 	return (bss_changed);
894fa8f007dSBjoern A. Zeeb }
895fa8f007dSBjoern A. Zeeb 
8966b4cac81SBjoern A. Zeeb static void
8976b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
8986b4cac81SBjoern A. Zeeb {
8996b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9006b4cac81SBjoern A. Zeeb 	int error;
9018ac540d3SBjoern A. Zeeb 	bool cancel;
9026b4cac81SBjoern A. Zeeb 
9038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9048ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9068ac540d3SBjoern A. Zeeb 	if (!cancel)
9076b4cac81SBjoern A. Zeeb 		return;
9086b4cac81SBjoern A. Zeeb 
9096b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9106b4cac81SBjoern A. Zeeb 
911bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
912bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
9136b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
9146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
9158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
9166b4cac81SBjoern A. Zeeb 
9176b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
9188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9198ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
9208ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
9218ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9238ac540d3SBjoern A. Zeeb 
924bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
9256b4cac81SBjoern A. Zeeb 
9268ac540d3SBjoern A. Zeeb 	if (cancel)
9276b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
9286b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
9296b4cac81SBjoern A. Zeeb }
9306b4cac81SBjoern A. Zeeb 
9316b4cac81SBjoern A. Zeeb static void
932086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
933086be6a8SBjoern A. Zeeb {
934086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
935086be6a8SBjoern A. Zeeb 	int error;
936086be6a8SBjoern A. Zeeb 	bool old;
937086be6a8SBjoern A. Zeeb 
938086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
939086be6a8SBjoern A. Zeeb 	if (old == new)
940086be6a8SBjoern A. Zeeb 		return;
941086be6a8SBjoern A. Zeeb 
942086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
943086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
944086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
945086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
946086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
947086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
948086be6a8SBjoern A. Zeeb 	}
949086be6a8SBjoern A. Zeeb }
950086be6a8SBjoern A. Zeeb 
951086be6a8SBjoern A. Zeeb static void
9526b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9536b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
9546b4cac81SBjoern A. Zeeb {
9556b4cac81SBjoern A. Zeeb 	sta->aid = 0;
956616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
9576b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
9586b4cac81SBjoern A. Zeeb 		enum ieee80211_bss_changed changed;
9596b4cac81SBjoern A. Zeeb 
9606b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
9616b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
9626b4cac81SBjoern A. Zeeb 
9636b4cac81SBjoern A. Zeeb 		changed = 0;
964616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
965616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
9666b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
967d9f59799SBjoern A. Zeeb 		/*
968d9f59799SBjoern A. Zeeb 		 * This will remove the sta from firmware for iwlwifi.
969d9f59799SBjoern A. Zeeb 		 * So confusing that they use state and flags and ... ^%$%#%$^.
970d9f59799SBjoern A. Zeeb 		 */
9716b4cac81SBjoern A. Zeeb 		IMPROVE();
9726b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
9736b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
9746b4cac81SBjoern A. Zeeb 		    changed);
975086be6a8SBjoern A. Zeeb 
976086be6a8SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
9776b4cac81SBjoern A. Zeeb 	}
9786b4cac81SBjoern A. Zeeb }
9796b4cac81SBjoern A. Zeeb 
9806b4cac81SBjoern A. Zeeb static void
9816b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
9826b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
9836b4cac81SBjoern A. Zeeb {
9846b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
9856b4cac81SBjoern A. Zeeb 	int tid;
986eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
9876b4cac81SBjoern A. Zeeb 
9886b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
9896b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
9906b4cac81SBjoern A. Zeeb 
9916b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
9926b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
9936b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
9946b4cac81SBjoern A. Zeeb 				continue;
9956b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
9966b4cac81SBjoern A. Zeeb 			continue;
9976b4cac81SBjoern A. Zeeb 
9986b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
9996b4cac81SBjoern A. Zeeb 			continue;
10006b4cac81SBjoern A. Zeeb 
10016b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10026b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
10036b4cac81SBjoern A. Zeeb 			continue;
10046b4cac81SBjoern A. Zeeb 
1005eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1006eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1007eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1008eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
10096b4cac81SBjoern A. Zeeb 			continue;
10106b4cac81SBjoern A. Zeeb 
10116b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
10126b4cac81SBjoern A. Zeeb 	}
10136b4cac81SBjoern A. Zeeb }
10146b4cac81SBjoern A. Zeeb 
10156b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
10166b4cac81SBjoern A. Zeeb 
10176b4cac81SBjoern A. Zeeb static int
10186b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10196b4cac81SBjoern A. Zeeb {
10206b4cac81SBjoern A. Zeeb 
10216b4cac81SBjoern A. Zeeb 	return (0);
10226b4cac81SBjoern A. Zeeb }
10236b4cac81SBjoern A. Zeeb 
10246b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
10256b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
10266b4cac81SBjoern A. Zeeb 
10276b4cac81SBjoern A. Zeeb static int
10286b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10296b4cac81SBjoern A. Zeeb {
10306b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1031c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
10326b4cac81SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *conf;
10336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10346b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10356b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10366b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10376b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10386b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10396b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
10406b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10416b4cac81SBjoern A. Zeeb 	uint32_t changed;
10426b4cac81SBjoern A. Zeeb 	int error;
10436b4cac81SBjoern A. Zeeb 
1044*2ac8a218SBjoern A. Zeeb 	/*
1045*2ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
1046*2ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
1047*2ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
1048*2ac8a218SBjoern A. Zeeb 	 */
1049*2ac8a218SBjoern A. Zeeb 
1050*2ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
1051*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
1052*2ac8a218SBjoern A. Zeeb 		return (EINVAL);
1053*2ac8a218SBjoern A. Zeeb 	}
1054*2ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
1055*2ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
1056*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
1057*2ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
1058*2ac8a218SBjoern A. Zeeb 		ieee80211_free_node(ni);
1059*2ac8a218SBjoern A. Zeeb 		return (EINVAL);
10606b4cac81SBjoern A. Zeeb 	}
10616b4cac81SBjoern A. Zeeb 
10626b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
1063*2ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
1064*2ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
1065*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
1066*2ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
1067*2ac8a218SBjoern A. Zeeb 		ieee80211_free_node(ni);
1068*2ac8a218SBjoern A. Zeeb 		return (ESRCH);
1069*2ac8a218SBjoern A. Zeeb 	}
1070*2ac8a218SBjoern A. Zeeb 
10716b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10726b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10736b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10746b4cac81SBjoern A. Zeeb 
10756b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
10768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
10776b4cac81SBjoern A. Zeeb 
10786b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
10796b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
10806b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1081c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
10826b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
10836b4cac81SBjoern A. Zeeb 	} else {
10846b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1085c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
10866b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1087c5e25798SBjoern A. Zeeb 		conf = &lchanctx->conf;
10886b4cac81SBjoern A. Zeeb 	}
10896b4cac81SBjoern A. Zeeb 
10906b4cac81SBjoern A. Zeeb 	conf->rx_chains_dynamic = 1;
10916b4cac81SBjoern A. Zeeb 	conf->rx_chains_static = 1;
10926b4cac81SBjoern A. Zeeb 	conf->radar_enabled =
10936b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
10946b4cac81SBjoern A. Zeeb 	conf->def.chan = chan;
10956b4cac81SBjoern A. Zeeb 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
10966b4cac81SBjoern A. Zeeb 	conf->def.center_freq1 = chan->center_freq;
10976b4cac81SBjoern A. Zeeb 	conf->def.center_freq2 = 0;
10989fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
1099*2ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
1100*2ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
11019fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
11029fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
11039fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
11049fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_40;
11059fb91463SBjoern A. Zeeb 		} else
11069fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_20;
11079fb91463SBjoern A. Zeeb 	}
11089fb91463SBjoern A. Zeeb #endif
11099fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
11109fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
11119fb91463SBjoern A. Zeeb #ifdef __notyet__
11129fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
11139fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_80P80;
11149fb91463SBjoern A. Zeeb 			conf->def.center_freq2 = 0;	/* XXX */
11159fb91463SBjoern A. Zeeb 		} else
11169fb91463SBjoern A. Zeeb #endif
11179fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
11189fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_160;
11199fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
11209fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_80;
11219fb91463SBjoern A. Zeeb 	}
11229fb91463SBjoern A. Zeeb #endif
11236b4cac81SBjoern A. Zeeb 	/* Responder ... */
11246b4cac81SBjoern A. Zeeb 	conf->min_def.chan = chan;
11256b4cac81SBjoern A. Zeeb 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
11266b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq1 = chan->center_freq;
11276b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq2 = 0;
11289fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
11296b4cac81SBjoern A. Zeeb 
11306ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
11316ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
11326ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
11336ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
11346ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
11356ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
11366ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
11376ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
11386ffb7bd4SBjoern A. Zeeb 
11396ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
11406ffb7bd4SBjoern A. Zeeb 
11416ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
11426ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
11436ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
11446ffb7bd4SBjoern A. Zeeb 
11456ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
11466ffb7bd4SBjoern A. Zeeb 
11476b4cac81SBjoern A. Zeeb 	error = 0;
11486b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
11496b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
11506b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
11516b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
11526b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
11536b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
11546b4cac81SBjoern A. Zeeb 	} else {
11556b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, conf);
11566b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
11576b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.chan = conf->def.chan;
11586b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.width = conf->def.width;
11596b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
11606b4cac81SBjoern A. Zeeb 			    conf->def.center_freq1;
11619fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
11629fb91463SBjoern A. Zeeb 			if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) {
11639fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
11649fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
11659fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 += 10;
11669fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
11679fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 -= 10;
11689fb91463SBjoern A. Zeeb 			}
11699fb91463SBjoern A. Zeeb #endif
11706b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
11716b4cac81SBjoern A. Zeeb 			    conf->def.center_freq2;
11726b4cac81SBjoern A. Zeeb 		} else {
1173018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1174018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
11756b4cac81SBjoern A. Zeeb 			goto out;
11766b4cac81SBjoern A. Zeeb 		}
11776ffb7bd4SBjoern A. Zeeb 
11786ffb7bd4SBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = conf;
11796ffb7bd4SBjoern A. Zeeb 
11806b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
11816b4cac81SBjoern A. Zeeb 		if (error == 0)
118268541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
118368541546SBjoern A. Zeeb 			    &vif->bss_conf, conf);
11846b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
11856b4cac81SBjoern A. Zeeb 			error = 0;
11866b4cac81SBjoern A. Zeeb 		if (error != 0) {
1187018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1188018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
11896b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, conf);
1190c5e25798SBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1191c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
11926b4cac81SBjoern A. Zeeb 			goto out;
11936b4cac81SBjoern A. Zeeb 		}
11946b4cac81SBjoern A. Zeeb 	}
11956b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
11966b4cac81SBjoern A. Zeeb 
11976b4cac81SBjoern A. Zeeb 	/* RATES */
11986b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
11996b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
12006b4cac81SBjoern A. Zeeb 
1201d9f59799SBjoern A. Zeeb 	/*
1202d9f59799SBjoern A. Zeeb 	 * This is a bandaid for now.  If we went through (*iv_update_bss)()
1203d9f59799SBjoern A. Zeeb 	 * and then removed the lsta we end up here without a lsta and have
1204d9f59799SBjoern A. Zeeb 	 * to manually allocate and link it in as lkpi_ic_node_alloc()/init()
1205d9f59799SBjoern A. Zeeb 	 * would normally do.
1206d9f59799SBjoern A. Zeeb 	 * XXX-BZ I do not like this but currently we have no good way of
1207d9f59799SBjoern A. Zeeb 	 * intercepting the bss swap and state changes and packets going out
1208d9f59799SBjoern A. Zeeb 	 * workflow so live with this.  It is a compat layer after all.
1209d9f59799SBjoern A. Zeeb 	 */
1210d9f59799SBjoern A. Zeeb 	if (ni->ni_drv_data == NULL) {
1211*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lkpi_lsta_alloc to be called: "
1212*2ac8a218SBjoern A. Zeeb 		    "ni %p lsta %p\n", __func__, __LINE__, ni, ni->ni_drv_data);
1213d9f59799SBjoern A. Zeeb 		lsta = lkpi_lsta_alloc(vap, ni->ni_macaddr, hw, ni);
1214d9f59799SBjoern A. Zeeb 		if (lsta == NULL) {
1215d9f59799SBjoern A. Zeeb 			error = ENOMEM;
1216018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_lsta_alloc "
1217018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1218d9f59799SBjoern A. Zeeb 			goto out;
1219d9f59799SBjoern A. Zeeb 		}
1220d9f59799SBjoern A. Zeeb 		lsta->ni = ieee80211_ref_node(ni);
1221d9f59799SBjoern A. Zeeb 	} else {
12226b4cac81SBjoern A. Zeeb 		lsta = ni->ni_drv_data;
1223d9f59799SBjoern A. Zeeb 	}
1224e24e8103SBjoern A. Zeeb 
1225e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1226*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
1227*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ this should have caught the upper lkpi_lsta_alloc() too! */
1228*2ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL)
1229*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1230*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1231*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1232*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1233*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1234*2ac8a218SBjoern A. Zeeb 
1235*2ac8a218SBjoern A. Zeeb 	/* Reference the ni for this cache of lsta. */
1236*2ac8a218SBjoern A. Zeeb 	ieee80211_ref_node(vap->iv_bss);
1237*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1238*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = true;
1239*2ac8a218SBjoern A. Zeeb 
1240*2ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1241e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1242e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1243e24e8103SBjoern A. Zeeb 
1244d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
12456b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12466b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
12476b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1248e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
12496b4cac81SBjoern A. Zeeb 	if (error != 0) {
12506b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1251018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1252018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
12536b4cac81SBjoern A. Zeeb 		goto out;
12546b4cac81SBjoern A. Zeeb 	}
12556b4cac81SBjoern A. Zeeb #if 0
12566b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
12576b4cac81SBjoern A. Zeeb #endif
12586b4cac81SBjoern A. Zeeb 
12599d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
12609d9ba2b7SBjoern A. Zeeb 
12616b4cac81SBjoern A. Zeeb 	/*
12626b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
12636b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
12646b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
12656b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1266d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1267d9f59799SBjoern A. Zeeb 	 * for all queues.
12686b4cac81SBjoern A. Zeeb 	 */
1269e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
12706b4cac81SBjoern A. Zeeb 
12716b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
12726b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
12736b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
12746b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
12756b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
12766b4cac81SBjoern A. Zeeb 
12776b4cac81SBjoern A. Zeeb 	/*
12786b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
12796b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
12806b4cac81SBjoern A. Zeeb 	 * - event_callback
12816b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
12826b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
12836b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
12846b4cac81SBjoern A. Zeeb 	 */
12856b4cac81SBjoern A. Zeeb 
12866b4cac81SBjoern A. Zeeb out:
12878ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
12886b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
12896b4cac81SBjoern A. Zeeb 	if (ni != NULL)
12906b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
12916b4cac81SBjoern A. Zeeb 	return (error);
12926b4cac81SBjoern A. Zeeb }
12936b4cac81SBjoern A. Zeeb 
12946b4cac81SBjoern A. Zeeb static int
12956b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
12966b4cac81SBjoern A. Zeeb {
12976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12986b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13006b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13016b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13026b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13036b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13046b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
13056b4cac81SBjoern A. Zeeb 	int error;
13066b4cac81SBjoern A. Zeeb 
13076b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13116b4cac81SBjoern A. Zeeb 
1312*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1313*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1314*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
1315*2ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
1316*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1317*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1318*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1319*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1320*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1321*2ac8a218SBjoern A. Zeeb #endif
1322*2ac8a218SBjoern A. Zeeb 
1323*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1324*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1325*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1326*2ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
1327*2ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1328*2ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
13296b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13306b4cac81SBjoern A. Zeeb 
133167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13326b4cac81SBjoern A. Zeeb 
13336b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
13348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
13356b4cac81SBjoern A. Zeeb 
1336d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1337d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1338d9f59799SBjoern A. Zeeb 
13396b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
13406b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
13416b4cac81SBjoern A. Zeeb 
13426b4cac81SBjoern A. Zeeb 	/* flush, no drop */
13436b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
13446b4cac81SBjoern A. Zeeb 
13456b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
13466b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
13476b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13486b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
13496b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
13506b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
13516b4cac81SBjoern A. Zeeb 	}
13526b4cac81SBjoern A. Zeeb 
13536b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
13546b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
13556b4cac81SBjoern A. Zeeb 
13566b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
13576b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1358d9f59799SBjoern A. Zeeb 
1359d9f59799SBjoern A. Zeeb 	/* Take the station down. */
13606b4cac81SBjoern A. Zeeb 
13616b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
13626b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13636b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1364d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1365e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
13666b4cac81SBjoern A. Zeeb 	if (error != 0) {
13676b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1368018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1369018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
13706b4cac81SBjoern A. Zeeb 		goto out;
13716b4cac81SBjoern A. Zeeb 	}
13726b4cac81SBjoern A. Zeeb #if 0
13736b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
13746b4cac81SBjoern A. Zeeb #endif
13756b4cac81SBjoern A. Zeeb 
137667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13776b4cac81SBjoern A. Zeeb 
1378*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1379*2ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
1380*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
1381*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
1382*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1383*2ac8a218SBjoern A. Zeeb 	ieee80211_free_node(ni);		/* was lvif->lvif_bss->ni */
1384*2ac8a218SBjoern A. Zeeb 
1385d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1386d9f59799SBjoern A. Zeeb 
1387d9f59799SBjoern A. Zeeb 	/* conf_tx */
1388d9f59799SBjoern A. Zeeb 
1389d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
13906b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1391c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
13926b4cac81SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
13936b4cac81SBjoern A. Zeeb 
13946b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
13956b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
139668541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
13976b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
13986b4cac81SBjoern A. Zeeb 
13996b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
14006b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1401c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1402c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
14036b4cac81SBjoern A. Zeeb 	}
14046b4cac81SBjoern A. Zeeb 
14056b4cac81SBjoern A. Zeeb out:
14068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
14086b4cac81SBjoern A. Zeeb 	return (error);
14096b4cac81SBjoern A. Zeeb }
14106b4cac81SBjoern A. Zeeb 
14116b4cac81SBjoern A. Zeeb static int
14126b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14136b4cac81SBjoern A. Zeeb {
14146b4cac81SBjoern A. Zeeb 	int error;
14156b4cac81SBjoern A. Zeeb 
14166b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
14176b4cac81SBjoern A. Zeeb 	if (error == 0)
14186b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
14196b4cac81SBjoern A. Zeeb 	return (error);
14206b4cac81SBjoern A. Zeeb }
14216b4cac81SBjoern A. Zeeb 
14226b4cac81SBjoern A. Zeeb static int
14236b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14246b4cac81SBjoern A. Zeeb {
14256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14296b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14306b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14316b4cac81SBjoern A. Zeeb 	int error;
14326b4cac81SBjoern A. Zeeb 
14336b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14356b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14366b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14376b4cac81SBjoern A. Zeeb 
14386b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14398ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1440*2ac8a218SBjoern A. Zeeb 
1441*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1442*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
1443*2ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1444*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1445*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1446*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1447*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1448*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1449*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1450*2ac8a218SBjoern A. Zeeb #endif
1451*2ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
1452*2ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
1453*2ac8a218SBjoern A. Zeeb 		goto out;
1454*2ac8a218SBjoern A. Zeeb 	}
1455*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1456*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1457*2ac8a218SBjoern A. Zeeb 
1458*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
14596b4cac81SBjoern A. Zeeb 
14606b4cac81SBjoern A. Zeeb 	/* Finish auth. */
14616b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
14626b4cac81SBjoern A. Zeeb 
14636b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
14646b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
14656b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1466e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1467018d93ecSBjoern A. Zeeb 	if (error != 0) {
1468018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1469018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
14706b4cac81SBjoern A. Zeeb 		goto out;
1471018d93ecSBjoern A. Zeeb 	}
14726b4cac81SBjoern A. Zeeb 
14736b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
14746b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
14756b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14766b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
14776b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
14786b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
14796b4cac81SBjoern A. Zeeb 	}
14806b4cac81SBjoern A. Zeeb 
14816b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
14826b4cac81SBjoern A. Zeeb 
14836b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
14846b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
14856b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14866b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
14876b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
14886b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
14896b4cac81SBjoern A. Zeeb 	}
14906b4cac81SBjoern A. Zeeb 
14916b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
1492e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), true, true);
14936b4cac81SBjoern A. Zeeb 
14946b4cac81SBjoern A. Zeeb 	/*
14956b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
14966b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
14976b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
14986b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
14996b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
15006b4cac81SBjoern A. Zeeb 	 * - event_callback
15016b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
15026b4cac81SBjoern A. Zeeb 	 */
15036b4cac81SBjoern A. Zeeb 
15046b4cac81SBjoern A. Zeeb out:
15058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15066b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15076b4cac81SBjoern A. Zeeb 	return (error);
15086b4cac81SBjoern A. Zeeb }
15096b4cac81SBjoern A. Zeeb 
15106b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
15116b4cac81SBjoern A. Zeeb static int
15126b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15136b4cac81SBjoern A. Zeeb {
15146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15176b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15196b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1520*2ac8a218SBjoern A. Zeeb 	int error;
15216b4cac81SBjoern A. Zeeb 
15226b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15246b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15256b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15266b4cac81SBjoern A. Zeeb 
15276b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
15288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1529*2ac8a218SBjoern A. Zeeb 
1530*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1531*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
1532*2ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1533*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1534*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1535*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1536*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1537*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1538*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1539*2ac8a218SBjoern A. Zeeb #endif
1540*2ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
1541*2ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
1542*2ac8a218SBjoern A. Zeeb 		goto out;
1543*2ac8a218SBjoern A. Zeeb 	}
1544*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1545*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1546*2ac8a218SBjoern A. Zeeb 
1547*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
1548*2ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
15496b4cac81SBjoern A. Zeeb 
15506b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
15516b4cac81SBjoern A. Zeeb 
15526b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
15536b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
15546b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15556b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
15566b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
15576b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
15586b4cac81SBjoern A. Zeeb 	}
15596b4cac81SBjoern A. Zeeb 
15606b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
15616b4cac81SBjoern A. Zeeb 
15626b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
15636b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
15646b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15656b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
15666b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
15676b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
15686b4cac81SBjoern A. Zeeb 	}
15696b4cac81SBjoern A. Zeeb 
1570*2ac8a218SBjoern A. Zeeb 	error = 0;
1571*2ac8a218SBjoern A. Zeeb out:
15728ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15736b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15746b4cac81SBjoern A. Zeeb 
1575*2ac8a218SBjoern A. Zeeb 	return (error);
15766b4cac81SBjoern A. Zeeb }
15776b4cac81SBjoern A. Zeeb 
15786b4cac81SBjoern A. Zeeb static int
1579d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15806b4cac81SBjoern A. Zeeb {
15816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15826b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15856b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
15866b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15876b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
15886b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1589d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
15906b4cac81SBjoern A. Zeeb 	int error;
15916b4cac81SBjoern A. Zeeb 
15926b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15936b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15946b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15956b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15966b4cac81SBjoern A. Zeeb 
1597*2ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1598*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1599*2ac8a218SBjoern A. Zeeb 
1600*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1601*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1602*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
1603*2ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
1604*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1605*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1606*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1607*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1608*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1609*2ac8a218SBjoern A. Zeeb #endif
1610*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1611*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1612*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1613*2ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
1614*2ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1615*2ac8a218SBjoern A. Zeeb 
1616*2ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
16176b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
16186b4cac81SBjoern A. Zeeb 
161967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1620d9f59799SBjoern A. Zeeb 
1621d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1622d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1623d9f59799SBjoern A. Zeeb 
1624d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1625d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1626d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1627d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1628d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1629d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1630d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1631d9f59799SBjoern A. Zeeb 	}
1632d9f59799SBjoern A. Zeeb 
16338ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1634d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1635d9f59799SBjoern A. Zeeb 
1636d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1637d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1638018d93ecSBjoern A. Zeeb 	if (error != 0) {
1639018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1640018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1641d9f59799SBjoern A. Zeeb 		goto outni;
1642018d93ecSBjoern A. Zeeb 	}
1643d9f59799SBjoern A. Zeeb 
1644d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1646d9f59799SBjoern A. Zeeb 
164767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1648d9f59799SBjoern A. Zeeb 
1649d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1650d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1651d9f59799SBjoern A. Zeeb 
1652d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1653d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1654d9f59799SBjoern A. Zeeb 
16556b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16566b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16576b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16586b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
16596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16606b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16616b4cac81SBjoern A. Zeeb 	}
16626b4cac81SBjoern A. Zeeb 
1663d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1664d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1665d9f59799SBjoern A. Zeeb 
1666d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1667d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1668d9f59799SBjoern A. Zeeb 
1669d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1670d9f59799SBjoern A. Zeeb 
16716b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
16726b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
16736b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
16746b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1675e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1676018d93ecSBjoern A. Zeeb 	if (error != 0) {
1677018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1678018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
16796b4cac81SBjoern A. Zeeb 		goto out;
1680018d93ecSBjoern A. Zeeb 	}
16816b4cac81SBjoern A. Zeeb 
168267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
16836b4cac81SBjoern A. Zeeb 
168416e688b2SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
168516e688b2SBjoern A. Zeeb 	/*
168616e688b2SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
168716e688b2SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
168816e688b2SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
168916e688b2SBjoern A. Zeeb 	 * a fw assert.
169016e688b2SBjoern A. Zeeb 	 */
169116e688b2SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
169216e688b2SBjoern A. Zeeb 
1693d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1694d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1695d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1696d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1697e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1698d9f59799SBjoern A. Zeeb 	if (error != 0) {
1699d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1700018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1701018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
1702d9f59799SBjoern A. Zeeb 		goto out;
1703d9f59799SBjoern A. Zeeb 	}
1704d9f59799SBjoern A. Zeeb 
170516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1706d9f59799SBjoern A. Zeeb 
1707d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1708d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1709d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1710d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1711616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
1712616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1713d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1714d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1715d9f59799SBjoern A. Zeeb 
1716*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1717*2ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
1718*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
1719*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
1720*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1721*2ac8a218SBjoern A. Zeeb 	ieee80211_free_node(ni);		/* was lvif->lvif_bss->ni */
1722*2ac8a218SBjoern A. Zeeb 
1723d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
1724d9f59799SBjoern A. Zeeb 
1725d9f59799SBjoern A. Zeeb 	/* conf_tx */
1726d9f59799SBjoern A. Zeeb 
1727d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1728d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1729c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1730d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1731d9f59799SBjoern A. Zeeb 
1732d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1733d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
173468541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1735d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1736d9f59799SBjoern A. Zeeb 
1737d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1738d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1739c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1740c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
1741d9f59799SBjoern A. Zeeb 	}
1742d9f59799SBjoern A. Zeeb 
1743d9f59799SBjoern A. Zeeb 	error = EALREADY;
17446b4cac81SBjoern A. Zeeb out:
17458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17466b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1747d9f59799SBjoern A. Zeeb outni:
17486b4cac81SBjoern A. Zeeb 	return (error);
17496b4cac81SBjoern A. Zeeb }
17506b4cac81SBjoern A. Zeeb 
17516b4cac81SBjoern A. Zeeb static int
1752d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1753d9f59799SBjoern A. Zeeb {
1754d9f59799SBjoern A. Zeeb 	int error;
1755d9f59799SBjoern A. Zeeb 
1756d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1757d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1758d9f59799SBjoern A. Zeeb 		return (error);
1759d9f59799SBjoern A. Zeeb 
1760d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1761d9f59799SBjoern A. Zeeb 
1762d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1763d9f59799SBjoern A. Zeeb 	return (error);
1764d9f59799SBjoern A. Zeeb }
1765d9f59799SBjoern A. Zeeb 
1766d9f59799SBjoern A. Zeeb static int
17676b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17686b4cac81SBjoern A. Zeeb {
17696b4cac81SBjoern A. Zeeb 	int error;
17706b4cac81SBjoern A. Zeeb 
1771d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
17726b4cac81SBjoern A. Zeeb 	return (error);
17736b4cac81SBjoern A. Zeeb }
17746b4cac81SBjoern A. Zeeb 
17756b4cac81SBjoern A. Zeeb static int
17766b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17776b4cac81SBjoern A. Zeeb {
17786b4cac81SBjoern A. Zeeb 	int error;
17796b4cac81SBjoern A. Zeeb 
1780d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
17816b4cac81SBjoern A. Zeeb 	return (error);
17826b4cac81SBjoern A. Zeeb }
17836b4cac81SBjoern A. Zeeb 
17846b4cac81SBjoern A. Zeeb static int
17856b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17866b4cac81SBjoern A. Zeeb {
17876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17906b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17916b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
17926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
17936b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
17946b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
17956b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
17966b4cac81SBjoern A. Zeeb 	int error;
17976b4cac81SBjoern A. Zeeb 
17986b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17996b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18006b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18016b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
18026b4cac81SBjoern A. Zeeb 
18036b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
18048ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1805*2ac8a218SBjoern A. Zeeb 
1806*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1807*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
1808*2ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1809*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1810*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1811*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1812*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1813*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1814*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1815*2ac8a218SBjoern A. Zeeb #endif
1816*2ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
1817*2ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
1818*2ac8a218SBjoern A. Zeeb 		goto out;
1819*2ac8a218SBjoern A. Zeeb 	}
1820*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1821*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1822*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1823*2ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
1824*2ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1825*2ac8a218SBjoern A. Zeeb 
1826*2ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
18276b4cac81SBjoern A. Zeeb 
18286b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
18296b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
18306b4cac81SBjoern A. Zeeb 
18319597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
18329597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
18339597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
18349597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
18356b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
18369597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
18374a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
18384a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
18394a67f1dfSBjoern A. Zeeb 		sta->wme = true;
18404a67f1dfSBjoern A. Zeeb #endif
1841e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1842018d93ecSBjoern A. Zeeb 	if (error != 0) {
1843018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1844018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18459597f7cbSBjoern A. Zeeb 		goto out;
1846018d93ecSBjoern A. Zeeb 	}
18476b4cac81SBjoern A. Zeeb 
18486b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
18496b4cac81SBjoern A. Zeeb 
18506b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
18516b4cac81SBjoern A. Zeeb 	bss_changed = 0;
18524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
18534a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
18544a67f1dfSBjoern A. Zeeb #endif
1855616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
1856616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
1857616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
18586b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
18596b4cac81SBjoern A. Zeeb 	}
18606b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
1861616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
1862616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
18636b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
18646b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
18656b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
18666b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
18676b4cac81SBjoern A. Zeeb 	}
18686b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
18696b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
18706b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
18716b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
18726b4cac81SBjoern A. Zeeb 	}
18736b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
18746b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
18756b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
18766b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
18776b4cac81SBjoern A. Zeeb 	}
1878fa8f007dSBjoern A. Zeeb 
1879fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1880fa8f007dSBjoern A. Zeeb 
18816b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
18826b4cac81SBjoern A. Zeeb 
18836b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
18846b4cac81SBjoern A. Zeeb 	 * - event_callback
18856b4cac81SBjoern A. Zeeb 	 */
18866b4cac81SBjoern A. Zeeb 
18876b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
18886b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
18896b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18906b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
18916b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
18926b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
18936b4cac81SBjoern A. Zeeb 	}
18946b4cac81SBjoern A. Zeeb 
1895086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1896086be6a8SBjoern A. Zeeb 
18976b4cac81SBjoern A. Zeeb 	/*
18986b4cac81SBjoern A. Zeeb 	 * And then:
18996b4cac81SBjoern A. Zeeb 	 * - (more packets)?
19006b4cac81SBjoern A. Zeeb 	 * - set_key
19016b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
19026b4cac81SBjoern A. Zeeb 	 * - set_key (?)
19036b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
19046b4cac81SBjoern A. Zeeb 	 */
19056b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
19066b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
19076b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
19086b4cac81SBjoern A. Zeeb 
19096b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
19106b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
19116b4cac81SBjoern A. Zeeb 	}
19126b4cac81SBjoern A. Zeeb 
19139fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
19149fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
19159fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
19169fb91463SBjoern A. Zeeb #endif
19179fb91463SBjoern A. Zeeb 
19186b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
19196b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
19206b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
19216b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1922e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
19236b4cac81SBjoern A. Zeeb 	if (error != 0) {
19246b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
1925018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
1926018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19276b4cac81SBjoern A. Zeeb 		goto out;
19286b4cac81SBjoern A. Zeeb 	}
19296b4cac81SBjoern A. Zeeb 
19306b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
19316b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
19326b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
19336b4cac81SBjoern A. Zeeb 	 *
19346b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
19356b4cac81SBjoern A. Zeeb 	 */
19366b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
19376b4cac81SBjoern A. Zeeb 
1938fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1939fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1940fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1941fa8f007dSBjoern A. Zeeb 
19426b4cac81SBjoern A. Zeeb out:
19438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19446b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
19456b4cac81SBjoern A. Zeeb 	return (error);
19466b4cac81SBjoern A. Zeeb }
19476b4cac81SBjoern A. Zeeb 
19486b4cac81SBjoern A. Zeeb static int
19496b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19506b4cac81SBjoern A. Zeeb {
19516b4cac81SBjoern A. Zeeb 	int error;
19526b4cac81SBjoern A. Zeeb 
19536b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
19546b4cac81SBjoern A. Zeeb 	if (error == 0)
19556b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
19566b4cac81SBjoern A. Zeeb 	return (error);
19576b4cac81SBjoern A. Zeeb }
19586b4cac81SBjoern A. Zeeb 
19596b4cac81SBjoern A. Zeeb static int
19606b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19616b4cac81SBjoern A. Zeeb {
19626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19636b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19646b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19656b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19666b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19676b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19686b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
1969d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1970d9f59799SBjoern A. Zeeb #if 0
1971d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1972d9f59799SBjoern A. Zeeb #endif
19736b4cac81SBjoern A. Zeeb 	int error;
19746b4cac81SBjoern A. Zeeb 
19756b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19766b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19776b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19786b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19796b4cac81SBjoern A. Zeeb 
1980*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1981*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1982*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
1983*2ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
1984*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1985*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1986*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1987*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1988*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
1989*2ac8a218SBjoern A. Zeeb #endif
1990*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
1991*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1992*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1993*2ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
1994*2ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1995*2ac8a218SBjoern A. Zeeb 
1996*2ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
19976b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19986b4cac81SBjoern A. Zeeb 
199967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2000d9f59799SBjoern A. Zeeb 
2001d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2003d9f59799SBjoern A. Zeeb 
2004d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2005d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2006d9f59799SBjoern A. Zeeb 
2007d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2008d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2009d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2010d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2011d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2012d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2013d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2014d9f59799SBjoern A. Zeeb 	}
2015d9f59799SBjoern A. Zeeb 
20168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2017d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2018d9f59799SBjoern A. Zeeb 
2019d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2020d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2021018d93ecSBjoern A. Zeeb 	if (error != 0) {
2022018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2023018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2024d9f59799SBjoern A. Zeeb 		goto outni;
2025018d93ecSBjoern A. Zeeb 	}
2026d9f59799SBjoern A. Zeeb 
2027d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2029d9f59799SBjoern A. Zeeb 
203067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2031d9f59799SBjoern A. Zeeb 
2032d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2033d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2034d9f59799SBjoern A. Zeeb 
2035d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2036d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2037d9f59799SBjoern A. Zeeb 
2038d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2039d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2040d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2041d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2042d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2043d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2044d9f59799SBjoern A. Zeeb 	}
2045d9f59799SBjoern A. Zeeb 
2046d9f59799SBjoern A. Zeeb #if 0
2047d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2048d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2049d9f59799SBjoern A. Zeeb 
2050d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2051d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2052d9f59799SBjoern A. Zeeb #endif
2053d9f59799SBjoern A. Zeeb 
2054d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2055d9f59799SBjoern A. Zeeb 
20566b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
20576b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20586b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
20596b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2060e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2061018d93ecSBjoern A. Zeeb 	if (error != 0) {
2062018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2063018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20646b4cac81SBjoern A. Zeeb 		goto out;
2065018d93ecSBjoern A. Zeeb 	}
20666b4cac81SBjoern A. Zeeb 
206767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20686b4cac81SBjoern A. Zeeb 
20696b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
20706b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20716b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
20726b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2073e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2074018d93ecSBjoern A. Zeeb 	if (error != 0) {
2075018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2076018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20776b4cac81SBjoern A. Zeeb 		goto out;
2078018d93ecSBjoern A. Zeeb 	}
20796b4cac81SBjoern A. Zeeb 
208067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20816b4cac81SBjoern A. Zeeb 
2082d9f59799SBjoern A. Zeeb #if 0
2083d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2084d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2085d9f59799SBjoern A. Zeeb #endif
2086d9f59799SBjoern A. Zeeb 
2087d9f59799SBjoern A. Zeeb 	error = EALREADY;
20886b4cac81SBjoern A. Zeeb out:
20898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20906b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2091d9f59799SBjoern A. Zeeb outni:
20926b4cac81SBjoern A. Zeeb 	return (error);
20936b4cac81SBjoern A. Zeeb }
20946b4cac81SBjoern A. Zeeb 
20956b4cac81SBjoern A. Zeeb static int
2096d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2097d9f59799SBjoern A. Zeeb {
2098d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2099d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2100d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2101d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2102d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2103d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2104d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2105d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2106d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2107d9f59799SBjoern A. Zeeb 	int error;
2108d9f59799SBjoern A. Zeeb 
2109d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2110d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2111d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2112d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2113d9f59799SBjoern A. Zeeb 
2114*2ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2115*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2116*2ac8a218SBjoern A. Zeeb 
2117*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2118*2ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2119*2ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
2120*2ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
2121*2ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2122*2ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2123*2ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2124*2ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2125*2ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
2126*2ac8a218SBjoern A. Zeeb #endif
2127*2ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
2128*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2129*2ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2130*2ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
2131*2ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2132*2ac8a218SBjoern A. Zeeb 
2133*2ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2134d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2135d9f59799SBjoern A. Zeeb 
213667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2137d9f59799SBjoern A. Zeeb 
2138d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2139d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2140d9f59799SBjoern A. Zeeb 
2141d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2142d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2143d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2144d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2145d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2146d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2147d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2148d9f59799SBjoern A. Zeeb 	}
2149d9f59799SBjoern A. Zeeb 
21508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2151d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2152d9f59799SBjoern A. Zeeb 
2153d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2154d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2155018d93ecSBjoern A. Zeeb 	if (error != 0) {
2156018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2157018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2158d9f59799SBjoern A. Zeeb 		goto outni;
2159018d93ecSBjoern A. Zeeb 	}
2160d9f59799SBjoern A. Zeeb 
2161d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21628ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2163d9f59799SBjoern A. Zeeb 
216467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2165d9f59799SBjoern A. Zeeb 
2166d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2167d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2168d9f59799SBjoern A. Zeeb 
2169d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2170d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2171d9f59799SBjoern A. Zeeb 
2172d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2173d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2174d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2175d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2176d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2177d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2178d9f59799SBjoern A. Zeeb 	}
2179d9f59799SBjoern A. Zeeb 
2180d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2181d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2182d9f59799SBjoern A. Zeeb 
2183d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2184d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2185d9f59799SBjoern A. Zeeb 
2186d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2187d9f59799SBjoern A. Zeeb 
2188d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2189d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2190d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2191d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2192e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2193018d93ecSBjoern A. Zeeb 	if (error != 0) {
2194018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2195018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2196d9f59799SBjoern A. Zeeb 		goto out;
2197018d93ecSBjoern A. Zeeb 	}
2198d9f59799SBjoern A. Zeeb 
219967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2200d9f59799SBjoern A. Zeeb 
2201d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2202d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2203d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2204d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2205e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2206018d93ecSBjoern A. Zeeb 	if (error != 0) {
2207018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2208018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2209d9f59799SBjoern A. Zeeb 		goto out;
2210018d93ecSBjoern A. Zeeb 	}
2211d9f59799SBjoern A. Zeeb 
221267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2213d9f59799SBjoern A. Zeeb 
2214d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2215d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2216d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2217d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2218e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2219018d93ecSBjoern A. Zeeb 	if (error != 0) {
2220018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2221018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2222d9f59799SBjoern A. Zeeb 		goto out;
2223018d93ecSBjoern A. Zeeb 	}
2224d9f59799SBjoern A. Zeeb 
222567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2226d9f59799SBjoern A. Zeeb 
222716e688b2SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
222816e688b2SBjoern A. Zeeb 	/*
222916e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
223016e688b2SBjoern A. Zeeb 	 * See comment there; removes the sta from fw.
223116e688b2SBjoern A. Zeeb 	 */
223216e688b2SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
223316e688b2SBjoern A. Zeeb 
2234d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2235d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2236d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2237d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2238e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2239d9f59799SBjoern A. Zeeb 	if (error != 0) {
2240d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2241018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2242018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2243d9f59799SBjoern A. Zeeb 		goto out;
2244d9f59799SBjoern A. Zeeb 	}
2245d9f59799SBjoern A. Zeeb 
224616e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2247d9f59799SBjoern A. Zeeb 
2248d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2249d9f59799SBjoern A. Zeeb 	bss_changed = 0;
2250d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2251d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2252616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2253616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2254d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2255d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2256d9f59799SBjoern A. Zeeb 
2257*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2258*2ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
2259*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
2260*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
2261*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2262*2ac8a218SBjoern A. Zeeb 	ieee80211_free_node(ni);		/* was lvif->lvif_bss->ni */
2263*2ac8a218SBjoern A. Zeeb 
2264d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
2265d9f59799SBjoern A. Zeeb 
2266d9f59799SBjoern A. Zeeb 	/* conf_tx */
2267d9f59799SBjoern A. Zeeb 
2268d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2269d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2270c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2271d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
2272d9f59799SBjoern A. Zeeb 
2273d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
2274d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
227568541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2276d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2277d9f59799SBjoern A. Zeeb 
2278d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2279d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
2280c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
2281c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2282d9f59799SBjoern A. Zeeb 	}
2283d9f59799SBjoern A. Zeeb 
2284d9f59799SBjoern A. Zeeb 	error = EALREADY;
2285d9f59799SBjoern A. Zeeb out:
22868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2287d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2288d9f59799SBjoern A. Zeeb outni:
2289d9f59799SBjoern A. Zeeb 	return (error);
2290d9f59799SBjoern A. Zeeb }
2291d9f59799SBjoern A. Zeeb 
2292d9f59799SBjoern A. Zeeb static int
2293d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2294d9f59799SBjoern A. Zeeb {
2295d9f59799SBjoern A. Zeeb 
2296d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2297d9f59799SBjoern A. Zeeb }
2298d9f59799SBjoern A. Zeeb 
2299d9f59799SBjoern A. Zeeb static int
23006b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23016b4cac81SBjoern A. Zeeb {
23026b4cac81SBjoern A. Zeeb 	int error;
23036b4cac81SBjoern A. Zeeb 
2304d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2305d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2306d9f59799SBjoern A. Zeeb 		return (error);
2307d9f59799SBjoern A. Zeeb 
2308d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2309d9f59799SBjoern A. Zeeb 
2310d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
23116b4cac81SBjoern A. Zeeb 	return (error);
23126b4cac81SBjoern A. Zeeb }
23136b4cac81SBjoern A. Zeeb 
2314d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
23156b4cac81SBjoern A. Zeeb 
23166b4cac81SBjoern A. Zeeb /*
23176b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
23186b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
23196b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
23206b4cac81SBjoern A. Zeeb  */
23216b4cac81SBjoern A. Zeeb struct fsm_state {
23226b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
23236b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
23246b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
23256b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
23266b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
23276b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
23286b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
23296b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2330d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2331d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
23326b4cac81SBjoern A. Zeeb 
23336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
23346b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
23356b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
23366b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2337d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
23386b4cac81SBjoern A. Zeeb 
2339d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2340d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2341d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2342d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2343d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
23446b4cac81SBjoern A. Zeeb 
2345d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2346d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2347d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
23486b4cac81SBjoern A. Zeeb 
23496b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
23506b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
23516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
23526b4cac81SBjoern A. Zeeb 
23536b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
23546b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
23556b4cac81SBjoern A. Zeeb };
23566b4cac81SBjoern A. Zeeb 
23576b4cac81SBjoern A. Zeeb static int
23586b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23596b4cac81SBjoern A. Zeeb {
23606b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
23616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23626b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23646b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
23656b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
23666b4cac81SBjoern A. Zeeb 	int error;
23676b4cac81SBjoern A. Zeeb 
23686b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
23696b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
23706b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
23716b4cac81SBjoern A. Zeeb 
23729d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23739d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
23746b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
23756b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
23769d9ba2b7SBjoern A. Zeeb #endif
23776b4cac81SBjoern A. Zeeb 
23786b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
23796b4cac81SBjoern A. Zeeb 
23806b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
23816b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
23826b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
23836b4cac81SBjoern A. Zeeb 
23846b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
23856b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
23866b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
23876b4cac81SBjoern A. Zeeb 
23886b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
23896b4cac81SBjoern A. Zeeb 
23906b4cac81SBjoern A. Zeeb 	} else {
23916b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
23926b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
23936b4cac81SBjoern A. Zeeb 		return (ENOSYS);
23946b4cac81SBjoern A. Zeeb 	}
23956b4cac81SBjoern A. Zeeb 
23966b4cac81SBjoern A. Zeeb 	error = 0;
23976b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
23986b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
23999d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24009d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2401d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2402d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2403d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2404d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
24059d9ba2b7SBjoern A. Zeeb #endif
24066b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
24076b4cac81SBjoern A. Zeeb 			break;
24086b4cac81SBjoern A. Zeeb 		}
24096b4cac81SBjoern A. Zeeb 	}
24106b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
24116b4cac81SBjoern A. Zeeb 
24126b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2413d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
24146b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
24156b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
24166b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
24176b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
24186b4cac81SBjoern A. Zeeb 		return (ENOSYS);
24196b4cac81SBjoern A. Zeeb 	}
24206b4cac81SBjoern A. Zeeb 
24216b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
24229d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24239d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2424d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2425d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2426d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2427d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
24289d9ba2b7SBjoern A. Zeeb #endif
24296b4cac81SBjoern A. Zeeb 		return (0);
24306b4cac81SBjoern A. Zeeb 	}
24316b4cac81SBjoern A. Zeeb 
24326b4cac81SBjoern A. Zeeb 	if (error != 0) {
24336b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
24346b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
24356b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
24366b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
243745c27ad5SBjoern A. Zeeb 		return (error);
24386b4cac81SBjoern A. Zeeb 	}
24396b4cac81SBjoern A. Zeeb 
24409d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24419d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2442d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2443d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
24446b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
24459d9ba2b7SBjoern A. Zeeb #endif
24466b4cac81SBjoern A. Zeeb 
24476b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
24486b4cac81SBjoern A. Zeeb }
24496b4cac81SBjoern A. Zeeb 
24506b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
24516b4cac81SBjoern A. Zeeb 
2452d9f59799SBjoern A. Zeeb /*
2453d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2454d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2455d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2456d9f59799SBjoern A. Zeeb  */
2457d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2458d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2459d9f59799SBjoern A. Zeeb {
2460d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2461*2ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2462d9f59799SBjoern A. Zeeb 
2463*2ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2464d9f59799SBjoern A. Zeeb 
2465ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2466*2ac8a218SBjoern A. Zeeb 
2467*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2468*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
2469*2ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2470*2ac8a218SBjoern A. Zeeb 
2471*2ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
2472*2ac8a218SBjoern A. Zeeb 	return (rni);
2473d9f59799SBjoern A. Zeeb }
2474d9f59799SBjoern A. Zeeb 
24754a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
24766b4cac81SBjoern A. Zeeb static int
24774a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
24786b4cac81SBjoern A. Zeeb {
24794a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
24806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24836b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
24846b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
24856b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
24866b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
24876b4cac81SBjoern A. Zeeb 	int error;
24886b4cac81SBjoern A. Zeeb 	uint16_t ac;
24896b4cac81SBjoern A. Zeeb 
24906b4cac81SBjoern A. Zeeb 	IMPROVE();
24916b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
24926b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
24936b4cac81SBjoern A. Zeeb 
24946b4cac81SBjoern A. Zeeb 	if (vap == NULL)
24956b4cac81SBjoern A. Zeeb 		return (0);
24966b4cac81SBjoern A. Zeeb 
24974a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
24984a67f1dfSBjoern A. Zeeb 		return (0);
24994a67f1dfSBjoern A. Zeeb 
25006b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
25016b4cac81SBjoern A. Zeeb 		return (0);
25026b4cac81SBjoern A. Zeeb 
25034a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
25044a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
25054a67f1dfSBjoern A. Zeeb 		return (0);
25064a67f1dfSBjoern A. Zeeb 	}
25074a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
25086b4cac81SBjoern A. Zeeb 
25094a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
25106b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
25116b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
25126b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
25136b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
25146b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
25156b4cac81SBjoern A. Zeeb 
25164a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25174a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25184a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25194a67f1dfSBjoern A. Zeeb 
25206b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
25216b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
25226b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
25236b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
25246b4cac81SBjoern A. Zeeb 
25256b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
25266b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
25276b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
25286b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
25296b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
25306b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
253168541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
25326b4cac81SBjoern A. Zeeb 		if (error != 0)
25333f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
25346b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
25356b4cac81SBjoern A. Zeeb 	}
25366b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
25376b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
25384a67f1dfSBjoern A. Zeeb 	if (!planned)
25396b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
25404a67f1dfSBjoern A. Zeeb 
25414a67f1dfSBjoern A. Zeeb 	return (changed);
25424a67f1dfSBjoern A. Zeeb }
25436b4cac81SBjoern A. Zeeb #endif
25446b4cac81SBjoern A. Zeeb 
25454a67f1dfSBjoern A. Zeeb static int
25464a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
25474a67f1dfSBjoern A. Zeeb {
25484a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25494a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
25504a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
25514a67f1dfSBjoern A. Zeeb 
25524a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
25534a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
25544a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
25556b4cac81SBjoern A. Zeeb 		return (0);
25564a67f1dfSBjoern A. Zeeb 
25574a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
25584a67f1dfSBjoern A. Zeeb 
25594a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
25604a67f1dfSBjoern A. Zeeb #endif
25614a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
25626b4cac81SBjoern A. Zeeb }
25636b4cac81SBjoern A. Zeeb 
25646b4cac81SBjoern A. Zeeb static struct ieee80211vap *
25656b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
25666b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
25676b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
25686b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
25696b4cac81SBjoern A. Zeeb {
25706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25716b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25726b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25736b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
25746b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2575a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
25766b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
25776b4cac81SBjoern A. Zeeb 	size_t len;
2578e3a0b120SBjoern A. Zeeb 	int error, i;
2579a6042e17SBjoern A. Zeeb 	uint16_t ac;
25806b4cac81SBjoern A. Zeeb 
25816b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
25826b4cac81SBjoern A. Zeeb 		return (NULL);
25836b4cac81SBjoern A. Zeeb 
25846b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
25856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25866b4cac81SBjoern A. Zeeb 
25876b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
25886b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
25896b4cac81SBjoern A. Zeeb 
25906b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
25916b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
25926b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
2593*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
2594*2ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
25956b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
25966b4cac81SBjoern A. Zeeb 
25976b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25986b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
25996b4cac81SBjoern A. Zeeb 	vif->p2p = false;
26006b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
26016b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
26026b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
26036b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
26046b4cac81SBjoern A. Zeeb 	IMPROVE();
26056b4cac81SBjoern A. Zeeb 
26066b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
26076b4cac81SBjoern A. Zeeb #if 1
26086b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
2609adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
26106ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
26116ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
26126ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
26136b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
26146b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
26156b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
26166b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
26176b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
26186b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2619616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
2620616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
2621616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
2622616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
26236ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2624caaa79c3SBjoern A. Zeeb 	/*
2625caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2626caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2627caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2628caaa79c3SBjoern A. Zeeb 	 */
26296ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
26306b4cac81SBjoern A. Zeeb #endif
26316b4cac81SBjoern A. Zeeb #if 0
26326b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
26336b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
26346b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
26356b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
26366b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
26376b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
26386b4cac81SBjoern A. Zeeb #endif
2639e3a0b120SBjoern A. Zeeb 
26406ffb7bd4SBjoern A. Zeeb 	/* Link Config */
26416ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
26426ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
26436ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
26446ffb7bd4SBjoern A. Zeeb 	}
26456ffb7bd4SBjoern A. Zeeb 
2646e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2647e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2648e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2649e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2650e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2651e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2652e3a0b120SBjoern A. Zeeb 		else
2653e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
26540cbcfa19SBjoern A. Zeeb 
26550cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
26560cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
2657e3a0b120SBjoern A. Zeeb 	}
2658e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2659e3a0b120SBjoern A. Zeeb 
26606b4cac81SBjoern A. Zeeb 	IMPROVE();
26616b4cac81SBjoern A. Zeeb 
26626b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
26636b4cac81SBjoern A. Zeeb 	if (error != 0) {
26643f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
26656b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
26666b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
26676b4cac81SBjoern A. Zeeb 		return (NULL);
26686b4cac81SBjoern A. Zeeb 	}
26696b4cac81SBjoern A. Zeeb 
26706b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
26716b4cac81SBjoern A. Zeeb 	if (error != 0) {
26726b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
26733f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
26746b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
26756b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
26766b4cac81SBjoern A. Zeeb 		return (NULL);
26776b4cac81SBjoern A. Zeeb 	}
26786b4cac81SBjoern A. Zeeb 
26798891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
26806b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
26818891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
26826b4cac81SBjoern A. Zeeb 
26836b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
26846b4cac81SBjoern A. Zeeb 	changed = 0;
26856b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
26866b4cac81SBjoern A. Zeeb 
2687a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2688a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2689a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2690a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2691a6042e17SBjoern A. Zeeb 
2692a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
2693a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
2694a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
2695a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
2696a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
2697a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2698a6042e17SBjoern A. Zeeb 		if (error != 0)
2699a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2700a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
2701a6042e17SBjoern A. Zeeb 	}
2702a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2703a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
2704a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
27056b4cac81SBjoern A. Zeeb 
27066b4cac81SBjoern A. Zeeb 	/* Force MC init. */
27076b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
27086b4cac81SBjoern A. Zeeb 
27096b4cac81SBjoern A. Zeeb 	IMPROVE();
27106b4cac81SBjoern A. Zeeb 
27116b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
27126b4cac81SBjoern A. Zeeb 
27136b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
27146b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
27156b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2716d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2717d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
27186b4cac81SBjoern A. Zeeb 
27196b4cac81SBjoern A. Zeeb 	/* Key management. */
27206b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2721b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
27226b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
27236b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
27246b4cac81SBjoern A. Zeeb #endif
27256b4cac81SBjoern A. Zeeb 	}
27266b4cac81SBjoern A. Zeeb 
27279fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
27289fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
27299fb91463SBjoern A. Zeeb #endif
27309fb91463SBjoern A. Zeeb 
27316b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
27326b4cac81SBjoern A. Zeeb 
27336b4cac81SBjoern A. Zeeb 	/* Complete setup. */
27346b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
27356b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
27366b4cac81SBjoern A. Zeeb 
27376b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
27386b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
27396b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
27406b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
27416b4cac81SBjoern A. Zeeb 
27426b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
27436b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
27446b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
27456b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
27466b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
27476b4cac81SBjoern A. Zeeb 	/* any others? */
27486b4cac81SBjoern A. Zeeb 	IMPROVE();
27496b4cac81SBjoern A. Zeeb 
27506b4cac81SBjoern A. Zeeb 	return (vap);
27516b4cac81SBjoern A. Zeeb }
27526b4cac81SBjoern A. Zeeb 
27534b0af114SBjoern A. Zeeb void
27544b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
27554b0af114SBjoern A. Zeeb {
27564b0af114SBjoern A. Zeeb 
27574b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
27584b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
27594b0af114SBjoern A. Zeeb 
27604b0af114SBjoern A. Zeeb 	IMPROVE();
27614b0af114SBjoern A. Zeeb }
27624b0af114SBjoern A. Zeeb 
27634b0af114SBjoern A. Zeeb void
27644b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
27654b0af114SBjoern A. Zeeb {
27664b0af114SBjoern A. Zeeb 
27674b0af114SBjoern A. Zeeb 	TODO();
27684b0af114SBjoern A. Zeeb }
27694b0af114SBjoern A. Zeeb 
27706b4cac81SBjoern A. Zeeb static void
27716b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
27726b4cac81SBjoern A. Zeeb {
27736b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
27746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27756b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27766b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27776b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27786b4cac81SBjoern A. Zeeb 
27796b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27806b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27816b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
27826b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
27836b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27846b4cac81SBjoern A. Zeeb 
27858891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
27866b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
27878891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
27886b4cac81SBjoern A. Zeeb 
27896b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
27906b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
2791dbf76919SBjoern A. Zeeb 
2792dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
2793dbf76919SBjoern A. Zeeb 
2794dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
2795dbf76919SBjoern A. Zeeb 
27966c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
27976c38c6b1SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw);
27986c38c6b1SBjoern A. Zeeb 
27996b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
28006b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
28016b4cac81SBjoern A. Zeeb }
28026b4cac81SBjoern A. Zeeb 
28036b4cac81SBjoern A. Zeeb static void
28046b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
28056b4cac81SBjoern A. Zeeb {
28066b4cac81SBjoern A. Zeeb 
28076b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
28086b4cac81SBjoern A. Zeeb 	TRACEOK();
28096b4cac81SBjoern A. Zeeb }
28106b4cac81SBjoern A. Zeeb 
28116b4cac81SBjoern A. Zeeb static void
28126b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
28136b4cac81SBjoern A. Zeeb {
28146b4cac81SBjoern A. Zeeb 
28156b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
28166b4cac81SBjoern A. Zeeb }
28176b4cac81SBjoern A. Zeeb 
28186b4cac81SBjoern A. Zeeb static void
28196b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
28206b4cac81SBjoern A. Zeeb {
28216b4cac81SBjoern A. Zeeb 
28226b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
28236b4cac81SBjoern A. Zeeb }
28246b4cac81SBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb /* Start / stop device. */
28266b4cac81SBjoern A. Zeeb static void
28276b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
28286b4cac81SBjoern A. Zeeb {
28296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28308d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
28316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28326b4cac81SBjoern A. Zeeb 	int error;
28338d58a057SBjoern A. Zeeb #endif
28346b4cac81SBjoern A. Zeeb 	bool start_all;
28356b4cac81SBjoern A. Zeeb 
28366b4cac81SBjoern A. Zeeb 	IMPROVE();
28376b4cac81SBjoern A. Zeeb 
28386b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28398d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
28406b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28418d58a057SBjoern A. Zeeb #endif
28426b4cac81SBjoern A. Zeeb 	start_all = false;
28436b4cac81SBjoern A. Zeeb 
28448ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
28458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
28466b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
28478d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
28486b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
28496b4cac81SBjoern A. Zeeb 		if (error == 0)
28508d58a057SBjoern A. Zeeb #endif
28516b4cac81SBjoern A. Zeeb 			start_all = true;
28526b4cac81SBjoern A. Zeeb 	} else {
28538d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
28546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
28558d58a057SBjoern A. Zeeb #endif
28566b4cac81SBjoern A. Zeeb 	}
28578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
28588ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
28596b4cac81SBjoern A. Zeeb 
28606b4cac81SBjoern A. Zeeb 	if (start_all)
28616b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
28626b4cac81SBjoern A. Zeeb }
28636b4cac81SBjoern A. Zeeb 
28646b4cac81SBjoern A. Zeeb bool
28656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
28666b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
28676b4cac81SBjoern A. Zeeb {
28686b4cac81SBjoern A. Zeeb 	int i;
28696b4cac81SBjoern A. Zeeb 
28706b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
28716b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
28726b4cac81SBjoern A. Zeeb 			return (true);
28736b4cac81SBjoern A. Zeeb 	}
28746b4cac81SBjoern A. Zeeb 
28756b4cac81SBjoern A. Zeeb 	return (false);
28766b4cac81SBjoern A. Zeeb }
28776b4cac81SBjoern A. Zeeb 
28786b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
28796b4cac81SBjoern A. Zeeb bool
28806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
28816b4cac81SBjoern A. Zeeb {
28826b4cac81SBjoern A. Zeeb 	size_t x;
28836b4cac81SBjoern A. Zeeb 	uint8_t l;
28846b4cac81SBjoern A. Zeeb 
28856b4cac81SBjoern A. Zeeb 	x = *xp;
28866b4cac81SBjoern A. Zeeb 
28876b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
28886b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
28896b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
28906b4cac81SBjoern A. Zeeb 	x += 2 + l;
28916b4cac81SBjoern A. Zeeb 
28926b4cac81SBjoern A. Zeeb 	if (x > ies_len)
28936b4cac81SBjoern A. Zeeb 		return (false);
28946b4cac81SBjoern A. Zeeb 
28956b4cac81SBjoern A. Zeeb 	*xp = x;
28966b4cac81SBjoern A. Zeeb 	return (true);
28976b4cac81SBjoern A. Zeeb }
28986b4cac81SBjoern A. Zeeb 
2899d9945d78SBjoern A. Zeeb static uint8_t *
2900d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2901d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
29026b4cac81SBjoern A. Zeeb {
2903d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
2904d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
29053dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
2906d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
2907d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
2908d9945d78SBjoern A. Zeeb 	uint8_t *pb;
2909d9945d78SBjoern A. Zeeb 	int band, i;
29106b4cac81SBjoern A. Zeeb 
29113dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
2912d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2913d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
2914d9945d78SBjoern A. Zeeb 			continue;
2915d9945d78SBjoern A. Zeeb 
2916d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
2917d9945d78SBjoern A. Zeeb 		/*
2918d9945d78SBjoern A. Zeeb 		 * This should not happen;
2919d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
2920d9945d78SBjoern A. Zeeb 		 */
2921d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
2922d9945d78SBjoern A. Zeeb 			continue;
2923d9945d78SBjoern A. Zeeb 
2924d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
2925d9945d78SBjoern A. Zeeb 		channels = supband->channels;
2926d9945d78SBjoern A. Zeeb 		chan = NULL;
2927d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
2928d9945d78SBjoern A. Zeeb 
2929d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2930d9945d78SBjoern A. Zeeb 				continue;
2931d9945d78SBjoern A. Zeeb 
29323dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
2933d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
2934d9945d78SBjoern A. Zeeb 			if (chan != NULL)
2935d9945d78SBjoern A. Zeeb 				break;
2936d9945d78SBjoern A. Zeeb 		}
2937d9945d78SBjoern A. Zeeb 
2938d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
2939d9945d78SBjoern A. Zeeb 		if (chan == NULL)
2940d9945d78SBjoern A. Zeeb 			continue;
2941d9945d78SBjoern A. Zeeb 
2942d9945d78SBjoern A. Zeeb 		pb = p;
29433dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
2944d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
2945d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
2946d9945d78SBjoern A. Zeeb 
29473dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
29483dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
29493dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
29503dd98026SBjoern A. Zeeb 
29513dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
29523dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
29533dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
29543dd98026SBjoern A. Zeeb 		}
29553dd98026SBjoern A. Zeeb #endif
29563dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
29573dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
29583dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
29593dd98026SBjoern A. Zeeb 
29603dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
29613dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
29623dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
29633dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
29643dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
29653dd98026SBjoern A. Zeeb 		}
29663dd98026SBjoern A. Zeeb #endif
29673dd98026SBjoern A. Zeeb 
2968d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
2969d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
2970d9945d78SBjoern A. Zeeb 	}
2971d9945d78SBjoern A. Zeeb 
2972d9945d78SBjoern A. Zeeb 	/* Add common_ies */
2973d9945d78SBjoern A. Zeeb 	pb = p;
2974d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
2975d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
2976d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
2977d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
2978d9945d78SBjoern A. Zeeb 	}
2979d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
2980d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
2981d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
2982d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
2983d9945d78SBjoern A. Zeeb 	}
2984d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
2985d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
2986d9945d78SBjoern A. Zeeb 
2987d9945d78SBjoern A. Zeeb 	return (p);
29886b4cac81SBjoern A. Zeeb }
29896b4cac81SBjoern A. Zeeb 
29906b4cac81SBjoern A. Zeeb static void
29916b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
29926b4cac81SBjoern A. Zeeb {
29936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29946b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29976b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
29986b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
29996b4cac81SBjoern A. Zeeb 	int error;
30008ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
30016b4cac81SBjoern A. Zeeb 
30026b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3004a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
30056b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
30068ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
30076b4cac81SBjoern A. Zeeb 		return;
30086b4cac81SBjoern A. Zeeb 	}
30098ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
30108ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
30116b4cac81SBjoern A. Zeeb 
30126b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
30136b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
30146b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3015d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
30166b4cac81SBjoern A. Zeeb 		return;
30176b4cac81SBjoern A. Zeeb 	}
30186b4cac81SBjoern A. Zeeb 
30196b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30208ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3021a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3022a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3023d3ef7fb4SBjoern A. Zeeb sw_scan:
30246b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
30256b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3026086be6a8SBjoern A. Zeeb 
3027086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3028086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3029086be6a8SBjoern A. Zeeb 
30306b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
30316b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
30326b4cac81SBjoern A. Zeeb 		IMPROVE();
30336b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
30346b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
30356b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
30366b4cac81SBjoern A. Zeeb 
30376b4cac81SBjoern A. Zeeb 	} else {
30386b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
30396b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
30406b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
30416b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
30426b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
30436b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3044d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3045d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3046d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
30473206587aSBjoern A. Zeeb 		bool running;
3048d9945d78SBjoern A. Zeeb 
3049d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3050d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
30516b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
30526b4cac81SBjoern A. Zeeb 
3053d9945d78SBjoern A. Zeeb 		band_mask = 0;
30546b4cac81SBjoern A. Zeeb 		nchan = 0;
3055d9945d78SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++) {
30566b4cac81SBjoern A. Zeeb 			nchan++;
3057d9945d78SBjoern A. Zeeb 			band = lkpi_net80211_chan_to_nl80211_band(
3058d9945d78SBjoern A. Zeeb 			    ss->ss_chans[ss->ss_next + i]);
3059d9945d78SBjoern A. Zeeb 			band_mask |= (1 << band);
3060d9945d78SBjoern A. Zeeb 		}
30613206587aSBjoern A. Zeeb 
30623206587aSBjoern A. Zeeb 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
30633206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
30643206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
30653206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
30663206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
30673206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
30683206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
30693206587aSBjoern A. Zeeb 			 */
30703206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
30713206587aSBjoern A. Zeeb 		}
30723206587aSBjoern A. Zeeb 
30736b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
30746b4cac81SBjoern A. Zeeb 
3075d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3076d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3077d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3078d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3079d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3080d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3081d9945d78SBjoern A. Zeeb 
3082d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3083d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3084d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3085d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3086d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3087d9945d78SBjoern A. Zeeb 		}
3088d9945d78SBjoern A. Zeeb 
30893206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3090d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3091d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
30926b4cac81SBjoern A. Zeeb 
30936b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
30946b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
30956b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
30966b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
30976b4cac81SBjoern A. Zeeb #if 0
30986b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
30996b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
31006b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
31016b4cac81SBjoern A. Zeeb #endif
31026b4cac81SBjoern A. Zeeb #ifdef __notyet__
31036b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
31046b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
31056b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
31066b4cac81SBjoern A. Zeeb #endif
3107e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
31086b4cac81SBjoern A. Zeeb 
31096b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
31106b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
31116b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
31126b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
31136b4cac81SBjoern A. Zeeb 			*(cpp + i) =
31146b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
31156b4cac81SBjoern A. Zeeb 		}
31166b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
31176b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
31186b4cac81SBjoern A. Zeeb 
31196b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
31203206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
31216b4cac81SBjoern A. Zeeb 			/* lc->flags */
31226b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
31236b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
31246b4cac81SBjoern A. Zeeb 			/* lc-> ... */
31256b4cac81SBjoern A. Zeeb 			lc++;
31266b4cac81SBjoern A. Zeeb 		}
31276b4cac81SBjoern A. Zeeb 
3128d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
31296b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
31306b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
31316b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3132d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
31336b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
31346b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
31356b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
31366b4cac81SBjoern A. Zeeb 				ssids++;
31376b4cac81SBjoern A. Zeeb 			}
31386b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
31396b4cac81SBjoern A. Zeeb 		} else {
31406b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
31416b4cac81SBjoern A. Zeeb 		}
31426b4cac81SBjoern A. Zeeb 
31436b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
31446b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
31456b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
31466b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
31476b4cac81SBjoern A. Zeeb 		/* s6gp->... */
31486b4cac81SBjoern A. Zeeb 
3149d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3150d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3151d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3152d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3153d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3154d9945d78SBjoern A. Zeeb 
31556b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
31566b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
31573206587aSBjoern A. Zeeb 
31583206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
31593206587aSBjoern A. Zeeb 		/* Re-check under lock. */
31603206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
31613206587aSBjoern A. Zeeb 		if (!running) {
31623206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
31633206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
31643206587aSBjoern A. Zeeb 
31653206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
31663206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
31673206587aSBjoern A. Zeeb 		}
31683206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
31693206587aSBjoern A. Zeeb 		if (running) {
31703206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
31713206587aSBjoern A. Zeeb 			return;
31723206587aSBjoern A. Zeeb 		}
31733206587aSBjoern A. Zeeb 
31746b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
31756b4cac81SBjoern A. Zeeb 		if (error != 0) {
3176d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3177d9945d78SBjoern A. Zeeb 
31783206587aSBjoern A. Zeeb 			/*
31793206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
31803206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
31813206587aSBjoern A. Zeeb 			 * and only there.
31823206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
31833206587aSBjoern A. Zeeb 			 * behave differently:
31843206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
31853206587aSBjoern A. Zeeb 			 *        and can then still return an error.
31863206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
31873206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
31883206587aSBjoern A. Zeeb 			 * ...
31893206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
31903206587aSBjoern A. Zeeb 			 * and balance between both code paths.
31913206587aSBjoern A. Zeeb 			 */
31923206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
31933206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
31943206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
31956b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
31963206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
31973206587aSBjoern A. Zeeb 			}
31983206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3199d3ef7fb4SBjoern A. Zeeb 
3200d3ef7fb4SBjoern A. Zeeb 			/*
3201d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3202d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3203d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3204d3ef7fb4SBjoern A. Zeeb 			 */
3205196cfd0bSBjoern A. Zeeb 			if (error == 1) {
32068ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3207a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
32088ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32093206587aSBjoern A. Zeeb 				/*
32103206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
32113206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
32123206587aSBjoern A. Zeeb 				 * currently not re-enable it?
32133206587aSBjoern A. Zeeb 				 */
32143206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3215196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3216196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3217196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3218196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3219196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3220196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3221196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3222196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3223d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3224196cfd0bSBjoern A. Zeeb 			}
3225d3ef7fb4SBjoern A. Zeeb 
3226d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3227d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
32286b4cac81SBjoern A. Zeeb 		}
32296b4cac81SBjoern A. Zeeb 	}
32306b4cac81SBjoern A. Zeeb }
32316b4cac81SBjoern A. Zeeb 
32326b4cac81SBjoern A. Zeeb static void
32336b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
32346b4cac81SBjoern A. Zeeb {
32356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32368ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
32376b4cac81SBjoern A. Zeeb 
32386b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32398ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3240a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
32418ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32426b4cac81SBjoern A. Zeeb 		return;
32436b4cac81SBjoern A. Zeeb 	}
32448ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
32458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32466b4cac81SBjoern A. Zeeb 
32478ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3248a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3249a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
32506b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
32516b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
32526b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
32536b4cac81SBjoern A. Zeeb 
3254a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3255a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
32566b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
32576b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32586b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3259a486fbbdSBjoern A. Zeeb 
32606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
32616b4cac81SBjoern A. Zeeb 
32626b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3263086be6a8SBjoern A. Zeeb 
3264086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3265086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
32666b4cac81SBjoern A. Zeeb 	}
32676b4cac81SBjoern A. Zeeb }
32686b4cac81SBjoern A. Zeeb 
32696b4cac81SBjoern A. Zeeb static void
3270a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3271a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3272a486fbbdSBjoern A. Zeeb {
3273a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
32748ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3275a486fbbdSBjoern A. Zeeb 
3276a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
32778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
32788ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
32798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32808ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3281a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3282a486fbbdSBjoern A. Zeeb }
3283a486fbbdSBjoern A. Zeeb 
3284a486fbbdSBjoern A. Zeeb static void
3285a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3286a486fbbdSBjoern A. Zeeb {
3287a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
32888ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3289a486fbbdSBjoern A. Zeeb 
3290a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
32918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
32928ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
32938ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32948ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3295a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3296a486fbbdSBjoern A. Zeeb }
3297a486fbbdSBjoern A. Zeeb 
3298a486fbbdSBjoern A. Zeeb static void
32996b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
33006b4cac81SBjoern A. Zeeb {
33016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33026b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3303b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3304b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
33056b4cac81SBjoern A. Zeeb 	int error;
33068ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
33076b4cac81SBjoern A. Zeeb 
33086b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3309b2cf3c21SBjoern A. Zeeb 
3310b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3311b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
33126b4cac81SBjoern A. Zeeb 		return;
33136b4cac81SBjoern A. Zeeb 
3314a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
33158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
33168ac540d3SBjoern A. Zeeb 	hw_scan_running =
33178ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
33188ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
33198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33208ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3321a486fbbdSBjoern A. Zeeb 		return;
3322a486fbbdSBjoern A. Zeeb 
3323b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3324b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
33256b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
33266b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
33276b4cac81SBjoern A. Zeeb 		return;
33286b4cac81SBjoern A. Zeeb 	}
33296b4cac81SBjoern A. Zeeb 
33306b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
33316b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
33326b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
33336b4cac81SBjoern A. Zeeb 		    c, chan);
33346b4cac81SBjoern A. Zeeb 		return;
33356b4cac81SBjoern A. Zeeb 	}
33366b4cac81SBjoern A. Zeeb 
33376b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
33386b4cac81SBjoern A. Zeeb 	IMPROVE();
33396b4cac81SBjoern A. Zeeb 
33406b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33414a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
33429fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
33439fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
33449fb91463SBjoern A. Zeeb #endif
33454a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
33466b4cac81SBjoern A. Zeeb 
33476b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
33486b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
33496b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
33506b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
33516b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
33526b4cac81SBjoern A. Zeeb 		IMPROVE();
33536b4cac81SBjoern A. Zeeb 	} else {
33546b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
33556b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
33566b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
33576b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
33586b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
33596b4cac81SBjoern A. Zeeb 	}
33606b4cac81SBjoern A. Zeeb 
33616b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
33626b4cac81SBjoern A. Zeeb 	IMPROVE();
33636b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
33646b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
33656b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
33666b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
33676b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
33686b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
33696b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
33706b4cac81SBjoern A. Zeeb 			    error);
33716b4cac81SBjoern A. Zeeb 	}
33726b4cac81SBjoern A. Zeeb }
33736b4cac81SBjoern A. Zeeb 
33746b4cac81SBjoern A. Zeeb static struct ieee80211_node *
33756b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
33766b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
33776b4cac81SBjoern A. Zeeb {
33786b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33804f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
33816b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33826b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
33836b4cac81SBjoern A. Zeeb 
33846b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
33856b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33866b4cac81SBjoern A. Zeeb 
33876b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
33884f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
33894f61ef8bSBjoern A. Zeeb 		return (NULL);
33904f61ef8bSBjoern A. Zeeb 
33916b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
33926b4cac81SBjoern A. Zeeb 	if (ni == NULL)
33936b4cac81SBjoern A. Zeeb 		return (NULL);
33946b4cac81SBjoern A. Zeeb 
33956b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33964f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
33976b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
33986b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
33996b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
34006b4cac81SBjoern A. Zeeb 		return (NULL);
34016b4cac81SBjoern A. Zeeb 	}
34026b4cac81SBjoern A. Zeeb 
34036b4cac81SBjoern A. Zeeb 	return (ni);
34046b4cac81SBjoern A. Zeeb }
34056b4cac81SBjoern A. Zeeb 
34066b4cac81SBjoern A. Zeeb static int
34076b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
34086b4cac81SBjoern A. Zeeb {
34096b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34106b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
34126b4cac81SBjoern A. Zeeb 	int error;
34136b4cac81SBjoern A. Zeeb 
34146b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
34156b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34166b4cac81SBjoern A. Zeeb 
34176b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
34186b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
34196b4cac81SBjoern A. Zeeb 		if (error != 0)
34206b4cac81SBjoern A. Zeeb 			return (error);
34216b4cac81SBjoern A. Zeeb 	}
34226b4cac81SBjoern A. Zeeb 
34236b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
34246b4cac81SBjoern A. Zeeb 
34256b4cac81SBjoern A. Zeeb 	/* Now take the reference before linking it to the table. */
34266b4cac81SBjoern A. Zeeb 	lsta->ni = ieee80211_ref_node(ni);
34276b4cac81SBjoern A. Zeeb 
34286b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
34296b4cac81SBjoern A. Zeeb 	IMPROVE();
34306b4cac81SBjoern A. Zeeb 
34316b4cac81SBjoern A. Zeeb 	return (0);
34326b4cac81SBjoern A. Zeeb }
34336b4cac81SBjoern A. Zeeb 
34346b4cac81SBjoern A. Zeeb static void
34356b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
34366b4cac81SBjoern A. Zeeb {
34376b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34386b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34396b4cac81SBjoern A. Zeeb 
34406b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
34416b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34426b4cac81SBjoern A. Zeeb 
34436b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
34446b4cac81SBjoern A. Zeeb 	IMPROVE();
34456b4cac81SBjoern A. Zeeb 
34466b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
34476b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
34486b4cac81SBjoern A. Zeeb }
34496b4cac81SBjoern A. Zeeb 
34506b4cac81SBjoern A. Zeeb static void
34516b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
34526b4cac81SBjoern A. Zeeb {
34536b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
34586b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34596b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3460d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
3461d9f59799SBjoern A. Zeeb 		goto out;
34626b4cac81SBjoern A. Zeeb 
34636b4cac81SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
34646b4cac81SBjoern A. Zeeb 	IMPROVE();
34656b4cac81SBjoern A. Zeeb 
34666b4cac81SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
34676b4cac81SBjoern A. Zeeb #ifdef __notyet__
34688cb9b68fSJohn Baldwin 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
34696b4cac81SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
34706b4cac81SBjoern A. Zeeb #endif
34716b4cac81SBjoern A. Zeeb 	/* Drain taskq. */
34726b4cac81SBjoern A. Zeeb 
34736b4cac81SBjoern A. Zeeb 	/* Drain sta->txq[] */
34746b4cac81SBjoern A. Zeeb 	mtx_destroy(&lsta->txq_mtx);
34756b4cac81SBjoern A. Zeeb 
34766b4cac81SBjoern A. Zeeb 	/* Remove lsta if added_to_drv. */
3477e24e8103SBjoern A. Zeeb 
34786b4cac81SBjoern A. Zeeb 	/* Remove lsta from vif */
3479e24e8103SBjoern A. Zeeb 	/* Remove ref from lsta node... */
3480d9f59799SBjoern A. Zeeb 	/* Free lsta. */
3481e24e8103SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, VAP_TO_LVIF(ni->ni_vap));
3482d9f59799SBjoern A. Zeeb 
3483d9f59799SBjoern A. Zeeb out:
34846b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
34856b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
34866b4cac81SBjoern A. Zeeb }
34876b4cac81SBjoern A. Zeeb 
34886b4cac81SBjoern A. Zeeb static int
34896b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
34906b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
34916b4cac81SBjoern A. Zeeb {
34926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
34936b4cac81SBjoern A. Zeeb 
34946b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
34956b4cac81SBjoern A. Zeeb 
34966b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
34976b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
34986b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
34996b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
35006b4cac81SBjoern A. Zeeb 
35019d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35029d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
35036b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
35046b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
35056b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
35069d9ba2b7SBjoern A. Zeeb #endif
35076b4cac81SBjoern A. Zeeb 
35086b4cac81SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
35096b4cac81SBjoern A. Zeeb 	return (0);
35106b4cac81SBjoern A. Zeeb }
35116b4cac81SBjoern A. Zeeb 
35126b4cac81SBjoern A. Zeeb static void
35136b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
35146b4cac81SBjoern A. Zeeb {
35156b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3516b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
35176b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3518b35f6cd0SBjoern A. Zeeb #endif
35196b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
35206b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
35216b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35236b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35246b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35256b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35266b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
35276b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
35286b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
35296b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3530e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
35316b4cac81SBjoern A. Zeeb 	void *buf;
3532e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
35336b4cac81SBjoern A. Zeeb 
35346b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
35356b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35369d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
35376b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
35386b4cac81SBjoern A. Zeeb #endif
35396b4cac81SBjoern A. Zeeb 
35406b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3541b35f6cd0SBjoern A. Zeeb 	k = NULL;
3542b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
35436b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
35446b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
35456b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
35466b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
35476b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
35486b4cac81SBjoern A. Zeeb 		if (k == NULL) {
35496b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
35506b4cac81SBjoern A. Zeeb 			m_freem(m);
35516b4cac81SBjoern A. Zeeb 			return;
35526b4cac81SBjoern A. Zeeb 		}
35536b4cac81SBjoern A. Zeeb 	}
35546b4cac81SBjoern A. Zeeb #endif
35556b4cac81SBjoern A. Zeeb 
35566b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35576b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35586b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35596b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
35606b4cac81SBjoern A. Zeeb 
35616b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
35626b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
35636b4cac81SBjoern A. Zeeb 
35646b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
35656b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
35666b4cac81SBjoern A. Zeeb 		if (k != NULL)
35676b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
35686b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
35696b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
35706b4cac81SBjoern A. Zeeb 		IMPROVE();
35716b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
35726b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
35736b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
35746b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
35756b4cac81SBjoern A. Zeeb 		}
35766b4cac81SBjoern A. Zeeb 
35776b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
35786b4cac81SBjoern A. Zeeb 	}
35796b4cac81SBjoern A. Zeeb 
35806b4cac81SBjoern A. Zeeb 	/*
35816b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
35826b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
35833d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
35843d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
35856b4cac81SBjoern A. Zeeb 	 */
35866b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
35876b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
35883f0083c4SBjoern A. Zeeb 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
35896b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
35906b4cac81SBjoern A. Zeeb 		m_freem(m);
35916b4cac81SBjoern A. Zeeb 		return;
35926b4cac81SBjoern A. Zeeb 	}
35936b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
35946b4cac81SBjoern A. Zeeb 
35956b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
35966b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
35976b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
35986b4cac81SBjoern A. Zeeb 	skb->m = m;
35996b4cac81SBjoern A. Zeeb #if 0
36006b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
36016b4cac81SBjoern A. Zeeb #else
36026b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
36036b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
36046b4cac81SBjoern A. Zeeb #endif
36056b4cac81SBjoern A. Zeeb 	/* Save the ni. */
36066b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
36076b4cac81SBjoern A. Zeeb 
36086b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
36096b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36106b4cac81SBjoern A. Zeeb 
3611e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3612e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3613e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3614e3a0b120SBjoern A. Zeeb 		skb->priority = 0;
3615e3a0b120SBjoern A. Zeeb 		ac = IEEE80211_AC_BE;
3616e3a0b120SBjoern A. Zeeb 	} else {
3617e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3618fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
3619e3a0b120SBjoern A. Zeeb 	}
36206b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
36216b4cac81SBjoern A. Zeeb 
36226b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
36236b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
36246b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
36256b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
36266b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
36276b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3628e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
36296b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
36306b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
36316b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
36326b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
36339fb91463SBjoern A. Zeeb #ifdef __notyet__
36349fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
36359fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
36369fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
36379fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
36389fb91463SBjoern A. Zeeb #endif
36399fb91463SBjoern A. Zeeb #endif
36406b4cac81SBjoern A. Zeeb 
36416b4cac81SBjoern A. Zeeb 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
36426b4cac81SBjoern A. Zeeb 	if (lsta != NULL) {
36436b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
3644b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
36456b4cac81SBjoern A. Zeeb 		info->control.hw_key = lsta->kc;
36466b4cac81SBjoern A. Zeeb #endif
36476b4cac81SBjoern A. Zeeb 	} else {
36486b4cac81SBjoern A. Zeeb 		sta = NULL;
36496b4cac81SBjoern A. Zeeb 	}
36506b4cac81SBjoern A. Zeeb 
36516b4cac81SBjoern A. Zeeb 	IMPROVE();
36526b4cac81SBjoern A. Zeeb 
36536b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
36546b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
36556b4cac81SBjoern A. Zeeb 
3656e3a0b120SBjoern A. Zeeb 		ltxq = NULL;
3657e3a0b120SBjoern A. Zeeb 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3658e3a0b120SBjoern A. Zeeb 			if (vif->type == NL80211_IFTYPE_STATION &&
3659e3a0b120SBjoern A. Zeeb 			    lsta->added_to_drv &&
3660e3a0b120SBjoern A. Zeeb 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3661d0d29110SBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3662e3a0b120SBjoern A. Zeeb 		} else if (lsta->added_to_drv &&
3663e3a0b120SBjoern A. Zeeb 		    sta->txq[skb->priority] != NULL) {
3664e3a0b120SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3665e3a0b120SBjoern A. Zeeb 		}
3666e3a0b120SBjoern A. Zeeb 		if (ltxq == NULL)
3667d0d29110SBjoern A. Zeeb 			goto ops_tx;
3668e3a0b120SBjoern A. Zeeb 
3669d0d29110SBjoern A. Zeeb 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3670d0d29110SBjoern A. Zeeb 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3671d0d29110SBjoern A. Zeeb 
3672eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
36736b4cac81SBjoern A. Zeeb 		skb_queue_tail(&ltxq->skbq, skb);
36749d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
36759d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3676d0d29110SBjoern A. Zeeb 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3677d0d29110SBjoern A. Zeeb 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3678d0d29110SBjoern A. Zeeb 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3679fb6eaf74SBjoern A. Zeeb 			    __func__, __LINE__,
3680d0d29110SBjoern A. Zeeb 			    curthread->td_tid, (unsigned int)ticks,
3681d0d29110SBjoern A. Zeeb 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3682d0d29110SBjoern A. Zeeb 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3683d0d29110SBjoern A. Zeeb 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
36849d9ba2b7SBjoern A. Zeeb #endif
3685eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
3686d0d29110SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
36876b4cac81SBjoern A. Zeeb 		return;
36886b4cac81SBjoern A. Zeeb 	}
36896b4cac81SBjoern A. Zeeb 
36906b4cac81SBjoern A. Zeeb ops_tx:
36919d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
36929d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3693d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3694d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
36956b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
36966b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
36979d9ba2b7SBjoern A. Zeeb #endif
36986b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
36996b4cac81SBjoern A. Zeeb 	control.sta = sta;
37006b4cac81SBjoern A. Zeeb 
37016b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
37026b4cac81SBjoern A. Zeeb 	return;
37036b4cac81SBjoern A. Zeeb }
37046b4cac81SBjoern A. Zeeb 
37056b4cac81SBjoern A. Zeeb static void
37066b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
37076b4cac81SBjoern A. Zeeb {
37086b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
37096b4cac81SBjoern A. Zeeb 	struct mbufq mq;
37106b4cac81SBjoern A. Zeeb 	struct mbuf *m;
37116b4cac81SBjoern A. Zeeb 
37126b4cac81SBjoern A. Zeeb 	lsta = ctx;
37136b4cac81SBjoern A. Zeeb 
37149d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37159d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
37166b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
37179d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
37186b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
37199d9ba2b7SBjoern A. Zeeb #endif
37206b4cac81SBjoern A. Zeeb 
37216b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
37226b4cac81SBjoern A. Zeeb 
37236b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_LOCK(lsta);
37246b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
37256b4cac81SBjoern A. Zeeb 	LKPI_80211_LSTA_UNLOCK(lsta);
37266b4cac81SBjoern A. Zeeb 
37276b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
37286b4cac81SBjoern A. Zeeb 	while (m != NULL) {
37296b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
37306b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
37316b4cac81SBjoern A. Zeeb 	}
37326b4cac81SBjoern A. Zeeb }
37336b4cac81SBjoern A. Zeeb 
37346b4cac81SBjoern A. Zeeb static int
37356b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
37366b4cac81SBjoern A. Zeeb {
37376b4cac81SBjoern A. Zeeb 
37386b4cac81SBjoern A. Zeeb 	/* XXX TODO */
37396b4cac81SBjoern A. Zeeb 	IMPROVE();
37406b4cac81SBjoern A. Zeeb 
37416b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
37426b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
37436b4cac81SBjoern A. Zeeb 
37446b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
37456b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
37466b4cac81SBjoern A. Zeeb }
37476b4cac81SBjoern A. Zeeb 
37489fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
37499fb91463SBjoern A. Zeeb static int
37509fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
37519fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
37529fb91463SBjoern A. Zeeb {
37539fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
37549fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37559fb91463SBjoern A. Zeeb 
37569fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
37579fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
37589fb91463SBjoern A. Zeeb 
37599fb91463SBjoern A. Zeeb 	IMPROVE_HT();
37609fb91463SBjoern A. Zeeb 
37619fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
37629fb91463SBjoern A. Zeeb }
37639fb91463SBjoern A. Zeeb 
37649fb91463SBjoern A. Zeeb static int
37659fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
37669fb91463SBjoern A. Zeeb {
37679fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
37689fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37699fb91463SBjoern A. Zeeb 
37709fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
37719fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
37729fb91463SBjoern A. Zeeb 
37739fb91463SBjoern A. Zeeb 	IMPROVE_HT();
37749fb91463SBjoern A. Zeeb 
37759fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
37769fb91463SBjoern A. Zeeb }
37779fb91463SBjoern A. Zeeb 
37789fb91463SBjoern A. Zeeb 
37799fb91463SBjoern A. Zeeb static int
37809fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
37819fb91463SBjoern A. Zeeb {
37829fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
37839fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37849fb91463SBjoern A. Zeeb 
37859fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
37869fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
37879fb91463SBjoern A. Zeeb 
37889fb91463SBjoern A. Zeeb 	IMPROVE_HT();
37899fb91463SBjoern A. Zeeb 
37909fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
37919fb91463SBjoern A. Zeeb }
37929fb91463SBjoern A. Zeeb 
37939fb91463SBjoern A. Zeeb static int
37949fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
37959fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
37969fb91463SBjoern A. Zeeb {
37979fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
37989fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37999fb91463SBjoern A. Zeeb 
38009fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38019fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38029fb91463SBjoern A. Zeeb 
38039fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38049fb91463SBjoern A. Zeeb 
38059fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
38069fb91463SBjoern A. Zeeb }
38079fb91463SBjoern A. Zeeb 
38089fb91463SBjoern A. Zeeb static int
38099fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
38109fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
38119fb91463SBjoern A. Zeeb {
38129fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38139fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38149fb91463SBjoern A. Zeeb 
38159fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38169fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38179fb91463SBjoern A. Zeeb 
38189fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38199fb91463SBjoern A. Zeeb 
38209fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
38219fb91463SBjoern A. Zeeb }
38229fb91463SBjoern A. Zeeb 
38239fb91463SBjoern A. Zeeb static void
38249fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38259fb91463SBjoern A. Zeeb {
38269fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38279fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38289fb91463SBjoern A. Zeeb 
38299fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38309fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38319fb91463SBjoern A. Zeeb 
38329fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38339fb91463SBjoern A. Zeeb 
38349fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
38359fb91463SBjoern A. Zeeb }
38369fb91463SBjoern A. Zeeb 
38379fb91463SBjoern A. Zeeb static void
38389fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38399fb91463SBjoern A. Zeeb {
38409fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38419fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38429fb91463SBjoern A. Zeeb 
38439fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38449fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38459fb91463SBjoern A. Zeeb 
38469fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38479fb91463SBjoern A. Zeeb 
38489fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
38499fb91463SBjoern A. Zeeb }
38509fb91463SBjoern A. Zeeb 
38519fb91463SBjoern A. Zeeb static void
38529fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
38539fb91463SBjoern A. Zeeb     int status)
38549fb91463SBjoern A. Zeeb {
38559fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38569fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38579fb91463SBjoern A. Zeeb 
38589fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38599fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38609fb91463SBjoern A. Zeeb 
38619fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38629fb91463SBjoern A. Zeeb 
38639fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
38649fb91463SBjoern A. Zeeb }
38659fb91463SBjoern A. Zeeb 
38669fb91463SBjoern A. Zeeb static int
38679fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
38689fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
38699fb91463SBjoern A. Zeeb {
38709fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38719fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38729fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38739fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
38749fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38759fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38769fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
38779fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
38789fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
38799fb91463SBjoern A. Zeeb 	int error;
38809fb91463SBjoern A. Zeeb 
38819fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38829fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38839fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38849fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
38859fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
38869fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
38879fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
38889fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
38899fb91463SBjoern A. Zeeb 
38909fb91463SBjoern A. Zeeb 	params.sta = sta;
38919fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
38929fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
38939fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
38949fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
38959fb91463SBjoern A. Zeeb 	else
38969fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
38979fb91463SBjoern A. Zeeb 	if (params.buf_size > hw->max_rx_aggregation_subframes)
38989fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
38999fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
39009fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
39019fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
39029fb91463SBjoern A. Zeeb 	params.amsdu = false;
39039fb91463SBjoern A. Zeeb 
39049fb91463SBjoern A. Zeeb 	IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
39059fb91463SBjoern A. Zeeb 
39069fb91463SBjoern A. Zeeb 	/* This may call kalloc.  Make sure we can sleep. */
39079fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
39089fb91463SBjoern A. Zeeb 	if (error != 0) {
39099fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
39109fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
39119fb91463SBjoern A. Zeeb 		return (error);
39129fb91463SBjoern A. Zeeb 	}
39139fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
39149fb91463SBjoern A. Zeeb 
39159fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
39169fb91463SBjoern A. Zeeb 	return (error);
39179fb91463SBjoern A. Zeeb }
39189fb91463SBjoern A. Zeeb 
39199fb91463SBjoern A. Zeeb static void
39209fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
39219fb91463SBjoern A. Zeeb {
39229fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39239fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39249fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39259fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
39269fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39279fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39289fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39299fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
39309fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
39319fb91463SBjoern A. Zeeb 	int error;
39329fb91463SBjoern A. Zeeb 	uint8_t tid;
39339fb91463SBjoern A. Zeeb 
39349fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39359fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39369fb91463SBjoern A. Zeeb 
39379fb91463SBjoern A. Zeeb 	/*
39389fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
39399fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
39409fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
39419fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
39429fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
39439fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
39449fb91463SBjoern A. Zeeb 	 * track some state itself.
39459fb91463SBjoern A. Zeeb 	 */
39469fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
39479fb91463SBjoern A. Zeeb 		goto net80211_only;
39489fb91463SBjoern A. Zeeb 
39499fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39509fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
39519fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
39529fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
39539fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
39549fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
39559fb91463SBjoern A. Zeeb 
39569fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
39579fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
39589fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
39599fb91463SBjoern A. Zeeb 			break;
39609fb91463SBjoern A. Zeeb 	}
39619fb91463SBjoern A. Zeeb 
39629fb91463SBjoern A. Zeeb 	params.sta = sta;
39639fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
39649fb91463SBjoern A. Zeeb 	params.buf_size = 0;
39659fb91463SBjoern A. Zeeb 	params.timeout = 0;
39669fb91463SBjoern A. Zeeb 	params.ssn = 0;
39679fb91463SBjoern A. Zeeb 	params.tid = tid;
39689fb91463SBjoern A. Zeeb 	params.amsdu = false;
39699fb91463SBjoern A. Zeeb 
39709fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
39719fb91463SBjoern A. Zeeb 	if (error != 0)
39729fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
39739fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
39749fb91463SBjoern A. Zeeb 
39759fb91463SBjoern A. Zeeb net80211_only:
39769fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
39779fb91463SBjoern A. Zeeb }
39789fb91463SBjoern A. Zeeb #endif
39799fb91463SBjoern A. Zeeb 
39809fb91463SBjoern A. Zeeb static void
39819fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
39829fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
39839fb91463SBjoern A. Zeeb {
39849fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
39859fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
39869fb91463SBjoern A. Zeeb 
39879fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
39889fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
39899fb91463SBjoern A. Zeeb 		return;
39909fb91463SBjoern A. Zeeb 
39919fb91463SBjoern A. Zeeb 	switch (band) {
39929fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
39939fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
39949fb91463SBjoern A. Zeeb 		break;
39959fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
39969fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
39979fb91463SBjoern A. Zeeb 		break;
39989fb91463SBjoern A. Zeeb 	default:
39999fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
40009fb91463SBjoern A. Zeeb 		return;
40019fb91463SBjoern A. Zeeb 	}
40029fb91463SBjoern A. Zeeb 
40039fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
40049fb91463SBjoern A. Zeeb 
40059fb91463SBjoern A. Zeeb 	/*
40069fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
40079fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
40089fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
40099fb91463SBjoern A. Zeeb 	 */
40109fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
40119fb91463SBjoern A. Zeeb 
40129fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
40139fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
40149fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
40159fb91463SBjoern A. Zeeb #ifdef __notyet__
40169fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
40179fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
40189fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
40199fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
40209fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
40219fb91463SBjoern A. Zeeb #endif
40229fb91463SBjoern A. Zeeb 
40239fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
40249fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
40259fb91463SBjoern A. Zeeb 
40269fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
40279fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
40289fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
40299fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
40309fb91463SBjoern A. Zeeb #endif
40319fb91463SBjoern A. Zeeb }
40329fb91463SBjoern A. Zeeb 
40336b4cac81SBjoern A. Zeeb static void
40346b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
40356b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
40366b4cac81SBjoern A. Zeeb {
40376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40396b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
40406b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
40416b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
40426b4cac81SBjoern A. Zeeb 
40436b4cac81SBjoern A. Zeeb 	/* Channels */
40446b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40456b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40466b4cac81SBjoern A. Zeeb 
40476b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
40486b4cac81SBjoern A. Zeeb 	nchans = 0;
40496b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
40506b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
40516b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
40526b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
40536b4cac81SBjoern A. Zeeb 		chan_flags = 0;
40546b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
40556b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
40569fb91463SBjoern A. Zeeb 
40579fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
40586b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
40599fb91463SBjoern A. Zeeb 
40609fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
40619fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
40626b4cac81SBjoern A. Zeeb 
40636b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4064cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
40656b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
40666b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
40676b4cac81SBjoern A. Zeeb 
40686b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
40693f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
40703f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
40716b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
40726b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
40736b4cac81SBjoern A. Zeeb 				continue;
40746b4cac81SBjoern A. Zeeb 			}
40756b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
40766b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
40776b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
40786b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
40796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
40806b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
40816b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
40826b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
40836b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
40846b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
40856b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
40866b4cac81SBjoern A. Zeeb 
40876b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
40886b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
40896b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
40905856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4091cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4092cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
40933f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
40943f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
40956b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
40966b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
40976b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4098cee56e77SBjoern A. Zeeb 			if (error != 0)
40996b4cac81SBjoern A. Zeeb 				break;
41006b4cac81SBjoern A. Zeeb 		}
41016b4cac81SBjoern A. Zeeb 	}
41026b4cac81SBjoern A. Zeeb 
41036b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
41046b4cac81SBjoern A. Zeeb 	nchans = 0;
41056b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
41066b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
41076b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
41086b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
41096b4cac81SBjoern A. Zeeb 		chan_flags = 0;
41106b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
41119fb91463SBjoern A. Zeeb 
41129fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
41139fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
41149fb91463SBjoern A. Zeeb 
41159fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
41166b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
41176b4cac81SBjoern A. Zeeb 
41186b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4119562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
41206b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
41216b4cac81SBjoern A. Zeeb 
41226b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
41236b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
41246b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4125562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
41266b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
41276b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4128562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
41296b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
41306b4cac81SBjoern A. Zeeb 		}
41316b4cac81SBjoern A. Zeeb #endif
41326b4cac81SBjoern A. Zeeb 
41336b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4134cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
41356b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
41366b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
41376b4cac81SBjoern A. Zeeb 
41386b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
41393f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
41403f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
41416b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
41426b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
41436b4cac81SBjoern A. Zeeb 				continue;
41446b4cac81SBjoern A. Zeeb 			}
41456b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
41466b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
41476b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
41486b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
41496b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
41506b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
41516b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
41526b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
41536b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
41546b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
41556b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
41566b4cac81SBjoern A. Zeeb 
41576b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
41586b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
41596b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
41605856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4161cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4162cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
41633f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
41643f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
41656b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
41666b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
41676b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4168cee56e77SBjoern A. Zeeb 			if (error != 0)
41696b4cac81SBjoern A. Zeeb 				break;
41706b4cac81SBjoern A. Zeeb 		}
41716b4cac81SBjoern A. Zeeb 	}
41726b4cac81SBjoern A. Zeeb }
41736b4cac81SBjoern A. Zeeb 
41746b4cac81SBjoern A. Zeeb static void *
41756b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
41766b4cac81SBjoern A. Zeeb {
41776b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
41786b4cac81SBjoern A. Zeeb 
41796b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
41806b4cac81SBjoern A. Zeeb 	if (ic == NULL)
41816b4cac81SBjoern A. Zeeb 		return (NULL);
41826b4cac81SBjoern A. Zeeb 
41836b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
41846b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
41856b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
41866b4cac81SBjoern A. Zeeb 
41876b4cac81SBjoern A. Zeeb 	return (ic);
41886b4cac81SBjoern A. Zeeb }
41896b4cac81SBjoern A. Zeeb 
41906b4cac81SBjoern A. Zeeb struct ieee80211_hw *
41916b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
41926b4cac81SBjoern A. Zeeb {
41936b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41946b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41956b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4196a5ae63edSBjoern A. Zeeb 	int ac;
41976b4cac81SBjoern A. Zeeb 
41986b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
41996b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
42006b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
42016b4cac81SBjoern A. Zeeb 		return (NULL);
42026b4cac81SBjoern A. Zeeb 
42036b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
42046b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4205652e22d3SBjoern A. Zeeb 
42068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
42078ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4208eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
42098891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
42106b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4211a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4212a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4213a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4214a5ae63edSBjoern A. Zeeb 	}
42156b4cac81SBjoern A. Zeeb 
42166b4cac81SBjoern A. Zeeb 	/*
42176b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
42186b4cac81SBjoern A. Zeeb 	 * not initialized.
42196b4cac81SBjoern A. Zeeb 	 */
42206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42216b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4222086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
42236b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
42246b4cac81SBjoern A. Zeeb 
42256b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
42266b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
42276b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
42286b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
42296b4cac81SBjoern A. Zeeb 		return (NULL);
42306b4cac81SBjoern A. Zeeb 	}
42316b4cac81SBjoern A. Zeeb 
42326b4cac81SBjoern A. Zeeb 	IMPROVE();
42336b4cac81SBjoern A. Zeeb 
42346b4cac81SBjoern A. Zeeb 	return (hw);
42356b4cac81SBjoern A. Zeeb }
42366b4cac81SBjoern A. Zeeb 
42376b4cac81SBjoern A. Zeeb void
42386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
42396b4cac81SBjoern A. Zeeb {
42406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42416b4cac81SBjoern A. Zeeb 
42426b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
42436b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
42446b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
42456b4cac81SBjoern A. Zeeb 
42466b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4247eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
42488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4249eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4250eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
42516b4cac81SBjoern A. Zeeb 	IMPROVE();
42526b4cac81SBjoern A. Zeeb }
42536b4cac81SBjoern A. Zeeb 
42546b4cac81SBjoern A. Zeeb void
42556b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
42566b4cac81SBjoern A. Zeeb {
42576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42586b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42596b4cac81SBjoern A. Zeeb 
42606b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
42616b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
42626b4cac81SBjoern A. Zeeb 
42636b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
42646b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
42656b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
42666b4cac81SBjoern A. Zeeb 
42676b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
42686b4cac81SBjoern A. Zeeb }
42696b4cac81SBjoern A. Zeeb 
42706b4cac81SBjoern A. Zeeb struct ieee80211_hw *
42716b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
42726b4cac81SBjoern A. Zeeb {
42736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42746b4cac81SBjoern A. Zeeb 
42756b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
42766b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
42776b4cac81SBjoern A. Zeeb }
42786b4cac81SBjoern A. Zeeb 
42796b4cac81SBjoern A. Zeeb static void
42806b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
42816b4cac81SBjoern A. Zeeb {
42826b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42836b4cac81SBjoern A. Zeeb 
42846b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
42856b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
42866b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
42876b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
42886b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
42896b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
42906b4cac81SBjoern A. Zeeb }
42916b4cac81SBjoern A. Zeeb 
42922e183d99SBjoern A. Zeeb int
42936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
42946b4cac81SBjoern A. Zeeb {
42956b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4297c5b96b3eSBjoern A. Zeeb 	int band, i;
42986b4cac81SBjoern A. Zeeb 
42996b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
43006b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
43016b4cac81SBjoern A. Zeeb 
4302652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
4303652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4304652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
4305652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
4306652e22d3SBjoern A. Zeeb 
43076b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
43086b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
43096b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
43106b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
43116b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
43126b4cac81SBjoern A. Zeeb 		/* We take the first one. */
43136b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
43146b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
43156b4cac81SBjoern A. Zeeb 	} else {
43166b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
43176b4cac81SBjoern A. Zeeb 	}
43186b4cac81SBjoern A. Zeeb 
43193d09d310SBjoern A. Zeeb #ifdef __not_yet__
43203d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
43216b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
43223d09d310SBjoern A. Zeeb #endif
43236b4cac81SBjoern A. Zeeb 
43246b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
43256b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
43266b4cac81SBjoern A. Zeeb 
43276b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
43286b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
43296b4cac81SBjoern A. Zeeb 	ic->ic_caps =
43306b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
43316b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
43326b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
43334a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
43346b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
43354a67f1dfSBjoern A. Zeeb #endif
43366b4cac81SBjoern A. Zeeb #if 0
43376b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
43386b4cac81SBjoern A. Zeeb #endif
43396b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
43406b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
43416b4cac81SBjoern A. Zeeb 	    ;
43426b4cac81SBjoern A. Zeeb #if 0
43436b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
43446b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
43456b4cac81SBjoern A. Zeeb #endif
4346cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
4347cc4e78d5SBjoern A. Zeeb 		/*
4348cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
4349cc4e78d5SBjoern A. Zeeb 		 *
4350cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4351cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
4352cc4e78d5SBjoern A. Zeeb 		 * the flag.
4353cc4e78d5SBjoern A. Zeeb 		 */
43546b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4355a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
43566b4cac81SBjoern A. Zeeb 	}
43576b4cac81SBjoern A. Zeeb 
4358527687a9SBjoern A. Zeeb 	/*
4359527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
4360527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
4361527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
4362527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4363527687a9SBjoern A. Zeeb 	 */
4364527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
4365527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
4366527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
4367527687a9SBjoern A. Zeeb 
4368527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4369527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
4370527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
4371527687a9SBjoern A. Zeeb 		}
4372527687a9SBjoern A. Zeeb 	}
4373527687a9SBjoern A. Zeeb 
43746b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
4375b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
43766b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
43776b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
43786b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
43796b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
43806b4cac81SBjoern A. Zeeb 	}
43816b4cac81SBjoern A. Zeeb #endif
43826b4cac81SBjoern A. Zeeb 
43836b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
43846b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
43856b4cac81SBjoern A. Zeeb 
43866b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
43876b4cac81SBjoern A. Zeeb 
43886b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
43896b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
43906b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
43916b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
43926b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
43936b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
43946b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
43956b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
43966b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
43976b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
43986b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
43996b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
44006b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
44016b4cac81SBjoern A. Zeeb 
4402a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4403a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4404a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4405a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4406a486fbbdSBjoern A. Zeeb 
44076b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
44086b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
44096b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
44106b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
44116b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
44126b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
44136b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
44146b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
44156b4cac81SBjoern A. Zeeb 
44169fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
44179fb91463SBjoern A. Zeeb 	lhw->ic_recv_action = ic->ic_recv_action;
44189fb91463SBjoern A. Zeeb 	ic->ic_recv_action = lkpi_ic_recv_action;
44199fb91463SBjoern A. Zeeb 	lhw->ic_send_action = ic->ic_send_action;
44209fb91463SBjoern A. Zeeb 	ic->ic_send_action = lkpi_ic_send_action;
44219fb91463SBjoern A. Zeeb 
44229fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
44239fb91463SBjoern A. Zeeb 	ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
44249fb91463SBjoern A. Zeeb 
44259fb91463SBjoern A. Zeeb 	lhw->ic_addba_request = ic->ic_addba_request;
44269fb91463SBjoern A. Zeeb 	ic->ic_addba_request = lkpi_ic_addba_request;
44279fb91463SBjoern A. Zeeb 	lhw->ic_addba_response = ic->ic_addba_response;
44289fb91463SBjoern A. Zeeb 	ic->ic_addba_response = lkpi_ic_addba_response;
44299fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop = ic->ic_addba_stop;
44309fb91463SBjoern A. Zeeb 	ic->ic_addba_stop = lkpi_ic_addba_stop;
44319fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
44329fb91463SBjoern A. Zeeb 	ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
44339fb91463SBjoern A. Zeeb 
44349fb91463SBjoern A. Zeeb 	lhw->ic_bar_response = ic->ic_bar_response;
44359fb91463SBjoern A. Zeeb 	ic->ic_bar_response = lkpi_ic_bar_response;
44369fb91463SBjoern A. Zeeb 
44379fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
44389fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
44399fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
44409fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
44419fb91463SBjoern A. Zeeb #endif
44429fb91463SBjoern A. Zeeb 
44436b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
44446b4cac81SBjoern A. Zeeb 
4445c5b96b3eSBjoern A. Zeeb 	/*
4446c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
4447c5b96b3eSBjoern A. Zeeb 	 * expect one.
4448d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
4449d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4450c5b96b3eSBjoern A. Zeeb 	 */
4451d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
4452f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4453c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
4454c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
4455c5b96b3eSBjoern A. Zeeb 
4456c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4457c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4458c5b96b3eSBjoern A. Zeeb 			continue;
4459c5b96b3eSBjoern A. Zeeb 
4460d9945d78SBjoern A. Zeeb 		lhw->supbands++;
4461d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4462d9945d78SBjoern A. Zeeb 
4463f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
4464f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
4465f454a4a1SBjoern A. Zeeb 			continue;
4466f454a4a1SBjoern A. Zeeb 
4467c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
4468c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4469c5b96b3eSBjoern A. Zeeb 
4470c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4471c5b96b3eSBjoern A. Zeeb 				continue;
4472c5b96b3eSBjoern A. Zeeb 
44734a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
44749fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
44759fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
44769fb91463SBjoern A. Zeeb #endif
4477c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
4478c5b96b3eSBjoern A. Zeeb 			break;
4479c5b96b3eSBjoern A. Zeeb 		}
4480c5b96b3eSBjoern A. Zeeb 	}
4481c5b96b3eSBjoern A. Zeeb 
4482d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4483d9945d78SBjoern A. Zeeb 
4484d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
4485d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4486d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4487d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4488d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4489d9945d78SBjoern A. Zeeb 	}
4490d9945d78SBjoern A. Zeeb 
4491d9945d78SBjoern A. Zeeb 	/*
4492d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
4493d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
4494d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
4495d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
4496d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
4497d9945d78SBjoern A. Zeeb 	 */
4498d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4499d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4500d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
450178ca45dfSBjoern A. Zeeb 
450278ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4503d9945d78SBjoern A. Zeeb 		/*
450478ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
450578ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
450678ca45dfSBjoern A. Zeeb 		 * space in.
4507d9945d78SBjoern A. Zeeb 		 */
4508d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
450978ca45dfSBjoern A. Zeeb 	}
4510d9945d78SBjoern A. Zeeb 
45119fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
45129fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
45139fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
45149fb91463SBjoern A. Zeeb #endif
45159fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
45169fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
45179fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
45189fb91463SBjoern A. Zeeb #endif
45199fb91463SBjoern A. Zeeb 
4520d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
452178ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
452278ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
452378ca45dfSBjoern A. Zeeb 			goto err;
4524d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
452578ca45dfSBjoern A. Zeeb 	}
4526d9945d78SBjoern A. Zeeb 
45276b4cac81SBjoern A. Zeeb 	if (bootverbose)
45286b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
45292e183d99SBjoern A. Zeeb 
45302e183d99SBjoern A. Zeeb 	return (0);
453178ca45dfSBjoern A. Zeeb err:
453278ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
453378ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
45346b4cac81SBjoern A. Zeeb }
45356b4cac81SBjoern A. Zeeb 
45366b4cac81SBjoern A. Zeeb void
45376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
45386b4cac81SBjoern A. Zeeb {
45396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45406b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45416b4cac81SBjoern A. Zeeb 
45426b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
45436b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
45446b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
45456b4cac81SBjoern A. Zeeb }
45466b4cac81SBjoern A. Zeeb 
45476b4cac81SBjoern A. Zeeb void
45486b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
45496b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
45506b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
45516b4cac81SBjoern A. Zeeb     void *arg)
45526b4cac81SBjoern A. Zeeb {
45536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45546b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
45556b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
455661a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
45576b4cac81SBjoern A. Zeeb 
45586b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
45596b4cac81SBjoern A. Zeeb 
45606b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
45616b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
456261a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4563adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
45646b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
45656b4cac81SBjoern A. Zeeb 		    __func__, flags);
45666b4cac81SBjoern A. Zeeb 	}
45676b4cac81SBjoern A. Zeeb 
4568adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
45696b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
457061a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
45716b4cac81SBjoern A. Zeeb 
45726b4cac81SBjoern A. Zeeb 	if (atomic)
45738891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
45746b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
45756b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
45766b4cac81SBjoern A. Zeeb 
45776b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
45786b4cac81SBjoern A. Zeeb 
45796b4cac81SBjoern A. Zeeb 		/*
45806b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
45816b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
45826b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
45836b4cac81SBjoern A. Zeeb 		 * driver does not know about.
45846b4cac81SBjoern A. Zeeb 		 */
45856b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
45866b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
45876b4cac81SBjoern A. Zeeb 			continue;
45886b4cac81SBjoern A. Zeeb 
45896b4cac81SBjoern A. Zeeb 		/*
459061a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
459161a68e50SBjoern A. Zeeb 		 * if we haven't yet.
459261a68e50SBjoern A. Zeeb 		 */
459361a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
459461a68e50SBjoern A. Zeeb 			continue;
459561a68e50SBjoern A. Zeeb 
459661a68e50SBjoern A. Zeeb 		/*
45976b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
45986b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
45996b4cac81SBjoern A. Zeeb 		 */
46006b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
46016b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
46026b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
46036b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
46046b4cac81SBjoern A. Zeeb 	}
46056b4cac81SBjoern A. Zeeb 	if (atomic)
46068891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
46076b4cac81SBjoern A. Zeeb }
46086b4cac81SBjoern A. Zeeb 
46096b4cac81SBjoern A. Zeeb void
46106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
46116b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
46126b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
46136b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
46146b4cac81SBjoern A. Zeeb     void *arg)
46156b4cac81SBjoern A. Zeeb {
46166b4cac81SBjoern A. Zeeb 
46176b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
46186b4cac81SBjoern A. Zeeb }
46196b4cac81SBjoern A. Zeeb 
46206b4cac81SBjoern A. Zeeb void
46216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
46226b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
46236b4cac81SBjoern A. Zeeb 	void *),
46246b4cac81SBjoern A. Zeeb     void *arg)
46256b4cac81SBjoern A. Zeeb {
4626c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4627c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4628c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4629c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
46306b4cac81SBjoern A. Zeeb 
4631c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
4632c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4633c5e25798SBjoern A. Zeeb 
4634c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
4635c5e25798SBjoern A. Zeeb 
4636c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
4637c5e25798SBjoern A. Zeeb 
4638c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4639c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4640c5e25798SBjoern A. Zeeb 
4641c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4642c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
4643c5e25798SBjoern A. Zeeb 			continue;
4644c5e25798SBjoern A. Zeeb 
4645c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4646c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
4647c5e25798SBjoern A. Zeeb 			continue;
4648c5e25798SBjoern A. Zeeb 
4649c5e25798SBjoern A. Zeeb 		iterfunc(hw, &lchanctx->conf, arg);
4650c5e25798SBjoern A. Zeeb 	}
4651c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
46526b4cac81SBjoern A. Zeeb }
46536b4cac81SBjoern A. Zeeb 
46546b4cac81SBjoern A. Zeeb void
46556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
46566b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
46576b4cac81SBjoern A. Zeeb {
46586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
46606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46616b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
46626b4cac81SBjoern A. Zeeb 
46636b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
46646b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
46656b4cac81SBjoern A. Zeeb 
46666b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
46676b4cac81SBjoern A. Zeeb 
46688891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
46696b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
46706b4cac81SBjoern A. Zeeb 
46716b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
46726b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
46736b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
46746b4cac81SBjoern A. Zeeb 				continue;
46756b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
46766b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
46776b4cac81SBjoern A. Zeeb 		}
46786b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
46796b4cac81SBjoern A. Zeeb 	}
46808891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
46816b4cac81SBjoern A. Zeeb }
46826b4cac81SBjoern A. Zeeb 
4683673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
4684673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4685673d62fcSBjoern A. Zeeb {
4686673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
4687673d62fcSBjoern A. Zeeb 
4688673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4689673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
4690673d62fcSBjoern A. Zeeb 	return (regd);
4691673d62fcSBjoern A. Zeeb }
4692673d62fcSBjoern A. Zeeb 
46936b4cac81SBjoern A. Zeeb int
46946b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
46956b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
46966b4cac81SBjoern A. Zeeb {
46976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46986b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46996b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
47006b4cac81SBjoern A. Zeeb 
47016b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
47026b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
47036b4cac81SBjoern A. Zeeb 
47046b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
47056b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
47066b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
47076b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
47086b4cac81SBjoern A. Zeeb 	}
47096b4cac81SBjoern A. Zeeb 
47106b4cac81SBjoern A. Zeeb 	TODO();
47116b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
47126b4cac81SBjoern A. Zeeb 
47136b4cac81SBjoern A. Zeeb 	return (0);
47146b4cac81SBjoern A. Zeeb }
47156b4cac81SBjoern A. Zeeb 
47166b4cac81SBjoern A. Zeeb void
47176b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
47186b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
47196b4cac81SBjoern A. Zeeb {
47206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47216b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47226b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
47236b4cac81SBjoern A. Zeeb 
47246b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
47256b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
47266b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
47276b4cac81SBjoern A. Zeeb 
47286b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
47296b4cac81SBjoern A. Zeeb 
47308ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
47316b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
47326b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
4733a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
47346b4cac81SBjoern A. Zeeb 	wakeup(lhw);
47358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
47366b4cac81SBjoern A. Zeeb 
47376b4cac81SBjoern A. Zeeb 	return;
47386b4cac81SBjoern A. Zeeb }
47396b4cac81SBjoern A. Zeeb 
4740e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
47416b4cac81SBjoern A. Zeeb void
47426b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4743e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4744e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
47456b4cac81SBjoern A. Zeeb {
47466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47476b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47486b4cac81SBjoern A. Zeeb 	struct mbuf *m;
47496b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
47506b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
47516b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
47526b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47536b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
47546b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
47556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
47569d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
4757170acccfSBjoern A. Zeeb 	int8_t rssi;
4758c0cadd99SBjoern A. Zeeb 	bool is_beacon;
47596b4cac81SBjoern A. Zeeb 
47606b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
47616b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
47626b4cac81SBjoern A. Zeeb 		IMPROVE();
47636b4cac81SBjoern A. Zeeb 		goto err;
47646b4cac81SBjoern A. Zeeb 	}
47656b4cac81SBjoern A. Zeeb 
47666b4cac81SBjoern A. Zeeb 	/*
47676b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
47686b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
47696b4cac81SBjoern A. Zeeb 	 */
47706b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
47716b4cac81SBjoern A. Zeeb 	if (m == NULL)
47726b4cac81SBjoern A. Zeeb 		goto err;
47736b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
47746b4cac81SBjoern A. Zeeb 
47756b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
47766b4cac81SBjoern A. Zeeb 	offset = m->m_len;
47776b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
47786b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
47796b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
47806b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
47816b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
47826b4cac81SBjoern A. Zeeb 	}
47836b4cac81SBjoern A. Zeeb 
47846b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
47856b4cac81SBjoern A. Zeeb 
47866b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
4787c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4788c0cadd99SBjoern A. Zeeb 
4789c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47909d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
47916b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
47926b4cac81SBjoern A. Zeeb 
47939d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
47946b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4795c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
47966b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
47976b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
47986b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
4799c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
48006b4cac81SBjoern A. Zeeb 
48019d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
48026b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
48036b4cac81SBjoern A. Zeeb 
48046b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
48059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4806c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
480760970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
48086b4cac81SBjoern A. Zeeb 			__func__,
4809c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
4810c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
48116b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
48126b4cac81SBjoern A. Zeeb 			rx_status->flag,
48136b4cac81SBjoern A. Zeeb 			rx_status->freq,
48146b4cac81SBjoern A. Zeeb 			rx_status->bw,
48156b4cac81SBjoern A. Zeeb 			rx_status->encoding,
48166b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
48176b4cac81SBjoern A. Zeeb 			rx_status->band,
48186b4cac81SBjoern A. Zeeb 			rx_status->chains,
48196b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
48206b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
48216b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
482260970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
48236b4cac81SBjoern A. Zeeb 			rx_status->signal,
48246b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
48256b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
48266b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
48276b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
48286b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
48296b4cac81SBjoern A. Zeeb 			rx_status->nss,
48306b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
48316b4cac81SBjoern A. Zeeb no_trace_beacons:
48326b4cac81SBjoern A. Zeeb #endif
48336b4cac81SBjoern A. Zeeb 
48346b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
48356b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
483660970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
483760970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
48386b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
48396b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4840170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
48416b4cac81SBjoern A. Zeeb 	else
4842170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
4843170acccfSBjoern A. Zeeb 	/*
4844170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
4845170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
4846170acccfSBjoern A. Zeeb 	 */
4847170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
4848170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
48496b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
48506b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
48516b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
48526b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
48536b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
48546b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
48556b4cac81SBjoern A. Zeeb 
48566b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
48576b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
48586b4cac81SBjoern A. Zeeb 
48596b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
48606b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48616b4cac81SBjoern A. Zeeb 
48626b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
48636b4cac81SBjoern A. Zeeb 	if (ok == 0) {
4864dbc06dd9SBjoern A. Zeeb 		m_freem(m);
48656b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
48666b4cac81SBjoern A. Zeeb 		goto err;
48676b4cac81SBjoern A. Zeeb 	}
48686b4cac81SBjoern A. Zeeb 
48696b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
48706b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
48716b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
48726b4cac81SBjoern A. Zeeb 	} else {
4873c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
4874c8dafefaSBjoern A. Zeeb 
48756b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
48766b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
48776b4cac81SBjoern A. Zeeb 		if (ni != NULL)
48786b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
48796b4cac81SBjoern A. Zeeb 	}
48806b4cac81SBjoern A. Zeeb 
48816b4cac81SBjoern A. Zeeb 	if (ni != NULL)
48826b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
48836b4cac81SBjoern A. Zeeb 	else
48846b4cac81SBjoern A. Zeeb 		/*
48856b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
48866b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
48876b4cac81SBjoern A. Zeeb 		 */
48886b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
48896b4cac81SBjoern A. Zeeb 
48909d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
48919d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4892c0cadd99SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
4893c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
4894c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
48959d9ba2b7SBjoern A. Zeeb #endif
48966b4cac81SBjoern A. Zeeb 
4897c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
4898c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
4899c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
4900c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
4901c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
4902c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
4903c8dafefaSBjoern A. Zeeb 
4904c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
4905c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
4906c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
4907c8dafefaSBjoern A. Zeeb 
4908c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
4909c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4910c8dafefaSBjoern A. Zeeb 
4911c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
4912c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
4913c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
4914c8dafefaSBjoern A. Zeeb 		/*
4915c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
4916c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
4917c8dafefaSBjoern A. Zeeb 		 */
4918c8dafefaSBjoern A. Zeeb skip_device_ts:
49199fb91463SBjoern A. Zeeb 		;
49209fb91463SBjoern A. Zeeb 	}
4921c8dafefaSBjoern A. Zeeb 
49226b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
49236b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
49246b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
49256b4cac81SBjoern A. Zeeb 
49266b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
49276b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
49286b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
49296b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
49306b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
49316b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
49326b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
49336b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
49346b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
49356b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
49366b4cac81SBjoern A. Zeeb #endif
49376b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
49386b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
49396b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
49406b4cac81SBjoern A. Zeeb 		IMPROVE();
49416b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
49426b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
49436b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
49446b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4945170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
49466b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
49476b4cac81SBjoern A. Zeeb 	}
49486b4cac81SBjoern A. Zeeb 
49496b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
49506b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
49516b4cac81SBjoern A. Zeeb 
4952e30e05d3SBjoern A. Zeeb #if 0
4953e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
4954e30e05d3SBjoern A. Zeeb 		/*
4955e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
4956e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
4957e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
4958e30e05d3SBjoern A. Zeeb 		*/
4959e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
4960e30e05d3SBjoern A. Zeeb 	}
4961e30e05d3SBjoern A. Zeeb #endif
4962e30e05d3SBjoern A. Zeeb 
49636b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
49649d9ba2b7SBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
49656b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
4966dbc06dd9SBjoern A. Zeeb 		if (ok < 0)
4967dbc06dd9SBjoern A. Zeeb 			m_freem(m);
49686b4cac81SBjoern A. Zeeb 	} else {
49699d9ba2b7SBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(ic, m);
4970dbc06dd9SBjoern A. Zeeb 		/* mbuf got consumed. */
49716b4cac81SBjoern A. Zeeb 	}
49726b4cac81SBjoern A. Zeeb 
49739d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49749d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
49759d9ba2b7SBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
49769d9ba2b7SBjoern A. Zeeb #endif
49776b4cac81SBjoern A. Zeeb 
49786b4cac81SBjoern A. Zeeb 	IMPROVE();
49796b4cac81SBjoern A. Zeeb 
49806b4cac81SBjoern A. Zeeb err:
49816b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
49826b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
49836b4cac81SBjoern A. Zeeb }
49846b4cac81SBjoern A. Zeeb 
49856b4cac81SBjoern A. Zeeb uint8_t
4986ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
49876b4cac81SBjoern A. Zeeb {
49886b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
4989ec190d91SBjoern A. Zeeb 	uint8_t tid;
4990ec190d91SBjoern A. Zeeb 
4991ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
4992ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
4993ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
4994ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
49956b4cac81SBjoern A. Zeeb 
49966b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
4997ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
4998ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
4999ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5000ec190d91SBjoern A. Zeeb 
5001ec190d91SBjoern A. Zeeb 	return (tid);
50026b4cac81SBjoern A. Zeeb }
50036b4cac81SBjoern A. Zeeb 
50046b4cac81SBjoern A. Zeeb struct wiphy *
50056b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
50066b4cac81SBjoern A. Zeeb {
50076b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
50086b4cac81SBjoern A. Zeeb 
50096b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
50106b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
50116b4cac81SBjoern A. Zeeb 		return (NULL);
50126b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
50136b4cac81SBjoern A. Zeeb 
50146b4cac81SBjoern A. Zeeb 	/* XXX TODO */
50156b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
50166b4cac81SBjoern A. Zeeb }
50176b4cac81SBjoern A. Zeeb 
50186b4cac81SBjoern A. Zeeb void
50196b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
50206b4cac81SBjoern A. Zeeb {
50216b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
50226b4cac81SBjoern A. Zeeb 
50236b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
50246b4cac81SBjoern A. Zeeb 		return;
50256b4cac81SBjoern A. Zeeb 
50266b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
50276b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
50286b4cac81SBjoern A. Zeeb }
50296b4cac81SBjoern A. Zeeb 
50306b4cac81SBjoern A. Zeeb uint32_t
50316b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
50326b4cac81SBjoern A. Zeeb     enum nl80211_band band)
50336b4cac81SBjoern A. Zeeb {
50346b4cac81SBjoern A. Zeeb 
50356b4cac81SBjoern A. Zeeb 	switch (band) {
50366b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
50376b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
50386b4cac81SBjoern A. Zeeb 		break;
50396b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
50406b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
50416b4cac81SBjoern A. Zeeb 		break;
50426b4cac81SBjoern A. Zeeb 	default:
50436b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
50446b4cac81SBjoern A. Zeeb 		break;
50456b4cac81SBjoern A. Zeeb 	}
50466b4cac81SBjoern A. Zeeb 
50476b4cac81SBjoern A. Zeeb 	return (0);
50486b4cac81SBjoern A. Zeeb }
50496b4cac81SBjoern A. Zeeb 
50506b4cac81SBjoern A. Zeeb uint32_t
50516b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
50526b4cac81SBjoern A. Zeeb {
50536b4cac81SBjoern A. Zeeb 
50546b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
50556b4cac81SBjoern A. Zeeb }
50566b4cac81SBjoern A. Zeeb 
50576b4cac81SBjoern A. Zeeb static struct lkpi_sta *
50586b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
50596b4cac81SBjoern A. Zeeb {
50606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
50616b4cac81SBjoern A. Zeeb 
50626b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
50636b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
50646b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
50656b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
50666b4cac81SBjoern A. Zeeb 			return (lsta);
50676b4cac81SBjoern A. Zeeb 		}
50686b4cac81SBjoern A. Zeeb 	}
50696b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
50706b4cac81SBjoern A. Zeeb 
50716b4cac81SBjoern A. Zeeb 	return (NULL);
50726b4cac81SBjoern A. Zeeb }
50736b4cac81SBjoern A. Zeeb 
50746b4cac81SBjoern A. Zeeb struct ieee80211_sta *
50756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
50766b4cac81SBjoern A. Zeeb {
50776b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50786b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
50796b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
50806b4cac81SBjoern A. Zeeb 
50816b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
50826b4cac81SBjoern A. Zeeb 
50836b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
50846b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
50856b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
50866b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
50876b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
50886b4cac81SBjoern A. Zeeb 			return (sta);
50896b4cac81SBjoern A. Zeeb 		}
50906b4cac81SBjoern A. Zeeb 	}
50916b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
50926b4cac81SBjoern A. Zeeb 	return (NULL);
50936b4cac81SBjoern A. Zeeb }
50946b4cac81SBjoern A. Zeeb 
50956b4cac81SBjoern A. Zeeb struct ieee80211_sta *
50962e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
50972e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
50986b4cac81SBjoern A. Zeeb {
50996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51006b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51026b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
51036b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
51046b4cac81SBjoern A. Zeeb 
51056b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
51066b4cac81SBjoern A. Zeeb 	sta = NULL;
51076b4cac81SBjoern A. Zeeb 
51088891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
51096b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
51106b4cac81SBjoern A. Zeeb 
51116b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
51126b4cac81SBjoern A. Zeeb 
51136b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
51146b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
51156b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
51166b4cac81SBjoern A. Zeeb 			continue;
51176b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
51186b4cac81SBjoern A. Zeeb 		if (sta != NULL)
51196b4cac81SBjoern A. Zeeb 			break;
51206b4cac81SBjoern A. Zeeb 	}
51218891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
51226b4cac81SBjoern A. Zeeb 
51236b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
51246b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
51256b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
51266b4cac81SBjoern A. Zeeb 			return (NULL);
51276b4cac81SBjoern A. Zeeb 	}
51286b4cac81SBjoern A. Zeeb 
51296b4cac81SBjoern A. Zeeb 	return (sta);
51306b4cac81SBjoern A. Zeeb }
51316b4cac81SBjoern A. Zeeb 
51326b4cac81SBjoern A. Zeeb struct sk_buff *
51336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
51346b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
51356b4cac81SBjoern A. Zeeb {
51366b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
51370cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51386b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
51396b4cac81SBjoern A. Zeeb 
51400cbcfa19SBjoern A. Zeeb 	skb = NULL;
51416b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
51426b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
51436b4cac81SBjoern A. Zeeb 
51440cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
51450cbcfa19SBjoern A. Zeeb 		goto stopped;
51460cbcfa19SBjoern A. Zeeb 
51470cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
51480cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
51490cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
51500cbcfa19SBjoern A. Zeeb 		goto stopped;
51510cbcfa19SBjoern A. Zeeb 	}
51520cbcfa19SBjoern A. Zeeb 
5153eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
5154eac3646fSBjoern A. Zeeb 
5155eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
51566b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
5157eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
51586b4cac81SBjoern A. Zeeb 
51590cbcfa19SBjoern A. Zeeb stopped:
51606b4cac81SBjoern A. Zeeb 	return (skb);
51616b4cac81SBjoern A. Zeeb }
51626b4cac81SBjoern A. Zeeb 
51636b4cac81SBjoern A. Zeeb void
51646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
516586220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
51666b4cac81SBjoern A. Zeeb {
51676b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
51686b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
516986220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
51706b4cac81SBjoern A. Zeeb 
51716b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
51726b4cac81SBjoern A. Zeeb 
51736b4cac81SBjoern A. Zeeb 	fc = bc = 0;
5174eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
51756b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
51766b4cac81SBjoern A. Zeeb 		fc++;
51776b4cac81SBjoern A. Zeeb 		bc += skb->len;
51786b4cac81SBjoern A. Zeeb 	}
5179eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
51806b4cac81SBjoern A. Zeeb 	if (frame_cnt)
51816b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
51826b4cac81SBjoern A. Zeeb 	if (byte_cnt)
51836b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
51846b4cac81SBjoern A. Zeeb 
51856b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
51866b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
51876b4cac81SBjoern A. Zeeb 	IMPROVE();
51886b4cac81SBjoern A. Zeeb }
51896b4cac81SBjoern A. Zeeb 
51906b4cac81SBjoern A. Zeeb /*
51916b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
51926b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
51936b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
51946b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
51956b4cac81SBjoern A. Zeeb  */
5196a8397571SBjoern A. Zeeb static void
5197a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
51986b4cac81SBjoern A. Zeeb     int status)
51996b4cac81SBjoern A. Zeeb {
52006b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
52016b4cac81SBjoern A. Zeeb 	struct mbuf *m;
52026b4cac81SBjoern A. Zeeb 
52036b4cac81SBjoern A. Zeeb 	m = skb->m;
52046b4cac81SBjoern A. Zeeb 	skb->m = NULL;
52056b4cac81SBjoern A. Zeeb 
52066b4cac81SBjoern A. Zeeb 	if (m != NULL) {
52076b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
52086b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
52096b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
52106b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
52116b4cac81SBjoern A. Zeeb 	}
5212a8397571SBjoern A. Zeeb }
52136b4cac81SBjoern A. Zeeb 
5214a8397571SBjoern A. Zeeb void
5215a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5216a8397571SBjoern A. Zeeb     int status)
5217a8397571SBjoern A. Zeeb {
5218a8397571SBjoern A. Zeeb 
5219a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
52206b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
52216b4cac81SBjoern A. Zeeb }
52226b4cac81SBjoern A. Zeeb 
5223383b3e8fSBjoern A. Zeeb void
5224a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5225a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
5226383b3e8fSBjoern A. Zeeb {
5227a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
5228383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
5229383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
5230383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
5231383b3e8fSBjoern A. Zeeb 	int status;
5232383b3e8fSBjoern A. Zeeb 
5233a8397571SBjoern A. Zeeb 	skb = txstat->skb;
5234383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
5235383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
5236383b3e8fSBjoern A. Zeeb 
5237383b3e8fSBjoern A. Zeeb 		m = skb->m;
5238383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
5239383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
5240383b3e8fSBjoern A. Zeeb 	} else {
5241383b3e8fSBjoern A. Zeeb 		ni = NULL;
5242383b3e8fSBjoern A. Zeeb 	}
5243383b3e8fSBjoern A. Zeeb 
5244a8397571SBjoern A. Zeeb 	info = txstat->info;
5245383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5246383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
5247383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5248383b3e8fSBjoern A. Zeeb 	} else {
5249383b3e8fSBjoern A. Zeeb 		status = 1;
5250383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5251383b3e8fSBjoern A. Zeeb 	}
5252383b3e8fSBjoern A. Zeeb 
5253383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
5254e2c5ab09SJohn Baldwin 		int ridx __unused;
5255383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5256383b3e8fSBjoern A. Zeeb 		int old_rate;
5257383b3e8fSBjoern A. Zeeb 
5258383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5259383b3e8fSBjoern A. Zeeb #endif
5260383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
5261383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5262383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
5263383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5264383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5265383b3e8fSBjoern A. Zeeb 		}
5266383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
5267a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5268383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
5269383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5270383b3e8fSBjoern A. Zeeb #endif
5271adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5272383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5273383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5274383b3e8fSBjoern A. Zeeb 		}
5275383b3e8fSBjoern A. Zeeb 
5276383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5277383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
5278383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5279383b3e8fSBjoern A. Zeeb 
5280383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5281383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5282383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5283383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
5284383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5285383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
5286383b3e8fSBjoern A. Zeeb 		}
5287383b3e8fSBjoern A. Zeeb #endif
5288383b3e8fSBjoern A. Zeeb 	}
5289383b3e8fSBjoern A. Zeeb 
5290383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5291383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5292383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5293383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
5294383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5295383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5296adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
5297383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
5298383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
5299383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
5300383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
5301383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
5302383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
5303383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
5304383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
5305383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
5306383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
5307383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
5308383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
5309383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
5310adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
5311383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
5312383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
5313383b3e8fSBjoern A. Zeeb #endif
5314383b3e8fSBjoern A. Zeeb 
5315a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
5316a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5317a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
5318a8397571SBjoern A. Zeeb 	} else {
5319383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5320383b3e8fSBjoern A. Zeeb 	}
5321a8397571SBjoern A. Zeeb }
5322a8397571SBjoern A. Zeeb 
5323a8397571SBjoern A. Zeeb void
5324a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5325a8397571SBjoern A. Zeeb {
5326a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
5327a8397571SBjoern A. Zeeb 
5328a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
5329a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
5330a8397571SBjoern A. Zeeb 	status.skb = skb;
5331a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
5332a8397571SBjoern A. Zeeb 
5333a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
5334a8397571SBjoern A. Zeeb }
5335383b3e8fSBjoern A. Zeeb 
53366b4cac81SBjoern A. Zeeb /*
53376b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
53386b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
53396b4cac81SBjoern A. Zeeb  * mbufs this should go away.
53406b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
53416b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
53426b4cac81SBjoern A. Zeeb  */
53436b4cac81SBjoern A. Zeeb static void
53446b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
53456b4cac81SBjoern A. Zeeb {
53466b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
53476b4cac81SBjoern A. Zeeb 	struct mbuf *m;
53486b4cac81SBjoern A. Zeeb 
53496b4cac81SBjoern A. Zeeb 	if (p == NULL)
53506b4cac81SBjoern A. Zeeb 		return;
53516b4cac81SBjoern A. Zeeb 
53526b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
53536b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
53546b4cac81SBjoern A. Zeeb 
53556b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
53566b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
53576b4cac81SBjoern A. Zeeb 	if (ni != NULL)
53586b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
53596b4cac81SBjoern A. Zeeb 	m_freem(m);
53606b4cac81SBjoern A. Zeeb }
53616b4cac81SBjoern A. Zeeb 
53626b4cac81SBjoern A. Zeeb void
53636b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
53646b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
53656b4cac81SBjoern A. Zeeb {
53666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53676b4cac81SBjoern A. Zeeb 
53686b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
53696b4cac81SBjoern A. Zeeb 	IMPROVE();
53706b4cac81SBjoern A. Zeeb 
53716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53726b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
53736b4cac81SBjoern A. Zeeb }
53746b4cac81SBjoern A. Zeeb 
53756b4cac81SBjoern A. Zeeb void
53766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
53776b4cac81SBjoern A. Zeeb     struct work_struct *w)
53786b4cac81SBjoern A. Zeeb {
53796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53806b4cac81SBjoern A. Zeeb 
53816b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
53826b4cac81SBjoern A. Zeeb 	IMPROVE();
53836b4cac81SBjoern A. Zeeb 
53846b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53856b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
53866b4cac81SBjoern A. Zeeb }
53876b4cac81SBjoern A. Zeeb 
53886b4cac81SBjoern A. Zeeb struct sk_buff *
5389ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5390ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5391ade774b1SBjoern A. Zeeb {
5392ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
5393ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
5394ade774b1SBjoern A. Zeeb 	uint8_t *p;
5395ade774b1SBjoern A. Zeeb 	size_t len;
5396ade774b1SBjoern A. Zeeb 
5397ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
5398ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
5399ade774b1SBjoern A. Zeeb 
5400ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5401ade774b1SBjoern A. Zeeb 	if (skb == NULL)
5402ade774b1SBjoern A. Zeeb 		return (NULL);
5403ade774b1SBjoern A. Zeeb 
5404ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
5405ade774b1SBjoern A. Zeeb 
5406ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
5407ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5408ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5409ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5410ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5411ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5412ade774b1SBjoern A. Zeeb 
5413ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
5414ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
5415ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
5416ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
5417ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
5418ade774b1SBjoern A. Zeeb 
5419ade774b1SBjoern A. Zeeb 	return (skb);
5420ade774b1SBjoern A. Zeeb }
5421ade774b1SBjoern A. Zeeb 
5422ade774b1SBjoern A. Zeeb struct sk_buff *
54236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
54246b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
54256b4cac81SBjoern A. Zeeb {
54266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54276b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
54286b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
54296b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
54306b4cac81SBjoern A. Zeeb 	uint16_t v;
54316b4cac81SBjoern A. Zeeb 
54326b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
54336b4cac81SBjoern A. Zeeb 	if (skb == NULL)
54346b4cac81SBjoern A. Zeeb 		return (NULL);
54356b4cac81SBjoern A. Zeeb 
54366b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
54376b4cac81SBjoern A. Zeeb 
54386b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
54396b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
54406b4cac81SBjoern A. Zeeb 
54416b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
54426b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
54436b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5444616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
54456b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
54466b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
54476b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
54486b4cac81SBjoern A. Zeeb 
54496b4cac81SBjoern A. Zeeb 	return (skb);
54506b4cac81SBjoern A. Zeeb }
54516b4cac81SBjoern A. Zeeb 
54526b4cac81SBjoern A. Zeeb struct sk_buff *
54536b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5454adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
54556b4cac81SBjoern A. Zeeb {
54566b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54576b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
54586b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
54596b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
54606b4cac81SBjoern A. Zeeb 
5461adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
5462adff403fSBjoern A. Zeeb 
54636b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
54646b4cac81SBjoern A. Zeeb 	if (skb == NULL)
54656b4cac81SBjoern A. Zeeb 		return (NULL);
54666b4cac81SBjoern A. Zeeb 
54676b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
54686b4cac81SBjoern A. Zeeb 
54696b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
54706b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
54716b4cac81SBjoern A. Zeeb 
54726b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
54736b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
54746b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
54756b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
54766b4cac81SBjoern A. Zeeb 
54776b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
54786b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
54796b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
54806b4cac81SBjoern A. Zeeb 
54816b4cac81SBjoern A. Zeeb 	return (skb);
54826b4cac81SBjoern A. Zeeb }
54836b4cac81SBjoern A. Zeeb 
54846b4cac81SBjoern A. Zeeb struct wireless_dev *
54856b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
54866b4cac81SBjoern A. Zeeb {
54876b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54886b4cac81SBjoern A. Zeeb 
54896b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
54906b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
54916b4cac81SBjoern A. Zeeb }
54926b4cac81SBjoern A. Zeeb 
54936b4cac81SBjoern A. Zeeb void
54946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
54956b4cac81SBjoern A. Zeeb {
54966b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54976b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
54986b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
54996b4cac81SBjoern A. Zeeb 	int arg;
55006b4cac81SBjoern A. Zeeb 
55016b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
55026b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
55036b4cac81SBjoern A. Zeeb 
55046b4cac81SBjoern A. Zeeb 	/*
5505f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
55066b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
55076b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
55086b4cac81SBjoern A. Zeeb 	 */
5509f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
5510bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
55116b4cac81SBjoern A. Zeeb 
5512018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5513018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
55146b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
55156b4cac81SBjoern A. Zeeb }
55166b4cac81SBjoern A. Zeeb 
5517bb81db90SBjoern A. Zeeb void
5518bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
5519bb81db90SBjoern A. Zeeb {
5520bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5521bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
5522bb81db90SBjoern A. Zeeb 
5523bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
5524bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
5525bb81db90SBjoern A. Zeeb 
5526bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5527bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
55283540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
5529bb81db90SBjoern A. Zeeb }
5530bb81db90SBjoern A. Zeeb 
55315edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
55325edde07cSBjoern A. Zeeb 
55335a9a0d78SBjoern A. Zeeb void
55345a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
55355a9a0d78SBjoern A. Zeeb {
55365a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55375a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55385a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
55395a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
55405a9a0d78SBjoern A. Zeeb 
55415a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
55425a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
55435a9a0d78SBjoern A. Zeeb 
55445a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
55455a9a0d78SBjoern A. Zeeb 
55465a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
55475a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
55485a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
55495a9a0d78SBjoern A. Zeeb 	else
55505a9a0d78SBjoern A. Zeeb 		ac_count = 1;
55515a9a0d78SBjoern A. Zeeb 
55525a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
55535a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
55545a9a0d78SBjoern A. Zeeb 
55555a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
55565a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
55575a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
55585a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
55590d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
55605a9a0d78SBjoern A. Zeeb 				/*
55615a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
55625a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
55635a9a0d78SBjoern A. Zeeb 				 */
55640d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
55650d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
55665a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
55675a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
55685a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
55695a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
55700d2cb6a6SBjoern A. Zeeb #endif
55715a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
55725a9a0d78SBjoern A. Zeeb 			}
55735a9a0d78SBjoern A. Zeeb 		}
55745a9a0d78SBjoern A. Zeeb 	}
55755a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
55765a9a0d78SBjoern A. Zeeb }
55775a9a0d78SBjoern A. Zeeb 
55785a9a0d78SBjoern A. Zeeb void
55795a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
55805a9a0d78SBjoern A. Zeeb {
55815a9a0d78SBjoern A. Zeeb 	int i;
55825a9a0d78SBjoern A. Zeeb 
55835a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
55845a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
55855a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
55865a9a0d78SBjoern A. Zeeb }
55875a9a0d78SBjoern A. Zeeb 
55885a9a0d78SBjoern A. Zeeb 
55895a9a0d78SBjoern A. Zeeb static void
55905a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
55915a9a0d78SBjoern A. Zeeb {
55925a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55935a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55945a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
55955a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
55965a9a0d78SBjoern A. Zeeb 
55975a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
55985a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
55995a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
56005a9a0d78SBjoern A. Zeeb 	else
56015a9a0d78SBjoern A. Zeeb 		ac_count = 1;
56025a9a0d78SBjoern A. Zeeb 
56035a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
56045a9a0d78SBjoern A. Zeeb 
56055a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
56065a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
56075a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
56085a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
56095a9a0d78SBjoern A. Zeeb 
56105a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
56115a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
56125a9a0d78SBjoern A. Zeeb 
56135a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
56145a9a0d78SBjoern A. Zeeb 
56155a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
56165a9a0d78SBjoern A. Zeeb 
56170d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
56185a9a0d78SBjoern A. Zeeb 				/*
56195a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
56205a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
56215a9a0d78SBjoern A. Zeeb 				 */
56220d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
56230d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
56245a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
56255a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
56265a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
56275a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
56280d2cb6a6SBjoern A. Zeeb #endif
56295a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
56305a9a0d78SBjoern A. Zeeb 
56315a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
56325a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
56335a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
56345a9a0d78SBjoern A. Zeeb 
56355a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
56365a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
56375a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
56385a9a0d78SBjoern A. Zeeb 
56395a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
56405a9a0d78SBjoern A. Zeeb 							continue;
56415a9a0d78SBjoern A. Zeeb 
56425a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
56435a9a0d78SBjoern A. Zeeb 							continue;
56445a9a0d78SBjoern A. Zeeb 
56455a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
56465a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
56475a9a0d78SBjoern A. Zeeb 							continue;
56485a9a0d78SBjoern A. Zeeb 
56495a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
56505a9a0d78SBjoern A. Zeeb 
56515a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
56525a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
56535a9a0d78SBjoern A. Zeeb 					}
56545a9a0d78SBjoern A. Zeeb 				}
56555a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
56565a9a0d78SBjoern A. Zeeb 			}
56575a9a0d78SBjoern A. Zeeb 		}
56585a9a0d78SBjoern A. Zeeb 	}
56595a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
56605a9a0d78SBjoern A. Zeeb }
56615a9a0d78SBjoern A. Zeeb 
56625a9a0d78SBjoern A. Zeeb void
56635a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
56645a9a0d78SBjoern A. Zeeb {
56655a9a0d78SBjoern A. Zeeb 	int i;
56665a9a0d78SBjoern A. Zeeb 
56675a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
56685a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
56695a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
56705a9a0d78SBjoern A. Zeeb }
56715a9a0d78SBjoern A. Zeeb 
56725a9a0d78SBjoern A. Zeeb void
56735a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
56745a9a0d78SBjoern A. Zeeb {
56755a9a0d78SBjoern A. Zeeb 
56765a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
56775a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
56785a9a0d78SBjoern A. Zeeb 
56795a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
56805a9a0d78SBjoern A. Zeeb }
56815a9a0d78SBjoern A. Zeeb 
56825a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
56835a9a0d78SBjoern A. Zeeb void
56845a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
56855a9a0d78SBjoern A. Zeeb {
56865a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56875a9a0d78SBjoern A. Zeeb 
56885a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56895a9a0d78SBjoern A. Zeeb 
56905a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
56915a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
56925a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
56935a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
56945a9a0d78SBjoern A. Zeeb }
56955a9a0d78SBjoern A. Zeeb 
56965a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
56975a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
56985a9a0d78SBjoern A. Zeeb {
56995a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57005a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
57015a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
57025a9a0d78SBjoern A. Zeeb 
57035a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57045a9a0d78SBjoern A. Zeeb 	txq = NULL;
57055a9a0d78SBjoern A. Zeeb 
57065a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
57075a9a0d78SBjoern A. Zeeb 
57085a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
57095a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
57105a9a0d78SBjoern A. Zeeb 		goto out;
57115a9a0d78SBjoern A. Zeeb 
57125a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
57135a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
57145a9a0d78SBjoern A. Zeeb 		goto out;
57155a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
57165a9a0d78SBjoern A. Zeeb 		goto out;
57175a9a0d78SBjoern A. Zeeb 
57185a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
57195a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
57205a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
57215a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
57225a9a0d78SBjoern A. Zeeb 
57235a9a0d78SBjoern A. Zeeb out:
57245a9a0d78SBjoern A. Zeeb 	return (txq);
57255a9a0d78SBjoern A. Zeeb }
57265a9a0d78SBjoern A. Zeeb 
57275a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
57285a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
57295a9a0d78SBjoern A. Zeeb {
57305a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57315a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
5732eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
57335a9a0d78SBjoern A. Zeeb 
57345a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
57355a9a0d78SBjoern A. Zeeb 
57365a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
57375a9a0d78SBjoern A. Zeeb 
57385a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
5739eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
5740eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
5741eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5742eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
57435a9a0d78SBjoern A. Zeeb 		goto out;
57445a9a0d78SBjoern A. Zeeb 
57455a9a0d78SBjoern A. Zeeb 	/* Make sure we do not double-schedule. */
57465a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_entry.tqe_next != NULL)
57475a9a0d78SBjoern A. Zeeb 		goto out;
57485a9a0d78SBjoern A. Zeeb 
57495a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57505a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
57515a9a0d78SBjoern A. Zeeb out:
57525a9a0d78SBjoern A. Zeeb 	return;
57535a9a0d78SBjoern A. Zeeb }
57545a9a0d78SBjoern A. Zeeb 
5755eac3646fSBjoern A. Zeeb void
5756eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
5757eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
5758eac3646fSBjoern A. Zeeb {
5759eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5760eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
5761eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
5762eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
5763eac3646fSBjoern A. Zeeb 
5764eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5765eac3646fSBjoern A. Zeeb 
5766eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
5767eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
5768eac3646fSBjoern A. Zeeb 	do {
5769eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
5770eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
5771eac3646fSBjoern A. Zeeb 			break;
5772eac3646fSBjoern A. Zeeb 
5773eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
5774eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
5775eac3646fSBjoern A. Zeeb 		do {
5776eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
5777eac3646fSBjoern A. Zeeb 			if (skb == NULL)
5778eac3646fSBjoern A. Zeeb 				break;
5779eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
5780eac3646fSBjoern A. Zeeb 		} while(1);
5781eac3646fSBjoern A. Zeeb 
5782eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
5783eac3646fSBjoern A. Zeeb 	} while (1);
5784eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
5785eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
5786eac3646fSBjoern A. Zeeb }
5787eac3646fSBjoern A. Zeeb 
57885a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
57895a9a0d78SBjoern A. Zeeb 
57905edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
57915edde07cSBjoern A. Zeeb 	u_int refcnt;
57925edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
57935edde07cSBjoern A. Zeeb };
57945edde07cSBjoern A. Zeeb 
57955edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
57965edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
57975edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
57985edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
57995edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
58005edde07cSBjoern A. Zeeb 	size_t ssid_len;
58015edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
58025edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
58035edde07cSBjoern A. Zeeb 
58045edde07cSBjoern A. Zeeb 	/*
58055edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
58065edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
58075edde07cSBjoern A. Zeeb 	 */
58085edde07cSBjoern A. Zeeb 	bool match;
58095edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
58105edde07cSBjoern A. Zeeb };
58115edde07cSBjoern A. Zeeb 
58125edde07cSBjoern A. Zeeb static void
58135edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
58145edde07cSBjoern A. Zeeb {
58155edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
58165edde07cSBjoern A. Zeeb 	size_t ielen;
58175edde07cSBjoern A. Zeeb 
58185edde07cSBjoern A. Zeeb 	lookup = arg;
58195edde07cSBjoern A. Zeeb 
58205edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
58215edde07cSBjoern A. Zeeb 	if (lookup->match)
58225edde07cSBjoern A. Zeeb 		return;
58235edde07cSBjoern A. Zeeb 
58245edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
58255edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
58265edde07cSBjoern A. Zeeb 		return;
58275edde07cSBjoern A. Zeeb 
58285edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
58295edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
58305edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
58315edde07cSBjoern A. Zeeb 		return;
58325edde07cSBjoern A. Zeeb 	}
58335edde07cSBjoern A. Zeeb 
58345edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
58355edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
58365edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
58375edde07cSBjoern A. Zeeb 		return;
58385edde07cSBjoern A. Zeeb 	}
58395edde07cSBjoern A. Zeeb 
58405edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
58415edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
58425edde07cSBjoern A. Zeeb 
58435edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
58445edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
58455edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
58465edde07cSBjoern A. Zeeb 			return;
58475edde07cSBjoern A. Zeeb 	}
58485edde07cSBjoern A. Zeeb 
58495edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
58505edde07cSBjoern A. Zeeb 		return;
58515edde07cSBjoern A. Zeeb 
58525edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
58535edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
58545edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
58555edde07cSBjoern A. Zeeb 			return;
58565edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
58575edde07cSBjoern A. Zeeb 			return;
58585edde07cSBjoern A. Zeeb 	}
58595edde07cSBjoern A. Zeeb 
58605edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
58615edde07cSBjoern A. Zeeb 
58625edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
58635edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
58645edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
58655edde07cSBjoern A. Zeeb 		return;
58665edde07cSBjoern A. Zeeb 
58675edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
58685edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
58695edde07cSBjoern A. Zeeb 	if (ielen)
58705edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
58715edde07cSBjoern A. Zeeb 
58725edde07cSBjoern A. Zeeb 	lookup->match = true;
58735edde07cSBjoern A. Zeeb }
58745edde07cSBjoern A. Zeeb 
58755edde07cSBjoern A. Zeeb struct cfg80211_bss *
58765edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
58775edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
58785edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
58795edde07cSBjoern A. Zeeb {
58805edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
58815edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
58825edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
58835edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
58845edde07cSBjoern A. Zeeb 
58855edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58865edde07cSBjoern A. Zeeb 
58875edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
58885edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
58895edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
58905edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
58915edde07cSBjoern A. Zeeb 		return (NULL);
58925edde07cSBjoern A. Zeeb 	}
58935edde07cSBjoern A. Zeeb 
58945edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
58955edde07cSBjoern A. Zeeb 	lookup.chan = chan;
58965edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
58975edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
58985edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
58995edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
59005edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
59015edde07cSBjoern A. Zeeb 	lookup.match = false;
59025edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
59035edde07cSBjoern A. Zeeb 
59045edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
59055edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
59065edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
59075edde07cSBjoern A. Zeeb 	if (!lookup.match) {
59085edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
59095edde07cSBjoern A. Zeeb 		return (NULL);
59105edde07cSBjoern A. Zeeb 	}
59115edde07cSBjoern A. Zeeb 
59125edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
59135edde07cSBjoern A. Zeeb 	return (&lbss->bss);
59145edde07cSBjoern A. Zeeb }
59155edde07cSBjoern A. Zeeb 
59165edde07cSBjoern A. Zeeb void
59175edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
59185edde07cSBjoern A. Zeeb {
59195edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
59205edde07cSBjoern A. Zeeb 
59215edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
59225edde07cSBjoern A. Zeeb 
59235edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
59245edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
59255edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
59265edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
59275edde07cSBjoern A. Zeeb 	}
59285edde07cSBjoern A. Zeeb }
59295edde07cSBjoern A. Zeeb 
59305edde07cSBjoern A. Zeeb void
59315edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
59325edde07cSBjoern A. Zeeb {
59335edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
59345edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
59355edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
59365edde07cSBjoern A. Zeeb 
59375edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59385edde07cSBjoern A. Zeeb 	ic = lhw->ic;
59395edde07cSBjoern A. Zeeb 
59405edde07cSBjoern A. Zeeb 	/*
59415edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
59425edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
59435edde07cSBjoern A. Zeeb 	 */
59445edde07cSBjoern A. Zeeb 	if (ic == NULL ||
59455edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
59465edde07cSBjoern A. Zeeb 		return;
59475edde07cSBjoern A. Zeeb 
59485edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
59495edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
59505edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
59515edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
59525edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
59535edde07cSBjoern A. Zeeb }
59545edde07cSBjoern A. Zeeb 
59555edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
59565edde07cSBjoern A. Zeeb 
59576b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
59586b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
59596b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
5960