xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision a6413bce4e36e4176bbcf16528cc3aeb834965ba)
16b4cac81SBjoern A. Zeeb /*-
2ec6185c5SBjoern A. Zeeb  * Copyright (c) 2020-2025 The FreeBSD Foundation
3c272abc5SBjoern A. Zeeb  * Copyright (c) 2020-2025 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
4211db70b6SBjoern A. Zeeb /*
4311db70b6SBjoern A. Zeeb  * TODO:
4411db70b6SBjoern A. Zeeb  * - lots :)
4511db70b6SBjoern A. Zeeb  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
4611db70b6SBjoern A. Zeeb  */
4711db70b6SBjoern A. Zeeb 
486b4cac81SBjoern A. Zeeb #include <sys/param.h>
496b4cac81SBjoern A. Zeeb #include <sys/types.h>
506b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
516b4cac81SBjoern A. Zeeb #include <sys/errno.h>
526b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
536b4cac81SBjoern A. Zeeb #include <sys/module.h>
546b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
5540839418SBjoern A. Zeeb #include <sys/sbuf.h>
566b4cac81SBjoern A. Zeeb #include <sys/socket.h>
576b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
586b4cac81SBjoern A. Zeeb #include <sys/queue.h>
596b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
60527687a9SBjoern A. Zeeb #include <sys/libkern.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net/if.h>
636b4cac81SBjoern A. Zeeb #include <net/if_var.h>
646b4cac81SBjoern A. Zeeb #include <net/if_media.h>
656b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
686b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
696b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
706b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
719fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
746b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
756b4cac81SBjoern A. Zeeb 
766b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
77a8f735a6SBjoern A. Zeeb #include <linux/rculist.h>
786b4cac81SBjoern A. Zeeb #include "linux_80211.h"
796b4cac81SBjoern A. Zeeb 
804a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
8111db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
8275d23d88SBjoern A. Zeeb #define	LKPI_80211_HT
832c44f1ffSBjoern A. Zeeb #define	LKPI_80211_VHT
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
317a1adefb1SBjoern A. Zeeb 		/* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
318a1adefb1SBjoern A. Zeeb 		if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
319a1adefb1SBjoern A. Zeeb 		    (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
320a1adefb1SBjoern A. Zeeb 			memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
321a1adefb1SBjoern A. Zeeb 			sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
322a1adefb1SBjoern A. Zeeb 		}
323a1adefb1SBjoern A. Zeeb 
32440839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
32640839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32940839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
33040839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
33140839418SBjoern A. Zeeb 
33240839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
33340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
33440839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
33540839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
33640839418SBjoern A. Zeeb 
33740839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33840839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33940839418SBjoern A. Zeeb 
34040839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
34140839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
34440839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
34540839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
34640839418SBjoern A. Zeeb 		}
34740839418SBjoern A. Zeeb 
34840839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34940839418SBjoern A. Zeeb 
35040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35140839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35240839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
35340839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
35440839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
35540839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35640839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35740839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35840839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35940839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
36040839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
36140839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
36240839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
36340839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
36440839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
36540839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
36640839418SBjoern A. Zeeb 	}
36740839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36840839418SBjoern A. Zeeb 
36940839418SBjoern A. Zeeb 	sbuf_finish(&s);
37040839418SBjoern A. Zeeb 	sbuf_delete(&s);
37140839418SBjoern A. Zeeb 
37240839418SBjoern A. Zeeb 	return (0);
37340839418SBjoern A. Zeeb }
37440839418SBjoern A. Zeeb 
37562d51a43SBjoern A. Zeeb static enum ieee80211_sta_rx_bw
37662d51a43SBjoern A. Zeeb lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
37762d51a43SBjoern A. Zeeb {
37862d51a43SBjoern A. Zeeb 	switch (cw) {
37962d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_320:
38062d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_320);
38162d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_160:
38262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80P80:
38362d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_160);
38462d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80:
38562d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_80);
38662d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_40:
38762d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_40);
38862d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20:
38962d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20_NOHT:
39062d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39162d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_5:
39262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_10:
39362d51a43SBjoern A. Zeeb 		/* Unsupported input. */
39462d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39562d51a43SBjoern A. Zeeb 	}
39662d51a43SBjoern A. Zeeb }
39762d51a43SBjoern A. Zeeb 
39862d51a43SBjoern A. Zeeb static enum nl80211_chan_width
39962d51a43SBjoern A. Zeeb lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bw rx_bw)
40062d51a43SBjoern A. Zeeb {
40162d51a43SBjoern A. Zeeb 	switch (rx_bw) {
40262d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_20:
40362d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_20);	/* _NOHT */
40462d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_40:
40562d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_40);
40662d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_80:
40762d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_80);
40862d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_160:
40962d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_160); /* 80P80 */
41062d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_320:
41162d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_320);
41262d51a43SBjoern A. Zeeb 	}
41362d51a43SBjoern A. Zeeb }
41462d51a43SBjoern A. Zeeb 
41562d51a43SBjoern A. Zeeb static void
41662d51a43SBjoern A. Zeeb lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
41762d51a43SBjoern A. Zeeb     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
41862d51a43SBjoern A. Zeeb {
41962d51a43SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
42062d51a43SBjoern A. Zeeb 	enum ieee80211_sta_rx_bw old_bw;
42162d51a43SBjoern A. Zeeb 	uint32_t changed;
42262d51a43SBjoern A. Zeeb 
42362d51a43SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
42462d51a43SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
42562d51a43SBjoern A. Zeeb 	if (chanctx_conf == NULL)
42662d51a43SBjoern A. Zeeb 		return;
42762d51a43SBjoern A. Zeeb 
42862d51a43SBjoern A. Zeeb 	old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
42962d51a43SBjoern A. Zeeb 	if (old_bw == sta->deflink.bandwidth)
43062d51a43SBjoern A. Zeeb 		return;
43162d51a43SBjoern A. Zeeb 
43262d51a43SBjoern A. Zeeb 	chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
43362d51a43SBjoern A. Zeeb 	if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
43462d51a43SBjoern A. Zeeb 	    !sta->deflink.ht_cap.ht_supported)
43562d51a43SBjoern A. Zeeb 		chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
43662d51a43SBjoern A. Zeeb 
43762d51a43SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
43862d51a43SBjoern A. Zeeb 
43962d51a43SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
44062d51a43SBjoern A. Zeeb 
44162d51a43SBjoern A. Zeeb 	changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
44262d51a43SBjoern A. Zeeb 	changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
44362d51a43SBjoern A. Zeeb 	lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
44462d51a43SBjoern A. Zeeb }
44562d51a43SBjoern A. Zeeb 
4469fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
4479fb91463SBjoern A. Zeeb static void
44862d51a43SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
44962d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
4509fb91463SBjoern A. Zeeb {
4519fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
4529fb91463SBjoern A. Zeeb 	uint8_t *ie;
4539fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
4549fb91463SBjoern A. Zeeb 	int i, rx_nss;
4559fb91463SBjoern A. Zeeb 
456f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
457f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
4589fb91463SBjoern A. Zeeb 		return;
459f9c7a07dSBjoern A. Zeeb 	}
4609fb91463SBjoern A. Zeeb 
4619fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
4629fb91463SBjoern A. Zeeb 
4639fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
4649fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
4659fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4669fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
4679fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
4689fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
4699fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
4709fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
4719fb91463SBjoern A. Zeeb 
4729fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
4739fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
4749fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
4759fb91463SBjoern A. Zeeb 		ie += 4;
4769fb91463SBjoern A. Zeeb 	ie += 2;
4779fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4789fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4799fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4809fb91463SBjoern A. Zeeb 
48162d51a43SBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
48262d51a43SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
483f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
4843e022a91SBjoern A. Zeeb 	else
4853e022a91SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
486f9c7a07dSBjoern A. Zeeb 
487f9c7a07dSBjoern A. Zeeb 	/*
488f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
489f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
490f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
491f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
492f9c7a07dSBjoern A. Zeeb 	 */
4939fb91463SBjoern A. Zeeb 	rx_nss = 0;
494f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4959fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4969fb91463SBjoern A. Zeeb 			rx_nss++;
4979fb91463SBjoern A. Zeeb 	}
498f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
499f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
5009fb91463SBjoern A. Zeeb 
501f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
502f9c7a07dSBjoern A. Zeeb 
503f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
504f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
505f9c7a07dSBjoern A. Zeeb 	else
506f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
507f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
508f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
509f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
510f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
511f9c7a07dSBjoern A. Zeeb 	}
512f9c7a07dSBjoern A. Zeeb #endif
5139fb91463SBjoern A. Zeeb }
5149fb91463SBjoern A. Zeeb #endif
5159fb91463SBjoern A. Zeeb 
5169fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5179fb91463SBjoern A. Zeeb static void
51862d51a43SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
51962d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
5209fb91463SBjoern A. Zeeb {
521f9c7a07dSBjoern A. Zeeb 	uint32_t width;
522f9c7a07dSBjoern A. Zeeb 	int rx_nss;
523f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
524f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
5259fb91463SBjoern A. Zeeb 
5265393cd34SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
5275393cd34SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
528f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
5299fb91463SBjoern A. Zeeb 		return;
530f9c7a07dSBjoern A. Zeeb 	}
5319fb91463SBjoern A. Zeeb 
532f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
533f9c7a07dSBjoern A. Zeeb 
534f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
535f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
536f9c7a07dSBjoern A. Zeeb 
5373e022a91SBjoern A. Zeeb 	/*
5383e022a91SBjoern A. Zeeb 	 * If VHT20/40 are selected do not update the bandwidth
5393e022a91SBjoern A. Zeeb 	 * from HT but stya on VHT.
5403e022a91SBjoern A. Zeeb 	 */
5413e022a91SBjoern A. Zeeb 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
5423e022a91SBjoern A. Zeeb 		goto skip_bw;
5433e022a91SBjoern A. Zeeb 
544f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
545f9c7a07dSBjoern A. Zeeb 	switch (width) {
546f9c7a07dSBjoern A. Zeeb #if 0
547f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
548f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
5499fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
550f9c7a07dSBjoern A. Zeeb 		break;
551f9c7a07dSBjoern A. Zeeb #endif
552f9c7a07dSBjoern A. Zeeb 	default:
553f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
554f9c7a07dSBjoern A. Zeeb #if 0
555f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
556f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
557f9c7a07dSBjoern A. Zeeb 		else
558f9c7a07dSBjoern A. Zeeb #endif
5599fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
5609fb91463SBjoern A. Zeeb 	}
5613e022a91SBjoern A. Zeeb skip_bw:
562f9c7a07dSBjoern A. Zeeb 
563f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
564f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
565f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
566f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
567f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
568f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
569f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
570f9c7a07dSBjoern A. Zeeb 			break;
571f9c7a07dSBjoern A. Zeeb 		}
572f9c7a07dSBjoern A. Zeeb 	}
573f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
574f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
575f9c7a07dSBjoern A. Zeeb 
576f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
577f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
578f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
579f9c7a07dSBjoern A. Zeeb 		break;
580f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
581f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
582f9c7a07dSBjoern A. Zeeb 		break;
583f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
584f9c7a07dSBjoern A. Zeeb 	default:
585f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
586f9c7a07dSBjoern A. Zeeb 		break;
587f9c7a07dSBjoern A. Zeeb 	}
5889fb91463SBjoern A. Zeeb }
5899fb91463SBjoern A. Zeeb #endif
5909fb91463SBjoern A. Zeeb 
591d9f59799SBjoern A. Zeeb static void
59262d51a43SBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
59362d51a43SBjoern A. Zeeb     struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
594f9c7a07dSBjoern A. Zeeb {
595f9c7a07dSBjoern A. Zeeb 
596f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
59762d51a43SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(vif, sta, ni);
598f9c7a07dSBjoern A. Zeeb #endif
599f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
60062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(vif, sta, ni);
601f9c7a07dSBjoern A. Zeeb #endif
60262d51a43SBjoern A. Zeeb 	/*
60362d51a43SBjoern A. Zeeb 	 * We are also called from node allocation which net80211
60462d51a43SBjoern A. Zeeb 	 * can do even on `ifconfig down`; in that case the chanctx
60562d51a43SBjoern A. Zeeb 	 * may still be valid and we get a discrepancy between
60662d51a43SBjoern A. Zeeb 	 * sta and chanctx.  Thus do not try to update the chanctx
60762d51a43SBjoern A. Zeeb 	 * when called from lkpi_lsta_alloc().
60862d51a43SBjoern A. Zeeb 	 */
60962d51a43SBjoern A. Zeeb 	if (updchnctx)
61062d51a43SBjoern A. Zeeb 		lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
611f9c7a07dSBjoern A. Zeeb }
612f9c7a07dSBjoern A. Zeeb 
613389265e3SBjoern A. Zeeb static uint8_t
614389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
615389265e3SBjoern A. Zeeb {
616389265e3SBjoern A. Zeeb 	uint8_t chains;
617389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
618389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
619389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
620389265e3SBjoern A. Zeeb 
621389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
622389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
623389265e3SBjoern A. Zeeb #endif
624389265e3SBjoern A. Zeeb 
625389265e3SBjoern A. Zeeb 	chains = 1;
626389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
627389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
628389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
629389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
630389265e3SBjoern A. Zeeb #endif
631389265e3SBjoern A. Zeeb 
632389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
633389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
634389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
635389265e3SBjoern A. Zeeb #endif
636389265e3SBjoern A. Zeeb 
637389265e3SBjoern A. Zeeb 	return (chains);
638389265e3SBjoern A. Zeeb }
639389265e3SBjoern A. Zeeb 
640f9c7a07dSBjoern A. Zeeb static void
64167674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
64267674c1cSBjoern A. Zeeb     const char *_f, int _l)
643d9f59799SBjoern A. Zeeb {
644d9f59799SBjoern A. Zeeb 
6459d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6469d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
647d9f59799SBjoern A. Zeeb 		return;
648d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
649d9f59799SBjoern A. Zeeb 		return;
650d9f59799SBjoern A. Zeeb 
651d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
65267674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
65367674c1cSBjoern A. Zeeb 	if (ni != NULL)
65467674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
655d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
656d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
65711db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
6589d9ba2b7SBjoern A. Zeeb #endif
659d9f59799SBjoern A. Zeeb }
660d9f59799SBjoern A. Zeeb 
661d9f59799SBjoern A. Zeeb static void
662d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
663d9f59799SBjoern A. Zeeb {
664d9f59799SBjoern A. Zeeb 
665cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(lsta->hw->wiphy);
666d9f59799SBjoern A. Zeeb 
667a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
668a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
669a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
670d9f59799SBjoern A. Zeeb }
671d9f59799SBjoern A. Zeeb 
6724f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
6734f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
6744f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
6754f61ef8bSBjoern A. Zeeb {
6764f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
6774f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
6784f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
6794f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
680b6b352e4SBjoern A. Zeeb 	int band, i, tid;
6814f61ef8bSBjoern A. Zeeb 
6824f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
6834f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
6844f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
6854f61ef8bSBjoern A. Zeeb 		return (NULL);
6864f61ef8bSBjoern A. Zeeb 
687a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
6884f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
6894f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
6904f61ef8bSBjoern A. Zeeb 	/*
6910936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
6920936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
6930936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
6940936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
6950936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
6960936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
6970936c648SBjoern A. Zeeb 	 * two separate allocations.
6984f61ef8bSBjoern A. Zeeb 	 */
6990936c648SBjoern A. Zeeb 	lsta->ni = ni;
7004f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
7014f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
7024f61ef8bSBjoern A. Zeeb 
7034f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7044f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7054f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
7064f61ef8bSBjoern A. Zeeb 
7074f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
708b6b352e4SBjoern A. Zeeb 
709b6b352e4SBjoern A. Zeeb 	/* TXQ */
7104f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
7114f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
7124f61ef8bSBjoern A. Zeeb 
713d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
7144f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
7154f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
7164f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
7174f61ef8bSBjoern A. Zeeb 			goto cleanup;
7184f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
7194f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
720d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
721d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
722d0d29110SBjoern A. Zeeb 				continue;
723d0d29110SBjoern A. Zeeb 			}
724d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
7254f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
7264f61ef8bSBjoern A. Zeeb 		} else {
727fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
7284f61ef8bSBjoern A. Zeeb 		}
729d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
7300cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
731d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
7324f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
7334f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
7340cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
735d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
736eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
7374f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
7384f61ef8bSBjoern A. Zeeb 	}
7394f61ef8bSBjoern A. Zeeb 
740b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
741b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
742b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
743b6b352e4SBjoern A. Zeeb 
744b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
745b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
746b6b352e4SBjoern A. Zeeb 			continue;
747b6b352e4SBjoern A. Zeeb 
748b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
74973cd1c5dSBjoern A. Zeeb 			switch (band) {
75073cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
75173cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
75273cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
75373cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
75473cd1c5dSBjoern A. Zeeb 				case 110:
75573cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
75673cd1c5dSBjoern A. Zeeb 				case 55:
75773cd1c5dSBjoern A. Zeeb 				case 20:
75873cd1c5dSBjoern A. Zeeb 				case 10:
759b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
76073cd1c5dSBjoern A. Zeeb 					break;
76173cd1c5dSBjoern A. Zeeb 				}
76273cd1c5dSBjoern A. Zeeb 				break;
76373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
76473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
76573cd1c5dSBjoern A. Zeeb 				case 240:
76673cd1c5dSBjoern A. Zeeb 				case 120:
76773cd1c5dSBjoern A. Zeeb 				case 60:
76873cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
76973cd1c5dSBjoern A. Zeeb 					break;
77073cd1c5dSBjoern A. Zeeb 				}
77173cd1c5dSBjoern A. Zeeb 				break;
77273cd1c5dSBjoern A. Zeeb 			}
773b6b352e4SBjoern A. Zeeb 		}
774b6b352e4SBjoern A. Zeeb 	}
7759fb91463SBjoern A. Zeeb 
7766ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
7779fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
778f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
7799fb91463SBjoern A. Zeeb 
78062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
7819fb91463SBjoern A. Zeeb 
782f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
783b6b352e4SBjoern A. Zeeb 
7846ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
7856ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
7866ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
7876ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
7886ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
7896ffb7bd4SBjoern A. Zeeb 	}
7906ffb7bd4SBjoern A. Zeeb 
7914f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
792fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
7934f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
794832b8e98SBjoern A. Zeeb 	mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
7950936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
7964f61ef8bSBjoern A. Zeeb 
7974f61ef8bSBjoern A. Zeeb 	return (lsta);
7984f61ef8bSBjoern A. Zeeb 
7994f61ef8bSBjoern A. Zeeb cleanup:
800eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
801eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
802eac3646fSBjoern A. Zeeb 
803eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
804eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
8054f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
806eac3646fSBjoern A. Zeeb 	}
8074f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8084f61ef8bSBjoern A. Zeeb 	return (NULL);
8094f61ef8bSBjoern A. Zeeb }
8104f61ef8bSBjoern A. Zeeb 
8110936c648SBjoern A. Zeeb static void
8120936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
8130936c648SBjoern A. Zeeb {
8140936c648SBjoern A. Zeeb 	struct mbuf *m;
8150936c648SBjoern A. Zeeb 
8160936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
8170936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
8180936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
8190936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
8200936c648SBjoern A. Zeeb 
8210936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
8220936c648SBjoern A. Zeeb 	IMPROVE();
8230936c648SBjoern A. Zeeb 
824fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
825fa4e4257SBjoern A. Zeeb 
826fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
8270936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
828fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
8290936c648SBjoern A. Zeeb 
8300936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
8310936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
8320936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
8330936c648SBjoern A. Zeeb 
8340936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
8350936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
8360936c648SBjoern A. Zeeb 	while (m != NULL) {
8370936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
8380936c648SBjoern A. Zeeb 
8390936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
8400936c648SBjoern A. Zeeb 		if (nim != NULL)
8410936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
8420936c648SBjoern A. Zeeb 		m_freem(m);
8430936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
8440936c648SBjoern A. Zeeb 	}
8450936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
8460936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
847fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
8480936c648SBjoern A. Zeeb 
8490936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
8500936c648SBjoern A. Zeeb 
8510936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
8520936c648SBjoern A. Zeeb 
8530936c648SBjoern A. Zeeb 	/* Free lsta. */
8540936c648SBjoern A. Zeeb 	lsta->ni = NULL;
8550936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
8560936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8570936c648SBjoern A. Zeeb }
8580936c648SBjoern A. Zeeb 
8590936c648SBjoern A. Zeeb 
8606b4cac81SBjoern A. Zeeb static enum nl80211_band
8616b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
8626b4cac81SBjoern A. Zeeb {
8636b4cac81SBjoern A. Zeeb 
8646b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
8656b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
8666b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
8676b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
8686b4cac81SBjoern A. Zeeb #ifdef __notyet__
8696b4cac81SBjoern A. Zeeb 	else if ()
8706b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
8716b4cac81SBjoern A. Zeeb 	else if ()
8726b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
8736b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
8746b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
8756b4cac81SBjoern A. Zeeb #endif
8766b4cac81SBjoern A. Zeeb 	else
8776b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
8786b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
8796b4cac81SBjoern A. Zeeb }
8806b4cac81SBjoern A. Zeeb 
8816b4cac81SBjoern A. Zeeb static uint32_t
8826b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
8836b4cac81SBjoern A. Zeeb {
8846b4cac81SBjoern A. Zeeb 
8856b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
8866b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
8876b4cac81SBjoern A. Zeeb 	switch (band) {
8886b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
8896b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
8906b4cac81SBjoern A. Zeeb 		break;
8916b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
8926b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
8936b4cac81SBjoern A. Zeeb 		break;
8946b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
8956b4cac81SBjoern A. Zeeb 		break;
8966b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
8976b4cac81SBjoern A. Zeeb 		break;
8986b4cac81SBjoern A. Zeeb 	default:
8996b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
9006b4cac81SBjoern A. Zeeb 		break;
9016b4cac81SBjoern A. Zeeb 	}
9026b4cac81SBjoern A. Zeeb 
9036b4cac81SBjoern A. Zeeb 	IMPROVE();
9046b4cac81SBjoern A. Zeeb 	return (0x00);
9056b4cac81SBjoern A. Zeeb }
9066b4cac81SBjoern A. Zeeb 
907e3a0b120SBjoern A. Zeeb #if 0
9086b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
9096b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
9106b4cac81SBjoern A. Zeeb {
9116b4cac81SBjoern A. Zeeb 
9126b4cac81SBjoern A. Zeeb 	switch (ac) {
9136b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
9146b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
9156b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
9166b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
9176b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
9186b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9196b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
9206b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
9216b4cac81SBjoern A. Zeeb 	default:
9226b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
9236b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9246b4cac81SBjoern A. Zeeb 	}
9256b4cac81SBjoern A. Zeeb }
926e3a0b120SBjoern A. Zeeb #endif
9276b4cac81SBjoern A. Zeeb 
9286b4cac81SBjoern A. Zeeb static enum nl80211_iftype
9296b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
9306b4cac81SBjoern A. Zeeb {
9316b4cac81SBjoern A. Zeeb 
9326b4cac81SBjoern A. Zeeb 	switch (opmode) {
9336b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
9346b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
9356b4cac81SBjoern A. Zeeb 		break;
9366b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
9376b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
9386b4cac81SBjoern A. Zeeb 		break;
9396b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
9406b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
9416b4cac81SBjoern A. Zeeb 		break;
9426b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
9436b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
9446b4cac81SBjoern A. Zeeb 		break;
9456b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
9466b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
9476b4cac81SBjoern A. Zeeb 		break;
9486b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
9496b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
9506b4cac81SBjoern A. Zeeb 		break;
9516b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
9526b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9536b4cac81SBjoern A. Zeeb 	default:
9546b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
9556b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9566b4cac81SBjoern A. Zeeb 	}
9576b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
9586b4cac81SBjoern A. Zeeb }
9596b4cac81SBjoern A. Zeeb 
960b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
96112a511c8SBjoern A. Zeeb static const char *
96212a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
96312a511c8SBjoern A. Zeeb {
96412a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
96512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
96612a511c8SBjoern A. Zeeb 		return ("WEP40");
967bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
968bf8c25f1SBjoern A. Zeeb 		return ("WEP104");
96912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
97012a511c8SBjoern A. Zeeb 		return ("TKIP");
97112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
97212a511c8SBjoern A. Zeeb 		return ("CCMP");
973bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
974bf8c25f1SBjoern A. Zeeb 		return ("CCMP_256");
97512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
97612a511c8SBjoern A. Zeeb 		return ("GCMP");
97712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
97812a511c8SBjoern A. Zeeb 		return ("GCMP_256");
979bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
980bf8c25f1SBjoern A. Zeeb 		return ("AES_CMAC");
981bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
982bf8c25f1SBjoern A. Zeeb 		return ("BIP_CMAC_256");
98312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
98412a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
98512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
98612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
98712a511c8SBjoern A. Zeeb 	default:
98812a511c8SBjoern A. Zeeb 		return ("??");
98912a511c8SBjoern A. Zeeb 	}
99012a511c8SBjoern A. Zeeb }
99112a511c8SBjoern A. Zeeb 
9926b4cac81SBjoern A. Zeeb static uint32_t
993bf8c25f1SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
994bf8c25f1SBjoern A. Zeeb     uint32_t wlan_cipher_suite)
9956b4cac81SBjoern A. Zeeb {
9966b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
9976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
9986b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
999bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
1000bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
10016b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
10026b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
10036b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
1004906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
10056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
1006bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM_256);
1007bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
1008bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_128);
1009bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
1010bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_256);
1011bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
1012bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_128);
10136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1014bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_256);
1015bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1016bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_128);
1017bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1018bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_256);
10196b4cac81SBjoern A. Zeeb 	default:
1020bf8c25f1SBjoern A. Zeeb 		ic_printf(ic, "%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
102112a511c8SBjoern A. Zeeb 		    __func__,
102212a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
102312a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
10246b4cac81SBjoern A. Zeeb 		return (0);
10256b4cac81SBjoern A. Zeeb 	}
1026bf8c25f1SBjoern A. Zeeb }
10276b4cac81SBjoern A. Zeeb 
10286b4cac81SBjoern A. Zeeb static uint32_t
10296b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
10306b4cac81SBjoern A. Zeeb {
10316b4cac81SBjoern A. Zeeb 
10326b4cac81SBjoern A. Zeeb 	switch (cipher) {
10336b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
10346b4cac81SBjoern A. Zeeb 		if (keylen < 8)
10356b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
10366b4cac81SBjoern A. Zeeb 		else
10376b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
10386b4cac81SBjoern A. Zeeb 		break;
1039bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
1040bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
1041bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
1042bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
1043bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM_256:
1044bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP_256);
1045bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_128:
1046bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP);
1047bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_256:
1048bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP_256);
1049bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_128:
1050bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_AES_CMAC);
1051bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_256:
1052bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1053bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_128:
1054bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1055bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_256:
1056bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1057bf8c25f1SBjoern A. Zeeb 
10586b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
10596b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
1060bf8c25f1SBjoern A. Zeeb 		/*
1061bf8c25f1SBjoern A. Zeeb 		 * TKIP w/ hw MIC support
1062bf8c25f1SBjoern A. Zeeb 		 * (gone wrong; should really be a crypto flag in net80211).
1063bf8c25f1SBjoern A. Zeeb 		 */
10646b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
10656b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
10666b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
10676b4cac81SBjoern A. Zeeb 		break;
10686b4cac81SBjoern A. Zeeb 	default:
10696b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
10706b4cac81SBjoern A. Zeeb 	};
10716b4cac81SBjoern A. Zeeb 	return (0);
10726b4cac81SBjoern A. Zeeb }
10736b4cac81SBjoern A. Zeeb #endif
10746b4cac81SBjoern A. Zeeb 
10756b4cac81SBjoern A. Zeeb #ifdef __notyet__
10766b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
10776b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
10786b4cac81SBjoern A. Zeeb {
10796b4cac81SBjoern A. Zeeb 
10806b4cac81SBjoern A. Zeeb 	/*
10816b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
10826b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
10836b4cac81SBjoern A. Zeeb 	 */
10846b4cac81SBjoern A. Zeeb 	switch (state) {
10856b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
10866b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
10876b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
10886b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
10896b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
10906b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
10916b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
10926b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
10936b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
10946b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
10956b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
10966b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
10976b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
10986b4cac81SBjoern A. Zeeb 	default:
10996b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
11006b4cac81SBjoern A. Zeeb 	};
11016b4cac81SBjoern A. Zeeb 
11026b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
11036b4cac81SBjoern A. Zeeb }
11046b4cac81SBjoern A. Zeeb #endif
11056b4cac81SBjoern A. Zeeb 
11066b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11076b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
11086b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
11096b4cac81SBjoern A. Zeeb {
11106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11116b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
11126b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
11136b4cac81SBjoern A. Zeeb 	int i, nchans;
11146b4cac81SBjoern A. Zeeb 
11156b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11166b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
11176b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
11186b4cac81SBjoern A. Zeeb 		return (NULL);
11196b4cac81SBjoern A. Zeeb 
11206b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
11216b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
11226b4cac81SBjoern A. Zeeb 		return (NULL);
11236b4cac81SBjoern A. Zeeb 
11246b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
11256b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
11266b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
11276b4cac81SBjoern A. Zeeb 			return (&channels[i]);
11286b4cac81SBjoern A. Zeeb 	}
11296b4cac81SBjoern A. Zeeb 
11306b4cac81SBjoern A. Zeeb 	return (NULL);
11316b4cac81SBjoern A. Zeeb }
11326b4cac81SBjoern A. Zeeb 
11332ac8a218SBjoern A. Zeeb #if 0
11346b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11356b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
11366b4cac81SBjoern A. Zeeb {
11376b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
11386b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
11396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11406b4cac81SBjoern A. Zeeb 
11416b4cac81SBjoern A. Zeeb 	chan = NULL;
11426b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
11436b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
11446b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
11456b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
11466b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
11476b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
11486b4cac81SBjoern A. Zeeb 	else
11496b4cac81SBjoern A. Zeeb 		c = NULL;
11506b4cac81SBjoern A. Zeeb 
11516b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
11526b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
11536b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
11546b4cac81SBjoern A. Zeeb 	}
11556b4cac81SBjoern A. Zeeb 
11566b4cac81SBjoern A. Zeeb 	return (chan);
11576b4cac81SBjoern A. Zeeb }
11582ac8a218SBjoern A. Zeeb #endif
11596b4cac81SBjoern A. Zeeb 
11602e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
11612e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
11622e183d99SBjoern A. Zeeb {
11632e183d99SBjoern A. Zeeb 	enum nl80211_band band;
11642e183d99SBjoern A. Zeeb 
11652e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11662e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
11672e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
11682e183d99SBjoern A. Zeeb 		int i;
11692e183d99SBjoern A. Zeeb 
11702e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
11712e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
11722e183d99SBjoern A. Zeeb 			continue;
11732e183d99SBjoern A. Zeeb 
11742e183d99SBjoern A. Zeeb 		channels = supband->channels;
11752e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
11762e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
11772e183d99SBjoern A. Zeeb 				return (&channels[i]);
11782e183d99SBjoern A. Zeeb 		}
11792e183d99SBjoern A. Zeeb 	}
11802e183d99SBjoern A. Zeeb 
11812e183d99SBjoern A. Zeeb 	return (NULL);
11822e183d99SBjoern A. Zeeb }
11832e183d99SBjoern A. Zeeb 
1184b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
11856b4cac81SBjoern A. Zeeb static int
118611db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
118711db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
118811db70b6SBjoern A. Zeeb {
118911db70b6SBjoern A. Zeeb 	int error;
119011db70b6SBjoern A. Zeeb 
119111db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
119211db70b6SBjoern A. Zeeb 		return (0);
119311db70b6SBjoern A. Zeeb 
119411db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
119511db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
119611db70b6SBjoern A. Zeeb 
119711db70b6SBjoern A. Zeeb 	error = 0;
119811db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
119911db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
120011db70b6SBjoern A. Zeeb 		int err;
120111db70b6SBjoern A. Zeeb 
120211db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
120311db70b6SBjoern A. Zeeb 			continue;
120411db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
120511db70b6SBjoern A. Zeeb 
1206a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1207a6f6329cSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1208a6f6329cSBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: running set_key cmd %d(%s) for "
1209a6f6329cSBjoern A. Zeeb 			    "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1210a6f6329cSBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1211a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1212a6f6329cSBjoern A. Zeeb #endif
1213a6f6329cSBjoern A. Zeeb 
121411db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
121511db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
121611db70b6SBjoern A. Zeeb 		if (err != 0) {
121711db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
121811db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
121911db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
122011db70b6SBjoern A. Zeeb 			error++;
122111db70b6SBjoern A. Zeeb 
122211db70b6SBjoern A. Zeeb 			/*
122311db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
122411db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
122511db70b6SBjoern A. Zeeb 			 * crash (firmware).
122611db70b6SBjoern A. Zeeb 			 */
122711db70b6SBjoern A. Zeeb 			continue;
122811db70b6SBjoern A. Zeeb 		}
122911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
123011db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
123111db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
1232a6f6329cSBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
123311db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1234a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
123511db70b6SBjoern A. Zeeb #endif
123611db70b6SBjoern A. Zeeb 
123711db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
123811db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
123911db70b6SBjoern A. Zeeb 	}
124011db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
124111db70b6SBjoern A. Zeeb 	return (error);
124211db70b6SBjoern A. Zeeb }
124311db70b6SBjoern A. Zeeb 
1244aa294e8eSBjoern A. Zeeb /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
1245aa294e8eSBjoern A. Zeeb /* See also lkpi_sta_del_keys() these days. */
124611db70b6SBjoern A. Zeeb static int
1247aa294e8eSBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
12486b4cac81SBjoern A. Zeeb {
12496b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
12506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12516b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12526b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
125311db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12546b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12556b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12566b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12576b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
12586b4cac81SBjoern A. Zeeb 	int error;
12596b4cac81SBjoern A. Zeeb 
12606b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
1261a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1262a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1263a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1264a6f6329cSBjoern A. Zeeb 		return (0);
1265a6f6329cSBjoern A. Zeeb 	}
12666b4cac81SBjoern A. Zeeb 
126711db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
126811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
126911db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
127011db70b6SBjoern A. Zeeb 		return (0);
127111db70b6SBjoern A. Zeeb 	}
1272a6f6329cSBjoern A. Zeeb 
127311db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
127411db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
127511db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
127611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
127711db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
127811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
127911db70b6SBjoern A. Zeeb 		return (0);
128011db70b6SBjoern A. Zeeb 	}
128111db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
128211db70b6SBjoern A. Zeeb 
128311db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
128411db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
128511db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
128611db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
128711db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
128811db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
128911db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
129011db70b6SBjoern A. Zeeb #endif
129111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
129211db70b6SBjoern A. Zeeb 		return (1);
129311db70b6SBjoern A. Zeeb 	}
129411db70b6SBjoern A. Zeeb 
129511db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
129611db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
129711db70b6SBjoern A. Zeeb 	if (kc == NULL) {
129811db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
129911db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
130011db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
130111db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
130211db70b6SBjoern A. Zeeb #endif
130311db70b6SBjoern A. Zeeb 		error = 1;
130411db70b6SBjoern A. Zeeb 		goto out;
130511db70b6SBjoern A. Zeeb 	}
130611db70b6SBjoern A. Zeeb 
1307a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1308a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1309a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running  set_key cmd %d(%s) for sta %6D: "
1310a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
1311a6f6329cSBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1312a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1313a6f6329cSBjoern A. Zeeb #endif
1314a6f6329cSBjoern A. Zeeb 
1315a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1316a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1317a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1318a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
131911db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
132011db70b6SBjoern A. Zeeb 	if (error != 0) {
132111db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
132211db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
132311db70b6SBjoern A. Zeeb 		error = 0;
132411db70b6SBjoern A. Zeeb 		goto out;
132511db70b6SBjoern A. Zeeb 	}
132611db70b6SBjoern A. Zeeb 
132711db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
132811db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
132911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1330a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
133111db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1332a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
133311db70b6SBjoern A. Zeeb #endif
133411db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
133511db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
133611db70b6SBjoern A. Zeeb 	error = 1;
133711db70b6SBjoern A. Zeeb out:
133811db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
133911db70b6SBjoern A. Zeeb 	return (error);
134011db70b6SBjoern A. Zeeb }
134111db70b6SBjoern A. Zeeb 
134211db70b6SBjoern A. Zeeb static int
1343aa294e8eSBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
134411db70b6SBjoern A. Zeeb {
134511db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
134611db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
134711db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
134811db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
134911db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
135011db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
135111db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
135211db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
135311db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
135411db70b6SBjoern A. Zeeb 	uint32_t lcipher;
135592942717SBjoern A. Zeeb 	uint16_t exp_flags;
13564b9ae864SBjoern A. Zeeb 	uint8_t keylen;
135711db70b6SBjoern A. Zeeb 	int error;
135811db70b6SBjoern A. Zeeb 
135911db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
1360a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1361a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1362a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1363a6f6329cSBjoern A. Zeeb 		return (0);
1364a6f6329cSBjoern A. Zeeb 	}
136511db70b6SBjoern A. Zeeb 
136611db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
136711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
136811db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
136911db70b6SBjoern A. Zeeb 		return (0);
137011db70b6SBjoern A. Zeeb 	}
137111db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
137211db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
137311db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
137411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
137511db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
137611db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
137711db70b6SBjoern A. Zeeb 		return (0);
137811db70b6SBjoern A. Zeeb 	}
137911db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
138011db70b6SBjoern A. Zeeb 
13814b9ae864SBjoern A. Zeeb 	keylen = k->wk_keylen;
13824b9ae864SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
13834b9ae864SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
13844b9ae864SBjoern A. Zeeb 	switch (lcipher) {
13854b9ae864SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
13864b9ae864SBjoern A. Zeeb 		break;
13874b9ae864SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
13884b9ae864SBjoern A. Zeeb 		keylen += 2 * k->wk_cipher->ic_miclen;
13894b9ae864SBjoern A. Zeeb 		break;
13904b9ae864SBjoern A. Zeeb 	default:
13914b9ae864SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
13924b9ae864SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
13934b9ae864SBjoern A. Zeeb 		IMPROVE();
13944b9ae864SBjoern A. Zeeb 		ieee80211_free_node(ni);
13954b9ae864SBjoern A. Zeeb 		return (0);
13964b9ae864SBjoern A. Zeeb 	}
13974b9ae864SBjoern A. Zeeb 
139811db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
139911db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
140011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
140111db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
140211db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
140311db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
140411db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
140511db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
140611db70b6SBjoern A. Zeeb 	}
140711db70b6SBjoern A. Zeeb 
14084b9ae864SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + keylen, M_LKPI80211, M_WAITOK | M_ZERO);
140911db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
141011db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
14116b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
14126b4cac81SBjoern A. Zeeb #if 0
14136b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
14146b4cac81SBjoern A. Zeeb #endif
14156b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
14166b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
14176b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
14186b4cac81SBjoern A. Zeeb 
141911db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
142011db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
142111db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
142211db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
142311db70b6SBjoern A. Zeeb 
14246b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
14256b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
14266b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
14276b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
14286b4cac81SBjoern A. Zeeb 		break;
14296b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
14304b9ae864SBjoern A. Zeeb 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, k->wk_txmic, k->wk_cipher->ic_miclen);
14314b9ae864SBjoern A. Zeeb 		memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, k->wk_rxmic, k->wk_cipher->ic_miclen);
143292942717SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
143392942717SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
143492942717SBjoern A. Zeeb 		break;
14356b4cac81SBjoern A. Zeeb 	default:
143611db70b6SBjoern A. Zeeb 		/* currently UNREACH */
14376b4cac81SBjoern A. Zeeb 		IMPROVE();
143811db70b6SBjoern A. Zeeb 		break;
14396b4cac81SBjoern A. Zeeb 	};
144011db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
14416b4cac81SBjoern A. Zeeb 
1442a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1443a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1444a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running set_key cmd %d(%s) for sta %6D: "
14454b9ae864SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u keylen %u flags %b\n", __func__,
14464b9ae864SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":", kc, kc->keyidx, kc->hw_key_idx,
14474b9ae864SBjoern A. Zeeb 		    kc->keylen, kc->flags, IEEE80211_KEY_FLAG_BITS);
1448a6f6329cSBjoern A. Zeeb #endif
1449a6f6329cSBjoern A. Zeeb 
1450a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1451a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1452a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1453a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
145411db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
145511db70b6SBjoern A. Zeeb 	if (error != 0) {
145611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
145711db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
145811db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
145911db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
146011db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
146111db70b6SBjoern A. Zeeb 		return (0);
14626b4cac81SBjoern A. Zeeb 	}
14636b4cac81SBjoern A. Zeeb 
146411db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
146511db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
146611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1467a6f6329cSBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %b\n", __func__,
146811db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
1469a6f6329cSBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
147011db70b6SBjoern A. Zeeb #endif
147111db70b6SBjoern A. Zeeb 
147292942717SBjoern A. Zeeb 	exp_flags = 0;
147392942717SBjoern A. Zeeb 	switch (kc->cipher) {
147492942717SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
147592942717SBjoern A. Zeeb 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
147692942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_PUT_IV_SPACE |
147792942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_GENERATE_MMIC |
147892942717SBjoern A. Zeeb 			IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
147992942717SBjoern A. Zeeb #define	TKIP_INVAL_COMBINATION						\
148092942717SBjoern A. Zeeb      (IEEE80211_KEY_FLAG_PUT_MIC_SPACE|IEEE80211_KEY_FLAG_GENERATE_MMIC)
148192942717SBjoern A. Zeeb 		if ((kc->flags & TKIP_INVAL_COMBINATION) == TKIP_INVAL_COMBINATION) {
148292942717SBjoern A. Zeeb 			ic_printf(ic, "%s: SET_KEY for %s returned invalid "
148392942717SBjoern A. Zeeb 			    "combination %b\n", __func__,
148492942717SBjoern A. Zeeb 			    lkpi_cipher_suite_to_name(kc->cipher),
148592942717SBjoern A. Zeeb 			    kc->flags, IEEE80211_KEY_FLAG_BITS);
148692942717SBjoern A. Zeeb 		}
148792942717SBjoern A. Zeeb #undef	TKIP_INVAL_COMBINATION
148892942717SBjoern A. Zeeb #ifdef __notyet__
148992942717SBjoern A. Zeeb 		/* Do flags surgery; special see linuxkpi_ieee80211_ifattach(). */
149092942717SBjoern A. Zeeb 		if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) != 0) {
149192942717SBjoern A. Zeeb 			k->wk_flags &= ~(IEEE80211_KEY_NOMICMGT|IEEE80211_KEY_NOMIC);
149292942717SBjoern A. Zeeb 			k->wk_flags |= IEEE80211_KEY_SWMIC;
149392942717SBjoern A. Zeeb 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC
149492942717SBjoern A. Zeeb 		}
149592942717SBjoern A. Zeeb #endif
149692942717SBjoern A. Zeeb 		break;
149792942717SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
149892942717SBjoern A. Zeeb 		exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
149992942717SBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_PUT_IV_SPACE |
150092942717SBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_GENERATE_IV |
150192942717SBjoern A. Zeeb 		    IEEE80211_KEY_FLAG_GENERATE_IV_MGMT);	/* Only needs IV geeration for MGMT frames. */
150292942717SBjoern A. Zeeb 		break;
150392942717SBjoern A. Zeeb 	}
150492942717SBjoern A. Zeeb 	if ((kc->flags & ~exp_flags) != 0)
150592942717SBjoern A. Zeeb 		ic_printf(ic, "%s: SET_KEY for %s returned unexpected key flags: "
150692942717SBjoern A. Zeeb 		    " %#06x & ~%#06x = %b\n", __func__,
150792942717SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(kc->cipher), kc->flags, exp_flags,
150892942717SBjoern A. Zeeb 		    (kc->flags & ~exp_flags), IEEE80211_KEY_FLAG_BITS);
150992942717SBjoern A. Zeeb 
151092942717SBjoern A. Zeeb #ifdef __notyet__
151192942717SBjoern A. Zeeb 	/* Do flags surgery. */
151292942717SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) == 0)
151392942717SBjoern A. Zeeb 		k->wk_flags |= IEEE80211_KEY_NOIVMGT;
151492942717SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
151592942717SBjoern A. Zeeb 		k->wk_flags |= IEEE80211_KEY_NOIV;
151692942717SBjoern A. Zeeb #endif
151792942717SBjoern A. Zeeb 
151811db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
15196b4cac81SBjoern A. Zeeb 	return (1);
15206b4cac81SBjoern A. Zeeb }
15216b4cac81SBjoern A. Zeeb 
1522b8dfc3ecSBjoern A. Zeeb static void
1523b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1524b8dfc3ecSBjoern A. Zeeb {
1525b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1526b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1527b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1528b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1529b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1530*a6413bceSBjoern A. Zeeb 	struct ieee80211_node *ni;
1531a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1532b8dfc3ecSBjoern A. Zeeb 
1533b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1534b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1535b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1536b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1537b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1538b8dfc3ecSBjoern A. Zeeb 
1539a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1540a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1541b8dfc3ecSBjoern A. Zeeb 
1542b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1543b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1544a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1545a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1546a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1547a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1548a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1549b8dfc3ecSBjoern A. Zeeb #endif
1550b8dfc3ecSBjoern A. Zeeb 
1551*a6413bceSBjoern A. Zeeb 	/*
1552*a6413bceSBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1553*a6413bceSBjoern A. Zeeb 	 */
1554*a6413bceSBjoern A. Zeeb 	/* Try to make sure the node does not go away while possibly unlocked. */
1555*a6413bceSBjoern A. Zeeb 	ni = NULL;
1556*a6413bceSBjoern A. Zeeb 	if (icislocked || ntislocked) {
1557*a6413bceSBjoern A. Zeeb 		if (vap->iv_bss != NULL)
1558*a6413bceSBjoern A. Zeeb 			ni = ieee80211_ref_node(vap->iv_bss);
1559*a6413bceSBjoern A. Zeeb 	}
1560*a6413bceSBjoern A. Zeeb 
1561a6165709SBjoern A. Zeeb 	if (icislocked)
1562a6165709SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
1563a6165709SBjoern A. Zeeb 	if (ntislocked)
1564b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1565b8dfc3ecSBjoern A. Zeeb 
1566b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1567b8dfc3ecSBjoern A. Zeeb 
1568*a6413bceSBjoern A. Zeeb 	KASSERT(lvif->key_update_iv_bss == NULL, ("%s: key_update_iv_bss not NULL %p",
1569*a6413bceSBjoern A. Zeeb 	    __func__, lvif->key_update_iv_bss));
1570*a6413bceSBjoern A. Zeeb 	lvif->key_update_iv_bss = ni;
1571*a6413bceSBjoern A. Zeeb 
1572b8dfc3ecSBjoern A. Zeeb 	/*
1573a6165709SBjoern A. Zeeb 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1574b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1575b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1576b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1577b8dfc3ecSBjoern A. Zeeb 	 */
1578a6165709SBjoern A. Zeeb 	if (icislocked)
1579a6165709SBjoern A. Zeeb 		refcount_acquire(&lvif->ic_unlocked);
1580a6165709SBjoern A. Zeeb 	if (ntislocked)
1581b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1582b8dfc3ecSBjoern A. Zeeb }
1583b8dfc3ecSBjoern A. Zeeb 
1584b8dfc3ecSBjoern A. Zeeb static void
1585b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1586b8dfc3ecSBjoern A. Zeeb {
1587b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1588b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1589b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1590b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1591b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1592a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1593b8dfc3ecSBjoern A. Zeeb 
1594b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1595b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1596b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1597b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1598b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1599b8dfc3ecSBjoern A. Zeeb 
1600a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1601a6165709SBjoern A. Zeeb 	MPASS(!icislocked);
1602a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1603a6165709SBjoern A. Zeeb 	MPASS(!ntislocked);
1604b8dfc3ecSBjoern A. Zeeb 
1605b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1606b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1607a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1608a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1609a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1610a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1611a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1612b8dfc3ecSBjoern A. Zeeb #endif
1613b8dfc3ecSBjoern A. Zeeb 
1614b8dfc3ecSBjoern A. Zeeb 	/*
1615b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1616b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1617b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1618b8dfc3ecSBjoern A. Zeeb 	 */
1619a6165709SBjoern A. Zeeb 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1620a6165709SBjoern A. Zeeb 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1621*a6413bceSBjoern A. Zeeb 
1622*a6413bceSBjoern A. Zeeb 	if (lvif->key_update_iv_bss != NULL) {
1623*a6413bceSBjoern A. Zeeb 		ieee80211_free_node(lvif->key_update_iv_bss);
1624*a6413bceSBjoern A. Zeeb 		lvif->key_update_iv_bss = NULL;
1625*a6413bceSBjoern A. Zeeb 	}
1626*a6413bceSBjoern A. Zeeb 
1627b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1628b8dfc3ecSBjoern A. Zeeb 
1629a6165709SBjoern A. Zeeb 	/*
1630a6165709SBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1631a6165709SBjoern A. Zeeb 	 * ic before nt to avoid a LOR.
1632a6165709SBjoern A. Zeeb 	 */
1633a6165709SBjoern A. Zeeb 	if (icislocked)
1634a6165709SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
1635a6165709SBjoern A. Zeeb 	if (ntislocked)
1636b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1637b8dfc3ecSBjoern A. Zeeb }
16386b4cac81SBjoern A. Zeeb #endif
16396b4cac81SBjoern A. Zeeb 
16406b4cac81SBjoern A. Zeeb static u_int
16416b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
16426b4cac81SBjoern A. Zeeb {
16436b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
16446b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
16456b4cac81SBjoern A. Zeeb 
16466b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
16476b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
16486b4cac81SBjoern A. Zeeb 
16496b4cac81SBjoern A. Zeeb 	mc_list = arg;
16506b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
16516b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
16526b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
16536b4cac81SBjoern A. Zeeb 			return (0);
16546b4cac81SBjoern A. Zeeb 	}
16556b4cac81SBjoern A. Zeeb 
16566b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
16576b4cac81SBjoern A. Zeeb 	if (addr == NULL)
16586b4cac81SBjoern A. Zeeb 		return (0);
16596b4cac81SBjoern A. Zeeb 
16606b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
16616b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
16626b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
16636b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
16646b4cac81SBjoern A. Zeeb 	mc_list->count++;
16656b4cac81SBjoern A. Zeeb 
16669d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16679d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
16686b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
16696b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
16709d9ba2b7SBjoern A. Zeeb #endif
16716b4cac81SBjoern A. Zeeb 
16726b4cac81SBjoern A. Zeeb 	return (1);
16736b4cac81SBjoern A. Zeeb }
16746b4cac81SBjoern A. Zeeb 
16756b4cac81SBjoern A. Zeeb static void
16766b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
16776b4cac81SBjoern A. Zeeb {
16786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16796b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16806b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
16816b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
16826b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
16836b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
16846b4cac81SBjoern A. Zeeb 	u64 mc;
16856b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
16866b4cac81SBjoern A. Zeeb 
16876b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
16886b4cac81SBjoern A. Zeeb 
16896b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
16906b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
16916b4cac81SBjoern A. Zeeb 		return;
16926b4cac81SBjoern A. Zeeb 
16936b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
16946b4cac81SBjoern A. Zeeb 		return;
16956b4cac81SBjoern A. Zeeb 
16966b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
16976b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
16986b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
16996b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
17006b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
17016b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
17026b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
17036b4cac81SBjoern A. Zeeb 	} else {
17046b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
17056b4cac81SBjoern A. Zeeb 	}
17066b4cac81SBjoern A. Zeeb 
17076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17086b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
17096b4cac81SBjoern A. Zeeb 	/*
17106b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
17116b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
17126b4cac81SBjoern A. Zeeb 	 */
17136b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
17146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
17156b4cac81SBjoern A. Zeeb 
17169d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17179d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
17186b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
17196b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
17209d9ba2b7SBjoern A. Zeeb #endif
17216b4cac81SBjoern A. Zeeb 
17226b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
17236b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
17246b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
17256b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
17266b4cac81SBjoern A. Zeeb 			mc_list.count--;
17276b4cac81SBjoern A. Zeeb 		}
17286b4cac81SBjoern A. Zeeb 	}
17296b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
17306b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
17316b4cac81SBjoern A. Zeeb }
17326b4cac81SBjoern A. Zeeb 
1733fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1734fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1735fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1736fa8f007dSBjoern A. Zeeb {
1737fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1738fa8f007dSBjoern A. Zeeb 
1739fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1740fa8f007dSBjoern A. Zeeb 
17419d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17429d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1743fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1744fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1745ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1746fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1747616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1748fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1749fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1750fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1751fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1752ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
17539d9ba2b7SBjoern A. Zeeb #endif
1754fa8f007dSBjoern A. Zeeb 
1755fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1756fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1757fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1758fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1759fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1760fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1761fa8f007dSBjoern A. Zeeb 	}
1762fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1763fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1764fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1765fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1766fa8f007dSBjoern A. Zeeb 	}
1767fa8f007dSBjoern A. Zeeb 
1768fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1769fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1770fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1771fa8f007dSBjoern A. Zeeb 
17729d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17739d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1774fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1775fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1776ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1777fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1778616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1779fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1780fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1781fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1782fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1783ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
17849d9ba2b7SBjoern A. Zeeb #endif
1785fa8f007dSBjoern A. Zeeb 
1786fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1787fa8f007dSBjoern A. Zeeb }
1788fa8f007dSBjoern A. Zeeb 
17896b4cac81SBjoern A. Zeeb static void
17906b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
17916b4cac81SBjoern A. Zeeb {
17926b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17936b4cac81SBjoern A. Zeeb 	int error;
17948ac540d3SBjoern A. Zeeb 	bool cancel;
17956b4cac81SBjoern A. Zeeb 
17968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
17978ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
17988ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
17998ac540d3SBjoern A. Zeeb 	if (!cancel)
18006b4cac81SBjoern A. Zeeb 		return;
18016b4cac81SBjoern A. Zeeb 
18026b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18036b4cac81SBjoern A. Zeeb 
1804bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1805cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
18066b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
18076b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1808cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
18096b4cac81SBjoern A. Zeeb 
18106b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
18118ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
18128ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
18138ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
18148ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
18158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
18168ac540d3SBjoern A. Zeeb 
1817bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
18186b4cac81SBjoern A. Zeeb 
18198ac540d3SBjoern A. Zeeb 	if (cancel)
18206b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
18216b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
18226b4cac81SBjoern A. Zeeb }
18236b4cac81SBjoern A. Zeeb 
18246b4cac81SBjoern A. Zeeb static void
1825086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1826086be6a8SBjoern A. Zeeb {
1827086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1828086be6a8SBjoern A. Zeeb 	int error;
1829086be6a8SBjoern A. Zeeb 	bool old;
1830086be6a8SBjoern A. Zeeb 
1831086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1832086be6a8SBjoern A. Zeeb 	if (old == new)
1833086be6a8SBjoern A. Zeeb 		return;
1834086be6a8SBjoern A. Zeeb 
1835086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1836086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1837086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1838086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1839086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1840086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1841086be6a8SBjoern A. Zeeb 	}
1842086be6a8SBjoern A. Zeeb }
1843086be6a8SBjoern A. Zeeb 
18445a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
18456b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
18466b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
18476b4cac81SBjoern A. Zeeb {
18485a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
18495a4d2461SBjoern A. Zeeb 
18505a4d2461SBjoern A. Zeeb 	changed = 0;
18516b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1852616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
18536b4cac81SBjoern A. Zeeb 
18546b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
18556b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
18566b4cac81SBjoern A. Zeeb 
1857616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1858616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
18596b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
18606b4cac81SBjoern A. Zeeb 		IMPROVE();
1861086be6a8SBjoern A. Zeeb 
18625a4d2461SBjoern A. Zeeb 		/*
18635a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
18645a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
18655a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
18665a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
18675a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
18685a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
18695a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
18705a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
18715a4d2461SBjoern A. Zeeb 		 */
18726b4cac81SBjoern A. Zeeb 	}
18735a4d2461SBjoern A. Zeeb 
18745a4d2461SBjoern A. Zeeb 	return (changed);
18756b4cac81SBjoern A. Zeeb }
18766b4cac81SBjoern A. Zeeb 
18776b4cac81SBjoern A. Zeeb static void
18786b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
18796b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
18806b4cac81SBjoern A. Zeeb {
18816b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
18826b4cac81SBjoern A. Zeeb 	int tid;
1883eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
18846b4cac81SBjoern A. Zeeb 
18856b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
18866b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
18876b4cac81SBjoern A. Zeeb 
18886b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
18896b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
18906b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
18916b4cac81SBjoern A. Zeeb 				continue;
18926b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
18936b4cac81SBjoern A. Zeeb 			continue;
18946b4cac81SBjoern A. Zeeb 
18956b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
18966b4cac81SBjoern A. Zeeb 			continue;
18976b4cac81SBjoern A. Zeeb 
18986b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
18996b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
19006b4cac81SBjoern A. Zeeb 			continue;
19016b4cac81SBjoern A. Zeeb 
1902eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1903eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1904eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1905eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
19066b4cac81SBjoern A. Zeeb 			continue;
19076b4cac81SBjoern A. Zeeb 
19086b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
19096b4cac81SBjoern A. Zeeb 	}
19106b4cac81SBjoern A. Zeeb }
19116b4cac81SBjoern A. Zeeb 
191288665349SBjoern A. Zeeb /*
191388665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
191488665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
191588665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
191688665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
191788665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
191888665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
191988665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
192088665349SBjoern A. Zeeb  * re-used.
192188665349SBjoern A. Zeeb  */
192288665349SBjoern A. Zeeb static void
192388665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
192488665349SBjoern A. Zeeb {
1925cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
192688665349SBjoern A. Zeeb 	struct mbufq mq;
192788665349SBjoern A. Zeeb 	struct mbuf *m;
192888665349SBjoern A. Zeeb 	int len;
192988665349SBjoern A. Zeeb 
1930cd0fcf9fSBjoern A. Zeeb 	/* There is no lockdep_assert_not_held_wiphy(). */
1931cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1932cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_not_held(&hw->wiphy->mtx);
193388665349SBjoern A. Zeeb 
193488665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
193588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
193688665349SBjoern A. Zeeb 	lsta->txq_ready = false;
193788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
193888665349SBjoern A. Zeeb 
193988665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
194088665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
194188665349SBjoern A. Zeeb 
194288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
194388665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
194488665349SBjoern A. Zeeb 	if (len <= 0) {
194588665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
194688665349SBjoern A. Zeeb 		return;
194788665349SBjoern A. Zeeb 	}
194888665349SBjoern A. Zeeb 
194988665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
195088665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
195188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
195288665349SBjoern A. Zeeb 
195388665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
195488665349SBjoern A. Zeeb 	while (m != NULL) {
195588665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
195688665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
195788665349SBjoern A. Zeeb 	}
195888665349SBjoern A. Zeeb }
195988665349SBjoern A. Zeeb 
196050d826beSBjoern A. Zeeb 
196150d826beSBjoern A. Zeeb static void
196250d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
196350d826beSBjoern A. Zeeb {
196450d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1965231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
196650d826beSBjoern A. Zeeb 
1967231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1968231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1969231168c7SBjoern A. Zeeb 
1970231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1971231168c7SBjoern A. Zeeb 		return;
1972231168c7SBjoern A. Zeeb 
197350d826beSBjoern A. Zeeb 	/* Remove vif context. */
1974231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
197550d826beSBjoern A. Zeeb 
197650d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
197750d826beSBjoern A. Zeeb 
197850d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
197950d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1980231168c7SBjoern A. Zeeb 
1981231168c7SBjoern A. Zeeb 	/* Cleanup. */
1982231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
198350d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1984a8a47a41SBjoern A. Zeeb 	list_del(&lchanctx->entry);
198550d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
198650d826beSBjoern A. Zeeb }
198750d826beSBjoern A. Zeeb 
198850d826beSBjoern A. Zeeb 
19896b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
19906b4cac81SBjoern A. Zeeb 
19916b4cac81SBjoern A. Zeeb static int
19926b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19936b4cac81SBjoern A. Zeeb {
19946b4cac81SBjoern A. Zeeb 
19956b4cac81SBjoern A. Zeeb 	return (0);
19966b4cac81SBjoern A. Zeeb }
19976b4cac81SBjoern A. Zeeb 
19986b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
19996b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
20006b4cac81SBjoern A. Zeeb 
20016b4cac81SBjoern A. Zeeb static int
20026b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20036b4cac81SBjoern A. Zeeb {
20046b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
2005c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
2006d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
20076b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20086b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20096b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20106b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20116b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
20126b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20136b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
20146b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
20156b4cac81SBjoern A. Zeeb 	uint32_t changed;
20166b4cac81SBjoern A. Zeeb 	int error;
20176b4cac81SBjoern A. Zeeb 
20182ac8a218SBjoern A. Zeeb 	/*
20192ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
20202ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
20212ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
20222ac8a218SBjoern A. Zeeb 	 */
20232ac8a218SBjoern A. Zeeb 
20242ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
20252ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
20262ac8a218SBjoern A. Zeeb 		return (EINVAL);
20272ac8a218SBjoern A. Zeeb 	}
20280936c648SBjoern A. Zeeb 	/*
20290936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
20300936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
20310936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
20320936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
20330936c648SBjoern A. Zeeb 	 */
20342ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
20352ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
20362ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
20372ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
20380936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20392ac8a218SBjoern A. Zeeb 		return (EINVAL);
20406b4cac81SBjoern A. Zeeb 	}
20416b4cac81SBjoern A. Zeeb 
20426b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20432ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
20442ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
20452ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
20462ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
20470936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20482ac8a218SBjoern A. Zeeb 		return (ESRCH);
20492ac8a218SBjoern A. Zeeb 	}
20502ac8a218SBjoern A. Zeeb 
20516b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20526b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20536b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20546b4cac81SBjoern A. Zeeb 
20550936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20560936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
20570936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
20580936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20590936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20600936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20610936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20620936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
206392690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
206492690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20650936c648SBjoern A. Zeeb 		return (EBUSY);
20660936c648SBjoern A. Zeeb 	}
20670936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20680936c648SBjoern A. Zeeb 
20696b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2070cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
20716b4cac81SBjoern A. Zeeb 
20726b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
2073560708cbSBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2074560708cbSBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
2075560708cbSBjoern A. Zeeb 	if (chanctx_conf != NULL) {
2076d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
20776b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
20786b4cac81SBjoern A. Zeeb 	} else {
20796b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
2080c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
20816b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
2082d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
20836b4cac81SBjoern A. Zeeb 	}
20846b4cac81SBjoern A. Zeeb 
2085d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
2086389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
2087d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
20886b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2089d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
2090d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
209190d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
209290d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
20939fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
20942ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
20952ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
209662d51a43SBjoern A. Zeeb 
20979fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
20989fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
209990d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2100d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
210190d8e307SBjoern A. Zeeb 		else
2102d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
21039fb91463SBjoern A. Zeeb 	}
21049fb91463SBjoern A. Zeeb #endif
21059fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
21065393cd34SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
21079fb91463SBjoern A. Zeeb #ifdef __notyet__
210890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2109d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
211090d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2111d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
211290d8e307SBjoern A. Zeeb 		else
211390d8e307SBjoern A. Zeeb #endif
211490d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2115d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
21169fb91463SBjoern A. Zeeb 	}
21179fb91463SBjoern A. Zeeb #endif
2118389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
21196b4cac81SBjoern A. Zeeb 	/* Responder ... */
212090d8e307SBjoern A. Zeeb #if 0
212190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2122d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
212390d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
212490d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
212590d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
212690d8e307SBjoern A. Zeeb #endif
212790d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
212890d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
212990d8e307SBjoern A. Zeeb #else
213090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
213190d8e307SBjoern A. Zeeb #endif
21326b4cac81SBjoern A. Zeeb 
21336ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
21346ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
21356ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
21366ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
21376ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
21386ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
21396ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
21406ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
21416ffb7bd4SBjoern A. Zeeb 
21426ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
21436ffb7bd4SBjoern A. Zeeb 
21446ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
21456ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
21466ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
21476ffb7bd4SBjoern A. Zeeb 
21486ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
21496ffb7bd4SBjoern A. Zeeb 
21506b4cac81SBjoern A. Zeeb 	error = 0;
2151560708cbSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
21526b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
21536b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
21546b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
21556b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2156d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
21576b4cac81SBjoern A. Zeeb 	} else {
2158d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
21596b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
21607b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
21617b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
21627b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
2163d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
21647b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
2165d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
21666b4cac81SBjoern A. Zeeb 		} else {
2167018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2168018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
21696b4cac81SBjoern A. Zeeb 			goto out;
21706b4cac81SBjoern A. Zeeb 		}
21716ffb7bd4SBjoern A. Zeeb 
2172a8a47a41SBjoern A. Zeeb 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2173560708cbSBjoern A. Zeeb 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
21746ffb7bd4SBjoern A. Zeeb 
21756b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
21766b4cac81SBjoern A. Zeeb 		if (error == 0)
217768541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2178d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
21796b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
21806b4cac81SBjoern A. Zeeb 			error = 0;
21816b4cac81SBjoern A. Zeeb 		if (error != 0) {
2182018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2183018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
2184d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2185560708cbSBjoern A. Zeeb 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2186d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2187a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
2188c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
21896b4cac81SBjoern A. Zeeb 			goto out;
21906b4cac81SBjoern A. Zeeb 		}
21916b4cac81SBjoern A. Zeeb 	}
21926b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
21936b4cac81SBjoern A. Zeeb 
21946b4cac81SBjoern A. Zeeb 	/* RATES */
21956b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
21966b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
21976b4cac81SBjoern A. Zeeb 
2198d9f59799SBjoern A. Zeeb 	/*
21990936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
22000936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
22010936c648SBjoern A. Zeeb 	 * under the hoods.
2202d9f59799SBjoern A. Zeeb 	 */
22030936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
22040936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
22056b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2206e24e8103SBjoern A. Zeeb 
220788665349SBjoern A. Zeeb 	/*
220888665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
220988665349SBjoern A. Zeeb 	 * that we can tx again.
221088665349SBjoern A. Zeeb 	 */
221188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
221288665349SBjoern A. Zeeb 	lsta->txq_ready = true;
221388665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
221488665349SBjoern A. Zeeb 
22152ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
2216a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2217e24e8103SBjoern A. Zeeb 
2218d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
22196b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22206b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
22216b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2222e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
22236b4cac81SBjoern A. Zeeb 	if (error != 0) {
22246b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2225018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2226018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22276b4cac81SBjoern A. Zeeb 		goto out;
22286b4cac81SBjoern A. Zeeb 	}
22296b4cac81SBjoern A. Zeeb #if 0
22306b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
22316b4cac81SBjoern A. Zeeb #endif
22326b4cac81SBjoern A. Zeeb 
22339d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22349d9ba2b7SBjoern A. Zeeb 
22351665ef97SBjoern A. Zeeb #if 0
22366b4cac81SBjoern A. Zeeb 	/*
22376b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
22386b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
22396b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
22406b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2241d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2242d9f59799SBjoern A. Zeeb 	 * for all queues.
22436b4cac81SBjoern A. Zeeb 	 */
2244e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
22451665ef97SBjoern A. Zeeb #endif
22466b4cac81SBjoern A. Zeeb 
22476b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
22486b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22496b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
22506b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
22516b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
22526b4cac81SBjoern A. Zeeb 
22536b4cac81SBjoern A. Zeeb 	/*
22546b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
22556b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
22566b4cac81SBjoern A. Zeeb 	 * - event_callback
22576b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
22586b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
22596b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
22606b4cac81SBjoern A. Zeeb 	 */
22616b4cac81SBjoern A. Zeeb 
2262cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2263105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2264105b9df2SBjoern A. Zeeb 
2265105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2266105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2267105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2268105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2269105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2270105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2271105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2272105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2273105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2274105b9df2SBjoern A. Zeeb 
2275105b9df2SBjoern A. Zeeb 	/*
2276105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2277105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2278105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2279105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2280105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2281105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2282105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2283105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2284105b9df2SBjoern A. Zeeb 	 */
2285105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2286105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2287105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
2288105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
2289105b9df2SBjoern A. Zeeb 	} else {
2290105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
2291105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
2292105b9df2SBjoern A. Zeeb 		/*
2293105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2294105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2295105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2296105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2297105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2298105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2299105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2300105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
2301105b9df2SBjoern A. Zeeb 		 */
2302105b9df2SBjoern A. Zeeb 	}
2303105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2304105b9df2SBjoern A. Zeeb 	goto out_relocked;
2305105b9df2SBjoern A. Zeeb 
23066b4cac81SBjoern A. Zeeb out:
2307cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
23086b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2309105b9df2SBjoern A. Zeeb out_relocked:
23100936c648SBjoern A. Zeeb 	/*
2311105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
23120936c648SBjoern A. Zeeb 	 * during the work of this function.
23130936c648SBjoern A. Zeeb 	 */
23146b4cac81SBjoern A. Zeeb 	if (ni != NULL)
23156b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
23166b4cac81SBjoern A. Zeeb 	return (error);
23176b4cac81SBjoern A. Zeeb }
23186b4cac81SBjoern A. Zeeb 
23196b4cac81SBjoern A. Zeeb static int
23206b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23216b4cac81SBjoern A. Zeeb {
23226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23236b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23246b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23256b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23266b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23276b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23286b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
23296b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23306b4cac81SBjoern A. Zeeb 	int error;
23316b4cac81SBjoern A. Zeeb 
23326b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23336b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23346b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23356b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23366b4cac81SBjoern A. Zeeb 
23372ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23382ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23392ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
23402ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
23412ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23422ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23432ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23442ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23452ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23462ac8a218SBjoern A. Zeeb #endif
23472ac8a218SBjoern A. Zeeb 
23482ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23492ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23502ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
23512ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
23522ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
23532ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
23546b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
23556b4cac81SBjoern A. Zeeb 
235667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
23576b4cac81SBjoern A. Zeeb 
23586b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2359cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
23606b4cac81SBjoern A. Zeeb 
2361d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2362d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2363d9f59799SBjoern A. Zeeb 
23646b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
236588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
23666b4cac81SBjoern A. Zeeb 
23676b4cac81SBjoern A. Zeeb 	/* flush, no drop */
23686b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
23696b4cac81SBjoern A. Zeeb 
23706b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23716b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23726b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23736b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
23746b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23756b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23766b4cac81SBjoern A. Zeeb 	}
23776b4cac81SBjoern A. Zeeb 
23786b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
23796b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
23806b4cac81SBjoern A. Zeeb 
23816b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
23826b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2383d9f59799SBjoern A. Zeeb 
2384d9f59799SBjoern A. Zeeb 	/* Take the station down. */
23856b4cac81SBjoern A. Zeeb 
23866b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
23876b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
23886b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2389d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2390e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
23916b4cac81SBjoern A. Zeeb 	if (error != 0) {
23926b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2393018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2394018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23956b4cac81SBjoern A. Zeeb 		goto out;
23966b4cac81SBjoern A. Zeeb 	}
23976b4cac81SBjoern A. Zeeb #if 0
23986b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
23996b4cac81SBjoern A. Zeeb #endif
24006b4cac81SBjoern A. Zeeb 
240167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
24026b4cac81SBjoern A. Zeeb 
24032ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24042ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
24052ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
24062ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
24072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2408d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
24090936c648SBjoern A. Zeeb 	/*
24100936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
24110936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
24120936c648SBjoern A. Zeeb 	 * and potentially freed.
24130936c648SBjoern A. Zeeb 	 */
24140936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2415d9f59799SBjoern A. Zeeb 
2416d9f59799SBjoern A. Zeeb 	/* conf_tx */
2417d9f59799SBjoern A. Zeeb 
241850d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
24196b4cac81SBjoern A. Zeeb 
24206b4cac81SBjoern A. Zeeb out:
2421cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24226b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24236b4cac81SBjoern A. Zeeb 	return (error);
24246b4cac81SBjoern A. Zeeb }
24256b4cac81SBjoern A. Zeeb 
24266b4cac81SBjoern A. Zeeb static int
24276b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24286b4cac81SBjoern A. Zeeb {
24296b4cac81SBjoern A. Zeeb 	int error;
24306b4cac81SBjoern A. Zeeb 
24316b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
24326b4cac81SBjoern A. Zeeb 	if (error == 0)
24336b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
24346b4cac81SBjoern A. Zeeb 	return (error);
24356b4cac81SBjoern A. Zeeb }
24366b4cac81SBjoern A. Zeeb 
24376b4cac81SBjoern A. Zeeb static int
24386b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24396b4cac81SBjoern A. Zeeb {
24406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24456b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24466b4cac81SBjoern A. Zeeb 	int error;
24476b4cac81SBjoern A. Zeeb 
24486b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24496b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24506b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24516b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24526b4cac81SBjoern A. Zeeb 
24536b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2454cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24552ac8a218SBjoern A. Zeeb 
24562ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24572ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24582ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24592ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24602ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24612ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24622ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24632ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24642ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24652ac8a218SBjoern A. Zeeb #endif
24662ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24672ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24682ac8a218SBjoern A. Zeeb 		goto out;
24692ac8a218SBjoern A. Zeeb 	}
24702ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24712ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24722ac8a218SBjoern A. Zeeb 
24732ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
24746b4cac81SBjoern A. Zeeb 
24756b4cac81SBjoern A. Zeeb 	/* Finish auth. */
24766b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
24776b4cac81SBjoern A. Zeeb 
24786b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
24796b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
24806b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2481e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2482018d93ecSBjoern A. Zeeb 	if (error != 0) {
2483018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2484018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24856b4cac81SBjoern A. Zeeb 		goto out;
2486018d93ecSBjoern A. Zeeb 	}
24876b4cac81SBjoern A. Zeeb 
24886b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
24896b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
24906b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24916b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
24926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
24936b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24946b4cac81SBjoern A. Zeeb 	}
24956b4cac81SBjoern A. Zeeb 
24966b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
24976b4cac81SBjoern A. Zeeb 
24986b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
24996b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
25006b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25016b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
25026b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
25036b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
25046b4cac81SBjoern A. Zeeb 	}
25056b4cac81SBjoern A. Zeeb 
25066b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
250788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
25086b4cac81SBjoern A. Zeeb 
25096b4cac81SBjoern A. Zeeb 	/*
25106b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
25116b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
25126b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
25136b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
25146b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
25156b4cac81SBjoern A. Zeeb 	 * - event_callback
25166b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
25176b4cac81SBjoern A. Zeeb 	 */
25186b4cac81SBjoern A. Zeeb 
25196b4cac81SBjoern A. Zeeb out:
2520cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
25216b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25226b4cac81SBjoern A. Zeeb 	return (error);
25236b4cac81SBjoern A. Zeeb }
25246b4cac81SBjoern A. Zeeb 
25256b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
25266b4cac81SBjoern A. Zeeb static int
25276b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25286b4cac81SBjoern A. Zeeb {
25296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25306b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25316b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25326b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25346b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
25352ac8a218SBjoern A. Zeeb 	int error;
25366b4cac81SBjoern A. Zeeb 
25376b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25396b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25406b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25416b4cac81SBjoern A. Zeeb 
25426b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2543cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
25442ac8a218SBjoern A. Zeeb 
25452ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25462ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
25472ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
25482ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25492ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25502ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25512ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25522ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25532ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25542ac8a218SBjoern A. Zeeb #endif
25552ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
25562ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
25572ac8a218SBjoern A. Zeeb 		goto out;
25582ac8a218SBjoern A. Zeeb 	}
25592ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25602ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25612ac8a218SBjoern A. Zeeb 
25622ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
25632ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
25646b4cac81SBjoern A. Zeeb 
25656b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
25666b4cac81SBjoern A. Zeeb 
25676b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25686b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25696b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25706b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
25716b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25726b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25736b4cac81SBjoern A. Zeeb 	}
25746b4cac81SBjoern A. Zeeb 
25756b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
25766b4cac81SBjoern A. Zeeb 
25776b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
25786b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
25796b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25806b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
25816b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
25826b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
25836b4cac81SBjoern A. Zeeb 	}
25846b4cac81SBjoern A. Zeeb 
25852ac8a218SBjoern A. Zeeb 	error = 0;
25862ac8a218SBjoern A. Zeeb out:
2587cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
25886b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25896b4cac81SBjoern A. Zeeb 
25902ac8a218SBjoern A. Zeeb 	return (error);
25916b4cac81SBjoern A. Zeeb }
25926b4cac81SBjoern A. Zeeb 
25936b4cac81SBjoern A. Zeeb static int
2594d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25956b4cac81SBjoern A. Zeeb {
25966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25976b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25986b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25996b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26006b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26026b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
26036b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2604d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
26056b4cac81SBjoern A. Zeeb 	int error;
26066b4cac81SBjoern A. Zeeb 
26076b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26116b4cac81SBjoern A. Zeeb 
26122ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2613cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
26142ac8a218SBjoern A. Zeeb 
26152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26162ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26172ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26182ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26192ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26202ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26212ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26222ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26232ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
26242ac8a218SBjoern A. Zeeb #endif
26252ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26262ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26272ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26282ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26292ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
26302ac8a218SBjoern A. Zeeb 
26312ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
26326b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
26336b4cac81SBjoern A. Zeeb 
263467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2635d9f59799SBjoern A. Zeeb 
2636d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2637d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2638d9f59799SBjoern A. Zeeb 
2639d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2640d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2641d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2642d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2643d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2644ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2645d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2646d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2647d9f59799SBjoern A. Zeeb 	}
2648d9f59799SBjoern A. Zeeb 
2649cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2650d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2651d9f59799SBjoern A. Zeeb 
265288665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2653d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2654018d93ecSBjoern A. Zeeb 	if (error != 0) {
2655018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2656018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2657d9f59799SBjoern A. Zeeb 		goto outni;
2658018d93ecSBjoern A. Zeeb 	}
2659d9f59799SBjoern A. Zeeb 
2660d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
266188665349SBjoern A. Zeeb 
266288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
266388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
266488665349SBjoern A. Zeeb 
2665cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2666d9f59799SBjoern A. Zeeb 
266767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2668d9f59799SBjoern A. Zeeb 
2669d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
267088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2671d9f59799SBjoern A. Zeeb 
2672d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2673d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2674d9f59799SBjoern A. Zeeb 
26756b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
26766b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
26776b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
26786b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2679ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
26806b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
26816b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
26826b4cac81SBjoern A. Zeeb 	}
26836b4cac81SBjoern A. Zeeb 
2684d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2685d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2686d9f59799SBjoern A. Zeeb 
2687d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2688d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2689d9f59799SBjoern A. Zeeb 
2690d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2691d9f59799SBjoern A. Zeeb 
26926b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
26936b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26946b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
26956b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2696e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2697018d93ecSBjoern A. Zeeb 	if (error != 0) {
2698018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2699018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27006b4cac81SBjoern A. Zeeb 		goto out;
2701018d93ecSBjoern A. Zeeb 	}
27026b4cac81SBjoern A. Zeeb 
27035a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
27045a4d2461SBjoern A. Zeeb 	bss_changed = 0;
27055a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
27066b4cac81SBjoern A. Zeeb 
27075a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
270816e688b2SBjoern A. Zeeb 
2709d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2710d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2711d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2712d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2713e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2714d9f59799SBjoern A. Zeeb 	if (error != 0) {
2715d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2716018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2717018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2718d9f59799SBjoern A. Zeeb 		goto out;
2719d9f59799SBjoern A. Zeeb 	}
2720d9f59799SBjoern A. Zeeb 
272116e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2722d9f59799SBjoern A. Zeeb 
2723d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2724d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2725d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2726616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2727616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2728d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2729d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2730d9f59799SBjoern A. Zeeb 
27312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27322ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
27332ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
27342ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
27352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2736d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
27370936c648SBjoern A. Zeeb 	/*
27380936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
27390936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
27400936c648SBjoern A. Zeeb 	 * and potentially freed.
27410936c648SBjoern A. Zeeb 	 */
27420936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2743d9f59799SBjoern A. Zeeb 
2744d9f59799SBjoern A. Zeeb 	/* conf_tx */
2745d9f59799SBjoern A. Zeeb 
274650d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2747d9f59799SBjoern A. Zeeb 
2748d9f59799SBjoern A. Zeeb 	error = EALREADY;
27496b4cac81SBjoern A. Zeeb out:
2750cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
27516b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2752d9f59799SBjoern A. Zeeb outni:
27536b4cac81SBjoern A. Zeeb 	return (error);
27546b4cac81SBjoern A. Zeeb }
27556b4cac81SBjoern A. Zeeb 
27566b4cac81SBjoern A. Zeeb static int
2757d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2758d9f59799SBjoern A. Zeeb {
2759d9f59799SBjoern A. Zeeb 	int error;
2760d9f59799SBjoern A. Zeeb 
2761d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2762d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2763d9f59799SBjoern A. Zeeb 		return (error);
2764d9f59799SBjoern A. Zeeb 
2765d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2766d9f59799SBjoern A. Zeeb 
2767d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2768d9f59799SBjoern A. Zeeb 	return (error);
2769d9f59799SBjoern A. Zeeb }
2770d9f59799SBjoern A. Zeeb 
2771d9f59799SBjoern A. Zeeb static int
27726b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27736b4cac81SBjoern A. Zeeb {
27746b4cac81SBjoern A. Zeeb 	int error;
27756b4cac81SBjoern A. Zeeb 
2776d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
27776b4cac81SBjoern A. Zeeb 	return (error);
27786b4cac81SBjoern A. Zeeb }
27796b4cac81SBjoern A. Zeeb 
27806b4cac81SBjoern A. Zeeb static int
27816b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27826b4cac81SBjoern A. Zeeb {
27836b4cac81SBjoern A. Zeeb 	int error;
27846b4cac81SBjoern A. Zeeb 
2785d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
27866b4cac81SBjoern A. Zeeb 	return (error);
27876b4cac81SBjoern A. Zeeb }
27886b4cac81SBjoern A. Zeeb 
27896b4cac81SBjoern A. Zeeb static int
27906b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27916b4cac81SBjoern A. Zeeb {
27926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27936b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27946b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27956b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27966b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
27976b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27986b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
27996b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
28006b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
28016b4cac81SBjoern A. Zeeb 	int error;
28026b4cac81SBjoern A. Zeeb 
28036b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
28046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28056b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28066b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28076b4cac81SBjoern A. Zeeb 
28086b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2809cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
28102ac8a218SBjoern A. Zeeb 
28112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28122ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
28132ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
28142ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28152ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28162ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28172ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28182ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28192ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28202ac8a218SBjoern A. Zeeb #endif
28212ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
28222ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
28232ac8a218SBjoern A. Zeeb 		goto out;
28242ac8a218SBjoern A. Zeeb 	}
28252ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28262ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28272ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28282ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28292ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28302ac8a218SBjoern A. Zeeb 
28312ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
28326b4cac81SBjoern A. Zeeb 
28336b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
28346b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
28356b4cac81SBjoern A. Zeeb 
28369597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
28379597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
28389597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
28399597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
28406b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
28419597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
28424a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
28434a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
28444a67f1dfSBjoern A. Zeeb 		sta->wme = true;
28454a67f1dfSBjoern A. Zeeb #endif
2846e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2847018d93ecSBjoern A. Zeeb 	if (error != 0) {
2848018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2849018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28509597f7cbSBjoern A. Zeeb 		goto out;
2851018d93ecSBjoern A. Zeeb 	}
28526b4cac81SBjoern A. Zeeb 
28536b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
28546b4cac81SBjoern A. Zeeb 
28556b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
28566b4cac81SBjoern A. Zeeb 	bss_changed = 0;
28574a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
28584a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
28594a67f1dfSBjoern A. Zeeb #endif
2860616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2861616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2862616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
28636b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
28646b4cac81SBjoern A. Zeeb 	}
28656b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2866616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2867616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
28686b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
28696b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
28706b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
28716b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
28726b4cac81SBjoern A. Zeeb 	}
28736b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
28746b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
28756b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
28766b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
28776b4cac81SBjoern A. Zeeb 	}
28786b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
28796b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
28806b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
28816b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
28826b4cac81SBjoern A. Zeeb 	}
2883fa8f007dSBjoern A. Zeeb 
2884fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
28856b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
28866b4cac81SBjoern A. Zeeb 
28876b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
28886b4cac81SBjoern A. Zeeb 	 * - event_callback
28896b4cac81SBjoern A. Zeeb 	 */
28906b4cac81SBjoern A. Zeeb 
28916b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
28926b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
28936b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
28946b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
28956b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
28966b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
28976b4cac81SBjoern A. Zeeb 	}
28986b4cac81SBjoern A. Zeeb 
2899086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2900086be6a8SBjoern A. Zeeb 
29016b4cac81SBjoern A. Zeeb 	/*
29026b4cac81SBjoern A. Zeeb 	 * And then:
29036b4cac81SBjoern A. Zeeb 	 * - (more packets)?
29046b4cac81SBjoern A. Zeeb 	 * - set_key
29056b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
29066b4cac81SBjoern A. Zeeb 	 * - set_key (?)
29076b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
29086b4cac81SBjoern A. Zeeb 	 */
29096b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
29106b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
29116b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
29126b4cac81SBjoern A. Zeeb 
29136b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
29146b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
29156b4cac81SBjoern A. Zeeb 	}
29166b4cac81SBjoern A. Zeeb 
2917f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
29189fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
291962d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
29209fb91463SBjoern A. Zeeb 
29216b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
29226b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
29236b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
29246b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2925e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
29266b4cac81SBjoern A. Zeeb 	if (error != 0) {
29276b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2928018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2929018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
29306b4cac81SBjoern A. Zeeb 		goto out;
29316b4cac81SBjoern A. Zeeb 	}
29326b4cac81SBjoern A. Zeeb 
29336b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
29346b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
29356b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
29366b4cac81SBjoern A. Zeeb 	 *
29376b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
29386b4cac81SBjoern A. Zeeb 	 */
29396b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
29406b4cac81SBjoern A. Zeeb 
2941fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2942fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2943fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2944fa8f007dSBjoern A. Zeeb 
29456b4cac81SBjoern A. Zeeb out:
2946cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
29476b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
29486b4cac81SBjoern A. Zeeb 	return (error);
29496b4cac81SBjoern A. Zeeb }
29506b4cac81SBjoern A. Zeeb 
29516b4cac81SBjoern A. Zeeb static int
29526b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29536b4cac81SBjoern A. Zeeb {
29546b4cac81SBjoern A. Zeeb 	int error;
29556b4cac81SBjoern A. Zeeb 
29566b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
29576b4cac81SBjoern A. Zeeb 	if (error == 0)
29586b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
29596b4cac81SBjoern A. Zeeb 	return (error);
29606b4cac81SBjoern A. Zeeb }
29616b4cac81SBjoern A. Zeeb 
29626b4cac81SBjoern A. Zeeb static int
29636b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29646b4cac81SBjoern A. Zeeb {
29656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29666b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29696b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
29706b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
29716b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2972d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2973d9f59799SBjoern A. Zeeb #if 0
2974d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2975d9f59799SBjoern A. Zeeb #endif
29766b4cac81SBjoern A. Zeeb 	int error;
29776b4cac81SBjoern A. Zeeb 
29786b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29806b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
29816b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29826b4cac81SBjoern A. Zeeb 
29832ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
29842ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
29852ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
29862ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
29872ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
29882ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
29892ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
29902ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
29912ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
29922ac8a218SBjoern A. Zeeb #endif
29932ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
29942ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
29952ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
29962ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
29972ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
29982ac8a218SBjoern A. Zeeb 
29992ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
30006b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
30016b4cac81SBjoern A. Zeeb 
300267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3003d9f59799SBjoern A. Zeeb 
3004d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3005cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3006d9f59799SBjoern A. Zeeb 
3007d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3008d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3009d9f59799SBjoern A. Zeeb 
3010d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3011d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3012d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3013d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3014d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3015ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3016d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3017d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3018d9f59799SBjoern A. Zeeb 	}
3019d9f59799SBjoern A. Zeeb 
3020cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3021d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3022d9f59799SBjoern A. Zeeb 
3023d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3024d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3025018d93ecSBjoern A. Zeeb 	if (error != 0) {
3026018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3027018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3028d9f59799SBjoern A. Zeeb 		goto outni;
3029018d93ecSBjoern A. Zeeb 	}
3030d9f59799SBjoern A. Zeeb 
3031d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
303288665349SBjoern A. Zeeb 
303388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
303488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
303588665349SBjoern A. Zeeb 
3036cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3037d9f59799SBjoern A. Zeeb 
303867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3039d9f59799SBjoern A. Zeeb 
3040d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
304188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3042d9f59799SBjoern A. Zeeb 
3043d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3044d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3045d9f59799SBjoern A. Zeeb 
3046d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3047d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3048d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3049d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3050ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3051d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3052d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3053d9f59799SBjoern A. Zeeb 	}
3054d9f59799SBjoern A. Zeeb 
3055d9f59799SBjoern A. Zeeb #if 0
3056d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3057d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3058d9f59799SBjoern A. Zeeb 
3059d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3060d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3061d9f59799SBjoern A. Zeeb #endif
3062d9f59799SBjoern A. Zeeb 
3063d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3064d9f59799SBjoern A. Zeeb 
30656b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
30666b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30676b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
30686b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3069e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3070018d93ecSBjoern A. Zeeb 	if (error != 0) {
3071018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3072018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
30736b4cac81SBjoern A. Zeeb 		goto out;
3074018d93ecSBjoern A. Zeeb 	}
30756b4cac81SBjoern A. Zeeb 
307667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
30776b4cac81SBjoern A. Zeeb 
307811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
307911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
308011db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
308111db70b6SBjoern A. Zeeb 		if (error != 0) {
308211db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
308311db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
308411db70b6SBjoern A. Zeeb 			/*
308511db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
308611db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
308711db70b6SBjoern A. Zeeb 			 * less appropriate time).
308811db70b6SBjoern A. Zeeb 			 */
308911db70b6SBjoern A. Zeeb 			/* goto out; */
309011db70b6SBjoern A. Zeeb 		}
309111db70b6SBjoern A. Zeeb 	}
309211db70b6SBjoern A. Zeeb #endif
309311db70b6SBjoern A. Zeeb 
30946b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
30956b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30966b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
30976b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3098e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3099018d93ecSBjoern A. Zeeb 	if (error != 0) {
3100018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3101018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
31026b4cac81SBjoern A. Zeeb 		goto out;
3103018d93ecSBjoern A. Zeeb 	}
31046b4cac81SBjoern A. Zeeb 
310567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
31066b4cac81SBjoern A. Zeeb 
3107d9f59799SBjoern A. Zeeb #if 0
3108d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
3109d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
3110d9f59799SBjoern A. Zeeb #endif
3111d9f59799SBjoern A. Zeeb 
3112d9f59799SBjoern A. Zeeb 	error = EALREADY;
31136b4cac81SBjoern A. Zeeb out:
3114cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
31156b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3116d9f59799SBjoern A. Zeeb outni:
31176b4cac81SBjoern A. Zeeb 	return (error);
31186b4cac81SBjoern A. Zeeb }
31196b4cac81SBjoern A. Zeeb 
31206b4cac81SBjoern A. Zeeb static int
3121d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3122d9f59799SBjoern A. Zeeb {
3123d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3124d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3125d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
3126d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3127d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
3128d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
3129d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3130d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
3131d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
3132d9f59799SBjoern A. Zeeb 	int error;
3133d9f59799SBjoern A. Zeeb 
3134d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
3135d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3136d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3137d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3138d9f59799SBjoern A. Zeeb 
31392ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3140cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
31412ac8a218SBjoern A. Zeeb 
31422ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
31432ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31442ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
31452ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
31462ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
31472ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
31482ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
31492ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
31502ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
31512ac8a218SBjoern A. Zeeb #endif
31522ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
31532ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
31542ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
31552ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
31562ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
31572ac8a218SBjoern A. Zeeb 
31582ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3159d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3160d9f59799SBjoern A. Zeeb 
316167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3162d9f59799SBjoern A. Zeeb 
3163d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3164d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3165d9f59799SBjoern A. Zeeb 
3166d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3167d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3168d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3169d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3170d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3171ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3172d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3173d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3174d9f59799SBjoern A. Zeeb 	}
3175d9f59799SBjoern A. Zeeb 
3176cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3177d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3178d9f59799SBjoern A. Zeeb 
3179d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3180d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3181018d93ecSBjoern A. Zeeb 	if (error != 0) {
3182018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3183018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3184d9f59799SBjoern A. Zeeb 		goto outni;
3185018d93ecSBjoern A. Zeeb 	}
3186d9f59799SBjoern A. Zeeb 
3187d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
318888665349SBjoern A. Zeeb 
318988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
319088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
319188665349SBjoern A. Zeeb 
3192cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3193d9f59799SBjoern A. Zeeb 
319467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3195d9f59799SBjoern A. Zeeb 
3196d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
319788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3198d9f59799SBjoern A. Zeeb 
3199d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3200d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3201d9f59799SBjoern A. Zeeb 
3202d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3203d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3204d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3205d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3206ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3207d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3208d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3209d9f59799SBjoern A. Zeeb 	}
3210d9f59799SBjoern A. Zeeb 
3211d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3212d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3213d9f59799SBjoern A. Zeeb 
3214d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3215d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3216d9f59799SBjoern A. Zeeb 
3217d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3218d9f59799SBjoern A. Zeeb 
3219d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3220d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3221d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3222d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3223e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3224018d93ecSBjoern A. Zeeb 	if (error != 0) {
3225018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3226018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3227d9f59799SBjoern A. Zeeb 		goto out;
3228018d93ecSBjoern A. Zeeb 	}
3229d9f59799SBjoern A. Zeeb 
323067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3231d9f59799SBjoern A. Zeeb 
323211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
323311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
323411db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
323511db70b6SBjoern A. Zeeb 		if (error != 0) {
323611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
323711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
323811db70b6SBjoern A. Zeeb 			/*
323911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
324011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
324111db70b6SBjoern A. Zeeb 			 * less appropriate time).
324211db70b6SBjoern A. Zeeb 			 */
324311db70b6SBjoern A. Zeeb 			/* goto out; */
324411db70b6SBjoern A. Zeeb 		}
324511db70b6SBjoern A. Zeeb 	}
324611db70b6SBjoern A. Zeeb #endif
324711db70b6SBjoern A. Zeeb 
3248d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3249d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3250d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3251d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3252e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3253018d93ecSBjoern A. Zeeb 	if (error != 0) {
3254018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3255018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3256d9f59799SBjoern A. Zeeb 		goto out;
3257018d93ecSBjoern A. Zeeb 	}
3258d9f59799SBjoern A. Zeeb 
325967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3260d9f59799SBjoern A. Zeeb 
3261d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3262d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3263d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3264d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3265e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3266018d93ecSBjoern A. Zeeb 	if (error != 0) {
3267018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3268018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3269d9f59799SBjoern A. Zeeb 		goto out;
3270018d93ecSBjoern A. Zeeb 	}
3271d9f59799SBjoern A. Zeeb 
32725a4d2461SBjoern A. Zeeb 	bss_changed = 0;
327316e688b2SBjoern A. Zeeb 	/*
32745a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
32755a4d2461SBjoern A. Zeeb 	 *
327616e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
32775a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
32785a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
32795a4d2461SBjoern A. Zeeb 	 *
32805a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
32815a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
32825a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
32835a4d2461SBjoern A. Zeeb 	 * a fw assert.
32845a4d2461SBjoern A. Zeeb 	 *
32855a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
32865a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
32875a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
32885a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
32895a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
32905a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
32915a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
32925a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
32935a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
32945a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
329516e688b2SBjoern A. Zeeb 	 */
32965a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
32975a4d2461SBjoern A. Zeeb 
32985a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
329916e688b2SBjoern A. Zeeb 
3300d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3301d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3302d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3303d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3304e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3305d9f59799SBjoern A. Zeeb 	if (error != 0) {
3306d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3307018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3308018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3309d9f59799SBjoern A. Zeeb 		goto out;
3310d9f59799SBjoern A. Zeeb 	}
3311d9f59799SBjoern A. Zeeb 
33125a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
33135a4d2461SBjoern A. Zeeb 
331416e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3315d9f59799SBjoern A. Zeeb 
3316d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3317d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3318d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3319616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3320616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3321d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
33225a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
33235a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
33245a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3325d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3326d9f59799SBjoern A. Zeeb 
33272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
33282ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
33292ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
33302ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
33320936c648SBjoern A. Zeeb 	/*
33330936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
33340936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
33350936c648SBjoern A. Zeeb 	 * and potentially freed.
33360936c648SBjoern A. Zeeb 	 */
33370936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3338d9f59799SBjoern A. Zeeb 
3339d9f59799SBjoern A. Zeeb 	/* conf_tx */
3340d9f59799SBjoern A. Zeeb 
334150d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3342d9f59799SBjoern A. Zeeb 
3343d9f59799SBjoern A. Zeeb 	error = EALREADY;
3344d9f59799SBjoern A. Zeeb out:
3345cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3346d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3347d9f59799SBjoern A. Zeeb outni:
3348d9f59799SBjoern A. Zeeb 	return (error);
3349d9f59799SBjoern A. Zeeb }
3350d9f59799SBjoern A. Zeeb 
3351d9f59799SBjoern A. Zeeb static int
3352d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3353d9f59799SBjoern A. Zeeb {
3354d9f59799SBjoern A. Zeeb 
3355d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3356d9f59799SBjoern A. Zeeb }
3357d9f59799SBjoern A. Zeeb 
3358d9f59799SBjoern A. Zeeb static int
33596b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
33606b4cac81SBjoern A. Zeeb {
33616b4cac81SBjoern A. Zeeb 	int error;
33626b4cac81SBjoern A. Zeeb 
3363d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3364d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3365d9f59799SBjoern A. Zeeb 		return (error);
3366d9f59799SBjoern A. Zeeb 
3367d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3368d9f59799SBjoern A. Zeeb 
3369d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
33706b4cac81SBjoern A. Zeeb 	return (error);
33716b4cac81SBjoern A. Zeeb }
33726b4cac81SBjoern A. Zeeb 
3373d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb /*
33766b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
33776b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
33786b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
33796b4cac81SBjoern A. Zeeb  */
33806b4cac81SBjoern A. Zeeb struct fsm_state {
33816b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
33826b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
33836b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
33846b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
33856b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
33866b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
33876b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
33886b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3389d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3390d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
33916b4cac81SBjoern A. Zeeb 
33926b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
33936b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
33946b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
33956b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3396d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
33976b4cac81SBjoern A. Zeeb 
3398d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3399d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3400d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3401d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3402d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
34036b4cac81SBjoern A. Zeeb 
3404d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3405d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3406d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
34076b4cac81SBjoern A. Zeeb 
34086b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
34096b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
34106b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
34116b4cac81SBjoern A. Zeeb 
34126b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
34136b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
34146b4cac81SBjoern A. Zeeb };
34156b4cac81SBjoern A. Zeeb 
34166b4cac81SBjoern A. Zeeb static int
34176b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
34186b4cac81SBjoern A. Zeeb {
34196b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
34206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
34216b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34226b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34236b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
34246b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
34256b4cac81SBjoern A. Zeeb 	int error;
34266b4cac81SBjoern A. Zeeb 
34276b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
34286b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
34296b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
34306b4cac81SBjoern A. Zeeb 
34319d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34329d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
34336b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
34346b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
34359d9ba2b7SBjoern A. Zeeb #endif
34366b4cac81SBjoern A. Zeeb 
34376b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
34386b4cac81SBjoern A. Zeeb 
34396b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
34406b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
34416b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
34426b4cac81SBjoern A. Zeeb 
34436b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
34446b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
34456b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
34466b4cac81SBjoern A. Zeeb 
34476b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
34486b4cac81SBjoern A. Zeeb 
34496b4cac81SBjoern A. Zeeb 	} else {
34506b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
34516b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
34526b4cac81SBjoern A. Zeeb 		return (ENOSYS);
34536b4cac81SBjoern A. Zeeb 	}
34546b4cac81SBjoern A. Zeeb 
34556b4cac81SBjoern A. Zeeb 	error = 0;
34566b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
34576b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
34589d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34599d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3460d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3461d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3462d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3463d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
34649d9ba2b7SBjoern A. Zeeb #endif
34656b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
34666b4cac81SBjoern A. Zeeb 			break;
34676b4cac81SBjoern A. Zeeb 		}
34686b4cac81SBjoern A. Zeeb 	}
34696b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
34706b4cac81SBjoern A. Zeeb 
34716b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3472d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
34736b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
34746b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
34756b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
34766b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
34776b4cac81SBjoern A. Zeeb 		return (ENOSYS);
34786b4cac81SBjoern A. Zeeb 	}
34796b4cac81SBjoern A. Zeeb 
34806b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
34819d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34829d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3483d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3484d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3485d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3486d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
34879d9ba2b7SBjoern A. Zeeb #endif
34886b4cac81SBjoern A. Zeeb 		return (0);
34896b4cac81SBjoern A. Zeeb 	}
34906b4cac81SBjoern A. Zeeb 
34916b4cac81SBjoern A. Zeeb 	if (error != 0) {
34926b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
34936b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
34946b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
34956b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
349645c27ad5SBjoern A. Zeeb 		return (error);
34976b4cac81SBjoern A. Zeeb 	}
34986b4cac81SBjoern A. Zeeb 
34999d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
35009d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3501d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3502d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
35036b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
35049d9ba2b7SBjoern A. Zeeb #endif
35056b4cac81SBjoern A. Zeeb 
35066b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
35076b4cac81SBjoern A. Zeeb }
35086b4cac81SBjoern A. Zeeb 
35096b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
35106b4cac81SBjoern A. Zeeb 
3511d9f59799SBjoern A. Zeeb /*
3512d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3513d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3514d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3515d9f59799SBjoern A. Zeeb  */
3516d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3517d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3518d9f59799SBjoern A. Zeeb {
3519d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35202ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3521d9f59799SBjoern A. Zeeb 
35222ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3523d9f59799SBjoern A. Zeeb 
3524ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35252ac8a218SBjoern A. Zeeb 
35262ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
35272ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
35282ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
35292ac8a218SBjoern A. Zeeb 
35302ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
35312ac8a218SBjoern A. Zeeb 	return (rni);
3532d9f59799SBjoern A. Zeeb }
3533d9f59799SBjoern A. Zeeb 
35344a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
35356b4cac81SBjoern A. Zeeb static int
35364a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
35376b4cac81SBjoern A. Zeeb {
35384a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
35396b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35406b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35416b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35426b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
35436b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
35446b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
35456b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
35466b4cac81SBjoern A. Zeeb 	int error;
35476b4cac81SBjoern A. Zeeb 	uint16_t ac;
35486b4cac81SBjoern A. Zeeb 
3549cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3550cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
3551cd0fcf9fSBjoern A. Zeeb 
35526b4cac81SBjoern A. Zeeb 	IMPROVE();
35536b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
35546b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
35556b4cac81SBjoern A. Zeeb 
35566b4cac81SBjoern A. Zeeb 	if (vap == NULL)
35576b4cac81SBjoern A. Zeeb 		return (0);
35586b4cac81SBjoern A. Zeeb 
35594a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
35604a67f1dfSBjoern A. Zeeb 		return (0);
35614a67f1dfSBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
35636b4cac81SBjoern A. Zeeb 		return (0);
35646b4cac81SBjoern A. Zeeb 
35654a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
35664a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
35674a67f1dfSBjoern A. Zeeb 		return (0);
35684a67f1dfSBjoern A. Zeeb 	}
35694a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
35706b4cac81SBjoern A. Zeeb 
35714a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
35726b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
35736b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
35746b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
35756b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
35766b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
35776b4cac81SBjoern A. Zeeb 
35784a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35794a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35804a67f1dfSBjoern A. Zeeb 
35816b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
35826b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
35836b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
35846b4cac81SBjoern A. Zeeb 
35856b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
35866b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
35876b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
35886b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
35896b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
35906b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
359168541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
35926b4cac81SBjoern A. Zeeb 		if (error != 0)
35933f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
35946b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
35956b4cac81SBjoern A. Zeeb 	}
35966b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
35974a67f1dfSBjoern A. Zeeb 	if (!planned)
35986b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35994a67f1dfSBjoern A. Zeeb 
36004a67f1dfSBjoern A. Zeeb 	return (changed);
36014a67f1dfSBjoern A. Zeeb }
36026b4cac81SBjoern A. Zeeb #endif
36036b4cac81SBjoern A. Zeeb 
36044a67f1dfSBjoern A. Zeeb static int
36054a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
36064a67f1dfSBjoern A. Zeeb {
36074a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
36084a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
36094a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
3610cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
36114a67f1dfSBjoern A. Zeeb 
36124a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
36134a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
36144a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
36156b4cac81SBjoern A. Zeeb 		return (0);
36164a67f1dfSBjoern A. Zeeb 
36174a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
3618cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36194a67f1dfSBjoern A. Zeeb 
3620cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
36214a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
3622cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36234a67f1dfSBjoern A. Zeeb #endif
36244a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
36256b4cac81SBjoern A. Zeeb }
36266b4cac81SBjoern A. Zeeb 
36274aff4048SBjoern A. Zeeb /*
36284aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
36294aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
36304aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
36314aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
36324aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
36334aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3634edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3635edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3636edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3637edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
36384aff4048SBjoern A. Zeeb  */
36394aff4048SBjoern A. Zeeb static void
36404aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
36414aff4048SBjoern A. Zeeb {
36424aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
36434aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
36444aff4048SBjoern A. Zeeb 
36454aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3646edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3647edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3648edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
36494aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
36504aff4048SBjoern A. Zeeb 		return;
36514aff4048SBjoern A. Zeeb 	}
36524aff4048SBjoern A. Zeeb 
36534aff4048SBjoern A. Zeeb 	vif = arg;
365457609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
36554aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
36564aff4048SBjoern A. Zeeb }
36574aff4048SBjoern A. Zeeb 
36586b4cac81SBjoern A. Zeeb static struct ieee80211vap *
36596b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
36606b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
36616b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
36626b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
36636b4cac81SBjoern A. Zeeb {
36646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36656b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36676b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3669a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
36706b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
367140839418SBjoern A. Zeeb 	struct sysctl_oid *node;
36726b4cac81SBjoern A. Zeeb 	size_t len;
3673e3a0b120SBjoern A. Zeeb 	int error, i;
3674a6042e17SBjoern A. Zeeb 	uint16_t ac;
36756b4cac81SBjoern A. Zeeb 
36766b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
36776b4cac81SBjoern A. Zeeb 		return (NULL);
36786b4cac81SBjoern A. Zeeb 
36796b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36806b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36816b4cac81SBjoern A. Zeeb 
36826b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
36836b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
36846b4cac81SBjoern A. Zeeb 
36856b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
36866b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3687a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
36882ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3689b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
36902ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
36916b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
36926b4cac81SBjoern A. Zeeb 
36936b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36946b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
36956b4cac81SBjoern A. Zeeb 	vif->p2p = false;
36966b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
36976b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
36986b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
36996b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
37006b4cac81SBjoern A. Zeeb 	IMPROVE();
37016b4cac81SBjoern A. Zeeb 
37026b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
37036b4cac81SBjoern A. Zeeb #if 1
3704560708cbSBjoern A. Zeeb 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3705adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
37066ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
37076ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
37084aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
37094aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
37106ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
37117b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
37126b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
37136b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
37146b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
37156b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
37166b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3717616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3718616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3719616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3720616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
37216ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3722caaa79c3SBjoern A. Zeeb 	/*
3723caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3724caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3725caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3726caaa79c3SBjoern A. Zeeb 	 */
37276ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
37286b4cac81SBjoern A. Zeeb #endif
37296b4cac81SBjoern A. Zeeb #if 0
37306b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
37316b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
37326b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
37336b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
37346b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
37356b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
37366b4cac81SBjoern A. Zeeb #endif
3737e3a0b120SBjoern A. Zeeb 
37386ffb7bd4SBjoern A. Zeeb 	/* Link Config */
37396ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
37406ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
37416ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
37426ffb7bd4SBjoern A. Zeeb 	}
37436ffb7bd4SBjoern A. Zeeb 
3744e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3745e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3746e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3747e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3748e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3749e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3750e3a0b120SBjoern A. Zeeb 		else
3751e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
37520cbcfa19SBjoern A. Zeeb 
37530cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
37540cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3755e3a0b120SBjoern A. Zeeb 	}
3756e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3757e3a0b120SBjoern A. Zeeb 
37586b4cac81SBjoern A. Zeeb 	IMPROVE();
37596b4cac81SBjoern A. Zeeb 
37606b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
37616b4cac81SBjoern A. Zeeb 	if (error != 0) {
37623f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
37636b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37646b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37656b4cac81SBjoern A. Zeeb 		return (NULL);
37666b4cac81SBjoern A. Zeeb 	}
37676b4cac81SBjoern A. Zeeb 
37686b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
37696b4cac81SBjoern A. Zeeb 	if (error != 0) {
37706b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
37713f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
37726b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37736b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37746b4cac81SBjoern A. Zeeb 		return (NULL);
37756b4cac81SBjoern A. Zeeb 	}
37766b4cac81SBjoern A. Zeeb 
37778891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
37786b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
37798891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
37806b4cac81SBjoern A. Zeeb 
37816b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
37826b4cac81SBjoern A. Zeeb 	changed = 0;
37836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
37846b4cac81SBjoern A. Zeeb 
3785a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3786a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3787cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3788a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3789a6042e17SBjoern A. Zeeb 
3790a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3791a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3792a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3793a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3794a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3795a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3796a6042e17SBjoern A. Zeeb 		if (error != 0)
3797a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3798a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3799a6042e17SBjoern A. Zeeb 	}
3800cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3801a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3802a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
38036b4cac81SBjoern A. Zeeb 
38046b4cac81SBjoern A. Zeeb 	/* Force MC init. */
38056b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
38066b4cac81SBjoern A. Zeeb 
38076b4cac81SBjoern A. Zeeb 	IMPROVE();
38086b4cac81SBjoern A. Zeeb 
38096b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
38106b4cac81SBjoern A. Zeeb 
38116b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
38126b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
38136b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3814d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3815d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
38166b4cac81SBjoern A. Zeeb 
3817b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
381811db70b6SBjoern A. Zeeb 	/* Key management. */
381911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
38206b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
38216b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3822b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3823b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
38246b4cac81SBjoern A. Zeeb 	}
382511db70b6SBjoern A. Zeeb #endif
38266b4cac81SBjoern A. Zeeb 
38279fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
38289fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
38299fb91463SBjoern A. Zeeb #endif
38309fb91463SBjoern A. Zeeb 
38316b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
38326b4cac81SBjoern A. Zeeb 
38336b4cac81SBjoern A. Zeeb 	/* Complete setup. */
38346b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
38356b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
38366b4cac81SBjoern A. Zeeb 
383711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
383811db70b6SBjoern A. Zeeb 	/*
383911db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
384011db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
384111db70b6SBjoern A. Zeeb 	 */
384211db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
384311db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
384411db70b6SBjoern A. Zeeb #endif
3845ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3846ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3847ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3848ac2c7271SBjoern A. Zeeb #endif
384911db70b6SBjoern A. Zeeb 
38506b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
38516b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
38526b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
38536b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
38546b4cac81SBjoern A. Zeeb 
38556b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
38566b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
38576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
38586b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
38596b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
38606b4cac81SBjoern A. Zeeb 	/* any others? */
386140839418SBjoern A. Zeeb 
386240839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
386340839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
386440839418SBjoern A. Zeeb 
386540839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
386640839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
386740839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
386840839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
386940839418SBjoern A. Zeeb 
387040839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
387140839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
387240839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
387340839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
387440839418SBjoern A. Zeeb 
38756b4cac81SBjoern A. Zeeb 	IMPROVE();
38766b4cac81SBjoern A. Zeeb 
38776b4cac81SBjoern A. Zeeb 	return (vap);
38786b4cac81SBjoern A. Zeeb }
38796b4cac81SBjoern A. Zeeb 
38804b0af114SBjoern A. Zeeb void
38814b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
38824b0af114SBjoern A. Zeeb {
38834b0af114SBjoern A. Zeeb 
38844b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
38854b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
38864b0af114SBjoern A. Zeeb 
38874b0af114SBjoern A. Zeeb 	IMPROVE();
38884b0af114SBjoern A. Zeeb }
38894b0af114SBjoern A. Zeeb 
38904b0af114SBjoern A. Zeeb void
38914b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
38924b0af114SBjoern A. Zeeb {
38934b0af114SBjoern A. Zeeb 
38944b0af114SBjoern A. Zeeb 	TODO();
38954b0af114SBjoern A. Zeeb }
38964b0af114SBjoern A. Zeeb 
38976b4cac81SBjoern A. Zeeb static void
38986b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
38996b4cac81SBjoern A. Zeeb {
39006b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
39016b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39026b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
39036b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
39046b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
39056b4cac81SBjoern A. Zeeb 
39066b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
39076b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
39086b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
39096b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39106b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39116b4cac81SBjoern A. Zeeb 
39124aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
39134aff4048SBjoern A. Zeeb 
391440839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
391540839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
391640839418SBjoern A. Zeeb 
39178891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
39186b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
39198891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
39206b4cac81SBjoern A. Zeeb 
39216b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
39226b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3923dbf76919SBjoern A. Zeeb 
3924dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3925dbf76919SBjoern A. Zeeb 
3926dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3927dbf76919SBjoern A. Zeeb 
39286c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
39297b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
39306c38c6b1SBjoern A. Zeeb 
39316b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
39326b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
39336b4cac81SBjoern A. Zeeb }
39346b4cac81SBjoern A. Zeeb 
39356b4cac81SBjoern A. Zeeb static void
39366b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
39376b4cac81SBjoern A. Zeeb {
39386b4cac81SBjoern A. Zeeb 
39396b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
39406b4cac81SBjoern A. Zeeb 	TRACEOK();
39416b4cac81SBjoern A. Zeeb }
39426b4cac81SBjoern A. Zeeb 
39436b4cac81SBjoern A. Zeeb static void
39446b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
39456b4cac81SBjoern A. Zeeb {
39466b4cac81SBjoern A. Zeeb 
39476b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
39486b4cac81SBjoern A. Zeeb }
39496b4cac81SBjoern A. Zeeb 
39506b4cac81SBjoern A. Zeeb static void
39516b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
39526b4cac81SBjoern A. Zeeb {
39536b4cac81SBjoern A. Zeeb 
39546b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
39556b4cac81SBjoern A. Zeeb }
39566b4cac81SBjoern A. Zeeb 
39576b4cac81SBjoern A. Zeeb /* Start / stop device. */
39586b4cac81SBjoern A. Zeeb static void
39596b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
39606b4cac81SBjoern A. Zeeb {
39616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39626b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3963cd0fcf9fSBjoern A. Zeeb #ifdef HW_START_STOP
39646b4cac81SBjoern A. Zeeb 	int error;
39658d58a057SBjoern A. Zeeb #endif
39666b4cac81SBjoern A. Zeeb 	bool start_all;
39676b4cac81SBjoern A. Zeeb 
39686b4cac81SBjoern A. Zeeb 	IMPROVE();
39696b4cac81SBjoern A. Zeeb 
39706b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39716b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39726b4cac81SBjoern A. Zeeb 	start_all = false;
39736b4cac81SBjoern A. Zeeb 
39748ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
3975cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
39766b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
39778d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
39786b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
39796b4cac81SBjoern A. Zeeb 		if (error == 0)
39808d58a057SBjoern A. Zeeb #endif
39816b4cac81SBjoern A. Zeeb 			start_all = true;
39826b4cac81SBjoern A. Zeeb 	} else {
39838d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
39847b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
39858d58a057SBjoern A. Zeeb #endif
39866b4cac81SBjoern A. Zeeb 	}
3987cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
39888ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
39896b4cac81SBjoern A. Zeeb 
39906b4cac81SBjoern A. Zeeb 	if (start_all)
39916b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
39926b4cac81SBjoern A. Zeeb }
39936b4cac81SBjoern A. Zeeb 
39946b4cac81SBjoern A. Zeeb bool
39956b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
39966b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
39976b4cac81SBjoern A. Zeeb {
39986b4cac81SBjoern A. Zeeb 	int i;
39996b4cac81SBjoern A. Zeeb 
40006b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
40016b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
40026b4cac81SBjoern A. Zeeb 			return (true);
40036b4cac81SBjoern A. Zeeb 	}
40046b4cac81SBjoern A. Zeeb 
40056b4cac81SBjoern A. Zeeb 	return (false);
40066b4cac81SBjoern A. Zeeb }
40076b4cac81SBjoern A. Zeeb 
40086b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
40096b4cac81SBjoern A. Zeeb bool
40106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
40116b4cac81SBjoern A. Zeeb {
40126b4cac81SBjoern A. Zeeb 	size_t x;
40136b4cac81SBjoern A. Zeeb 	uint8_t l;
40146b4cac81SBjoern A. Zeeb 
40156b4cac81SBjoern A. Zeeb 	x = *xp;
40166b4cac81SBjoern A. Zeeb 
40176b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
40186b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
40196b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
40206b4cac81SBjoern A. Zeeb 	x += 2 + l;
40216b4cac81SBjoern A. Zeeb 
40226b4cac81SBjoern A. Zeeb 	if (x > ies_len)
40236b4cac81SBjoern A. Zeeb 		return (false);
40246b4cac81SBjoern A. Zeeb 
40256b4cac81SBjoern A. Zeeb 	*xp = x;
40266b4cac81SBjoern A. Zeeb 	return (true);
40276b4cac81SBjoern A. Zeeb }
40286b4cac81SBjoern A. Zeeb 
4029d9945d78SBjoern A. Zeeb static uint8_t *
4030d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4031d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
40326b4cac81SBjoern A. Zeeb {
4033d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
4034d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
40353dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
4036d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
4037d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
4038d9945d78SBjoern A. Zeeb 	uint8_t *pb;
4039d9945d78SBjoern A. Zeeb 	int band, i;
40406b4cac81SBjoern A. Zeeb 
40413dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
4042d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4043d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
4044d9945d78SBjoern A. Zeeb 			continue;
4045d9945d78SBjoern A. Zeeb 
4046d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
4047d9945d78SBjoern A. Zeeb 		/*
4048d9945d78SBjoern A. Zeeb 		 * This should not happen;
4049d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
4050d9945d78SBjoern A. Zeeb 		 */
4051d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
4052d9945d78SBjoern A. Zeeb 			continue;
4053d9945d78SBjoern A. Zeeb 
4054d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
4055d9945d78SBjoern A. Zeeb 		channels = supband->channels;
4056d9945d78SBjoern A. Zeeb 		chan = NULL;
4057d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
4058d9945d78SBjoern A. Zeeb 
4059d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4060d9945d78SBjoern A. Zeeb 				continue;
4061d9945d78SBjoern A. Zeeb 
40623dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
4063d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
4064d9945d78SBjoern A. Zeeb 			if (chan != NULL)
4065d9945d78SBjoern A. Zeeb 				break;
4066d9945d78SBjoern A. Zeeb 		}
4067d9945d78SBjoern A. Zeeb 
4068d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
4069d9945d78SBjoern A. Zeeb 		if (chan == NULL)
4070d9945d78SBjoern A. Zeeb 			continue;
4071d9945d78SBjoern A. Zeeb 
4072d9945d78SBjoern A. Zeeb 		pb = p;
40733dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
4074d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
4075d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
4076d9945d78SBjoern A. Zeeb 
40773dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
40783dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
40793dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
40803dd98026SBjoern A. Zeeb 
40813dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
40823dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
40833dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
40843dd98026SBjoern A. Zeeb 		}
40853dd98026SBjoern A. Zeeb #endif
40863dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
40875393cd34SBjoern A. Zeeb 		if (band == NL80211_BAND_5GHZ &&
40885393cd34SBjoern A. Zeeb 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
40893dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
40903dd98026SBjoern A. Zeeb 
40913dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
40923dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
40933dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
40943dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
40953dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
40963dd98026SBjoern A. Zeeb 		}
40973dd98026SBjoern A. Zeeb #endif
40983dd98026SBjoern A. Zeeb 
4099d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
4100d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
4101d9945d78SBjoern A. Zeeb 	}
4102d9945d78SBjoern A. Zeeb 
4103d9945d78SBjoern A. Zeeb 	/* Add common_ies */
4104d9945d78SBjoern A. Zeeb 	pb = p;
4105d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4106d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
4107d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4108d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
4109d9945d78SBjoern A. Zeeb 	}
4110d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
4111d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
4112d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
4113d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
4114d9945d78SBjoern A. Zeeb 	}
4115d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
4116d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
4117d9945d78SBjoern A. Zeeb 
4118d9945d78SBjoern A. Zeeb 	return (p);
41196b4cac81SBjoern A. Zeeb }
41206b4cac81SBjoern A. Zeeb 
41216b4cac81SBjoern A. Zeeb static void
41226b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
41236b4cac81SBjoern A. Zeeb {
41246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41256b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
41276b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
41286b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
41296b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
41306b4cac81SBjoern A. Zeeb 	int error;
41318ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
41326b4cac81SBjoern A. Zeeb 
41336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4135a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
41366b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
41378ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41386b4cac81SBjoern A. Zeeb 		return;
41396b4cac81SBjoern A. Zeeb 	}
41408ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41418ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41426b4cac81SBjoern A. Zeeb 
41436b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
41446b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
41456b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
4146d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
41476b4cac81SBjoern A. Zeeb 		return;
41486b4cac81SBjoern A. Zeeb 	}
41496b4cac81SBjoern A. Zeeb 
41506b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41518ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4152a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4153a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4154d3ef7fb4SBjoern A. Zeeb sw_scan:
41556b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41566b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4157086be6a8SBjoern A. Zeeb 
4158086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4159086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
4160086be6a8SBjoern A. Zeeb 
41616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
41626b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
41636b4cac81SBjoern A. Zeeb 		IMPROVE();
41646b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
41656b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
41666b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
41676b4cac81SBjoern A. Zeeb 
41686b4cac81SBjoern A. Zeeb 	} else {
41696b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
41706b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
41716b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
41726b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
41736b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4174d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
4175d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
4176d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
41773206587aSBjoern A. Zeeb 		bool running;
4178d9945d78SBjoern A. Zeeb 
4179d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4180d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
41816b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
41826b4cac81SBjoern A. Zeeb 
4183d9945d78SBjoern A. Zeeb 		band_mask = 0;
41846b4cac81SBjoern A. Zeeb 		nchan = 0;
4185c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4186c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4187d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
41886b4cac81SBjoern A. Zeeb 				nchan++;
4189d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
4190d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
4191d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
4192d9945d78SBjoern A. Zeeb 			}
4193c272abc5SBjoern A. Zeeb #else
4194c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
4195c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4196c272abc5SBjoern A. Zeeb 				switch (band) {
4197c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
4198c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
4199c272abc5SBjoern A. Zeeb 					break;
4200c272abc5SBjoern A. Zeeb 				default:
4201c272abc5SBjoern A. Zeeb 					continue;
4202c272abc5SBjoern A. Zeeb 				}
4203c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
4204c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
4205c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
4206c272abc5SBjoern A. Zeeb 				}
4207c272abc5SBjoern A. Zeeb 			}
4208c272abc5SBjoern A. Zeeb #endif
4209c272abc5SBjoern A. Zeeb 		} else {
42103206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
42113206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
42123206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
42133206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
42143206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
42153206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
42163206587aSBjoern A. Zeeb 			 */
42173206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
42183206587aSBjoern A. Zeeb 		}
42193206587aSBjoern A. Zeeb 
42206b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
42216b4cac81SBjoern A. Zeeb 
4222d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4223d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4224d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4225d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4226d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4227d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4228d9945d78SBjoern A. Zeeb 
4229d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4230d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4231d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4232d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4233d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4234d9945d78SBjoern A. Zeeb 		}
4235d9945d78SBjoern A. Zeeb 
42363206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4237d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4238d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
42396b4cac81SBjoern A. Zeeb 
42406b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
42416b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
42426b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
42436b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
42446b4cac81SBjoern A. Zeeb #if 0
42456b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
42466b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
42476b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
42486b4cac81SBjoern A. Zeeb #endif
42496b4cac81SBjoern A. Zeeb #ifdef __notyet__
42506b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
42516b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
42526b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
42536b4cac81SBjoern A. Zeeb #endif
4254e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
42556b4cac81SBjoern A. Zeeb 
42566b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
42576b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
42586b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
42596b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
42606b4cac81SBjoern A. Zeeb 			*(cpp + i) =
42616b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
42626b4cac81SBjoern A. Zeeb 		}
4263c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
42646b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4265c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
42666b4cac81SBjoern A. Zeeb 
4267c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
42686b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
42693206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
42706b4cac81SBjoern A. Zeeb 			/* lc->flags */
42716b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
42726b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
42736b4cac81SBjoern A. Zeeb 			/* lc-> ... */
42746b4cac81SBjoern A. Zeeb 			lc++;
42756b4cac81SBjoern A. Zeeb 		}
4276c272abc5SBjoern A. Zeeb #else
4277c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4278c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4279c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4280c272abc5SBjoern A. Zeeb 
4281c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4282c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4283c272abc5SBjoern A. Zeeb 				continue;
4284c272abc5SBjoern A. Zeeb 
4285c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4286c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4287c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4288c272abc5SBjoern A. Zeeb 				continue;
4289c272abc5SBjoern A. Zeeb 
4290c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4291c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4292c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4293c272abc5SBjoern A. Zeeb 				lc++;
4294c272abc5SBjoern A. Zeeb 			}
4295c272abc5SBjoern A. Zeeb 		}
4296c272abc5SBjoern A. Zeeb #endif
42976b4cac81SBjoern A. Zeeb 
4298d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
42996b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
43006b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
43016b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4302d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
43036b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
43046b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
43056b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
43066b4cac81SBjoern A. Zeeb 				ssids++;
43076b4cac81SBjoern A. Zeeb 			}
43086b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
43096b4cac81SBjoern A. Zeeb 		} else {
43106b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
43116b4cac81SBjoern A. Zeeb 		}
43126b4cac81SBjoern A. Zeeb 
43136b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
43146b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
43156b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
43166b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
43176b4cac81SBjoern A. Zeeb 		/* s6gp->... */
43186b4cac81SBjoern A. Zeeb 
4319d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4320d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4321d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4322d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4323d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4324d9945d78SBjoern A. Zeeb 
43256b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
43266b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
43273206587aSBjoern A. Zeeb 
43283206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
43293206587aSBjoern A. Zeeb 		/* Re-check under lock. */
43303206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
43313206587aSBjoern A. Zeeb 		if (!running) {
43323206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
43333206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
43343206587aSBjoern A. Zeeb 
43353206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
43363206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
43373206587aSBjoern A. Zeeb 		}
43383206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43393206587aSBjoern A. Zeeb 		if (running) {
43403206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
43413206587aSBjoern A. Zeeb 			return;
43423206587aSBjoern A. Zeeb 		}
43433206587aSBjoern A. Zeeb 
43446b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
43456b4cac81SBjoern A. Zeeb 		if (error != 0) {
4346d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4347d9945d78SBjoern A. Zeeb 
43483206587aSBjoern A. Zeeb 			/*
43493206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
43503206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
43513206587aSBjoern A. Zeeb 			 * and only there.
43523206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
43533206587aSBjoern A. Zeeb 			 * behave differently:
43543206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
43553206587aSBjoern A. Zeeb 			 *        and can then still return an error.
43563206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
43573206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
43583206587aSBjoern A. Zeeb 			 * ...
43593206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
43603206587aSBjoern A. Zeeb 			 * and balance between both code paths.
43613206587aSBjoern A. Zeeb 			 */
43623206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
43633206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
43643206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
43656b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
43663206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
43673206587aSBjoern A. Zeeb 			}
43683206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4369d3ef7fb4SBjoern A. Zeeb 
4370d3ef7fb4SBjoern A. Zeeb 			/*
4371d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4372d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4373d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4374d3ef7fb4SBjoern A. Zeeb 			 */
4375196cfd0bSBjoern A. Zeeb 			if (error == 1) {
43768ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4377a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
43788ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43793206587aSBjoern A. Zeeb 				/*
43803206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
43813206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
43823206587aSBjoern A. Zeeb 				 * currently not re-enable it?
43833206587aSBjoern A. Zeeb 				 */
43843206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4385196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4386196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4387196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4388196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4389196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4390196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4391196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4392196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4393d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4394196cfd0bSBjoern A. Zeeb 			}
4395d3ef7fb4SBjoern A. Zeeb 
4396d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4397d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
43986b4cac81SBjoern A. Zeeb 		}
43996b4cac81SBjoern A. Zeeb 	}
44006b4cac81SBjoern A. Zeeb }
44016b4cac81SBjoern A. Zeeb 
44026b4cac81SBjoern A. Zeeb static void
44036b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
44046b4cac81SBjoern A. Zeeb {
44056b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44068ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
44076b4cac81SBjoern A. Zeeb 
44086b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4410a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
44118ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44126b4cac81SBjoern A. Zeeb 		return;
44136b4cac81SBjoern A. Zeeb 	}
44148ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44166b4cac81SBjoern A. Zeeb 
44178ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4418a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4419a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
44206b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
44216b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
44226b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
44236b4cac81SBjoern A. Zeeb 
4424a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4425a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
44266b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
44276b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
44286b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4429a486fbbdSBjoern A. Zeeb 
44306b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
44316b4cac81SBjoern A. Zeeb 
44326b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4433086be6a8SBjoern A. Zeeb 
4434086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4435086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
44366b4cac81SBjoern A. Zeeb 	}
44376b4cac81SBjoern A. Zeeb }
44386b4cac81SBjoern A. Zeeb 
44396b4cac81SBjoern A. Zeeb static void
4440a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4441a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4442a486fbbdSBjoern A. Zeeb {
4443a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
44448ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4445a486fbbdSBjoern A. Zeeb 
4446a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
44478ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44488ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44508ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4451a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4452a486fbbdSBjoern A. Zeeb }
4453a486fbbdSBjoern A. Zeeb 
4454a486fbbdSBjoern A. Zeeb static void
4455a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4456a486fbbdSBjoern A. Zeeb {
4457a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
44588ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4459a486fbbdSBjoern A. Zeeb 
4460a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
44618ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44628ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44648ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4465a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4466a486fbbdSBjoern A. Zeeb }
4467a486fbbdSBjoern A. Zeeb 
4468a486fbbdSBjoern A. Zeeb static void
44696b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
44706b4cac81SBjoern A. Zeeb {
44716b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44726b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4473b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4474b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
44756b4cac81SBjoern A. Zeeb 	int error;
44768ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
44776b4cac81SBjoern A. Zeeb 
44786b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4479b2cf3c21SBjoern A. Zeeb 
4480b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4481b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
44826b4cac81SBjoern A. Zeeb 		return;
44836b4cac81SBjoern A. Zeeb 
4484a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
44858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44868ac540d3SBjoern A. Zeeb 	hw_scan_running =
44878ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
44888ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
44898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44908ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4491a486fbbdSBjoern A. Zeeb 		return;
4492a486fbbdSBjoern A. Zeeb 
4493b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4494b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
44956b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
44966b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
44976b4cac81SBjoern A. Zeeb 		return;
44986b4cac81SBjoern A. Zeeb 	}
44996b4cac81SBjoern A. Zeeb 
45006b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
45016b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
45026b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
45036b4cac81SBjoern A. Zeeb 		    c, chan);
45046b4cac81SBjoern A. Zeeb 		return;
45056b4cac81SBjoern A. Zeeb 	}
45066b4cac81SBjoern A. Zeeb 
45076b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
45086b4cac81SBjoern A. Zeeb 	IMPROVE();
45096b4cac81SBjoern A. Zeeb 
45106b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45114a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
45129fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
451311450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
45149fb91463SBjoern A. Zeeb #endif
45154a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
45166b4cac81SBjoern A. Zeeb 
45176b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
45186b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
45196b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
45206b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
45216b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
45226b4cac81SBjoern A. Zeeb 		IMPROVE();
45236b4cac81SBjoern A. Zeeb 	} else {
45246b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
45256b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
45266b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
45276b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
45286b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
45296b4cac81SBjoern A. Zeeb 	}
45306b4cac81SBjoern A. Zeeb 
45316b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
45326b4cac81SBjoern A. Zeeb 	IMPROVE();
45336b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
45346b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
45356b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
45366b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
45376b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
45386b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
45396b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
45406b4cac81SBjoern A. Zeeb 			    error);
45416b4cac81SBjoern A. Zeeb 	}
45426b4cac81SBjoern A. Zeeb }
45436b4cac81SBjoern A. Zeeb 
45446b4cac81SBjoern A. Zeeb static struct ieee80211_node *
45456b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
45466b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
45476b4cac81SBjoern A. Zeeb {
45486b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45504f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
45516b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
45526b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45536b4cac81SBjoern A. Zeeb 
45546b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
45556b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45566b4cac81SBjoern A. Zeeb 
45576b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
45584f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
45594f61ef8bSBjoern A. Zeeb 		return (NULL);
45604f61ef8bSBjoern A. Zeeb 
45616b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
45626b4cac81SBjoern A. Zeeb 	if (ni == NULL)
45636b4cac81SBjoern A. Zeeb 		return (NULL);
45646b4cac81SBjoern A. Zeeb 
45656b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45664f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
45676b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
45686b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
45696b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
45706b4cac81SBjoern A. Zeeb 		return (NULL);
45716b4cac81SBjoern A. Zeeb 	}
45726b4cac81SBjoern A. Zeeb 
45736b4cac81SBjoern A. Zeeb 	return (ni);
45746b4cac81SBjoern A. Zeeb }
45756b4cac81SBjoern A. Zeeb 
45766b4cac81SBjoern A. Zeeb static int
45776b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
45786b4cac81SBjoern A. Zeeb {
45796b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45816b4cac81SBjoern A. Zeeb 	int error;
45826b4cac81SBjoern A. Zeeb 
45836b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45846b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45856b4cac81SBjoern A. Zeeb 
45866b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
45876b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
45886b4cac81SBjoern A. Zeeb 		if (error != 0)
45896b4cac81SBjoern A. Zeeb 			return (error);
45906b4cac81SBjoern A. Zeeb 	}
45916b4cac81SBjoern A. Zeeb 
45926b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
45936b4cac81SBjoern A. Zeeb 	IMPROVE();
45946b4cac81SBjoern A. Zeeb 
45956b4cac81SBjoern A. Zeeb 	return (0);
45966b4cac81SBjoern A. Zeeb }
45976b4cac81SBjoern A. Zeeb 
45986b4cac81SBjoern A. Zeeb static void
45996b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
46006b4cac81SBjoern A. Zeeb {
46016b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46026b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46036b4cac81SBjoern A. Zeeb 
46046b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46066b4cac81SBjoern A. Zeeb 
46076b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
46086b4cac81SBjoern A. Zeeb 	IMPROVE();
46096b4cac81SBjoern A. Zeeb 
46106b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
46116b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
46126b4cac81SBjoern A. Zeeb }
46136b4cac81SBjoern A. Zeeb 
46146b4cac81SBjoern A. Zeeb static void
46156b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
46166b4cac81SBjoern A. Zeeb {
46176b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
46186b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46196b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46206b4cac81SBjoern A. Zeeb 
46216b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
46226b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
46236b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
46246b4cac81SBjoern A. Zeeb 
46250936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
46266b4cac81SBjoern A. Zeeb 
46270936c648SBjoern A. Zeeb 	/*
46280936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
46290936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
46300936c648SBjoern A. Zeeb 	 */
46310936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
46326b4cac81SBjoern A. Zeeb 
46336b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
46346b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
46356b4cac81SBjoern A. Zeeb }
46366b4cac81SBjoern A. Zeeb 
46379a45c3caSBjoern A. Zeeb /*
46389a45c3caSBjoern A. Zeeb  * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
46399a45c3caSBjoern A. Zeeb  * call path.
46409a45c3caSBjoern A. Zeeb  * Unfortunately they have slightly different invariants.  See
46419a45c3caSBjoern A. Zeeb  * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
46429a45c3caSBjoern A. Zeeb  * Both take care of the ni reference in case of error, and otherwise during
46439a45c3caSBjoern A. Zeeb  * the callback after transmit.
46449a45c3caSBjoern A. Zeeb  * The difference is that in case of error (*ic_raw_xmit) needs us to release
46459a45c3caSBjoern A. Zeeb  * the mbuf, while (*ic_transmit) will free the mbuf itself.
46469a45c3caSBjoern A. Zeeb  */
46476b4cac81SBjoern A. Zeeb static int
46489a45c3caSBjoern A. Zeeb lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
46499a45c3caSBjoern A. Zeeb     const struct ieee80211_bpf_params *params __unused,
46509a45c3caSBjoern A. Zeeb     bool freem)
46516b4cac81SBjoern A. Zeeb {
46526b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4653c816f64eSBjoern A. Zeeb 	int error;
46546b4cac81SBjoern A. Zeeb 
46556b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4656fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
46572372f8ccSBjoern A. Zeeb #if 0
465888665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
46592372f8ccSBjoern A. Zeeb #else
46602372f8ccSBjoern A. Zeeb 	/*
46612372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46622372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46632372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46642372f8ccSBjoern A. Zeeb 	 */
46652372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
46662372f8ccSBjoern A. Zeeb #endif
4667fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46689a45c3caSBjoern A. Zeeb 		if (freem)
46690936c648SBjoern A. Zeeb 			m_free(m);
46700936c648SBjoern A. Zeeb 		return (ENETDOWN);
46710936c648SBjoern A. Zeeb 	}
46726b4cac81SBjoern A. Zeeb 
46736b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
4674c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lsta->txq, m);
4675c816f64eSBjoern A. Zeeb 	if (error != 0) {
4676c816f64eSBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46779a45c3caSBjoern A. Zeeb 		if (freem)
4678c816f64eSBjoern A. Zeeb 			m_free(m);
4679c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4680c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4681c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4682c816f64eSBjoern A. Zeeb 			    __func__, error);
4683c816f64eSBjoern A. Zeeb #endif
4684c816f64eSBjoern A. Zeeb 		return (ENETDOWN);
4685c816f64eSBjoern A. Zeeb 	}
4686fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4687fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46886b4cac81SBjoern A. Zeeb 
46899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46909d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
46916b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
46926b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
46936b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
46949d9ba2b7SBjoern A. Zeeb #endif
46956b4cac81SBjoern A. Zeeb 
46966b4cac81SBjoern A. Zeeb 	return (0);
46976b4cac81SBjoern A. Zeeb }
46986b4cac81SBjoern A. Zeeb 
46999a45c3caSBjoern A. Zeeb static int
47009a45c3caSBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
47019a45c3caSBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
47029a45c3caSBjoern A. Zeeb {
47039a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, true));
47049a45c3caSBjoern A. Zeeb }
47059a45c3caSBjoern A. Zeeb 
470611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
47074b9ae864SBjoern A. Zeeb /*
47084b9ae864SBjoern A. Zeeb  * This is a bit of a hack given we know we are operating on a
47094b9ae864SBjoern A. Zeeb  * single frame and we know that hardware will deal with it.
47104b9ae864SBjoern A. Zeeb  * But otherwise the enmic bit and the encrypt bit need to be
47114b9ae864SBjoern A. Zeeb  * decoupled.
47124b9ae864SBjoern A. Zeeb  */
471311db70b6SBjoern A. Zeeb static int
471498e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
471598e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
471611db70b6SBjoern A. Zeeb {
471798e55905SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
471898e55905SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
47194b9ae864SBjoern A. Zeeb 	uint8_t *p;
472098e55905SBjoern A. Zeeb 
472198e55905SBjoern A. Zeeb 	/*
47224b9ae864SBjoern A. Zeeb 	 * TKIP only happens on data.
47234b9ae864SBjoern A. Zeeb 	 */
47244b9ae864SBjoern A. Zeeb 	hdr = (void *)skb->data;
47254b9ae864SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control))
47264b9ae864SBjoern A. Zeeb 		return (0);
47274b9ae864SBjoern A. Zeeb 
47284b9ae864SBjoern A. Zeeb 	/*
47294b9ae864SBjoern A. Zeeb 	 * "enmic" (though we do not do that).
47304b9ae864SBjoern A. Zeeb 	 */
47314b9ae864SBjoern A. Zeeb 	/* any conditions to not apply this? */
47324b9ae864SBjoern A. Zeeb 	if (skb_tailroom(skb) < k->wk_cipher->ic_miclen)
47334b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
47344b9ae864SBjoern A. Zeeb 
47354b9ae864SBjoern A. Zeeb 	p = skb_put(skb, k->wk_cipher->ic_miclen);
47364b9ae864SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
47374b9ae864SBjoern A. Zeeb 		goto encrypt;
47384b9ae864SBjoern A. Zeeb 
47394b9ae864SBjoern A. Zeeb 	/*
47404b9ae864SBjoern A. Zeeb 	 * (*enmic) which we hopefully do not have to do with hw accel.
47414b9ae864SBjoern A. Zeeb 	 * That means if we make it here we have a problem.
47424b9ae864SBjoern A. Zeeb 	 */
47434b9ae864SBjoern A. Zeeb 	TODO("(*enmic)");
47444b9ae864SBjoern A. Zeeb 	return (ENXIO);
47454b9ae864SBjoern A. Zeeb 
47464b9ae864SBjoern A. Zeeb encrypt:
47474b9ae864SBjoern A. Zeeb 	/*
47484b9ae864SBjoern A. Zeeb 	 * "encrypt" (though we do not do that).
47494b9ae864SBjoern A. Zeeb 	 */
47504b9ae864SBjoern A. Zeeb 	/*
47514b9ae864SBjoern A. Zeeb 	 * Check if we have anything to do as requested by driver
475298e55905SBjoern A. Zeeb 	 * or if we are done?
475398e55905SBjoern A. Zeeb 	 */
475498e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
475598e55905SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
475698e55905SBjoern A. Zeeb 			return (0);
475798e55905SBjoern A. Zeeb 
475898e55905SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
475998e55905SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
47604b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
476198e55905SBjoern A. Zeeb 
476298e55905SBjoern A. Zeeb 	hdr = (void *)skb->data;
476398e55905SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
476498e55905SBjoern A. Zeeb 	p = skb_push(skb, hlen);
476598e55905SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
476698e55905SBjoern A. Zeeb 
476798e55905SBjoern A. Zeeb 	/* If driver request space only we are done. */
476898e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
476998e55905SBjoern A. Zeeb 		return (0);
477098e55905SBjoern A. Zeeb 
477198e55905SBjoern A. Zeeb 	p += hdrlen;
477298e55905SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
477398e55905SBjoern A. Zeeb 
47744b9ae864SBjoern A. Zeeb 	/* If we make it hear we do sw encryption. */
47754b9ae864SBjoern A. Zeeb 	TODO("sw encrypt");
477698e55905SBjoern A. Zeeb 	return (ENXIO);
477798e55905SBjoern A. Zeeb }
477898e55905SBjoern A. Zeeb static int
477998e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
478098e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
478198e55905SBjoern A. Zeeb {
478211db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
478311db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
478411db70b6SBjoern A. Zeeb 	uint8_t *p;
478511db70b6SBjoern A. Zeeb 
478611db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
478711db70b6SBjoern A. Zeeb 
478811db70b6SBjoern A. Zeeb 	/*
478911db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
479011db70b6SBjoern A. Zeeb 	 * or if we are done?
479111db70b6SBjoern A. Zeeb 	 */
479211db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
479311db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
479411db70b6SBjoern A. Zeeb 	    /* MFP */
479511db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
479611db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
479711db70b6SBjoern A. Zeeb 			return (0);
479811db70b6SBjoern A. Zeeb 
479911db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
480011db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
48014b9ae864SBjoern A. Zeeb 		return (ENOBUFS);
480211db70b6SBjoern A. Zeeb 
480311db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
480411db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
480511db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
480611db70b6SBjoern A. Zeeb 
480711db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
480811db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
480911db70b6SBjoern A. Zeeb 		return (0);
481011db70b6SBjoern A. Zeeb 
481111db70b6SBjoern A. Zeeb 	p += hdrlen;
481211db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
481311db70b6SBjoern A. Zeeb 
481411db70b6SBjoern A. Zeeb 	return (0);
481511db70b6SBjoern A. Zeeb }
481698e55905SBjoern A. Zeeb 
481798e55905SBjoern A. Zeeb static int
481898e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
481998e55905SBjoern A. Zeeb     struct sk_buff *skb)
482098e55905SBjoern A. Zeeb {
482198e55905SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
482298e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
482398e55905SBjoern A. Zeeb 
482498e55905SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
482598e55905SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
482698e55905SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
482798e55905SBjoern A. Zeeb 
482898e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
482998e55905SBjoern A. Zeeb 
483098e55905SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
483198e55905SBjoern A. Zeeb 	info->control.hw_key = kc;
483298e55905SBjoern A. Zeeb 
483398e55905SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
483498e55905SBjoern A. Zeeb 	if (kc == NULL) {
483598e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
483698e55905SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
483798e55905SBjoern A. Zeeb 		return (ENXIO);
483898e55905SBjoern A. Zeeb 	}
483998e55905SBjoern A. Zeeb 
484098e55905SBjoern A. Zeeb 	switch (kc->cipher) {
484198e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
484298e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
484398e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
484498e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
484598e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
484698e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
484798e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
484898e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
484998e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
485098e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
485198e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
485298e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
485398e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
485498e55905SBjoern A. Zeeb 	default:
485598e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
485698e55905SBjoern A. Zeeb 		    "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
485798e55905SBjoern A. Zeeb 		    skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
485898e55905SBjoern A. Zeeb 		return (EOPNOTSUPP);
485998e55905SBjoern A. Zeeb 	}
486098e55905SBjoern A. Zeeb }
486198e55905SBjoern A. Zeeb 
486298e55905SBjoern A. Zeeb static uint8_t
486398e55905SBjoern A. Zeeb lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
486498e55905SBjoern A. Zeeb {
486598e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
486698e55905SBjoern A. Zeeb 
486798e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
486898e55905SBjoern A. Zeeb 	if (kc == NULL)
486998e55905SBjoern A. Zeeb 		return (0);
487098e55905SBjoern A. Zeeb 
487198e55905SBjoern A. Zeeb 	IMPROVE("which other flags need tailroom?");
487298e55905SBjoern A. Zeeb 	if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
487398e55905SBjoern A. Zeeb 		return (32);	/* Large enough to hold everything and pow2. */
487498e55905SBjoern A. Zeeb 
487598e55905SBjoern A. Zeeb 	return (0);
487698e55905SBjoern A. Zeeb }
487711db70b6SBjoern A. Zeeb #endif
487811db70b6SBjoern A. Zeeb 
48796b4cac81SBjoern A. Zeeb static void
48806b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
48816b4cac81SBjoern A. Zeeb {
48826b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
48836b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
48846b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
48856b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
48866b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
48876b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
48896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
48906b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
48916b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
48926b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
48936b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
48946b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4895e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4896ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
48976b4cac81SBjoern A. Zeeb 	void *buf;
489811db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
489998e55905SBjoern A. Zeeb 	uint8_t ac, tid, tailroom;
49006b4cac81SBjoern A. Zeeb 
49016b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
49026b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49039d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
49046b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
49056b4cac81SBjoern A. Zeeb #endif
49066b4cac81SBjoern A. Zeeb 
49076b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4908b35f6cd0SBjoern A. Zeeb 	k = NULL;
490911db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
49106b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
49116b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
491211db70b6SBjoern A. Zeeb 
491311db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
491411db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
491511db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
491611db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
491711db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
491811db70b6SBjoern A. Zeeb 		}
491911db70b6SBjoern A. Zeeb #endif
492011db70b6SBjoern A. Zeeb 
492111db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
492211db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
49236b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
49246b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
49256b4cac81SBjoern A. Zeeb 			if (k == NULL) {
49266b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
49276b4cac81SBjoern A. Zeeb 				m_freem(m);
49286b4cac81SBjoern A. Zeeb 				return;
49296b4cac81SBjoern A. Zeeb 			}
49306b4cac81SBjoern A. Zeeb 		}
493111db70b6SBjoern A. Zeeb 	}
49326b4cac81SBjoern A. Zeeb 
49336b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
49346b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
49356b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
49366b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
49376b4cac81SBjoern A. Zeeb 
49386b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
49396b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
49406b4cac81SBjoern A. Zeeb 
49416b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
49426b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
49436b4cac81SBjoern A. Zeeb 		if (k != NULL)
49446b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
49456b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
49466b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
49476b4cac81SBjoern A. Zeeb 		IMPROVE();
49486b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
49496b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
49506b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
49516b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
49526b4cac81SBjoern A. Zeeb 		}
49536b4cac81SBjoern A. Zeeb 
49546b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
49556b4cac81SBjoern A. Zeeb 	}
49566b4cac81SBjoern A. Zeeb 
495798e55905SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
495898e55905SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
495998e55905SBjoern A. Zeeb 		tailroom = lkpi_hw_crypto_tailroom(lsta, k);
496098e55905SBjoern A. Zeeb 	else
496198e55905SBjoern A. Zeeb #endif
496298e55905SBjoern A. Zeeb 		tailroom = 0;
496398e55905SBjoern A. Zeeb 
49646b4cac81SBjoern A. Zeeb 	/*
49656b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
49666b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
49673d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
49683d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
49696b4cac81SBjoern A. Zeeb 	 */
497098e55905SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
49716b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4972bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4973bcf1d8eeSBjoern A. Zeeb 
4974bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4975bcf1d8eeSBjoern A. Zeeb 			int tid;
4976bcf1d8eeSBjoern A. Zeeb 
4977bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4978bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4979bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4980bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4981bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4982bcf1d8eeSBjoern A. Zeeb 					continue;
4983bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4984bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4985bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4986bcf1d8eeSBjoern A. Zeeb 			}
4987bcf1d8eeSBjoern A. Zeeb 		}
49886b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
49896b4cac81SBjoern A. Zeeb 		m_freem(m);
49906b4cac81SBjoern A. Zeeb 		return;
49916b4cac81SBjoern A. Zeeb 	}
49926b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
49936b4cac81SBjoern A. Zeeb 
49946b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
49956b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
49966b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
49976b4cac81SBjoern A. Zeeb 	skb->m = m;
49986b4cac81SBjoern A. Zeeb #if 0
49996b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
50006b4cac81SBjoern A. Zeeb #else
50016b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
50026b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
50036b4cac81SBjoern A. Zeeb #endif
50046b4cac81SBjoern A. Zeeb 	/* Save the ni. */
50056b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
50066b4cac81SBjoern A. Zeeb 
50076b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
50086b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50096b4cac81SBjoern A. Zeeb 
5010e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
5011e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
5012e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
50131665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
50141665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
50151665ef97SBjoern A. Zeeb 			skb->priority = 7;
50161665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
50171665ef97SBjoern A. Zeeb 		} else {
50181665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
50191665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
5020e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
5021e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
50221665ef97SBjoern A. Zeeb 		}
5023e3a0b120SBjoern A. Zeeb 	} else {
5024e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
5025fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
5026e3a0b120SBjoern A. Zeeb 	}
50276b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
50286b4cac81SBjoern A. Zeeb 
50296b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
50306b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
50316b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
50326b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
50336b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
50346b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
5035e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
50366b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
50376b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
50386b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
50396b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
50409fb91463SBjoern A. Zeeb #ifdef __notyet__
50419fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50429fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
50439fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
50449fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
50459fb91463SBjoern A. Zeeb #endif
50469fb91463SBjoern A. Zeeb #endif
50476b4cac81SBjoern A. Zeeb 
50486b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
5049b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
505011db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
505111db70b6SBjoern A. Zeeb 		int error;
505211db70b6SBjoern A. Zeeb 
505311db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
505411db70b6SBjoern A. Zeeb 		if (error != 0) {
505511db70b6SBjoern A. Zeeb 			/*
505611db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
505711db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
505811db70b6SBjoern A. Zeeb 			 */
505911db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
506011db70b6SBjoern A. Zeeb 			return;
506111db70b6SBjoern A. Zeeb 		}
506211db70b6SBjoern A. Zeeb 	}
50636b4cac81SBjoern A. Zeeb #endif
50646b4cac81SBjoern A. Zeeb 
50656b4cac81SBjoern A. Zeeb 	IMPROVE();
50666b4cac81SBjoern A. Zeeb 
5067e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
5068e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
5069e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
5070e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
5071e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
5072d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
5073e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
5074e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
5075e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
5076e3a0b120SBjoern A. Zeeb 	}
5077e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
5078d0d29110SBjoern A. Zeeb 		goto ops_tx;
5079e3a0b120SBjoern A. Zeeb 
5080d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
5081d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
5082d0d29110SBjoern A. Zeeb 
5083eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
50846b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
50859d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50869d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5087d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
5088d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5089d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
5090fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
5091d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
5092d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5093d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
5094d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
50959d9ba2b7SBjoern A. Zeeb #endif
5096eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5097cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5098d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
5099cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
51006b4cac81SBjoern A. Zeeb 	return;
51016b4cac81SBjoern A. Zeeb 
51026b4cac81SBjoern A. Zeeb ops_tx:
51039d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51049d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5105d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5106d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
51076b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
51086b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
51099d9ba2b7SBjoern A. Zeeb #endif
51106b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
51116b4cac81SBjoern A. Zeeb 	control.sta = sta;
5112cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
51136b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
5114cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
51156b4cac81SBjoern A. Zeeb }
51166b4cac81SBjoern A. Zeeb 
51176b4cac81SBjoern A. Zeeb static void
51186b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
51196b4cac81SBjoern A. Zeeb {
51206b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
51216b4cac81SBjoern A. Zeeb 	struct mbufq mq;
51226b4cac81SBjoern A. Zeeb 	struct mbuf *m;
512388665349SBjoern A. Zeeb 	bool shall_tx;
51246b4cac81SBjoern A. Zeeb 
51256b4cac81SBjoern A. Zeeb 	lsta = ctx;
51266b4cac81SBjoern A. Zeeb 
51279d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
51289d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
51296b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
51309d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
51316b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
51329d9ba2b7SBjoern A. Zeeb #endif
51336b4cac81SBjoern A. Zeeb 
51346b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
51356b4cac81SBjoern A. Zeeb 
5136fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
5137fa4e4257SBjoern A. Zeeb 	/*
5138fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
513988665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
514088665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
514188665349SBjoern A. Zeeb 	 * point to try to TX.
514288665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
514388665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
5144fa4e4257SBjoern A. Zeeb 	 */
51452372f8ccSBjoern A. Zeeb #if 0
514688665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
51472372f8ccSBjoern A. Zeeb #else
51482372f8ccSBjoern A. Zeeb 	/*
51492372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
51502372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
51512372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
51522372f8ccSBjoern A. Zeeb 	 */
51532372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
51542372f8ccSBjoern A. Zeeb #endif
515588665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
51566b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
515788665349SBjoern A. Zeeb 	/*
515888665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
515988665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
516088665349SBjoern A. Zeeb 	 */
5161fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
51626b4cac81SBjoern A. Zeeb 
51636b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
51646b4cac81SBjoern A. Zeeb 	while (m != NULL) {
51656b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
51666b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
51676b4cac81SBjoern A. Zeeb 	}
51686b4cac81SBjoern A. Zeeb }
51696b4cac81SBjoern A. Zeeb 
51706b4cac81SBjoern A. Zeeb static int
51716b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
51726b4cac81SBjoern A. Zeeb {
51736b4cac81SBjoern A. Zeeb 
51746b4cac81SBjoern A. Zeeb 	/* XXX TODO */
51756b4cac81SBjoern A. Zeeb 	IMPROVE();
51766b4cac81SBjoern A. Zeeb 
51776b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
51786b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
51796b4cac81SBjoern A. Zeeb 
51806b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
51819a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, false));
51826b4cac81SBjoern A. Zeeb }
51836b4cac81SBjoern A. Zeeb 
51849fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
51859fb91463SBjoern A. Zeeb static int
51869fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
51879fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
51889fb91463SBjoern A. Zeeb {
51899fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51909fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51919fb91463SBjoern A. Zeeb 
51929fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51939fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51949fb91463SBjoern A. Zeeb 
5195310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
51969fb91463SBjoern A. Zeeb 
51979fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
51989fb91463SBjoern A. Zeeb }
51999fb91463SBjoern A. Zeeb 
52009fb91463SBjoern A. Zeeb static int
52019fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
52029fb91463SBjoern A. Zeeb {
52039fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52049fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52059fb91463SBjoern A. Zeeb 
52069fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52079fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52089fb91463SBjoern A. Zeeb 
5209310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
52109fb91463SBjoern A. Zeeb 
52119fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
52129fb91463SBjoern A. Zeeb }
52139fb91463SBjoern A. Zeeb 
52149fb91463SBjoern A. Zeeb 
52159fb91463SBjoern A. Zeeb static int
52169fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
52179fb91463SBjoern A. Zeeb {
52189fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52199fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52209fb91463SBjoern A. Zeeb 
52219fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52229fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
52239fb91463SBjoern A. Zeeb 
5224310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
52259fb91463SBjoern A. Zeeb 
52269fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
52279fb91463SBjoern A. Zeeb }
52289fb91463SBjoern A. Zeeb 
5229310743c4SBjoern A. Zeeb /*
5230310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5231310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5232310743c4SBjoern A. Zeeb  *
5233310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
5234310743c4SBjoern A. Zeeb  */
52359fb91463SBjoern A. Zeeb static int
52369fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
52379fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
52389fb91463SBjoern A. Zeeb {
52399fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52409fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5241310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5242310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5243310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5244310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5245310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5246310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5247310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5248310743c4SBjoern A. Zeeb 	int error;
52499fb91463SBjoern A. Zeeb 
52509fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52519fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5252310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5253310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5254310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5255310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5256310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5257310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52589fb91463SBjoern A. Zeeb 
5259310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5260310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5261310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5262310743c4SBjoern A. Zeeb 		return (0);
5263310743c4SBjoern A. Zeeb 	}
5264310743c4SBjoern A. Zeeb 
5265310743c4SBjoern A. Zeeb 	params.sta = sta;
5266310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
5267310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
5268310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5269310743c4SBjoern A. Zeeb 	params.timeout = 0;
5270310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5271310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5272310743c4SBjoern A. Zeeb 	params.amsdu = false;
5273310743c4SBjoern A. Zeeb 
5274310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5275cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5276310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5277cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5278310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5279310743c4SBjoern A. Zeeb 	if (error != 0) {
5280310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5281310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5282310743c4SBjoern A. Zeeb 		return (0);
5283310743c4SBjoern A. Zeeb 	}
52849fb91463SBjoern A. Zeeb 
52859fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
52869fb91463SBjoern A. Zeeb }
52879fb91463SBjoern A. Zeeb 
5288310743c4SBjoern A. Zeeb /*
5289310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5290310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
5291310743c4SBjoern A. Zeeb  *
5292310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
5293310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
5294310743c4SBjoern A. Zeeb  */
52959fb91463SBjoern A. Zeeb static int
52969fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
52979fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
52989fb91463SBjoern A. Zeeb {
52999fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53009fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5301310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5302310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5303310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5304310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5305310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5306310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5307310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5308310743c4SBjoern A. Zeeb 	int error;
53099fb91463SBjoern A. Zeeb 
53109fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53119fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5312310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5313310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5314310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5315310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5316310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5317310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53189fb91463SBjoern A. Zeeb 
5319310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5320310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5321310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5322310743c4SBjoern A. Zeeb 		return (0);
5323310743c4SBjoern A. Zeeb 	}
5324310743c4SBjoern A. Zeeb 
5325310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
5326310743c4SBjoern A. Zeeb 		params.sta = sta;
5327310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5328310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
5329310743c4SBjoern A. Zeeb 		params.timeout = 0;
5330310743c4SBjoern A. Zeeb 		params.ssn = 0;
5331310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5332310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5333310743c4SBjoern A. Zeeb 			params.amsdu = true;
5334310743c4SBjoern A. Zeeb 		else
5335310743c4SBjoern A. Zeeb 			params.amsdu = false;
5336310743c4SBjoern A. Zeeb 	} else {
5337310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
5338310743c4SBjoern A. Zeeb 		params.sta = sta;
5339310743c4SBjoern A. Zeeb 		switch (status) {
5340310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
5341310743c4SBjoern A. Zeeb 		default:
5342310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5343310743c4SBjoern A. Zeeb 			break;
5344310743c4SBjoern A. Zeeb 		}
5345310743c4SBjoern A. Zeeb 		params.buf_size = 0;
5346310743c4SBjoern A. Zeeb 		params.timeout = 0;
5347310743c4SBjoern A. Zeeb 		params.ssn = 0;
5348310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5349310743c4SBjoern A. Zeeb 		params.amsdu = false;
5350310743c4SBjoern A. Zeeb 	}
5351310743c4SBjoern A. Zeeb 
5352310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5353cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5354310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5355cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5356310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5357310743c4SBjoern A. Zeeb 	if (error != 0) {
5358310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5359310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5360310743c4SBjoern A. Zeeb 		return (0);
5361310743c4SBjoern A. Zeeb 	}
5362310743c4SBjoern A. Zeeb 
5363310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
53649fb91463SBjoern A. Zeeb 
53659fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
53669fb91463SBjoern A. Zeeb }
53679fb91463SBjoern A. Zeeb 
5368310743c4SBjoern A. Zeeb /*
5369310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5370310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5371310743c4SBjoern A. Zeeb  */
53729fb91463SBjoern A. Zeeb static void
53739fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
53749fb91463SBjoern A. Zeeb {
53759fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53769fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5377310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5378310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5379310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5380310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5381310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5382310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5383310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5384310743c4SBjoern A. Zeeb 	int error;
53859fb91463SBjoern A. Zeeb 
53869fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53879fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5388310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5389310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5390310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5391310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5392310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5393310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53949fb91463SBjoern A. Zeeb 
5395310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5396310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5397310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5398310743c4SBjoern A. Zeeb 		goto n80211;
5399310743c4SBjoern A. Zeeb 	}
54009fb91463SBjoern A. Zeeb 
5401310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5402310743c4SBjoern A. Zeeb 	params.sta = sta;
5403310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5404310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5405310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5406310743c4SBjoern A. Zeeb 	params.timeout = 0;
5407310743c4SBjoern A. Zeeb 	params.ssn = 0;
5408310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5409310743c4SBjoern A. Zeeb 	params.amsdu = false;
5410310743c4SBjoern A. Zeeb 
5411310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5412cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5413310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5414cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5415310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5416310743c4SBjoern A. Zeeb 	if (error != 0) {
5417310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5418310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5419310743c4SBjoern A. Zeeb 		goto n80211;
5420310743c4SBjoern A. Zeeb 	}
5421310743c4SBjoern A. Zeeb 
5422310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5423310743c4SBjoern A. Zeeb 
5424310743c4SBjoern A. Zeeb n80211:
54259fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
54269fb91463SBjoern A. Zeeb }
54279fb91463SBjoern A. Zeeb 
54289fb91463SBjoern A. Zeeb static void
54299fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
54309fb91463SBjoern A. Zeeb {
54319fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54329fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54339fb91463SBjoern A. Zeeb 
54349fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54359fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54369fb91463SBjoern A. Zeeb 
54379fb91463SBjoern A. Zeeb 	IMPROVE_HT();
54389fb91463SBjoern A. Zeeb 
54399fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
54409fb91463SBjoern A. Zeeb }
54419fb91463SBjoern A. Zeeb 
54429fb91463SBjoern A. Zeeb static void
54439fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
54449fb91463SBjoern A. Zeeb     int status)
54459fb91463SBjoern A. Zeeb {
54469fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54479fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54489fb91463SBjoern A. Zeeb 
54499fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54509fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54519fb91463SBjoern A. Zeeb 
54529fb91463SBjoern A. Zeeb 	IMPROVE_HT();
54539fb91463SBjoern A. Zeeb 
54549fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
54559fb91463SBjoern A. Zeeb }
54569fb91463SBjoern A. Zeeb 
54579fb91463SBjoern A. Zeeb static int
54589fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
54599fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
54609fb91463SBjoern A. Zeeb {
54619fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54629fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54639fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54649fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
54659fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54669fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
54679fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
54689fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5469310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
54709fb91463SBjoern A. Zeeb 	int error;
54719fb91463SBjoern A. Zeeb 
54729fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54739fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54749fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54759fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
54769fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
54779fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
54789fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
54799fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
54809fb91463SBjoern A. Zeeb 
5481310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5482310743c4SBjoern A. Zeeb 
5483310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5484310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5485310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5486310743c4SBjoern A. Zeeb 		return (-ENXIO);
5487310743c4SBjoern A. Zeeb 	}
5488310743c4SBjoern A. Zeeb 
54899fb91463SBjoern A. Zeeb 	params.sta = sta;
54909fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
54919fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
54929fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
54939fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
54949fb91463SBjoern A. Zeeb 	else
54959fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5496310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5497310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
54989fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
54999fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
55009fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
55019fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5502310743c4SBjoern A. Zeeb 
5503310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5504310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5505310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5506310743c4SBjoern A. Zeeb 		params.amsdu = true;
5507310743c4SBjoern A. Zeeb 	else
55089fb91463SBjoern A. Zeeb 		params.amsdu = false;
55099fb91463SBjoern A. Zeeb 
5510cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
55119fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5512cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
55139fb91463SBjoern A. Zeeb 	if (error != 0) {
55149fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
55159fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
55169fb91463SBjoern A. Zeeb 		return (error);
55179fb91463SBjoern A. Zeeb 	}
5518310743c4SBjoern A. Zeeb 
5519310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5520310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5521310743c4SBjoern A. Zeeb 	}
5522310743c4SBjoern A. Zeeb 
55239fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
55249fb91463SBjoern A. Zeeb 
55259fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
55269fb91463SBjoern A. Zeeb 	return (error);
55279fb91463SBjoern A. Zeeb }
55289fb91463SBjoern A. Zeeb 
55299fb91463SBjoern A. Zeeb static void
55309fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
55319fb91463SBjoern A. Zeeb {
55329fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
55339fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55349fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
55359fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
55369fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
55379fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
55389fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
55399fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5540310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
55419fb91463SBjoern A. Zeeb 	int error;
55429fb91463SBjoern A. Zeeb 	uint8_t tid;
554365c573e4SBjoern A. Zeeb 	bool ic_locked;
55449fb91463SBjoern A. Zeeb 
55459fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
55469fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
55479fb91463SBjoern A. Zeeb 
55489fb91463SBjoern A. Zeeb 	/*
55499fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
55509fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
55519fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
55529fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
55539fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
55549fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
55559fb91463SBjoern A. Zeeb 	 * track some state itself.
55569fb91463SBjoern A. Zeeb 	 */
55579fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
55589fb91463SBjoern A. Zeeb 		goto net80211_only;
55599fb91463SBjoern A. Zeeb 
55609fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
55619fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
55629fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
55639fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
55649fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
55659fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
55669fb91463SBjoern A. Zeeb 
55679fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
55689fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
55699fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
55709fb91463SBjoern A. Zeeb 			break;
55719fb91463SBjoern A. Zeeb 	}
55729fb91463SBjoern A. Zeeb 
55739fb91463SBjoern A. Zeeb 	params.sta = sta;
55749fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
55759fb91463SBjoern A. Zeeb 	params.buf_size = 0;
55769fb91463SBjoern A. Zeeb 	params.timeout = 0;
55779fb91463SBjoern A. Zeeb 	params.ssn = 0;
55789fb91463SBjoern A. Zeeb 	params.tid = tid;
55799fb91463SBjoern A. Zeeb 	params.amsdu = false;
55809fb91463SBjoern A. Zeeb 
558165c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
558265c573e4SBjoern A. Zeeb 	if (ic_locked)
558365c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5584cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
55859fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5586cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
558765c573e4SBjoern A. Zeeb 	if (ic_locked)
558865c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
55899fb91463SBjoern A. Zeeb 	if (error != 0)
55909fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
55919fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
55929fb91463SBjoern A. Zeeb 
55939fb91463SBjoern A. Zeeb net80211_only:
55949fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
55959fb91463SBjoern A. Zeeb }
55969fb91463SBjoern A. Zeeb #endif
55979fb91463SBjoern A. Zeeb 
55989fb91463SBjoern A. Zeeb static void
55999fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
56009fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
56019fb91463SBjoern A. Zeeb {
56029fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
56039fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
56049fb91463SBjoern A. Zeeb 
56059fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
56069fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
56079fb91463SBjoern A. Zeeb 		return;
56089fb91463SBjoern A. Zeeb 
56099fb91463SBjoern A. Zeeb 	switch (band) {
56109fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
56119fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
56129fb91463SBjoern A. Zeeb 		break;
56139fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
56149fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
56159fb91463SBjoern A. Zeeb 		break;
56169fb91463SBjoern A. Zeeb 	default:
56179fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
56189fb91463SBjoern A. Zeeb 		return;
56199fb91463SBjoern A. Zeeb 	}
56209fb91463SBjoern A. Zeeb 
56219fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
56229fb91463SBjoern A. Zeeb 
56239fb91463SBjoern A. Zeeb 	/*
56249fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
56259fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
56269fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
56279fb91463SBjoern A. Zeeb 	 */
56289fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
56299fb91463SBjoern A. Zeeb 
56309fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
56319fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
56329fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
56339fb91463SBjoern A. Zeeb #ifdef __notyet__
56349fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
56359fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
56369fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
56379fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
56389fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
56399fb91463SBjoern A. Zeeb #endif
56409fb91463SBjoern A. Zeeb 
56419fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
56429fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
56439fb91463SBjoern A. Zeeb 
56449fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
56459fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
56469fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
56479fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
56489fb91463SBjoern A. Zeeb #endif
56499fb91463SBjoern A. Zeeb }
56509fb91463SBjoern A. Zeeb 
56516b4cac81SBjoern A. Zeeb static void
56526b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
56536b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
56546b4cac81SBjoern A. Zeeb {
56556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
56576b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
56586b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
56596b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
56606b4cac81SBjoern A. Zeeb 
56616b4cac81SBjoern A. Zeeb 	/* Channels */
56626b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
56636b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
56646b4cac81SBjoern A. Zeeb 
56656b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
56666b4cac81SBjoern A. Zeeb 	nchans = 0;
56676b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
56686b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
56696b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
56706b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
56716b4cac81SBjoern A. Zeeb 		chan_flags = 0;
56726b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
56736b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
56749fb91463SBjoern A. Zeeb 
56759fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
56766b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
56779fb91463SBjoern A. Zeeb 
56789fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
56799fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
56806b4cac81SBjoern A. Zeeb 
56816b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5682cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
56836b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
56846b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
56856b4cac81SBjoern A. Zeeb 
56866b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
56873f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
56883f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
56896b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
56906b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
56916b4cac81SBjoern A. Zeeb 				continue;
56926b4cac81SBjoern A. Zeeb 			}
56936b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
56946b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
56956b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
56966b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
56976b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
56986b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
56996b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
57006b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
57016b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
57026b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
57036b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
57046b4cac81SBjoern A. Zeeb 
57056b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
57066b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
57076b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
57085856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5709cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5710cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
57113f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
57123f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
57136b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
57146b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
57156b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5716cee56e77SBjoern A. Zeeb 			if (error != 0)
57176b4cac81SBjoern A. Zeeb 				break;
57186b4cac81SBjoern A. Zeeb 		}
57196b4cac81SBjoern A. Zeeb 	}
57206b4cac81SBjoern A. Zeeb 
57216b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
57226b4cac81SBjoern A. Zeeb 	nchans = 0;
57236b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
57246b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
57256b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
57266b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
57276b4cac81SBjoern A. Zeeb 		chan_flags = 0;
57286b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
57299fb91463SBjoern A. Zeeb 
57309fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
57319fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
57329fb91463SBjoern A. Zeeb 
57339fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
57346b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
57356b4cac81SBjoern A. Zeeb 
57366b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5737562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
57386b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5739ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5740ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
57416b4cac81SBjoern A. Zeeb 
57426b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
57436b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
57446b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5745562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
57466b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
57476b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5748562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
57496b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
57506b4cac81SBjoern A. Zeeb 		}
57516b4cac81SBjoern A. Zeeb #endif
57526b4cac81SBjoern A. Zeeb 
57536b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5754cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
57556b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
57566b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
57576b4cac81SBjoern A. Zeeb 
57586b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
57593f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
57603f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
57616b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
57626b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
57636b4cac81SBjoern A. Zeeb 				continue;
57646b4cac81SBjoern A. Zeeb 			}
57656b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
57666b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
57676b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
57686b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
57696b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
57706b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
57716b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
57726b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
57736b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
57746b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
57756b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
57766b4cac81SBjoern A. Zeeb 
57776b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
57786b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
57796b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
57805856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5781cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5782cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
57833f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
57843f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
57856b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
57866b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
57876b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5788cee56e77SBjoern A. Zeeb 			if (error != 0)
57896b4cac81SBjoern A. Zeeb 				break;
57906b4cac81SBjoern A. Zeeb 		}
57916b4cac81SBjoern A. Zeeb 	}
57926b4cac81SBjoern A. Zeeb }
57936b4cac81SBjoern A. Zeeb 
57946b4cac81SBjoern A. Zeeb static void *
57956b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
57966b4cac81SBjoern A. Zeeb {
57976b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57986b4cac81SBjoern A. Zeeb 
57996b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
58006b4cac81SBjoern A. Zeeb 
58016b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
58026b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
58036b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
58046b4cac81SBjoern A. Zeeb 
58056b4cac81SBjoern A. Zeeb 	return (ic);
58066b4cac81SBjoern A. Zeeb }
58076b4cac81SBjoern A. Zeeb 
58086b4cac81SBjoern A. Zeeb struct ieee80211_hw *
58096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
58106b4cac81SBjoern A. Zeeb {
58116b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
58126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58136b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5814a5ae63edSBjoern A. Zeeb 	int ac;
58156b4cac81SBjoern A. Zeeb 
58166b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
58176b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
58186b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
58196b4cac81SBjoern A. Zeeb 		return (NULL);
58206b4cac81SBjoern A. Zeeb 
58216b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58226b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5823652e22d3SBjoern A. Zeeb 
58248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5825eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
58268891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
58276b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5828a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5829a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5830a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5831a5ae63edSBjoern A. Zeeb 	}
58326b4cac81SBjoern A. Zeeb 
5833a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf */
5834a8a47a41SBjoern A. Zeeb 	INIT_LIST_HEAD(&lhw->lchanctx_list);
5835a8a47a41SBjoern A. Zeeb 
5836759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5837759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5838759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5839832b8e98SBjoern A. Zeeb 	mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
5840759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5841759a996dSBjoern A. Zeeb 
58426b4cac81SBjoern A. Zeeb 	/*
58436b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
58446b4cac81SBjoern A. Zeeb 	 * not initialized.
58456b4cac81SBjoern A. Zeeb 	 */
58466b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
58476b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5848086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
58496b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
58506b4cac81SBjoern A. Zeeb 
58516b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
58526b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
58536b4cac81SBjoern A. Zeeb 
58546b4cac81SBjoern A. Zeeb 	IMPROVE();
58556b4cac81SBjoern A. Zeeb 
58566b4cac81SBjoern A. Zeeb 	return (hw);
58576b4cac81SBjoern A. Zeeb }
58586b4cac81SBjoern A. Zeeb 
58596b4cac81SBjoern A. Zeeb void
58606b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
58616b4cac81SBjoern A. Zeeb {
58626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5863759a996dSBjoern A. Zeeb 	struct mbuf *m;
58646b4cac81SBjoern A. Zeeb 
58656b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58666b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
58676b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
58686b4cac81SBjoern A. Zeeb 
5869759a996dSBjoern A. Zeeb 	/*
5870759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5871759a996dSBjoern A. Zeeb 	 */
5872759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5873759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5874759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5875759a996dSBjoern A. Zeeb 
5876759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5877759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5878759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5879759a996dSBjoern A. Zeeb 
5880759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5881759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5882759a996dSBjoern A. Zeeb 	while (m != NULL) {
5883dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
5884759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5885759a996dSBjoern A. Zeeb 
5886759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5887759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5888759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5889759a996dSBjoern A. Zeeb 
5890759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5891759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5892759a996dSBjoern A. Zeeb 		}
5893dbae3dcfSBjoern A. Zeeb #else
5894dbae3dcfSBjoern A. Zeeb 		if (m->m_pkthdr.PH_loc.ptr != NULL) {
5895dbae3dcfSBjoern A. Zeeb 			struct ieee80211_node *ni;
5896dbae3dcfSBjoern A. Zeeb 
5897dbae3dcfSBjoern A. Zeeb 			ni = m->m_pkthdr.PH_loc.ptr;
5898dbae3dcfSBjoern A. Zeeb 			ieee80211_free_node(ni);
5899dbae3dcfSBjoern A. Zeeb 		}
5900dbae3dcfSBjoern A. Zeeb #endif
5901759a996dSBjoern A. Zeeb 		m_freem(m);
5902759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5903759a996dSBjoern A. Zeeb 	}
5904759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5905759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5906759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5907759a996dSBjoern A. Zeeb 
5908a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf. */
5909a8a47a41SBjoern A. Zeeb 	if (!list_empty_careful(&lhw->lchanctx_list)) {
5910a8a47a41SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx, *next;
5911a8a47a41SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
5912a8a47a41SBjoern A. Zeeb 
5913a8a47a41SBjoern A. Zeeb 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
5914a8a47a41SBjoern A. Zeeb 			if (lchanctx->added_to_drv) {
5915a8a47a41SBjoern A. Zeeb 				/* In reality we should panic? */
5916a8a47a41SBjoern A. Zeeb 				chanctx_conf = &lchanctx->chanctx_conf;
5917a8a47a41SBjoern A. Zeeb 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
5918a8a47a41SBjoern A. Zeeb 			}
5919a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
5920a8a47a41SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
5921a8a47a41SBjoern A. Zeeb 		}
5922a8a47a41SBjoern A. Zeeb 	}
5923a8a47a41SBjoern A. Zeeb 
59246b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5925eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
59268ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5927eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
59286b4cac81SBjoern A. Zeeb 	IMPROVE();
59296b4cac81SBjoern A. Zeeb }
59306b4cac81SBjoern A. Zeeb 
59316b4cac81SBjoern A. Zeeb void
59326b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
59336b4cac81SBjoern A. Zeeb {
59346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59356b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59366b4cac81SBjoern A. Zeeb 
59376b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59386b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59396b4cac81SBjoern A. Zeeb 
59406b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
59416b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
59426b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
59436b4cac81SBjoern A. Zeeb 
59446b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
59456b4cac81SBjoern A. Zeeb }
59466b4cac81SBjoern A. Zeeb 
59476b4cac81SBjoern A. Zeeb struct ieee80211_hw *
59486b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
59496b4cac81SBjoern A. Zeeb {
59506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59516b4cac81SBjoern A. Zeeb 
59526b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59536b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
59546b4cac81SBjoern A. Zeeb }
59556b4cac81SBjoern A. Zeeb 
59566b4cac81SBjoern A. Zeeb static void
59576b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
59586b4cac81SBjoern A. Zeeb {
59596b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59606b4cac81SBjoern A. Zeeb 
59616b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59626b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
59636b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
59646b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
59656b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
59666b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
59676b4cac81SBjoern A. Zeeb }
59686b4cac81SBjoern A. Zeeb 
59692e183d99SBjoern A. Zeeb int
59706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
59716b4cac81SBjoern A. Zeeb {
59726b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5974c5b96b3eSBjoern A. Zeeb 	int band, i;
59756b4cac81SBjoern A. Zeeb 
59766b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59776b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59786b4cac81SBjoern A. Zeeb 
5979652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5980652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5981652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5982652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5983652e22d3SBjoern A. Zeeb 
59846b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
59856b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
59866b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
59876b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
59886b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
59896b4cac81SBjoern A. Zeeb 		/* We take the first one. */
59906b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
59916b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
59926b4cac81SBjoern A. Zeeb 	} else {
59936b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
59946b4cac81SBjoern A. Zeeb 	}
59956b4cac81SBjoern A. Zeeb 
59963d09d310SBjoern A. Zeeb #ifdef __not_yet__
59973d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
59986b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
59993d09d310SBjoern A. Zeeb #endif
60006b4cac81SBjoern A. Zeeb 
60016b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
60026b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
60036b4cac81SBjoern A. Zeeb 
60046b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
60056b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
60066b4cac81SBjoern A. Zeeb 	ic->ic_caps =
60076b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
60086b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
60096b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
60104a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
60116b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
60124a67f1dfSBjoern A. Zeeb #endif
60136b4cac81SBjoern A. Zeeb #if 0
60146b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
60156b4cac81SBjoern A. Zeeb #endif
60166b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
60176b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
60186b4cac81SBjoern A. Zeeb 	    ;
60196b4cac81SBjoern A. Zeeb #if 0
60206b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
60216b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
60226b4cac81SBjoern A. Zeeb #endif
6023cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
6024cc4e78d5SBjoern A. Zeeb 		/*
6025cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
6026cc4e78d5SBjoern A. Zeeb 		 *
6027cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
6028cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
6029cc4e78d5SBjoern A. Zeeb 		 * the flag.
6030cc4e78d5SBjoern A. Zeeb 		 */
60316b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
6032a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
60336b4cac81SBjoern A. Zeeb 	}
60346b4cac81SBjoern A. Zeeb 
603563578bf2SBjoern A. Zeeb 	/* Does HW support Fragmentation offload? */
603663578bf2SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
603763578bf2SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
603863578bf2SBjoern A. Zeeb 
6039527687a9SBjoern A. Zeeb 	/*
6040527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
6041527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
6042527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
6043527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
6044527687a9SBjoern A. Zeeb 	 */
6045527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
6046527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
6047527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
6048527687a9SBjoern A. Zeeb 
6049527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
6050527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
6051527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
6052527687a9SBjoern A. Zeeb 		}
6053527687a9SBjoern A. Zeeb 	}
6054527687a9SBjoern A. Zeeb 
60556b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
6056b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
605711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
6058bf8c25f1SBjoern A. Zeeb 		uint32_t hwciphers;
6059bf8c25f1SBjoern A. Zeeb 
6060bf8c25f1SBjoern A. Zeeb 		hwciphers = 0;
606192942717SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
606292942717SBjoern A. Zeeb 			uint32_t cs;
606392942717SBjoern A. Zeeb 
606492942717SBjoern A. Zeeb 			cs = lkpi_l80211_to_net80211_cyphers(
6065bf8c25f1SBjoern A. Zeeb 			    ic, hw->wiphy->cipher_suites[i]);
606692942717SBjoern A. Zeeb 			if (cs == IEEE80211_CRYPTO_TKIP) {
606792942717SBjoern A. Zeeb 				/*
606892942717SBjoern A. Zeeb 				 * We do set this here.  We will only find out
606992942717SBjoern A. Zeeb 				 * when doing a SET_KEY operation depending on
607092942717SBjoern A. Zeeb 				 * what the driver returns.
607192942717SBjoern A. Zeeb 				 * net80211::ieee80211_crypto_newkey()
607292942717SBjoern A. Zeeb 				 * checks this so we will have to do flags
607392942717SBjoern A. Zeeb 				 * surgery later.
607492942717SBjoern A. Zeeb 				 */
607592942717SBjoern A. Zeeb 				cs |= IEEE80211_CRYPTO_TKIPMIC;
607692942717SBjoern A. Zeeb 			}
607792942717SBjoern A. Zeeb 			hwciphers |= cs;
607892942717SBjoern A. Zeeb 		}
6079bf8c25f1SBjoern A. Zeeb 		/*
6080bf8c25f1SBjoern A. Zeeb 		 * (20250415) nothing anywhere in the path checks we actually
6081bf8c25f1SBjoern A. Zeeb 		 * support all these in net80211.
6082bf8c25f1SBjoern A. Zeeb 		 * net80211 supports _256 variants but the ioctl does not.
6083bf8c25f1SBjoern A. Zeeb 		 */
6084bf8c25f1SBjoern A. Zeeb 		IMPROVE("as net80211 grows more support, enable them");
608592942717SBjoern A. Zeeb 		hwciphers &= (IEEE80211_CRYPTO_WEP |
608692942717SBjoern A. Zeeb 		    IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
6087bf8c25f1SBjoern A. Zeeb 		    IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
6088bf8c25f1SBjoern A. Zeeb 		/* We only support CCMP here, so further filter. */
6089bf8c25f1SBjoern A. Zeeb 		hwciphers &= IEEE80211_CRYPTO_AES_CCM;
6090bf8c25f1SBjoern A. Zeeb 		ieee80211_set_hardware_ciphers(ic, hwciphers);
60916b4cac81SBjoern A. Zeeb 	}
60926b4cac81SBjoern A. Zeeb #endif
60936b4cac81SBjoern A. Zeeb 
60946b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
60956b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
60966b4cac81SBjoern A. Zeeb 
60976b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
60986b4cac81SBjoern A. Zeeb 
60996b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
61006b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
61016b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
61026b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
61036b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
61046b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
61056b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
61066b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
61076b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
61086b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
61096b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
61106b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
61116b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
61126b4cac81SBjoern A. Zeeb 
6113a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
6114a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6115a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6116a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6117a486fbbdSBjoern A. Zeeb 
61186b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
61196b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
61206b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
61216b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
61226b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
61236b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
61246b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
61256b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
61266b4cac81SBjoern A. Zeeb 
61279fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
612886bc7259SBjoern A. Zeeb 	/*
612986bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
613086bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
613186bc7259SBjoern A. Zeeb 	 */
613286bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
61339fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
61349fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
61359fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
61369fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
61379fb91463SBjoern A. Zeeb 
61389fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
61399fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
61409fb91463SBjoern A. Zeeb 
61419fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
61429fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
61439fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
61449fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
61459fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
61469fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
61479fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
61489fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
61499fb91463SBjoern A. Zeeb 
61509fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
61519fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
61529fb91463SBjoern A. Zeeb 
61539fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
61549fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
61559fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
61569fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
615786bc7259SBjoern A. Zeeb 	}
61589fb91463SBjoern A. Zeeb #endif
61599fb91463SBjoern A. Zeeb 
61606b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
61616b4cac81SBjoern A. Zeeb 
6162c5b96b3eSBjoern A. Zeeb 	/*
6163c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
6164c5b96b3eSBjoern A. Zeeb 	 * expect one.
6165d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
6166d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6167c5b96b3eSBjoern A. Zeeb 	 */
6168d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
6169f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6170c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6171c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6172c5b96b3eSBjoern A. Zeeb 
6173c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
6174c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6175c5b96b3eSBjoern A. Zeeb 			continue;
6176c5b96b3eSBjoern A. Zeeb 
6177d9945d78SBjoern A. Zeeb 		lhw->supbands++;
6178d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6179d9945d78SBjoern A. Zeeb 
6180f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
6181f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
6182f454a4a1SBjoern A. Zeeb 			continue;
6183f454a4a1SBjoern A. Zeeb 
6184c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
6185c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6186c5b96b3eSBjoern A. Zeeb 
6187c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6188c5b96b3eSBjoern A. Zeeb 				continue;
6189c5b96b3eSBjoern A. Zeeb 
61904a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
61919fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
619211450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
61939fb91463SBjoern A. Zeeb #endif
6194c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
6195c5b96b3eSBjoern A. Zeeb 			break;
6196c5b96b3eSBjoern A. Zeeb 		}
6197c5b96b3eSBjoern A. Zeeb 	}
6198c5b96b3eSBjoern A. Zeeb 
6199d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6200d9945d78SBjoern A. Zeeb 
6201d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
6202d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6203d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6204d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6205d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6206d9945d78SBjoern A. Zeeb 	}
6207d9945d78SBjoern A. Zeeb 
6208d9945d78SBjoern A. Zeeb 	/*
6209d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
6210d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
6211d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
6212d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
6213d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
6214d9945d78SBjoern A. Zeeb 	 */
6215d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6216d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
6217d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
621878ca45dfSBjoern A. Zeeb 
621978ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6220d9945d78SBjoern A. Zeeb 		/*
622178ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
622278ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
622378ca45dfSBjoern A. Zeeb 		 * space in.
6224d9945d78SBjoern A. Zeeb 		 */
6225d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
622678ca45dfSBjoern A. Zeeb 	}
6227d9945d78SBjoern A. Zeeb 
62289fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
62299fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
62309fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
62319fb91463SBjoern A. Zeeb #endif
62329fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
62331f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
62349fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
62359fb91463SBjoern A. Zeeb #endif
62369fb91463SBjoern A. Zeeb 
6237d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
623878ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
623978ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
624078ca45dfSBjoern A. Zeeb 			goto err;
6241d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
624278ca45dfSBjoern A. Zeeb 	}
6243d9945d78SBjoern A. Zeeb 
62446b4cac81SBjoern A. Zeeb 	if (bootverbose)
62456b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
62462e183d99SBjoern A. Zeeb 
62472e183d99SBjoern A. Zeeb 	return (0);
624878ca45dfSBjoern A. Zeeb err:
624978ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
625078ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
62516b4cac81SBjoern A. Zeeb }
62526b4cac81SBjoern A. Zeeb 
62536b4cac81SBjoern A. Zeeb void
62546b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
62556b4cac81SBjoern A. Zeeb {
62566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
62586b4cac81SBjoern A. Zeeb 
62596b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62606b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
62616b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
62626b4cac81SBjoern A. Zeeb }
62636b4cac81SBjoern A. Zeeb 
62646b4cac81SBjoern A. Zeeb void
62656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
62666b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
62676b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
62686b4cac81SBjoern A. Zeeb     void *arg)
62696b4cac81SBjoern A. Zeeb {
62706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
627361a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
62746b4cac81SBjoern A. Zeeb 
62756b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62766b4cac81SBjoern A. Zeeb 
62776b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
62786b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
627961a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6280adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
62816b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
62826b4cac81SBjoern A. Zeeb 		    __func__, flags);
62836b4cac81SBjoern A. Zeeb 	}
62846b4cac81SBjoern A. Zeeb 
6285adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
62866b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
628761a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
62886b4cac81SBjoern A. Zeeb 
62896b4cac81SBjoern A. Zeeb 	if (atomic)
62908891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
62916b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
62926b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
62936b4cac81SBjoern A. Zeeb 
62946b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
62956b4cac81SBjoern A. Zeeb 
62966b4cac81SBjoern A. Zeeb 		/*
62976b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
62986b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
62996b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
63006b4cac81SBjoern A. Zeeb 		 * driver does not know about.
63016b4cac81SBjoern A. Zeeb 		 */
63026b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
63036b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
63046b4cac81SBjoern A. Zeeb 			continue;
63056b4cac81SBjoern A. Zeeb 
63066b4cac81SBjoern A. Zeeb 		/*
630761a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
630861a68e50SBjoern A. Zeeb 		 * if we haven't yet.
630961a68e50SBjoern A. Zeeb 		 */
631061a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
631161a68e50SBjoern A. Zeeb 			continue;
631261a68e50SBjoern A. Zeeb 
631361a68e50SBjoern A. Zeeb 		/*
63146b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
63156b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
63166b4cac81SBjoern A. Zeeb 		 */
63176b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
63186b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
63196b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
63206b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
63216b4cac81SBjoern A. Zeeb 	}
63226b4cac81SBjoern A. Zeeb 	if (atomic)
63238891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
63246b4cac81SBjoern A. Zeeb }
63256b4cac81SBjoern A. Zeeb 
632611db70b6SBjoern A. Zeeb static void
632711db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
632811db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
632911db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
633011db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
633111db70b6SBjoern A. Zeeb     void *arg)
633211db70b6SBjoern A. Zeeb {
633311db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
633411db70b6SBjoern A. Zeeb 		return;
633511db70b6SBjoern A. Zeeb 
633611db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
633711db70b6SBjoern A. Zeeb 		return;
633811db70b6SBjoern A. Zeeb 
633911db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
634011db70b6SBjoern A. Zeeb }
634111db70b6SBjoern A. Zeeb 
63426b4cac81SBjoern A. Zeeb void
63436b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
63446b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
63456b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
63466b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
634711db70b6SBjoern A. Zeeb     void *arg, bool rcu)
63486b4cac81SBjoern A. Zeeb {
634911db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
635011db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
63516b4cac81SBjoern A. Zeeb 
635211db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
635311db70b6SBjoern A. Zeeb 
635411db70b6SBjoern A. Zeeb 	if (rcu) {
6355a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6356a8f735a6SBjoern A. Zeeb 
635711db70b6SBjoern A. Zeeb 		if (vif == NULL) {
635811db70b6SBjoern A. Zeeb 			TODO();
635911db70b6SBjoern A. Zeeb 		} else {
6360a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
636111db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
636211db70b6SBjoern A. Zeeb 				    keyix++)
636311db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
636411db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
636511db70b6SBjoern A. Zeeb 			}
636611db70b6SBjoern A. Zeeb 		}
636711db70b6SBjoern A. Zeeb 	} else {
636811db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
636911db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
637011db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
637111db70b6SBjoern A. Zeeb 
637211db70b6SBjoern A. Zeeb 		if (vif == NULL) {
637311db70b6SBjoern A. Zeeb 			TODO();
637411db70b6SBjoern A. Zeeb 		} else {
6375a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
637611db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
637711db70b6SBjoern A. Zeeb 				    keyix++)
637811db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
637911db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
638011db70b6SBjoern A. Zeeb 			}
638111db70b6SBjoern A. Zeeb 		}
638211db70b6SBjoern A. Zeeb 	}
63836b4cac81SBjoern A. Zeeb }
63846b4cac81SBjoern A. Zeeb 
63856b4cac81SBjoern A. Zeeb void
63866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
63876b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
63886b4cac81SBjoern A. Zeeb 	void *),
63896b4cac81SBjoern A. Zeeb     void *arg)
63906b4cac81SBjoern A. Zeeb {
6391c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6392c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
63936b4cac81SBjoern A. Zeeb 
6394c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
6395c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6396c5e25798SBjoern A. Zeeb 
6397c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6398c5e25798SBjoern A. Zeeb 
6399a8a47a41SBjoern A. Zeeb 	rcu_read_lock();
6400a8a47a41SBjoern A. Zeeb 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6401c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
6402c5e25798SBjoern A. Zeeb 			continue;
6403d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6404c5e25798SBjoern A. Zeeb 	}
6405a8a47a41SBjoern A. Zeeb 	rcu_read_unlock();
64066b4cac81SBjoern A. Zeeb }
64076b4cac81SBjoern A. Zeeb 
64086b4cac81SBjoern A. Zeeb void
64096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
64106b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
64116b4cac81SBjoern A. Zeeb {
64126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
64146b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
64156b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
64166b4cac81SBjoern A. Zeeb 
64176b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
64186b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
64196b4cac81SBjoern A. Zeeb 
64206b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
64216b4cac81SBjoern A. Zeeb 
64228891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
64236b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
64246b4cac81SBjoern A. Zeeb 
6425a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6426a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
64276b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
64286b4cac81SBjoern A. Zeeb 				continue;
64296b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
64306b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
64316b4cac81SBjoern A. Zeeb 		}
6432a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
64336b4cac81SBjoern A. Zeeb 	}
64348891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
64356b4cac81SBjoern A. Zeeb }
64366b4cac81SBjoern A. Zeeb 
6437673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6438673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6439673d62fcSBjoern A. Zeeb {
6440673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6441673d62fcSBjoern A. Zeeb 
6442673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6443673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6444673d62fcSBjoern A. Zeeb 	return (regd);
6445673d62fcSBjoern A. Zeeb }
6446673d62fcSBjoern A. Zeeb 
64476b4cac81SBjoern A. Zeeb int
64486b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
64496b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
64506b4cac81SBjoern A. Zeeb {
64516b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64526b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
64536b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
64546b4cac81SBjoern A. Zeeb 
64556b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
64566b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
64576b4cac81SBjoern A. Zeeb 
64586b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
64596b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
64606b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
64616b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
64626b4cac81SBjoern A. Zeeb 	}
64636b4cac81SBjoern A. Zeeb 
64646b4cac81SBjoern A. Zeeb 	TODO();
64656b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
64666b4cac81SBjoern A. Zeeb 
64676b4cac81SBjoern A. Zeeb 	return (0);
64686b4cac81SBjoern A. Zeeb }
64696b4cac81SBjoern A. Zeeb 
64706b4cac81SBjoern A. Zeeb void
64716b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
64726b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
64736b4cac81SBjoern A. Zeeb {
64746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
64756b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
64766b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
64776b4cac81SBjoern A. Zeeb 
64786b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
64796b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
64806b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
64816b4cac81SBjoern A. Zeeb 
64826b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
64836b4cac81SBjoern A. Zeeb 
64848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
64856b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
64866b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6487a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
64886b4cac81SBjoern A. Zeeb 	wakeup(lhw);
64898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
64906b4cac81SBjoern A. Zeeb 
64916b4cac81SBjoern A. Zeeb 	return;
64926b4cac81SBjoern A. Zeeb }
64936b4cac81SBjoern A. Zeeb 
6494759a996dSBjoern A. Zeeb static void
6495759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6496759a996dSBjoern A. Zeeb {
6497759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6498dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6499759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6500dbae3dcfSBjoern A. Zeeb #endif
6501759a996dSBjoern A. Zeeb 	int ok;
6502759a996dSBjoern A. Zeeb 
6503759a996dSBjoern A. Zeeb 	ni = NULL;
6504dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6505759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6506759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6507759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6508759a996dSBjoern A. Zeeb 
6509759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6510759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6511759a996dSBjoern A. Zeeb 	}
6512dbae3dcfSBjoern A. Zeeb #else
6513dbae3dcfSBjoern A. Zeeb 	if (m->m_pkthdr.PH_loc.ptr != NULL) {
6514dbae3dcfSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6515dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = NULL;
6516dbae3dcfSBjoern A. Zeeb 	}
6517dbae3dcfSBjoern A. Zeeb #endif
6518759a996dSBjoern A. Zeeb 
6519759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6520759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6521759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6522759a996dSBjoern A. Zeeb 		if (ok < 0)
6523759a996dSBjoern A. Zeeb 			m_freem(m);
6524759a996dSBjoern A. Zeeb 	} else {
6525759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6526759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6527759a996dSBjoern A. Zeeb 	}
6528759a996dSBjoern A. Zeeb 
6529759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6530759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6531bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6532759a996dSBjoern A. Zeeb #endif
6533759a996dSBjoern A. Zeeb }
6534759a996dSBjoern A. Zeeb 
6535759a996dSBjoern A. Zeeb static void
6536759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6537759a996dSBjoern A. Zeeb {
6538759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6539759a996dSBjoern A. Zeeb 	struct mbufq mq;
6540759a996dSBjoern A. Zeeb 	struct mbuf *m;
6541759a996dSBjoern A. Zeeb 
6542759a996dSBjoern A. Zeeb 	lhw = ctx;
6543759a996dSBjoern A. Zeeb 
6544759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6545759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6546bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6547bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6548759a996dSBjoern A. Zeeb #endif
6549759a996dSBjoern A. Zeeb 
6550759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6551759a996dSBjoern A. Zeeb 
6552759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6553759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6554759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6555759a996dSBjoern A. Zeeb 
6556759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6557759a996dSBjoern A. Zeeb 	while (m != NULL) {
6558759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6559759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6560759a996dSBjoern A. Zeeb 	}
6561759a996dSBjoern A. Zeeb }
6562759a996dSBjoern A. Zeeb 
656349010ba7SBjoern A. Zeeb static void
6564a1adefb1SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
656549010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
656649010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
656749010ba7SBjoern A. Zeeb     uint8_t *rssip)
656849010ba7SBjoern A. Zeeb {
656949010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
6570a1adefb1SBjoern A. Zeeb 	struct rate_info rxrate;
657149010ba7SBjoern A. Zeeb 	int i;
657249010ba7SBjoern A. Zeeb 	uint8_t rssi;
657349010ba7SBjoern A. Zeeb 
6574a1adefb1SBjoern A. Zeeb 	memset(&rxrate, 0, sizeof(rxrate));
657549010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
657649010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
657749010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
657849010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
657949010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
658049010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
658149010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
658249010ba7SBjoern A. Zeeb 	else
658349010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
658449010ba7SBjoern A. Zeeb 	/*
658549010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
658649010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
658749010ba7SBjoern A. Zeeb 	 */
658849010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
658949010ba7SBjoern A. Zeeb 	if (rssip != NULL)
659049010ba7SBjoern A. Zeeb 		*rssip = rssi;
659149010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
659249010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
659349010ba7SBjoern A. Zeeb 	rx_stats->c_band =
659449010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
659549010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
659649010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
659749010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
659849010ba7SBjoern A. Zeeb 
659949010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
660049010ba7SBjoern A. Zeeb 
660149010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
660249010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
660349010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
660449010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
660549010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
660649010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
660749010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
660849010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
660949010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
661049010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
661149010ba7SBjoern A. Zeeb 	}
661249010ba7SBjoern A. Zeeb 
661349010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
661449010ba7SBjoern A. Zeeb 		int cc;
661549010ba7SBjoern A. Zeeb 		int8_t crssi;
661649010ba7SBjoern A. Zeeb 
661749010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
661849010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
661949010ba7SBjoern A. Zeeb 
662049010ba7SBjoern A. Zeeb 		cc = 0;
662149010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
662249010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
662349010ba7SBjoern A. Zeeb 				continue;
662449010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
662549010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
662649010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
662749010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
662849010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
662949010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
663049010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
663149010ba7SBjoern A. Zeeb 			cc++;
663249010ba7SBjoern A. Zeeb 		}
663349010ba7SBjoern A. Zeeb 		if (cc > 0)
663449010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
663549010ba7SBjoern A. Zeeb 	}
663649010ba7SBjoern A. Zeeb 
663749010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
663849010ba7SBjoern A. Zeeb 
663949010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
664049010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
6641a1adefb1SBjoern A. Zeeb 	{
6642a1adefb1SBjoern A. Zeeb 		uint32_t legacy = 0;
6643a1adefb1SBjoern A. Zeeb 
664449010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
664549010ba7SBjoern A. Zeeb 		if (supband != NULL)
6646a1adefb1SBjoern A. Zeeb 			legacy = supband->bitrates[rx_status->rate_idx].bitrate;
6647a1adefb1SBjoern A. Zeeb 		rx_stats->c_rate = legacy;
6648a1adefb1SBjoern A. Zeeb 		rxrate.legacy = legacy;
664949010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
665049010ba7SBjoern A. Zeeb 		break;
6651a1adefb1SBjoern A. Zeeb 	}
665249010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
665349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
665449010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6655a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_MCS;
6656a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6657a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6658a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6659a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6660a1adefb1SBjoern A. Zeeb 		}
666149010ba7SBjoern A. Zeeb 		break;
666249010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
666349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
666449010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
666549010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
6666a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
6667a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6668a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6669a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6670a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6671a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6672a1adefb1SBjoern A. Zeeb 		}
667349010ba7SBjoern A. Zeeb 		break;
667449010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
6675a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
6676a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6677a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6678a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
6679a1adefb1SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6680a1adefb1SBjoern A. Zeeb 		break;
668149010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
6682a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
6683a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6684a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6685a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
668649010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
668749010ba7SBjoern A. Zeeb 		break;
668849010ba7SBjoern A. Zeeb 	}
668949010ba7SBjoern A. Zeeb 
6690a1adefb1SBjoern A. Zeeb 	rxrate.bw = rx_status->bw;
669149010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
669249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
669349010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
669449010ba7SBjoern A. Zeeb 		break;
669549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
669649010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
669749010ba7SBjoern A. Zeeb 		break;
669849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
669949010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
670049010ba7SBjoern A. Zeeb 		break;
670149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
670249010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
670349010ba7SBjoern A. Zeeb 		break;
670449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
670549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
670649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
670749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
670849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
670949010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
671049010ba7SBjoern A. Zeeb 		break;
671149010ba7SBjoern A. Zeeb 	}
671249010ba7SBjoern A. Zeeb 
671349010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
671449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
671549010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
671649010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
671749010ba7SBjoern A. Zeeb 
671849010ba7SBjoern A. Zeeb 	/*
671949010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
672049010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
672149010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
672249010ba7SBjoern A. Zeeb 	 */
672349010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
672449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
672549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
672649010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
672749010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
672849010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
672949010ba7SBjoern A. Zeeb 	}
6730731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
6731731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
6732731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
6733731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
6734731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6735731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
673649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
673749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6738394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6739394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
674049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
674149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
674249010ba7SBjoern A. Zeeb #endif
6743a1adefb1SBjoern A. Zeeb 
6744a1adefb1SBjoern A. Zeeb 	if (lsta != NULL) {
6745a1adefb1SBjoern A. Zeeb 		memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
6746a1adefb1SBjoern A. Zeeb 		lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
6747a1adefb1SBjoern A. Zeeb 	}
674849010ba7SBjoern A. Zeeb }
674949010ba7SBjoern A. Zeeb 
6750e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
67516b4cac81SBjoern A. Zeeb void
67526b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6753e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6754e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
67556b4cac81SBjoern A. Zeeb {
67566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
67586b4cac81SBjoern A. Zeeb 	struct mbuf *m;
67596b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
67606b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
67616b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
67626b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
67636b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
67646b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
67656b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
6766c816f64eSBjoern A. Zeeb 	int i, offset, ok, error;
676749010ba7SBjoern A. Zeeb 	uint8_t rssi;
6768c0cadd99SBjoern A. Zeeb 	bool is_beacon;
67696b4cac81SBjoern A. Zeeb 
6770c013f810SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6771c013f810SBjoern A. Zeeb 	ic = lhw->ic;
6772c013f810SBjoern A. Zeeb 
67736b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
67746b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
6775c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
67766b4cac81SBjoern A. Zeeb 		IMPROVE();
67776b4cac81SBjoern A. Zeeb 		goto err;
67786b4cac81SBjoern A. Zeeb 	}
67796b4cac81SBjoern A. Zeeb 
67806b4cac81SBjoern A. Zeeb 	/*
67816b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
67826b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
67836b4cac81SBjoern A. Zeeb 	 */
67846b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
6785c013f810SBjoern A. Zeeb 	if (m == NULL) {
6786c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
67876b4cac81SBjoern A. Zeeb 		goto err;
6788c013f810SBjoern A. Zeeb 	}
67896b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
67906b4cac81SBjoern A. Zeeb 
67916b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
67926b4cac81SBjoern A. Zeeb 	offset = m->m_len;
67936b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
67946b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
67956b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
67966b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
67976b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
67986b4cac81SBjoern A. Zeeb 	}
67996b4cac81SBjoern A. Zeeb 
68006b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
68016b4cac81SBjoern A. Zeeb 
68026b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6803c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6804c0cadd99SBjoern A. Zeeb 
6805c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
68069d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
68076b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
68086b4cac81SBjoern A. Zeeb 
68099d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
681073e3969fSBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
6811c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
681273e3969fSBjoern A. Zeeb 		    __func__, skb, skb->len, skb->data_len,
68136b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
68146b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6815c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
68166b4cac81SBjoern A. Zeeb 
68179d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
68186b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
68196b4cac81SBjoern A. Zeeb 
68206b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
68219d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6822f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
682360970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
68246b4cac81SBjoern A. Zeeb 			__func__,
6825c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6826c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
68276b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6828f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
68296b4cac81SBjoern A. Zeeb 			rx_status->freq,
68306b4cac81SBjoern A. Zeeb 			rx_status->bw,
68316b4cac81SBjoern A. Zeeb 			rx_status->encoding,
68326b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
68336b4cac81SBjoern A. Zeeb 			rx_status->band,
68346b4cac81SBjoern A. Zeeb 			rx_status->chains,
68356b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
68366b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
68376b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
683860970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
68396b4cac81SBjoern A. Zeeb 			rx_status->signal,
68406b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
68416b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
68426b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
68436b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
68446b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
68456b4cac81SBjoern A. Zeeb 			rx_status->nss,
68466b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
68476b4cac81SBjoern A. Zeeb no_trace_beacons:
68486b4cac81SBjoern A. Zeeb #endif
68496b4cac81SBjoern A. Zeeb 
685058246901SBjoern A. Zeeb 	lsta = NULL;
68516b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
68526b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
68536b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
68546b4cac81SBjoern A. Zeeb 	} else {
6855c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6856c8dafefaSBjoern A. Zeeb 
68576b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
68586b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
68596b4cac81SBjoern A. Zeeb 		if (ni != NULL)
68606b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
68616b4cac81SBjoern A. Zeeb 	}
68626b4cac81SBjoern A. Zeeb 
6863a1adefb1SBjoern A. Zeeb 	rssi = 0;
6864a1adefb1SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
6865a1adefb1SBjoern A. Zeeb 
6866a1adefb1SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
6867a1adefb1SBjoern A. Zeeb 	if (ok == 0) {
6868a1adefb1SBjoern A. Zeeb 		m_freem(m);
6869a1adefb1SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6870a1adefb1SBjoern A. Zeeb 		goto err;
6871a1adefb1SBjoern A. Zeeb 	}
6872a1adefb1SBjoern A. Zeeb 
68736b4cac81SBjoern A. Zeeb 	if (ni != NULL)
68746b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
68756b4cac81SBjoern A. Zeeb 	else
68766b4cac81SBjoern A. Zeeb 		/*
68776b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
68786b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
68796b4cac81SBjoern A. Zeeb 		 */
68806b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
68816b4cac81SBjoern A. Zeeb 
68829d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
68839d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6884bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6885c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6886c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
68879d9ba2b7SBjoern A. Zeeb #endif
68886b4cac81SBjoern A. Zeeb 
6889c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6890c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6891c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6892c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6893c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6894c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6895c8dafefaSBjoern A. Zeeb 
6896c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6897c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6898c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6899c8dafefaSBjoern A. Zeeb 
6900c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6901c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6902c8dafefaSBjoern A. Zeeb 
6903c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6904c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6905c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6906c8dafefaSBjoern A. Zeeb 		/*
6907c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6908c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6909c8dafefaSBjoern A. Zeeb 		 */
6910c8dafefaSBjoern A. Zeeb skip_device_ts:
69119fb91463SBjoern A. Zeeb 		;
69129fb91463SBjoern A. Zeeb 	}
6913c8dafefaSBjoern A. Zeeb 
69146b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
69156b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
69166b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
69176b4cac81SBjoern A. Zeeb 
69186b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
69196b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
69206b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
69216b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
69226b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
69236b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
69246b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
69256b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
69266b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
69276b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
69286b4cac81SBjoern A. Zeeb #endif
69296b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
69306b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
69316b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
69326b4cac81SBjoern A. Zeeb 		IMPROVE();
69336b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
69346b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
69356b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
69366b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6937170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
69386b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
69396b4cac81SBjoern A. Zeeb 	}
69406b4cac81SBjoern A. Zeeb 
69416b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
69426b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
69436b4cac81SBjoern A. Zeeb 
6944e30e05d3SBjoern A. Zeeb #if 0
6945e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6946e30e05d3SBjoern A. Zeeb 		/*
6947e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6948e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6949e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6950e30e05d3SBjoern A. Zeeb 		*/
6951e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6952e30e05d3SBjoern A. Zeeb 	}
6953e30e05d3SBjoern A. Zeeb #endif
6954e30e05d3SBjoern A. Zeeb 
6955c013f810SBjoern A. Zeeb 	/* Attach meta-information to the mbuf for the deferred RX path. */
69566b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6957dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6958759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6959759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6960759a996dSBjoern A. Zeeb 
6961759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6962759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6963c013f810SBjoern A. Zeeb 		if (mtag == NULL) {
6964c013f810SBjoern A. Zeeb 			m_freem(m);
6965c013f810SBjoern A. Zeeb 			counter_u64_add(ic->ic_ierrors, 1);
6966c013f810SBjoern A. Zeeb 			goto err;
6967c013f810SBjoern A. Zeeb 		}
6968759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6969759a996dSBjoern A. Zeeb 		rxni->ni = ni;		/* We hold a reference. */
6970759a996dSBjoern A. Zeeb 		m_tag_prepend(m, mtag);
6971dbae3dcfSBjoern A. Zeeb #else
6972dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = ni;	/* We hold a reference. */
6973dbae3dcfSBjoern A. Zeeb #endif
6974759a996dSBjoern A. Zeeb 	}
69756b4cac81SBjoern A. Zeeb 
6976759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6977759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6978759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6979759a996dSBjoern A. Zeeb 		m_freem(m);
6980c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6981759a996dSBjoern A. Zeeb 		goto err;
6982759a996dSBjoern A. Zeeb 	}
6983759a996dSBjoern A. Zeeb 
6984c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lhw->rxq, m);
6985c816f64eSBjoern A. Zeeb 	if (error != 0) {
6986c816f64eSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6987c816f64eSBjoern A. Zeeb 		m_freem(m);
6988c816f64eSBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6989c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6990c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6991c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
6992c816f64eSBjoern A. Zeeb 			    __func__, error);
6993c816f64eSBjoern A. Zeeb #endif
6994c816f64eSBjoern A. Zeeb 		goto err;
6995c816f64eSBjoern A. Zeeb 	}
6996759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6997759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
69986b4cac81SBjoern A. Zeeb 
69996b4cac81SBjoern A. Zeeb 	IMPROVE();
70006b4cac81SBjoern A. Zeeb 
70016b4cac81SBjoern A. Zeeb err:
70026b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
70036b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
70046b4cac81SBjoern A. Zeeb }
70056b4cac81SBjoern A. Zeeb 
70066b4cac81SBjoern A. Zeeb uint8_t
7007ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
70086b4cac81SBjoern A. Zeeb {
70096b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
7010ec190d91SBjoern A. Zeeb 	uint8_t tid;
7011ec190d91SBjoern A. Zeeb 
7012ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
7013ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
7014ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
7015ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
70166b4cac81SBjoern A. Zeeb 
70176b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
7018ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
7019ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
7020ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
7021ec190d91SBjoern A. Zeeb 
7022ec190d91SBjoern A. Zeeb 	return (tid);
70236b4cac81SBjoern A. Zeeb }
70246b4cac81SBjoern A. Zeeb 
7025ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7026ac1d519cSBjoern A. Zeeb 
7027ac1d519cSBjoern A. Zeeb static void
7028ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
7029ac1d519cSBjoern A. Zeeb {
7030ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7031ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
7032ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
7033ac1d519cSBjoern A. Zeeb 
7034ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
7035ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7036ac1d519cSBjoern A. Zeeb 
7037ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
7038ac1d519cSBjoern A. Zeeb 
7039ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7040ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
7041ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
7042ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
7043ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7044ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
7045ac1d519cSBjoern A. Zeeb 		return;
7046ac1d519cSBjoern A. Zeeb 	}
7047ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
7048ac1d519cSBjoern A. Zeeb 
7049ac1d519cSBjoern A. Zeeb 	/* More work to do? */
7050ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
7051ac1d519cSBjoern A. Zeeb 		schedule_work(work);
7052ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7053ac1d519cSBjoern A. Zeeb 
7054ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
7055ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
7056ac1d519cSBjoern A. Zeeb 
7057ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
7058ac1d519cSBjoern A. Zeeb }
7059ac1d519cSBjoern A. Zeeb 
7060ac1d519cSBjoern A. Zeeb void
7061ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
7062ac1d519cSBjoern A. Zeeb {
7063ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7064ac1d519cSBjoern A. Zeeb 
7065ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7066ac1d519cSBjoern A. Zeeb 
7067ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7068ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
7069ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
7070ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
7071ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7072ac1d519cSBjoern A. Zeeb 
7073ac1d519cSBjoern A. Zeeb 	/*
7074ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
7075ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
7076ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
7077ac1d519cSBjoern A. Zeeb 	 */
7078ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
7079ac1d519cSBjoern A. Zeeb }
7080ac1d519cSBjoern A. Zeeb 
7081ac1d519cSBjoern A. Zeeb void
7082ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
7083ac1d519cSBjoern A. Zeeb {
7084ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7085ac1d519cSBjoern A. Zeeb 
7086ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7087ac1d519cSBjoern A. Zeeb 
7088ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7089ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
7090ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
7091ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
7092ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7093ac1d519cSBjoern A. Zeeb }
7094ac1d519cSBjoern A. Zeeb 
7095ac1d519cSBjoern A. Zeeb void
7096ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
7097ac1d519cSBjoern A. Zeeb {
7098ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7099ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
7100ac1d519cSBjoern A. Zeeb 
7101ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7102ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7103ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
7104ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
7105ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7106ac1d519cSBjoern A. Zeeb 		return;
7107ac1d519cSBjoern A. Zeeb 	}
7108ac1d519cSBjoern A. Zeeb 
7109ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
7110ac1d519cSBjoern A. Zeeb 
7111ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7112ac1d519cSBjoern A. Zeeb 		    entry);
7113ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
7114ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7115ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
7116ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7117ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
7118ac1d519cSBjoern A. Zeeb 			break;
7119ac1d519cSBjoern A. Zeeb 	}
7120ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7121ac1d519cSBjoern A. Zeeb }
7122ac1d519cSBjoern A. Zeeb 
7123ac1d519cSBjoern A. Zeeb void
7124ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7125ac1d519cSBjoern A. Zeeb {
7126ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
7127ac1d519cSBjoern A. Zeeb 
7128ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
7129ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7130ac1d519cSBjoern A. Zeeb }
7131ac1d519cSBjoern A. Zeeb 
7132ac1d519cSBjoern A. Zeeb void
7133ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7134ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
7135ac1d519cSBjoern A. Zeeb {
7136ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
7137ac1d519cSBjoern A. Zeeb 		/* Run right away. */
7138ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
7139ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
7140ac1d519cSBjoern A. Zeeb 	} else {
7141ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
7142ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
7143ac1d519cSBjoern A. Zeeb 	}
7144ac1d519cSBjoern A. Zeeb }
7145ac1d519cSBjoern A. Zeeb 
7146ac1d519cSBjoern A. Zeeb void
7147ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7148ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
7149ac1d519cSBjoern A. Zeeb {
7150ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
7151ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
7152ac1d519cSBjoern A. Zeeb }
7153ac1d519cSBjoern A. Zeeb 
7154ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7155ac1d519cSBjoern A. Zeeb 
71566b4cac81SBjoern A. Zeeb struct wiphy *
71576b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
71586b4cac81SBjoern A. Zeeb {
71596b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7160ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
71616b4cac81SBjoern A. Zeeb 
71626b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
71636b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
71646b4cac81SBjoern A. Zeeb 		return (NULL);
71656b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
71666b4cac81SBjoern A. Zeeb 
7167ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7168ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
7169ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7170ac1d519cSBjoern A. Zeeb 
7171ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7172ac1d519cSBjoern A. Zeeb 
7173ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
7174ac1d519cSBjoern A. Zeeb 	TODO();
7175ac1d519cSBjoern A. Zeeb 
7176ac1d519cSBjoern A. Zeeb 	return (wiphy);
71776b4cac81SBjoern A. Zeeb }
71786b4cac81SBjoern A. Zeeb 
71796b4cac81SBjoern A. Zeeb void
71806b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
71816b4cac81SBjoern A. Zeeb {
71826b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
71836b4cac81SBjoern A. Zeeb 
71846b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
71856b4cac81SBjoern A. Zeeb 		return;
71866b4cac81SBjoern A. Zeeb 
7187ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
7188ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
7189ac1d519cSBjoern A. Zeeb 
71906b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7191ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7192ac1d519cSBjoern A. Zeeb 
71936b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
71946b4cac81SBjoern A. Zeeb }
71956b4cac81SBjoern A. Zeeb 
7196a7c19b8aSBjoern A. Zeeb static uint32_t
7197a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7198a7c19b8aSBjoern A. Zeeb {
7199a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
7200a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7201a7c19b8aSBjoern A. Zeeb }
7202a7c19b8aSBjoern A. Zeeb 
7203a7c19b8aSBjoern A. Zeeb static uint32_t
7204a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7205a7c19b8aSBjoern A. Zeeb {
7206a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
7207a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7208a7c19b8aSBjoern A. Zeeb }
7209a7c19b8aSBjoern A. Zeeb 
7210a7c19b8aSBjoern A. Zeeb uint32_t
7211a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7212a7c19b8aSBjoern A. Zeeb {
7213a7c19b8aSBjoern A. Zeeb 
7214a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
7215a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
7216a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7217a7c19b8aSBjoern A. Zeeb 
7218a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7219a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7220a7c19b8aSBjoern A. Zeeb 
7221a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
7222a7c19b8aSBjoern A. Zeeb 
7223a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7224a7c19b8aSBjoern A. Zeeb }
7225a7c19b8aSBjoern A. Zeeb 
72266b4cac81SBjoern A. Zeeb uint32_t
72276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
72286b4cac81SBjoern A. Zeeb     enum nl80211_band band)
72296b4cac81SBjoern A. Zeeb {
72306b4cac81SBjoern A. Zeeb 
72316b4cac81SBjoern A. Zeeb 	switch (band) {
72326b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
72336b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
72346b4cac81SBjoern A. Zeeb 		break;
72356b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
72366b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
72376b4cac81SBjoern A. Zeeb 		break;
72386b4cac81SBjoern A. Zeeb 	default:
72396b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
72406b4cac81SBjoern A. Zeeb 		break;
72416b4cac81SBjoern A. Zeeb 	}
72426b4cac81SBjoern A. Zeeb 
72436b4cac81SBjoern A. Zeeb 	return (0);
72446b4cac81SBjoern A. Zeeb }
72456b4cac81SBjoern A. Zeeb 
72466b4cac81SBjoern A. Zeeb uint32_t
72476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
72486b4cac81SBjoern A. Zeeb {
72496b4cac81SBjoern A. Zeeb 
72506b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
72516b4cac81SBjoern A. Zeeb }
72526b4cac81SBjoern A. Zeeb 
7253ac867c20SBjoern A. Zeeb #if 0
72546b4cac81SBjoern A. Zeeb static struct lkpi_sta *
72556b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
72566b4cac81SBjoern A. Zeeb {
72576b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
72586b4cac81SBjoern A. Zeeb 
7259a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7260a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72616b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
7262a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
72636b4cac81SBjoern A. Zeeb 			return (lsta);
72646b4cac81SBjoern A. Zeeb 		}
72656b4cac81SBjoern A. Zeeb 	}
7266a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
72676b4cac81SBjoern A. Zeeb 
72686b4cac81SBjoern A. Zeeb 	return (NULL);
72696b4cac81SBjoern A. Zeeb }
7270ac867c20SBjoern A. Zeeb #endif
72716b4cac81SBjoern A. Zeeb 
72726b4cac81SBjoern A. Zeeb struct ieee80211_sta *
72736b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
72746b4cac81SBjoern A. Zeeb {
72756b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7276a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
72776b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
72786b4cac81SBjoern A. Zeeb 
72796b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
72806b4cac81SBjoern A. Zeeb 
7281a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7282a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72836b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
72846b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7285a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
72866b4cac81SBjoern A. Zeeb 			return (sta);
72876b4cac81SBjoern A. Zeeb 		}
72886b4cac81SBjoern A. Zeeb 	}
7289a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
72906b4cac81SBjoern A. Zeeb 	return (NULL);
72916b4cac81SBjoern A. Zeeb }
72926b4cac81SBjoern A. Zeeb 
72936b4cac81SBjoern A. Zeeb struct ieee80211_sta *
72942e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
72952e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
72966b4cac81SBjoern A. Zeeb {
72976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72986b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72996b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
73006b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
73016b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
73026b4cac81SBjoern A. Zeeb 
73036b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
73046b4cac81SBjoern A. Zeeb 	sta = NULL;
73056b4cac81SBjoern A. Zeeb 
73068891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
73076b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
73086b4cac81SBjoern A. Zeeb 
73096b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
73106b4cac81SBjoern A. Zeeb 
73116b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
73126b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
73136b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
73146b4cac81SBjoern A. Zeeb 			continue;
73156b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
73166b4cac81SBjoern A. Zeeb 		if (sta != NULL)
73176b4cac81SBjoern A. Zeeb 			break;
73186b4cac81SBjoern A. Zeeb 	}
73198891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
73206b4cac81SBjoern A. Zeeb 
73216b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
73226b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
73236b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
73246b4cac81SBjoern A. Zeeb 			return (NULL);
73256b4cac81SBjoern A. Zeeb 	}
73266b4cac81SBjoern A. Zeeb 
73276b4cac81SBjoern A. Zeeb 	return (sta);
73286b4cac81SBjoern A. Zeeb }
73296b4cac81SBjoern A. Zeeb 
73306b4cac81SBjoern A. Zeeb struct sk_buff *
73316b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
73326b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
73336b4cac81SBjoern A. Zeeb {
73346b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73350cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
73366b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
73376b4cac81SBjoern A. Zeeb 
73380cbcfa19SBjoern A. Zeeb 	skb = NULL;
73396b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73406b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
73416b4cac81SBjoern A. Zeeb 
73420cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
73430cbcfa19SBjoern A. Zeeb 		goto stopped;
73440cbcfa19SBjoern A. Zeeb 
73450cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
73460cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
73470cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
73480cbcfa19SBjoern A. Zeeb 		goto stopped;
73490cbcfa19SBjoern A. Zeeb 	}
73500cbcfa19SBjoern A. Zeeb 
7351eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
7352eac3646fSBjoern A. Zeeb 
7353eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
73546b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
7355eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
73566b4cac81SBjoern A. Zeeb 
73570cbcfa19SBjoern A. Zeeb stopped:
73586b4cac81SBjoern A. Zeeb 	return (skb);
73596b4cac81SBjoern A. Zeeb }
73606b4cac81SBjoern A. Zeeb 
73616b4cac81SBjoern A. Zeeb void
73626b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
736386220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
73646b4cac81SBjoern A. Zeeb {
73656b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73666b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
736786220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
73686b4cac81SBjoern A. Zeeb 
73696b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73706b4cac81SBjoern A. Zeeb 
73716b4cac81SBjoern A. Zeeb 	fc = bc = 0;
7372eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
73736b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
73746b4cac81SBjoern A. Zeeb 		fc++;
73756b4cac81SBjoern A. Zeeb 		bc += skb->len;
73766b4cac81SBjoern A. Zeeb 	}
7377eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
73786b4cac81SBjoern A. Zeeb 	if (frame_cnt)
73796b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
73806b4cac81SBjoern A. Zeeb 	if (byte_cnt)
73816b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
73826b4cac81SBjoern A. Zeeb 
73836b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
73846b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
73856b4cac81SBjoern A. Zeeb 	IMPROVE();
73866b4cac81SBjoern A. Zeeb }
73876b4cac81SBjoern A. Zeeb 
73886b4cac81SBjoern A. Zeeb /*
73896b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
73906b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
73916b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
73926b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
73936b4cac81SBjoern A. Zeeb  */
7394a8397571SBjoern A. Zeeb static void
7395a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
73966b4cac81SBjoern A. Zeeb     int status)
73976b4cac81SBjoern A. Zeeb {
73986b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
73996b4cac81SBjoern A. Zeeb 	struct mbuf *m;
74006b4cac81SBjoern A. Zeeb 
74016b4cac81SBjoern A. Zeeb 	m = skb->m;
74026b4cac81SBjoern A. Zeeb 	skb->m = NULL;
74036b4cac81SBjoern A. Zeeb 
74046b4cac81SBjoern A. Zeeb 	if (m != NULL) {
74056b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
74066b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
74076b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
74086b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
74096b4cac81SBjoern A. Zeeb 	}
7410a8397571SBjoern A. Zeeb }
74116b4cac81SBjoern A. Zeeb 
7412a8397571SBjoern A. Zeeb void
7413a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7414a8397571SBjoern A. Zeeb     int status)
7415a8397571SBjoern A. Zeeb {
7416a8397571SBjoern A. Zeeb 
7417a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
74186b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
74196b4cac81SBjoern A. Zeeb }
74206b4cac81SBjoern A. Zeeb 
7421383b3e8fSBjoern A. Zeeb void
7422a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7423a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
7424383b3e8fSBjoern A. Zeeb {
7425a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
7426383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
7427383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
7428383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
7429383b3e8fSBjoern A. Zeeb 	int status;
7430383b3e8fSBjoern A. Zeeb 
7431a8397571SBjoern A. Zeeb 	skb = txstat->skb;
7432383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
7433383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
7434383b3e8fSBjoern A. Zeeb 
7435383b3e8fSBjoern A. Zeeb 		m = skb->m;
7436383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
7437383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
7438383b3e8fSBjoern A. Zeeb 	} else {
7439383b3e8fSBjoern A. Zeeb 		ni = NULL;
7440383b3e8fSBjoern A. Zeeb 	}
7441383b3e8fSBjoern A. Zeeb 
7442a8397571SBjoern A. Zeeb 	info = txstat->info;
7443383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7444383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
7445383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7446383b3e8fSBjoern A. Zeeb 	} else {
7447383b3e8fSBjoern A. Zeeb 		status = 1;
7448383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7449383b3e8fSBjoern A. Zeeb 	}
7450383b3e8fSBjoern A. Zeeb 
7451383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
7452383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
7453383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7454383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
7455383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7456383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7457383b3e8fSBjoern A. Zeeb 		}
7458383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
7459a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7460383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
7461383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7462383b3e8fSBjoern A. Zeeb #endif
7463adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7464383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7465383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7466383b3e8fSBjoern A. Zeeb 		}
7467383b3e8fSBjoern A. Zeeb 
7468d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7469383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
747046de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7471383b3e8fSBjoern A. Zeeb 
7472383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7473383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7474d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
747546de4d9fSAdrian Chadd 			    txs.long_retries);
7476383b3e8fSBjoern A. Zeeb 		}
7477383b3e8fSBjoern A. Zeeb #endif
7478383b3e8fSBjoern A. Zeeb 	}
7479383b3e8fSBjoern A. Zeeb 
7480383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7481383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7482383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7483383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7484383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7485383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7486adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7487383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7488383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7489383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7490383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7491383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7492383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7493383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7494383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7495383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7496383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7497383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7498383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7499383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7500adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7501383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7502383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7503383b3e8fSBjoern A. Zeeb #endif
7504383b3e8fSBjoern A. Zeeb 
7505a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7506a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7507a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7508a8397571SBjoern A. Zeeb 	} else {
7509383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7510383b3e8fSBjoern A. Zeeb 	}
7511a8397571SBjoern A. Zeeb }
7512a8397571SBjoern A. Zeeb 
7513a8397571SBjoern A. Zeeb void
7514a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7515a8397571SBjoern A. Zeeb {
7516a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7517a8397571SBjoern A. Zeeb 
7518a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7519a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7520a8397571SBjoern A. Zeeb 	status.skb = skb;
7521a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7522a8397571SBjoern A. Zeeb 
7523a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7524a8397571SBjoern A. Zeeb }
7525383b3e8fSBjoern A. Zeeb 
75266b4cac81SBjoern A. Zeeb /*
75276b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
75286b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
75296b4cac81SBjoern A. Zeeb  * mbufs this should go away.
75306b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
75316b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
75326b4cac81SBjoern A. Zeeb  */
75336b4cac81SBjoern A. Zeeb static void
75346b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
75356b4cac81SBjoern A. Zeeb {
75366b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
75376b4cac81SBjoern A. Zeeb 	struct mbuf *m;
75386b4cac81SBjoern A. Zeeb 
75396b4cac81SBjoern A. Zeeb 	if (p == NULL)
75406b4cac81SBjoern A. Zeeb 		return;
75416b4cac81SBjoern A. Zeeb 
75426b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
75436b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
75446b4cac81SBjoern A. Zeeb 
75456b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
75466b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
75476b4cac81SBjoern A. Zeeb 	if (ni != NULL)
75486b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
75496b4cac81SBjoern A. Zeeb 	m_freem(m);
75506b4cac81SBjoern A. Zeeb }
75516b4cac81SBjoern A. Zeeb 
75526b4cac81SBjoern A. Zeeb void
75536b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
75546b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
75556b4cac81SBjoern A. Zeeb {
75566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75576b4cac81SBjoern A. Zeeb 
75586b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
75596b4cac81SBjoern A. Zeeb 	IMPROVE();
75606b4cac81SBjoern A. Zeeb 
75616b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
75626b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
75636b4cac81SBjoern A. Zeeb }
75646b4cac81SBjoern A. Zeeb 
75656b4cac81SBjoern A. Zeeb void
75666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
75676b4cac81SBjoern A. Zeeb     struct work_struct *w)
75686b4cac81SBjoern A. Zeeb {
75696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75706b4cac81SBjoern A. Zeeb 
75716b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
75726b4cac81SBjoern A. Zeeb 	IMPROVE();
75736b4cac81SBjoern A. Zeeb 
75746b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
75756b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
75766b4cac81SBjoern A. Zeeb }
75776b4cac81SBjoern A. Zeeb 
75786b4cac81SBjoern A. Zeeb struct sk_buff *
7579ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7580ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7581ade774b1SBjoern A. Zeeb {
7582ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7583ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7584ade774b1SBjoern A. Zeeb 	uint8_t *p;
7585ade774b1SBjoern A. Zeeb 	size_t len;
7586ade774b1SBjoern A. Zeeb 
7587ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7588ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7589ade774b1SBjoern A. Zeeb 
7590ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7591ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7592ade774b1SBjoern A. Zeeb 		return (NULL);
7593ade774b1SBjoern A. Zeeb 
7594ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7595ade774b1SBjoern A. Zeeb 
7596ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7597ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7598ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7599ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7600ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7601ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7602ade774b1SBjoern A. Zeeb 
7603ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7604ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7605ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7606ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7607ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7608ade774b1SBjoern A. Zeeb 
7609ade774b1SBjoern A. Zeeb 	return (skb);
7610ade774b1SBjoern A. Zeeb }
7611ade774b1SBjoern A. Zeeb 
7612ade774b1SBjoern A. Zeeb struct sk_buff *
76136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
76146b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
76156b4cac81SBjoern A. Zeeb {
76166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76176b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
76186b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
76196b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
76206b4cac81SBjoern A. Zeeb 	uint16_t v;
76216b4cac81SBjoern A. Zeeb 
76226b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
76236b4cac81SBjoern A. Zeeb 	if (skb == NULL)
76246b4cac81SBjoern A. Zeeb 		return (NULL);
76256b4cac81SBjoern A. Zeeb 
76266b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
76276b4cac81SBjoern A. Zeeb 
76286b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76296b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
76306b4cac81SBjoern A. Zeeb 
76316b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
76326b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
76336b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7634616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
76356b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
76366b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
76376b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
76386b4cac81SBjoern A. Zeeb 
76396b4cac81SBjoern A. Zeeb 	return (skb);
76406b4cac81SBjoern A. Zeeb }
76416b4cac81SBjoern A. Zeeb 
76426b4cac81SBjoern A. Zeeb struct sk_buff *
76436b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7644adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
76456b4cac81SBjoern A. Zeeb {
76466b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76476b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
76486b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
76496b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
76506b4cac81SBjoern A. Zeeb 
7651adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7652adff403fSBjoern A. Zeeb 
76536b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
76546b4cac81SBjoern A. Zeeb 	if (skb == NULL)
76556b4cac81SBjoern A. Zeeb 		return (NULL);
76566b4cac81SBjoern A. Zeeb 
76576b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
76586b4cac81SBjoern A. Zeeb 
76596b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76606b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
76616b4cac81SBjoern A. Zeeb 
76626b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
76636b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
76646b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
76656b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
76666b4cac81SBjoern A. Zeeb 
76676b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
76686b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
76696b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
76706b4cac81SBjoern A. Zeeb 
76716b4cac81SBjoern A. Zeeb 	return (skb);
76726b4cac81SBjoern A. Zeeb }
76736b4cac81SBjoern A. Zeeb 
76746b4cac81SBjoern A. Zeeb struct wireless_dev *
76756b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
76766b4cac81SBjoern A. Zeeb {
76776b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76786b4cac81SBjoern A. Zeeb 
76796b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76806b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
76816b4cac81SBjoern A. Zeeb }
76826b4cac81SBjoern A. Zeeb 
76836b4cac81SBjoern A. Zeeb void
76846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
76856b4cac81SBjoern A. Zeeb {
76866b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76876b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
76886b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
76896b4cac81SBjoern A. Zeeb 	int arg;
76906b4cac81SBjoern A. Zeeb 
76916b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
76926b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
76936b4cac81SBjoern A. Zeeb 
76946b4cac81SBjoern A. Zeeb 	/*
7695f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
76966b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
76976b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
76986b4cac81SBjoern A. Zeeb 	 */
7699f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7700bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
77016b4cac81SBjoern A. Zeeb 
7702018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7703018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
77046b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
77056b4cac81SBjoern A. Zeeb }
77066b4cac81SBjoern A. Zeeb 
7707bb81db90SBjoern A. Zeeb void
7708bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7709bb81db90SBjoern A. Zeeb {
7710bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7711bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7712bb81db90SBjoern A. Zeeb 
7713bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7714bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7715bb81db90SBjoern A. Zeeb 
7716bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7717bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
77183540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7719bb81db90SBjoern A. Zeeb }
7720bb81db90SBjoern A. Zeeb 
77215edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
77225edde07cSBjoern A. Zeeb 
77235a9a0d78SBjoern A. Zeeb void
77245a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
77255a9a0d78SBjoern A. Zeeb {
77265a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77275a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
77285a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
77295a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
77305a9a0d78SBjoern A. Zeeb 
77315a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
77325a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
77335a9a0d78SBjoern A. Zeeb 
77345a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
77355a9a0d78SBjoern A. Zeeb 
77365a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
77375a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
77385a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
77395a9a0d78SBjoern A. Zeeb 	else
77405a9a0d78SBjoern A. Zeeb 		ac_count = 1;
77415a9a0d78SBjoern A. Zeeb 
77425a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
77435a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
77445a9a0d78SBjoern A. Zeeb 
77455a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
77465a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
77475a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
77485a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
77490d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
77505a9a0d78SBjoern A. Zeeb 				/*
77515a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
77525a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
77535a9a0d78SBjoern A. Zeeb 				 */
77540d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
77550d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
77565a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
77575a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
77585a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
77595a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
77600d2cb6a6SBjoern A. Zeeb #endif
77615a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
77625a9a0d78SBjoern A. Zeeb 			}
77635a9a0d78SBjoern A. Zeeb 		}
77645a9a0d78SBjoern A. Zeeb 	}
77655a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
77665a9a0d78SBjoern A. Zeeb }
77675a9a0d78SBjoern A. Zeeb 
77685a9a0d78SBjoern A. Zeeb void
77695a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
77705a9a0d78SBjoern A. Zeeb {
77715a9a0d78SBjoern A. Zeeb 	int i;
77725a9a0d78SBjoern A. Zeeb 
77735a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
77745a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
77755a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
77765a9a0d78SBjoern A. Zeeb }
77775a9a0d78SBjoern A. Zeeb 
77785a9a0d78SBjoern A. Zeeb 
77795a9a0d78SBjoern A. Zeeb static void
77805a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
77815a9a0d78SBjoern A. Zeeb {
77825a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77835a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
77845a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
77855a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
77865a9a0d78SBjoern A. Zeeb 
77875a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
77885a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
77895a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
77905a9a0d78SBjoern A. Zeeb 	else
77915a9a0d78SBjoern A. Zeeb 		ac_count = 1;
77925a9a0d78SBjoern A. Zeeb 
77935a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
77945a9a0d78SBjoern A. Zeeb 
77955a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
77965a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
77975a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
77985a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
77995a9a0d78SBjoern A. Zeeb 
78005a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
78015a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
78025a9a0d78SBjoern A. Zeeb 
78035a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
78045a9a0d78SBjoern A. Zeeb 
78055a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
78065a9a0d78SBjoern A. Zeeb 
78070d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
78085a9a0d78SBjoern A. Zeeb 				/*
78095a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
78105a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
78115a9a0d78SBjoern A. Zeeb 				 */
78120d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
78130d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
78145a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
78155a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
78165a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
78175a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
78180d2cb6a6SBjoern A. Zeeb #endif
78195a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
78205a9a0d78SBjoern A. Zeeb 
7821a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7822a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
78235a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
78245a9a0d78SBjoern A. Zeeb 
78255a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
78265a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
78275a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
78285a9a0d78SBjoern A. Zeeb 
78295a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
78305a9a0d78SBjoern A. Zeeb 							continue;
78315a9a0d78SBjoern A. Zeeb 
78325a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
78335a9a0d78SBjoern A. Zeeb 							continue;
78345a9a0d78SBjoern A. Zeeb 
78355a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
78365a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
78375a9a0d78SBjoern A. Zeeb 							continue;
78385a9a0d78SBjoern A. Zeeb 
78395a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
78405a9a0d78SBjoern A. Zeeb 
78415a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
78425a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
78435a9a0d78SBjoern A. Zeeb 					}
78445a9a0d78SBjoern A. Zeeb 				}
7845a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
78465a9a0d78SBjoern A. Zeeb 			}
78475a9a0d78SBjoern A. Zeeb 		}
78485a9a0d78SBjoern A. Zeeb 	}
78495a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
78505a9a0d78SBjoern A. Zeeb }
78515a9a0d78SBjoern A. Zeeb 
78525a9a0d78SBjoern A. Zeeb void
78535a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
78545a9a0d78SBjoern A. Zeeb {
78555a9a0d78SBjoern A. Zeeb 	int i;
78565a9a0d78SBjoern A. Zeeb 
78575a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
78585a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
78595a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
78605a9a0d78SBjoern A. Zeeb }
78615a9a0d78SBjoern A. Zeeb 
78625a9a0d78SBjoern A. Zeeb void
78635a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
78645a9a0d78SBjoern A. Zeeb {
78655a9a0d78SBjoern A. Zeeb 
78665a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
78675a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
78685a9a0d78SBjoern A. Zeeb 
78695a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
78705a9a0d78SBjoern A. Zeeb }
78715a9a0d78SBjoern A. Zeeb 
78725a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
78735a9a0d78SBjoern A. Zeeb void
78745a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
78755a9a0d78SBjoern A. Zeeb {
78765a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
78775a9a0d78SBjoern A. Zeeb 
78785a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
78795a9a0d78SBjoern A. Zeeb 
78805a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
78815a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
78825a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
78835a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
78845a9a0d78SBjoern A. Zeeb }
78855a9a0d78SBjoern A. Zeeb 
78865a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
78875a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
78885a9a0d78SBjoern A. Zeeb {
78895a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
78905a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
78915a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
78925a9a0d78SBjoern A. Zeeb 
78935a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
78945a9a0d78SBjoern A. Zeeb 	txq = NULL;
78955a9a0d78SBjoern A. Zeeb 
78965a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
78975a9a0d78SBjoern A. Zeeb 
78985a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
78995a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
79005a9a0d78SBjoern A. Zeeb 		goto out;
79015a9a0d78SBjoern A. Zeeb 
79025a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
79035a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
79045a9a0d78SBjoern A. Zeeb 		goto out;
79055a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
79065a9a0d78SBjoern A. Zeeb 		goto out;
79075a9a0d78SBjoern A. Zeeb 
79085a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
79095a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
79105a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
79115a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
79125a9a0d78SBjoern A. Zeeb 
79135a9a0d78SBjoern A. Zeeb out:
79145a9a0d78SBjoern A. Zeeb 	return (txq);
79155a9a0d78SBjoern A. Zeeb }
79165a9a0d78SBjoern A. Zeeb 
79175a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
79185a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
79195a9a0d78SBjoern A. Zeeb {
79205a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
79215a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7922eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
79235a9a0d78SBjoern A. Zeeb 
79245a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
79255a9a0d78SBjoern A. Zeeb 
79265a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
79275a9a0d78SBjoern A. Zeeb 
79285a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7929eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7930eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7931eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7932eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
79335a9a0d78SBjoern A. Zeeb 		goto out;
79345a9a0d78SBjoern A. Zeeb 
793541b746e0SAustin Shafer 	/*
793641b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
793741b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
793841b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
793941b746e0SAustin Shafer 	 */
794041b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
79415a9a0d78SBjoern A. Zeeb 		goto out;
79425a9a0d78SBjoern A. Zeeb 
79435a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
79445a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
79455a9a0d78SBjoern A. Zeeb out:
79465a9a0d78SBjoern A. Zeeb 	return;
79475a9a0d78SBjoern A. Zeeb }
79485a9a0d78SBjoern A. Zeeb 
7949eac3646fSBjoern A. Zeeb void
7950eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7951eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7952eac3646fSBjoern A. Zeeb {
7953eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7954eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7955eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7956eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7957eac3646fSBjoern A. Zeeb 
7958eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7959eac3646fSBjoern A. Zeeb 
7960eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7961eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7962eac3646fSBjoern A. Zeeb 	do {
7963eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7964eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7965eac3646fSBjoern A. Zeeb 			break;
7966eac3646fSBjoern A. Zeeb 
7967eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7968eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7969eac3646fSBjoern A. Zeeb 		do {
7970eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7971eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7972eac3646fSBjoern A. Zeeb 				break;
7973eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7974eac3646fSBjoern A. Zeeb 		} while(1);
7975eac3646fSBjoern A. Zeeb 
7976eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7977eac3646fSBjoern A. Zeeb 	} while (1);
7978eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7979eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7980eac3646fSBjoern A. Zeeb }
7981eac3646fSBjoern A. Zeeb 
79825a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
79835a9a0d78SBjoern A. Zeeb 
79845edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
79855edde07cSBjoern A. Zeeb 	u_int refcnt;
79865edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
79875edde07cSBjoern A. Zeeb };
79885edde07cSBjoern A. Zeeb 
79895edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
79905edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
79915edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
79925edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
79935edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
79945edde07cSBjoern A. Zeeb 	size_t ssid_len;
79955edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
79965edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
79975edde07cSBjoern A. Zeeb 
79985edde07cSBjoern A. Zeeb 	/*
79995edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
80005edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
80015edde07cSBjoern A. Zeeb 	 */
80025edde07cSBjoern A. Zeeb 	bool match;
80035edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
80045edde07cSBjoern A. Zeeb };
80055edde07cSBjoern A. Zeeb 
80065edde07cSBjoern A. Zeeb static void
80075edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
80085edde07cSBjoern A. Zeeb {
80095edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
80105edde07cSBjoern A. Zeeb 	size_t ielen;
80115edde07cSBjoern A. Zeeb 
80125edde07cSBjoern A. Zeeb 	lookup = arg;
80135edde07cSBjoern A. Zeeb 
80145edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
80155edde07cSBjoern A. Zeeb 	if (lookup->match)
80165edde07cSBjoern A. Zeeb 		return;
80175edde07cSBjoern A. Zeeb 
80185edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
80195edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
80205edde07cSBjoern A. Zeeb 		return;
80215edde07cSBjoern A. Zeeb 
80225edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
80235edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
80245edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
80255edde07cSBjoern A. Zeeb 		return;
80265edde07cSBjoern A. Zeeb 	}
80275edde07cSBjoern A. Zeeb 
80285edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
80295edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
80305edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
80315edde07cSBjoern A. Zeeb 		return;
80325edde07cSBjoern A. Zeeb 	}
80335edde07cSBjoern A. Zeeb 
80345edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
80355edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
80365edde07cSBjoern A. Zeeb 
80375edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
80385edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
80395edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
80405edde07cSBjoern A. Zeeb 			return;
80415edde07cSBjoern A. Zeeb 	}
80425edde07cSBjoern A. Zeeb 
80435edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
80445edde07cSBjoern A. Zeeb 		return;
80455edde07cSBjoern A. Zeeb 
80465edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
80475edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
80485edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
80495edde07cSBjoern A. Zeeb 			return;
80505edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
80515edde07cSBjoern A. Zeeb 			return;
80525edde07cSBjoern A. Zeeb 	}
80535edde07cSBjoern A. Zeeb 
80545edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
80555edde07cSBjoern A. Zeeb 
80565edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
80575edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
80585edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
80595edde07cSBjoern A. Zeeb 		return;
80605edde07cSBjoern A. Zeeb 
80615edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
80625edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
80635edde07cSBjoern A. Zeeb 	if (ielen)
80645edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
80655edde07cSBjoern A. Zeeb 
80665edde07cSBjoern A. Zeeb 	lookup->match = true;
80675edde07cSBjoern A. Zeeb }
80685edde07cSBjoern A. Zeeb 
80695edde07cSBjoern A. Zeeb struct cfg80211_bss *
80705edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
80715edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
80725edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
80735edde07cSBjoern A. Zeeb {
80745edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
80755edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
80765edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
80775edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
80785edde07cSBjoern A. Zeeb 
80795edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
80805edde07cSBjoern A. Zeeb 
80815edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
80825edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
80835edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
80845edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
80855edde07cSBjoern A. Zeeb 		return (NULL);
80865edde07cSBjoern A. Zeeb 	}
80875edde07cSBjoern A. Zeeb 
80885edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
80895edde07cSBjoern A. Zeeb 	lookup.chan = chan;
80905edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
80915edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
80925edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
80935edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
80945edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
80955edde07cSBjoern A. Zeeb 	lookup.match = false;
80965edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
80975edde07cSBjoern A. Zeeb 
80985edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
80995edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
81005edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
81015edde07cSBjoern A. Zeeb 	if (!lookup.match) {
81025edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
81035edde07cSBjoern A. Zeeb 		return (NULL);
81045edde07cSBjoern A. Zeeb 	}
81055edde07cSBjoern A. Zeeb 
81065edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
81075edde07cSBjoern A. Zeeb 	return (&lbss->bss);
81085edde07cSBjoern A. Zeeb }
81095edde07cSBjoern A. Zeeb 
81105edde07cSBjoern A. Zeeb void
81115edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
81125edde07cSBjoern A. Zeeb {
81135edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
81145edde07cSBjoern A. Zeeb 
81155edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
81165edde07cSBjoern A. Zeeb 
81175edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
81185edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
81195edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
81205edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
81215edde07cSBjoern A. Zeeb 	}
81225edde07cSBjoern A. Zeeb }
81235edde07cSBjoern A. Zeeb 
81245edde07cSBjoern A. Zeeb void
81255edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
81265edde07cSBjoern A. Zeeb {
81275edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
81285edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
81295edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
81305edde07cSBjoern A. Zeeb 
81315edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
81325edde07cSBjoern A. Zeeb 	ic = lhw->ic;
81335edde07cSBjoern A. Zeeb 
81345edde07cSBjoern A. Zeeb 	/*
81355edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
81365edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
81375edde07cSBjoern A. Zeeb 	 */
81385edde07cSBjoern A. Zeeb 	if (ic == NULL ||
81395edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
81405edde07cSBjoern A. Zeeb 		return;
81415edde07cSBjoern A. Zeeb 
81425edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
81435edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
81445edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
81455edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
81465edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
81475edde07cSBjoern A. Zeeb }
81485edde07cSBjoern A. Zeeb 
81495edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
81505edde07cSBjoern A. Zeeb 
8151ac1d519cSBjoern A. Zeeb /*
8152ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
8153ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
8154ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
8155ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
8156ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
8157ac1d519cSBjoern A. Zeeb  */
8158ac1d519cSBjoern A. Zeeb 
8159ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8160ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
8161ac1d519cSBjoern A. Zeeb {
8162ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
8163ac1d519cSBjoern A. Zeeb 	uint32_t changed;
8164ac1d519cSBjoern A. Zeeb 	int error;
8165ac1d519cSBjoern A. Zeeb 
8166ac1d519cSBjoern A. Zeeb 	changed = 0;
8167ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
8168ac1d519cSBjoern A. Zeeb 		cd = NULL;
8169ac1d519cSBjoern A. Zeeb 	else
8170ac1d519cSBjoern A. Zeeb 		cd = &new->def;
8171ac1d519cSBjoern A. Zeeb 
8172ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
8173ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
8174ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
8175ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8176ac1d519cSBjoern A. Zeeb 	}
8177ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8178ac1d519cSBjoern A. Zeeb 
8179ac1d519cSBjoern A. Zeeb 	if (changed == 0)
8180ac1d519cSBjoern A. Zeeb 		return (0);
8181ac1d519cSBjoern A. Zeeb 
8182ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
8183ac1d519cSBjoern A. Zeeb 	return (error);
8184ac1d519cSBjoern A. Zeeb }
8185ac1d519cSBjoern A. Zeeb 
8186ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8187ac1d519cSBjoern A. Zeeb 
81886b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
81896b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
81906b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8191