xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision c816f64e66a0d21196ede35da3eca19e54b59a03)
16b4cac81SBjoern A. Zeeb /*-
2ec6185c5SBjoern 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 
4211db70b6SBjoern A. Zeeb /*
4311db70b6SBjoern A. Zeeb  * TODO:
4411db70b6SBjoern A. Zeeb  * - lots :)
4511db70b6SBjoern A. Zeeb  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
4611db70b6SBjoern A. Zeeb  */
4711db70b6SBjoern A. Zeeb 
486b4cac81SBjoern A. Zeeb #include <sys/param.h>
496b4cac81SBjoern A. Zeeb #include <sys/types.h>
506b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
516b4cac81SBjoern A. Zeeb #include <sys/errno.h>
526b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
536b4cac81SBjoern A. Zeeb #include <sys/module.h>
546b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
5540839418SBjoern A. Zeeb #include <sys/sbuf.h>
566b4cac81SBjoern A. Zeeb #include <sys/socket.h>
576b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
586b4cac81SBjoern A. Zeeb #include <sys/queue.h>
596b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
60527687a9SBjoern A. Zeeb #include <sys/libkern.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net/if.h>
636b4cac81SBjoern A. Zeeb #include <net/if_var.h>
646b4cac81SBjoern A. Zeeb #include <net/if_media.h>
656b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
686b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
696b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
706b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
719fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
746b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
756b4cac81SBjoern A. Zeeb 
766b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
77a8f735a6SBjoern A. Zeeb #include <linux/rculist.h>
786b4cac81SBjoern A. Zeeb #include "linux_80211.h"
796b4cac81SBjoern A. Zeeb 
804a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
8111db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
8275d23d88SBjoern A. Zeeb #define	LKPI_80211_HT
832c44f1ffSBjoern A. Zeeb #define	LKPI_80211_VHT
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
31740839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
31840839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
31940839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32140839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32240839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
32340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
32440839418SBjoern A. Zeeb 
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
32640839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
32940839418SBjoern A. Zeeb 
33040839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33140839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33240839418SBjoern A. Zeeb 
33340839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
33440839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
33540839418SBjoern A. Zeeb 
33640839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
33740839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
33840839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
33940839418SBjoern A. Zeeb 		}
34040839418SBjoern A. Zeeb 
34140839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
34440839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
34540839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
34640839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
34740839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
34840839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
34940839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35040839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35140839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35240839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35340839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
35440839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
35540839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
35640839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35740839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
35840839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
35940839418SBjoern A. Zeeb 	}
36040839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36140839418SBjoern A. Zeeb 
36240839418SBjoern A. Zeeb 	sbuf_finish(&s);
36340839418SBjoern A. Zeeb 	sbuf_delete(&s);
36440839418SBjoern A. Zeeb 
36540839418SBjoern A. Zeeb 	return (0);
36640839418SBjoern A. Zeeb }
36740839418SBjoern A. Zeeb 
36862d51a43SBjoern A. Zeeb static enum ieee80211_sta_rx_bw
36962d51a43SBjoern A. Zeeb lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
37062d51a43SBjoern A. Zeeb {
37162d51a43SBjoern A. Zeeb 	switch (cw) {
37262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_320:
37362d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_320);
37462d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_160:
37562d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80P80:
37662d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_160);
37762d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80:
37862d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_80);
37962d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_40:
38062d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_40);
38162d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20:
38262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20_NOHT:
38362d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
38462d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_5:
38562d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_10:
38662d51a43SBjoern A. Zeeb 		/* Unsupported input. */
38762d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
38862d51a43SBjoern A. Zeeb 	}
38962d51a43SBjoern A. Zeeb }
39062d51a43SBjoern A. Zeeb 
39162d51a43SBjoern A. Zeeb static enum nl80211_chan_width
39262d51a43SBjoern A. Zeeb lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bw rx_bw)
39362d51a43SBjoern A. Zeeb {
39462d51a43SBjoern A. Zeeb 	switch (rx_bw) {
39562d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_20:
39662d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_20);	/* _NOHT */
39762d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_40:
39862d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_40);
39962d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_80:
40062d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_80);
40162d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_160:
40262d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_160); /* 80P80 */
40362d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_320:
40462d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_320);
40562d51a43SBjoern A. Zeeb 	}
40662d51a43SBjoern A. Zeeb }
40762d51a43SBjoern A. Zeeb 
40862d51a43SBjoern A. Zeeb static void
40962d51a43SBjoern A. Zeeb lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
41062d51a43SBjoern A. Zeeb     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
41162d51a43SBjoern A. Zeeb {
41262d51a43SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
41362d51a43SBjoern A. Zeeb 	enum ieee80211_sta_rx_bw old_bw;
41462d51a43SBjoern A. Zeeb 	uint32_t changed;
41562d51a43SBjoern A. Zeeb 
41662d51a43SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
41762d51a43SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
41862d51a43SBjoern A. Zeeb 	if (chanctx_conf == NULL)
41962d51a43SBjoern A. Zeeb 		return;
42062d51a43SBjoern A. Zeeb 
42162d51a43SBjoern A. Zeeb 	old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
42262d51a43SBjoern A. Zeeb 	if (old_bw == sta->deflink.bandwidth)
42362d51a43SBjoern A. Zeeb 		return;
42462d51a43SBjoern A. Zeeb 
42562d51a43SBjoern A. Zeeb 	chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
42662d51a43SBjoern A. Zeeb 	if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
42762d51a43SBjoern A. Zeeb 	    !sta->deflink.ht_cap.ht_supported)
42862d51a43SBjoern A. Zeeb 		chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
42962d51a43SBjoern A. Zeeb 
43062d51a43SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
43162d51a43SBjoern A. Zeeb 
43262d51a43SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
43362d51a43SBjoern A. Zeeb 
43462d51a43SBjoern A. Zeeb 	changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
43562d51a43SBjoern A. Zeeb 	changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
43662d51a43SBjoern A. Zeeb 	lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
43762d51a43SBjoern A. Zeeb }
43862d51a43SBjoern A. Zeeb 
4399fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
4409fb91463SBjoern A. Zeeb static void
44162d51a43SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
44262d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
4439fb91463SBjoern A. Zeeb {
4449fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
4459fb91463SBjoern A. Zeeb 	uint8_t *ie;
4469fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
4479fb91463SBjoern A. Zeeb 	int i, rx_nss;
4489fb91463SBjoern A. Zeeb 
449f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
450f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
4519fb91463SBjoern A. Zeeb 		return;
452f9c7a07dSBjoern A. Zeeb 	}
4539fb91463SBjoern A. Zeeb 
4549fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
4559fb91463SBjoern A. Zeeb 
4569fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
4579fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
4589fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4599fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
4609fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
4619fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
4629fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
4639fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
4649fb91463SBjoern A. Zeeb 
4659fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
4669fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
4679fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
4689fb91463SBjoern A. Zeeb 		ie += 4;
4699fb91463SBjoern A. Zeeb 	ie += 2;
4709fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4719fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4729fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4739fb91463SBjoern A. Zeeb 
47462d51a43SBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
47562d51a43SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
476f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
4773e022a91SBjoern A. Zeeb 	else
4783e022a91SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
479f9c7a07dSBjoern A. Zeeb 
480f9c7a07dSBjoern A. Zeeb 	/*
481f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
482f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
483f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
484f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
485f9c7a07dSBjoern A. Zeeb 	 */
4869fb91463SBjoern A. Zeeb 	rx_nss = 0;
487f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4889fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4899fb91463SBjoern A. Zeeb 			rx_nss++;
4909fb91463SBjoern A. Zeeb 	}
491f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
492f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
4939fb91463SBjoern A. Zeeb 
494f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
495f9c7a07dSBjoern A. Zeeb 
496f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
497f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
498f9c7a07dSBjoern A. Zeeb 	else
499f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
500f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
501f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
502f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
503f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
504f9c7a07dSBjoern A. Zeeb 	}
505f9c7a07dSBjoern A. Zeeb #endif
5069fb91463SBjoern A. Zeeb }
5079fb91463SBjoern A. Zeeb #endif
5089fb91463SBjoern A. Zeeb 
5099fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5109fb91463SBjoern A. Zeeb static void
51162d51a43SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
51262d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
5139fb91463SBjoern A. Zeeb {
514f9c7a07dSBjoern A. Zeeb 	uint32_t width;
515f9c7a07dSBjoern A. Zeeb 	int rx_nss;
516f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
517f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
5189fb91463SBjoern A. Zeeb 
5195393cd34SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
5205393cd34SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
521f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
5229fb91463SBjoern A. Zeeb 		return;
523f9c7a07dSBjoern A. Zeeb 	}
5249fb91463SBjoern A. Zeeb 
525f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
526f9c7a07dSBjoern A. Zeeb 
527f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
528f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
529f9c7a07dSBjoern A. Zeeb 
5303e022a91SBjoern A. Zeeb 	/*
5313e022a91SBjoern A. Zeeb 	 * If VHT20/40 are selected do not update the bandwidth
5323e022a91SBjoern A. Zeeb 	 * from HT but stya on VHT.
5333e022a91SBjoern A. Zeeb 	 */
5343e022a91SBjoern A. Zeeb 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
5353e022a91SBjoern A. Zeeb 		goto skip_bw;
5363e022a91SBjoern A. Zeeb 
537f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
538f9c7a07dSBjoern A. Zeeb 	switch (width) {
539f9c7a07dSBjoern A. Zeeb #if 0
540f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
541f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
5429fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
543f9c7a07dSBjoern A. Zeeb 		break;
544f9c7a07dSBjoern A. Zeeb #endif
545f9c7a07dSBjoern A. Zeeb 	default:
546f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
547f9c7a07dSBjoern A. Zeeb #if 0
548f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
549f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
550f9c7a07dSBjoern A. Zeeb 		else
551f9c7a07dSBjoern A. Zeeb #endif
5529fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
5539fb91463SBjoern A. Zeeb 	}
5543e022a91SBjoern A. Zeeb skip_bw:
555f9c7a07dSBjoern A. Zeeb 
556f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
557f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
558f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
559f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
560f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
561f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
562f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
563f9c7a07dSBjoern A. Zeeb 			break;
564f9c7a07dSBjoern A. Zeeb 		}
565f9c7a07dSBjoern A. Zeeb 	}
566f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
567f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
568f9c7a07dSBjoern A. Zeeb 
569f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
570f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
571f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
572f9c7a07dSBjoern A. Zeeb 		break;
573f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
574f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
575f9c7a07dSBjoern A. Zeeb 		break;
576f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
577f9c7a07dSBjoern A. Zeeb 	default:
578f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
579f9c7a07dSBjoern A. Zeeb 		break;
580f9c7a07dSBjoern A. Zeeb 	}
5819fb91463SBjoern A. Zeeb }
5829fb91463SBjoern A. Zeeb #endif
5839fb91463SBjoern A. Zeeb 
584d9f59799SBjoern A. Zeeb static void
58562d51a43SBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
58662d51a43SBjoern A. Zeeb     struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
587f9c7a07dSBjoern A. Zeeb {
588f9c7a07dSBjoern A. Zeeb 
589f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
59062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(vif, sta, ni);
591f9c7a07dSBjoern A. Zeeb #endif
592f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
59362d51a43SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(vif, sta, ni);
594f9c7a07dSBjoern A. Zeeb #endif
59562d51a43SBjoern A. Zeeb 	/*
59662d51a43SBjoern A. Zeeb 	 * We are also called from node allocation which net80211
59762d51a43SBjoern A. Zeeb 	 * can do even on `ifconfig down`; in that case the chanctx
59862d51a43SBjoern A. Zeeb 	 * may still be valid and we get a discrepancy between
59962d51a43SBjoern A. Zeeb 	 * sta and chanctx.  Thus do not try to update the chanctx
60062d51a43SBjoern A. Zeeb 	 * when called from lkpi_lsta_alloc().
60162d51a43SBjoern A. Zeeb 	 */
60262d51a43SBjoern A. Zeeb 	if (updchnctx)
60362d51a43SBjoern A. Zeeb 		lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
604f9c7a07dSBjoern A. Zeeb }
605f9c7a07dSBjoern A. Zeeb 
606389265e3SBjoern A. Zeeb static uint8_t
607389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
608389265e3SBjoern A. Zeeb {
609389265e3SBjoern A. Zeeb 	uint8_t chains;
610389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
611389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
612389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
613389265e3SBjoern A. Zeeb 
614389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
615389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
616389265e3SBjoern A. Zeeb #endif
617389265e3SBjoern A. Zeeb 
618389265e3SBjoern A. Zeeb 	chains = 1;
619389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
620389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
621389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
622389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
623389265e3SBjoern A. Zeeb #endif
624389265e3SBjoern A. Zeeb 
625389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
626389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
627389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
628389265e3SBjoern A. Zeeb #endif
629389265e3SBjoern A. Zeeb 
630389265e3SBjoern A. Zeeb 	return (chains);
631389265e3SBjoern A. Zeeb }
632389265e3SBjoern A. Zeeb 
633f9c7a07dSBjoern A. Zeeb static void
63467674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
63567674c1cSBjoern A. Zeeb     const char *_f, int _l)
636d9f59799SBjoern A. Zeeb {
637d9f59799SBjoern A. Zeeb 
6389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6399d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
640d9f59799SBjoern A. Zeeb 		return;
641d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
642d9f59799SBjoern A. Zeeb 		return;
643d9f59799SBjoern A. Zeeb 
644d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
64567674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
64667674c1cSBjoern A. Zeeb 	if (ni != NULL)
64767674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
648d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
649d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
65011db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
6519d9ba2b7SBjoern A. Zeeb #endif
652d9f59799SBjoern A. Zeeb }
653d9f59799SBjoern A. Zeeb 
654d9f59799SBjoern A. Zeeb static void
655d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
656d9f59799SBjoern A. Zeeb {
657d9f59799SBjoern A. Zeeb 
658cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(lsta->hw->wiphy);
659d9f59799SBjoern A. Zeeb 
660a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
661a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
662a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
663d9f59799SBjoern A. Zeeb }
664d9f59799SBjoern A. Zeeb 
6654f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
6664f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
6674f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
6684f61ef8bSBjoern A. Zeeb {
6694f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
6704f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
6714f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
6724f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
673b6b352e4SBjoern A. Zeeb 	int band, i, tid;
6744f61ef8bSBjoern A. Zeeb 
6754f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
6764f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
6774f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
6784f61ef8bSBjoern A. Zeeb 		return (NULL);
6794f61ef8bSBjoern A. Zeeb 
680a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
6814f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
6824f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
6834f61ef8bSBjoern A. Zeeb 	/*
6840936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
6850936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
6860936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
6870936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
6880936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
6890936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
6900936c648SBjoern A. Zeeb 	 * two separate allocations.
6914f61ef8bSBjoern A. Zeeb 	 */
6920936c648SBjoern A. Zeeb 	lsta->ni = ni;
6934f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
6944f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
6954f61ef8bSBjoern A. Zeeb 
6964f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
6974f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
6984f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
6994f61ef8bSBjoern A. Zeeb 
7004f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
701b6b352e4SBjoern A. Zeeb 
702b6b352e4SBjoern A. Zeeb 	/* TXQ */
7034f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
7044f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
7054f61ef8bSBjoern A. Zeeb 
706d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
7074f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
7084f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
7094f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
7104f61ef8bSBjoern A. Zeeb 			goto cleanup;
7114f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
7124f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
713d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
714d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
715d0d29110SBjoern A. Zeeb 				continue;
716d0d29110SBjoern A. Zeeb 			}
717d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
7184f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
7194f61ef8bSBjoern A. Zeeb 		} else {
720fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
7214f61ef8bSBjoern A. Zeeb 		}
722d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
7230cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
724d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
7254f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
7264f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
7270cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
728d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
729eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
7304f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
7314f61ef8bSBjoern A. Zeeb 	}
7324f61ef8bSBjoern A. Zeeb 
733b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
734b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
735b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
736b6b352e4SBjoern A. Zeeb 
737b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
738b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
739b6b352e4SBjoern A. Zeeb 			continue;
740b6b352e4SBjoern A. Zeeb 
741b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
74273cd1c5dSBjoern A. Zeeb 			switch (band) {
74373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
74473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
74573cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
74673cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
74773cd1c5dSBjoern A. Zeeb 				case 110:
74873cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
74973cd1c5dSBjoern A. Zeeb 				case 55:
75073cd1c5dSBjoern A. Zeeb 				case 20:
75173cd1c5dSBjoern A. Zeeb 				case 10:
752b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
75373cd1c5dSBjoern A. Zeeb 					break;
75473cd1c5dSBjoern A. Zeeb 				}
75573cd1c5dSBjoern A. Zeeb 				break;
75673cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
75773cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
75873cd1c5dSBjoern A. Zeeb 				case 240:
75973cd1c5dSBjoern A. Zeeb 				case 120:
76073cd1c5dSBjoern A. Zeeb 				case 60:
76173cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
76273cd1c5dSBjoern A. Zeeb 					break;
76373cd1c5dSBjoern A. Zeeb 				}
76473cd1c5dSBjoern A. Zeeb 				break;
76573cd1c5dSBjoern A. Zeeb 			}
766b6b352e4SBjoern A. Zeeb 		}
767b6b352e4SBjoern A. Zeeb 	}
7689fb91463SBjoern A. Zeeb 
7696ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
7709fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
771f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
7729fb91463SBjoern A. Zeeb 
77362d51a43SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
77462d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
77562d51a43SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
7769fb91463SBjoern A. Zeeb 
777f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
778b6b352e4SBjoern A. Zeeb 
7796ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
7806ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
7816ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
7826ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
7836ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
7846ffb7bd4SBjoern A. Zeeb 	}
7856ffb7bd4SBjoern A. Zeeb 
7864f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
787fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
7884f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
7894f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
7900936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
7914f61ef8bSBjoern A. Zeeb 
7924f61ef8bSBjoern A. Zeeb 	return (lsta);
7934f61ef8bSBjoern A. Zeeb 
7944f61ef8bSBjoern A. Zeeb cleanup:
795eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
796eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
797eac3646fSBjoern A. Zeeb 
798eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
799eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
8004f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
801eac3646fSBjoern A. Zeeb 	}
8024f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8034f61ef8bSBjoern A. Zeeb 	return (NULL);
8044f61ef8bSBjoern A. Zeeb }
8054f61ef8bSBjoern A. Zeeb 
8060936c648SBjoern A. Zeeb static void
8070936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
8080936c648SBjoern A. Zeeb {
8090936c648SBjoern A. Zeeb 	struct mbuf *m;
8100936c648SBjoern A. Zeeb 
8110936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
8120936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
8130936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
8140936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
8150936c648SBjoern A. Zeeb 
8160936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
8170936c648SBjoern A. Zeeb 	IMPROVE();
8180936c648SBjoern A. Zeeb 
819fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
820fa4e4257SBjoern A. Zeeb 
821fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
8220936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
823fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
8240936c648SBjoern A. Zeeb 
8250936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
8260936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
8270936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
8280936c648SBjoern A. Zeeb 
8290936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
8300936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
8310936c648SBjoern A. Zeeb 	while (m != NULL) {
8320936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
8330936c648SBjoern A. Zeeb 
8340936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
8350936c648SBjoern A. Zeeb 		if (nim != NULL)
8360936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
8370936c648SBjoern A. Zeeb 		m_freem(m);
8380936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
8390936c648SBjoern A. Zeeb 	}
8400936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
8410936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
842fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
8430936c648SBjoern A. Zeeb 
8440936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
8450936c648SBjoern A. Zeeb 
8460936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
8470936c648SBjoern A. Zeeb 
8480936c648SBjoern A. Zeeb 	/* Free lsta. */
8490936c648SBjoern A. Zeeb 	lsta->ni = NULL;
8500936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
8510936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8520936c648SBjoern A. Zeeb }
8530936c648SBjoern A. Zeeb 
8540936c648SBjoern A. Zeeb 
8556b4cac81SBjoern A. Zeeb static enum nl80211_band
8566b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
8576b4cac81SBjoern A. Zeeb {
8586b4cac81SBjoern A. Zeeb 
8596b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
8606b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
8616b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
8626b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
8636b4cac81SBjoern A. Zeeb #ifdef __notyet__
8646b4cac81SBjoern A. Zeeb 	else if ()
8656b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
8666b4cac81SBjoern A. Zeeb 	else if ()
8676b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
8686b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
8696b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
8706b4cac81SBjoern A. Zeeb #endif
8716b4cac81SBjoern A. Zeeb 	else
8726b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
8736b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
8746b4cac81SBjoern A. Zeeb }
8756b4cac81SBjoern A. Zeeb 
8766b4cac81SBjoern A. Zeeb static uint32_t
8776b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
8786b4cac81SBjoern A. Zeeb {
8796b4cac81SBjoern A. Zeeb 
8806b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
8816b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
8826b4cac81SBjoern A. Zeeb 	switch (band) {
8836b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
8846b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
8856b4cac81SBjoern A. Zeeb 		break;
8866b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
8876b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
8886b4cac81SBjoern A. Zeeb 		break;
8896b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
8906b4cac81SBjoern A. Zeeb 		break;
8916b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
8926b4cac81SBjoern A. Zeeb 		break;
8936b4cac81SBjoern A. Zeeb 	default:
8946b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
8956b4cac81SBjoern A. Zeeb 		break;
8966b4cac81SBjoern A. Zeeb 	}
8976b4cac81SBjoern A. Zeeb 
8986b4cac81SBjoern A. Zeeb 	IMPROVE();
8996b4cac81SBjoern A. Zeeb 	return (0x00);
9006b4cac81SBjoern A. Zeeb }
9016b4cac81SBjoern A. Zeeb 
902e3a0b120SBjoern A. Zeeb #if 0
9036b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
9046b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
9056b4cac81SBjoern A. Zeeb {
9066b4cac81SBjoern A. Zeeb 
9076b4cac81SBjoern A. Zeeb 	switch (ac) {
9086b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
9096b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
9106b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
9116b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
9126b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
9136b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9146b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
9156b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
9166b4cac81SBjoern A. Zeeb 	default:
9176b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
9186b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9196b4cac81SBjoern A. Zeeb 	}
9206b4cac81SBjoern A. Zeeb }
921e3a0b120SBjoern A. Zeeb #endif
9226b4cac81SBjoern A. Zeeb 
9236b4cac81SBjoern A. Zeeb static enum nl80211_iftype
9246b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
9256b4cac81SBjoern A. Zeeb {
9266b4cac81SBjoern A. Zeeb 
9276b4cac81SBjoern A. Zeeb 	switch (opmode) {
9286b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
9296b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
9306b4cac81SBjoern A. Zeeb 		break;
9316b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
9326b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
9336b4cac81SBjoern A. Zeeb 		break;
9346b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
9356b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
9366b4cac81SBjoern A. Zeeb 		break;
9376b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
9386b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
9396b4cac81SBjoern A. Zeeb 		break;
9406b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
9416b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
9426b4cac81SBjoern A. Zeeb 		break;
9436b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
9446b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
9456b4cac81SBjoern A. Zeeb 		break;
9466b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
9476b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9486b4cac81SBjoern A. Zeeb 	default:
9496b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
9506b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9516b4cac81SBjoern A. Zeeb 	}
9526b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
9536b4cac81SBjoern A. Zeeb }
9546b4cac81SBjoern A. Zeeb 
955b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
95612a511c8SBjoern A. Zeeb static const char *
95712a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
95812a511c8SBjoern A. Zeeb {
95912a511c8SBjoern A. Zeeb 
96012a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
96112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
96212a511c8SBjoern A. Zeeb 		return ("WEP40");
96312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
96412a511c8SBjoern A. Zeeb 		return ("TKIP");
96512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
96612a511c8SBjoern A. Zeeb 		return ("CCMP");
96712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
96812a511c8SBjoern A. Zeeb 		return ("WEP104");
96912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
97012a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
97112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
97212a511c8SBjoern A. Zeeb 		return ("GCMP");
97312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
97412a511c8SBjoern A. Zeeb 		return ("GCMP_256");
97512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
97612a511c8SBjoern A. Zeeb 		return ("CCMP_256");
97712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
97812a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
97912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
98012a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
98112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
98212a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
98312a511c8SBjoern A. Zeeb 	default:
98412a511c8SBjoern A. Zeeb 		return ("??");
98512a511c8SBjoern A. Zeeb 	}
98612a511c8SBjoern A. Zeeb }
98712a511c8SBjoern A. Zeeb 
9886b4cac81SBjoern A. Zeeb static uint32_t
9896b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
9906b4cac81SBjoern A. Zeeb {
9916b4cac81SBjoern A. Zeeb 
9926b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
9936b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
9946b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
9956b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
9966b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
9976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
998906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
9996b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
10006b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
10016b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
10026b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
10036b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
10046b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
10056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
10066b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
10076b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
100812a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
100912a511c8SBjoern A. Zeeb 		    __func__,
101012a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
101112a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
10126b4cac81SBjoern A. Zeeb 		break;
10136b4cac81SBjoern A. Zeeb 	default:
101412a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
101512a511c8SBjoern A. Zeeb 		    __func__,
101612a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
101712a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
10186b4cac81SBjoern A. Zeeb 	}
10196b4cac81SBjoern A. Zeeb 
10206b4cac81SBjoern A. Zeeb 	return (0);
10216b4cac81SBjoern A. Zeeb }
10226b4cac81SBjoern A. Zeeb 
10236b4cac81SBjoern A. Zeeb static uint32_t
10246b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
10256b4cac81SBjoern A. Zeeb {
10266b4cac81SBjoern A. Zeeb 
10276b4cac81SBjoern A. Zeeb 	switch (cipher) {
10286b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
10296b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
10306b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
10316b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
10326b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
10336b4cac81SBjoern A. Zeeb 		if (keylen < 8)
10346b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
10356b4cac81SBjoern A. Zeeb 		else
10366b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
10376b4cac81SBjoern A. Zeeb 		break;
10386b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
10396b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
10406b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
10416b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
10426b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
10436b4cac81SBjoern A. Zeeb 		break;
10446b4cac81SBjoern A. Zeeb 	default:
10456b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
10466b4cac81SBjoern A. Zeeb 	};
10476b4cac81SBjoern A. Zeeb 	return (0);
10486b4cac81SBjoern A. Zeeb }
10496b4cac81SBjoern A. Zeeb #endif
10506b4cac81SBjoern A. Zeeb 
10516b4cac81SBjoern A. Zeeb #ifdef __notyet__
10526b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
10536b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
10546b4cac81SBjoern A. Zeeb {
10556b4cac81SBjoern A. Zeeb 
10566b4cac81SBjoern A. Zeeb 	/*
10576b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
10586b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
10596b4cac81SBjoern A. Zeeb 	 */
10606b4cac81SBjoern A. Zeeb 	switch (state) {
10616b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
10626b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
10636b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
10646b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
10656b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
10666b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
10676b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
10686b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
10696b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
10706b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
10716b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
10726b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
10736b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
10746b4cac81SBjoern A. Zeeb 	default:
10756b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
10766b4cac81SBjoern A. Zeeb 	};
10776b4cac81SBjoern A. Zeeb 
10786b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
10796b4cac81SBjoern A. Zeeb }
10806b4cac81SBjoern A. Zeeb #endif
10816b4cac81SBjoern A. Zeeb 
10826b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
10836b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
10846b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
10856b4cac81SBjoern A. Zeeb {
10866b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10876b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
10886b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
10896b4cac81SBjoern A. Zeeb 	int i, nchans;
10906b4cac81SBjoern A. Zeeb 
10916b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10926b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
10936b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
10946b4cac81SBjoern A. Zeeb 		return (NULL);
10956b4cac81SBjoern A. Zeeb 
10966b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
10976b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
10986b4cac81SBjoern A. Zeeb 		return (NULL);
10996b4cac81SBjoern A. Zeeb 
11006b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
11016b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
11026b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
11036b4cac81SBjoern A. Zeeb 			return (&channels[i]);
11046b4cac81SBjoern A. Zeeb 	}
11056b4cac81SBjoern A. Zeeb 
11066b4cac81SBjoern A. Zeeb 	return (NULL);
11076b4cac81SBjoern A. Zeeb }
11086b4cac81SBjoern A. Zeeb 
11092ac8a218SBjoern A. Zeeb #if 0
11106b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11116b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
11126b4cac81SBjoern A. Zeeb {
11136b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
11146b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
11156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11166b4cac81SBjoern A. Zeeb 
11176b4cac81SBjoern A. Zeeb 	chan = NULL;
11186b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
11196b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
11206b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
11216b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
11226b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
11236b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
11246b4cac81SBjoern A. Zeeb 	else
11256b4cac81SBjoern A. Zeeb 		c = NULL;
11266b4cac81SBjoern A. Zeeb 
11276b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
11286b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
11296b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
11306b4cac81SBjoern A. Zeeb 	}
11316b4cac81SBjoern A. Zeeb 
11326b4cac81SBjoern A. Zeeb 	return (chan);
11336b4cac81SBjoern A. Zeeb }
11342ac8a218SBjoern A. Zeeb #endif
11356b4cac81SBjoern A. Zeeb 
11362e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
11372e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
11382e183d99SBjoern A. Zeeb {
11392e183d99SBjoern A. Zeeb 	enum nl80211_band band;
11402e183d99SBjoern A. Zeeb 
11412e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11422e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
11432e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
11442e183d99SBjoern A. Zeeb 		int i;
11452e183d99SBjoern A. Zeeb 
11462e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
11472e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
11482e183d99SBjoern A. Zeeb 			continue;
11492e183d99SBjoern A. Zeeb 
11502e183d99SBjoern A. Zeeb 		channels = supband->channels;
11512e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
11522e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
11532e183d99SBjoern A. Zeeb 				return (&channels[i]);
11542e183d99SBjoern A. Zeeb 		}
11552e183d99SBjoern A. Zeeb 	}
11562e183d99SBjoern A. Zeeb 
11572e183d99SBjoern A. Zeeb 	return (NULL);
11582e183d99SBjoern A. Zeeb }
11592e183d99SBjoern A. Zeeb 
1160b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
11616b4cac81SBjoern A. Zeeb static int
116211db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
116311db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
116411db70b6SBjoern A. Zeeb {
116511db70b6SBjoern A. Zeeb 	int error;
116611db70b6SBjoern A. Zeeb 
116711db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
116811db70b6SBjoern A. Zeeb 		return (0);
116911db70b6SBjoern A. Zeeb 
117011db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
117111db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
117211db70b6SBjoern A. Zeeb 
117311db70b6SBjoern A. Zeeb 	error = 0;
117411db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
117511db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
117611db70b6SBjoern A. Zeeb 		int err;
117711db70b6SBjoern A. Zeeb 
117811db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
117911db70b6SBjoern A. Zeeb 			continue;
118011db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
118111db70b6SBjoern A. Zeeb 
118211db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
118311db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
118411db70b6SBjoern A. Zeeb 		if (err != 0) {
118511db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
118611db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
118711db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
118811db70b6SBjoern A. Zeeb 			error++;
118911db70b6SBjoern A. Zeeb 
119011db70b6SBjoern A. Zeeb 			/*
119111db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
119211db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
119311db70b6SBjoern A. Zeeb 			 * crash (firmware).
119411db70b6SBjoern A. Zeeb 			 */
119511db70b6SBjoern A. Zeeb 			continue;
119611db70b6SBjoern A. Zeeb 		}
119711db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
119811db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
119911db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
120011db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
120111db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
120211db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
120311db70b6SBjoern A. Zeeb #endif
120411db70b6SBjoern A. Zeeb 
120511db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
120611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
120711db70b6SBjoern A. Zeeb 	}
120811db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
120911db70b6SBjoern A. Zeeb 	return (error);
121011db70b6SBjoern A. Zeeb }
121111db70b6SBjoern A. Zeeb 
121211db70b6SBjoern A. Zeeb static int
121311db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
12146b4cac81SBjoern A. Zeeb {
12156b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
12166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
121911db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12206b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12216b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12226b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12236b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
12246b4cac81SBjoern A. Zeeb 	int error;
12256b4cac81SBjoern A. Zeeb 
12266b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
12276b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
12286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
12296b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
12306b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
12316b4cac81SBjoern A. Zeeb 
123211db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
123311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
123411db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
123511db70b6SBjoern A. Zeeb 		return (0);
123611db70b6SBjoern A. Zeeb 	}
123711db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
123811db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
123911db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
124011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
124111db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
124211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
124311db70b6SBjoern A. Zeeb 		return (0);
124411db70b6SBjoern A. Zeeb 	}
124511db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
124611db70b6SBjoern A. Zeeb 
124711db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
124811db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
124911db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
125011db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
125111db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
125211db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
125311db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
125411db70b6SBjoern A. Zeeb #endif
125511db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
125611db70b6SBjoern A. Zeeb 		return (1);
125711db70b6SBjoern A. Zeeb 	}
125811db70b6SBjoern A. Zeeb 
125911db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
126011db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
126111db70b6SBjoern A. Zeeb 	if (kc == NULL) {
126211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
126311db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
126411db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
126511db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
126611db70b6SBjoern A. Zeeb #endif
126711db70b6SBjoern A. Zeeb 		error = 1;
126811db70b6SBjoern A. Zeeb 		goto out;
126911db70b6SBjoern A. Zeeb 	}
127011db70b6SBjoern A. Zeeb 
127111db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
127211db70b6SBjoern A. Zeeb 	if (error != 0) {
127311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
127411db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
127511db70b6SBjoern A. Zeeb 		error = 0;
127611db70b6SBjoern A. Zeeb 		goto out;
127711db70b6SBjoern A. Zeeb 	}
127811db70b6SBjoern A. Zeeb 
127911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
128011db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
128111db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
128211db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
128311db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
128411db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
128511db70b6SBjoern A. Zeeb #endif
128611db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
128711db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
128811db70b6SBjoern A. Zeeb 	error = 1;
128911db70b6SBjoern A. Zeeb out:
129011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
129111db70b6SBjoern A. Zeeb 	return (error);
129211db70b6SBjoern A. Zeeb }
129311db70b6SBjoern A. Zeeb 
129411db70b6SBjoern A. Zeeb static int
129511db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
129611db70b6SBjoern A. Zeeb {
129711db70b6SBjoern A. Zeeb 
129811db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
129911db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
130011db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
130111db70b6SBjoern A. Zeeb }
130211db70b6SBjoern A. Zeeb 
130311db70b6SBjoern A. Zeeb static int
130411db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
130511db70b6SBjoern A. Zeeb {
130611db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
130711db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
130811db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
130911db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
131011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
131111db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
131211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
131311db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
131411db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
131511db70b6SBjoern A. Zeeb 	uint32_t lcipher;
131611db70b6SBjoern A. Zeeb 	int error;
131711db70b6SBjoern A. Zeeb 
131811db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
131911db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
132011db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
132111db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
132211db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
132311db70b6SBjoern A. Zeeb 
132411db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
132511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
132611db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
132711db70b6SBjoern A. Zeeb 		return (0);
132811db70b6SBjoern A. Zeeb 	}
132911db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
133011db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
133111db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
133211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
133311db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
133411db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
133511db70b6SBjoern A. Zeeb 		return (0);
133611db70b6SBjoern A. Zeeb 	}
133711db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
133811db70b6SBjoern A. Zeeb 
133911db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
134011db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
134111db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
134211db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
134311db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
134411db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
134511db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
134611db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
134711db70b6SBjoern A. Zeeb 	}
134811db70b6SBjoern A. Zeeb 
134911db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
13506b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
135111db70b6SBjoern A. Zeeb 	switch (lcipher) {
135211db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
135311db70b6SBjoern A. Zeeb 		break;
135411db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
135511db70b6SBjoern A. Zeeb 	default:
135612a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
135712a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
135811db70b6SBjoern A. Zeeb 		IMPROVE();
135911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
136011db70b6SBjoern A. Zeeb 		return (0);
136111db70b6SBjoern A. Zeeb 	}
136211db70b6SBjoern A. Zeeb 
136311db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
136411db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
136511db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
136611db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
13676b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
13686b4cac81SBjoern A. Zeeb #if 0
13696b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
13706b4cac81SBjoern A. Zeeb #endif
13716b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
13726b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
13736b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
13746b4cac81SBjoern A. Zeeb 
137511db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
137611db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
137711db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
137811db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
137911db70b6SBjoern A. Zeeb 
13806b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
13816b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
13826b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
13836b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
13846b4cac81SBjoern A. Zeeb 		break;
13856b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
13866b4cac81SBjoern A. Zeeb 	default:
138711db70b6SBjoern A. Zeeb 		/* currently UNREACH */
13886b4cac81SBjoern A. Zeeb 		IMPROVE();
138911db70b6SBjoern A. Zeeb 		break;
13906b4cac81SBjoern A. Zeeb 	};
139111db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
13926b4cac81SBjoern A. Zeeb 
139311db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
139411db70b6SBjoern A. Zeeb 	if (error != 0) {
139511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
139611db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
139711db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
139811db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
139911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
140011db70b6SBjoern A. Zeeb 		return (0);
14016b4cac81SBjoern A. Zeeb 	}
14026b4cac81SBjoern A. Zeeb 
140311db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
140411db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
140511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
140611db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
140711db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
140811db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
140911db70b6SBjoern A. Zeeb #endif
141011db70b6SBjoern A. Zeeb 
141111db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
14126b4cac81SBjoern A. Zeeb 	return (1);
14136b4cac81SBjoern A. Zeeb }
14146b4cac81SBjoern A. Zeeb 
14156b4cac81SBjoern A. Zeeb static  int
14166b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
14176b4cac81SBjoern A. Zeeb {
14186b4cac81SBjoern A. Zeeb 
141911db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
14206b4cac81SBjoern A. Zeeb }
1421b8dfc3ecSBjoern A. Zeeb 
1422b8dfc3ecSBjoern A. Zeeb static void
1423b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1424b8dfc3ecSBjoern A. Zeeb {
1425b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1426b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1427b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1428b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1429b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1430a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1431b8dfc3ecSBjoern A. Zeeb 
1432b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1433b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1434b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1435b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1436b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1437b8dfc3ecSBjoern A. Zeeb 
1438a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1439a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1440b8dfc3ecSBjoern A. Zeeb 
1441b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1442b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1443a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1444a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1445a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1446a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1447a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1448b8dfc3ecSBjoern A. Zeeb #endif
1449b8dfc3ecSBjoern A. Zeeb 
1450b8dfc3ecSBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
1451a6165709SBjoern A. Zeeb 	if (icislocked)
1452a6165709SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
1453a6165709SBjoern A. Zeeb 	if (ntislocked)
1454b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1455b8dfc3ecSBjoern A. Zeeb 
1456b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1457b8dfc3ecSBjoern A. Zeeb 
1458b8dfc3ecSBjoern A. Zeeb 	/*
1459a6165709SBjoern A. Zeeb 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1460b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1461b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1462b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1463b8dfc3ecSBjoern A. Zeeb 	 */
1464a6165709SBjoern A. Zeeb 	if (icislocked)
1465a6165709SBjoern A. Zeeb 		refcount_acquire(&lvif->ic_unlocked);
1466a6165709SBjoern A. Zeeb 	if (ntislocked)
1467b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1468b8dfc3ecSBjoern A. Zeeb }
1469b8dfc3ecSBjoern A. Zeeb 
1470b8dfc3ecSBjoern A. Zeeb static void
1471b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1472b8dfc3ecSBjoern A. Zeeb {
1473b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1474b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1475b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1476b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1477b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1478a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1479b8dfc3ecSBjoern A. Zeeb 
1480b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1481b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1482b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1483b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1484b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1485b8dfc3ecSBjoern A. Zeeb 
1486a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1487a6165709SBjoern A. Zeeb 	MPASS(!icislocked);
1488a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1489a6165709SBjoern A. Zeeb 	MPASS(!ntislocked);
1490b8dfc3ecSBjoern A. Zeeb 
1491b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1492b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1493a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1494a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1495a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1496a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1497a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1498b8dfc3ecSBjoern A. Zeeb #endif
1499b8dfc3ecSBjoern A. Zeeb 
1500b8dfc3ecSBjoern A. Zeeb 	/*
1501b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1502b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1503b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1504b8dfc3ecSBjoern A. Zeeb 	 */
1505a6165709SBjoern A. Zeeb 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1506a6165709SBjoern A. Zeeb 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1507b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1508b8dfc3ecSBjoern A. Zeeb 
1509a6165709SBjoern A. Zeeb 	/*
1510a6165709SBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1511a6165709SBjoern A. Zeeb 	 * ic before nt to avoid a LOR.
1512a6165709SBjoern A. Zeeb 	 */
1513a6165709SBjoern A. Zeeb 	if (icislocked)
1514a6165709SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
1515a6165709SBjoern A. Zeeb 	if (ntislocked)
1516b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1517b8dfc3ecSBjoern A. Zeeb }
15186b4cac81SBjoern A. Zeeb #endif
15196b4cac81SBjoern A. Zeeb 
15206b4cac81SBjoern A. Zeeb static u_int
15216b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15226b4cac81SBjoern A. Zeeb {
15236b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
15246b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
15256b4cac81SBjoern A. Zeeb 
15266b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
15276b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
15286b4cac81SBjoern A. Zeeb 
15296b4cac81SBjoern A. Zeeb 	mc_list = arg;
15306b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
15316b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
15326b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
15336b4cac81SBjoern A. Zeeb 			return (0);
15346b4cac81SBjoern A. Zeeb 	}
15356b4cac81SBjoern A. Zeeb 
15366b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
15376b4cac81SBjoern A. Zeeb 	if (addr == NULL)
15386b4cac81SBjoern A. Zeeb 		return (0);
15396b4cac81SBjoern A. Zeeb 
15406b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
15416b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
15426b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
15436b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
15446b4cac81SBjoern A. Zeeb 	mc_list->count++;
15456b4cac81SBjoern A. Zeeb 
15469d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15479d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
15486b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
15496b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
15509d9ba2b7SBjoern A. Zeeb #endif
15516b4cac81SBjoern A. Zeeb 
15526b4cac81SBjoern A. Zeeb 	return (1);
15536b4cac81SBjoern A. Zeeb }
15546b4cac81SBjoern A. Zeeb 
15556b4cac81SBjoern A. Zeeb static void
15566b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
15576b4cac81SBjoern A. Zeeb {
15586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15596b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15606b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
15616b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
15626b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
15636b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
15646b4cac81SBjoern A. Zeeb 	u64 mc;
15656b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
15666b4cac81SBjoern A. Zeeb 
15676b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
15686b4cac81SBjoern A. Zeeb 
15696b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
15706b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
15716b4cac81SBjoern A. Zeeb 		return;
15726b4cac81SBjoern A. Zeeb 
15736b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
15746b4cac81SBjoern A. Zeeb 		return;
15756b4cac81SBjoern A. Zeeb 
15766b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
15776b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
15786b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
15796b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
15806b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
15816b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
15826b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
15836b4cac81SBjoern A. Zeeb 	} else {
15846b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
15856b4cac81SBjoern A. Zeeb 	}
15866b4cac81SBjoern A. Zeeb 
15876b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
15886b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
15896b4cac81SBjoern A. Zeeb 	/*
15906b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
15916b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
15926b4cac81SBjoern A. Zeeb 	 */
15936b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
15946b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
15956b4cac81SBjoern A. Zeeb 
15969d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15979d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
15986b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
15996b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
16009d9ba2b7SBjoern A. Zeeb #endif
16016b4cac81SBjoern A. Zeeb 
16026b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
16036b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
16046b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
16056b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
16066b4cac81SBjoern A. Zeeb 			mc_list.count--;
16076b4cac81SBjoern A. Zeeb 		}
16086b4cac81SBjoern A. Zeeb 	}
16096b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
16106b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
16116b4cac81SBjoern A. Zeeb }
16126b4cac81SBjoern A. Zeeb 
1613fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1614fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1615fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1616fa8f007dSBjoern A. Zeeb {
1617fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1618fa8f007dSBjoern A. Zeeb 
1619fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1620fa8f007dSBjoern A. Zeeb 
16219d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16229d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1623fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1624fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1625ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1626fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1627616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1628fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1629fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1630fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1631fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1632ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
16339d9ba2b7SBjoern A. Zeeb #endif
1634fa8f007dSBjoern A. Zeeb 
1635fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1636fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1637fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1638fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1639fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1640fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1641fa8f007dSBjoern A. Zeeb 	}
1642fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1643fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1644fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1645fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1646fa8f007dSBjoern A. Zeeb 	}
1647fa8f007dSBjoern A. Zeeb 
1648fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1649fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1650fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1651fa8f007dSBjoern A. Zeeb 
16529d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16539d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1654fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1655fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1656ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1657fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1658616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1659fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1660fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1661fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1662fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1663ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
16649d9ba2b7SBjoern A. Zeeb #endif
1665fa8f007dSBjoern A. Zeeb 
1666fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1667fa8f007dSBjoern A. Zeeb }
1668fa8f007dSBjoern A. Zeeb 
16696b4cac81SBjoern A. Zeeb static void
16706b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
16716b4cac81SBjoern A. Zeeb {
16726b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16736b4cac81SBjoern A. Zeeb 	int error;
16748ac540d3SBjoern A. Zeeb 	bool cancel;
16756b4cac81SBjoern A. Zeeb 
16768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
16778ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
16788ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
16798ac540d3SBjoern A. Zeeb 	if (!cancel)
16806b4cac81SBjoern A. Zeeb 		return;
16816b4cac81SBjoern A. Zeeb 
16826b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16836b4cac81SBjoern A. Zeeb 
1684bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1685cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
16866b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
16876b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1688cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
16896b4cac81SBjoern A. Zeeb 
16906b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
16918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
16928ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
16938ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
16948ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
16958ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
16968ac540d3SBjoern A. Zeeb 
1697bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
16986b4cac81SBjoern A. Zeeb 
16998ac540d3SBjoern A. Zeeb 	if (cancel)
17006b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
17016b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
17026b4cac81SBjoern A. Zeeb }
17036b4cac81SBjoern A. Zeeb 
17046b4cac81SBjoern A. Zeeb static void
1705086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1706086be6a8SBjoern A. Zeeb {
1707086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1708086be6a8SBjoern A. Zeeb 	int error;
1709086be6a8SBjoern A. Zeeb 	bool old;
1710086be6a8SBjoern A. Zeeb 
1711086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1712086be6a8SBjoern A. Zeeb 	if (old == new)
1713086be6a8SBjoern A. Zeeb 		return;
1714086be6a8SBjoern A. Zeeb 
1715086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1716086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1717086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1718086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1719086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1720086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1721086be6a8SBjoern A. Zeeb 	}
1722086be6a8SBjoern A. Zeeb }
1723086be6a8SBjoern A. Zeeb 
17245a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
17256b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
17266b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
17276b4cac81SBjoern A. Zeeb {
17285a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
17295a4d2461SBjoern A. Zeeb 
17305a4d2461SBjoern A. Zeeb 	changed = 0;
17316b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1732616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
17336b4cac81SBjoern A. Zeeb 
17346b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
17356b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
17366b4cac81SBjoern A. Zeeb 
1737616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1738616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
17396b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
17406b4cac81SBjoern A. Zeeb 		IMPROVE();
1741086be6a8SBjoern A. Zeeb 
17425a4d2461SBjoern A. Zeeb 		/*
17435a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
17445a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
17455a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
17465a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
17475a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
17485a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
17495a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
17505a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
17515a4d2461SBjoern A. Zeeb 		 */
17526b4cac81SBjoern A. Zeeb 	}
17535a4d2461SBjoern A. Zeeb 
17545a4d2461SBjoern A. Zeeb 	return (changed);
17556b4cac81SBjoern A. Zeeb }
17566b4cac81SBjoern A. Zeeb 
17576b4cac81SBjoern A. Zeeb static void
17586b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
17596b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
17606b4cac81SBjoern A. Zeeb {
17616b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
17626b4cac81SBjoern A. Zeeb 	int tid;
1763eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
17646b4cac81SBjoern A. Zeeb 
17656b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
17666b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
17676b4cac81SBjoern A. Zeeb 
17686b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
17696b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
17706b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
17716b4cac81SBjoern A. Zeeb 				continue;
17726b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
17736b4cac81SBjoern A. Zeeb 			continue;
17746b4cac81SBjoern A. Zeeb 
17756b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
17766b4cac81SBjoern A. Zeeb 			continue;
17776b4cac81SBjoern A. Zeeb 
17786b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
17796b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
17806b4cac81SBjoern A. Zeeb 			continue;
17816b4cac81SBjoern A. Zeeb 
1782eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1783eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1784eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1785eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
17866b4cac81SBjoern A. Zeeb 			continue;
17876b4cac81SBjoern A. Zeeb 
17886b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
17896b4cac81SBjoern A. Zeeb 	}
17906b4cac81SBjoern A. Zeeb }
17916b4cac81SBjoern A. Zeeb 
179288665349SBjoern A. Zeeb /*
179388665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
179488665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
179588665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
179688665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
179788665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
179888665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
179988665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
180088665349SBjoern A. Zeeb  * re-used.
180188665349SBjoern A. Zeeb  */
180288665349SBjoern A. Zeeb static void
180388665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
180488665349SBjoern A. Zeeb {
1805cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
180688665349SBjoern A. Zeeb 	struct mbufq mq;
180788665349SBjoern A. Zeeb 	struct mbuf *m;
180888665349SBjoern A. Zeeb 	int len;
180988665349SBjoern A. Zeeb 
1810cd0fcf9fSBjoern A. Zeeb 	/* There is no lockdep_assert_not_held_wiphy(). */
1811cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1812cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_not_held(&hw->wiphy->mtx);
181388665349SBjoern A. Zeeb 
181488665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
181588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
181688665349SBjoern A. Zeeb 	lsta->txq_ready = false;
181788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
181888665349SBjoern A. Zeeb 
181988665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
182088665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
182188665349SBjoern A. Zeeb 
182288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
182388665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
182488665349SBjoern A. Zeeb 	if (len <= 0) {
182588665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
182688665349SBjoern A. Zeeb 		return;
182788665349SBjoern A. Zeeb 	}
182888665349SBjoern A. Zeeb 
182988665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
183088665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
183188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
183288665349SBjoern A. Zeeb 
183388665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
183488665349SBjoern A. Zeeb 	while (m != NULL) {
183588665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
183688665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
183788665349SBjoern A. Zeeb 	}
183888665349SBjoern A. Zeeb }
183988665349SBjoern A. Zeeb 
184050d826beSBjoern A. Zeeb 
184150d826beSBjoern A. Zeeb static void
184250d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
184350d826beSBjoern A. Zeeb {
184450d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1845231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
184650d826beSBjoern A. Zeeb 
1847231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1848231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1849231168c7SBjoern A. Zeeb 
1850231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1851231168c7SBjoern A. Zeeb 		return;
1852231168c7SBjoern A. Zeeb 
185350d826beSBjoern A. Zeeb 	/* Remove vif context. */
1854231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
185550d826beSBjoern A. Zeeb 
185650d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
185750d826beSBjoern A. Zeeb 
185850d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
185950d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1860231168c7SBjoern A. Zeeb 
1861231168c7SBjoern A. Zeeb 	/* Cleanup. */
1862231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
186350d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1864a8a47a41SBjoern A. Zeeb 	list_del(&lchanctx->entry);
186550d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
186650d826beSBjoern A. Zeeb }
186750d826beSBjoern A. Zeeb 
186850d826beSBjoern A. Zeeb 
18696b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
18706b4cac81SBjoern A. Zeeb 
18716b4cac81SBjoern A. Zeeb static int
18726b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18736b4cac81SBjoern A. Zeeb {
18746b4cac81SBjoern A. Zeeb 
18756b4cac81SBjoern A. Zeeb 	return (0);
18766b4cac81SBjoern A. Zeeb }
18776b4cac81SBjoern A. Zeeb 
18786b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
18796b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
18806b4cac81SBjoern A. Zeeb 
18816b4cac81SBjoern A. Zeeb static int
18826b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18836b4cac81SBjoern A. Zeeb {
18846b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1885c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1886d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
18876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18906b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18916b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
18926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
18936b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
18946b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
18956b4cac81SBjoern A. Zeeb 	uint32_t changed;
18966b4cac81SBjoern A. Zeeb 	int error;
18976b4cac81SBjoern A. Zeeb 
18982ac8a218SBjoern A. Zeeb 	/*
18992ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
19002ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
19012ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
19022ac8a218SBjoern A. Zeeb 	 */
19032ac8a218SBjoern A. Zeeb 
19042ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
19052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
19062ac8a218SBjoern A. Zeeb 		return (EINVAL);
19072ac8a218SBjoern A. Zeeb 	}
19080936c648SBjoern A. Zeeb 	/*
19090936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
19100936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
19110936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
19120936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
19130936c648SBjoern A. Zeeb 	 */
19142ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
19152ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
19162ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
19172ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
19180936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19192ac8a218SBjoern A. Zeeb 		return (EINVAL);
19206b4cac81SBjoern A. Zeeb 	}
19216b4cac81SBjoern A. Zeeb 
19226b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19232ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
19242ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
19252ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
19262ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
19270936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19282ac8a218SBjoern A. Zeeb 		return (ESRCH);
19292ac8a218SBjoern A. Zeeb 	}
19302ac8a218SBjoern A. Zeeb 
19316b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19326b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19336b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19346b4cac81SBjoern A. Zeeb 
19350936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19360936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19370936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
19380936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19390936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19400936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19410936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19420936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
194392690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
194492690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19450936c648SBjoern A. Zeeb 		return (EBUSY);
19460936c648SBjoern A. Zeeb 	}
19470936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19480936c648SBjoern A. Zeeb 
19496b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1950cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
19516b4cac81SBjoern A. Zeeb 
19526b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
1953560708cbSBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1954560708cbSBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1955560708cbSBjoern A. Zeeb 	if (chanctx_conf != NULL) {
1956d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
19576b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
19586b4cac81SBjoern A. Zeeb 	} else {
19596b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1960c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
19616b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1962d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
19636b4cac81SBjoern A. Zeeb 	}
19646b4cac81SBjoern A. Zeeb 
1965d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1966389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1967d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
19686b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1969d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1970d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
197190d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
197290d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
19739fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
19742ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
19752ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
197662d51a43SBjoern A. Zeeb 
19779fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
19789fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
197990d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1980d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
198190d8e307SBjoern A. Zeeb 		else
1982d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
19839fb91463SBjoern A. Zeeb 	}
19849fb91463SBjoern A. Zeeb #endif
19859fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
19865393cd34SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
19879fb91463SBjoern A. Zeeb #ifdef __notyet__
198890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1989d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
199090d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1991d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
199290d8e307SBjoern A. Zeeb 		else
199390d8e307SBjoern A. Zeeb #endif
199490d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1995d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
19969fb91463SBjoern A. Zeeb 	}
19979fb91463SBjoern A. Zeeb #endif
1998389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
19996b4cac81SBjoern A. Zeeb 	/* Responder ... */
200090d8e307SBjoern A. Zeeb #if 0
200190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2002d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
200390d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
200490d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
200590d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
200690d8e307SBjoern A. Zeeb #endif
200790d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
200890d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
200990d8e307SBjoern A. Zeeb #else
201090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
201190d8e307SBjoern A. Zeeb #endif
20126b4cac81SBjoern A. Zeeb 
20136ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
20146ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
20156ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
20166ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
20176ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
20186ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
20196ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
20206ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
20216ffb7bd4SBjoern A. Zeeb 
20226ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
20236ffb7bd4SBjoern A. Zeeb 
20246ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
20256ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
20266ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
20276ffb7bd4SBjoern A. Zeeb 
20286ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
20296ffb7bd4SBjoern A. Zeeb 
20306b4cac81SBjoern A. Zeeb 	error = 0;
2031560708cbSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
20326b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
20336b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
20346b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
20356b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2036d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
20376b4cac81SBjoern A. Zeeb 	} else {
2038d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
20396b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
20407b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
20417b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
20427b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
2043d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
20447b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
2045d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
20466b4cac81SBjoern A. Zeeb 		} else {
2047018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2048018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
20496b4cac81SBjoern A. Zeeb 			goto out;
20506b4cac81SBjoern A. Zeeb 		}
20516ffb7bd4SBjoern A. Zeeb 
2052a8a47a41SBjoern A. Zeeb 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2053560708cbSBjoern A. Zeeb 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
20546ffb7bd4SBjoern A. Zeeb 
20556b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
20566b4cac81SBjoern A. Zeeb 		if (error == 0)
205768541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2058d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
20596b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
20606b4cac81SBjoern A. Zeeb 			error = 0;
20616b4cac81SBjoern A. Zeeb 		if (error != 0) {
2062018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2063018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
2064d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2065560708cbSBjoern A. Zeeb 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2066d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2067a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
2068c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
20696b4cac81SBjoern A. Zeeb 			goto out;
20706b4cac81SBjoern A. Zeeb 		}
20716b4cac81SBjoern A. Zeeb 	}
20726b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
20736b4cac81SBjoern A. Zeeb 
20746b4cac81SBjoern A. Zeeb 	/* RATES */
20756b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
20766b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
20776b4cac81SBjoern A. Zeeb 
2078d9f59799SBjoern A. Zeeb 	/*
20790936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
20800936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
20810936c648SBjoern A. Zeeb 	 * under the hoods.
2082d9f59799SBjoern A. Zeeb 	 */
20830936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
20840936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
20856b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2086e24e8103SBjoern A. Zeeb 
208788665349SBjoern A. Zeeb 	/*
208888665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
208988665349SBjoern A. Zeeb 	 * that we can tx again.
209088665349SBjoern A. Zeeb 	 */
209188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
209288665349SBjoern A. Zeeb 	lsta->txq_ready = true;
209388665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
209488665349SBjoern A. Zeeb 
20952ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
2096a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2097e24e8103SBjoern A. Zeeb 
2098d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
20996b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21006b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
21016b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2102e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
21036b4cac81SBjoern A. Zeeb 	if (error != 0) {
21046b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2105018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2106018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21076b4cac81SBjoern A. Zeeb 		goto out;
21086b4cac81SBjoern A. Zeeb 	}
21096b4cac81SBjoern A. Zeeb #if 0
21106b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
21116b4cac81SBjoern A. Zeeb #endif
21126b4cac81SBjoern A. Zeeb 
21139d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21149d9ba2b7SBjoern A. Zeeb 
21151665ef97SBjoern A. Zeeb #if 0
21166b4cac81SBjoern A. Zeeb 	/*
21176b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
21186b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
21196b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
21206b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2121d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2122d9f59799SBjoern A. Zeeb 	 * for all queues.
21236b4cac81SBjoern A. Zeeb 	 */
2124e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
21251665ef97SBjoern A. Zeeb #endif
21266b4cac81SBjoern A. Zeeb 
21276b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21286b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21296b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
21306b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21316b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
21326b4cac81SBjoern A. Zeeb 
21336b4cac81SBjoern A. Zeeb 	/*
21346b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
21356b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
21366b4cac81SBjoern A. Zeeb 	 * - event_callback
21376b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
21386b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
21396b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
21406b4cac81SBjoern A. Zeeb 	 */
21416b4cac81SBjoern A. Zeeb 
2142cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2143105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2144105b9df2SBjoern A. Zeeb 
2145105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2146105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2147105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2148105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2149105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2150105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2151105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2152105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2153105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2154105b9df2SBjoern A. Zeeb 
2155105b9df2SBjoern A. Zeeb 	/*
2156105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2157105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2158105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2159105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2160105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2161105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2162105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2163105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2164105b9df2SBjoern A. Zeeb 	 */
2165105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2166105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2167105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
2168105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
2169105b9df2SBjoern A. Zeeb 	} else {
2170105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
2171105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
2172105b9df2SBjoern A. Zeeb 		/*
2173105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2174105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2175105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2176105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2177105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2178105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2179105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2180105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
2181105b9df2SBjoern A. Zeeb 		 */
2182105b9df2SBjoern A. Zeeb 	}
2183105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2184105b9df2SBjoern A. Zeeb 	goto out_relocked;
2185105b9df2SBjoern A. Zeeb 
21866b4cac81SBjoern A. Zeeb out:
2187cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
21886b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2189105b9df2SBjoern A. Zeeb out_relocked:
21900936c648SBjoern A. Zeeb 	/*
2191105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
21920936c648SBjoern A. Zeeb 	 * during the work of this function.
21930936c648SBjoern A. Zeeb 	 */
21946b4cac81SBjoern A. Zeeb 	if (ni != NULL)
21956b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
21966b4cac81SBjoern A. Zeeb 	return (error);
21976b4cac81SBjoern A. Zeeb }
21986b4cac81SBjoern A. Zeeb 
21996b4cac81SBjoern A. Zeeb static int
22006b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22016b4cac81SBjoern A. Zeeb {
22026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22036b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22056b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22086b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22096b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22106b4cac81SBjoern A. Zeeb 	int error;
22116b4cac81SBjoern A. Zeeb 
22126b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22136b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22146b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22156b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22166b4cac81SBjoern A. Zeeb 
22172ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22182ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22192ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22202ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22212ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22222ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22232ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22242ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22252ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22262ac8a218SBjoern A. Zeeb #endif
22272ac8a218SBjoern A. Zeeb 
22282ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22292ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22302ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22312ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22322ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22332ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
22346b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22356b4cac81SBjoern A. Zeeb 
223667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22376b4cac81SBjoern A. Zeeb 
22386b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2239cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
22406b4cac81SBjoern A. Zeeb 
2241d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2242d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2243d9f59799SBjoern A. Zeeb 
22446b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
224588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
22466b4cac81SBjoern A. Zeeb 
22476b4cac81SBjoern A. Zeeb 	/* flush, no drop */
22486b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
22496b4cac81SBjoern A. Zeeb 
22506b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22516b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22526b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22536b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
22546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22556b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22566b4cac81SBjoern A. Zeeb 	}
22576b4cac81SBjoern A. Zeeb 
22586b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
22596b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
22606b4cac81SBjoern A. Zeeb 
22616b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
22626b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2263d9f59799SBjoern A. Zeeb 
2264d9f59799SBjoern A. Zeeb 	/* Take the station down. */
22656b4cac81SBjoern A. Zeeb 
22666b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
22676b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22686b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2269d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2270e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
22716b4cac81SBjoern A. Zeeb 	if (error != 0) {
22726b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2273018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2274018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22756b4cac81SBjoern A. Zeeb 		goto out;
22766b4cac81SBjoern A. Zeeb 	}
22776b4cac81SBjoern A. Zeeb #if 0
22786b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
22796b4cac81SBjoern A. Zeeb #endif
22806b4cac81SBjoern A. Zeeb 
228167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22826b4cac81SBjoern A. Zeeb 
22832ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22842ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
22852ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
22862ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
22872ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2288d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
22890936c648SBjoern A. Zeeb 	/*
22900936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
22910936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
22920936c648SBjoern A. Zeeb 	 * and potentially freed.
22930936c648SBjoern A. Zeeb 	 */
22940936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2295d9f59799SBjoern A. Zeeb 
2296d9f59799SBjoern A. Zeeb 	/* conf_tx */
2297d9f59799SBjoern A. Zeeb 
229850d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
22996b4cac81SBjoern A. Zeeb 
23006b4cac81SBjoern A. Zeeb out:
2301cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
23026b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
23036b4cac81SBjoern A. Zeeb 	return (error);
23046b4cac81SBjoern A. Zeeb }
23056b4cac81SBjoern A. Zeeb 
23066b4cac81SBjoern A. Zeeb static int
23076b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23086b4cac81SBjoern A. Zeeb {
23096b4cac81SBjoern A. Zeeb 	int error;
23106b4cac81SBjoern A. Zeeb 
23116b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
23126b4cac81SBjoern A. Zeeb 	if (error == 0)
23136b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
23146b4cac81SBjoern A. Zeeb 	return (error);
23156b4cac81SBjoern A. Zeeb }
23166b4cac81SBjoern A. Zeeb 
23176b4cac81SBjoern A. Zeeb static int
23186b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23196b4cac81SBjoern A. Zeeb {
23206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23246b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23256b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23266b4cac81SBjoern A. Zeeb 	int error;
23276b4cac81SBjoern A. Zeeb 
23286b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23326b4cac81SBjoern A. Zeeb 
23336b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2334cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
23352ac8a218SBjoern A. Zeeb 
23362ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23372ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
23382ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
23392ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23402ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23412ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23422ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23432ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23442ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23452ac8a218SBjoern A. Zeeb #endif
23462ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
23472ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
23482ac8a218SBjoern A. Zeeb 		goto out;
23492ac8a218SBjoern A. Zeeb 	}
23502ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23522ac8a218SBjoern A. Zeeb 
23532ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
23546b4cac81SBjoern A. Zeeb 
23556b4cac81SBjoern A. Zeeb 	/* Finish auth. */
23566b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
23576b4cac81SBjoern A. Zeeb 
23586b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
23596b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
23606b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2361e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2362018d93ecSBjoern A. Zeeb 	if (error != 0) {
2363018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2364018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23656b4cac81SBjoern A. Zeeb 		goto out;
2366018d93ecSBjoern A. Zeeb 	}
23676b4cac81SBjoern A. Zeeb 
23686b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23696b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23706b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23716b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
23726b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23736b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23746b4cac81SBjoern A. Zeeb 	}
23756b4cac81SBjoern A. Zeeb 
23766b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
23776b4cac81SBjoern A. Zeeb 
23786b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
23796b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
23806b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23816b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
23826b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
23836b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
23846b4cac81SBjoern A. Zeeb 	}
23856b4cac81SBjoern A. Zeeb 
23866b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
238788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
23886b4cac81SBjoern A. Zeeb 
23896b4cac81SBjoern A. Zeeb 	/*
23906b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
23916b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
23926b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
23936b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
23946b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
23956b4cac81SBjoern A. Zeeb 	 * - event_callback
23966b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
23976b4cac81SBjoern A. Zeeb 	 */
23986b4cac81SBjoern A. Zeeb 
23996b4cac81SBjoern A. Zeeb out:
2400cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24026b4cac81SBjoern A. Zeeb 	return (error);
24036b4cac81SBjoern A. Zeeb }
24046b4cac81SBjoern A. Zeeb 
24056b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
24066b4cac81SBjoern A. Zeeb static int
24076b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24086b4cac81SBjoern A. Zeeb {
24096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24116b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24126b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24136b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24146b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24152ac8a218SBjoern A. Zeeb 	int error;
24166b4cac81SBjoern A. Zeeb 
24176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24216b4cac81SBjoern A. Zeeb 
24226b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2423cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24242ac8a218SBjoern A. Zeeb 
24252ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24262ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24272ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24282ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24292ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24302ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24312ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24322ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24332ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24342ac8a218SBjoern A. Zeeb #endif
24352ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24362ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24372ac8a218SBjoern A. Zeeb 		goto out;
24382ac8a218SBjoern A. Zeeb 	}
24392ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24402ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24412ac8a218SBjoern A. Zeeb 
24422ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
24432ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
24446b4cac81SBjoern A. Zeeb 
24456b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
24466b4cac81SBjoern A. Zeeb 
24476b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
24486b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
24496b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24506b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
24516b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
24526b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24536b4cac81SBjoern A. Zeeb 	}
24546b4cac81SBjoern A. Zeeb 
24556b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
24566b4cac81SBjoern A. Zeeb 
24576b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
24586b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
24596b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24606b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
24616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
24626b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
24636b4cac81SBjoern A. Zeeb 	}
24646b4cac81SBjoern A. Zeeb 
24652ac8a218SBjoern A. Zeeb 	error = 0;
24662ac8a218SBjoern A. Zeeb out:
2467cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24686b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24696b4cac81SBjoern A. Zeeb 
24702ac8a218SBjoern A. Zeeb 	return (error);
24716b4cac81SBjoern A. Zeeb }
24726b4cac81SBjoern A. Zeeb 
24736b4cac81SBjoern A. Zeeb static int
2474d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24756b4cac81SBjoern A. Zeeb {
24766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24806b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
24816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24826b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
24836b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2484d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
24856b4cac81SBjoern A. Zeeb 	int error;
24866b4cac81SBjoern A. Zeeb 
24876b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24896b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24906b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24916b4cac81SBjoern A. Zeeb 
24922ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2493cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24942ac8a218SBjoern A. Zeeb 
24952ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24962ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24972ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
24982ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
24992ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25002ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25012ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25022ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25032ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25042ac8a218SBjoern A. Zeeb #endif
25052ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25072ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25082ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25092ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25102ac8a218SBjoern A. Zeeb 
25112ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25126b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25136b4cac81SBjoern A. Zeeb 
251467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2515d9f59799SBjoern A. Zeeb 
2516d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2517d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2518d9f59799SBjoern A. Zeeb 
2519d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2520d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2521d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2522d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2523d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2524ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2525d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2526d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2527d9f59799SBjoern A. Zeeb 	}
2528d9f59799SBjoern A. Zeeb 
2529cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2530d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2531d9f59799SBjoern A. Zeeb 
253288665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2533d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2534018d93ecSBjoern A. Zeeb 	if (error != 0) {
2535018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2536018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2537d9f59799SBjoern A. Zeeb 		goto outni;
2538018d93ecSBjoern A. Zeeb 	}
2539d9f59799SBjoern A. Zeeb 
2540d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
254188665349SBjoern A. Zeeb 
254288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
254388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
254488665349SBjoern A. Zeeb 
2545cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2546d9f59799SBjoern A. Zeeb 
254767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2548d9f59799SBjoern A. Zeeb 
2549d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
255088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2551d9f59799SBjoern A. Zeeb 
2552d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2553d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2554d9f59799SBjoern A. Zeeb 
25556b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25566b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25576b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25586b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2559ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
25606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25616b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25626b4cac81SBjoern A. Zeeb 	}
25636b4cac81SBjoern A. Zeeb 
2564d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2565d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2566d9f59799SBjoern A. Zeeb 
2567d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2568d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2569d9f59799SBjoern A. Zeeb 
2570d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2571d9f59799SBjoern A. Zeeb 
25726b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
25736b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
25746b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
25756b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2576e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2577018d93ecSBjoern A. Zeeb 	if (error != 0) {
2578018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2579018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25806b4cac81SBjoern A. Zeeb 		goto out;
2581018d93ecSBjoern A. Zeeb 	}
25826b4cac81SBjoern A. Zeeb 
25835a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
25845a4d2461SBjoern A. Zeeb 	bss_changed = 0;
25855a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
25866b4cac81SBjoern A. Zeeb 
25875a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
258816e688b2SBjoern A. Zeeb 
2589d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2590d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2591d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2592d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2593e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2594d9f59799SBjoern A. Zeeb 	if (error != 0) {
2595d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2596018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2597018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2598d9f59799SBjoern A. Zeeb 		goto out;
2599d9f59799SBjoern A. Zeeb 	}
2600d9f59799SBjoern A. Zeeb 
260116e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2602d9f59799SBjoern A. Zeeb 
2603d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2604d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2605d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2606616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2607616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2608d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2609d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2610d9f59799SBjoern A. Zeeb 
26112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26122ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
26132ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
26142ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2616d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
26170936c648SBjoern A. Zeeb 	/*
26180936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
26190936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
26200936c648SBjoern A. Zeeb 	 * and potentially freed.
26210936c648SBjoern A. Zeeb 	 */
26220936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2623d9f59799SBjoern A. Zeeb 
2624d9f59799SBjoern A. Zeeb 	/* conf_tx */
2625d9f59799SBjoern A. Zeeb 
262650d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2627d9f59799SBjoern A. Zeeb 
2628d9f59799SBjoern A. Zeeb 	error = EALREADY;
26296b4cac81SBjoern A. Zeeb out:
2630cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
26316b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2632d9f59799SBjoern A. Zeeb outni:
26336b4cac81SBjoern A. Zeeb 	return (error);
26346b4cac81SBjoern A. Zeeb }
26356b4cac81SBjoern A. Zeeb 
26366b4cac81SBjoern A. Zeeb static int
2637d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2638d9f59799SBjoern A. Zeeb {
2639d9f59799SBjoern A. Zeeb 	int error;
2640d9f59799SBjoern A. Zeeb 
2641d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2642d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2643d9f59799SBjoern A. Zeeb 		return (error);
2644d9f59799SBjoern A. Zeeb 
2645d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2646d9f59799SBjoern A. Zeeb 
2647d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2648d9f59799SBjoern A. Zeeb 	return (error);
2649d9f59799SBjoern A. Zeeb }
2650d9f59799SBjoern A. Zeeb 
2651d9f59799SBjoern A. Zeeb static int
26526b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26536b4cac81SBjoern A. Zeeb {
26546b4cac81SBjoern A. Zeeb 	int error;
26556b4cac81SBjoern A. Zeeb 
2656d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
26576b4cac81SBjoern A. Zeeb 	return (error);
26586b4cac81SBjoern A. Zeeb }
26596b4cac81SBjoern A. Zeeb 
26606b4cac81SBjoern A. Zeeb static int
26616b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26626b4cac81SBjoern A. Zeeb {
26636b4cac81SBjoern A. Zeeb 	int error;
26646b4cac81SBjoern A. Zeeb 
2665d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
26666b4cac81SBjoern A. Zeeb 	return (error);
26676b4cac81SBjoern A. Zeeb }
26686b4cac81SBjoern A. Zeeb 
26696b4cac81SBjoern A. Zeeb static int
26706b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26716b4cac81SBjoern A. Zeeb {
26726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26736b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26746b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26756b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26766b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26776b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26786b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
26796b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
26806b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
26816b4cac81SBjoern A. Zeeb 	int error;
26826b4cac81SBjoern A. Zeeb 
26836b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26846b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26856b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26866b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26876b4cac81SBjoern A. Zeeb 
26886b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2689cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
26902ac8a218SBjoern A. Zeeb 
26912ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26922ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
26932ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
26942ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26952ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26962ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26972ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26982ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26992ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27002ac8a218SBjoern A. Zeeb #endif
27012ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
27022ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
27032ac8a218SBjoern A. Zeeb 		goto out;
27042ac8a218SBjoern A. Zeeb 	}
27052ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27072ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27082ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27092ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27102ac8a218SBjoern A. Zeeb 
27112ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27126b4cac81SBjoern A. Zeeb 
27136b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
27146b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
27156b4cac81SBjoern A. Zeeb 
27169597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
27179597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
27189597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
27199597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
27206b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27219597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
27224a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27234a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
27244a67f1dfSBjoern A. Zeeb 		sta->wme = true;
27254a67f1dfSBjoern A. Zeeb #endif
2726e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2727018d93ecSBjoern A. Zeeb 	if (error != 0) {
2728018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2729018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27309597f7cbSBjoern A. Zeeb 		goto out;
2731018d93ecSBjoern A. Zeeb 	}
27326b4cac81SBjoern A. Zeeb 
27336b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
27346b4cac81SBjoern A. Zeeb 
27356b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
27366b4cac81SBjoern A. Zeeb 	bss_changed = 0;
27374a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27384a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
27394a67f1dfSBjoern A. Zeeb #endif
2740616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2741616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2742616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
27436b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
27446b4cac81SBjoern A. Zeeb 	}
27456b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2746616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2747616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
27486b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
27496b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
27506b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
27516b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
27526b4cac81SBjoern A. Zeeb 	}
27536b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
27546b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
27556b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
27566b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
27576b4cac81SBjoern A. Zeeb 	}
27586b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
27596b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
27606b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
27616b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
27626b4cac81SBjoern A. Zeeb 	}
2763fa8f007dSBjoern A. Zeeb 
2764fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
27656b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
27666b4cac81SBjoern A. Zeeb 
27676b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
27686b4cac81SBjoern A. Zeeb 	 * - event_callback
27696b4cac81SBjoern A. Zeeb 	 */
27706b4cac81SBjoern A. Zeeb 
27716b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
27726b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
27736b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
27746b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
27756b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
27766b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
27776b4cac81SBjoern A. Zeeb 	}
27786b4cac81SBjoern A. Zeeb 
2779086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2780086be6a8SBjoern A. Zeeb 
27816b4cac81SBjoern A. Zeeb 	/*
27826b4cac81SBjoern A. Zeeb 	 * And then:
27836b4cac81SBjoern A. Zeeb 	 * - (more packets)?
27846b4cac81SBjoern A. Zeeb 	 * - set_key
27856b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
27866b4cac81SBjoern A. Zeeb 	 * - set_key (?)
27876b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
27886b4cac81SBjoern A. Zeeb 	 */
27896b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
27906b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
27916b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
27926b4cac81SBjoern A. Zeeb 
27936b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
27946b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
27956b4cac81SBjoern A. Zeeb 	}
27966b4cac81SBjoern A. Zeeb 
2797f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
27989fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
279962d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
28009fb91463SBjoern A. Zeeb 
28016b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
28026b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28036b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28046b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2805e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
28066b4cac81SBjoern A. Zeeb 	if (error != 0) {
28076b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2808018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2809018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28106b4cac81SBjoern A. Zeeb 		goto out;
28116b4cac81SBjoern A. Zeeb 	}
28126b4cac81SBjoern A. Zeeb 
28136b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
28146b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
28156b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
28166b4cac81SBjoern A. Zeeb 	 *
28176b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
28186b4cac81SBjoern A. Zeeb 	 */
28196b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
28206b4cac81SBjoern A. Zeeb 
2821fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2822fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2823fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2824fa8f007dSBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb out:
2826cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
28276b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
28286b4cac81SBjoern A. Zeeb 	return (error);
28296b4cac81SBjoern A. Zeeb }
28306b4cac81SBjoern A. Zeeb 
28316b4cac81SBjoern A. Zeeb static int
28326b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28336b4cac81SBjoern A. Zeeb {
28346b4cac81SBjoern A. Zeeb 	int error;
28356b4cac81SBjoern A. Zeeb 
28366b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
28376b4cac81SBjoern A. Zeeb 	if (error == 0)
28386b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
28396b4cac81SBjoern A. Zeeb 	return (error);
28406b4cac81SBjoern A. Zeeb }
28416b4cac81SBjoern A. Zeeb 
28426b4cac81SBjoern A. Zeeb static int
28436b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28446b4cac81SBjoern A. Zeeb {
28456b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28466b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28486b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28496b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
28506b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28516b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2852d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2853d9f59799SBjoern A. Zeeb #if 0
2854d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2855d9f59799SBjoern A. Zeeb #endif
28566b4cac81SBjoern A. Zeeb 	int error;
28576b4cac81SBjoern A. Zeeb 
28586b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
28596b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28606b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28616b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28626b4cac81SBjoern A. Zeeb 
28632ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28642ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28652ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
28662ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
28672ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28682ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28692ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28702ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28712ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28722ac8a218SBjoern A. Zeeb #endif
28732ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28742ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28752ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28762ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28772ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28782ac8a218SBjoern A. Zeeb 
28792ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
28806b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
28816b4cac81SBjoern A. Zeeb 
288267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2883d9f59799SBjoern A. Zeeb 
2884d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2885cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2886d9f59799SBjoern A. Zeeb 
2887d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2888d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2889d9f59799SBjoern A. Zeeb 
2890d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2891d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2892d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2893d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2894d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2895ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2896d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2897d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2898d9f59799SBjoern A. Zeeb 	}
2899d9f59799SBjoern A. Zeeb 
2900cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2901d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2902d9f59799SBjoern A. Zeeb 
2903d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2904d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2905018d93ecSBjoern A. Zeeb 	if (error != 0) {
2906018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2907018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2908d9f59799SBjoern A. Zeeb 		goto outni;
2909018d93ecSBjoern A. Zeeb 	}
2910d9f59799SBjoern A. Zeeb 
2911d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
291288665349SBjoern A. Zeeb 
291388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
291488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
291588665349SBjoern A. Zeeb 
2916cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2917d9f59799SBjoern A. Zeeb 
291867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2919d9f59799SBjoern A. Zeeb 
2920d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
292188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2922d9f59799SBjoern A. Zeeb 
2923d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2924d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2925d9f59799SBjoern A. Zeeb 
2926d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2927d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2928d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2929d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2930ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2931d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2932d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2933d9f59799SBjoern A. Zeeb 	}
2934d9f59799SBjoern A. Zeeb 
2935d9f59799SBjoern A. Zeeb #if 0
2936d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2937d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2938d9f59799SBjoern A. Zeeb 
2939d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2940d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2941d9f59799SBjoern A. Zeeb #endif
2942d9f59799SBjoern A. Zeeb 
2943d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2944d9f59799SBjoern A. Zeeb 
29456b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
29466b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
29476b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
29486b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2949e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2950018d93ecSBjoern A. Zeeb 	if (error != 0) {
2951018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2952018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
29536b4cac81SBjoern A. Zeeb 		goto out;
2954018d93ecSBjoern A. Zeeb 	}
29556b4cac81SBjoern A. Zeeb 
295667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
29576b4cac81SBjoern A. Zeeb 
295811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
295911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
296011db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
296111db70b6SBjoern A. Zeeb 		if (error != 0) {
296211db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
296311db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
296411db70b6SBjoern A. Zeeb 			/*
296511db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
296611db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
296711db70b6SBjoern A. Zeeb 			 * less appropriate time).
296811db70b6SBjoern A. Zeeb 			 */
296911db70b6SBjoern A. Zeeb 			/* goto out; */
297011db70b6SBjoern A. Zeeb 		}
297111db70b6SBjoern A. Zeeb 	}
297211db70b6SBjoern A. Zeeb #endif
297311db70b6SBjoern A. Zeeb 
29746b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
29756b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
29766b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
29776b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2978e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2979018d93ecSBjoern A. Zeeb 	if (error != 0) {
2980018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2981018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
29826b4cac81SBjoern A. Zeeb 		goto out;
2983018d93ecSBjoern A. Zeeb 	}
29846b4cac81SBjoern A. Zeeb 
298567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
29866b4cac81SBjoern A. Zeeb 
2987d9f59799SBjoern A. Zeeb #if 0
2988d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2989d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2990d9f59799SBjoern A. Zeeb #endif
2991d9f59799SBjoern A. Zeeb 
2992d9f59799SBjoern A. Zeeb 	error = EALREADY;
29936b4cac81SBjoern A. Zeeb out:
2994cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
29956b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2996d9f59799SBjoern A. Zeeb outni:
29976b4cac81SBjoern A. Zeeb 	return (error);
29986b4cac81SBjoern A. Zeeb }
29996b4cac81SBjoern A. Zeeb 
30006b4cac81SBjoern A. Zeeb static int
3001d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3002d9f59799SBjoern A. Zeeb {
3003d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3004d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3005d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
3006d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3007d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
3008d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
3009d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3010d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
3011d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
3012d9f59799SBjoern A. Zeeb 	int error;
3013d9f59799SBjoern A. Zeeb 
3014d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
3015d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3016d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3017d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3018d9f59799SBjoern A. Zeeb 
30192ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3020cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
30212ac8a218SBjoern A. Zeeb 
30222ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30232ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30242ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
30252ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
30262ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
30272ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
30282ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
30292ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
30302ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
30312ac8a218SBjoern A. Zeeb #endif
30322ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
30332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30342ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
30352ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
30362ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
30372ac8a218SBjoern A. Zeeb 
30382ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3039d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3040d9f59799SBjoern A. Zeeb 
304167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3042d9f59799SBjoern A. Zeeb 
3043d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3044d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3045d9f59799SBjoern A. Zeeb 
3046d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3047d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3048d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3049d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3050d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3051ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3052d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3053d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3054d9f59799SBjoern A. Zeeb 	}
3055d9f59799SBjoern A. Zeeb 
3056cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3057d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3058d9f59799SBjoern A. Zeeb 
3059d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3060d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3061018d93ecSBjoern A. Zeeb 	if (error != 0) {
3062018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3063018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3064d9f59799SBjoern A. Zeeb 		goto outni;
3065018d93ecSBjoern A. Zeeb 	}
3066d9f59799SBjoern A. Zeeb 
3067d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
306888665349SBjoern A. Zeeb 
306988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
307088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
307188665349SBjoern A. Zeeb 
3072cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3073d9f59799SBjoern A. Zeeb 
307467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3075d9f59799SBjoern A. Zeeb 
3076d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
307788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3078d9f59799SBjoern A. Zeeb 
3079d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3080d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3081d9f59799SBjoern A. Zeeb 
3082d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3083d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3084d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3085d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3086ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3087d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3088d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3089d9f59799SBjoern A. Zeeb 	}
3090d9f59799SBjoern A. Zeeb 
3091d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3092d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3093d9f59799SBjoern A. Zeeb 
3094d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3095d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3096d9f59799SBjoern A. Zeeb 
3097d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3098d9f59799SBjoern A. Zeeb 
3099d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3100d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3101d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3102d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3103e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3104018d93ecSBjoern A. Zeeb 	if (error != 0) {
3105018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3106018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3107d9f59799SBjoern A. Zeeb 		goto out;
3108018d93ecSBjoern A. Zeeb 	}
3109d9f59799SBjoern A. Zeeb 
311067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3111d9f59799SBjoern A. Zeeb 
311211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
311311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
311411db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
311511db70b6SBjoern A. Zeeb 		if (error != 0) {
311611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
311711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
311811db70b6SBjoern A. Zeeb 			/*
311911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
312011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
312111db70b6SBjoern A. Zeeb 			 * less appropriate time).
312211db70b6SBjoern A. Zeeb 			 */
312311db70b6SBjoern A. Zeeb 			/* goto out; */
312411db70b6SBjoern A. Zeeb 		}
312511db70b6SBjoern A. Zeeb 	}
312611db70b6SBjoern A. Zeeb #endif
312711db70b6SBjoern A. Zeeb 
3128d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3129d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3130d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3131d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3132e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3133018d93ecSBjoern A. Zeeb 	if (error != 0) {
3134018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3135018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3136d9f59799SBjoern A. Zeeb 		goto out;
3137018d93ecSBjoern A. Zeeb 	}
3138d9f59799SBjoern A. Zeeb 
313967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3140d9f59799SBjoern A. Zeeb 
3141d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3142d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3143d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3144d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3145e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3146018d93ecSBjoern A. Zeeb 	if (error != 0) {
3147018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3148018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3149d9f59799SBjoern A. Zeeb 		goto out;
3150018d93ecSBjoern A. Zeeb 	}
3151d9f59799SBjoern A. Zeeb 
31525a4d2461SBjoern A. Zeeb 	bss_changed = 0;
315316e688b2SBjoern A. Zeeb 	/*
31545a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
31555a4d2461SBjoern A. Zeeb 	 *
315616e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
31575a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
31585a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
31595a4d2461SBjoern A. Zeeb 	 *
31605a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
31615a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
31625a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
31635a4d2461SBjoern A. Zeeb 	 * a fw assert.
31645a4d2461SBjoern A. Zeeb 	 *
31655a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
31665a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
31675a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
31685a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
31695a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
31705a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
31715a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
31725a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
31735a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
31745a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
317516e688b2SBjoern A. Zeeb 	 */
31765a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
31775a4d2461SBjoern A. Zeeb 
31785a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
317916e688b2SBjoern A. Zeeb 
3180d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3181d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3182d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3183d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3184e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3185d9f59799SBjoern A. Zeeb 	if (error != 0) {
3186d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3187018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3188018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3189d9f59799SBjoern A. Zeeb 		goto out;
3190d9f59799SBjoern A. Zeeb 	}
3191d9f59799SBjoern A. Zeeb 
31925a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
31935a4d2461SBjoern A. Zeeb 
319416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3195d9f59799SBjoern A. Zeeb 
3196d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3197d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3198d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3199616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3200616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3201d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
32025a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
32035a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
32045a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3205d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3206d9f59799SBjoern A. Zeeb 
32072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32082ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
32092ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
32102ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32120936c648SBjoern A. Zeeb 	/*
32130936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
32140936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
32150936c648SBjoern A. Zeeb 	 * and potentially freed.
32160936c648SBjoern A. Zeeb 	 */
32170936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3218d9f59799SBjoern A. Zeeb 
3219d9f59799SBjoern A. Zeeb 	/* conf_tx */
3220d9f59799SBjoern A. Zeeb 
322150d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3222d9f59799SBjoern A. Zeeb 
3223d9f59799SBjoern A. Zeeb 	error = EALREADY;
3224d9f59799SBjoern A. Zeeb out:
3225cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3226d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3227d9f59799SBjoern A. Zeeb outni:
3228d9f59799SBjoern A. Zeeb 	return (error);
3229d9f59799SBjoern A. Zeeb }
3230d9f59799SBjoern A. Zeeb 
3231d9f59799SBjoern A. Zeeb static int
3232d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3233d9f59799SBjoern A. Zeeb {
3234d9f59799SBjoern A. Zeeb 
3235d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3236d9f59799SBjoern A. Zeeb }
3237d9f59799SBjoern A. Zeeb 
3238d9f59799SBjoern A. Zeeb static int
32396b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
32406b4cac81SBjoern A. Zeeb {
32416b4cac81SBjoern A. Zeeb 	int error;
32426b4cac81SBjoern A. Zeeb 
3243d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3244d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3245d9f59799SBjoern A. Zeeb 		return (error);
3246d9f59799SBjoern A. Zeeb 
3247d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3248d9f59799SBjoern A. Zeeb 
3249d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
32506b4cac81SBjoern A. Zeeb 	return (error);
32516b4cac81SBjoern A. Zeeb }
32526b4cac81SBjoern A. Zeeb 
3253d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32546b4cac81SBjoern A. Zeeb 
32556b4cac81SBjoern A. Zeeb /*
32566b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
32576b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
32586b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
32596b4cac81SBjoern A. Zeeb  */
32606b4cac81SBjoern A. Zeeb struct fsm_state {
32616b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
32626b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
32636b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
32646b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
32656b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
32666b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
32676b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
32686b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3269d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3270d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
32716b4cac81SBjoern A. Zeeb 
32726b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
32736b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
32746b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
32756b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3276d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
32776b4cac81SBjoern A. Zeeb 
3278d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3279d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3280d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3281d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3282d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
32836b4cac81SBjoern A. Zeeb 
3284d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3285d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3286d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
32876b4cac81SBjoern A. Zeeb 
32886b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
32896b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
32906b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
32916b4cac81SBjoern A. Zeeb 
32926b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
32936b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
32946b4cac81SBjoern A. Zeeb };
32956b4cac81SBjoern A. Zeeb 
32966b4cac81SBjoern A. Zeeb static int
32976b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
32986b4cac81SBjoern A. Zeeb {
32996b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33016b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33026b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33036b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
33046b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
33056b4cac81SBjoern A. Zeeb 	int error;
33066b4cac81SBjoern A. Zeeb 
33076b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
33086b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
33096b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
33106b4cac81SBjoern A. Zeeb 
33119d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33129d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
33136b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
33146b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
33159d9ba2b7SBjoern A. Zeeb #endif
33166b4cac81SBjoern A. Zeeb 
33176b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
33186b4cac81SBjoern A. Zeeb 
33196b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
33206b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33216b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
33226b4cac81SBjoern A. Zeeb 
33236b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
33246b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
33256b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
33266b4cac81SBjoern A. Zeeb 
33276b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
33286b4cac81SBjoern A. Zeeb 
33296b4cac81SBjoern A. Zeeb 	} else {
33306b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
33316b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
33326b4cac81SBjoern A. Zeeb 		return (ENOSYS);
33336b4cac81SBjoern A. Zeeb 	}
33346b4cac81SBjoern A. Zeeb 
33356b4cac81SBjoern A. Zeeb 	error = 0;
33366b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
33376b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
33389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33399d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3340d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3341d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3342d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3343d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
33449d9ba2b7SBjoern A. Zeeb #endif
33456b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
33466b4cac81SBjoern A. Zeeb 			break;
33476b4cac81SBjoern A. Zeeb 		}
33486b4cac81SBjoern A. Zeeb 	}
33496b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
33506b4cac81SBjoern A. Zeeb 
33516b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3352d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
33536b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
33546b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
33556b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
33566b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
33576b4cac81SBjoern A. Zeeb 		return (ENOSYS);
33586b4cac81SBjoern A. Zeeb 	}
33596b4cac81SBjoern A. Zeeb 
33606b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
33619d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33629d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3363d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3364d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3365d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3366d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
33679d9ba2b7SBjoern A. Zeeb #endif
33686b4cac81SBjoern A. Zeeb 		return (0);
33696b4cac81SBjoern A. Zeeb 	}
33706b4cac81SBjoern A. Zeeb 
33716b4cac81SBjoern A. Zeeb 	if (error != 0) {
33726b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
33736b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
33746b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
33756b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
337645c27ad5SBjoern A. Zeeb 		return (error);
33776b4cac81SBjoern A. Zeeb 	}
33786b4cac81SBjoern A. Zeeb 
33799d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3381d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3382d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
33836b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
33849d9ba2b7SBjoern A. Zeeb #endif
33856b4cac81SBjoern A. Zeeb 
33866b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
33876b4cac81SBjoern A. Zeeb }
33886b4cac81SBjoern A. Zeeb 
33896b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
33906b4cac81SBjoern A. Zeeb 
3391d9f59799SBjoern A. Zeeb /*
3392d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3393d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3394d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3395d9f59799SBjoern A. Zeeb  */
3396d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3397d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3398d9f59799SBjoern A. Zeeb {
3399d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34002ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3401d9f59799SBjoern A. Zeeb 
34022ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3403d9f59799SBjoern A. Zeeb 
3404ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
34052ac8a218SBjoern A. Zeeb 
34062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
34072ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34082ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
34092ac8a218SBjoern A. Zeeb 
34102ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
34112ac8a218SBjoern A. Zeeb 	return (rni);
3412d9f59799SBjoern A. Zeeb }
3413d9f59799SBjoern A. Zeeb 
34144a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
34156b4cac81SBjoern A. Zeeb static int
34164a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
34176b4cac81SBjoern A. Zeeb {
34184a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
34196b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34206b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34216b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34226b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
34236b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
34246b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
34256b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
34266b4cac81SBjoern A. Zeeb 	int error;
34276b4cac81SBjoern A. Zeeb 	uint16_t ac;
34286b4cac81SBjoern A. Zeeb 
3429cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3430cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
3431cd0fcf9fSBjoern A. Zeeb 
34326b4cac81SBjoern A. Zeeb 	IMPROVE();
34336b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
34346b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
34356b4cac81SBjoern A. Zeeb 
34366b4cac81SBjoern A. Zeeb 	if (vap == NULL)
34376b4cac81SBjoern A. Zeeb 		return (0);
34386b4cac81SBjoern A. Zeeb 
34394a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
34404a67f1dfSBjoern A. Zeeb 		return (0);
34414a67f1dfSBjoern A. Zeeb 
34426b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
34436b4cac81SBjoern A. Zeeb 		return (0);
34446b4cac81SBjoern A. Zeeb 
34454a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
34464a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
34474a67f1dfSBjoern A. Zeeb 		return (0);
34484a67f1dfSBjoern A. Zeeb 	}
34494a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
34506b4cac81SBjoern A. Zeeb 
34514a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
34526b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
34536b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
34546b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
34556b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
34566b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
34576b4cac81SBjoern A. Zeeb 
34584a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
34594a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
34604a67f1dfSBjoern A. Zeeb 
34616b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
34626b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
34636b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
34646b4cac81SBjoern A. Zeeb 
34656b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
34666b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
34676b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
34686b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
34696b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
34706b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
347168541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
34726b4cac81SBjoern A. Zeeb 		if (error != 0)
34733f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
34746b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
34756b4cac81SBjoern A. Zeeb 	}
34766b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
34774a67f1dfSBjoern A. Zeeb 	if (!planned)
34786b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34794a67f1dfSBjoern A. Zeeb 
34804a67f1dfSBjoern A. Zeeb 	return (changed);
34814a67f1dfSBjoern A. Zeeb }
34826b4cac81SBjoern A. Zeeb #endif
34836b4cac81SBjoern A. Zeeb 
34844a67f1dfSBjoern A. Zeeb static int
34854a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
34864a67f1dfSBjoern A. Zeeb {
34874a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
34884a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
34894a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
3490cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
34914a67f1dfSBjoern A. Zeeb 
34924a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
34934a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
34944a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
34956b4cac81SBjoern A. Zeeb 		return (0);
34964a67f1dfSBjoern A. Zeeb 
34974a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
3498cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
34994a67f1dfSBjoern A. Zeeb 
3500cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
35014a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
3502cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
35034a67f1dfSBjoern A. Zeeb #endif
35044a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
35056b4cac81SBjoern A. Zeeb }
35066b4cac81SBjoern A. Zeeb 
35074aff4048SBjoern A. Zeeb /*
35084aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
35094aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
35104aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
35114aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
35124aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
35134aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3514edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3515edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3516edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3517edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
35184aff4048SBjoern A. Zeeb  */
35194aff4048SBjoern A. Zeeb static void
35204aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
35214aff4048SBjoern A. Zeeb {
35224aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
35234aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35244aff4048SBjoern A. Zeeb 
35254aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3526edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3527edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3528edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
35294aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
35304aff4048SBjoern A. Zeeb 		return;
35314aff4048SBjoern A. Zeeb 	}
35324aff4048SBjoern A. Zeeb 
35334aff4048SBjoern A. Zeeb 	vif = arg;
353457609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
35354aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
35364aff4048SBjoern A. Zeeb }
35374aff4048SBjoern A. Zeeb 
35386b4cac81SBjoern A. Zeeb static struct ieee80211vap *
35396b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
35406b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
35416b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
35426b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
35436b4cac81SBjoern A. Zeeb {
35446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35456b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35466b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35476b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
35486b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3549a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
35506b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
355140839418SBjoern A. Zeeb 	struct sysctl_oid *node;
35526b4cac81SBjoern A. Zeeb 	size_t len;
3553e3a0b120SBjoern A. Zeeb 	int error, i;
3554a6042e17SBjoern A. Zeeb 	uint16_t ac;
35556b4cac81SBjoern A. Zeeb 
35566b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
35576b4cac81SBjoern A. Zeeb 		return (NULL);
35586b4cac81SBjoern A. Zeeb 
35596b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35606b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35616b4cac81SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
35636b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
35646b4cac81SBjoern A. Zeeb 
35656b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
35666b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3567a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
35682ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3569b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
35702ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
35716b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
35726b4cac81SBjoern A. Zeeb 
35736b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35746b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
35756b4cac81SBjoern A. Zeeb 	vif->p2p = false;
35766b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
35776b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
35786b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
35796b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
35806b4cac81SBjoern A. Zeeb 	IMPROVE();
35816b4cac81SBjoern A. Zeeb 
35826b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
35836b4cac81SBjoern A. Zeeb #if 1
3584560708cbSBjoern A. Zeeb 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3585adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
35866ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
35876ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
35884aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
35894aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
35906ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
35917b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
35926b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
35936b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
35946b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
35956b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
35966b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3597616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3598616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3599616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3600616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
36016ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3602caaa79c3SBjoern A. Zeeb 	/*
3603caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3604caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3605caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3606caaa79c3SBjoern A. Zeeb 	 */
36076ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
36086b4cac81SBjoern A. Zeeb #endif
36096b4cac81SBjoern A. Zeeb #if 0
36106b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
36116b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
36126b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
36136b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
36146b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
36156b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
36166b4cac81SBjoern A. Zeeb #endif
3617e3a0b120SBjoern A. Zeeb 
36186ffb7bd4SBjoern A. Zeeb 	/* Link Config */
36196ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
36206ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
36216ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
36226ffb7bd4SBjoern A. Zeeb 	}
36236ffb7bd4SBjoern A. Zeeb 
3624e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3625e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3626e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3627e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3628e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3629e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3630e3a0b120SBjoern A. Zeeb 		else
3631e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
36320cbcfa19SBjoern A. Zeeb 
36330cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
36340cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3635e3a0b120SBjoern A. Zeeb 	}
3636e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3637e3a0b120SBjoern A. Zeeb 
36386b4cac81SBjoern A. Zeeb 	IMPROVE();
36396b4cac81SBjoern A. Zeeb 
36406b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
36416b4cac81SBjoern A. Zeeb 	if (error != 0) {
36423f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
36436b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
36446b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
36456b4cac81SBjoern A. Zeeb 		return (NULL);
36466b4cac81SBjoern A. Zeeb 	}
36476b4cac81SBjoern A. Zeeb 
36486b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
36496b4cac81SBjoern A. Zeeb 	if (error != 0) {
36506b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
36513f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
36526b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
36536b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
36546b4cac81SBjoern A. Zeeb 		return (NULL);
36556b4cac81SBjoern A. Zeeb 	}
36566b4cac81SBjoern A. Zeeb 
36578891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36586b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
36598891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36606b4cac81SBjoern A. Zeeb 
36616b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
36626b4cac81SBjoern A. Zeeb 	changed = 0;
36636b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
36646b4cac81SBjoern A. Zeeb 
3665a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3666a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3667cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3668a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3669a6042e17SBjoern A. Zeeb 
3670a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3671a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3672a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3673a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3674a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3675a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3676a6042e17SBjoern A. Zeeb 		if (error != 0)
3677a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3678a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3679a6042e17SBjoern A. Zeeb 	}
3680cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3681a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3682a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
36836b4cac81SBjoern A. Zeeb 
36846b4cac81SBjoern A. Zeeb 	/* Force MC init. */
36856b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
36866b4cac81SBjoern A. Zeeb 
36876b4cac81SBjoern A. Zeeb 	IMPROVE();
36886b4cac81SBjoern A. Zeeb 
36896b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
36906b4cac81SBjoern A. Zeeb 
36916b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
36926b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
36936b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3694d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3695d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
36966b4cac81SBjoern A. Zeeb 
3697b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
369811db70b6SBjoern A. Zeeb 	/* Key management. */
369911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
37006b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
37016b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3702b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3703b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
37046b4cac81SBjoern A. Zeeb 	}
370511db70b6SBjoern A. Zeeb #endif
37066b4cac81SBjoern A. Zeeb 
37079fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
37089fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
37099fb91463SBjoern A. Zeeb #endif
37109fb91463SBjoern A. Zeeb 
37116b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
37126b4cac81SBjoern A. Zeeb 
37136b4cac81SBjoern A. Zeeb 	/* Complete setup. */
37146b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
37156b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
37166b4cac81SBjoern A. Zeeb 
371711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
371811db70b6SBjoern A. Zeeb 	/*
371911db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
372011db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
372111db70b6SBjoern A. Zeeb 	 */
372211db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
372311db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
372411db70b6SBjoern A. Zeeb #endif
3725ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3726ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3727ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3728ac2c7271SBjoern A. Zeeb #endif
372911db70b6SBjoern A. Zeeb 
37306b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
37316b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
37326b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
37336b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
37346b4cac81SBjoern A. Zeeb 
37356b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
37366b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
37376b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
37386b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
37396b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
37406b4cac81SBjoern A. Zeeb 	/* any others? */
374140839418SBjoern A. Zeeb 
374240839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
374340839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
374440839418SBjoern A. Zeeb 
374540839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
374640839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
374740839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
374840839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
374940839418SBjoern A. Zeeb 
375040839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
375140839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
375240839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
375340839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
375440839418SBjoern A. Zeeb 
37556b4cac81SBjoern A. Zeeb 	IMPROVE();
37566b4cac81SBjoern A. Zeeb 
37576b4cac81SBjoern A. Zeeb 	return (vap);
37586b4cac81SBjoern A. Zeeb }
37596b4cac81SBjoern A. Zeeb 
37604b0af114SBjoern A. Zeeb void
37614b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
37624b0af114SBjoern A. Zeeb {
37634b0af114SBjoern A. Zeeb 
37644b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
37654b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
37664b0af114SBjoern A. Zeeb 
37674b0af114SBjoern A. Zeeb 	IMPROVE();
37684b0af114SBjoern A. Zeeb }
37694b0af114SBjoern A. Zeeb 
37704b0af114SBjoern A. Zeeb void
37714b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
37724b0af114SBjoern A. Zeeb {
37734b0af114SBjoern A. Zeeb 
37744b0af114SBjoern A. Zeeb 	TODO();
37754b0af114SBjoern A. Zeeb }
37764b0af114SBjoern A. Zeeb 
37776b4cac81SBjoern A. Zeeb static void
37786b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
37796b4cac81SBjoern A. Zeeb {
37806b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
37816b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37826b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
37856b4cac81SBjoern A. Zeeb 
37866b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
37876b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
37886b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
37896b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37916b4cac81SBjoern A. Zeeb 
37924aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
37934aff4048SBjoern A. Zeeb 
379440839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
379540839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
379640839418SBjoern A. Zeeb 
37978891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
37986b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
37998891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
38006b4cac81SBjoern A. Zeeb 
38016b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
38026b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3803dbf76919SBjoern A. Zeeb 
3804dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3805dbf76919SBjoern A. Zeeb 
3806dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3807dbf76919SBjoern A. Zeeb 
38086c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
38097b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
38106c38c6b1SBjoern A. Zeeb 
38116b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
38126b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
38136b4cac81SBjoern A. Zeeb }
38146b4cac81SBjoern A. Zeeb 
38156b4cac81SBjoern A. Zeeb static void
38166b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
38176b4cac81SBjoern A. Zeeb {
38186b4cac81SBjoern A. Zeeb 
38196b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
38206b4cac81SBjoern A. Zeeb 	TRACEOK();
38216b4cac81SBjoern A. Zeeb }
38226b4cac81SBjoern A. Zeeb 
38236b4cac81SBjoern A. Zeeb static void
38246b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
38256b4cac81SBjoern A. Zeeb {
38266b4cac81SBjoern A. Zeeb 
38276b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38286b4cac81SBjoern A. Zeeb }
38296b4cac81SBjoern A. Zeeb 
38306b4cac81SBjoern A. Zeeb static void
38316b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
38326b4cac81SBjoern A. Zeeb {
38336b4cac81SBjoern A. Zeeb 
38346b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38356b4cac81SBjoern A. Zeeb }
38366b4cac81SBjoern A. Zeeb 
38376b4cac81SBjoern A. Zeeb /* Start / stop device. */
38386b4cac81SBjoern A. Zeeb static void
38396b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
38406b4cac81SBjoern A. Zeeb {
38416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38426b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3843cd0fcf9fSBjoern A. Zeeb #ifdef HW_START_STOP
38446b4cac81SBjoern A. Zeeb 	int error;
38458d58a057SBjoern A. Zeeb #endif
38466b4cac81SBjoern A. Zeeb 	bool start_all;
38476b4cac81SBjoern A. Zeeb 
38486b4cac81SBjoern A. Zeeb 	IMPROVE();
38496b4cac81SBjoern A. Zeeb 
38506b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38516b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38526b4cac81SBjoern A. Zeeb 	start_all = false;
38536b4cac81SBjoern A. Zeeb 
38548ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
3855cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
38566b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
38578d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
38586b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
38596b4cac81SBjoern A. Zeeb 		if (error == 0)
38608d58a057SBjoern A. Zeeb #endif
38616b4cac81SBjoern A. Zeeb 			start_all = true;
38626b4cac81SBjoern A. Zeeb 	} else {
38638d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
38647b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
38658d58a057SBjoern A. Zeeb #endif
38666b4cac81SBjoern A. Zeeb 	}
3867cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
38688ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
38696b4cac81SBjoern A. Zeeb 
38706b4cac81SBjoern A. Zeeb 	if (start_all)
38716b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
38726b4cac81SBjoern A. Zeeb }
38736b4cac81SBjoern A. Zeeb 
38746b4cac81SBjoern A. Zeeb bool
38756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
38766b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
38776b4cac81SBjoern A. Zeeb {
38786b4cac81SBjoern A. Zeeb 	int i;
38796b4cac81SBjoern A. Zeeb 
38806b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
38816b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
38826b4cac81SBjoern A. Zeeb 			return (true);
38836b4cac81SBjoern A. Zeeb 	}
38846b4cac81SBjoern A. Zeeb 
38856b4cac81SBjoern A. Zeeb 	return (false);
38866b4cac81SBjoern A. Zeeb }
38876b4cac81SBjoern A. Zeeb 
38886b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
38896b4cac81SBjoern A. Zeeb bool
38906b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
38916b4cac81SBjoern A. Zeeb {
38926b4cac81SBjoern A. Zeeb 	size_t x;
38936b4cac81SBjoern A. Zeeb 	uint8_t l;
38946b4cac81SBjoern A. Zeeb 
38956b4cac81SBjoern A. Zeeb 	x = *xp;
38966b4cac81SBjoern A. Zeeb 
38976b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
38986b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
38996b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
39006b4cac81SBjoern A. Zeeb 	x += 2 + l;
39016b4cac81SBjoern A. Zeeb 
39026b4cac81SBjoern A. Zeeb 	if (x > ies_len)
39036b4cac81SBjoern A. Zeeb 		return (false);
39046b4cac81SBjoern A. Zeeb 
39056b4cac81SBjoern A. Zeeb 	*xp = x;
39066b4cac81SBjoern A. Zeeb 	return (true);
39076b4cac81SBjoern A. Zeeb }
39086b4cac81SBjoern A. Zeeb 
3909d9945d78SBjoern A. Zeeb static uint8_t *
3910d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3911d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
39126b4cac81SBjoern A. Zeeb {
3913d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3914d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
39153dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3916d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3917d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3918d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3919d9945d78SBjoern A. Zeeb 	int band, i;
39206b4cac81SBjoern A. Zeeb 
39213dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3922d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3923d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3924d9945d78SBjoern A. Zeeb 			continue;
3925d9945d78SBjoern A. Zeeb 
3926d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3927d9945d78SBjoern A. Zeeb 		/*
3928d9945d78SBjoern A. Zeeb 		 * This should not happen;
3929d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3930d9945d78SBjoern A. Zeeb 		 */
3931d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3932d9945d78SBjoern A. Zeeb 			continue;
3933d9945d78SBjoern A. Zeeb 
3934d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3935d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3936d9945d78SBjoern A. Zeeb 		chan = NULL;
3937d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3938d9945d78SBjoern A. Zeeb 
3939d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3940d9945d78SBjoern A. Zeeb 				continue;
3941d9945d78SBjoern A. Zeeb 
39423dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3943d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3944d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3945d9945d78SBjoern A. Zeeb 				break;
3946d9945d78SBjoern A. Zeeb 		}
3947d9945d78SBjoern A. Zeeb 
3948d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3949d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3950d9945d78SBjoern A. Zeeb 			continue;
3951d9945d78SBjoern A. Zeeb 
3952d9945d78SBjoern A. Zeeb 		pb = p;
39533dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3954d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3955d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3956d9945d78SBjoern A. Zeeb 
39573dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
39583dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
39593dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
39603dd98026SBjoern A. Zeeb 
39613dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
39623dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
39633dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
39643dd98026SBjoern A. Zeeb 		}
39653dd98026SBjoern A. Zeeb #endif
39663dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
39675393cd34SBjoern A. Zeeb 		if (band == NL80211_BAND_5GHZ &&
39685393cd34SBjoern A. Zeeb 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
39693dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
39703dd98026SBjoern A. Zeeb 
39713dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
39723dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
39733dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
39743dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
39753dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
39763dd98026SBjoern A. Zeeb 		}
39773dd98026SBjoern A. Zeeb #endif
39783dd98026SBjoern A. Zeeb 
3979d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3980d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3981d9945d78SBjoern A. Zeeb 	}
3982d9945d78SBjoern A. Zeeb 
3983d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3984d9945d78SBjoern A. Zeeb 	pb = p;
3985d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3986d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3987d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3988d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3989d9945d78SBjoern A. Zeeb 	}
3990d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3991d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3992d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3993d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3994d9945d78SBjoern A. Zeeb 	}
3995d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3996d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3997d9945d78SBjoern A. Zeeb 
3998d9945d78SBjoern A. Zeeb 	return (p);
39996b4cac81SBjoern A. Zeeb }
40006b4cac81SBjoern A. Zeeb 
40016b4cac81SBjoern A. Zeeb static void
40026b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
40036b4cac81SBjoern A. Zeeb {
40046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40056b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40066b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
40076b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
40086b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
40096b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
40106b4cac81SBjoern A. Zeeb 	int error;
40118ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40126b4cac81SBjoern A. Zeeb 
40136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4015a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40166b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
40178ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40186b4cac81SBjoern A. Zeeb 		return;
40196b4cac81SBjoern A. Zeeb 	}
40208ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40226b4cac81SBjoern A. Zeeb 
40236b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
40246b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
40256b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
4026d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
40276b4cac81SBjoern A. Zeeb 		return;
40286b4cac81SBjoern A. Zeeb 	}
40296b4cac81SBjoern A. Zeeb 
40306b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40318ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4032a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4033a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4034d3ef7fb4SBjoern A. Zeeb sw_scan:
40356b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40366b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4037086be6a8SBjoern A. Zeeb 
4038086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4039086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
4040086be6a8SBjoern A. Zeeb 
40416b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
40426b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
40436b4cac81SBjoern A. Zeeb 		IMPROVE();
40446b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
40456b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
40466b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
40476b4cac81SBjoern A. Zeeb 
40486b4cac81SBjoern A. Zeeb 	} else {
40496b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
40506b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
40516b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
40526b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
40536b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4054d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
4055d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
4056d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
40573206587aSBjoern A. Zeeb 		bool running;
4058d9945d78SBjoern A. Zeeb 
4059d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4060d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
40616b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
40626b4cac81SBjoern A. Zeeb 
4063d9945d78SBjoern A. Zeeb 		band_mask = 0;
40646b4cac81SBjoern A. Zeeb 		nchan = 0;
4065c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4066c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4067d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
40686b4cac81SBjoern A. Zeeb 				nchan++;
4069d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
4070d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
4071d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
4072d9945d78SBjoern A. Zeeb 			}
4073c272abc5SBjoern A. Zeeb #else
4074c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
4075c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4076c272abc5SBjoern A. Zeeb 				switch (band) {
4077c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
4078c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
4079c272abc5SBjoern A. Zeeb 					break;
4080c272abc5SBjoern A. Zeeb 				default:
4081c272abc5SBjoern A. Zeeb 					continue;
4082c272abc5SBjoern A. Zeeb 				}
4083c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
4084c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
4085c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
4086c272abc5SBjoern A. Zeeb 				}
4087c272abc5SBjoern A. Zeeb 			}
4088c272abc5SBjoern A. Zeeb #endif
4089c272abc5SBjoern A. Zeeb 		} else {
40903206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
40913206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
40923206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
40933206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
40943206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
40953206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
40963206587aSBjoern A. Zeeb 			 */
40973206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
40983206587aSBjoern A. Zeeb 		}
40993206587aSBjoern A. Zeeb 
41006b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
41016b4cac81SBjoern A. Zeeb 
4102d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4103d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4104d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4105d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4106d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4107d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4108d9945d78SBjoern A. Zeeb 
4109d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4110d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4111d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4112d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4113d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4114d9945d78SBjoern A. Zeeb 		}
4115d9945d78SBjoern A. Zeeb 
41163206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4117d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4118d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
41196b4cac81SBjoern A. Zeeb 
41206b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
41216b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
41226b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
41236b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
41246b4cac81SBjoern A. Zeeb #if 0
41256b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
41266b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
41276b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
41286b4cac81SBjoern A. Zeeb #endif
41296b4cac81SBjoern A. Zeeb #ifdef __notyet__
41306b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
41316b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
41326b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
41336b4cac81SBjoern A. Zeeb #endif
4134e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
41356b4cac81SBjoern A. Zeeb 
41366b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
41376b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
41386b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
41396b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
41406b4cac81SBjoern A. Zeeb 			*(cpp + i) =
41416b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
41426b4cac81SBjoern A. Zeeb 		}
4143c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
41446b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4145c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
41466b4cac81SBjoern A. Zeeb 
4147c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
41486b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
41493206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
41506b4cac81SBjoern A. Zeeb 			/* lc->flags */
41516b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
41526b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
41536b4cac81SBjoern A. Zeeb 			/* lc-> ... */
41546b4cac81SBjoern A. Zeeb 			lc++;
41556b4cac81SBjoern A. Zeeb 		}
4156c272abc5SBjoern A. Zeeb #else
4157c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4158c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4159c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4160c272abc5SBjoern A. Zeeb 
4161c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4162c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4163c272abc5SBjoern A. Zeeb 				continue;
4164c272abc5SBjoern A. Zeeb 
4165c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4166c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4167c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4168c272abc5SBjoern A. Zeeb 				continue;
4169c272abc5SBjoern A. Zeeb 
4170c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4171c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4172c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4173c272abc5SBjoern A. Zeeb 				lc++;
4174c272abc5SBjoern A. Zeeb 			}
4175c272abc5SBjoern A. Zeeb 		}
4176c272abc5SBjoern A. Zeeb #endif
41776b4cac81SBjoern A. Zeeb 
4178d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
41796b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
41806b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
41816b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4182d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
41836b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
41846b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
41856b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
41866b4cac81SBjoern A. Zeeb 				ssids++;
41876b4cac81SBjoern A. Zeeb 			}
41886b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
41896b4cac81SBjoern A. Zeeb 		} else {
41906b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
41916b4cac81SBjoern A. Zeeb 		}
41926b4cac81SBjoern A. Zeeb 
41936b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
41946b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
41956b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
41966b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
41976b4cac81SBjoern A. Zeeb 		/* s6gp->... */
41986b4cac81SBjoern A. Zeeb 
4199d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4200d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4201d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4202d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4203d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4204d9945d78SBjoern A. Zeeb 
42056b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
42066b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
42073206587aSBjoern A. Zeeb 
42083206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
42093206587aSBjoern A. Zeeb 		/* Re-check under lock. */
42103206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
42113206587aSBjoern A. Zeeb 		if (!running) {
42123206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
42133206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
42143206587aSBjoern A. Zeeb 
42153206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
42163206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
42173206587aSBjoern A. Zeeb 		}
42183206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42193206587aSBjoern A. Zeeb 		if (running) {
42203206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
42213206587aSBjoern A. Zeeb 			return;
42223206587aSBjoern A. Zeeb 		}
42233206587aSBjoern A. Zeeb 
42246b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
42256b4cac81SBjoern A. Zeeb 		if (error != 0) {
4226d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4227d9945d78SBjoern A. Zeeb 
42283206587aSBjoern A. Zeeb 			/*
42293206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
42303206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
42313206587aSBjoern A. Zeeb 			 * and only there.
42323206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
42333206587aSBjoern A. Zeeb 			 * behave differently:
42343206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
42353206587aSBjoern A. Zeeb 			 *        and can then still return an error.
42363206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
42373206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
42383206587aSBjoern A. Zeeb 			 * ...
42393206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
42403206587aSBjoern A. Zeeb 			 * and balance between both code paths.
42413206587aSBjoern A. Zeeb 			 */
42423206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
42433206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
42443206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
42456b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
42463206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
42473206587aSBjoern A. Zeeb 			}
42483206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4249d3ef7fb4SBjoern A. Zeeb 
4250d3ef7fb4SBjoern A. Zeeb 			/*
4251d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4252d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4253d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4254d3ef7fb4SBjoern A. Zeeb 			 */
4255196cfd0bSBjoern A. Zeeb 			if (error == 1) {
42568ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4257a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
42588ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42593206587aSBjoern A. Zeeb 				/*
42603206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
42613206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
42623206587aSBjoern A. Zeeb 				 * currently not re-enable it?
42633206587aSBjoern A. Zeeb 				 */
42643206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4265196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4266196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4267196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4268196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4269196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4270196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4271196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4272196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4273d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4274196cfd0bSBjoern A. Zeeb 			}
4275d3ef7fb4SBjoern A. Zeeb 
4276d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4277d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
42786b4cac81SBjoern A. Zeeb 		}
42796b4cac81SBjoern A. Zeeb 	}
42806b4cac81SBjoern A. Zeeb }
42816b4cac81SBjoern A. Zeeb 
42826b4cac81SBjoern A. Zeeb static void
42836b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
42846b4cac81SBjoern A. Zeeb {
42856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42868ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
42876b4cac81SBjoern A. Zeeb 
42886b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4290a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
42918ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42926b4cac81SBjoern A. Zeeb 		return;
42936b4cac81SBjoern A. Zeeb 	}
42948ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
42958ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42966b4cac81SBjoern A. Zeeb 
42978ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4298a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4299a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
43006b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
43016b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
43026b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
43036b4cac81SBjoern A. Zeeb 
4304a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4305a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
43066b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
43076b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
43086b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4309a486fbbdSBjoern A. Zeeb 
43106b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
43116b4cac81SBjoern A. Zeeb 
43126b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4313086be6a8SBjoern A. Zeeb 
4314086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4315086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
43166b4cac81SBjoern A. Zeeb 	}
43176b4cac81SBjoern A. Zeeb }
43186b4cac81SBjoern A. Zeeb 
43196b4cac81SBjoern A. Zeeb static void
4320a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4321a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4322a486fbbdSBjoern A. Zeeb {
4323a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43248ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4325a486fbbdSBjoern A. Zeeb 
4326a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
43278ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43288ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43298ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43308ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4331a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4332a486fbbdSBjoern A. Zeeb }
4333a486fbbdSBjoern A. Zeeb 
4334a486fbbdSBjoern A. Zeeb static void
4335a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4336a486fbbdSBjoern A. Zeeb {
4337a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43388ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4339a486fbbdSBjoern A. Zeeb 
4340a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
43418ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43428ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43448ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4345a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4346a486fbbdSBjoern A. Zeeb }
4347a486fbbdSBjoern A. Zeeb 
4348a486fbbdSBjoern A. Zeeb static void
43496b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
43506b4cac81SBjoern A. Zeeb {
43516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43526b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4353b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4354b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
43556b4cac81SBjoern A. Zeeb 	int error;
43568ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
43576b4cac81SBjoern A. Zeeb 
43586b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4359b2cf3c21SBjoern A. Zeeb 
4360b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4361b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
43626b4cac81SBjoern A. Zeeb 		return;
43636b4cac81SBjoern A. Zeeb 
4364a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
43658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43668ac540d3SBjoern A. Zeeb 	hw_scan_running =
43678ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
43688ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
43698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43708ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4371a486fbbdSBjoern A. Zeeb 		return;
4372a486fbbdSBjoern A. Zeeb 
4373b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4374b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
43756b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
43766b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
43776b4cac81SBjoern A. Zeeb 		return;
43786b4cac81SBjoern A. Zeeb 	}
43796b4cac81SBjoern A. Zeeb 
43806b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
43816b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
43826b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
43836b4cac81SBjoern A. Zeeb 		    c, chan);
43846b4cac81SBjoern A. Zeeb 		return;
43856b4cac81SBjoern A. Zeeb 	}
43866b4cac81SBjoern A. Zeeb 
43876b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
43886b4cac81SBjoern A. Zeeb 	IMPROVE();
43896b4cac81SBjoern A. Zeeb 
43906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
43914a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
43929fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
439311450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
43949fb91463SBjoern A. Zeeb #endif
43954a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
43966b4cac81SBjoern A. Zeeb 
43976b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
43986b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
43996b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
44006b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
44016b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
44026b4cac81SBjoern A. Zeeb 		IMPROVE();
44036b4cac81SBjoern A. Zeeb 	} else {
44046b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
44056b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
44066b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
44076b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
44086b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
44096b4cac81SBjoern A. Zeeb 	}
44106b4cac81SBjoern A. Zeeb 
44116b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
44126b4cac81SBjoern A. Zeeb 	IMPROVE();
44136b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
44146b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
44156b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
44166b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
44176b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
44186b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
44196b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
44206b4cac81SBjoern A. Zeeb 			    error);
44216b4cac81SBjoern A. Zeeb 	}
44226b4cac81SBjoern A. Zeeb }
44236b4cac81SBjoern A. Zeeb 
44246b4cac81SBjoern A. Zeeb static struct ieee80211_node *
44256b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
44266b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
44276b4cac81SBjoern A. Zeeb {
44286b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44304f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
44316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44326b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
44336b4cac81SBjoern A. Zeeb 
44346b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
44356b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44366b4cac81SBjoern A. Zeeb 
44376b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
44384f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
44394f61ef8bSBjoern A. Zeeb 		return (NULL);
44404f61ef8bSBjoern A. Zeeb 
44416b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
44426b4cac81SBjoern A. Zeeb 	if (ni == NULL)
44436b4cac81SBjoern A. Zeeb 		return (NULL);
44446b4cac81SBjoern A. Zeeb 
44456b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44464f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
44476b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
44486b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
44496b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
44506b4cac81SBjoern A. Zeeb 		return (NULL);
44516b4cac81SBjoern A. Zeeb 	}
44526b4cac81SBjoern A. Zeeb 
44536b4cac81SBjoern A. Zeeb 	return (ni);
44546b4cac81SBjoern A. Zeeb }
44556b4cac81SBjoern A. Zeeb 
44566b4cac81SBjoern A. Zeeb static int
44576b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
44586b4cac81SBjoern A. Zeeb {
44596b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44606b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44616b4cac81SBjoern A. Zeeb 	int error;
44626b4cac81SBjoern A. Zeeb 
44636b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44646b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44656b4cac81SBjoern A. Zeeb 
44666b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
44676b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
44686b4cac81SBjoern A. Zeeb 		if (error != 0)
44696b4cac81SBjoern A. Zeeb 			return (error);
44706b4cac81SBjoern A. Zeeb 	}
44716b4cac81SBjoern A. Zeeb 
44726b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
44736b4cac81SBjoern A. Zeeb 	IMPROVE();
44746b4cac81SBjoern A. Zeeb 
44756b4cac81SBjoern A. Zeeb 	return (0);
44766b4cac81SBjoern A. Zeeb }
44776b4cac81SBjoern A. Zeeb 
44786b4cac81SBjoern A. Zeeb static void
44796b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
44806b4cac81SBjoern A. Zeeb {
44816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44836b4cac81SBjoern A. Zeeb 
44846b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44856b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44866b4cac81SBjoern A. Zeeb 
44876b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
44886b4cac81SBjoern A. Zeeb 	IMPROVE();
44896b4cac81SBjoern A. Zeeb 
44906b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
44916b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
44926b4cac81SBjoern A. Zeeb }
44936b4cac81SBjoern A. Zeeb 
44946b4cac81SBjoern A. Zeeb static void
44956b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
44966b4cac81SBjoern A. Zeeb {
44976b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44996b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45006b4cac81SBjoern A. Zeeb 
45016b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45026b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45036b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
45046b4cac81SBjoern A. Zeeb 
45050936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
45066b4cac81SBjoern A. Zeeb 
45070936c648SBjoern A. Zeeb 	/*
45080936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
45090936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
45100936c648SBjoern A. Zeeb 	 */
45110936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
45126b4cac81SBjoern A. Zeeb 
45136b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
45146b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
45156b4cac81SBjoern A. Zeeb }
45166b4cac81SBjoern A. Zeeb 
45176b4cac81SBjoern A. Zeeb static int
45186b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
45196b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
45206b4cac81SBjoern A. Zeeb {
45216b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4522*c816f64eSBjoern A. Zeeb 	int error;
45236b4cac81SBjoern A. Zeeb 
45246b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4525fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
45262372f8ccSBjoern A. Zeeb #if 0
452788665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
45282372f8ccSBjoern A. Zeeb #else
45292372f8ccSBjoern A. Zeeb 	/*
45302372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
45312372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
45322372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
45332372f8ccSBjoern A. Zeeb 	 */
45342372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
45352372f8ccSBjoern A. Zeeb #endif
4536fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4537fa4e4257SBjoern A. Zeeb 		/*
4538fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4539fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4540fa4e4257SBjoern A. Zeeb 		 */
45410936c648SBjoern A. Zeeb 		m_free(m);
45420936c648SBjoern A. Zeeb 		return (ENETDOWN);
45430936c648SBjoern A. Zeeb 	}
45446b4cac81SBjoern A. Zeeb 
45456b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
4546*c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lsta->txq, m);
4547*c816f64eSBjoern A. Zeeb 	if (error != 0) {
4548*c816f64eSBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4549*c816f64eSBjoern A. Zeeb 		/*
4550*c816f64eSBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4551*c816f64eSBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4552*c816f64eSBjoern A. Zeeb 		 */
4553*c816f64eSBjoern A. Zeeb 		m_free(m);
4554*c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4555*c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4556*c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4557*c816f64eSBjoern A. Zeeb 			    __func__, error);
4558*c816f64eSBjoern A. Zeeb #endif
4559*c816f64eSBjoern A. Zeeb 		return (ENETDOWN);
4560*c816f64eSBjoern A. Zeeb 	}
4561fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4562fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
45636b4cac81SBjoern A. Zeeb 
45649d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45659d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
45666b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
45676b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
45686b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
45699d9ba2b7SBjoern A. Zeeb #endif
45706b4cac81SBjoern A. Zeeb 
45716b4cac81SBjoern A. Zeeb 	return (0);
45726b4cac81SBjoern A. Zeeb }
45736b4cac81SBjoern A. Zeeb 
457411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
457511db70b6SBjoern A. Zeeb static int
457611db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
457711db70b6SBjoern A. Zeeb     struct sk_buff *skb)
457811db70b6SBjoern A. Zeeb {
457911db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
458011db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
458111db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
458211db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
458311db70b6SBjoern A. Zeeb 	uint8_t *p;
458411db70b6SBjoern A. Zeeb 
458511db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
458611db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
458711db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
458811db70b6SBjoern A. Zeeb 
458911db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
459011db70b6SBjoern A. Zeeb 
459111db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
459211db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
459311db70b6SBjoern A. Zeeb 
459411db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
459511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
459611db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
459711db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
459811db70b6SBjoern A. Zeeb 		return (ENXIO);
459911db70b6SBjoern A. Zeeb 	}
460011db70b6SBjoern A. Zeeb 
460111db70b6SBjoern A. Zeeb 
460211db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
460311db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
460411db70b6SBjoern A. Zeeb 
460511db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
460611db70b6SBjoern A. Zeeb 
460711db70b6SBjoern A. Zeeb 	/*
460811db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
460911db70b6SBjoern A. Zeeb 	 * or if we are done?
461011db70b6SBjoern A. Zeeb 	 */
461111db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
461211db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
461311db70b6SBjoern A. Zeeb 	    /* MFP */
461411db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
461511db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
461611db70b6SBjoern A. Zeeb 			return (0);
461711db70b6SBjoern A. Zeeb 
461811db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
461911db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
462011db70b6SBjoern A. Zeeb 		return (ENOSPC);
462111db70b6SBjoern A. Zeeb 
462211db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
462311db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
462411db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
462511db70b6SBjoern A. Zeeb 
462611db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
462711db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
462811db70b6SBjoern A. Zeeb 		return (0);
462911db70b6SBjoern A. Zeeb 
463011db70b6SBjoern A. Zeeb 	p += hdrlen;
463111db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
463211db70b6SBjoern A. Zeeb 
463311db70b6SBjoern A. Zeeb 	return (0);
463411db70b6SBjoern A. Zeeb }
463511db70b6SBjoern A. Zeeb #endif
463611db70b6SBjoern A. Zeeb 
46376b4cac81SBjoern A. Zeeb static void
46386b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
46396b4cac81SBjoern A. Zeeb {
46406b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
46416b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
46426b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
46436b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
46446b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46456b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46466b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
46476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
46486b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
46496b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
46506b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
46516b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
46526b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4653e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4654ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
46556b4cac81SBjoern A. Zeeb 	void *buf;
465611db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4657e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
46586b4cac81SBjoern A. Zeeb 
46596b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
46606b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46619d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
46626b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
46636b4cac81SBjoern A. Zeeb #endif
46646b4cac81SBjoern A. Zeeb 
46656b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4666b35f6cd0SBjoern A. Zeeb 	k = NULL;
466711db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
46686b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
46696b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
467011db70b6SBjoern A. Zeeb 
467111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
467211db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
467311db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
467411db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
467511db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
467611db70b6SBjoern A. Zeeb 		}
467711db70b6SBjoern A. Zeeb #endif
467811db70b6SBjoern A. Zeeb 
467911db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
468011db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
46816b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
46826b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
46836b4cac81SBjoern A. Zeeb 			if (k == NULL) {
46846b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
46856b4cac81SBjoern A. Zeeb 				m_freem(m);
46866b4cac81SBjoern A. Zeeb 				return;
46876b4cac81SBjoern A. Zeeb 			}
46886b4cac81SBjoern A. Zeeb 		}
468911db70b6SBjoern A. Zeeb 	}
46906b4cac81SBjoern A. Zeeb 
46916b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46926b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46936b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
46946b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
46956b4cac81SBjoern A. Zeeb 
46966b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
46976b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
46986b4cac81SBjoern A. Zeeb 
46996b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
47006b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
47016b4cac81SBjoern A. Zeeb 		if (k != NULL)
47026b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
47036b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
47046b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
47056b4cac81SBjoern A. Zeeb 		IMPROVE();
47066b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
47076b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
47086b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
47096b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
47106b4cac81SBjoern A. Zeeb 		}
47116b4cac81SBjoern A. Zeeb 
47126b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
47136b4cac81SBjoern A. Zeeb 	}
47146b4cac81SBjoern A. Zeeb 
47156b4cac81SBjoern A. Zeeb 	/*
47166b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
47176b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
47183d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
47193d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
47206b4cac81SBjoern A. Zeeb 	 */
47216b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
47226b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4723bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4724bcf1d8eeSBjoern A. Zeeb 
4725bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4726bcf1d8eeSBjoern A. Zeeb 			int tid;
4727bcf1d8eeSBjoern A. Zeeb 
4728bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4729bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4730bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4731bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4732bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4733bcf1d8eeSBjoern A. Zeeb 					continue;
4734bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4735bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4736bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4737bcf1d8eeSBjoern A. Zeeb 			}
4738bcf1d8eeSBjoern A. Zeeb 		}
47396b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
47406b4cac81SBjoern A. Zeeb 		m_freem(m);
47416b4cac81SBjoern A. Zeeb 		return;
47426b4cac81SBjoern A. Zeeb 	}
47436b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
47446b4cac81SBjoern A. Zeeb 
47456b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
47466b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
47476b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
47486b4cac81SBjoern A. Zeeb 	skb->m = m;
47496b4cac81SBjoern A. Zeeb #if 0
47506b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
47516b4cac81SBjoern A. Zeeb #else
47526b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
47536b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
47546b4cac81SBjoern A. Zeeb #endif
47556b4cac81SBjoern A. Zeeb 	/* Save the ni. */
47566b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
47576b4cac81SBjoern A. Zeeb 
47586b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
47596b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
47606b4cac81SBjoern A. Zeeb 
4761e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4762e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4763e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
47641665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
47651665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
47661665ef97SBjoern A. Zeeb 			skb->priority = 7;
47671665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
47681665ef97SBjoern A. Zeeb 		} else {
47691665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
47701665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4771e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4772e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
47731665ef97SBjoern A. Zeeb 		}
4774e3a0b120SBjoern A. Zeeb 	} else {
4775e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4776fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4777e3a0b120SBjoern A. Zeeb 	}
47786b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
47796b4cac81SBjoern A. Zeeb 
47806b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
47816b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
47826b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
47836b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
47846b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
47856b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4786e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
47876b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
47886b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
47896b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
47906b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
47919fb91463SBjoern A. Zeeb #ifdef __notyet__
47929fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
47939fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
47949fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
47959fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
47969fb91463SBjoern A. Zeeb #endif
47979fb91463SBjoern A. Zeeb #endif
47986b4cac81SBjoern A. Zeeb 
47996b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4800b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
480111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
480211db70b6SBjoern A. Zeeb 		int error;
480311db70b6SBjoern A. Zeeb 
480411db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
480511db70b6SBjoern A. Zeeb 		if (error != 0) {
480611db70b6SBjoern A. Zeeb 			/*
480711db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
480811db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
480911db70b6SBjoern A. Zeeb 			 */
481011db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
481111db70b6SBjoern A. Zeeb 			return;
481211db70b6SBjoern A. Zeeb 		}
481311db70b6SBjoern A. Zeeb 	}
48146b4cac81SBjoern A. Zeeb #endif
48156b4cac81SBjoern A. Zeeb 
48166b4cac81SBjoern A. Zeeb 	IMPROVE();
48176b4cac81SBjoern A. Zeeb 
4818e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4819e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4820e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4821e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4822e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4823d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4824e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4825e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4826e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4827e3a0b120SBjoern A. Zeeb 	}
4828e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4829d0d29110SBjoern A. Zeeb 		goto ops_tx;
4830e3a0b120SBjoern A. Zeeb 
4831d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4832d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4833d0d29110SBjoern A. Zeeb 
4834eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
48356b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
48369d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
48379d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4838d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4839d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4840d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4841fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4842d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4843d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4844d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4845d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
48469d9ba2b7SBjoern A. Zeeb #endif
4847eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
4848cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
4849d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
4850cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
48516b4cac81SBjoern A. Zeeb 	return;
48526b4cac81SBjoern A. Zeeb 
48536b4cac81SBjoern A. Zeeb ops_tx:
48549d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
48559d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4856d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4857d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
48586b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
48596b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
48609d9ba2b7SBjoern A. Zeeb #endif
48616b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
48626b4cac81SBjoern A. Zeeb 	control.sta = sta;
4863cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
48646b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
4865cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
48666b4cac81SBjoern A. Zeeb }
48676b4cac81SBjoern A. Zeeb 
48686b4cac81SBjoern A. Zeeb static void
48696b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
48706b4cac81SBjoern A. Zeeb {
48716b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
48726b4cac81SBjoern A. Zeeb 	struct mbufq mq;
48736b4cac81SBjoern A. Zeeb 	struct mbuf *m;
487488665349SBjoern A. Zeeb 	bool shall_tx;
48756b4cac81SBjoern A. Zeeb 
48766b4cac81SBjoern A. Zeeb 	lsta = ctx;
48776b4cac81SBjoern A. Zeeb 
48789d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
48799d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
48806b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
48819d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
48826b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
48839d9ba2b7SBjoern A. Zeeb #endif
48846b4cac81SBjoern A. Zeeb 
48856b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
48866b4cac81SBjoern A. Zeeb 
4887fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4888fa4e4257SBjoern A. Zeeb 	/*
4889fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
489088665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
489188665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
489288665349SBjoern A. Zeeb 	 * point to try to TX.
489388665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
489488665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4895fa4e4257SBjoern A. Zeeb 	 */
48962372f8ccSBjoern A. Zeeb #if 0
489788665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
48982372f8ccSBjoern A. Zeeb #else
48992372f8ccSBjoern A. Zeeb 	/*
49002372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
49012372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
49022372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
49032372f8ccSBjoern A. Zeeb 	 */
49042372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
49052372f8ccSBjoern A. Zeeb #endif
490688665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
49076b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
490888665349SBjoern A. Zeeb 	/*
490988665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
491088665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
491188665349SBjoern A. Zeeb 	 */
4912fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
49136b4cac81SBjoern A. Zeeb 
49146b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
49156b4cac81SBjoern A. Zeeb 	while (m != NULL) {
49166b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
49176b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
49186b4cac81SBjoern A. Zeeb 	}
49196b4cac81SBjoern A. Zeeb }
49206b4cac81SBjoern A. Zeeb 
49216b4cac81SBjoern A. Zeeb static int
49226b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
49236b4cac81SBjoern A. Zeeb {
49246b4cac81SBjoern A. Zeeb 
49256b4cac81SBjoern A. Zeeb 	/* XXX TODO */
49266b4cac81SBjoern A. Zeeb 	IMPROVE();
49276b4cac81SBjoern A. Zeeb 
49286b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
49296b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
49306b4cac81SBjoern A. Zeeb 
49316b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
49326b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
49336b4cac81SBjoern A. Zeeb }
49346b4cac81SBjoern A. Zeeb 
49359fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
49369fb91463SBjoern A. Zeeb static int
49379fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
49389fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
49399fb91463SBjoern A. Zeeb {
49409fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49419fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49429fb91463SBjoern A. Zeeb 
49439fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49449fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49459fb91463SBjoern A. Zeeb 
4946310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
49479fb91463SBjoern A. Zeeb 
49489fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
49499fb91463SBjoern A. Zeeb }
49509fb91463SBjoern A. Zeeb 
49519fb91463SBjoern A. Zeeb static int
49529fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
49539fb91463SBjoern A. Zeeb {
49549fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49559fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49569fb91463SBjoern A. Zeeb 
49579fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49589fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49599fb91463SBjoern A. Zeeb 
4960310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
49619fb91463SBjoern A. Zeeb 
49629fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
49639fb91463SBjoern A. Zeeb }
49649fb91463SBjoern A. Zeeb 
49659fb91463SBjoern A. Zeeb 
49669fb91463SBjoern A. Zeeb static int
49679fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49689fb91463SBjoern A. Zeeb {
49699fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49709fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49719fb91463SBjoern A. Zeeb 
49729fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49739fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49749fb91463SBjoern A. Zeeb 
4975310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
49769fb91463SBjoern A. Zeeb 
49779fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
49789fb91463SBjoern A. Zeeb }
49799fb91463SBjoern A. Zeeb 
4980310743c4SBjoern A. Zeeb /*
4981310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4982310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4983310743c4SBjoern A. Zeeb  *
4984310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4985310743c4SBjoern A. Zeeb  */
49869fb91463SBjoern A. Zeeb static int
49879fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
49889fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
49899fb91463SBjoern A. Zeeb {
49909fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49919fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4992310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4993310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4994310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4995310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4996310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4997310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4998310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4999310743c4SBjoern A. Zeeb 	int error;
50009fb91463SBjoern A. Zeeb 
50019fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50029fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5003310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5004310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5005310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5006310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5007310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5008310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50099fb91463SBjoern A. Zeeb 
5010310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5011310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5012310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5013310743c4SBjoern A. Zeeb 		return (0);
5014310743c4SBjoern A. Zeeb 	}
5015310743c4SBjoern A. Zeeb 
5016310743c4SBjoern A. Zeeb 	params.sta = sta;
5017310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
5018310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
5019310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5020310743c4SBjoern A. Zeeb 	params.timeout = 0;
5021310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5022310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5023310743c4SBjoern A. Zeeb 	params.amsdu = false;
5024310743c4SBjoern A. Zeeb 
5025310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5026cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5027310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5028cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5029310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5030310743c4SBjoern A. Zeeb 	if (error != 0) {
5031310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5032310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5033310743c4SBjoern A. Zeeb 		return (0);
5034310743c4SBjoern A. Zeeb 	}
50359fb91463SBjoern A. Zeeb 
50369fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
50379fb91463SBjoern A. Zeeb }
50389fb91463SBjoern A. Zeeb 
5039310743c4SBjoern A. Zeeb /*
5040310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5041310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
5042310743c4SBjoern A. Zeeb  *
5043310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
5044310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
5045310743c4SBjoern A. Zeeb  */
50469fb91463SBjoern A. Zeeb static int
50479fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
50489fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
50499fb91463SBjoern A. Zeeb {
50509fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50519fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5052310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5053310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5054310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5055310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5056310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5057310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5058310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5059310743c4SBjoern A. Zeeb 	int error;
50609fb91463SBjoern A. Zeeb 
50619fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50629fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5063310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5064310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5065310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5066310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5067310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5068310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50699fb91463SBjoern A. Zeeb 
5070310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5071310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5072310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5073310743c4SBjoern A. Zeeb 		return (0);
5074310743c4SBjoern A. Zeeb 	}
5075310743c4SBjoern A. Zeeb 
5076310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
5077310743c4SBjoern A. Zeeb 		params.sta = sta;
5078310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5079310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
5080310743c4SBjoern A. Zeeb 		params.timeout = 0;
5081310743c4SBjoern A. Zeeb 		params.ssn = 0;
5082310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5083310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5084310743c4SBjoern A. Zeeb 			params.amsdu = true;
5085310743c4SBjoern A. Zeeb 		else
5086310743c4SBjoern A. Zeeb 			params.amsdu = false;
5087310743c4SBjoern A. Zeeb 	} else {
5088310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
5089310743c4SBjoern A. Zeeb 		params.sta = sta;
5090310743c4SBjoern A. Zeeb 		switch (status) {
5091310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
5092310743c4SBjoern A. Zeeb 		default:
5093310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5094310743c4SBjoern A. Zeeb 			break;
5095310743c4SBjoern A. Zeeb 		}
5096310743c4SBjoern A. Zeeb 		params.buf_size = 0;
5097310743c4SBjoern A. Zeeb 		params.timeout = 0;
5098310743c4SBjoern A. Zeeb 		params.ssn = 0;
5099310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5100310743c4SBjoern A. Zeeb 		params.amsdu = false;
5101310743c4SBjoern A. Zeeb 	}
5102310743c4SBjoern A. Zeeb 
5103310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5104cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5105310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5106cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5107310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5108310743c4SBjoern A. Zeeb 	if (error != 0) {
5109310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5110310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5111310743c4SBjoern A. Zeeb 		return (0);
5112310743c4SBjoern A. Zeeb 	}
5113310743c4SBjoern A. Zeeb 
5114310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
51159fb91463SBjoern A. Zeeb 
51169fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
51179fb91463SBjoern A. Zeeb }
51189fb91463SBjoern A. Zeeb 
5119310743c4SBjoern A. Zeeb /*
5120310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5121310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5122310743c4SBjoern A. Zeeb  */
51239fb91463SBjoern A. Zeeb static void
51249fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
51259fb91463SBjoern A. Zeeb {
51269fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51279fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5128310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5129310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5130310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5131310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5132310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5133310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5134310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5135310743c4SBjoern A. Zeeb 	int error;
51369fb91463SBjoern A. Zeeb 
51379fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51389fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5139310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5140310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5141310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5142310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5143310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5144310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51459fb91463SBjoern A. Zeeb 
5146310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5147310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5148310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5149310743c4SBjoern A. Zeeb 		goto n80211;
5150310743c4SBjoern A. Zeeb 	}
51519fb91463SBjoern A. Zeeb 
5152310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5153310743c4SBjoern A. Zeeb 	params.sta = sta;
5154310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5155310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5156310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5157310743c4SBjoern A. Zeeb 	params.timeout = 0;
5158310743c4SBjoern A. Zeeb 	params.ssn = 0;
5159310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5160310743c4SBjoern A. Zeeb 	params.amsdu = false;
5161310743c4SBjoern A. Zeeb 
5162310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5163cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5164310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5165cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5166310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5167310743c4SBjoern A. Zeeb 	if (error != 0) {
5168310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5169310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5170310743c4SBjoern A. Zeeb 		goto n80211;
5171310743c4SBjoern A. Zeeb 	}
5172310743c4SBjoern A. Zeeb 
5173310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5174310743c4SBjoern A. Zeeb 
5175310743c4SBjoern A. Zeeb n80211:
51769fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
51779fb91463SBjoern A. Zeeb }
51789fb91463SBjoern A. Zeeb 
51799fb91463SBjoern A. Zeeb static void
51809fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
51819fb91463SBjoern A. Zeeb {
51829fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51839fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51849fb91463SBjoern A. Zeeb 
51859fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51869fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51879fb91463SBjoern A. Zeeb 
51889fb91463SBjoern A. Zeeb 	IMPROVE_HT();
51899fb91463SBjoern A. Zeeb 
51909fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
51919fb91463SBjoern A. Zeeb }
51929fb91463SBjoern A. Zeeb 
51939fb91463SBjoern A. Zeeb static void
51949fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
51959fb91463SBjoern A. Zeeb     int status)
51969fb91463SBjoern A. Zeeb {
51979fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51989fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51999fb91463SBjoern A. Zeeb 
52009fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52019fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52029fb91463SBjoern A. Zeeb 
52039fb91463SBjoern A. Zeeb 	IMPROVE_HT();
52049fb91463SBjoern A. Zeeb 
52059fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
52069fb91463SBjoern A. Zeeb }
52079fb91463SBjoern A. Zeeb 
52089fb91463SBjoern A. Zeeb static int
52099fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
52109fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
52119fb91463SBjoern A. Zeeb {
52129fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52139fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52149fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52159fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
52169fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
52179fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
52189fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
52199fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5220310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
52219fb91463SBjoern A. Zeeb 	int error;
52229fb91463SBjoern A. Zeeb 
52239fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52249fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52259fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
52269fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
52279fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
52289fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
52299fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
52309fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52319fb91463SBjoern A. Zeeb 
5232310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5233310743c4SBjoern A. Zeeb 
5234310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5235310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5236310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5237310743c4SBjoern A. Zeeb 		return (-ENXIO);
5238310743c4SBjoern A. Zeeb 	}
5239310743c4SBjoern A. Zeeb 
52409fb91463SBjoern A. Zeeb 	params.sta = sta;
52419fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
52429fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
52439fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
52449fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
52459fb91463SBjoern A. Zeeb 	else
52469fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5247310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5248310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
52499fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
52509fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
52519fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
52529fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5253310743c4SBjoern A. Zeeb 
5254310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5255310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5256310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5257310743c4SBjoern A. Zeeb 		params.amsdu = true;
5258310743c4SBjoern A. Zeeb 	else
52599fb91463SBjoern A. Zeeb 		params.amsdu = false;
52609fb91463SBjoern A. Zeeb 
5261cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
52629fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5263cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
52649fb91463SBjoern A. Zeeb 	if (error != 0) {
52659fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
52669fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
52679fb91463SBjoern A. Zeeb 		return (error);
52689fb91463SBjoern A. Zeeb 	}
5269310743c4SBjoern A. Zeeb 
5270310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5271310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5272310743c4SBjoern A. Zeeb 	}
5273310743c4SBjoern A. Zeeb 
52749fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
52759fb91463SBjoern A. Zeeb 
52769fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
52779fb91463SBjoern A. Zeeb 	return (error);
52789fb91463SBjoern A. Zeeb }
52799fb91463SBjoern A. Zeeb 
52809fb91463SBjoern A. Zeeb static void
52819fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
52829fb91463SBjoern A. Zeeb {
52839fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52849fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52859fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52869fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
52879fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
52889fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
52899fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
52909fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5291310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
52929fb91463SBjoern A. Zeeb 	int error;
52939fb91463SBjoern A. Zeeb 	uint8_t tid;
529465c573e4SBjoern A. Zeeb 	bool ic_locked;
52959fb91463SBjoern A. Zeeb 
52969fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52979fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52989fb91463SBjoern A. Zeeb 
52999fb91463SBjoern A. Zeeb 	/*
53009fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
53019fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
53029fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
53039fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
53049fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
53059fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
53069fb91463SBjoern A. Zeeb 	 * track some state itself.
53079fb91463SBjoern A. Zeeb 	 */
53089fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
53099fb91463SBjoern A. Zeeb 		goto net80211_only;
53109fb91463SBjoern A. Zeeb 
53119fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53129fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
53139fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
53149fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
53159fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
53169fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53179fb91463SBjoern A. Zeeb 
53189fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
53199fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
53209fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
53219fb91463SBjoern A. Zeeb 			break;
53229fb91463SBjoern A. Zeeb 	}
53239fb91463SBjoern A. Zeeb 
53249fb91463SBjoern A. Zeeb 	params.sta = sta;
53259fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
53269fb91463SBjoern A. Zeeb 	params.buf_size = 0;
53279fb91463SBjoern A. Zeeb 	params.timeout = 0;
53289fb91463SBjoern A. Zeeb 	params.ssn = 0;
53299fb91463SBjoern A. Zeeb 	params.tid = tid;
53309fb91463SBjoern A. Zeeb 	params.amsdu = false;
53319fb91463SBjoern A. Zeeb 
533265c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
533365c573e4SBjoern A. Zeeb 	if (ic_locked)
533465c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5335cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
53369fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5337cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
533865c573e4SBjoern A. Zeeb 	if (ic_locked)
533965c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
53409fb91463SBjoern A. Zeeb 	if (error != 0)
53419fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
53429fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
53439fb91463SBjoern A. Zeeb 
53449fb91463SBjoern A. Zeeb net80211_only:
53459fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
53469fb91463SBjoern A. Zeeb }
53479fb91463SBjoern A. Zeeb #endif
53489fb91463SBjoern A. Zeeb 
53499fb91463SBjoern A. Zeeb static void
53509fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
53519fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
53529fb91463SBjoern A. Zeeb {
53539fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
53549fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
53559fb91463SBjoern A. Zeeb 
53569fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
53579fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
53589fb91463SBjoern A. Zeeb 		return;
53599fb91463SBjoern A. Zeeb 
53609fb91463SBjoern A. Zeeb 	switch (band) {
53619fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
53629fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
53639fb91463SBjoern A. Zeeb 		break;
53649fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
53659fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
53669fb91463SBjoern A. Zeeb 		break;
53679fb91463SBjoern A. Zeeb 	default:
53689fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
53699fb91463SBjoern A. Zeeb 		return;
53709fb91463SBjoern A. Zeeb 	}
53719fb91463SBjoern A. Zeeb 
53729fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
53739fb91463SBjoern A. Zeeb 
53749fb91463SBjoern A. Zeeb 	/*
53759fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
53769fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
53779fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
53789fb91463SBjoern A. Zeeb 	 */
53799fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
53809fb91463SBjoern A. Zeeb 
53819fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
53829fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
53839fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
53849fb91463SBjoern A. Zeeb #ifdef __notyet__
53859fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
53869fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
53879fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
53889fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
53899fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
53909fb91463SBjoern A. Zeeb #endif
53919fb91463SBjoern A. Zeeb 
53929fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
53939fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
53949fb91463SBjoern A. Zeeb 
53959fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
53969fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
53979fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
53989fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
53999fb91463SBjoern A. Zeeb #endif
54009fb91463SBjoern A. Zeeb }
54019fb91463SBjoern A. Zeeb 
54026b4cac81SBjoern A. Zeeb static void
54036b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
54046b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
54056b4cac81SBjoern A. Zeeb {
54066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54076b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54086b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
54096b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
54106b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
54116b4cac81SBjoern A. Zeeb 
54126b4cac81SBjoern A. Zeeb 	/* Channels */
54136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
54146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54156b4cac81SBjoern A. Zeeb 
54166b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
54176b4cac81SBjoern A. Zeeb 	nchans = 0;
54186b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
54196b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
54206b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
54216b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
54226b4cac81SBjoern A. Zeeb 		chan_flags = 0;
54236b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
54246b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
54259fb91463SBjoern A. Zeeb 
54269fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
54276b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
54289fb91463SBjoern A. Zeeb 
54299fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
54309fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
54316b4cac81SBjoern A. Zeeb 
54326b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5433cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
54346b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
54356b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
54366b4cac81SBjoern A. Zeeb 
54376b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
54383f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
54393f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
54406b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
54416b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
54426b4cac81SBjoern A. Zeeb 				continue;
54436b4cac81SBjoern A. Zeeb 			}
54446b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
54456b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
54466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
54476b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
54486b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
54496b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
54506b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
54516b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
54526b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
54536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
54546b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
54556b4cac81SBjoern A. Zeeb 
54566b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
54576b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
54586b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
54595856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5460cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5461cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
54623f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
54633f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
54646b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
54656b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
54666b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5467cee56e77SBjoern A. Zeeb 			if (error != 0)
54686b4cac81SBjoern A. Zeeb 				break;
54696b4cac81SBjoern A. Zeeb 		}
54706b4cac81SBjoern A. Zeeb 	}
54716b4cac81SBjoern A. Zeeb 
54726b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
54736b4cac81SBjoern A. Zeeb 	nchans = 0;
54746b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
54756b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
54766b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
54776b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
54786b4cac81SBjoern A. Zeeb 		chan_flags = 0;
54796b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
54809fb91463SBjoern A. Zeeb 
54819fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
54829fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
54839fb91463SBjoern A. Zeeb 
54849fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
54856b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
54866b4cac81SBjoern A. Zeeb 
54876b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5488562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
54896b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5490ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5491ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
54926b4cac81SBjoern A. Zeeb 
54936b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
54946b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
54956b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5496562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
54976b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
54986b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5499562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
55006b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
55016b4cac81SBjoern A. Zeeb 		}
55026b4cac81SBjoern A. Zeeb #endif
55036b4cac81SBjoern A. Zeeb 
55046b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5505cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
55066b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
55076b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
55086b4cac81SBjoern A. Zeeb 
55096b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
55103f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
55113f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
55126b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
55136b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
55146b4cac81SBjoern A. Zeeb 				continue;
55156b4cac81SBjoern A. Zeeb 			}
55166b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
55176b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
55186b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
55196b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
55206b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
55216b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
55226b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
55236b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
55246b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
55256b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
55266b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
55276b4cac81SBjoern A. Zeeb 
55286b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
55296b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
55306b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
55315856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5532cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5533cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
55343f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
55353f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
55366b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
55376b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
55386b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5539cee56e77SBjoern A. Zeeb 			if (error != 0)
55406b4cac81SBjoern A. Zeeb 				break;
55416b4cac81SBjoern A. Zeeb 		}
55426b4cac81SBjoern A. Zeeb 	}
55436b4cac81SBjoern A. Zeeb }
55446b4cac81SBjoern A. Zeeb 
55456b4cac81SBjoern A. Zeeb static void *
55466b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
55476b4cac81SBjoern A. Zeeb {
55486b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
55496b4cac81SBjoern A. Zeeb 
55506b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
55516b4cac81SBjoern A. Zeeb 
55526b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
55536b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
55546b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
55556b4cac81SBjoern A. Zeeb 
55566b4cac81SBjoern A. Zeeb 	return (ic);
55576b4cac81SBjoern A. Zeeb }
55586b4cac81SBjoern A. Zeeb 
55596b4cac81SBjoern A. Zeeb struct ieee80211_hw *
55606b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
55616b4cac81SBjoern A. Zeeb {
55626b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
55636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55646b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5565a5ae63edSBjoern A. Zeeb 	int ac;
55666b4cac81SBjoern A. Zeeb 
55676b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
55686b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
55696b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
55706b4cac81SBjoern A. Zeeb 		return (NULL);
55716b4cac81SBjoern A. Zeeb 
55726b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
55736b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5574652e22d3SBjoern A. Zeeb 
55758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5576eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
55778891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
55786b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5579a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5580a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5581a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5582a5ae63edSBjoern A. Zeeb 	}
55836b4cac81SBjoern A. Zeeb 
5584a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf */
5585a8a47a41SBjoern A. Zeeb 	INIT_LIST_HEAD(&lhw->lchanctx_list);
5586a8a47a41SBjoern A. Zeeb 
5587759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5588759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5589759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5590759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5591759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5592759a996dSBjoern A. Zeeb 
55936b4cac81SBjoern A. Zeeb 	/*
55946b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
55956b4cac81SBjoern A. Zeeb 	 * not initialized.
55966b4cac81SBjoern A. Zeeb 	 */
55976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
55986b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5599086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
56006b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
56016b4cac81SBjoern A. Zeeb 
56026b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
56036b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
56046b4cac81SBjoern A. Zeeb 
56056b4cac81SBjoern A. Zeeb 	IMPROVE();
56066b4cac81SBjoern A. Zeeb 
56076b4cac81SBjoern A. Zeeb 	return (hw);
56086b4cac81SBjoern A. Zeeb }
56096b4cac81SBjoern A. Zeeb 
56106b4cac81SBjoern A. Zeeb void
56116b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
56126b4cac81SBjoern A. Zeeb {
56136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5614759a996dSBjoern A. Zeeb 	struct mbuf *m;
56156b4cac81SBjoern A. Zeeb 
56166b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56176b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
56186b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
56196b4cac81SBjoern A. Zeeb 
5620759a996dSBjoern A. Zeeb 	/*
5621759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5622759a996dSBjoern A. Zeeb 	 */
5623759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5624759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5625759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5626759a996dSBjoern A. Zeeb 
5627759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5628759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5629759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5630759a996dSBjoern A. Zeeb 
5631759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5632759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5633759a996dSBjoern A. Zeeb 	while (m != NULL) {
5634759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5635759a996dSBjoern A. Zeeb 
5636759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5637759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5638759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5639759a996dSBjoern A. Zeeb 
5640759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5641759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5642759a996dSBjoern A. Zeeb 		}
5643759a996dSBjoern A. Zeeb 		m_freem(m);
5644759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5645759a996dSBjoern A. Zeeb 	}
5646759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5647759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5648759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5649759a996dSBjoern A. Zeeb 
5650a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf. */
5651a8a47a41SBjoern A. Zeeb 	if (!list_empty_careful(&lhw->lchanctx_list)) {
5652a8a47a41SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx, *next;
5653a8a47a41SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
5654a8a47a41SBjoern A. Zeeb 
5655a8a47a41SBjoern A. Zeeb 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
5656a8a47a41SBjoern A. Zeeb 			if (lchanctx->added_to_drv) {
5657a8a47a41SBjoern A. Zeeb 				/* In reality we should panic? */
5658a8a47a41SBjoern A. Zeeb 				chanctx_conf = &lchanctx->chanctx_conf;
5659a8a47a41SBjoern A. Zeeb 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
5660a8a47a41SBjoern A. Zeeb 			}
5661a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
5662a8a47a41SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
5663a8a47a41SBjoern A. Zeeb 		}
5664a8a47a41SBjoern A. Zeeb 	}
5665a8a47a41SBjoern A. Zeeb 
56666b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5667eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
56688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5669eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
56706b4cac81SBjoern A. Zeeb 	IMPROVE();
56716b4cac81SBjoern A. Zeeb }
56726b4cac81SBjoern A. Zeeb 
56736b4cac81SBjoern A. Zeeb void
56746b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
56756b4cac81SBjoern A. Zeeb {
56766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56776b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
56786b4cac81SBjoern A. Zeeb 
56796b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56806b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
56816b4cac81SBjoern A. Zeeb 
56826b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
56836b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
56846b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
56856b4cac81SBjoern A. Zeeb 
56866b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
56876b4cac81SBjoern A. Zeeb }
56886b4cac81SBjoern A. Zeeb 
56896b4cac81SBjoern A. Zeeb struct ieee80211_hw *
56906b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
56916b4cac81SBjoern A. Zeeb {
56926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56936b4cac81SBjoern A. Zeeb 
56946b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
56956b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
56966b4cac81SBjoern A. Zeeb }
56976b4cac81SBjoern A. Zeeb 
56986b4cac81SBjoern A. Zeeb static void
56996b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
57006b4cac81SBjoern A. Zeeb {
57016b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57026b4cac81SBjoern A. Zeeb 
57036b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57046b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
57056b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
57066b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
57076b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
57086b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
57096b4cac81SBjoern A. Zeeb }
57106b4cac81SBjoern A. Zeeb 
57112e183d99SBjoern A. Zeeb int
57126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
57136b4cac81SBjoern A. Zeeb {
57146b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5716c5b96b3eSBjoern A. Zeeb 	int band, i;
57176b4cac81SBjoern A. Zeeb 
57186b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57196b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57206b4cac81SBjoern A. Zeeb 
5721652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5722652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5723652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5724652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5725652e22d3SBjoern A. Zeeb 
57266b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
57276b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
57286b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
57296b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
57306b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
57316b4cac81SBjoern A. Zeeb 		/* We take the first one. */
57326b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
57336b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
57346b4cac81SBjoern A. Zeeb 	} else {
57356b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
57366b4cac81SBjoern A. Zeeb 	}
57376b4cac81SBjoern A. Zeeb 
57383d09d310SBjoern A. Zeeb #ifdef __not_yet__
57393d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
57406b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
57413d09d310SBjoern A. Zeeb #endif
57426b4cac81SBjoern A. Zeeb 
57436b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
57446b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
57456b4cac81SBjoern A. Zeeb 
57466b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
57476b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
57486b4cac81SBjoern A. Zeeb 	ic->ic_caps =
57496b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
57506b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
57516b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
57524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
57536b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
57544a67f1dfSBjoern A. Zeeb #endif
57556b4cac81SBjoern A. Zeeb #if 0
57566b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
57576b4cac81SBjoern A. Zeeb #endif
57586b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
57596b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
57606b4cac81SBjoern A. Zeeb 	    ;
57616b4cac81SBjoern A. Zeeb #if 0
57626b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
57636b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
57646b4cac81SBjoern A. Zeeb #endif
5765cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5766cc4e78d5SBjoern A. Zeeb 		/*
5767cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5768cc4e78d5SBjoern A. Zeeb 		 *
5769cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5770cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5771cc4e78d5SBjoern A. Zeeb 		 * the flag.
5772cc4e78d5SBjoern A. Zeeb 		 */
57736b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5774a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
57756b4cac81SBjoern A. Zeeb 	}
57766b4cac81SBjoern A. Zeeb 
577763578bf2SBjoern A. Zeeb 	/* Does HW support Fragmentation offload? */
577863578bf2SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
577963578bf2SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
578063578bf2SBjoern A. Zeeb 
5781527687a9SBjoern A. Zeeb 	/*
5782527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5783527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5784527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5785527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5786527687a9SBjoern A. Zeeb 	 */
5787527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5788527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5789527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5790527687a9SBjoern A. Zeeb 
5791527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5792527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5793527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5794527687a9SBjoern A. Zeeb 		}
5795527687a9SBjoern A. Zeeb 	}
5796527687a9SBjoern A. Zeeb 
57976b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5798b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
579911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
58006b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
58016b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
58026b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
58036b4cac81SBjoern A. Zeeb 	}
58046b4cac81SBjoern A. Zeeb #endif
58056b4cac81SBjoern A. Zeeb 
58066b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
58076b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
58086b4cac81SBjoern A. Zeeb 
58096b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
58106b4cac81SBjoern A. Zeeb 
58116b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
58126b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
58136b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
58146b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
58156b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
58166b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
58176b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
58186b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
58196b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
58206b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
58216b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
58226b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
58236b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
58246b4cac81SBjoern A. Zeeb 
5825a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5826a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5827a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5828a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5829a486fbbdSBjoern A. Zeeb 
58306b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
58316b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
58326b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
58336b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
58346b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
58356b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
58366b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
58376b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
58386b4cac81SBjoern A. Zeeb 
58399fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
584086bc7259SBjoern A. Zeeb 	/*
584186bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
584286bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
584386bc7259SBjoern A. Zeeb 	 */
584486bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
58459fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
58469fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
58479fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
58489fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
58499fb91463SBjoern A. Zeeb 
58509fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
58519fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
58529fb91463SBjoern A. Zeeb 
58539fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
58549fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
58559fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
58569fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
58579fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
58589fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
58599fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
58609fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
58619fb91463SBjoern A. Zeeb 
58629fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
58639fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
58649fb91463SBjoern A. Zeeb 
58659fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
58669fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
58679fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
58689fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
586986bc7259SBjoern A. Zeeb 	}
58709fb91463SBjoern A. Zeeb #endif
58719fb91463SBjoern A. Zeeb 
58726b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
58736b4cac81SBjoern A. Zeeb 
5874c5b96b3eSBjoern A. Zeeb 	/*
5875c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5876c5b96b3eSBjoern A. Zeeb 	 * expect one.
5877d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5878d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5879c5b96b3eSBjoern A. Zeeb 	 */
5880d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5881f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5882c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5883c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5884c5b96b3eSBjoern A. Zeeb 
5885c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5886c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5887c5b96b3eSBjoern A. Zeeb 			continue;
5888c5b96b3eSBjoern A. Zeeb 
5889d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5890d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5891d9945d78SBjoern A. Zeeb 
5892f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5893f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5894f454a4a1SBjoern A. Zeeb 			continue;
5895f454a4a1SBjoern A. Zeeb 
5896c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5897c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5898c5b96b3eSBjoern A. Zeeb 
5899c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5900c5b96b3eSBjoern A. Zeeb 				continue;
5901c5b96b3eSBjoern A. Zeeb 
59024a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
59039fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
590411450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
59059fb91463SBjoern A. Zeeb #endif
5906c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5907c5b96b3eSBjoern A. Zeeb 			break;
5908c5b96b3eSBjoern A. Zeeb 		}
5909c5b96b3eSBjoern A. Zeeb 	}
5910c5b96b3eSBjoern A. Zeeb 
5911d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5912d9945d78SBjoern A. Zeeb 
5913d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5914d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5915d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5916d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5917d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5918d9945d78SBjoern A. Zeeb 	}
5919d9945d78SBjoern A. Zeeb 
5920d9945d78SBjoern A. Zeeb 	/*
5921d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5922d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5923d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5924d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5925d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5926d9945d78SBjoern A. Zeeb 	 */
5927d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5928d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5929d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
593078ca45dfSBjoern A. Zeeb 
593178ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5932d9945d78SBjoern A. Zeeb 		/*
593378ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
593478ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
593578ca45dfSBjoern A. Zeeb 		 * space in.
5936d9945d78SBjoern A. Zeeb 		 */
5937d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
593878ca45dfSBjoern A. Zeeb 	}
5939d9945d78SBjoern A. Zeeb 
59409fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
59419fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
59429fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
59439fb91463SBjoern A. Zeeb #endif
59449fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
59451f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
59469fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
59479fb91463SBjoern A. Zeeb #endif
59489fb91463SBjoern A. Zeeb 
5949d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
595078ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
595178ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
595278ca45dfSBjoern A. Zeeb 			goto err;
5953d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
595478ca45dfSBjoern A. Zeeb 	}
5955d9945d78SBjoern A. Zeeb 
59566b4cac81SBjoern A. Zeeb 	if (bootverbose)
59576b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
59582e183d99SBjoern A. Zeeb 
59592e183d99SBjoern A. Zeeb 	return (0);
596078ca45dfSBjoern A. Zeeb err:
596178ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
596278ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
59636b4cac81SBjoern A. Zeeb }
59646b4cac81SBjoern A. Zeeb 
59656b4cac81SBjoern A. Zeeb void
59666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
59676b4cac81SBjoern A. Zeeb {
59686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59696b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59706b4cac81SBjoern A. Zeeb 
59716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59726b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59736b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
59746b4cac81SBjoern A. Zeeb }
59756b4cac81SBjoern A. Zeeb 
59766b4cac81SBjoern A. Zeeb void
59776b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
59786b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
59796b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
59806b4cac81SBjoern A. Zeeb     void *arg)
59816b4cac81SBjoern A. Zeeb {
59826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59836b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59846b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
598561a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
59866b4cac81SBjoern A. Zeeb 
59876b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59886b4cac81SBjoern A. Zeeb 
59896b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
59906b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
599161a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5992adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
59936b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
59946b4cac81SBjoern A. Zeeb 		    __func__, flags);
59956b4cac81SBjoern A. Zeeb 	}
59966b4cac81SBjoern A. Zeeb 
5997adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
59986b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
599961a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
60006b4cac81SBjoern A. Zeeb 
60016b4cac81SBjoern A. Zeeb 	if (atomic)
60028891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
60036b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
60046b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
60056b4cac81SBjoern A. Zeeb 
60066b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
60076b4cac81SBjoern A. Zeeb 
60086b4cac81SBjoern A. Zeeb 		/*
60096b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
60106b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
60116b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
60126b4cac81SBjoern A. Zeeb 		 * driver does not know about.
60136b4cac81SBjoern A. Zeeb 		 */
60146b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
60156b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
60166b4cac81SBjoern A. Zeeb 			continue;
60176b4cac81SBjoern A. Zeeb 
60186b4cac81SBjoern A. Zeeb 		/*
601961a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
602061a68e50SBjoern A. Zeeb 		 * if we haven't yet.
602161a68e50SBjoern A. Zeeb 		 */
602261a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
602361a68e50SBjoern A. Zeeb 			continue;
602461a68e50SBjoern A. Zeeb 
602561a68e50SBjoern A. Zeeb 		/*
60266b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
60276b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
60286b4cac81SBjoern A. Zeeb 		 */
60296b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
60306b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
60316b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
60326b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
60336b4cac81SBjoern A. Zeeb 	}
60346b4cac81SBjoern A. Zeeb 	if (atomic)
60358891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
60366b4cac81SBjoern A. Zeeb }
60376b4cac81SBjoern A. Zeeb 
603811db70b6SBjoern A. Zeeb static void
603911db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
604011db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
604111db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
604211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
604311db70b6SBjoern A. Zeeb     void *arg)
604411db70b6SBjoern A. Zeeb {
604511db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
604611db70b6SBjoern A. Zeeb 		return;
604711db70b6SBjoern A. Zeeb 
604811db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
604911db70b6SBjoern A. Zeeb 		return;
605011db70b6SBjoern A. Zeeb 
605111db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
605211db70b6SBjoern A. Zeeb }
605311db70b6SBjoern A. Zeeb 
60546b4cac81SBjoern A. Zeeb void
60556b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
60566b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
60576b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
60586b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
605911db70b6SBjoern A. Zeeb     void *arg, bool rcu)
60606b4cac81SBjoern A. Zeeb {
606111db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
606211db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
60636b4cac81SBjoern A. Zeeb 
606411db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
606511db70b6SBjoern A. Zeeb 
606611db70b6SBjoern A. Zeeb 	if (rcu) {
6067a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6068a8f735a6SBjoern A. Zeeb 
606911db70b6SBjoern A. Zeeb 		if (vif == NULL) {
607011db70b6SBjoern A. Zeeb 			TODO();
607111db70b6SBjoern A. Zeeb 		} else {
6072a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
607311db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
607411db70b6SBjoern A. Zeeb 				    keyix++)
607511db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
607611db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
607711db70b6SBjoern A. Zeeb 			}
607811db70b6SBjoern A. Zeeb 		}
607911db70b6SBjoern A. Zeeb 	} else {
608011db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
608111db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
608211db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
608311db70b6SBjoern A. Zeeb 
608411db70b6SBjoern A. Zeeb 		if (vif == NULL) {
608511db70b6SBjoern A. Zeeb 			TODO();
608611db70b6SBjoern A. Zeeb 		} else {
6087a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
608811db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
608911db70b6SBjoern A. Zeeb 				    keyix++)
609011db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
609111db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
609211db70b6SBjoern A. Zeeb 			}
609311db70b6SBjoern A. Zeeb 		}
609411db70b6SBjoern A. Zeeb 	}
60956b4cac81SBjoern A. Zeeb }
60966b4cac81SBjoern A. Zeeb 
60976b4cac81SBjoern A. Zeeb void
60986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
60996b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
61006b4cac81SBjoern A. Zeeb 	void *),
61016b4cac81SBjoern A. Zeeb     void *arg)
61026b4cac81SBjoern A. Zeeb {
6103c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6104c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
61056b4cac81SBjoern A. Zeeb 
6106c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
6107c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6108c5e25798SBjoern A. Zeeb 
6109c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6110c5e25798SBjoern A. Zeeb 
6111a8a47a41SBjoern A. Zeeb 	rcu_read_lock();
6112a8a47a41SBjoern A. Zeeb 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6113c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
6114c5e25798SBjoern A. Zeeb 			continue;
6115d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6116c5e25798SBjoern A. Zeeb 	}
6117a8a47a41SBjoern A. Zeeb 	rcu_read_unlock();
61186b4cac81SBjoern A. Zeeb }
61196b4cac81SBjoern A. Zeeb 
61206b4cac81SBjoern A. Zeeb void
61216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
61226b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
61236b4cac81SBjoern A. Zeeb {
61246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61256b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
61266b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
61276b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
61286b4cac81SBjoern A. Zeeb 
61296b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
61306b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
61316b4cac81SBjoern A. Zeeb 
61326b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61336b4cac81SBjoern A. Zeeb 
61348891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
61356b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
61366b4cac81SBjoern A. Zeeb 
6137a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6138a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
61396b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
61406b4cac81SBjoern A. Zeeb 				continue;
61416b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
61426b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
61436b4cac81SBjoern A. Zeeb 		}
6144a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
61456b4cac81SBjoern A. Zeeb 	}
61468891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
61476b4cac81SBjoern A. Zeeb }
61486b4cac81SBjoern A. Zeeb 
6149673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6150673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6151673d62fcSBjoern A. Zeeb {
6152673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6153673d62fcSBjoern A. Zeeb 
6154673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6155673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6156673d62fcSBjoern A. Zeeb 	return (regd);
6157673d62fcSBjoern A. Zeeb }
6158673d62fcSBjoern A. Zeeb 
61596b4cac81SBjoern A. Zeeb int
61606b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
61616b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
61626b4cac81SBjoern A. Zeeb {
61636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61646b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61656b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
61666b4cac81SBjoern A. Zeeb 
61676b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
61686b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
61696b4cac81SBjoern A. Zeeb 
61706b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
61716b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
61726b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
61736b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
61746b4cac81SBjoern A. Zeeb 	}
61756b4cac81SBjoern A. Zeeb 
61766b4cac81SBjoern A. Zeeb 	TODO();
61776b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
61786b4cac81SBjoern A. Zeeb 
61796b4cac81SBjoern A. Zeeb 	return (0);
61806b4cac81SBjoern A. Zeeb }
61816b4cac81SBjoern A. Zeeb 
61826b4cac81SBjoern A. Zeeb void
61836b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
61846b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
61856b4cac81SBjoern A. Zeeb {
61866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61886b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
61896b4cac81SBjoern A. Zeeb 
61906b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
61916b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
61926b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
61936b4cac81SBjoern A. Zeeb 
61946b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
61956b4cac81SBjoern A. Zeeb 
61968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
61976b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
61986b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6199a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
62006b4cac81SBjoern A. Zeeb 	wakeup(lhw);
62018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
62026b4cac81SBjoern A. Zeeb 
62036b4cac81SBjoern A. Zeeb 	return;
62046b4cac81SBjoern A. Zeeb }
62056b4cac81SBjoern A. Zeeb 
6206759a996dSBjoern A. Zeeb static void
6207759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6208759a996dSBjoern A. Zeeb {
6209759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6210759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6211759a996dSBjoern A. Zeeb 	int ok;
6212759a996dSBjoern A. Zeeb 
6213759a996dSBjoern A. Zeeb 	ni = NULL;
6214759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6215759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6216759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6217759a996dSBjoern A. Zeeb 
6218759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6219759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6220759a996dSBjoern A. Zeeb 	}
6221759a996dSBjoern A. Zeeb 
6222759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6223759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6224759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6225759a996dSBjoern A. Zeeb 		if (ok < 0)
6226759a996dSBjoern A. Zeeb 			m_freem(m);
6227759a996dSBjoern A. Zeeb 	} else {
6228759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6229759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6230759a996dSBjoern A. Zeeb 	}
6231759a996dSBjoern A. Zeeb 
6232759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6233759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6234bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6235759a996dSBjoern A. Zeeb #endif
6236759a996dSBjoern A. Zeeb }
6237759a996dSBjoern A. Zeeb 
6238759a996dSBjoern A. Zeeb static void
6239759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6240759a996dSBjoern A. Zeeb {
6241759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6242759a996dSBjoern A. Zeeb 	struct mbufq mq;
6243759a996dSBjoern A. Zeeb 	struct mbuf *m;
6244759a996dSBjoern A. Zeeb 
6245759a996dSBjoern A. Zeeb 	lhw = ctx;
6246759a996dSBjoern A. Zeeb 
6247759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6248759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6249bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6250bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6251759a996dSBjoern A. Zeeb #endif
6252759a996dSBjoern A. Zeeb 
6253759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6254759a996dSBjoern A. Zeeb 
6255759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6256759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6257759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6258759a996dSBjoern A. Zeeb 
6259759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6260759a996dSBjoern A. Zeeb 	while (m != NULL) {
6261759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6262759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6263759a996dSBjoern A. Zeeb 	}
6264759a996dSBjoern A. Zeeb }
6265759a996dSBjoern A. Zeeb 
626649010ba7SBjoern A. Zeeb static void
626749010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
626849010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
626949010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
627049010ba7SBjoern A. Zeeb     uint8_t *rssip)
627149010ba7SBjoern A. Zeeb {
627249010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
627349010ba7SBjoern A. Zeeb 	int i;
627449010ba7SBjoern A. Zeeb 	uint8_t rssi;
627549010ba7SBjoern A. Zeeb 
627649010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
627749010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
627849010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
627949010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
628049010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
628149010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
628249010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
628349010ba7SBjoern A. Zeeb 	else
628449010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
628549010ba7SBjoern A. Zeeb 	/*
628649010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
628749010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
628849010ba7SBjoern A. Zeeb 	 */
628949010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
629049010ba7SBjoern A. Zeeb 	if (rssip != NULL)
629149010ba7SBjoern A. Zeeb 		*rssip = rssi;
629249010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
629349010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
629449010ba7SBjoern A. Zeeb 	rx_stats->c_band =
629549010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
629649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
629749010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
629849010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
629949010ba7SBjoern A. Zeeb 
630049010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
630149010ba7SBjoern A. Zeeb 
630249010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
630349010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
630449010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
630549010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
630649010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
630749010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
630849010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
630949010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
631049010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
631149010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
631249010ba7SBjoern A. Zeeb 	}
631349010ba7SBjoern A. Zeeb 
631449010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
631549010ba7SBjoern A. Zeeb 		int cc;
631649010ba7SBjoern A. Zeeb 		int8_t crssi;
631749010ba7SBjoern A. Zeeb 
631849010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
631949010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
632049010ba7SBjoern A. Zeeb 
632149010ba7SBjoern A. Zeeb 		cc = 0;
632249010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
632349010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
632449010ba7SBjoern A. Zeeb 				continue;
632549010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
632649010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
632749010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
632849010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
632949010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
633049010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
633149010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
633249010ba7SBjoern A. Zeeb 			cc++;
633349010ba7SBjoern A. Zeeb 		}
633449010ba7SBjoern A. Zeeb 		if (cc > 0)
633549010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
633649010ba7SBjoern A. Zeeb 	}
633749010ba7SBjoern A. Zeeb 
633849010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
633949010ba7SBjoern A. Zeeb 
634049010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
634149010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
634249010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
634349010ba7SBjoern A. Zeeb 		if (supband != NULL)
634449010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
634549010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
634649010ba7SBjoern A. Zeeb 		break;
634749010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
634849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
634949010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
635049010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
635149010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
635249010ba7SBjoern A. Zeeb 		break;
635349010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
635449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
635549010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
635649010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
635749010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
635849010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
635949010ba7SBjoern A. Zeeb 		break;
636049010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
636149010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
636249010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
636349010ba7SBjoern A. Zeeb 		break;
636449010ba7SBjoern A. Zeeb 	}
636549010ba7SBjoern A. Zeeb 
636649010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
636749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
636849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
636949010ba7SBjoern A. Zeeb 		break;
637049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
637149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
637249010ba7SBjoern A. Zeeb 		break;
637349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
637449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
637549010ba7SBjoern A. Zeeb 		break;
637649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
637749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
637849010ba7SBjoern A. Zeeb 		break;
637949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
638049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
638149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
638249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
638349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
638449010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
638549010ba7SBjoern A. Zeeb 		break;
638649010ba7SBjoern A. Zeeb 	}
638749010ba7SBjoern A. Zeeb 
638849010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
638949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
639049010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
639149010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
639249010ba7SBjoern A. Zeeb 
639349010ba7SBjoern A. Zeeb 	/*
639449010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
639549010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
639649010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
639749010ba7SBjoern A. Zeeb 	 */
639849010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
639949010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
640049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
640149010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
640249010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
640349010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
640449010ba7SBjoern A. Zeeb 	}
640549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
640649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6407394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6408394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
6409394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6410394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
641149010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
641249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
641349010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
641449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
641549010ba7SBjoern A. Zeeb #endif
641649010ba7SBjoern A. Zeeb }
641749010ba7SBjoern A. Zeeb 
6418e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
64196b4cac81SBjoern A. Zeeb void
64206b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6421e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6422e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
64236b4cac81SBjoern A. Zeeb {
64246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64256b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
64266b4cac81SBjoern A. Zeeb 	struct mbuf *m;
64276b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
64286b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
64296b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
64306b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
64316b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
64326b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
64336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
6434*c816f64eSBjoern A. Zeeb 	int i, offset, ok, error;
643549010ba7SBjoern A. Zeeb 	uint8_t rssi;
6436c0cadd99SBjoern A. Zeeb 	bool is_beacon;
64376b4cac81SBjoern A. Zeeb 
64386b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
64396b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
64406b4cac81SBjoern A. Zeeb 		IMPROVE();
64416b4cac81SBjoern A. Zeeb 		goto err;
64426b4cac81SBjoern A. Zeeb 	}
64436b4cac81SBjoern A. Zeeb 
64446b4cac81SBjoern A. Zeeb 	/*
64456b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
64466b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
64476b4cac81SBjoern A. Zeeb 	 */
64486b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
64496b4cac81SBjoern A. Zeeb 	if (m == NULL)
64506b4cac81SBjoern A. Zeeb 		goto err;
64516b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
64526b4cac81SBjoern A. Zeeb 
64536b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
64546b4cac81SBjoern A. Zeeb 	offset = m->m_len;
64556b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
64566b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
64576b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
64586b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
64596b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
64606b4cac81SBjoern A. Zeeb 	}
64616b4cac81SBjoern A. Zeeb 
64626b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
64636b4cac81SBjoern A. Zeeb 
64646b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6465c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6466c0cadd99SBjoern A. Zeeb 
6467c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
64689d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
64696b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
64706b4cac81SBjoern A. Zeeb 
64719d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
64726b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6473c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
64746b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
64756b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
64766b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6477c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
64786b4cac81SBjoern A. Zeeb 
64799d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
64806b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
64816b4cac81SBjoern A. Zeeb 
64826b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
64839d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6484f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
648560970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
64866b4cac81SBjoern A. Zeeb 			__func__,
6487c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6488c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
64896b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6490f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
64916b4cac81SBjoern A. Zeeb 			rx_status->freq,
64926b4cac81SBjoern A. Zeeb 			rx_status->bw,
64936b4cac81SBjoern A. Zeeb 			rx_status->encoding,
64946b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
64956b4cac81SBjoern A. Zeeb 			rx_status->band,
64966b4cac81SBjoern A. Zeeb 			rx_status->chains,
64976b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
64986b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
64996b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
650060970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
65016b4cac81SBjoern A. Zeeb 			rx_status->signal,
65026b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
65036b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
65046b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
65056b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
65066b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
65076b4cac81SBjoern A. Zeeb 			rx_status->nss,
65086b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
65096b4cac81SBjoern A. Zeeb no_trace_beacons:
65106b4cac81SBjoern A. Zeeb #endif
65116b4cac81SBjoern A. Zeeb 
651249010ba7SBjoern A. Zeeb 	rssi = 0;
651349010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
65146b4cac81SBjoern A. Zeeb 
65156b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
65166b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
65176b4cac81SBjoern A. Zeeb 
65186b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
65196b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6520dbc06dd9SBjoern A. Zeeb 		m_freem(m);
65216b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
65226b4cac81SBjoern A. Zeeb 		goto err;
65236b4cac81SBjoern A. Zeeb 	}
65246b4cac81SBjoern A. Zeeb 
652558246901SBjoern A. Zeeb 	lsta = NULL;
65266b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
65276b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
65286b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
65296b4cac81SBjoern A. Zeeb 	} else {
6530c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6531c8dafefaSBjoern A. Zeeb 
65326b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
65336b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
65346b4cac81SBjoern A. Zeeb 		if (ni != NULL)
65356b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
65366b4cac81SBjoern A. Zeeb 	}
65376b4cac81SBjoern A. Zeeb 
65386b4cac81SBjoern A. Zeeb 	if (ni != NULL)
65396b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
65406b4cac81SBjoern A. Zeeb 	else
65416b4cac81SBjoern A. Zeeb 		/*
65426b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
65436b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
65446b4cac81SBjoern A. Zeeb 		 */
65456b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
65466b4cac81SBjoern A. Zeeb 
65479d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
65489d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6549bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6550c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6551c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
65529d9ba2b7SBjoern A. Zeeb #endif
65536b4cac81SBjoern A. Zeeb 
6554c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6555c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6556c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6557c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6558c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6559c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6560c8dafefaSBjoern A. Zeeb 
6561c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6562c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6563c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6564c8dafefaSBjoern A. Zeeb 
6565c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6566c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6567c8dafefaSBjoern A. Zeeb 
6568c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6569c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6570c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6571c8dafefaSBjoern A. Zeeb 		/*
6572c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6573c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6574c8dafefaSBjoern A. Zeeb 		 */
6575c8dafefaSBjoern A. Zeeb skip_device_ts:
65769fb91463SBjoern A. Zeeb 		;
65779fb91463SBjoern A. Zeeb 	}
6578c8dafefaSBjoern A. Zeeb 
65796b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
65806b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
65816b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
65826b4cac81SBjoern A. Zeeb 
65836b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
65846b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
65856b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
65866b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
65876b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
65886b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
65896b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
65906b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
65916b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
65926b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
65936b4cac81SBjoern A. Zeeb #endif
65946b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
65956b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
65966b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
65976b4cac81SBjoern A. Zeeb 		IMPROVE();
65986b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
65996b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
66006b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
66016b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6602170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
66036b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
66046b4cac81SBjoern A. Zeeb 	}
66056b4cac81SBjoern A. Zeeb 
66066b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
66076b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
66086b4cac81SBjoern A. Zeeb 
6609e30e05d3SBjoern A. Zeeb #if 0
6610e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6611e30e05d3SBjoern A. Zeeb 		/*
6612e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6613e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6614e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6615e30e05d3SBjoern A. Zeeb 		*/
6616e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6617e30e05d3SBjoern A. Zeeb 	}
6618e30e05d3SBjoern A. Zeeb #endif
6619e30e05d3SBjoern A. Zeeb 
6620759a996dSBjoern A. Zeeb 	/*
6621759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6622759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6623759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6624759a996dSBjoern A. Zeeb 	 */
66256b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6626759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6627759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6628759a996dSBjoern A. Zeeb 
6629759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6630759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6631759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6632759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6633759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6634759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6635759a996dSBjoern A. Zeeb 		}
66366b4cac81SBjoern A. Zeeb 	}
66376b4cac81SBjoern A. Zeeb 
6638759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6639759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6640759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6641759a996dSBjoern A. Zeeb 		m_freem(m);
6642759a996dSBjoern A. Zeeb 		goto err;
6643759a996dSBjoern A. Zeeb 	}
6644759a996dSBjoern A. Zeeb 
6645*c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lhw->rxq, m);
6646*c816f64eSBjoern A. Zeeb 	if (error != 0) {
6647*c816f64eSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6648*c816f64eSBjoern A. Zeeb 		m_freem(m);
6649*c816f64eSBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6650*c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6651*c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6652*c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
6653*c816f64eSBjoern A. Zeeb 			    __func__, error);
6654*c816f64eSBjoern A. Zeeb #endif
6655*c816f64eSBjoern A. Zeeb 		goto err;
6656*c816f64eSBjoern A. Zeeb 	}
6657759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6658759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
66596b4cac81SBjoern A. Zeeb 
66606b4cac81SBjoern A. Zeeb 	IMPROVE();
66616b4cac81SBjoern A. Zeeb 
66626b4cac81SBjoern A. Zeeb err:
66636b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
66646b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
66656b4cac81SBjoern A. Zeeb }
66666b4cac81SBjoern A. Zeeb 
66676b4cac81SBjoern A. Zeeb uint8_t
6668ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
66696b4cac81SBjoern A. Zeeb {
66706b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6671ec190d91SBjoern A. Zeeb 	uint8_t tid;
6672ec190d91SBjoern A. Zeeb 
6673ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6674ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6675ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6676ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
66776b4cac81SBjoern A. Zeeb 
66786b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6679ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6680ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6681ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6682ec190d91SBjoern A. Zeeb 
6683ec190d91SBjoern A. Zeeb 	return (tid);
66846b4cac81SBjoern A. Zeeb }
66856b4cac81SBjoern A. Zeeb 
6686ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6687ac1d519cSBjoern A. Zeeb 
6688ac1d519cSBjoern A. Zeeb static void
6689ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6690ac1d519cSBjoern A. Zeeb {
6691ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6692ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6693ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6694ac1d519cSBjoern A. Zeeb 
6695ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6696ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6697ac1d519cSBjoern A. Zeeb 
6698ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6699ac1d519cSBjoern A. Zeeb 
6700ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6701ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6702ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6703ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6704ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6705ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6706ac1d519cSBjoern A. Zeeb 		return;
6707ac1d519cSBjoern A. Zeeb 	}
6708ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6709ac1d519cSBjoern A. Zeeb 
6710ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6711ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6712ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6713ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6714ac1d519cSBjoern A. Zeeb 
6715ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6716ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6717ac1d519cSBjoern A. Zeeb 
6718ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6719ac1d519cSBjoern A. Zeeb }
6720ac1d519cSBjoern A. Zeeb 
6721ac1d519cSBjoern A. Zeeb void
6722ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6723ac1d519cSBjoern A. Zeeb {
6724ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6725ac1d519cSBjoern A. Zeeb 
6726ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6727ac1d519cSBjoern A. Zeeb 
6728ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6729ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6730ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6731ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6732ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6733ac1d519cSBjoern A. Zeeb 
6734ac1d519cSBjoern A. Zeeb 	/*
6735ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6736ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6737ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6738ac1d519cSBjoern A. Zeeb 	 */
6739ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6740ac1d519cSBjoern A. Zeeb }
6741ac1d519cSBjoern A. Zeeb 
6742ac1d519cSBjoern A. Zeeb void
6743ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6744ac1d519cSBjoern A. Zeeb {
6745ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6746ac1d519cSBjoern A. Zeeb 
6747ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6748ac1d519cSBjoern A. Zeeb 
6749ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6750ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6751ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6752ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6753ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6754ac1d519cSBjoern A. Zeeb }
6755ac1d519cSBjoern A. Zeeb 
6756ac1d519cSBjoern A. Zeeb void
6757ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6758ac1d519cSBjoern A. Zeeb {
6759ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6760ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6761ac1d519cSBjoern A. Zeeb 
6762ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6763ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6764ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6765ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6766ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6767ac1d519cSBjoern A. Zeeb 		return;
6768ac1d519cSBjoern A. Zeeb 	}
6769ac1d519cSBjoern A. Zeeb 
6770ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6771ac1d519cSBjoern A. Zeeb 
6772ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6773ac1d519cSBjoern A. Zeeb 		    entry);
6774ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6775ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6776ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6777ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6778ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6779ac1d519cSBjoern A. Zeeb 			break;
6780ac1d519cSBjoern A. Zeeb 	}
6781ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6782ac1d519cSBjoern A. Zeeb }
6783ac1d519cSBjoern A. Zeeb 
6784ac1d519cSBjoern A. Zeeb void
6785ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6786ac1d519cSBjoern A. Zeeb {
6787ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6788ac1d519cSBjoern A. Zeeb 
6789ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6790ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6791ac1d519cSBjoern A. Zeeb }
6792ac1d519cSBjoern A. Zeeb 
6793ac1d519cSBjoern A. Zeeb void
6794ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6795ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6796ac1d519cSBjoern A. Zeeb {
6797ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6798ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6799ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6800ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6801ac1d519cSBjoern A. Zeeb 	} else {
6802ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6803ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6804ac1d519cSBjoern A. Zeeb 	}
6805ac1d519cSBjoern A. Zeeb }
6806ac1d519cSBjoern A. Zeeb 
6807ac1d519cSBjoern A. Zeeb void
6808ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6809ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6810ac1d519cSBjoern A. Zeeb {
6811ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6812ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6813ac1d519cSBjoern A. Zeeb }
6814ac1d519cSBjoern A. Zeeb 
6815ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6816ac1d519cSBjoern A. Zeeb 
68176b4cac81SBjoern A. Zeeb struct wiphy *
68186b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
68196b4cac81SBjoern A. Zeeb {
68206b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6821ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
68226b4cac81SBjoern A. Zeeb 
68236b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
68246b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
68256b4cac81SBjoern A. Zeeb 		return (NULL);
68266b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
68276b4cac81SBjoern A. Zeeb 
6828ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6829ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6830ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6831ac1d519cSBjoern A. Zeeb 
6832ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6833ac1d519cSBjoern A. Zeeb 
6834ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6835ac1d519cSBjoern A. Zeeb 	TODO();
6836ac1d519cSBjoern A. Zeeb 
6837ac1d519cSBjoern A. Zeeb 	return (wiphy);
68386b4cac81SBjoern A. Zeeb }
68396b4cac81SBjoern A. Zeeb 
68406b4cac81SBjoern A. Zeeb void
68416b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
68426b4cac81SBjoern A. Zeeb {
68436b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
68446b4cac81SBjoern A. Zeeb 
68456b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
68466b4cac81SBjoern A. Zeeb 		return;
68476b4cac81SBjoern A. Zeeb 
6848ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6849ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6850ac1d519cSBjoern A. Zeeb 
68516b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6852ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6853ac1d519cSBjoern A. Zeeb 
68546b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
68556b4cac81SBjoern A. Zeeb }
68566b4cac81SBjoern A. Zeeb 
6857a7c19b8aSBjoern A. Zeeb static uint32_t
6858a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6859a7c19b8aSBjoern A. Zeeb {
6860a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6861a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6862a7c19b8aSBjoern A. Zeeb }
6863a7c19b8aSBjoern A. Zeeb 
6864a7c19b8aSBjoern A. Zeeb static uint32_t
6865a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6866a7c19b8aSBjoern A. Zeeb {
6867a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6868a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6869a7c19b8aSBjoern A. Zeeb }
6870a7c19b8aSBjoern A. Zeeb 
6871a7c19b8aSBjoern A. Zeeb uint32_t
6872a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6873a7c19b8aSBjoern A. Zeeb {
6874a7c19b8aSBjoern A. Zeeb 
6875a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6876a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6877a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6878a7c19b8aSBjoern A. Zeeb 
6879a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6880a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6881a7c19b8aSBjoern A. Zeeb 
6882a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6883a7c19b8aSBjoern A. Zeeb 
6884a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6885a7c19b8aSBjoern A. Zeeb }
6886a7c19b8aSBjoern A. Zeeb 
68876b4cac81SBjoern A. Zeeb uint32_t
68886b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
68896b4cac81SBjoern A. Zeeb     enum nl80211_band band)
68906b4cac81SBjoern A. Zeeb {
68916b4cac81SBjoern A. Zeeb 
68926b4cac81SBjoern A. Zeeb 	switch (band) {
68936b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
68946b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
68956b4cac81SBjoern A. Zeeb 		break;
68966b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
68976b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
68986b4cac81SBjoern A. Zeeb 		break;
68996b4cac81SBjoern A. Zeeb 	default:
69006b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
69016b4cac81SBjoern A. Zeeb 		break;
69026b4cac81SBjoern A. Zeeb 	}
69036b4cac81SBjoern A. Zeeb 
69046b4cac81SBjoern A. Zeeb 	return (0);
69056b4cac81SBjoern A. Zeeb }
69066b4cac81SBjoern A. Zeeb 
69076b4cac81SBjoern A. Zeeb uint32_t
69086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
69096b4cac81SBjoern A. Zeeb {
69106b4cac81SBjoern A. Zeeb 
69116b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
69126b4cac81SBjoern A. Zeeb }
69136b4cac81SBjoern A. Zeeb 
6914ac867c20SBjoern A. Zeeb #if 0
69156b4cac81SBjoern A. Zeeb static struct lkpi_sta *
69166b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
69176b4cac81SBjoern A. Zeeb {
69186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
69196b4cac81SBjoern A. Zeeb 
6920a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6921a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
69226b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6923a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
69246b4cac81SBjoern A. Zeeb 			return (lsta);
69256b4cac81SBjoern A. Zeeb 		}
69266b4cac81SBjoern A. Zeeb 	}
6927a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
69286b4cac81SBjoern A. Zeeb 
69296b4cac81SBjoern A. Zeeb 	return (NULL);
69306b4cac81SBjoern A. Zeeb }
6931ac867c20SBjoern A. Zeeb #endif
69326b4cac81SBjoern A. Zeeb 
69336b4cac81SBjoern A. Zeeb struct ieee80211_sta *
69346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
69356b4cac81SBjoern A. Zeeb {
69366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6937a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
69386b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
69396b4cac81SBjoern A. Zeeb 
69406b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
69416b4cac81SBjoern A. Zeeb 
6942a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6943a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
69446b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
69456b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6946a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
69476b4cac81SBjoern A. Zeeb 			return (sta);
69486b4cac81SBjoern A. Zeeb 		}
69496b4cac81SBjoern A. Zeeb 	}
6950a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
69516b4cac81SBjoern A. Zeeb 	return (NULL);
69526b4cac81SBjoern A. Zeeb }
69536b4cac81SBjoern A. Zeeb 
69546b4cac81SBjoern A. Zeeb struct ieee80211_sta *
69552e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
69562e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
69576b4cac81SBjoern A. Zeeb {
69586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
69596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
69606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
69616b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
69626b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
69636b4cac81SBjoern A. Zeeb 
69646b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
69656b4cac81SBjoern A. Zeeb 	sta = NULL;
69666b4cac81SBjoern A. Zeeb 
69678891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
69686b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
69696b4cac81SBjoern A. Zeeb 
69706b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
69716b4cac81SBjoern A. Zeeb 
69726b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
69736b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
69746b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
69756b4cac81SBjoern A. Zeeb 			continue;
69766b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
69776b4cac81SBjoern A. Zeeb 		if (sta != NULL)
69786b4cac81SBjoern A. Zeeb 			break;
69796b4cac81SBjoern A. Zeeb 	}
69808891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
69816b4cac81SBjoern A. Zeeb 
69826b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
69836b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
69846b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
69856b4cac81SBjoern A. Zeeb 			return (NULL);
69866b4cac81SBjoern A. Zeeb 	}
69876b4cac81SBjoern A. Zeeb 
69886b4cac81SBjoern A. Zeeb 	return (sta);
69896b4cac81SBjoern A. Zeeb }
69906b4cac81SBjoern A. Zeeb 
69916b4cac81SBjoern A. Zeeb struct sk_buff *
69926b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
69936b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
69946b4cac81SBjoern A. Zeeb {
69956b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
69960cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
69976b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
69986b4cac81SBjoern A. Zeeb 
69990cbcfa19SBjoern A. Zeeb 	skb = NULL;
70006b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
70016b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
70026b4cac81SBjoern A. Zeeb 
70030cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
70040cbcfa19SBjoern A. Zeeb 		goto stopped;
70050cbcfa19SBjoern A. Zeeb 
70060cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
70070cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
70080cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
70090cbcfa19SBjoern A. Zeeb 		goto stopped;
70100cbcfa19SBjoern A. Zeeb 	}
70110cbcfa19SBjoern A. Zeeb 
7012eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
7013eac3646fSBjoern A. Zeeb 
7014eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
70156b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
7016eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
70176b4cac81SBjoern A. Zeeb 
70180cbcfa19SBjoern A. Zeeb stopped:
70196b4cac81SBjoern A. Zeeb 	return (skb);
70206b4cac81SBjoern A. Zeeb }
70216b4cac81SBjoern A. Zeeb 
70226b4cac81SBjoern A. Zeeb void
70236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
702486220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
70256b4cac81SBjoern A. Zeeb {
70266b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
70276b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
702886220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
70296b4cac81SBjoern A. Zeeb 
70306b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
70316b4cac81SBjoern A. Zeeb 
70326b4cac81SBjoern A. Zeeb 	fc = bc = 0;
7033eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
70346b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
70356b4cac81SBjoern A. Zeeb 		fc++;
70366b4cac81SBjoern A. Zeeb 		bc += skb->len;
70376b4cac81SBjoern A. Zeeb 	}
7038eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
70396b4cac81SBjoern A. Zeeb 	if (frame_cnt)
70406b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
70416b4cac81SBjoern A. Zeeb 	if (byte_cnt)
70426b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
70436b4cac81SBjoern A. Zeeb 
70446b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
70456b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
70466b4cac81SBjoern A. Zeeb 	IMPROVE();
70476b4cac81SBjoern A. Zeeb }
70486b4cac81SBjoern A. Zeeb 
70496b4cac81SBjoern A. Zeeb /*
70506b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
70516b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
70526b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
70536b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
70546b4cac81SBjoern A. Zeeb  */
7055a8397571SBjoern A. Zeeb static void
7056a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
70576b4cac81SBjoern A. Zeeb     int status)
70586b4cac81SBjoern A. Zeeb {
70596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
70606b4cac81SBjoern A. Zeeb 	struct mbuf *m;
70616b4cac81SBjoern A. Zeeb 
70626b4cac81SBjoern A. Zeeb 	m = skb->m;
70636b4cac81SBjoern A. Zeeb 	skb->m = NULL;
70646b4cac81SBjoern A. Zeeb 
70656b4cac81SBjoern A. Zeeb 	if (m != NULL) {
70666b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
70676b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
70686b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
70696b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
70706b4cac81SBjoern A. Zeeb 	}
7071a8397571SBjoern A. Zeeb }
70726b4cac81SBjoern A. Zeeb 
7073a8397571SBjoern A. Zeeb void
7074a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7075a8397571SBjoern A. Zeeb     int status)
7076a8397571SBjoern A. Zeeb {
7077a8397571SBjoern A. Zeeb 
7078a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
70796b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
70806b4cac81SBjoern A. Zeeb }
70816b4cac81SBjoern A. Zeeb 
7082383b3e8fSBjoern A. Zeeb void
7083a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7084a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
7085383b3e8fSBjoern A. Zeeb {
7086a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
7087383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
7088383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
7089383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
7090383b3e8fSBjoern A. Zeeb 	int status;
7091383b3e8fSBjoern A. Zeeb 
7092a8397571SBjoern A. Zeeb 	skb = txstat->skb;
7093383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
7094383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
7095383b3e8fSBjoern A. Zeeb 
7096383b3e8fSBjoern A. Zeeb 		m = skb->m;
7097383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
7098383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
7099383b3e8fSBjoern A. Zeeb 	} else {
7100383b3e8fSBjoern A. Zeeb 		ni = NULL;
7101383b3e8fSBjoern A. Zeeb 	}
7102383b3e8fSBjoern A. Zeeb 
7103a8397571SBjoern A. Zeeb 	info = txstat->info;
7104383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7105383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
7106383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7107383b3e8fSBjoern A. Zeeb 	} else {
7108383b3e8fSBjoern A. Zeeb 		status = 1;
7109383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7110383b3e8fSBjoern A. Zeeb 	}
7111383b3e8fSBjoern A. Zeeb 
7112383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
7113383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
7114383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7115383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
7116383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7117383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7118383b3e8fSBjoern A. Zeeb 		}
7119383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
7120a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7121383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
7122383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7123383b3e8fSBjoern A. Zeeb #endif
7124adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7125383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7126383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7127383b3e8fSBjoern A. Zeeb 		}
7128383b3e8fSBjoern A. Zeeb 
7129d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7130383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
713146de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7132383b3e8fSBjoern A. Zeeb 
7133383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7134383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7135d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
713646de4d9fSAdrian Chadd 			    txs.long_retries);
7137383b3e8fSBjoern A. Zeeb 		}
7138383b3e8fSBjoern A. Zeeb #endif
7139383b3e8fSBjoern A. Zeeb 	}
7140383b3e8fSBjoern A. Zeeb 
7141383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7142383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7143383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7144383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7145383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7146383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7147adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7148383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7149383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7150383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7151383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7152383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7153383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7154383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7155383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7156383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7157383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7158383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7159383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7160383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7161adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7162383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7163383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7164383b3e8fSBjoern A. Zeeb #endif
7165383b3e8fSBjoern A. Zeeb 
7166a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7167a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7168a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7169a8397571SBjoern A. Zeeb 	} else {
7170383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7171383b3e8fSBjoern A. Zeeb 	}
7172a8397571SBjoern A. Zeeb }
7173a8397571SBjoern A. Zeeb 
7174a8397571SBjoern A. Zeeb void
7175a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7176a8397571SBjoern A. Zeeb {
7177a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7178a8397571SBjoern A. Zeeb 
7179a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7180a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7181a8397571SBjoern A. Zeeb 	status.skb = skb;
7182a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7183a8397571SBjoern A. Zeeb 
7184a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7185a8397571SBjoern A. Zeeb }
7186383b3e8fSBjoern A. Zeeb 
71876b4cac81SBjoern A. Zeeb /*
71886b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
71896b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
71906b4cac81SBjoern A. Zeeb  * mbufs this should go away.
71916b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
71926b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
71936b4cac81SBjoern A. Zeeb  */
71946b4cac81SBjoern A. Zeeb static void
71956b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
71966b4cac81SBjoern A. Zeeb {
71976b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
71986b4cac81SBjoern A. Zeeb 	struct mbuf *m;
71996b4cac81SBjoern A. Zeeb 
72006b4cac81SBjoern A. Zeeb 	if (p == NULL)
72016b4cac81SBjoern A. Zeeb 		return;
72026b4cac81SBjoern A. Zeeb 
72036b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
72046b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
72056b4cac81SBjoern A. Zeeb 
72066b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
72076b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
72086b4cac81SBjoern A. Zeeb 	if (ni != NULL)
72096b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
72106b4cac81SBjoern A. Zeeb 	m_freem(m);
72116b4cac81SBjoern A. Zeeb }
72126b4cac81SBjoern A. Zeeb 
72136b4cac81SBjoern A. Zeeb void
72146b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
72156b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
72166b4cac81SBjoern A. Zeeb {
72176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72186b4cac81SBjoern A. Zeeb 
72196b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
72206b4cac81SBjoern A. Zeeb 	IMPROVE();
72216b4cac81SBjoern A. Zeeb 
72226b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72236b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
72246b4cac81SBjoern A. Zeeb }
72256b4cac81SBjoern A. Zeeb 
72266b4cac81SBjoern A. Zeeb void
72276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
72286b4cac81SBjoern A. Zeeb     struct work_struct *w)
72296b4cac81SBjoern A. Zeeb {
72306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72316b4cac81SBjoern A. Zeeb 
72326b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
72336b4cac81SBjoern A. Zeeb 	IMPROVE();
72346b4cac81SBjoern A. Zeeb 
72356b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72366b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
72376b4cac81SBjoern A. Zeeb }
72386b4cac81SBjoern A. Zeeb 
72396b4cac81SBjoern A. Zeeb struct sk_buff *
7240ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7241ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7242ade774b1SBjoern A. Zeeb {
7243ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7244ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7245ade774b1SBjoern A. Zeeb 	uint8_t *p;
7246ade774b1SBjoern A. Zeeb 	size_t len;
7247ade774b1SBjoern A. Zeeb 
7248ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7249ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7250ade774b1SBjoern A. Zeeb 
7251ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7252ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7253ade774b1SBjoern A. Zeeb 		return (NULL);
7254ade774b1SBjoern A. Zeeb 
7255ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7256ade774b1SBjoern A. Zeeb 
7257ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7258ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7259ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7260ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7261ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7262ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7263ade774b1SBjoern A. Zeeb 
7264ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7265ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7266ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7267ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7268ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7269ade774b1SBjoern A. Zeeb 
7270ade774b1SBjoern A. Zeeb 	return (skb);
7271ade774b1SBjoern A. Zeeb }
7272ade774b1SBjoern A. Zeeb 
7273ade774b1SBjoern A. Zeeb struct sk_buff *
72746b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
72756b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
72766b4cac81SBjoern A. Zeeb {
72776b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72786b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
72796b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
72806b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
72816b4cac81SBjoern A. Zeeb 	uint16_t v;
72826b4cac81SBjoern A. Zeeb 
72836b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
72846b4cac81SBjoern A. Zeeb 	if (skb == NULL)
72856b4cac81SBjoern A. Zeeb 		return (NULL);
72866b4cac81SBjoern A. Zeeb 
72876b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
72886b4cac81SBjoern A. Zeeb 
72896b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
72906b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
72916b4cac81SBjoern A. Zeeb 
72926b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
72936b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
72946b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7295616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
72966b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
72976b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
72986b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
72996b4cac81SBjoern A. Zeeb 
73006b4cac81SBjoern A. Zeeb 	return (skb);
73016b4cac81SBjoern A. Zeeb }
73026b4cac81SBjoern A. Zeeb 
73036b4cac81SBjoern A. Zeeb struct sk_buff *
73046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7305adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
73066b4cac81SBjoern A. Zeeb {
73076b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73086b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
73096b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
73106b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
73116b4cac81SBjoern A. Zeeb 
7312adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7313adff403fSBjoern A. Zeeb 
73146b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
73156b4cac81SBjoern A. Zeeb 	if (skb == NULL)
73166b4cac81SBjoern A. Zeeb 		return (NULL);
73176b4cac81SBjoern A. Zeeb 
73186b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
73196b4cac81SBjoern A. Zeeb 
73206b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
73216b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
73226b4cac81SBjoern A. Zeeb 
73236b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
73246b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
73256b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
73266b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
73276b4cac81SBjoern A. Zeeb 
73286b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
73296b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
73306b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
73316b4cac81SBjoern A. Zeeb 
73326b4cac81SBjoern A. Zeeb 	return (skb);
73336b4cac81SBjoern A. Zeeb }
73346b4cac81SBjoern A. Zeeb 
73356b4cac81SBjoern A. Zeeb struct wireless_dev *
73366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
73376b4cac81SBjoern A. Zeeb {
73386b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73396b4cac81SBjoern A. Zeeb 
73406b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
73416b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
73426b4cac81SBjoern A. Zeeb }
73436b4cac81SBjoern A. Zeeb 
73446b4cac81SBjoern A. Zeeb void
73456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
73466b4cac81SBjoern A. Zeeb {
73476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73486b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
73496b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
73506b4cac81SBjoern A. Zeeb 	int arg;
73516b4cac81SBjoern A. Zeeb 
73526b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
73536b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
73546b4cac81SBjoern A. Zeeb 
73556b4cac81SBjoern A. Zeeb 	/*
7356f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
73576b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
73586b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
73596b4cac81SBjoern A. Zeeb 	 */
7360f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7361bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
73626b4cac81SBjoern A. Zeeb 
7363018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7364018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
73656b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
73666b4cac81SBjoern A. Zeeb }
73676b4cac81SBjoern A. Zeeb 
7368bb81db90SBjoern A. Zeeb void
7369bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7370bb81db90SBjoern A. Zeeb {
7371bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7372bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7373bb81db90SBjoern A. Zeeb 
7374bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7375bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7376bb81db90SBjoern A. Zeeb 
7377bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7378bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
73793540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7380bb81db90SBjoern A. Zeeb }
7381bb81db90SBjoern A. Zeeb 
73825edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
73835edde07cSBjoern A. Zeeb 
73845a9a0d78SBjoern A. Zeeb void
73855a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
73865a9a0d78SBjoern A. Zeeb {
73875a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73885a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73895a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
73905a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
73915a9a0d78SBjoern A. Zeeb 
73925a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
73935a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
73945a9a0d78SBjoern A. Zeeb 
73955a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
73965a9a0d78SBjoern A. Zeeb 
73975a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
73985a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
73995a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
74005a9a0d78SBjoern A. Zeeb 	else
74015a9a0d78SBjoern A. Zeeb 		ac_count = 1;
74025a9a0d78SBjoern A. Zeeb 
74035a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
74045a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
74055a9a0d78SBjoern A. Zeeb 
74065a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
74075a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
74085a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
74095a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
74100d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
74115a9a0d78SBjoern A. Zeeb 				/*
74125a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
74135a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
74145a9a0d78SBjoern A. Zeeb 				 */
74150d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
74160d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
74175a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
74185a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
74195a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
74205a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
74210d2cb6a6SBjoern A. Zeeb #endif
74225a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
74235a9a0d78SBjoern A. Zeeb 			}
74245a9a0d78SBjoern A. Zeeb 		}
74255a9a0d78SBjoern A. Zeeb 	}
74265a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
74275a9a0d78SBjoern A. Zeeb }
74285a9a0d78SBjoern A. Zeeb 
74295a9a0d78SBjoern A. Zeeb void
74305a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
74315a9a0d78SBjoern A. Zeeb {
74325a9a0d78SBjoern A. Zeeb 	int i;
74335a9a0d78SBjoern A. Zeeb 
74345a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
74355a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
74365a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
74375a9a0d78SBjoern A. Zeeb }
74385a9a0d78SBjoern A. Zeeb 
74395a9a0d78SBjoern A. Zeeb 
74405a9a0d78SBjoern A. Zeeb static void
74415a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
74425a9a0d78SBjoern A. Zeeb {
74435a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74445a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
74455a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
74465a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
74475a9a0d78SBjoern A. Zeeb 
74485a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
74495a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
74505a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
74515a9a0d78SBjoern A. Zeeb 	else
74525a9a0d78SBjoern A. Zeeb 		ac_count = 1;
74535a9a0d78SBjoern A. Zeeb 
74545a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
74555a9a0d78SBjoern A. Zeeb 
74565a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
74575a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
74585a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
74595a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
74605a9a0d78SBjoern A. Zeeb 
74615a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
74625a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
74635a9a0d78SBjoern A. Zeeb 
74645a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
74655a9a0d78SBjoern A. Zeeb 
74665a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
74675a9a0d78SBjoern A. Zeeb 
74680d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
74695a9a0d78SBjoern A. Zeeb 				/*
74705a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
74715a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
74725a9a0d78SBjoern A. Zeeb 				 */
74730d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
74740d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
74755a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
74765a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
74775a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
74785a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
74790d2cb6a6SBjoern A. Zeeb #endif
74805a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
74815a9a0d78SBjoern A. Zeeb 
7482a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7483a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
74845a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
74855a9a0d78SBjoern A. Zeeb 
74865a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
74875a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
74885a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
74895a9a0d78SBjoern A. Zeeb 
74905a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
74915a9a0d78SBjoern A. Zeeb 							continue;
74925a9a0d78SBjoern A. Zeeb 
74935a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
74945a9a0d78SBjoern A. Zeeb 							continue;
74955a9a0d78SBjoern A. Zeeb 
74965a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
74975a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
74985a9a0d78SBjoern A. Zeeb 							continue;
74995a9a0d78SBjoern A. Zeeb 
75005a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
75015a9a0d78SBjoern A. Zeeb 
75025a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
75035a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
75045a9a0d78SBjoern A. Zeeb 					}
75055a9a0d78SBjoern A. Zeeb 				}
7506a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
75075a9a0d78SBjoern A. Zeeb 			}
75085a9a0d78SBjoern A. Zeeb 		}
75095a9a0d78SBjoern A. Zeeb 	}
75105a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
75115a9a0d78SBjoern A. Zeeb }
75125a9a0d78SBjoern A. Zeeb 
75135a9a0d78SBjoern A. Zeeb void
75145a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
75155a9a0d78SBjoern A. Zeeb {
75165a9a0d78SBjoern A. Zeeb 	int i;
75175a9a0d78SBjoern A. Zeeb 
75185a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
75195a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
75205a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
75215a9a0d78SBjoern A. Zeeb }
75225a9a0d78SBjoern A. Zeeb 
75235a9a0d78SBjoern A. Zeeb void
75245a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
75255a9a0d78SBjoern A. Zeeb {
75265a9a0d78SBjoern A. Zeeb 
75275a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
75285a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
75295a9a0d78SBjoern A. Zeeb 
75305a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
75315a9a0d78SBjoern A. Zeeb }
75325a9a0d78SBjoern A. Zeeb 
75335a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
75345a9a0d78SBjoern A. Zeeb void
75355a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
75365a9a0d78SBjoern A. Zeeb {
75375a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75385a9a0d78SBjoern A. Zeeb 
75395a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
75405a9a0d78SBjoern A. Zeeb 
75415a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
75425a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
75435a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
75445a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
75455a9a0d78SBjoern A. Zeeb }
75465a9a0d78SBjoern A. Zeeb 
75475a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
75485a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
75495a9a0d78SBjoern A. Zeeb {
75505a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75515a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
75525a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
75535a9a0d78SBjoern A. Zeeb 
75545a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
75555a9a0d78SBjoern A. Zeeb 	txq = NULL;
75565a9a0d78SBjoern A. Zeeb 
75575a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
75585a9a0d78SBjoern A. Zeeb 
75595a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
75605a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
75615a9a0d78SBjoern A. Zeeb 		goto out;
75625a9a0d78SBjoern A. Zeeb 
75635a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
75645a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
75655a9a0d78SBjoern A. Zeeb 		goto out;
75665a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
75675a9a0d78SBjoern A. Zeeb 		goto out;
75685a9a0d78SBjoern A. Zeeb 
75695a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
75705a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
75715a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
75725a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
75735a9a0d78SBjoern A. Zeeb 
75745a9a0d78SBjoern A. Zeeb out:
75755a9a0d78SBjoern A. Zeeb 	return (txq);
75765a9a0d78SBjoern A. Zeeb }
75775a9a0d78SBjoern A. Zeeb 
75785a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
75795a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
75805a9a0d78SBjoern A. Zeeb {
75815a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75825a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7583eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
75845a9a0d78SBjoern A. Zeeb 
75855a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
75865a9a0d78SBjoern A. Zeeb 
75875a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
75885a9a0d78SBjoern A. Zeeb 
75895a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7590eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7591eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7592eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7593eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
75945a9a0d78SBjoern A. Zeeb 		goto out;
75955a9a0d78SBjoern A. Zeeb 
759641b746e0SAustin Shafer 	/*
759741b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
759841b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
759941b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
760041b746e0SAustin Shafer 	 */
760141b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
76025a9a0d78SBjoern A. Zeeb 		goto out;
76035a9a0d78SBjoern A. Zeeb 
76045a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
76055a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
76065a9a0d78SBjoern A. Zeeb out:
76075a9a0d78SBjoern A. Zeeb 	return;
76085a9a0d78SBjoern A. Zeeb }
76095a9a0d78SBjoern A. Zeeb 
7610eac3646fSBjoern A. Zeeb void
7611eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7612eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7613eac3646fSBjoern A. Zeeb {
7614eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7615eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7616eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7617eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7618eac3646fSBjoern A. Zeeb 
7619eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7620eac3646fSBjoern A. Zeeb 
7621eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7622eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7623eac3646fSBjoern A. Zeeb 	do {
7624eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7625eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7626eac3646fSBjoern A. Zeeb 			break;
7627eac3646fSBjoern A. Zeeb 
7628eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7629eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7630eac3646fSBjoern A. Zeeb 		do {
7631eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7632eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7633eac3646fSBjoern A. Zeeb 				break;
7634eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7635eac3646fSBjoern A. Zeeb 		} while(1);
7636eac3646fSBjoern A. Zeeb 
7637eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7638eac3646fSBjoern A. Zeeb 	} while (1);
7639eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7640eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7641eac3646fSBjoern A. Zeeb }
7642eac3646fSBjoern A. Zeeb 
76435a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
76445a9a0d78SBjoern A. Zeeb 
76455edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
76465edde07cSBjoern A. Zeeb 	u_int refcnt;
76475edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
76485edde07cSBjoern A. Zeeb };
76495edde07cSBjoern A. Zeeb 
76505edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
76515edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
76525edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
76535edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
76545edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
76555edde07cSBjoern A. Zeeb 	size_t ssid_len;
76565edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
76575edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
76585edde07cSBjoern A. Zeeb 
76595edde07cSBjoern A. Zeeb 	/*
76605edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
76615edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
76625edde07cSBjoern A. Zeeb 	 */
76635edde07cSBjoern A. Zeeb 	bool match;
76645edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
76655edde07cSBjoern A. Zeeb };
76665edde07cSBjoern A. Zeeb 
76675edde07cSBjoern A. Zeeb static void
76685edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
76695edde07cSBjoern A. Zeeb {
76705edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
76715edde07cSBjoern A. Zeeb 	size_t ielen;
76725edde07cSBjoern A. Zeeb 
76735edde07cSBjoern A. Zeeb 	lookup = arg;
76745edde07cSBjoern A. Zeeb 
76755edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
76765edde07cSBjoern A. Zeeb 	if (lookup->match)
76775edde07cSBjoern A. Zeeb 		return;
76785edde07cSBjoern A. Zeeb 
76795edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
76805edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
76815edde07cSBjoern A. Zeeb 		return;
76825edde07cSBjoern A. Zeeb 
76835edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
76845edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
76855edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
76865edde07cSBjoern A. Zeeb 		return;
76875edde07cSBjoern A. Zeeb 	}
76885edde07cSBjoern A. Zeeb 
76895edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
76905edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
76915edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
76925edde07cSBjoern A. Zeeb 		return;
76935edde07cSBjoern A. Zeeb 	}
76945edde07cSBjoern A. Zeeb 
76955edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
76965edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
76975edde07cSBjoern A. Zeeb 
76985edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
76995edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
77005edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
77015edde07cSBjoern A. Zeeb 			return;
77025edde07cSBjoern A. Zeeb 	}
77035edde07cSBjoern A. Zeeb 
77045edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
77055edde07cSBjoern A. Zeeb 		return;
77065edde07cSBjoern A. Zeeb 
77075edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
77085edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
77095edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
77105edde07cSBjoern A. Zeeb 			return;
77115edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
77125edde07cSBjoern A. Zeeb 			return;
77135edde07cSBjoern A. Zeeb 	}
77145edde07cSBjoern A. Zeeb 
77155edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
77165edde07cSBjoern A. Zeeb 
77175edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
77185edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
77195edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
77205edde07cSBjoern A. Zeeb 		return;
77215edde07cSBjoern A. Zeeb 
77225edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
77235edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
77245edde07cSBjoern A. Zeeb 	if (ielen)
77255edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
77265edde07cSBjoern A. Zeeb 
77275edde07cSBjoern A. Zeeb 	lookup->match = true;
77285edde07cSBjoern A. Zeeb }
77295edde07cSBjoern A. Zeeb 
77305edde07cSBjoern A. Zeeb struct cfg80211_bss *
77315edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
77325edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
77335edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
77345edde07cSBjoern A. Zeeb {
77355edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
77365edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
77375edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
77385edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
77395edde07cSBjoern A. Zeeb 
77405edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
77415edde07cSBjoern A. Zeeb 
77425edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
77435edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
77445edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
77455edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
77465edde07cSBjoern A. Zeeb 		return (NULL);
77475edde07cSBjoern A. Zeeb 	}
77485edde07cSBjoern A. Zeeb 
77495edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
77505edde07cSBjoern A. Zeeb 	lookup.chan = chan;
77515edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
77525edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
77535edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
77545edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
77555edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
77565edde07cSBjoern A. Zeeb 	lookup.match = false;
77575edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
77585edde07cSBjoern A. Zeeb 
77595edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
77605edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
77615edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
77625edde07cSBjoern A. Zeeb 	if (!lookup.match) {
77635edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
77645edde07cSBjoern A. Zeeb 		return (NULL);
77655edde07cSBjoern A. Zeeb 	}
77665edde07cSBjoern A. Zeeb 
77675edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
77685edde07cSBjoern A. Zeeb 	return (&lbss->bss);
77695edde07cSBjoern A. Zeeb }
77705edde07cSBjoern A. Zeeb 
77715edde07cSBjoern A. Zeeb void
77725edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
77735edde07cSBjoern A. Zeeb {
77745edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
77755edde07cSBjoern A. Zeeb 
77765edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
77775edde07cSBjoern A. Zeeb 
77785edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
77795edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
77805edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
77815edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
77825edde07cSBjoern A. Zeeb 	}
77835edde07cSBjoern A. Zeeb }
77845edde07cSBjoern A. Zeeb 
77855edde07cSBjoern A. Zeeb void
77865edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
77875edde07cSBjoern A. Zeeb {
77885edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
77895edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
77905edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
77915edde07cSBjoern A. Zeeb 
77925edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
77935edde07cSBjoern A. Zeeb 	ic = lhw->ic;
77945edde07cSBjoern A. Zeeb 
77955edde07cSBjoern A. Zeeb 	/*
77965edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
77975edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
77985edde07cSBjoern A. Zeeb 	 */
77995edde07cSBjoern A. Zeeb 	if (ic == NULL ||
78005edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
78015edde07cSBjoern A. Zeeb 		return;
78025edde07cSBjoern A. Zeeb 
78035edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
78045edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
78055edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
78065edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
78075edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
78085edde07cSBjoern A. Zeeb }
78095edde07cSBjoern A. Zeeb 
78105edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
78115edde07cSBjoern A. Zeeb 
7812ac1d519cSBjoern A. Zeeb /*
7813ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7814ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7815ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7816ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7817ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7818ac1d519cSBjoern A. Zeeb  */
7819ac1d519cSBjoern A. Zeeb 
7820ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7821ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7822ac1d519cSBjoern A. Zeeb {
7823ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7824ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7825ac1d519cSBjoern A. Zeeb 	int error;
7826ac1d519cSBjoern A. Zeeb 
7827ac1d519cSBjoern A. Zeeb 	changed = 0;
7828ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7829ac1d519cSBjoern A. Zeeb 		cd = NULL;
7830ac1d519cSBjoern A. Zeeb 	else
7831ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7832ac1d519cSBjoern A. Zeeb 
7833ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7834ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7835ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7836ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7837ac1d519cSBjoern A. Zeeb 	}
7838ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7839ac1d519cSBjoern A. Zeeb 
7840ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7841ac1d519cSBjoern A. Zeeb 		return (0);
7842ac1d519cSBjoern A. Zeeb 
7843ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7844ac1d519cSBjoern A. Zeeb 	return (error);
7845ac1d519cSBjoern A. Zeeb }
7846ac1d519cSBjoern A. Zeeb 
7847ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7848ac1d519cSBjoern A. Zeeb 
78496b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
78506b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
78516b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7852