xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 02382a0ac822b88e17d86643e27964eba18479e0)
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");
110b71d3043SBjoern A. Zeeb 
111b71d3043SBjoern A. Zeeb static bool lkpi_hwcrypto_tkip = false;
112b71d3043SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, tkip, CTLFLAG_RDTUN,
113b71d3043SBjoern A. Zeeb     &lkpi_hwcrypto_tkip, 0, "Enable LinuxKPI 802.11 TKIP crypto offload");
11411db70b6SBjoern A. Zeeb #endif
11511db70b6SBjoern A. Zeeb 
11640839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11740839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11840839418SBjoern A. Zeeb 
11940839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1209d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1219d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1229d9ba2b7SBjoern A. Zeeb 
1239d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1246b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1259d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1266b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1276b4cac81SBjoern A. Zeeb #else
1286b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1296b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1306b4cac81SBjoern A. Zeeb #endif
1316b4cac81SBjoern A. Zeeb 
1326b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1336b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1346b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1356b4cac81SBjoern A. Zeeb #endif
1366b4cac81SBjoern A. Zeeb 
1376b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1386b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1396b4cac81SBjoern A. Zeeb 
140b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
141b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
142b0f73768SBjoern A. Zeeb 
143fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
144fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1494f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1514f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1524f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1534f61ef8bSBjoern A. Zeeb #if 0
1544f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1554f61ef8bSBjoern A. Zeeb #endif
1564f61ef8bSBjoern A. Zeeb };
1574f61ef8bSBjoern A. Zeeb 
1586b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1596b4cac81SBjoern A. Zeeb 	/*
1606b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1616b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1626b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1636b4cac81SBjoern A. Zeeb 	 */
1646b4cac81SBjoern A. Zeeb };
1656b4cac81SBjoern A. Zeeb 
166ac867c20SBjoern A. Zeeb #if 0
1676b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1686b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
169ac867c20SBjoern A. Zeeb #endif
17088665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1716b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
172759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1736b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1744a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1754a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1764a67f1dfSBjoern A. Zeeb #endif
1776b4cac81SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb static const char *
17940839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
18040839418SBjoern A. Zeeb {
18140839418SBjoern A. Zeeb 
18240839418SBjoern A. Zeeb 	switch (bw) {
18340839418SBjoern A. Zeeb 
18440839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18540839418SBjoern A. Zeeb 		return ("20");
18640839418SBjoern A. Zeeb 		break;
18740839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18840839418SBjoern A. Zeeb 		return ("5");
18940839418SBjoern A. Zeeb 		break;
19040839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
19140839418SBjoern A. Zeeb 		return ("10");
19240839418SBjoern A. Zeeb 		break;
19340839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19440839418SBjoern A. Zeeb 		return ("40");
19540839418SBjoern A. Zeeb 		break;
19640839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19740839418SBjoern A. Zeeb 		return ("80");
19840839418SBjoern A. Zeeb 		break;
19940839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
20040839418SBjoern A. Zeeb 		return ("160");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
20340839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20440839418SBjoern A. Zeeb 		return ("HE_RU");
20540839418SBjoern A. Zeeb 		break;
20640839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20740839418SBjoern A. Zeeb 		return ("320");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
21040839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
21140839418SBjoern A. Zeeb 		return ("EHT_RU");
21240839418SBjoern A. Zeeb 		break;
21340839418SBjoern A. Zeeb 	default:
21440839418SBjoern A. Zeeb 		return ("?");
21540839418SBjoern A. Zeeb 		break;
21640839418SBjoern A. Zeeb 	}
21740839418SBjoern A. Zeeb }
21840839418SBjoern A. Zeeb 
21940839418SBjoern A. Zeeb static void
22040839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
22140839418SBjoern A. Zeeb     const uint64_t flags)
22240839418SBjoern A. Zeeb {
22340839418SBjoern A. Zeeb 	int bit, i;
22440839418SBjoern A. Zeeb 
22540839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22640839418SBjoern A. Zeeb 
22740839418SBjoern A. Zeeb 	i = 0;
22840839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22940839418SBjoern A. Zeeb 
23040839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
23140839418SBjoern A. Zeeb 			continue;
23240839418SBjoern A. Zeeb 
23340839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23440839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23540839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23640839418SBjoern A. Zeeb 		i++;							\
23740839418SBjoern A. Zeeb 		break;
23840839418SBjoern A. Zeeb 
23940839418SBjoern A. Zeeb 		switch (bit) {
24040839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
26140839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
26240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
26340839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26440839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26540839418SBjoern A. Zeeb 		default:
26640839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26740839418SBjoern A. Zeeb 			break;
26840839418SBjoern A. Zeeb 		}
26940839418SBjoern A. Zeeb 	}
27040839418SBjoern A. Zeeb #undef	EXPAND_CASE
27140839418SBjoern A. Zeeb 	if (i > 0)
27240839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
27340839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27440839418SBjoern A. Zeeb }
27540839418SBjoern A. Zeeb 
27640839418SBjoern A. Zeeb static int
27740839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27840839418SBjoern A. Zeeb {
27940839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28040839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28140839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
28240839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28340839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28440839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28540839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28640839418SBjoern A. Zeeb 	struct station_info sinfo;
28740839418SBjoern A. Zeeb 	struct sbuf s;
28840839418SBjoern A. Zeeb 	int error;
28940839418SBjoern A. Zeeb 
29040839418SBjoern A. Zeeb 	if (req->newptr)
29140839418SBjoern A. Zeeb 		return (EPERM);
29240839418SBjoern A. Zeeb 
29340839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29440839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29540839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29640839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29740839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29840839418SBjoern A. Zeeb 
29940839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
302a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
30340839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30440839418SBjoern A. Zeeb 
30540839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30640839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30740839418SBjoern A. Zeeb 
30840839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30940839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
31040839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
31140839418SBjoern A. Zeeb 			continue;
31240839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 		if (error != 0) {
31740839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31840839418SBjoern A. Zeeb 			continue;
31940839418SBjoern A. Zeeb 		}
32040839418SBjoern A. Zeeb 
321a1adefb1SBjoern A. Zeeb 		/* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
322a1adefb1SBjoern A. Zeeb 		if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
323a1adefb1SBjoern A. Zeeb 		    (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
324a1adefb1SBjoern A. Zeeb 			memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
325a1adefb1SBjoern A. Zeeb 			sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
326a1adefb1SBjoern A. Zeeb 		}
327a1adefb1SBjoern A. Zeeb 
32840839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
32940839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
33040839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
33140839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
33240839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
33340839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
33440839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
33540839418SBjoern A. Zeeb 
33640839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
33740839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
33840839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
33940839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
34040839418SBjoern A. Zeeb 
34140839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
34240839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
34340839418SBjoern A. Zeeb 
34440839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
34540839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
34640839418SBjoern A. Zeeb 
34740839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
34840839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
34940839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
35040839418SBjoern A. Zeeb 		}
35140839418SBjoern A. Zeeb 
35240839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
35340839418SBjoern A. Zeeb 
35440839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35540839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35640839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
35740839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
35840839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
35940839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
36040839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
36140839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
36240839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
36340839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
36440839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
36540839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
36640839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
36740839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
36840839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
36940839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
37040839418SBjoern A. Zeeb 	}
37140839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
37240839418SBjoern A. Zeeb 
37340839418SBjoern A. Zeeb 	sbuf_finish(&s);
37440839418SBjoern A. Zeeb 	sbuf_delete(&s);
37540839418SBjoern A. Zeeb 
37640839418SBjoern A. Zeeb 	return (0);
37740839418SBjoern A. Zeeb }
37840839418SBjoern A. Zeeb 
37962d51a43SBjoern A. Zeeb static enum ieee80211_sta_rx_bw
38062d51a43SBjoern A. Zeeb lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
38162d51a43SBjoern A. Zeeb {
38262d51a43SBjoern A. Zeeb 	switch (cw) {
38362d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_320:
38462d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_320);
38562d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_160:
38662d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80P80:
38762d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_160);
38862d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80:
38962d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_80);
39062d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_40:
39162d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_40);
39262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20:
39362d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20_NOHT:
39462d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39562d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_5:
39662d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_10:
39762d51a43SBjoern A. Zeeb 		/* Unsupported input. */
39862d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39962d51a43SBjoern A. Zeeb 	}
40062d51a43SBjoern A. Zeeb }
40162d51a43SBjoern A. Zeeb 
40262d51a43SBjoern A. Zeeb static enum nl80211_chan_width
40362d51a43SBjoern A. Zeeb lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bw rx_bw)
40462d51a43SBjoern A. Zeeb {
40562d51a43SBjoern A. Zeeb 	switch (rx_bw) {
40662d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_20:
40762d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_20);	/* _NOHT */
40862d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_40:
40962d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_40);
41062d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_80:
41162d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_80);
41262d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_160:
41362d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_160); /* 80P80 */
41462d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_320:
41562d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_320);
41662d51a43SBjoern A. Zeeb 	}
41762d51a43SBjoern A. Zeeb }
41862d51a43SBjoern A. Zeeb 
41962d51a43SBjoern A. Zeeb static void
42062d51a43SBjoern A. Zeeb lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
42162d51a43SBjoern A. Zeeb     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
42262d51a43SBjoern A. Zeeb {
42362d51a43SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
42462d51a43SBjoern A. Zeeb 	enum ieee80211_sta_rx_bw old_bw;
42562d51a43SBjoern A. Zeeb 	uint32_t changed;
42662d51a43SBjoern A. Zeeb 
42762d51a43SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
42862d51a43SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
42962d51a43SBjoern A. Zeeb 	if (chanctx_conf == NULL)
43062d51a43SBjoern A. Zeeb 		return;
43162d51a43SBjoern A. Zeeb 
43262d51a43SBjoern A. Zeeb 	old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
43362d51a43SBjoern A. Zeeb 	if (old_bw == sta->deflink.bandwidth)
43462d51a43SBjoern A. Zeeb 		return;
43562d51a43SBjoern A. Zeeb 
43662d51a43SBjoern A. Zeeb 	chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
43762d51a43SBjoern A. Zeeb 	if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
43862d51a43SBjoern A. Zeeb 	    !sta->deflink.ht_cap.ht_supported)
43962d51a43SBjoern A. Zeeb 		chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
44062d51a43SBjoern A. Zeeb 
44162d51a43SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
44262d51a43SBjoern A. Zeeb 
44362d51a43SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
44462d51a43SBjoern A. Zeeb 
44562d51a43SBjoern A. Zeeb 	changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
44662d51a43SBjoern A. Zeeb 	changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
44762d51a43SBjoern A. Zeeb 	lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
44862d51a43SBjoern A. Zeeb }
44962d51a43SBjoern A. Zeeb 
4509fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
4519fb91463SBjoern A. Zeeb static void
45262d51a43SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
45362d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
4549fb91463SBjoern A. Zeeb {
4559fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
4569fb91463SBjoern A. Zeeb 	uint8_t *ie;
4579fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
4589fb91463SBjoern A. Zeeb 	int i, rx_nss;
4599fb91463SBjoern A. Zeeb 
460f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
461f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
4629fb91463SBjoern A. Zeeb 		return;
463f9c7a07dSBjoern A. Zeeb 	}
4649fb91463SBjoern A. Zeeb 
4659fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
4669fb91463SBjoern A. Zeeb 
4679fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
4689fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
4699fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4709fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
4719fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
4729fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
4739fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
4749fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
4759fb91463SBjoern A. Zeeb 
4769fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
4779fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
4789fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
4799fb91463SBjoern A. Zeeb 		ie += 4;
4809fb91463SBjoern A. Zeeb 	ie += 2;
4819fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4829fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4839fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4849fb91463SBjoern A. Zeeb 
48562d51a43SBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
48662d51a43SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
487f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
4883e022a91SBjoern A. Zeeb 	else
4893e022a91SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
490f9c7a07dSBjoern A. Zeeb 
491f9c7a07dSBjoern A. Zeeb 	/*
492f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
493f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
494f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
495f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
496f9c7a07dSBjoern A. Zeeb 	 */
4979fb91463SBjoern A. Zeeb 	rx_nss = 0;
498f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4999fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
5009fb91463SBjoern A. Zeeb 			rx_nss++;
5019fb91463SBjoern A. Zeeb 	}
502f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
503f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
5049fb91463SBjoern A. Zeeb 
505f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
506f9c7a07dSBjoern A. Zeeb 
507f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
508f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
509f9c7a07dSBjoern A. Zeeb 	else
510f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
511f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
512f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
513f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
514f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
515f9c7a07dSBjoern A. Zeeb 	}
516f9c7a07dSBjoern A. Zeeb #endif
5179fb91463SBjoern A. Zeeb }
5189fb91463SBjoern A. Zeeb #endif
5199fb91463SBjoern A. Zeeb 
5209fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5219fb91463SBjoern A. Zeeb static void
52262d51a43SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
52362d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
5249fb91463SBjoern A. Zeeb {
5259df0d1f3SBjoern A. Zeeb 	enum ieee80211_sta_rx_bw bw;
526f9c7a07dSBjoern A. Zeeb 	uint32_t width;
527f9c7a07dSBjoern A. Zeeb 	int rx_nss;
528f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
529f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
5309fb91463SBjoern A. Zeeb 
5315393cd34SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
5325393cd34SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
533f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
5349fb91463SBjoern A. Zeeb 		return;
535f9c7a07dSBjoern A. Zeeb 	}
5369fb91463SBjoern A. Zeeb 
537f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
538f9c7a07dSBjoern A. Zeeb 
539f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
540f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
541f9c7a07dSBjoern A. Zeeb 
5423e022a91SBjoern A. Zeeb 	/*
5433e022a91SBjoern A. Zeeb 	 * If VHT20/40 are selected do not update the bandwidth
5443e022a91SBjoern A. Zeeb 	 * from HT but stya on VHT.
5453e022a91SBjoern A. Zeeb 	 */
5463e022a91SBjoern A. Zeeb 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
5473e022a91SBjoern A. Zeeb 		goto skip_bw;
5483e022a91SBjoern A. Zeeb 
5499df0d1f3SBjoern A. Zeeb 	bw = sta->deflink.bandwidth;
550f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
551f9c7a07dSBjoern A. Zeeb 	switch (width) {
5529df0d1f3SBjoern A. Zeeb 	/* Deprecated. */
553f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
554f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
5559df0d1f3SBjoern A. Zeeb 		bw = IEEE80211_STA_RX_BW_160;
556f9c7a07dSBjoern A. Zeeb 		break;
557f9c7a07dSBjoern A. Zeeb 	default:
558f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
559f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
5609df0d1f3SBjoern A. Zeeb 			bw = IEEE80211_STA_RX_BW_160;
561f9c7a07dSBjoern A. Zeeb 		else
5629df0d1f3SBjoern A. Zeeb 			bw = IEEE80211_STA_RX_BW_80;
5639fb91463SBjoern A. Zeeb 	}
5649df0d1f3SBjoern A. Zeeb 	/*
5659df0d1f3SBjoern A. Zeeb 	 * While we can set what is possibly supported we also need to be
5669df0d1f3SBjoern A. Zeeb 	 * on a channel which supports that bandwidth; e.g., we can support
5679df0d1f3SBjoern A. Zeeb 	 * VHT160 but the AP only does VHT80.
5689df0d1f3SBjoern A. Zeeb 	 * Further ni_chan will also have filtered out what we disabled
5699df0d1f3SBjoern A. Zeeb 	 * by configuration.
5709df0d1f3SBjoern A. Zeeb 	 * Once net80211 channel selection is fixed for 802.11-2020 and
5719df0d1f3SBjoern A. Zeeb 	 * VHT160 we can possibly spare ourselves the above.
5729df0d1f3SBjoern A. Zeeb 	 */
5739df0d1f3SBjoern A. Zeeb 	if (bw == IEEE80211_STA_RX_BW_160 &&
5749df0d1f3SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT160(ni->ni_chan) &&
5759df0d1f3SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
5769df0d1f3SBjoern A. Zeeb 		bw = IEEE80211_STA_RX_BW_80;
5779df0d1f3SBjoern A. Zeeb 	if (bw == IEEE80211_STA_RX_BW_80 &&
5789df0d1f3SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
5799df0d1f3SBjoern A. Zeeb 		bw = sta->deflink.bandwidth;
5809df0d1f3SBjoern A. Zeeb 	sta->deflink.bandwidth = bw;
5813e022a91SBjoern A. Zeeb skip_bw:
582f9c7a07dSBjoern A. Zeeb 
583f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
584f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
585f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
586f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
587f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
588f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
589f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
590f9c7a07dSBjoern A. Zeeb 			break;
591f9c7a07dSBjoern A. Zeeb 		}
592f9c7a07dSBjoern A. Zeeb 	}
593f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
594f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
595f9c7a07dSBjoern A. Zeeb 
596f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
597f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
598f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
599f9c7a07dSBjoern A. Zeeb 		break;
600f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
601f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
602f9c7a07dSBjoern A. Zeeb 		break;
603f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
604f9c7a07dSBjoern A. Zeeb 	default:
605f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
606f9c7a07dSBjoern A. Zeeb 		break;
607f9c7a07dSBjoern A. Zeeb 	}
6089fb91463SBjoern A. Zeeb }
6099fb91463SBjoern A. Zeeb #endif
6109fb91463SBjoern A. Zeeb 
611d9f59799SBjoern A. Zeeb static void
61262d51a43SBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
61362d51a43SBjoern A. Zeeb     struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
614f9c7a07dSBjoern A. Zeeb {
615f9c7a07dSBjoern A. Zeeb 
616f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
61762d51a43SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(vif, sta, ni);
618f9c7a07dSBjoern A. Zeeb #endif
619f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
62062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(vif, sta, ni);
621f9c7a07dSBjoern A. Zeeb #endif
62262d51a43SBjoern A. Zeeb 	/*
62362d51a43SBjoern A. Zeeb 	 * We are also called from node allocation which net80211
62462d51a43SBjoern A. Zeeb 	 * can do even on `ifconfig down`; in that case the chanctx
62562d51a43SBjoern A. Zeeb 	 * may still be valid and we get a discrepancy between
62662d51a43SBjoern A. Zeeb 	 * sta and chanctx.  Thus do not try to update the chanctx
62762d51a43SBjoern A. Zeeb 	 * when called from lkpi_lsta_alloc().
62862d51a43SBjoern A. Zeeb 	 */
62962d51a43SBjoern A. Zeeb 	if (updchnctx)
63062d51a43SBjoern A. Zeeb 		lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
631f9c7a07dSBjoern A. Zeeb }
632f9c7a07dSBjoern A. Zeeb 
633389265e3SBjoern A. Zeeb static uint8_t
634389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
635389265e3SBjoern A. Zeeb {
636389265e3SBjoern A. Zeeb 	uint8_t chains;
637389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
638389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
639389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
640389265e3SBjoern A. Zeeb 
641389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
642389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
643389265e3SBjoern A. Zeeb #endif
644389265e3SBjoern A. Zeeb 
645389265e3SBjoern A. Zeeb 	chains = 1;
646389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
647389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
648389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
649389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
650389265e3SBjoern A. Zeeb #endif
651389265e3SBjoern A. Zeeb 
652389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
653389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
654389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
655389265e3SBjoern A. Zeeb #endif
656389265e3SBjoern A. Zeeb 
657389265e3SBjoern A. Zeeb 	return (chains);
658389265e3SBjoern A. Zeeb }
659389265e3SBjoern A. Zeeb 
660f9c7a07dSBjoern A. Zeeb static void
66167674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
66267674c1cSBjoern A. Zeeb     const char *_f, int _l)
663d9f59799SBjoern A. Zeeb {
664d9f59799SBjoern A. Zeeb 
6659d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6669d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
667d9f59799SBjoern A. Zeeb 		return;
668d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
669d9f59799SBjoern A. Zeeb 		return;
670d9f59799SBjoern A. Zeeb 
671d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
67267674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
67367674c1cSBjoern A. Zeeb 	if (ni != NULL)
67467674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
675d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
676d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
67711db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
6789d9ba2b7SBjoern A. Zeeb #endif
679d9f59799SBjoern A. Zeeb }
680d9f59799SBjoern A. Zeeb 
681d9f59799SBjoern A. Zeeb static void
682d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
683d9f59799SBjoern A. Zeeb {
684d9f59799SBjoern A. Zeeb 
685cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(lsta->hw->wiphy);
686d9f59799SBjoern A. Zeeb 
687a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
688a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
689a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
690d9f59799SBjoern A. Zeeb }
691d9f59799SBjoern A. Zeeb 
6924f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
6934f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
6944f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
6954f61ef8bSBjoern A. Zeeb {
6964f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
6974f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
6984f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
6994f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
700b6b352e4SBjoern A. Zeeb 	int band, i, tid;
7014f61ef8bSBjoern A. Zeeb 
7024f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
7034f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
7044f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
7054f61ef8bSBjoern A. Zeeb 		return (NULL);
7064f61ef8bSBjoern A. Zeeb 
707a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
7084f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
7094f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
7104f61ef8bSBjoern A. Zeeb 	/*
7110936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
7120936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
7130936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
7140936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
7150936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
7160936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
7170936c648SBjoern A. Zeeb 	 * two separate allocations.
7184f61ef8bSBjoern A. Zeeb 	 */
7190936c648SBjoern A. Zeeb 	lsta->ni = ni;
7204f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
7214f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
7224f61ef8bSBjoern A. Zeeb 
7234f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7244f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7254f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
7264f61ef8bSBjoern A. Zeeb 
7274f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
728b6b352e4SBjoern A. Zeeb 
729b6b352e4SBjoern A. Zeeb 	/* TXQ */
7304f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
7314f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
7324f61ef8bSBjoern A. Zeeb 
733d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
7344f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
7354f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
7364f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
7374f61ef8bSBjoern A. Zeeb 			goto cleanup;
7384f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
7394f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
740d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
741d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
742d0d29110SBjoern A. Zeeb 				continue;
743d0d29110SBjoern A. Zeeb 			}
744d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
7454f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
7464f61ef8bSBjoern A. Zeeb 		} else {
747fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
7484f61ef8bSBjoern A. Zeeb 		}
749d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
7500cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
751d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
7524f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
7534f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
7540cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
755d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
756eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
7574f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
7584f61ef8bSBjoern A. Zeeb 	}
7594f61ef8bSBjoern A. Zeeb 
760b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
761b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
762b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
763b6b352e4SBjoern A. Zeeb 
764b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
765b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
766b6b352e4SBjoern A. Zeeb 			continue;
767b6b352e4SBjoern A. Zeeb 
768b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
76973cd1c5dSBjoern A. Zeeb 			switch (band) {
77073cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
77173cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
77273cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
77373cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
77473cd1c5dSBjoern A. Zeeb 				case 110:
77573cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
77673cd1c5dSBjoern A. Zeeb 				case 55:
77773cd1c5dSBjoern A. Zeeb 				case 20:
77873cd1c5dSBjoern A. Zeeb 				case 10:
779b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
78073cd1c5dSBjoern A. Zeeb 					break;
78173cd1c5dSBjoern A. Zeeb 				}
78273cd1c5dSBjoern A. Zeeb 				break;
78373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
78473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
78573cd1c5dSBjoern A. Zeeb 				case 240:
78673cd1c5dSBjoern A. Zeeb 				case 120:
78773cd1c5dSBjoern A. Zeeb 				case 60:
78873cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
78973cd1c5dSBjoern A. Zeeb 					break;
79073cd1c5dSBjoern A. Zeeb 				}
79173cd1c5dSBjoern A. Zeeb 				break;
79273cd1c5dSBjoern A. Zeeb 			}
793b6b352e4SBjoern A. Zeeb 		}
794b6b352e4SBjoern A. Zeeb 	}
7959fb91463SBjoern A. Zeeb 
7966ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
7979fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
798f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
7999fb91463SBjoern A. Zeeb 
80062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
8019fb91463SBjoern A. Zeeb 
802f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
803b6b352e4SBjoern A. Zeeb 
8046ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
8056ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
8066ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
8076ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
8086ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
8096ffb7bd4SBjoern A. Zeeb 	}
81071034267SBjoern A. Zeeb 	IMPROVE("11be");
81171034267SBjoern A. Zeeb 	sta->mlo = false;
8126ffb7bd4SBjoern A. Zeeb 
8134f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
814fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
8154f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
816832b8e98SBjoern A. Zeeb 	mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
8170936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
8184f61ef8bSBjoern A. Zeeb 
8194f61ef8bSBjoern A. Zeeb 	return (lsta);
8204f61ef8bSBjoern A. Zeeb 
8214f61ef8bSBjoern A. Zeeb cleanup:
822eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
823eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
824eac3646fSBjoern A. Zeeb 
825eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
826eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
8274f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
828eac3646fSBjoern A. Zeeb 	}
8294f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8304f61ef8bSBjoern A. Zeeb 	return (NULL);
8314f61ef8bSBjoern A. Zeeb }
8324f61ef8bSBjoern A. Zeeb 
8330936c648SBjoern A. Zeeb static void
8340936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
8350936c648SBjoern A. Zeeb {
8360936c648SBjoern A. Zeeb 	struct mbuf *m;
8370936c648SBjoern A. Zeeb 
8380936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
8390936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
8400936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
8410936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
8420936c648SBjoern A. Zeeb 
8430936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
8440936c648SBjoern A. Zeeb 	IMPROVE();
8450936c648SBjoern A. Zeeb 
846fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
847fa4e4257SBjoern A. Zeeb 
848fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
8490936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
850fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
8510936c648SBjoern A. Zeeb 
8520936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
8530936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
8540936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
8550936c648SBjoern A. Zeeb 
8560936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
8570936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
8580936c648SBjoern A. Zeeb 	while (m != NULL) {
8590936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
8600936c648SBjoern A. Zeeb 
8610936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
8620936c648SBjoern A. Zeeb 		if (nim != NULL)
8630936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
8640936c648SBjoern A. Zeeb 		m_freem(m);
8650936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
8660936c648SBjoern A. Zeeb 	}
8670936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
8680936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
869fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
8700936c648SBjoern A. Zeeb 
8710936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
8720936c648SBjoern A. Zeeb 
8730936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
8740936c648SBjoern A. Zeeb 
8750936c648SBjoern A. Zeeb 	/* Free lsta. */
8760936c648SBjoern A. Zeeb 	lsta->ni = NULL;
8770936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
8780936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8790936c648SBjoern A. Zeeb }
8800936c648SBjoern A. Zeeb 
8810936c648SBjoern A. Zeeb 
8826b4cac81SBjoern A. Zeeb static enum nl80211_band
8836b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
8846b4cac81SBjoern A. Zeeb {
8856b4cac81SBjoern A. Zeeb 
8866b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
8876b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
8886b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
8896b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
8906b4cac81SBjoern A. Zeeb #ifdef __notyet__
8916b4cac81SBjoern A. Zeeb 	else if ()
8926b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
8936b4cac81SBjoern A. Zeeb 	else if ()
8946b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
8956b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
8966b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
8976b4cac81SBjoern A. Zeeb #endif
8986b4cac81SBjoern A. Zeeb 	else
8996b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
9006b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
9016b4cac81SBjoern A. Zeeb }
9026b4cac81SBjoern A. Zeeb 
9036b4cac81SBjoern A. Zeeb static uint32_t
9046b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
9056b4cac81SBjoern A. Zeeb {
9066b4cac81SBjoern A. Zeeb 
9076b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
9086b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
9096b4cac81SBjoern A. Zeeb 	switch (band) {
9106b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
9116b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
9126b4cac81SBjoern A. Zeeb 		break;
9136b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
9146b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
9156b4cac81SBjoern A. Zeeb 		break;
9166b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
9176b4cac81SBjoern A. Zeeb 		break;
9186b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
9196b4cac81SBjoern A. Zeeb 		break;
9206b4cac81SBjoern A. Zeeb 	default:
9216b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
9226b4cac81SBjoern A. Zeeb 		break;
9236b4cac81SBjoern A. Zeeb 	}
9246b4cac81SBjoern A. Zeeb 
9256b4cac81SBjoern A. Zeeb 	IMPROVE();
9266b4cac81SBjoern A. Zeeb 	return (0x00);
9276b4cac81SBjoern A. Zeeb }
9286b4cac81SBjoern A. Zeeb 
929e3a0b120SBjoern A. Zeeb #if 0
9306b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
9316b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
9326b4cac81SBjoern A. Zeeb {
9336b4cac81SBjoern A. Zeeb 
9346b4cac81SBjoern A. Zeeb 	switch (ac) {
9356b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
9366b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
9376b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
9386b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
9396b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
9406b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9416b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
9426b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
9436b4cac81SBjoern A. Zeeb 	default:
9446b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
9456b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9466b4cac81SBjoern A. Zeeb 	}
9476b4cac81SBjoern A. Zeeb }
948e3a0b120SBjoern A. Zeeb #endif
9496b4cac81SBjoern A. Zeeb 
9506b4cac81SBjoern A. Zeeb static enum nl80211_iftype
9516b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
9526b4cac81SBjoern A. Zeeb {
9536b4cac81SBjoern A. Zeeb 
9546b4cac81SBjoern A. Zeeb 	switch (opmode) {
9556b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
9566b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
9576b4cac81SBjoern A. Zeeb 		break;
9586b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
9596b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
9606b4cac81SBjoern A. Zeeb 		break;
9616b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
9626b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
9636b4cac81SBjoern A. Zeeb 		break;
9646b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
9656b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
9666b4cac81SBjoern A. Zeeb 		break;
9676b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
9686b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
9696b4cac81SBjoern A. Zeeb 		break;
9706b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
9716b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
9726b4cac81SBjoern A. Zeeb 		break;
9736b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
9746b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9756b4cac81SBjoern A. Zeeb 	default:
9766b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
9776b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9786b4cac81SBjoern A. Zeeb 	}
9796b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
9806b4cac81SBjoern A. Zeeb }
9816b4cac81SBjoern A. Zeeb 
982b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
98312a511c8SBjoern A. Zeeb static const char *
98412a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
98512a511c8SBjoern A. Zeeb {
98612a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
98712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
98812a511c8SBjoern A. Zeeb 		return ("WEP40");
989bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
990bf8c25f1SBjoern A. Zeeb 		return ("WEP104");
99112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
99212a511c8SBjoern A. Zeeb 		return ("TKIP");
99312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
99412a511c8SBjoern A. Zeeb 		return ("CCMP");
995bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
996bf8c25f1SBjoern A. Zeeb 		return ("CCMP_256");
99712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
99812a511c8SBjoern A. Zeeb 		return ("GCMP");
99912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
100012a511c8SBjoern A. Zeeb 		return ("GCMP_256");
1001bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
1002bf8c25f1SBjoern A. Zeeb 		return ("AES_CMAC");
1003bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1004bf8c25f1SBjoern A. Zeeb 		return ("BIP_CMAC_256");
100512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
100612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
100712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
100812a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
100912a511c8SBjoern A. Zeeb 	default:
101012a511c8SBjoern A. Zeeb 		return ("??");
101112a511c8SBjoern A. Zeeb 	}
101212a511c8SBjoern A. Zeeb }
101312a511c8SBjoern A. Zeeb 
10146b4cac81SBjoern A. Zeeb static uint32_t
1015bf8c25f1SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
1016bf8c25f1SBjoern A. Zeeb     uint32_t wlan_cipher_suite)
10176b4cac81SBjoern A. Zeeb {
10186b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
10196b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
10206b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
1021bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
1022bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
10236b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
10246b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
10256b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
1026906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
10276b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
1028bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM_256);
1029bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
1030bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_128);
1031bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
1032bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_256);
1033bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
1034bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_128);
10356b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1036bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_256);
1037bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1038bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_128);
1039bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1040bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_256);
10416b4cac81SBjoern A. Zeeb 	default:
1042bf8c25f1SBjoern A. Zeeb 		ic_printf(ic, "%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
104312a511c8SBjoern A. Zeeb 		    __func__,
104412a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
104512a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
10466b4cac81SBjoern A. Zeeb 		return (0);
10476b4cac81SBjoern A. Zeeb 	}
1048bf8c25f1SBjoern A. Zeeb }
10496b4cac81SBjoern A. Zeeb 
10506b4cac81SBjoern A. Zeeb static uint32_t
10516b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
10526b4cac81SBjoern A. Zeeb {
10536b4cac81SBjoern A. Zeeb 
10546b4cac81SBjoern A. Zeeb 	switch (cipher) {
10556b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
10566b4cac81SBjoern A. Zeeb 		if (keylen < 8)
10576b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
10586b4cac81SBjoern A. Zeeb 		else
10596b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
10606b4cac81SBjoern A. Zeeb 		break;
1061bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
1062bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
1063bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
1064bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
1065bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM_256:
1066bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP_256);
1067bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_128:
1068bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP);
1069bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_256:
1070bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP_256);
1071bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_128:
1072bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_AES_CMAC);
1073bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_256:
1074bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1075bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_128:
1076bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1077bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_256:
1078bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1079bf8c25f1SBjoern A. Zeeb 
10806b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
10816b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
1082bf8c25f1SBjoern A. Zeeb 		/*
1083bf8c25f1SBjoern A. Zeeb 		 * TKIP w/ hw MIC support
1084bf8c25f1SBjoern A. Zeeb 		 * (gone wrong; should really be a crypto flag in net80211).
1085bf8c25f1SBjoern A. Zeeb 		 */
10866b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
10876b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
10886b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
10896b4cac81SBjoern A. Zeeb 		break;
10906b4cac81SBjoern A. Zeeb 	default:
10916b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
10926b4cac81SBjoern A. Zeeb 	};
10936b4cac81SBjoern A. Zeeb 	return (0);
10946b4cac81SBjoern A. Zeeb }
10956b4cac81SBjoern A. Zeeb #endif
10966b4cac81SBjoern A. Zeeb 
10976b4cac81SBjoern A. Zeeb #ifdef __notyet__
10986b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
10996b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
11006b4cac81SBjoern A. Zeeb {
11016b4cac81SBjoern A. Zeeb 
11026b4cac81SBjoern A. Zeeb 	/*
11036b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
11046b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
11056b4cac81SBjoern A. Zeeb 	 */
11066b4cac81SBjoern A. Zeeb 	switch (state) {
11076b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
11086b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
11096b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
11106b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
11116b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
11126b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
11136b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
11146b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
11156b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
11166b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
11176b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
11186b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
11196b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
11206b4cac81SBjoern A. Zeeb 	default:
11216b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
11226b4cac81SBjoern A. Zeeb 	};
11236b4cac81SBjoern A. Zeeb 
11246b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
11256b4cac81SBjoern A. Zeeb }
11266b4cac81SBjoern A. Zeeb #endif
11276b4cac81SBjoern A. Zeeb 
11286b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11296b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
11306b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
11316b4cac81SBjoern A. Zeeb {
11326b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11336b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
11346b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
11356b4cac81SBjoern A. Zeeb 	int i, nchans;
11366b4cac81SBjoern A. Zeeb 
11376b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11386b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
11396b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
11406b4cac81SBjoern A. Zeeb 		return (NULL);
11416b4cac81SBjoern A. Zeeb 
11426b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
11436b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
11446b4cac81SBjoern A. Zeeb 		return (NULL);
11456b4cac81SBjoern A. Zeeb 
11466b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
11476b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
11486b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
11496b4cac81SBjoern A. Zeeb 			return (&channels[i]);
11506b4cac81SBjoern A. Zeeb 	}
11516b4cac81SBjoern A. Zeeb 
11526b4cac81SBjoern A. Zeeb 	return (NULL);
11536b4cac81SBjoern A. Zeeb }
11546b4cac81SBjoern A. Zeeb 
11552ac8a218SBjoern A. Zeeb #if 0
11566b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11576b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
11586b4cac81SBjoern A. Zeeb {
11596b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
11606b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
11616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11626b4cac81SBjoern A. Zeeb 
11636b4cac81SBjoern A. Zeeb 	chan = NULL;
11646b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
11656b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
11666b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
11676b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
11686b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
11696b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
11706b4cac81SBjoern A. Zeeb 	else
11716b4cac81SBjoern A. Zeeb 		c = NULL;
11726b4cac81SBjoern A. Zeeb 
11736b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
11746b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
11756b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
11766b4cac81SBjoern A. Zeeb 	}
11776b4cac81SBjoern A. Zeeb 
11786b4cac81SBjoern A. Zeeb 	return (chan);
11796b4cac81SBjoern A. Zeeb }
11802ac8a218SBjoern A. Zeeb #endif
11816b4cac81SBjoern A. Zeeb 
11822e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
11832e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
11842e183d99SBjoern A. Zeeb {
11852e183d99SBjoern A. Zeeb 	enum nl80211_band band;
11862e183d99SBjoern A. Zeeb 
11872e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11882e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
11892e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
11902e183d99SBjoern A. Zeeb 		int i;
11912e183d99SBjoern A. Zeeb 
11922e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
11932e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
11942e183d99SBjoern A. Zeeb 			continue;
11952e183d99SBjoern A. Zeeb 
11962e183d99SBjoern A. Zeeb 		channels = supband->channels;
11972e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
11982e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
11992e183d99SBjoern A. Zeeb 				return (&channels[i]);
12002e183d99SBjoern A. Zeeb 		}
12012e183d99SBjoern A. Zeeb 	}
12022e183d99SBjoern A. Zeeb 
12032e183d99SBjoern A. Zeeb 	return (NULL);
12042e183d99SBjoern A. Zeeb }
12052e183d99SBjoern A. Zeeb 
1206b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
12076b4cac81SBjoern A. Zeeb static int
120811db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
120911db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
121011db70b6SBjoern A. Zeeb {
121111db70b6SBjoern A. Zeeb 	int error;
121211db70b6SBjoern A. Zeeb 
121311db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
121411db70b6SBjoern A. Zeeb 		return (0);
121511db70b6SBjoern A. Zeeb 
121611db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
121711db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
121811db70b6SBjoern A. Zeeb 
121911db70b6SBjoern A. Zeeb 	error = 0;
122011db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
122111db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
122211db70b6SBjoern A. Zeeb 		int err;
122311db70b6SBjoern A. Zeeb 
122411db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
122511db70b6SBjoern A. Zeeb 			continue;
122611db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
122711db70b6SBjoern A. Zeeb 
1228a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1229a6f6329cSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1230a6f6329cSBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: running set_key cmd %d(%s) for "
1231a6f6329cSBjoern A. Zeeb 			    "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1232a6f6329cSBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1233a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1234a6f6329cSBjoern A. Zeeb #endif
1235a6f6329cSBjoern A. Zeeb 
123611db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
123711db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
123811db70b6SBjoern A. Zeeb 		if (err != 0) {
123911db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
124011db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
124111db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
124211db70b6SBjoern A. Zeeb 			error++;
124311db70b6SBjoern A. Zeeb 
124411db70b6SBjoern A. Zeeb 			/*
124511db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
124611db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
124711db70b6SBjoern A. Zeeb 			 * crash (firmware).
124811db70b6SBjoern A. Zeeb 			 */
124911db70b6SBjoern A. Zeeb 			continue;
125011db70b6SBjoern A. Zeeb 		}
125111db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
125211db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
125311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
1254a6f6329cSBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
125511db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1256a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
125711db70b6SBjoern A. Zeeb #endif
125811db70b6SBjoern A. Zeeb 
125911db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
126011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
126111db70b6SBjoern A. Zeeb 	}
126211db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
126311db70b6SBjoern A. Zeeb 	return (error);
126411db70b6SBjoern A. Zeeb }
126511db70b6SBjoern A. Zeeb 
1266aa294e8eSBjoern A. Zeeb /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
1267aa294e8eSBjoern A. Zeeb /* See also lkpi_sta_del_keys() these days. */
126811db70b6SBjoern A. Zeeb static int
1269aa294e8eSBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
12706b4cac81SBjoern A. Zeeb {
12716b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
12726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12736b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12746b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
127511db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12766b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12776b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12786b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12796b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
12806b4cac81SBjoern A. Zeeb 	int error;
12816b4cac81SBjoern A. Zeeb 
12826b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
1283a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1284a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1285a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1286a6f6329cSBjoern A. Zeeb 		return (0);
1287a6f6329cSBjoern A. Zeeb 	}
12886b4cac81SBjoern A. Zeeb 
128911db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
129011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
129111db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
129211db70b6SBjoern A. Zeeb 		return (0);
129311db70b6SBjoern A. Zeeb 	}
1294a6f6329cSBjoern A. Zeeb 
129511db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
129611db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
129711db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
129811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
129911db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
130011db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
130111db70b6SBjoern A. Zeeb 		return (0);
130211db70b6SBjoern A. Zeeb 	}
130311db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
130411db70b6SBjoern A. Zeeb 
130511db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
130611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
130711db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
130811db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
130911db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
131011db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
131111db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
131211db70b6SBjoern A. Zeeb #endif
131311db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
131411db70b6SBjoern A. Zeeb 		return (1);
131511db70b6SBjoern A. Zeeb 	}
131611db70b6SBjoern A. Zeeb 
131711db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
131811db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
131911db70b6SBjoern A. Zeeb 	if (kc == NULL) {
132011db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
132111db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
132211db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
132311db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
132411db70b6SBjoern A. Zeeb #endif
132511db70b6SBjoern A. Zeeb 		error = 1;
132611db70b6SBjoern A. Zeeb 		goto out;
132711db70b6SBjoern A. Zeeb 	}
132811db70b6SBjoern A. Zeeb 
1329a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1330a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1331a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running  set_key cmd %d(%s) for sta %6D: "
1332a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
1333a6f6329cSBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1334a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1335a6f6329cSBjoern A. Zeeb #endif
1336a6f6329cSBjoern A. Zeeb 
1337a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1338a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1339a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1340a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
134111db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
134211db70b6SBjoern A. Zeeb 	if (error != 0) {
134311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
134411db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
134511db70b6SBjoern A. Zeeb 		error = 0;
134611db70b6SBjoern A. Zeeb 		goto out;
134711db70b6SBjoern A. Zeeb 	}
134811db70b6SBjoern A. Zeeb 
134911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
135011db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
135111db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1352a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
135311db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1354a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
135511db70b6SBjoern A. Zeeb #endif
135611db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
135711db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
135811db70b6SBjoern A. Zeeb 	error = 1;
135911db70b6SBjoern A. Zeeb out:
136011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
136111db70b6SBjoern A. Zeeb 	return (error);
136211db70b6SBjoern A. Zeeb }
136311db70b6SBjoern A. Zeeb 
136411db70b6SBjoern A. Zeeb static int
1365aa294e8eSBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
136611db70b6SBjoern A. Zeeb {
136711db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
136811db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
136911db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
137011db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
137111db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
137211db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
137311db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
137411db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
137511db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
137611db70b6SBjoern A. Zeeb 	uint32_t lcipher;
137792942717SBjoern A. Zeeb 	uint16_t exp_flags;
13784b9ae864SBjoern A. Zeeb 	uint8_t keylen;
137911db70b6SBjoern A. Zeeb 	int error;
138011db70b6SBjoern A. Zeeb 
138111db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
1382a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1383a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1384a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1385a6f6329cSBjoern A. Zeeb 		return (0);
1386a6f6329cSBjoern A. Zeeb 	}
138711db70b6SBjoern A. Zeeb 
138811db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
138911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
139011db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
139111db70b6SBjoern A. Zeeb 		return (0);
139211db70b6SBjoern A. Zeeb 	}
139311db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
139411db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
139511db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
139611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
139711db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
139811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
139911db70b6SBjoern A. Zeeb 		return (0);
140011db70b6SBjoern A. Zeeb 	}
140111db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
140211db70b6SBjoern A. Zeeb 
14034b9ae864SBjoern A. Zeeb 	keylen = k->wk_keylen;
14044b9ae864SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
14054b9ae864SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
14064b9ae864SBjoern A. Zeeb 	switch (lcipher) {
14074b9ae864SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
14084b9ae864SBjoern A. Zeeb 		break;
14094b9ae864SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
14104b9ae864SBjoern A. Zeeb 		keylen += 2 * k->wk_cipher->ic_miclen;
14114b9ae864SBjoern A. Zeeb 		break;
14124b9ae864SBjoern A. Zeeb 	default:
14134b9ae864SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
14144b9ae864SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
14154b9ae864SBjoern A. Zeeb 		IMPROVE();
14164b9ae864SBjoern A. Zeeb 		ieee80211_free_node(ni);
14174b9ae864SBjoern A. Zeeb 		return (0);
14184b9ae864SBjoern A. Zeeb 	}
14194b9ae864SBjoern A. Zeeb 
142011db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
142111db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
142211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
142311db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
142411db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
142511db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
142611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
142711db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
142811db70b6SBjoern A. Zeeb 	}
142911db70b6SBjoern A. Zeeb 
14304b9ae864SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + keylen, M_LKPI80211, M_WAITOK | M_ZERO);
143111db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
143211db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
14336b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
14346b4cac81SBjoern A. Zeeb #if 0
14356b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
14366b4cac81SBjoern A. Zeeb #endif
14376b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
14386b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
14396b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
14406b4cac81SBjoern A. Zeeb 
144111db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
144211db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
144311db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
144411db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
144511db70b6SBjoern A. Zeeb 
14466b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
14476b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
14486b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
14496b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
14506b4cac81SBjoern A. Zeeb 		break;
14516b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
14524b9ae864SBjoern A. Zeeb 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, k->wk_txmic, k->wk_cipher->ic_miclen);
14534b9ae864SBjoern A. Zeeb 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, k->wk_rxmic, k->wk_cipher->ic_miclen);
145492942717SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
145592942717SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
145692942717SBjoern A. Zeeb 		break;
14576b4cac81SBjoern A. Zeeb 	default:
145811db70b6SBjoern A. Zeeb 		/* currently UNREACH */
14596b4cac81SBjoern A. Zeeb 		IMPROVE();
146011db70b6SBjoern A. Zeeb 		break;
14616b4cac81SBjoern A. Zeeb 	};
146211db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
14636b4cac81SBjoern A. Zeeb 
1464a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1465a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1466a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running set_key cmd %d(%s) for sta %6D: "
14674b9ae864SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u keylen %u flags %b\n", __func__,
14684b9ae864SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":", kc, kc->keyidx, kc->hw_key_idx,
14694b9ae864SBjoern A. Zeeb 		    kc->keylen, kc->flags, IEEE80211_KEY_FLAG_BITS);
1470a6f6329cSBjoern A. Zeeb #endif
1471a6f6329cSBjoern A. Zeeb 
1472a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1473a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1474a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1475a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
147611db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
147711db70b6SBjoern A. Zeeb 	if (error != 0) {
147811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
147911db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
148011db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
148111db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
148211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
148311db70b6SBjoern A. Zeeb 		return (0);
14846b4cac81SBjoern A. Zeeb 	}
14856b4cac81SBjoern A. Zeeb 
148611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
148711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
148811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1489a6f6329cSBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %b\n", __func__,
149011db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
1491a6f6329cSBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
149211db70b6SBjoern A. Zeeb #endif
149311db70b6SBjoern A. Zeeb 
149492942717SBjoern A. Zeeb 	exp_flags = 0;
149592942717SBjoern A. Zeeb 	switch (kc->cipher) {
149692942717SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
149792942717SBjoern A. Zeeb 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
149892942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_PUT_IV_SPACE |
149992942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_GENERATE_MMIC |
150092942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
150192942717SBjoern A. Zeeb #define	TKIP_INVAL_COMBINATION						\
150292942717SBjoern A. Zeeb      (IEEE80211_KEY_FLAG_PUT_MIC_SPACE|IEEE80211_KEY_FLAG_GENERATE_MMIC)
150392942717SBjoern A. Zeeb 		if ((kc->flags & TKIP_INVAL_COMBINATION) == TKIP_INVAL_COMBINATION) {
150492942717SBjoern A. Zeeb 			ic_printf(ic, "%s: SET_KEY for %s returned invalid "
150592942717SBjoern A. Zeeb 			    "combination %b\n", __func__,
150692942717SBjoern A. Zeeb 			    lkpi_cipher_suite_to_name(kc->cipher),
150792942717SBjoern A. Zeeb 			    kc->flags, IEEE80211_KEY_FLAG_BITS);
150892942717SBjoern A. Zeeb 		}
150992942717SBjoern A. Zeeb #undef	TKIP_INVAL_COMBINATION
151092942717SBjoern A. Zeeb #ifdef __notyet__
151192942717SBjoern A. Zeeb 		/* Do flags surgery; special see linuxkpi_ieee80211_ifattach(). */
151292942717SBjoern A. Zeeb 		if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) != 0) {
151392942717SBjoern A. Zeeb 			k->wk_flags &= ~(IEEE80211_KEY_NOMICMGT|IEEE80211_KEY_NOMIC);
151492942717SBjoern A. Zeeb 			k->wk_flags |= IEEE80211_KEY_SWMIC;
151592942717SBjoern A. Zeeb 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC
151692942717SBjoern A. Zeeb 		}
151792942717SBjoern A. Zeeb #endif
151892942717SBjoern A. Zeeb 		break;
151992942717SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
152092942717SBjoern A. Zeeb 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
152192942717SBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_PUT_IV_SPACE |
152292942717SBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_GENERATE_IV |
152391716e8dSBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_GENERATE_IV_MGMT |	/* Only needs IV geeration for MGMT frames. */
152491716e8dSBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_SW_MGMT_TX);		/* MFP in software */
152592942717SBjoern A. Zeeb 		break;
152692942717SBjoern A. Zeeb 	}
152792942717SBjoern A. Zeeb 	if ((kc->flags & ~exp_flags) != 0)
152892942717SBjoern A. Zeeb 		ic_printf(ic, "%s: SET_KEY for %s returned unexpected key flags: "
152992942717SBjoern A. Zeeb 		    " %#06x & ~%#06x = %b\n", __func__,
153092942717SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(kc->cipher), kc->flags, exp_flags,
153192942717SBjoern A. Zeeb 		    (kc->flags & ~exp_flags), IEEE80211_KEY_FLAG_BITS);
153292942717SBjoern A. Zeeb 
153392942717SBjoern A. Zeeb #ifdef __notyet__
153492942717SBjoern A. Zeeb 	/* Do flags surgery. */
153592942717SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) == 0)
153692942717SBjoern A. Zeeb 		k->wk_flags |= IEEE80211_KEY_NOIVMGT;
153792942717SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
153892942717SBjoern A. Zeeb 		k->wk_flags |= IEEE80211_KEY_NOIV;
153992942717SBjoern A. Zeeb #endif
154092942717SBjoern A. Zeeb 
154111db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
15426b4cac81SBjoern A. Zeeb 	return (1);
15436b4cac81SBjoern A. Zeeb }
15446b4cac81SBjoern A. Zeeb 
1545b8dfc3ecSBjoern A. Zeeb static void
1546b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1547b8dfc3ecSBjoern A. Zeeb {
1548b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1549b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1550b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1551b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1552b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1553a6413bceSBjoern A. Zeeb 	struct ieee80211_node *ni;
1554a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1555b8dfc3ecSBjoern A. Zeeb 
1556b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1557b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1558b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1559b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1560b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1561b8dfc3ecSBjoern A. Zeeb 
1562a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1563a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1564b8dfc3ecSBjoern A. Zeeb 
1565b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1566b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1567a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1568a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1569a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1570a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1571a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1572b8dfc3ecSBjoern A. Zeeb #endif
1573b8dfc3ecSBjoern A. Zeeb 
1574a6413bceSBjoern A. Zeeb 	/*
1575a6413bceSBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1576a6413bceSBjoern A. Zeeb 	 */
1577a6413bceSBjoern A. Zeeb 	/* Try to make sure the node does not go away while possibly unlocked. */
1578a6413bceSBjoern A. Zeeb 	ni = NULL;
1579a6413bceSBjoern A. Zeeb 	if (icislocked || ntislocked) {
1580a6413bceSBjoern A. Zeeb 		if (vap->iv_bss != NULL)
1581a6413bceSBjoern A. Zeeb 			ni = ieee80211_ref_node(vap->iv_bss);
1582a6413bceSBjoern A. Zeeb 	}
1583a6413bceSBjoern A. Zeeb 
1584a6165709SBjoern A. Zeeb 	if (icislocked)
1585a6165709SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
1586a6165709SBjoern A. Zeeb 	if (ntislocked)
1587b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1588b8dfc3ecSBjoern A. Zeeb 
1589b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1590b8dfc3ecSBjoern A. Zeeb 
1591a6413bceSBjoern A. Zeeb 	KASSERT(lvif->key_update_iv_bss == NULL, ("%s: key_update_iv_bss not NULL %p",
1592a6413bceSBjoern A. Zeeb 	    __func__, lvif->key_update_iv_bss));
1593a6413bceSBjoern A. Zeeb 	lvif->key_update_iv_bss = ni;
1594a6413bceSBjoern A. Zeeb 
1595b8dfc3ecSBjoern A. Zeeb 	/*
1596a6165709SBjoern A. Zeeb 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1597b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1598b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1599b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1600b8dfc3ecSBjoern A. Zeeb 	 */
1601a6165709SBjoern A. Zeeb 	if (icislocked)
1602a6165709SBjoern A. Zeeb 		refcount_acquire(&lvif->ic_unlocked);
1603a6165709SBjoern A. Zeeb 	if (ntislocked)
1604b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1605b8dfc3ecSBjoern A. Zeeb }
1606b8dfc3ecSBjoern A. Zeeb 
1607b8dfc3ecSBjoern A. Zeeb static void
1608b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1609b8dfc3ecSBjoern A. Zeeb {
1610b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1611b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1612b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1613b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1614b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1615a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1616b8dfc3ecSBjoern A. Zeeb 
1617b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1618b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1619b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1620b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1621b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1622b8dfc3ecSBjoern A. Zeeb 
1623a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1624a6165709SBjoern A. Zeeb 	MPASS(!icislocked);
1625a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1626a6165709SBjoern A. Zeeb 	MPASS(!ntislocked);
1627b8dfc3ecSBjoern A. Zeeb 
1628b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1629b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1630a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1631a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1632a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1633a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1634a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1635b8dfc3ecSBjoern A. Zeeb #endif
1636b8dfc3ecSBjoern A. Zeeb 
1637b8dfc3ecSBjoern A. Zeeb 	/*
1638b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1639b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1640b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1641b8dfc3ecSBjoern A. Zeeb 	 */
1642a6165709SBjoern A. Zeeb 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1643a6165709SBjoern A. Zeeb 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1644a6413bceSBjoern A. Zeeb 
1645a6413bceSBjoern A. Zeeb 	if (lvif->key_update_iv_bss != NULL) {
1646a6413bceSBjoern A. Zeeb 		ieee80211_free_node(lvif->key_update_iv_bss);
1647a6413bceSBjoern A. Zeeb 		lvif->key_update_iv_bss = NULL;
1648a6413bceSBjoern A. Zeeb 	}
1649a6413bceSBjoern A. Zeeb 
1650b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1651b8dfc3ecSBjoern A. Zeeb 
1652a6165709SBjoern A. Zeeb 	/*
1653a6165709SBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1654a6165709SBjoern A. Zeeb 	 * ic before nt to avoid a LOR.
1655a6165709SBjoern A. Zeeb 	 */
1656a6165709SBjoern A. Zeeb 	if (icislocked)
1657a6165709SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
1658a6165709SBjoern A. Zeeb 	if (ntislocked)
1659b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1660b8dfc3ecSBjoern A. Zeeb }
16616b4cac81SBjoern A. Zeeb #endif
16626b4cac81SBjoern A. Zeeb 
16636b4cac81SBjoern A. Zeeb static u_int
16646b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
16656b4cac81SBjoern A. Zeeb {
16666b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
16676b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
16686b4cac81SBjoern A. Zeeb 
16696b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
16706b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
16716b4cac81SBjoern A. Zeeb 
16726b4cac81SBjoern A. Zeeb 	mc_list = arg;
16736b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
16746b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
16756b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
16766b4cac81SBjoern A. Zeeb 			return (0);
16776b4cac81SBjoern A. Zeeb 	}
16786b4cac81SBjoern A. Zeeb 
16796b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
16806b4cac81SBjoern A. Zeeb 	if (addr == NULL)
16816b4cac81SBjoern A. Zeeb 		return (0);
16826b4cac81SBjoern A. Zeeb 
16836b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
16846b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
16856b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
16866b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
16876b4cac81SBjoern A. Zeeb 	mc_list->count++;
16886b4cac81SBjoern A. Zeeb 
16899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16909d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
16916b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
16926b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
16939d9ba2b7SBjoern A. Zeeb #endif
16946b4cac81SBjoern A. Zeeb 
16956b4cac81SBjoern A. Zeeb 	return (1);
16966b4cac81SBjoern A. Zeeb }
16976b4cac81SBjoern A. Zeeb 
16986b4cac81SBjoern A. Zeeb static void
16996b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
17006b4cac81SBjoern A. Zeeb {
17016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
17026b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17036b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
17046b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
17056b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
17066b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
17076b4cac81SBjoern A. Zeeb 	u64 mc;
17086b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
17096b4cac81SBjoern A. Zeeb 
17106b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
17116b4cac81SBjoern A. Zeeb 
17126b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
17136b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
17146b4cac81SBjoern A. Zeeb 		return;
17156b4cac81SBjoern A. Zeeb 
17166b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
17176b4cac81SBjoern A. Zeeb 		return;
17186b4cac81SBjoern A. Zeeb 
17196b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
17206b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
17216b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
17226b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
17236b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
17246b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
17256b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
17266b4cac81SBjoern A. Zeeb 	} else {
17276b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
17286b4cac81SBjoern A. Zeeb 	}
17296b4cac81SBjoern A. Zeeb 
17306b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17316b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
17326b4cac81SBjoern A. Zeeb 	/*
17336b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
17346b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
17356b4cac81SBjoern A. Zeeb 	 */
17366b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
17376b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
17386b4cac81SBjoern A. Zeeb 
17399d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17409d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
17416b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
17426b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
17439d9ba2b7SBjoern A. Zeeb #endif
17446b4cac81SBjoern A. Zeeb 
17456b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
17466b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
17476b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
17486b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
17496b4cac81SBjoern A. Zeeb 			mc_list.count--;
17506b4cac81SBjoern A. Zeeb 		}
17516b4cac81SBjoern A. Zeeb 	}
17526b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
17536b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
17546b4cac81SBjoern A. Zeeb }
17556b4cac81SBjoern A. Zeeb 
1756fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1757fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1758fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1759fa8f007dSBjoern A. Zeeb {
1760fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1761fa8f007dSBjoern A. Zeeb 
1762fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1763fa8f007dSBjoern A. Zeeb 
17649d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17659d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1766fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1767fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1768ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1769fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1770616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1771fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1772fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1773fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1774fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1775ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
17769d9ba2b7SBjoern A. Zeeb #endif
1777fa8f007dSBjoern A. Zeeb 
1778fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1779fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1780fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1781fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1782fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1783fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1784fa8f007dSBjoern A. Zeeb 	}
1785fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1786fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1787fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1788fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1789fa8f007dSBjoern A. Zeeb 	}
1790fa8f007dSBjoern A. Zeeb 
1791fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1792fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1793fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1794fa8f007dSBjoern A. Zeeb 
17959d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17969d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1797fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1798fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1799ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1800fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1801616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1802fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1803fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1804fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1805fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1806ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
18079d9ba2b7SBjoern A. Zeeb #endif
1808fa8f007dSBjoern A. Zeeb 
1809fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1810fa8f007dSBjoern A. Zeeb }
1811fa8f007dSBjoern A. Zeeb 
18126b4cac81SBjoern A. Zeeb static void
18136b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
18146b4cac81SBjoern A. Zeeb {
18156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18166b4cac81SBjoern A. Zeeb 	int error;
18178ac540d3SBjoern A. Zeeb 	bool cancel;
18186b4cac81SBjoern A. Zeeb 
18198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
18208ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
18218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
18228ac540d3SBjoern A. Zeeb 	if (!cancel)
18236b4cac81SBjoern A. Zeeb 		return;
18246b4cac81SBjoern A. Zeeb 
18256b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18266b4cac81SBjoern A. Zeeb 
1827bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1828cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
18296b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
18306b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1831cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
18326b4cac81SBjoern A. Zeeb 
18336b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
18348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
18358ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
18368ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
18378ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
18388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
18398ac540d3SBjoern A. Zeeb 
1840bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
18416b4cac81SBjoern A. Zeeb 
18428ac540d3SBjoern A. Zeeb 	if (cancel)
18436b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
18446b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
18456b4cac81SBjoern A. Zeeb }
18466b4cac81SBjoern A. Zeeb 
18476b4cac81SBjoern A. Zeeb static void
1848086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1849086be6a8SBjoern A. Zeeb {
1850086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1851086be6a8SBjoern A. Zeeb 	int error;
1852086be6a8SBjoern A. Zeeb 	bool old;
1853086be6a8SBjoern A. Zeeb 
1854086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1855086be6a8SBjoern A. Zeeb 	if (old == new)
1856086be6a8SBjoern A. Zeeb 		return;
1857086be6a8SBjoern A. Zeeb 
1858086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1859086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1860086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1861086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1862086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1863086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1864086be6a8SBjoern A. Zeeb 	}
1865086be6a8SBjoern A. Zeeb }
1866086be6a8SBjoern A. Zeeb 
18675a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
18686b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
18696b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
18706b4cac81SBjoern A. Zeeb {
18715a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
18725a4d2461SBjoern A. Zeeb 
18735a4d2461SBjoern A. Zeeb 	changed = 0;
18746b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1875616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
18766b4cac81SBjoern A. Zeeb 
18776b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
18786b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
18796b4cac81SBjoern A. Zeeb 
1880616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1881616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
18826b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
18836b4cac81SBjoern A. Zeeb 		IMPROVE();
1884086be6a8SBjoern A. Zeeb 
18855a4d2461SBjoern A. Zeeb 		/*
18865a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
18875a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
18885a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
18895a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
18905a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
18915a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
18925a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
18935a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
18945a4d2461SBjoern A. Zeeb 		 */
18956b4cac81SBjoern A. Zeeb 	}
18965a4d2461SBjoern A. Zeeb 
18975a4d2461SBjoern A. Zeeb 	return (changed);
18986b4cac81SBjoern A. Zeeb }
18996b4cac81SBjoern A. Zeeb 
19006b4cac81SBjoern A. Zeeb static void
19016b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
19026b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
19036b4cac81SBjoern A. Zeeb {
19046b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
19056b4cac81SBjoern A. Zeeb 	int tid;
1906eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
19076b4cac81SBjoern A. Zeeb 
19086b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
19096b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
19106b4cac81SBjoern A. Zeeb 
19116b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
19126b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
19136b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
19146b4cac81SBjoern A. Zeeb 				continue;
19156b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
19166b4cac81SBjoern A. Zeeb 			continue;
19176b4cac81SBjoern A. Zeeb 
19186b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
19196b4cac81SBjoern A. Zeeb 			continue;
19206b4cac81SBjoern A. Zeeb 
19216b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
19226b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
19236b4cac81SBjoern A. Zeeb 			continue;
19246b4cac81SBjoern A. Zeeb 
1925eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1926eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1927eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1928eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
19296b4cac81SBjoern A. Zeeb 			continue;
19306b4cac81SBjoern A. Zeeb 
19316b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
19326b4cac81SBjoern A. Zeeb 	}
19336b4cac81SBjoern A. Zeeb }
19346b4cac81SBjoern A. Zeeb 
193588665349SBjoern A. Zeeb /*
193688665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
193788665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
193888665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
193988665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
194088665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
194188665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
194288665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
194388665349SBjoern A. Zeeb  * re-used.
194488665349SBjoern A. Zeeb  */
194588665349SBjoern A. Zeeb static void
194688665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
194788665349SBjoern A. Zeeb {
1948cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
194988665349SBjoern A. Zeeb 	struct mbufq mq;
195088665349SBjoern A. Zeeb 	struct mbuf *m;
195188665349SBjoern A. Zeeb 	int len;
195288665349SBjoern A. Zeeb 
1953cd0fcf9fSBjoern A. Zeeb 	/* There is no lockdep_assert_not_held_wiphy(). */
1954cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1955cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_not_held(&hw->wiphy->mtx);
195688665349SBjoern A. Zeeb 
195788665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
195888665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
195988665349SBjoern A. Zeeb 	lsta->txq_ready = false;
196088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
196188665349SBjoern A. Zeeb 
196288665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
196388665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
196488665349SBjoern A. Zeeb 
196588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
196688665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
196788665349SBjoern A. Zeeb 	if (len <= 0) {
196888665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
196988665349SBjoern A. Zeeb 		return;
197088665349SBjoern A. Zeeb 	}
197188665349SBjoern A. Zeeb 
197288665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
197388665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
197488665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
197588665349SBjoern A. Zeeb 
197688665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
197788665349SBjoern A. Zeeb 	while (m != NULL) {
197888665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
197988665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
198088665349SBjoern A. Zeeb 	}
198188665349SBjoern A. Zeeb }
198288665349SBjoern A. Zeeb 
198350d826beSBjoern A. Zeeb 
198450d826beSBjoern A. Zeeb static void
198550d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
198650d826beSBjoern A. Zeeb {
198750d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1988231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
198950d826beSBjoern A. Zeeb 
1990231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1991231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1992231168c7SBjoern A. Zeeb 
1993231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1994231168c7SBjoern A. Zeeb 		return;
1995231168c7SBjoern A. Zeeb 
199650d826beSBjoern A. Zeeb 	/* Remove vif context. */
1997231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
199850d826beSBjoern A. Zeeb 
199950d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
200050d826beSBjoern A. Zeeb 
200150d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
200250d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2003231168c7SBjoern A. Zeeb 
2004231168c7SBjoern A. Zeeb 	/* Cleanup. */
2005231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
200650d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2007a8a47a41SBjoern A. Zeeb 	list_del(&lchanctx->entry);
200850d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
200950d826beSBjoern A. Zeeb }
201050d826beSBjoern A. Zeeb 
201150d826beSBjoern A. Zeeb 
20126b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
20136b4cac81SBjoern A. Zeeb 
20146b4cac81SBjoern A. Zeeb static int
20156b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20166b4cac81SBjoern A. Zeeb {
20176b4cac81SBjoern A. Zeeb 
20186b4cac81SBjoern A. Zeeb 	return (0);
20196b4cac81SBjoern A. Zeeb }
20206b4cac81SBjoern A. Zeeb 
20216b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
20226b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
20236b4cac81SBjoern A. Zeeb 
20246b4cac81SBjoern A. Zeeb static int
20256b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20266b4cac81SBjoern A. Zeeb {
20276b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
2028c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
2029d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
20306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20326b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20336b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20346b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20356b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20366b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
20376b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
20386b4cac81SBjoern A. Zeeb 	uint32_t changed;
20396b4cac81SBjoern A. Zeeb 	int error;
204016d987feSBjoern A. Zeeb 	bool synched;
20416b4cac81SBjoern A. Zeeb 
20422ac8a218SBjoern A. Zeeb 	/*
20432ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
20442ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
20452ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
20462ac8a218SBjoern A. Zeeb 	 */
20472ac8a218SBjoern A. Zeeb 
20482ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
20492ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
20502ac8a218SBjoern A. Zeeb 		return (EINVAL);
20512ac8a218SBjoern A. Zeeb 	}
20520936c648SBjoern A. Zeeb 	/*
20530936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
20540936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
20550936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
20560936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
20570936c648SBjoern A. Zeeb 	 */
20582ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
20592ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
20602ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
20612ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
20620936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20632ac8a218SBjoern A. Zeeb 		return (EINVAL);
20646b4cac81SBjoern A. Zeeb 	}
20656b4cac81SBjoern A. Zeeb 
20666b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20672ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
20682ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
20692ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
20702ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
20710936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20722ac8a218SBjoern A. Zeeb 		return (ESRCH);
20732ac8a218SBjoern A. Zeeb 	}
20742ac8a218SBjoern A. Zeeb 
20756b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20766b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20776b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20786b4cac81SBjoern A. Zeeb 
20790936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20800936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
20810936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
20820936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20830936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20840936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20850936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20860936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
208792690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
208892690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20890936c648SBjoern A. Zeeb 		return (EBUSY);
20900936c648SBjoern A. Zeeb 	}
20910936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20920936c648SBjoern A. Zeeb 
20936b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2094cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
20956b4cac81SBjoern A. Zeeb 
20966b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
2097560708cbSBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2098560708cbSBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
2099560708cbSBjoern A. Zeeb 	if (chanctx_conf != NULL) {
2100d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
21016b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
21026b4cac81SBjoern A. Zeeb 	} else {
21036b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
2104c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
21056b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
2106d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
21076b4cac81SBjoern A. Zeeb 	}
21086b4cac81SBjoern A. Zeeb 
2109d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
2110389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
2111d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
21126b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2113d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
2114d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
211590d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
211690d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
21179fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
21182ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
21192ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
212062d51a43SBjoern A. Zeeb 
21219fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
21229fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
212390d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2124d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
212590d8e307SBjoern A. Zeeb 		else
2126d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
21279fb91463SBjoern A. Zeeb 	}
21289fb91463SBjoern A. Zeeb #endif
21299fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
21305393cd34SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
213190d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2132d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
213390d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2134d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
21354bf049bfSBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2136d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
21379fb91463SBjoern A. Zeeb 	}
21389fb91463SBjoern A. Zeeb #endif
2139389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
21406b4cac81SBjoern A. Zeeb 	/* Responder ... */
214190d8e307SBjoern A. Zeeb #if 0
214290d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2143d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
214490d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
214590d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
214690d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
214790d8e307SBjoern A. Zeeb #endif
214890d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
214990d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
215090d8e307SBjoern A. Zeeb #else
215190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
215290d8e307SBjoern A. Zeeb #endif
21536b4cac81SBjoern A. Zeeb 
21546ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
21556ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
21566ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
21576ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
21586ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
21596ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
21606ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
21616ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
21626ffb7bd4SBjoern A. Zeeb 
21636ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
21646ffb7bd4SBjoern A. Zeeb 
21656ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
21666ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
21676ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
21686ffb7bd4SBjoern A. Zeeb 
21696ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
21706ffb7bd4SBjoern A. Zeeb 
21716b4cac81SBjoern A. Zeeb 	error = 0;
2172560708cbSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
21736b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
21746b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
21756b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
21766b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2177d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
21786b4cac81SBjoern A. Zeeb 	} else {
2179d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
21806b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
21817b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
21827b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
21837b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
2184d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
21857b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
2186d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
21876b4cac81SBjoern A. Zeeb 		} else {
2188018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2189018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
21906b4cac81SBjoern A. Zeeb 			goto out;
21916b4cac81SBjoern A. Zeeb 		}
21926ffb7bd4SBjoern A. Zeeb 
2193a8a47a41SBjoern A. Zeeb 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2194560708cbSBjoern A. Zeeb 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
21956ffb7bd4SBjoern A. Zeeb 
21966b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
21976b4cac81SBjoern A. Zeeb 		if (error == 0)
219868541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2199d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
22006b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
22016b4cac81SBjoern A. Zeeb 			error = 0;
22026b4cac81SBjoern A. Zeeb 		if (error != 0) {
2203018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2204018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
2205d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2206560708cbSBjoern A. Zeeb 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2207d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2208a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
2209c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
22106b4cac81SBjoern A. Zeeb 			goto out;
22116b4cac81SBjoern A. Zeeb 		}
22126b4cac81SBjoern A. Zeeb 	}
22136b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
22146b4cac81SBjoern A. Zeeb 
22156b4cac81SBjoern A. Zeeb 	/* RATES */
22166b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
22176b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
22186b4cac81SBjoern A. Zeeb 
2219d9f59799SBjoern A. Zeeb 	/*
22200936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
22210936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
22220936c648SBjoern A. Zeeb 	 * under the hoods.
2223d9f59799SBjoern A. Zeeb 	 */
22240936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
22250936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
22266b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2227e24e8103SBjoern A. Zeeb 
22282ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
2229a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2230e24e8103SBjoern A. Zeeb 
2231d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
22326b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22336b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
22346b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2235e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
22366b4cac81SBjoern A. Zeeb 	if (error != 0) {
22376b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2238018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2239018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22406b4cac81SBjoern A. Zeeb 		goto out;
22416b4cac81SBjoern A. Zeeb 	}
22426b4cac81SBjoern A. Zeeb #if 0
22436b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
22446b4cac81SBjoern A. Zeeb #endif
22456b4cac81SBjoern A. Zeeb 
22469d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22479d9ba2b7SBjoern A. Zeeb 
22481665ef97SBjoern A. Zeeb #if 0
22496b4cac81SBjoern A. Zeeb 	/*
22506b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
22516b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
22526b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
22536b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2254d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2255d9f59799SBjoern A. Zeeb 	 * for all queues.
22566b4cac81SBjoern A. Zeeb 	 */
2257e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
22581665ef97SBjoern A. Zeeb #endif
22596b4cac81SBjoern A. Zeeb 
22606b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
22616b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22626b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
22636b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
22646b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
22656b4cac81SBjoern A. Zeeb 
22666b4cac81SBjoern A. Zeeb 	/*
22676b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
22686b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
22696b4cac81SBjoern A. Zeeb 	 * - event_callback
22706b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
22716b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
22726b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
22736b4cac81SBjoern A. Zeeb 	 */
22746b4cac81SBjoern A. Zeeb 
2275cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2276105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2277105b9df2SBjoern A. Zeeb 
2278105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2279105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2280105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2281105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2282105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2283105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2284105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2285105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2286105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2287105b9df2SBjoern A. Zeeb 
2288105b9df2SBjoern A. Zeeb 	/*
2289105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2290105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2291105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2292105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2293105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2294105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2295105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2296105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2297105b9df2SBjoern A. Zeeb 	 */
2298105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2299105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2300105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
230116d987feSBjoern A. Zeeb 		lvif->lvif_bss_synched = synched = true;
2302105b9df2SBjoern A. Zeeb 	} else {
2303105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
230416d987feSBjoern A. Zeeb 		lvif->lvif_bss_synched = synched = false;
2305105b9df2SBjoern A. Zeeb 		/*
2306105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2307105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2308105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2309105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2310105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2311105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2312105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2313105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
231416d987feSBjoern A. Zeeb 		 * Problem is if we do not error a MGMT/AUTH frame will be
231516d987feSBjoern A. Zeeb 		 * sent from net80211::sta_newstate(); disable lsta queue below.
2316105b9df2SBjoern A. Zeeb 		 */
2317105b9df2SBjoern A. Zeeb 	}
2318105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
231916d987feSBjoern A. Zeeb 	/*
232016d987feSBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-added it,
232116d987feSBjoern A. Zeeb 	 * that we can tx again but only if the vif/iv_bss are in sync.
232216d987feSBjoern A. Zeeb 	 * Otherwise this should prevent the MGMT/AUTH frame from being
232316d987feSBjoern A. Zeeb 	 * sent triggering a warning in iwlwifi.
232416d987feSBjoern A. Zeeb 	 */
232516d987feSBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
232616d987feSBjoern A. Zeeb 	lsta->txq_ready = synched;
232716d987feSBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2328105b9df2SBjoern A. Zeeb 	goto out_relocked;
2329105b9df2SBjoern A. Zeeb 
23306b4cac81SBjoern A. Zeeb out:
2331cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
23326b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2333105b9df2SBjoern A. Zeeb out_relocked:
23340936c648SBjoern A. Zeeb 	/*
2335105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
23360936c648SBjoern A. Zeeb 	 * during the work of this function.
23370936c648SBjoern A. Zeeb 	 */
23386b4cac81SBjoern A. Zeeb 	if (ni != NULL)
23396b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
23406b4cac81SBjoern A. Zeeb 	return (error);
23416b4cac81SBjoern A. Zeeb }
23426b4cac81SBjoern A. Zeeb 
23436b4cac81SBjoern A. Zeeb static int
23446b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23456b4cac81SBjoern A. Zeeb {
23466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23476b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23496b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23506b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23516b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23526b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
23536b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23546b4cac81SBjoern A. Zeeb 	int error;
23556b4cac81SBjoern A. Zeeb 
23566b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23576b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23586b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23596b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23606b4cac81SBjoern A. Zeeb 
23612ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23622ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23632ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23642ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23652ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23662ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23672ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23682ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23692ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23702ac8a218SBjoern A. Zeeb #endif
23712ac8a218SBjoern A. Zeeb 
23722ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23732ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23742ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
23752ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
23762ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
23772ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
23786b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
23796b4cac81SBjoern A. Zeeb 
238067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
23816b4cac81SBjoern A. Zeeb 
23826b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2383cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
23846b4cac81SBjoern A. Zeeb 
2385d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2386d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2387d9f59799SBjoern A. Zeeb 
23886b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
238988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
23906b4cac81SBjoern A. Zeeb 
23916b4cac81SBjoern A. Zeeb 	/* flush, no drop */
23926b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
23936b4cac81SBjoern A. Zeeb 
23946b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23956b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23966b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23976b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
23986b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23996b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24006b4cac81SBjoern A. Zeeb 	}
24016b4cac81SBjoern A. Zeeb 
24026b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
24036b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
24046b4cac81SBjoern A. Zeeb 
24056b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
24066b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2407d9f59799SBjoern A. Zeeb 
2408d9f59799SBjoern A. Zeeb 	/* Take the station down. */
24096b4cac81SBjoern A. Zeeb 
24106b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
24116b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
24126b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2413d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2414e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
24156b4cac81SBjoern A. Zeeb 	if (error != 0) {
24166b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2417018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2418018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24196b4cac81SBjoern A. Zeeb 		goto out;
24206b4cac81SBjoern A. Zeeb 	}
24216b4cac81SBjoern A. Zeeb #if 0
24226b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
24236b4cac81SBjoern A. Zeeb #endif
24246b4cac81SBjoern A. Zeeb 
242567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
24266b4cac81SBjoern A. Zeeb 
24272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24282ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24292ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24302ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2432d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24330936c648SBjoern A. Zeeb 	/*
24340936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24350936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24360936c648SBjoern A. Zeeb 	 * and potentially freed.
24370936c648SBjoern A. Zeeb 	 */
24380936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2439d9f59799SBjoern A. Zeeb 
2440d9f59799SBjoern A. Zeeb 	/* conf_tx */
2441d9f59799SBjoern A. Zeeb 
244250d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
24436b4cac81SBjoern A. Zeeb 
24446b4cac81SBjoern A. Zeeb out:
2445cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24466b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24476b4cac81SBjoern A. Zeeb 	return (error);
24486b4cac81SBjoern A. Zeeb }
24496b4cac81SBjoern A. Zeeb 
24506b4cac81SBjoern A. Zeeb static int
24516b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24526b4cac81SBjoern A. Zeeb {
24536b4cac81SBjoern A. Zeeb 	int error;
24546b4cac81SBjoern A. Zeeb 
24556b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
24566b4cac81SBjoern A. Zeeb 	if (error == 0)
24576b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
24586b4cac81SBjoern A. Zeeb 	return (error);
24596b4cac81SBjoern A. Zeeb }
24606b4cac81SBjoern A. Zeeb 
24616b4cac81SBjoern A. Zeeb static int
24626b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24636b4cac81SBjoern A. Zeeb {
24646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24656b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24676b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24686b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24696b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24706b4cac81SBjoern A. Zeeb 	int error;
24716b4cac81SBjoern A. Zeeb 
24726b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24736b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24746b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24756b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24766b4cac81SBjoern A. Zeeb 
24776b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2478cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24792ac8a218SBjoern A. Zeeb 
24802ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24812ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24822ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24832ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24842ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24852ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24862ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24872ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24882ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24892ac8a218SBjoern A. Zeeb #endif
24902ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24912ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24922ac8a218SBjoern A. Zeeb 		goto out;
24932ac8a218SBjoern A. Zeeb 	}
24942ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24952ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24962ac8a218SBjoern A. Zeeb 
24972ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
24986b4cac81SBjoern A. Zeeb 
24996b4cac81SBjoern A. Zeeb 	/* Finish auth. */
25006b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
25016b4cac81SBjoern A. Zeeb 
25026b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
25036b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
25046b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2505e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2506018d93ecSBjoern A. Zeeb 	if (error != 0) {
2507018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2508018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25096b4cac81SBjoern A. Zeeb 		goto out;
2510018d93ecSBjoern A. Zeeb 	}
25116b4cac81SBjoern A. Zeeb 
25126b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25136b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25146b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25156b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
25166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25176b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25186b4cac81SBjoern A. Zeeb 	}
25196b4cac81SBjoern A. Zeeb 
25206b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
25216b4cac81SBjoern A. Zeeb 
25226b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
25236b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
25246b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25256b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
25266b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
25276b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
25286b4cac81SBjoern A. Zeeb 	}
25296b4cac81SBjoern A. Zeeb 
25306b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
253188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
25326b4cac81SBjoern A. Zeeb 
25336b4cac81SBjoern A. Zeeb 	/*
25346b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
25356b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
25366b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
25376b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
25386b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
25396b4cac81SBjoern A. Zeeb 	 * - event_callback
25406b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
25416b4cac81SBjoern A. Zeeb 	 */
25426b4cac81SBjoern A. Zeeb 
25436b4cac81SBjoern A. Zeeb out:
2544cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
25456b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25466b4cac81SBjoern A. Zeeb 	return (error);
25476b4cac81SBjoern A. Zeeb }
25486b4cac81SBjoern A. Zeeb 
25496b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
25506b4cac81SBjoern A. Zeeb static int
25516b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25526b4cac81SBjoern A. Zeeb {
25536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25546b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25556b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25566b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25576b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25586b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
25592ac8a218SBjoern A. Zeeb 	int error;
25606b4cac81SBjoern A. Zeeb 
25616b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25626b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25636b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25646b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25656b4cac81SBjoern A. Zeeb 
25666b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2567cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
25682ac8a218SBjoern A. Zeeb 
25692ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25702ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
25712ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
25722ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25732ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25742ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25752ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25762ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25772ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25782ac8a218SBjoern A. Zeeb #endif
25792ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
25802ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
25812ac8a218SBjoern A. Zeeb 		goto out;
25822ac8a218SBjoern A. Zeeb 	}
25832ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25842ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25852ac8a218SBjoern A. Zeeb 
25862ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
25872ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
25886b4cac81SBjoern A. Zeeb 
25896b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
25906b4cac81SBjoern A. Zeeb 
25916b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25926b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25936b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25946b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
25956b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25966b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25976b4cac81SBjoern A. Zeeb 	}
25986b4cac81SBjoern A. Zeeb 
25996b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
26006b4cac81SBjoern A. Zeeb 
26016b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
26026b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
26036b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
26046b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
26056b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
26066b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
26076b4cac81SBjoern A. Zeeb 	}
26086b4cac81SBjoern A. Zeeb 
26092ac8a218SBjoern A. Zeeb 	error = 0;
26102ac8a218SBjoern A. Zeeb out:
2611cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
26126b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
26136b4cac81SBjoern A. Zeeb 
26142ac8a218SBjoern A. Zeeb 	return (error);
26156b4cac81SBjoern A. Zeeb }
26166b4cac81SBjoern A. Zeeb 
26176b4cac81SBjoern A. Zeeb static int
2618d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26196b4cac81SBjoern A. Zeeb {
26206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26246b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26256b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
26276b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2628d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
26296b4cac81SBjoern A. Zeeb 	int error;
26306b4cac81SBjoern A. Zeeb 
26316b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26326b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26336b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26346b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26356b4cac81SBjoern A. Zeeb 
26362ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2637cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
26382ac8a218SBjoern A. Zeeb 
26392ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26402ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26412ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26422ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26432ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26442ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26452ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26462ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26472ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
26482ac8a218SBjoern A. Zeeb #endif
26492ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26512ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26522ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26532ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
26542ac8a218SBjoern A. Zeeb 
26552ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
26566b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
26576b4cac81SBjoern A. Zeeb 
265867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2659d9f59799SBjoern A. Zeeb 
2660d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2661d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2662d9f59799SBjoern A. Zeeb 
2663d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2664d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2665d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2666d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2667d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2668ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2669d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2670d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2671d9f59799SBjoern A. Zeeb 	}
2672d9f59799SBjoern A. Zeeb 
2673cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2674d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2675d9f59799SBjoern A. Zeeb 
267688665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2677d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2678018d93ecSBjoern A. Zeeb 	if (error != 0) {
2679018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2680018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2681d9f59799SBjoern A. Zeeb 		goto outni;
2682018d93ecSBjoern A. Zeeb 	}
2683d9f59799SBjoern A. Zeeb 
2684d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
268588665349SBjoern A. Zeeb 
268688665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
268788665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
268888665349SBjoern A. Zeeb 
2689cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2690d9f59799SBjoern A. Zeeb 
269167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2692d9f59799SBjoern A. Zeeb 
2693d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
269488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2695d9f59799SBjoern A. Zeeb 
2696d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2697d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2698d9f59799SBjoern A. Zeeb 
26996b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
27006b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
27016b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
27026b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2703ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
27046b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
27056b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
27066b4cac81SBjoern A. Zeeb 	}
27076b4cac81SBjoern A. Zeeb 
2708d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2709d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2710d9f59799SBjoern A. Zeeb 
2711d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2712d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2713d9f59799SBjoern A. Zeeb 
2714d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2715d9f59799SBjoern A. Zeeb 
27166b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
27176b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27186b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
27196b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2720e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2721018d93ecSBjoern A. Zeeb 	if (error != 0) {
2722018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2723018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27246b4cac81SBjoern A. Zeeb 		goto out;
2725018d93ecSBjoern A. Zeeb 	}
27266b4cac81SBjoern A. Zeeb 
27275a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
27285a4d2461SBjoern A. Zeeb 	bss_changed = 0;
27295a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
27306b4cac81SBjoern A. Zeeb 
27315a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
273216e688b2SBjoern A. Zeeb 
2733d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2734d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2735d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2736d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2737e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2738d9f59799SBjoern A. Zeeb 	if (error != 0) {
2739d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2740018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2741018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2742d9f59799SBjoern A. Zeeb 		goto out;
2743d9f59799SBjoern A. Zeeb 	}
2744d9f59799SBjoern A. Zeeb 
274516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2746d9f59799SBjoern A. Zeeb 
2747d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2748d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2749d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2750616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2751616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2752d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2753d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2754d9f59799SBjoern A. Zeeb 
27552ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27562ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
27572ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
27582ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
27592ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2760d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
27610936c648SBjoern A. Zeeb 	/*
27620936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
27630936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
27640936c648SBjoern A. Zeeb 	 * and potentially freed.
27650936c648SBjoern A. Zeeb 	 */
27660936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2767d9f59799SBjoern A. Zeeb 
2768d9f59799SBjoern A. Zeeb 	/* conf_tx */
2769d9f59799SBjoern A. Zeeb 
277050d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2771d9f59799SBjoern A. Zeeb 
2772d9f59799SBjoern A. Zeeb 	error = EALREADY;
27736b4cac81SBjoern A. Zeeb out:
2774cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
27756b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2776d9f59799SBjoern A. Zeeb outni:
27776b4cac81SBjoern A. Zeeb 	return (error);
27786b4cac81SBjoern A. Zeeb }
27796b4cac81SBjoern A. Zeeb 
27806b4cac81SBjoern A. Zeeb static int
2781d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2782d9f59799SBjoern A. Zeeb {
2783d9f59799SBjoern A. Zeeb 	int error;
2784d9f59799SBjoern A. Zeeb 
2785d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2786d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2787d9f59799SBjoern A. Zeeb 		return (error);
2788d9f59799SBjoern A. Zeeb 
2789d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2790d9f59799SBjoern A. Zeeb 
2791d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2792d9f59799SBjoern A. Zeeb 	return (error);
2793d9f59799SBjoern A. Zeeb }
2794d9f59799SBjoern A. Zeeb 
2795d9f59799SBjoern A. Zeeb static int
27966b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27976b4cac81SBjoern A. Zeeb {
27986b4cac81SBjoern A. Zeeb 	int error;
27996b4cac81SBjoern A. Zeeb 
2800d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
28016b4cac81SBjoern A. Zeeb 	return (error);
28026b4cac81SBjoern A. Zeeb }
28036b4cac81SBjoern A. Zeeb 
28046b4cac81SBjoern A. Zeeb static int
28056b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28066b4cac81SBjoern A. Zeeb {
28076b4cac81SBjoern A. Zeeb 	int error;
28086b4cac81SBjoern A. Zeeb 
2809d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
28106b4cac81SBjoern A. Zeeb 	return (error);
28116b4cac81SBjoern A. Zeeb }
28126b4cac81SBjoern A. Zeeb 
28136b4cac81SBjoern A. Zeeb static int
28146b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28156b4cac81SBjoern A. Zeeb {
28166b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28176b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28186b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28196b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28206b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
28216b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28226b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28236b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
28246b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
28256b4cac81SBjoern A. Zeeb 	int error;
28266b4cac81SBjoern A. Zeeb 
28276b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
28286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28296b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28306b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28316b4cac81SBjoern A. Zeeb 
28326b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2833cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
28342ac8a218SBjoern A. Zeeb 
28352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28362ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
28372ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
28382ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28392ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28402ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28412ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28422ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28432ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28442ac8a218SBjoern A. Zeeb #endif
28452ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
28462ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
28472ac8a218SBjoern A. Zeeb 		goto out;
28482ac8a218SBjoern A. Zeeb 	}
28492ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28512ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28522ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28532ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28542ac8a218SBjoern A. Zeeb 
28552ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
28566b4cac81SBjoern A. Zeeb 
28576b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
28586b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
28596b4cac81SBjoern A. Zeeb 
28609597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
28619597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
28629597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
28639597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
28646b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
28659597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
28664a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
28674a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
28684a67f1dfSBjoern A. Zeeb 		sta->wme = true;
28694a67f1dfSBjoern A. Zeeb #endif
2870e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2871018d93ecSBjoern A. Zeeb 	if (error != 0) {
2872018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2873018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28749597f7cbSBjoern A. Zeeb 		goto out;
2875018d93ecSBjoern A. Zeeb 	}
28766b4cac81SBjoern A. Zeeb 
28776b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
28786b4cac81SBjoern A. Zeeb 
28796b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
28806b4cac81SBjoern A. Zeeb 	bss_changed = 0;
28814a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
28824a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
28834a67f1dfSBjoern A. Zeeb #endif
2884616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2885616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2886616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
28876b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
28886b4cac81SBjoern A. Zeeb 	}
28896b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2890616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2891616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
28926b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
28936b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
28946b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
28956b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
28966b4cac81SBjoern A. Zeeb 	}
28976b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
28986b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
28996b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
29006b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
29016b4cac81SBjoern A. Zeeb 	}
29026b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
29036b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
29046b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
29056b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
29066b4cac81SBjoern A. Zeeb 	}
2907fa8f007dSBjoern A. Zeeb 
2908fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
29096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
29106b4cac81SBjoern A. Zeeb 
29116b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
29126b4cac81SBjoern A. Zeeb 	 * - event_callback
29136b4cac81SBjoern A. Zeeb 	 */
29146b4cac81SBjoern A. Zeeb 
29156b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
29166b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
29176b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
29186b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
29196b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
29206b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
29216b4cac81SBjoern A. Zeeb 	}
29226b4cac81SBjoern A. Zeeb 
2923086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2924086be6a8SBjoern A. Zeeb 
29256b4cac81SBjoern A. Zeeb 	/*
29266b4cac81SBjoern A. Zeeb 	 * And then:
29276b4cac81SBjoern A. Zeeb 	 * - (more packets)?
29286b4cac81SBjoern A. Zeeb 	 * - set_key
29296b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
29306b4cac81SBjoern A. Zeeb 	 * - set_key (?)
29316b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
29326b4cac81SBjoern A. Zeeb 	 */
29336b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
29346b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
29356b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
29366b4cac81SBjoern A. Zeeb 
29376b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
29386b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
29396b4cac81SBjoern A. Zeeb 	}
29406b4cac81SBjoern A. Zeeb 
2941f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
29429fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
294362d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
29449fb91463SBjoern A. Zeeb 
29456b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
29466b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
29476b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
29486b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2949e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
29506b4cac81SBjoern A. Zeeb 	if (error != 0) {
29516b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2952018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2953018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
29546b4cac81SBjoern A. Zeeb 		goto out;
29556b4cac81SBjoern A. Zeeb 	}
29566b4cac81SBjoern A. Zeeb 
29576b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
29586b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
29596b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
29606b4cac81SBjoern A. Zeeb 	 *
29616b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
29626b4cac81SBjoern A. Zeeb 	 */
29636b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
29646b4cac81SBjoern A. Zeeb 
2965fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2966fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2967fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2968fa8f007dSBjoern A. Zeeb 
29696b4cac81SBjoern A. Zeeb out:
2970cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
29716b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
29726b4cac81SBjoern A. Zeeb 	return (error);
29736b4cac81SBjoern A. Zeeb }
29746b4cac81SBjoern A. Zeeb 
29756b4cac81SBjoern A. Zeeb static int
29766b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29776b4cac81SBjoern A. Zeeb {
29786b4cac81SBjoern A. Zeeb 	int error;
29796b4cac81SBjoern A. Zeeb 
29806b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
29816b4cac81SBjoern A. Zeeb 	if (error == 0)
29826b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
29836b4cac81SBjoern A. Zeeb 	return (error);
29846b4cac81SBjoern A. Zeeb }
29856b4cac81SBjoern A. Zeeb 
29866b4cac81SBjoern A. Zeeb static int
29876b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29886b4cac81SBjoern A. Zeeb {
29896b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29906b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29916b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29926b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29936b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
29946b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
29956b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2996d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2997d9f59799SBjoern A. Zeeb #if 0
2998d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2999d9f59799SBjoern A. Zeeb #endif
30006b4cac81SBjoern A. Zeeb 	int error;
30016b4cac81SBjoern A. Zeeb 
30026b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
30036b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
30046b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
30056b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
30066b4cac81SBjoern A. Zeeb 
30072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30082ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30092ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
30102ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
30112ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
30122ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
30132ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
30142ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
30152ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
30162ac8a218SBjoern A. Zeeb #endif
30172ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
30182ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30192ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
30202ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
30212ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
30222ac8a218SBjoern A. Zeeb 
30232ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
30246b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
30256b4cac81SBjoern A. Zeeb 
302667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3027d9f59799SBjoern A. Zeeb 
3028d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3029cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3030d9f59799SBjoern A. Zeeb 
3031d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3032d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3033d9f59799SBjoern A. Zeeb 
3034d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3035d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3036d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3037d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3038d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3039ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3040d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3041d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3042d9f59799SBjoern A. Zeeb 	}
3043d9f59799SBjoern A. Zeeb 
3044cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3045d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3046d9f59799SBjoern A. Zeeb 
3047d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3048d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3049018d93ecSBjoern A. Zeeb 	if (error != 0) {
3050018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3051018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3052d9f59799SBjoern A. Zeeb 		goto outni;
3053018d93ecSBjoern A. Zeeb 	}
3054d9f59799SBjoern A. Zeeb 
3055d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
305688665349SBjoern A. Zeeb 
305788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
305888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
305988665349SBjoern A. Zeeb 
3060cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3061d9f59799SBjoern A. Zeeb 
306267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3063d9f59799SBjoern A. Zeeb 
3064d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
306588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3066d9f59799SBjoern A. Zeeb 
3067d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3068d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3069d9f59799SBjoern A. Zeeb 
3070d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3071d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3072d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3073d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3074ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3075d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3076d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3077d9f59799SBjoern A. Zeeb 	}
3078d9f59799SBjoern A. Zeeb 
3079d9f59799SBjoern A. Zeeb #if 0
3080d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3081d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3082d9f59799SBjoern A. Zeeb 
3083d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3084d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3085d9f59799SBjoern A. Zeeb #endif
3086d9f59799SBjoern A. Zeeb 
3087d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3088d9f59799SBjoern A. Zeeb 
30896b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
30906b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30916b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
30926b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3093e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3094018d93ecSBjoern A. Zeeb 	if (error != 0) {
3095018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3096018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
30976b4cac81SBjoern A. Zeeb 		goto out;
3098018d93ecSBjoern A. Zeeb 	}
30996b4cac81SBjoern A. Zeeb 
310067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
31016b4cac81SBjoern A. Zeeb 
310211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
310311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
310411db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
310511db70b6SBjoern A. Zeeb 		if (error != 0) {
310611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
310711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
310811db70b6SBjoern A. Zeeb 			/*
310911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
311011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
311111db70b6SBjoern A. Zeeb 			 * less appropriate time).
311211db70b6SBjoern A. Zeeb 			 */
311311db70b6SBjoern A. Zeeb 			/* goto out; */
311411db70b6SBjoern A. Zeeb 		}
311511db70b6SBjoern A. Zeeb 	}
311611db70b6SBjoern A. Zeeb #endif
311711db70b6SBjoern A. Zeeb 
31186b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
31196b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
31206b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
31216b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3122e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3123018d93ecSBjoern A. Zeeb 	if (error != 0) {
3124018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3125018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
31266b4cac81SBjoern A. Zeeb 		goto out;
3127018d93ecSBjoern A. Zeeb 	}
31286b4cac81SBjoern A. Zeeb 
312967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
31306b4cac81SBjoern A. Zeeb 
3131d9f59799SBjoern A. Zeeb #if 0
3132d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
3133d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
3134d9f59799SBjoern A. Zeeb #endif
3135d9f59799SBjoern A. Zeeb 
3136d9f59799SBjoern A. Zeeb 	error = EALREADY;
31376b4cac81SBjoern A. Zeeb out:
3138cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
31396b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3140d9f59799SBjoern A. Zeeb outni:
31416b4cac81SBjoern A. Zeeb 	return (error);
31426b4cac81SBjoern A. Zeeb }
31436b4cac81SBjoern A. Zeeb 
31446b4cac81SBjoern A. Zeeb static int
3145d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3146d9f59799SBjoern A. Zeeb {
3147d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3148d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3149d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
3150d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3151d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
3152d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
3153d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3154d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
3155d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
3156d9f59799SBjoern A. Zeeb 	int error;
3157d9f59799SBjoern A. Zeeb 
3158d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
3159d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3160d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3161d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3162d9f59799SBjoern A. Zeeb 
31632ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3164cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
31652ac8a218SBjoern A. Zeeb 
31662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
31672ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31682ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
31692ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
31702ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
31712ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
31722ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
31732ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
31742ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
31752ac8a218SBjoern A. Zeeb #endif
31762ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
31772ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
31782ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
31792ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
31802ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
31812ac8a218SBjoern A. Zeeb 
31822ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3183d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3184d9f59799SBjoern A. Zeeb 
318567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3186d9f59799SBjoern A. Zeeb 
3187d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3188d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3189d9f59799SBjoern A. Zeeb 
3190d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3191d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3192d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3193d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3194d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3195ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3196d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3197d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3198d9f59799SBjoern A. Zeeb 	}
3199d9f59799SBjoern A. Zeeb 
3200cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3201d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3202d9f59799SBjoern A. Zeeb 
3203d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3204d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3205018d93ecSBjoern A. Zeeb 	if (error != 0) {
3206018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3207018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3208d9f59799SBjoern A. Zeeb 		goto outni;
3209018d93ecSBjoern A. Zeeb 	}
3210d9f59799SBjoern A. Zeeb 
3211d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
321288665349SBjoern A. Zeeb 
321388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
321488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
321588665349SBjoern A. Zeeb 
3216cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3217d9f59799SBjoern A. Zeeb 
321867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3219d9f59799SBjoern A. Zeeb 
3220d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
322188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3222d9f59799SBjoern A. Zeeb 
3223d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3224d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3225d9f59799SBjoern A. Zeeb 
3226d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3227d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3228d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3229d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3230ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3231d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3232d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3233d9f59799SBjoern A. Zeeb 	}
3234d9f59799SBjoern A. Zeeb 
3235d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3236d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3237d9f59799SBjoern A. Zeeb 
3238d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3239d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3240d9f59799SBjoern A. Zeeb 
3241d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3242d9f59799SBjoern A. Zeeb 
3243d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3244d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3245d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3246d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3247e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3248018d93ecSBjoern A. Zeeb 	if (error != 0) {
3249018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3250018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3251d9f59799SBjoern A. Zeeb 		goto out;
3252018d93ecSBjoern A. Zeeb 	}
3253d9f59799SBjoern A. Zeeb 
325467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3255d9f59799SBjoern A. Zeeb 
325611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
325711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
325811db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
325911db70b6SBjoern A. Zeeb 		if (error != 0) {
326011db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
326111db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
326211db70b6SBjoern A. Zeeb 			/*
326311db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
326411db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
326511db70b6SBjoern A. Zeeb 			 * less appropriate time).
326611db70b6SBjoern A. Zeeb 			 */
326711db70b6SBjoern A. Zeeb 			/* goto out; */
326811db70b6SBjoern A. Zeeb 		}
326911db70b6SBjoern A. Zeeb 	}
327011db70b6SBjoern A. Zeeb #endif
327111db70b6SBjoern A. Zeeb 
3272d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3273d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3274d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3275d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3276e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3277018d93ecSBjoern A. Zeeb 	if (error != 0) {
3278018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3279018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3280d9f59799SBjoern A. Zeeb 		goto out;
3281018d93ecSBjoern A. Zeeb 	}
3282d9f59799SBjoern A. Zeeb 
328367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3284d9f59799SBjoern A. Zeeb 
3285d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3286d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3287d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3288d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3289e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3290018d93ecSBjoern A. Zeeb 	if (error != 0) {
3291018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3292018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3293d9f59799SBjoern A. Zeeb 		goto out;
3294018d93ecSBjoern A. Zeeb 	}
3295d9f59799SBjoern A. Zeeb 
32965a4d2461SBjoern A. Zeeb 	bss_changed = 0;
329716e688b2SBjoern A. Zeeb 	/*
32985a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
32995a4d2461SBjoern A. Zeeb 	 *
330016e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
33015a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
33025a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
33035a4d2461SBjoern A. Zeeb 	 *
33045a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
33055a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
33065a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
33075a4d2461SBjoern A. Zeeb 	 * a fw assert.
33085a4d2461SBjoern A. Zeeb 	 *
33095a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
33105a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
33115a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
33125a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
33135a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
33145a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
33155a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
33165a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
33175a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
33185a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
331916e688b2SBjoern A. Zeeb 	 */
33205a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
33215a4d2461SBjoern A. Zeeb 
33225a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
332316e688b2SBjoern A. Zeeb 
3324d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3325d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3326d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3327d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3328e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3329d9f59799SBjoern A. Zeeb 	if (error != 0) {
3330d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3331018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3332018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3333d9f59799SBjoern A. Zeeb 		goto out;
3334d9f59799SBjoern A. Zeeb 	}
3335d9f59799SBjoern A. Zeeb 
33365a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
33375a4d2461SBjoern A. Zeeb 
333816e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3339d9f59799SBjoern A. Zeeb 
3340d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3341d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3342d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3343616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3344616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3345d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
33465a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
33475a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
33485a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3349d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3350d9f59799SBjoern A. Zeeb 
33512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
33522ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
33532ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
33542ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33552ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
33560936c648SBjoern A. Zeeb 	/*
33570936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
33580936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
33590936c648SBjoern A. Zeeb 	 * and potentially freed.
33600936c648SBjoern A. Zeeb 	 */
33610936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3362d9f59799SBjoern A. Zeeb 
3363d9f59799SBjoern A. Zeeb 	/* conf_tx */
3364d9f59799SBjoern A. Zeeb 
336550d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3366d9f59799SBjoern A. Zeeb 
3367d9f59799SBjoern A. Zeeb 	error = EALREADY;
3368d9f59799SBjoern A. Zeeb out:
3369cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3370d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3371d9f59799SBjoern A. Zeeb outni:
3372d9f59799SBjoern A. Zeeb 	return (error);
3373d9f59799SBjoern A. Zeeb }
3374d9f59799SBjoern A. Zeeb 
3375d9f59799SBjoern A. Zeeb static int
3376d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3377d9f59799SBjoern A. Zeeb {
3378d9f59799SBjoern A. Zeeb 
3379d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3380d9f59799SBjoern A. Zeeb }
3381d9f59799SBjoern A. Zeeb 
3382d9f59799SBjoern A. Zeeb static int
33836b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
33846b4cac81SBjoern A. Zeeb {
33856b4cac81SBjoern A. Zeeb 	int error;
33866b4cac81SBjoern A. Zeeb 
3387d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3388d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3389d9f59799SBjoern A. Zeeb 		return (error);
3390d9f59799SBjoern A. Zeeb 
3391d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3392d9f59799SBjoern A. Zeeb 
3393d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
33946b4cac81SBjoern A. Zeeb 	return (error);
33956b4cac81SBjoern A. Zeeb }
33966b4cac81SBjoern A. Zeeb 
3397d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
33986b4cac81SBjoern A. Zeeb 
33996b4cac81SBjoern A. Zeeb /*
34006b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
34016b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
34026b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
34036b4cac81SBjoern A. Zeeb  */
34046b4cac81SBjoern A. Zeeb struct fsm_state {
34056b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
34066b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
34076b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
34086b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
34096b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
34106b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
34116b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
34126b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3413d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3414d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
34156b4cac81SBjoern A. Zeeb 
34166b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
34176b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
34186b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
34196b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3420d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
34216b4cac81SBjoern A. Zeeb 
3422d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3423d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3424d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3425d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3426d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
34276b4cac81SBjoern A. Zeeb 
3428d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3429d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3430d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
34316b4cac81SBjoern A. Zeeb 
34326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
34336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
34346b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
34356b4cac81SBjoern A. Zeeb 
34366b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
34376b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
34386b4cac81SBjoern A. Zeeb };
34396b4cac81SBjoern A. Zeeb 
34406b4cac81SBjoern A. Zeeb static int
34416b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
34426b4cac81SBjoern A. Zeeb {
34436b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34466b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34476b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
34486b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
34496b4cac81SBjoern A. Zeeb 	int error;
34506b4cac81SBjoern A. Zeeb 
34516b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
34526b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
34536b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
34546b4cac81SBjoern A. Zeeb 
34559d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34569d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
34576b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
34586b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
34599d9ba2b7SBjoern A. Zeeb #endif
34606b4cac81SBjoern A. Zeeb 
34616b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
34626b4cac81SBjoern A. Zeeb 
34636b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
34646b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
34656b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
34666b4cac81SBjoern A. Zeeb 
34676b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
34686b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
34696b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
34706b4cac81SBjoern A. Zeeb 
34716b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
34726b4cac81SBjoern A. Zeeb 
34736b4cac81SBjoern A. Zeeb 	} else {
34746b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
34756b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
34766b4cac81SBjoern A. Zeeb 		return (ENOSYS);
34776b4cac81SBjoern A. Zeeb 	}
34786b4cac81SBjoern A. Zeeb 
34796b4cac81SBjoern A. Zeeb 	error = 0;
34806b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
34816b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
34829d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34839d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3484d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3485d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3486d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3487d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
34889d9ba2b7SBjoern A. Zeeb #endif
34896b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
34906b4cac81SBjoern A. Zeeb 			break;
34916b4cac81SBjoern A. Zeeb 		}
34926b4cac81SBjoern A. Zeeb 	}
34936b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
34946b4cac81SBjoern A. Zeeb 
34956b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3496d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
34976b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
34986b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
34996b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
35006b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
35016b4cac81SBjoern A. Zeeb 		return (ENOSYS);
35026b4cac81SBjoern A. Zeeb 	}
35036b4cac81SBjoern A. Zeeb 
35046b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
35059d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35069d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3507d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3508d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3509d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3510d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
35119d9ba2b7SBjoern A. Zeeb #endif
35126b4cac81SBjoern A. Zeeb 		return (0);
35136b4cac81SBjoern A. Zeeb 	}
35146b4cac81SBjoern A. Zeeb 
35156b4cac81SBjoern A. Zeeb 	if (error != 0) {
35166b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
35176b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
35186b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
35196b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
352045c27ad5SBjoern A. Zeeb 		return (error);
35216b4cac81SBjoern A. Zeeb 	}
35226b4cac81SBjoern A. Zeeb 
35239d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35249d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3525d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3526d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
35276b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
35289d9ba2b7SBjoern A. Zeeb #endif
35296b4cac81SBjoern A. Zeeb 
35306b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
35316b4cac81SBjoern A. Zeeb }
35326b4cac81SBjoern A. Zeeb 
35336b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
35346b4cac81SBjoern A. Zeeb 
3535d9f59799SBjoern A. Zeeb /*
3536d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3537d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3538d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3539d9f59799SBjoern A. Zeeb  */
3540d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3541d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3542d9f59799SBjoern A. Zeeb {
3543d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35442ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3545d9f59799SBjoern A. Zeeb 
35462ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3547d9f59799SBjoern A. Zeeb 
3548ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35492ac8a218SBjoern A. Zeeb 
35502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
35512ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
35522ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
35532ac8a218SBjoern A. Zeeb 
35542ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
35552ac8a218SBjoern A. Zeeb 	return (rni);
3556d9f59799SBjoern A. Zeeb }
3557d9f59799SBjoern A. Zeeb 
35584a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
35596b4cac81SBjoern A. Zeeb static int
35604a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
35616b4cac81SBjoern A. Zeeb {
35624a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
35636b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35646b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35656b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35666b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
35676b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
35686b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
35696b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
35706b4cac81SBjoern A. Zeeb 	int error;
35716b4cac81SBjoern A. Zeeb 	uint16_t ac;
35726b4cac81SBjoern A. Zeeb 
3573cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3574cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
3575cd0fcf9fSBjoern A. Zeeb 
35766b4cac81SBjoern A. Zeeb 	IMPROVE();
35776b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
35786b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
35796b4cac81SBjoern A. Zeeb 
35806b4cac81SBjoern A. Zeeb 	if (vap == NULL)
35816b4cac81SBjoern A. Zeeb 		return (0);
35826b4cac81SBjoern A. Zeeb 
35834a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
35844a67f1dfSBjoern A. Zeeb 		return (0);
35854a67f1dfSBjoern A. Zeeb 
35866b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
35876b4cac81SBjoern A. Zeeb 		return (0);
35886b4cac81SBjoern A. Zeeb 
35894a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
35904a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
35914a67f1dfSBjoern A. Zeeb 		return (0);
35924a67f1dfSBjoern A. Zeeb 	}
35934a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
35946b4cac81SBjoern A. Zeeb 
35954a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
35966b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
35976b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
35986b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
35996b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
36006b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
36016b4cac81SBjoern A. Zeeb 
36024a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
36034a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36044a67f1dfSBjoern A. Zeeb 
36056b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
36066b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
36076b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
36086b4cac81SBjoern A. Zeeb 
36096b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
36106b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
36116b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
36126b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
36136b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
36146b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
361568541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
36166b4cac81SBjoern A. Zeeb 		if (error != 0)
36173f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
36186b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
36196b4cac81SBjoern A. Zeeb 	}
36206b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
36214a67f1dfSBjoern A. Zeeb 	if (!planned)
36226b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
36234a67f1dfSBjoern A. Zeeb 
36244a67f1dfSBjoern A. Zeeb 	return (changed);
36254a67f1dfSBjoern A. Zeeb }
36266b4cac81SBjoern A. Zeeb #endif
36276b4cac81SBjoern A. Zeeb 
36284a67f1dfSBjoern A. Zeeb static int
36294a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
36304a67f1dfSBjoern A. Zeeb {
36314a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
36324a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
36334a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
3634cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
36354a67f1dfSBjoern A. Zeeb 
36364a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
36374a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
36384a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
36396b4cac81SBjoern A. Zeeb 		return (0);
36404a67f1dfSBjoern A. Zeeb 
36414a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
3642cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36434a67f1dfSBjoern A. Zeeb 
3644cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
36454a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
3646cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36474a67f1dfSBjoern A. Zeeb #endif
36484a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
36496b4cac81SBjoern A. Zeeb }
36506b4cac81SBjoern A. Zeeb 
36514aff4048SBjoern A. Zeeb /*
36524aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
36534aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
36544aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
36554aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
36564aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
36574aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3658edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3659edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3660edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3661edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
36624aff4048SBjoern A. Zeeb  */
36634aff4048SBjoern A. Zeeb static void
36644aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
36654aff4048SBjoern A. Zeeb {
36664aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
36674aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36684aff4048SBjoern A. Zeeb 
36694aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3670edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3671edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3672edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
36734aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
36744aff4048SBjoern A. Zeeb 		return;
36754aff4048SBjoern A. Zeeb 	}
36764aff4048SBjoern A. Zeeb 
36774aff4048SBjoern A. Zeeb 	vif = arg;
367857609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
36794aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
36804aff4048SBjoern A. Zeeb }
36814aff4048SBjoern A. Zeeb 
36826b4cac81SBjoern A. Zeeb static struct ieee80211vap *
36836b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
36846b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
36856b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
36866b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
36876b4cac81SBjoern A. Zeeb {
36886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36896b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36906b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36916b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36926b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3693a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
36946b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
369540839418SBjoern A. Zeeb 	struct sysctl_oid *node;
36966b4cac81SBjoern A. Zeeb 	size_t len;
3697e3a0b120SBjoern A. Zeeb 	int error, i;
3698a6042e17SBjoern A. Zeeb 	uint16_t ac;
36996b4cac81SBjoern A. Zeeb 
37006b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
37016b4cac81SBjoern A. Zeeb 		return (NULL);
37026b4cac81SBjoern A. Zeeb 
37036b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37056b4cac81SBjoern A. Zeeb 
37066b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
37076b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
37086b4cac81SBjoern A. Zeeb 
37096b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
37106b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3711a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
37122ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3713b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
37142ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
37156b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
37166b4cac81SBjoern A. Zeeb 
37176b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
37186b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
37196b4cac81SBjoern A. Zeeb 	vif->p2p = false;
37206b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
37216b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
37226b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
37236b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
37246b4cac81SBjoern A. Zeeb 	IMPROVE();
37256b4cac81SBjoern A. Zeeb 
37266b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
37276b4cac81SBjoern A. Zeeb #if 1
3728560708cbSBjoern A. Zeeb 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3729adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
37306ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
37316ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
37324aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
37334aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
37346ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
37357b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
37366b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
37376b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
37386b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
37396b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
37406b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3741616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3742616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3743616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3744616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
37456ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3746caaa79c3SBjoern A. Zeeb 	/*
3747caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3748caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3749caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3750caaa79c3SBjoern A. Zeeb 	 */
37516ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
37526b4cac81SBjoern A. Zeeb #endif
37536b4cac81SBjoern A. Zeeb #if 0
37546b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
37556b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
37566b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
37576b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
37586b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
37596b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
37606b4cac81SBjoern A. Zeeb #endif
3761e3a0b120SBjoern A. Zeeb 
37626ffb7bd4SBjoern A. Zeeb 	/* Link Config */
37636ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
37646ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
37656ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
37666ffb7bd4SBjoern A. Zeeb 	}
37676ffb7bd4SBjoern A. Zeeb 
3768e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3769e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3770e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3771e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3772e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3773e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3774e3a0b120SBjoern A. Zeeb 		else
3775e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
37760cbcfa19SBjoern A. Zeeb 
37770cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
37780cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3779e3a0b120SBjoern A. Zeeb 	}
3780e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3781e3a0b120SBjoern A. Zeeb 
37826b4cac81SBjoern A. Zeeb 	IMPROVE();
37836b4cac81SBjoern A. Zeeb 
37846b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
37856b4cac81SBjoern A. Zeeb 	if (error != 0) {
37863f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
37876b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37886b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37896b4cac81SBjoern A. Zeeb 		return (NULL);
37906b4cac81SBjoern A. Zeeb 	}
37916b4cac81SBjoern A. Zeeb 
37926b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
37936b4cac81SBjoern A. Zeeb 	if (error != 0) {
37946b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
37953f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
37966b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37976b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37986b4cac81SBjoern A. Zeeb 		return (NULL);
37996b4cac81SBjoern A. Zeeb 	}
38006b4cac81SBjoern A. Zeeb 
38018891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
38026b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
38038891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
38046b4cac81SBjoern A. Zeeb 
38056b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
38066b4cac81SBjoern A. Zeeb 	changed = 0;
38076b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
38086b4cac81SBjoern A. Zeeb 
3809a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3810a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3811cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3812a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3813a6042e17SBjoern A. Zeeb 
3814a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3815a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3816a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3817a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3818a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3819a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3820a6042e17SBjoern A. Zeeb 		if (error != 0)
3821a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3822a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3823a6042e17SBjoern A. Zeeb 	}
3824cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3825a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3826a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
38276b4cac81SBjoern A. Zeeb 
38286b4cac81SBjoern A. Zeeb 	/* Force MC init. */
38296b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
38306b4cac81SBjoern A. Zeeb 
38316b4cac81SBjoern A. Zeeb 	IMPROVE();
38326b4cac81SBjoern A. Zeeb 
38336b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
38346b4cac81SBjoern A. Zeeb 
38356b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
38366b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
38376b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3838d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3839d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
38406b4cac81SBjoern A. Zeeb 
3841b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
384211db70b6SBjoern A. Zeeb 	/* Key management. */
384311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
38446b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
38456b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3846b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3847b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
38486b4cac81SBjoern A. Zeeb 	}
384911db70b6SBjoern A. Zeeb #endif
38506b4cac81SBjoern A. Zeeb 
38519fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38529fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
38539fb91463SBjoern A. Zeeb #endif
38549fb91463SBjoern A. Zeeb 
38556b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
38566b4cac81SBjoern A. Zeeb 
38576b4cac81SBjoern A. Zeeb 	/* Complete setup. */
38586b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
38596b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
38606b4cac81SBjoern A. Zeeb 
386111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
386211db70b6SBjoern A. Zeeb 	/*
386311db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
386411db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
386511db70b6SBjoern A. Zeeb 	 */
386611db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
386711db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
386811db70b6SBjoern A. Zeeb #endif
3869ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3870ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3871ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3872ac2c7271SBjoern A. Zeeb #endif
387311db70b6SBjoern A. Zeeb 
38746b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
38756b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
38766b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
38776b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
38786b4cac81SBjoern A. Zeeb 
38796b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
38806b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
38816b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
38826b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
38836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
38846b4cac81SBjoern A. Zeeb 	/* any others? */
388540839418SBjoern A. Zeeb 
388640839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
388740839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
388840839418SBjoern A. Zeeb 
388940839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
389040839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
389140839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
389240839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
389340839418SBjoern A. Zeeb 
389440839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
389540839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
389640839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
389740839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
389840839418SBjoern A. Zeeb 
38996b4cac81SBjoern A. Zeeb 	IMPROVE();
39006b4cac81SBjoern A. Zeeb 
39016b4cac81SBjoern A. Zeeb 	return (vap);
39026b4cac81SBjoern A. Zeeb }
39036b4cac81SBjoern A. Zeeb 
39044b0af114SBjoern A. Zeeb void
39054b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
39064b0af114SBjoern A. Zeeb {
39074b0af114SBjoern A. Zeeb 
39084b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
39094b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
39104b0af114SBjoern A. Zeeb 
39114b0af114SBjoern A. Zeeb 	IMPROVE();
39124b0af114SBjoern A. Zeeb }
39134b0af114SBjoern A. Zeeb 
39144b0af114SBjoern A. Zeeb void
39154b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
39164b0af114SBjoern A. Zeeb {
39174b0af114SBjoern A. Zeeb 
39184b0af114SBjoern A. Zeeb 	TODO();
39194b0af114SBjoern A. Zeeb }
39204b0af114SBjoern A. Zeeb 
39216b4cac81SBjoern A. Zeeb static void
39226b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
39236b4cac81SBjoern A. Zeeb {
39246b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
39256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39266b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39286b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39296b4cac81SBjoern A. Zeeb 
39306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
39316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
39326b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
39336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39356b4cac81SBjoern A. Zeeb 
39364aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
39374aff4048SBjoern A. Zeeb 
393840839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
393940839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
394040839418SBjoern A. Zeeb 
39418891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
39426b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
39438891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
39446b4cac81SBjoern A. Zeeb 
39456b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
39466b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3947dbf76919SBjoern A. Zeeb 
3948dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3949dbf76919SBjoern A. Zeeb 
3950dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3951dbf76919SBjoern A. Zeeb 
39526c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
39537b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
39546c38c6b1SBjoern A. Zeeb 
39556b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
39566b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
39576b4cac81SBjoern A. Zeeb }
39586b4cac81SBjoern A. Zeeb 
39596b4cac81SBjoern A. Zeeb static void
39606b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
39616b4cac81SBjoern A. Zeeb {
39626b4cac81SBjoern A. Zeeb 
39636b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
39646b4cac81SBjoern A. Zeeb 	TRACEOK();
39656b4cac81SBjoern A. Zeeb }
39666b4cac81SBjoern A. Zeeb 
39676b4cac81SBjoern A. Zeeb static void
39686b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
39696b4cac81SBjoern A. Zeeb {
39706b4cac81SBjoern A. Zeeb 
39716b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
39726b4cac81SBjoern A. Zeeb }
39736b4cac81SBjoern A. Zeeb 
39746b4cac81SBjoern A. Zeeb static void
39756b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
39766b4cac81SBjoern A. Zeeb {
39776b4cac81SBjoern A. Zeeb 
39786b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
39796b4cac81SBjoern A. Zeeb }
39806b4cac81SBjoern A. Zeeb 
39816b4cac81SBjoern A. Zeeb /* Start / stop device. */
39826b4cac81SBjoern A. Zeeb static void
39836b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
39846b4cac81SBjoern A. Zeeb {
39856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39866b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3987cd0fcf9fSBjoern A. Zeeb #ifdef HW_START_STOP
39886b4cac81SBjoern A. Zeeb 	int error;
39898d58a057SBjoern A. Zeeb #endif
39906b4cac81SBjoern A. Zeeb 	bool start_all;
39916b4cac81SBjoern A. Zeeb 
39926b4cac81SBjoern A. Zeeb 	IMPROVE();
39936b4cac81SBjoern A. Zeeb 
39946b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39956b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39966b4cac81SBjoern A. Zeeb 	start_all = false;
39976b4cac81SBjoern A. Zeeb 
39988ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
3999cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
40006b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
40018d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
40026b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
40036b4cac81SBjoern A. Zeeb 		if (error == 0)
40048d58a057SBjoern A. Zeeb #endif
40056b4cac81SBjoern A. Zeeb 			start_all = true;
40066b4cac81SBjoern A. Zeeb 	} else {
40078d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
40087b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
40098d58a057SBjoern A. Zeeb #endif
40106b4cac81SBjoern A. Zeeb 	}
4011cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
40128ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
40136b4cac81SBjoern A. Zeeb 
40146b4cac81SBjoern A. Zeeb 	if (start_all)
40156b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
40166b4cac81SBjoern A. Zeeb }
40176b4cac81SBjoern A. Zeeb 
40186b4cac81SBjoern A. Zeeb bool
40196b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
40206b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
40216b4cac81SBjoern A. Zeeb {
40226b4cac81SBjoern A. Zeeb 	int i;
40236b4cac81SBjoern A. Zeeb 
40246b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
40256b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
40266b4cac81SBjoern A. Zeeb 			return (true);
40276b4cac81SBjoern A. Zeeb 	}
40286b4cac81SBjoern A. Zeeb 
40296b4cac81SBjoern A. Zeeb 	return (false);
40306b4cac81SBjoern A. Zeeb }
40316b4cac81SBjoern A. Zeeb 
40326b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
40336b4cac81SBjoern A. Zeeb bool
40346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
40356b4cac81SBjoern A. Zeeb {
40366b4cac81SBjoern A. Zeeb 	size_t x;
40376b4cac81SBjoern A. Zeeb 	uint8_t l;
40386b4cac81SBjoern A. Zeeb 
40396b4cac81SBjoern A. Zeeb 	x = *xp;
40406b4cac81SBjoern A. Zeeb 
40416b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
40426b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
40436b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
40446b4cac81SBjoern A. Zeeb 	x += 2 + l;
40456b4cac81SBjoern A. Zeeb 
40466b4cac81SBjoern A. Zeeb 	if (x > ies_len)
40476b4cac81SBjoern A. Zeeb 		return (false);
40486b4cac81SBjoern A. Zeeb 
40496b4cac81SBjoern A. Zeeb 	*xp = x;
40506b4cac81SBjoern A. Zeeb 	return (true);
40516b4cac81SBjoern A. Zeeb }
40526b4cac81SBjoern A. Zeeb 
4053d9945d78SBjoern A. Zeeb static uint8_t *
4054d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4055d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
40566b4cac81SBjoern A. Zeeb {
4057d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
4058d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
40593dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
4060d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
4061d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
4062d9945d78SBjoern A. Zeeb 	uint8_t *pb;
4063d9945d78SBjoern A. Zeeb 	int band, i;
40646b4cac81SBjoern A. Zeeb 
40653dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
4066d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4067d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
4068d9945d78SBjoern A. Zeeb 			continue;
4069d9945d78SBjoern A. Zeeb 
4070d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4071d9945d78SBjoern A. Zeeb 		/*
4072d9945d78SBjoern A. Zeeb 		 * This should not happen;
4073d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
4074d9945d78SBjoern A. Zeeb 		 */
4075d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4076d9945d78SBjoern A. Zeeb 			continue;
4077d9945d78SBjoern A. Zeeb 
4078d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
4079d9945d78SBjoern A. Zeeb 		channels = supband->channels;
4080d9945d78SBjoern A. Zeeb 		chan = NULL;
4081d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4082d9945d78SBjoern A. Zeeb 
4083d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4084d9945d78SBjoern A. Zeeb 				continue;
4085d9945d78SBjoern A. Zeeb 
40863dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
4087d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
4088d9945d78SBjoern A. Zeeb 			if (chan != NULL)
4089d9945d78SBjoern A. Zeeb 				break;
4090d9945d78SBjoern A. Zeeb 		}
4091d9945d78SBjoern A. Zeeb 
4092d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
4093d9945d78SBjoern A. Zeeb 		if (chan == NULL)
4094d9945d78SBjoern A. Zeeb 			continue;
4095d9945d78SBjoern A. Zeeb 
4096d9945d78SBjoern A. Zeeb 		pb = p;
40973dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
4098d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
4099d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
4100d9945d78SBjoern A. Zeeb 
41013dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
41023dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
41033dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
41043dd98026SBjoern A. Zeeb 
41053dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
41063dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
41073dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
41083dd98026SBjoern A. Zeeb 		}
41093dd98026SBjoern A. Zeeb #endif
41103dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
41115393cd34SBjoern A. Zeeb 		if (band == NL80211_BAND_5GHZ &&
41125393cd34SBjoern A. Zeeb 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
41133dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
41143dd98026SBjoern A. Zeeb 
41153dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
41163dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
41173dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
41183dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
41193dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
41203dd98026SBjoern A. Zeeb 		}
41213dd98026SBjoern A. Zeeb #endif
41223dd98026SBjoern A. Zeeb 
4123d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
4124d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
4125d9945d78SBjoern A. Zeeb 	}
4126d9945d78SBjoern A. Zeeb 
4127d9945d78SBjoern A. Zeeb 	/* Add common_ies */
4128d9945d78SBjoern A. Zeeb 	pb = p;
4129d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4130d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
4131d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4132d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
4133d9945d78SBjoern A. Zeeb 	}
4134d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
4135d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
4136d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
4137d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
4138d9945d78SBjoern A. Zeeb 	}
4139d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
4140d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
4141d9945d78SBjoern A. Zeeb 
4142d9945d78SBjoern A. Zeeb 	return (p);
41436b4cac81SBjoern A. Zeeb }
41446b4cac81SBjoern A. Zeeb 
41456b4cac81SBjoern A. Zeeb static void
41466b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
41476b4cac81SBjoern A. Zeeb {
41486b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41496b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41506b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41516b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
41526b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
41536b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
41546b4cac81SBjoern A. Zeeb 	int error;
41558ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
41566b4cac81SBjoern A. Zeeb 
41576b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41588ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4159a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
41606b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
41618ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41626b4cac81SBjoern A. Zeeb 		return;
41636b4cac81SBjoern A. Zeeb 	}
41648ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41666b4cac81SBjoern A. Zeeb 
41676b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
41686b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
41696b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
4170d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
41716b4cac81SBjoern A. Zeeb 		return;
41726b4cac81SBjoern A. Zeeb 	}
41736b4cac81SBjoern A. Zeeb 
41746b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41758ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4176a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4177a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4178d3ef7fb4SBjoern A. Zeeb sw_scan:
41796b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41806b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4181086be6a8SBjoern A. Zeeb 
4182086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4183086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
4184086be6a8SBjoern A. Zeeb 
41856b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
41866b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
41876b4cac81SBjoern A. Zeeb 		IMPROVE();
41886b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
41896b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
41906b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
41916b4cac81SBjoern A. Zeeb 
41926b4cac81SBjoern A. Zeeb 	} else {
41936b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
41946b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
41956b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
41966b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
41976b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4198d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
4199d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
4200d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
42013206587aSBjoern A. Zeeb 		bool running;
4202d9945d78SBjoern A. Zeeb 
4203d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4204d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
42056b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
42066b4cac81SBjoern A. Zeeb 
4207d9945d78SBjoern A. Zeeb 		band_mask = 0;
42086b4cac81SBjoern A. Zeeb 		nchan = 0;
4209c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4210c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4211d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
42126b4cac81SBjoern A. Zeeb 				nchan++;
4213d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
4214d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
4215d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
4216d9945d78SBjoern A. Zeeb 			}
4217c272abc5SBjoern A. Zeeb #else
4218c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
4219c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4220c272abc5SBjoern A. Zeeb 				switch (band) {
4221c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
4222c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
4223c272abc5SBjoern A. Zeeb 					break;
4224c272abc5SBjoern A. Zeeb 				default:
4225c272abc5SBjoern A. Zeeb 					continue;
4226c272abc5SBjoern A. Zeeb 				}
4227c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
4228c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
4229c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
4230c272abc5SBjoern A. Zeeb 				}
4231c272abc5SBjoern A. Zeeb 			}
4232c272abc5SBjoern A. Zeeb #endif
4233c272abc5SBjoern A. Zeeb 		} else {
42343206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
42353206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
42363206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
42373206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
42383206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
42393206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
42403206587aSBjoern A. Zeeb 			 */
42413206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
42423206587aSBjoern A. Zeeb 		}
42433206587aSBjoern A. Zeeb 
42446b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
42456b4cac81SBjoern A. Zeeb 
4246d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4247d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4248d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4249d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4250d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4251d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4252d9945d78SBjoern A. Zeeb 
4253d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4254d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4255d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4256d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4257d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4258d9945d78SBjoern A. Zeeb 		}
4259d9945d78SBjoern A. Zeeb 
42603206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4261d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4262d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
42636b4cac81SBjoern A. Zeeb 
42646b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
42656b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
42666b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
42676b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
42686b4cac81SBjoern A. Zeeb #if 0
42696b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
42706b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
42716b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
42726b4cac81SBjoern A. Zeeb #endif
42736b4cac81SBjoern A. Zeeb #ifdef __notyet__
42746b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
42756b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
42766b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
42776b4cac81SBjoern A. Zeeb #endif
4278e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
42796b4cac81SBjoern A. Zeeb 
42806b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
42816b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
42826b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
42836b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
42846b4cac81SBjoern A. Zeeb 			*(cpp + i) =
42856b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
42866b4cac81SBjoern A. Zeeb 		}
4287c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
42886b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4289c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
42906b4cac81SBjoern A. Zeeb 
4291c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
42926b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
42933206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
42946b4cac81SBjoern A. Zeeb 			/* lc->flags */
42956b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
42966b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
42976b4cac81SBjoern A. Zeeb 			/* lc-> ... */
42986b4cac81SBjoern A. Zeeb 			lc++;
42996b4cac81SBjoern A. Zeeb 		}
4300c272abc5SBjoern A. Zeeb #else
4301c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4302c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4303c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4304c272abc5SBjoern A. Zeeb 
4305c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4306c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4307c272abc5SBjoern A. Zeeb 				continue;
4308c272abc5SBjoern A. Zeeb 
4309c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4310c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4311c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4312c272abc5SBjoern A. Zeeb 				continue;
4313c272abc5SBjoern A. Zeeb 
4314c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4315c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4316c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4317c272abc5SBjoern A. Zeeb 				lc++;
4318c272abc5SBjoern A. Zeeb 			}
4319c272abc5SBjoern A. Zeeb 		}
4320c272abc5SBjoern A. Zeeb #endif
43216b4cac81SBjoern A. Zeeb 
4322d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
43236b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
43246b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
43256b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4326d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
43276b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
43286b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
43296b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
43306b4cac81SBjoern A. Zeeb 				ssids++;
43316b4cac81SBjoern A. Zeeb 			}
43326b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
43336b4cac81SBjoern A. Zeeb 		} else {
43346b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
43356b4cac81SBjoern A. Zeeb 		}
43366b4cac81SBjoern A. Zeeb 
43376b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
43386b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
43396b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
43406b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
43416b4cac81SBjoern A. Zeeb 		/* s6gp->... */
43426b4cac81SBjoern A. Zeeb 
4343d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4344d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4345d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4346d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4347d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4348d9945d78SBjoern A. Zeeb 
43496b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
43506b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
43513206587aSBjoern A. Zeeb 
43523206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
43533206587aSBjoern A. Zeeb 		/* Re-check under lock. */
43543206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
43553206587aSBjoern A. Zeeb 		if (!running) {
43563206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
43573206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
43583206587aSBjoern A. Zeeb 
43593206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
43603206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
43613206587aSBjoern A. Zeeb 		}
43623206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43633206587aSBjoern A. Zeeb 		if (running) {
43643206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
43653206587aSBjoern A. Zeeb 			return;
43663206587aSBjoern A. Zeeb 		}
43673206587aSBjoern A. Zeeb 
43686b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
43696b4cac81SBjoern A. Zeeb 		if (error != 0) {
4370d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4371d9945d78SBjoern A. Zeeb 
43723206587aSBjoern A. Zeeb 			/*
43733206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
43743206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
43753206587aSBjoern A. Zeeb 			 * and only there.
43763206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
43773206587aSBjoern A. Zeeb 			 * behave differently:
43783206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
43793206587aSBjoern A. Zeeb 			 *        and can then still return an error.
43803206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
43813206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
43823206587aSBjoern A. Zeeb 			 * ...
43833206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
43843206587aSBjoern A. Zeeb 			 * and balance between both code paths.
43853206587aSBjoern A. Zeeb 			 */
43863206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
43873206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
43883206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
43896b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
43903206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
43913206587aSBjoern A. Zeeb 			}
43923206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4393d3ef7fb4SBjoern A. Zeeb 
4394d3ef7fb4SBjoern A. Zeeb 			/*
4395d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4396d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4397d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4398d3ef7fb4SBjoern A. Zeeb 			 */
4399196cfd0bSBjoern A. Zeeb 			if (error == 1) {
44008ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4401a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
44028ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44033206587aSBjoern A. Zeeb 				/*
44043206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
44053206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
44063206587aSBjoern A. Zeeb 				 * currently not re-enable it?
44073206587aSBjoern A. Zeeb 				 */
44083206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4409196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4410196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4411196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4412196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4413196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4414196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4415196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4416196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4417d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4418196cfd0bSBjoern A. Zeeb 			}
4419d3ef7fb4SBjoern A. Zeeb 
4420d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4421d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
44226b4cac81SBjoern A. Zeeb 		}
44236b4cac81SBjoern A. Zeeb 	}
44246b4cac81SBjoern A. Zeeb }
44256b4cac81SBjoern A. Zeeb 
44266b4cac81SBjoern A. Zeeb static void
44276b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
44286b4cac81SBjoern A. Zeeb {
44296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44308ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
44316b4cac81SBjoern A. Zeeb 
44326b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44338ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4434a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
44358ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44366b4cac81SBjoern A. Zeeb 		return;
44376b4cac81SBjoern A. Zeeb 	}
44388ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44398ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44406b4cac81SBjoern A. Zeeb 
44418ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4442a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4443a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
44446b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
44456b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
44466b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
44476b4cac81SBjoern A. Zeeb 
4448a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4449a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
44506b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
44516b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
44526b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4453a486fbbdSBjoern A. Zeeb 
44546b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
44556b4cac81SBjoern A. Zeeb 
44566b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4457086be6a8SBjoern A. Zeeb 
4458086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4459086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
44606b4cac81SBjoern A. Zeeb 	}
44616b4cac81SBjoern A. Zeeb }
44626b4cac81SBjoern A. Zeeb 
44636b4cac81SBjoern A. Zeeb static void
4464a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4465a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4466a486fbbdSBjoern A. Zeeb {
4467a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
44688ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4469a486fbbdSBjoern A. Zeeb 
4470a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
44718ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44728ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44738ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44748ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4475a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4476a486fbbdSBjoern A. Zeeb }
4477a486fbbdSBjoern A. Zeeb 
4478a486fbbdSBjoern A. Zeeb static void
4479a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4480a486fbbdSBjoern A. Zeeb {
4481a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
44828ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4483a486fbbdSBjoern A. Zeeb 
4484a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
44858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44868ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44878ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44888ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4489a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4490a486fbbdSBjoern A. Zeeb }
4491a486fbbdSBjoern A. Zeeb 
4492a486fbbdSBjoern A. Zeeb static void
44936b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
44946b4cac81SBjoern A. Zeeb {
44956b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44966b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4497b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4498b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
44996b4cac81SBjoern A. Zeeb 	int error;
45008ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
45016b4cac81SBjoern A. Zeeb 
45026b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4503b2cf3c21SBjoern A. Zeeb 
4504b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4505b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
45066b4cac81SBjoern A. Zeeb 		return;
45076b4cac81SBjoern A. Zeeb 
4508a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
45098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
45108ac540d3SBjoern A. Zeeb 	hw_scan_running =
45118ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
45128ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
45138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
45148ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4515a486fbbdSBjoern A. Zeeb 		return;
4516a486fbbdSBjoern A. Zeeb 
4517b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4518b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
45196b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
45206b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
45216b4cac81SBjoern A. Zeeb 		return;
45226b4cac81SBjoern A. Zeeb 	}
45236b4cac81SBjoern A. Zeeb 
45246b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
45256b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
45266b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
45276b4cac81SBjoern A. Zeeb 		    c, chan);
45286b4cac81SBjoern A. Zeeb 		return;
45296b4cac81SBjoern A. Zeeb 	}
45306b4cac81SBjoern A. Zeeb 
45316b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
45326b4cac81SBjoern A. Zeeb 	IMPROVE();
45336b4cac81SBjoern A. Zeeb 
45346b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45354a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
45369fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
453711450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
45389fb91463SBjoern A. Zeeb #endif
45394a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
45406b4cac81SBjoern A. Zeeb 
45416b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
45426b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
45436b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
45446b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
45456b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
45466b4cac81SBjoern A. Zeeb 		IMPROVE();
45476b4cac81SBjoern A. Zeeb 	} else {
45486b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
45496b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
45506b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
45516b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
45526b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
45536b4cac81SBjoern A. Zeeb 	}
45546b4cac81SBjoern A. Zeeb 
45556b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
45566b4cac81SBjoern A. Zeeb 	IMPROVE();
45576b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
45586b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
45596b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
45606b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
45616b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
45626b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
45636b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
45646b4cac81SBjoern A. Zeeb 			    error);
45656b4cac81SBjoern A. Zeeb 	}
45666b4cac81SBjoern A. Zeeb }
45676b4cac81SBjoern A. Zeeb 
45686b4cac81SBjoern A. Zeeb static struct ieee80211_node *
45696b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
45706b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
45716b4cac81SBjoern A. Zeeb {
45726b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45744f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
45756b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
45766b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45776b4cac81SBjoern A. Zeeb 
45786b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
45796b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45806b4cac81SBjoern A. Zeeb 
45816b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
45824f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
45834f61ef8bSBjoern A. Zeeb 		return (NULL);
45844f61ef8bSBjoern A. Zeeb 
45856b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
45866b4cac81SBjoern A. Zeeb 	if (ni == NULL)
45876b4cac81SBjoern A. Zeeb 		return (NULL);
45886b4cac81SBjoern A. Zeeb 
45896b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45904f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
45916b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
45926b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
45936b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
45946b4cac81SBjoern A. Zeeb 		return (NULL);
45956b4cac81SBjoern A. Zeeb 	}
45966b4cac81SBjoern A. Zeeb 
45976b4cac81SBjoern A. Zeeb 	return (ni);
45986b4cac81SBjoern A. Zeeb }
45996b4cac81SBjoern A. Zeeb 
46006b4cac81SBjoern A. Zeeb static int
46016b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
46026b4cac81SBjoern A. Zeeb {
46036b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46056b4cac81SBjoern A. Zeeb 	int error;
46066b4cac81SBjoern A. Zeeb 
46076b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46086b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46096b4cac81SBjoern A. Zeeb 
46106b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
46116b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
46126b4cac81SBjoern A. Zeeb 		if (error != 0)
46136b4cac81SBjoern A. Zeeb 			return (error);
46146b4cac81SBjoern A. Zeeb 	}
46156b4cac81SBjoern A. Zeeb 
46166b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
46176b4cac81SBjoern A. Zeeb 	IMPROVE();
46186b4cac81SBjoern A. Zeeb 
46196b4cac81SBjoern A. Zeeb 	return (0);
46206b4cac81SBjoern A. Zeeb }
46216b4cac81SBjoern A. Zeeb 
46226b4cac81SBjoern A. Zeeb static void
46236b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
46246b4cac81SBjoern A. Zeeb {
46256b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46276b4cac81SBjoern A. Zeeb 
46286b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46296b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46306b4cac81SBjoern A. Zeeb 
46316b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
46326b4cac81SBjoern A. Zeeb 	IMPROVE();
46336b4cac81SBjoern A. Zeeb 
46346b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
46356b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
46366b4cac81SBjoern A. Zeeb }
46376b4cac81SBjoern A. Zeeb 
46386b4cac81SBjoern A. Zeeb static void
46396b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
46406b4cac81SBjoern A. Zeeb {
46416b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46426b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46436b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46446b4cac81SBjoern A. Zeeb 
46456b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46466b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46476b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
46486b4cac81SBjoern A. Zeeb 
46490936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
46506b4cac81SBjoern A. Zeeb 
46510936c648SBjoern A. Zeeb 	/*
46520936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
46530936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
46540936c648SBjoern A. Zeeb 	 */
46550936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
46566b4cac81SBjoern A. Zeeb 
46576b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
46586b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
46596b4cac81SBjoern A. Zeeb }
46606b4cac81SBjoern A. Zeeb 
46619a45c3caSBjoern A. Zeeb /*
46629a45c3caSBjoern A. Zeeb  * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
46639a45c3caSBjoern A. Zeeb  * call path.
46649a45c3caSBjoern A. Zeeb  * Unfortunately they have slightly different invariants.  See
46659a45c3caSBjoern A. Zeeb  * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
46669a45c3caSBjoern A. Zeeb  * Both take care of the ni reference in case of error, and otherwise during
46679a45c3caSBjoern A. Zeeb  * the callback after transmit.
46689a45c3caSBjoern A. Zeeb  * The difference is that in case of error (*ic_raw_xmit) needs us to release
46699a45c3caSBjoern A. Zeeb  * the mbuf, while (*ic_transmit) will free the mbuf itself.
46709a45c3caSBjoern A. Zeeb  */
46716b4cac81SBjoern A. Zeeb static int
46729a45c3caSBjoern A. Zeeb lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
46739a45c3caSBjoern A. Zeeb     const struct ieee80211_bpf_params *params __unused,
46749a45c3caSBjoern A. Zeeb     bool freem)
46756b4cac81SBjoern A. Zeeb {
46766b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4677c816f64eSBjoern A. Zeeb 	int error;
46786b4cac81SBjoern A. Zeeb 
46796b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4680fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
46812372f8ccSBjoern A. Zeeb #if 0
468288665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
46832372f8ccSBjoern A. Zeeb #else
46842372f8ccSBjoern A. Zeeb 	/*
46852372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46862372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46872372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46882372f8ccSBjoern A. Zeeb 	 */
46892372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
46902372f8ccSBjoern A. Zeeb #endif
4691fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46929a45c3caSBjoern A. Zeeb 		if (freem)
46930936c648SBjoern A. Zeeb 			m_free(m);
46940936c648SBjoern A. Zeeb 		return (ENETDOWN);
46950936c648SBjoern A. Zeeb 	}
46966b4cac81SBjoern A. Zeeb 
46976b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
4698c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lsta->txq, m);
4699c816f64eSBjoern A. Zeeb 	if (error != 0) {
4700c816f64eSBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47019a45c3caSBjoern A. Zeeb 		if (freem)
4702c816f64eSBjoern A. Zeeb 			m_free(m);
4703c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4704c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4705c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4706c816f64eSBjoern A. Zeeb 			    __func__, error);
4707c816f64eSBjoern A. Zeeb #endif
4708c816f64eSBjoern A. Zeeb 		return (ENETDOWN);
4709c816f64eSBjoern A. Zeeb 	}
4710fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4711fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47126b4cac81SBjoern A. Zeeb 
47139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
47156b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
47166b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
47176b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
47189d9ba2b7SBjoern A. Zeeb #endif
47196b4cac81SBjoern A. Zeeb 
47206b4cac81SBjoern A. Zeeb 	return (0);
47216b4cac81SBjoern A. Zeeb }
47226b4cac81SBjoern A. Zeeb 
47239a45c3caSBjoern A. Zeeb static int
47249a45c3caSBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
47259a45c3caSBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
47269a45c3caSBjoern A. Zeeb {
47279a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, true));
47289a45c3caSBjoern A. Zeeb }
47299a45c3caSBjoern A. Zeeb 
473011db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
47314b9ae864SBjoern A. Zeeb /*
47324b9ae864SBjoern A. Zeeb  * This is a bit of a hack given we know we are operating on a
47334b9ae864SBjoern A. Zeeb  * single frame and we know that hardware will deal with it.
47344b9ae864SBjoern A. Zeeb  * But otherwise the enmic bit and the encrypt bit need to be
47354b9ae864SBjoern A. Zeeb  * decoupled.
47364b9ae864SBjoern A. Zeeb  */
473711db70b6SBjoern A. Zeeb static int
473898e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
473998e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
474011db70b6SBjoern A. Zeeb {
474198e55905SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
474298e55905SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
47434b9ae864SBjoern A. Zeeb 	uint8_t *p;
474498e55905SBjoern A. Zeeb 
474598e55905SBjoern A. Zeeb 	/*
47464b9ae864SBjoern A. Zeeb 	 * TKIP only happens on data.
47474b9ae864SBjoern A. Zeeb 	 */
47484b9ae864SBjoern A. Zeeb 	hdr = (void *)skb->data;
47494b9ae864SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control))
47504b9ae864SBjoern A. Zeeb 		return (0);
47514b9ae864SBjoern A. Zeeb 
47524b9ae864SBjoern A. Zeeb 	/*
47534b9ae864SBjoern A. Zeeb 	 * "enmic" (though we do not do that).
47544b9ae864SBjoern A. Zeeb 	 */
47554b9ae864SBjoern A. Zeeb 	/* any conditions to not apply this? */
47564b9ae864SBjoern A. Zeeb 	if (skb_tailroom(skb) < k->wk_cipher->ic_miclen)
47574b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
47584b9ae864SBjoern A. Zeeb 
47594b9ae864SBjoern A. Zeeb 	p = skb_put(skb, k->wk_cipher->ic_miclen);
47604b9ae864SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
47614b9ae864SBjoern A. Zeeb 		goto encrypt;
47624b9ae864SBjoern A. Zeeb 
47634b9ae864SBjoern A. Zeeb 	/*
47644b9ae864SBjoern A. Zeeb 	 * (*enmic) which we hopefully do not have to do with hw accel.
47654b9ae864SBjoern A. Zeeb 	 * That means if we make it here we have a problem.
47664b9ae864SBjoern A. Zeeb 	 */
47674b9ae864SBjoern A. Zeeb 	TODO("(*enmic)");
47684b9ae864SBjoern A. Zeeb 	return (ENXIO);
47694b9ae864SBjoern A. Zeeb 
47704b9ae864SBjoern A. Zeeb encrypt:
47714b9ae864SBjoern A. Zeeb 	/*
47724b9ae864SBjoern A. Zeeb 	 * "encrypt" (though we do not do that).
47734b9ae864SBjoern A. Zeeb 	 */
47744b9ae864SBjoern A. Zeeb 	/*
47754b9ae864SBjoern A. Zeeb 	 * Check if we have anything to do as requested by driver
477698e55905SBjoern A. Zeeb 	 * or if we are done?
477798e55905SBjoern A. Zeeb 	 */
477898e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
477998e55905SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
478098e55905SBjoern A. Zeeb 			return (0);
478198e55905SBjoern A. Zeeb 
478298e55905SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
478398e55905SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
47844b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
478598e55905SBjoern A. Zeeb 
478698e55905SBjoern A. Zeeb 	hdr = (void *)skb->data;
478798e55905SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
478898e55905SBjoern A. Zeeb 	p = skb_push(skb, hlen);
478998e55905SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
479098e55905SBjoern A. Zeeb 
479198e55905SBjoern A. Zeeb 	/* If driver request space only we are done. */
479298e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
479398e55905SBjoern A. Zeeb 		return (0);
479498e55905SBjoern A. Zeeb 
479598e55905SBjoern A. Zeeb 	p += hdrlen;
479698e55905SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
479798e55905SBjoern A. Zeeb 
47984b9ae864SBjoern A. Zeeb 	/* If we make it hear we do sw encryption. */
47994b9ae864SBjoern A. Zeeb 	TODO("sw encrypt");
480098e55905SBjoern A. Zeeb 	return (ENXIO);
480198e55905SBjoern A. Zeeb }
480298e55905SBjoern A. Zeeb static int
480398e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
480498e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
480598e55905SBjoern A. Zeeb {
480611db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
480711db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
480811db70b6SBjoern A. Zeeb 	uint8_t *p;
480911db70b6SBjoern A. Zeeb 
481011db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
481111db70b6SBjoern A. Zeeb 
481211db70b6SBjoern A. Zeeb 	/*
481311db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
481411db70b6SBjoern A. Zeeb 	 * or if we are done?
481511db70b6SBjoern A. Zeeb 	 */
481611db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
481711db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
481811db70b6SBjoern A. Zeeb 	    /* MFP */
481911db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
482011db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
482111db70b6SBjoern A. Zeeb 			return (0);
482211db70b6SBjoern A. Zeeb 
482311db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
482411db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
48254b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
482611db70b6SBjoern A. Zeeb 
482711db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
482811db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
482911db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
483011db70b6SBjoern A. Zeeb 
483111db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
483211db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
483311db70b6SBjoern A. Zeeb 		return (0);
483411db70b6SBjoern A. Zeeb 
483511db70b6SBjoern A. Zeeb 	p += hdrlen;
483611db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
483711db70b6SBjoern A. Zeeb 
483811db70b6SBjoern A. Zeeb 	return (0);
483911db70b6SBjoern A. Zeeb }
484098e55905SBjoern A. Zeeb 
484198e55905SBjoern A. Zeeb static int
484298e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
484398e55905SBjoern A. Zeeb     struct sk_buff *skb)
484498e55905SBjoern A. Zeeb {
484598e55905SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
484698e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
484798e55905SBjoern A. Zeeb 
484898e55905SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
484998e55905SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
485098e55905SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
485198e55905SBjoern A. Zeeb 
485298e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
485398e55905SBjoern A. Zeeb 
485498e55905SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
485598e55905SBjoern A. Zeeb 	info->control.hw_key = kc;
485698e55905SBjoern A. Zeeb 
485798e55905SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
485898e55905SBjoern A. Zeeb 	if (kc == NULL) {
485998e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
486098e55905SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
486198e55905SBjoern A. Zeeb 		return (ENXIO);
486298e55905SBjoern A. Zeeb 	}
486398e55905SBjoern A. Zeeb 
486498e55905SBjoern A. Zeeb 	switch (kc->cipher) {
486598e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
486698e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
486798e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
486898e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
486998e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
487098e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
487198e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
487298e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
487398e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
487498e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
487598e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
487698e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
487798e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
487898e55905SBjoern A. Zeeb 	default:
487998e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
488098e55905SBjoern A. Zeeb 		    "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
488198e55905SBjoern A. Zeeb 		    skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
488298e55905SBjoern A. Zeeb 		return (EOPNOTSUPP);
488398e55905SBjoern A. Zeeb 	}
488498e55905SBjoern A. Zeeb }
488598e55905SBjoern A. Zeeb 
488698e55905SBjoern A. Zeeb static uint8_t
488798e55905SBjoern A. Zeeb lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
488898e55905SBjoern A. Zeeb {
488998e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
489098e55905SBjoern A. Zeeb 
489198e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
489298e55905SBjoern A. Zeeb 	if (kc == NULL)
489398e55905SBjoern A. Zeeb 		return (0);
489498e55905SBjoern A. Zeeb 
489598e55905SBjoern A. Zeeb 	IMPROVE("which other flags need tailroom?");
489698e55905SBjoern A. Zeeb 	if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
489798e55905SBjoern A. Zeeb 		return (32);	/* Large enough to hold everything and pow2. */
489898e55905SBjoern A. Zeeb 
489998e55905SBjoern A. Zeeb 	return (0);
490098e55905SBjoern A. Zeeb }
490111db70b6SBjoern A. Zeeb #endif
490211db70b6SBjoern A. Zeeb 
49036b4cac81SBjoern A. Zeeb static void
49046b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
49056b4cac81SBjoern A. Zeeb {
49066b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
49076b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
49086b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
49096b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
49106b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
49116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49126b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
49136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
49146b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
49156b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
49166b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
49176b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
49186b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4919e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4920ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
49216b4cac81SBjoern A. Zeeb 	void *buf;
492211db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
492398e55905SBjoern A. Zeeb 	uint8_t ac, tid, tailroom;
49246b4cac81SBjoern A. Zeeb 
49256b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
49266b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49279d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
49286b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
49296b4cac81SBjoern A. Zeeb #endif
49306b4cac81SBjoern A. Zeeb 
49316b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4932b35f6cd0SBjoern A. Zeeb 	k = NULL;
493311db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
49346b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
49356b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
493611db70b6SBjoern A. Zeeb 
493711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
493811db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
493911db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
494011db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
494111db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
494211db70b6SBjoern A. Zeeb 		}
494311db70b6SBjoern A. Zeeb #endif
494411db70b6SBjoern A. Zeeb 
494511db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
494611db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
49476b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
49486b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
49496b4cac81SBjoern A. Zeeb 			if (k == NULL) {
49506b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
49516b4cac81SBjoern A. Zeeb 				m_freem(m);
49526b4cac81SBjoern A. Zeeb 				return;
49536b4cac81SBjoern A. Zeeb 			}
49546b4cac81SBjoern A. Zeeb 		}
495511db70b6SBjoern A. Zeeb 	}
49566b4cac81SBjoern A. Zeeb 
49576b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
49586b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
49596b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
49606b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
49616b4cac81SBjoern A. Zeeb 
49626b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
49636b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
49646b4cac81SBjoern A. Zeeb 
49656b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
49666b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
49676b4cac81SBjoern A. Zeeb 		if (k != NULL)
49686b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
49696b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
49706b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
49716b4cac81SBjoern A. Zeeb 		IMPROVE();
49726b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
49736b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
49746b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
49756b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
49766b4cac81SBjoern A. Zeeb 		}
49776b4cac81SBjoern A. Zeeb 
49786b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
49796b4cac81SBjoern A. Zeeb 	}
49806b4cac81SBjoern A. Zeeb 
498198e55905SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
498298e55905SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
498398e55905SBjoern A. Zeeb 		tailroom = lkpi_hw_crypto_tailroom(lsta, k);
498498e55905SBjoern A. Zeeb 	else
498598e55905SBjoern A. Zeeb #endif
498698e55905SBjoern A. Zeeb 		tailroom = 0;
498798e55905SBjoern A. Zeeb 
49886b4cac81SBjoern A. Zeeb 	/*
49896b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
49906b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
49913d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
49923d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
49936b4cac81SBjoern A. Zeeb 	 */
499498e55905SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
49956b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4996bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4997bcf1d8eeSBjoern A. Zeeb 
4998bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4999bcf1d8eeSBjoern A. Zeeb 			int tid;
5000bcf1d8eeSBjoern A. Zeeb 
5001bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
5002bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
5003bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
5004bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
5005bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
5006bcf1d8eeSBjoern A. Zeeb 					continue;
5007bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5008bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
5009bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
5010bcf1d8eeSBjoern A. Zeeb 			}
5011bcf1d8eeSBjoern A. Zeeb 		}
50126b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
50136b4cac81SBjoern A. Zeeb 		m_freem(m);
50146b4cac81SBjoern A. Zeeb 		return;
50156b4cac81SBjoern A. Zeeb 	}
50166b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
50176b4cac81SBjoern A. Zeeb 
50186b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
50196b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
50206b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
50216b4cac81SBjoern A. Zeeb 	skb->m = m;
50226b4cac81SBjoern A. Zeeb #if 0
50236b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
50246b4cac81SBjoern A. Zeeb #else
50256b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
50266b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
50276b4cac81SBjoern A. Zeeb #endif
50286b4cac81SBjoern A. Zeeb 	/* Save the ni. */
50296b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
50306b4cac81SBjoern A. Zeeb 
50316b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
50326b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50336b4cac81SBjoern A. Zeeb 
5034e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
5035e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
5036e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
50371665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
50381665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
50391665ef97SBjoern A. Zeeb 			skb->priority = 7;
50401665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
50411665ef97SBjoern A. Zeeb 		} else {
50421665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
50431665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
5044e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
5045e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
50461665ef97SBjoern A. Zeeb 		}
5047e3a0b120SBjoern A. Zeeb 	} else {
5048e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
5049fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
5050e3a0b120SBjoern A. Zeeb 	}
50516b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
50526b4cac81SBjoern A. Zeeb 
50536b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
50546b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
50556b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
50566b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
50576b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
50586b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
5059e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
50606b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
50616b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
50626b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
50636b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
50649fb91463SBjoern A. Zeeb #ifdef __notyet__
50659fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50669fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
50679fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
50689fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
50699fb91463SBjoern A. Zeeb #endif
50709fb91463SBjoern A. Zeeb #endif
50716b4cac81SBjoern A. Zeeb 
50726b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
5073b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
507411db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
507511db70b6SBjoern A. Zeeb 		int error;
507611db70b6SBjoern A. Zeeb 
507711db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
507811db70b6SBjoern A. Zeeb 		if (error != 0) {
507911db70b6SBjoern A. Zeeb 			/*
508011db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
508111db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
508211db70b6SBjoern A. Zeeb 			 */
508311db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
508411db70b6SBjoern A. Zeeb 			return;
508511db70b6SBjoern A. Zeeb 		}
508611db70b6SBjoern A. Zeeb 	}
50876b4cac81SBjoern A. Zeeb #endif
50886b4cac81SBjoern A. Zeeb 
50896b4cac81SBjoern A. Zeeb 	IMPROVE();
50906b4cac81SBjoern A. Zeeb 
5091e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
5092e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
5093e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
5094e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
5095e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
5096d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
5097e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
5098e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
5099e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
5100e3a0b120SBjoern A. Zeeb 	}
5101e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
5102d0d29110SBjoern A. Zeeb 		goto ops_tx;
5103e3a0b120SBjoern A. Zeeb 
5104d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
5105d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
5106d0d29110SBjoern A. Zeeb 
5107eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
51086b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
51099d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51109d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5111325aa4dbSMark Johnston 		printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
5112d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5113d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
5114fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
5115325aa4dbSMark Johnston 		    curthread->td_tid, jiffies,
5116d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5117d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
5118d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
51199d9ba2b7SBjoern A. Zeeb #endif
5120eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5121cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5122d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
5123cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
51246b4cac81SBjoern A. Zeeb 	return;
51256b4cac81SBjoern A. Zeeb 
51266b4cac81SBjoern A. Zeeb ops_tx:
51279d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51289d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5129d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5130d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
51316b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
51326b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
51339d9ba2b7SBjoern A. Zeeb #endif
51346b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
51356b4cac81SBjoern A. Zeeb 	control.sta = sta;
5136cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
51376b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
5138cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
51396b4cac81SBjoern A. Zeeb }
51406b4cac81SBjoern A. Zeeb 
51416b4cac81SBjoern A. Zeeb static void
51426b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
51436b4cac81SBjoern A. Zeeb {
51446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51456b4cac81SBjoern A. Zeeb 	struct mbufq mq;
51466b4cac81SBjoern A. Zeeb 	struct mbuf *m;
514788665349SBjoern A. Zeeb 	bool shall_tx;
51486b4cac81SBjoern A. Zeeb 
51496b4cac81SBjoern A. Zeeb 	lsta = ctx;
51506b4cac81SBjoern A. Zeeb 
51519d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51529d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
51536b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
51549d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
51556b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
51569d9ba2b7SBjoern A. Zeeb #endif
51576b4cac81SBjoern A. Zeeb 
51586b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
51596b4cac81SBjoern A. Zeeb 
5160fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
5161fa4e4257SBjoern A. Zeeb 	/*
5162fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
516388665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
516488665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
516588665349SBjoern A. Zeeb 	 * point to try to TX.
516688665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
516788665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
5168fa4e4257SBjoern A. Zeeb 	 */
51692372f8ccSBjoern A. Zeeb #if 0
517088665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
51712372f8ccSBjoern A. Zeeb #else
51722372f8ccSBjoern A. Zeeb 	/*
51732372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
51742372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
51752372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
51762372f8ccSBjoern A. Zeeb 	 */
51772372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
51782372f8ccSBjoern A. Zeeb #endif
517988665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
51806b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
518188665349SBjoern A. Zeeb 	/*
518288665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
518388665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
518488665349SBjoern A. Zeeb 	 */
5185fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
51866b4cac81SBjoern A. Zeeb 
51876b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
51886b4cac81SBjoern A. Zeeb 	while (m != NULL) {
51896b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
51906b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
51916b4cac81SBjoern A. Zeeb 	}
51926b4cac81SBjoern A. Zeeb }
51936b4cac81SBjoern A. Zeeb 
51946b4cac81SBjoern A. Zeeb static int
51956b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
51966b4cac81SBjoern A. Zeeb {
51976b4cac81SBjoern A. Zeeb 
51986b4cac81SBjoern A. Zeeb 	/* XXX TODO */
51996b4cac81SBjoern A. Zeeb 	IMPROVE();
52006b4cac81SBjoern A. Zeeb 
52016b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
52026b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
52036b4cac81SBjoern A. Zeeb 
52046b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
52059a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, false));
52066b4cac81SBjoern A. Zeeb }
52076b4cac81SBjoern A. Zeeb 
52089fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
52099fb91463SBjoern A. Zeeb static int
52109fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
52119fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
52129fb91463SBjoern A. Zeeb {
52139fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52149fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52159fb91463SBjoern A. Zeeb 
52169fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52179fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52189fb91463SBjoern A. Zeeb 
5219310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
52209fb91463SBjoern A. Zeeb 
52219fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
52229fb91463SBjoern A. Zeeb }
52239fb91463SBjoern A. Zeeb 
52249fb91463SBjoern A. Zeeb static int
52259fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
52269fb91463SBjoern A. Zeeb {
52279fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52289fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52299fb91463SBjoern A. Zeeb 
52309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52329fb91463SBjoern A. Zeeb 
5233310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
52349fb91463SBjoern A. Zeeb 
52359fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
52369fb91463SBjoern A. Zeeb }
52379fb91463SBjoern A. Zeeb 
52389fb91463SBjoern A. Zeeb 
52399fb91463SBjoern A. Zeeb static int
52409fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
52419fb91463SBjoern A. Zeeb {
52429fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52439fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52449fb91463SBjoern A. Zeeb 
52459fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52469fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52479fb91463SBjoern A. Zeeb 
5248310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
52499fb91463SBjoern A. Zeeb 
52509fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
52519fb91463SBjoern A. Zeeb }
52529fb91463SBjoern A. Zeeb 
5253310743c4SBjoern A. Zeeb /*
5254310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5255310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5256310743c4SBjoern A. Zeeb  *
5257310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
5258310743c4SBjoern A. Zeeb  */
52599fb91463SBjoern A. Zeeb static int
52609fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
52619fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
52629fb91463SBjoern A. Zeeb {
52639fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52649fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5265310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5266310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5267310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5268310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5269310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5270310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5271310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5272310743c4SBjoern A. Zeeb 	int error;
52739fb91463SBjoern A. Zeeb 
52749fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52759fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5276310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5277310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5278310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5279310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5280310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5281310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52829fb91463SBjoern A. Zeeb 
5283310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5284310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5285310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5286310743c4SBjoern A. Zeeb 		return (0);
5287310743c4SBjoern A. Zeeb 	}
5288310743c4SBjoern A. Zeeb 
5289310743c4SBjoern A. Zeeb 	params.sta = sta;
5290310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
5291310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
5292310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5293310743c4SBjoern A. Zeeb 	params.timeout = 0;
5294310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5295310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5296310743c4SBjoern A. Zeeb 	params.amsdu = false;
5297310743c4SBjoern A. Zeeb 
5298310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5299cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5300310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5301cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5302310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5303310743c4SBjoern A. Zeeb 	if (error != 0) {
5304310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5305310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5306310743c4SBjoern A. Zeeb 		return (0);
5307310743c4SBjoern A. Zeeb 	}
53089fb91463SBjoern A. Zeeb 
53099fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
53109fb91463SBjoern A. Zeeb }
53119fb91463SBjoern A. Zeeb 
5312310743c4SBjoern A. Zeeb /*
5313310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5314310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
5315310743c4SBjoern A. Zeeb  *
5316310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
5317310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
5318310743c4SBjoern A. Zeeb  */
53199fb91463SBjoern A. Zeeb static int
53209fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
53219fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
53229fb91463SBjoern A. Zeeb {
53239fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53249fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5325310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5326310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5327310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5328310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5329310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5330310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5331310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5332310743c4SBjoern A. Zeeb 	int error;
53339fb91463SBjoern A. Zeeb 
53349fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53359fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5336310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5337310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5338310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5339310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5340310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5341310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53429fb91463SBjoern A. Zeeb 
5343310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5344310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5345310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5346310743c4SBjoern A. Zeeb 		return (0);
5347310743c4SBjoern A. Zeeb 	}
5348310743c4SBjoern A. Zeeb 
5349310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
5350310743c4SBjoern A. Zeeb 		params.sta = sta;
5351310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5352310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
5353310743c4SBjoern A. Zeeb 		params.timeout = 0;
5354310743c4SBjoern A. Zeeb 		params.ssn = 0;
5355310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5356310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5357310743c4SBjoern A. Zeeb 			params.amsdu = true;
5358310743c4SBjoern A. Zeeb 		else
5359310743c4SBjoern A. Zeeb 			params.amsdu = false;
5360310743c4SBjoern A. Zeeb 	} else {
5361310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
5362310743c4SBjoern A. Zeeb 		params.sta = sta;
5363310743c4SBjoern A. Zeeb 		switch (status) {
5364310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
5365310743c4SBjoern A. Zeeb 		default:
5366310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5367310743c4SBjoern A. Zeeb 			break;
5368310743c4SBjoern A. Zeeb 		}
5369310743c4SBjoern A. Zeeb 		params.buf_size = 0;
5370310743c4SBjoern A. Zeeb 		params.timeout = 0;
5371310743c4SBjoern A. Zeeb 		params.ssn = 0;
5372310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5373310743c4SBjoern A. Zeeb 		params.amsdu = false;
5374310743c4SBjoern A. Zeeb 	}
5375310743c4SBjoern A. Zeeb 
5376310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5377cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5378310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5379cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5380310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5381310743c4SBjoern A. Zeeb 	if (error != 0) {
5382310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5383310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5384310743c4SBjoern A. Zeeb 		return (0);
5385310743c4SBjoern A. Zeeb 	}
5386310743c4SBjoern A. Zeeb 
5387310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
53889fb91463SBjoern A. Zeeb 
53899fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
53909fb91463SBjoern A. Zeeb }
53919fb91463SBjoern A. Zeeb 
5392310743c4SBjoern A. Zeeb /*
5393310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5394310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5395310743c4SBjoern A. Zeeb  */
53969fb91463SBjoern A. Zeeb static void
53979fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
53989fb91463SBjoern A. Zeeb {
53999fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54009fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5401310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5402310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5403310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5404310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5405310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5406310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5407310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5408310743c4SBjoern A. Zeeb 	int error;
54099fb91463SBjoern A. Zeeb 
54109fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54119fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5412310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5413310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5414310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5415310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5416310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5417310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
54189fb91463SBjoern A. Zeeb 
5419310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5420310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5421310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5422310743c4SBjoern A. Zeeb 		goto n80211;
5423310743c4SBjoern A. Zeeb 	}
54249fb91463SBjoern A. Zeeb 
5425310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5426310743c4SBjoern A. Zeeb 	params.sta = sta;
5427310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5428310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5429310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5430310743c4SBjoern A. Zeeb 	params.timeout = 0;
5431310743c4SBjoern A. Zeeb 	params.ssn = 0;
5432310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5433310743c4SBjoern A. Zeeb 	params.amsdu = false;
5434310743c4SBjoern A. Zeeb 
5435310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5436cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5437310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5438cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5439310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5440310743c4SBjoern A. Zeeb 	if (error != 0) {
5441310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5442310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5443310743c4SBjoern A. Zeeb 		goto n80211;
5444310743c4SBjoern A. Zeeb 	}
5445310743c4SBjoern A. Zeeb 
5446310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5447310743c4SBjoern A. Zeeb 
5448310743c4SBjoern A. Zeeb n80211:
54499fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
54509fb91463SBjoern A. Zeeb }
54519fb91463SBjoern A. Zeeb 
54529fb91463SBjoern A. Zeeb static void
54539fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
54549fb91463SBjoern A. Zeeb {
54559fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54569fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54579fb91463SBjoern A. Zeeb 
54589fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54599fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54609fb91463SBjoern A. Zeeb 
54619fb91463SBjoern A. Zeeb 	IMPROVE_HT();
54629fb91463SBjoern A. Zeeb 
54639fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
54649fb91463SBjoern A. Zeeb }
54659fb91463SBjoern A. Zeeb 
54669fb91463SBjoern A. Zeeb static void
54679fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
54689fb91463SBjoern A. Zeeb     int status)
54699fb91463SBjoern A. Zeeb {
54709fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54719fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54729fb91463SBjoern A. Zeeb 
54739fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54749fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54759fb91463SBjoern A. Zeeb 
54769fb91463SBjoern A. Zeeb 	IMPROVE_HT();
54779fb91463SBjoern A. Zeeb 
54789fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
54799fb91463SBjoern A. Zeeb }
54809fb91463SBjoern A. Zeeb 
54819fb91463SBjoern A. Zeeb static int
54829fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
54839fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
54849fb91463SBjoern A. Zeeb {
54859fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54869fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54879fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54889fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
54899fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54909fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
54919fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
54929fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5493310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
54949fb91463SBjoern A. Zeeb 	int error;
54959fb91463SBjoern A. Zeeb 
54969fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54979fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54989fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54999fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
55009fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
55019fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
55029fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
55039fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
55049fb91463SBjoern A. Zeeb 
5505310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5506310743c4SBjoern A. Zeeb 
5507310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5508310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5509310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5510310743c4SBjoern A. Zeeb 		return (-ENXIO);
5511310743c4SBjoern A. Zeeb 	}
5512310743c4SBjoern A. Zeeb 
55139fb91463SBjoern A. Zeeb 	params.sta = sta;
55149fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
55159fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
55169fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
55179fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
55189fb91463SBjoern A. Zeeb 	else
55199fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5520310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5521310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
55229fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
55239fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
55249fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
55259fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5526310743c4SBjoern A. Zeeb 
5527310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5528310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5529310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5530310743c4SBjoern A. Zeeb 		params.amsdu = true;
5531310743c4SBjoern A. Zeeb 	else
55329fb91463SBjoern A. Zeeb 		params.amsdu = false;
55339fb91463SBjoern A. Zeeb 
5534cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
55359fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5536cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
55379fb91463SBjoern A. Zeeb 	if (error != 0) {
55389fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
55399fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
55409fb91463SBjoern A. Zeeb 		return (error);
55419fb91463SBjoern A. Zeeb 	}
5542310743c4SBjoern A. Zeeb 
5543310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5544310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5545310743c4SBjoern A. Zeeb 	}
5546310743c4SBjoern A. Zeeb 
55479fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
55489fb91463SBjoern A. Zeeb 
55499fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
55509fb91463SBjoern A. Zeeb 	return (error);
55519fb91463SBjoern A. Zeeb }
55529fb91463SBjoern A. Zeeb 
55539fb91463SBjoern A. Zeeb static void
55549fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
55559fb91463SBjoern A. Zeeb {
55569fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
55579fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55589fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
55599fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
55609fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55619fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
55629fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
55639fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5564310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
55659fb91463SBjoern A. Zeeb 	int error;
55669fb91463SBjoern A. Zeeb 	uint8_t tid;
556765c573e4SBjoern A. Zeeb 	bool ic_locked;
55689fb91463SBjoern A. Zeeb 
55699fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
55709fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
55719fb91463SBjoern A. Zeeb 
55729fb91463SBjoern A. Zeeb 	/*
55739fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
55749fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
55759fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
55769fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
55779fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
55789fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
55799fb91463SBjoern A. Zeeb 	 * track some state itself.
55809fb91463SBjoern A. Zeeb 	 */
55819fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
55829fb91463SBjoern A. Zeeb 		goto net80211_only;
55839fb91463SBjoern A. Zeeb 
55849fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
55859fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
55869fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
55879fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
55889fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
55899fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
55909fb91463SBjoern A. Zeeb 
55919fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
55929fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
55939fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
55949fb91463SBjoern A. Zeeb 			break;
55959fb91463SBjoern A. Zeeb 	}
55969fb91463SBjoern A. Zeeb 
55979fb91463SBjoern A. Zeeb 	params.sta = sta;
55989fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
55999fb91463SBjoern A. Zeeb 	params.buf_size = 0;
56009fb91463SBjoern A. Zeeb 	params.timeout = 0;
56019fb91463SBjoern A. Zeeb 	params.ssn = 0;
56029fb91463SBjoern A. Zeeb 	params.tid = tid;
56039fb91463SBjoern A. Zeeb 	params.amsdu = false;
56049fb91463SBjoern A. Zeeb 
560565c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
560665c573e4SBjoern A. Zeeb 	if (ic_locked)
560765c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5608cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
56099fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5610cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
561165c573e4SBjoern A. Zeeb 	if (ic_locked)
561265c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
56139fb91463SBjoern A. Zeeb 	if (error != 0)
56149fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
56159fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
56169fb91463SBjoern A. Zeeb 
56179fb91463SBjoern A. Zeeb net80211_only:
56189fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
56199fb91463SBjoern A. Zeeb }
56209fb91463SBjoern A. Zeeb #endif
56219fb91463SBjoern A. Zeeb 
56229fb91463SBjoern A. Zeeb static void
56239fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
56249fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
56259fb91463SBjoern A. Zeeb {
56269fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
56279fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
56289fb91463SBjoern A. Zeeb 
56299fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
56309fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
56319fb91463SBjoern A. Zeeb 		return;
56329fb91463SBjoern A. Zeeb 
56339fb91463SBjoern A. Zeeb 	switch (band) {
56349fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
56359fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
56369fb91463SBjoern A. Zeeb 		break;
56379fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
56389fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
56399fb91463SBjoern A. Zeeb 		break;
56409fb91463SBjoern A. Zeeb 	default:
56419fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
56429fb91463SBjoern A. Zeeb 		return;
56439fb91463SBjoern A. Zeeb 	}
56449fb91463SBjoern A. Zeeb 
56459fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
56469fb91463SBjoern A. Zeeb 
56479fb91463SBjoern A. Zeeb 	/*
56489fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
56499fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
56509fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
56519fb91463SBjoern A. Zeeb 	 */
56529fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
56539fb91463SBjoern A. Zeeb 
56549fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
56559fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
56569fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
56579fb91463SBjoern A. Zeeb #ifdef __notyet__
56589fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
56599fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
56609fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
56619fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
56629fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
56639fb91463SBjoern A. Zeeb #endif
56649fb91463SBjoern A. Zeeb 
56659fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
56669fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
56679fb91463SBjoern A. Zeeb 
56689fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
56699fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
56709fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
56719fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
56729fb91463SBjoern A. Zeeb #endif
56739fb91463SBjoern A. Zeeb }
56749fb91463SBjoern A. Zeeb 
56756b4cac81SBjoern A. Zeeb static void
56766b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
56776b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
56786b4cac81SBjoern A. Zeeb {
56796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
56816b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
56826b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
56836b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
56846b4cac81SBjoern A. Zeeb 
56856b4cac81SBjoern A. Zeeb 	/* Channels */
56866b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
56876b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
56886b4cac81SBjoern A. Zeeb 
56896b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
56906b4cac81SBjoern A. Zeeb 	nchans = 0;
56916b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
56926b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
56936b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
56946b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
56956b4cac81SBjoern A. Zeeb 		chan_flags = 0;
56966b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
56976b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
56989fb91463SBjoern A. Zeeb 
56999fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
57006b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
57019fb91463SBjoern A. Zeeb 
57029fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
57039fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
57046b4cac81SBjoern A. Zeeb 
57056b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5706cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
57076b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
57086b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
57096b4cac81SBjoern A. Zeeb 
57106b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
57113f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
57123f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
57136b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
57146b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
57156b4cac81SBjoern A. Zeeb 				continue;
57166b4cac81SBjoern A. Zeeb 			}
57176b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
57186b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
57196b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
57206b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
57216b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
57226b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
57236b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
57246b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
57256b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
57266b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
57276b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
57286b4cac81SBjoern A. Zeeb 
57296b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
57306b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
57316b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
57325856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5733cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5734cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
57353f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
57363f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
57376b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
57386b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
57396b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5740cee56e77SBjoern A. Zeeb 			if (error != 0)
57416b4cac81SBjoern A. Zeeb 				break;
57426b4cac81SBjoern A. Zeeb 		}
57436b4cac81SBjoern A. Zeeb 	}
57446b4cac81SBjoern A. Zeeb 
57456b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
57466b4cac81SBjoern A. Zeeb 	nchans = 0;
57476b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
57486b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
57496b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
57506b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
57516b4cac81SBjoern A. Zeeb 		chan_flags = 0;
57526b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
57539fb91463SBjoern A. Zeeb 
57549fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
57559fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
57569fb91463SBjoern A. Zeeb 
57579fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
57586b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
57596b4cac81SBjoern A. Zeeb 
57606b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5761562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
57626b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5763ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5764ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
57656b4cac81SBjoern A. Zeeb 
57666b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
57676b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
57686b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5769562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
57706b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
57716b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5772562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
57736b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
57746b4cac81SBjoern A. Zeeb 		}
57756b4cac81SBjoern A. Zeeb #endif
57766b4cac81SBjoern A. Zeeb 
57776b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5778cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
57796b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
57806b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
57816b4cac81SBjoern A. Zeeb 
57826b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
57833f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
57843f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
57856b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
57866b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
57876b4cac81SBjoern A. Zeeb 				continue;
57886b4cac81SBjoern A. Zeeb 			}
57896b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
57906b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
57916b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
57926b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
57936b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
57946b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
57956b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
57966b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
57976b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
57986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
57996b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
58006b4cac81SBjoern A. Zeeb 
58016b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
58026b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
58036b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
58045856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5805cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5806cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
58073f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
58083f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
58096b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
58106b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
58116b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5812cee56e77SBjoern A. Zeeb 			if (error != 0)
58136b4cac81SBjoern A. Zeeb 				break;
58146b4cac81SBjoern A. Zeeb 		}
58156b4cac81SBjoern A. Zeeb 	}
58166b4cac81SBjoern A. Zeeb }
58176b4cac81SBjoern A. Zeeb 
58186b4cac81SBjoern A. Zeeb static void *
58196b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
58206b4cac81SBjoern A. Zeeb {
58216b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58226b4cac81SBjoern A. Zeeb 
58236b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
58246b4cac81SBjoern A. Zeeb 
58256b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
58266b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
58276b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
58286b4cac81SBjoern A. Zeeb 
58296b4cac81SBjoern A. Zeeb 	return (ic);
58306b4cac81SBjoern A. Zeeb }
58316b4cac81SBjoern A. Zeeb 
58326b4cac81SBjoern A. Zeeb struct ieee80211_hw *
58336b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
58346b4cac81SBjoern A. Zeeb {
58356b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
58366b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58376b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5838a5ae63edSBjoern A. Zeeb 	int ac;
58396b4cac81SBjoern A. Zeeb 
58406b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
58416b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
58426b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
58436b4cac81SBjoern A. Zeeb 		return (NULL);
58446b4cac81SBjoern A. Zeeb 
58456b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58466b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5847652e22d3SBjoern A. Zeeb 
58488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5849eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
58508891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
58516b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5852a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5853a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5854a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5855a5ae63edSBjoern A. Zeeb 	}
58566b4cac81SBjoern A. Zeeb 
5857a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf */
5858a8a47a41SBjoern A. Zeeb 	INIT_LIST_HEAD(&lhw->lchanctx_list);
5859a8a47a41SBjoern A. Zeeb 
5860759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5861759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5862759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5863832b8e98SBjoern A. Zeeb 	mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
5864759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5865759a996dSBjoern A. Zeeb 
58666b4cac81SBjoern A. Zeeb 	/*
58676b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
58686b4cac81SBjoern A. Zeeb 	 * not initialized.
58696b4cac81SBjoern A. Zeeb 	 */
58706b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
58716b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5872086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
58736b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
58746b4cac81SBjoern A. Zeeb 
58756b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
58766b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
58776b4cac81SBjoern A. Zeeb 
58786b4cac81SBjoern A. Zeeb 	IMPROVE();
58796b4cac81SBjoern A. Zeeb 
58806b4cac81SBjoern A. Zeeb 	return (hw);
58816b4cac81SBjoern A. Zeeb }
58826b4cac81SBjoern A. Zeeb 
58836b4cac81SBjoern A. Zeeb void
58846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
58856b4cac81SBjoern A. Zeeb {
58866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5887759a996dSBjoern A. Zeeb 	struct mbuf *m;
58886b4cac81SBjoern A. Zeeb 
58896b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58906b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
58916b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
58926b4cac81SBjoern A. Zeeb 
5893759a996dSBjoern A. Zeeb 	/*
5894759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5895759a996dSBjoern A. Zeeb 	 */
5896759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5897759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5898759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5899759a996dSBjoern A. Zeeb 
5900759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5901759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5902759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5903759a996dSBjoern A. Zeeb 
5904759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5905759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5906759a996dSBjoern A. Zeeb 	while (m != NULL) {
5907dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
5908759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5909759a996dSBjoern A. Zeeb 
5910759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5911759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5912759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5913759a996dSBjoern A. Zeeb 
5914759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5915759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5916759a996dSBjoern A. Zeeb 		}
5917dbae3dcfSBjoern A. Zeeb #else
5918dbae3dcfSBjoern A. Zeeb 		if (m->m_pkthdr.PH_loc.ptr != NULL) {
5919dbae3dcfSBjoern A. Zeeb 			struct ieee80211_node *ni;
5920dbae3dcfSBjoern A. Zeeb 
5921dbae3dcfSBjoern A. Zeeb 			ni = m->m_pkthdr.PH_loc.ptr;
5922dbae3dcfSBjoern A. Zeeb 			ieee80211_free_node(ni);
5923dbae3dcfSBjoern A. Zeeb 		}
5924dbae3dcfSBjoern A. Zeeb #endif
5925759a996dSBjoern A. Zeeb 		m_freem(m);
5926759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5927759a996dSBjoern A. Zeeb 	}
5928759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5929759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5930759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5931759a996dSBjoern A. Zeeb 
5932a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf. */
5933a8a47a41SBjoern A. Zeeb 	if (!list_empty_careful(&lhw->lchanctx_list)) {
5934a8a47a41SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx, *next;
5935a8a47a41SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
5936a8a47a41SBjoern A. Zeeb 
5937a8a47a41SBjoern A. Zeeb 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
5938a8a47a41SBjoern A. Zeeb 			if (lchanctx->added_to_drv) {
5939a8a47a41SBjoern A. Zeeb 				/* In reality we should panic? */
5940a8a47a41SBjoern A. Zeeb 				chanctx_conf = &lchanctx->chanctx_conf;
5941a8a47a41SBjoern A. Zeeb 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
5942a8a47a41SBjoern A. Zeeb 			}
5943a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
5944a8a47a41SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
5945a8a47a41SBjoern A. Zeeb 		}
5946a8a47a41SBjoern A. Zeeb 	}
5947a8a47a41SBjoern A. Zeeb 
59486b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5949eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
59508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5951eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
59526b4cac81SBjoern A. Zeeb 	IMPROVE();
59536b4cac81SBjoern A. Zeeb }
59546b4cac81SBjoern A. Zeeb 
59556b4cac81SBjoern A. Zeeb void
59566b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
59576b4cac81SBjoern A. Zeeb {
59586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59596b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59606b4cac81SBjoern A. Zeeb 
59616b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59626b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59636b4cac81SBjoern A. Zeeb 
59646b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
59656b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
59666b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
59676b4cac81SBjoern A. Zeeb 
59686b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
59696b4cac81SBjoern A. Zeeb }
59706b4cac81SBjoern A. Zeeb 
59716b4cac81SBjoern A. Zeeb struct ieee80211_hw *
59726b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
59736b4cac81SBjoern A. Zeeb {
59746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59756b4cac81SBjoern A. Zeeb 
59766b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59776b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
59786b4cac81SBjoern A. Zeeb }
59796b4cac81SBjoern A. Zeeb 
59806b4cac81SBjoern A. Zeeb static void
59816b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
59826b4cac81SBjoern A. Zeeb {
59836b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59846b4cac81SBjoern A. Zeeb 
59856b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59866b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
59876b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
59886b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
59896b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
59906b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
59916b4cac81SBjoern A. Zeeb }
59926b4cac81SBjoern A. Zeeb 
59932e183d99SBjoern A. Zeeb int
59946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
59956b4cac81SBjoern A. Zeeb {
59966b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5998c5b96b3eSBjoern A. Zeeb 	int band, i;
59996b4cac81SBjoern A. Zeeb 
60006b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
60016b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
60026b4cac81SBjoern A. Zeeb 
6003652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
6004652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
6005652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
6006652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
6007652e22d3SBjoern A. Zeeb 
60086b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
60096b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
60106b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
60116b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
60126b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
60136b4cac81SBjoern A. Zeeb 		/* We take the first one. */
60146b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
60156b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
60166b4cac81SBjoern A. Zeeb 	} else {
60176b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
60186b4cac81SBjoern A. Zeeb 	}
60196b4cac81SBjoern A. Zeeb 
60203d09d310SBjoern A. Zeeb #ifdef __not_yet__
60213d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
60226b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
60233d09d310SBjoern A. Zeeb #endif
60246b4cac81SBjoern A. Zeeb 
60256b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
60266b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
60276b4cac81SBjoern A. Zeeb 
60286b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
60296b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
60306b4cac81SBjoern A. Zeeb 	ic->ic_caps =
60316b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
60326b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
60336b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
60344a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
60356b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
60364a67f1dfSBjoern A. Zeeb #endif
60376b4cac81SBjoern A. Zeeb #if 0
60386b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
60396b4cac81SBjoern A. Zeeb #endif
60406b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
60416b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
60426b4cac81SBjoern A. Zeeb 	    ;
60436b4cac81SBjoern A. Zeeb #if 0
60446b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
60456b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
60466b4cac81SBjoern A. Zeeb #endif
6047cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
6048cc4e78d5SBjoern A. Zeeb 		/*
6049cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
6050cc4e78d5SBjoern A. Zeeb 		 *
6051cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
6052cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
6053cc4e78d5SBjoern A. Zeeb 		 * the flag.
6054cc4e78d5SBjoern A. Zeeb 		 */
60556b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
6056a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
60576b4cac81SBjoern A. Zeeb 	}
60586b4cac81SBjoern A. Zeeb 
605963578bf2SBjoern A. Zeeb 	/* Does HW support Fragmentation offload? */
606063578bf2SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
606163578bf2SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
606263578bf2SBjoern A. Zeeb 
6063527687a9SBjoern A. Zeeb 	/*
6064527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
6065527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
6066527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
6067527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
6068527687a9SBjoern A. Zeeb 	 */
6069527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
6070527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
6071527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
6072527687a9SBjoern A. Zeeb 
6073527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
6074527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
6075527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
6076527687a9SBjoern A. Zeeb 		}
6077527687a9SBjoern A. Zeeb 	}
6078527687a9SBjoern A. Zeeb 
60796b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
6080b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
608111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
6082bf8c25f1SBjoern A. Zeeb 		uint32_t hwciphers;
6083bf8c25f1SBjoern A. Zeeb 
6084bf8c25f1SBjoern A. Zeeb 		hwciphers = 0;
608592942717SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
608692942717SBjoern A. Zeeb 			uint32_t cs;
608792942717SBjoern A. Zeeb 
608892942717SBjoern A. Zeeb 			cs = lkpi_l80211_to_net80211_cyphers(
6089bf8c25f1SBjoern A. Zeeb 			    ic, hw->wiphy->cipher_suites[i]);
609092942717SBjoern A. Zeeb 			if (cs == IEEE80211_CRYPTO_TKIP) {
609192942717SBjoern A. Zeeb 				/*
609292942717SBjoern A. Zeeb 				 * We do set this here.  We will only find out
609392942717SBjoern A. Zeeb 				 * when doing a SET_KEY operation depending on
609492942717SBjoern A. Zeeb 				 * what the driver returns.
609592942717SBjoern A. Zeeb 				 * net80211::ieee80211_crypto_newkey()
609692942717SBjoern A. Zeeb 				 * checks this so we will have to do flags
609792942717SBjoern A. Zeeb 				 * surgery later.
609892942717SBjoern A. Zeeb 				 */
609992942717SBjoern A. Zeeb 				cs |= IEEE80211_CRYPTO_TKIPMIC;
610092942717SBjoern A. Zeeb 			}
610192942717SBjoern A. Zeeb 			hwciphers |= cs;
610292942717SBjoern A. Zeeb 		}
6103bf8c25f1SBjoern A. Zeeb 		/*
6104bf8c25f1SBjoern A. Zeeb 		 * (20250415) nothing anywhere in the path checks we actually
6105bf8c25f1SBjoern A. Zeeb 		 * support all these in net80211.
6106bf8c25f1SBjoern A. Zeeb 		 * net80211 supports _256 variants but the ioctl does not.
6107bf8c25f1SBjoern A. Zeeb 		 */
6108bf8c25f1SBjoern A. Zeeb 		IMPROVE("as net80211 grows more support, enable them");
610992942717SBjoern A. Zeeb 		hwciphers &= (IEEE80211_CRYPTO_WEP |
611092942717SBjoern A. Zeeb 		    IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
6111bf8c25f1SBjoern A. Zeeb 		    IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
6112b71d3043SBjoern A. Zeeb 		/*
6113b71d3043SBjoern A. Zeeb 		 * We only support CCMP here, so further filter.
6114b71d3043SBjoern A. Zeeb 		 * Also permit TKIP if turned on.
6115b71d3043SBjoern A. Zeeb 		 */
6116b71d3043SBjoern A. Zeeb 		hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
6117b71d3043SBjoern A. Zeeb 		    (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
6118b71d3043SBjoern A. Zeeb 		    IEEE80211_CRYPTO_TKIPMIC) : 0));
6119bf8c25f1SBjoern A. Zeeb 		ieee80211_set_hardware_ciphers(ic, hwciphers);
61206b4cac81SBjoern A. Zeeb 	}
61216b4cac81SBjoern A. Zeeb #endif
61226b4cac81SBjoern A. Zeeb 
61236b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
61246b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
61256b4cac81SBjoern A. Zeeb 
61266b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
61276b4cac81SBjoern A. Zeeb 
61286b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
61296b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
61306b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
61316b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
61326b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
61336b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
61346b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
61356b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
61366b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
61376b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
61386b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
61396b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
61406b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
61416b4cac81SBjoern A. Zeeb 
6142a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
6143a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6144a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6145a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6146a486fbbdSBjoern A. Zeeb 
61476b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
61486b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
61496b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
61506b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
61516b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
61526b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
61536b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
61546b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
61556b4cac81SBjoern A. Zeeb 
61569fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
615786bc7259SBjoern A. Zeeb 	/*
615886bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
615986bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
616086bc7259SBjoern A. Zeeb 	 */
616186bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
61629fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
61639fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
61649fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
61659fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
61669fb91463SBjoern A. Zeeb 
61679fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
61689fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
61699fb91463SBjoern A. Zeeb 
61709fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
61719fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
61729fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
61739fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
61749fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
61759fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
61769fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
61779fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
61789fb91463SBjoern A. Zeeb 
61799fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
61809fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
61819fb91463SBjoern A. Zeeb 
61829fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
61839fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
61849fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
61859fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
618686bc7259SBjoern A. Zeeb 	}
61879fb91463SBjoern A. Zeeb #endif
61889fb91463SBjoern A. Zeeb 
61896b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
61906b4cac81SBjoern A. Zeeb 
6191c5b96b3eSBjoern A. Zeeb 	/*
6192c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
6193c5b96b3eSBjoern A. Zeeb 	 * expect one.
6194d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
6195d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6196c5b96b3eSBjoern A. Zeeb 	 */
6197d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
6198f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6199c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6200c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6201c5b96b3eSBjoern A. Zeeb 
6202c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
6203c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6204c5b96b3eSBjoern A. Zeeb 			continue;
6205c5b96b3eSBjoern A. Zeeb 
6206d9945d78SBjoern A. Zeeb 		lhw->supbands++;
6207d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6208d9945d78SBjoern A. Zeeb 
6209f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
6210f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
6211f454a4a1SBjoern A. Zeeb 			continue;
6212f454a4a1SBjoern A. Zeeb 
6213c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
6214c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6215c5b96b3eSBjoern A. Zeeb 
6216c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6217c5b96b3eSBjoern A. Zeeb 				continue;
6218c5b96b3eSBjoern A. Zeeb 
62194a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
62209fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
622111450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
62229fb91463SBjoern A. Zeeb #endif
6223c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
6224c5b96b3eSBjoern A. Zeeb 			break;
6225c5b96b3eSBjoern A. Zeeb 		}
6226c5b96b3eSBjoern A. Zeeb 	}
6227c5b96b3eSBjoern A. Zeeb 
6228d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6229d9945d78SBjoern A. Zeeb 
6230d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
6231d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6232d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6233d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6234d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6235d9945d78SBjoern A. Zeeb 	}
6236d9945d78SBjoern A. Zeeb 
6237d9945d78SBjoern A. Zeeb 	/*
6238d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
6239d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
6240d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
6241d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
6242d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
6243d9945d78SBjoern A. Zeeb 	 */
6244d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6245d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
6246d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
624778ca45dfSBjoern A. Zeeb 
624878ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6249d9945d78SBjoern A. Zeeb 		/*
625078ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
625178ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
625278ca45dfSBjoern A. Zeeb 		 * space in.
6253d9945d78SBjoern A. Zeeb 		 */
6254d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
625578ca45dfSBjoern A. Zeeb 	}
6256d9945d78SBjoern A. Zeeb 
62579fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
62589fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
62599fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
62609fb91463SBjoern A. Zeeb #endif
62619fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
62621f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
62639fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
62649fb91463SBjoern A. Zeeb #endif
62659fb91463SBjoern A. Zeeb 
6266d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
626778ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
626878ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
626978ca45dfSBjoern A. Zeeb 			goto err;
6270d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
627178ca45dfSBjoern A. Zeeb 	}
6272d9945d78SBjoern A. Zeeb 
62736b4cac81SBjoern A. Zeeb 	if (bootverbose)
62746b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
62752e183d99SBjoern A. Zeeb 
62762e183d99SBjoern A. Zeeb 	return (0);
627778ca45dfSBjoern A. Zeeb err:
627878ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
627978ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
62806b4cac81SBjoern A. Zeeb }
62816b4cac81SBjoern A. Zeeb 
62826b4cac81SBjoern A. Zeeb void
62836b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
62846b4cac81SBjoern A. Zeeb {
62856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62866b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
62876b4cac81SBjoern A. Zeeb 
62886b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62896b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
62906b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
62916b4cac81SBjoern A. Zeeb }
62926b4cac81SBjoern A. Zeeb 
62936b4cac81SBjoern A. Zeeb void
62946b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
62956b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
62966b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
62976b4cac81SBjoern A. Zeeb     void *arg)
62986b4cac81SBjoern A. Zeeb {
62996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63006b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
63016b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
630261a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
63036b4cac81SBjoern A. Zeeb 
63046b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63056b4cac81SBjoern A. Zeeb 
63066b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
63076b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
630861a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6309adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
63106b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
63116b4cac81SBjoern A. Zeeb 		    __func__, flags);
63126b4cac81SBjoern A. Zeeb 	}
63136b4cac81SBjoern A. Zeeb 
6314adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
63156b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
631661a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
63176b4cac81SBjoern A. Zeeb 
63186b4cac81SBjoern A. Zeeb 	if (atomic)
63198891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
63206b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
63216b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
63226b4cac81SBjoern A. Zeeb 
63236b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
63246b4cac81SBjoern A. Zeeb 
63256b4cac81SBjoern A. Zeeb 		/*
63266b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
63276b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
63286b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
63296b4cac81SBjoern A. Zeeb 		 * driver does not know about.
63306b4cac81SBjoern A. Zeeb 		 */
63316b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
63326b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
63336b4cac81SBjoern A. Zeeb 			continue;
63346b4cac81SBjoern A. Zeeb 
63356b4cac81SBjoern A. Zeeb 		/*
633661a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
633761a68e50SBjoern A. Zeeb 		 * if we haven't yet.
633861a68e50SBjoern A. Zeeb 		 */
633961a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
634061a68e50SBjoern A. Zeeb 			continue;
634161a68e50SBjoern A. Zeeb 
634261a68e50SBjoern A. Zeeb 		/*
63436b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
63446b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
63456b4cac81SBjoern A. Zeeb 		 */
63466b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
63476b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
63486b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
63496b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
63506b4cac81SBjoern A. Zeeb 	}
63516b4cac81SBjoern A. Zeeb 	if (atomic)
63528891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
63536b4cac81SBjoern A. Zeeb }
63546b4cac81SBjoern A. Zeeb 
635511db70b6SBjoern A. Zeeb static void
635611db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
635711db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
635811db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
635911db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
636011db70b6SBjoern A. Zeeb     void *arg)
636111db70b6SBjoern A. Zeeb {
636211db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
636311db70b6SBjoern A. Zeeb 		return;
636411db70b6SBjoern A. Zeeb 
636511db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
636611db70b6SBjoern A. Zeeb 		return;
636711db70b6SBjoern A. Zeeb 
636811db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
636911db70b6SBjoern A. Zeeb }
637011db70b6SBjoern A. Zeeb 
63716b4cac81SBjoern A. Zeeb void
63726b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
63736b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
63746b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
63756b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
637611db70b6SBjoern A. Zeeb     void *arg, bool rcu)
63776b4cac81SBjoern A. Zeeb {
637811db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
637911db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
63806b4cac81SBjoern A. Zeeb 
638111db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
638211db70b6SBjoern A. Zeeb 
638311db70b6SBjoern A. Zeeb 	if (rcu) {
6384a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6385a8f735a6SBjoern A. Zeeb 
638611db70b6SBjoern A. Zeeb 		if (vif == NULL) {
638711db70b6SBjoern A. Zeeb 			TODO();
638811db70b6SBjoern A. Zeeb 		} else {
6389a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
639011db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
639111db70b6SBjoern A. Zeeb 				    keyix++)
639211db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
639311db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
639411db70b6SBjoern A. Zeeb 			}
639511db70b6SBjoern A. Zeeb 		}
639611db70b6SBjoern A. Zeeb 	} else {
639711db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
639811db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
639911db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
640011db70b6SBjoern A. Zeeb 
640111db70b6SBjoern A. Zeeb 		if (vif == NULL) {
640211db70b6SBjoern A. Zeeb 			TODO();
640311db70b6SBjoern A. Zeeb 		} else {
6404a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
640511db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
640611db70b6SBjoern A. Zeeb 				    keyix++)
640711db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
640811db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
640911db70b6SBjoern A. Zeeb 			}
641011db70b6SBjoern A. Zeeb 		}
641111db70b6SBjoern A. Zeeb 	}
64126b4cac81SBjoern A. Zeeb }
64136b4cac81SBjoern A. Zeeb 
64146b4cac81SBjoern A. Zeeb void
64156b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
64166b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
64176b4cac81SBjoern A. Zeeb 	void *),
64186b4cac81SBjoern A. Zeeb     void *arg)
64196b4cac81SBjoern A. Zeeb {
6420c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6421c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
64226b4cac81SBjoern A. Zeeb 
6423c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
6424c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6425c5e25798SBjoern A. Zeeb 
6426c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6427c5e25798SBjoern A. Zeeb 
6428a8a47a41SBjoern A. Zeeb 	rcu_read_lock();
6429a8a47a41SBjoern A. Zeeb 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6430c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
6431c5e25798SBjoern A. Zeeb 			continue;
6432d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6433c5e25798SBjoern A. Zeeb 	}
6434a8a47a41SBjoern A. Zeeb 	rcu_read_unlock();
64356b4cac81SBjoern A. Zeeb }
64366b4cac81SBjoern A. Zeeb 
64376b4cac81SBjoern A. Zeeb void
64386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
64396b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
64406b4cac81SBjoern A. Zeeb {
64416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
64436b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
64446b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
64456b4cac81SBjoern A. Zeeb 
64466b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
64476b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
64486b4cac81SBjoern A. Zeeb 
64496b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
64506b4cac81SBjoern A. Zeeb 
64518891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
64526b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
64536b4cac81SBjoern A. Zeeb 
6454a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6455a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
64566b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
64576b4cac81SBjoern A. Zeeb 				continue;
64586b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
64596b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
64606b4cac81SBjoern A. Zeeb 		}
6461a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
64626b4cac81SBjoern A. Zeeb 	}
64638891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
64646b4cac81SBjoern A. Zeeb }
64656b4cac81SBjoern A. Zeeb 
6466673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6467673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6468673d62fcSBjoern A. Zeeb {
6469673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6470673d62fcSBjoern A. Zeeb 
6471673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6472673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6473673d62fcSBjoern A. Zeeb 	return (regd);
6474673d62fcSBjoern A. Zeeb }
6475673d62fcSBjoern A. Zeeb 
64766b4cac81SBjoern A. Zeeb int
64776b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
64786b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
64796b4cac81SBjoern A. Zeeb {
64806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
64826b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
64836b4cac81SBjoern A. Zeeb 
64846b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
64856b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
64866b4cac81SBjoern A. Zeeb 
64876b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
64886b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
64896b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
64906b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
64916b4cac81SBjoern A. Zeeb 	}
64926b4cac81SBjoern A. Zeeb 
64936b4cac81SBjoern A. Zeeb 	TODO();
64946b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
64956b4cac81SBjoern A. Zeeb 
64966b4cac81SBjoern A. Zeeb 	return (0);
64976b4cac81SBjoern A. Zeeb }
64986b4cac81SBjoern A. Zeeb 
64996b4cac81SBjoern A. Zeeb void
65006b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
65016b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
65026b4cac81SBjoern A. Zeeb {
65036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
65046b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
65056b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
65066b4cac81SBjoern A. Zeeb 
65076b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
65086b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
65096b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
65106b4cac81SBjoern A. Zeeb 
65116b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
65126b4cac81SBjoern A. Zeeb 
65138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
65146b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
65156b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6516a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
65176b4cac81SBjoern A. Zeeb 	wakeup(lhw);
65188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
65196b4cac81SBjoern A. Zeeb 
65206b4cac81SBjoern A. Zeeb 	return;
65216b4cac81SBjoern A. Zeeb }
65226b4cac81SBjoern A. Zeeb 
6523759a996dSBjoern A. Zeeb static void
6524759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6525759a996dSBjoern A. Zeeb {
6526759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6527dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6528759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6529dbae3dcfSBjoern A. Zeeb #endif
6530759a996dSBjoern A. Zeeb 	int ok;
6531759a996dSBjoern A. Zeeb 
6532759a996dSBjoern A. Zeeb 	ni = NULL;
6533dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6534759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6535759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6536759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6537759a996dSBjoern A. Zeeb 
6538759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6539759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6540759a996dSBjoern A. Zeeb 	}
6541dbae3dcfSBjoern A. Zeeb #else
6542dbae3dcfSBjoern A. Zeeb 	if (m->m_pkthdr.PH_loc.ptr != NULL) {
6543dbae3dcfSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6544dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = NULL;
6545dbae3dcfSBjoern A. Zeeb 	}
6546dbae3dcfSBjoern A. Zeeb #endif
6547759a996dSBjoern A. Zeeb 
6548759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6549759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6550759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6551759a996dSBjoern A. Zeeb 		if (ok < 0)
6552759a996dSBjoern A. Zeeb 			m_freem(m);
6553759a996dSBjoern A. Zeeb 	} else {
6554759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6555759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6556759a996dSBjoern A. Zeeb 	}
6557759a996dSBjoern A. Zeeb 
6558759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6559759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6560bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6561759a996dSBjoern A. Zeeb #endif
6562759a996dSBjoern A. Zeeb }
6563759a996dSBjoern A. Zeeb 
6564759a996dSBjoern A. Zeeb static void
6565759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6566759a996dSBjoern A. Zeeb {
6567759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6568759a996dSBjoern A. Zeeb 	struct mbufq mq;
6569759a996dSBjoern A. Zeeb 	struct mbuf *m;
6570759a996dSBjoern A. Zeeb 
6571759a996dSBjoern A. Zeeb 	lhw = ctx;
6572759a996dSBjoern A. Zeeb 
6573759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6574759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6575bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6576bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6577759a996dSBjoern A. Zeeb #endif
6578759a996dSBjoern A. Zeeb 
6579759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6580759a996dSBjoern A. Zeeb 
6581759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6582759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6583759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6584759a996dSBjoern A. Zeeb 
6585759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6586759a996dSBjoern A. Zeeb 	while (m != NULL) {
6587759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6588759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6589759a996dSBjoern A. Zeeb 	}
6590759a996dSBjoern A. Zeeb }
6591759a996dSBjoern A. Zeeb 
659249010ba7SBjoern A. Zeeb static void
6593a1adefb1SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
659449010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
659549010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
659649010ba7SBjoern A. Zeeb     uint8_t *rssip)
659749010ba7SBjoern A. Zeeb {
659849010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
6599a1adefb1SBjoern A. Zeeb 	struct rate_info rxrate;
660049010ba7SBjoern A. Zeeb 	int i;
660149010ba7SBjoern A. Zeeb 	uint8_t rssi;
660249010ba7SBjoern A. Zeeb 
6603a1adefb1SBjoern A. Zeeb 	memset(&rxrate, 0, sizeof(rxrate));
660449010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
660549010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
660649010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
660749010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
660849010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
660949010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
661049010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
661149010ba7SBjoern A. Zeeb 	else
661249010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
661349010ba7SBjoern A. Zeeb 	/*
661449010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
661549010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
661649010ba7SBjoern A. Zeeb 	 */
661749010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
661849010ba7SBjoern A. Zeeb 	if (rssip != NULL)
661949010ba7SBjoern A. Zeeb 		*rssip = rssi;
662049010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
662149010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
662249010ba7SBjoern A. Zeeb 	rx_stats->c_band =
662349010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
662449010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
662549010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
662649010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
662749010ba7SBjoern A. Zeeb 
662849010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
662949010ba7SBjoern A. Zeeb 
663049010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
663149010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
663249010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
663349010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
663449010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
663549010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
663649010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
663749010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
663849010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
663949010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
664049010ba7SBjoern A. Zeeb 	}
664149010ba7SBjoern A. Zeeb 
664249010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
664349010ba7SBjoern A. Zeeb 		int cc;
664449010ba7SBjoern A. Zeeb 		int8_t crssi;
664549010ba7SBjoern A. Zeeb 
664649010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
664749010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
664849010ba7SBjoern A. Zeeb 
664949010ba7SBjoern A. Zeeb 		cc = 0;
665049010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
665149010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
665249010ba7SBjoern A. Zeeb 				continue;
665349010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
665449010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
665549010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
665649010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
665749010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
665849010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
665949010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
666049010ba7SBjoern A. Zeeb 			cc++;
666149010ba7SBjoern A. Zeeb 		}
666249010ba7SBjoern A. Zeeb 		if (cc > 0)
666349010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
666449010ba7SBjoern A. Zeeb 	}
666549010ba7SBjoern A. Zeeb 
666649010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
666749010ba7SBjoern A. Zeeb 
666849010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
666949010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
6670a1adefb1SBjoern A. Zeeb 	{
6671a1adefb1SBjoern A. Zeeb 		uint32_t legacy = 0;
6672a1adefb1SBjoern A. Zeeb 
667349010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
667449010ba7SBjoern A. Zeeb 		if (supband != NULL)
6675a1adefb1SBjoern A. Zeeb 			legacy = supband->bitrates[rx_status->rate_idx].bitrate;
6676a1adefb1SBjoern A. Zeeb 		rx_stats->c_rate = legacy;
6677a1adefb1SBjoern A. Zeeb 		rxrate.legacy = legacy;
667849010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
667949010ba7SBjoern A. Zeeb 		break;
6680a1adefb1SBjoern A. Zeeb 	}
668149010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
668249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
668349010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6684a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_MCS;
6685a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6686a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6687a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6688a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6689a1adefb1SBjoern A. Zeeb 		}
669049010ba7SBjoern A. Zeeb 		break;
669149010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
669249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
669349010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
669449010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
6695a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
6696a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6697a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6698a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6699a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6700a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6701a1adefb1SBjoern A. Zeeb 		}
670249010ba7SBjoern A. Zeeb 		break;
670349010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
6704a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
6705a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6706a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6707a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
6708a1adefb1SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6709a1adefb1SBjoern A. Zeeb 		break;
671049010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
6711a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
6712a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6713a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6714a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
671549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
671649010ba7SBjoern A. Zeeb 		break;
671749010ba7SBjoern A. Zeeb 	}
671849010ba7SBjoern A. Zeeb 
6719a1adefb1SBjoern A. Zeeb 	rxrate.bw = rx_status->bw;
672049010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
672149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
672249010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
672349010ba7SBjoern A. Zeeb 		break;
672449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
672549010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
672649010ba7SBjoern A. Zeeb 		break;
672749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
672849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
672949010ba7SBjoern A. Zeeb 		break;
673049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
673149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
673249010ba7SBjoern A. Zeeb 		break;
673349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
673449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
673549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
673649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
673749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
673849010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
673949010ba7SBjoern A. Zeeb 		break;
674049010ba7SBjoern A. Zeeb 	}
674149010ba7SBjoern A. Zeeb 
674249010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
674349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
674449010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
674549010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
674649010ba7SBjoern A. Zeeb 
674749010ba7SBjoern A. Zeeb 	/*
674849010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
674949010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
675049010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
675149010ba7SBjoern A. Zeeb 	 */
675249010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
675349010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
675449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
675549010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
675649010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
675749010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
675849010ba7SBjoern A. Zeeb 	}
6759731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
6760731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
6761731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
6762731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
6763731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6764731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
676549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
676649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6767394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6768394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
676949010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
677049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
677149010ba7SBjoern A. Zeeb #endif
6772a1adefb1SBjoern A. Zeeb 
6773a1adefb1SBjoern A. Zeeb 	if (lsta != NULL) {
6774a1adefb1SBjoern A. Zeeb 		memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
6775a1adefb1SBjoern A. Zeeb 		lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
6776a1adefb1SBjoern A. Zeeb 	}
677749010ba7SBjoern A. Zeeb }
677849010ba7SBjoern A. Zeeb 
6779e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
67806b4cac81SBjoern A. Zeeb void
67816b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6782e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6783e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
67846b4cac81SBjoern A. Zeeb {
67856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67866b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
67876b4cac81SBjoern A. Zeeb 	struct mbuf *m;
67886b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
67896b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
67906b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
67916b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
67926b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
67936b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
67946b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
6795c816f64eSBjoern A. Zeeb 	int i, offset, ok, error;
679649010ba7SBjoern A. Zeeb 	uint8_t rssi;
6797c0cadd99SBjoern A. Zeeb 	bool is_beacon;
67986b4cac81SBjoern A. Zeeb 
6799c013f810SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6800c013f810SBjoern A. Zeeb 	ic = lhw->ic;
6801c013f810SBjoern A. Zeeb 
68026b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
68036b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
6804c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
68056b4cac81SBjoern A. Zeeb 		IMPROVE();
68066b4cac81SBjoern A. Zeeb 		goto err;
68076b4cac81SBjoern A. Zeeb 	}
68086b4cac81SBjoern A. Zeeb 
68096b4cac81SBjoern A. Zeeb 	/*
68106b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
68116b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
68126b4cac81SBjoern A. Zeeb 	 */
6813*02382a0aSBjoern A. Zeeb 	m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
6814c013f810SBjoern A. Zeeb 	if (m == NULL) {
6815c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
68166b4cac81SBjoern A. Zeeb 		goto err;
6817c013f810SBjoern A. Zeeb 	}
68186b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
68196b4cac81SBjoern A. Zeeb 
68206b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
68216b4cac81SBjoern A. Zeeb 	offset = m->m_len;
68226b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
68236b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
68246b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
68256b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
68266b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
68276b4cac81SBjoern A. Zeeb 	}
68286b4cac81SBjoern A. Zeeb 
68296b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
68306b4cac81SBjoern A. Zeeb 
68316b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6832c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6833c0cadd99SBjoern A. Zeeb 
6834c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
68359d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
68366b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
68376b4cac81SBjoern A. Zeeb 
68389d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
683973e3969fSBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
6840c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
684173e3969fSBjoern A. Zeeb 		    __func__, skb, skb->len, skb->data_len,
68426b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
68436b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6844c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
68456b4cac81SBjoern A. Zeeb 
68469d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
68476b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
68486b4cac81SBjoern A. Zeeb 
68496b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
68509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6851f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
685260970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
68536b4cac81SBjoern A. Zeeb 			__func__,
6854c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6855c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
68566b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6857f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
68586b4cac81SBjoern A. Zeeb 			rx_status->freq,
68596b4cac81SBjoern A. Zeeb 			rx_status->bw,
68606b4cac81SBjoern A. Zeeb 			rx_status->encoding,
68616b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
68626b4cac81SBjoern A. Zeeb 			rx_status->band,
68636b4cac81SBjoern A. Zeeb 			rx_status->chains,
68646b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
68656b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
68666b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
686760970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
68686b4cac81SBjoern A. Zeeb 			rx_status->signal,
68696b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
68706b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
68716b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
68726b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
68736b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
68746b4cac81SBjoern A. Zeeb 			rx_status->nss,
68756b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
68766b4cac81SBjoern A. Zeeb no_trace_beacons:
68776b4cac81SBjoern A. Zeeb #endif
68786b4cac81SBjoern A. Zeeb 
687958246901SBjoern A. Zeeb 	lsta = NULL;
68806b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
68816b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
68826b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
68836b4cac81SBjoern A. Zeeb 	} else {
6884c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6885c8dafefaSBjoern A. Zeeb 
68866b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
68876b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
68886b4cac81SBjoern A. Zeeb 		if (ni != NULL)
68896b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
68906b4cac81SBjoern A. Zeeb 	}
68916b4cac81SBjoern A. Zeeb 
6892a1adefb1SBjoern A. Zeeb 	rssi = 0;
6893a1adefb1SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
6894a1adefb1SBjoern A. Zeeb 
6895a1adefb1SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
6896a1adefb1SBjoern A. Zeeb 	if (ok == 0) {
6897a1adefb1SBjoern A. Zeeb 		m_freem(m);
6898a1adefb1SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6899a1adefb1SBjoern A. Zeeb 		goto err;
6900a1adefb1SBjoern A. Zeeb 	}
6901a1adefb1SBjoern A. Zeeb 
69026b4cac81SBjoern A. Zeeb 	if (ni != NULL)
69036b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
69046b4cac81SBjoern A. Zeeb 	else
69056b4cac81SBjoern A. Zeeb 		/*
69066b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
69076b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
69086b4cac81SBjoern A. Zeeb 		 */
69096b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
69106b4cac81SBjoern A. Zeeb 
69119d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
69129d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6913bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6914c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6915c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
69169d9ba2b7SBjoern A. Zeeb #endif
69176b4cac81SBjoern A. Zeeb 
6918c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6919c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6920c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6921c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6922c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6923c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6924c8dafefaSBjoern A. Zeeb 
6925c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6926c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6927c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6928c8dafefaSBjoern A. Zeeb 
6929c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6930c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6931c8dafefaSBjoern A. Zeeb 
6932c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6933c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6934c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6935c8dafefaSBjoern A. Zeeb 		/*
6936c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6937c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6938c8dafefaSBjoern A. Zeeb 		 */
6939c8dafefaSBjoern A. Zeeb skip_device_ts:
69409fb91463SBjoern A. Zeeb 		;
69419fb91463SBjoern A. Zeeb 	}
6942c8dafefaSBjoern A. Zeeb 
69436b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
69446b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
69456b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
69466b4cac81SBjoern A. Zeeb 
69476b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
69486b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
69496b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
69506b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
69516b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
69526b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
69536b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
69546b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
69556b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
69566b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
69576b4cac81SBjoern A. Zeeb #endif
69586b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
69596b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
69606b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
69616b4cac81SBjoern A. Zeeb 		IMPROVE();
69626b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
69636b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
69646b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
69656b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6966170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
69676b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
69686b4cac81SBjoern A. Zeeb 	}
69696b4cac81SBjoern A. Zeeb 
69706b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
69716b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
69726b4cac81SBjoern A. Zeeb 
6973e30e05d3SBjoern A. Zeeb #if 0
6974e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6975e30e05d3SBjoern A. Zeeb 		/*
6976e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6977e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6978e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6979e30e05d3SBjoern A. Zeeb 		*/
6980e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6981e30e05d3SBjoern A. Zeeb 	}
6982e30e05d3SBjoern A. Zeeb #endif
6983e30e05d3SBjoern A. Zeeb 
6984c013f810SBjoern A. Zeeb 	/* Attach meta-information to the mbuf for the deferred RX path. */
69856b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6986dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6987759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6988759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6989759a996dSBjoern A. Zeeb 
6990759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6991759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6992c013f810SBjoern A. Zeeb 		if (mtag == NULL) {
6993c013f810SBjoern A. Zeeb 			m_freem(m);
6994c013f810SBjoern A. Zeeb 			counter_u64_add(ic->ic_ierrors, 1);
6995c013f810SBjoern A. Zeeb 			goto err;
6996c013f810SBjoern A. Zeeb 		}
6997759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6998759a996dSBjoern A. Zeeb 		rxni->ni = ni;		/* We hold a reference. */
6999759a996dSBjoern A. Zeeb 		m_tag_prepend(m, mtag);
7000dbae3dcfSBjoern A. Zeeb #else
7001dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = ni;	/* We hold a reference. */
7002dbae3dcfSBjoern A. Zeeb #endif
7003759a996dSBjoern A. Zeeb 	}
70046b4cac81SBjoern A. Zeeb 
7005759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
7006759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
7007759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7008759a996dSBjoern A. Zeeb 		m_freem(m);
7009c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
7010759a996dSBjoern A. Zeeb 		goto err;
7011759a996dSBjoern A. Zeeb 	}
7012759a996dSBjoern A. Zeeb 
7013c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lhw->rxq, m);
7014c816f64eSBjoern A. Zeeb 	if (error != 0) {
7015c816f64eSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7016c816f64eSBjoern A. Zeeb 		m_freem(m);
7017c816f64eSBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
7018c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7019c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7020c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
7021c816f64eSBjoern A. Zeeb 			    __func__, error);
7022c816f64eSBjoern A. Zeeb #endif
7023c816f64eSBjoern A. Zeeb 		goto err;
7024c816f64eSBjoern A. Zeeb 	}
7025759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
7026759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
70276b4cac81SBjoern A. Zeeb 
70286b4cac81SBjoern A. Zeeb 	IMPROVE();
70296b4cac81SBjoern A. Zeeb 
70306b4cac81SBjoern A. Zeeb err:
70316b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
70326b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
70336b4cac81SBjoern A. Zeeb }
70346b4cac81SBjoern A. Zeeb 
70356b4cac81SBjoern A. Zeeb uint8_t
7036ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
70376b4cac81SBjoern A. Zeeb {
70386b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
7039ec190d91SBjoern A. Zeeb 	uint8_t tid;
7040ec190d91SBjoern A. Zeeb 
7041ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
7042ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
7043ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
7044ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
70456b4cac81SBjoern A. Zeeb 
70466b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
7047ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
7048ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
7049ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
7050ec190d91SBjoern A. Zeeb 
7051ec190d91SBjoern A. Zeeb 	return (tid);
70526b4cac81SBjoern A. Zeeb }
70536b4cac81SBjoern A. Zeeb 
7054ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7055ac1d519cSBjoern A. Zeeb 
7056ac1d519cSBjoern A. Zeeb static void
7057ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
7058ac1d519cSBjoern A. Zeeb {
7059ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7060ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
7061ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
7062ac1d519cSBjoern A. Zeeb 
7063ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
7064ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7065ac1d519cSBjoern A. Zeeb 
7066ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
7067ac1d519cSBjoern A. Zeeb 
7068ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7069ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
7070ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
7071ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
7072ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7073ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
7074ac1d519cSBjoern A. Zeeb 		return;
7075ac1d519cSBjoern A. Zeeb 	}
7076ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
7077ac1d519cSBjoern A. Zeeb 
7078ac1d519cSBjoern A. Zeeb 	/* More work to do? */
7079ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
7080ac1d519cSBjoern A. Zeeb 		schedule_work(work);
7081ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7082ac1d519cSBjoern A. Zeeb 
7083ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
7084ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
7085ac1d519cSBjoern A. Zeeb 
7086ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
7087ac1d519cSBjoern A. Zeeb }
7088ac1d519cSBjoern A. Zeeb 
7089ac1d519cSBjoern A. Zeeb void
7090ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
7091ac1d519cSBjoern A. Zeeb {
7092ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7093ac1d519cSBjoern A. Zeeb 
7094ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7095ac1d519cSBjoern A. Zeeb 
7096ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7097ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
7098ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
7099ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
7100ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7101ac1d519cSBjoern A. Zeeb 
7102ac1d519cSBjoern A. Zeeb 	/*
7103ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
7104ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
7105ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
7106ac1d519cSBjoern A. Zeeb 	 */
7107ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
7108ac1d519cSBjoern A. Zeeb }
7109ac1d519cSBjoern A. Zeeb 
7110ac1d519cSBjoern A. Zeeb void
7111ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
7112ac1d519cSBjoern A. Zeeb {
7113ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7114ac1d519cSBjoern A. Zeeb 
7115ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7116ac1d519cSBjoern A. Zeeb 
7117ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7118ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
7119ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
7120ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
7121ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7122ac1d519cSBjoern A. Zeeb }
7123ac1d519cSBjoern A. Zeeb 
7124ac1d519cSBjoern A. Zeeb void
7125ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
7126ac1d519cSBjoern A. Zeeb {
7127ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7128ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
7129ac1d519cSBjoern A. Zeeb 
7130ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7131ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7132ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
7133ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
7134ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7135ac1d519cSBjoern A. Zeeb 		return;
7136ac1d519cSBjoern A. Zeeb 	}
7137ac1d519cSBjoern A. Zeeb 
7138ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
7139ac1d519cSBjoern A. Zeeb 
7140ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7141ac1d519cSBjoern A. Zeeb 		    entry);
7142ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
7143ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7144ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
7145ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7146ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
7147ac1d519cSBjoern A. Zeeb 			break;
7148ac1d519cSBjoern A. Zeeb 	}
7149ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7150ac1d519cSBjoern A. Zeeb }
7151ac1d519cSBjoern A. Zeeb 
7152ac1d519cSBjoern A. Zeeb void
7153ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7154ac1d519cSBjoern A. Zeeb {
7155ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
7156ac1d519cSBjoern A. Zeeb 
7157ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
7158ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7159ac1d519cSBjoern A. Zeeb }
7160ac1d519cSBjoern A. Zeeb 
7161ac1d519cSBjoern A. Zeeb void
7162ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7163ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
7164ac1d519cSBjoern A. Zeeb {
7165ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
7166ac1d519cSBjoern A. Zeeb 		/* Run right away. */
7167ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
7168ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
7169ac1d519cSBjoern A. Zeeb 	} else {
7170ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
7171ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
7172ac1d519cSBjoern A. Zeeb 	}
7173ac1d519cSBjoern A. Zeeb }
7174ac1d519cSBjoern A. Zeeb 
7175ac1d519cSBjoern A. Zeeb void
7176ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7177ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
7178ac1d519cSBjoern A. Zeeb {
7179ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
7180ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
7181ac1d519cSBjoern A. Zeeb }
7182ac1d519cSBjoern A. Zeeb 
7183ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7184ac1d519cSBjoern A. Zeeb 
71856b4cac81SBjoern A. Zeeb struct wiphy *
71866b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
71876b4cac81SBjoern A. Zeeb {
71886b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7189ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
71906b4cac81SBjoern A. Zeeb 
71916b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
71926b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
71936b4cac81SBjoern A. Zeeb 		return (NULL);
71946b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
71956b4cac81SBjoern A. Zeeb 
7196ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7197ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
7198ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7199ac1d519cSBjoern A. Zeeb 
7200ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7201ac1d519cSBjoern A. Zeeb 
7202ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
7203ac1d519cSBjoern A. Zeeb 	TODO();
7204ac1d519cSBjoern A. Zeeb 
7205ac1d519cSBjoern A. Zeeb 	return (wiphy);
72066b4cac81SBjoern A. Zeeb }
72076b4cac81SBjoern A. Zeeb 
72086b4cac81SBjoern A. Zeeb void
72096b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
72106b4cac81SBjoern A. Zeeb {
72116b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
72126b4cac81SBjoern A. Zeeb 
72136b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
72146b4cac81SBjoern A. Zeeb 		return;
72156b4cac81SBjoern A. Zeeb 
7216ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
7217ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
7218ac1d519cSBjoern A. Zeeb 
72196b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7220ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7221ac1d519cSBjoern A. Zeeb 
72226b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
72236b4cac81SBjoern A. Zeeb }
72246b4cac81SBjoern A. Zeeb 
7225a7c19b8aSBjoern A. Zeeb static uint32_t
7226a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7227a7c19b8aSBjoern A. Zeeb {
7228a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
7229a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7230a7c19b8aSBjoern A. Zeeb }
7231a7c19b8aSBjoern A. Zeeb 
7232a7c19b8aSBjoern A. Zeeb static uint32_t
7233a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7234a7c19b8aSBjoern A. Zeeb {
7235a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
7236a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7237a7c19b8aSBjoern A. Zeeb }
7238a7c19b8aSBjoern A. Zeeb 
7239a7c19b8aSBjoern A. Zeeb uint32_t
7240a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7241a7c19b8aSBjoern A. Zeeb {
7242a7c19b8aSBjoern A. Zeeb 
7243a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
7244a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
7245a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7246a7c19b8aSBjoern A. Zeeb 
7247a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7248a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7249a7c19b8aSBjoern A. Zeeb 
7250a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
7251a7c19b8aSBjoern A. Zeeb 
7252a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7253a7c19b8aSBjoern A. Zeeb }
7254a7c19b8aSBjoern A. Zeeb 
72556b4cac81SBjoern A. Zeeb uint32_t
72566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
72576b4cac81SBjoern A. Zeeb     enum nl80211_band band)
72586b4cac81SBjoern A. Zeeb {
72596b4cac81SBjoern A. Zeeb 
72606b4cac81SBjoern A. Zeeb 	switch (band) {
72616b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
72626b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
72636b4cac81SBjoern A. Zeeb 		break;
72646b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
72656b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
72666b4cac81SBjoern A. Zeeb 		break;
72676b4cac81SBjoern A. Zeeb 	default:
72686b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
72696b4cac81SBjoern A. Zeeb 		break;
72706b4cac81SBjoern A. Zeeb 	}
72716b4cac81SBjoern A. Zeeb 
72726b4cac81SBjoern A. Zeeb 	return (0);
72736b4cac81SBjoern A. Zeeb }
72746b4cac81SBjoern A. Zeeb 
72756b4cac81SBjoern A. Zeeb uint32_t
72766b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
72776b4cac81SBjoern A. Zeeb {
72786b4cac81SBjoern A. Zeeb 
72796b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
72806b4cac81SBjoern A. Zeeb }
72816b4cac81SBjoern A. Zeeb 
7282ac867c20SBjoern A. Zeeb #if 0
72836b4cac81SBjoern A. Zeeb static struct lkpi_sta *
72846b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
72856b4cac81SBjoern A. Zeeb {
72866b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
72876b4cac81SBjoern A. Zeeb 
7288a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7289a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72906b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
7291a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
72926b4cac81SBjoern A. Zeeb 			return (lsta);
72936b4cac81SBjoern A. Zeeb 		}
72946b4cac81SBjoern A. Zeeb 	}
7295a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
72966b4cac81SBjoern A. Zeeb 
72976b4cac81SBjoern A. Zeeb 	return (NULL);
72986b4cac81SBjoern A. Zeeb }
7299ac867c20SBjoern A. Zeeb #endif
73006b4cac81SBjoern A. Zeeb 
73016b4cac81SBjoern A. Zeeb struct ieee80211_sta *
73026b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
73036b4cac81SBjoern A. Zeeb {
73046b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7305a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
73066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
73076b4cac81SBjoern A. Zeeb 
73086b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
73096b4cac81SBjoern A. Zeeb 
7310a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7311a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
73126b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
73136b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7314a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
73156b4cac81SBjoern A. Zeeb 			return (sta);
73166b4cac81SBjoern A. Zeeb 		}
73176b4cac81SBjoern A. Zeeb 	}
7318a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
73196b4cac81SBjoern A. Zeeb 	return (NULL);
73206b4cac81SBjoern A. Zeeb }
73216b4cac81SBjoern A. Zeeb 
73226b4cac81SBjoern A. Zeeb struct ieee80211_sta *
73232e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
73242e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
73256b4cac81SBjoern A. Zeeb {
73266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73276b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73286b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
73296b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
73306b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
73316b4cac81SBjoern A. Zeeb 
73326b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
73336b4cac81SBjoern A. Zeeb 	sta = NULL;
73346b4cac81SBjoern A. Zeeb 
73358891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
73366b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
73376b4cac81SBjoern A. Zeeb 
73386b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
73396b4cac81SBjoern A. Zeeb 
73406b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
73416b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
73426b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
73436b4cac81SBjoern A. Zeeb 			continue;
73446b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
73456b4cac81SBjoern A. Zeeb 		if (sta != NULL)
73466b4cac81SBjoern A. Zeeb 			break;
73476b4cac81SBjoern A. Zeeb 	}
73488891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
73496b4cac81SBjoern A. Zeeb 
73506b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
73516b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
73526b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
73536b4cac81SBjoern A. Zeeb 			return (NULL);
73546b4cac81SBjoern A. Zeeb 	}
73556b4cac81SBjoern A. Zeeb 
73566b4cac81SBjoern A. Zeeb 	return (sta);
73576b4cac81SBjoern A. Zeeb }
73586b4cac81SBjoern A. Zeeb 
73596b4cac81SBjoern A. Zeeb struct sk_buff *
73606b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
73616b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
73626b4cac81SBjoern A. Zeeb {
73636b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73640cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73656b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
73666b4cac81SBjoern A. Zeeb 
73670cbcfa19SBjoern A. Zeeb 	skb = NULL;
73686b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73696b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
73706b4cac81SBjoern A. Zeeb 
73710cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
73720cbcfa19SBjoern A. Zeeb 		goto stopped;
73730cbcfa19SBjoern A. Zeeb 
73740cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
73750cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
73760cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
73770cbcfa19SBjoern A. Zeeb 		goto stopped;
73780cbcfa19SBjoern A. Zeeb 	}
73790cbcfa19SBjoern A. Zeeb 
7380eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
7381eac3646fSBjoern A. Zeeb 
7382eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
73836b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
7384eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
73856b4cac81SBjoern A. Zeeb 
73860cbcfa19SBjoern A. Zeeb stopped:
73876b4cac81SBjoern A. Zeeb 	return (skb);
73886b4cac81SBjoern A. Zeeb }
73896b4cac81SBjoern A. Zeeb 
73906b4cac81SBjoern A. Zeeb void
73916b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
739286220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
73936b4cac81SBjoern A. Zeeb {
73946b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73956b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
739686220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
73976b4cac81SBjoern A. Zeeb 
73986b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73996b4cac81SBjoern A. Zeeb 
74006b4cac81SBjoern A. Zeeb 	fc = bc = 0;
7401eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
74026b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
74036b4cac81SBjoern A. Zeeb 		fc++;
74046b4cac81SBjoern A. Zeeb 		bc += skb->len;
74056b4cac81SBjoern A. Zeeb 	}
7406eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
74076b4cac81SBjoern A. Zeeb 	if (frame_cnt)
74086b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
74096b4cac81SBjoern A. Zeeb 	if (byte_cnt)
74106b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
74116b4cac81SBjoern A. Zeeb 
74126b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
74136b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
74146b4cac81SBjoern A. Zeeb 	IMPROVE();
74156b4cac81SBjoern A. Zeeb }
74166b4cac81SBjoern A. Zeeb 
74176b4cac81SBjoern A. Zeeb /*
74186b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
74196b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
74206b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
74216b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
74226b4cac81SBjoern A. Zeeb  */
7423a8397571SBjoern A. Zeeb static void
7424a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
74256b4cac81SBjoern A. Zeeb     int status)
74266b4cac81SBjoern A. Zeeb {
74276b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
74286b4cac81SBjoern A. Zeeb 	struct mbuf *m;
74296b4cac81SBjoern A. Zeeb 
74306b4cac81SBjoern A. Zeeb 	m = skb->m;
74316b4cac81SBjoern A. Zeeb 	skb->m = NULL;
74326b4cac81SBjoern A. Zeeb 
74336b4cac81SBjoern A. Zeeb 	if (m != NULL) {
74346b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
74356b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
74366b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
74376b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
74386b4cac81SBjoern A. Zeeb 	}
7439a8397571SBjoern A. Zeeb }
74406b4cac81SBjoern A. Zeeb 
7441a8397571SBjoern A. Zeeb void
7442a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7443a8397571SBjoern A. Zeeb     int status)
7444a8397571SBjoern A. Zeeb {
7445a8397571SBjoern A. Zeeb 
7446a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
74476b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
74486b4cac81SBjoern A. Zeeb }
74496b4cac81SBjoern A. Zeeb 
7450383b3e8fSBjoern A. Zeeb void
7451a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7452a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
7453383b3e8fSBjoern A. Zeeb {
7454a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
7455383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
7456383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
7457383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
7458383b3e8fSBjoern A. Zeeb 	int status;
7459383b3e8fSBjoern A. Zeeb 
7460a8397571SBjoern A. Zeeb 	skb = txstat->skb;
7461383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
7462383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
7463383b3e8fSBjoern A. Zeeb 
7464383b3e8fSBjoern A. Zeeb 		m = skb->m;
7465383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
7466383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
7467383b3e8fSBjoern A. Zeeb 	} else {
7468383b3e8fSBjoern A. Zeeb 		ni = NULL;
7469383b3e8fSBjoern A. Zeeb 	}
7470383b3e8fSBjoern A. Zeeb 
7471a8397571SBjoern A. Zeeb 	info = txstat->info;
7472383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7473383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
7474383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7475383b3e8fSBjoern A. Zeeb 	} else {
7476383b3e8fSBjoern A. Zeeb 		status = 1;
7477383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7478383b3e8fSBjoern A. Zeeb 	}
7479383b3e8fSBjoern A. Zeeb 
7480383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
7481383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
7482383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7483383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
7484383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7485383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7486383b3e8fSBjoern A. Zeeb 		}
7487383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
7488a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7489383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
7490383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7491383b3e8fSBjoern A. Zeeb #endif
7492adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7493383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7494383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7495383b3e8fSBjoern A. Zeeb 		}
7496383b3e8fSBjoern A. Zeeb 
7497d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7498383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
749946de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7500383b3e8fSBjoern A. Zeeb 
7501383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7502383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7503d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
750446de4d9fSAdrian Chadd 			    txs.long_retries);
7505383b3e8fSBjoern A. Zeeb 		}
7506383b3e8fSBjoern A. Zeeb #endif
7507383b3e8fSBjoern A. Zeeb 	}
7508383b3e8fSBjoern A. Zeeb 
7509383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7510383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7511383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7512383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7513383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7514383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7515adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7516383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7517383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7518383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7519383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7520383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7521383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7522383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7523383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7524383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7525383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7526383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7527383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7528383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7529adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7530383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7531383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7532383b3e8fSBjoern A. Zeeb #endif
7533383b3e8fSBjoern A. Zeeb 
7534a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7535a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7536a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7537a8397571SBjoern A. Zeeb 	} else {
7538383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7539383b3e8fSBjoern A. Zeeb 	}
7540a8397571SBjoern A. Zeeb }
7541a8397571SBjoern A. Zeeb 
7542a8397571SBjoern A. Zeeb void
7543a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7544a8397571SBjoern A. Zeeb {
7545a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7546a8397571SBjoern A. Zeeb 
7547a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7548a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7549a8397571SBjoern A. Zeeb 	status.skb = skb;
7550a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7551a8397571SBjoern A. Zeeb 
7552a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7553a8397571SBjoern A. Zeeb }
7554383b3e8fSBjoern A. Zeeb 
75556b4cac81SBjoern A. Zeeb /*
75566b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
75576b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
75586b4cac81SBjoern A. Zeeb  * mbufs this should go away.
75596b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
75606b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
75616b4cac81SBjoern A. Zeeb  */
75626b4cac81SBjoern A. Zeeb static void
75636b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
75646b4cac81SBjoern A. Zeeb {
75656b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
75666b4cac81SBjoern A. Zeeb 	struct mbuf *m;
75676b4cac81SBjoern A. Zeeb 
75686b4cac81SBjoern A. Zeeb 	if (p == NULL)
75696b4cac81SBjoern A. Zeeb 		return;
75706b4cac81SBjoern A. Zeeb 
75716b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
75726b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
75736b4cac81SBjoern A. Zeeb 
75746b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
75756b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
75766b4cac81SBjoern A. Zeeb 	if (ni != NULL)
75776b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
75786b4cac81SBjoern A. Zeeb 	m_freem(m);
75796b4cac81SBjoern A. Zeeb }
75806b4cac81SBjoern A. Zeeb 
75816b4cac81SBjoern A. Zeeb void
75826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
75836b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
75846b4cac81SBjoern A. Zeeb {
75856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75866b4cac81SBjoern A. Zeeb 
75876b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
75886b4cac81SBjoern A. Zeeb 	IMPROVE();
75896b4cac81SBjoern A. Zeeb 
75906b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
75916b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
75926b4cac81SBjoern A. Zeeb }
75936b4cac81SBjoern A. Zeeb 
75946b4cac81SBjoern A. Zeeb void
75956b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
75966b4cac81SBjoern A. Zeeb     struct work_struct *w)
75976b4cac81SBjoern A. Zeeb {
75986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75996b4cac81SBjoern A. Zeeb 
76006b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
76016b4cac81SBjoern A. Zeeb 	IMPROVE();
76026b4cac81SBjoern A. Zeeb 
76036b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
76046b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
76056b4cac81SBjoern A. Zeeb }
76066b4cac81SBjoern A. Zeeb 
76076b4cac81SBjoern A. Zeeb struct sk_buff *
7608ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7609ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7610ade774b1SBjoern A. Zeeb {
7611ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7612ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7613ade774b1SBjoern A. Zeeb 	uint8_t *p;
7614ade774b1SBjoern A. Zeeb 	size_t len;
7615ade774b1SBjoern A. Zeeb 
7616ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7617ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7618ade774b1SBjoern A. Zeeb 
7619ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7620ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7621ade774b1SBjoern A. Zeeb 		return (NULL);
7622ade774b1SBjoern A. Zeeb 
7623ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7624ade774b1SBjoern A. Zeeb 
7625ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7626ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7627ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7628ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7629ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7630ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7631ade774b1SBjoern A. Zeeb 
7632ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7633ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7634ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7635ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7636ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7637ade774b1SBjoern A. Zeeb 
7638ade774b1SBjoern A. Zeeb 	return (skb);
7639ade774b1SBjoern A. Zeeb }
7640ade774b1SBjoern A. Zeeb 
7641ade774b1SBjoern A. Zeeb struct sk_buff *
76426b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
76436b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
76446b4cac81SBjoern A. Zeeb {
76456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76466b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
76476b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
76486b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
76496b4cac81SBjoern A. Zeeb 	uint16_t v;
76506b4cac81SBjoern A. Zeeb 
76516b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
76526b4cac81SBjoern A. Zeeb 	if (skb == NULL)
76536b4cac81SBjoern A. Zeeb 		return (NULL);
76546b4cac81SBjoern A. Zeeb 
76556b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
76566b4cac81SBjoern A. Zeeb 
76576b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76586b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
76596b4cac81SBjoern A. Zeeb 
76606b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
76616b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
76626b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7663616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
76646b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
76656b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
76666b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
76676b4cac81SBjoern A. Zeeb 
76686b4cac81SBjoern A. Zeeb 	return (skb);
76696b4cac81SBjoern A. Zeeb }
76706b4cac81SBjoern A. Zeeb 
76716b4cac81SBjoern A. Zeeb struct sk_buff *
76726b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7673adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
76746b4cac81SBjoern A. Zeeb {
76756b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76766b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
76776b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
76786b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
76796b4cac81SBjoern A. Zeeb 
7680adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7681adff403fSBjoern A. Zeeb 
76826b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
76836b4cac81SBjoern A. Zeeb 	if (skb == NULL)
76846b4cac81SBjoern A. Zeeb 		return (NULL);
76856b4cac81SBjoern A. Zeeb 
76866b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
76876b4cac81SBjoern A. Zeeb 
76886b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76896b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
76906b4cac81SBjoern A. Zeeb 
76916b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
76926b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
76936b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
76946b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
76956b4cac81SBjoern A. Zeeb 
76966b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
76976b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
76986b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
76996b4cac81SBjoern A. Zeeb 
77006b4cac81SBjoern A. Zeeb 	return (skb);
77016b4cac81SBjoern A. Zeeb }
77026b4cac81SBjoern A. Zeeb 
77036b4cac81SBjoern A. Zeeb struct wireless_dev *
77046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
77056b4cac81SBjoern A. Zeeb {
77066b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
77076b4cac81SBjoern A. Zeeb 
77086b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
77096b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
77106b4cac81SBjoern A. Zeeb }
77116b4cac81SBjoern A. Zeeb 
77126b4cac81SBjoern A. Zeeb void
77136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
77146b4cac81SBjoern A. Zeeb {
77156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
77166b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
77176b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
77186b4cac81SBjoern A. Zeeb 	int arg;
77196b4cac81SBjoern A. Zeeb 
77206b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
77216b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
77226b4cac81SBjoern A. Zeeb 
77236b4cac81SBjoern A. Zeeb 	/*
7724f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
77256b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
77266b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
77276b4cac81SBjoern A. Zeeb 	 */
7728f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7729bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
77306b4cac81SBjoern A. Zeeb 
7731018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7732018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
77336b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
77346b4cac81SBjoern A. Zeeb }
77356b4cac81SBjoern A. Zeeb 
7736bb81db90SBjoern A. Zeeb void
7737bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7738bb81db90SBjoern A. Zeeb {
7739bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7740bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7741bb81db90SBjoern A. Zeeb 
7742bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7743bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7744bb81db90SBjoern A. Zeeb 
7745bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7746bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
77473540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7748bb81db90SBjoern A. Zeeb }
7749bb81db90SBjoern A. Zeeb 
77505edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
77515edde07cSBjoern A. Zeeb 
77525a9a0d78SBjoern A. Zeeb void
77535a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
77545a9a0d78SBjoern A. Zeeb {
77555a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77565a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
77575a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
77585a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
77595a9a0d78SBjoern A. Zeeb 
77605a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
77615a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
77625a9a0d78SBjoern A. Zeeb 
77635a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
77645a9a0d78SBjoern A. Zeeb 
77655a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
77665a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
77675a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
77685a9a0d78SBjoern A. Zeeb 	else
77695a9a0d78SBjoern A. Zeeb 		ac_count = 1;
77705a9a0d78SBjoern A. Zeeb 
77715a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
77725a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
77735a9a0d78SBjoern A. Zeeb 
77745a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
77755a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
77765a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
77775a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
77780d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
77795a9a0d78SBjoern A. Zeeb 				/*
77805a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
77815a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
77825a9a0d78SBjoern A. Zeeb 				 */
77830d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
77840d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
77855a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
77865a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
77875a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
77885a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
77890d2cb6a6SBjoern A. Zeeb #endif
77905a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
77915a9a0d78SBjoern A. Zeeb 			}
77925a9a0d78SBjoern A. Zeeb 		}
77935a9a0d78SBjoern A. Zeeb 	}
77945a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
77955a9a0d78SBjoern A. Zeeb }
77965a9a0d78SBjoern A. Zeeb 
77975a9a0d78SBjoern A. Zeeb void
77985a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
77995a9a0d78SBjoern A. Zeeb {
78005a9a0d78SBjoern A. Zeeb 	int i;
78015a9a0d78SBjoern A. Zeeb 
78025a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
78035a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
78045a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
78055a9a0d78SBjoern A. Zeeb }
78065a9a0d78SBjoern A. Zeeb 
78075a9a0d78SBjoern A. Zeeb 
78085a9a0d78SBjoern A. Zeeb static void
78095a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
78105a9a0d78SBjoern A. Zeeb {
78115a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
78125a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
78135a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
78145a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
78155a9a0d78SBjoern A. Zeeb 
78165a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
78175a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
78185a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
78195a9a0d78SBjoern A. Zeeb 	else
78205a9a0d78SBjoern A. Zeeb 		ac_count = 1;
78215a9a0d78SBjoern A. Zeeb 
78225a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
78235a9a0d78SBjoern A. Zeeb 
78245a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
78255a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
78265a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
78275a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
78285a9a0d78SBjoern A. Zeeb 
78295a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
78305a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
78315a9a0d78SBjoern A. Zeeb 
78325a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
78335a9a0d78SBjoern A. Zeeb 
78345a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
78355a9a0d78SBjoern A. Zeeb 
78360d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
78375a9a0d78SBjoern A. Zeeb 				/*
78385a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
78395a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
78405a9a0d78SBjoern A. Zeeb 				 */
78410d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
78420d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
78435a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
78445a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
78455a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
78465a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
78470d2cb6a6SBjoern A. Zeeb #endif
78485a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
78495a9a0d78SBjoern A. Zeeb 
7850a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7851a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
78525a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
78535a9a0d78SBjoern A. Zeeb 
78545a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
78555a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
78565a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
78575a9a0d78SBjoern A. Zeeb 
78585a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
78595a9a0d78SBjoern A. Zeeb 							continue;
78605a9a0d78SBjoern A. Zeeb 
78615a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
78625a9a0d78SBjoern A. Zeeb 							continue;
78635a9a0d78SBjoern A. Zeeb 
78645a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
78655a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
78665a9a0d78SBjoern A. Zeeb 							continue;
78675a9a0d78SBjoern A. Zeeb 
78685a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
78695a9a0d78SBjoern A. Zeeb 
78705a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
78715a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
78725a9a0d78SBjoern A. Zeeb 					}
78735a9a0d78SBjoern A. Zeeb 				}
7874a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
78755a9a0d78SBjoern A. Zeeb 			}
78765a9a0d78SBjoern A. Zeeb 		}
78775a9a0d78SBjoern A. Zeeb 	}
78785a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
78795a9a0d78SBjoern A. Zeeb }
78805a9a0d78SBjoern A. Zeeb 
78815a9a0d78SBjoern A. Zeeb void
78825a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
78835a9a0d78SBjoern A. Zeeb {
78845a9a0d78SBjoern A. Zeeb 	int i;
78855a9a0d78SBjoern A. Zeeb 
78865a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
78875a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
78885a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
78895a9a0d78SBjoern A. Zeeb }
78905a9a0d78SBjoern A. Zeeb 
78915a9a0d78SBjoern A. Zeeb void
78925a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
78935a9a0d78SBjoern A. Zeeb {
78945a9a0d78SBjoern A. Zeeb 
78955a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
78965a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
78975a9a0d78SBjoern A. Zeeb 
78985a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
78995a9a0d78SBjoern A. Zeeb }
79005a9a0d78SBjoern A. Zeeb 
79015a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
79025a9a0d78SBjoern A. Zeeb void
79035a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
79045a9a0d78SBjoern A. Zeeb {
79055a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
79065a9a0d78SBjoern A. Zeeb 
79075a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
79085a9a0d78SBjoern A. Zeeb 
79095a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
79105a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
79115a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
79125a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
79135a9a0d78SBjoern A. Zeeb }
79145a9a0d78SBjoern A. Zeeb 
79155a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
79165a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
79175a9a0d78SBjoern A. Zeeb {
79185a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
79195a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
79205a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
79215a9a0d78SBjoern A. Zeeb 
79225a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
79235a9a0d78SBjoern A. Zeeb 	txq = NULL;
79245a9a0d78SBjoern A. Zeeb 
79255a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
79265a9a0d78SBjoern A. Zeeb 
79275a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
79285a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
79295a9a0d78SBjoern A. Zeeb 		goto out;
79305a9a0d78SBjoern A. Zeeb 
79315a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
79325a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
79335a9a0d78SBjoern A. Zeeb 		goto out;
79345a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
79355a9a0d78SBjoern A. Zeeb 		goto out;
79365a9a0d78SBjoern A. Zeeb 
79375a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
79385a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
79395a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
79405a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
79415a9a0d78SBjoern A. Zeeb 
79425a9a0d78SBjoern A. Zeeb out:
79435a9a0d78SBjoern A. Zeeb 	return (txq);
79445a9a0d78SBjoern A. Zeeb }
79455a9a0d78SBjoern A. Zeeb 
79465a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
79475a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
79485a9a0d78SBjoern A. Zeeb {
79495a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
79505a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7951eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
79525a9a0d78SBjoern A. Zeeb 
79535a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
79545a9a0d78SBjoern A. Zeeb 
79555a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
79565a9a0d78SBjoern A. Zeeb 
79575a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7958eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7959eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7960eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7961eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
79625a9a0d78SBjoern A. Zeeb 		goto out;
79635a9a0d78SBjoern A. Zeeb 
796441b746e0SAustin Shafer 	/*
796541b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
796641b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
796741b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
796841b746e0SAustin Shafer 	 */
796941b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
79705a9a0d78SBjoern A. Zeeb 		goto out;
79715a9a0d78SBjoern A. Zeeb 
79725a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
79735a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
79745a9a0d78SBjoern A. Zeeb out:
79755a9a0d78SBjoern A. Zeeb 	return;
79765a9a0d78SBjoern A. Zeeb }
79775a9a0d78SBjoern A. Zeeb 
7978eac3646fSBjoern A. Zeeb void
7979eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7980eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7981eac3646fSBjoern A. Zeeb {
7982eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7983eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7984eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7985eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7986eac3646fSBjoern A. Zeeb 
7987eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7988eac3646fSBjoern A. Zeeb 
7989eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7990eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7991eac3646fSBjoern A. Zeeb 	do {
7992eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7993eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7994eac3646fSBjoern A. Zeeb 			break;
7995eac3646fSBjoern A. Zeeb 
7996eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7997eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7998eac3646fSBjoern A. Zeeb 		do {
7999eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
8000eac3646fSBjoern A. Zeeb 			if (skb == NULL)
8001eac3646fSBjoern A. Zeeb 				break;
8002eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
8003eac3646fSBjoern A. Zeeb 		} while(1);
8004eac3646fSBjoern A. Zeeb 
8005eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
8006eac3646fSBjoern A. Zeeb 	} while (1);
8007eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
8008eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
8009eac3646fSBjoern A. Zeeb }
8010eac3646fSBjoern A. Zeeb 
80115a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
80125a9a0d78SBjoern A. Zeeb 
80135edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
80145edde07cSBjoern A. Zeeb 	u_int refcnt;
80155edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
80165edde07cSBjoern A. Zeeb };
80175edde07cSBjoern A. Zeeb 
80185edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
80195edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
80205edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
80215edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
80225edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
80235edde07cSBjoern A. Zeeb 	size_t ssid_len;
80245edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
80255edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
80265edde07cSBjoern A. Zeeb 
80275edde07cSBjoern A. Zeeb 	/*
80285edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
80295edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
80305edde07cSBjoern A. Zeeb 	 */
80315edde07cSBjoern A. Zeeb 	bool match;
80325edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
80335edde07cSBjoern A. Zeeb };
80345edde07cSBjoern A. Zeeb 
80355edde07cSBjoern A. Zeeb static void
80365edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
80375edde07cSBjoern A. Zeeb {
80385edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
80395edde07cSBjoern A. Zeeb 	size_t ielen;
80405edde07cSBjoern A. Zeeb 
80415edde07cSBjoern A. Zeeb 	lookup = arg;
80425edde07cSBjoern A. Zeeb 
80435edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
80445edde07cSBjoern A. Zeeb 	if (lookup->match)
80455edde07cSBjoern A. Zeeb 		return;
80465edde07cSBjoern A. Zeeb 
80475edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
80485edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
80495edde07cSBjoern A. Zeeb 		return;
80505edde07cSBjoern A. Zeeb 
80515edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
80525edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
80535edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
80545edde07cSBjoern A. Zeeb 		return;
80555edde07cSBjoern A. Zeeb 	}
80565edde07cSBjoern A. Zeeb 
80575edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
80585edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
80595edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
80605edde07cSBjoern A. Zeeb 		return;
80615edde07cSBjoern A. Zeeb 	}
80625edde07cSBjoern A. Zeeb 
80635edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
80645edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
80655edde07cSBjoern A. Zeeb 
80665edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
80675edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
80685edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
80695edde07cSBjoern A. Zeeb 			return;
80705edde07cSBjoern A. Zeeb 	}
80715edde07cSBjoern A. Zeeb 
80725edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
80735edde07cSBjoern A. Zeeb 		return;
80745edde07cSBjoern A. Zeeb 
80755edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
80765edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
80775edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
80785edde07cSBjoern A. Zeeb 			return;
80795edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
80805edde07cSBjoern A. Zeeb 			return;
80815edde07cSBjoern A. Zeeb 	}
80825edde07cSBjoern A. Zeeb 
80835edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
80845edde07cSBjoern A. Zeeb 
80855edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
80865edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
80875edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
80885edde07cSBjoern A. Zeeb 		return;
80895edde07cSBjoern A. Zeeb 
80905edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
80915edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
80925edde07cSBjoern A. Zeeb 	if (ielen)
80935edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
80945edde07cSBjoern A. Zeeb 
80955edde07cSBjoern A. Zeeb 	lookup->match = true;
80965edde07cSBjoern A. Zeeb }
80975edde07cSBjoern A. Zeeb 
80985edde07cSBjoern A. Zeeb struct cfg80211_bss *
80995edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
81005edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
81015edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
81025edde07cSBjoern A. Zeeb {
81035edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
81045edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
81055edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
81065edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
81075edde07cSBjoern A. Zeeb 
81085edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
81095edde07cSBjoern A. Zeeb 
81105edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
81115edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
81125edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
81135edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
81145edde07cSBjoern A. Zeeb 		return (NULL);
81155edde07cSBjoern A. Zeeb 	}
81165edde07cSBjoern A. Zeeb 
81175edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
81185edde07cSBjoern A. Zeeb 	lookup.chan = chan;
81195edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
81205edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
81215edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
81225edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
81235edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
81245edde07cSBjoern A. Zeeb 	lookup.match = false;
81255edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
81265edde07cSBjoern A. Zeeb 
81275edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
81285edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
81295edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
81305edde07cSBjoern A. Zeeb 	if (!lookup.match) {
81315edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
81325edde07cSBjoern A. Zeeb 		return (NULL);
81335edde07cSBjoern A. Zeeb 	}
81345edde07cSBjoern A. Zeeb 
81355edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
81365edde07cSBjoern A. Zeeb 	return (&lbss->bss);
81375edde07cSBjoern A. Zeeb }
81385edde07cSBjoern A. Zeeb 
81395edde07cSBjoern A. Zeeb void
81405edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
81415edde07cSBjoern A. Zeeb {
81425edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
81435edde07cSBjoern A. Zeeb 
81445edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
81455edde07cSBjoern A. Zeeb 
81465edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
81475edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
81485edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
81495edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
81505edde07cSBjoern A. Zeeb 	}
81515edde07cSBjoern A. Zeeb }
81525edde07cSBjoern A. Zeeb 
81535edde07cSBjoern A. Zeeb void
81545edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
81555edde07cSBjoern A. Zeeb {
81565edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
81575edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
81585edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
81595edde07cSBjoern A. Zeeb 
81605edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
81615edde07cSBjoern A. Zeeb 	ic = lhw->ic;
81625edde07cSBjoern A. Zeeb 
81635edde07cSBjoern A. Zeeb 	/*
81645edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
81655edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
81665edde07cSBjoern A. Zeeb 	 */
81675edde07cSBjoern A. Zeeb 	if (ic == NULL ||
81685edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
81695edde07cSBjoern A. Zeeb 		return;
81705edde07cSBjoern A. Zeeb 
81715edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
81725edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
81735edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
81745edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
81755edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
81765edde07cSBjoern A. Zeeb }
81775edde07cSBjoern A. Zeeb 
81785edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
81795edde07cSBjoern A. Zeeb 
8180ac1d519cSBjoern A. Zeeb /*
8181ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
8182ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
8183ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
8184ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
8185ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
8186ac1d519cSBjoern A. Zeeb  */
8187ac1d519cSBjoern A. Zeeb 
8188ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8189ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
8190ac1d519cSBjoern A. Zeeb {
8191ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
8192ac1d519cSBjoern A. Zeeb 	uint32_t changed;
8193ac1d519cSBjoern A. Zeeb 	int error;
8194ac1d519cSBjoern A. Zeeb 
8195ac1d519cSBjoern A. Zeeb 	changed = 0;
8196ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
8197ac1d519cSBjoern A. Zeeb 		cd = NULL;
8198ac1d519cSBjoern A. Zeeb 	else
8199ac1d519cSBjoern A. Zeeb 		cd = &new->def;
8200ac1d519cSBjoern A. Zeeb 
8201ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
8202ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
8203ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
8204ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8205ac1d519cSBjoern A. Zeeb 	}
8206ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8207ac1d519cSBjoern A. Zeeb 
8208ac1d519cSBjoern A. Zeeb 	if (changed == 0)
8209ac1d519cSBjoern A. Zeeb 		return (0);
8210ac1d519cSBjoern A. Zeeb 
8211ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
8212ac1d519cSBjoern A. Zeeb 	return (error);
8213ac1d519cSBjoern A. Zeeb }
8214ac1d519cSBjoern A. Zeeb 
8215ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8216ac1d519cSBjoern A. Zeeb 
82176b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
82186b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
82196b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8220