xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision ec6185c52661d3af0dac6dcc8701fc49fae3e1d9)
16b4cac81SBjoern A. Zeeb /*-
2*ec6185c5SBjoern A. Zeeb  * Copyright (c) 2020-2025 The FreeBSD Foundation
3c272abc5SBjoern A. Zeeb  * Copyright (c) 2020-2025 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 
144ac867c20SBjoern A. Zeeb #if 0
1456b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1466b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
147ac867c20SBjoern A. Zeeb #endif
14888665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1496b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
150759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1516b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1534a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1544a67f1dfSBjoern A. Zeeb #endif
1556b4cac81SBjoern A. Zeeb 
1569fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
1579fb91463SBjoern A. Zeeb static void
1589fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
1599fb91463SBjoern A. Zeeb {
1609fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
1619fb91463SBjoern A. Zeeb 	uint8_t *ie;
1629fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
1639fb91463SBjoern A. Zeeb 	int i, rx_nss;
1649fb91463SBjoern A. Zeeb 
1659fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
1669fb91463SBjoern A. Zeeb 		return;
1679fb91463SBjoern A. Zeeb 
1689fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
1699fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1709fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
1719fb91463SBjoern A. Zeeb 
1729fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
1739fb91463SBjoern A. Zeeb 
1749fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
1759fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
1769fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1779fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
1789fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
1799fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1809fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
1819fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
1829fb91463SBjoern A. Zeeb 
1839fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
1849fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
1859fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1869fb91463SBjoern A. Zeeb 		ie += 4;
1879fb91463SBjoern A. Zeeb 	ie += 2;
1889fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
1899fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
1909fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
1919fb91463SBjoern A. Zeeb 
1929fb91463SBjoern A. Zeeb 	rx_nss = 0;
1939fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
1949fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
1959fb91463SBjoern A. Zeeb 			rx_nss++;
1969fb91463SBjoern A. Zeeb 	}
1979fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
1989fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
1999fb91463SBjoern A. Zeeb 
2009fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
2019fb91463SBjoern A. Zeeb }
2029fb91463SBjoern A. Zeeb #endif
2039fb91463SBjoern A. Zeeb 
2049fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
2059fb91463SBjoern A. Zeeb static void
2069fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
2079fb91463SBjoern A. Zeeb {
2089fb91463SBjoern A. Zeeb 
2099fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
2109fb91463SBjoern A. Zeeb 		return;
2119fb91463SBjoern A. Zeeb 
2129fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
2139fb91463SBjoern A. Zeeb #ifdef __notyet__
2149fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
2159fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
2169fb91463SBjoern A. Zeeb 		} else
2179fb91463SBjoern A. Zeeb #endif
2189fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2199fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
2209fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2219fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
2229fb91463SBjoern A. Zeeb 	}
2239fb91463SBjoern A. Zeeb 
2249fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
2259fb91463SBjoern A. Zeeb 	return;
2269fb91463SBjoern A. Zeeb }
2279fb91463SBjoern A. Zeeb #endif
2289fb91463SBjoern A. Zeeb 
229d9f59799SBjoern A. Zeeb static void
23067674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
23167674c1cSBjoern A. Zeeb     const char *_f, int _l)
232d9f59799SBjoern A. Zeeb {
233d9f59799SBjoern A. Zeeb 
2349d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
2359d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
236d9f59799SBjoern A. Zeeb 		return;
237d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
238d9f59799SBjoern A. Zeeb 		return;
239d9f59799SBjoern A. Zeeb 
240d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
24167674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
24267674c1cSBjoern A. Zeeb 	if (ni != NULL)
24367674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
244d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
245d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
246d9f59799SBjoern A. Zeeb 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
2479d9ba2b7SBjoern A. Zeeb #endif
248d9f59799SBjoern A. Zeeb }
249d9f59799SBjoern A. Zeeb 
250d9f59799SBjoern A. Zeeb static void
251d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
252d9f59799SBjoern A. Zeeb {
253d9f59799SBjoern A. Zeeb 
254d9f59799SBjoern A. Zeeb 
255d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2560936c648SBjoern A. Zeeb 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
2570936c648SBjoern A. Zeeb 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
2580936c648SBjoern A. Zeeb 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
259d9f59799SBjoern A. Zeeb 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
260d9f59799SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
261d9f59799SBjoern A. Zeeb }
262d9f59799SBjoern A. Zeeb 
2634f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
2644f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
2654f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
2664f61ef8bSBjoern A. Zeeb {
2674f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
2684f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
2694f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
2704f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
271b6b352e4SBjoern A. Zeeb 	int band, i, tid;
2729fb91463SBjoern A. Zeeb 	int ht_rx_nss;
2739fb91463SBjoern A. Zeeb 	int vht_rx_nss;
2744f61ef8bSBjoern A. Zeeb 
2754f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
2764f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
2774f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
2784f61ef8bSBjoern A. Zeeb 		return (NULL);
2794f61ef8bSBjoern A. Zeeb 
2804f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
2814f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
2824f61ef8bSBjoern A. Zeeb 	/*
2830936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
2840936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
2850936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
2860936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
2870936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
2880936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
2890936c648SBjoern A. Zeeb 	 * two separate allocations.
2904f61ef8bSBjoern A. Zeeb 	 */
2910936c648SBjoern A. Zeeb 	lsta->ni = ni;
2924f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
2934f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
2944f61ef8bSBjoern A. Zeeb 
2954f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2964f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2974f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2984f61ef8bSBjoern A. Zeeb 
2994f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
300b6b352e4SBjoern A. Zeeb 
301b6b352e4SBjoern A. Zeeb 	/* TXQ */
3024f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
3034f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
3044f61ef8bSBjoern A. Zeeb 
305d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
3064f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
3074f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
3084f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
3094f61ef8bSBjoern A. Zeeb 			goto cleanup;
3104f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
3114f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
312d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
313d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
314d0d29110SBjoern A. Zeeb 				continue;
315d0d29110SBjoern A. Zeeb 			}
316d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
3174f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
3184f61ef8bSBjoern A. Zeeb 		} else {
319fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
3204f61ef8bSBjoern A. Zeeb 		}
321d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
3220cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
323d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
3244f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
3254f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
3260cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
327d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
328eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
3294f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
3304f61ef8bSBjoern A. Zeeb 	}
3314f61ef8bSBjoern A. Zeeb 
332b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
333b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
334b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
335b6b352e4SBjoern A. Zeeb 
336b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
337b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
338b6b352e4SBjoern A. Zeeb 			continue;
339b6b352e4SBjoern A. Zeeb 
340b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
341b6b352e4SBjoern A. Zeeb 
342b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
343b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
344b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
345b6b352e4SBjoern A. Zeeb 		}
346b6b352e4SBjoern A. Zeeb 	}
3479fb91463SBjoern A. Zeeb 
3486ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
3499fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
350f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
3519fb91463SBjoern A. Zeeb 
3529fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
3539fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3549fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
3559fb91463SBjoern A. Zeeb #endif
3569fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
3579fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
3589fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
3599fb91463SBjoern A. Zeeb #endif
3609fb91463SBjoern A. Zeeb 
3619fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
3629fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
3639fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
364b6b352e4SBjoern A. Zeeb 
3656ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
3666ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
3676ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
3686ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
3696ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
3706ffb7bd4SBjoern A. Zeeb 	}
3716ffb7bd4SBjoern A. Zeeb 
3724f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
373fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
3744f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
3754f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
3760936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
3774f61ef8bSBjoern A. Zeeb 
3784f61ef8bSBjoern A. Zeeb 	return (lsta);
3794f61ef8bSBjoern A. Zeeb 
3804f61ef8bSBjoern A. Zeeb cleanup:
381eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
382eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
383eac3646fSBjoern A. Zeeb 
384eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
385eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
3864f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
387eac3646fSBjoern A. Zeeb 	}
3884f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
3894f61ef8bSBjoern A. Zeeb 	return (NULL);
3904f61ef8bSBjoern A. Zeeb }
3914f61ef8bSBjoern A. Zeeb 
3920936c648SBjoern A. Zeeb static void
3930936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
3940936c648SBjoern A. Zeeb {
3950936c648SBjoern A. Zeeb 	struct mbuf *m;
3960936c648SBjoern A. Zeeb 
3970936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
3980936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
3990936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
4000936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
4010936c648SBjoern A. Zeeb 
4020936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
4030936c648SBjoern A. Zeeb 	IMPROVE();
4040936c648SBjoern A. Zeeb 
405fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
406fa4e4257SBjoern A. Zeeb 
407fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4080936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
409fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4100936c648SBjoern A. Zeeb 
4110936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
4120936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
4130936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
4140936c648SBjoern A. Zeeb 
4150936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4160936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
4170936c648SBjoern A. Zeeb 	while (m != NULL) {
4180936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
4190936c648SBjoern A. Zeeb 
4200936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
4210936c648SBjoern A. Zeeb 		if (nim != NULL)
4220936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
4230936c648SBjoern A. Zeeb 		m_freem(m);
4240936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
4250936c648SBjoern A. Zeeb 	}
4260936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
4270936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
428fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
4290936c648SBjoern A. Zeeb 
4300936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
4310936c648SBjoern A. Zeeb 
4320936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
4330936c648SBjoern A. Zeeb 
4340936c648SBjoern A. Zeeb 	/* Free lsta. */
4350936c648SBjoern A. Zeeb 	lsta->ni = NULL;
4360936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
4370936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
4380936c648SBjoern A. Zeeb }
4390936c648SBjoern A. Zeeb 
4400936c648SBjoern A. Zeeb 
4416b4cac81SBjoern A. Zeeb static enum nl80211_band
4426b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
4436b4cac81SBjoern A. Zeeb {
4446b4cac81SBjoern A. Zeeb 
4456b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
4466b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
4476b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
4486b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
4496b4cac81SBjoern A. Zeeb #ifdef __notyet__
4506b4cac81SBjoern A. Zeeb 	else if ()
4516b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
4526b4cac81SBjoern A. Zeeb 	else if ()
4536b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
4546b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
4556b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
4566b4cac81SBjoern A. Zeeb #endif
4576b4cac81SBjoern A. Zeeb 	else
4586b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
4596b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
4606b4cac81SBjoern A. Zeeb }
4616b4cac81SBjoern A. Zeeb 
4626b4cac81SBjoern A. Zeeb static uint32_t
4636b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
4646b4cac81SBjoern A. Zeeb {
4656b4cac81SBjoern A. Zeeb 
4666b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
4676b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
4686b4cac81SBjoern A. Zeeb 	switch (band) {
4696b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
4706b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
4716b4cac81SBjoern A. Zeeb 		break;
4726b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
4736b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
4746b4cac81SBjoern A. Zeeb 		break;
4756b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
4766b4cac81SBjoern A. Zeeb 		break;
4776b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
4786b4cac81SBjoern A. Zeeb 		break;
4796b4cac81SBjoern A. Zeeb 	default:
4806b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
4816b4cac81SBjoern A. Zeeb 		break;
4826b4cac81SBjoern A. Zeeb 	}
4836b4cac81SBjoern A. Zeeb 
4846b4cac81SBjoern A. Zeeb 	IMPROVE();
4856b4cac81SBjoern A. Zeeb 	return (0x00);
4866b4cac81SBjoern A. Zeeb }
4876b4cac81SBjoern A. Zeeb 
488e3a0b120SBjoern A. Zeeb #if 0
4896b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
4906b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
4916b4cac81SBjoern A. Zeeb {
4926b4cac81SBjoern A. Zeeb 
4936b4cac81SBjoern A. Zeeb 	switch (ac) {
4946b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
4956b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
4966b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
4976b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
4986b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
4996b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5006b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
5016b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
5026b4cac81SBjoern A. Zeeb 	default:
5036b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
5046b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
5056b4cac81SBjoern A. Zeeb 	}
5066b4cac81SBjoern A. Zeeb }
507e3a0b120SBjoern A. Zeeb #endif
5086b4cac81SBjoern A. Zeeb 
5096b4cac81SBjoern A. Zeeb static enum nl80211_iftype
5106b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
5116b4cac81SBjoern A. Zeeb {
5126b4cac81SBjoern A. Zeeb 
5136b4cac81SBjoern A. Zeeb 	switch (opmode) {
5146b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
5156b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
5166b4cac81SBjoern A. Zeeb 		break;
5176b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
5186b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
5196b4cac81SBjoern A. Zeeb 		break;
5206b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
5216b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
5226b4cac81SBjoern A. Zeeb 		break;
5236b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
5246b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
5256b4cac81SBjoern A. Zeeb 		break;
5266b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
5276b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
5286b4cac81SBjoern A. Zeeb 		break;
5296b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
5306b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
5316b4cac81SBjoern A. Zeeb 		break;
5326b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
5336b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5346b4cac81SBjoern A. Zeeb 	default:
5356b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
5366b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
5376b4cac81SBjoern A. Zeeb 	}
5386b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
5396b4cac81SBjoern A. Zeeb }
5406b4cac81SBjoern A. Zeeb 
541b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
5426b4cac81SBjoern A. Zeeb static uint32_t
5436b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
5446b4cac81SBjoern A. Zeeb {
5456b4cac81SBjoern A. Zeeb 
5466b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
5476b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
5486b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5496b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
5506b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
5516b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
552906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
5536b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
5546b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
5556b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
5566b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
5576b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
5586b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
5596b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5606b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5616b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5626b4cac81SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
5636b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5646b4cac81SBjoern A. Zeeb 		break;
5656b4cac81SBjoern A. Zeeb 	default:
5666b4cac81SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
5676b4cac81SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
5686b4cac81SBjoern A. Zeeb 	}
5696b4cac81SBjoern A. Zeeb 
5706b4cac81SBjoern A. Zeeb 	return (0);
5716b4cac81SBjoern A. Zeeb }
5726b4cac81SBjoern A. Zeeb 
5736b4cac81SBjoern A. Zeeb static uint32_t
5746b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
5756b4cac81SBjoern A. Zeeb {
5766b4cac81SBjoern A. Zeeb 
5776b4cac81SBjoern A. Zeeb 	switch (cipher) {
5786b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
5796b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
5806b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
5816b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
5826b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
5836b4cac81SBjoern A. Zeeb 		if (keylen < 8)
5846b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
5856b4cac81SBjoern A. Zeeb 		else
5866b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
5876b4cac81SBjoern A. Zeeb 		break;
5886b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
5896b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
5906b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
5916b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
5926b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
5936b4cac81SBjoern A. Zeeb 		break;
5946b4cac81SBjoern A. Zeeb 	default:
5956b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
5966b4cac81SBjoern A. Zeeb 	};
5976b4cac81SBjoern A. Zeeb 	return (0);
5986b4cac81SBjoern A. Zeeb }
5996b4cac81SBjoern A. Zeeb #endif
6006b4cac81SBjoern A. Zeeb 
6016b4cac81SBjoern A. Zeeb #ifdef __notyet__
6026b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
6036b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
6046b4cac81SBjoern A. Zeeb {
6056b4cac81SBjoern A. Zeeb 
6066b4cac81SBjoern A. Zeeb 	/*
6076b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
6086b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
6096b4cac81SBjoern A. Zeeb 	 */
6106b4cac81SBjoern A. Zeeb 	switch (state) {
6116b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
6126b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
6136b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
6146b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
6156b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
6166b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
6176b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
6186b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
6196b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
6206b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
6216b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
6226b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
6236b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
6246b4cac81SBjoern A. Zeeb 	default:
6256b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
6266b4cac81SBjoern A. Zeeb 	};
6276b4cac81SBjoern A. Zeeb 
6286b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
6296b4cac81SBjoern A. Zeeb }
6306b4cac81SBjoern A. Zeeb #endif
6316b4cac81SBjoern A. Zeeb 
6326b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6336b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
6346b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
6356b4cac81SBjoern A. Zeeb {
6366b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
6376b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
6386b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
6396b4cac81SBjoern A. Zeeb 	int i, nchans;
6406b4cac81SBjoern A. Zeeb 
6416b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
6426b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
6436b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
6446b4cac81SBjoern A. Zeeb 		return (NULL);
6456b4cac81SBjoern A. Zeeb 
6466b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
6476b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
6486b4cac81SBjoern A. Zeeb 		return (NULL);
6496b4cac81SBjoern A. Zeeb 
6506b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
6516b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
6526b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
6536b4cac81SBjoern A. Zeeb 			return (&channels[i]);
6546b4cac81SBjoern A. Zeeb 	}
6556b4cac81SBjoern A. Zeeb 
6566b4cac81SBjoern A. Zeeb 	return (NULL);
6576b4cac81SBjoern A. Zeeb }
6586b4cac81SBjoern A. Zeeb 
6592ac8a218SBjoern A. Zeeb #if 0
6606b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
6616b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
6626b4cac81SBjoern A. Zeeb {
6636b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
6646b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
6656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6666b4cac81SBjoern A. Zeeb 
6676b4cac81SBjoern A. Zeeb 	chan = NULL;
6686b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
6696b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
6706b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
6716b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
6726b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
6736b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
6746b4cac81SBjoern A. Zeeb 	else
6756b4cac81SBjoern A. Zeeb 		c = NULL;
6766b4cac81SBjoern A. Zeeb 
6776b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6786b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
6796b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
6806b4cac81SBjoern A. Zeeb 	}
6816b4cac81SBjoern A. Zeeb 
6826b4cac81SBjoern A. Zeeb 	return (chan);
6836b4cac81SBjoern A. Zeeb }
6842ac8a218SBjoern A. Zeeb #endif
6856b4cac81SBjoern A. Zeeb 
6862e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
6872e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
6882e183d99SBjoern A. Zeeb {
6892e183d99SBjoern A. Zeeb 	enum nl80211_band band;
6902e183d99SBjoern A. Zeeb 
6912e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6922e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6932e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6942e183d99SBjoern A. Zeeb 		int i;
6952e183d99SBjoern A. Zeeb 
6962e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
6972e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6982e183d99SBjoern A. Zeeb 			continue;
6992e183d99SBjoern A. Zeeb 
7002e183d99SBjoern A. Zeeb 		channels = supband->channels;
7012e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
7022e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
7032e183d99SBjoern A. Zeeb 				return (&channels[i]);
7042e183d99SBjoern A. Zeeb 		}
7052e183d99SBjoern A. Zeeb 	}
7062e183d99SBjoern A. Zeeb 
7072e183d99SBjoern A. Zeeb 	return (NULL);
7082e183d99SBjoern A. Zeeb }
7092e183d99SBjoern A. Zeeb 
710b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
7116b4cac81SBjoern A. Zeeb static int
7126b4cac81SBjoern A. Zeeb _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
7136b4cac81SBjoern A. Zeeb     enum set_key_cmd cmd)
7146b4cac81SBjoern A. Zeeb {
7156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
7166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
7176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
7186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7196b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
7206b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
7216b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
7226b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
7236b4cac81SBjoern A. Zeeb 	int error;
7246b4cac81SBjoern A. Zeeb 
7256b4cac81SBjoern A. Zeeb 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
7266b4cac81SBjoern A. Zeeb 
7276b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
7286b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
7296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
7306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7326b4cac81SBjoern A. Zeeb 
7336b4cac81SBjoern A. Zeeb 	memset(&kc, 0, sizeof(kc));
7346b4cac81SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
7356b4cac81SBjoern A. Zeeb 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
7366b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
7376b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
7386b4cac81SBjoern A. Zeeb #if 0
7396b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
7406b4cac81SBjoern A. Zeeb #endif
7416b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
7426b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
7436b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
7446b4cac81SBjoern A. Zeeb 
7456b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
7466b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
7476b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
7486b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
7496b4cac81SBjoern A. Zeeb 		break;
7506b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7516b4cac81SBjoern A. Zeeb 	default:
7526b4cac81SBjoern A. Zeeb 		IMPROVE();
7536b4cac81SBjoern A. Zeeb 		return (0);
7546b4cac81SBjoern A. Zeeb 	};
7556b4cac81SBjoern A. Zeeb 
7566b4cac81SBjoern A. Zeeb 	ni = vap->iv_bss;
7576b4cac81SBjoern A. Zeeb 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
7586b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
7596b4cac81SBjoern A. Zeeb 		struct lkpi_sta *lsta;
7606b4cac81SBjoern A. Zeeb 
7616b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
7626b4cac81SBjoern A. Zeeb 		lsta->kc = kc;
7636b4cac81SBjoern A. Zeeb 	}
7646b4cac81SBjoern A. Zeeb 
7656b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
7666b4cac81SBjoern A. Zeeb 	if (error != 0) {
7676b4cac81SBjoern A. Zeeb 		/* XXX-BZ leaking kc currently */
7686b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
7696b4cac81SBjoern A. Zeeb 		return (0);
7706b4cac81SBjoern A. Zeeb 	} else {
7716b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
7726b4cac81SBjoern A. Zeeb 		    "flags %#10x\n", __func__,
7736b4cac81SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
7746b4cac81SBjoern A. Zeeb 		return (1);
7756b4cac81SBjoern A. Zeeb 	}
7766b4cac81SBjoern A. Zeeb }
7776b4cac81SBjoern A. Zeeb 
7786b4cac81SBjoern A. Zeeb static int
7796b4cac81SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
7806b4cac81SBjoern A. Zeeb {
7816b4cac81SBjoern A. Zeeb 
7826b4cac81SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
7836b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
7846b4cac81SBjoern A. Zeeb }
7856b4cac81SBjoern A. Zeeb static  int
7866b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
7876b4cac81SBjoern A. Zeeb {
7886b4cac81SBjoern A. Zeeb 
7896b4cac81SBjoern A. Zeeb 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
7906b4cac81SBjoern A. Zeeb }
7916b4cac81SBjoern A. Zeeb #endif
7926b4cac81SBjoern A. Zeeb 
7936b4cac81SBjoern A. Zeeb static u_int
7946b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
7956b4cac81SBjoern A. Zeeb {
7966b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
7976b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
7986b4cac81SBjoern A. Zeeb 
7996b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
8006b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
8016b4cac81SBjoern A. Zeeb 
8026b4cac81SBjoern A. Zeeb 	mc_list = arg;
8036b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
8046b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
8056b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
8066b4cac81SBjoern A. Zeeb 			return (0);
8076b4cac81SBjoern A. Zeeb 	}
8086b4cac81SBjoern A. Zeeb 
8096b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
8106b4cac81SBjoern A. Zeeb 	if (addr == NULL)
8116b4cac81SBjoern A. Zeeb 		return (0);
8126b4cac81SBjoern A. Zeeb 
8136b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
8146b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
8156b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
8166b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
8176b4cac81SBjoern A. Zeeb 	mc_list->count++;
8186b4cac81SBjoern A. Zeeb 
8199d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8209d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8216b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
8226b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
8239d9ba2b7SBjoern A. Zeeb #endif
8246b4cac81SBjoern A. Zeeb 
8256b4cac81SBjoern A. Zeeb 	return (1);
8266b4cac81SBjoern A. Zeeb }
8276b4cac81SBjoern A. Zeeb 
8286b4cac81SBjoern A. Zeeb static void
8296b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
8306b4cac81SBjoern A. Zeeb {
8316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
8326b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8336b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
8346b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
8356b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
8366b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
8376b4cac81SBjoern A. Zeeb 	u64 mc;
8386b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
8396b4cac81SBjoern A. Zeeb 
8406b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
8416b4cac81SBjoern A. Zeeb 
8426b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
8436b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
8446b4cac81SBjoern A. Zeeb 		return;
8456b4cac81SBjoern A. Zeeb 
8466b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
8476b4cac81SBjoern A. Zeeb 		return;
8486b4cac81SBjoern A. Zeeb 
8496b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
8506b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
8516b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
8526b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
8536b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8546b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
8556b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
8566b4cac81SBjoern A. Zeeb 	} else {
8576b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
8586b4cac81SBjoern A. Zeeb 	}
8596b4cac81SBjoern A. Zeeb 
8606b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8616b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
8626b4cac81SBjoern A. Zeeb 	/*
8636b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
8646b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
8656b4cac81SBjoern A. Zeeb 	 */
8666b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
8676b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
8686b4cac81SBjoern A. Zeeb 
8699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
8716b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
8726b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
8739d9ba2b7SBjoern A. Zeeb #endif
8746b4cac81SBjoern A. Zeeb 
8756b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
8766b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
8776b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
8786b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
8796b4cac81SBjoern A. Zeeb 			mc_list.count--;
8806b4cac81SBjoern A. Zeeb 		}
8816b4cac81SBjoern A. Zeeb 	}
8826b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
8836b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
8846b4cac81SBjoern A. Zeeb }
8856b4cac81SBjoern A. Zeeb 
886fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
887fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
888fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
889fa8f007dSBjoern A. Zeeb {
890fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
891fa8f007dSBjoern A. Zeeb 
892fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
893fa8f007dSBjoern A. Zeeb 
8949d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
8959d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
896fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
897fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
898ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
899fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
900616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
901fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
902fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
903fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
904fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
905ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
9069d9ba2b7SBjoern A. Zeeb #endif
907fa8f007dSBjoern A. Zeeb 
908fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
909fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
910fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
911fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
912fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
913fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
914fa8f007dSBjoern A. Zeeb 	}
915fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
916fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
917fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
918fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
919fa8f007dSBjoern A. Zeeb 	}
920fa8f007dSBjoern A. Zeeb 
921fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
922fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
923fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
924fa8f007dSBjoern A. Zeeb 
9259d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
9269d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
927fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
928fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
929ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
930fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
931616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
932fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
933fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
934fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
935fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
936ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
9379d9ba2b7SBjoern A. Zeeb #endif
938fa8f007dSBjoern A. Zeeb 
939fa8f007dSBjoern A. Zeeb 	return (bss_changed);
940fa8f007dSBjoern A. Zeeb }
941fa8f007dSBjoern A. Zeeb 
9426b4cac81SBjoern A. Zeeb static void
9436b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
9446b4cac81SBjoern A. Zeeb {
9456b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9466b4cac81SBjoern A. Zeeb 	int error;
9478ac540d3SBjoern A. Zeeb 	bool cancel;
9486b4cac81SBjoern A. Zeeb 
9498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9508ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9528ac540d3SBjoern A. Zeeb 	if (!cancel)
9536b4cac81SBjoern A. Zeeb 		return;
9546b4cac81SBjoern A. Zeeb 
9556b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9566b4cac81SBjoern A. Zeeb 
957bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
958bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
9596b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
9606b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
9618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
9626b4cac81SBjoern A. Zeeb 
9636b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
9648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
9658ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
9668ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
9678ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
9688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
9698ac540d3SBjoern A. Zeeb 
970bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
9716b4cac81SBjoern A. Zeeb 
9728ac540d3SBjoern A. Zeeb 	if (cancel)
9736b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
9746b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
9756b4cac81SBjoern A. Zeeb }
9766b4cac81SBjoern A. Zeeb 
9776b4cac81SBjoern A. Zeeb static void
978086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
979086be6a8SBjoern A. Zeeb {
980086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
981086be6a8SBjoern A. Zeeb 	int error;
982086be6a8SBjoern A. Zeeb 	bool old;
983086be6a8SBjoern A. Zeeb 
984086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
985086be6a8SBjoern A. Zeeb 	if (old == new)
986086be6a8SBjoern A. Zeeb 		return;
987086be6a8SBjoern A. Zeeb 
988086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
989086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
990086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
991086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
992086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
993086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
994086be6a8SBjoern A. Zeeb 	}
995086be6a8SBjoern A. Zeeb }
996086be6a8SBjoern A. Zeeb 
9975a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
9986b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9996b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
10006b4cac81SBjoern A. Zeeb {
10015a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
10025a4d2461SBjoern A. Zeeb 
10035a4d2461SBjoern A. Zeeb 	changed = 0;
10046b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1005616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
10066b4cac81SBjoern A. Zeeb 
10076b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
10086b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
10096b4cac81SBjoern A. Zeeb 
1010616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1011616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
10126b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
10136b4cac81SBjoern A. Zeeb 		IMPROVE();
1014086be6a8SBjoern A. Zeeb 
10155a4d2461SBjoern A. Zeeb 		/*
10165a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
10175a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
10185a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
10195a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
10205a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
10215a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
10225a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
10235a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
10245a4d2461SBjoern A. Zeeb 		 */
10256b4cac81SBjoern A. Zeeb 	}
10265a4d2461SBjoern A. Zeeb 
10275a4d2461SBjoern A. Zeeb 	return (changed);
10286b4cac81SBjoern A. Zeeb }
10296b4cac81SBjoern A. Zeeb 
10306b4cac81SBjoern A. Zeeb static void
10316b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
10326b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
10336b4cac81SBjoern A. Zeeb {
10346b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
10356b4cac81SBjoern A. Zeeb 	int tid;
1036eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
10376b4cac81SBjoern A. Zeeb 
10386b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
10396b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
10406b4cac81SBjoern A. Zeeb 
10416b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
10426b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
10436b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
10446b4cac81SBjoern A. Zeeb 				continue;
10456b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
10466b4cac81SBjoern A. Zeeb 			continue;
10476b4cac81SBjoern A. Zeeb 
10486b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
10496b4cac81SBjoern A. Zeeb 			continue;
10506b4cac81SBjoern A. Zeeb 
10516b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
10526b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
10536b4cac81SBjoern A. Zeeb 			continue;
10546b4cac81SBjoern A. Zeeb 
1055eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1056eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1057eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1058eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
10596b4cac81SBjoern A. Zeeb 			continue;
10606b4cac81SBjoern A. Zeeb 
10616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
10626b4cac81SBjoern A. Zeeb 	}
10636b4cac81SBjoern A. Zeeb }
10646b4cac81SBjoern A. Zeeb 
106588665349SBjoern A. Zeeb /*
106688665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
106788665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
106888665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
106988665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
107088665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
107188665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
107288665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
107388665349SBjoern A. Zeeb  * re-used.
107488665349SBjoern A. Zeeb  */
107588665349SBjoern A. Zeeb static void
107688665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
107788665349SBjoern A. Zeeb {
107888665349SBjoern A. Zeeb 	struct mbufq mq;
107988665349SBjoern A. Zeeb 	struct mbuf *m;
108088665349SBjoern A. Zeeb 	int len;
108188665349SBjoern A. Zeeb 
108288665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
108388665349SBjoern A. Zeeb 
108488665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
108588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
108688665349SBjoern A. Zeeb 	lsta->txq_ready = false;
108788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
108888665349SBjoern A. Zeeb 
108988665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
109088665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
109188665349SBjoern A. Zeeb 
109288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
109388665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
109488665349SBjoern A. Zeeb 	if (len <= 0) {
109588665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
109688665349SBjoern A. Zeeb 		return;
109788665349SBjoern A. Zeeb 	}
109888665349SBjoern A. Zeeb 
109988665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
110088665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
110188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
110288665349SBjoern A. Zeeb 
110388665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
110488665349SBjoern A. Zeeb 	while (m != NULL) {
110588665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
110688665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
110788665349SBjoern A. Zeeb 	}
110888665349SBjoern A. Zeeb }
110988665349SBjoern A. Zeeb 
11106b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
11116b4cac81SBjoern A. Zeeb 
11126b4cac81SBjoern A. Zeeb static int
11136b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11146b4cac81SBjoern A. Zeeb {
11156b4cac81SBjoern A. Zeeb 
11166b4cac81SBjoern A. Zeeb 	return (0);
11176b4cac81SBjoern A. Zeeb }
11186b4cac81SBjoern A. Zeeb 
11196b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
11206b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
11216b4cac81SBjoern A. Zeeb 
11226b4cac81SBjoern A. Zeeb static int
11236b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
11246b4cac81SBjoern A. Zeeb {
11256b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1126c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1127d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
11286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11296b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11306b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
11316b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
11326b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
11346b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
11356b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
11366b4cac81SBjoern A. Zeeb 	uint32_t changed;
11376b4cac81SBjoern A. Zeeb 	int error;
11386b4cac81SBjoern A. Zeeb 
11392ac8a218SBjoern A. Zeeb 	/*
11402ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
11412ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
11422ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
11432ac8a218SBjoern A. Zeeb 	 */
11442ac8a218SBjoern A. Zeeb 
11452ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
11462ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
11472ac8a218SBjoern A. Zeeb 		return (EINVAL);
11482ac8a218SBjoern A. Zeeb 	}
11490936c648SBjoern A. Zeeb 	/*
11500936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
11510936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
11520936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
11530936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
11540936c648SBjoern A. Zeeb 	 */
11552ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
11562ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
11572ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
11582ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
11590936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11602ac8a218SBjoern A. Zeeb 		return (EINVAL);
11616b4cac81SBjoern A. Zeeb 	}
11626b4cac81SBjoern A. Zeeb 
11636b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
11642ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
11652ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
11662ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
11672ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
11680936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11692ac8a218SBjoern A. Zeeb 		return (ESRCH);
11702ac8a218SBjoern A. Zeeb 	}
11712ac8a218SBjoern A. Zeeb 
11726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11736b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11746b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11756b4cac81SBjoern A. Zeeb 
11760936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
11770936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
11780936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
11790936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
11800936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
11810936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
11820936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
11830936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
118492690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
118592690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
11860936c648SBjoern A. Zeeb 		return (EBUSY);
11870936c648SBjoern A. Zeeb 	}
11880936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
11890936c648SBjoern A. Zeeb 
11906b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
11918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
11926b4cac81SBjoern A. Zeeb 
11936b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
11946b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1195d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1196d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
11976b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
11986b4cac81SBjoern A. Zeeb 	} else {
11996b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1200c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
12016b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1202d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
12036b4cac81SBjoern A. Zeeb 	}
12046b4cac81SBjoern A. Zeeb 
1205d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1206d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1207d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
12086b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1209d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1210d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1211d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1212d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
12139fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
12142ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
12152ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
12169fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
12179fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
12189fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1219d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
12209fb91463SBjoern A. Zeeb 		} else
1221d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
12229fb91463SBjoern A. Zeeb 	}
12239fb91463SBjoern A. Zeeb #endif
12249fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
12259fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
12269fb91463SBjoern A. Zeeb #ifdef __notyet__
12279fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1228d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1229d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
12309fb91463SBjoern A. Zeeb 		} else
12319fb91463SBjoern A. Zeeb #endif
12329fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1233d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
12349fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1235d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
12369fb91463SBjoern A. Zeeb 	}
12379fb91463SBjoern A. Zeeb #endif
12386b4cac81SBjoern A. Zeeb 	/* Responder ... */
1239d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1240d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1241d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1242d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
12439fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
12446b4cac81SBjoern A. Zeeb 
12456ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
12466ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
12476ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
12486ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
12496ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
12506ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
12516ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
12526ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
12536ffb7bd4SBjoern A. Zeeb 
12546ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
12556ffb7bd4SBjoern A. Zeeb 
12566ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
12576ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
12586ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
12596ffb7bd4SBjoern A. Zeeb 
12606ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
12616ffb7bd4SBjoern A. Zeeb 
12626b4cac81SBjoern A. Zeeb 	error = 0;
12636b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
12646b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
12656b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
12666b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
12676b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1268d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
12696b4cac81SBjoern A. Zeeb 	} else {
1270d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
12716b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
12727b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
12737b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
12747b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1275d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
12769fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
1277943a19c6SBjoern A. Zeeb 			if (vif->bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_40) {
12789fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
12799fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1280943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 += 10;
12819fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1282943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 -= 10;
12839fb91463SBjoern A. Zeeb 			}
12849fb91463SBjoern A. Zeeb #endif
12857b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1286d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
12876b4cac81SBjoern A. Zeeb 		} else {
1288018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1289018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
12906b4cac81SBjoern A. Zeeb 			goto out;
12916b4cac81SBjoern A. Zeeb 		}
12926ffb7bd4SBjoern A. Zeeb 
1293d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
12946ffb7bd4SBjoern A. Zeeb 
12956b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
12966b4cac81SBjoern A. Zeeb 		if (error == 0)
129768541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1298d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
12996b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
13006b4cac81SBjoern A. Zeeb 			error = 0;
13016b4cac81SBjoern A. Zeeb 		if (error != 0) {
1302018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1303018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1304d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1305d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1306c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
13076b4cac81SBjoern A. Zeeb 			goto out;
13086b4cac81SBjoern A. Zeeb 		}
13096b4cac81SBjoern A. Zeeb 	}
13106b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
13116b4cac81SBjoern A. Zeeb 
13126b4cac81SBjoern A. Zeeb 	/* RATES */
13136b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
13146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
13156b4cac81SBjoern A. Zeeb 
1316d9f59799SBjoern A. Zeeb 	/*
13170936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
13180936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
13190936c648SBjoern A. Zeeb 	 * under the hoods.
1320d9f59799SBjoern A. Zeeb 	 */
13210936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
13220936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
13236b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1324e24e8103SBjoern A. Zeeb 
132588665349SBjoern A. Zeeb 	/*
132688665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
132788665349SBjoern A. Zeeb 	 * that we can tx again.
132888665349SBjoern A. Zeeb 	 */
132988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
133088665349SBjoern A. Zeeb 	lsta->txq_ready = true;
133188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
133288665349SBjoern A. Zeeb 
1333e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
13342ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1335e24e8103SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1336e24e8103SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1337e24e8103SBjoern A. Zeeb 
1338d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
13396b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
13406b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
13416b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1342e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
13436b4cac81SBjoern A. Zeeb 	if (error != 0) {
13446b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1345018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1346018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
13476b4cac81SBjoern A. Zeeb 		goto out;
13486b4cac81SBjoern A. Zeeb 	}
13496b4cac81SBjoern A. Zeeb #if 0
13506b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
13516b4cac81SBjoern A. Zeeb #endif
13526b4cac81SBjoern A. Zeeb 
13539d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
13549d9ba2b7SBjoern A. Zeeb 
13551665ef97SBjoern A. Zeeb #if 0
13566b4cac81SBjoern A. Zeeb 	/*
13576b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
13586b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
13596b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
13606b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1361d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1362d9f59799SBjoern A. Zeeb 	 * for all queues.
13636b4cac81SBjoern A. Zeeb 	 */
1364e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
13651665ef97SBjoern A. Zeeb #endif
13666b4cac81SBjoern A. Zeeb 
13676b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
13686b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
13696b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
13706b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
13716b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
13726b4cac81SBjoern A. Zeeb 
13736b4cac81SBjoern A. Zeeb 	/*
13746b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
13756b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
13766b4cac81SBjoern A. Zeeb 	 * - event_callback
13776b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
13786b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
13796b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
13806b4cac81SBjoern A. Zeeb 	 */
13816b4cac81SBjoern A. Zeeb 
1382105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1383105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1384105b9df2SBjoern A. Zeeb 
1385105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1386105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1387105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1388105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1389105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1390105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1391105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1392105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1393105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1394105b9df2SBjoern A. Zeeb 
1395105b9df2SBjoern A. Zeeb 	/*
1396105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1397105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1398105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1399105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1400105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1401105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1402105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1403105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1404105b9df2SBjoern A. Zeeb 	 */
1405105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1406105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1407105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1408105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1409105b9df2SBjoern A. Zeeb 	} else {
1410105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1411105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1412105b9df2SBjoern A. Zeeb 		/*
1413105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1414105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1415105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1416105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1417105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1418105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1419105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1420105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1421105b9df2SBjoern A. Zeeb 		 */
1422105b9df2SBjoern A. Zeeb 	}
1423105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1424105b9df2SBjoern A. Zeeb 	goto out_relocked;
1425105b9df2SBjoern A. Zeeb 
14266b4cac81SBjoern A. Zeeb out:
14278ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14286b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1429105b9df2SBjoern A. Zeeb out_relocked:
14300936c648SBjoern A. Zeeb 	/*
1431105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
14320936c648SBjoern A. Zeeb 	 * during the work of this function.
14330936c648SBjoern A. Zeeb 	 */
14346b4cac81SBjoern A. Zeeb 	if (ni != NULL)
14356b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
14366b4cac81SBjoern A. Zeeb 	return (error);
14376b4cac81SBjoern A. Zeeb }
14386b4cac81SBjoern A. Zeeb 
14396b4cac81SBjoern A. Zeeb static int
14406b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
14416b4cac81SBjoern A. Zeeb {
14426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
14436b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14446b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
14456b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
14466b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
14476b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
14486b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
14496b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
14506b4cac81SBjoern A. Zeeb 	int error;
14516b4cac81SBjoern A. Zeeb 
14526b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
14536b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14546b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
14556b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
14566b4cac81SBjoern A. Zeeb 
14572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
14582ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14592ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
14602ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
14612ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
14622ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
14632ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
14642ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
14652ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
14662ac8a218SBjoern A. Zeeb #endif
14672ac8a218SBjoern A. Zeeb 
14682ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
14692ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
14702ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
14712ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
14722ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
14732ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
14746b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
14756b4cac81SBjoern A. Zeeb 
147667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
14776b4cac81SBjoern A. Zeeb 
14786b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
14798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14806b4cac81SBjoern A. Zeeb 
1481d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1482d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1483d9f59799SBjoern A. Zeeb 
14846b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
148588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
14866b4cac81SBjoern A. Zeeb 
14876b4cac81SBjoern A. Zeeb 	/* flush, no drop */
14886b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
14896b4cac81SBjoern A. Zeeb 
14906b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
14916b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
14926b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
14936b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
14946b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
14956b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
14966b4cac81SBjoern A. Zeeb 	}
14976b4cac81SBjoern A. Zeeb 
14986b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
14996b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
15006b4cac81SBjoern A. Zeeb 
15016b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
15026b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1503d9f59799SBjoern A. Zeeb 
1504d9f59799SBjoern A. Zeeb 	/* Take the station down. */
15056b4cac81SBjoern A. Zeeb 
15066b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
15076b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
15086b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1509d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1510e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
15116b4cac81SBjoern A. Zeeb 	if (error != 0) {
15126b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1513018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1514018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
15156b4cac81SBjoern A. Zeeb 		goto out;
15166b4cac81SBjoern A. Zeeb 	}
15176b4cac81SBjoern A. Zeeb #if 0
15186b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
15196b4cac81SBjoern A. Zeeb #endif
15206b4cac81SBjoern A. Zeeb 
152167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
15226b4cac81SBjoern A. Zeeb 
15232ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15242ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
15252ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
15262ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
15272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1528d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
15290936c648SBjoern A. Zeeb 	/*
15300936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
15310936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
15320936c648SBjoern A. Zeeb 	 * and potentially freed.
15330936c648SBjoern A. Zeeb 	 */
15340936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1535d9f59799SBjoern A. Zeeb 
1536d9f59799SBjoern A. Zeeb 	/* conf_tx */
1537d9f59799SBjoern A. Zeeb 
1538d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
15396b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1540c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1541d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
15426b4cac81SBjoern A. Zeeb 
1543d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
15446b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
154568541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
15466b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
15476b4cac81SBjoern A. Zeeb 
15485a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
15495a4d2461SBjoern A. Zeeb 
15506b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
1551d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1552d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1553c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
15546b4cac81SBjoern A. Zeeb 	}
15556b4cac81SBjoern A. Zeeb 
15566b4cac81SBjoern A. Zeeb out:
15578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
15586b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
15596b4cac81SBjoern A. Zeeb 	return (error);
15606b4cac81SBjoern A. Zeeb }
15616b4cac81SBjoern A. Zeeb 
15626b4cac81SBjoern A. Zeeb static int
15636b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15646b4cac81SBjoern A. Zeeb {
15656b4cac81SBjoern A. Zeeb 	int error;
15666b4cac81SBjoern A. Zeeb 
15676b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
15686b4cac81SBjoern A. Zeeb 	if (error == 0)
15696b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
15706b4cac81SBjoern A. Zeeb 	return (error);
15716b4cac81SBjoern A. Zeeb }
15726b4cac81SBjoern A. Zeeb 
15736b4cac81SBjoern A. Zeeb static int
15746b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15756b4cac81SBjoern A. Zeeb {
15766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15806b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15816b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
15826b4cac81SBjoern A. Zeeb 	int error;
15836b4cac81SBjoern A. Zeeb 
15846b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
15856b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15866b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
15876b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
15886b4cac81SBjoern A. Zeeb 
15896b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
15908ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
15912ac8a218SBjoern A. Zeeb 
15922ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
15932ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
15942ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
15952ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15962ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
15972ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
15982ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
15992ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16002ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16012ac8a218SBjoern A. Zeeb #endif
16022ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16032ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16042ac8a218SBjoern A. Zeeb 		goto out;
16052ac8a218SBjoern A. Zeeb 	}
16062ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16082ac8a218SBjoern A. Zeeb 
16092ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 	/* Finish auth. */
16126b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
16136b4cac81SBjoern A. Zeeb 
16146b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
16156b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
16166b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1617e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1618018d93ecSBjoern A. Zeeb 	if (error != 0) {
1619018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1620018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
16216b4cac81SBjoern A. Zeeb 		goto out;
1622018d93ecSBjoern A. Zeeb 	}
16236b4cac81SBjoern A. Zeeb 
16246b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
16256b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
16266b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16276b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
16286b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
16296b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
16306b4cac81SBjoern A. Zeeb 	}
16316b4cac81SBjoern A. Zeeb 
16326b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
16336b4cac81SBjoern A. Zeeb 
16346b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
16356b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
16366b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
16376b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
16386b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
16396b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
16406b4cac81SBjoern A. Zeeb 	}
16416b4cac81SBjoern A. Zeeb 
16426b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
164388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
16446b4cac81SBjoern A. Zeeb 
16456b4cac81SBjoern A. Zeeb 	/*
16466b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
16476b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
16486b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
16496b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
16506b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
16516b4cac81SBjoern A. Zeeb 	 * - event_callback
16526b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
16536b4cac81SBjoern A. Zeeb 	 */
16546b4cac81SBjoern A. Zeeb 
16556b4cac81SBjoern A. Zeeb out:
16568ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
16576b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
16586b4cac81SBjoern A. Zeeb 	return (error);
16596b4cac81SBjoern A. Zeeb }
16606b4cac81SBjoern A. Zeeb 
16616b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
16626b4cac81SBjoern A. Zeeb static int
16636b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16646b4cac81SBjoern A. Zeeb {
16656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16666b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16696b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16706b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16712ac8a218SBjoern A. Zeeb 	int error;
16726b4cac81SBjoern A. Zeeb 
16736b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16746b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16756b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16766b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16776b4cac81SBjoern A. Zeeb 
16786b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16802ac8a218SBjoern A. Zeeb 
16812ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16822ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
16832ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
16842ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16852ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16862ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16872ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16882ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16892ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
16902ac8a218SBjoern A. Zeeb #endif
16912ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
16922ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
16932ac8a218SBjoern A. Zeeb 		goto out;
16942ac8a218SBjoern A. Zeeb 	}
16952ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
16962ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16972ac8a218SBjoern A. Zeeb 
16982ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
16992ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
17006b4cac81SBjoern A. Zeeb 
17016b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
17026b4cac81SBjoern A. Zeeb 
17036b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
17046b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
17056b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17066b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
17076b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
17086b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
17096b4cac81SBjoern A. Zeeb 	}
17106b4cac81SBjoern A. Zeeb 
17116b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
17126b4cac81SBjoern A. Zeeb 
17136b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
17146b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
17156b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
17166b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
17176b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
17186b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
17196b4cac81SBjoern A. Zeeb 	}
17206b4cac81SBjoern A. Zeeb 
17212ac8a218SBjoern A. Zeeb 	error = 0;
17222ac8a218SBjoern A. Zeeb out:
17238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
17246b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
17256b4cac81SBjoern A. Zeeb 
17262ac8a218SBjoern A. Zeeb 	return (error);
17276b4cac81SBjoern A. Zeeb }
17286b4cac81SBjoern A. Zeeb 
17296b4cac81SBjoern A. Zeeb static int
1730d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17316b4cac81SBjoern A. Zeeb {
17326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17336b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17346b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
17356b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
17366b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
17376b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
17386b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
17396b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
1740d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
17416b4cac81SBjoern A. Zeeb 	int error;
17426b4cac81SBjoern A. Zeeb 
17436b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
17446b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17456b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
17466b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
17476b4cac81SBjoern A. Zeeb 
17482ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17492ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17502ac8a218SBjoern A. Zeeb 
17512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17522ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17532ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
17542ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
17552ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17562ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17572ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17582ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17592ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
17602ac8a218SBjoern A. Zeeb #endif
17612ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
17622ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17632ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
17642ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
17652ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
17662ac8a218SBjoern A. Zeeb 
17672ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
17686b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
17696b4cac81SBjoern A. Zeeb 
177067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1771d9f59799SBjoern A. Zeeb 
1772d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1773d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1774d9f59799SBjoern A. Zeeb 
1775d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1776d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1777d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
1778d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1779d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1780ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
1781d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1782d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
1783d9f59799SBjoern A. Zeeb 	}
1784d9f59799SBjoern A. Zeeb 
17858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1786d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1787d9f59799SBjoern A. Zeeb 
178888665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
1789d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
1790018d93ecSBjoern A. Zeeb 	if (error != 0) {
1791018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1792018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1793d9f59799SBjoern A. Zeeb 		goto outni;
1794018d93ecSBjoern A. Zeeb 	}
1795d9f59799SBjoern A. Zeeb 
1796d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
179788665349SBjoern A. Zeeb 
179888665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
179988665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
180088665349SBjoern A. Zeeb 
18018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
1802d9f59799SBjoern A. Zeeb 
180367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1804d9f59799SBjoern A. Zeeb 
1805d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
180688665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
1807d9f59799SBjoern A. Zeeb 
1808d9f59799SBjoern A. Zeeb 	/* flush, no drop */
1809d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1810d9f59799SBjoern A. Zeeb 
18116b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
18126b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
18136b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18146b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
1815ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
18166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
18176b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
18186b4cac81SBjoern A. Zeeb 	}
18196b4cac81SBjoern A. Zeeb 
1820d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
1821d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
1822d9f59799SBjoern A. Zeeb 
1823d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
1824d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1825d9f59799SBjoern A. Zeeb 
1826d9f59799SBjoern A. Zeeb 	/* Take the station down. */
1827d9f59799SBjoern A. Zeeb 
18286b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
18296b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18306b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
18316b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1832e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1833018d93ecSBjoern A. Zeeb 	if (error != 0) {
1834018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1835018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18366b4cac81SBjoern A. Zeeb 		goto out;
1837018d93ecSBjoern A. Zeeb 	}
18386b4cac81SBjoern A. Zeeb 
18395a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
18405a4d2461SBjoern A. Zeeb 	bss_changed = 0;
18415a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
18426b4cac81SBjoern A. Zeeb 
18435a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
184416e688b2SBjoern A. Zeeb 
1845d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1846d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1847d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1848d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1849e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1850d9f59799SBjoern A. Zeeb 	if (error != 0) {
1851d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1852018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1853018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
1854d9f59799SBjoern A. Zeeb 		goto out;
1855d9f59799SBjoern A. Zeeb 	}
1856d9f59799SBjoern A. Zeeb 
185716e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1858d9f59799SBjoern A. Zeeb 
1859d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
1860d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
1861d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
1862616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
1863616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1864d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
1865d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1866d9f59799SBjoern A. Zeeb 
18672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
18682ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
18692ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
18702ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
18712ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1872d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
18730936c648SBjoern A. Zeeb 	/*
18740936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
18750936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
18760936c648SBjoern A. Zeeb 	 * and potentially freed.
18770936c648SBjoern A. Zeeb 	 */
18780936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1879d9f59799SBjoern A. Zeeb 
1880d9f59799SBjoern A. Zeeb 	/* conf_tx */
1881d9f59799SBjoern A. Zeeb 
1882d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
1883d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1884c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1885d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
1886d9f59799SBjoern A. Zeeb 
1887d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1888d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
188968541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1890d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
1891d9f59799SBjoern A. Zeeb 
18925a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
18935a4d2461SBjoern A. Zeeb 
1894d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
1895d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1896d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1897c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
1898d9f59799SBjoern A. Zeeb 	}
1899d9f59799SBjoern A. Zeeb 
1900d9f59799SBjoern A. Zeeb 	error = EALREADY;
19016b4cac81SBjoern A. Zeeb out:
19028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19036b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1904d9f59799SBjoern A. Zeeb outni:
19056b4cac81SBjoern A. Zeeb 	return (error);
19066b4cac81SBjoern A. Zeeb }
19076b4cac81SBjoern A. Zeeb 
19086b4cac81SBjoern A. Zeeb static int
1909d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1910d9f59799SBjoern A. Zeeb {
1911d9f59799SBjoern A. Zeeb 	int error;
1912d9f59799SBjoern A. Zeeb 
1913d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1914d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
1915d9f59799SBjoern A. Zeeb 		return (error);
1916d9f59799SBjoern A. Zeeb 
1917d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
1918d9f59799SBjoern A. Zeeb 
1919d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1920d9f59799SBjoern A. Zeeb 	return (error);
1921d9f59799SBjoern A. Zeeb }
1922d9f59799SBjoern A. Zeeb 
1923d9f59799SBjoern A. Zeeb static int
19246b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19256b4cac81SBjoern A. Zeeb {
19266b4cac81SBjoern A. Zeeb 	int error;
19276b4cac81SBjoern A. Zeeb 
1928d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
19296b4cac81SBjoern A. Zeeb 	return (error);
19306b4cac81SBjoern A. Zeeb }
19316b4cac81SBjoern A. Zeeb 
19326b4cac81SBjoern A. Zeeb static int
19336b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19346b4cac81SBjoern A. Zeeb {
19356b4cac81SBjoern A. Zeeb 	int error;
19366b4cac81SBjoern A. Zeeb 
1937d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
19386b4cac81SBjoern A. Zeeb 	return (error);
19396b4cac81SBjoern A. Zeeb }
19406b4cac81SBjoern A. Zeeb 
19416b4cac81SBjoern A. Zeeb static int
19426b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19436b4cac81SBjoern A. Zeeb {
19446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19456b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19466b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19476b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19486b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19496b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19506b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19516b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19526b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
19536b4cac81SBjoern A. Zeeb 	int error;
19546b4cac81SBjoern A. Zeeb 
19556b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19566b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19576b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19586b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19596b4cac81SBjoern A. Zeeb 
19606b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
19618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19622ac8a218SBjoern A. Zeeb 
19632ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19642ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19652ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
19662ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19672ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19682ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19692ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19702ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19712ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19722ac8a218SBjoern A. Zeeb #endif
19732ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
19742ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
19752ac8a218SBjoern A. Zeeb 		goto out;
19762ac8a218SBjoern A. Zeeb 	}
19772ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19782ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19792ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19802ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19812ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19822ac8a218SBjoern A. Zeeb 
19832ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
19846b4cac81SBjoern A. Zeeb 
19856b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
19866b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
19876b4cac81SBjoern A. Zeeb 
19889597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
19899597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
19909597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
19919597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
19926b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19939597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
19944a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
19954a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
19964a67f1dfSBjoern A. Zeeb 		sta->wme = true;
19974a67f1dfSBjoern A. Zeeb #endif
1998e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1999018d93ecSBjoern A. Zeeb 	if (error != 0) {
2000018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2001018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20029597f7cbSBjoern A. Zeeb 		goto out;
2003018d93ecSBjoern A. Zeeb 	}
20046b4cac81SBjoern A. Zeeb 
20056b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
20066b4cac81SBjoern A. Zeeb 
20076b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
20086b4cac81SBjoern A. Zeeb 	bss_changed = 0;
20094a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
20104a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
20114a67f1dfSBjoern A. Zeeb #endif
2012616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2013616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2014616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
20156b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
20166b4cac81SBjoern A. Zeeb 	}
20176b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2018616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2019616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
20206b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
20216b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
20226b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
20236b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
20246b4cac81SBjoern A. Zeeb 	}
20256b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
20266b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
20276b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
20286b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
20296b4cac81SBjoern A. Zeeb 	}
20306b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
20316b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
20326b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
20336b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
20346b4cac81SBjoern A. Zeeb 	}
2035fa8f007dSBjoern A. Zeeb 
2036fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2037fa8f007dSBjoern A. Zeeb 
20386b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
20396b4cac81SBjoern A. Zeeb 
20406b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
20416b4cac81SBjoern A. Zeeb 	 * - event_callback
20426b4cac81SBjoern A. Zeeb 	 */
20436b4cac81SBjoern A. Zeeb 
20446b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20456b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20466b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20476b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
20486b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20496b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20506b4cac81SBjoern A. Zeeb 	}
20516b4cac81SBjoern A. Zeeb 
2052086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2053086be6a8SBjoern A. Zeeb 
20546b4cac81SBjoern A. Zeeb 	/*
20556b4cac81SBjoern A. Zeeb 	 * And then:
20566b4cac81SBjoern A. Zeeb 	 * - (more packets)?
20576b4cac81SBjoern A. Zeeb 	 * - set_key
20586b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
20596b4cac81SBjoern A. Zeeb 	 * - set_key (?)
20606b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
20616b4cac81SBjoern A. Zeeb 	 */
20626b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
20636b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
20646b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
20656b4cac81SBjoern A. Zeeb 
20666b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
20676b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
20686b4cac81SBjoern A. Zeeb 	}
20696b4cac81SBjoern A. Zeeb 
20709fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
20719fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
20729fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
20739fb91463SBjoern A. Zeeb #endif
20749fb91463SBjoern A. Zeeb 
20756b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
20766b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20776b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
20786b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2079e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
20806b4cac81SBjoern A. Zeeb 	if (error != 0) {
20816b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2082018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2083018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20846b4cac81SBjoern A. Zeeb 		goto out;
20856b4cac81SBjoern A. Zeeb 	}
20866b4cac81SBjoern A. Zeeb 
20876b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
20886b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
20896b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
20906b4cac81SBjoern A. Zeeb 	 *
20916b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
20926b4cac81SBjoern A. Zeeb 	 */
20936b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
20946b4cac81SBjoern A. Zeeb 
2095fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2096fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2097fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2098fa8f007dSBjoern A. Zeeb 
20996b4cac81SBjoern A. Zeeb out:
21008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21026b4cac81SBjoern A. Zeeb 	return (error);
21036b4cac81SBjoern A. Zeeb }
21046b4cac81SBjoern A. Zeeb 
21056b4cac81SBjoern A. Zeeb static int
21066b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21076b4cac81SBjoern A. Zeeb {
21086b4cac81SBjoern A. Zeeb 	int error;
21096b4cac81SBjoern A. Zeeb 
21106b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
21116b4cac81SBjoern A. Zeeb 	if (error == 0)
21126b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
21136b4cac81SBjoern A. Zeeb 	return (error);
21146b4cac81SBjoern A. Zeeb }
21156b4cac81SBjoern A. Zeeb 
21166b4cac81SBjoern A. Zeeb static int
21176b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21186b4cac81SBjoern A. Zeeb {
21196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21206b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21216b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21226b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21236b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
21246b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21256b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2126d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2127d9f59799SBjoern A. Zeeb #if 0
2128d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2129d9f59799SBjoern A. Zeeb #endif
21306b4cac81SBjoern A. Zeeb 	int error;
21316b4cac81SBjoern A. Zeeb 
21326b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21336b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21346b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21356b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21366b4cac81SBjoern A. Zeeb 
21372ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21382ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21392ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
21402ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
21412ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21422ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21432ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21442ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21452ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21462ac8a218SBjoern A. Zeeb #endif
21472ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21482ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21492ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
21502ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
21512ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
21522ac8a218SBjoern A. Zeeb 
21532ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
21546b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
21556b4cac81SBjoern A. Zeeb 
215667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2157d9f59799SBjoern A. Zeeb 
2158d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2160d9f59799SBjoern A. Zeeb 
2161d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2162d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2163d9f59799SBjoern A. Zeeb 
2164d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2165d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2166d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2167d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2168d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2169ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2170d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2171d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2172d9f59799SBjoern A. Zeeb 	}
2173d9f59799SBjoern A. Zeeb 
21748ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2175d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2176d9f59799SBjoern A. Zeeb 
2177d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2178d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2179018d93ecSBjoern A. Zeeb 	if (error != 0) {
2180018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2181018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2182d9f59799SBjoern A. Zeeb 		goto outni;
2183018d93ecSBjoern A. Zeeb 	}
2184d9f59799SBjoern A. Zeeb 
2185d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
218688665349SBjoern A. Zeeb 
218788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
218888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
218988665349SBjoern A. Zeeb 
21908ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2191d9f59799SBjoern A. Zeeb 
219267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2193d9f59799SBjoern A. Zeeb 
2194d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
219588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2196d9f59799SBjoern A. Zeeb 
2197d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2198d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2199d9f59799SBjoern A. Zeeb 
2200d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2201d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2202d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2203d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2204ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2205d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2206d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2207d9f59799SBjoern A. Zeeb 	}
2208d9f59799SBjoern A. Zeeb 
2209d9f59799SBjoern A. Zeeb #if 0
2210d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2211d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2212d9f59799SBjoern A. Zeeb 
2213d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2214d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2215d9f59799SBjoern A. Zeeb #endif
2216d9f59799SBjoern A. Zeeb 
2217d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2218d9f59799SBjoern A. Zeeb 
22196b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
22206b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22216b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
22226b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2223e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2224018d93ecSBjoern A. Zeeb 	if (error != 0) {
2225018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2226018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22276b4cac81SBjoern A. Zeeb 		goto out;
2228018d93ecSBjoern A. Zeeb 	}
22296b4cac81SBjoern A. Zeeb 
223067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22316b4cac81SBjoern A. Zeeb 
22326b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
22336b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22346b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
22356b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2236e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2237018d93ecSBjoern A. Zeeb 	if (error != 0) {
2238018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2239018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22406b4cac81SBjoern A. Zeeb 		goto out;
2241018d93ecSBjoern A. Zeeb 	}
22426b4cac81SBjoern A. Zeeb 
224367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22446b4cac81SBjoern A. Zeeb 
2245d9f59799SBjoern A. Zeeb #if 0
2246d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2247d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2248d9f59799SBjoern A. Zeeb #endif
2249d9f59799SBjoern A. Zeeb 
2250d9f59799SBjoern A. Zeeb 	error = EALREADY;
22516b4cac81SBjoern A. Zeeb out:
22528ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22536b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2254d9f59799SBjoern A. Zeeb outni:
22556b4cac81SBjoern A. Zeeb 	return (error);
22566b4cac81SBjoern A. Zeeb }
22576b4cac81SBjoern A. Zeeb 
22586b4cac81SBjoern A. Zeeb static int
2259d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2260d9f59799SBjoern A. Zeeb {
2261d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2262d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2263d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2264d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2265d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2266d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2267d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2268d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2269d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2270d9f59799SBjoern A. Zeeb 	int error;
2271d9f59799SBjoern A. Zeeb 
2272d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2273d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2274d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2275d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2276d9f59799SBjoern A. Zeeb 
22772ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22782ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22792ac8a218SBjoern A. Zeeb 
22802ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22812ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22822ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22832ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22842ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22852ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22862ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22872ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22882ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22892ac8a218SBjoern A. Zeeb #endif
22902ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22912ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22922ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22932ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22942ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22952ac8a218SBjoern A. Zeeb 
22962ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2297d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2298d9f59799SBjoern A. Zeeb 
229967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2300d9f59799SBjoern A. Zeeb 
2301d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2302d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2303d9f59799SBjoern A. Zeeb 
2304d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2305d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2306d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2307d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2308d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2309ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2310d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2311d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2312d9f59799SBjoern A. Zeeb 	}
2313d9f59799SBjoern A. Zeeb 
23148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2315d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2316d9f59799SBjoern A. Zeeb 
2317d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2318d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2319018d93ecSBjoern A. Zeeb 	if (error != 0) {
2320018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2321018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2322d9f59799SBjoern A. Zeeb 		goto outni;
2323018d93ecSBjoern A. Zeeb 	}
2324d9f59799SBjoern A. Zeeb 
2325d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
232688665349SBjoern A. Zeeb 
232788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
232888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
232988665349SBjoern A. Zeeb 
23308ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2331d9f59799SBjoern A. Zeeb 
233267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2333d9f59799SBjoern A. Zeeb 
2334d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
233588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2336d9f59799SBjoern A. Zeeb 
2337d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2338d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2339d9f59799SBjoern A. Zeeb 
2340d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2341d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2342d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2343d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2344ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2345d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2346d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2347d9f59799SBjoern A. Zeeb 	}
2348d9f59799SBjoern A. Zeeb 
2349d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2350d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2351d9f59799SBjoern A. Zeeb 
2352d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2353d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2354d9f59799SBjoern A. Zeeb 
2355d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2356d9f59799SBjoern A. Zeeb 
2357d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2358d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2359d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2360d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2361e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2362018d93ecSBjoern A. Zeeb 	if (error != 0) {
2363018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2364018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2365d9f59799SBjoern A. Zeeb 		goto out;
2366018d93ecSBjoern A. Zeeb 	}
2367d9f59799SBjoern A. Zeeb 
236867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2369d9f59799SBjoern A. Zeeb 
2370d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2371d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2372d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2373d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2374e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2375018d93ecSBjoern A. Zeeb 	if (error != 0) {
2376018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2377018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2378d9f59799SBjoern A. Zeeb 		goto out;
2379018d93ecSBjoern A. Zeeb 	}
2380d9f59799SBjoern A. Zeeb 
238167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2382d9f59799SBjoern A. Zeeb 
2383d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2384d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2385d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2386d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2387e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2388018d93ecSBjoern A. Zeeb 	if (error != 0) {
2389018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2390018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2391d9f59799SBjoern A. Zeeb 		goto out;
2392018d93ecSBjoern A. Zeeb 	}
2393d9f59799SBjoern A. Zeeb 
23945a4d2461SBjoern A. Zeeb 	bss_changed = 0;
239516e688b2SBjoern A. Zeeb 	/*
23965a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
23975a4d2461SBjoern A. Zeeb 	 *
239816e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
23995a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
24005a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
24015a4d2461SBjoern A. Zeeb 	 *
24025a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
24035a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
24045a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
24055a4d2461SBjoern A. Zeeb 	 * a fw assert.
24065a4d2461SBjoern A. Zeeb 	 *
24075a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
24085a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
24095a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
24105a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
24115a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
24125a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
24135a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
24145a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
24155a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
24165a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
241716e688b2SBjoern A. Zeeb 	 */
24185a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
24195a4d2461SBjoern A. Zeeb 
24205a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
242116e688b2SBjoern A. Zeeb 
2422d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2423d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2424d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2425d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2426e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2427d9f59799SBjoern A. Zeeb 	if (error != 0) {
2428d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2429018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2430018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2431d9f59799SBjoern A. Zeeb 		goto out;
2432d9f59799SBjoern A. Zeeb 	}
2433d9f59799SBjoern A. Zeeb 
24345a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24355a4d2461SBjoern A. Zeeb 
243616e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2437d9f59799SBjoern A. Zeeb 
2438d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2439d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2440d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2441616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2442616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2443d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
24445a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
24455a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
24465a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2447d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2448d9f59799SBjoern A. Zeeb 
24492ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24502ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24512ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24522ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24532ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24540936c648SBjoern A. Zeeb 	/*
24550936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24560936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24570936c648SBjoern A. Zeeb 	 * and potentially freed.
24580936c648SBjoern A. Zeeb 	 */
24590936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2460d9f59799SBjoern A. Zeeb 
2461d9f59799SBjoern A. Zeeb 	/* conf_tx */
2462d9f59799SBjoern A. Zeeb 
2463d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2464d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2465c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2466d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2467d9f59799SBjoern A. Zeeb 
2468d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2469d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
247068541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2471d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2472d9f59799SBjoern A. Zeeb 
24735a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24745a4d2461SBjoern A. Zeeb 
2475d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2476d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2477d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2478c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2479d9f59799SBjoern A. Zeeb 	}
2480d9f59799SBjoern A. Zeeb 
2481d9f59799SBjoern A. Zeeb 	error = EALREADY;
2482d9f59799SBjoern A. Zeeb out:
24838ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2484d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2485d9f59799SBjoern A. Zeeb outni:
2486d9f59799SBjoern A. Zeeb 	return (error);
2487d9f59799SBjoern A. Zeeb }
2488d9f59799SBjoern A. Zeeb 
2489d9f59799SBjoern A. Zeeb static int
2490d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2491d9f59799SBjoern A. Zeeb {
2492d9f59799SBjoern A. Zeeb 
2493d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2494d9f59799SBjoern A. Zeeb }
2495d9f59799SBjoern A. Zeeb 
2496d9f59799SBjoern A. Zeeb static int
24976b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24986b4cac81SBjoern A. Zeeb {
24996b4cac81SBjoern A. Zeeb 	int error;
25006b4cac81SBjoern A. Zeeb 
2501d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2502d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2503d9f59799SBjoern A. Zeeb 		return (error);
2504d9f59799SBjoern A. Zeeb 
2505d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2506d9f59799SBjoern A. Zeeb 
2507d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
25086b4cac81SBjoern A. Zeeb 	return (error);
25096b4cac81SBjoern A. Zeeb }
25106b4cac81SBjoern A. Zeeb 
2511d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
25126b4cac81SBjoern A. Zeeb 
25136b4cac81SBjoern A. Zeeb /*
25146b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
25156b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
25166b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
25176b4cac81SBjoern A. Zeeb  */
25186b4cac81SBjoern A. Zeeb struct fsm_state {
25196b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
25206b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
25216b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
25226b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
25236b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
25246b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
25256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
25266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2527d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2528d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
25296b4cac81SBjoern A. Zeeb 
25306b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
25316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
25326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
25336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2534d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
25356b4cac81SBjoern A. Zeeb 
2536d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2537d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2538d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2539d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2540d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
25416b4cac81SBjoern A. Zeeb 
2542d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2543d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2544d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
25456b4cac81SBjoern A. Zeeb 
25466b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
25476b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
25486b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
25496b4cac81SBjoern A. Zeeb 
25506b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
25516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
25526b4cac81SBjoern A. Zeeb };
25536b4cac81SBjoern A. Zeeb 
25546b4cac81SBjoern A. Zeeb static int
25556b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25566b4cac81SBjoern A. Zeeb {
25576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
25586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25606b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25616b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
25626b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
25636b4cac81SBjoern A. Zeeb 	int error;
25646b4cac81SBjoern A. Zeeb 
25656b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
25666b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
25676b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
25686b4cac81SBjoern A. Zeeb 
25699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
25716b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
25726b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
25739d9ba2b7SBjoern A. Zeeb #endif
25746b4cac81SBjoern A. Zeeb 
25756b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
25766b4cac81SBjoern A. Zeeb 
25776b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
25786b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
25796b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
25806b4cac81SBjoern A. Zeeb 
25816b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
25826b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
25836b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
25846b4cac81SBjoern A. Zeeb 
25856b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
25866b4cac81SBjoern A. Zeeb 
25876b4cac81SBjoern A. Zeeb 	} else {
25886b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
25896b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
25906b4cac81SBjoern A. Zeeb 		return (ENOSYS);
25916b4cac81SBjoern A. Zeeb 	}
25926b4cac81SBjoern A. Zeeb 
25936b4cac81SBjoern A. Zeeb 	error = 0;
25946b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
25956b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
25969d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25979d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
2598d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2599d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
2600d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
2601d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
26029d9ba2b7SBjoern A. Zeeb #endif
26036b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
26046b4cac81SBjoern A. Zeeb 			break;
26056b4cac81SBjoern A. Zeeb 		}
26066b4cac81SBjoern A. Zeeb 	}
26076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
26086b4cac81SBjoern A. Zeeb 
26096b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
2610d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
26116b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
26126b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
26136b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
26146b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
26156b4cac81SBjoern A. Zeeb 		return (ENOSYS);
26166b4cac81SBjoern A. Zeeb 	}
26176b4cac81SBjoern A. Zeeb 
26186b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
26199d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26209d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
2621d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2622d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
2623d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
2624d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
26259d9ba2b7SBjoern A. Zeeb #endif
26266b4cac81SBjoern A. Zeeb 		return (0);
26276b4cac81SBjoern A. Zeeb 	}
26286b4cac81SBjoern A. Zeeb 
26296b4cac81SBjoern A. Zeeb 	if (error != 0) {
26306b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
26316b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
26326b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
26336b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
263445c27ad5SBjoern A. Zeeb 		return (error);
26356b4cac81SBjoern A. Zeeb 	}
26366b4cac81SBjoern A. Zeeb 
26379d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26389d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
2639d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2640d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
26416b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
26429d9ba2b7SBjoern A. Zeeb #endif
26436b4cac81SBjoern A. Zeeb 
26446b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
26456b4cac81SBjoern A. Zeeb }
26466b4cac81SBjoern A. Zeeb 
26476b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
26486b4cac81SBjoern A. Zeeb 
2649d9f59799SBjoern A. Zeeb /*
2650d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2651d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2652d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
2653d9f59799SBjoern A. Zeeb  */
2654d9f59799SBjoern A. Zeeb static struct ieee80211_node *
2655d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2656d9f59799SBjoern A. Zeeb {
2657d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26582ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
2659d9f59799SBjoern A. Zeeb 
26602ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2661d9f59799SBjoern A. Zeeb 
2662ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26632ac8a218SBjoern A. Zeeb 
26642ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26652ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26672ac8a218SBjoern A. Zeeb 
26682ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
26692ac8a218SBjoern A. Zeeb 	return (rni);
2670d9f59799SBjoern A. Zeeb }
2671d9f59799SBjoern A. Zeeb 
26724a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
26736b4cac81SBjoern A. Zeeb static int
26744a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
26756b4cac81SBjoern A. Zeeb {
26764a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
26776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26806b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
26816b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
26826b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
26836b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
26846b4cac81SBjoern A. Zeeb 	int error;
26856b4cac81SBjoern A. Zeeb 	uint16_t ac;
26866b4cac81SBjoern A. Zeeb 
26876b4cac81SBjoern A. Zeeb 	IMPROVE();
26886b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
26896b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
26906b4cac81SBjoern A. Zeeb 
26916b4cac81SBjoern A. Zeeb 	if (vap == NULL)
26926b4cac81SBjoern A. Zeeb 		return (0);
26936b4cac81SBjoern A. Zeeb 
26944a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
26954a67f1dfSBjoern A. Zeeb 		return (0);
26964a67f1dfSBjoern A. Zeeb 
26976b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
26986b4cac81SBjoern A. Zeeb 		return (0);
26996b4cac81SBjoern A. Zeeb 
27004a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
27014a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
27024a67f1dfSBjoern A. Zeeb 		return (0);
27034a67f1dfSBjoern A. Zeeb 	}
27044a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
27056b4cac81SBjoern A. Zeeb 
27064a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
27076b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
27086b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
27096b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
27106b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
27116b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
27126b4cac81SBjoern A. Zeeb 
27134a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27144a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27154a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27164a67f1dfSBjoern A. Zeeb 
27176b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
27186b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
27196b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
27206b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
27216b4cac81SBjoern A. Zeeb 
27226b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
27236b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
27246b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
27256b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
27266b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
27276b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
272868541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
27296b4cac81SBjoern A. Zeeb 		if (error != 0)
27303f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
27316b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
27326b4cac81SBjoern A. Zeeb 	}
27336b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27346b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
27354a67f1dfSBjoern A. Zeeb 	if (!planned)
27366b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
27374a67f1dfSBjoern A. Zeeb 
27384a67f1dfSBjoern A. Zeeb 	return (changed);
27394a67f1dfSBjoern A. Zeeb }
27406b4cac81SBjoern A. Zeeb #endif
27416b4cac81SBjoern A. Zeeb 
27424a67f1dfSBjoern A. Zeeb static int
27434a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
27444a67f1dfSBjoern A. Zeeb {
27454a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27464a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
27474a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
27484a67f1dfSBjoern A. Zeeb 
27494a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
27504a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
27514a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
27526b4cac81SBjoern A. Zeeb 		return (0);
27534a67f1dfSBjoern A. Zeeb 
27544a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
27554a67f1dfSBjoern A. Zeeb 
27564a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
27574a67f1dfSBjoern A. Zeeb #endif
27584a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
27596b4cac81SBjoern A. Zeeb }
27606b4cac81SBjoern A. Zeeb 
27614aff4048SBjoern A. Zeeb /*
27624aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
27634aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
27644aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
27654aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
27664aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
27674aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
2768edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
2769edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
2770edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
2771edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
27724aff4048SBjoern A. Zeeb  */
27734aff4048SBjoern A. Zeeb static void
27744aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
27754aff4048SBjoern A. Zeeb {
27764aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
27774aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27784aff4048SBjoern A. Zeeb 
27794aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
2780edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
2781edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
2782edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
27834aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
27844aff4048SBjoern A. Zeeb 		return;
27854aff4048SBjoern A. Zeeb 	}
27864aff4048SBjoern A. Zeeb 
27874aff4048SBjoern A. Zeeb 	vif = arg;
278857609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
27894aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
27904aff4048SBjoern A. Zeeb }
27914aff4048SBjoern A. Zeeb 
27926b4cac81SBjoern A. Zeeb static struct ieee80211vap *
27936b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
27946b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
27956b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
27966b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
27976b4cac81SBjoern A. Zeeb {
27986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27996b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28006b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28016b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
28026b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2803a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
28046b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
28056b4cac81SBjoern A. Zeeb 	size_t len;
2806e3a0b120SBjoern A. Zeeb 	int error, i;
2807a6042e17SBjoern A. Zeeb 	uint16_t ac;
28086b4cac81SBjoern A. Zeeb 
28096b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
28106b4cac81SBjoern A. Zeeb 		return (NULL);
28116b4cac81SBjoern A. Zeeb 
28126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
28136b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28146b4cac81SBjoern A. Zeeb 
28156b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
28166b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
28176b4cac81SBjoern A. Zeeb 
28186b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
28196b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
28206b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lvif->lsta_head);
28212ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
28222ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
28236b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
28246b4cac81SBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28266b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
28276b4cac81SBjoern A. Zeeb 	vif->p2p = false;
28286b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
28296b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
28306b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
28316b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
28326b4cac81SBjoern A. Zeeb 	IMPROVE();
28336b4cac81SBjoern A. Zeeb 
28346b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
28356b4cac81SBjoern A. Zeeb #if 1
28366b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
2837adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
28386ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
28396ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
28404aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
28414aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
28426ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
28437b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
28446b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
28456b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
28466b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
28476b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
28486b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2849616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
2850616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
2851616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
2852616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
28536ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2854caaa79c3SBjoern A. Zeeb 	/*
2855caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
2856caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
2857caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
2858caaa79c3SBjoern A. Zeeb 	 */
28596ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
28606b4cac81SBjoern A. Zeeb #endif
28616b4cac81SBjoern A. Zeeb #if 0
28626b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
28636b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
28646b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
28656b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
28666b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
28676b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
28686b4cac81SBjoern A. Zeeb #endif
2869e3a0b120SBjoern A. Zeeb 
28706ffb7bd4SBjoern A. Zeeb 	/* Link Config */
28716ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
28726ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
28736ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
28746ffb7bd4SBjoern A. Zeeb 	}
28756ffb7bd4SBjoern A. Zeeb 
2876e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
2877e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2878e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2879e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2880e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
2881e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
2882e3a0b120SBjoern A. Zeeb 		else
2883e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
28840cbcfa19SBjoern A. Zeeb 
28850cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
28860cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
2887e3a0b120SBjoern A. Zeeb 	}
2888e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2889e3a0b120SBjoern A. Zeeb 
28906b4cac81SBjoern A. Zeeb 	IMPROVE();
28916b4cac81SBjoern A. Zeeb 
28926b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
28936b4cac81SBjoern A. Zeeb 	if (error != 0) {
28943f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
28956b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
28966b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
28976b4cac81SBjoern A. Zeeb 		return (NULL);
28986b4cac81SBjoern A. Zeeb 	}
28996b4cac81SBjoern A. Zeeb 
29006b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
29016b4cac81SBjoern A. Zeeb 	if (error != 0) {
29026b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
29033f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
29046b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
29056b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
29066b4cac81SBjoern A. Zeeb 		return (NULL);
29076b4cac81SBjoern A. Zeeb 	}
29086b4cac81SBjoern A. Zeeb 
29098891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
29106b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
29118891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
29126b4cac81SBjoern A. Zeeb 
29136b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
29146b4cac81SBjoern A. Zeeb 	changed = 0;
29156b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
29166b4cac81SBjoern A. Zeeb 
2917a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2918a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2919a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2920a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2921a6042e17SBjoern A. Zeeb 
2922a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
2923a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
2924a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
2925a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
2926a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
2927a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2928a6042e17SBjoern A. Zeeb 		if (error != 0)
2929a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2930a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
2931a6042e17SBjoern A. Zeeb 	}
2932a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2933a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
2934a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
29356b4cac81SBjoern A. Zeeb 
29366b4cac81SBjoern A. Zeeb 	/* Force MC init. */
29376b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
29386b4cac81SBjoern A. Zeeb 
29396b4cac81SBjoern A. Zeeb 	IMPROVE();
29406b4cac81SBjoern A. Zeeb 
29416b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
29426b4cac81SBjoern A. Zeeb 
29436b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
29446b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
29456b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
2946d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
2947d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
29486b4cac81SBjoern A. Zeeb 
29496b4cac81SBjoern A. Zeeb 	/* Key management. */
29506b4cac81SBjoern A. Zeeb 	if (lhw->ops->set_key != NULL) {
2951b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
29526b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
29536b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
29546b4cac81SBjoern A. Zeeb #endif
29556b4cac81SBjoern A. Zeeb 	}
29566b4cac81SBjoern A. Zeeb 
29579fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
29589fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
29599fb91463SBjoern A. Zeeb #endif
29609fb91463SBjoern A. Zeeb 
29616b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
29626b4cac81SBjoern A. Zeeb 
29636b4cac81SBjoern A. Zeeb 	/* Complete setup. */
29646b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
29656b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
29666b4cac81SBjoern A. Zeeb 
29676b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
29686b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
29696b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
29706b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
29716b4cac81SBjoern A. Zeeb 
29726b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
29736b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
29746b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
29756b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
29766b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
29776b4cac81SBjoern A. Zeeb 	/* any others? */
29786b4cac81SBjoern A. Zeeb 	IMPROVE();
29796b4cac81SBjoern A. Zeeb 
29806b4cac81SBjoern A. Zeeb 	return (vap);
29816b4cac81SBjoern A. Zeeb }
29826b4cac81SBjoern A. Zeeb 
29834b0af114SBjoern A. Zeeb void
29844b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
29854b0af114SBjoern A. Zeeb {
29864b0af114SBjoern A. Zeeb 
29874b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
29884b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
29894b0af114SBjoern A. Zeeb 
29904b0af114SBjoern A. Zeeb 	IMPROVE();
29914b0af114SBjoern A. Zeeb }
29924b0af114SBjoern A. Zeeb 
29934b0af114SBjoern A. Zeeb void
29944b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
29954b0af114SBjoern A. Zeeb {
29964b0af114SBjoern A. Zeeb 
29974b0af114SBjoern A. Zeeb 	TODO();
29984b0af114SBjoern A. Zeeb }
29994b0af114SBjoern A. Zeeb 
30006b4cac81SBjoern A. Zeeb static void
30016b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
30026b4cac81SBjoern A. Zeeb {
30036b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30056b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30066b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30076b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30086b4cac81SBjoern A. Zeeb 
30096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
30106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
30116b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
30126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30136b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30146b4cac81SBjoern A. Zeeb 
30154aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
30164aff4048SBjoern A. Zeeb 
30178891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
30186b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
30198891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
30206b4cac81SBjoern A. Zeeb 
30216b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
30226b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3023dbf76919SBjoern A. Zeeb 
3024dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3025dbf76919SBjoern A. Zeeb 
3026dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3027dbf76919SBjoern A. Zeeb 
30286c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
30297b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
30306c38c6b1SBjoern A. Zeeb 
30316b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
30326b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
30336b4cac81SBjoern A. Zeeb }
30346b4cac81SBjoern A. Zeeb 
30356b4cac81SBjoern A. Zeeb static void
30366b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
30376b4cac81SBjoern A. Zeeb {
30386b4cac81SBjoern A. Zeeb 
30396b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
30406b4cac81SBjoern A. Zeeb 	TRACEOK();
30416b4cac81SBjoern A. Zeeb }
30426b4cac81SBjoern A. Zeeb 
30436b4cac81SBjoern A. Zeeb static void
30446b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
30456b4cac81SBjoern A. Zeeb {
30466b4cac81SBjoern A. Zeeb 
30476b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30486b4cac81SBjoern A. Zeeb }
30496b4cac81SBjoern A. Zeeb 
30506b4cac81SBjoern A. Zeeb static void
30516b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
30526b4cac81SBjoern A. Zeeb {
30536b4cac81SBjoern A. Zeeb 
30546b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
30556b4cac81SBjoern A. Zeeb }
30566b4cac81SBjoern A. Zeeb 
30576b4cac81SBjoern A. Zeeb /* Start / stop device. */
30586b4cac81SBjoern A. Zeeb static void
30596b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
30606b4cac81SBjoern A. Zeeb {
30616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30628d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30636b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
30646b4cac81SBjoern A. Zeeb 	int error;
30658d58a057SBjoern A. Zeeb #endif
30666b4cac81SBjoern A. Zeeb 	bool start_all;
30676b4cac81SBjoern A. Zeeb 
30686b4cac81SBjoern A. Zeeb 	IMPROVE();
30696b4cac81SBjoern A. Zeeb 
30706b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
30718d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30738d58a057SBjoern A. Zeeb #endif
30746b4cac81SBjoern A. Zeeb 	start_all = false;
30756b4cac81SBjoern A. Zeeb 
30768ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
30778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
30786b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
30798d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30806b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
30816b4cac81SBjoern A. Zeeb 		if (error == 0)
30828d58a057SBjoern A. Zeeb #endif
30836b4cac81SBjoern A. Zeeb 			start_all = true;
30846b4cac81SBjoern A. Zeeb 	} else {
30858d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
30867b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
30878d58a057SBjoern A. Zeeb #endif
30886b4cac81SBjoern A. Zeeb 	}
30898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
30908ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
30916b4cac81SBjoern A. Zeeb 
30926b4cac81SBjoern A. Zeeb 	if (start_all)
30936b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
30946b4cac81SBjoern A. Zeeb }
30956b4cac81SBjoern A. Zeeb 
30966b4cac81SBjoern A. Zeeb bool
30976b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
30986b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
30996b4cac81SBjoern A. Zeeb {
31006b4cac81SBjoern A. Zeeb 	int i;
31016b4cac81SBjoern A. Zeeb 
31026b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
31036b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
31046b4cac81SBjoern A. Zeeb 			return (true);
31056b4cac81SBjoern A. Zeeb 	}
31066b4cac81SBjoern A. Zeeb 
31076b4cac81SBjoern A. Zeeb 	return (false);
31086b4cac81SBjoern A. Zeeb }
31096b4cac81SBjoern A. Zeeb 
31106b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
31116b4cac81SBjoern A. Zeeb bool
31126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
31136b4cac81SBjoern A. Zeeb {
31146b4cac81SBjoern A. Zeeb 	size_t x;
31156b4cac81SBjoern A. Zeeb 	uint8_t l;
31166b4cac81SBjoern A. Zeeb 
31176b4cac81SBjoern A. Zeeb 	x = *xp;
31186b4cac81SBjoern A. Zeeb 
31196b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
31206b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
31216b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
31226b4cac81SBjoern A. Zeeb 	x += 2 + l;
31236b4cac81SBjoern A. Zeeb 
31246b4cac81SBjoern A. Zeeb 	if (x > ies_len)
31256b4cac81SBjoern A. Zeeb 		return (false);
31266b4cac81SBjoern A. Zeeb 
31276b4cac81SBjoern A. Zeeb 	*xp = x;
31286b4cac81SBjoern A. Zeeb 	return (true);
31296b4cac81SBjoern A. Zeeb }
31306b4cac81SBjoern A. Zeeb 
3131d9945d78SBjoern A. Zeeb static uint8_t *
3132d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3133d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
31346b4cac81SBjoern A. Zeeb {
3135d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3136d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
31373dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3138d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3139d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3140d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3141d9945d78SBjoern A. Zeeb 	int band, i;
31426b4cac81SBjoern A. Zeeb 
31433dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3144d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3145d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3146d9945d78SBjoern A. Zeeb 			continue;
3147d9945d78SBjoern A. Zeeb 
3148d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3149d9945d78SBjoern A. Zeeb 		/*
3150d9945d78SBjoern A. Zeeb 		 * This should not happen;
3151d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3152d9945d78SBjoern A. Zeeb 		 */
3153d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3154d9945d78SBjoern A. Zeeb 			continue;
3155d9945d78SBjoern A. Zeeb 
3156d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3157d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3158d9945d78SBjoern A. Zeeb 		chan = NULL;
3159d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3160d9945d78SBjoern A. Zeeb 
3161d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3162d9945d78SBjoern A. Zeeb 				continue;
3163d9945d78SBjoern A. Zeeb 
31643dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3165d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3166d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3167d9945d78SBjoern A. Zeeb 				break;
3168d9945d78SBjoern A. Zeeb 		}
3169d9945d78SBjoern A. Zeeb 
3170d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3171d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3172d9945d78SBjoern A. Zeeb 			continue;
3173d9945d78SBjoern A. Zeeb 
3174d9945d78SBjoern A. Zeeb 		pb = p;
31753dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3176d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3177d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3178d9945d78SBjoern A. Zeeb 
31793dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
31803dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
31813dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
31823dd98026SBjoern A. Zeeb 
31833dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
31843dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
31853dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
31863dd98026SBjoern A. Zeeb 		}
31873dd98026SBjoern A. Zeeb #endif
31883dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
31893dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
31903dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
31913dd98026SBjoern A. Zeeb 
31923dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
31933dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
31943dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
31953dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
31963dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
31973dd98026SBjoern A. Zeeb 		}
31983dd98026SBjoern A. Zeeb #endif
31993dd98026SBjoern A. Zeeb 
3200d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3201d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3202d9945d78SBjoern A. Zeeb 	}
3203d9945d78SBjoern A. Zeeb 
3204d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3205d9945d78SBjoern A. Zeeb 	pb = p;
3206d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3207d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3208d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3209d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3210d9945d78SBjoern A. Zeeb 	}
3211d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3212d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3213d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3214d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3215d9945d78SBjoern A. Zeeb 	}
3216d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3217d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3218d9945d78SBjoern A. Zeeb 
3219d9945d78SBjoern A. Zeeb 	return (p);
32206b4cac81SBjoern A. Zeeb }
32216b4cac81SBjoern A. Zeeb 
32226b4cac81SBjoern A. Zeeb static void
32236b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
32246b4cac81SBjoern A. Zeeb {
32256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32296b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
32306b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
32316b4cac81SBjoern A. Zeeb 	int error;
32328ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
32336b4cac81SBjoern A. Zeeb 
32346b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3236a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
32376b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
32388ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32396b4cac81SBjoern A. Zeeb 		return;
32406b4cac81SBjoern A. Zeeb 	}
32418ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
32428ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
32436b4cac81SBjoern A. Zeeb 
32446b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
32456b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
32466b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3247d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
32486b4cac81SBjoern A. Zeeb 		return;
32496b4cac81SBjoern A. Zeeb 	}
32506b4cac81SBjoern A. Zeeb 
32516b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32528ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3253a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3254a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3255d3ef7fb4SBjoern A. Zeeb sw_scan:
32566b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
32576b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3258086be6a8SBjoern A. Zeeb 
3259086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3260086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3261086be6a8SBjoern A. Zeeb 
32626b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
32636b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
32646b4cac81SBjoern A. Zeeb 		IMPROVE();
32656b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
32666b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
32676b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
32686b4cac81SBjoern A. Zeeb 
32696b4cac81SBjoern A. Zeeb 	} else {
32706b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
32716b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
32726b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
32736b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
32746b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3275d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3276d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3277d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
32783206587aSBjoern A. Zeeb 		bool running;
3279d9945d78SBjoern A. Zeeb 
3280d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3281d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
32826b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
32836b4cac81SBjoern A. Zeeb 
3284d9945d78SBjoern A. Zeeb 		band_mask = 0;
32856b4cac81SBjoern A. Zeeb 		nchan = 0;
3286c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3287c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3288d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
32896b4cac81SBjoern A. Zeeb 				nchan++;
3290d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3291d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3292d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3293d9945d78SBjoern A. Zeeb 			}
3294c272abc5SBjoern A. Zeeb #else
3295c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3296c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3297c272abc5SBjoern A. Zeeb 				switch (band) {
3298c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3299c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3300c272abc5SBjoern A. Zeeb 					break;
3301c272abc5SBjoern A. Zeeb 				default:
3302c272abc5SBjoern A. Zeeb 					continue;
3303c272abc5SBjoern A. Zeeb 				}
3304c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3305c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3306c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3307c272abc5SBjoern A. Zeeb 				}
3308c272abc5SBjoern A. Zeeb 			}
3309c272abc5SBjoern A. Zeeb #endif
3310c272abc5SBjoern A. Zeeb 		} else {
33113206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
33123206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
33133206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
33143206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
33153206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
33163206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
33173206587aSBjoern A. Zeeb 			 */
33183206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
33193206587aSBjoern A. Zeeb 		}
33203206587aSBjoern A. Zeeb 
33216b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
33226b4cac81SBjoern A. Zeeb 
3323d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3324d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3325d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3326d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3327d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3328d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3329d9945d78SBjoern A. Zeeb 
3330d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3331d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3332d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3333d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3334d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3335d9945d78SBjoern A. Zeeb 		}
3336d9945d78SBjoern A. Zeeb 
33373206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3338d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3339d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
33406b4cac81SBjoern A. Zeeb 
33416b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
33426b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
33436b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
33446b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
33456b4cac81SBjoern A. Zeeb #if 0
33466b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
33476b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
33486b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
33496b4cac81SBjoern A. Zeeb #endif
33506b4cac81SBjoern A. Zeeb #ifdef __notyet__
33516b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
33526b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
33536b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
33546b4cac81SBjoern A. Zeeb #endif
3355e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
33566b4cac81SBjoern A. Zeeb 
33576b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
33586b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
33596b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
33606b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
33616b4cac81SBjoern A. Zeeb 			*(cpp + i) =
33626b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
33636b4cac81SBjoern A. Zeeb 		}
3364c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
33656b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3366c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
33676b4cac81SBjoern A. Zeeb 
3368c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
33696b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
33703206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
33716b4cac81SBjoern A. Zeeb 			/* lc->flags */
33726b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
33736b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
33746b4cac81SBjoern A. Zeeb 			/* lc-> ... */
33756b4cac81SBjoern A. Zeeb 			lc++;
33766b4cac81SBjoern A. Zeeb 		}
3377c272abc5SBjoern A. Zeeb #else
3378c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3379c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3380c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3381c272abc5SBjoern A. Zeeb 
3382c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3383c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3384c272abc5SBjoern A. Zeeb 				continue;
3385c272abc5SBjoern A. Zeeb 
3386c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
3387c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
3388c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
3389c272abc5SBjoern A. Zeeb 				continue;
3390c272abc5SBjoern A. Zeeb 
3391c272abc5SBjoern A. Zeeb 			channels = supband->channels;
3392c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
3393c272abc5SBjoern A. Zeeb 				*lc = channels[i];
3394c272abc5SBjoern A. Zeeb 				lc++;
3395c272abc5SBjoern A. Zeeb 			}
3396c272abc5SBjoern A. Zeeb 		}
3397c272abc5SBjoern A. Zeeb #endif
33986b4cac81SBjoern A. Zeeb 
3399d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
34006b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
34016b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
34026b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3403d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
34046b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
34056b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
34066b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
34076b4cac81SBjoern A. Zeeb 				ssids++;
34086b4cac81SBjoern A. Zeeb 			}
34096b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
34106b4cac81SBjoern A. Zeeb 		} else {
34116b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
34126b4cac81SBjoern A. Zeeb 		}
34136b4cac81SBjoern A. Zeeb 
34146b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
34156b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
34166b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
34176b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
34186b4cac81SBjoern A. Zeeb 		/* s6gp->... */
34196b4cac81SBjoern A. Zeeb 
3420d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3421d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3422d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3423d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3424d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3425d9945d78SBjoern A. Zeeb 
34266b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
34276b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
34283206587aSBjoern A. Zeeb 
34293206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
34303206587aSBjoern A. Zeeb 		/* Re-check under lock. */
34313206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
34323206587aSBjoern A. Zeeb 		if (!running) {
34333206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
34343206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
34353206587aSBjoern A. Zeeb 
34363206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
34373206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
34383206587aSBjoern A. Zeeb 		}
34393206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34403206587aSBjoern A. Zeeb 		if (running) {
34413206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
34423206587aSBjoern A. Zeeb 			return;
34433206587aSBjoern A. Zeeb 		}
34443206587aSBjoern A. Zeeb 
34456b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
34466b4cac81SBjoern A. Zeeb 		if (error != 0) {
3447d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3448d9945d78SBjoern A. Zeeb 
34493206587aSBjoern A. Zeeb 			/*
34503206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
34513206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
34523206587aSBjoern A. Zeeb 			 * and only there.
34533206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
34543206587aSBjoern A. Zeeb 			 * behave differently:
34553206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
34563206587aSBjoern A. Zeeb 			 *        and can then still return an error.
34573206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
34583206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
34593206587aSBjoern A. Zeeb 			 * ...
34603206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
34613206587aSBjoern A. Zeeb 			 * and balance between both code paths.
34623206587aSBjoern A. Zeeb 			 */
34633206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
34643206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
34653206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
34666b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
34673206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
34683206587aSBjoern A. Zeeb 			}
34693206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3470d3ef7fb4SBjoern A. Zeeb 
3471d3ef7fb4SBjoern A. Zeeb 			/*
3472d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3473d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3474d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3475d3ef7fb4SBjoern A. Zeeb 			 */
3476196cfd0bSBjoern A. Zeeb 			if (error == 1) {
34778ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3478a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
34798ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
34803206587aSBjoern A. Zeeb 				/*
34813206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
34823206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
34833206587aSBjoern A. Zeeb 				 * currently not re-enable it?
34843206587aSBjoern A. Zeeb 				 */
34853206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3486196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3487196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3488196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3489196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3490196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3491196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3492196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3493196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
3494d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
3495196cfd0bSBjoern A. Zeeb 			}
3496d3ef7fb4SBjoern A. Zeeb 
3497d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3498d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
34996b4cac81SBjoern A. Zeeb 		}
35006b4cac81SBjoern A. Zeeb 	}
35016b4cac81SBjoern A. Zeeb }
35026b4cac81SBjoern A. Zeeb 
35036b4cac81SBjoern A. Zeeb static void
35046b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
35056b4cac81SBjoern A. Zeeb {
35066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35078ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
35086b4cac81SBjoern A. Zeeb 
35096b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35108ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3511a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
35128ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35136b4cac81SBjoern A. Zeeb 		return;
35146b4cac81SBjoern A. Zeeb 	}
35158ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
35168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35176b4cac81SBjoern A. Zeeb 
35188ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3519a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
3520a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
35216b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
35226b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
35236b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
35246b4cac81SBjoern A. Zeeb 
3525a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
3526a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
35276b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
35286b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
35296b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3530a486fbbdSBjoern A. Zeeb 
35316b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
35326b4cac81SBjoern A. Zeeb 
35336b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
3534086be6a8SBjoern A. Zeeb 
3535086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3536086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
35376b4cac81SBjoern A. Zeeb 	}
35386b4cac81SBjoern A. Zeeb }
35396b4cac81SBjoern A. Zeeb 
35406b4cac81SBjoern A. Zeeb static void
3541a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3542a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
3543a486fbbdSBjoern A. Zeeb {
3544a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
35458ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3546a486fbbdSBjoern A. Zeeb 
3547a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
35488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35498ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
35508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35518ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3552a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
3553a486fbbdSBjoern A. Zeeb }
3554a486fbbdSBjoern A. Zeeb 
3555a486fbbdSBjoern A. Zeeb static void
3556a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3557a486fbbdSBjoern A. Zeeb {
3558a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
35598ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
3560a486fbbdSBjoern A. Zeeb 
3561a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
35628ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35638ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
35648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35658ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
3566a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
3567a486fbbdSBjoern A. Zeeb }
3568a486fbbdSBjoern A. Zeeb 
3569a486fbbdSBjoern A. Zeeb static void
35706b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
35716b4cac81SBjoern A. Zeeb {
35726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35736b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3574b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
3575b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
35766b4cac81SBjoern A. Zeeb 	int error;
35778ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
35786b4cac81SBjoern A. Zeeb 
35796b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
3580b2cf3c21SBjoern A. Zeeb 
3581b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
3582b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
35836b4cac81SBjoern A. Zeeb 		return;
35846b4cac81SBjoern A. Zeeb 
3585a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
35868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
35878ac540d3SBjoern A. Zeeb 	hw_scan_running =
35888ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
35898ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
35908ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
35918ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
3592a486fbbdSBjoern A. Zeeb 		return;
3593a486fbbdSBjoern A. Zeeb 
3594b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
3595b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
35966b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
35976b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
35986b4cac81SBjoern A. Zeeb 		return;
35996b4cac81SBjoern A. Zeeb 	}
36006b4cac81SBjoern A. Zeeb 
36016b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
36026b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
36036b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
36046b4cac81SBjoern A. Zeeb 		    c, chan);
36056b4cac81SBjoern A. Zeeb 		return;
36066b4cac81SBjoern A. Zeeb 	}
36076b4cac81SBjoern A. Zeeb 
36086b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
36096b4cac81SBjoern A. Zeeb 	IMPROVE();
36106b4cac81SBjoern A. Zeeb 
36116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36124a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
36139fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
36149fb91463SBjoern A. Zeeb 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
36159fb91463SBjoern A. Zeeb #endif
36164a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
36176b4cac81SBjoern A. Zeeb 
36186b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
36196b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
36206b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
36216b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
36226b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
36236b4cac81SBjoern A. Zeeb 		IMPROVE();
36246b4cac81SBjoern A. Zeeb 	} else {
36256b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
36266b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
36276b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
36286b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
36296b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
36306b4cac81SBjoern A. Zeeb 	}
36316b4cac81SBjoern A. Zeeb 
36326b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
36336b4cac81SBjoern A. Zeeb 	IMPROVE();
36346b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
36356b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
36366b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
36376b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
36386b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
36396b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
36406b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
36416b4cac81SBjoern A. Zeeb 			    error);
36426b4cac81SBjoern A. Zeeb 	}
36436b4cac81SBjoern A. Zeeb }
36446b4cac81SBjoern A. Zeeb 
36456b4cac81SBjoern A. Zeeb static struct ieee80211_node *
36466b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
36476b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
36486b4cac81SBjoern A. Zeeb {
36496b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36514f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
36526b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36536b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
36546b4cac81SBjoern A. Zeeb 
36556b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
36566b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36576b4cac81SBjoern A. Zeeb 
36586b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
36594f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
36604f61ef8bSBjoern A. Zeeb 		return (NULL);
36614f61ef8bSBjoern A. Zeeb 
36626b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
36636b4cac81SBjoern A. Zeeb 	if (ni == NULL)
36646b4cac81SBjoern A. Zeeb 		return (NULL);
36656b4cac81SBjoern A. Zeeb 
36666b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36674f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
36686b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
36696b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
36706b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
36716b4cac81SBjoern A. Zeeb 		return (NULL);
36726b4cac81SBjoern A. Zeeb 	}
36736b4cac81SBjoern A. Zeeb 
36746b4cac81SBjoern A. Zeeb 	return (ni);
36756b4cac81SBjoern A. Zeeb }
36766b4cac81SBjoern A. Zeeb 
36776b4cac81SBjoern A. Zeeb static int
36786b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
36796b4cac81SBjoern A. Zeeb {
36806b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
36816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36826b4cac81SBjoern A. Zeeb 	int error;
36836b4cac81SBjoern A. Zeeb 
36846b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
36856b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36866b4cac81SBjoern A. Zeeb 
36876b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
36886b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
36896b4cac81SBjoern A. Zeeb 		if (error != 0)
36906b4cac81SBjoern A. Zeeb 			return (error);
36916b4cac81SBjoern A. Zeeb 	}
36926b4cac81SBjoern A. Zeeb 
36936b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
36946b4cac81SBjoern A. Zeeb 	IMPROVE();
36956b4cac81SBjoern A. Zeeb 
36966b4cac81SBjoern A. Zeeb 	return (0);
36976b4cac81SBjoern A. Zeeb }
36986b4cac81SBjoern A. Zeeb 
36996b4cac81SBjoern A. Zeeb static void
37006b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
37016b4cac81SBjoern A. Zeeb {
37026b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
37036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37046b4cac81SBjoern A. Zeeb 
37056b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
37066b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37076b4cac81SBjoern A. Zeeb 
37086b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
37096b4cac81SBjoern A. Zeeb 	IMPROVE();
37106b4cac81SBjoern A. Zeeb 
37116b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
37126b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
37136b4cac81SBjoern A. Zeeb }
37146b4cac81SBjoern A. Zeeb 
37156b4cac81SBjoern A. Zeeb static void
37166b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
37176b4cac81SBjoern A. Zeeb {
37186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
37196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37206b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
37216b4cac81SBjoern A. Zeeb 
37226b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
37236b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37246b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
37256b4cac81SBjoern A. Zeeb 
37260936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
37276b4cac81SBjoern A. Zeeb 
37280936c648SBjoern A. Zeeb 	/*
37290936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
37300936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
37310936c648SBjoern A. Zeeb 	 */
37320936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
37336b4cac81SBjoern A. Zeeb 
37346b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
37356b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
37366b4cac81SBjoern A. Zeeb }
37376b4cac81SBjoern A. Zeeb 
37386b4cac81SBjoern A. Zeeb static int
37396b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
37406b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
37416b4cac81SBjoern A. Zeeb {
37426b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
37436b4cac81SBjoern A. Zeeb 
37446b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
3745fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
37462372f8ccSBjoern A. Zeeb #if 0
374788665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
37482372f8ccSBjoern A. Zeeb #else
37492372f8ccSBjoern A. Zeeb 	/*
37502372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
37512372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
37522372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
37532372f8ccSBjoern A. Zeeb 	 */
37542372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
37552372f8ccSBjoern A. Zeeb #endif
3756fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3757fa4e4257SBjoern A. Zeeb 		/*
3758fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3759fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
3760fa4e4257SBjoern A. Zeeb 		 */
37610936c648SBjoern A. Zeeb 		m_free(m);
37620936c648SBjoern A. Zeeb 		return (ENETDOWN);
37630936c648SBjoern A. Zeeb 	}
37646b4cac81SBjoern A. Zeeb 
37656b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
37666b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
3767fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3768fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
37696b4cac81SBjoern A. Zeeb 
37709d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
37719d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
37726b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
37736b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
37746b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
37759d9ba2b7SBjoern A. Zeeb #endif
37766b4cac81SBjoern A. Zeeb 
37776b4cac81SBjoern A. Zeeb 	return (0);
37786b4cac81SBjoern A. Zeeb }
37796b4cac81SBjoern A. Zeeb 
37806b4cac81SBjoern A. Zeeb static void
37816b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
37826b4cac81SBjoern A. Zeeb {
37836b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
3784b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
37856b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
3786b35f6cd0SBjoern A. Zeeb #endif
37876b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
37886b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
37896b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
37906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37916b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37926b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37936b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
37946b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
37956b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
37966b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
37976b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3798e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
3799ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
38006b4cac81SBjoern A. Zeeb 	void *buf;
3801e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
38026b4cac81SBjoern A. Zeeb 
38036b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
38046b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
38059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
38066b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
38076b4cac81SBjoern A. Zeeb #endif
38086b4cac81SBjoern A. Zeeb 
38096b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
3810b35f6cd0SBjoern A. Zeeb 	k = NULL;
3811b35f6cd0SBjoern A. Zeeb #ifndef LKPI_80211_HW_CRYPTO
38126b4cac81SBjoern A. Zeeb 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
38136b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
38146b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
38156b4cac81SBjoern A. Zeeb 		/* Retrieve key for TX && do software encryption. */
38166b4cac81SBjoern A. Zeeb 		k = ieee80211_crypto_encap(ni, m);
38176b4cac81SBjoern A. Zeeb 		if (k == NULL) {
38186b4cac81SBjoern A. Zeeb 			ieee80211_free_node(ni);
38196b4cac81SBjoern A. Zeeb 			m_freem(m);
38206b4cac81SBjoern A. Zeeb 			return;
38216b4cac81SBjoern A. Zeeb 		}
38226b4cac81SBjoern A. Zeeb 	}
38236b4cac81SBjoern A. Zeeb #endif
38246b4cac81SBjoern A. Zeeb 
38256b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
38266b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38276b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38286b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
38296b4cac81SBjoern A. Zeeb 
38306b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
38316b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
38326b4cac81SBjoern A. Zeeb 
38336b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
38346b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
38356b4cac81SBjoern A. Zeeb 		if (k != NULL)
38366b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
38376b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
38386b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
38396b4cac81SBjoern A. Zeeb 		IMPROVE();
38406b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
38416b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
38426b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
38436b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
38446b4cac81SBjoern A. Zeeb 		}
38456b4cac81SBjoern A. Zeeb 
38466b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
38476b4cac81SBjoern A. Zeeb 	}
38486b4cac81SBjoern A. Zeeb 
38496b4cac81SBjoern A. Zeeb 	/*
38506b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
38516b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
38523d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
38533d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
38546b4cac81SBjoern A. Zeeb 	 */
38556b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
38566b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
38573f0083c4SBjoern A. Zeeb 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
38586b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
38596b4cac81SBjoern A. Zeeb 		m_freem(m);
38606b4cac81SBjoern A. Zeeb 		return;
38616b4cac81SBjoern A. Zeeb 	}
38626b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
38636b4cac81SBjoern A. Zeeb 
38646b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
38656b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
38666b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
38676b4cac81SBjoern A. Zeeb 	skb->m = m;
38686b4cac81SBjoern A. Zeeb #if 0
38696b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
38706b4cac81SBjoern A. Zeeb #else
38716b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
38726b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
38736b4cac81SBjoern A. Zeeb #endif
38746b4cac81SBjoern A. Zeeb 	/* Save the ni. */
38756b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
38766b4cac81SBjoern A. Zeeb 
38776b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
38786b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
38796b4cac81SBjoern A. Zeeb 
3880e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
3881e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3882e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
38831665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
38841665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
38851665ef97SBjoern A. Zeeb 			skb->priority = 7;
38861665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
38871665ef97SBjoern A. Zeeb 		} else {
38881665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
38891665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
3890e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
3891e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
38921665ef97SBjoern A. Zeeb 		}
3893e3a0b120SBjoern A. Zeeb 	} else {
3894e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3895fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
3896e3a0b120SBjoern A. Zeeb 	}
38976b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
38986b4cac81SBjoern A. Zeeb 
38996b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
39006b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
39016b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
39026b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
39036b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
39046b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3905e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
39066b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
39076b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
39086b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
39096b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
39109fb91463SBjoern A. Zeeb #ifdef __notyet__
39119fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
39129fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
39139fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
39149fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
39159fb91463SBjoern A. Zeeb #endif
39169fb91463SBjoern A. Zeeb #endif
39176b4cac81SBjoern A. Zeeb 
39186b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3919b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
39206b4cac81SBjoern A. Zeeb 	info->control.hw_key = lsta->kc;
39216b4cac81SBjoern A. Zeeb #endif
39226b4cac81SBjoern A. Zeeb 
39236b4cac81SBjoern A. Zeeb 	IMPROVE();
39246b4cac81SBjoern A. Zeeb 
3925e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
3926e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
3927e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
3928e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
3929e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3930d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3931e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
3932e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
3933e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3934e3a0b120SBjoern A. Zeeb 	}
3935e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
3936d0d29110SBjoern A. Zeeb 		goto ops_tx;
3937e3a0b120SBjoern A. Zeeb 
3938d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3939d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3940d0d29110SBjoern A. Zeeb 
3941eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
39426b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
39439d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39449d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3945d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3946d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3947d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3948fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
3949d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
3950d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3951d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3952d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
39539d9ba2b7SBjoern A. Zeeb #endif
3954eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
395545bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3956d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
395745bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
39586b4cac81SBjoern A. Zeeb 	return;
39596b4cac81SBjoern A. Zeeb 
39606b4cac81SBjoern A. Zeeb ops_tx:
39619d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39629d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3963d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3964d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
39656b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
39666b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
39679d9ba2b7SBjoern A. Zeeb #endif
39686b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
39696b4cac81SBjoern A. Zeeb 	control.sta = sta;
397045bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
39716b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
397245bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
39736b4cac81SBjoern A. Zeeb }
39746b4cac81SBjoern A. Zeeb 
39756b4cac81SBjoern A. Zeeb static void
39766b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
39776b4cac81SBjoern A. Zeeb {
39786b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
39796b4cac81SBjoern A. Zeeb 	struct mbufq mq;
39806b4cac81SBjoern A. Zeeb 	struct mbuf *m;
398188665349SBjoern A. Zeeb 	bool shall_tx;
39826b4cac81SBjoern A. Zeeb 
39836b4cac81SBjoern A. Zeeb 	lsta = ctx;
39846b4cac81SBjoern A. Zeeb 
39859d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
39869d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
39876b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
39889d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
39896b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
39909d9ba2b7SBjoern A. Zeeb #endif
39916b4cac81SBjoern A. Zeeb 
39926b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
39936b4cac81SBjoern A. Zeeb 
3994fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3995fa4e4257SBjoern A. Zeeb 	/*
3996fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
399788665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
399888665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
399988665349SBjoern A. Zeeb 	 * point to try to TX.
400088665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
400188665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4002fa4e4257SBjoern A. Zeeb 	 */
40032372f8ccSBjoern A. Zeeb #if 0
400488665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
40052372f8ccSBjoern A. Zeeb #else
40062372f8ccSBjoern A. Zeeb 	/*
40072372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
40082372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
40092372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
40102372f8ccSBjoern A. Zeeb 	 */
40112372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
40122372f8ccSBjoern A. Zeeb #endif
401388665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
40146b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
401588665349SBjoern A. Zeeb 	/*
401688665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
401788665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
401888665349SBjoern A. Zeeb 	 */
4019fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
40206b4cac81SBjoern A. Zeeb 
40216b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
40226b4cac81SBjoern A. Zeeb 	while (m != NULL) {
40236b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
40246b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
40256b4cac81SBjoern A. Zeeb 	}
40266b4cac81SBjoern A. Zeeb }
40276b4cac81SBjoern A. Zeeb 
40286b4cac81SBjoern A. Zeeb static int
40296b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
40306b4cac81SBjoern A. Zeeb {
40316b4cac81SBjoern A. Zeeb 
40326b4cac81SBjoern A. Zeeb 	/* XXX TODO */
40336b4cac81SBjoern A. Zeeb 	IMPROVE();
40346b4cac81SBjoern A. Zeeb 
40356b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
40366b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
40376b4cac81SBjoern A. Zeeb 
40386b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
40396b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
40406b4cac81SBjoern A. Zeeb }
40416b4cac81SBjoern A. Zeeb 
40429fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
40439fb91463SBjoern A. Zeeb static int
40449fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
40459fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
40469fb91463SBjoern A. Zeeb {
40479fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40489fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40499fb91463SBjoern A. Zeeb 
40509fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40519fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40529fb91463SBjoern A. Zeeb 
4053310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
40549fb91463SBjoern A. Zeeb 
40559fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
40569fb91463SBjoern A. Zeeb }
40579fb91463SBjoern A. Zeeb 
40589fb91463SBjoern A. Zeeb static int
40599fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
40609fb91463SBjoern A. Zeeb {
40619fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40629fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40639fb91463SBjoern A. Zeeb 
40649fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40659fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40669fb91463SBjoern A. Zeeb 
4067310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
40689fb91463SBjoern A. Zeeb 
40699fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
40709fb91463SBjoern A. Zeeb }
40719fb91463SBjoern A. Zeeb 
40729fb91463SBjoern A. Zeeb 
40739fb91463SBjoern A. Zeeb static int
40749fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
40759fb91463SBjoern A. Zeeb {
40769fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40779fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40789fb91463SBjoern A. Zeeb 
40799fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
40809fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
40819fb91463SBjoern A. Zeeb 
4082310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
40839fb91463SBjoern A. Zeeb 
40849fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
40859fb91463SBjoern A. Zeeb }
40869fb91463SBjoern A. Zeeb 
4087310743c4SBjoern A. Zeeb /*
4088310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4089310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4090310743c4SBjoern A. Zeeb  *
4091310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4092310743c4SBjoern A. Zeeb  */
40939fb91463SBjoern A. Zeeb static int
40949fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
40959fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
40969fb91463SBjoern A. Zeeb {
40979fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
40989fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4099310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4100310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4101310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4102310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4103310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4104310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4105310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4106310743c4SBjoern A. Zeeb 	int error;
41079fb91463SBjoern A. Zeeb 
41089fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
41099fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4110310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4111310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4112310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4113310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4114310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4115310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
41169fb91463SBjoern A. Zeeb 
4117310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4118310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4119310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4120310743c4SBjoern A. Zeeb 		return (0);
4121310743c4SBjoern A. Zeeb 	}
4122310743c4SBjoern A. Zeeb 
4123310743c4SBjoern A. Zeeb 	params.sta = sta;
4124310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4125310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4126310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4127310743c4SBjoern A. Zeeb 	params.timeout = 0;
4128310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4129310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4130310743c4SBjoern A. Zeeb 	params.amsdu = false;
4131310743c4SBjoern A. Zeeb 
4132310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4133310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4134310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4135310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4136310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4137310743c4SBjoern A. Zeeb 	if (error != 0) {
4138310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4139310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4140310743c4SBjoern A. Zeeb 		return (0);
4141310743c4SBjoern A. Zeeb 	}
41429fb91463SBjoern A. Zeeb 
41439fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
41449fb91463SBjoern A. Zeeb }
41459fb91463SBjoern A. Zeeb 
4146310743c4SBjoern A. Zeeb /*
4147310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4148310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4149310743c4SBjoern A. Zeeb  *
4150310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4151310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4152310743c4SBjoern A. Zeeb  */
41539fb91463SBjoern A. Zeeb static int
41549fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
41559fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
41569fb91463SBjoern A. Zeeb {
41579fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
41589fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4159310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4160310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4161310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4162310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4163310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4164310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4165310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4166310743c4SBjoern A. Zeeb 	int error;
41679fb91463SBjoern A. Zeeb 
41689fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
41699fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4170310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4171310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4172310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4173310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4174310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4175310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
41769fb91463SBjoern A. Zeeb 
4177310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4178310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4179310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4180310743c4SBjoern A. Zeeb 		return (0);
4181310743c4SBjoern A. Zeeb 	}
4182310743c4SBjoern A. Zeeb 
4183310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4184310743c4SBjoern A. Zeeb 		params.sta = sta;
4185310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4186310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4187310743c4SBjoern A. Zeeb 		params.timeout = 0;
4188310743c4SBjoern A. Zeeb 		params.ssn = 0;
4189310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4190310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4191310743c4SBjoern A. Zeeb 			params.amsdu = true;
4192310743c4SBjoern A. Zeeb 		else
4193310743c4SBjoern A. Zeeb 			params.amsdu = false;
4194310743c4SBjoern A. Zeeb 	} else {
4195310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4196310743c4SBjoern A. Zeeb 		params.sta = sta;
4197310743c4SBjoern A. Zeeb 		switch (status) {
4198310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4199310743c4SBjoern A. Zeeb 		default:
4200310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4201310743c4SBjoern A. Zeeb 			break;
4202310743c4SBjoern A. Zeeb 		}
4203310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4204310743c4SBjoern A. Zeeb 		params.timeout = 0;
4205310743c4SBjoern A. Zeeb 		params.ssn = 0;
4206310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4207310743c4SBjoern A. Zeeb 		params.amsdu = false;
4208310743c4SBjoern A. Zeeb 	}
4209310743c4SBjoern A. Zeeb 
4210310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4211310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4212310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4213310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4214310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4215310743c4SBjoern A. Zeeb 	if (error != 0) {
4216310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4217310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4218310743c4SBjoern A. Zeeb 		return (0);
4219310743c4SBjoern A. Zeeb 	}
4220310743c4SBjoern A. Zeeb 
4221310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
42229fb91463SBjoern A. Zeeb 
42239fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
42249fb91463SBjoern A. Zeeb }
42259fb91463SBjoern A. Zeeb 
4226310743c4SBjoern A. Zeeb /*
4227310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4228310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4229310743c4SBjoern A. Zeeb  */
42309fb91463SBjoern A. Zeeb static void
42319fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
42329fb91463SBjoern A. Zeeb {
42339fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
42349fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4235310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4236310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4237310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4238310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4239310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4240310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4241310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4242310743c4SBjoern A. Zeeb 	int error;
42439fb91463SBjoern A. Zeeb 
42449fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
42459fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4246310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4247310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4248310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4249310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4250310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4251310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
42529fb91463SBjoern A. Zeeb 
4253310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4254310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4255310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4256310743c4SBjoern A. Zeeb 		goto n80211;
4257310743c4SBjoern A. Zeeb 	}
42589fb91463SBjoern A. Zeeb 
4259310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4260310743c4SBjoern A. Zeeb 	params.sta = sta;
4261310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4262310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4263310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4264310743c4SBjoern A. Zeeb 	params.timeout = 0;
4265310743c4SBjoern A. Zeeb 	params.ssn = 0;
4266310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4267310743c4SBjoern A. Zeeb 	params.amsdu = false;
4268310743c4SBjoern A. Zeeb 
4269310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4270310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4271310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4272310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4273310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4274310743c4SBjoern A. Zeeb 	if (error != 0) {
4275310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4276310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4277310743c4SBjoern A. Zeeb 		goto n80211;
4278310743c4SBjoern A. Zeeb 	}
4279310743c4SBjoern A. Zeeb 
4280310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4281310743c4SBjoern A. Zeeb 
4282310743c4SBjoern A. Zeeb n80211:
42839fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
42849fb91463SBjoern A. Zeeb }
42859fb91463SBjoern A. Zeeb 
42869fb91463SBjoern A. Zeeb static void
42879fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
42889fb91463SBjoern A. Zeeb {
42899fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
42909fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42919fb91463SBjoern A. Zeeb 
42929fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
42939fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
42949fb91463SBjoern A. Zeeb 
42959fb91463SBjoern A. Zeeb 	IMPROVE_HT();
42969fb91463SBjoern A. Zeeb 
42979fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
42989fb91463SBjoern A. Zeeb }
42999fb91463SBjoern A. Zeeb 
43009fb91463SBjoern A. Zeeb static void
43019fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
43029fb91463SBjoern A. Zeeb     int status)
43039fb91463SBjoern A. Zeeb {
43049fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43059fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43069fb91463SBjoern A. Zeeb 
43079fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
43089fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
43099fb91463SBjoern A. Zeeb 
43109fb91463SBjoern A. Zeeb 	IMPROVE_HT();
43119fb91463SBjoern A. Zeeb 
43129fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
43139fb91463SBjoern A. Zeeb }
43149fb91463SBjoern A. Zeeb 
43159fb91463SBjoern A. Zeeb static int
43169fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
43179fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
43189fb91463SBjoern A. Zeeb {
43199fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43209fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43219fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43229fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
43239fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
43249fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
43259fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43269fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4327310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
43289fb91463SBjoern A. Zeeb 	int error;
43299fb91463SBjoern A. Zeeb 
43309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
43319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
43329fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
43339fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
43349fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
43359fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
43369fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
43379fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
43389fb91463SBjoern A. Zeeb 
4339310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
4340310743c4SBjoern A. Zeeb 
4341310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4342310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
4343310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
4344310743c4SBjoern A. Zeeb 		return (-ENXIO);
4345310743c4SBjoern A. Zeeb 	}
4346310743c4SBjoern A. Zeeb 
43479fb91463SBjoern A. Zeeb 	params.sta = sta;
43489fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
43499fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
43509fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
43519fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
43529fb91463SBjoern A. Zeeb 	else
43539fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4354310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
4355310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
43569fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
43579fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
43589fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
43599fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4360310743c4SBjoern A. Zeeb 
4361310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
4362310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
4363310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
4364310743c4SBjoern A. Zeeb 		params.amsdu = true;
4365310743c4SBjoern A. Zeeb 	else
43669fb91463SBjoern A. Zeeb 		params.amsdu = false;
43679fb91463SBjoern A. Zeeb 
4368310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
43699fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4370310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
43719fb91463SBjoern A. Zeeb 	if (error != 0) {
43729fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
43739fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
43749fb91463SBjoern A. Zeeb 		return (error);
43759fb91463SBjoern A. Zeeb 	}
4376310743c4SBjoern A. Zeeb 
4377310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
4378310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
4379310743c4SBjoern A. Zeeb 	}
4380310743c4SBjoern A. Zeeb 
43819fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
43829fb91463SBjoern A. Zeeb 
43839fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
43849fb91463SBjoern A. Zeeb 	return (error);
43859fb91463SBjoern A. Zeeb }
43869fb91463SBjoern A. Zeeb 
43879fb91463SBjoern A. Zeeb static void
43889fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
43899fb91463SBjoern A. Zeeb {
43909fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
43919fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43929fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43939fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
43949fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
43959fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
43969fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43979fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4398310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
43999fb91463SBjoern A. Zeeb 	int error;
44009fb91463SBjoern A. Zeeb 	uint8_t tid;
44019fb91463SBjoern A. Zeeb 
44029fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
44039fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
44049fb91463SBjoern A. Zeeb 
44059fb91463SBjoern A. Zeeb 	/*
44069fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
44079fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
44089fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
44099fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
44109fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
44119fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
44129fb91463SBjoern A. Zeeb 	 * track some state itself.
44139fb91463SBjoern A. Zeeb 	 */
44149fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
44159fb91463SBjoern A. Zeeb 		goto net80211_only;
44169fb91463SBjoern A. Zeeb 
44179fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44189fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
44199fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
44209fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
44219fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
44229fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
44239fb91463SBjoern A. Zeeb 
44249fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
44259fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
44269fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
44279fb91463SBjoern A. Zeeb 			break;
44289fb91463SBjoern A. Zeeb 	}
44299fb91463SBjoern A. Zeeb 
44309fb91463SBjoern A. Zeeb 	params.sta = sta;
44319fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
44329fb91463SBjoern A. Zeeb 	params.buf_size = 0;
44339fb91463SBjoern A. Zeeb 	params.timeout = 0;
44349fb91463SBjoern A. Zeeb 	params.ssn = 0;
44359fb91463SBjoern A. Zeeb 	params.tid = tid;
44369fb91463SBjoern A. Zeeb 	params.amsdu = false;
44379fb91463SBjoern A. Zeeb 
4438310743c4SBjoern A. Zeeb 	// IEEE80211_UNLOCK(ic);
4439310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
44409fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4441310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4442310743c4SBjoern A. Zeeb 	// IEEE80211_LOCK(ic);
44439fb91463SBjoern A. Zeeb 	if (error != 0)
44449fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
44459fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
44469fb91463SBjoern A. Zeeb 
44479fb91463SBjoern A. Zeeb net80211_only:
44489fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
44499fb91463SBjoern A. Zeeb }
44509fb91463SBjoern A. Zeeb #endif
44519fb91463SBjoern A. Zeeb 
44529fb91463SBjoern A. Zeeb static void
44539fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
44549fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
44559fb91463SBjoern A. Zeeb {
44569fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
44579fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
44589fb91463SBjoern A. Zeeb 
44599fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
44609fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
44619fb91463SBjoern A. Zeeb 		return;
44629fb91463SBjoern A. Zeeb 
44639fb91463SBjoern A. Zeeb 	switch (band) {
44649fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
44659fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
44669fb91463SBjoern A. Zeeb 		break;
44679fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
44689fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
44699fb91463SBjoern A. Zeeb 		break;
44709fb91463SBjoern A. Zeeb 	default:
44719fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
44729fb91463SBjoern A. Zeeb 		return;
44739fb91463SBjoern A. Zeeb 	}
44749fb91463SBjoern A. Zeeb 
44759fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
44769fb91463SBjoern A. Zeeb 
44779fb91463SBjoern A. Zeeb 	/*
44789fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
44799fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
44809fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
44819fb91463SBjoern A. Zeeb 	 */
44829fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
44839fb91463SBjoern A. Zeeb 
44849fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
44859fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
44869fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
44879fb91463SBjoern A. Zeeb #ifdef __notyet__
44889fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
44899fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
44909fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
44919fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
44929fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
44939fb91463SBjoern A. Zeeb #endif
44949fb91463SBjoern A. Zeeb 
44959fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
44969fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
44979fb91463SBjoern A. Zeeb 
44989fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
44999fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
45009fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
45019fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
45029fb91463SBjoern A. Zeeb #endif
45039fb91463SBjoern A. Zeeb }
45049fb91463SBjoern A. Zeeb 
45056b4cac81SBjoern A. Zeeb static void
45066b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
45076b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
45086b4cac81SBjoern A. Zeeb {
45096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
45116b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
45126b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
45136b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
45146b4cac81SBjoern A. Zeeb 
45156b4cac81SBjoern A. Zeeb 	/* Channels */
45166b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45176b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45186b4cac81SBjoern A. Zeeb 
45196b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
45206b4cac81SBjoern A. Zeeb 	nchans = 0;
45216b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
45226b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
45236b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
45246b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
45256b4cac81SBjoern A. Zeeb 		chan_flags = 0;
45266b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
45276b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
45289fb91463SBjoern A. Zeeb 
45299fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
45306b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
45319fb91463SBjoern A. Zeeb 
45329fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
45339fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
45346b4cac81SBjoern A. Zeeb 
45356b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4536cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
45376b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
45386b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
45396b4cac81SBjoern A. Zeeb 
45406b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
45413f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
45423f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
45436b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
45446b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
45456b4cac81SBjoern A. Zeeb 				continue;
45466b4cac81SBjoern A. Zeeb 			}
45476b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
45486b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
45496b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
45506b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
45516b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
45526b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
45536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
45546b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
45556b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
45566b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
45576b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
45586b4cac81SBjoern A. Zeeb 
45596b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
45606b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
45616b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
45625856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4563cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4564cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
45653f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
45663f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
45676b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
45686b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
45696b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4570cee56e77SBjoern A. Zeeb 			if (error != 0)
45716b4cac81SBjoern A. Zeeb 				break;
45726b4cac81SBjoern A. Zeeb 		}
45736b4cac81SBjoern A. Zeeb 	}
45746b4cac81SBjoern A. Zeeb 
45756b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
45766b4cac81SBjoern A. Zeeb 	nchans = 0;
45776b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
45786b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
45796b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
45806b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
45816b4cac81SBjoern A. Zeeb 		chan_flags = 0;
45826b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
45839fb91463SBjoern A. Zeeb 
45849fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
45859fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
45869fb91463SBjoern A. Zeeb 
45879fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
45886b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
45896b4cac81SBjoern A. Zeeb 
45906b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4591562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
45926b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
4593ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
4594ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
45956b4cac81SBjoern A. Zeeb 
45966b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
45976b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
45986b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4599562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
46006b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
46016b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4602562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
46036b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
46046b4cac81SBjoern A. Zeeb 		}
46056b4cac81SBjoern A. Zeeb #endif
46066b4cac81SBjoern A. Zeeb 
46076b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4608cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
46096b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
46106b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
46116b4cac81SBjoern A. Zeeb 
46126b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
46133f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
46143f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
46156b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
46166b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
46176b4cac81SBjoern A. Zeeb 				continue;
46186b4cac81SBjoern A. Zeeb 			}
46196b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
46206b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
46216b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
46226b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
46236b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
46246b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
46256b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
46266b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
46276b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
46286b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
46296b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
46306b4cac81SBjoern A. Zeeb 
46316b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
46326b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
46336b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
46345856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
4635cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
4636cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
46373f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
46383f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
46396b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
46406b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
46416b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
4642cee56e77SBjoern A. Zeeb 			if (error != 0)
46436b4cac81SBjoern A. Zeeb 				break;
46446b4cac81SBjoern A. Zeeb 		}
46456b4cac81SBjoern A. Zeeb 	}
46466b4cac81SBjoern A. Zeeb }
46476b4cac81SBjoern A. Zeeb 
46486b4cac81SBjoern A. Zeeb static void *
46496b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
46506b4cac81SBjoern A. Zeeb {
46516b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46526b4cac81SBjoern A. Zeeb 
46536b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
46546b4cac81SBjoern A. Zeeb 
46556b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
46566b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
46576b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
46586b4cac81SBjoern A. Zeeb 
46596b4cac81SBjoern A. Zeeb 	return (ic);
46606b4cac81SBjoern A. Zeeb }
46616b4cac81SBjoern A. Zeeb 
46626b4cac81SBjoern A. Zeeb struct ieee80211_hw *
46636b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
46646b4cac81SBjoern A. Zeeb {
46656b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
46666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46676b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
4668a5ae63edSBjoern A. Zeeb 	int ac;
46696b4cac81SBjoern A. Zeeb 
46706b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
46716b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
46726b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
46736b4cac81SBjoern A. Zeeb 		return (NULL);
46746b4cac81SBjoern A. Zeeb 
46756b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
46766b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
4677652e22d3SBjoern A. Zeeb 
46788ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
46798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4680eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
46818891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
46826b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
4683a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4684a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
4685a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4686a5ae63edSBjoern A. Zeeb 	}
46876b4cac81SBjoern A. Zeeb 
4688759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
4689759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4690759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4691759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4692759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
4693759a996dSBjoern A. Zeeb 
46946b4cac81SBjoern A. Zeeb 	/*
46956b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
46966b4cac81SBjoern A. Zeeb 	 * not initialized.
46976b4cac81SBjoern A. Zeeb 	 */
46986b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
46996b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
4700086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
47016b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
47026b4cac81SBjoern A. Zeeb 
47036b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
47046b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
47056b4cac81SBjoern A. Zeeb 
47066b4cac81SBjoern A. Zeeb 	IMPROVE();
47076b4cac81SBjoern A. Zeeb 
47086b4cac81SBjoern A. Zeeb 	return (hw);
47096b4cac81SBjoern A. Zeeb }
47106b4cac81SBjoern A. Zeeb 
47116b4cac81SBjoern A. Zeeb void
47126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
47136b4cac81SBjoern A. Zeeb {
47146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4715759a996dSBjoern A. Zeeb 	struct mbuf *m;
47166b4cac81SBjoern A. Zeeb 
47176b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
47186b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
47196b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
47206b4cac81SBjoern A. Zeeb 
4721759a996dSBjoern A. Zeeb 	/*
4722759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
4723759a996dSBjoern A. Zeeb 	 */
4724759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4725759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
4726759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4727759a996dSBjoern A. Zeeb 
4728759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4729759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4730759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4731759a996dSBjoern A. Zeeb 
4732759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
4733759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
4734759a996dSBjoern A. Zeeb 	while (m != NULL) {
4735759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
4736759a996dSBjoern A. Zeeb 
4737759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4738759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
4739759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
4740759a996dSBjoern A. Zeeb 
4741759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4742759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
4743759a996dSBjoern A. Zeeb 		}
4744759a996dSBjoern A. Zeeb 		m_freem(m);
4745759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
4746759a996dSBjoern A. Zeeb 	}
4747759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4748759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4749759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4750759a996dSBjoern A. Zeeb 
47516b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
4752eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
47538ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4754eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4755eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
47566b4cac81SBjoern A. Zeeb 	IMPROVE();
47576b4cac81SBjoern A. Zeeb }
47586b4cac81SBjoern A. Zeeb 
47596b4cac81SBjoern A. Zeeb void
47606b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
47616b4cac81SBjoern A. Zeeb {
47626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47636b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47646b4cac81SBjoern A. Zeeb 
47656b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
47666b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
47676b4cac81SBjoern A. Zeeb 
47686b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
47696b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
47706b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
47716b4cac81SBjoern A. Zeeb 
47726b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
47736b4cac81SBjoern A. Zeeb }
47746b4cac81SBjoern A. Zeeb 
47756b4cac81SBjoern A. Zeeb struct ieee80211_hw *
47766b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
47776b4cac81SBjoern A. Zeeb {
47786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47796b4cac81SBjoern A. Zeeb 
47806b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
47816b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
47826b4cac81SBjoern A. Zeeb }
47836b4cac81SBjoern A. Zeeb 
47846b4cac81SBjoern A. Zeeb static void
47856b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
47866b4cac81SBjoern A. Zeeb {
47876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47886b4cac81SBjoern A. Zeeb 
47896b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
47906b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
47916b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
47926b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
47936b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
47946b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
47956b4cac81SBjoern A. Zeeb }
47966b4cac81SBjoern A. Zeeb 
47972e183d99SBjoern A. Zeeb int
47986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
47996b4cac81SBjoern A. Zeeb {
48006b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4802c5b96b3eSBjoern A. Zeeb 	int band, i;
48036b4cac81SBjoern A. Zeeb 
48046b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
48056b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
48066b4cac81SBjoern A. Zeeb 
4807652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
4808652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4809652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
4810652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
4811652e22d3SBjoern A. Zeeb 
48126b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
48136b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
48146b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
48156b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
48166b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
48176b4cac81SBjoern A. Zeeb 		/* We take the first one. */
48186b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
48196b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
48206b4cac81SBjoern A. Zeeb 	} else {
48216b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
48226b4cac81SBjoern A. Zeeb 	}
48236b4cac81SBjoern A. Zeeb 
48243d09d310SBjoern A. Zeeb #ifdef __not_yet__
48253d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
48266b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
48273d09d310SBjoern A. Zeeb #endif
48286b4cac81SBjoern A. Zeeb 
48296b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
48306b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
48316b4cac81SBjoern A. Zeeb 
48326b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
48336b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
48346b4cac81SBjoern A. Zeeb 	ic->ic_caps =
48356b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
48366b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
48376b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
48384a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
48396b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
48404a67f1dfSBjoern A. Zeeb #endif
48416b4cac81SBjoern A. Zeeb #if 0
48426b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
48436b4cac81SBjoern A. Zeeb #endif
48446b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
48456b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
48466b4cac81SBjoern A. Zeeb 	    ;
48476b4cac81SBjoern A. Zeeb #if 0
48486b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
48496b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
48506b4cac81SBjoern A. Zeeb #endif
4851cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
4852cc4e78d5SBjoern A. Zeeb 		/*
4853cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
4854cc4e78d5SBjoern A. Zeeb 		 *
4855cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4856cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
4857cc4e78d5SBjoern A. Zeeb 		 * the flag.
4858cc4e78d5SBjoern A. Zeeb 		 */
48596b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4860a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
48616b4cac81SBjoern A. Zeeb 	}
48626b4cac81SBjoern A. Zeeb 
4863527687a9SBjoern A. Zeeb 	/*
4864527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
4865527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
4866527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
4867527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4868527687a9SBjoern A. Zeeb 	 */
4869527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
4870527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
4871527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
4872527687a9SBjoern A. Zeeb 
4873527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4874527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
4875527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
4876527687a9SBjoern A. Zeeb 		}
4877527687a9SBjoern A. Zeeb 	}
4878527687a9SBjoern A. Zeeb 
48796b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
4880b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
48816b4cac81SBjoern A. Zeeb 	if (hw->wiphy->n_cipher_suites > 0) {
48826b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
48836b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
48846b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
48856b4cac81SBjoern A. Zeeb 	}
48866b4cac81SBjoern A. Zeeb #endif
48876b4cac81SBjoern A. Zeeb 
48886b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
48896b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
48906b4cac81SBjoern A. Zeeb 
48916b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
48926b4cac81SBjoern A. Zeeb 
48936b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
48946b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
48956b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
48966b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
48976b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
48986b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
48996b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
49006b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
49016b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
49026b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
49036b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
49046b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
49056b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
49066b4cac81SBjoern A. Zeeb 
4907a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4908a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4909a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4910a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4911a486fbbdSBjoern A. Zeeb 
49126b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
49136b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
49146b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
49156b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
49166b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
49176b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
49186b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
49196b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
49206b4cac81SBjoern A. Zeeb 
49219fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
492286bc7259SBjoern A. Zeeb 	/*
492386bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
492486bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
492586bc7259SBjoern A. Zeeb 	 */
492686bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
49279fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
49289fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
49299fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
49309fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
49319fb91463SBjoern A. Zeeb 
49329fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
49339fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
49349fb91463SBjoern A. Zeeb 
49359fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
49369fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
49379fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
49389fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
49399fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
49409fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
49419fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
49429fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
49439fb91463SBjoern A. Zeeb 
49449fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
49459fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
49469fb91463SBjoern A. Zeeb 
49479fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
49489fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
49499fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
49509fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
495186bc7259SBjoern A. Zeeb 	}
49529fb91463SBjoern A. Zeeb #endif
49539fb91463SBjoern A. Zeeb 
49546b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
49556b4cac81SBjoern A. Zeeb 
4956c5b96b3eSBjoern A. Zeeb 	/*
4957c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
4958c5b96b3eSBjoern A. Zeeb 	 * expect one.
4959d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
4960d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4961c5b96b3eSBjoern A. Zeeb 	 */
4962d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
4963f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4964c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
4965c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
4966c5b96b3eSBjoern A. Zeeb 
4967c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4968c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4969c5b96b3eSBjoern A. Zeeb 			continue;
4970c5b96b3eSBjoern A. Zeeb 
4971d9945d78SBjoern A. Zeeb 		lhw->supbands++;
4972d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4973d9945d78SBjoern A. Zeeb 
4974f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
4975f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
4976f454a4a1SBjoern A. Zeeb 			continue;
4977f454a4a1SBjoern A. Zeeb 
4978c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
4979c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4980c5b96b3eSBjoern A. Zeeb 
4981c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4982c5b96b3eSBjoern A. Zeeb 				continue;
4983c5b96b3eSBjoern A. Zeeb 
49844a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
49859fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
49869fb91463SBjoern A. Zeeb 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
49879fb91463SBjoern A. Zeeb #endif
4988c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
4989c5b96b3eSBjoern A. Zeeb 			break;
4990c5b96b3eSBjoern A. Zeeb 		}
4991c5b96b3eSBjoern A. Zeeb 	}
4992c5b96b3eSBjoern A. Zeeb 
4993d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4994d9945d78SBjoern A. Zeeb 
4995d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
4996d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4997d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4998d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4999d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5000d9945d78SBjoern A. Zeeb 	}
5001d9945d78SBjoern A. Zeeb 
5002d9945d78SBjoern A. Zeeb 	/*
5003d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5004d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5005d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5006d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5007d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5008d9945d78SBjoern A. Zeeb 	 */
5009d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5010d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5011d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
501278ca45dfSBjoern A. Zeeb 
501378ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5014d9945d78SBjoern A. Zeeb 		/*
501578ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
501678ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
501778ca45dfSBjoern A. Zeeb 		 * space in.
5018d9945d78SBjoern A. Zeeb 		 */
5019d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
502078ca45dfSBjoern A. Zeeb 	}
5021d9945d78SBjoern A. Zeeb 
50229fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
50239fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
50249fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
50259fb91463SBjoern A. Zeeb #endif
50269fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
50279fb91463SBjoern A. Zeeb 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
50289fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
50299fb91463SBjoern A. Zeeb #endif
50309fb91463SBjoern A. Zeeb 
5031d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
503278ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
503378ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
503478ca45dfSBjoern A. Zeeb 			goto err;
5035d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
503678ca45dfSBjoern A. Zeeb 	}
5037d9945d78SBjoern A. Zeeb 
50386b4cac81SBjoern A. Zeeb 	if (bootverbose)
50396b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
50402e183d99SBjoern A. Zeeb 
50412e183d99SBjoern A. Zeeb 	return (0);
504278ca45dfSBjoern A. Zeeb err:
504378ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
504478ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
50456b4cac81SBjoern A. Zeeb }
50466b4cac81SBjoern A. Zeeb 
50476b4cac81SBjoern A. Zeeb void
50486b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
50496b4cac81SBjoern A. Zeeb {
50506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50516b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
50526b4cac81SBjoern A. Zeeb 
50536b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
50546b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
50556b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
50566b4cac81SBjoern A. Zeeb }
50576b4cac81SBjoern A. Zeeb 
50586b4cac81SBjoern A. Zeeb void
50596b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
50606b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
50616b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
50626b4cac81SBjoern A. Zeeb     void *arg)
50636b4cac81SBjoern A. Zeeb {
50646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50666b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
506761a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
50686b4cac81SBjoern A. Zeeb 
50696b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
50706b4cac81SBjoern A. Zeeb 
50716b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
50726b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
507361a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5074adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
50756b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
50766b4cac81SBjoern A. Zeeb 		    __func__, flags);
50776b4cac81SBjoern A. Zeeb 	}
50786b4cac81SBjoern A. Zeeb 
5079adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
50806b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
508161a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
50826b4cac81SBjoern A. Zeeb 
50836b4cac81SBjoern A. Zeeb 	if (atomic)
50848891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
50856b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
50866b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
50876b4cac81SBjoern A. Zeeb 
50886b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
50896b4cac81SBjoern A. Zeeb 
50906b4cac81SBjoern A. Zeeb 		/*
50916b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
50926b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
50936b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
50946b4cac81SBjoern A. Zeeb 		 * driver does not know about.
50956b4cac81SBjoern A. Zeeb 		 */
50966b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
50976b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
50986b4cac81SBjoern A. Zeeb 			continue;
50996b4cac81SBjoern A. Zeeb 
51006b4cac81SBjoern A. Zeeb 		/*
510161a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
510261a68e50SBjoern A. Zeeb 		 * if we haven't yet.
510361a68e50SBjoern A. Zeeb 		 */
510461a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
510561a68e50SBjoern A. Zeeb 			continue;
510661a68e50SBjoern A. Zeeb 
510761a68e50SBjoern A. Zeeb 		/*
51086b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
51096b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
51106b4cac81SBjoern A. Zeeb 		 */
51116b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
51126b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
51136b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
51146b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
51156b4cac81SBjoern A. Zeeb 	}
51166b4cac81SBjoern A. Zeeb 	if (atomic)
51178891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
51186b4cac81SBjoern A. Zeeb }
51196b4cac81SBjoern A. Zeeb 
51206b4cac81SBjoern A. Zeeb void
51216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
51226b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
51236b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
51246b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
51256b4cac81SBjoern A. Zeeb     void *arg)
51266b4cac81SBjoern A. Zeeb {
51276b4cac81SBjoern A. Zeeb 
51286b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
51296b4cac81SBjoern A. Zeeb }
51306b4cac81SBjoern A. Zeeb 
51316b4cac81SBjoern A. Zeeb void
51326b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
51336b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
51346b4cac81SBjoern A. Zeeb 	void *),
51356b4cac81SBjoern A. Zeeb     void *arg)
51366b4cac81SBjoern A. Zeeb {
5137c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5138c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5139c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5140c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
51416b4cac81SBjoern A. Zeeb 
5142c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5143c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5144c5e25798SBjoern A. Zeeb 
5145c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5146c5e25798SBjoern A. Zeeb 
5147c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5148c5e25798SBjoern A. Zeeb 
5149c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5150c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5151c5e25798SBjoern A. Zeeb 
5152c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5153c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5154c5e25798SBjoern A. Zeeb 			continue;
5155c5e25798SBjoern A. Zeeb 
5156c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5157c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5158c5e25798SBjoern A. Zeeb 			continue;
5159c5e25798SBjoern A. Zeeb 
5160d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5161c5e25798SBjoern A. Zeeb 	}
5162c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
51636b4cac81SBjoern A. Zeeb }
51646b4cac81SBjoern A. Zeeb 
51656b4cac81SBjoern A. Zeeb void
51666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
51676b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
51686b4cac81SBjoern A. Zeeb {
51696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
51716b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51726b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
51736b4cac81SBjoern A. Zeeb 
51746b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
51756b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
51766b4cac81SBjoern A. Zeeb 
51776b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
51786b4cac81SBjoern A. Zeeb 
51798891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
51806b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
51816b4cac81SBjoern A. Zeeb 
51826b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_LOCK(lvif);
51836b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
51846b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
51856b4cac81SBjoern A. Zeeb 				continue;
51866b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
51876b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
51886b4cac81SBjoern A. Zeeb 		}
51896b4cac81SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
51906b4cac81SBjoern A. Zeeb 	}
51918891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
51926b4cac81SBjoern A. Zeeb }
51936b4cac81SBjoern A. Zeeb 
5194673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5195673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5196673d62fcSBjoern A. Zeeb {
5197673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5198673d62fcSBjoern A. Zeeb 
5199673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5200673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5201673d62fcSBjoern A. Zeeb 	return (regd);
5202673d62fcSBjoern A. Zeeb }
5203673d62fcSBjoern A. Zeeb 
52046b4cac81SBjoern A. Zeeb int
52056b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
52066b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
52076b4cac81SBjoern A. Zeeb {
52086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52096b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
52106b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
52116b4cac81SBjoern A. Zeeb 
52126b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
52136b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
52146b4cac81SBjoern A. Zeeb 
52156b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
52166b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
52176b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
52186b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
52196b4cac81SBjoern A. Zeeb 	}
52206b4cac81SBjoern A. Zeeb 
52216b4cac81SBjoern A. Zeeb 	TODO();
52226b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
52236b4cac81SBjoern A. Zeeb 
52246b4cac81SBjoern A. Zeeb 	return (0);
52256b4cac81SBjoern A. Zeeb }
52266b4cac81SBjoern A. Zeeb 
52276b4cac81SBjoern A. Zeeb void
52286b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
52296b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
52306b4cac81SBjoern A. Zeeb {
52316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52326b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
52336b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
52346b4cac81SBjoern A. Zeeb 
52356b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
52366b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
52376b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
52386b4cac81SBjoern A. Zeeb 
52396b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
52406b4cac81SBjoern A. Zeeb 
52418ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
52426b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
52436b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5244a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
52456b4cac81SBjoern A. Zeeb 	wakeup(lhw);
52468ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
52476b4cac81SBjoern A. Zeeb 
52486b4cac81SBjoern A. Zeeb 	return;
52496b4cac81SBjoern A. Zeeb }
52506b4cac81SBjoern A. Zeeb 
5251759a996dSBjoern A. Zeeb static void
5252759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5253759a996dSBjoern A. Zeeb {
5254759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5255759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5256759a996dSBjoern A. Zeeb 	int ok;
5257759a996dSBjoern A. Zeeb 
5258759a996dSBjoern A. Zeeb 	ni = NULL;
5259759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5260759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5261759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5262759a996dSBjoern A. Zeeb 
5263759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5264759a996dSBjoern A. Zeeb 		ni = rxni->ni;
5265759a996dSBjoern A. Zeeb 	}
5266759a996dSBjoern A. Zeeb 
5267759a996dSBjoern A. Zeeb 	if (ni != NULL) {
5268759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
5269759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
5270759a996dSBjoern A. Zeeb 		if (ok < 0)
5271759a996dSBjoern A. Zeeb 			m_freem(m);
5272759a996dSBjoern A. Zeeb 	} else {
5273759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5274759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
5275759a996dSBjoern A. Zeeb 	}
5276759a996dSBjoern A. Zeeb 
5277759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5278759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5279bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5280759a996dSBjoern A. Zeeb #endif
5281759a996dSBjoern A. Zeeb }
5282759a996dSBjoern A. Zeeb 
5283759a996dSBjoern A. Zeeb static void
5284759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5285759a996dSBjoern A. Zeeb {
5286759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5287759a996dSBjoern A. Zeeb 	struct mbufq mq;
5288759a996dSBjoern A. Zeeb 	struct mbuf *m;
5289759a996dSBjoern A. Zeeb 
5290759a996dSBjoern A. Zeeb 	lhw = ctx;
5291759a996dSBjoern A. Zeeb 
5292759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5293759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5294bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5295bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5296759a996dSBjoern A. Zeeb #endif
5297759a996dSBjoern A. Zeeb 
5298759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
5299759a996dSBjoern A. Zeeb 
5300759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5301759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
5302759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5303759a996dSBjoern A. Zeeb 
5304759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
5305759a996dSBjoern A. Zeeb 	while (m != NULL) {
5306759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5307759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
5308759a996dSBjoern A. Zeeb 	}
5309759a996dSBjoern A. Zeeb }
5310759a996dSBjoern A. Zeeb 
5311e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
53126b4cac81SBjoern A. Zeeb void
53136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
5314e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
5315e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
53166b4cac81SBjoern A. Zeeb {
53176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53196b4cac81SBjoern A. Zeeb 	struct mbuf *m;
53206b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
53216b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
53226b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
53236b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
53246b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
53256b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
53266b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
53279d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
5328170acccfSBjoern A. Zeeb 	int8_t rssi;
5329c0cadd99SBjoern A. Zeeb 	bool is_beacon;
53306b4cac81SBjoern A. Zeeb 
53316b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
53326b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
53336b4cac81SBjoern A. Zeeb 		IMPROVE();
53346b4cac81SBjoern A. Zeeb 		goto err;
53356b4cac81SBjoern A. Zeeb 	}
53366b4cac81SBjoern A. Zeeb 
53376b4cac81SBjoern A. Zeeb 	/*
53386b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
53396b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
53406b4cac81SBjoern A. Zeeb 	 */
53416b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
53426b4cac81SBjoern A. Zeeb 	if (m == NULL)
53436b4cac81SBjoern A. Zeeb 		goto err;
53446b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
53456b4cac81SBjoern A. Zeeb 
53466b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
53476b4cac81SBjoern A. Zeeb 	offset = m->m_len;
53486b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
53496b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
53506b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
53516b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
53526b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
53536b4cac81SBjoern A. Zeeb 	}
53546b4cac81SBjoern A. Zeeb 
53556b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
53566b4cac81SBjoern A. Zeeb 
53576b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
5358c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
5359c0cadd99SBjoern A. Zeeb 
5360c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
53619d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
53626b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
53636b4cac81SBjoern A. Zeeb 
53649d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
53656b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5366c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
53676b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
53686b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
53696b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
5370c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
53716b4cac81SBjoern A. Zeeb 
53729d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
53736b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
53746b4cac81SBjoern A. Zeeb 
53756b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
53769d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5377f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
537860970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
53796b4cac81SBjoern A. Zeeb 			__func__,
5380c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
5381c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
53826b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
5383f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
53846b4cac81SBjoern A. Zeeb 			rx_status->freq,
53856b4cac81SBjoern A. Zeeb 			rx_status->bw,
53866b4cac81SBjoern A. Zeeb 			rx_status->encoding,
53876b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
53886b4cac81SBjoern A. Zeeb 			rx_status->band,
53896b4cac81SBjoern A. Zeeb 			rx_status->chains,
53906b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
53916b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
53926b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
539360970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
53946b4cac81SBjoern A. Zeeb 			rx_status->signal,
53956b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
53966b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
53976b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
53986b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
53996b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
54006b4cac81SBjoern A. Zeeb 			rx_status->nss,
54016b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
54026b4cac81SBjoern A. Zeeb no_trace_beacons:
54036b4cac81SBjoern A. Zeeb #endif
54046b4cac81SBjoern A. Zeeb 
54056b4cac81SBjoern A. Zeeb 	memset(&rx_stats, 0, sizeof(rx_stats));
54066b4cac81SBjoern A. Zeeb 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
540760970a32SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
540860970a32SBjoern A. Zeeb 	rx_stats.c_nf = -96;
54096b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
54106b4cac81SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5411170acccfSBjoern A. Zeeb 		rssi = rx_status->signal;
54126b4cac81SBjoern A. Zeeb 	else
5413170acccfSBjoern A. Zeeb 		rssi = rx_stats.c_nf;
5414170acccfSBjoern A. Zeeb 	/*
5415170acccfSBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
5416170acccfSBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
5417170acccfSBjoern A. Zeeb 	 */
5418170acccfSBjoern A. Zeeb 	rssi -= rx_stats.c_nf;
5419170acccfSBjoern A. Zeeb 	rx_stats.c_rssi = rssi * 2;
54206b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_BAND;
54216b4cac81SBjoern A. Zeeb 	rx_stats.c_band =
54226b4cac81SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
54236b4cac81SBjoern A. Zeeb 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
54246b4cac81SBjoern A. Zeeb 	rx_stats.c_freq = rx_status->freq;
54256b4cac81SBjoern A. Zeeb 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
54266b4cac81SBjoern A. Zeeb 
5427*ec6185c5SBjoern A. Zeeb 	/*
5428*ec6185c5SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
5429*ec6185c5SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
5430*ec6185c5SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
5431*ec6185c5SBjoern A. Zeeb 	 */
5432*ec6185c5SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
5433*ec6185c5SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
5434*ec6185c5SBjoern A. Zeeb 		rx_stats.c_pktflags |= IEEE80211_RX_F_DECRYPTED;
5435*ec6185c5SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
5436*ec6185c5SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
5437*ec6185c5SBjoern A. Zeeb 			rx_stats.c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
5438*ec6185c5SBjoern A. Zeeb 	}
5439*ec6185c5SBjoern A. Zeeb #endif
5440*ec6185c5SBjoern A. Zeeb 
54416b4cac81SBjoern A. Zeeb 	/* XXX (*sta_statistics)() to get to some of that? */
54426b4cac81SBjoern A. Zeeb 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
54436b4cac81SBjoern A. Zeeb 
54446b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54456b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54466b4cac81SBjoern A. Zeeb 
54476b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
54486b4cac81SBjoern A. Zeeb 	if (ok == 0) {
5449dbc06dd9SBjoern A. Zeeb 		m_freem(m);
54506b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
54516b4cac81SBjoern A. Zeeb 		goto err;
54526b4cac81SBjoern A. Zeeb 	}
54536b4cac81SBjoern A. Zeeb 
545458246901SBjoern A. Zeeb 	lsta = NULL;
54556b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
54566b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
54576b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
54586b4cac81SBjoern A. Zeeb 	} else {
5459c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
5460c8dafefaSBjoern A. Zeeb 
54616b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
54626b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
54636b4cac81SBjoern A. Zeeb 		if (ni != NULL)
54646b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
54656b4cac81SBjoern A. Zeeb 	}
54666b4cac81SBjoern A. Zeeb 
54676b4cac81SBjoern A. Zeeb 	if (ni != NULL)
54686b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
54696b4cac81SBjoern A. Zeeb 	else
54706b4cac81SBjoern A. Zeeb 		/*
54716b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
54726b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
54736b4cac81SBjoern A. Zeeb 		 */
54746b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
54756b4cac81SBjoern A. Zeeb 
54769d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
54779d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5478bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5479c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5480c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
54819d9ba2b7SBjoern A. Zeeb #endif
54826b4cac81SBjoern A. Zeeb 
5483c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
5484c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
5485c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5486c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
5487c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
5488c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
5489c8dafefaSBjoern A. Zeeb 
5490c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
5491c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5492c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
5493c8dafefaSBjoern A. Zeeb 
5494c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
5495c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5496c8dafefaSBjoern A. Zeeb 
5497c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
5498c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
5499c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5500c8dafefaSBjoern A. Zeeb 		/*
5501c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
5502c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5503c8dafefaSBjoern A. Zeeb 		 */
5504c8dafefaSBjoern A. Zeeb skip_device_ts:
55059fb91463SBjoern A. Zeeb 		;
55069fb91463SBjoern A. Zeeb 	}
5507c8dafefaSBjoern A. Zeeb 
55086b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
55096b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
55106b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
55116b4cac81SBjoern A. Zeeb 
55126b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
55136b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
55146b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
55156b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
55166b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
55176b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
55186b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
55196b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
55206b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
55216b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
55226b4cac81SBjoern A. Zeeb #endif
55236b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
55246b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
55256b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
55266b4cac81SBjoern A. Zeeb 		IMPROVE();
55276b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
55286b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
55296b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
55306b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5531170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
55326b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
55336b4cac81SBjoern A. Zeeb 	}
55346b4cac81SBjoern A. Zeeb 
55356b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
55366b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
55376b4cac81SBjoern A. Zeeb 
5538e30e05d3SBjoern A. Zeeb #if 0
5539e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
5540e30e05d3SBjoern A. Zeeb 		/*
5541e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
5542e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5543e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
5544e30e05d3SBjoern A. Zeeb 		*/
5545e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5546e30e05d3SBjoern A. Zeeb 	}
5547e30e05d3SBjoern A. Zeeb #endif
5548e30e05d3SBjoern A. Zeeb 
5549759a996dSBjoern A. Zeeb 	/*
5550759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
5551759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
5552759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
5553759a996dSBjoern A. Zeeb 	 */
55546b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
5555759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5556759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5557759a996dSBjoern A. Zeeb 
5558759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5559759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5560759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5561759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5562759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
5563759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
5564759a996dSBjoern A. Zeeb 		}
55656b4cac81SBjoern A. Zeeb 	}
55666b4cac81SBjoern A. Zeeb 
5567759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5568759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
5569759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5570759a996dSBjoern A. Zeeb 		m_freem(m);
5571759a996dSBjoern A. Zeeb 		goto err;
5572759a996dSBjoern A. Zeeb 	}
5573759a996dSBjoern A. Zeeb 
5574759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
5575759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5576759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
55776b4cac81SBjoern A. Zeeb 
55786b4cac81SBjoern A. Zeeb 	IMPROVE();
55796b4cac81SBjoern A. Zeeb 
55806b4cac81SBjoern A. Zeeb err:
55816b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
55826b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
55836b4cac81SBjoern A. Zeeb }
55846b4cac81SBjoern A. Zeeb 
55856b4cac81SBjoern A. Zeeb uint8_t
5586ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
55876b4cac81SBjoern A. Zeeb {
55886b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
5589ec190d91SBjoern A. Zeeb 	uint8_t tid;
5590ec190d91SBjoern A. Zeeb 
5591ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
5592ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5593ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5594ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
55956b4cac81SBjoern A. Zeeb 
55966b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
5597ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
5598ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5599ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5600ec190d91SBjoern A. Zeeb 
5601ec190d91SBjoern A. Zeeb 	return (tid);
56026b4cac81SBjoern A. Zeeb }
56036b4cac81SBjoern A. Zeeb 
5604ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
5605ac1d519cSBjoern A. Zeeb 
5606ac1d519cSBjoern A. Zeeb static void
5607ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
5608ac1d519cSBjoern A. Zeeb {
5609ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5610ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
5611ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
5612ac1d519cSBjoern A. Zeeb 
5613ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
5614ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5615ac1d519cSBjoern A. Zeeb 
5616ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
5617ac1d519cSBjoern A. Zeeb 
5618ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5619ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
5620ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
5621ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
5622ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5623ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
5624ac1d519cSBjoern A. Zeeb 		return;
5625ac1d519cSBjoern A. Zeeb 	}
5626ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
5627ac1d519cSBjoern A. Zeeb 
5628ac1d519cSBjoern A. Zeeb 	/* More work to do? */
5629ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
5630ac1d519cSBjoern A. Zeeb 		schedule_work(work);
5631ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5632ac1d519cSBjoern A. Zeeb 
5633ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
5634ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
5635ac1d519cSBjoern A. Zeeb 
5636ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
5637ac1d519cSBjoern A. Zeeb }
5638ac1d519cSBjoern A. Zeeb 
5639ac1d519cSBjoern A. Zeeb void
5640ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
5641ac1d519cSBjoern A. Zeeb {
5642ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5643ac1d519cSBjoern A. Zeeb 
5644ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5645ac1d519cSBjoern A. Zeeb 
5646ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5647ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
5648ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
5649ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
5650ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5651ac1d519cSBjoern A. Zeeb 
5652ac1d519cSBjoern A. Zeeb 	/*
5653ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
5654ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
5655ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
5656ac1d519cSBjoern A. Zeeb 	 */
5657ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
5658ac1d519cSBjoern A. Zeeb }
5659ac1d519cSBjoern A. Zeeb 
5660ac1d519cSBjoern A. Zeeb void
5661ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
5662ac1d519cSBjoern A. Zeeb {
5663ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5664ac1d519cSBjoern A. Zeeb 
5665ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5666ac1d519cSBjoern A. Zeeb 
5667ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5668ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
5669ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
5670ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
5671ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5672ac1d519cSBjoern A. Zeeb }
5673ac1d519cSBjoern A. Zeeb 
5674ac1d519cSBjoern A. Zeeb void
5675ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
5676ac1d519cSBjoern A. Zeeb {
5677ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5678ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
5679ac1d519cSBjoern A. Zeeb 
5680ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5681ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5682ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
5683ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
5684ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5685ac1d519cSBjoern A. Zeeb 		return;
5686ac1d519cSBjoern A. Zeeb 	}
5687ac1d519cSBjoern A. Zeeb 
5688ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
5689ac1d519cSBjoern A. Zeeb 
5690ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
5691ac1d519cSBjoern A. Zeeb 		    entry);
5692ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
5693ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5694ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
5695ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5696ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
5697ac1d519cSBjoern A. Zeeb 			break;
5698ac1d519cSBjoern A. Zeeb 	}
5699ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5700ac1d519cSBjoern A. Zeeb }
5701ac1d519cSBjoern A. Zeeb 
5702ac1d519cSBjoern A. Zeeb void
5703ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
5704ac1d519cSBjoern A. Zeeb {
5705ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
5706ac1d519cSBjoern A. Zeeb 
5707ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
5708ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
5709ac1d519cSBjoern A. Zeeb }
5710ac1d519cSBjoern A. Zeeb 
5711ac1d519cSBjoern A. Zeeb void
5712ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
5713ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
5714ac1d519cSBjoern A. Zeeb {
5715ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
5716ac1d519cSBjoern A. Zeeb 		/* Run right away. */
5717ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
5718ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
5719ac1d519cSBjoern A. Zeeb 	} else {
5720ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
5721ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
5722ac1d519cSBjoern A. Zeeb 	}
5723ac1d519cSBjoern A. Zeeb }
5724ac1d519cSBjoern A. Zeeb 
5725ac1d519cSBjoern A. Zeeb void
5726ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
5727ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
5728ac1d519cSBjoern A. Zeeb {
5729ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
5730ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
5731ac1d519cSBjoern A. Zeeb }
5732ac1d519cSBjoern A. Zeeb 
5733ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
5734ac1d519cSBjoern A. Zeeb 
57356b4cac81SBjoern A. Zeeb struct wiphy *
57366b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
57376b4cac81SBjoern A. Zeeb {
57386b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
5739ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
57406b4cac81SBjoern A. Zeeb 
57416b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
57426b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
57436b4cac81SBjoern A. Zeeb 		return (NULL);
57446b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
57456b4cac81SBjoern A. Zeeb 
5746ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
5747ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
5748ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
5749ac1d519cSBjoern A. Zeeb 
5750ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5751ac1d519cSBjoern A. Zeeb 
5752ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
5753ac1d519cSBjoern A. Zeeb 	TODO();
5754ac1d519cSBjoern A. Zeeb 
5755ac1d519cSBjoern A. Zeeb 	return (wiphy);
57566b4cac81SBjoern A. Zeeb }
57576b4cac81SBjoern A. Zeeb 
57586b4cac81SBjoern A. Zeeb void
57596b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
57606b4cac81SBjoern A. Zeeb {
57616b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
57626b4cac81SBjoern A. Zeeb 
57636b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
57646b4cac81SBjoern A. Zeeb 		return;
57656b4cac81SBjoern A. Zeeb 
5766ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
5767ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
5768ac1d519cSBjoern A. Zeeb 
57696b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5770ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
5771ac1d519cSBjoern A. Zeeb 
57726b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
57736b4cac81SBjoern A. Zeeb }
57746b4cac81SBjoern A. Zeeb 
57756b4cac81SBjoern A. Zeeb uint32_t
57766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
57776b4cac81SBjoern A. Zeeb     enum nl80211_band band)
57786b4cac81SBjoern A. Zeeb {
57796b4cac81SBjoern A. Zeeb 
57806b4cac81SBjoern A. Zeeb 	switch (band) {
57816b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
57826b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
57836b4cac81SBjoern A. Zeeb 		break;
57846b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
57856b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
57866b4cac81SBjoern A. Zeeb 		break;
57876b4cac81SBjoern A. Zeeb 	default:
57886b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
57896b4cac81SBjoern A. Zeeb 		break;
57906b4cac81SBjoern A. Zeeb 	}
57916b4cac81SBjoern A. Zeeb 
57926b4cac81SBjoern A. Zeeb 	return (0);
57936b4cac81SBjoern A. Zeeb }
57946b4cac81SBjoern A. Zeeb 
57956b4cac81SBjoern A. Zeeb uint32_t
57966b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
57976b4cac81SBjoern A. Zeeb {
57986b4cac81SBjoern A. Zeeb 
57996b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
58006b4cac81SBjoern A. Zeeb }
58016b4cac81SBjoern A. Zeeb 
5802ac867c20SBjoern A. Zeeb #if 0
58036b4cac81SBjoern A. Zeeb static struct lkpi_sta *
58046b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
58056b4cac81SBjoern A. Zeeb {
58066b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
58076b4cac81SBjoern A. Zeeb 
58086b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
58096b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
58106b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
58116b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
58126b4cac81SBjoern A. Zeeb 			return (lsta);
58136b4cac81SBjoern A. Zeeb 		}
58146b4cac81SBjoern A. Zeeb 	}
58156b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
58166b4cac81SBjoern A. Zeeb 
58176b4cac81SBjoern A. Zeeb 	return (NULL);
58186b4cac81SBjoern A. Zeeb }
5819ac867c20SBjoern A. Zeeb #endif
58206b4cac81SBjoern A. Zeeb 
58216b4cac81SBjoern A. Zeeb struct ieee80211_sta *
58226b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
58236b4cac81SBjoern A. Zeeb {
58246b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58256b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
58266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
58276b4cac81SBjoern A. Zeeb 
58286b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
58296b4cac81SBjoern A. Zeeb 
58306b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
58316b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
58326b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
58336b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
58346b4cac81SBjoern A. Zeeb 			LKPI_80211_LVIF_UNLOCK(lvif);
58356b4cac81SBjoern A. Zeeb 			return (sta);
58366b4cac81SBjoern A. Zeeb 		}
58376b4cac81SBjoern A. Zeeb 	}
58386b4cac81SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
58396b4cac81SBjoern A. Zeeb 	return (NULL);
58406b4cac81SBjoern A. Zeeb }
58416b4cac81SBjoern A. Zeeb 
58426b4cac81SBjoern A. Zeeb struct ieee80211_sta *
58432e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
58442e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
58456b4cac81SBjoern A. Zeeb {
58466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58486b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
58496b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
58506b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
58516b4cac81SBjoern A. Zeeb 
58526b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
58536b4cac81SBjoern A. Zeeb 	sta = NULL;
58546b4cac81SBjoern A. Zeeb 
58558891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
58566b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58576b4cac81SBjoern A. Zeeb 
58586b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
58596b4cac81SBjoern A. Zeeb 
58606b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
58616b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
58626b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
58636b4cac81SBjoern A. Zeeb 			continue;
58646b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
58656b4cac81SBjoern A. Zeeb 		if (sta != NULL)
58666b4cac81SBjoern A. Zeeb 			break;
58676b4cac81SBjoern A. Zeeb 	}
58688891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58696b4cac81SBjoern A. Zeeb 
58706b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
58716b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
58726b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
58736b4cac81SBjoern A. Zeeb 			return (NULL);
58746b4cac81SBjoern A. Zeeb 	}
58756b4cac81SBjoern A. Zeeb 
58766b4cac81SBjoern A. Zeeb 	return (sta);
58776b4cac81SBjoern A. Zeeb }
58786b4cac81SBjoern A. Zeeb 
58796b4cac81SBjoern A. Zeeb struct sk_buff *
58806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
58816b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
58826b4cac81SBjoern A. Zeeb {
58836b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
58840cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58856b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
58866b4cac81SBjoern A. Zeeb 
58870cbcfa19SBjoern A. Zeeb 	skb = NULL;
58886b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
58896b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
58906b4cac81SBjoern A. Zeeb 
58910cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
58920cbcfa19SBjoern A. Zeeb 		goto stopped;
58930cbcfa19SBjoern A. Zeeb 
58940cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
58950cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
58960cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
58970cbcfa19SBjoern A. Zeeb 		goto stopped;
58980cbcfa19SBjoern A. Zeeb 	}
58990cbcfa19SBjoern A. Zeeb 
5900eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
5901eac3646fSBjoern A. Zeeb 
5902eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
59036b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
5904eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
59056b4cac81SBjoern A. Zeeb 
59060cbcfa19SBjoern A. Zeeb stopped:
59076b4cac81SBjoern A. Zeeb 	return (skb);
59086b4cac81SBjoern A. Zeeb }
59096b4cac81SBjoern A. Zeeb 
59106b4cac81SBjoern A. Zeeb void
59116b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
591286220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
59136b4cac81SBjoern A. Zeeb {
59146b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
59156b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
591686220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
59176b4cac81SBjoern A. Zeeb 
59186b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
59196b4cac81SBjoern A. Zeeb 
59206b4cac81SBjoern A. Zeeb 	fc = bc = 0;
5921eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
59226b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
59236b4cac81SBjoern A. Zeeb 		fc++;
59246b4cac81SBjoern A. Zeeb 		bc += skb->len;
59256b4cac81SBjoern A. Zeeb 	}
5926eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
59276b4cac81SBjoern A. Zeeb 	if (frame_cnt)
59286b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
59296b4cac81SBjoern A. Zeeb 	if (byte_cnt)
59306b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
59316b4cac81SBjoern A. Zeeb 
59326b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
59336b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
59346b4cac81SBjoern A. Zeeb 	IMPROVE();
59356b4cac81SBjoern A. Zeeb }
59366b4cac81SBjoern A. Zeeb 
59376b4cac81SBjoern A. Zeeb /*
59386b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
59396b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
59406b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
59416b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
59426b4cac81SBjoern A. Zeeb  */
5943a8397571SBjoern A. Zeeb static void
5944a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
59456b4cac81SBjoern A. Zeeb     int status)
59466b4cac81SBjoern A. Zeeb {
59476b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
59486b4cac81SBjoern A. Zeeb 	struct mbuf *m;
59496b4cac81SBjoern A. Zeeb 
59506b4cac81SBjoern A. Zeeb 	m = skb->m;
59516b4cac81SBjoern A. Zeeb 	skb->m = NULL;
59526b4cac81SBjoern A. Zeeb 
59536b4cac81SBjoern A. Zeeb 	if (m != NULL) {
59546b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
59556b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
59566b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
59576b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
59586b4cac81SBjoern A. Zeeb 	}
5959a8397571SBjoern A. Zeeb }
59606b4cac81SBjoern A. Zeeb 
5961a8397571SBjoern A. Zeeb void
5962a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5963a8397571SBjoern A. Zeeb     int status)
5964a8397571SBjoern A. Zeeb {
5965a8397571SBjoern A. Zeeb 
5966a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
59676b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
59686b4cac81SBjoern A. Zeeb }
59696b4cac81SBjoern A. Zeeb 
5970383b3e8fSBjoern A. Zeeb void
5971a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5972a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
5973383b3e8fSBjoern A. Zeeb {
5974a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
5975383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
5976383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
5977383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
5978383b3e8fSBjoern A. Zeeb 	int status;
5979383b3e8fSBjoern A. Zeeb 
5980a8397571SBjoern A. Zeeb 	skb = txstat->skb;
5981383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
5982383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
5983383b3e8fSBjoern A. Zeeb 
5984383b3e8fSBjoern A. Zeeb 		m = skb->m;
5985383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
5986383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
5987383b3e8fSBjoern A. Zeeb 	} else {
5988383b3e8fSBjoern A. Zeeb 		ni = NULL;
5989383b3e8fSBjoern A. Zeeb 	}
5990383b3e8fSBjoern A. Zeeb 
5991a8397571SBjoern A. Zeeb 	info = txstat->info;
5992383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5993383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
5994383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5995383b3e8fSBjoern A. Zeeb 	} else {
5996383b3e8fSBjoern A. Zeeb 		status = 1;
5997383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5998383b3e8fSBjoern A. Zeeb 	}
5999383b3e8fSBjoern A. Zeeb 
6000383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6001e2c5ab09SJohn Baldwin 		int ridx __unused;
6002383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6003383b3e8fSBjoern A. Zeeb 		int old_rate;
6004383b3e8fSBjoern A. Zeeb 
6005383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6006383b3e8fSBjoern A. Zeeb #endif
6007383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6008383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6009383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6010383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6011383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6012383b3e8fSBjoern A. Zeeb 		}
6013383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6014a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6015383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6016383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6017383b3e8fSBjoern A. Zeeb #endif
6018adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6019383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6020383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6021383b3e8fSBjoern A. Zeeb 		}
6022383b3e8fSBjoern A. Zeeb 
6023383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6024383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6025383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6026383b3e8fSBjoern A. Zeeb 
6027383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6028383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6029383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6030383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6031383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6032383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6033383b3e8fSBjoern A. Zeeb 		}
6034383b3e8fSBjoern A. Zeeb #endif
6035383b3e8fSBjoern A. Zeeb 	}
6036383b3e8fSBjoern A. Zeeb 
6037383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6038383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6039383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6040383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6041383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6042383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6043adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6044383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6045383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6046383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6047383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6048383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6049383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6050383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6051383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6052383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6053383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6054383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6055383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6056383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6057adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6058383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6059383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6060383b3e8fSBjoern A. Zeeb #endif
6061383b3e8fSBjoern A. Zeeb 
6062a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6063a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6064a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6065a8397571SBjoern A. Zeeb 	} else {
6066383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6067383b3e8fSBjoern A. Zeeb 	}
6068a8397571SBjoern A. Zeeb }
6069a8397571SBjoern A. Zeeb 
6070a8397571SBjoern A. Zeeb void
6071a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6072a8397571SBjoern A. Zeeb {
6073a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6074a8397571SBjoern A. Zeeb 
6075a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6076a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6077a8397571SBjoern A. Zeeb 	status.skb = skb;
6078a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6079a8397571SBjoern A. Zeeb 
6080a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6081a8397571SBjoern A. Zeeb }
6082383b3e8fSBjoern A. Zeeb 
60836b4cac81SBjoern A. Zeeb /*
60846b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
60856b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
60866b4cac81SBjoern A. Zeeb  * mbufs this should go away.
60876b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
60886b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
60896b4cac81SBjoern A. Zeeb  */
60906b4cac81SBjoern A. Zeeb static void
60916b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
60926b4cac81SBjoern A. Zeeb {
60936b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
60946b4cac81SBjoern A. Zeeb 	struct mbuf *m;
60956b4cac81SBjoern A. Zeeb 
60966b4cac81SBjoern A. Zeeb 	if (p == NULL)
60976b4cac81SBjoern A. Zeeb 		return;
60986b4cac81SBjoern A. Zeeb 
60996b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
61006b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
61016b4cac81SBjoern A. Zeeb 
61026b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
61036b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
61046b4cac81SBjoern A. Zeeb 	if (ni != NULL)
61056b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
61066b4cac81SBjoern A. Zeeb 	m_freem(m);
61076b4cac81SBjoern A. Zeeb }
61086b4cac81SBjoern A. Zeeb 
61096b4cac81SBjoern A. Zeeb void
61106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
61116b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
61126b4cac81SBjoern A. Zeeb {
61136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61146b4cac81SBjoern A. Zeeb 
61156b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
61166b4cac81SBjoern A. Zeeb 	IMPROVE();
61176b4cac81SBjoern A. Zeeb 
61186b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61196b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
61206b4cac81SBjoern A. Zeeb }
61216b4cac81SBjoern A. Zeeb 
61226b4cac81SBjoern A. Zeeb void
61236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
61246b4cac81SBjoern A. Zeeb     struct work_struct *w)
61256b4cac81SBjoern A. Zeeb {
61266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61276b4cac81SBjoern A. Zeeb 
61286b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
61296b4cac81SBjoern A. Zeeb 	IMPROVE();
61306b4cac81SBjoern A. Zeeb 
61316b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61326b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
61336b4cac81SBjoern A. Zeeb }
61346b4cac81SBjoern A. Zeeb 
61356b4cac81SBjoern A. Zeeb struct sk_buff *
6136ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
6137ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
6138ade774b1SBjoern A. Zeeb {
6139ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
6140ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
6141ade774b1SBjoern A. Zeeb 	uint8_t *p;
6142ade774b1SBjoern A. Zeeb 	size_t len;
6143ade774b1SBjoern A. Zeeb 
6144ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
6145ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
6146ade774b1SBjoern A. Zeeb 
6147ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
6148ade774b1SBjoern A. Zeeb 	if (skb == NULL)
6149ade774b1SBjoern A. Zeeb 		return (NULL);
6150ade774b1SBjoern A. Zeeb 
6151ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
6152ade774b1SBjoern A. Zeeb 
6153ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
6154ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
6155ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
6156ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
6157ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
6158ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
6159ade774b1SBjoern A. Zeeb 
6160ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
6161ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
6162ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
6163ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
6164ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
6165ade774b1SBjoern A. Zeeb 
6166ade774b1SBjoern A. Zeeb 	return (skb);
6167ade774b1SBjoern A. Zeeb }
6168ade774b1SBjoern A. Zeeb 
6169ade774b1SBjoern A. Zeeb struct sk_buff *
61706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
61716b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
61726b4cac81SBjoern A. Zeeb {
61736b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
61746b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
61756b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
61766b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
61776b4cac81SBjoern A. Zeeb 	uint16_t v;
61786b4cac81SBjoern A. Zeeb 
61796b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
61806b4cac81SBjoern A. Zeeb 	if (skb == NULL)
61816b4cac81SBjoern A. Zeeb 		return (NULL);
61826b4cac81SBjoern A. Zeeb 
61836b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
61846b4cac81SBjoern A. Zeeb 
61856b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
61866b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
61876b4cac81SBjoern A. Zeeb 
61886b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
61896b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
61906b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
6191616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
61926b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
61936b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
61946b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
61956b4cac81SBjoern A. Zeeb 
61966b4cac81SBjoern A. Zeeb 	return (skb);
61976b4cac81SBjoern A. Zeeb }
61986b4cac81SBjoern A. Zeeb 
61996b4cac81SBjoern A. Zeeb struct sk_buff *
62006b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
6201adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
62026b4cac81SBjoern A. Zeeb {
62036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62046b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
62056b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
62066b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
62076b4cac81SBjoern A. Zeeb 
6208adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
6209adff403fSBjoern A. Zeeb 
62106b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
62116b4cac81SBjoern A. Zeeb 	if (skb == NULL)
62126b4cac81SBjoern A. Zeeb 		return (NULL);
62136b4cac81SBjoern A. Zeeb 
62146b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
62156b4cac81SBjoern A. Zeeb 
62166b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
62176b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
62186b4cac81SBjoern A. Zeeb 
62196b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
62206b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
62216b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
62226b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
62236b4cac81SBjoern A. Zeeb 
62246b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
62256b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
62266b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
62276b4cac81SBjoern A. Zeeb 
62286b4cac81SBjoern A. Zeeb 	return (skb);
62296b4cac81SBjoern A. Zeeb }
62306b4cac81SBjoern A. Zeeb 
62316b4cac81SBjoern A. Zeeb struct wireless_dev *
62326b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
62336b4cac81SBjoern A. Zeeb {
62346b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62356b4cac81SBjoern A. Zeeb 
62366b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
62376b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
62386b4cac81SBjoern A. Zeeb }
62396b4cac81SBjoern A. Zeeb 
62406b4cac81SBjoern A. Zeeb void
62416b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
62426b4cac81SBjoern A. Zeeb {
62436b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62446b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
62456b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
62466b4cac81SBjoern A. Zeeb 	int arg;
62476b4cac81SBjoern A. Zeeb 
62486b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
62496b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
62506b4cac81SBjoern A. Zeeb 
62516b4cac81SBjoern A. Zeeb 	/*
6252f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
62536b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
62546b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
62556b4cac81SBjoern A. Zeeb 	 */
6256f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
6257bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
62586b4cac81SBjoern A. Zeeb 
6259018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6260018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
62616b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
62626b4cac81SBjoern A. Zeeb }
62636b4cac81SBjoern A. Zeeb 
6264bb81db90SBjoern A. Zeeb void
6265bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
6266bb81db90SBjoern A. Zeeb {
6267bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6268bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
6269bb81db90SBjoern A. Zeeb 
6270bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
6271bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
6272bb81db90SBjoern A. Zeeb 
6273bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6274bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
62753540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
6276bb81db90SBjoern A. Zeeb }
6277bb81db90SBjoern A. Zeeb 
62785edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
62795edde07cSBjoern A. Zeeb 
62805a9a0d78SBjoern A. Zeeb void
62815a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
62825a9a0d78SBjoern A. Zeeb {
62835a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62845a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62855a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
62865a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
62875a9a0d78SBjoern A. Zeeb 
62885a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
62895a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
62905a9a0d78SBjoern A. Zeeb 
62915a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
62925a9a0d78SBjoern A. Zeeb 
62935a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
62945a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
62955a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
62965a9a0d78SBjoern A. Zeeb 	else
62975a9a0d78SBjoern A. Zeeb 		ac_count = 1;
62985a9a0d78SBjoern A. Zeeb 
62995a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
63005a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
63015a9a0d78SBjoern A. Zeeb 
63025a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
63035a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
63045a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
63055a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
63060d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63075a9a0d78SBjoern A. Zeeb 				/*
63085a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
63095a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
63105a9a0d78SBjoern A. Zeeb 				 */
63110d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
63120d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
63135a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
63145a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
63155a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
63165a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
63170d2cb6a6SBjoern A. Zeeb #endif
63185a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
63195a9a0d78SBjoern A. Zeeb 			}
63205a9a0d78SBjoern A. Zeeb 		}
63215a9a0d78SBjoern A. Zeeb 	}
63225a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
63235a9a0d78SBjoern A. Zeeb }
63245a9a0d78SBjoern A. Zeeb 
63255a9a0d78SBjoern A. Zeeb void
63265a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
63275a9a0d78SBjoern A. Zeeb {
63285a9a0d78SBjoern A. Zeeb 	int i;
63295a9a0d78SBjoern A. Zeeb 
63305a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
63315a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
63325a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
63335a9a0d78SBjoern A. Zeeb }
63345a9a0d78SBjoern A. Zeeb 
63355a9a0d78SBjoern A. Zeeb 
63365a9a0d78SBjoern A. Zeeb static void
63375a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
63385a9a0d78SBjoern A. Zeeb {
63395a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63405a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
63415a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
63425a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
63435a9a0d78SBjoern A. Zeeb 
63445a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
63455a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
63465a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
63475a9a0d78SBjoern A. Zeeb 	else
63485a9a0d78SBjoern A. Zeeb 		ac_count = 1;
63495a9a0d78SBjoern A. Zeeb 
63505a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
63515a9a0d78SBjoern A. Zeeb 
63525a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
63535a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
63545a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
63555a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
63565a9a0d78SBjoern A. Zeeb 
63575a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
63585a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
63595a9a0d78SBjoern A. Zeeb 
63605a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
63615a9a0d78SBjoern A. Zeeb 
63625a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
63635a9a0d78SBjoern A. Zeeb 
63640d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63655a9a0d78SBjoern A. Zeeb 				/*
63665a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
63675a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
63685a9a0d78SBjoern A. Zeeb 				 */
63690d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
63700d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
63715a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
63725a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
63735a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
63745a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
63750d2cb6a6SBjoern A. Zeeb #endif
63765a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
63775a9a0d78SBjoern A. Zeeb 
63785a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_LOCK(lvif);
63795a9a0d78SBjoern A. Zeeb 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
63805a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
63815a9a0d78SBjoern A. Zeeb 
63825a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
63835a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
63845a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
63855a9a0d78SBjoern A. Zeeb 
63865a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
63875a9a0d78SBjoern A. Zeeb 							continue;
63885a9a0d78SBjoern A. Zeeb 
63895a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
63905a9a0d78SBjoern A. Zeeb 							continue;
63915a9a0d78SBjoern A. Zeeb 
63925a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
63935a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
63945a9a0d78SBjoern A. Zeeb 							continue;
63955a9a0d78SBjoern A. Zeeb 
63965a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
63975a9a0d78SBjoern A. Zeeb 
63985a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
63995a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
64005a9a0d78SBjoern A. Zeeb 					}
64015a9a0d78SBjoern A. Zeeb 				}
64025a9a0d78SBjoern A. Zeeb 				LKPI_80211_LVIF_UNLOCK(lvif);
64035a9a0d78SBjoern A. Zeeb 			}
64045a9a0d78SBjoern A. Zeeb 		}
64055a9a0d78SBjoern A. Zeeb 	}
64065a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
64075a9a0d78SBjoern A. Zeeb }
64085a9a0d78SBjoern A. Zeeb 
64095a9a0d78SBjoern A. Zeeb void
64105a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
64115a9a0d78SBjoern A. Zeeb {
64125a9a0d78SBjoern A. Zeeb 	int i;
64135a9a0d78SBjoern A. Zeeb 
64145a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
64155a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
64165a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
64175a9a0d78SBjoern A. Zeeb }
64185a9a0d78SBjoern A. Zeeb 
64195a9a0d78SBjoern A. Zeeb void
64205a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
64215a9a0d78SBjoern A. Zeeb {
64225a9a0d78SBjoern A. Zeeb 
64235a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
64245a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
64255a9a0d78SBjoern A. Zeeb 
64265a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
64275a9a0d78SBjoern A. Zeeb }
64285a9a0d78SBjoern A. Zeeb 
64295a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
64305a9a0d78SBjoern A. Zeeb void
64315a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
64325a9a0d78SBjoern A. Zeeb {
64335a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64345a9a0d78SBjoern A. Zeeb 
64355a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
64365a9a0d78SBjoern A. Zeeb 
64375a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
64385a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
64395a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
64405a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
64415a9a0d78SBjoern A. Zeeb }
64425a9a0d78SBjoern A. Zeeb 
64435a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
64445a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
64455a9a0d78SBjoern A. Zeeb {
64465a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64475a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
64485a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
64495a9a0d78SBjoern A. Zeeb 
64505a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
64515a9a0d78SBjoern A. Zeeb 	txq = NULL;
64525a9a0d78SBjoern A. Zeeb 
64535a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
64545a9a0d78SBjoern A. Zeeb 
64555a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
64565a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
64575a9a0d78SBjoern A. Zeeb 		goto out;
64585a9a0d78SBjoern A. Zeeb 
64595a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
64605a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
64615a9a0d78SBjoern A. Zeeb 		goto out;
64625a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
64635a9a0d78SBjoern A. Zeeb 		goto out;
64645a9a0d78SBjoern A. Zeeb 
64655a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
64665a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
64675a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
64685a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
64695a9a0d78SBjoern A. Zeeb 
64705a9a0d78SBjoern A. Zeeb out:
64715a9a0d78SBjoern A. Zeeb 	return (txq);
64725a9a0d78SBjoern A. Zeeb }
64735a9a0d78SBjoern A. Zeeb 
64745a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
64755a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
64765a9a0d78SBjoern A. Zeeb {
64775a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64785a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
6479eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
64805a9a0d78SBjoern A. Zeeb 
64815a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
64825a9a0d78SBjoern A. Zeeb 
64835a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
64845a9a0d78SBjoern A. Zeeb 
64855a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
6486eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
6487eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
6488eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
6489eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
64905a9a0d78SBjoern A. Zeeb 		goto out;
64915a9a0d78SBjoern A. Zeeb 
649241b746e0SAustin Shafer 	/*
649341b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
649441b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
649541b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
649641b746e0SAustin Shafer 	 */
649741b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
64985a9a0d78SBjoern A. Zeeb 		goto out;
64995a9a0d78SBjoern A. Zeeb 
65005a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
65015a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
65025a9a0d78SBjoern A. Zeeb out:
65035a9a0d78SBjoern A. Zeeb 	return;
65045a9a0d78SBjoern A. Zeeb }
65055a9a0d78SBjoern A. Zeeb 
6506eac3646fSBjoern A. Zeeb void
6507eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
6508eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
6509eac3646fSBjoern A. Zeeb {
6510eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6511eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
6512eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
6513eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
6514eac3646fSBjoern A. Zeeb 
6515eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6516eac3646fSBjoern A. Zeeb 
6517eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
6518eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
6519eac3646fSBjoern A. Zeeb 	do {
6520eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
6521eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
6522eac3646fSBjoern A. Zeeb 			break;
6523eac3646fSBjoern A. Zeeb 
6524eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
6525eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
6526eac3646fSBjoern A. Zeeb 		do {
6527eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6528eac3646fSBjoern A. Zeeb 			if (skb == NULL)
6529eac3646fSBjoern A. Zeeb 				break;
6530eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
6531eac3646fSBjoern A. Zeeb 		} while(1);
6532eac3646fSBjoern A. Zeeb 
6533eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
6534eac3646fSBjoern A. Zeeb 	} while (1);
6535eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
6536eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6537eac3646fSBjoern A. Zeeb }
6538eac3646fSBjoern A. Zeeb 
65395a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
65405a9a0d78SBjoern A. Zeeb 
65415edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
65425edde07cSBjoern A. Zeeb 	u_int refcnt;
65435edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
65445edde07cSBjoern A. Zeeb };
65455edde07cSBjoern A. Zeeb 
65465edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
65475edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
65485edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
65495edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
65505edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
65515edde07cSBjoern A. Zeeb 	size_t ssid_len;
65525edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
65535edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
65545edde07cSBjoern A. Zeeb 
65555edde07cSBjoern A. Zeeb 	/*
65565edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
65575edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
65585edde07cSBjoern A. Zeeb 	 */
65595edde07cSBjoern A. Zeeb 	bool match;
65605edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
65615edde07cSBjoern A. Zeeb };
65625edde07cSBjoern A. Zeeb 
65635edde07cSBjoern A. Zeeb static void
65645edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
65655edde07cSBjoern A. Zeeb {
65665edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
65675edde07cSBjoern A. Zeeb 	size_t ielen;
65685edde07cSBjoern A. Zeeb 
65695edde07cSBjoern A. Zeeb 	lookup = arg;
65705edde07cSBjoern A. Zeeb 
65715edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
65725edde07cSBjoern A. Zeeb 	if (lookup->match)
65735edde07cSBjoern A. Zeeb 		return;
65745edde07cSBjoern A. Zeeb 
65755edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
65765edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
65775edde07cSBjoern A. Zeeb 		return;
65785edde07cSBjoern A. Zeeb 
65795edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
65805edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
65815edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
65825edde07cSBjoern A. Zeeb 		return;
65835edde07cSBjoern A. Zeeb 	}
65845edde07cSBjoern A. Zeeb 
65855edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
65865edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
65875edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
65885edde07cSBjoern A. Zeeb 		return;
65895edde07cSBjoern A. Zeeb 	}
65905edde07cSBjoern A. Zeeb 
65915edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
65925edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
65935edde07cSBjoern A. Zeeb 
65945edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
65955edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
65965edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
65975edde07cSBjoern A. Zeeb 			return;
65985edde07cSBjoern A. Zeeb 	}
65995edde07cSBjoern A. Zeeb 
66005edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
66015edde07cSBjoern A. Zeeb 		return;
66025edde07cSBjoern A. Zeeb 
66035edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
66045edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
66055edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
66065edde07cSBjoern A. Zeeb 			return;
66075edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
66085edde07cSBjoern A. Zeeb 			return;
66095edde07cSBjoern A. Zeeb 	}
66105edde07cSBjoern A. Zeeb 
66115edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
66125edde07cSBjoern A. Zeeb 
66135edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
66145edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
66155edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
66165edde07cSBjoern A. Zeeb 		return;
66175edde07cSBjoern A. Zeeb 
66185edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
66195edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
66205edde07cSBjoern A. Zeeb 	if (ielen)
66215edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
66225edde07cSBjoern A. Zeeb 
66235edde07cSBjoern A. Zeeb 	lookup->match = true;
66245edde07cSBjoern A. Zeeb }
66255edde07cSBjoern A. Zeeb 
66265edde07cSBjoern A. Zeeb struct cfg80211_bss *
66275edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
66285edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
66295edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
66305edde07cSBjoern A. Zeeb {
66315edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
66325edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
66335edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
66345edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
66355edde07cSBjoern A. Zeeb 
66365edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
66375edde07cSBjoern A. Zeeb 
66385edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
66395edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
66405edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
66415edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
66425edde07cSBjoern A. Zeeb 		return (NULL);
66435edde07cSBjoern A. Zeeb 	}
66445edde07cSBjoern A. Zeeb 
66455edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
66465edde07cSBjoern A. Zeeb 	lookup.chan = chan;
66475edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
66485edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
66495edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
66505edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
66515edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
66525edde07cSBjoern A. Zeeb 	lookup.match = false;
66535edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
66545edde07cSBjoern A. Zeeb 
66555edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
66565edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
66575edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
66585edde07cSBjoern A. Zeeb 	if (!lookup.match) {
66595edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
66605edde07cSBjoern A. Zeeb 		return (NULL);
66615edde07cSBjoern A. Zeeb 	}
66625edde07cSBjoern A. Zeeb 
66635edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
66645edde07cSBjoern A. Zeeb 	return (&lbss->bss);
66655edde07cSBjoern A. Zeeb }
66665edde07cSBjoern A. Zeeb 
66675edde07cSBjoern A. Zeeb void
66685edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
66695edde07cSBjoern A. Zeeb {
66705edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
66715edde07cSBjoern A. Zeeb 
66725edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
66735edde07cSBjoern A. Zeeb 
66745edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
66755edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
66765edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
66775edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
66785edde07cSBjoern A. Zeeb 	}
66795edde07cSBjoern A. Zeeb }
66805edde07cSBjoern A. Zeeb 
66815edde07cSBjoern A. Zeeb void
66825edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
66835edde07cSBjoern A. Zeeb {
66845edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
66855edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
66865edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
66875edde07cSBjoern A. Zeeb 
66885edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
66895edde07cSBjoern A. Zeeb 	ic = lhw->ic;
66905edde07cSBjoern A. Zeeb 
66915edde07cSBjoern A. Zeeb 	/*
66925edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
66935edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
66945edde07cSBjoern A. Zeeb 	 */
66955edde07cSBjoern A. Zeeb 	if (ic == NULL ||
66965edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
66975edde07cSBjoern A. Zeeb 		return;
66985edde07cSBjoern A. Zeeb 
66995edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
67005edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
67015edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
67025edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
67035edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
67045edde07cSBjoern A. Zeeb }
67055edde07cSBjoern A. Zeeb 
67065edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
67075edde07cSBjoern A. Zeeb 
6708ac1d519cSBjoern A. Zeeb /*
6709ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
6710ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
6711ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
6712ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
6713ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
6714ac1d519cSBjoern A. Zeeb  */
6715ac1d519cSBjoern A. Zeeb 
6716ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
6717ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
6718ac1d519cSBjoern A. Zeeb {
6719ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
6720ac1d519cSBjoern A. Zeeb 	uint32_t changed;
6721ac1d519cSBjoern A. Zeeb 	int error;
6722ac1d519cSBjoern A. Zeeb 
6723ac1d519cSBjoern A. Zeeb 	changed = 0;
6724ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
6725ac1d519cSBjoern A. Zeeb 		cd = NULL;
6726ac1d519cSBjoern A. Zeeb 	else
6727ac1d519cSBjoern A. Zeeb 		cd = &new->def;
6728ac1d519cSBjoern A. Zeeb 
6729ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
6730ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
6731ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
6732ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
6733ac1d519cSBjoern A. Zeeb 	}
6734ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
6735ac1d519cSBjoern A. Zeeb 
6736ac1d519cSBjoern A. Zeeb 	if (changed == 0)
6737ac1d519cSBjoern A. Zeeb 		return (0);
6738ac1d519cSBjoern A. Zeeb 
6739ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
6740ac1d519cSBjoern A. Zeeb 	return (error);
6741ac1d519cSBjoern A. Zeeb }
6742ac1d519cSBjoern A. Zeeb 
6743ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6744ac1d519cSBjoern A. Zeeb 
67456b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
67466b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
67476b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6748