xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 906521f8176b13533556d742db4ab28e847b85c0)
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);
147759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1486b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1494a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1504a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1514a67f1dfSBjoern A. Zeeb #endif
1526b4cac81SBjoern A. Zeeb 
1539fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
1549fb91463SBjoern A. Zeeb static void
1559fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
1569fb91463SBjoern A. Zeeb {
1579fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
1589fb91463SBjoern A. Zeeb 	uint8_t *ie;
1599fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
1609fb91463SBjoern A. Zeeb 	int i, rx_nss;
1619fb91463SBjoern A. Zeeb 
1629fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
1639fb91463SBjoern A. Zeeb 		return;
1649fb91463SBjoern A. Zeeb 
1659fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
1669fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1679fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
1689fb91463SBjoern A. Zeeb 
1699fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
1709fb91463SBjoern A. Zeeb 
1719fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
1729fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
1739fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1749fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
1759fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
1769fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1779fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
1789fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
1799fb91463SBjoern A. Zeeb 
1809fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
1819fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
1829fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1839fb91463SBjoern A. Zeeb 		ie += 4;
1849fb91463SBjoern A. Zeeb 	ie += 2;
1859fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
1869fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
1879fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
1889fb91463SBjoern A. Zeeb 
1899fb91463SBjoern A. Zeeb 	rx_nss = 0;
1909fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
1919fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
1929fb91463SBjoern A. Zeeb 			rx_nss++;
1939fb91463SBjoern A. Zeeb 	}
1949fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
1959fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
1969fb91463SBjoern A. Zeeb 
1979fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
1989fb91463SBjoern A. Zeeb }
1999fb91463SBjoern A. Zeeb #endif
2009fb91463SBjoern A. Zeeb 
2019fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
2029fb91463SBjoern A. Zeeb static void
2039fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
2049fb91463SBjoern A. Zeeb {
2059fb91463SBjoern A. Zeeb 
2069fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
2079fb91463SBjoern A. Zeeb 		return;
2089fb91463SBjoern A. Zeeb 
2099fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
2109fb91463SBjoern A. Zeeb #ifdef __notyet__
2119fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
2129fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
2139fb91463SBjoern A. Zeeb 		} else
2149fb91463SBjoern A. Zeeb #endif
2159fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2169fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
2179fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2189fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
2199fb91463SBjoern A. Zeeb 	}
2209fb91463SBjoern A. Zeeb 
2219fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
2229fb91463SBjoern A. Zeeb 	return;
2239fb91463SBjoern A. Zeeb }
2249fb91463SBjoern A. Zeeb #endif
2259fb91463SBjoern A. Zeeb 
226d9f59799SBjoern A. Zeeb static void
22767674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
22867674c1cSBjoern A. Zeeb     const char *_f, int _l)
229d9f59799SBjoern A. Zeeb {
230d9f59799SBjoern A. Zeeb 
2319d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2329d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
233d9f59799SBjoern A. Zeeb 		return;
234d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
235d9f59799SBjoern A. Zeeb 		return;
236d9f59799SBjoern A. Zeeb 
237d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
23867674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
23967674c1cSBjoern A. Zeeb 	if (ni != NULL)
24067674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
241d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
242d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
243d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
2449d9ba2b7SBjoern A. Zeeb #endif
245d9f59799SBjoern A. Zeeb }
246d9f59799SBjoern A. Zeeb 
247d9f59799SBjoern A. Zeeb static void
248d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
249d9f59799SBjoern A. Zeeb {
250d9f59799SBjoern A. Zeeb 
251d9f59799SBjoern A. Zeeb 
252d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2530936c648SBjoern A. Zeeb 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
2540936c648SBjoern A. Zeeb 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
2550936c648SBjoern A. Zeeb 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
256d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
257d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
258d9f59799SBjoern A. Zeeb }
259d9f59799SBjoern A. Zeeb 
2604f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
2614f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
2624f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
2634f61ef8bSBjoern A. Zeeb {
2644f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
2654f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
2664f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
2674f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
268b6b352e4SBjoern A. Zeeb 	int band, i, tid;
2699fb91463SBjoern A. Zeeb 	int ht_rx_nss;
2709fb91463SBjoern A. Zeeb 	int vht_rx_nss;
2714f61ef8bSBjoern A. Zeeb 
2724f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2734f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
2744f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2754f61ef8bSBjoern A. Zeeb 		return (NULL);
2764f61ef8bSBjoern A. Zeeb 
2774f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2784f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2794f61ef8bSBjoern A. Zeeb 	/*
2800936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
2810936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
2820936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
2830936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
2840936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
2850936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
2860936c648SBjoern A. Zeeb 	 * two separate allocations.
2874f61ef8bSBjoern A. Zeeb 	 */
2880936c648SBjoern A. Zeeb 	lsta->ni = ni;
2894f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2904f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2914f61ef8bSBjoern A. Zeeb 
2924f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2934f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2944f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2954f61ef8bSBjoern A. Zeeb 
2964f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
297b6b352e4SBjoern A. Zeeb 
298b6b352e4SBjoern A. Zeeb 	/* TXQ */
2994f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
3004f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
3014f61ef8bSBjoern A. Zeeb 
302d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
3034f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
3044f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
3054f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
3064f61ef8bSBjoern A. Zeeb 			goto cleanup;
3074f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
3084f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
309d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
310d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
311d0d29110SBjoern A. Zeeb 				continue;
312d0d29110SBjoern A. Zeeb 			}
313d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
3144f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
3154f61ef8bSBjoern A. Zeeb 		} else {
316fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
3174f61ef8bSBjoern A. Zeeb 		}
318d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
3190cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
320d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
3214f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
3224f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
3230cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
324d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
325eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
3264f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
3274f61ef8bSBjoern A. Zeeb 	}
3284f61ef8bSBjoern A. Zeeb 
329b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
330b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
331b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
332b6b352e4SBjoern A. Zeeb 
333b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
334b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
335b6b352e4SBjoern A. Zeeb 			continue;
336b6b352e4SBjoern A. Zeeb 
337b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
338b6b352e4SBjoern A. Zeeb 
339b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
340b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
341b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
342b6b352e4SBjoern A. Zeeb 		}
343b6b352e4SBjoern A. Zeeb 	}
3449fb91463SBjoern A. Zeeb 
3456ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
3469fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
3479fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = 0;
3489fb91463SBjoern A. Zeeb 
3499fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
3509fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3519fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
3529fb91463SBjoern A. Zeeb #endif
3539fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
3549fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
3559fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
3569fb91463SBjoern A. Zeeb #endif
3579fb91463SBjoern A. Zeeb 
3589fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
3599fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
3609fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
361b6b352e4SBjoern A. Zeeb 
3626ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
3636ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
3646ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
3656ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
3666ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
3676ffb7bd4SBjoern A. Zeeb 	}
3686ffb7bd4SBjoern A. Zeeb 
3694f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
370fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
3714f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
3724f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
3730936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
3744f61ef8bSBjoern A. Zeeb 
3754f61ef8bSBjoern A. Zeeb 	return (lsta);
3764f61ef8bSBjoern A. Zeeb 
3774f61ef8bSBjoern A. Zeeb cleanup:
378eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
379eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
380eac3646fSBjoern A. Zeeb 
381eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
382eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
3834f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
384eac3646fSBjoern A. Zeeb 	}
3854f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
3864f61ef8bSBjoern A. Zeeb 	return (NULL);
3874f61ef8bSBjoern A. Zeeb }
3884f61ef8bSBjoern A. Zeeb 
3890936c648SBjoern A. Zeeb static void
3900936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
3910936c648SBjoern A. Zeeb {
3920936c648SBjoern A. Zeeb 	struct mbuf *m;
3930936c648SBjoern A. Zeeb 
3940936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
3950936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
3960936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
3970936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
3980936c648SBjoern A. Zeeb 
3990936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
4000936c648SBjoern A. Zeeb 	IMPROVE();
4010936c648SBjoern A. Zeeb 
402fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
403fa4e4257SBjoern A. Zeeb 
404fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4050936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
406fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4070936c648SBjoern A. Zeeb 
4080936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
4090936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
4100936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
4110936c648SBjoern A. Zeeb 
4120936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4130936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
4140936c648SBjoern A. Zeeb 	while (m != NULL) {
4150936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
4160936c648SBjoern A. Zeeb 
4170936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
4180936c648SBjoern A. Zeeb 		if (nim != NULL)
4190936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
4200936c648SBjoern A. Zeeb 		m_freem(m);
4210936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
4220936c648SBjoern A. Zeeb 	}
4230936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
4240936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
425fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
4260936c648SBjoern A. Zeeb 
4270936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
4280936c648SBjoern A. Zeeb 
4290936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
4300936c648SBjoern A. Zeeb 
4310936c648SBjoern A. Zeeb 	/* Free lsta. */
4320936c648SBjoern A. Zeeb 	lsta->ni = NULL;
4330936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
4340936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
4350936c648SBjoern A. Zeeb }
4360936c648SBjoern A. Zeeb 
4370936c648SBjoern A. Zeeb 
4386b4cac81SBjoern A. Zeeb static enum nl80211_band
4396b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
4406b4cac81SBjoern A. Zeeb {
4416b4cac81SBjoern A. Zeeb 
4426b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
4436b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
4446b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
4456b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
4466b4cac81SBjoern A. Zeeb #ifdef __notyet__
4476b4cac81SBjoern A. Zeeb 	else if ()
4486b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
4496b4cac81SBjoern A. Zeeb 	else if ()
4506b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
4516b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
4526b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
4536b4cac81SBjoern A. Zeeb #endif
4546b4cac81SBjoern A. Zeeb 	else
4556b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
4566b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
4576b4cac81SBjoern A. Zeeb }
4586b4cac81SBjoern A. Zeeb 
4596b4cac81SBjoern A. Zeeb static uint32_t
4606b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
4616b4cac81SBjoern A. Zeeb {
4626b4cac81SBjoern A. Zeeb 
4636b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
4646b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
4656b4cac81SBjoern A. Zeeb 	switch (band) {
4666b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
4676b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
4686b4cac81SBjoern A. Zeeb 		break;
4696b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
4706b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
4716b4cac81SBjoern A. Zeeb 		break;
4726b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
4736b4cac81SBjoern A. Zeeb 		break;
4746b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
4756b4cac81SBjoern A. Zeeb 		break;
4766b4cac81SBjoern A. Zeeb 	default:
4776b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
4786b4cac81SBjoern A. Zeeb 		break;
4796b4cac81SBjoern A. Zeeb 	}
4806b4cac81SBjoern A. Zeeb 
4816b4cac81SBjoern A. Zeeb 	IMPROVE();
4826b4cac81SBjoern A. Zeeb 	return (0x00);
4836b4cac81SBjoern A. Zeeb }
4846b4cac81SBjoern A. Zeeb 
485e3a0b120SBjoern A. Zeeb #if 0
4866b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
4876b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
4886b4cac81SBjoern A. Zeeb {
4896b4cac81SBjoern A. Zeeb 
4906b4cac81SBjoern A. Zeeb 	switch (ac) {
4916b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
4926b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
4936b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
4946b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
4956b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
4966b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
4976b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
4986b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
4996b4cac81SBjoern A. Zeeb 	default:
5006b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
5016b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5026b4cac81SBjoern A. Zeeb 	}
5036b4cac81SBjoern A. Zeeb }
504e3a0b120SBjoern A. Zeeb #endif
5056b4cac81SBjoern A. Zeeb 
5066b4cac81SBjoern A. Zeeb static enum nl80211_iftype
5076b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
5086b4cac81SBjoern A. Zeeb {
5096b4cac81SBjoern A. Zeeb 
5106b4cac81SBjoern A. Zeeb 	switch (opmode) {
5116b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
5126b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
5136b4cac81SBjoern A. Zeeb 		break;
5146b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
5156b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
5166b4cac81SBjoern A. Zeeb 		break;
5176b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
5186b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
5196b4cac81SBjoern A. Zeeb 		break;
5206b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
5216b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
5226b4cac81SBjoern A. Zeeb 		break;
5236b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
5246b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
5256b4cac81SBjoern A. Zeeb 		break;
5266b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
5276b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
5286b4cac81SBjoern A. Zeeb 		break;
5296b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
5306b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5316b4cac81SBjoern A. Zeeb 	default:
5326b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
5336b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5346b4cac81SBjoern A. Zeeb 	}
5356b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
5366b4cac81SBjoern A. Zeeb }
5376b4cac81SBjoern A. Zeeb 
538b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
5396b4cac81SBjoern A. Zeeb static uint32_t
5406b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
5416b4cac81SBjoern A. Zeeb {
5426b4cac81SBjoern A. Zeeb 
5436b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
5446b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
5456b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5476b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
5486b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
549*906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
5506b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
5516b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5526b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
5536b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
5546b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
5556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
5566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5576b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5586b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5596b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
5606b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5616b4cac81SBjoern A. Zeeb 		break;
5626b4cac81SBjoern A. Zeeb 	default:
5636b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
5646b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5656b4cac81SBjoern A. Zeeb 	}
5666b4cac81SBjoern A. Zeeb 
5676b4cac81SBjoern A. Zeeb 	return (0);
5686b4cac81SBjoern A. Zeeb }
5696b4cac81SBjoern A. Zeeb 
5706b4cac81SBjoern A. Zeeb static uint32_t
5716b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
5726b4cac81SBjoern A. Zeeb {
5736b4cac81SBjoern A. Zeeb 
5746b4cac81SBjoern A. Zeeb 	switch (cipher) {
5756b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
5766b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
5776b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
5786b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
5796b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
5806b4cac81SBjoern A. Zeeb 		if (keylen < 8)
5816b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
5826b4cac81SBjoern A. Zeeb 		else
5836b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
5846b4cac81SBjoern A. Zeeb 		break;
5856b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
5866b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
5876b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
5886b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
5896b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
5906b4cac81SBjoern A. Zeeb 		break;
5916b4cac81SBjoern A. Zeeb 	default:
5926b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
5936b4cac81SBjoern A. Zeeb 	};
5946b4cac81SBjoern A. Zeeb 	return (0);
5956b4cac81SBjoern A. Zeeb }
5966b4cac81SBjoern A. Zeeb #endif
5976b4cac81SBjoern A. Zeeb 
5986b4cac81SBjoern A. Zeeb #ifdef __notyet__
5996b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
6006b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
6016b4cac81SBjoern A. Zeeb {
6026b4cac81SBjoern A. Zeeb 
6036b4cac81SBjoern A. Zeeb 	/*
6046b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
6056b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
6066b4cac81SBjoern A. Zeeb 	 */
6076b4cac81SBjoern A. Zeeb 	switch (state) {
6086b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
6096b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
6106b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
6116b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
6126b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
6136b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
6146b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
6156b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
6166b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
6176b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
6186b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
6196b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
6206b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
6216b4cac81SBjoern A. Zeeb 	default:
6226b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
6236b4cac81SBjoern A. Zeeb 	};
6246b4cac81SBjoern A. Zeeb 
6256b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
6266b4cac81SBjoern A. Zeeb }
6276b4cac81SBjoern A. Zeeb #endif
6286b4cac81SBjoern A. Zeeb 
6296b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6306b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
6316b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
6326b4cac81SBjoern A. Zeeb {
6336b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6346b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
6356b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
6366b4cac81SBjoern A. Zeeb 	int i, nchans;
6376b4cac81SBjoern A. Zeeb 
6386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6396b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
6406b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
6416b4cac81SBjoern A. Zeeb 		return (NULL);
6426b4cac81SBjoern A. Zeeb 
6436b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
6446b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
6456b4cac81SBjoern A. Zeeb 		return (NULL);
6466b4cac81SBjoern A. Zeeb 
6476b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
6486b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
6496b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
6506b4cac81SBjoern A. Zeeb 			return (&channels[i]);
6516b4cac81SBjoern A. Zeeb 	}
6526b4cac81SBjoern A. Zeeb 
6536b4cac81SBjoern A. Zeeb 	return (NULL);
6546b4cac81SBjoern A. Zeeb }
6556b4cac81SBjoern A. Zeeb 
6562ac8a218SBjoern A. Zeeb #if 0
6576b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6586b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
6596b4cac81SBjoern A. Zeeb {
6606b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6616b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
6626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6636b4cac81SBjoern A. Zeeb 
6646b4cac81SBjoern A. Zeeb 	chan = NULL;
6656b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
6666b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
6676b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
6686b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
6696b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
6706b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
6716b4cac81SBjoern A. Zeeb 	else
6726b4cac81SBjoern A. Zeeb 		c = NULL;
6736b4cac81SBjoern A. Zeeb 
6746b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6756b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
6766b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
6776b4cac81SBjoern A. Zeeb 	}
6786b4cac81SBjoern A. Zeeb 
6796b4cac81SBjoern A. Zeeb 	return (chan);
6806b4cac81SBjoern A. Zeeb }
6812ac8a218SBjoern A. Zeeb #endif
6826b4cac81SBjoern A. Zeeb 
6832e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
6842e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
6852e183d99SBjoern A. Zeeb {
6862e183d99SBjoern A. Zeeb 	enum nl80211_band band;
6872e183d99SBjoern A. Zeeb 
6882e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6892e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6902e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6912e183d99SBjoern A. Zeeb 		int i;
6922e183d99SBjoern A. Zeeb 
6932e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
6942e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6952e183d99SBjoern A. Zeeb 			continue;
6962e183d99SBjoern A. Zeeb 
6972e183d99SBjoern A. Zeeb 		channels = supband->channels;
6982e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6992e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
7002e183d99SBjoern A. Zeeb 				return (&channels[i]);
7012e183d99SBjoern A. Zeeb 		}
7022e183d99SBjoern A. Zeeb 	}
7032e183d99SBjoern A. Zeeb 
7042e183d99SBjoern A. Zeeb 	return (NULL);
7052e183d99SBjoern A. Zeeb }
7062e183d99SBjoern A. Zeeb 
707b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
7086b4cac81SBjoern A. Zeeb static int
7096b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
7106b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
7116b4cac81SBjoern A. Zeeb {
7126b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
7136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7146b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7166b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
7176b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
7186b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
7196b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
7206b4cac81SBjoern A. Zeeb 	int error;
7216b4cac81SBjoern A. Zeeb 
7226b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
7236b4cac81SBjoern A. Zeeb 
7246b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
7256b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
7266b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7276b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7286b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7296b4cac81SBjoern A. Zeeb 
7306b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
7316b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
7326b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
7336b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
7346b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
7356b4cac81SBjoern A. Zeeb #if 0
7366b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
7376b4cac81SBjoern A. Zeeb #endif
7386b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
7396b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
7406b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
7416b4cac81SBjoern A. Zeeb 
7426b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
7436b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
7446b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
7456b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
7466b4cac81SBjoern A. Zeeb 		break;
7476b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7486b4cac81SBjoern A. Zeeb 	default:
7496b4cac81SBjoern A. Zeeb 		IMPROVE();
7506b4cac81SBjoern A. Zeeb 		return (0);
7516b4cac81SBjoern A. Zeeb 	};
7526b4cac81SBjoern A. Zeeb 
7536b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
7546b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
7556b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
7566b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
7576b4cac81SBjoern A. Zeeb 
7586b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
7596b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
7606b4cac81SBjoern A. Zeeb 	}
7616b4cac81SBjoern A. Zeeb 
7626b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
7636b4cac81SBjoern A. Zeeb 	if (error != 0) {
7646b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
7656b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
7666b4cac81SBjoern A. Zeeb 		return (0);
7676b4cac81SBjoern A. Zeeb 	} else {
7686b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
7696b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
7706b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
7716b4cac81SBjoern A. Zeeb 		return (1);
7726b4cac81SBjoern A. Zeeb 	}
7736b4cac81SBjoern A. Zeeb }
7746b4cac81SBjoern A. Zeeb 
7756b4cac81SBjoern A. Zeeb static int
7766b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
7776b4cac81SBjoern A. Zeeb {
7786b4cac81SBjoern A. Zeeb 
7796b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
7806b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
7816b4cac81SBjoern A. Zeeb }
7826b4cac81SBjoern A. Zeeb static  int
7836b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
7846b4cac81SBjoern A. Zeeb {
7856b4cac81SBjoern A. Zeeb 
7866b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
7876b4cac81SBjoern A. Zeeb }
7886b4cac81SBjoern A. Zeeb #endif
7896b4cac81SBjoern A. Zeeb 
7906b4cac81SBjoern A. Zeeb static u_int
7916b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
7926b4cac81SBjoern A. Zeeb {
7936b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
7946b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7956b4cac81SBjoern A. Zeeb 
7966b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
7976b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
7986b4cac81SBjoern A. Zeeb 
7996b4cac81SBjoern A. Zeeb 	mc_list = arg;
8006b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
8016b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
8026b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
8036b4cac81SBjoern A. Zeeb 			return (0);
8046b4cac81SBjoern A. Zeeb 	}
8056b4cac81SBjoern A. Zeeb 
8066b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
8076b4cac81SBjoern A. Zeeb 	if (addr == NULL)
8086b4cac81SBjoern A. Zeeb 		return (0);
8096b4cac81SBjoern A. Zeeb 
8106b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
8116b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
8126b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
8136b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
8146b4cac81SBjoern A. Zeeb 	mc_list->count++;
8156b4cac81SBjoern A. Zeeb 
8169d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8179d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8186b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
8196b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
8209d9ba2b7SBjoern A. Zeeb #endif
8216b4cac81SBjoern A. Zeeb 
8226b4cac81SBjoern A. Zeeb 	return (1);
8236b4cac81SBjoern A. Zeeb }
8246b4cac81SBjoern A. Zeeb 
8256b4cac81SBjoern A. Zeeb static void
8266b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
8276b4cac81SBjoern A. Zeeb {
8286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8296b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8306b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
8316b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
8326b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
8336b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
8346b4cac81SBjoern A. Zeeb 	u64 mc;
8356b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
8366b4cac81SBjoern A. Zeeb 
8376b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
8386b4cac81SBjoern A. Zeeb 
8396b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
8406b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
8416b4cac81SBjoern A. Zeeb 		return;
8426b4cac81SBjoern A. Zeeb 
8436b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
8446b4cac81SBjoern A. Zeeb 		return;
8456b4cac81SBjoern A. Zeeb 
8466b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
8476b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
8486b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
8496b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
8506b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8516b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
8526b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
8536b4cac81SBjoern A. Zeeb 	} else {
8546b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
8556b4cac81SBjoern A. Zeeb 	}
8566b4cac81SBjoern A. Zeeb 
8576b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8586b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
8596b4cac81SBjoern A. Zeeb 	/*
8606b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
8616b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
8626b4cac81SBjoern A. Zeeb 	 */
8636b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
8646b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
8656b4cac81SBjoern A. Zeeb 
8669d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8679d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8686b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
8696b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
8709d9ba2b7SBjoern A. Zeeb #endif
8716b4cac81SBjoern A. Zeeb 
8726b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
8736b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
8746b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
8756b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
8766b4cac81SBjoern A. Zeeb 			mc_list.count--;
8776b4cac81SBjoern A. Zeeb 		}
8786b4cac81SBjoern A. Zeeb 	}
8796b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
8806b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
8816b4cac81SBjoern A. Zeeb }
8826b4cac81SBjoern A. Zeeb 
883fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
884fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
885fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
886fa8f007dSBjoern A. Zeeb {
887fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
888fa8f007dSBjoern A. Zeeb 
889fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
890fa8f007dSBjoern A. Zeeb 
8919d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8929d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
893fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
894fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
895fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
896fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
897616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
898fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
899fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
900fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
901fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
902fa8f007dSBjoern A. Zeeb 			bss_changed);
9039d9ba2b7SBjoern A. Zeeb #endif
904fa8f007dSBjoern A. Zeeb 
905fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
906fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
907fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
908fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
909fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
910fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
911fa8f007dSBjoern A. Zeeb 	}
912fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
913fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
914fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
915fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
916fa8f007dSBjoern A. Zeeb 	}
917fa8f007dSBjoern A. Zeeb 
918fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
919fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
920fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
921fa8f007dSBjoern A. Zeeb 
9229d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
9239d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
924fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
925fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
926fa8f007dSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#08x\n",
927fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
928616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
929fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
930fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
931fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
932fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
933fa8f007dSBjoern A. Zeeb 			bss_changed);
9349d9ba2b7SBjoern A. Zeeb #endif
935fa8f007dSBjoern A. Zeeb 
936fa8f007dSBjoern A. Zeeb 	return (bss_changed);
937fa8f007dSBjoern A. Zeeb }
938fa8f007dSBjoern A. Zeeb 
9396b4cac81SBjoern A. Zeeb static void
9406b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
9416b4cac81SBjoern A. Zeeb {
9426b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9436b4cac81SBjoern A. Zeeb 	int error;
9448ac540d3SBjoern A. Zeeb 	bool cancel;
9456b4cac81SBjoern A. Zeeb 
9468ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9478ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9498ac540d3SBjoern A. Zeeb 	if (!cancel)
9506b4cac81SBjoern A. Zeeb 		return;
9516b4cac81SBjoern A. Zeeb 
9526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9536b4cac81SBjoern A. Zeeb 
954bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
955bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
9566b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
9576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
9588ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
9596b4cac81SBjoern A. Zeeb 
9606b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
9618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9628ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
9638ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
9648ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9668ac540d3SBjoern A. Zeeb 
967bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
9686b4cac81SBjoern A. Zeeb 
9698ac540d3SBjoern A. Zeeb 	if (cancel)
9706b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
9716b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
9726b4cac81SBjoern A. Zeeb }
9736b4cac81SBjoern A. Zeeb 
9746b4cac81SBjoern A. Zeeb static void
975086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
976086be6a8SBjoern A. Zeeb {
977086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
978086be6a8SBjoern A. Zeeb 	int error;
979086be6a8SBjoern A. Zeeb 	bool old;
980086be6a8SBjoern A. Zeeb 
981086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
982086be6a8SBjoern A. Zeeb 	if (old == new)
983086be6a8SBjoern A. Zeeb 		return;
984086be6a8SBjoern A. Zeeb 
985086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
986086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
987086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
988086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
989086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
990086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
991086be6a8SBjoern A. Zeeb 	}
992086be6a8SBjoern A. Zeeb }
993086be6a8SBjoern A. Zeeb 
994086be6a8SBjoern A. Zeeb static void
9956b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9966b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
9976b4cac81SBjoern A. Zeeb {
9986b4cac81SBjoern A. Zeeb 	sta->aid = 0;
999616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
10006b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
10016b4cac81SBjoern A. Zeeb 		enum ieee80211_bss_changed changed;
10026b4cac81SBjoern A. Zeeb 
10036b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
10046b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
10056b4cac81SBjoern A. Zeeb 
10066b4cac81SBjoern A. Zeeb 		changed = 0;
1007616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1008616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
10096b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
1010d9f59799SBjoern A. Zeeb 		/*
1011d9f59799SBjoern A. Zeeb 		 * This will remove the sta from firmware for iwlwifi.
1012d9f59799SBjoern A. Zeeb 		 * So confusing that they use state and flags and ... ^%$%#%$^.
1013d9f59799SBjoern A. Zeeb 		 */
10146b4cac81SBjoern A. Zeeb 		IMPROVE();
10156b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
10166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
10176b4cac81SBjoern A. Zeeb 		    changed);
1018086be6a8SBjoern A. Zeeb 
1019086be6a8SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
10206b4cac81SBjoern A. Zeeb 	}
10216b4cac81SBjoern A. Zeeb }
10226b4cac81SBjoern A. Zeeb 
10236b4cac81SBjoern A. Zeeb static void
10246b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
10256b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
10266b4cac81SBjoern A. Zeeb {
10276b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
10286b4cac81SBjoern A. Zeeb 	int tid;
1029eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
10306b4cac81SBjoern A. Zeeb 
10316b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
10326b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
10336b4cac81SBjoern A. Zeeb 
10346b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
10356b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
10366b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
10376b4cac81SBjoern A. Zeeb 				continue;
10386b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
10396b4cac81SBjoern A. Zeeb 			continue;
10406b4cac81SBjoern A. Zeeb 
10416b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
10426b4cac81SBjoern A. Zeeb 			continue;
10436b4cac81SBjoern A. Zeeb 
10446b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10456b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
10466b4cac81SBjoern A. Zeeb 			continue;
10476b4cac81SBjoern A. Zeeb 
1048eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1049eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1050eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1051eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
10526b4cac81SBjoern A. Zeeb 			continue;
10536b4cac81SBjoern A. Zeeb 
10546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
10556b4cac81SBjoern A. Zeeb 	}
10566b4cac81SBjoern A. Zeeb }
10576b4cac81SBjoern A. Zeeb 
10586b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
10596b4cac81SBjoern A. Zeeb 
10606b4cac81SBjoern A. Zeeb static int
10616b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10626b4cac81SBjoern A. Zeeb {
10636b4cac81SBjoern A. Zeeb 
10646b4cac81SBjoern A. Zeeb 	return (0);
10656b4cac81SBjoern A. Zeeb }
10666b4cac81SBjoern A. Zeeb 
10676b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
10686b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
10696b4cac81SBjoern A. Zeeb 
10706b4cac81SBjoern A. Zeeb static int
10716b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
10726b4cac81SBjoern A. Zeeb {
10736b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1074c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
10756b4cac81SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *conf;
10766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
10796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10806b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10826b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
10836b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
10846b4cac81SBjoern A. Zeeb 	uint32_t changed;
10856b4cac81SBjoern A. Zeeb 	int error;
10866b4cac81SBjoern A. Zeeb 
10872ac8a218SBjoern A. Zeeb 	/*
10882ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
10892ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
10902ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
10912ac8a218SBjoern A. Zeeb 	 */
10922ac8a218SBjoern A. Zeeb 
10932ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
10942ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
10952ac8a218SBjoern A. Zeeb 		return (EINVAL);
10962ac8a218SBjoern A. Zeeb 	}
10970936c648SBjoern A. Zeeb 	/*
10980936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
10990936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
11000936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
11010936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
11020936c648SBjoern A. Zeeb 	 */
11032ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11042ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
11052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
11062ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
11070936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11082ac8a218SBjoern A. Zeeb 		return (EINVAL);
11096b4cac81SBjoern A. Zeeb 	}
11106b4cac81SBjoern A. Zeeb 
11116b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11122ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
11132ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
11142ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
11152ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
11160936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11172ac8a218SBjoern A. Zeeb 		return (ESRCH);
11182ac8a218SBjoern A. Zeeb 	}
11192ac8a218SBjoern A. Zeeb 
11206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11216b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11226b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11236b4cac81SBjoern A. Zeeb 
11240936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
11250936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
11260936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
11270936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
11280936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
11290936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
11300936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
11310936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
11320936c648SBjoern A. Zeeb 		return (EBUSY);
11330936c648SBjoern A. Zeeb 	}
11340936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
11350936c648SBjoern A. Zeeb 
11366b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
11386b4cac81SBjoern A. Zeeb 
11396b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
11406b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
11416b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1142c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
11436b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
11446b4cac81SBjoern A. Zeeb 	} else {
11456b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1146c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
11476b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1148c5e25798SBjoern A. Zeeb 		conf = &lchanctx->conf;
11496b4cac81SBjoern A. Zeeb 	}
11506b4cac81SBjoern A. Zeeb 
11516b4cac81SBjoern A. Zeeb 	conf->rx_chains_dynamic = 1;
11526b4cac81SBjoern A. Zeeb 	conf->rx_chains_static = 1;
11536b4cac81SBjoern A. Zeeb 	conf->radar_enabled =
11546b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
11556b4cac81SBjoern A. Zeeb 	conf->def.chan = chan;
11566b4cac81SBjoern A. Zeeb 	conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
11576b4cac81SBjoern A. Zeeb 	conf->def.center_freq1 = chan->center_freq;
11586b4cac81SBjoern A. Zeeb 	conf->def.center_freq2 = 0;
11599fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
11602ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
11612ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
11629fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
11639fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
11649fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
11659fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_40;
11669fb91463SBjoern A. Zeeb 		} else
11679fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_20;
11689fb91463SBjoern A. Zeeb 	}
11699fb91463SBjoern A. Zeeb #endif
11709fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
11719fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
11729fb91463SBjoern A. Zeeb #ifdef __notyet__
11739fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
11749fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_80P80;
11759fb91463SBjoern A. Zeeb 			conf->def.center_freq2 = 0;	/* XXX */
11769fb91463SBjoern A. Zeeb 		} else
11779fb91463SBjoern A. Zeeb #endif
11789fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
11799fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_160;
11809fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
11819fb91463SBjoern A. Zeeb 			conf->def.width = NL80211_CHAN_WIDTH_80;
11829fb91463SBjoern A. Zeeb 	}
11839fb91463SBjoern A. Zeeb #endif
11846b4cac81SBjoern A. Zeeb 	/* Responder ... */
11856b4cac81SBjoern A. Zeeb 	conf->min_def.chan = chan;
11866b4cac81SBjoern A. Zeeb 	conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
11876b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq1 = chan->center_freq;
11886b4cac81SBjoern A. Zeeb 	conf->min_def.center_freq2 = 0;
11899fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
11906b4cac81SBjoern A. Zeeb 
11916ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
11926ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
11936ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
11946ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
11956ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
11966ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
11976ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
11986ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
11996ffb7bd4SBjoern A. Zeeb 
12006ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
12016ffb7bd4SBjoern A. Zeeb 
12026ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
12036ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
12046ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
12056ffb7bd4SBjoern A. Zeeb 
12066ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
12076ffb7bd4SBjoern A. Zeeb 
12086b4cac81SBjoern A. Zeeb 	error = 0;
12096b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
12106b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
12116b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
12126b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
12136b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
12146b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, conf, changed);
12156b4cac81SBjoern A. Zeeb 	} else {
12166b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, conf);
12176b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
12186b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.chan = conf->def.chan;
12196b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.width = conf->def.width;
12206b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq1 =
12216b4cac81SBjoern A. Zeeb 			    conf->def.center_freq1;
12229fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
12239fb91463SBjoern A. Zeeb 			if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) {
12249fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
12259fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
12269fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 += 10;
12279fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
12289fb91463SBjoern A. Zeeb 					vif->bss_conf.chandef.center_freq1 -= 10;
12299fb91463SBjoern A. Zeeb 			}
12309fb91463SBjoern A. Zeeb #endif
12316b4cac81SBjoern A. Zeeb 			vif->bss_conf.chandef.center_freq2 =
12326b4cac81SBjoern A. Zeeb 			    conf->def.center_freq2;
12336b4cac81SBjoern A. Zeeb 		} else {
1234018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1235018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
12366b4cac81SBjoern A. Zeeb 			goto out;
12376b4cac81SBjoern A. Zeeb 		}
12386ffb7bd4SBjoern A. Zeeb 
12396ffb7bd4SBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = conf;
12406ffb7bd4SBjoern A. Zeeb 
12416b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
12426b4cac81SBjoern A. Zeeb 		if (error == 0)
124368541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
124468541546SBjoern A. Zeeb 			    &vif->bss_conf, conf);
12456b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
12466b4cac81SBjoern A. Zeeb 			error = 0;
12476b4cac81SBjoern A. Zeeb 		if (error != 0) {
1248018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1249018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
12506b4cac81SBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, conf);
1251c5e25798SBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1252c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
12536b4cac81SBjoern A. Zeeb 			goto out;
12546b4cac81SBjoern A. Zeeb 		}
12556b4cac81SBjoern A. Zeeb 	}
12566b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
12576b4cac81SBjoern A. Zeeb 
12586b4cac81SBjoern A. Zeeb 	/* RATES */
12596b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
12606b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
12616b4cac81SBjoern A. Zeeb 
1262d9f59799SBjoern A. Zeeb 	/*
12630936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
12640936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
12650936c648SBjoern A. Zeeb 	 * under the hoods.
1266d9f59799SBjoern A. Zeeb 	 */
12670936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
12680936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
12696b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1270e24e8103SBjoern A. Zeeb 
1271e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
12720936c648SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened. */
12730936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? or deal as error? */
12742ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL)
12752ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
12762ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
12772ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
12782ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
12792ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
12802ac8a218SBjoern A. Zeeb 
12810936c648SBjoern A. Zeeb 	/*
12820936c648SBjoern A. Zeeb 	 * Reference the ni for this cache of lsta/ni on lvif->lvif_bss
12830936c648SBjoern A. Zeeb 	 * essentially out lsta version of the iv_bss.
12840936c648SBjoern A. Zeeb 	 * Do NOT use iv_bss here anymore as that may have diverged from our
12850936c648SBjoern A. Zeeb 	 * function local ni already and would lead to inconsistencies.
12860936c648SBjoern A. Zeeb 	 */
12870936c648SBjoern A. Zeeb 	ieee80211_ref_node(ni);
12882ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
12892ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = true;
12902ac8a218SBjoern A. Zeeb 
12912ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1292e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1293e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1294e24e8103SBjoern A. Zeeb 
1295d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
12966b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
12976b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
12986b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1299e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
13006b4cac81SBjoern A. Zeeb 	if (error != 0) {
13016b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1302018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1303018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
13046b4cac81SBjoern A. Zeeb 		goto out;
13056b4cac81SBjoern A. Zeeb 	}
13066b4cac81SBjoern A. Zeeb #if 0
13076b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
13086b4cac81SBjoern A. Zeeb #endif
13096b4cac81SBjoern A. Zeeb 
13109d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13119d9ba2b7SBjoern A. Zeeb 
13126b4cac81SBjoern A. Zeeb 	/*
13136b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
13146b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
13156b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
13166b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1317d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1318d9f59799SBjoern A. Zeeb 	 * for all queues.
13196b4cac81SBjoern A. Zeeb 	 */
1320e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
13216b4cac81SBjoern A. Zeeb 
13226b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
13236b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13246b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
13256b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
13266b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
13276b4cac81SBjoern A. Zeeb 
13286b4cac81SBjoern A. Zeeb 	/*
13296b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
13306b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
13316b4cac81SBjoern A. Zeeb 	 * - event_callback
13326b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
13336b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
13346b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
13356b4cac81SBjoern A. Zeeb 	 */
13366b4cac81SBjoern A. Zeeb 
13376b4cac81SBjoern A. Zeeb out:
13388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
13396b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
13400936c648SBjoern A. Zeeb 	/*
13410936c648SBjoern A. Zeeb 	 * Release the reference that keop the ni stable locally
13420936c648SBjoern A. Zeeb 	 * during the work of this function.
13430936c648SBjoern A. Zeeb 	 */
13446b4cac81SBjoern A. Zeeb 	if (ni != NULL)
13456b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
13466b4cac81SBjoern A. Zeeb 	return (error);
13476b4cac81SBjoern A. Zeeb }
13486b4cac81SBjoern A. Zeeb 
13496b4cac81SBjoern A. Zeeb static int
13506b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
13516b4cac81SBjoern A. Zeeb {
13526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13536b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13546b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
13556b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
13566b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
13576b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
13586b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
13596b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
13606b4cac81SBjoern A. Zeeb 	int error;
13616b4cac81SBjoern A. Zeeb 
13626b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
13636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13646b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
13656b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
13666b4cac81SBjoern A. Zeeb 
13672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
13682ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13692ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
13702ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
13712ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
13722ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
13732ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
13742ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
13752ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
13762ac8a218SBjoern A. Zeeb #endif
13772ac8a218SBjoern A. Zeeb 
13782ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
13792ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
13802ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
13812ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
13822ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
13832ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
13846b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
13856b4cac81SBjoern A. Zeeb 
138667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13876b4cac81SBjoern A. Zeeb 
13886b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
13898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
13906b4cac81SBjoern A. Zeeb 
1391d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1392d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1393d9f59799SBjoern A. Zeeb 
13946b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
13956b4cac81SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
13966b4cac81SBjoern A. Zeeb 
13976b4cac81SBjoern A. Zeeb 	/* flush, no drop */
13986b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
13996b4cac81SBjoern A. Zeeb 
14006b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
14016b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
14026b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14036b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
14046b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
14056b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
14066b4cac81SBjoern A. Zeeb 	}
14076b4cac81SBjoern A. Zeeb 
14086b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
14096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
14106b4cac81SBjoern A. Zeeb 
14116b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
14126b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1413d9f59799SBjoern A. Zeeb 
1414d9f59799SBjoern A. Zeeb 	/* Take the station down. */
14156b4cac81SBjoern A. Zeeb 
14166b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
14176b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
14186b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1419d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1420e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
14216b4cac81SBjoern A. Zeeb 	if (error != 0) {
14226b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1423018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1424018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
14256b4cac81SBjoern A. Zeeb 		goto out;
14266b4cac81SBjoern A. Zeeb 	}
14276b4cac81SBjoern A. Zeeb #if 0
14286b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
14296b4cac81SBjoern A. Zeeb #endif
14306b4cac81SBjoern A. Zeeb 
143167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
14326b4cac81SBjoern A. Zeeb 
14332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
14342ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
14352ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
14362ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
14372ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1438d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
14390936c648SBjoern A. Zeeb 	/*
14400936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
14410936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
14420936c648SBjoern A. Zeeb 	 * and potentially freed.
14430936c648SBjoern A. Zeeb 	 */
14440936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1445d9f59799SBjoern A. Zeeb 
1446d9f59799SBjoern A. Zeeb 	/* conf_tx */
1447d9f59799SBjoern A. Zeeb 
1448d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
14496b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1450c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
14516b4cac81SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
14526b4cac81SBjoern A. Zeeb 
14536b4cac81SBjoern A. Zeeb 		conf = vif->chanctx_conf;
14546b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
145568541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
14566b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
14576b4cac81SBjoern A. Zeeb 
14586b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
14596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1460c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1461c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
14626b4cac81SBjoern A. Zeeb 	}
14636b4cac81SBjoern A. Zeeb 
14646b4cac81SBjoern A. Zeeb out:
14658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14666b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
14676b4cac81SBjoern A. Zeeb 	return (error);
14686b4cac81SBjoern A. Zeeb }
14696b4cac81SBjoern A. Zeeb 
14706b4cac81SBjoern A. Zeeb static int
14716b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14726b4cac81SBjoern A. Zeeb {
14736b4cac81SBjoern A. Zeeb 	int error;
14746b4cac81SBjoern A. Zeeb 
14756b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
14766b4cac81SBjoern A. Zeeb 	if (error == 0)
14776b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
14786b4cac81SBjoern A. Zeeb 	return (error);
14796b4cac81SBjoern A. Zeeb }
14806b4cac81SBjoern A. Zeeb 
14816b4cac81SBjoern A. Zeeb static int
14826b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14836b4cac81SBjoern A. Zeeb {
14846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14856b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14866b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14876b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14886b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14896b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14906b4cac81SBjoern A. Zeeb 	int error;
14916b4cac81SBjoern A. Zeeb 
14926b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14936b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14946b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14956b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14966b4cac81SBjoern A. Zeeb 
14976b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14988ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14992ac8a218SBjoern A. Zeeb 
15002ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15012ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
15022ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
15032ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15042ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
15052ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
15062ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
15072ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
15082ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
15092ac8a218SBjoern A. Zeeb #endif
15102ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
15112ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
15122ac8a218SBjoern A. Zeeb 		goto out;
15132ac8a218SBjoern A. Zeeb 	}
15142ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
15152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
15162ac8a218SBjoern A. Zeeb 
15172ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
15186b4cac81SBjoern A. Zeeb 
15196b4cac81SBjoern A. Zeeb 	/* Finish auth. */
15206b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
15216b4cac81SBjoern A. Zeeb 
15226b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
15236b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
15246b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1525e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1526018d93ecSBjoern A. Zeeb 	if (error != 0) {
1527018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1528018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
15296b4cac81SBjoern A. Zeeb 		goto out;
1530018d93ecSBjoern A. Zeeb 	}
15316b4cac81SBjoern A. Zeeb 
15326b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
15336b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
15346b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15356b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
15366b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
15376b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
15386b4cac81SBjoern A. Zeeb 	}
15396b4cac81SBjoern A. Zeeb 
15406b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
15416b4cac81SBjoern A. Zeeb 
15426b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
15436b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
15446b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
15456b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
15466b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
15476b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
15486b4cac81SBjoern A. Zeeb 	}
15496b4cac81SBjoern A. Zeeb 
15506b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
1551e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), true, true);
15526b4cac81SBjoern A. Zeeb 
15536b4cac81SBjoern A. Zeeb 	/*
15546b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
15556b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
15566b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
15576b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
15586b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
15596b4cac81SBjoern A. Zeeb 	 * - event_callback
15606b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
15616b4cac81SBjoern A. Zeeb 	 */
15626b4cac81SBjoern A. Zeeb 
15636b4cac81SBjoern A. Zeeb out:
15648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15656b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15666b4cac81SBjoern A. Zeeb 	return (error);
15676b4cac81SBjoern A. Zeeb }
15686b4cac81SBjoern A. Zeeb 
15696b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
15706b4cac81SBjoern A. Zeeb static int
15716b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15726b4cac81SBjoern A. Zeeb {
15736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15746b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15756b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15766b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15776b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15786b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
15792ac8a218SBjoern A. Zeeb 	int error;
15806b4cac81SBjoern A. Zeeb 
15816b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15826b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15836b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15846b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15856b4cac81SBjoern A. Zeeb 
15866b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
15878ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15882ac8a218SBjoern A. Zeeb 
15892ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15902ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
15912ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
15922ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15932ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
15942ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
15952ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
15962ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
15972ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
15982ac8a218SBjoern A. Zeeb #endif
15992ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16002ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16012ac8a218SBjoern A. Zeeb 		goto out;
16022ac8a218SBjoern A. Zeeb 	}
16032ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16042ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16052ac8a218SBjoern A. Zeeb 
16062ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
16072ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
16086b4cac81SBjoern A. Zeeb 
16096b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16126b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16136b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16146b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
16156b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16166b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16176b4cac81SBjoern A. Zeeb 	}
16186b4cac81SBjoern A. Zeeb 
16196b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
16206b4cac81SBjoern A. Zeeb 
16216b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
16226b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
16236b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16246b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
16256b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
16266b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
16276b4cac81SBjoern A. Zeeb 	}
16286b4cac81SBjoern A. Zeeb 
16292ac8a218SBjoern A. Zeeb 	error = 0;
16302ac8a218SBjoern A. Zeeb out:
16318ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16326b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16336b4cac81SBjoern A. Zeeb 
16342ac8a218SBjoern A. Zeeb 	return (error);
16356b4cac81SBjoern A. Zeeb }
16366b4cac81SBjoern A. Zeeb 
16376b4cac81SBjoern A. Zeeb static int
1638d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16396b4cac81SBjoern A. Zeeb {
16406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16446b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16456b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16466b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
16476b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1648d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
16496b4cac81SBjoern A. Zeeb 	int error;
16506b4cac81SBjoern A. Zeeb 
16516b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16536b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16546b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16556b4cac81SBjoern A. Zeeb 
16562ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16572ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16582ac8a218SBjoern A. Zeeb 
16592ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16602ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16612ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
16622ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
16632ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16642ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16652ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16662ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16672ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16682ac8a218SBjoern A. Zeeb #endif
16692ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16702ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16712ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
16722ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
16732ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
16742ac8a218SBjoern A. Zeeb 
16752ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
16766b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
16776b4cac81SBjoern A. Zeeb 
167867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1679d9f59799SBjoern A. Zeeb 
1680d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1681d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1682d9f59799SBjoern A. Zeeb 
1683d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1684d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1685d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1686d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1687d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1688d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1689d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1690d9f59799SBjoern A. Zeeb 	}
1691d9f59799SBjoern A. Zeeb 
16928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1693d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1694d9f59799SBjoern A. Zeeb 
1695d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
1696d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1697018d93ecSBjoern A. Zeeb 	if (error != 0) {
1698018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1699018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1700d9f59799SBjoern A. Zeeb 		goto outni;
1701018d93ecSBjoern A. Zeeb 	}
1702d9f59799SBjoern A. Zeeb 
1703d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17048ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1705d9f59799SBjoern A. Zeeb 
170667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1707d9f59799SBjoern A. Zeeb 
1708d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
1709d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
1710d9f59799SBjoern A. Zeeb 
1711d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1712d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1713d9f59799SBjoern A. Zeeb 
17146b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
17156b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
17166b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17176b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
17186b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
17196b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
17206b4cac81SBjoern A. Zeeb 	}
17216b4cac81SBjoern A. Zeeb 
1722d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1723d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1724d9f59799SBjoern A. Zeeb 
1725d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1726d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1727d9f59799SBjoern A. Zeeb 
1728d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1729d9f59799SBjoern A. Zeeb 
17306b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
17316b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
17326b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
17336b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1734e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1735018d93ecSBjoern A. Zeeb 	if (error != 0) {
1736018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1737018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
17386b4cac81SBjoern A. Zeeb 		goto out;
1739018d93ecSBjoern A. Zeeb 	}
17406b4cac81SBjoern A. Zeeb 
174167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
17426b4cac81SBjoern A. Zeeb 
174316e688b2SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
174416e688b2SBjoern A. Zeeb 	/*
174516e688b2SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
174616e688b2SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
174716e688b2SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
174816e688b2SBjoern A. Zeeb 	 * a fw assert.
174916e688b2SBjoern A. Zeeb 	 */
175016e688b2SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
175116e688b2SBjoern A. Zeeb 
1752d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1753d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1754d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1755d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1756e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1757d9f59799SBjoern A. Zeeb 	if (error != 0) {
1758d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1759018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1760018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
1761d9f59799SBjoern A. Zeeb 		goto out;
1762d9f59799SBjoern A. Zeeb 	}
1763d9f59799SBjoern A. Zeeb 
176416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1765d9f59799SBjoern A. Zeeb 
1766d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1767d9f59799SBjoern A. Zeeb 	bss_changed = 0;
1768d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1769d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1770616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
1771616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1772d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1773d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1774d9f59799SBjoern A. Zeeb 
17752ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17762ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
17772ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
17782ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
17792ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1780d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
17810936c648SBjoern A. Zeeb 	/*
17820936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
17830936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
17840936c648SBjoern A. Zeeb 	 * and potentially freed.
17850936c648SBjoern A. Zeeb 	 */
17860936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1787d9f59799SBjoern A. Zeeb 
1788d9f59799SBjoern A. Zeeb 	/* conf_tx */
1789d9f59799SBjoern A. Zeeb 
1790d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1791d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1792c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1793d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
1794d9f59799SBjoern A. Zeeb 
1795d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
1796d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
179768541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1798d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1799d9f59799SBjoern A. Zeeb 
1800d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1801d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
1802c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
1803c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
1804d9f59799SBjoern A. Zeeb 	}
1805d9f59799SBjoern A. Zeeb 
1806d9f59799SBjoern A. Zeeb 	error = EALREADY;
18076b4cac81SBjoern A. Zeeb out:
18088ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
18096b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1810d9f59799SBjoern A. Zeeb outni:
18116b4cac81SBjoern A. Zeeb 	return (error);
18126b4cac81SBjoern A. Zeeb }
18136b4cac81SBjoern A. Zeeb 
18146b4cac81SBjoern A. Zeeb static int
1815d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1816d9f59799SBjoern A. Zeeb {
1817d9f59799SBjoern A. Zeeb 	int error;
1818d9f59799SBjoern A. Zeeb 
1819d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1820d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1821d9f59799SBjoern A. Zeeb 		return (error);
1822d9f59799SBjoern A. Zeeb 
1823d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1824d9f59799SBjoern A. Zeeb 
1825d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1826d9f59799SBjoern A. Zeeb 	return (error);
1827d9f59799SBjoern A. Zeeb }
1828d9f59799SBjoern A. Zeeb 
1829d9f59799SBjoern A. Zeeb static int
18306b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18316b4cac81SBjoern A. Zeeb {
18326b4cac81SBjoern A. Zeeb 	int error;
18336b4cac81SBjoern A. Zeeb 
1834d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
18356b4cac81SBjoern A. Zeeb 	return (error);
18366b4cac81SBjoern A. Zeeb }
18376b4cac81SBjoern A. Zeeb 
18386b4cac81SBjoern A. Zeeb static int
18396b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18406b4cac81SBjoern A. Zeeb {
18416b4cac81SBjoern A. Zeeb 	int error;
18426b4cac81SBjoern A. Zeeb 
1843d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
18446b4cac81SBjoern A. Zeeb 	return (error);
18456b4cac81SBjoern A. Zeeb }
18466b4cac81SBjoern A. Zeeb 
18476b4cac81SBjoern A. Zeeb static int
18486b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18496b4cac81SBjoern A. Zeeb {
18506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18516b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18526b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18536b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18546b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
18556b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
18566b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
18576b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
18586b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
18596b4cac81SBjoern A. Zeeb 	int error;
18606b4cac81SBjoern A. Zeeb 
18616b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
18626b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18636b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18646b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
18656b4cac81SBjoern A. Zeeb 
18666b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
18678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
18682ac8a218SBjoern A. Zeeb 
18692ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18702ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
18712ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
18722ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
18732ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
18742ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
18752ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
18762ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
18772ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
18782ac8a218SBjoern A. Zeeb #endif
18792ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
18802ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
18812ac8a218SBjoern A. Zeeb 		goto out;
18822ac8a218SBjoern A. Zeeb 	}
18832ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
18842ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
18852ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
18862ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
18872ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
18882ac8a218SBjoern A. Zeeb 
18892ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
18906b4cac81SBjoern A. Zeeb 
18916b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
18926b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
18936b4cac81SBjoern A. Zeeb 
18949597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
18959597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
18969597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
18979597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
18986b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
18999597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
19004a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19014a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
19024a67f1dfSBjoern A. Zeeb 		sta->wme = true;
19034a67f1dfSBjoern A. Zeeb #endif
1904e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1905018d93ecSBjoern A. Zeeb 	if (error != 0) {
1906018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
1907018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19089597f7cbSBjoern A. Zeeb 		goto out;
1909018d93ecSBjoern A. Zeeb 	}
19106b4cac81SBjoern A. Zeeb 
19116b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
19126b4cac81SBjoern A. Zeeb 
19136b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
19146b4cac81SBjoern A. Zeeb 	bss_changed = 0;
19154a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19164a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
19174a67f1dfSBjoern A. Zeeb #endif
1918616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
1919616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
1920616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
19216b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
19226b4cac81SBjoern A. Zeeb 	}
19236b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
1924616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
1925616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
19266b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
19276b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
19286b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
19296b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
19306b4cac81SBjoern A. Zeeb 	}
19316b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
19326b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
19336b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
19346b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
19356b4cac81SBjoern A. Zeeb 	}
19366b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
19376b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
19386b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
19396b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
19406b4cac81SBjoern A. Zeeb 	}
1941fa8f007dSBjoern A. Zeeb 
1942fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1943fa8f007dSBjoern A. Zeeb 
19446b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
19456b4cac81SBjoern A. Zeeb 
19466b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
19476b4cac81SBjoern A. Zeeb 	 * - event_callback
19486b4cac81SBjoern A. Zeeb 	 */
19496b4cac81SBjoern A. Zeeb 
19506b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
19516b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
19526b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19536b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
19546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
19556b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
19566b4cac81SBjoern A. Zeeb 	}
19576b4cac81SBjoern A. Zeeb 
1958086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
1959086be6a8SBjoern A. Zeeb 
19606b4cac81SBjoern A. Zeeb 	/*
19616b4cac81SBjoern A. Zeeb 	 * And then:
19626b4cac81SBjoern A. Zeeb 	 * - (more packets)?
19636b4cac81SBjoern A. Zeeb 	 * - set_key
19646b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
19656b4cac81SBjoern A. Zeeb 	 * - set_key (?)
19666b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
19676b4cac81SBjoern A. Zeeb 	 */
19686b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
19696b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
19706b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
19716b4cac81SBjoern A. Zeeb 
19726b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
19736b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
19746b4cac81SBjoern A. Zeeb 	}
19756b4cac81SBjoern A. Zeeb 
19769fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
19779fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
19789fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
19799fb91463SBjoern A. Zeeb #endif
19809fb91463SBjoern A. Zeeb 
19816b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
19826b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
19836b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
19846b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
1985e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
19866b4cac81SBjoern A. Zeeb 	if (error != 0) {
19876b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
1988018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
1989018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19906b4cac81SBjoern A. Zeeb 		goto out;
19916b4cac81SBjoern A. Zeeb 	}
19926b4cac81SBjoern A. Zeeb 
19936b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
19946b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
19956b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
19966b4cac81SBjoern A. Zeeb 	 *
19976b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
19986b4cac81SBjoern A. Zeeb 	 */
19996b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
20006b4cac81SBjoern A. Zeeb 
2001fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2002fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2003fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2004fa8f007dSBjoern A. Zeeb 
20056b4cac81SBjoern A. Zeeb out:
20068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
20086b4cac81SBjoern A. Zeeb 	return (error);
20096b4cac81SBjoern A. Zeeb }
20106b4cac81SBjoern A. Zeeb 
20116b4cac81SBjoern A. Zeeb static int
20126b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20136b4cac81SBjoern A. Zeeb {
20146b4cac81SBjoern A. Zeeb 	int error;
20156b4cac81SBjoern A. Zeeb 
20166b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
20176b4cac81SBjoern A. Zeeb 	if (error == 0)
20186b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
20196b4cac81SBjoern A. Zeeb 	return (error);
20206b4cac81SBjoern A. Zeeb }
20216b4cac81SBjoern A. Zeeb 
20226b4cac81SBjoern A. Zeeb static int
20236b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20246b4cac81SBjoern A. Zeeb {
20256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20296b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20306b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20316b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2032d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2033d9f59799SBjoern A. Zeeb #if 0
2034d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2035d9f59799SBjoern A. Zeeb #endif
20366b4cac81SBjoern A. Zeeb 	int error;
20376b4cac81SBjoern A. Zeeb 
20386b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20396b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20406b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20416b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20426b4cac81SBjoern A. Zeeb 
20432ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20442ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20452ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
20462ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
20472ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20482ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20492ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20502ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20512ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20522ac8a218SBjoern A. Zeeb #endif
20532ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20542ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20552ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
20562ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
20572ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
20582ac8a218SBjoern A. Zeeb 
20592ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
20606b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
20616b4cac81SBjoern A. Zeeb 
206267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2063d9f59799SBjoern A. Zeeb 
2064d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2066d9f59799SBjoern A. Zeeb 
2067d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2068d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2069d9f59799SBjoern A. Zeeb 
2070d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2071d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2072d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2073d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2074d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2075d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2076d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2077d9f59799SBjoern A. Zeeb 	}
2078d9f59799SBjoern A. Zeeb 
20798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2080d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2081d9f59799SBjoern A. Zeeb 
2082d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2083d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2084018d93ecSBjoern A. Zeeb 	if (error != 0) {
2085018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2086018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2087d9f59799SBjoern A. Zeeb 		goto outni;
2088018d93ecSBjoern A. Zeeb 	}
2089d9f59799SBjoern A. Zeeb 
2090d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2092d9f59799SBjoern A. Zeeb 
209367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2094d9f59799SBjoern A. Zeeb 
2095d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2096d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2097d9f59799SBjoern A. Zeeb 
2098d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2099d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2100d9f59799SBjoern A. Zeeb 
2101d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2102d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2103d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2104d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2105d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2106d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2107d9f59799SBjoern A. Zeeb 	}
2108d9f59799SBjoern A. Zeeb 
2109d9f59799SBjoern A. Zeeb #if 0
2110d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2111d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2112d9f59799SBjoern A. Zeeb 
2113d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2114d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2115d9f59799SBjoern A. Zeeb #endif
2116d9f59799SBjoern A. Zeeb 
2117d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2118d9f59799SBjoern A. Zeeb 
21196b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
21206b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21216b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
21226b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2123e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2124018d93ecSBjoern A. Zeeb 	if (error != 0) {
2125018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2126018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21276b4cac81SBjoern A. Zeeb 		goto out;
2128018d93ecSBjoern A. Zeeb 	}
21296b4cac81SBjoern A. Zeeb 
213067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21316b4cac81SBjoern A. Zeeb 
21326b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
21336b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21346b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
21356b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2136e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2137018d93ecSBjoern A. Zeeb 	if (error != 0) {
2138018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2139018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21406b4cac81SBjoern A. Zeeb 		goto out;
2141018d93ecSBjoern A. Zeeb 	}
21426b4cac81SBjoern A. Zeeb 
214367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21446b4cac81SBjoern A. Zeeb 
2145d9f59799SBjoern A. Zeeb #if 0
2146d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2147d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2148d9f59799SBjoern A. Zeeb #endif
2149d9f59799SBjoern A. Zeeb 
2150d9f59799SBjoern A. Zeeb 	error = EALREADY;
21516b4cac81SBjoern A. Zeeb out:
21528ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21536b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2154d9f59799SBjoern A. Zeeb outni:
21556b4cac81SBjoern A. Zeeb 	return (error);
21566b4cac81SBjoern A. Zeeb }
21576b4cac81SBjoern A. Zeeb 
21586b4cac81SBjoern A. Zeeb static int
2159d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2160d9f59799SBjoern A. Zeeb {
2161d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2162d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2163d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2164d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2165d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2166d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2167d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2168d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2169d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2170d9f59799SBjoern A. Zeeb 	int error;
2171d9f59799SBjoern A. Zeeb 
2172d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2173d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2174d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2175d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2176d9f59799SBjoern A. Zeeb 
21772ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21782ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21792ac8a218SBjoern A. Zeeb 
21802ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21812ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21822ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
21832ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
21842ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21852ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21862ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21872ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21882ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21892ac8a218SBjoern A. Zeeb #endif
21902ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21912ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21922ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
21932ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
21942ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
21952ac8a218SBjoern A. Zeeb 
21962ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2197d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2198d9f59799SBjoern A. Zeeb 
219967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2200d9f59799SBjoern A. Zeeb 
2201d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2202d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2203d9f59799SBjoern A. Zeeb 
2204d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2205d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2206d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2207d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2208d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2209d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2210d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2211d9f59799SBjoern A. Zeeb 	}
2212d9f59799SBjoern A. Zeeb 
22138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2214d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2215d9f59799SBjoern A. Zeeb 
2216d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2217d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2218018d93ecSBjoern A. Zeeb 	if (error != 0) {
2219018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2220018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2221d9f59799SBjoern A. Zeeb 		goto outni;
2222018d93ecSBjoern A. Zeeb 	}
2223d9f59799SBjoern A. Zeeb 
2224d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2226d9f59799SBjoern A. Zeeb 
222767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2228d9f59799SBjoern A. Zeeb 
2229d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
2230d9f59799SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, true, true);
2231d9f59799SBjoern A. Zeeb 
2232d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2233d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2234d9f59799SBjoern A. Zeeb 
2235d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2236d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2237d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2238d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2239d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2240d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2241d9f59799SBjoern A. Zeeb 	}
2242d9f59799SBjoern A. Zeeb 
2243d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2244d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2245d9f59799SBjoern A. Zeeb 
2246d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2247d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2248d9f59799SBjoern A. Zeeb 
2249d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2250d9f59799SBjoern A. Zeeb 
2251d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2252d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2253d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2254d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2255e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2256018d93ecSBjoern A. Zeeb 	if (error != 0) {
2257018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2258018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2259d9f59799SBjoern A. Zeeb 		goto out;
2260018d93ecSBjoern A. Zeeb 	}
2261d9f59799SBjoern A. Zeeb 
226267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2263d9f59799SBjoern A. Zeeb 
2264d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2265d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2266d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2267d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2268e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2269018d93ecSBjoern A. Zeeb 	if (error != 0) {
2270018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2271018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2272d9f59799SBjoern A. Zeeb 		goto out;
2273018d93ecSBjoern A. Zeeb 	}
2274d9f59799SBjoern A. Zeeb 
227567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2276d9f59799SBjoern A. Zeeb 
2277d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2278d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2279d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2280d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2281e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2282018d93ecSBjoern A. Zeeb 	if (error != 0) {
2283018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2284018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2285d9f59799SBjoern A. Zeeb 		goto out;
2286018d93ecSBjoern A. Zeeb 	}
2287d9f59799SBjoern A. Zeeb 
228867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2289d9f59799SBjoern A. Zeeb 
229016e688b2SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
229116e688b2SBjoern A. Zeeb 	/*
229216e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
229316e688b2SBjoern A. Zeeb 	 * See comment there; removes the sta from fw.
229416e688b2SBjoern A. Zeeb 	 */
229516e688b2SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
229616e688b2SBjoern A. Zeeb 
2297d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2298d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2299d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2300d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2301e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2302d9f59799SBjoern A. Zeeb 	if (error != 0) {
2303d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2304018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2305018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2306d9f59799SBjoern A. Zeeb 		goto out;
2307d9f59799SBjoern A. Zeeb 	}
2308d9f59799SBjoern A. Zeeb 
230916e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2310d9f59799SBjoern A. Zeeb 
2311d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2312d9f59799SBjoern A. Zeeb 	bss_changed = 0;
2313d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2314d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2315616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2316616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2317d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2318d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2319d9f59799SBjoern A. Zeeb 
23202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23212ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23222ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23232ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23242ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2325d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23260936c648SBjoern A. Zeeb 	/*
23270936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23280936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23290936c648SBjoern A. Zeeb 	 * and potentially freed.
23300936c648SBjoern A. Zeeb 	 */
23310936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2332d9f59799SBjoern A. Zeeb 
2333d9f59799SBjoern A. Zeeb 	/* conf_tx */
2334d9f59799SBjoern A. Zeeb 
2335d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2336d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2337c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2338d9f59799SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *conf;
2339d9f59799SBjoern A. Zeeb 
2340d9f59799SBjoern A. Zeeb 		conf = vif->chanctx_conf;
2341d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
234268541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2343d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2344d9f59799SBjoern A. Zeeb 
2345d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2346d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, conf);
2347c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(conf);
2348c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2349d9f59799SBjoern A. Zeeb 	}
2350d9f59799SBjoern A. Zeeb 
2351d9f59799SBjoern A. Zeeb 	error = EALREADY;
2352d9f59799SBjoern A. Zeeb out:
23538ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2354d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2355d9f59799SBjoern A. Zeeb outni:
2356d9f59799SBjoern A. Zeeb 	return (error);
2357d9f59799SBjoern A. Zeeb }
2358d9f59799SBjoern A. Zeeb 
2359d9f59799SBjoern A. Zeeb static int
2360d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2361d9f59799SBjoern A. Zeeb {
2362d9f59799SBjoern A. Zeeb 
2363d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2364d9f59799SBjoern A. Zeeb }
2365d9f59799SBjoern A. Zeeb 
2366d9f59799SBjoern A. Zeeb static int
23676b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23686b4cac81SBjoern A. Zeeb {
23696b4cac81SBjoern A. Zeeb 	int error;
23706b4cac81SBjoern A. Zeeb 
2371d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2372d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2373d9f59799SBjoern A. Zeeb 		return (error);
2374d9f59799SBjoern A. Zeeb 
2375d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2376d9f59799SBjoern A. Zeeb 
2377d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
23786b4cac81SBjoern A. Zeeb 	return (error);
23796b4cac81SBjoern A. Zeeb }
23806b4cac81SBjoern A. Zeeb 
2381d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
23826b4cac81SBjoern A. Zeeb 
23836b4cac81SBjoern A. Zeeb /*
23846b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
23856b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
23866b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
23876b4cac81SBjoern A. Zeeb  */
23886b4cac81SBjoern A. Zeeb struct fsm_state {
23896b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
23906b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
23916b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
23926b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
23936b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
23946b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
23956b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
23966b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2397d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2398d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
23996b4cac81SBjoern A. Zeeb 
24006b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
24016b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
24026b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
24036b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2404d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
24056b4cac81SBjoern A. Zeeb 
2406d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2407d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2408d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2409d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2410d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
24116b4cac81SBjoern A. Zeeb 
2412d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2413d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2414d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
24156b4cac81SBjoern A. Zeeb 
24166b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
24176b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
24186b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
24196b4cac81SBjoern A. Zeeb 
24206b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
24216b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
24226b4cac81SBjoern A. Zeeb };
24236b4cac81SBjoern A. Zeeb 
24246b4cac81SBjoern A. Zeeb static int
24256b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24266b4cac81SBjoern A. Zeeb {
24276b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
24286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24296b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24306b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24316b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
24326b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
24336b4cac81SBjoern A. Zeeb 	int error;
24346b4cac81SBjoern A. Zeeb 
24356b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
24366b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
24376b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
24386b4cac81SBjoern A. Zeeb 
24399d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24409d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
24416b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
24426b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
24439d9ba2b7SBjoern A. Zeeb #endif
24446b4cac81SBjoern A. Zeeb 
24456b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
24466b4cac81SBjoern A. Zeeb 
24476b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
24486b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
24496b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
24506b4cac81SBjoern A. Zeeb 
24516b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
24526b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
24536b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
24546b4cac81SBjoern A. Zeeb 
24556b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
24566b4cac81SBjoern A. Zeeb 
24576b4cac81SBjoern A. Zeeb 	} else {
24586b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
24596b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
24606b4cac81SBjoern A. Zeeb 		return (ENOSYS);
24616b4cac81SBjoern A. Zeeb 	}
24626b4cac81SBjoern A. Zeeb 
24636b4cac81SBjoern A. Zeeb 	error = 0;
24646b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
24656b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
24669d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24679d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2468d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2469d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2470d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2471d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
24729d9ba2b7SBjoern A. Zeeb #endif
24736b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
24746b4cac81SBjoern A. Zeeb 			break;
24756b4cac81SBjoern A. Zeeb 		}
24766b4cac81SBjoern A. Zeeb 	}
24776b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
24786b4cac81SBjoern A. Zeeb 
24796b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2480d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
24816b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
24826b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
24836b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
24846b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
24856b4cac81SBjoern A. Zeeb 		return (ENOSYS);
24866b4cac81SBjoern A. Zeeb 	}
24876b4cac81SBjoern A. Zeeb 
24886b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
24899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24909d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2491d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2492d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2493d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2494d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
24959d9ba2b7SBjoern A. Zeeb #endif
24966b4cac81SBjoern A. Zeeb 		return (0);
24976b4cac81SBjoern A. Zeeb 	}
24986b4cac81SBjoern A. Zeeb 
24996b4cac81SBjoern A. Zeeb 	if (error != 0) {
25006b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
25016b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
25026b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
25036b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
250445c27ad5SBjoern A. Zeeb 		return (error);
25056b4cac81SBjoern A. Zeeb 	}
25066b4cac81SBjoern A. Zeeb 
25079d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25089d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2509d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2510d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
25116b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
25129d9ba2b7SBjoern A. Zeeb #endif
25136b4cac81SBjoern A. Zeeb 
25146b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
25156b4cac81SBjoern A. Zeeb }
25166b4cac81SBjoern A. Zeeb 
25176b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
25186b4cac81SBjoern A. Zeeb 
2519d9f59799SBjoern A. Zeeb /*
2520d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2521d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2522d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2523d9f59799SBjoern A. Zeeb  */
2524d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2525d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2526d9f59799SBjoern A. Zeeb {
2527d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25282ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2529d9f59799SBjoern A. Zeeb 
25302ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2531d9f59799SBjoern A. Zeeb 
2532ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25332ac8a218SBjoern A. Zeeb 
25342ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25352ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
25362ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25372ac8a218SBjoern A. Zeeb 
25382ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
25392ac8a218SBjoern A. Zeeb 	return (rni);
2540d9f59799SBjoern A. Zeeb }
2541d9f59799SBjoern A. Zeeb 
25424a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25436b4cac81SBjoern A. Zeeb static int
25444a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
25456b4cac81SBjoern A. Zeeb {
25464a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
25476b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25496b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25506b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
25516b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
25526b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
25536b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
25546b4cac81SBjoern A. Zeeb 	int error;
25556b4cac81SBjoern A. Zeeb 	uint16_t ac;
25566b4cac81SBjoern A. Zeeb 
25576b4cac81SBjoern A. Zeeb 	IMPROVE();
25586b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
25596b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
25606b4cac81SBjoern A. Zeeb 
25616b4cac81SBjoern A. Zeeb 	if (vap == NULL)
25626b4cac81SBjoern A. Zeeb 		return (0);
25636b4cac81SBjoern A. Zeeb 
25644a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
25654a67f1dfSBjoern A. Zeeb 		return (0);
25664a67f1dfSBjoern A. Zeeb 
25676b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
25686b4cac81SBjoern A. Zeeb 		return (0);
25696b4cac81SBjoern A. Zeeb 
25704a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
25714a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
25724a67f1dfSBjoern A. Zeeb 		return (0);
25734a67f1dfSBjoern A. Zeeb 	}
25744a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
25756b4cac81SBjoern A. Zeeb 
25764a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
25776b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
25786b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
25796b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
25806b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
25816b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
25826b4cac81SBjoern A. Zeeb 
25834a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25844a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25854a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25864a67f1dfSBjoern A. Zeeb 
25876b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
25886b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
25896b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
25906b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
25916b4cac81SBjoern A. Zeeb 
25926b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
25936b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
25946b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
25956b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
25966b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
25976b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
259868541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
25996b4cac81SBjoern A. Zeeb 		if (error != 0)
26003f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
26016b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
26026b4cac81SBjoern A. Zeeb 	}
26036b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
26046b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
26054a67f1dfSBjoern A. Zeeb 	if (!planned)
26066b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
26074a67f1dfSBjoern A. Zeeb 
26084a67f1dfSBjoern A. Zeeb 	return (changed);
26094a67f1dfSBjoern A. Zeeb }
26106b4cac81SBjoern A. Zeeb #endif
26116b4cac81SBjoern A. Zeeb 
26124a67f1dfSBjoern A. Zeeb static int
26134a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
26144a67f1dfSBjoern A. Zeeb {
26154a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26164a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
26174a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
26184a67f1dfSBjoern A. Zeeb 
26194a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
26204a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
26214a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
26226b4cac81SBjoern A. Zeeb 		return (0);
26234a67f1dfSBjoern A. Zeeb 
26244a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
26254a67f1dfSBjoern A. Zeeb 
26264a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
26274a67f1dfSBjoern A. Zeeb #endif
26284a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
26296b4cac81SBjoern A. Zeeb }
26306b4cac81SBjoern A. Zeeb 
26316b4cac81SBjoern A. Zeeb static struct ieee80211vap *
26326b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
26336b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
26346b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
26356b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
26366b4cac81SBjoern A. Zeeb {
26376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26386b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26406b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
26416b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2642a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
26436b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
26446b4cac81SBjoern A. Zeeb 	size_t len;
2645e3a0b120SBjoern A. Zeeb 	int error, i;
2646a6042e17SBjoern A. Zeeb 	uint16_t ac;
26476b4cac81SBjoern A. Zeeb 
26486b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
26496b4cac81SBjoern A. Zeeb 		return (NULL);
26506b4cac81SBjoern A. Zeeb 
26516b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
26526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26536b4cac81SBjoern A. Zeeb 
26546b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
26556b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
26566b4cac81SBjoern A. Zeeb 
26576b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
26586b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
26596b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
26602ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
26612ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26626b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
26636b4cac81SBjoern A. Zeeb 
26646b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26656b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
26666b4cac81SBjoern A. Zeeb 	vif->p2p = false;
26676b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
26686b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
26696b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
26706b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
26716b4cac81SBjoern A. Zeeb 	IMPROVE();
26726b4cac81SBjoern A. Zeeb 
26736b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
26746b4cac81SBjoern A. Zeeb #if 1
26756b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
2676adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
26776ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
26786ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
26796ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
26806b4cac81SBjoern A. Zeeb 	vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
26816b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
26826b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
26836b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
26846b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
26856b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2686616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
2687616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
2688616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
2689616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
26906ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2691caaa79c3SBjoern A. Zeeb 	/*
2692caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2693caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2694caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2695caaa79c3SBjoern A. Zeeb 	 */
26966ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
26976b4cac81SBjoern A. Zeeb #endif
26986b4cac81SBjoern A. Zeeb #if 0
26996b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
27006b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
27016b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
27026b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
27036b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
27046b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
27056b4cac81SBjoern A. Zeeb #endif
2706e3a0b120SBjoern A. Zeeb 
27076ffb7bd4SBjoern A. Zeeb 	/* Link Config */
27086ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
27096ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
27106ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
27116ffb7bd4SBjoern A. Zeeb 	}
27126ffb7bd4SBjoern A. Zeeb 
2713e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2714e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2715e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2716e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2717e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2718e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2719e3a0b120SBjoern A. Zeeb 		else
2720e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
27210cbcfa19SBjoern A. Zeeb 
27220cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
27230cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
2724e3a0b120SBjoern A. Zeeb 	}
2725e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2726e3a0b120SBjoern A. Zeeb 
27276b4cac81SBjoern A. Zeeb 	IMPROVE();
27286b4cac81SBjoern A. Zeeb 
27296b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
27306b4cac81SBjoern A. Zeeb 	if (error != 0) {
27313f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
27326b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
27336b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
27346b4cac81SBjoern A. Zeeb 		return (NULL);
27356b4cac81SBjoern A. Zeeb 	}
27366b4cac81SBjoern A. Zeeb 
27376b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
27386b4cac81SBjoern A. Zeeb 	if (error != 0) {
27396b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
27403f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
27416b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
27426b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
27436b4cac81SBjoern A. Zeeb 		return (NULL);
27446b4cac81SBjoern A. Zeeb 	}
27456b4cac81SBjoern A. Zeeb 
27468891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
27476b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
27488891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
27496b4cac81SBjoern A. Zeeb 
27506b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
27516b4cac81SBjoern A. Zeeb 	changed = 0;
27526b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
27536b4cac81SBjoern A. Zeeb 
2754a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2755a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2756a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2757a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2758a6042e17SBjoern A. Zeeb 
2759a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
2760a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
2761a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
2762a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
2763a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
2764a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2765a6042e17SBjoern A. Zeeb 		if (error != 0)
2766a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2767a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
2768a6042e17SBjoern A. Zeeb 	}
2769a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2770a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
2771a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
27726b4cac81SBjoern A. Zeeb 
27736b4cac81SBjoern A. Zeeb 	/* Force MC init. */
27746b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
27756b4cac81SBjoern A. Zeeb 
27766b4cac81SBjoern A. Zeeb 	IMPROVE();
27776b4cac81SBjoern A. Zeeb 
27786b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
27796b4cac81SBjoern A. Zeeb 
27806b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
27816b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
27826b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2783d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2784d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
27856b4cac81SBjoern A. Zeeb 
27866b4cac81SBjoern A. Zeeb 	/* Key management. */
27876b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2788b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
27896b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
27906b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
27916b4cac81SBjoern A. Zeeb #endif
27926b4cac81SBjoern A. Zeeb 	}
27936b4cac81SBjoern A. Zeeb 
27949fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
27959fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
27969fb91463SBjoern A. Zeeb #endif
27979fb91463SBjoern A. Zeeb 
27986b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
27996b4cac81SBjoern A. Zeeb 
28006b4cac81SBjoern A. Zeeb 	/* Complete setup. */
28016b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
28026b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
28036b4cac81SBjoern A. Zeeb 
28046b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
28056b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
28066b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
28076b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
28086b4cac81SBjoern A. Zeeb 
28096b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
28106b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
28116b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
28126b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
28136b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
28146b4cac81SBjoern A. Zeeb 	/* any others? */
28156b4cac81SBjoern A. Zeeb 	IMPROVE();
28166b4cac81SBjoern A. Zeeb 
28176b4cac81SBjoern A. Zeeb 	return (vap);
28186b4cac81SBjoern A. Zeeb }
28196b4cac81SBjoern A. Zeeb 
28204b0af114SBjoern A. Zeeb void
28214b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
28224b0af114SBjoern A. Zeeb {
28234b0af114SBjoern A. Zeeb 
28244b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
28254b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
28264b0af114SBjoern A. Zeeb 
28274b0af114SBjoern A. Zeeb 	IMPROVE();
28284b0af114SBjoern A. Zeeb }
28294b0af114SBjoern A. Zeeb 
28304b0af114SBjoern A. Zeeb void
28314b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
28324b0af114SBjoern A. Zeeb {
28334b0af114SBjoern A. Zeeb 
28344b0af114SBjoern A. Zeeb 	TODO();
28354b0af114SBjoern A. Zeeb }
28364b0af114SBjoern A. Zeeb 
28376b4cac81SBjoern A. Zeeb static void
28386b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
28396b4cac81SBjoern A. Zeeb {
28406b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
28416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28426b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28436b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28446b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28456b4cac81SBjoern A. Zeeb 
28466b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28476b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28486b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
28496b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28506b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28516b4cac81SBjoern A. Zeeb 
28528891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
28536b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
28548891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
28556b4cac81SBjoern A. Zeeb 
28566b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
28576b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
2858dbf76919SBjoern A. Zeeb 
2859dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
2860dbf76919SBjoern A. Zeeb 
2861dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
2862dbf76919SBjoern A. Zeeb 
28636c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
28646c38c6b1SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw);
28656c38c6b1SBjoern A. Zeeb 
28666b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
28676b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
28686b4cac81SBjoern A. Zeeb }
28696b4cac81SBjoern A. Zeeb 
28706b4cac81SBjoern A. Zeeb static void
28716b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
28726b4cac81SBjoern A. Zeeb {
28736b4cac81SBjoern A. Zeeb 
28746b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
28756b4cac81SBjoern A. Zeeb 	TRACEOK();
28766b4cac81SBjoern A. Zeeb }
28776b4cac81SBjoern A. Zeeb 
28786b4cac81SBjoern A. Zeeb static void
28796b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
28806b4cac81SBjoern A. Zeeb {
28816b4cac81SBjoern A. Zeeb 
28826b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
28836b4cac81SBjoern A. Zeeb }
28846b4cac81SBjoern A. Zeeb 
28856b4cac81SBjoern A. Zeeb static void
28866b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
28876b4cac81SBjoern A. Zeeb {
28886b4cac81SBjoern A. Zeeb 
28896b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
28906b4cac81SBjoern A. Zeeb }
28916b4cac81SBjoern A. Zeeb 
28926b4cac81SBjoern A. Zeeb /* Start / stop device. */
28936b4cac81SBjoern A. Zeeb static void
28946b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
28956b4cac81SBjoern A. Zeeb {
28966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28978d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
28986b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28996b4cac81SBjoern A. Zeeb 	int error;
29008d58a057SBjoern A. Zeeb #endif
29016b4cac81SBjoern A. Zeeb 	bool start_all;
29026b4cac81SBjoern A. Zeeb 
29036b4cac81SBjoern A. Zeeb 	IMPROVE();
29046b4cac81SBjoern A. Zeeb 
29056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
29068d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29088d58a057SBjoern A. Zeeb #endif
29096b4cac81SBjoern A. Zeeb 	start_all = false;
29106b4cac81SBjoern A. Zeeb 
29118ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
29128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
29136b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
29148d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29156b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
29166b4cac81SBjoern A. Zeeb 		if (error == 0)
29178d58a057SBjoern A. Zeeb #endif
29186b4cac81SBjoern A. Zeeb 			start_all = true;
29196b4cac81SBjoern A. Zeeb 	} else {
29208d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
29216b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw);
29228d58a057SBjoern A. Zeeb #endif
29236b4cac81SBjoern A. Zeeb 	}
29248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
29258ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
29266b4cac81SBjoern A. Zeeb 
29276b4cac81SBjoern A. Zeeb 	if (start_all)
29286b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
29296b4cac81SBjoern A. Zeeb }
29306b4cac81SBjoern A. Zeeb 
29316b4cac81SBjoern A. Zeeb bool
29326b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
29336b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
29346b4cac81SBjoern A. Zeeb {
29356b4cac81SBjoern A. Zeeb 	int i;
29366b4cac81SBjoern A. Zeeb 
29376b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
29386b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
29396b4cac81SBjoern A. Zeeb 			return (true);
29406b4cac81SBjoern A. Zeeb 	}
29416b4cac81SBjoern A. Zeeb 
29426b4cac81SBjoern A. Zeeb 	return (false);
29436b4cac81SBjoern A. Zeeb }
29446b4cac81SBjoern A. Zeeb 
29456b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
29466b4cac81SBjoern A. Zeeb bool
29476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
29486b4cac81SBjoern A. Zeeb {
29496b4cac81SBjoern A. Zeeb 	size_t x;
29506b4cac81SBjoern A. Zeeb 	uint8_t l;
29516b4cac81SBjoern A. Zeeb 
29526b4cac81SBjoern A. Zeeb 	x = *xp;
29536b4cac81SBjoern A. Zeeb 
29546b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
29556b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
29566b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
29576b4cac81SBjoern A. Zeeb 	x += 2 + l;
29586b4cac81SBjoern A. Zeeb 
29596b4cac81SBjoern A. Zeeb 	if (x > ies_len)
29606b4cac81SBjoern A. Zeeb 		return (false);
29616b4cac81SBjoern A. Zeeb 
29626b4cac81SBjoern A. Zeeb 	*xp = x;
29636b4cac81SBjoern A. Zeeb 	return (true);
29646b4cac81SBjoern A. Zeeb }
29656b4cac81SBjoern A. Zeeb 
2966d9945d78SBjoern A. Zeeb static uint8_t *
2967d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
2968d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
29696b4cac81SBjoern A. Zeeb {
2970d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
2971d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
29723dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
2973d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
2974d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
2975d9945d78SBjoern A. Zeeb 	uint8_t *pb;
2976d9945d78SBjoern A. Zeeb 	int band, i;
29776b4cac81SBjoern A. Zeeb 
29783dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
2979d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2980d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
2981d9945d78SBjoern A. Zeeb 			continue;
2982d9945d78SBjoern A. Zeeb 
2983d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
2984d9945d78SBjoern A. Zeeb 		/*
2985d9945d78SBjoern A. Zeeb 		 * This should not happen;
2986d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
2987d9945d78SBjoern A. Zeeb 		 */
2988d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
2989d9945d78SBjoern A. Zeeb 			continue;
2990d9945d78SBjoern A. Zeeb 
2991d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
2992d9945d78SBjoern A. Zeeb 		channels = supband->channels;
2993d9945d78SBjoern A. Zeeb 		chan = NULL;
2994d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
2995d9945d78SBjoern A. Zeeb 
2996d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2997d9945d78SBjoern A. Zeeb 				continue;
2998d9945d78SBjoern A. Zeeb 
29993dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3000d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3001d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3002d9945d78SBjoern A. Zeeb 				break;
3003d9945d78SBjoern A. Zeeb 		}
3004d9945d78SBjoern A. Zeeb 
3005d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3006d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3007d9945d78SBjoern A. Zeeb 			continue;
3008d9945d78SBjoern A. Zeeb 
3009d9945d78SBjoern A. Zeeb 		pb = p;
30103dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3011d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3012d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3013d9945d78SBjoern A. Zeeb 
30143dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
30153dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
30163dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
30173dd98026SBjoern A. Zeeb 
30183dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
30193dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
30203dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
30213dd98026SBjoern A. Zeeb 		}
30223dd98026SBjoern A. Zeeb #endif
30233dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
30243dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
30253dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
30263dd98026SBjoern A. Zeeb 
30273dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
30283dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
30293dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
30303dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
30313dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
30323dd98026SBjoern A. Zeeb 		}
30333dd98026SBjoern A. Zeeb #endif
30343dd98026SBjoern A. Zeeb 
3035d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3036d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3037d9945d78SBjoern A. Zeeb 	}
3038d9945d78SBjoern A. Zeeb 
3039d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3040d9945d78SBjoern A. Zeeb 	pb = p;
3041d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3042d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3043d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3044d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3045d9945d78SBjoern A. Zeeb 	}
3046d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3047d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3048d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3049d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3050d9945d78SBjoern A. Zeeb 	}
3051d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3052d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3053d9945d78SBjoern A. Zeeb 
3054d9945d78SBjoern A. Zeeb 	return (p);
30556b4cac81SBjoern A. Zeeb }
30566b4cac81SBjoern A. Zeeb 
30576b4cac81SBjoern A. Zeeb static void
30586b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
30596b4cac81SBjoern A. Zeeb {
30606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30616b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30626b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30636b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30646b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
30656b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
30666b4cac81SBjoern A. Zeeb 	int error;
30678ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
30686b4cac81SBjoern A. Zeeb 
30696b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30708ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3071a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
30726b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
30738ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
30746b4cac81SBjoern A. Zeeb 		return;
30756b4cac81SBjoern A. Zeeb 	}
30768ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
30778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
30786b4cac81SBjoern A. Zeeb 
30796b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
30806b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
30816b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3082d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
30836b4cac81SBjoern A. Zeeb 		return;
30846b4cac81SBjoern A. Zeeb 	}
30856b4cac81SBjoern A. Zeeb 
30866b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30878ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3088a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3089a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3090d3ef7fb4SBjoern A. Zeeb sw_scan:
30916b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
30926b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3093086be6a8SBjoern A. Zeeb 
3094086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3095086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3096086be6a8SBjoern A. Zeeb 
30976b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
30986b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
30996b4cac81SBjoern A. Zeeb 		IMPROVE();
31006b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
31016b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
31026b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
31036b4cac81SBjoern A. Zeeb 
31046b4cac81SBjoern A. Zeeb 	} else {
31056b4cac81SBjoern A. Zeeb 		struct ieee80211_channel *c;
31066b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
31076b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
31086b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
31096b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
31106b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3111d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3112d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3113d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
31143206587aSBjoern A. Zeeb 		bool running;
3115d9945d78SBjoern A. Zeeb 
3116d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3117d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
31186b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
31196b4cac81SBjoern A. Zeeb 
3120d9945d78SBjoern A. Zeeb 		band_mask = 0;
31216b4cac81SBjoern A. Zeeb 		nchan = 0;
3122d9945d78SBjoern A. Zeeb 		for (i = ss->ss_next; i < ss->ss_last; i++) {
31236b4cac81SBjoern A. Zeeb 			nchan++;
3124d9945d78SBjoern A. Zeeb 			band = lkpi_net80211_chan_to_nl80211_band(
3125d9945d78SBjoern A. Zeeb 			    ss->ss_chans[ss->ss_next + i]);
3126d9945d78SBjoern A. Zeeb 			band_mask |= (1 << band);
3127d9945d78SBjoern A. Zeeb 		}
31283206587aSBjoern A. Zeeb 
31293206587aSBjoern A. Zeeb 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
31303206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
31313206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
31323206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
31333206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
31343206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
31353206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
31363206587aSBjoern A. Zeeb 			 */
31373206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
31383206587aSBjoern A. Zeeb 		}
31393206587aSBjoern A. Zeeb 
31406b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
31416b4cac81SBjoern A. Zeeb 
3142d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3143d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3144d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3145d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3146d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3147d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3148d9945d78SBjoern A. Zeeb 
3149d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3150d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3151d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3152d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3153d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3154d9945d78SBjoern A. Zeeb 		}
3155d9945d78SBjoern A. Zeeb 
31563206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3157d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3158d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
31596b4cac81SBjoern A. Zeeb 
31606b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
31616b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
31626b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
31636b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
31646b4cac81SBjoern A. Zeeb #if 0
31656b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
31666b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
31676b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
31686b4cac81SBjoern A. Zeeb #endif
31696b4cac81SBjoern A. Zeeb #ifdef __notyet__
31706b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
31716b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
31726b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
31736b4cac81SBjoern A. Zeeb #endif
3174e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
31756b4cac81SBjoern A. Zeeb 
31766b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
31776b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
31786b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
31796b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
31806b4cac81SBjoern A. Zeeb 			*(cpp + i) =
31816b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
31826b4cac81SBjoern A. Zeeb 		}
31836b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
31846b4cac81SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
31856b4cac81SBjoern A. Zeeb 
31866b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
31873206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
31886b4cac81SBjoern A. Zeeb 			/* lc->flags */
31896b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
31906b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
31916b4cac81SBjoern A. Zeeb 			/* lc-> ... */
31926b4cac81SBjoern A. Zeeb 			lc++;
31936b4cac81SBjoern A. Zeeb 		}
31946b4cac81SBjoern A. Zeeb 
3195d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
31966b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
31976b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
31986b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3199d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
32006b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
32016b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
32026b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
32036b4cac81SBjoern A. Zeeb 				ssids++;
32046b4cac81SBjoern A. Zeeb 			}
32056b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
32066b4cac81SBjoern A. Zeeb 		} else {
32076b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
32086b4cac81SBjoern A. Zeeb 		}
32096b4cac81SBjoern A. Zeeb 
32106b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
32116b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
32126b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
32136b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
32146b4cac81SBjoern A. Zeeb 		/* s6gp->... */
32156b4cac81SBjoern A. Zeeb 
3216d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3217d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3218d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3219d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3220d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3221d9945d78SBjoern A. Zeeb 
32226b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32236b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
32243206587aSBjoern A. Zeeb 
32253206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
32263206587aSBjoern A. Zeeb 		/* Re-check under lock. */
32273206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
32283206587aSBjoern A. Zeeb 		if (!running) {
32293206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
32303206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
32313206587aSBjoern A. Zeeb 
32323206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
32333206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
32343206587aSBjoern A. Zeeb 		}
32353206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32363206587aSBjoern A. Zeeb 		if (running) {
32373206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
32383206587aSBjoern A. Zeeb 			return;
32393206587aSBjoern A. Zeeb 		}
32403206587aSBjoern A. Zeeb 
32416b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
32426b4cac81SBjoern A. Zeeb 		if (error != 0) {
3243d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3244d9945d78SBjoern A. Zeeb 
32453206587aSBjoern A. Zeeb 			/*
32463206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
32473206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
32483206587aSBjoern A. Zeeb 			 * and only there.
32493206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
32503206587aSBjoern A. Zeeb 			 * behave differently:
32513206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
32523206587aSBjoern A. Zeeb 			 *        and can then still return an error.
32533206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
32543206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
32553206587aSBjoern A. Zeeb 			 * ...
32563206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
32573206587aSBjoern A. Zeeb 			 * and balance between both code paths.
32583206587aSBjoern A. Zeeb 			 */
32593206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
32603206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
32613206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
32626b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
32633206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
32643206587aSBjoern A. Zeeb 			}
32653206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3266d3ef7fb4SBjoern A. Zeeb 
3267d3ef7fb4SBjoern A. Zeeb 			/*
3268d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3269d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3270d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3271d3ef7fb4SBjoern A. Zeeb 			 */
3272196cfd0bSBjoern A. Zeeb 			if (error == 1) {
32738ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3274a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
32758ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32763206587aSBjoern A. Zeeb 				/*
32773206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
32783206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
32793206587aSBjoern A. Zeeb 				 * currently not re-enable it?
32803206587aSBjoern A. Zeeb 				 */
32813206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3282196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3283196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3284196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3285196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3286196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3287196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3288196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3289196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3290d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3291196cfd0bSBjoern A. Zeeb 			}
3292d3ef7fb4SBjoern A. Zeeb 
3293d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3294d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
32956b4cac81SBjoern A. Zeeb 		}
32966b4cac81SBjoern A. Zeeb 	}
32976b4cac81SBjoern A. Zeeb }
32986b4cac81SBjoern A. Zeeb 
32996b4cac81SBjoern A. Zeeb static void
33006b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
33016b4cac81SBjoern A. Zeeb {
33026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33038ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
33046b4cac81SBjoern A. Zeeb 
33056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3307a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
33088ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33096b4cac81SBjoern A. Zeeb 		return;
33106b4cac81SBjoern A. Zeeb 	}
33118ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
33128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33136b4cac81SBjoern A. Zeeb 
33148ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3315a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3316a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
33176b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
33186b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
33196b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
33206b4cac81SBjoern A. Zeeb 
3321a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3322a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
33236b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
33246b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33256b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3326a486fbbdSBjoern A. Zeeb 
33276b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
33286b4cac81SBjoern A. Zeeb 
33296b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3330086be6a8SBjoern A. Zeeb 
3331086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3332086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
33336b4cac81SBjoern A. Zeeb 	}
33346b4cac81SBjoern A. Zeeb }
33356b4cac81SBjoern A. Zeeb 
33366b4cac81SBjoern A. Zeeb static void
3337a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3338a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3339a486fbbdSBjoern A. Zeeb {
3340a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33418ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3342a486fbbdSBjoern A. Zeeb 
3343a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
33448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
33458ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
33468ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33478ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3348a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3349a486fbbdSBjoern A. Zeeb }
3350a486fbbdSBjoern A. Zeeb 
3351a486fbbdSBjoern A. Zeeb static void
3352a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3353a486fbbdSBjoern A. Zeeb {
3354a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33558ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3356a486fbbdSBjoern A. Zeeb 
3357a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
33588ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
33598ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
33608ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33618ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3362a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3363a486fbbdSBjoern A. Zeeb }
3364a486fbbdSBjoern A. Zeeb 
3365a486fbbdSBjoern A. Zeeb static void
33666b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
33676b4cac81SBjoern A. Zeeb {
33686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3370b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3371b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
33726b4cac81SBjoern A. Zeeb 	int error;
33738ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3376b2cf3c21SBjoern A. Zeeb 
3377b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3378b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
33796b4cac81SBjoern A. Zeeb 		return;
33806b4cac81SBjoern A. Zeeb 
3381a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
33828ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
33838ac540d3SBjoern A. Zeeb 	hw_scan_running =
33848ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
33858ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
33868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
33878ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3388a486fbbdSBjoern A. Zeeb 		return;
3389a486fbbdSBjoern A. Zeeb 
3390b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3391b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
33926b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
33936b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
33946b4cac81SBjoern A. Zeeb 		return;
33956b4cac81SBjoern A. Zeeb 	}
33966b4cac81SBjoern A. Zeeb 
33976b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
33986b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
33996b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
34006b4cac81SBjoern A. Zeeb 		    c, chan);
34016b4cac81SBjoern A. Zeeb 		return;
34026b4cac81SBjoern A. Zeeb 	}
34036b4cac81SBjoern A. Zeeb 
34046b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
34056b4cac81SBjoern A. Zeeb 	IMPROVE();
34066b4cac81SBjoern A. Zeeb 
34076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34084a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
34099fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
34109fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
34119fb91463SBjoern A. Zeeb #endif
34124a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
34136b4cac81SBjoern A. Zeeb 
34146b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
34156b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
34166b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
34176b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
34186b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
34196b4cac81SBjoern A. Zeeb 		IMPROVE();
34206b4cac81SBjoern A. Zeeb 	} else {
34216b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
34226b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
34236b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
34246b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
34256b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
34266b4cac81SBjoern A. Zeeb 	}
34276b4cac81SBjoern A. Zeeb 
34286b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
34296b4cac81SBjoern A. Zeeb 	IMPROVE();
34306b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
34316b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
34326b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
34336b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
34346b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
34356b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
34366b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
34376b4cac81SBjoern A. Zeeb 			    error);
34386b4cac81SBjoern A. Zeeb 	}
34396b4cac81SBjoern A. Zeeb }
34406b4cac81SBjoern A. Zeeb 
34416b4cac81SBjoern A. Zeeb static struct ieee80211_node *
34426b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
34436b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
34446b4cac81SBjoern A. Zeeb {
34456b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34474f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
34486b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34496b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
34506b4cac81SBjoern A. Zeeb 
34516b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
34526b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34536b4cac81SBjoern A. Zeeb 
34546b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
34554f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
34564f61ef8bSBjoern A. Zeeb 		return (NULL);
34574f61ef8bSBjoern A. Zeeb 
34586b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
34596b4cac81SBjoern A. Zeeb 	if (ni == NULL)
34606b4cac81SBjoern A. Zeeb 		return (NULL);
34616b4cac81SBjoern A. Zeeb 
34626b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34634f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
34646b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
34656b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
34666b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
34676b4cac81SBjoern A. Zeeb 		return (NULL);
34686b4cac81SBjoern A. Zeeb 	}
34696b4cac81SBjoern A. Zeeb 
34706b4cac81SBjoern A. Zeeb 	return (ni);
34716b4cac81SBjoern A. Zeeb }
34726b4cac81SBjoern A. Zeeb 
34736b4cac81SBjoern A. Zeeb static int
34746b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
34756b4cac81SBjoern A. Zeeb {
34766b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34776b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34786b4cac81SBjoern A. Zeeb 	int error;
34796b4cac81SBjoern A. Zeeb 
34806b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
34816b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
34826b4cac81SBjoern A. Zeeb 
34836b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
34846b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
34856b4cac81SBjoern A. Zeeb 		if (error != 0)
34866b4cac81SBjoern A. Zeeb 			return (error);
34876b4cac81SBjoern A. Zeeb 	}
34886b4cac81SBjoern A. Zeeb 
34896b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
34906b4cac81SBjoern A. Zeeb 	IMPROVE();
34916b4cac81SBjoern A. Zeeb 
34926b4cac81SBjoern A. Zeeb 	return (0);
34936b4cac81SBjoern A. Zeeb }
34946b4cac81SBjoern A. Zeeb 
34956b4cac81SBjoern A. Zeeb static void
34966b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
34976b4cac81SBjoern A. Zeeb {
34986b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35006b4cac81SBjoern A. Zeeb 
35016b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35026b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35036b4cac81SBjoern A. Zeeb 
35046b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
35056b4cac81SBjoern A. Zeeb 	IMPROVE();
35066b4cac81SBjoern A. Zeeb 
35076b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
35086b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
35096b4cac81SBjoern A. Zeeb }
35106b4cac81SBjoern A. Zeeb 
35116b4cac81SBjoern A. Zeeb static void
35126b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
35136b4cac81SBjoern A. Zeeb {
35146b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35166b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35176b4cac81SBjoern A. Zeeb 
35186b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
35196b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35206b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
35216b4cac81SBjoern A. Zeeb 
35220936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
35236b4cac81SBjoern A. Zeeb 
35240936c648SBjoern A. Zeeb 	/*
35250936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
35260936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
35270936c648SBjoern A. Zeeb 	 */
35280936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
35296b4cac81SBjoern A. Zeeb 
35306b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
35316b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
35326b4cac81SBjoern A. Zeeb }
35336b4cac81SBjoern A. Zeeb 
35346b4cac81SBjoern A. Zeeb static int
35356b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
35366b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
35376b4cac81SBjoern A. Zeeb {
35386b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
35396b4cac81SBjoern A. Zeeb 
35406b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3541fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
35420936c648SBjoern A. Zeeb 	if (!lsta->txq_ready) {
3543fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3544fa4e4257SBjoern A. Zeeb 		/*
3545fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3546fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
3547fa4e4257SBjoern A. Zeeb 		 */
35480936c648SBjoern A. Zeeb 		m_free(m);
35490936c648SBjoern A. Zeeb 		return (ENETDOWN);
35500936c648SBjoern A. Zeeb 	}
35516b4cac81SBjoern A. Zeeb 
35526b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
35536b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
3554fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3555fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
35566b4cac81SBjoern A. Zeeb 
35579d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35589d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
35596b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
35606b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
35616b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
35629d9ba2b7SBjoern A. Zeeb #endif
35636b4cac81SBjoern A. Zeeb 
35646b4cac81SBjoern A. Zeeb 	return (0);
35656b4cac81SBjoern A. Zeeb }
35666b4cac81SBjoern A. Zeeb 
35676b4cac81SBjoern A. Zeeb static void
35686b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
35696b4cac81SBjoern A. Zeeb {
35706b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3571b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
35726b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3573b35f6cd0SBjoern A. Zeeb #endif
35746b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
35756b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
35766b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35776b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35786b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35796b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35806b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35816b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
35826b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
35836b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
35846b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3585e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
35866b4cac81SBjoern A. Zeeb 	void *buf;
3587e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
35886b4cac81SBjoern A. Zeeb 
35896b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
35906b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35919d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
35926b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
35936b4cac81SBjoern A. Zeeb #endif
35946b4cac81SBjoern A. Zeeb 
35956b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3596b35f6cd0SBjoern A. Zeeb 	k = NULL;
3597b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
35986b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
35996b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
36006b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
36016b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
36026b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
36036b4cac81SBjoern A. Zeeb 		if (k == NULL) {
36046b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
36056b4cac81SBjoern A. Zeeb 			m_freem(m);
36066b4cac81SBjoern A. Zeeb 			return;
36076b4cac81SBjoern A. Zeeb 		}
36086b4cac81SBjoern A. Zeeb 	}
36096b4cac81SBjoern A. Zeeb #endif
36106b4cac81SBjoern A. Zeeb 
36116b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36136b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36146b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
36156b4cac81SBjoern A. Zeeb 
36166b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
36176b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
36186b4cac81SBjoern A. Zeeb 
36196b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
36206b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
36216b4cac81SBjoern A. Zeeb 		if (k != NULL)
36226b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
36236b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
36246b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
36256b4cac81SBjoern A. Zeeb 		IMPROVE();
36266b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
36276b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
36286b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
36296b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
36306b4cac81SBjoern A. Zeeb 		}
36316b4cac81SBjoern A. Zeeb 
36326b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
36336b4cac81SBjoern A. Zeeb 	}
36346b4cac81SBjoern A. Zeeb 
36356b4cac81SBjoern A. Zeeb 	/*
36366b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
36376b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
36383d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
36393d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
36406b4cac81SBjoern A. Zeeb 	 */
36416b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
36426b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
36433f0083c4SBjoern A. Zeeb 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
36446b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
36456b4cac81SBjoern A. Zeeb 		m_freem(m);
36466b4cac81SBjoern A. Zeeb 		return;
36476b4cac81SBjoern A. Zeeb 	}
36486b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
36496b4cac81SBjoern A. Zeeb 
36506b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
36516b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
36526b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
36536b4cac81SBjoern A. Zeeb 	skb->m = m;
36546b4cac81SBjoern A. Zeeb #if 0
36556b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
36566b4cac81SBjoern A. Zeeb #else
36576b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
36586b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
36596b4cac81SBjoern A. Zeeb #endif
36606b4cac81SBjoern A. Zeeb 	/* Save the ni. */
36616b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
36626b4cac81SBjoern A. Zeeb 
36636b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
36646b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36656b4cac81SBjoern A. Zeeb 
3666e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3667e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3668e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3669e3a0b120SBjoern A. Zeeb 		skb->priority = 0;
3670e3a0b120SBjoern A. Zeeb 		ac = IEEE80211_AC_BE;
3671e3a0b120SBjoern A. Zeeb 	} else {
3672e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3673fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
3674e3a0b120SBjoern A. Zeeb 	}
36756b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
36766b4cac81SBjoern A. Zeeb 
36776b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
36786b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
36796b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
36806b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
36816b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
36826b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3683e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
36846b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
36856b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
36866b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
36876b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
36889fb91463SBjoern A. Zeeb #ifdef __notyet__
36899fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
36909fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
36919fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
36929fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
36939fb91463SBjoern A. Zeeb #endif
36949fb91463SBjoern A. Zeeb #endif
36956b4cac81SBjoern A. Zeeb 
36966b4cac81SBjoern A. Zeeb 	lsta = lkpi_find_lsta_by_ni(lvif, ni);
36976b4cac81SBjoern A. Zeeb 	if (lsta != NULL) {
36986b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
3699b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
37006b4cac81SBjoern A. Zeeb 		info->control.hw_key = lsta->kc;
37016b4cac81SBjoern A. Zeeb #endif
37026b4cac81SBjoern A. Zeeb 	} else {
37036b4cac81SBjoern A. Zeeb 		sta = NULL;
37046b4cac81SBjoern A. Zeeb 	}
37056b4cac81SBjoern A. Zeeb 
37066b4cac81SBjoern A. Zeeb 	IMPROVE();
37076b4cac81SBjoern A. Zeeb 
37086b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
37096b4cac81SBjoern A. Zeeb 		struct lkpi_txq *ltxq;
37106b4cac81SBjoern A. Zeeb 
3711e3a0b120SBjoern A. Zeeb 		ltxq = NULL;
3712e3a0b120SBjoern A. Zeeb 		if (!ieee80211_is_data_present(hdr->frame_control)) {
3713e3a0b120SBjoern A. Zeeb 			if (vif->type == NL80211_IFTYPE_STATION &&
3714e3a0b120SBjoern A. Zeeb 			    lsta->added_to_drv &&
3715e3a0b120SBjoern A. Zeeb 			    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3716d0d29110SBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3717e3a0b120SBjoern A. Zeeb 		} else if (lsta->added_to_drv &&
3718e3a0b120SBjoern A. Zeeb 		    sta->txq[skb->priority] != NULL) {
3719e3a0b120SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3720e3a0b120SBjoern A. Zeeb 		}
3721e3a0b120SBjoern A. Zeeb 		if (ltxq == NULL)
3722d0d29110SBjoern A. Zeeb 			goto ops_tx;
3723e3a0b120SBjoern A. Zeeb 
3724d0d29110SBjoern A. Zeeb 		KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3725d0d29110SBjoern A. Zeeb 		    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3726d0d29110SBjoern A. Zeeb 
3727eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
37286b4cac81SBjoern A. Zeeb 		skb_queue_tail(&ltxq->skbq, skb);
37299d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37309d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3731d0d29110SBjoern A. Zeeb 			printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3732d0d29110SBjoern A. Zeeb 			    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3733d0d29110SBjoern A. Zeeb 			    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3734fb6eaf74SBjoern A. Zeeb 			    __func__, __LINE__,
3735d0d29110SBjoern A. Zeeb 			    curthread->td_tid, (unsigned int)ticks,
3736d0d29110SBjoern A. Zeeb 			    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3737d0d29110SBjoern A. Zeeb 			    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3738d0d29110SBjoern A. Zeeb 			    ltxq->txq.tid, ac, skb->priority, skb->qmap);
37399d9ba2b7SBjoern A. Zeeb #endif
3740eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
3741d0d29110SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
37426b4cac81SBjoern A. Zeeb 		return;
37436b4cac81SBjoern A. Zeeb 	}
37446b4cac81SBjoern A. Zeeb 
37456b4cac81SBjoern A. Zeeb ops_tx:
37469d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37479d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3748d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3749d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
37506b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
37516b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
37529d9ba2b7SBjoern A. Zeeb #endif
37536b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
37546b4cac81SBjoern A. Zeeb 	control.sta = sta;
37556b4cac81SBjoern A. Zeeb 
37566b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
37576b4cac81SBjoern A. Zeeb 	return;
37586b4cac81SBjoern A. Zeeb }
37596b4cac81SBjoern A. Zeeb 
37606b4cac81SBjoern A. Zeeb static void
37616b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
37626b4cac81SBjoern A. Zeeb {
37636b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
37646b4cac81SBjoern A. Zeeb 	struct mbufq mq;
37656b4cac81SBjoern A. Zeeb 	struct mbuf *m;
37666b4cac81SBjoern A. Zeeb 
37676b4cac81SBjoern A. Zeeb 	lsta = ctx;
37686b4cac81SBjoern A. Zeeb 
37699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
37716b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
37729d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
37736b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
37749d9ba2b7SBjoern A. Zeeb #endif
37756b4cac81SBjoern A. Zeeb 
37766b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
37776b4cac81SBjoern A. Zeeb 
3778fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3779fa4e4257SBjoern A. Zeeb 	/*
3780fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
3781fa4e4257SBjoern A. Zeeb 	 * disassoc frame still.
3782fa4e4257SBjoern A. Zeeb 	 */
37836b4cac81SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
3784fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
37856b4cac81SBjoern A. Zeeb 
37866b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
37876b4cac81SBjoern A. Zeeb 	while (m != NULL) {
37886b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
37896b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
37906b4cac81SBjoern A. Zeeb 	}
37916b4cac81SBjoern A. Zeeb }
37926b4cac81SBjoern A. Zeeb 
37936b4cac81SBjoern A. Zeeb static int
37946b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
37956b4cac81SBjoern A. Zeeb {
37966b4cac81SBjoern A. Zeeb 
37976b4cac81SBjoern A. Zeeb 	/* XXX TODO */
37986b4cac81SBjoern A. Zeeb 	IMPROVE();
37996b4cac81SBjoern A. Zeeb 
38006b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
38016b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
38026b4cac81SBjoern A. Zeeb 
38036b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
38046b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
38056b4cac81SBjoern A. Zeeb }
38066b4cac81SBjoern A. Zeeb 
38079fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38089fb91463SBjoern A. Zeeb static int
38099fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
38109fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
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_recv_action(ni, wh, frm, efrm));
38219fb91463SBjoern A. Zeeb }
38229fb91463SBjoern A. Zeeb 
38239fb91463SBjoern A. Zeeb static int
38249fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
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 	return (lhw->ic_send_action(ni, category, action, sa));
38359fb91463SBjoern A. Zeeb }
38369fb91463SBjoern A. Zeeb 
38379fb91463SBjoern A. Zeeb 
38389fb91463SBjoern A. Zeeb static int
38399fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38409fb91463SBjoern A. Zeeb {
38419fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38429fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38439fb91463SBjoern A. Zeeb 
38449fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38459fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38469fb91463SBjoern A. Zeeb 
38479fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38489fb91463SBjoern A. Zeeb 
38499fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
38509fb91463SBjoern A. Zeeb }
38519fb91463SBjoern A. Zeeb 
38529fb91463SBjoern A. Zeeb static int
38539fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
38549fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
38559fb91463SBjoern A. Zeeb {
38569fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38579fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38589fb91463SBjoern A. Zeeb 
38599fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38609fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38619fb91463SBjoern A. Zeeb 
38629fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38639fb91463SBjoern A. Zeeb 
38649fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
38659fb91463SBjoern A. Zeeb }
38669fb91463SBjoern A. Zeeb 
38679fb91463SBjoern A. Zeeb static int
38689fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
38699fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
38709fb91463SBjoern A. Zeeb {
38719fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38729fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38739fb91463SBjoern A. Zeeb 
38749fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38759fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38769fb91463SBjoern A. Zeeb 
38779fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38789fb91463SBjoern A. Zeeb 
38799fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
38809fb91463SBjoern A. Zeeb }
38819fb91463SBjoern A. Zeeb 
38829fb91463SBjoern A. Zeeb static void
38839fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38849fb91463SBjoern A. Zeeb {
38859fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
38869fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38879fb91463SBjoern A. Zeeb 
38889fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
38899fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
38909fb91463SBjoern A. Zeeb 
38919fb91463SBjoern A. Zeeb 	IMPROVE_HT();
38929fb91463SBjoern A. Zeeb 
38939fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
38949fb91463SBjoern A. Zeeb }
38959fb91463SBjoern A. Zeeb 
38969fb91463SBjoern A. Zeeb static void
38979fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
38989fb91463SBjoern A. Zeeb {
38999fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39009fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39019fb91463SBjoern A. Zeeb 
39029fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39039fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39049fb91463SBjoern A. Zeeb 
39059fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39069fb91463SBjoern A. Zeeb 
39079fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
39089fb91463SBjoern A. Zeeb }
39099fb91463SBjoern A. Zeeb 
39109fb91463SBjoern A. Zeeb static void
39119fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
39129fb91463SBjoern A. Zeeb     int status)
39139fb91463SBjoern A. Zeeb {
39149fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39159fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39169fb91463SBjoern A. Zeeb 
39179fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39189fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39199fb91463SBjoern A. Zeeb 
39209fb91463SBjoern A. Zeeb 	IMPROVE_HT();
39219fb91463SBjoern A. Zeeb 
39229fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
39239fb91463SBjoern A. Zeeb }
39249fb91463SBjoern A. Zeeb 
39259fb91463SBjoern A. Zeeb static int
39269fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
39279fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
39289fb91463SBjoern A. Zeeb {
39299fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39309fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39319fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39329fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
39339fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39349fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39359fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39369fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
39379fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
39389fb91463SBjoern A. Zeeb 	int error;
39399fb91463SBjoern A. Zeeb 
39409fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39419fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39429fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39439fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
39449fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
39459fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
39469fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
39479fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
39489fb91463SBjoern A. Zeeb 
39499fb91463SBjoern A. Zeeb 	params.sta = sta;
39509fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
39519fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
39529fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
39539fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
39549fb91463SBjoern A. Zeeb 	else
39559fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
39569fb91463SBjoern A. Zeeb 	if (params.buf_size > hw->max_rx_aggregation_subframes)
39579fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
39589fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
39599fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
39609fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
39619fb91463SBjoern A. Zeeb 	params.amsdu = false;
39629fb91463SBjoern A. Zeeb 
39639fb91463SBjoern A. Zeeb 	IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
39649fb91463SBjoern A. Zeeb 
39659fb91463SBjoern A. Zeeb 	/* This may call kalloc.  Make sure we can sleep. */
39669fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
39679fb91463SBjoern A. Zeeb 	if (error != 0) {
39689fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
39699fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
39709fb91463SBjoern A. Zeeb 		return (error);
39719fb91463SBjoern A. Zeeb 	}
39729fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
39739fb91463SBjoern A. Zeeb 
39749fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
39759fb91463SBjoern A. Zeeb 	return (error);
39769fb91463SBjoern A. Zeeb }
39779fb91463SBjoern A. Zeeb 
39789fb91463SBjoern A. Zeeb static void
39799fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
39809fb91463SBjoern A. Zeeb {
39819fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
39829fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39839fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39849fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
39859fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39869fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39879fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39889fb91463SBjoern A. Zeeb         struct ieee80211_sta *sta;
39899fb91463SBjoern A. Zeeb 	struct ieee80211_ampdu_params params;
39909fb91463SBjoern A. Zeeb 	int error;
39919fb91463SBjoern A. Zeeb 	uint8_t tid;
39929fb91463SBjoern A. Zeeb 
39939fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
39949fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
39959fb91463SBjoern A. Zeeb 
39969fb91463SBjoern A. Zeeb 	/*
39979fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
39989fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
39999fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
40009fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
40019fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
40029fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
40039fb91463SBjoern A. Zeeb 	 * track some state itself.
40049fb91463SBjoern A. Zeeb 	 */
40059fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
40069fb91463SBjoern A. Zeeb 		goto net80211_only;
40079fb91463SBjoern A. Zeeb 
40089fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40099fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
40109fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
40119fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
40129fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
40139fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
40149fb91463SBjoern A. Zeeb 
40159fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
40169fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
40179fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
40189fb91463SBjoern A. Zeeb 			break;
40199fb91463SBjoern A. Zeeb 	}
40209fb91463SBjoern A. Zeeb 
40219fb91463SBjoern A. Zeeb 	params.sta = sta;
40229fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
40239fb91463SBjoern A. Zeeb 	params.buf_size = 0;
40249fb91463SBjoern A. Zeeb 	params.timeout = 0;
40259fb91463SBjoern A. Zeeb 	params.ssn = 0;
40269fb91463SBjoern A. Zeeb 	params.tid = tid;
40279fb91463SBjoern A. Zeeb 	params.amsdu = false;
40289fb91463SBjoern A. Zeeb 
40299fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
40309fb91463SBjoern A. Zeeb 	if (error != 0)
40319fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
40329fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
40339fb91463SBjoern A. Zeeb 
40349fb91463SBjoern A. Zeeb net80211_only:
40359fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
40369fb91463SBjoern A. Zeeb }
40379fb91463SBjoern A. Zeeb #endif
40389fb91463SBjoern A. Zeeb 
40399fb91463SBjoern A. Zeeb static void
40409fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
40419fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
40429fb91463SBjoern A. Zeeb {
40439fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
40449fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
40459fb91463SBjoern A. Zeeb 
40469fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
40479fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
40489fb91463SBjoern A. Zeeb 		return;
40499fb91463SBjoern A. Zeeb 
40509fb91463SBjoern A. Zeeb 	switch (band) {
40519fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
40529fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
40539fb91463SBjoern A. Zeeb 		break;
40549fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
40559fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
40569fb91463SBjoern A. Zeeb 		break;
40579fb91463SBjoern A. Zeeb 	default:
40589fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
40599fb91463SBjoern A. Zeeb 		return;
40609fb91463SBjoern A. Zeeb 	}
40619fb91463SBjoern A. Zeeb 
40629fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
40639fb91463SBjoern A. Zeeb 
40649fb91463SBjoern A. Zeeb 	/*
40659fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
40669fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
40679fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
40689fb91463SBjoern A. Zeeb 	 */
40699fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
40709fb91463SBjoern A. Zeeb 
40719fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
40729fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
40739fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
40749fb91463SBjoern A. Zeeb #ifdef __notyet__
40759fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
40769fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
40779fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
40789fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
40799fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
40809fb91463SBjoern A. Zeeb #endif
40819fb91463SBjoern A. Zeeb 
40829fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
40839fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
40849fb91463SBjoern A. Zeeb 
40859fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
40869fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
40879fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
40889fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
40899fb91463SBjoern A. Zeeb #endif
40909fb91463SBjoern A. Zeeb }
40919fb91463SBjoern A. Zeeb 
40926b4cac81SBjoern A. Zeeb static void
40936b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
40946b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
40956b4cac81SBjoern A. Zeeb {
40966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40976b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40986b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
40996b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
41006b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
41016b4cac81SBjoern A. Zeeb 
41026b4cac81SBjoern A. Zeeb 	/* Channels */
41036b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41056b4cac81SBjoern A. Zeeb 
41066b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
41076b4cac81SBjoern A. Zeeb 	nchans = 0;
41086b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
41096b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
41106b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
41116b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
41126b4cac81SBjoern A. Zeeb 		chan_flags = 0;
41136b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
41146b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
41159fb91463SBjoern A. Zeeb 
41169fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
41176b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
41189fb91463SBjoern A. Zeeb 
41199fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
41209fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
41216b4cac81SBjoern A. Zeeb 
41226b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4123cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
41246b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
41256b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
41266b4cac81SBjoern A. Zeeb 
41276b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
41283f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
41293f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
41306b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
41316b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
41326b4cac81SBjoern A. Zeeb 				continue;
41336b4cac81SBjoern A. Zeeb 			}
41346b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
41356b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
41366b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
41376b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
41386b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
41396b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
41406b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
41416b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
41426b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
41436b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
41446b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
41456b4cac81SBjoern A. Zeeb 
41466b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
41476b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
41486b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
41495856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4150cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4151cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
41523f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
41533f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
41546b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
41556b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
41566b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4157cee56e77SBjoern A. Zeeb 			if (error != 0)
41586b4cac81SBjoern A. Zeeb 				break;
41596b4cac81SBjoern A. Zeeb 		}
41606b4cac81SBjoern A. Zeeb 	}
41616b4cac81SBjoern A. Zeeb 
41626b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
41636b4cac81SBjoern A. Zeeb 	nchans = 0;
41646b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
41656b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
41666b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
41676b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
41686b4cac81SBjoern A. Zeeb 		chan_flags = 0;
41696b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
41709fb91463SBjoern A. Zeeb 
41719fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
41729fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
41739fb91463SBjoern A. Zeeb 
41749fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
41756b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
41766b4cac81SBjoern A. Zeeb 
41776b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4178562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
41796b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
41806b4cac81SBjoern A. Zeeb 
41816b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
41826b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
41836b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4184562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
41856b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
41866b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4187562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
41886b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
41896b4cac81SBjoern A. Zeeb 		}
41906b4cac81SBjoern A. Zeeb #endif
41916b4cac81SBjoern A. Zeeb 
41926b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4193cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
41946b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
41956b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
41966b4cac81SBjoern A. Zeeb 
41976b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
41983f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
41993f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
42006b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
42016b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
42026b4cac81SBjoern A. Zeeb 				continue;
42036b4cac81SBjoern A. Zeeb 			}
42046b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
42056b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
42066b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
42076b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
42086b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
42096b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
42106b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
42116b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
42126b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
42136b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
42146b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
42156b4cac81SBjoern A. Zeeb 
42166b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
42176b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
42186b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
42195856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4220cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4221cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
42223f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
42233f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
42246b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
42256b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
42266b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4227cee56e77SBjoern A. Zeeb 			if (error != 0)
42286b4cac81SBjoern A. Zeeb 				break;
42296b4cac81SBjoern A. Zeeb 		}
42306b4cac81SBjoern A. Zeeb 	}
42316b4cac81SBjoern A. Zeeb }
42326b4cac81SBjoern A. Zeeb 
42336b4cac81SBjoern A. Zeeb static void *
42346b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
42356b4cac81SBjoern A. Zeeb {
42366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42376b4cac81SBjoern A. Zeeb 
42386b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
42396b4cac81SBjoern A. Zeeb 	if (ic == NULL)
42406b4cac81SBjoern A. Zeeb 		return (NULL);
42416b4cac81SBjoern A. Zeeb 
42426b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
42436b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
42446b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
42456b4cac81SBjoern A. Zeeb 
42466b4cac81SBjoern A. Zeeb 	return (ic);
42476b4cac81SBjoern A. Zeeb }
42486b4cac81SBjoern A. Zeeb 
42496b4cac81SBjoern A. Zeeb struct ieee80211_hw *
42506b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
42516b4cac81SBjoern A. Zeeb {
42526b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
42536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42546b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4255a5ae63edSBjoern A. Zeeb 	int ac;
42566b4cac81SBjoern A. Zeeb 
42576b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
42586b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
42596b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
42606b4cac81SBjoern A. Zeeb 		return (NULL);
42616b4cac81SBjoern A. Zeeb 
42626b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
42636b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4264652e22d3SBjoern A. Zeeb 
42658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
42668ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4267eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
42688891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
42696b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4270a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4271a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4272a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4273a5ae63edSBjoern A. Zeeb 	}
42746b4cac81SBjoern A. Zeeb 
4275759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
4276759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4277759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4278759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4279759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
4280759a996dSBjoern A. Zeeb 
42816b4cac81SBjoern A. Zeeb 	/*
42826b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
42836b4cac81SBjoern A. Zeeb 	 * not initialized.
42846b4cac81SBjoern A. Zeeb 	 */
42856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42866b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4287086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
42886b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
42896b4cac81SBjoern A. Zeeb 
42906b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
42916b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
42926b4cac81SBjoern A. Zeeb 	if (lhw->ic == NULL) {
42936b4cac81SBjoern A. Zeeb 		ieee80211_free_hw(hw);
42946b4cac81SBjoern A. Zeeb 		return (NULL);
42956b4cac81SBjoern A. Zeeb 	}
42966b4cac81SBjoern A. Zeeb 
42976b4cac81SBjoern A. Zeeb 	IMPROVE();
42986b4cac81SBjoern A. Zeeb 
42996b4cac81SBjoern A. Zeeb 	return (hw);
43006b4cac81SBjoern A. Zeeb }
43016b4cac81SBjoern A. Zeeb 
43026b4cac81SBjoern A. Zeeb void
43036b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
43046b4cac81SBjoern A. Zeeb {
43056b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4306759a996dSBjoern A. Zeeb 	struct mbuf *m;
43076b4cac81SBjoern A. Zeeb 
43086b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
43096b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
43106b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
43116b4cac81SBjoern A. Zeeb 
4312759a996dSBjoern A. Zeeb 	/*
4313759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
4314759a996dSBjoern A. Zeeb 	 */
4315759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4316759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
4317759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4318759a996dSBjoern A. Zeeb 
4319759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4320759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4321759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4322759a996dSBjoern A. Zeeb 
4323759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4324759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
4325759a996dSBjoern A. Zeeb 	while (m != NULL) {
4326759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
4327759a996dSBjoern A. Zeeb 
4328759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4329759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
4330759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
4331759a996dSBjoern A. Zeeb 
4332759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4333759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
4334759a996dSBjoern A. Zeeb 		}
4335759a996dSBjoern A. Zeeb 		m_freem(m);
4336759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
4337759a996dSBjoern A. Zeeb 	}
4338759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4339759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4340759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4341759a996dSBjoern A. Zeeb 
43426b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4343eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
43448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4345eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4346eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
43476b4cac81SBjoern A. Zeeb 	IMPROVE();
43486b4cac81SBjoern A. Zeeb }
43496b4cac81SBjoern A. Zeeb 
43506b4cac81SBjoern A. Zeeb void
43516b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
43526b4cac81SBjoern A. Zeeb {
43536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43546b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43556b4cac81SBjoern A. Zeeb 
43566b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
43576b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
43586b4cac81SBjoern A. Zeeb 
43596b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
43606b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
43616b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
43626b4cac81SBjoern A. Zeeb 
43636b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
43646b4cac81SBjoern A. Zeeb }
43656b4cac81SBjoern A. Zeeb 
43666b4cac81SBjoern A. Zeeb struct ieee80211_hw *
43676b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
43686b4cac81SBjoern A. Zeeb {
43696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43706b4cac81SBjoern A. Zeeb 
43716b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
43726b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
43736b4cac81SBjoern A. Zeeb }
43746b4cac81SBjoern A. Zeeb 
43756b4cac81SBjoern A. Zeeb static void
43766b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
43776b4cac81SBjoern A. Zeeb {
43786b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43796b4cac81SBjoern A. Zeeb 
43806b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
43816b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
43826b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
43836b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
43846b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
43856b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
43866b4cac81SBjoern A. Zeeb }
43876b4cac81SBjoern A. Zeeb 
43882e183d99SBjoern A. Zeeb int
43896b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
43906b4cac81SBjoern A. Zeeb {
43916b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4393c5b96b3eSBjoern A. Zeeb 	int band, i;
43946b4cac81SBjoern A. Zeeb 
43956b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
43966b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
43976b4cac81SBjoern A. Zeeb 
4398652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
4399652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4400652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
4401652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
4402652e22d3SBjoern A. Zeeb 
44036b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
44046b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
44056b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
44066b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
44076b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
44086b4cac81SBjoern A. Zeeb 		/* We take the first one. */
44096b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
44106b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
44116b4cac81SBjoern A. Zeeb 	} else {
44126b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
44136b4cac81SBjoern A. Zeeb 	}
44146b4cac81SBjoern A. Zeeb 
44153d09d310SBjoern A. Zeeb #ifdef __not_yet__
44163d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
44176b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
44183d09d310SBjoern A. Zeeb #endif
44196b4cac81SBjoern A. Zeeb 
44206b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
44216b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
44226b4cac81SBjoern A. Zeeb 
44236b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
44246b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
44256b4cac81SBjoern A. Zeeb 	ic->ic_caps =
44266b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
44276b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
44286b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
44294a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
44306b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
44314a67f1dfSBjoern A. Zeeb #endif
44326b4cac81SBjoern A. Zeeb #if 0
44336b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
44346b4cac81SBjoern A. Zeeb #endif
44356b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
44366b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
44376b4cac81SBjoern A. Zeeb 	    ;
44386b4cac81SBjoern A. Zeeb #if 0
44396b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
44406b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
44416b4cac81SBjoern A. Zeeb #endif
4442cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
4443cc4e78d5SBjoern A. Zeeb 		/*
4444cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
4445cc4e78d5SBjoern A. Zeeb 		 *
4446cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4447cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
4448cc4e78d5SBjoern A. Zeeb 		 * the flag.
4449cc4e78d5SBjoern A. Zeeb 		 */
44506b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4451a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
44526b4cac81SBjoern A. Zeeb 	}
44536b4cac81SBjoern A. Zeeb 
4454527687a9SBjoern A. Zeeb 	/*
4455527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
4456527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
4457527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
4458527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4459527687a9SBjoern A. Zeeb 	 */
4460527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
4461527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
4462527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
4463527687a9SBjoern A. Zeeb 
4464527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4465527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
4466527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
4467527687a9SBjoern A. Zeeb 		}
4468527687a9SBjoern A. Zeeb 	}
4469527687a9SBjoern A. Zeeb 
44706b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
4471b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
44726b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
44736b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
44746b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
44756b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
44766b4cac81SBjoern A. Zeeb 	}
44776b4cac81SBjoern A. Zeeb #endif
44786b4cac81SBjoern A. Zeeb 
44796b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
44806b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
44816b4cac81SBjoern A. Zeeb 
44826b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
44836b4cac81SBjoern A. Zeeb 
44846b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
44856b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
44866b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
44876b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
44886b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
44896b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
44906b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
44916b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
44926b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
44936b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
44946b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
44956b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
44966b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
44976b4cac81SBjoern A. Zeeb 
4498a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4499a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4500a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4501a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4502a486fbbdSBjoern A. Zeeb 
45036b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
45046b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
45056b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
45066b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
45076b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
45086b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
45096b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
45106b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
45116b4cac81SBjoern A. Zeeb 
45129fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45139fb91463SBjoern A. Zeeb 	lhw->ic_recv_action = ic->ic_recv_action;
45149fb91463SBjoern A. Zeeb 	ic->ic_recv_action = lkpi_ic_recv_action;
45159fb91463SBjoern A. Zeeb 	lhw->ic_send_action = ic->ic_send_action;
45169fb91463SBjoern A. Zeeb 	ic->ic_send_action = lkpi_ic_send_action;
45179fb91463SBjoern A. Zeeb 
45189fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
45199fb91463SBjoern A. Zeeb 	ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
45209fb91463SBjoern A. Zeeb 
45219fb91463SBjoern A. Zeeb 	lhw->ic_addba_request = ic->ic_addba_request;
45229fb91463SBjoern A. Zeeb 	ic->ic_addba_request = lkpi_ic_addba_request;
45239fb91463SBjoern A. Zeeb 	lhw->ic_addba_response = ic->ic_addba_response;
45249fb91463SBjoern A. Zeeb 	ic->ic_addba_response = lkpi_ic_addba_response;
45259fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop = ic->ic_addba_stop;
45269fb91463SBjoern A. Zeeb 	ic->ic_addba_stop = lkpi_ic_addba_stop;
45279fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
45289fb91463SBjoern A. Zeeb 	ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
45299fb91463SBjoern A. Zeeb 
45309fb91463SBjoern A. Zeeb 	lhw->ic_bar_response = ic->ic_bar_response;
45319fb91463SBjoern A. Zeeb 	ic->ic_bar_response = lkpi_ic_bar_response;
45329fb91463SBjoern A. Zeeb 
45339fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
45349fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
45359fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
45369fb91463SBjoern A. Zeeb 	ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
45379fb91463SBjoern A. Zeeb #endif
45389fb91463SBjoern A. Zeeb 
45396b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
45406b4cac81SBjoern A. Zeeb 
4541c5b96b3eSBjoern A. Zeeb 	/*
4542c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
4543c5b96b3eSBjoern A. Zeeb 	 * expect one.
4544d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
4545d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4546c5b96b3eSBjoern A. Zeeb 	 */
4547d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
4548f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4549c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
4550c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
4551c5b96b3eSBjoern A. Zeeb 
4552c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4553c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4554c5b96b3eSBjoern A. Zeeb 			continue;
4555c5b96b3eSBjoern A. Zeeb 
4556d9945d78SBjoern A. Zeeb 		lhw->supbands++;
4557d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4558d9945d78SBjoern A. Zeeb 
4559f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
4560f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
4561f454a4a1SBjoern A. Zeeb 			continue;
4562f454a4a1SBjoern A. Zeeb 
4563c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
4564c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4565c5b96b3eSBjoern A. Zeeb 
4566c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4567c5b96b3eSBjoern A. Zeeb 				continue;
4568c5b96b3eSBjoern A. Zeeb 
45694a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
45709fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45719fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
45729fb91463SBjoern A. Zeeb #endif
4573c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
4574c5b96b3eSBjoern A. Zeeb 			break;
4575c5b96b3eSBjoern A. Zeeb 		}
4576c5b96b3eSBjoern A. Zeeb 	}
4577c5b96b3eSBjoern A. Zeeb 
4578d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4579d9945d78SBjoern A. Zeeb 
4580d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
4581d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4582d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4583d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4584d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4585d9945d78SBjoern A. Zeeb 	}
4586d9945d78SBjoern A. Zeeb 
4587d9945d78SBjoern A. Zeeb 	/*
4588d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
4589d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
4590d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
4591d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
4592d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
4593d9945d78SBjoern A. Zeeb 	 */
4594d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4595d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4596d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
459778ca45dfSBjoern A. Zeeb 
459878ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4599d9945d78SBjoern A. Zeeb 		/*
460078ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
460178ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
460278ca45dfSBjoern A. Zeeb 		 * space in.
4603d9945d78SBjoern A. Zeeb 		 */
4604d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
460578ca45dfSBjoern A. Zeeb 	}
4606d9945d78SBjoern A. Zeeb 
46079fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
46089fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
46099fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
46109fb91463SBjoern A. Zeeb #endif
46119fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
46129fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
46139fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
46149fb91463SBjoern A. Zeeb #endif
46159fb91463SBjoern A. Zeeb 
4616d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
461778ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
461878ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
461978ca45dfSBjoern A. Zeeb 			goto err;
4620d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
462178ca45dfSBjoern A. Zeeb 	}
4622d9945d78SBjoern A. Zeeb 
46236b4cac81SBjoern A. Zeeb 	if (bootverbose)
46246b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
46252e183d99SBjoern A. Zeeb 
46262e183d99SBjoern A. Zeeb 	return (0);
462778ca45dfSBjoern A. Zeeb err:
462878ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
462978ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
46306b4cac81SBjoern A. Zeeb }
46316b4cac81SBjoern A. Zeeb 
46326b4cac81SBjoern A. Zeeb void
46336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
46346b4cac81SBjoern A. Zeeb {
46356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46376b4cac81SBjoern A. Zeeb 
46386b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
46396b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
46406b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
46416b4cac81SBjoern A. Zeeb }
46426b4cac81SBjoern A. Zeeb 
46436b4cac81SBjoern A. Zeeb void
46446b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
46456b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
46466b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
46476b4cac81SBjoern A. Zeeb     void *arg)
46486b4cac81SBjoern A. Zeeb {
46496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46506b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
46516b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
465261a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
46536b4cac81SBjoern A. Zeeb 
46546b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
46556b4cac81SBjoern A. Zeeb 
46566b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
46576b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
465861a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4659adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
46606b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
46616b4cac81SBjoern A. Zeeb 		    __func__, flags);
46626b4cac81SBjoern A. Zeeb 	}
46636b4cac81SBjoern A. Zeeb 
4664adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
46656b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
466661a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
46676b4cac81SBjoern A. Zeeb 
46686b4cac81SBjoern A. Zeeb 	if (atomic)
46698891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
46706b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
46716b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
46726b4cac81SBjoern A. Zeeb 
46736b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
46746b4cac81SBjoern A. Zeeb 
46756b4cac81SBjoern A. Zeeb 		/*
46766b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
46776b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
46786b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
46796b4cac81SBjoern A. Zeeb 		 * driver does not know about.
46806b4cac81SBjoern A. Zeeb 		 */
46816b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
46826b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
46836b4cac81SBjoern A. Zeeb 			continue;
46846b4cac81SBjoern A. Zeeb 
46856b4cac81SBjoern A. Zeeb 		/*
468661a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
468761a68e50SBjoern A. Zeeb 		 * if we haven't yet.
468861a68e50SBjoern A. Zeeb 		 */
468961a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
469061a68e50SBjoern A. Zeeb 			continue;
469161a68e50SBjoern A. Zeeb 
469261a68e50SBjoern A. Zeeb 		/*
46936b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
46946b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
46956b4cac81SBjoern A. Zeeb 		 */
46966b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
46976b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
46986b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
46996b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
47006b4cac81SBjoern A. Zeeb 	}
47016b4cac81SBjoern A. Zeeb 	if (atomic)
47028891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
47036b4cac81SBjoern A. Zeeb }
47046b4cac81SBjoern A. Zeeb 
47056b4cac81SBjoern A. Zeeb void
47066b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
47076b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
47086b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
47096b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
47106b4cac81SBjoern A. Zeeb     void *arg)
47116b4cac81SBjoern A. Zeeb {
47126b4cac81SBjoern A. Zeeb 
47136b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
47146b4cac81SBjoern A. Zeeb }
47156b4cac81SBjoern A. Zeeb 
47166b4cac81SBjoern A. Zeeb void
47176b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
47186b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
47196b4cac81SBjoern A. Zeeb 	void *),
47206b4cac81SBjoern A. Zeeb     void *arg)
47216b4cac81SBjoern A. Zeeb {
4722c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4723c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4724c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4725c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
47266b4cac81SBjoern A. Zeeb 
4727c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
4728c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4729c5e25798SBjoern A. Zeeb 
4730c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
4731c5e25798SBjoern A. Zeeb 
4732c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
4733c5e25798SBjoern A. Zeeb 
4734c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
4735c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4736c5e25798SBjoern A. Zeeb 
4737c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4738c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
4739c5e25798SBjoern A. Zeeb 			continue;
4740c5e25798SBjoern A. Zeeb 
4741c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4742c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
4743c5e25798SBjoern A. Zeeb 			continue;
4744c5e25798SBjoern A. Zeeb 
4745c5e25798SBjoern A. Zeeb 		iterfunc(hw, &lchanctx->conf, arg);
4746c5e25798SBjoern A. Zeeb 	}
4747c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
47486b4cac81SBjoern A. Zeeb }
47496b4cac81SBjoern A. Zeeb 
47506b4cac81SBjoern A. Zeeb void
47516b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
47526b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
47536b4cac81SBjoern A. Zeeb {
47546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47556b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
47566b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
47576b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
47586b4cac81SBjoern A. Zeeb 
47596b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
47606b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
47616b4cac81SBjoern A. Zeeb 
47626b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
47636b4cac81SBjoern A. Zeeb 
47648891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
47656b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
47666b4cac81SBjoern A. Zeeb 
47676b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
47686b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
47696b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
47706b4cac81SBjoern A. Zeeb 				continue;
47716b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
47726b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
47736b4cac81SBjoern A. Zeeb 		}
47746b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
47756b4cac81SBjoern A. Zeeb 	}
47768891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
47776b4cac81SBjoern A. Zeeb }
47786b4cac81SBjoern A. Zeeb 
4779673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
4780673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4781673d62fcSBjoern A. Zeeb {
4782673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
4783673d62fcSBjoern A. Zeeb 
4784673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4785673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
4786673d62fcSBjoern A. Zeeb 	return (regd);
4787673d62fcSBjoern A. Zeeb }
4788673d62fcSBjoern A. Zeeb 
47896b4cac81SBjoern A. Zeeb int
47906b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
47916b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
47926b4cac81SBjoern A. Zeeb {
47936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47946b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47956b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
47966b4cac81SBjoern A. Zeeb 
47976b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
47986b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
47996b4cac81SBjoern A. Zeeb 
48006b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
48016b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
48026b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
48036b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
48046b4cac81SBjoern A. Zeeb 	}
48056b4cac81SBjoern A. Zeeb 
48066b4cac81SBjoern A. Zeeb 	TODO();
48076b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
48086b4cac81SBjoern A. Zeeb 
48096b4cac81SBjoern A. Zeeb 	return (0);
48106b4cac81SBjoern A. Zeeb }
48116b4cac81SBjoern A. Zeeb 
48126b4cac81SBjoern A. Zeeb void
48136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
48146b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
48156b4cac81SBjoern A. Zeeb {
48166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48176b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48186b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
48196b4cac81SBjoern A. Zeeb 
48206b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
48216b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48226b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
48236b4cac81SBjoern A. Zeeb 
48246b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
48256b4cac81SBjoern A. Zeeb 
48268ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
48276b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
48286b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
4829a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
48306b4cac81SBjoern A. Zeeb 	wakeup(lhw);
48318ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
48326b4cac81SBjoern A. Zeeb 
48336b4cac81SBjoern A. Zeeb 	return;
48346b4cac81SBjoern A. Zeeb }
48356b4cac81SBjoern A. Zeeb 
4836759a996dSBjoern A. Zeeb static void
4837759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
4838759a996dSBjoern A. Zeeb {
4839759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
4840759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
4841759a996dSBjoern A. Zeeb 	int ok;
4842759a996dSBjoern A. Zeeb 
4843759a996dSBjoern A. Zeeb 	ni = NULL;
4844759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4845759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
4846759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
4847759a996dSBjoern A. Zeeb 
4848759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4849759a996dSBjoern A. Zeeb 		ni = rxni->ni;
4850759a996dSBjoern A. Zeeb 	}
4851759a996dSBjoern A. Zeeb 
4852759a996dSBjoern A. Zeeb 	if (ni != NULL) {
4853759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
4854759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
4855759a996dSBjoern A. Zeeb 		if (ok < 0)
4856759a996dSBjoern A. Zeeb 			m_freem(m);
4857759a996dSBjoern A. Zeeb 	} else {
4858759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
4859759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
4860759a996dSBjoern A. Zeeb 	}
4861759a996dSBjoern A. Zeeb 
4862759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4863759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4864759a996dSBjoern A. Zeeb 		printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
4865759a996dSBjoern A. Zeeb #endif
4866759a996dSBjoern A. Zeeb }
4867759a996dSBjoern A. Zeeb 
4868759a996dSBjoern A. Zeeb static void
4869759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
4870759a996dSBjoern A. Zeeb {
4871759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
4872759a996dSBjoern A. Zeeb 	struct mbufq mq;
4873759a996dSBjoern A. Zeeb 	struct mbuf *m;
4874759a996dSBjoern A. Zeeb 
4875759a996dSBjoern A. Zeeb 	lhw = ctx;
4876759a996dSBjoern A. Zeeb 
4877759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4878759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4879759a996dSBjoern A. Zeeb 		printf("%s:%d lhw %p pending %d mbuf_qlen %d\n",
4880759a996dSBjoern A. Zeeb 		    __func__, __LINE__, lhw, pending, mbufq_len(&lhw->rxq));
4881759a996dSBjoern A. Zeeb #endif
4882759a996dSBjoern A. Zeeb 
4883759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
4884759a996dSBjoern A. Zeeb 
4885759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4886759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
4887759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4888759a996dSBjoern A. Zeeb 
4889759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
4890759a996dSBjoern A. Zeeb 	while (m != NULL) {
4891759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
4892759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
4893759a996dSBjoern A. Zeeb 	}
4894759a996dSBjoern A. Zeeb }
4895759a996dSBjoern A. Zeeb 
4896e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
48976b4cac81SBjoern A. Zeeb void
48986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4899e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
4900e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
49016b4cac81SBjoern A. Zeeb {
49026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49036b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49046b4cac81SBjoern A. Zeeb 	struct mbuf *m;
49056b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
49066b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
49076b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
49086b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
49096b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
49106b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
49116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49129d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
4913170acccfSBjoern A. Zeeb 	int8_t rssi;
4914c0cadd99SBjoern A. Zeeb 	bool is_beacon;
49156b4cac81SBjoern A. Zeeb 
49166b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
49176b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
49186b4cac81SBjoern A. Zeeb 		IMPROVE();
49196b4cac81SBjoern A. Zeeb 		goto err;
49206b4cac81SBjoern A. Zeeb 	}
49216b4cac81SBjoern A. Zeeb 
49226b4cac81SBjoern A. Zeeb 	/*
49236b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
49246b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
49256b4cac81SBjoern A. Zeeb 	 */
49266b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
49276b4cac81SBjoern A. Zeeb 	if (m == NULL)
49286b4cac81SBjoern A. Zeeb 		goto err;
49296b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
49306b4cac81SBjoern A. Zeeb 
49316b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
49326b4cac81SBjoern A. Zeeb 	offset = m->m_len;
49336b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
49346b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
49356b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
49366b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
49376b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
49386b4cac81SBjoern A. Zeeb 	}
49396b4cac81SBjoern A. Zeeb 
49406b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
49416b4cac81SBjoern A. Zeeb 
49426b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
4943c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
4944c0cadd99SBjoern A. Zeeb 
4945c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49469d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
49476b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
49486b4cac81SBjoern A. Zeeb 
49499d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
49506b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
4951c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
49526b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
49536b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
49546b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
4955c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
49566b4cac81SBjoern A. Zeeb 
49579d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
49586b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
49596b4cac81SBjoern A. Zeeb 
49606b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
49619d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
4962c8dafefaSBjoern A. Zeeb 		printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
496360970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
49646b4cac81SBjoern A. Zeeb 			__func__,
4965c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
4966c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
49676b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
49686b4cac81SBjoern A. Zeeb 			rx_status->flag,
49696b4cac81SBjoern A. Zeeb 			rx_status->freq,
49706b4cac81SBjoern A. Zeeb 			rx_status->bw,
49716b4cac81SBjoern A. Zeeb 			rx_status->encoding,
49726b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
49736b4cac81SBjoern A. Zeeb 			rx_status->band,
49746b4cac81SBjoern A. Zeeb 			rx_status->chains,
49756b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
49766b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
49776b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
497860970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
49796b4cac81SBjoern A. Zeeb 			rx_status->signal,
49806b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
49816b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
49826b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
49836b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
49846b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
49856b4cac81SBjoern A. Zeeb 			rx_status->nss,
49866b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
49876b4cac81SBjoern A. Zeeb no_trace_beacons:
49886b4cac81SBjoern A. Zeeb #endif
49896b4cac81SBjoern A. Zeeb 
49906b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
49916b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
499260970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
499360970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
49946b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
49956b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4996170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
49976b4cac81SBjoern A. Zeeb 	else
4998170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
4999170acccfSBjoern A. Zeeb 	/*
5000170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
5001170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
5002170acccfSBjoern A. Zeeb 	 */
5003170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
5004170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
50056b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
50066b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
50076b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
50086b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
50096b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
50106b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
50116b4cac81SBjoern A. Zeeb 
50126b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
50136b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
50146b4cac81SBjoern A. Zeeb 
50156b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
50166b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
50176b4cac81SBjoern A. Zeeb 
50186b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
50196b4cac81SBjoern A. Zeeb 	if (ok == 0) {
5020dbc06dd9SBjoern A. Zeeb 		m_freem(m);
50216b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
50226b4cac81SBjoern A. Zeeb 		goto err;
50236b4cac81SBjoern A. Zeeb 	}
50246b4cac81SBjoern A. Zeeb 
50256b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
50266b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
50276b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
50286b4cac81SBjoern A. Zeeb 	} else {
5029c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
5030c8dafefaSBjoern A. Zeeb 
50316b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
50326b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
50336b4cac81SBjoern A. Zeeb 		if (ni != NULL)
50346b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
50356b4cac81SBjoern A. Zeeb 	}
50366b4cac81SBjoern A. Zeeb 
50376b4cac81SBjoern A. Zeeb 	if (ni != NULL)
50386b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
50396b4cac81SBjoern A. Zeeb 	else
50406b4cac81SBjoern A. Zeeb 		/*
50416b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
50426b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
50436b4cac81SBjoern A. Zeeb 		 */
50446b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
50456b4cac81SBjoern A. Zeeb 
50469d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50479d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5048c0cadd99SBjoern A. Zeeb 		printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5049c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5050c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
50519d9ba2b7SBjoern A. Zeeb #endif
50526b4cac81SBjoern A. Zeeb 
5053c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
5054c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
5055c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5056c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
5057c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
5058c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
5059c8dafefaSBjoern A. Zeeb 
5060c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
5061c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5062c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
5063c8dafefaSBjoern A. Zeeb 
5064c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
5065c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5066c8dafefaSBjoern A. Zeeb 
5067c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
5068c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
5069c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5070c8dafefaSBjoern A. Zeeb 		/*
5071c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
5072c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5073c8dafefaSBjoern A. Zeeb 		 */
5074c8dafefaSBjoern A. Zeeb skip_device_ts:
50759fb91463SBjoern A. Zeeb 		;
50769fb91463SBjoern A. Zeeb 	}
5077c8dafefaSBjoern A. Zeeb 
50786b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
50796b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
50806b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
50816b4cac81SBjoern A. Zeeb 
50826b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
50836b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
50846b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
50856b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
50866b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
50876b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
50886b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
50896b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
50906b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
50916b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
50926b4cac81SBjoern A. Zeeb #endif
50936b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
50946b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
50956b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
50966b4cac81SBjoern A. Zeeb 		IMPROVE();
50976b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
50986b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
50996b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
51006b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5101170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
51026b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
51036b4cac81SBjoern A. Zeeb 	}
51046b4cac81SBjoern A. Zeeb 
51056b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
51066b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
51076b4cac81SBjoern A. Zeeb 
5108e30e05d3SBjoern A. Zeeb #if 0
5109e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
5110e30e05d3SBjoern A. Zeeb 		/*
5111e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
5112e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5113e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
5114e30e05d3SBjoern A. Zeeb 		*/
5115e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5116e30e05d3SBjoern A. Zeeb 	}
5117e30e05d3SBjoern A. Zeeb #endif
5118e30e05d3SBjoern A. Zeeb 
5119759a996dSBjoern A. Zeeb 	/*
5120759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
5121759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
5122759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
5123759a996dSBjoern A. Zeeb 	 */
51246b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
5125759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5126759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5127759a996dSBjoern A. Zeeb 
5128759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5129759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5130759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5131759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5132759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
5133759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
5134759a996dSBjoern A. Zeeb 		}
51356b4cac81SBjoern A. Zeeb 	}
51366b4cac81SBjoern A. Zeeb 
5137759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5138759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
5139759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5140759a996dSBjoern A. Zeeb 		m_freem(m);
5141759a996dSBjoern A. Zeeb 		goto err;
5142759a996dSBjoern A. Zeeb 	}
5143759a996dSBjoern A. Zeeb 
5144759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
5145759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5146759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
51476b4cac81SBjoern A. Zeeb 
51486b4cac81SBjoern A. Zeeb 	IMPROVE();
51496b4cac81SBjoern A. Zeeb 
51506b4cac81SBjoern A. Zeeb err:
51516b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
51526b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
51536b4cac81SBjoern A. Zeeb }
51546b4cac81SBjoern A. Zeeb 
51556b4cac81SBjoern A. Zeeb uint8_t
5156ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
51576b4cac81SBjoern A. Zeeb {
51586b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
5159ec190d91SBjoern A. Zeeb 	uint8_t tid;
5160ec190d91SBjoern A. Zeeb 
5161ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
5162ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5163ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5164ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
51656b4cac81SBjoern A. Zeeb 
51666b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
5167ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
5168ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5169ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5170ec190d91SBjoern A. Zeeb 
5171ec190d91SBjoern A. Zeeb 	return (tid);
51726b4cac81SBjoern A. Zeeb }
51736b4cac81SBjoern A. Zeeb 
51746b4cac81SBjoern A. Zeeb struct wiphy *
51756b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
51766b4cac81SBjoern A. Zeeb {
51776b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
51786b4cac81SBjoern A. Zeeb 
51796b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
51806b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
51816b4cac81SBjoern A. Zeeb 		return (NULL);
51826b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
51836b4cac81SBjoern A. Zeeb 
51846b4cac81SBjoern A. Zeeb 	/* XXX TODO */
51856b4cac81SBjoern A. Zeeb 	return (LWIPHY_TO_WIPHY(lwiphy));
51866b4cac81SBjoern A. Zeeb }
51876b4cac81SBjoern A. Zeeb 
51886b4cac81SBjoern A. Zeeb void
51896b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
51906b4cac81SBjoern A. Zeeb {
51916b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
51926b4cac81SBjoern A. Zeeb 
51936b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
51946b4cac81SBjoern A. Zeeb 		return;
51956b4cac81SBjoern A. Zeeb 
51966b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
51976b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
51986b4cac81SBjoern A. Zeeb }
51996b4cac81SBjoern A. Zeeb 
52006b4cac81SBjoern A. Zeeb uint32_t
52016b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
52026b4cac81SBjoern A. Zeeb     enum nl80211_band band)
52036b4cac81SBjoern A. Zeeb {
52046b4cac81SBjoern A. Zeeb 
52056b4cac81SBjoern A. Zeeb 	switch (band) {
52066b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
52076b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
52086b4cac81SBjoern A. Zeeb 		break;
52096b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
52106b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
52116b4cac81SBjoern A. Zeeb 		break;
52126b4cac81SBjoern A. Zeeb 	default:
52136b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
52146b4cac81SBjoern A. Zeeb 		break;
52156b4cac81SBjoern A. Zeeb 	}
52166b4cac81SBjoern A. Zeeb 
52176b4cac81SBjoern A. Zeeb 	return (0);
52186b4cac81SBjoern A. Zeeb }
52196b4cac81SBjoern A. Zeeb 
52206b4cac81SBjoern A. Zeeb uint32_t
52216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
52226b4cac81SBjoern A. Zeeb {
52236b4cac81SBjoern A. Zeeb 
52246b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
52256b4cac81SBjoern A. Zeeb }
52266b4cac81SBjoern A. Zeeb 
52276b4cac81SBjoern A. Zeeb static struct lkpi_sta *
52286b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
52296b4cac81SBjoern A. Zeeb {
52306b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
52316b4cac81SBjoern A. Zeeb 
52326b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
52336b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
52346b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
52356b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
52366b4cac81SBjoern A. Zeeb 			return (lsta);
52376b4cac81SBjoern A. Zeeb 		}
52386b4cac81SBjoern A. Zeeb 	}
52396b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
52406b4cac81SBjoern A. Zeeb 
52416b4cac81SBjoern A. Zeeb 	return (NULL);
52426b4cac81SBjoern A. Zeeb }
52436b4cac81SBjoern A. Zeeb 
52446b4cac81SBjoern A. Zeeb struct ieee80211_sta *
52456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
52466b4cac81SBjoern A. Zeeb {
52476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
52486b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
52496b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
52506b4cac81SBjoern A. Zeeb 
52516b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
52526b4cac81SBjoern A. Zeeb 
52536b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
52546b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
52556b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
52566b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
52576b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
52586b4cac81SBjoern A. Zeeb 			return (sta);
52596b4cac81SBjoern A. Zeeb 		}
52606b4cac81SBjoern A. Zeeb 	}
52616b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
52626b4cac81SBjoern A. Zeeb 	return (NULL);
52636b4cac81SBjoern A. Zeeb }
52646b4cac81SBjoern A. Zeeb 
52656b4cac81SBjoern A. Zeeb struct ieee80211_sta *
52662e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
52672e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
52686b4cac81SBjoern A. Zeeb {
52696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
52716b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
52726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
52736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
52746b4cac81SBjoern A. Zeeb 
52756b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
52766b4cac81SBjoern A. Zeeb 	sta = NULL;
52776b4cac81SBjoern A. Zeeb 
52788891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
52796b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
52806b4cac81SBjoern A. Zeeb 
52816b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
52826b4cac81SBjoern A. Zeeb 
52836b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
52846b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
52856b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
52866b4cac81SBjoern A. Zeeb 			continue;
52876b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
52886b4cac81SBjoern A. Zeeb 		if (sta != NULL)
52896b4cac81SBjoern A. Zeeb 			break;
52906b4cac81SBjoern A. Zeeb 	}
52918891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
52926b4cac81SBjoern A. Zeeb 
52936b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
52946b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
52956b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
52966b4cac81SBjoern A. Zeeb 			return (NULL);
52976b4cac81SBjoern A. Zeeb 	}
52986b4cac81SBjoern A. Zeeb 
52996b4cac81SBjoern A. Zeeb 	return (sta);
53006b4cac81SBjoern A. Zeeb }
53016b4cac81SBjoern A. Zeeb 
53026b4cac81SBjoern A. Zeeb struct sk_buff *
53036b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
53046b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
53056b4cac81SBjoern A. Zeeb {
53066b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
53070cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53086b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
53096b4cac81SBjoern A. Zeeb 
53100cbcfa19SBjoern A. Zeeb 	skb = NULL;
53116b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
53126b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
53136b4cac81SBjoern A. Zeeb 
53140cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
53150cbcfa19SBjoern A. Zeeb 		goto stopped;
53160cbcfa19SBjoern A. Zeeb 
53170cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
53180cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
53190cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
53200cbcfa19SBjoern A. Zeeb 		goto stopped;
53210cbcfa19SBjoern A. Zeeb 	}
53220cbcfa19SBjoern A. Zeeb 
5323eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
5324eac3646fSBjoern A. Zeeb 
5325eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
53266b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
5327eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
53286b4cac81SBjoern A. Zeeb 
53290cbcfa19SBjoern A. Zeeb stopped:
53306b4cac81SBjoern A. Zeeb 	return (skb);
53316b4cac81SBjoern A. Zeeb }
53326b4cac81SBjoern A. Zeeb 
53336b4cac81SBjoern A. Zeeb void
53346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
533586220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
53366b4cac81SBjoern A. Zeeb {
53376b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
53386b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
533986220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
53406b4cac81SBjoern A. Zeeb 
53416b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
53426b4cac81SBjoern A. Zeeb 
53436b4cac81SBjoern A. Zeeb 	fc = bc = 0;
5344eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
53456b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
53466b4cac81SBjoern A. Zeeb 		fc++;
53476b4cac81SBjoern A. Zeeb 		bc += skb->len;
53486b4cac81SBjoern A. Zeeb 	}
5349eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
53506b4cac81SBjoern A. Zeeb 	if (frame_cnt)
53516b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
53526b4cac81SBjoern A. Zeeb 	if (byte_cnt)
53536b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
53546b4cac81SBjoern A. Zeeb 
53556b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
53566b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
53576b4cac81SBjoern A. Zeeb 	IMPROVE();
53586b4cac81SBjoern A. Zeeb }
53596b4cac81SBjoern A. Zeeb 
53606b4cac81SBjoern A. Zeeb /*
53616b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
53626b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
53636b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
53646b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
53656b4cac81SBjoern A. Zeeb  */
5366a8397571SBjoern A. Zeeb static void
5367a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
53686b4cac81SBjoern A. Zeeb     int status)
53696b4cac81SBjoern A. Zeeb {
53706b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
53716b4cac81SBjoern A. Zeeb 	struct mbuf *m;
53726b4cac81SBjoern A. Zeeb 
53736b4cac81SBjoern A. Zeeb 	m = skb->m;
53746b4cac81SBjoern A. Zeeb 	skb->m = NULL;
53756b4cac81SBjoern A. Zeeb 
53766b4cac81SBjoern A. Zeeb 	if (m != NULL) {
53776b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
53786b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
53796b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
53806b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
53816b4cac81SBjoern A. Zeeb 	}
5382a8397571SBjoern A. Zeeb }
53836b4cac81SBjoern A. Zeeb 
5384a8397571SBjoern A. Zeeb void
5385a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5386a8397571SBjoern A. Zeeb     int status)
5387a8397571SBjoern A. Zeeb {
5388a8397571SBjoern A. Zeeb 
5389a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
53906b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
53916b4cac81SBjoern A. Zeeb }
53926b4cac81SBjoern A. Zeeb 
5393383b3e8fSBjoern A. Zeeb void
5394a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5395a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
5396383b3e8fSBjoern A. Zeeb {
5397a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
5398383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
5399383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
5400383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
5401383b3e8fSBjoern A. Zeeb 	int status;
5402383b3e8fSBjoern A. Zeeb 
5403a8397571SBjoern A. Zeeb 	skb = txstat->skb;
5404383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
5405383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
5406383b3e8fSBjoern A. Zeeb 
5407383b3e8fSBjoern A. Zeeb 		m = skb->m;
5408383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
5409383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
5410383b3e8fSBjoern A. Zeeb 	} else {
5411383b3e8fSBjoern A. Zeeb 		ni = NULL;
5412383b3e8fSBjoern A. Zeeb 	}
5413383b3e8fSBjoern A. Zeeb 
5414a8397571SBjoern A. Zeeb 	info = txstat->info;
5415383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5416383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
5417383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5418383b3e8fSBjoern A. Zeeb 	} else {
5419383b3e8fSBjoern A. Zeeb 		status = 1;
5420383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5421383b3e8fSBjoern A. Zeeb 	}
5422383b3e8fSBjoern A. Zeeb 
5423383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
5424e2c5ab09SJohn Baldwin 		int ridx __unused;
5425383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5426383b3e8fSBjoern A. Zeeb 		int old_rate;
5427383b3e8fSBjoern A. Zeeb 
5428383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5429383b3e8fSBjoern A. Zeeb #endif
5430383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
5431383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5432383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
5433383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5434383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5435383b3e8fSBjoern A. Zeeb 		}
5436383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
5437a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5438383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
5439383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5440383b3e8fSBjoern A. Zeeb #endif
5441adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5442383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5443383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5444383b3e8fSBjoern A. Zeeb 		}
5445383b3e8fSBjoern A. Zeeb 
5446383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5447383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
5448383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5449383b3e8fSBjoern A. Zeeb 
5450383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5451383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5452383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5453383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
5454383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5455383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
5456383b3e8fSBjoern A. Zeeb 		}
5457383b3e8fSBjoern A. Zeeb #endif
5458383b3e8fSBjoern A. Zeeb 	}
5459383b3e8fSBjoern A. Zeeb 
5460383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5461383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5462383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5463383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
5464383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5465383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5466adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
5467383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
5468383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
5469383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
5470383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
5471383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
5472383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
5473383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
5474383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
5475383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
5476383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
5477383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
5478383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
5479383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
5480adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
5481383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
5482383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
5483383b3e8fSBjoern A. Zeeb #endif
5484383b3e8fSBjoern A. Zeeb 
5485a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
5486a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5487a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
5488a8397571SBjoern A. Zeeb 	} else {
5489383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5490383b3e8fSBjoern A. Zeeb 	}
5491a8397571SBjoern A. Zeeb }
5492a8397571SBjoern A. Zeeb 
5493a8397571SBjoern A. Zeeb void
5494a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5495a8397571SBjoern A. Zeeb {
5496a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
5497a8397571SBjoern A. Zeeb 
5498a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
5499a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
5500a8397571SBjoern A. Zeeb 	status.skb = skb;
5501a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
5502a8397571SBjoern A. Zeeb 
5503a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
5504a8397571SBjoern A. Zeeb }
5505383b3e8fSBjoern A. Zeeb 
55066b4cac81SBjoern A. Zeeb /*
55076b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
55086b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
55096b4cac81SBjoern A. Zeeb  * mbufs this should go away.
55106b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
55116b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
55126b4cac81SBjoern A. Zeeb  */
55136b4cac81SBjoern A. Zeeb static void
55146b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
55156b4cac81SBjoern A. Zeeb {
55166b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
55176b4cac81SBjoern A. Zeeb 	struct mbuf *m;
55186b4cac81SBjoern A. Zeeb 
55196b4cac81SBjoern A. Zeeb 	if (p == NULL)
55206b4cac81SBjoern A. Zeeb 		return;
55216b4cac81SBjoern A. Zeeb 
55226b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
55236b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
55246b4cac81SBjoern A. Zeeb 
55256b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
55266b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
55276b4cac81SBjoern A. Zeeb 	if (ni != NULL)
55286b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
55296b4cac81SBjoern A. Zeeb 	m_freem(m);
55306b4cac81SBjoern A. Zeeb }
55316b4cac81SBjoern A. Zeeb 
55326b4cac81SBjoern A. Zeeb void
55336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
55346b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
55356b4cac81SBjoern A. Zeeb {
55366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55376b4cac81SBjoern A. Zeeb 
55386b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
55396b4cac81SBjoern A. Zeeb 	IMPROVE();
55406b4cac81SBjoern A. Zeeb 
55416b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55426b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
55436b4cac81SBjoern A. Zeeb }
55446b4cac81SBjoern A. Zeeb 
55456b4cac81SBjoern A. Zeeb void
55466b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
55476b4cac81SBjoern A. Zeeb     struct work_struct *w)
55486b4cac81SBjoern A. Zeeb {
55496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55506b4cac81SBjoern A. Zeeb 
55516b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
55526b4cac81SBjoern A. Zeeb 	IMPROVE();
55536b4cac81SBjoern A. Zeeb 
55546b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
55556b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
55566b4cac81SBjoern A. Zeeb }
55576b4cac81SBjoern A. Zeeb 
55586b4cac81SBjoern A. Zeeb struct sk_buff *
5559ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5560ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
5561ade774b1SBjoern A. Zeeb {
5562ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
5563ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
5564ade774b1SBjoern A. Zeeb 	uint8_t *p;
5565ade774b1SBjoern A. Zeeb 	size_t len;
5566ade774b1SBjoern A. Zeeb 
5567ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
5568ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
5569ade774b1SBjoern A. Zeeb 
5570ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5571ade774b1SBjoern A. Zeeb 	if (skb == NULL)
5572ade774b1SBjoern A. Zeeb 		return (NULL);
5573ade774b1SBjoern A. Zeeb 
5574ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
5575ade774b1SBjoern A. Zeeb 
5576ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
5577ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5578ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5579ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5580ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5581ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5582ade774b1SBjoern A. Zeeb 
5583ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
5584ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
5585ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
5586ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
5587ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
5588ade774b1SBjoern A. Zeeb 
5589ade774b1SBjoern A. Zeeb 	return (skb);
5590ade774b1SBjoern A. Zeeb }
5591ade774b1SBjoern A. Zeeb 
5592ade774b1SBjoern A. Zeeb struct sk_buff *
55936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
55946b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
55956b4cac81SBjoern A. Zeeb {
55966b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55976b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
55986b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
55996b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
56006b4cac81SBjoern A. Zeeb 	uint16_t v;
56016b4cac81SBjoern A. Zeeb 
56026b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
56036b4cac81SBjoern A. Zeeb 	if (skb == NULL)
56046b4cac81SBjoern A. Zeeb 		return (NULL);
56056b4cac81SBjoern A. Zeeb 
56066b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
56076b4cac81SBjoern A. Zeeb 
56086b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
56096b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
56106b4cac81SBjoern A. Zeeb 
56116b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
56126b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
56136b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5614616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
56156b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
56166b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
56176b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
56186b4cac81SBjoern A. Zeeb 
56196b4cac81SBjoern A. Zeeb 	return (skb);
56206b4cac81SBjoern A. Zeeb }
56216b4cac81SBjoern A. Zeeb 
56226b4cac81SBjoern A. Zeeb struct sk_buff *
56236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5624adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
56256b4cac81SBjoern A. Zeeb {
56266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56276b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
56286b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
56296b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
56306b4cac81SBjoern A. Zeeb 
5631adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
5632adff403fSBjoern A. Zeeb 
56336b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
56346b4cac81SBjoern A. Zeeb 	if (skb == NULL)
56356b4cac81SBjoern A. Zeeb 		return (NULL);
56366b4cac81SBjoern A. Zeeb 
56376b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
56386b4cac81SBjoern A. Zeeb 
56396b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
56406b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
56416b4cac81SBjoern A. Zeeb 
56426b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
56436b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
56446b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
56456b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
56466b4cac81SBjoern A. Zeeb 
56476b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
56486b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
56496b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
56506b4cac81SBjoern A. Zeeb 
56516b4cac81SBjoern A. Zeeb 	return (skb);
56526b4cac81SBjoern A. Zeeb }
56536b4cac81SBjoern A. Zeeb 
56546b4cac81SBjoern A. Zeeb struct wireless_dev *
56556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
56566b4cac81SBjoern A. Zeeb {
56576b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56586b4cac81SBjoern A. Zeeb 
56596b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
56606b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
56616b4cac81SBjoern A. Zeeb }
56626b4cac81SBjoern A. Zeeb 
56636b4cac81SBjoern A. Zeeb void
56646b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
56656b4cac81SBjoern A. Zeeb {
56666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56676b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
56686b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
56696b4cac81SBjoern A. Zeeb 	int arg;
56706b4cac81SBjoern A. Zeeb 
56716b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
56726b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
56736b4cac81SBjoern A. Zeeb 
56746b4cac81SBjoern A. Zeeb 	/*
5675f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
56766b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
56776b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
56786b4cac81SBjoern A. Zeeb 	 */
5679f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
5680bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
56816b4cac81SBjoern A. Zeeb 
5682018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5683018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
56846b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
56856b4cac81SBjoern A. Zeeb }
56866b4cac81SBjoern A. Zeeb 
5687bb81db90SBjoern A. Zeeb void
5688bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
5689bb81db90SBjoern A. Zeeb {
5690bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5691bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
5692bb81db90SBjoern A. Zeeb 
5693bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
5694bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
5695bb81db90SBjoern A. Zeeb 
5696bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5697bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
56983540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
5699bb81db90SBjoern A. Zeeb }
5700bb81db90SBjoern A. Zeeb 
57015edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
57025edde07cSBjoern A. Zeeb 
57035a9a0d78SBjoern A. Zeeb void
57045a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
57055a9a0d78SBjoern A. Zeeb {
57065a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57075a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57085a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
57095a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
57105a9a0d78SBjoern A. Zeeb 
57115a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
57125a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
57135a9a0d78SBjoern A. Zeeb 
57145a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
57155a9a0d78SBjoern A. Zeeb 
57165a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
57175a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
57185a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
57195a9a0d78SBjoern A. Zeeb 	else
57205a9a0d78SBjoern A. Zeeb 		ac_count = 1;
57215a9a0d78SBjoern A. Zeeb 
57225a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
57235a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
57245a9a0d78SBjoern A. Zeeb 
57255a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
57265a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
57275a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
57285a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
57290d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
57305a9a0d78SBjoern A. Zeeb 				/*
57315a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
57325a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
57335a9a0d78SBjoern A. Zeeb 				 */
57340d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
57350d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
57365a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
57375a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
57385a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
57395a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
57400d2cb6a6SBjoern A. Zeeb #endif
57415a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
57425a9a0d78SBjoern A. Zeeb 			}
57435a9a0d78SBjoern A. Zeeb 		}
57445a9a0d78SBjoern A. Zeeb 	}
57455a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
57465a9a0d78SBjoern A. Zeeb }
57475a9a0d78SBjoern A. Zeeb 
57485a9a0d78SBjoern A. Zeeb void
57495a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
57505a9a0d78SBjoern A. Zeeb {
57515a9a0d78SBjoern A. Zeeb 	int i;
57525a9a0d78SBjoern A. Zeeb 
57535a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
57545a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
57555a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
57565a9a0d78SBjoern A. Zeeb }
57575a9a0d78SBjoern A. Zeeb 
57585a9a0d78SBjoern A. Zeeb 
57595a9a0d78SBjoern A. Zeeb static void
57605a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
57615a9a0d78SBjoern A. Zeeb {
57625a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57635a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57645a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
57655a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
57665a9a0d78SBjoern A. Zeeb 
57675a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
57685a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
57695a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
57705a9a0d78SBjoern A. Zeeb 	else
57715a9a0d78SBjoern A. Zeeb 		ac_count = 1;
57725a9a0d78SBjoern A. Zeeb 
57735a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
57745a9a0d78SBjoern A. Zeeb 
57755a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
57765a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
57775a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
57785a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
57795a9a0d78SBjoern A. Zeeb 
57805a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
57815a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
57825a9a0d78SBjoern A. Zeeb 
57835a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
57845a9a0d78SBjoern A. Zeeb 
57855a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
57865a9a0d78SBjoern A. Zeeb 
57870d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
57885a9a0d78SBjoern A. Zeeb 				/*
57895a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
57905a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
57915a9a0d78SBjoern A. Zeeb 				 */
57920d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
57930d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
57945a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
57955a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
57965a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
57975a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
57980d2cb6a6SBjoern A. Zeeb #endif
57995a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
58005a9a0d78SBjoern A. Zeeb 
58015a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
58025a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
58035a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
58045a9a0d78SBjoern A. Zeeb 
58055a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
58065a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
58075a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
58085a9a0d78SBjoern A. Zeeb 
58095a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
58105a9a0d78SBjoern A. Zeeb 							continue;
58115a9a0d78SBjoern A. Zeeb 
58125a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
58135a9a0d78SBjoern A. Zeeb 							continue;
58145a9a0d78SBjoern A. Zeeb 
58155a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
58165a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
58175a9a0d78SBjoern A. Zeeb 							continue;
58185a9a0d78SBjoern A. Zeeb 
58195a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
58205a9a0d78SBjoern A. Zeeb 
58215a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
58225a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
58235a9a0d78SBjoern A. Zeeb 					}
58245a9a0d78SBjoern A. Zeeb 				}
58255a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
58265a9a0d78SBjoern A. Zeeb 			}
58275a9a0d78SBjoern A. Zeeb 		}
58285a9a0d78SBjoern A. Zeeb 	}
58295a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58305a9a0d78SBjoern A. Zeeb }
58315a9a0d78SBjoern A. Zeeb 
58325a9a0d78SBjoern A. Zeeb void
58335a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
58345a9a0d78SBjoern A. Zeeb {
58355a9a0d78SBjoern A. Zeeb 	int i;
58365a9a0d78SBjoern A. Zeeb 
58375a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
58385a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
58395a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
58405a9a0d78SBjoern A. Zeeb }
58415a9a0d78SBjoern A. Zeeb 
58425a9a0d78SBjoern A. Zeeb void
58435a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
58445a9a0d78SBjoern A. Zeeb {
58455a9a0d78SBjoern A. Zeeb 
58465a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
58475a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
58485a9a0d78SBjoern A. Zeeb 
58495a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
58505a9a0d78SBjoern A. Zeeb }
58515a9a0d78SBjoern A. Zeeb 
58525a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
58535a9a0d78SBjoern A. Zeeb void
58545a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
58555a9a0d78SBjoern A. Zeeb {
58565a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58575a9a0d78SBjoern A. Zeeb 
58585a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58595a9a0d78SBjoern A. Zeeb 
58605a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
58615a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
58625a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
58635a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
58645a9a0d78SBjoern A. Zeeb }
58655a9a0d78SBjoern A. Zeeb 
58665a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
58675a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
58685a9a0d78SBjoern A. Zeeb {
58695a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58705a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
58715a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
58725a9a0d78SBjoern A. Zeeb 
58735a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58745a9a0d78SBjoern A. Zeeb 	txq = NULL;
58755a9a0d78SBjoern A. Zeeb 
58765a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
58775a9a0d78SBjoern A. Zeeb 
58785a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
58795a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
58805a9a0d78SBjoern A. Zeeb 		goto out;
58815a9a0d78SBjoern A. Zeeb 
58825a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
58835a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
58845a9a0d78SBjoern A. Zeeb 		goto out;
58855a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
58865a9a0d78SBjoern A. Zeeb 		goto out;
58875a9a0d78SBjoern A. Zeeb 
58885a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
58895a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
58905a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
58915a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
58925a9a0d78SBjoern A. Zeeb 
58935a9a0d78SBjoern A. Zeeb out:
58945a9a0d78SBjoern A. Zeeb 	return (txq);
58955a9a0d78SBjoern A. Zeeb }
58965a9a0d78SBjoern A. Zeeb 
58975a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
58985a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
58995a9a0d78SBjoern A. Zeeb {
59005a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59015a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
5902eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
59035a9a0d78SBjoern A. Zeeb 
59045a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
59055a9a0d78SBjoern A. Zeeb 
59065a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
59075a9a0d78SBjoern A. Zeeb 
59085a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
5909eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
5910eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
5911eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5912eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
59135a9a0d78SBjoern A. Zeeb 		goto out;
59145a9a0d78SBjoern A. Zeeb 
59155a9a0d78SBjoern A. Zeeb 	/* Make sure we do not double-schedule. */
59165a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_entry.tqe_next != NULL)
59175a9a0d78SBjoern A. Zeeb 		goto out;
59185a9a0d78SBjoern A. Zeeb 
59195a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59205a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
59215a9a0d78SBjoern A. Zeeb out:
59225a9a0d78SBjoern A. Zeeb 	return;
59235a9a0d78SBjoern A. Zeeb }
59245a9a0d78SBjoern A. Zeeb 
5925eac3646fSBjoern A. Zeeb void
5926eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
5927eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
5928eac3646fSBjoern A. Zeeb {
5929eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5930eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
5931eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
5932eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
5933eac3646fSBjoern A. Zeeb 
5934eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5935eac3646fSBjoern A. Zeeb 
5936eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
5937eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
5938eac3646fSBjoern A. Zeeb 	do {
5939eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
5940eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
5941eac3646fSBjoern A. Zeeb 			break;
5942eac3646fSBjoern A. Zeeb 
5943eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
5944eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
5945eac3646fSBjoern A. Zeeb 		do {
5946eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
5947eac3646fSBjoern A. Zeeb 			if (skb == NULL)
5948eac3646fSBjoern A. Zeeb 				break;
5949eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
5950eac3646fSBjoern A. Zeeb 		} while(1);
5951eac3646fSBjoern A. Zeeb 
5952eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
5953eac3646fSBjoern A. Zeeb 	} while (1);
5954eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
5955eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
5956eac3646fSBjoern A. Zeeb }
5957eac3646fSBjoern A. Zeeb 
59585a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
59595a9a0d78SBjoern A. Zeeb 
59605edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
59615edde07cSBjoern A. Zeeb 	u_int refcnt;
59625edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
59635edde07cSBjoern A. Zeeb };
59645edde07cSBjoern A. Zeeb 
59655edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
59665edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
59675edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
59685edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
59695edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
59705edde07cSBjoern A. Zeeb 	size_t ssid_len;
59715edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
59725edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
59735edde07cSBjoern A. Zeeb 
59745edde07cSBjoern A. Zeeb 	/*
59755edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
59765edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
59775edde07cSBjoern A. Zeeb 	 */
59785edde07cSBjoern A. Zeeb 	bool match;
59795edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
59805edde07cSBjoern A. Zeeb };
59815edde07cSBjoern A. Zeeb 
59825edde07cSBjoern A. Zeeb static void
59835edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
59845edde07cSBjoern A. Zeeb {
59855edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
59865edde07cSBjoern A. Zeeb 	size_t ielen;
59875edde07cSBjoern A. Zeeb 
59885edde07cSBjoern A. Zeeb 	lookup = arg;
59895edde07cSBjoern A. Zeeb 
59905edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
59915edde07cSBjoern A. Zeeb 	if (lookup->match)
59925edde07cSBjoern A. Zeeb 		return;
59935edde07cSBjoern A. Zeeb 
59945edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
59955edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
59965edde07cSBjoern A. Zeeb 		return;
59975edde07cSBjoern A. Zeeb 
59985edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
59995edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
60005edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
60015edde07cSBjoern A. Zeeb 		return;
60025edde07cSBjoern A. Zeeb 	}
60035edde07cSBjoern A. Zeeb 
60045edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
60055edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
60065edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
60075edde07cSBjoern A. Zeeb 		return;
60085edde07cSBjoern A. Zeeb 	}
60095edde07cSBjoern A. Zeeb 
60105edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
60115edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
60125edde07cSBjoern A. Zeeb 
60135edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
60145edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
60155edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
60165edde07cSBjoern A. Zeeb 			return;
60175edde07cSBjoern A. Zeeb 	}
60185edde07cSBjoern A. Zeeb 
60195edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
60205edde07cSBjoern A. Zeeb 		return;
60215edde07cSBjoern A. Zeeb 
60225edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
60235edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
60245edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
60255edde07cSBjoern A. Zeeb 			return;
60265edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
60275edde07cSBjoern A. Zeeb 			return;
60285edde07cSBjoern A. Zeeb 	}
60295edde07cSBjoern A. Zeeb 
60305edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
60315edde07cSBjoern A. Zeeb 
60325edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
60335edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
60345edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
60355edde07cSBjoern A. Zeeb 		return;
60365edde07cSBjoern A. Zeeb 
60375edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
60385edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
60395edde07cSBjoern A. Zeeb 	if (ielen)
60405edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
60415edde07cSBjoern A. Zeeb 
60425edde07cSBjoern A. Zeeb 	lookup->match = true;
60435edde07cSBjoern A. Zeeb }
60445edde07cSBjoern A. Zeeb 
60455edde07cSBjoern A. Zeeb struct cfg80211_bss *
60465edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
60475edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
60485edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
60495edde07cSBjoern A. Zeeb {
60505edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
60515edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
60525edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
60535edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
60545edde07cSBjoern A. Zeeb 
60555edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
60565edde07cSBjoern A. Zeeb 
60575edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
60585edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
60595edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
60605edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
60615edde07cSBjoern A. Zeeb 		return (NULL);
60625edde07cSBjoern A. Zeeb 	}
60635edde07cSBjoern A. Zeeb 
60645edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
60655edde07cSBjoern A. Zeeb 	lookup.chan = chan;
60665edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
60675edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
60685edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
60695edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
60705edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
60715edde07cSBjoern A. Zeeb 	lookup.match = false;
60725edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
60735edde07cSBjoern A. Zeeb 
60745edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
60755edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
60765edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
60775edde07cSBjoern A. Zeeb 	if (!lookup.match) {
60785edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
60795edde07cSBjoern A. Zeeb 		return (NULL);
60805edde07cSBjoern A. Zeeb 	}
60815edde07cSBjoern A. Zeeb 
60825edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
60835edde07cSBjoern A. Zeeb 	return (&lbss->bss);
60845edde07cSBjoern A. Zeeb }
60855edde07cSBjoern A. Zeeb 
60865edde07cSBjoern A. Zeeb void
60875edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
60885edde07cSBjoern A. Zeeb {
60895edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
60905edde07cSBjoern A. Zeeb 
60915edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
60925edde07cSBjoern A. Zeeb 
60935edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
60945edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
60955edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
60965edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
60975edde07cSBjoern A. Zeeb 	}
60985edde07cSBjoern A. Zeeb }
60995edde07cSBjoern A. Zeeb 
61005edde07cSBjoern A. Zeeb void
61015edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
61025edde07cSBjoern A. Zeeb {
61035edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
61045edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
61055edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
61065edde07cSBjoern A. Zeeb 
61075edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
61085edde07cSBjoern A. Zeeb 	ic = lhw->ic;
61095edde07cSBjoern A. Zeeb 
61105edde07cSBjoern A. Zeeb 	/*
61115edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
61125edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
61135edde07cSBjoern A. Zeeb 	 */
61145edde07cSBjoern A. Zeeb 	if (ic == NULL ||
61155edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
61165edde07cSBjoern A. Zeeb 		return;
61175edde07cSBjoern A. Zeeb 
61185edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
61195edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
61205edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
61215edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
61225edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
61235edde07cSBjoern A. Zeeb }
61245edde07cSBjoern A. Zeeb 
61255edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
61265edde07cSBjoern A. Zeeb 
61276b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
61286b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
61296b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6130