xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 1f73e0ed539d2be48fed70b09e87ecccce4ca869)
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
829fb91463SBjoern A. Zeeb /* #define	LKPI_80211_HT */
8311db70b6SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
31740839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
31840839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
31940839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32140839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32240839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
32340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
32440839418SBjoern A. Zeeb 
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
32640839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
32940839418SBjoern A. Zeeb 
33040839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33140839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33240839418SBjoern A. Zeeb 
33340839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
33440839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
33540839418SBjoern A. Zeeb 
33640839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
33740839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
33840839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
33940839418SBjoern A. Zeeb 		}
34040839418SBjoern A. Zeeb 
34140839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
34440839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
34540839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
34640839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
34740839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
34840839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
34940839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35040839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35140839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35240839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35340839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
35440839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
35540839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
35640839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35740839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
35840839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
35940839418SBjoern A. Zeeb 	}
36040839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36140839418SBjoern A. Zeeb 
36240839418SBjoern A. Zeeb 	sbuf_finish(&s);
36340839418SBjoern A. Zeeb 	sbuf_delete(&s);
36440839418SBjoern A. Zeeb 
36540839418SBjoern A. Zeeb 	return (0);
36640839418SBjoern A. Zeeb }
36740839418SBjoern A. Zeeb 
3689fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3699fb91463SBjoern A. Zeeb static void
3709fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
3719fb91463SBjoern A. Zeeb {
3729fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
3739fb91463SBjoern A. Zeeb 	uint8_t *ie;
3749fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
3759fb91463SBjoern A. Zeeb 	int i, rx_nss;
3769fb91463SBjoern A. Zeeb 
3779fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
3789fb91463SBjoern A. Zeeb 		return;
3799fb91463SBjoern A. Zeeb 
3809fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3819fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3829fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
3839fb91463SBjoern A. Zeeb 
3849fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
3859fb91463SBjoern A. Zeeb 
3869fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
3879fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
3889fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3899fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
3909fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
3919fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3929fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
3939fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
3949fb91463SBjoern A. Zeeb 
3959fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
3969fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
3979fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
3989fb91463SBjoern A. Zeeb 		ie += 4;
3999fb91463SBjoern A. Zeeb 	ie += 2;
4009fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4019fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4029fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4039fb91463SBjoern A. Zeeb 
4049fb91463SBjoern A. Zeeb 	rx_nss = 0;
4059fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
4069fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4079fb91463SBjoern A. Zeeb 			rx_nss++;
4089fb91463SBjoern A. Zeeb 	}
4099fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
4109fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
4119fb91463SBjoern A. Zeeb 
4129fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
4139fb91463SBjoern A. Zeeb }
4149fb91463SBjoern A. Zeeb #endif
4159fb91463SBjoern A. Zeeb 
4169fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4179fb91463SBjoern A. Zeeb static void
4189fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
4199fb91463SBjoern A. Zeeb {
4209fb91463SBjoern A. Zeeb 
4219fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
4229fb91463SBjoern A. Zeeb 		return;
4239fb91463SBjoern A. Zeeb 
4249fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
4259fb91463SBjoern A. Zeeb #ifdef __notyet__
4269fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
4279fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
4289fb91463SBjoern A. Zeeb 		} else
4299fb91463SBjoern A. Zeeb #endif
4309fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
4319fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
4329fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
4339fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4349fb91463SBjoern A. Zeeb 	}
4359fb91463SBjoern A. Zeeb 
4369fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
4379fb91463SBjoern A. Zeeb 	return;
4389fb91463SBjoern A. Zeeb }
4399fb91463SBjoern A. Zeeb #endif
4409fb91463SBjoern A. Zeeb 
441d9f59799SBjoern A. Zeeb static void
44267674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
44367674c1cSBjoern A. Zeeb     const char *_f, int _l)
444d9f59799SBjoern A. Zeeb {
445d9f59799SBjoern A. Zeeb 
4469d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4479d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
448d9f59799SBjoern A. Zeeb 		return;
449d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
450d9f59799SBjoern A. Zeeb 		return;
451d9f59799SBjoern A. Zeeb 
452d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
45367674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
45467674c1cSBjoern A. Zeeb 	if (ni != NULL)
45567674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
456d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
457d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
45811db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
4599d9ba2b7SBjoern A. Zeeb #endif
460d9f59799SBjoern A. Zeeb }
461d9f59799SBjoern A. Zeeb 
462d9f59799SBjoern A. Zeeb static void
463d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
464d9f59799SBjoern A. Zeeb {
465d9f59799SBjoern A. Zeeb 
466d9f59799SBjoern A. Zeeb 
467a8f735a6SBjoern A. Zeeb 	wiphy_lock(lsta->hw->wiphy);
468a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
469a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
470a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
471a8f735a6SBjoern A. Zeeb 	wiphy_unlock(lsta->hw->wiphy);
472d9f59799SBjoern A. Zeeb }
473d9f59799SBjoern A. Zeeb 
4744f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
4754f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
4764f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
4774f61ef8bSBjoern A. Zeeb {
4784f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
4794f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
4804f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
4814f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
482b6b352e4SBjoern A. Zeeb 	int band, i, tid;
4839fb91463SBjoern A. Zeeb 	int ht_rx_nss;
4849fb91463SBjoern A. Zeeb 	int vht_rx_nss;
4854f61ef8bSBjoern A. Zeeb 
4864f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
4874f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
4884f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
4894f61ef8bSBjoern A. Zeeb 		return (NULL);
4904f61ef8bSBjoern A. Zeeb 
491a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
4924f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
4934f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
4944f61ef8bSBjoern A. Zeeb 	/*
4950936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
4960936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
4970936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
4980936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
4990936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
5000936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
5010936c648SBjoern A. Zeeb 	 * two separate allocations.
5024f61ef8bSBjoern A. Zeeb 	 */
5030936c648SBjoern A. Zeeb 	lsta->ni = ni;
5044f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
5054f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
5064f61ef8bSBjoern A. Zeeb 
5074f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5084f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5094f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
5104f61ef8bSBjoern A. Zeeb 
5114f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
512b6b352e4SBjoern A. Zeeb 
513b6b352e4SBjoern A. Zeeb 	/* TXQ */
5144f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
5154f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
5164f61ef8bSBjoern A. Zeeb 
517d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
5184f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
5194f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
5204f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
5214f61ef8bSBjoern A. Zeeb 			goto cleanup;
5224f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
5234f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
524d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
525d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
526d0d29110SBjoern A. Zeeb 				continue;
527d0d29110SBjoern A. Zeeb 			}
528d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
5294f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
5304f61ef8bSBjoern A. Zeeb 		} else {
531fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
5324f61ef8bSBjoern A. Zeeb 		}
533d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
5340cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
535d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
5364f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
5374f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
5380cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
539d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
540eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
5414f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
5424f61ef8bSBjoern A. Zeeb 	}
5434f61ef8bSBjoern A. Zeeb 
544b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
545b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
546b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
547b6b352e4SBjoern A. Zeeb 
548b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
549b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
550b6b352e4SBjoern A. Zeeb 			continue;
551b6b352e4SBjoern A. Zeeb 
552b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
553b6b352e4SBjoern A. Zeeb 
554b6b352e4SBjoern A. Zeeb 			IMPROVE("Further supband->bitrates[i]* checks?");
555b6b352e4SBjoern A. Zeeb 			/* or should we get them from the ni? */
556b6b352e4SBjoern A. Zeeb 			sta->deflink.supp_rates[band] |= BIT(i);
557b6b352e4SBjoern A. Zeeb 		}
558b6b352e4SBjoern A. Zeeb 	}
5599fb91463SBjoern A. Zeeb 
5606ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
5619fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
562f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
5639fb91463SBjoern A. Zeeb 
5649fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
5659fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
5669fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
5679fb91463SBjoern A. Zeeb #endif
5689fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
5699fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5709fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
5719fb91463SBjoern A. Zeeb #endif
5729fb91463SBjoern A. Zeeb 
5739fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
5749fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
5759fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
576b6b352e4SBjoern A. Zeeb 
5776ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
5786ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
5796ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
5806ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
5816ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
5826ffb7bd4SBjoern A. Zeeb 	}
5836ffb7bd4SBjoern A. Zeeb 
5844f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
585fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
5864f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
5874f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
5880936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
5894f61ef8bSBjoern A. Zeeb 
5904f61ef8bSBjoern A. Zeeb 	return (lsta);
5914f61ef8bSBjoern A. Zeeb 
5924f61ef8bSBjoern A. Zeeb cleanup:
593eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
594eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
595eac3646fSBjoern A. Zeeb 
596eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
597eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
5984f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
599eac3646fSBjoern A. Zeeb 	}
6004f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6014f61ef8bSBjoern A. Zeeb 	return (NULL);
6024f61ef8bSBjoern A. Zeeb }
6034f61ef8bSBjoern A. Zeeb 
6040936c648SBjoern A. Zeeb static void
6050936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
6060936c648SBjoern A. Zeeb {
6070936c648SBjoern A. Zeeb 	struct mbuf *m;
6080936c648SBjoern A. Zeeb 
6090936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
6100936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
6110936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
6120936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
6130936c648SBjoern A. Zeeb 
6140936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
6150936c648SBjoern A. Zeeb 	IMPROVE();
6160936c648SBjoern A. Zeeb 
617fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
618fa4e4257SBjoern A. Zeeb 
619fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
6200936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
621fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
6220936c648SBjoern A. Zeeb 
6230936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
6240936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
6250936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
6260936c648SBjoern A. Zeeb 
6270936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
6280936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
6290936c648SBjoern A. Zeeb 	while (m != NULL) {
6300936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
6310936c648SBjoern A. Zeeb 
6320936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
6330936c648SBjoern A. Zeeb 		if (nim != NULL)
6340936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
6350936c648SBjoern A. Zeeb 		m_freem(m);
6360936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
6370936c648SBjoern A. Zeeb 	}
6380936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
6390936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
640fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
6410936c648SBjoern A. Zeeb 
6420936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
6430936c648SBjoern A. Zeeb 
6440936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
6450936c648SBjoern A. Zeeb 
6460936c648SBjoern A. Zeeb 	/* Free lsta. */
6470936c648SBjoern A. Zeeb 	lsta->ni = NULL;
6480936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
6490936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6500936c648SBjoern A. Zeeb }
6510936c648SBjoern A. Zeeb 
6520936c648SBjoern A. Zeeb 
6536b4cac81SBjoern A. Zeeb static enum nl80211_band
6546b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
6556b4cac81SBjoern A. Zeeb {
6566b4cac81SBjoern A. Zeeb 
6576b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
6586b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
6596b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
6606b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
6616b4cac81SBjoern A. Zeeb #ifdef __notyet__
6626b4cac81SBjoern A. Zeeb 	else if ()
6636b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
6646b4cac81SBjoern A. Zeeb 	else if ()
6656b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
6666b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
6676b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
6686b4cac81SBjoern A. Zeeb #endif
6696b4cac81SBjoern A. Zeeb 	else
6706b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
6716b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
6726b4cac81SBjoern A. Zeeb }
6736b4cac81SBjoern A. Zeeb 
6746b4cac81SBjoern A. Zeeb static uint32_t
6756b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
6766b4cac81SBjoern A. Zeeb {
6776b4cac81SBjoern A. Zeeb 
6786b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
6796b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
6806b4cac81SBjoern A. Zeeb 	switch (band) {
6816b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
6826b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
6836b4cac81SBjoern A. Zeeb 		break;
6846b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
6856b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
6866b4cac81SBjoern A. Zeeb 		break;
6876b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
6886b4cac81SBjoern A. Zeeb 		break;
6896b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
6906b4cac81SBjoern A. Zeeb 		break;
6916b4cac81SBjoern A. Zeeb 	default:
6926b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
6936b4cac81SBjoern A. Zeeb 		break;
6946b4cac81SBjoern A. Zeeb 	}
6956b4cac81SBjoern A. Zeeb 
6966b4cac81SBjoern A. Zeeb 	IMPROVE();
6976b4cac81SBjoern A. Zeeb 	return (0x00);
6986b4cac81SBjoern A. Zeeb }
6996b4cac81SBjoern A. Zeeb 
700e3a0b120SBjoern A. Zeeb #if 0
7016b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
7026b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
7036b4cac81SBjoern A. Zeeb {
7046b4cac81SBjoern A. Zeeb 
7056b4cac81SBjoern A. Zeeb 	switch (ac) {
7066b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
7076b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
7086b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
7096b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
7106b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
7116b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7126b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
7136b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
7146b4cac81SBjoern A. Zeeb 	default:
7156b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
7166b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7176b4cac81SBjoern A. Zeeb 	}
7186b4cac81SBjoern A. Zeeb }
719e3a0b120SBjoern A. Zeeb #endif
7206b4cac81SBjoern A. Zeeb 
7216b4cac81SBjoern A. Zeeb static enum nl80211_iftype
7226b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
7236b4cac81SBjoern A. Zeeb {
7246b4cac81SBjoern A. Zeeb 
7256b4cac81SBjoern A. Zeeb 	switch (opmode) {
7266b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
7276b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
7286b4cac81SBjoern A. Zeeb 		break;
7296b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
7306b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
7316b4cac81SBjoern A. Zeeb 		break;
7326b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
7336b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
7346b4cac81SBjoern A. Zeeb 		break;
7356b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
7366b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
7376b4cac81SBjoern A. Zeeb 		break;
7386b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
7396b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
7406b4cac81SBjoern A. Zeeb 		break;
7416b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
7426b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
7436b4cac81SBjoern A. Zeeb 		break;
7446b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
7456b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7466b4cac81SBjoern A. Zeeb 	default:
7476b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
7486b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7496b4cac81SBjoern A. Zeeb 	}
7506b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
7516b4cac81SBjoern A. Zeeb }
7526b4cac81SBjoern A. Zeeb 
753b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
75412a511c8SBjoern A. Zeeb static const char *
75512a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
75612a511c8SBjoern A. Zeeb {
75712a511c8SBjoern A. Zeeb 
75812a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
75912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
76012a511c8SBjoern A. Zeeb 		return ("WEP40");
76112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
76212a511c8SBjoern A. Zeeb 		return ("TKIP");
76312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
76412a511c8SBjoern A. Zeeb 		return ("CCMP");
76512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
76612a511c8SBjoern A. Zeeb 		return ("WEP104");
76712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
76812a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
76912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
77012a511c8SBjoern A. Zeeb 		return ("GCMP");
77112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
77212a511c8SBjoern A. Zeeb 		return ("GCMP_256");
77312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
77412a511c8SBjoern A. Zeeb 		return ("CCMP_256");
77512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
77612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
77712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
77812a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
77912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
78012a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
78112a511c8SBjoern A. Zeeb 	default:
78212a511c8SBjoern A. Zeeb 		return ("??");
78312a511c8SBjoern A. Zeeb 	}
78412a511c8SBjoern A. Zeeb }
78512a511c8SBjoern A. Zeeb 
7866b4cac81SBjoern A. Zeeb static uint32_t
7876b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
7886b4cac81SBjoern A. Zeeb {
7896b4cac81SBjoern A. Zeeb 
7906b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
7916b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
7926b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
7936b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
7946b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
7956b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
796906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
7976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
7986b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
7996b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
8006b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
8016b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
8026b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
8036b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
8046b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
8056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
80612a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
80712a511c8SBjoern A. Zeeb 		    __func__,
80812a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
80912a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8106b4cac81SBjoern A. Zeeb 		break;
8116b4cac81SBjoern A. Zeeb 	default:
81212a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
81312a511c8SBjoern A. Zeeb 		    __func__,
81412a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
81512a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8166b4cac81SBjoern A. Zeeb 	}
8176b4cac81SBjoern A. Zeeb 
8186b4cac81SBjoern A. Zeeb 	return (0);
8196b4cac81SBjoern A. Zeeb }
8206b4cac81SBjoern A. Zeeb 
8216b4cac81SBjoern A. Zeeb static uint32_t
8226b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
8236b4cac81SBjoern A. Zeeb {
8246b4cac81SBjoern A. Zeeb 
8256b4cac81SBjoern A. Zeeb 	switch (cipher) {
8266b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
8276b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
8286b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
8296b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
8306b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
8316b4cac81SBjoern A. Zeeb 		if (keylen < 8)
8326b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
8336b4cac81SBjoern A. Zeeb 		else
8346b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
8356b4cac81SBjoern A. Zeeb 		break;
8366b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
8376b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
8386b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
8396b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
8406b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
8416b4cac81SBjoern A. Zeeb 		break;
8426b4cac81SBjoern A. Zeeb 	default:
8436b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
8446b4cac81SBjoern A. Zeeb 	};
8456b4cac81SBjoern A. Zeeb 	return (0);
8466b4cac81SBjoern A. Zeeb }
8476b4cac81SBjoern A. Zeeb #endif
8486b4cac81SBjoern A. Zeeb 
8496b4cac81SBjoern A. Zeeb #ifdef __notyet__
8506b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
8516b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
8526b4cac81SBjoern A. Zeeb {
8536b4cac81SBjoern A. Zeeb 
8546b4cac81SBjoern A. Zeeb 	/*
8556b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
8566b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
8576b4cac81SBjoern A. Zeeb 	 */
8586b4cac81SBjoern A. Zeeb 	switch (state) {
8596b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
8606b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
8616b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
8626b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
8636b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
8646b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
8656b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
8666b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
8676b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
8686b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
8696b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
8706b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
8716b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
8726b4cac81SBjoern A. Zeeb 	default:
8736b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
8746b4cac81SBjoern A. Zeeb 	};
8756b4cac81SBjoern A. Zeeb 
8766b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
8776b4cac81SBjoern A. Zeeb }
8786b4cac81SBjoern A. Zeeb #endif
8796b4cac81SBjoern A. Zeeb 
8806b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
8816b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
8826b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
8836b4cac81SBjoern A. Zeeb {
8846b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
8856b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
8866b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
8876b4cac81SBjoern A. Zeeb 	int i, nchans;
8886b4cac81SBjoern A. Zeeb 
8896b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
8906b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
8916b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
8926b4cac81SBjoern A. Zeeb 		return (NULL);
8936b4cac81SBjoern A. Zeeb 
8946b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
8956b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
8966b4cac81SBjoern A. Zeeb 		return (NULL);
8976b4cac81SBjoern A. Zeeb 
8986b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
8996b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
9006b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
9016b4cac81SBjoern A. Zeeb 			return (&channels[i]);
9026b4cac81SBjoern A. Zeeb 	}
9036b4cac81SBjoern A. Zeeb 
9046b4cac81SBjoern A. Zeeb 	return (NULL);
9056b4cac81SBjoern A. Zeeb }
9066b4cac81SBjoern A. Zeeb 
9072ac8a218SBjoern A. Zeeb #if 0
9086b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9096b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
9106b4cac81SBjoern A. Zeeb {
9116b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
9126b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
9136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9146b4cac81SBjoern A. Zeeb 
9156b4cac81SBjoern A. Zeeb 	chan = NULL;
9166b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
9176b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
9186b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
9196b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
9206b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
9216b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
9226b4cac81SBjoern A. Zeeb 	else
9236b4cac81SBjoern A. Zeeb 		c = NULL;
9246b4cac81SBjoern A. Zeeb 
9256b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
9266b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
9276b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
9286b4cac81SBjoern A. Zeeb 	}
9296b4cac81SBjoern A. Zeeb 
9306b4cac81SBjoern A. Zeeb 	return (chan);
9316b4cac81SBjoern A. Zeeb }
9322ac8a218SBjoern A. Zeeb #endif
9336b4cac81SBjoern A. Zeeb 
9342e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
9352e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
9362e183d99SBjoern A. Zeeb {
9372e183d99SBjoern A. Zeeb 	enum nl80211_band band;
9382e183d99SBjoern A. Zeeb 
9392e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9402e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
9412e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
9422e183d99SBjoern A. Zeeb 		int i;
9432e183d99SBjoern A. Zeeb 
9442e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
9452e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
9462e183d99SBjoern A. Zeeb 			continue;
9472e183d99SBjoern A. Zeeb 
9482e183d99SBjoern A. Zeeb 		channels = supband->channels;
9492e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
9502e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
9512e183d99SBjoern A. Zeeb 				return (&channels[i]);
9522e183d99SBjoern A. Zeeb 		}
9532e183d99SBjoern A. Zeeb 	}
9542e183d99SBjoern A. Zeeb 
9552e183d99SBjoern A. Zeeb 	return (NULL);
9562e183d99SBjoern A. Zeeb }
9572e183d99SBjoern A. Zeeb 
958b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
9596b4cac81SBjoern A. Zeeb static int
96011db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
96111db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
96211db70b6SBjoern A. Zeeb {
96311db70b6SBjoern A. Zeeb 	int error;
96411db70b6SBjoern A. Zeeb 
96511db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
96611db70b6SBjoern A. Zeeb 		return (0);
96711db70b6SBjoern A. Zeeb 
96811db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
96911db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
97011db70b6SBjoern A. Zeeb 
97111db70b6SBjoern A. Zeeb 	error = 0;
97211db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
97311db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
97411db70b6SBjoern A. Zeeb 		int err;
97511db70b6SBjoern A. Zeeb 
97611db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
97711db70b6SBjoern A. Zeeb 			continue;
97811db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
97911db70b6SBjoern A. Zeeb 
98011db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
98111db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
98211db70b6SBjoern A. Zeeb 		if (err != 0) {
98311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
98411db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
98511db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
98611db70b6SBjoern A. Zeeb 			error++;
98711db70b6SBjoern A. Zeeb 
98811db70b6SBjoern A. Zeeb 			/*
98911db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
99011db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
99111db70b6SBjoern A. Zeeb 			 * crash (firmware).
99211db70b6SBjoern A. Zeeb 			 */
99311db70b6SBjoern A. Zeeb 			continue;
99411db70b6SBjoern A. Zeeb 		}
99511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
99611db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
99711db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
99811db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
99911db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
100011db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
100111db70b6SBjoern A. Zeeb #endif
100211db70b6SBjoern A. Zeeb 
100311db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
100411db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
100511db70b6SBjoern A. Zeeb 	}
100611db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
100711db70b6SBjoern A. Zeeb 	return (error);
100811db70b6SBjoern A. Zeeb }
100911db70b6SBjoern A. Zeeb 
101011db70b6SBjoern A. Zeeb static int
101111db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
10126b4cac81SBjoern A. Zeeb {
10136b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
10146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10156b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10166b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
101711db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10186b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10196b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
10206b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10216b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
102211db70b6SBjoern A. Zeeb 	struct ieee80211_node_table *nt;
10236b4cac81SBjoern A. Zeeb 	int error;
102411db70b6SBjoern A. Zeeb 	bool islocked;
10256b4cac81SBjoern A. Zeeb 
10266b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
10276b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
10286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10296b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10306b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10316b4cac81SBjoern A. Zeeb 
103211db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
103311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
103411db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
103511db70b6SBjoern A. Zeeb 		return (0);
103611db70b6SBjoern A. Zeeb 	}
103711db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
103811db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
103911db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
104011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
104111db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
104211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
104311db70b6SBjoern A. Zeeb 		return (0);
104411db70b6SBjoern A. Zeeb 	}
104511db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
104611db70b6SBjoern A. Zeeb 
104711db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
104811db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
104911db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
105011db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
105111db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
105211db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
105311db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
105411db70b6SBjoern A. Zeeb #endif
105511db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
105611db70b6SBjoern A. Zeeb 		return (1);
105711db70b6SBjoern A. Zeeb 	}
105811db70b6SBjoern A. Zeeb 
105911db70b6SBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
106011db70b6SBjoern A. Zeeb 	nt = &ic->ic_sta;
106111db70b6SBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
106211db70b6SBjoern A. Zeeb 	if (islocked)
106311db70b6SBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
106411db70b6SBjoern A. Zeeb 
106511db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
106611db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
106711db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
106811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
106911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
107011db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
107111db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
107211db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
107311db70b6SBjoern A. Zeeb #endif
107411db70b6SBjoern A. Zeeb 		error = 1;
107511db70b6SBjoern A. Zeeb 		goto out;
107611db70b6SBjoern A. Zeeb 	}
107711db70b6SBjoern A. Zeeb 
107811db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
107911db70b6SBjoern A. Zeeb 	if (error != 0) {
108011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
108111db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
108211db70b6SBjoern A. Zeeb 		error = 0;
108311db70b6SBjoern A. Zeeb 		goto out;
108411db70b6SBjoern A. Zeeb 	}
108511db70b6SBjoern A. Zeeb 
108611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
108711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
108811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
108911db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
109011db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
109111db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
109211db70b6SBjoern A. Zeeb #endif
109311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
109411db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
109511db70b6SBjoern A. Zeeb 	error = 1;
109611db70b6SBjoern A. Zeeb out:
109711db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
109811db70b6SBjoern A. Zeeb 	if (islocked)
109911db70b6SBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
110011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
110111db70b6SBjoern A. Zeeb 	return (error);
110211db70b6SBjoern A. Zeeb }
110311db70b6SBjoern A. Zeeb 
110411db70b6SBjoern A. Zeeb static int
110511db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
110611db70b6SBjoern A. Zeeb {
110711db70b6SBjoern A. Zeeb 
110811db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
110911db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
111011db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
111111db70b6SBjoern A. Zeeb }
111211db70b6SBjoern A. Zeeb 
111311db70b6SBjoern A. Zeeb static int
111411db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
111511db70b6SBjoern A. Zeeb {
111611db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
111711db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
111811db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
111911db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
112011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
112111db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
112211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
112311db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
112411db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
112511db70b6SBjoern A. Zeeb 	uint32_t lcipher;
112611db70b6SBjoern A. Zeeb 	int error;
112711db70b6SBjoern A. Zeeb 
112811db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
112911db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
113011db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
113111db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
113211db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
113311db70b6SBjoern A. Zeeb 
113411db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
113511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
113611db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
113711db70b6SBjoern A. Zeeb 		return (0);
113811db70b6SBjoern A. Zeeb 	}
113911db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
114011db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
114111db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
114211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
114311db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
114411db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
114511db70b6SBjoern A. Zeeb 		return (0);
114611db70b6SBjoern A. Zeeb 	}
114711db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
114811db70b6SBjoern A. Zeeb 
114911db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
115011db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
115111db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
115211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
115311db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
115411db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
115511db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
115611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
115711db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
115811db70b6SBjoern A. Zeeb 	}
115911db70b6SBjoern A. Zeeb 
116011db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
11616b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
116211db70b6SBjoern A. Zeeb 	switch (lcipher) {
116311db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
116411db70b6SBjoern A. Zeeb 		break;
116511db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
116611db70b6SBjoern A. Zeeb 	default:
116712a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
116812a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
116911db70b6SBjoern A. Zeeb 		IMPROVE();
117011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
117111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
117211db70b6SBjoern A. Zeeb 		return (0);
117311db70b6SBjoern A. Zeeb 	}
117411db70b6SBjoern A. Zeeb 
117511db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
117611db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
117711db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
117811db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
11796b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
11806b4cac81SBjoern A. Zeeb #if 0
11816b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
11826b4cac81SBjoern A. Zeeb #endif
11836b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
11846b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
11856b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
11866b4cac81SBjoern A. Zeeb 
118711db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
118811db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
118911db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
119011db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
119111db70b6SBjoern A. Zeeb 
11926b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
11936b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
11946b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
11956b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
11966b4cac81SBjoern A. Zeeb 		break;
11976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
11986b4cac81SBjoern A. Zeeb 	default:
119911db70b6SBjoern A. Zeeb 		/* currently UNREACH */
12006b4cac81SBjoern A. Zeeb 		IMPROVE();
120111db70b6SBjoern A. Zeeb 		break;
12026b4cac81SBjoern A. Zeeb 	};
120311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
12046b4cac81SBjoern A. Zeeb 
120511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
120611db70b6SBjoern A. Zeeb 	if (error != 0) {
120711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
120811db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
120911db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
121011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
121111db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
121211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
121311db70b6SBjoern A. Zeeb 		return (0);
12146b4cac81SBjoern A. Zeeb 	}
12156b4cac81SBjoern A. Zeeb 
121611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
121711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
121811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
121911db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
122011db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
122111db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
122211db70b6SBjoern A. Zeeb #endif
122311db70b6SBjoern A. Zeeb 
122411db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
122511db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
12266b4cac81SBjoern A. Zeeb 	return (1);
12276b4cac81SBjoern A. Zeeb }
12286b4cac81SBjoern A. Zeeb 
12296b4cac81SBjoern A. Zeeb static  int
12306b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
12316b4cac81SBjoern A. Zeeb {
12326b4cac81SBjoern A. Zeeb 
123311db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
12346b4cac81SBjoern A. Zeeb }
12356b4cac81SBjoern A. Zeeb #endif
12366b4cac81SBjoern A. Zeeb 
12376b4cac81SBjoern A. Zeeb static u_int
12386b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
12396b4cac81SBjoern A. Zeeb {
12406b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
12416b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
12426b4cac81SBjoern A. Zeeb 
12436b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
12446b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
12456b4cac81SBjoern A. Zeeb 
12466b4cac81SBjoern A. Zeeb 	mc_list = arg;
12476b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
12486b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
12496b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
12506b4cac81SBjoern A. Zeeb 			return (0);
12516b4cac81SBjoern A. Zeeb 	}
12526b4cac81SBjoern A. Zeeb 
12536b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
12546b4cac81SBjoern A. Zeeb 	if (addr == NULL)
12556b4cac81SBjoern A. Zeeb 		return (0);
12566b4cac81SBjoern A. Zeeb 
12576b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
12586b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
12596b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
12606b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
12616b4cac81SBjoern A. Zeeb 	mc_list->count++;
12626b4cac81SBjoern A. Zeeb 
12639d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
12649d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
12656b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
12666b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
12679d9ba2b7SBjoern A. Zeeb #endif
12686b4cac81SBjoern A. Zeeb 
12696b4cac81SBjoern A. Zeeb 	return (1);
12706b4cac81SBjoern A. Zeeb }
12716b4cac81SBjoern A. Zeeb 
12726b4cac81SBjoern A. Zeeb static void
12736b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
12746b4cac81SBjoern A. Zeeb {
12756b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12766b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12776b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
12786b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
12796b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
12806b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
12816b4cac81SBjoern A. Zeeb 	u64 mc;
12826b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
12836b4cac81SBjoern A. Zeeb 
12846b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
12856b4cac81SBjoern A. Zeeb 
12866b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
12876b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
12886b4cac81SBjoern A. Zeeb 		return;
12896b4cac81SBjoern A. Zeeb 
12906b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
12916b4cac81SBjoern A. Zeeb 		return;
12926b4cac81SBjoern A. Zeeb 
12936b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
12946b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
12956b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
12966b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
12976b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
12986b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
12996b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
13006b4cac81SBjoern A. Zeeb 	} else {
13016b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
13026b4cac81SBjoern A. Zeeb 	}
13036b4cac81SBjoern A. Zeeb 
13046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13056b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
13066b4cac81SBjoern A. Zeeb 	/*
13076b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
13086b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
13096b4cac81SBjoern A. Zeeb 	 */
13106b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
13116b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
13126b4cac81SBjoern A. Zeeb 
13139d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13149d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13156b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
13166b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
13179d9ba2b7SBjoern A. Zeeb #endif
13186b4cac81SBjoern A. Zeeb 
13196b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
13206b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
13216b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
13226b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
13236b4cac81SBjoern A. Zeeb 			mc_list.count--;
13246b4cac81SBjoern A. Zeeb 		}
13256b4cac81SBjoern A. Zeeb 	}
13266b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
13276b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
13286b4cac81SBjoern A. Zeeb }
13296b4cac81SBjoern A. Zeeb 
1330fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1331fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1332fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1333fa8f007dSBjoern A. Zeeb {
1334fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1335fa8f007dSBjoern A. Zeeb 
1336fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1337fa8f007dSBjoern A. Zeeb 
13389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13399d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1340fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1341fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1342ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1343fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1344616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1345fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1346fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1347fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1348fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1349ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
13509d9ba2b7SBjoern A. Zeeb #endif
1351fa8f007dSBjoern A. Zeeb 
1352fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1353fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1354fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1355fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1356fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1357fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1358fa8f007dSBjoern A. Zeeb 	}
1359fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1360fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1361fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1362fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1363fa8f007dSBjoern A. Zeeb 	}
1364fa8f007dSBjoern A. Zeeb 
1365fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1366fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1367fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1368fa8f007dSBjoern A. Zeeb 
13699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1371fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1372fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1373ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1374fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1375616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1376fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1377fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1378fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1379fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1380ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
13819d9ba2b7SBjoern A. Zeeb #endif
1382fa8f007dSBjoern A. Zeeb 
1383fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1384fa8f007dSBjoern A. Zeeb }
1385fa8f007dSBjoern A. Zeeb 
13866b4cac81SBjoern A. Zeeb static void
13876b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
13886b4cac81SBjoern A. Zeeb {
13896b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13906b4cac81SBjoern A. Zeeb 	int error;
13918ac540d3SBjoern A. Zeeb 	bool cancel;
13926b4cac81SBjoern A. Zeeb 
13938ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
13948ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
13958ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
13968ac540d3SBjoern A. Zeeb 	if (!cancel)
13976b4cac81SBjoern A. Zeeb 		return;
13986b4cac81SBjoern A. Zeeb 
13996b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14006b4cac81SBjoern A. Zeeb 
1401bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1402bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14036b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
14046b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
14058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14066b4cac81SBjoern A. Zeeb 
14076b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
14088ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
14098ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
14108ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
14118ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
14128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
14138ac540d3SBjoern A. Zeeb 
1414bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
14156b4cac81SBjoern A. Zeeb 
14168ac540d3SBjoern A. Zeeb 	if (cancel)
14176b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
14186b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
14196b4cac81SBjoern A. Zeeb }
14206b4cac81SBjoern A. Zeeb 
14216b4cac81SBjoern A. Zeeb static void
1422086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1423086be6a8SBjoern A. Zeeb {
1424086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1425086be6a8SBjoern A. Zeeb 	int error;
1426086be6a8SBjoern A. Zeeb 	bool old;
1427086be6a8SBjoern A. Zeeb 
1428086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1429086be6a8SBjoern A. Zeeb 	if (old == new)
1430086be6a8SBjoern A. Zeeb 		return;
1431086be6a8SBjoern A. Zeeb 
1432086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1433086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1434086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1435086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1436086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1437086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1438086be6a8SBjoern A. Zeeb 	}
1439086be6a8SBjoern A. Zeeb }
1440086be6a8SBjoern A. Zeeb 
14415a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
14426b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
14436b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
14446b4cac81SBjoern A. Zeeb {
14455a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
14465a4d2461SBjoern A. Zeeb 
14475a4d2461SBjoern A. Zeeb 	changed = 0;
14486b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1449616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
14506b4cac81SBjoern A. Zeeb 
14516b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
14526b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
14536b4cac81SBjoern A. Zeeb 
1454616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1455616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
14566b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
14576b4cac81SBjoern A. Zeeb 		IMPROVE();
1458086be6a8SBjoern A. Zeeb 
14595a4d2461SBjoern A. Zeeb 		/*
14605a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
14615a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
14625a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
14635a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
14645a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
14655a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
14665a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
14675a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
14685a4d2461SBjoern A. Zeeb 		 */
14696b4cac81SBjoern A. Zeeb 	}
14705a4d2461SBjoern A. Zeeb 
14715a4d2461SBjoern A. Zeeb 	return (changed);
14726b4cac81SBjoern A. Zeeb }
14736b4cac81SBjoern A. Zeeb 
14746b4cac81SBjoern A. Zeeb static void
14756b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
14766b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
14776b4cac81SBjoern A. Zeeb {
14786b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
14796b4cac81SBjoern A. Zeeb 	int tid;
1480eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
14816b4cac81SBjoern A. Zeeb 
14826b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
14836b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
14846b4cac81SBjoern A. Zeeb 
14856b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
14866b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
14876b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
14886b4cac81SBjoern A. Zeeb 				continue;
14896b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
14906b4cac81SBjoern A. Zeeb 			continue;
14916b4cac81SBjoern A. Zeeb 
14926b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
14936b4cac81SBjoern A. Zeeb 			continue;
14946b4cac81SBjoern A. Zeeb 
14956b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
14966b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
14976b4cac81SBjoern A. Zeeb 			continue;
14986b4cac81SBjoern A. Zeeb 
1499eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1500eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1501eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1502eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
15036b4cac81SBjoern A. Zeeb 			continue;
15046b4cac81SBjoern A. Zeeb 
15056b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
15066b4cac81SBjoern A. Zeeb 	}
15076b4cac81SBjoern A. Zeeb }
15086b4cac81SBjoern A. Zeeb 
150988665349SBjoern A. Zeeb /*
151088665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
151188665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
151288665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
151388665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
151488665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
151588665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
151688665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
151788665349SBjoern A. Zeeb  * re-used.
151888665349SBjoern A. Zeeb  */
151988665349SBjoern A. Zeeb static void
152088665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
152188665349SBjoern A. Zeeb {
152288665349SBjoern A. Zeeb 	struct mbufq mq;
152388665349SBjoern A. Zeeb 	struct mbuf *m;
152488665349SBjoern A. Zeeb 	int len;
152588665349SBjoern A. Zeeb 
152688665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
152788665349SBjoern A. Zeeb 
152888665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
152988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
153088665349SBjoern A. Zeeb 	lsta->txq_ready = false;
153188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
153288665349SBjoern A. Zeeb 
153388665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
153488665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
153588665349SBjoern A. Zeeb 
153688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
153788665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
153888665349SBjoern A. Zeeb 	if (len <= 0) {
153988665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
154088665349SBjoern A. Zeeb 		return;
154188665349SBjoern A. Zeeb 	}
154288665349SBjoern A. Zeeb 
154388665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
154488665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
154588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
154688665349SBjoern A. Zeeb 
154788665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
154888665349SBjoern A. Zeeb 	while (m != NULL) {
154988665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
155088665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
155188665349SBjoern A. Zeeb 	}
155288665349SBjoern A. Zeeb }
155388665349SBjoern A. Zeeb 
15546b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
15556b4cac81SBjoern A. Zeeb 
15566b4cac81SBjoern A. Zeeb static int
15576b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15586b4cac81SBjoern A. Zeeb {
15596b4cac81SBjoern A. Zeeb 
15606b4cac81SBjoern A. Zeeb 	return (0);
15616b4cac81SBjoern A. Zeeb }
15626b4cac81SBjoern A. Zeeb 
15636b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
15646b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
15656b4cac81SBjoern A. Zeeb 
15666b4cac81SBjoern A. Zeeb static int
15676b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15686b4cac81SBjoern A. Zeeb {
15696b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1570c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1571d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
15726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15736b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15746b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15756b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15766b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
15776b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15786b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
15796b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
15806b4cac81SBjoern A. Zeeb 	uint32_t changed;
15816b4cac81SBjoern A. Zeeb 	int error;
15826b4cac81SBjoern A. Zeeb 
15832ac8a218SBjoern A. Zeeb 	/*
15842ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
15852ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
15862ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
15872ac8a218SBjoern A. Zeeb 	 */
15882ac8a218SBjoern A. Zeeb 
15892ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
15902ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
15912ac8a218SBjoern A. Zeeb 		return (EINVAL);
15922ac8a218SBjoern A. Zeeb 	}
15930936c648SBjoern A. Zeeb 	/*
15940936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
15950936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
15960936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
15970936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
15980936c648SBjoern A. Zeeb 	 */
15992ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
16002ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
16012ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
16022ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
16030936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16042ac8a218SBjoern A. Zeeb 		return (EINVAL);
16056b4cac81SBjoern A. Zeeb 	}
16066b4cac81SBjoern A. Zeeb 
16076b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16082ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
16092ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
16102ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
16112ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
16120936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16132ac8a218SBjoern A. Zeeb 		return (ESRCH);
16142ac8a218SBjoern A. Zeeb 	}
16152ac8a218SBjoern A. Zeeb 
16166b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16176b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16186b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16196b4cac81SBjoern A. Zeeb 
16200936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16210936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
16220936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
16230936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16240936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16250936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16260936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16270936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
162892690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
162992690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16300936c648SBjoern A. Zeeb 		return (EBUSY);
16310936c648SBjoern A. Zeeb 	}
16320936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16330936c648SBjoern A. Zeeb 
16346b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16366b4cac81SBjoern A. Zeeb 
16376b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
16386b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1639d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1640d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
16416b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
16426b4cac81SBjoern A. Zeeb 	} else {
16436b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1644c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
16456b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1646d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
16476b4cac81SBjoern A. Zeeb 	}
16486b4cac81SBjoern A. Zeeb 
1649d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1650d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1651d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
16526b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1653d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1654d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1655d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1656d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
16579fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
16582ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
16592ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
16609fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
16619fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
16629fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1663d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
16649fb91463SBjoern A. Zeeb 		} else
1665d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
16669fb91463SBjoern A. Zeeb 	}
16679fb91463SBjoern A. Zeeb #endif
16689fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
16699fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
16709fb91463SBjoern A. Zeeb #ifdef __notyet__
16719fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1672d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1673d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
16749fb91463SBjoern A. Zeeb 		} else
16759fb91463SBjoern A. Zeeb #endif
16769fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1677d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
16789fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1679d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
16809fb91463SBjoern A. Zeeb 	}
16819fb91463SBjoern A. Zeeb #endif
16826b4cac81SBjoern A. Zeeb 	/* Responder ... */
1683d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1684d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1685d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1686d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
16879fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
16886b4cac81SBjoern A. Zeeb 
16896ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
16906ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
16916ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
16926ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
16936ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
16946ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
16956ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
16966ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
16976ffb7bd4SBjoern A. Zeeb 
16986ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
16996ffb7bd4SBjoern A. Zeeb 
17006ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
17016ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
17026ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
17036ffb7bd4SBjoern A. Zeeb 
17046ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
17056ffb7bd4SBjoern A. Zeeb 
17066b4cac81SBjoern A. Zeeb 	error = 0;
17076b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
17086b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
17096b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
17106b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
17116b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1712d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
17136b4cac81SBjoern A. Zeeb 	} else {
1714d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
17156b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
17167b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
17177b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
17187b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1719d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
17209fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
1721943a19c6SBjoern A. Zeeb 			if (vif->bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_40) {
17229fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
17239fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1724943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 += 10;
17259fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1726943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 -= 10;
17279fb91463SBjoern A. Zeeb 			}
17289fb91463SBjoern A. Zeeb #endif
17297b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1730d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
17316b4cac81SBjoern A. Zeeb 		} else {
1732018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1733018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
17346b4cac81SBjoern A. Zeeb 			goto out;
17356b4cac81SBjoern A. Zeeb 		}
17366ffb7bd4SBjoern A. Zeeb 
1737d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
17386ffb7bd4SBjoern A. Zeeb 
17396b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
17406b4cac81SBjoern A. Zeeb 		if (error == 0)
174168541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1742d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
17436b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
17446b4cac81SBjoern A. Zeeb 			error = 0;
17456b4cac81SBjoern A. Zeeb 		if (error != 0) {
1746018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1747018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1748d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1749d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1750c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
17516b4cac81SBjoern A. Zeeb 			goto out;
17526b4cac81SBjoern A. Zeeb 		}
17536b4cac81SBjoern A. Zeeb 	}
17546b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
17556b4cac81SBjoern A. Zeeb 
17566b4cac81SBjoern A. Zeeb 	/* RATES */
17576b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
17586b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
17596b4cac81SBjoern A. Zeeb 
1760d9f59799SBjoern A. Zeeb 	/*
17610936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
17620936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
17630936c648SBjoern A. Zeeb 	 * under the hoods.
1764d9f59799SBjoern A. Zeeb 	 */
17650936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
17660936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
17676b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1768e24e8103SBjoern A. Zeeb 
176988665349SBjoern A. Zeeb 	/*
177088665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
177188665349SBjoern A. Zeeb 	 * that we can tx again.
177288665349SBjoern A. Zeeb 	 */
177388665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
177488665349SBjoern A. Zeeb 	lsta->txq_ready = true;
177588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
177688665349SBjoern A. Zeeb 
1777a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
17782ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1779a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1780a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1781e24e8103SBjoern A. Zeeb 
1782d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
17836b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
17846b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
17856b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1786e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
17876b4cac81SBjoern A. Zeeb 	if (error != 0) {
17886b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1789018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1790018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
17916b4cac81SBjoern A. Zeeb 		goto out;
17926b4cac81SBjoern A. Zeeb 	}
17936b4cac81SBjoern A. Zeeb #if 0
17946b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
17956b4cac81SBjoern A. Zeeb #endif
17966b4cac81SBjoern A. Zeeb 
17979d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
17989d9ba2b7SBjoern A. Zeeb 
17991665ef97SBjoern A. Zeeb #if 0
18006b4cac81SBjoern A. Zeeb 	/*
18016b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
18026b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
18036b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
18046b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1805d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1806d9f59799SBjoern A. Zeeb 	 * for all queues.
18076b4cac81SBjoern A. Zeeb 	 */
1808e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
18091665ef97SBjoern A. Zeeb #endif
18106b4cac81SBjoern A. Zeeb 
18116b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
18126b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18136b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
18146b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
18156b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
18166b4cac81SBjoern A. Zeeb 
18176b4cac81SBjoern A. Zeeb 	/*
18186b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
18196b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
18206b4cac81SBjoern A. Zeeb 	 * - event_callback
18216b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
18226b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
18236b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
18246b4cac81SBjoern A. Zeeb 	 */
18256b4cac81SBjoern A. Zeeb 
1826105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1827105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1828105b9df2SBjoern A. Zeeb 
1829105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1830105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1831105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1832105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1833105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1834105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1835105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1836105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1837105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1838105b9df2SBjoern A. Zeeb 
1839105b9df2SBjoern A. Zeeb 	/*
1840105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1841105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1842105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1843105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1844105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1845105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1846105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1847105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1848105b9df2SBjoern A. Zeeb 	 */
1849105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1850105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1851105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1852105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1853105b9df2SBjoern A. Zeeb 	} else {
1854105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1855105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1856105b9df2SBjoern A. Zeeb 		/*
1857105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1858105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1859105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1860105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1861105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1862105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1863105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1864105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1865105b9df2SBjoern A. Zeeb 		 */
1866105b9df2SBjoern A. Zeeb 	}
1867105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1868105b9df2SBjoern A. Zeeb 	goto out_relocked;
1869105b9df2SBjoern A. Zeeb 
18706b4cac81SBjoern A. Zeeb out:
18718ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
18726b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1873105b9df2SBjoern A. Zeeb out_relocked:
18740936c648SBjoern A. Zeeb 	/*
1875105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
18760936c648SBjoern A. Zeeb 	 * during the work of this function.
18770936c648SBjoern A. Zeeb 	 */
18786b4cac81SBjoern A. Zeeb 	if (ni != NULL)
18796b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
18806b4cac81SBjoern A. Zeeb 	return (error);
18816b4cac81SBjoern A. Zeeb }
18826b4cac81SBjoern A. Zeeb 
18836b4cac81SBjoern A. Zeeb static int
18846b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18856b4cac81SBjoern A. Zeeb {
18866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
18876b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
18886b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
18896b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18906b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
18916b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
18926b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
18936b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
18946b4cac81SBjoern A. Zeeb 	int error;
18956b4cac81SBjoern A. Zeeb 
18966b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
18976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
18986b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
18996b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19006b4cac81SBjoern A. Zeeb 
19012ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19022ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19032ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
19042ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
19052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19062ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19072ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19082ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19092ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19102ac8a218SBjoern A. Zeeb #endif
19112ac8a218SBjoern A. Zeeb 
19122ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19132ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19142ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19152ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19162ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19172ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
19186b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19196b4cac81SBjoern A. Zeeb 
192067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19216b4cac81SBjoern A. Zeeb 
19226b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
19238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19246b4cac81SBjoern A. Zeeb 
1925d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1926d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1927d9f59799SBjoern A. Zeeb 
19286b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
192988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
19306b4cac81SBjoern A. Zeeb 
19316b4cac81SBjoern A. Zeeb 	/* flush, no drop */
19326b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
19336b4cac81SBjoern A. Zeeb 
19346b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
19356b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
19366b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19376b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
19386b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
19396b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
19406b4cac81SBjoern A. Zeeb 	}
19416b4cac81SBjoern A. Zeeb 
19426b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
19436b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
19446b4cac81SBjoern A. Zeeb 
19456b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
19466b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1947d9f59799SBjoern A. Zeeb 
1948d9f59799SBjoern A. Zeeb 	/* Take the station down. */
19496b4cac81SBjoern A. Zeeb 
19506b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
19516b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
19526b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1953d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1954e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
19556b4cac81SBjoern A. Zeeb 	if (error != 0) {
19566b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1957018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1958018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19596b4cac81SBjoern A. Zeeb 		goto out;
19606b4cac81SBjoern A. Zeeb 	}
19616b4cac81SBjoern A. Zeeb #if 0
19626b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
19636b4cac81SBjoern A. Zeeb #endif
19646b4cac81SBjoern A. Zeeb 
196567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19666b4cac81SBjoern A. Zeeb 
19672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19682ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
19692ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
19702ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
19712ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1972d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
19730936c648SBjoern A. Zeeb 	/*
19740936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
19750936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
19760936c648SBjoern A. Zeeb 	 * and potentially freed.
19770936c648SBjoern A. Zeeb 	 */
19780936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1979d9f59799SBjoern A. Zeeb 
1980d9f59799SBjoern A. Zeeb 	/* conf_tx */
1981d9f59799SBjoern A. Zeeb 
1982d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
19836b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1984c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
1985d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
19866b4cac81SBjoern A. Zeeb 
1987d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
19886b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
198968541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
19906b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
19916b4cac81SBjoern A. Zeeb 
19925a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
19935a4d2461SBjoern A. Zeeb 
19946b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
1995d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1996d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1997c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
19986b4cac81SBjoern A. Zeeb 	}
19996b4cac81SBjoern A. Zeeb 
20006b4cac81SBjoern A. Zeeb out:
20018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20026b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
20036b4cac81SBjoern A. Zeeb 	return (error);
20046b4cac81SBjoern A. Zeeb }
20056b4cac81SBjoern A. Zeeb 
20066b4cac81SBjoern A. Zeeb static int
20076b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20086b4cac81SBjoern A. Zeeb {
20096b4cac81SBjoern A. Zeeb 	int error;
20106b4cac81SBjoern A. Zeeb 
20116b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
20126b4cac81SBjoern A. Zeeb 	if (error == 0)
20136b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
20146b4cac81SBjoern A. Zeeb 	return (error);
20156b4cac81SBjoern A. Zeeb }
20166b4cac81SBjoern A. Zeeb 
20176b4cac81SBjoern A. Zeeb static int
20186b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20196b4cac81SBjoern A. Zeeb {
20206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20216b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20226b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20236b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20246b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20256b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
20266b4cac81SBjoern A. Zeeb 	int error;
20276b4cac81SBjoern A. Zeeb 
20286b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20296b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20306b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20316b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20326b4cac81SBjoern A. Zeeb 
20336b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
20352ac8a218SBjoern A. Zeeb 
20362ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20372ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
20382ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
20392ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20402ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20412ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20422ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20432ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20442ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20452ac8a218SBjoern A. Zeeb #endif
20462ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
20472ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
20482ac8a218SBjoern A. Zeeb 		goto out;
20492ac8a218SBjoern A. Zeeb 	}
20502ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20512ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20522ac8a218SBjoern A. Zeeb 
20532ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
20546b4cac81SBjoern A. Zeeb 
20556b4cac81SBjoern A. Zeeb 	/* Finish auth. */
20566b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
20576b4cac81SBjoern A. Zeeb 
20586b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
20596b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
20606b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2061e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2062018d93ecSBjoern A. Zeeb 	if (error != 0) {
2063018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2064018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20656b4cac81SBjoern A. Zeeb 		goto out;
2066018d93ecSBjoern A. Zeeb 	}
20676b4cac81SBjoern A. Zeeb 
20686b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20696b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20706b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20716b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
20726b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20736b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20746b4cac81SBjoern A. Zeeb 	}
20756b4cac81SBjoern A. Zeeb 
20766b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
20776b4cac81SBjoern A. Zeeb 
20786b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
20796b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
20806b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20816b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
20826b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
20836b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
20846b4cac81SBjoern A. Zeeb 	}
20856b4cac81SBjoern A. Zeeb 
20866b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
208788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
20886b4cac81SBjoern A. Zeeb 
20896b4cac81SBjoern A. Zeeb 	/*
20906b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
20916b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
20926b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
20936b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
20946b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
20956b4cac81SBjoern A. Zeeb 	 * - event_callback
20966b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
20976b4cac81SBjoern A. Zeeb 	 */
20986b4cac81SBjoern A. Zeeb 
20996b4cac81SBjoern A. Zeeb out:
21008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21016b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21026b4cac81SBjoern A. Zeeb 	return (error);
21036b4cac81SBjoern A. Zeeb }
21046b4cac81SBjoern A. Zeeb 
21056b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
21066b4cac81SBjoern A. Zeeb static int
21076b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21086b4cac81SBjoern A. Zeeb {
21096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21116b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21126b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21136b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21146b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21152ac8a218SBjoern A. Zeeb 	int error;
21166b4cac81SBjoern A. Zeeb 
21176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21216b4cac81SBjoern A. Zeeb 
21226b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21242ac8a218SBjoern A. Zeeb 
21252ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21262ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21272ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21282ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21292ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21302ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21312ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21322ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21332ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21342ac8a218SBjoern A. Zeeb #endif
21352ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21362ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21372ac8a218SBjoern A. Zeeb 		goto out;
21382ac8a218SBjoern A. Zeeb 	}
21392ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21402ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21412ac8a218SBjoern A. Zeeb 
21422ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
21432ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
21446b4cac81SBjoern A. Zeeb 
21456b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
21466b4cac81SBjoern A. Zeeb 
21476b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21486b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21496b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21506b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
21516b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21526b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21536b4cac81SBjoern A. Zeeb 	}
21546b4cac81SBjoern A. Zeeb 
21556b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
21566b4cac81SBjoern A. Zeeb 
21576b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21586b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21596b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21606b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21616b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21626b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21636b4cac81SBjoern A. Zeeb 	}
21646b4cac81SBjoern A. Zeeb 
21652ac8a218SBjoern A. Zeeb 	error = 0;
21662ac8a218SBjoern A. Zeeb out:
21678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21686b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21696b4cac81SBjoern A. Zeeb 
21702ac8a218SBjoern A. Zeeb 	return (error);
21716b4cac81SBjoern A. Zeeb }
21726b4cac81SBjoern A. Zeeb 
21736b4cac81SBjoern A. Zeeb static int
2174d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21756b4cac81SBjoern A. Zeeb {
21766b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21806b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
21816b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21826b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
21836b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2184d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
21856b4cac81SBjoern A. Zeeb 	int error;
21866b4cac81SBjoern A. Zeeb 
21876b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21896b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21906b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21916b4cac81SBjoern A. Zeeb 
21922ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21932ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21942ac8a218SBjoern A. Zeeb 
21952ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21962ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21972ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
21982ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
21992ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22002ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22012ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22022ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22032ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22042ac8a218SBjoern A. Zeeb #endif
22052ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22062ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22072ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22082ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22092ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22102ac8a218SBjoern A. Zeeb 
22112ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
22126b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22136b4cac81SBjoern A. Zeeb 
221467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2215d9f59799SBjoern A. Zeeb 
2216d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2217d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2218d9f59799SBjoern A. Zeeb 
2219d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2220d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2221d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2222d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2223d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2224ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2225d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2226d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2227d9f59799SBjoern A. Zeeb 	}
2228d9f59799SBjoern A. Zeeb 
22298ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2230d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2231d9f59799SBjoern A. Zeeb 
223288665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2233d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2234018d93ecSBjoern A. Zeeb 	if (error != 0) {
2235018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2236018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2237d9f59799SBjoern A. Zeeb 		goto outni;
2238018d93ecSBjoern A. Zeeb 	}
2239d9f59799SBjoern A. Zeeb 
2240d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
224188665349SBjoern A. Zeeb 
224288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
224388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
224488665349SBjoern A. Zeeb 
22458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2246d9f59799SBjoern A. Zeeb 
224767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2248d9f59799SBjoern A. Zeeb 
2249d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
225088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2251d9f59799SBjoern A. Zeeb 
2252d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2253d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2254d9f59799SBjoern A. Zeeb 
22556b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22566b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22576b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22586b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2259ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
22606b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22616b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22626b4cac81SBjoern A. Zeeb 	}
22636b4cac81SBjoern A. Zeeb 
2264d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2265d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2266d9f59799SBjoern A. Zeeb 
2267d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2268d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2269d9f59799SBjoern A. Zeeb 
2270d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2271d9f59799SBjoern A. Zeeb 
22726b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
22736b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22746b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
22756b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2276e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2277018d93ecSBjoern A. Zeeb 	if (error != 0) {
2278018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2279018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22806b4cac81SBjoern A. Zeeb 		goto out;
2281018d93ecSBjoern A. Zeeb 	}
22826b4cac81SBjoern A. Zeeb 
22835a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
22845a4d2461SBjoern A. Zeeb 	bss_changed = 0;
22855a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
22866b4cac81SBjoern A. Zeeb 
22875a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
228816e688b2SBjoern A. Zeeb 
2289d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2290d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2291d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2292d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2293e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2294d9f59799SBjoern A. Zeeb 	if (error != 0) {
2295d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2296018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2297018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2298d9f59799SBjoern A. Zeeb 		goto out;
2299d9f59799SBjoern A. Zeeb 	}
2300d9f59799SBjoern A. Zeeb 
230116e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2302d9f59799SBjoern A. Zeeb 
2303d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2304d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2305d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2306616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2307616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2308d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2309d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2310d9f59799SBjoern A. Zeeb 
23112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23122ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23132ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23142ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2316d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23170936c648SBjoern A. Zeeb 	/*
23180936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23190936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23200936c648SBjoern A. Zeeb 	 * and potentially freed.
23210936c648SBjoern A. Zeeb 	 */
23220936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2323d9f59799SBjoern A. Zeeb 
2324d9f59799SBjoern A. Zeeb 	/* conf_tx */
2325d9f59799SBjoern A. Zeeb 
2326d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2327d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2328c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2329d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2330d9f59799SBjoern A. Zeeb 
2331d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2332d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
233368541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2334d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2335d9f59799SBjoern A. Zeeb 
23365a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
23375a4d2461SBjoern A. Zeeb 
2338d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2339d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2340d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2341c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2342d9f59799SBjoern A. Zeeb 	}
2343d9f59799SBjoern A. Zeeb 
2344d9f59799SBjoern A. Zeeb 	error = EALREADY;
23456b4cac81SBjoern A. Zeeb out:
23468ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
23476b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2348d9f59799SBjoern A. Zeeb outni:
23496b4cac81SBjoern A. Zeeb 	return (error);
23506b4cac81SBjoern A. Zeeb }
23516b4cac81SBjoern A. Zeeb 
23526b4cac81SBjoern A. Zeeb static int
2353d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2354d9f59799SBjoern A. Zeeb {
2355d9f59799SBjoern A. Zeeb 	int error;
2356d9f59799SBjoern A. Zeeb 
2357d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2358d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2359d9f59799SBjoern A. Zeeb 		return (error);
2360d9f59799SBjoern A. Zeeb 
2361d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2362d9f59799SBjoern A. Zeeb 
2363d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2364d9f59799SBjoern A. Zeeb 	return (error);
2365d9f59799SBjoern A. Zeeb }
2366d9f59799SBjoern A. Zeeb 
2367d9f59799SBjoern A. Zeeb static int
23686b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23696b4cac81SBjoern A. Zeeb {
23706b4cac81SBjoern A. Zeeb 	int error;
23716b4cac81SBjoern A. Zeeb 
2372d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
23736b4cac81SBjoern A. Zeeb 	return (error);
23746b4cac81SBjoern A. Zeeb }
23756b4cac81SBjoern A. Zeeb 
23766b4cac81SBjoern A. Zeeb static int
23776b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23786b4cac81SBjoern A. Zeeb {
23796b4cac81SBjoern A. Zeeb 	int error;
23806b4cac81SBjoern A. Zeeb 
2381d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
23826b4cac81SBjoern A. Zeeb 	return (error);
23836b4cac81SBjoern A. Zeeb }
23846b4cac81SBjoern A. Zeeb 
23856b4cac81SBjoern A. Zeeb static int
23866b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23876b4cac81SBjoern A. Zeeb {
23886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23896b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23906b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23916b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23926b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
23936b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23946b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
23956b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23966b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
23976b4cac81SBjoern A. Zeeb 	int error;
23986b4cac81SBjoern A. Zeeb 
23996b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24006b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24016b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24026b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24036b4cac81SBjoern A. Zeeb 
24046b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
24058ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
24062ac8a218SBjoern A. Zeeb 
24072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24082ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24092ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24102ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24112ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24122ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24132ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24142ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24152ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24162ac8a218SBjoern A. Zeeb #endif
24172ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24182ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24192ac8a218SBjoern A. Zeeb 		goto out;
24202ac8a218SBjoern A. Zeeb 	}
24212ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24222ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24232ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
24242ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
24252ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
24262ac8a218SBjoern A. Zeeb 
24272ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
24286b4cac81SBjoern A. Zeeb 
24296b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
24306b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
24316b4cac81SBjoern A. Zeeb 
24329597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
24339597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
24349597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
24359597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
24366b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
24379597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
24384a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
24394a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
24404a67f1dfSBjoern A. Zeeb 		sta->wme = true;
24414a67f1dfSBjoern A. Zeeb #endif
2442e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2443018d93ecSBjoern A. Zeeb 	if (error != 0) {
2444018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2445018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24469597f7cbSBjoern A. Zeeb 		goto out;
2447018d93ecSBjoern A. Zeeb 	}
24486b4cac81SBjoern A. Zeeb 
24496b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
24506b4cac81SBjoern A. Zeeb 
24516b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
24526b4cac81SBjoern A. Zeeb 	bss_changed = 0;
24534a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
24544a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
24554a67f1dfSBjoern A. Zeeb #endif
2456616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2457616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2458616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
24596b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
24606b4cac81SBjoern A. Zeeb 	}
24616b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2462616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2463616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
24646b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
24656b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
24666b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
24676b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
24686b4cac81SBjoern A. Zeeb 	}
24696b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
24706b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
24716b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
24726b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
24736b4cac81SBjoern A. Zeeb 	}
24746b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
24756b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
24766b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
24776b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
24786b4cac81SBjoern A. Zeeb 	}
2479fa8f007dSBjoern A. Zeeb 
2480fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2481fa8f007dSBjoern A. Zeeb 
24826b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
24836b4cac81SBjoern A. Zeeb 
24846b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
24856b4cac81SBjoern A. Zeeb 	 * - event_callback
24866b4cac81SBjoern 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 
2496086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2497086be6a8SBjoern A. Zeeb 
24986b4cac81SBjoern A. Zeeb 	/*
24996b4cac81SBjoern A. Zeeb 	 * And then:
25006b4cac81SBjoern A. Zeeb 	 * - (more packets)?
25016b4cac81SBjoern A. Zeeb 	 * - set_key
25026b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
25036b4cac81SBjoern A. Zeeb 	 * - set_key (?)
25046b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
25056b4cac81SBjoern A. Zeeb 	 */
25066b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
25076b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
25086b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
25096b4cac81SBjoern A. Zeeb 
25106b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
25116b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
25126b4cac81SBjoern A. Zeeb 	}
25136b4cac81SBjoern A. Zeeb 
25149fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
25159fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
25169fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
25179fb91463SBjoern A. Zeeb #endif
25189fb91463SBjoern A. Zeeb 
25196b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
25206b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
25216b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
25226b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2523e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
25246b4cac81SBjoern A. Zeeb 	if (error != 0) {
25256b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2526018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2527018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25286b4cac81SBjoern A. Zeeb 		goto out;
25296b4cac81SBjoern A. Zeeb 	}
25306b4cac81SBjoern A. Zeeb 
25316b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
25326b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
25336b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
25346b4cac81SBjoern A. Zeeb 	 *
25356b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
25366b4cac81SBjoern A. Zeeb 	 */
25376b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
25386b4cac81SBjoern A. Zeeb 
2539fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2540fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2541fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2542fa8f007dSBjoern A. Zeeb 
25436b4cac81SBjoern A. Zeeb out:
25448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
25456b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25466b4cac81SBjoern A. Zeeb 	return (error);
25476b4cac81SBjoern A. Zeeb }
25486b4cac81SBjoern A. Zeeb 
25496b4cac81SBjoern A. Zeeb static int
25506b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25516b4cac81SBjoern A. Zeeb {
25526b4cac81SBjoern A. Zeeb 	int error;
25536b4cac81SBjoern A. Zeeb 
25546b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
25556b4cac81SBjoern A. Zeeb 	if (error == 0)
25566b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
25576b4cac81SBjoern A. Zeeb 	return (error);
25586b4cac81SBjoern A. Zeeb }
25596b4cac81SBjoern A. Zeeb 
25606b4cac81SBjoern A. Zeeb static int
25616b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25626b4cac81SBjoern A. Zeeb {
25636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25666b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25676b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25686b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25696b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2570d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2571d9f59799SBjoern A. Zeeb #if 0
2572d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2573d9f59799SBjoern A. Zeeb #endif
25746b4cac81SBjoern A. Zeeb 	int error;
25756b4cac81SBjoern A. Zeeb 
25766b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25776b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25786b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25796b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25806b4cac81SBjoern A. Zeeb 
25812ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25822ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25832ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
25842ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
25852ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25862ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25872ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25882ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25892ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25902ac8a218SBjoern A. Zeeb #endif
25912ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25922ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25932ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25942ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25952ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25962ac8a218SBjoern A. Zeeb 
25972ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25986b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25996b4cac81SBjoern A. Zeeb 
260067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2601d9f59799SBjoern A. Zeeb 
2602d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
26038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2604d9f59799SBjoern A. Zeeb 
2605d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2606d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2607d9f59799SBjoern A. Zeeb 
2608d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2609d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2610d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2611d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2612d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2613ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2614d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2615d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2616d9f59799SBjoern A. Zeeb 	}
2617d9f59799SBjoern A. Zeeb 
26188ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2619d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2620d9f59799SBjoern A. Zeeb 
2621d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2622d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2623018d93ecSBjoern A. Zeeb 	if (error != 0) {
2624018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2625018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2626d9f59799SBjoern A. Zeeb 		goto outni;
2627018d93ecSBjoern A. Zeeb 	}
2628d9f59799SBjoern A. Zeeb 
2629d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
263088665349SBjoern A. Zeeb 
263188665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
263288665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
263388665349SBjoern A. Zeeb 
26348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2635d9f59799SBjoern A. Zeeb 
263667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2637d9f59799SBjoern A. Zeeb 
2638d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
263988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2640d9f59799SBjoern A. Zeeb 
2641d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2642d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2643d9f59799SBjoern A. Zeeb 
2644d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2645d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2646d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2647d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2648ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2649d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2650d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2651d9f59799SBjoern A. Zeeb 	}
2652d9f59799SBjoern A. Zeeb 
2653d9f59799SBjoern A. Zeeb #if 0
2654d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2655d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2656d9f59799SBjoern A. Zeeb 
2657d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2658d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2659d9f59799SBjoern A. Zeeb #endif
2660d9f59799SBjoern A. Zeeb 
2661d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2662d9f59799SBjoern A. Zeeb 
26636b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
26646b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26656b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
26666b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2667e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2668018d93ecSBjoern A. Zeeb 	if (error != 0) {
2669018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2670018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26716b4cac81SBjoern A. Zeeb 		goto out;
2672018d93ecSBjoern A. Zeeb 	}
26736b4cac81SBjoern A. Zeeb 
267467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
26756b4cac81SBjoern A. Zeeb 
267611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
267711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
267811db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
267911db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
268011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
268111db70b6SBjoern A. Zeeb 		if (error != 0) {
268211db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
268311db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
268411db70b6SBjoern A. Zeeb 			/*
268511db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
268611db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
268711db70b6SBjoern A. Zeeb 			 * less appropriate time).
268811db70b6SBjoern A. Zeeb 			 */
268911db70b6SBjoern A. Zeeb 			/* goto out; */
269011db70b6SBjoern A. Zeeb 		}
269111db70b6SBjoern A. Zeeb 	}
269211db70b6SBjoern A. Zeeb #endif
269311db70b6SBjoern A. Zeeb 
26946b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
26956b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26966b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
26976b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2698e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2699018d93ecSBjoern A. Zeeb 	if (error != 0) {
2700018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2701018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27026b4cac81SBjoern A. Zeeb 		goto out;
2703018d93ecSBjoern A. Zeeb 	}
27046b4cac81SBjoern A. Zeeb 
270567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27066b4cac81SBjoern A. Zeeb 
2707d9f59799SBjoern A. Zeeb #if 0
2708d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2709d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2710d9f59799SBjoern A. Zeeb #endif
2711d9f59799SBjoern A. Zeeb 
2712d9f59799SBjoern A. Zeeb 	error = EALREADY;
27136b4cac81SBjoern A. Zeeb out:
27148ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27156b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2716d9f59799SBjoern A. Zeeb outni:
27176b4cac81SBjoern A. Zeeb 	return (error);
27186b4cac81SBjoern A. Zeeb }
27196b4cac81SBjoern A. Zeeb 
27206b4cac81SBjoern A. Zeeb static int
2721d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2722d9f59799SBjoern A. Zeeb {
2723d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2724d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2725d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2726d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2727d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2728d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2729d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2730d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2731d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2732d9f59799SBjoern A. Zeeb 	int error;
2733d9f59799SBjoern A. Zeeb 
2734d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2735d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2736d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2737d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2738d9f59799SBjoern A. Zeeb 
27392ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
27402ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
27412ac8a218SBjoern A. Zeeb 
27422ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27432ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27442ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
27452ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
27462ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
27472ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27482ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27492ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27502ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27512ac8a218SBjoern A. Zeeb #endif
27522ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27532ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27542ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27552ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27562ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27572ac8a218SBjoern A. Zeeb 
27582ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2759d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2760d9f59799SBjoern A. Zeeb 
276167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2762d9f59799SBjoern A. Zeeb 
2763d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2764d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2765d9f59799SBjoern A. Zeeb 
2766d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2767d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2768d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2769d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2770d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2771ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2772d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2773d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2774d9f59799SBjoern A. Zeeb 	}
2775d9f59799SBjoern A. Zeeb 
27768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2777d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2778d9f59799SBjoern A. Zeeb 
2779d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2780d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2781018d93ecSBjoern A. Zeeb 	if (error != 0) {
2782018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2783018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2784d9f59799SBjoern A. Zeeb 		goto outni;
2785018d93ecSBjoern A. Zeeb 	}
2786d9f59799SBjoern A. Zeeb 
2787d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
278888665349SBjoern A. Zeeb 
278988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
279088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
279188665349SBjoern A. Zeeb 
27928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2793d9f59799SBjoern A. Zeeb 
279467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2795d9f59799SBjoern A. Zeeb 
2796d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
279788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2798d9f59799SBjoern A. Zeeb 
2799d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2800d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2801d9f59799SBjoern A. Zeeb 
2802d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2803d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2804d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2805d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2806ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2807d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2808d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2809d9f59799SBjoern A. Zeeb 	}
2810d9f59799SBjoern A. Zeeb 
2811d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2812d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2813d9f59799SBjoern A. Zeeb 
2814d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2815d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2816d9f59799SBjoern A. Zeeb 
2817d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2818d9f59799SBjoern A. Zeeb 
2819d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2820d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2821d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2822d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2823e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2824018d93ecSBjoern A. Zeeb 	if (error != 0) {
2825018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2826018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2827d9f59799SBjoern A. Zeeb 		goto out;
2828018d93ecSBjoern A. Zeeb 	}
2829d9f59799SBjoern A. Zeeb 
283067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2831d9f59799SBjoern A. Zeeb 
283211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
283311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
283411db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
283511db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
283611db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
283711db70b6SBjoern A. Zeeb 		if (error != 0) {
283811db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
283911db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
284011db70b6SBjoern A. Zeeb 			/*
284111db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
284211db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
284311db70b6SBjoern A. Zeeb 			 * less appropriate time).
284411db70b6SBjoern A. Zeeb 			 */
284511db70b6SBjoern A. Zeeb 			/* goto out; */
284611db70b6SBjoern A. Zeeb 		}
284711db70b6SBjoern A. Zeeb 	}
284811db70b6SBjoern A. Zeeb #endif
284911db70b6SBjoern A. Zeeb 
2850d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2851d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2852d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2853d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2854e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2855018d93ecSBjoern A. Zeeb 	if (error != 0) {
2856018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2857018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2858d9f59799SBjoern A. Zeeb 		goto out;
2859018d93ecSBjoern A. Zeeb 	}
2860d9f59799SBjoern A. Zeeb 
286167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2862d9f59799SBjoern A. Zeeb 
2863d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2864d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2865d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2866d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2867e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2868018d93ecSBjoern A. Zeeb 	if (error != 0) {
2869018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2870018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2871d9f59799SBjoern A. Zeeb 		goto out;
2872018d93ecSBjoern A. Zeeb 	}
2873d9f59799SBjoern A. Zeeb 
28745a4d2461SBjoern A. Zeeb 	bss_changed = 0;
287516e688b2SBjoern A. Zeeb 	/*
28765a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
28775a4d2461SBjoern A. Zeeb 	 *
287816e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
28795a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
28805a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
28815a4d2461SBjoern A. Zeeb 	 *
28825a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
28835a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
28845a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
28855a4d2461SBjoern A. Zeeb 	 * a fw assert.
28865a4d2461SBjoern A. Zeeb 	 *
28875a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
28885a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
28895a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
28905a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
28915a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
28925a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
28935a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
28945a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
28955a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
28965a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
289716e688b2SBjoern A. Zeeb 	 */
28985a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
28995a4d2461SBjoern A. Zeeb 
29005a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
290116e688b2SBjoern A. Zeeb 
2902d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2903d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2904d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2905d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2906e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2907d9f59799SBjoern A. Zeeb 	if (error != 0) {
2908d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2909018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2910018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2911d9f59799SBjoern A. Zeeb 		goto out;
2912d9f59799SBjoern A. Zeeb 	}
2913d9f59799SBjoern A. Zeeb 
29145a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
29155a4d2461SBjoern A. Zeeb 
291616e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2917d9f59799SBjoern A. Zeeb 
2918d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2919d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2920d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2921616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2922616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2923d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
29245a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
29255a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
29265a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2927d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2928d9f59799SBjoern A. Zeeb 
29292ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
29302ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
29312ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
29322ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
29332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
29340936c648SBjoern A. Zeeb 	/*
29350936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
29360936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
29370936c648SBjoern A. Zeeb 	 * and potentially freed.
29380936c648SBjoern A. Zeeb 	 */
29390936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2940d9f59799SBjoern A. Zeeb 
2941d9f59799SBjoern A. Zeeb 	/* conf_tx */
2942d9f59799SBjoern A. Zeeb 
2943d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2944d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2945c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2946d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2947d9f59799SBjoern A. Zeeb 
2948d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2949d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
295068541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2951d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2952d9f59799SBjoern A. Zeeb 
29535a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
29545a4d2461SBjoern A. Zeeb 
2955d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2956d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2957d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2958c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2959d9f59799SBjoern A. Zeeb 	}
2960d9f59799SBjoern A. Zeeb 
2961d9f59799SBjoern A. Zeeb 	error = EALREADY;
2962d9f59799SBjoern A. Zeeb out:
29638ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2964d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2965d9f59799SBjoern A. Zeeb outni:
2966d9f59799SBjoern A. Zeeb 	return (error);
2967d9f59799SBjoern A. Zeeb }
2968d9f59799SBjoern A. Zeeb 
2969d9f59799SBjoern A. Zeeb static int
2970d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2971d9f59799SBjoern A. Zeeb {
2972d9f59799SBjoern A. Zeeb 
2973d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2974d9f59799SBjoern A. Zeeb }
2975d9f59799SBjoern A. Zeeb 
2976d9f59799SBjoern A. Zeeb static int
29776b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29786b4cac81SBjoern A. Zeeb {
29796b4cac81SBjoern A. Zeeb 	int error;
29806b4cac81SBjoern A. Zeeb 
2981d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2982d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2983d9f59799SBjoern A. Zeeb 		return (error);
2984d9f59799SBjoern A. Zeeb 
2985d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2986d9f59799SBjoern A. Zeeb 
2987d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
29886b4cac81SBjoern A. Zeeb 	return (error);
29896b4cac81SBjoern A. Zeeb }
29906b4cac81SBjoern A. Zeeb 
2991d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
29926b4cac81SBjoern A. Zeeb 
29936b4cac81SBjoern A. Zeeb /*
29946b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
29956b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
29966b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
29976b4cac81SBjoern A. Zeeb  */
29986b4cac81SBjoern A. Zeeb struct fsm_state {
29996b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
30006b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
30016b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
30026b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
30036b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
30046b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
30056b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
30066b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3007d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3008d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
30096b4cac81SBjoern A. Zeeb 
30106b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30116b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30126b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
30136b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3014d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
30156b4cac81SBjoern A. Zeeb 
3016d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3017d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3018d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3019d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3020d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
30216b4cac81SBjoern A. Zeeb 
3022d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3023d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3024d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
30256b4cac81SBjoern A. Zeeb 
30266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
30276b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
30286b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
30296b4cac81SBjoern A. Zeeb 
30306b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
30316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
30326b4cac81SBjoern A. Zeeb };
30336b4cac81SBjoern A. Zeeb 
30346b4cac81SBjoern A. Zeeb static int
30356b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
30366b4cac81SBjoern A. Zeeb {
30376b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30386b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30396b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30406b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30416b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
30426b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
30436b4cac81SBjoern A. Zeeb 	int error;
30446b4cac81SBjoern A. Zeeb 
30456b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
30466b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
30476b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
30486b4cac81SBjoern A. Zeeb 
30499d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
30516b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
30526b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
30539d9ba2b7SBjoern A. Zeeb #endif
30546b4cac81SBjoern A. Zeeb 
30556b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
30566b4cac81SBjoern A. Zeeb 
30576b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
30586b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
30596b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
30606b4cac81SBjoern A. Zeeb 
30616b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
30626b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
30636b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
30646b4cac81SBjoern A. Zeeb 
30656b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
30666b4cac81SBjoern A. Zeeb 
30676b4cac81SBjoern A. Zeeb 	} else {
30686b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
30696b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
30706b4cac81SBjoern A. Zeeb 		return (ENOSYS);
30716b4cac81SBjoern A. Zeeb 	}
30726b4cac81SBjoern A. Zeeb 
30736b4cac81SBjoern A. Zeeb 	error = 0;
30746b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
30756b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
30769d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30779d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3078d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3079d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3080d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3081d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
30829d9ba2b7SBjoern A. Zeeb #endif
30836b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
30846b4cac81SBjoern A. Zeeb 			break;
30856b4cac81SBjoern A. Zeeb 		}
30866b4cac81SBjoern A. Zeeb 	}
30876b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
30886b4cac81SBjoern A. Zeeb 
30896b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3090d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
30916b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
30926b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
30936b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
30946b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
30956b4cac81SBjoern A. Zeeb 		return (ENOSYS);
30966b4cac81SBjoern A. Zeeb 	}
30976b4cac81SBjoern A. Zeeb 
30986b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
30999d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31009d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3101d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3102d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3103d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3104d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
31059d9ba2b7SBjoern A. Zeeb #endif
31066b4cac81SBjoern A. Zeeb 		return (0);
31076b4cac81SBjoern A. Zeeb 	}
31086b4cac81SBjoern A. Zeeb 
31096b4cac81SBjoern A. Zeeb 	if (error != 0) {
31106b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
31116b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
31126b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31136b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
311445c27ad5SBjoern A. Zeeb 		return (error);
31156b4cac81SBjoern A. Zeeb 	}
31166b4cac81SBjoern A. Zeeb 
31179d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31189d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3119d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3120d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
31216b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31229d9ba2b7SBjoern A. Zeeb #endif
31236b4cac81SBjoern A. Zeeb 
31246b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
31256b4cac81SBjoern A. Zeeb }
31266b4cac81SBjoern A. Zeeb 
31276b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
31286b4cac81SBjoern A. Zeeb 
3129d9f59799SBjoern A. Zeeb /*
3130d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3131d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3132d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3133d9f59799SBjoern A. Zeeb  */
3134d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3135d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3136d9f59799SBjoern A. Zeeb {
3137d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31382ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3139d9f59799SBjoern A. Zeeb 
31402ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3141d9f59799SBjoern A. Zeeb 
3142ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
31432ac8a218SBjoern A. Zeeb 
31442ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
31452ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
31462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
31472ac8a218SBjoern A. Zeeb 
31482ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
31492ac8a218SBjoern A. Zeeb 	return (rni);
3150d9f59799SBjoern A. Zeeb }
3151d9f59799SBjoern A. Zeeb 
31524a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
31536b4cac81SBjoern A. Zeeb static int
31544a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
31556b4cac81SBjoern A. Zeeb {
31564a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
31576b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
31586b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31596b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31606b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
31616b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
31626b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
31636b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
31646b4cac81SBjoern A. Zeeb 	int error;
31656b4cac81SBjoern A. Zeeb 	uint16_t ac;
31666b4cac81SBjoern A. Zeeb 
31676b4cac81SBjoern A. Zeeb 	IMPROVE();
31686b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
31696b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
31706b4cac81SBjoern A. Zeeb 
31716b4cac81SBjoern A. Zeeb 	if (vap == NULL)
31726b4cac81SBjoern A. Zeeb 		return (0);
31736b4cac81SBjoern A. Zeeb 
31744a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
31754a67f1dfSBjoern A. Zeeb 		return (0);
31764a67f1dfSBjoern A. Zeeb 
31776b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
31786b4cac81SBjoern A. Zeeb 		return (0);
31796b4cac81SBjoern A. Zeeb 
31804a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
31814a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
31824a67f1dfSBjoern A. Zeeb 		return (0);
31834a67f1dfSBjoern A. Zeeb 	}
31844a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
31856b4cac81SBjoern A. Zeeb 
31864a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
31876b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
31886b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
31896b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
31906b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
31916b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
31926b4cac81SBjoern A. Zeeb 
31934a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
31944a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
31954a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
31964a67f1dfSBjoern A. Zeeb 
31976b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
31986b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
31996b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
32006b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
32016b4cac81SBjoern A. Zeeb 
32026b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
32036b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
32046b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
32056b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
32066b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
32076b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
320868541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
32096b4cac81SBjoern A. Zeeb 		if (error != 0)
32103f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
32116b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
32126b4cac81SBjoern A. Zeeb 	}
32136b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
32146b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
32154a67f1dfSBjoern A. Zeeb 	if (!planned)
32166b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
32174a67f1dfSBjoern A. Zeeb 
32184a67f1dfSBjoern A. Zeeb 	return (changed);
32194a67f1dfSBjoern A. Zeeb }
32206b4cac81SBjoern A. Zeeb #endif
32216b4cac81SBjoern A. Zeeb 
32224a67f1dfSBjoern A. Zeeb static int
32234a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
32244a67f1dfSBjoern A. Zeeb {
32254a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
32264a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
32274a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
32284a67f1dfSBjoern A. Zeeb 
32294a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
32304a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
32314a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
32326b4cac81SBjoern A. Zeeb 		return (0);
32334a67f1dfSBjoern A. Zeeb 
32344a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
32354a67f1dfSBjoern A. Zeeb 
32364a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
32374a67f1dfSBjoern A. Zeeb #endif
32384a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
32396b4cac81SBjoern A. Zeeb }
32406b4cac81SBjoern A. Zeeb 
32414aff4048SBjoern A. Zeeb /*
32424aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
32434aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
32444aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
32454aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
32464aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
32474aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3248edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3249edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3250edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3251edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
32524aff4048SBjoern A. Zeeb  */
32534aff4048SBjoern A. Zeeb static void
32544aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
32554aff4048SBjoern A. Zeeb {
32564aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
32574aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32584aff4048SBjoern A. Zeeb 
32594aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3260edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3261edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3262edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
32634aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
32644aff4048SBjoern A. Zeeb 		return;
32654aff4048SBjoern A. Zeeb 	}
32664aff4048SBjoern A. Zeeb 
32674aff4048SBjoern A. Zeeb 	vif = arg;
326857609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
32694aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
32704aff4048SBjoern A. Zeeb }
32714aff4048SBjoern A. Zeeb 
32726b4cac81SBjoern A. Zeeb static struct ieee80211vap *
32736b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
32746b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
32756b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
32766b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
32776b4cac81SBjoern A. Zeeb {
32786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32796b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32806b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32816b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
32826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3283a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
32846b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
328540839418SBjoern A. Zeeb 	struct sysctl_oid *node;
32866b4cac81SBjoern A. Zeeb 	size_t len;
3287e3a0b120SBjoern A. Zeeb 	int error, i;
3288a6042e17SBjoern A. Zeeb 	uint16_t ac;
32896b4cac81SBjoern A. Zeeb 
32906b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
32916b4cac81SBjoern A. Zeeb 		return (NULL);
32926b4cac81SBjoern A. Zeeb 
32936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
32946b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32956b4cac81SBjoern A. Zeeb 
32966b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
32976b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
32986b4cac81SBjoern A. Zeeb 
32996b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
33006b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3301a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
33022ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
33032ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33046b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
33056b4cac81SBjoern A. Zeeb 
33066b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33076b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
33086b4cac81SBjoern A. Zeeb 	vif->p2p = false;
33096b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
33106b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
33116b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
33126b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
33136b4cac81SBjoern A. Zeeb 	IMPROVE();
33146b4cac81SBjoern A. Zeeb 
33156b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
33166b4cac81SBjoern A. Zeeb #if 1
33176b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3318adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
33196ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
33206ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
33214aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
33224aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
33236ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
33247b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
33256b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
33266b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
33276b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
33286b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
33296b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3330616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3331616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3332616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3333616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
33346ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3335caaa79c3SBjoern A. Zeeb 	/*
3336caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3337caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3338caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3339caaa79c3SBjoern A. Zeeb 	 */
33406ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
33416b4cac81SBjoern A. Zeeb #endif
33426b4cac81SBjoern A. Zeeb #if 0
33436b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
33446b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
33456b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
33466b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
33476b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
33486b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
33496b4cac81SBjoern A. Zeeb #endif
3350e3a0b120SBjoern A. Zeeb 
33516ffb7bd4SBjoern A. Zeeb 	/* Link Config */
33526ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
33536ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
33546ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
33556ffb7bd4SBjoern A. Zeeb 	}
33566ffb7bd4SBjoern A. Zeeb 
3357e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3358e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3359e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3360e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3361e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3362e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3363e3a0b120SBjoern A. Zeeb 		else
3364e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
33650cbcfa19SBjoern A. Zeeb 
33660cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
33670cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3368e3a0b120SBjoern A. Zeeb 	}
3369e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3370e3a0b120SBjoern A. Zeeb 
33716b4cac81SBjoern A. Zeeb 	IMPROVE();
33726b4cac81SBjoern A. Zeeb 
33736b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
33746b4cac81SBjoern A. Zeeb 	if (error != 0) {
33753f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
33766b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
33776b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
33786b4cac81SBjoern A. Zeeb 		return (NULL);
33796b4cac81SBjoern A. Zeeb 	}
33806b4cac81SBjoern A. Zeeb 
33816b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
33826b4cac81SBjoern A. Zeeb 	if (error != 0) {
33836b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
33843f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
33856b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
33866b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
33876b4cac81SBjoern A. Zeeb 		return (NULL);
33886b4cac81SBjoern A. Zeeb 	}
33896b4cac81SBjoern A. Zeeb 
33908891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
33916b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
33928891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
33936b4cac81SBjoern A. Zeeb 
33946b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
33956b4cac81SBjoern A. Zeeb 	changed = 0;
33966b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
33976b4cac81SBjoern A. Zeeb 
3398a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3399a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3400a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3401a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3402a6042e17SBjoern A. Zeeb 
3403a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3404a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3405a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3406a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3407a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3408a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3409a6042e17SBjoern A. Zeeb 		if (error != 0)
3410a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3411a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3412a6042e17SBjoern A. Zeeb 	}
3413a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3414a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3415a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34166b4cac81SBjoern A. Zeeb 
34176b4cac81SBjoern A. Zeeb 	/* Force MC init. */
34186b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
34196b4cac81SBjoern A. Zeeb 
34206b4cac81SBjoern A. Zeeb 	IMPROVE();
34216b4cac81SBjoern A. Zeeb 
34226b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
34236b4cac81SBjoern A. Zeeb 
34246b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
34256b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
34266b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3427d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3428d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
34296b4cac81SBjoern A. Zeeb 
3430b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
343111db70b6SBjoern A. Zeeb 	/* Key management. */
343211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
34336b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
34346b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
34356b4cac81SBjoern A. Zeeb 	}
343611db70b6SBjoern A. Zeeb #endif
34376b4cac81SBjoern A. Zeeb 
34389fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
34399fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
34409fb91463SBjoern A. Zeeb #endif
34419fb91463SBjoern A. Zeeb 
34426b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
34436b4cac81SBjoern A. Zeeb 
34446b4cac81SBjoern A. Zeeb 	/* Complete setup. */
34456b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
34466b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
34476b4cac81SBjoern A. Zeeb 
344811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
344911db70b6SBjoern A. Zeeb 	/*
345011db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
345111db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
345211db70b6SBjoern A. Zeeb 	 */
345311db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
345411db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
345511db70b6SBjoern A. Zeeb #endif
345611db70b6SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
34586b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
34596b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
34606b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
34616b4cac81SBjoern A. Zeeb 
34626b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
34636b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
34646b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
34656b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
34666b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
34676b4cac81SBjoern A. Zeeb 	/* any others? */
346840839418SBjoern A. Zeeb 
346940839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
347040839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
347140839418SBjoern A. Zeeb 
347240839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
347340839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
347440839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
347540839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
347640839418SBjoern A. Zeeb 
347740839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
347840839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
347940839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
348040839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
348140839418SBjoern A. Zeeb 
34826b4cac81SBjoern A. Zeeb 	IMPROVE();
34836b4cac81SBjoern A. Zeeb 
34846b4cac81SBjoern A. Zeeb 	return (vap);
34856b4cac81SBjoern A. Zeeb }
34866b4cac81SBjoern A. Zeeb 
34874b0af114SBjoern A. Zeeb void
34884b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
34894b0af114SBjoern A. Zeeb {
34904b0af114SBjoern A. Zeeb 
34914b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
34924b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
34934b0af114SBjoern A. Zeeb 
34944b0af114SBjoern A. Zeeb 	IMPROVE();
34954b0af114SBjoern A. Zeeb }
34964b0af114SBjoern A. Zeeb 
34974b0af114SBjoern A. Zeeb void
34984b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
34994b0af114SBjoern A. Zeeb {
35004b0af114SBjoern A. Zeeb 
35014b0af114SBjoern A. Zeeb 	TODO();
35024b0af114SBjoern A. Zeeb }
35034b0af114SBjoern A. Zeeb 
35046b4cac81SBjoern A. Zeeb static void
35056b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
35066b4cac81SBjoern A. Zeeb {
35076b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35096b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35116b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35126b4cac81SBjoern A. Zeeb 
35136b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35146b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35156b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
35166b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35176b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35186b4cac81SBjoern A. Zeeb 
35194aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
35204aff4048SBjoern A. Zeeb 
352140839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
352240839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
352340839418SBjoern A. Zeeb 
35248891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
35256b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
35268891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
35276b4cac81SBjoern A. Zeeb 
35286b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
35296b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3530dbf76919SBjoern A. Zeeb 
3531dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3532dbf76919SBjoern A. Zeeb 
3533dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3534dbf76919SBjoern A. Zeeb 
35356c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
35367b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
35376c38c6b1SBjoern A. Zeeb 
35386b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
35396b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
35406b4cac81SBjoern A. Zeeb }
35416b4cac81SBjoern A. Zeeb 
35426b4cac81SBjoern A. Zeeb static void
35436b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
35446b4cac81SBjoern A. Zeeb {
35456b4cac81SBjoern A. Zeeb 
35466b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
35476b4cac81SBjoern A. Zeeb 	TRACEOK();
35486b4cac81SBjoern A. Zeeb }
35496b4cac81SBjoern A. Zeeb 
35506b4cac81SBjoern A. Zeeb static void
35516b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
35526b4cac81SBjoern A. Zeeb {
35536b4cac81SBjoern A. Zeeb 
35546b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35556b4cac81SBjoern A. Zeeb }
35566b4cac81SBjoern A. Zeeb 
35576b4cac81SBjoern A. Zeeb static void
35586b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
35596b4cac81SBjoern A. Zeeb {
35606b4cac81SBjoern A. Zeeb 
35616b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35626b4cac81SBjoern A. Zeeb }
35636b4cac81SBjoern A. Zeeb 
35646b4cac81SBjoern A. Zeeb /* Start / stop device. */
35656b4cac81SBjoern A. Zeeb static void
35666b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
35676b4cac81SBjoern A. Zeeb {
35686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35698d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35716b4cac81SBjoern A. Zeeb 	int error;
35728d58a057SBjoern A. Zeeb #endif
35736b4cac81SBjoern A. Zeeb 	bool start_all;
35746b4cac81SBjoern A. Zeeb 
35756b4cac81SBjoern A. Zeeb 	IMPROVE();
35766b4cac81SBjoern A. Zeeb 
35776b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35788d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35808d58a057SBjoern A. Zeeb #endif
35816b4cac81SBjoern A. Zeeb 	start_all = false;
35826b4cac81SBjoern A. Zeeb 
35838ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
35848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
35856b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
35868d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35876b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
35886b4cac81SBjoern A. Zeeb 		if (error == 0)
35898d58a057SBjoern A. Zeeb #endif
35906b4cac81SBjoern A. Zeeb 			start_all = true;
35916b4cac81SBjoern A. Zeeb 	} else {
35928d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35937b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
35948d58a057SBjoern A. Zeeb #endif
35956b4cac81SBjoern A. Zeeb 	}
35968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
35978ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
35986b4cac81SBjoern A. Zeeb 
35996b4cac81SBjoern A. Zeeb 	if (start_all)
36006b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
36016b4cac81SBjoern A. Zeeb }
36026b4cac81SBjoern A. Zeeb 
36036b4cac81SBjoern A. Zeeb bool
36046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
36056b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
36066b4cac81SBjoern A. Zeeb {
36076b4cac81SBjoern A. Zeeb 	int i;
36086b4cac81SBjoern A. Zeeb 
36096b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
36106b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
36116b4cac81SBjoern A. Zeeb 			return (true);
36126b4cac81SBjoern A. Zeeb 	}
36136b4cac81SBjoern A. Zeeb 
36146b4cac81SBjoern A. Zeeb 	return (false);
36156b4cac81SBjoern A. Zeeb }
36166b4cac81SBjoern A. Zeeb 
36176b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
36186b4cac81SBjoern A. Zeeb bool
36196b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
36206b4cac81SBjoern A. Zeeb {
36216b4cac81SBjoern A. Zeeb 	size_t x;
36226b4cac81SBjoern A. Zeeb 	uint8_t l;
36236b4cac81SBjoern A. Zeeb 
36246b4cac81SBjoern A. Zeeb 	x = *xp;
36256b4cac81SBjoern A. Zeeb 
36266b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
36276b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
36286b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
36296b4cac81SBjoern A. Zeeb 	x += 2 + l;
36306b4cac81SBjoern A. Zeeb 
36316b4cac81SBjoern A. Zeeb 	if (x > ies_len)
36326b4cac81SBjoern A. Zeeb 		return (false);
36336b4cac81SBjoern A. Zeeb 
36346b4cac81SBjoern A. Zeeb 	*xp = x;
36356b4cac81SBjoern A. Zeeb 	return (true);
36366b4cac81SBjoern A. Zeeb }
36376b4cac81SBjoern A. Zeeb 
3638d9945d78SBjoern A. Zeeb static uint8_t *
3639d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3640d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
36416b4cac81SBjoern A. Zeeb {
3642d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3643d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
36443dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3645d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3646d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3647d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3648d9945d78SBjoern A. Zeeb 	int band, i;
36496b4cac81SBjoern A. Zeeb 
36503dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3651d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3652d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3653d9945d78SBjoern A. Zeeb 			continue;
3654d9945d78SBjoern A. Zeeb 
3655d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3656d9945d78SBjoern A. Zeeb 		/*
3657d9945d78SBjoern A. Zeeb 		 * This should not happen;
3658d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3659d9945d78SBjoern A. Zeeb 		 */
3660d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3661d9945d78SBjoern A. Zeeb 			continue;
3662d9945d78SBjoern A. Zeeb 
3663d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3664d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3665d9945d78SBjoern A. Zeeb 		chan = NULL;
3666d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3667d9945d78SBjoern A. Zeeb 
3668d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3669d9945d78SBjoern A. Zeeb 				continue;
3670d9945d78SBjoern A. Zeeb 
36713dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3672d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3673d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3674d9945d78SBjoern A. Zeeb 				break;
3675d9945d78SBjoern A. Zeeb 		}
3676d9945d78SBjoern A. Zeeb 
3677d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3678d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3679d9945d78SBjoern A. Zeeb 			continue;
3680d9945d78SBjoern A. Zeeb 
3681d9945d78SBjoern A. Zeeb 		pb = p;
36823dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3683d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3684d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3685d9945d78SBjoern A. Zeeb 
36863dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
36873dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
36883dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
36893dd98026SBjoern A. Zeeb 
36903dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
36913dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
36923dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
36933dd98026SBjoern A. Zeeb 		}
36943dd98026SBjoern A. Zeeb #endif
36953dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
36963dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
36973dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
36983dd98026SBjoern A. Zeeb 
36993dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37003dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
37013dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
37023dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
37033dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
37043dd98026SBjoern A. Zeeb 		}
37053dd98026SBjoern A. Zeeb #endif
37063dd98026SBjoern A. Zeeb 
3707d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3708d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3709d9945d78SBjoern A. Zeeb 	}
3710d9945d78SBjoern A. Zeeb 
3711d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3712d9945d78SBjoern A. Zeeb 	pb = p;
3713d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3714d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3715d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3716d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3717d9945d78SBjoern A. Zeeb 	}
3718d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3719d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3720d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3721d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3722d9945d78SBjoern A. Zeeb 	}
3723d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3724d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3725d9945d78SBjoern A. Zeeb 
3726d9945d78SBjoern A. Zeeb 	return (p);
37276b4cac81SBjoern A. Zeeb }
37286b4cac81SBjoern A. Zeeb 
37296b4cac81SBjoern A. Zeeb static void
37306b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
37316b4cac81SBjoern A. Zeeb {
37326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37336b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37346b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37356b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
37366b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
37376b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
37386b4cac81SBjoern A. Zeeb 	int error;
37398ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
37406b4cac81SBjoern A. Zeeb 
37416b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37428ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3743a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
37446b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
37458ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37466b4cac81SBjoern A. Zeeb 		return;
37476b4cac81SBjoern A. Zeeb 	}
37488ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
37498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37506b4cac81SBjoern A. Zeeb 
37516b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
37526b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
37536b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3754d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
37556b4cac81SBjoern A. Zeeb 		return;
37566b4cac81SBjoern A. Zeeb 	}
37576b4cac81SBjoern A. Zeeb 
37586b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37598ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3760a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3761a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3762d3ef7fb4SBjoern A. Zeeb sw_scan:
37636b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
37646b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3765086be6a8SBjoern A. Zeeb 
3766086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3767086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3768086be6a8SBjoern A. Zeeb 
37696b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
37706b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
37716b4cac81SBjoern A. Zeeb 		IMPROVE();
37726b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
37736b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
37746b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
37756b4cac81SBjoern A. Zeeb 
37766b4cac81SBjoern A. Zeeb 	} else {
37776b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
37786b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
37796b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
37806b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
37816b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3782d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3783d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3784d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
37853206587aSBjoern A. Zeeb 		bool running;
3786d9945d78SBjoern A. Zeeb 
3787d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3788d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
37896b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
37906b4cac81SBjoern A. Zeeb 
3791d9945d78SBjoern A. Zeeb 		band_mask = 0;
37926b4cac81SBjoern A. Zeeb 		nchan = 0;
3793c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3794c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3795d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
37966b4cac81SBjoern A. Zeeb 				nchan++;
3797d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3798d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3799d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3800d9945d78SBjoern A. Zeeb 			}
3801c272abc5SBjoern A. Zeeb #else
3802c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3803c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3804c272abc5SBjoern A. Zeeb 				switch (band) {
3805c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3806c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3807c272abc5SBjoern A. Zeeb 					break;
3808c272abc5SBjoern A. Zeeb 				default:
3809c272abc5SBjoern A. Zeeb 					continue;
3810c272abc5SBjoern A. Zeeb 				}
3811c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3812c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3813c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3814c272abc5SBjoern A. Zeeb 				}
3815c272abc5SBjoern A. Zeeb 			}
3816c272abc5SBjoern A. Zeeb #endif
3817c272abc5SBjoern A. Zeeb 		} else {
38183206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
38193206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
38203206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
38213206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
38223206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
38233206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
38243206587aSBjoern A. Zeeb 			 */
38253206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
38263206587aSBjoern A. Zeeb 		}
38273206587aSBjoern A. Zeeb 
38286b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
38296b4cac81SBjoern A. Zeeb 
3830d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3831d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3832d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3833d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3834d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3835d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3836d9945d78SBjoern A. Zeeb 
3837d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3838d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3839d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3840d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3841d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3842d9945d78SBjoern A. Zeeb 		}
3843d9945d78SBjoern A. Zeeb 
38443206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3845d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3846d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
38476b4cac81SBjoern A. Zeeb 
38486b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
38496b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
38506b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
38516b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
38526b4cac81SBjoern A. Zeeb #if 0
38536b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
38546b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
38556b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
38566b4cac81SBjoern A. Zeeb #endif
38576b4cac81SBjoern A. Zeeb #ifdef __notyet__
38586b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
38596b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
38606b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
38616b4cac81SBjoern A. Zeeb #endif
3862e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
38636b4cac81SBjoern A. Zeeb 
38646b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
38656b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
38666b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
38676b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
38686b4cac81SBjoern A. Zeeb 			*(cpp + i) =
38696b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
38706b4cac81SBjoern A. Zeeb 		}
3871c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
38726b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3873c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
38746b4cac81SBjoern A. Zeeb 
3875c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
38766b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
38773206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
38786b4cac81SBjoern A. Zeeb 			/* lc->flags */
38796b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
38806b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
38816b4cac81SBjoern A. Zeeb 			/* lc-> ... */
38826b4cac81SBjoern A. Zeeb 			lc++;
38836b4cac81SBjoern A. Zeeb 		}
3884c272abc5SBjoern A. Zeeb #else
3885c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3886c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3887c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3888c272abc5SBjoern A. Zeeb 
3889c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3890c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3891c272abc5SBjoern A. Zeeb 				continue;
3892c272abc5SBjoern A. Zeeb 
3893c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
3894c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
3895c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
3896c272abc5SBjoern A. Zeeb 				continue;
3897c272abc5SBjoern A. Zeeb 
3898c272abc5SBjoern A. Zeeb 			channels = supband->channels;
3899c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
3900c272abc5SBjoern A. Zeeb 				*lc = channels[i];
3901c272abc5SBjoern A. Zeeb 				lc++;
3902c272abc5SBjoern A. Zeeb 			}
3903c272abc5SBjoern A. Zeeb 		}
3904c272abc5SBjoern A. Zeeb #endif
39056b4cac81SBjoern A. Zeeb 
3906d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
39076b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
39086b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
39096b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3910d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
39116b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
39126b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
39136b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
39146b4cac81SBjoern A. Zeeb 				ssids++;
39156b4cac81SBjoern A. Zeeb 			}
39166b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
39176b4cac81SBjoern A. Zeeb 		} else {
39186b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
39196b4cac81SBjoern A. Zeeb 		}
39206b4cac81SBjoern A. Zeeb 
39216b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
39226b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
39236b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
39246b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
39256b4cac81SBjoern A. Zeeb 		/* s6gp->... */
39266b4cac81SBjoern A. Zeeb 
3927d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3928d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3929d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3930d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3931d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3932d9945d78SBjoern A. Zeeb 
39336b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
39346b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
39353206587aSBjoern A. Zeeb 
39363206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
39373206587aSBjoern A. Zeeb 		/* Re-check under lock. */
39383206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
39393206587aSBjoern A. Zeeb 		if (!running) {
39403206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
39413206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
39423206587aSBjoern A. Zeeb 
39433206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
39443206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
39453206587aSBjoern A. Zeeb 		}
39463206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
39473206587aSBjoern A. Zeeb 		if (running) {
39483206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
39493206587aSBjoern A. Zeeb 			return;
39503206587aSBjoern A. Zeeb 		}
39513206587aSBjoern A. Zeeb 
39526b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
39536b4cac81SBjoern A. Zeeb 		if (error != 0) {
3954d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3955d9945d78SBjoern A. Zeeb 
39563206587aSBjoern A. Zeeb 			/*
39573206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
39583206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
39593206587aSBjoern A. Zeeb 			 * and only there.
39603206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
39613206587aSBjoern A. Zeeb 			 * behave differently:
39623206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
39633206587aSBjoern A. Zeeb 			 *        and can then still return an error.
39643206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
39653206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
39663206587aSBjoern A. Zeeb 			 * ...
39673206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
39683206587aSBjoern A. Zeeb 			 * and balance between both code paths.
39693206587aSBjoern A. Zeeb 			 */
39703206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
39713206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
39723206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
39736b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
39743206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
39753206587aSBjoern A. Zeeb 			}
39763206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3977d3ef7fb4SBjoern A. Zeeb 
3978d3ef7fb4SBjoern A. Zeeb 			/*
3979d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
3980d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
3981d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
3982d3ef7fb4SBjoern A. Zeeb 			 */
3983196cfd0bSBjoern A. Zeeb 			if (error == 1) {
39848ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3985a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
39868ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
39873206587aSBjoern A. Zeeb 				/*
39883206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
39893206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
39903206587aSBjoern A. Zeeb 				 * currently not re-enable it?
39913206587aSBjoern A. Zeeb 				 */
39923206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3993196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
3994196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
3995196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
3996196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
3997196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
3998196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3999196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4000196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4001d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4002196cfd0bSBjoern A. Zeeb 			}
4003d3ef7fb4SBjoern A. Zeeb 
4004d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4005d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
40066b4cac81SBjoern A. Zeeb 		}
40076b4cac81SBjoern A. Zeeb 	}
40086b4cac81SBjoern A. Zeeb }
40096b4cac81SBjoern A. Zeeb 
40106b4cac81SBjoern A. Zeeb static void
40116b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
40126b4cac81SBjoern A. Zeeb {
40136b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40148ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40156b4cac81SBjoern A. Zeeb 
40166b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40178ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4018a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
40198ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40206b4cac81SBjoern A. Zeeb 		return;
40216b4cac81SBjoern A. Zeeb 	}
40228ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40246b4cac81SBjoern A. Zeeb 
40258ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4026a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4027a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
40286b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
40296b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
40306b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
40316b4cac81SBjoern A. Zeeb 
4032a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4033a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
40346b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
40356b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40366b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4037a486fbbdSBjoern A. Zeeb 
40386b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
40396b4cac81SBjoern A. Zeeb 
40406b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4041086be6a8SBjoern A. Zeeb 
4042086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4043086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
40446b4cac81SBjoern A. Zeeb 	}
40456b4cac81SBjoern A. Zeeb }
40466b4cac81SBjoern A. Zeeb 
40476b4cac81SBjoern A. Zeeb static void
4048a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4049a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4050a486fbbdSBjoern A. Zeeb {
4051a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
40528ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4053a486fbbdSBjoern A. Zeeb 
4054a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
40558ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
40568ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40578ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40588ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4059a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4060a486fbbdSBjoern A. Zeeb }
4061a486fbbdSBjoern A. Zeeb 
4062a486fbbdSBjoern A. Zeeb static void
4063a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4064a486fbbdSBjoern A. Zeeb {
4065a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
40668ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4067a486fbbdSBjoern A. Zeeb 
4068a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
40698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
40708ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40718ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40728ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4073a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4074a486fbbdSBjoern A. Zeeb }
4075a486fbbdSBjoern A. Zeeb 
4076a486fbbdSBjoern A. Zeeb static void
40776b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
40786b4cac81SBjoern A. Zeeb {
40796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4081b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4082b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
40836b4cac81SBjoern A. Zeeb 	int error;
40848ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
40856b4cac81SBjoern A. Zeeb 
40866b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4087b2cf3c21SBjoern A. Zeeb 
4088b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4089b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
40906b4cac81SBjoern A. Zeeb 		return;
40916b4cac81SBjoern A. Zeeb 
4092a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
40938ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
40948ac540d3SBjoern A. Zeeb 	hw_scan_running =
40958ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
40968ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
40978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40988ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4099a486fbbdSBjoern A. Zeeb 		return;
4100a486fbbdSBjoern A. Zeeb 
4101b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4102b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
41036b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
41046b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
41056b4cac81SBjoern A. Zeeb 		return;
41066b4cac81SBjoern A. Zeeb 	}
41076b4cac81SBjoern A. Zeeb 
41086b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
41096b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
41106b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
41116b4cac81SBjoern A. Zeeb 		    c, chan);
41126b4cac81SBjoern A. Zeeb 		return;
41136b4cac81SBjoern A. Zeeb 	}
41146b4cac81SBjoern A. Zeeb 
41156b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
41166b4cac81SBjoern A. Zeeb 	IMPROVE();
41176b4cac81SBjoern A. Zeeb 
41186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41194a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
41209fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
412111450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
41229fb91463SBjoern A. Zeeb #endif
41234a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
41246b4cac81SBjoern A. Zeeb 
41256b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
41266b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
41276b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
41286b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
41296b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
41306b4cac81SBjoern A. Zeeb 		IMPROVE();
41316b4cac81SBjoern A. Zeeb 	} else {
41326b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
41336b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
41346b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
41356b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
41366b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
41376b4cac81SBjoern A. Zeeb 	}
41386b4cac81SBjoern A. Zeeb 
41396b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
41406b4cac81SBjoern A. Zeeb 	IMPROVE();
41416b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
41426b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
41436b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
41446b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
41456b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
41466b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
41476b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
41486b4cac81SBjoern A. Zeeb 			    error);
41496b4cac81SBjoern A. Zeeb 	}
41506b4cac81SBjoern A. Zeeb }
41516b4cac81SBjoern A. Zeeb 
41526b4cac81SBjoern A. Zeeb static struct ieee80211_node *
41536b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
41546b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
41556b4cac81SBjoern A. Zeeb {
41566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
41576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41584f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
41596b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
41616b4cac81SBjoern A. Zeeb 
41626b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
41636b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41646b4cac81SBjoern A. Zeeb 
41656b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
41664f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
41674f61ef8bSBjoern A. Zeeb 		return (NULL);
41684f61ef8bSBjoern A. Zeeb 
41696b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
41706b4cac81SBjoern A. Zeeb 	if (ni == NULL)
41716b4cac81SBjoern A. Zeeb 		return (NULL);
41726b4cac81SBjoern A. Zeeb 
41736b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41744f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
41756b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
41766b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
41776b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
41786b4cac81SBjoern A. Zeeb 		return (NULL);
41796b4cac81SBjoern A. Zeeb 	}
41806b4cac81SBjoern A. Zeeb 
41816b4cac81SBjoern A. Zeeb 	return (ni);
41826b4cac81SBjoern A. Zeeb }
41836b4cac81SBjoern A. Zeeb 
41846b4cac81SBjoern A. Zeeb static int
41856b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
41866b4cac81SBjoern A. Zeeb {
41876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
41886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41896b4cac81SBjoern A. Zeeb 	int error;
41906b4cac81SBjoern A. Zeeb 
41916b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
41926b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41936b4cac81SBjoern A. Zeeb 
41946b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
41956b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
41966b4cac81SBjoern A. Zeeb 		if (error != 0)
41976b4cac81SBjoern A. Zeeb 			return (error);
41986b4cac81SBjoern A. Zeeb 	}
41996b4cac81SBjoern A. Zeeb 
42006b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
42016b4cac81SBjoern A. Zeeb 	IMPROVE();
42026b4cac81SBjoern A. Zeeb 
42036b4cac81SBjoern A. Zeeb 	return (0);
42046b4cac81SBjoern A. Zeeb }
42056b4cac81SBjoern A. Zeeb 
42066b4cac81SBjoern A. Zeeb static void
42076b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
42086b4cac81SBjoern A. Zeeb {
42096b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42106b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42116b4cac81SBjoern A. Zeeb 
42126b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42146b4cac81SBjoern A. Zeeb 
42156b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
42166b4cac81SBjoern A. Zeeb 	IMPROVE();
42176b4cac81SBjoern A. Zeeb 
42186b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
42196b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
42206b4cac81SBjoern A. Zeeb }
42216b4cac81SBjoern A. Zeeb 
42226b4cac81SBjoern A. Zeeb static void
42236b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
42246b4cac81SBjoern A. Zeeb {
42256b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42276b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42286b4cac81SBjoern A. Zeeb 
42296b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42306b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42316b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
42326b4cac81SBjoern A. Zeeb 
42330936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
42346b4cac81SBjoern A. Zeeb 
42350936c648SBjoern A. Zeeb 	/*
42360936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
42370936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
42380936c648SBjoern A. Zeeb 	 */
42390936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
42406b4cac81SBjoern A. Zeeb 
42416b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
42426b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
42436b4cac81SBjoern A. Zeeb }
42446b4cac81SBjoern A. Zeeb 
42456b4cac81SBjoern A. Zeeb static int
42466b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
42476b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
42486b4cac81SBjoern A. Zeeb {
42496b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42506b4cac81SBjoern A. Zeeb 
42516b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4252fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
42532372f8ccSBjoern A. Zeeb #if 0
425488665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
42552372f8ccSBjoern A. Zeeb #else
42562372f8ccSBjoern A. Zeeb 	/*
42572372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
42582372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
42592372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
42602372f8ccSBjoern A. Zeeb 	 */
42612372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
42622372f8ccSBjoern A. Zeeb #endif
4263fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4264fa4e4257SBjoern A. Zeeb 		/*
4265fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4266fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4267fa4e4257SBjoern A. Zeeb 		 */
42680936c648SBjoern A. Zeeb 		m_free(m);
42690936c648SBjoern A. Zeeb 		return (ENETDOWN);
42700936c648SBjoern A. Zeeb 	}
42716b4cac81SBjoern A. Zeeb 
42726b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
42736b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4274fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4275fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
42766b4cac81SBjoern A. Zeeb 
42779d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
42789d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
42796b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
42806b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
42816b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
42829d9ba2b7SBjoern A. Zeeb #endif
42836b4cac81SBjoern A. Zeeb 
42846b4cac81SBjoern A. Zeeb 	return (0);
42856b4cac81SBjoern A. Zeeb }
42866b4cac81SBjoern A. Zeeb 
428711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
428811db70b6SBjoern A. Zeeb static int
428911db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
429011db70b6SBjoern A. Zeeb     struct sk_buff *skb)
429111db70b6SBjoern A. Zeeb {
429211db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
429311db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
429411db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
429511db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
429611db70b6SBjoern A. Zeeb 	uint8_t *p;
429711db70b6SBjoern A. Zeeb 
429811db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
429911db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
430011db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
430111db70b6SBjoern A. Zeeb 
430211db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
430311db70b6SBjoern A. Zeeb 
430411db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
430511db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
430611db70b6SBjoern A. Zeeb 
430711db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
430811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
430911db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
431011db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
431111db70b6SBjoern A. Zeeb 		return (ENXIO);
431211db70b6SBjoern A. Zeeb 	}
431311db70b6SBjoern A. Zeeb 
431411db70b6SBjoern A. Zeeb 
431511db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
431611db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
431711db70b6SBjoern A. Zeeb 
431811db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
431911db70b6SBjoern A. Zeeb 
432011db70b6SBjoern A. Zeeb 	/*
432111db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
432211db70b6SBjoern A. Zeeb 	 * or if we are done?
432311db70b6SBjoern A. Zeeb 	 */
432411db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
432511db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
432611db70b6SBjoern A. Zeeb 	    /* MFP */
432711db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
432811db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
432911db70b6SBjoern A. Zeeb 			return (0);
433011db70b6SBjoern A. Zeeb 
433111db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
433211db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
433311db70b6SBjoern A. Zeeb 		return (ENOSPC);
433411db70b6SBjoern A. Zeeb 
433511db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
433611db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
433711db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
433811db70b6SBjoern A. Zeeb 
433911db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
434011db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
434111db70b6SBjoern A. Zeeb 		return (0);
434211db70b6SBjoern A. Zeeb 
434311db70b6SBjoern A. Zeeb 	p += hdrlen;
434411db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
434511db70b6SBjoern A. Zeeb 
434611db70b6SBjoern A. Zeeb 	return (0);
434711db70b6SBjoern A. Zeeb }
434811db70b6SBjoern A. Zeeb #endif
434911db70b6SBjoern A. Zeeb 
43506b4cac81SBjoern A. Zeeb static void
43516b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
43526b4cac81SBjoern A. Zeeb {
43536b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
43546b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
43556b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
43566b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
43576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43596b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43606b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
43616b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
43626b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
43636b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
43646b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
43656b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4366e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4367ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
43686b4cac81SBjoern A. Zeeb 	void *buf;
436911db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4370e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
43716b4cac81SBjoern A. Zeeb 
43726b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
43736b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
43749d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
43756b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
43766b4cac81SBjoern A. Zeeb #endif
43776b4cac81SBjoern A. Zeeb 
43786b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4379b35f6cd0SBjoern A. Zeeb 	k = NULL;
438011db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
43816b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
43826b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
438311db70b6SBjoern A. Zeeb 
438411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
438511db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
438611db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
438711db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
438811db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
438911db70b6SBjoern A. Zeeb 		}
439011db70b6SBjoern A. Zeeb #endif
439111db70b6SBjoern A. Zeeb 
439211db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
439311db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
43946b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
43956b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
43966b4cac81SBjoern A. Zeeb 			if (k == NULL) {
43976b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
43986b4cac81SBjoern A. Zeeb 				m_freem(m);
43996b4cac81SBjoern A. Zeeb 				return;
44006b4cac81SBjoern A. Zeeb 			}
44016b4cac81SBjoern A. Zeeb 		}
440211db70b6SBjoern A. Zeeb 	}
44036b4cac81SBjoern A. Zeeb 
44046b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44056b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44076b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
44086b4cac81SBjoern A. Zeeb 
44096b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
44106b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
44116b4cac81SBjoern A. Zeeb 
44126b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
44136b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
44146b4cac81SBjoern A. Zeeb 		if (k != NULL)
44156b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
44166b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
44176b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
44186b4cac81SBjoern A. Zeeb 		IMPROVE();
44196b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
44206b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
44216b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
44226b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
44236b4cac81SBjoern A. Zeeb 		}
44246b4cac81SBjoern A. Zeeb 
44256b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
44266b4cac81SBjoern A. Zeeb 	}
44276b4cac81SBjoern A. Zeeb 
44286b4cac81SBjoern A. Zeeb 	/*
44296b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
44306b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
44313d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
44323d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
44336b4cac81SBjoern A. Zeeb 	 */
44346b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
44356b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4436bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4437bcf1d8eeSBjoern A. Zeeb 
4438bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4439bcf1d8eeSBjoern A. Zeeb 			int tid;
4440bcf1d8eeSBjoern A. Zeeb 
4441bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4442bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4443bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4444bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4445bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4446bcf1d8eeSBjoern A. Zeeb 					continue;
4447bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4448bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4449bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4450bcf1d8eeSBjoern A. Zeeb 			}
4451bcf1d8eeSBjoern A. Zeeb 		}
44526b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
44536b4cac81SBjoern A. Zeeb 		m_freem(m);
44546b4cac81SBjoern A. Zeeb 		return;
44556b4cac81SBjoern A. Zeeb 	}
44566b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
44576b4cac81SBjoern A. Zeeb 
44586b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
44596b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
44606b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
44616b4cac81SBjoern A. Zeeb 	skb->m = m;
44626b4cac81SBjoern A. Zeeb #if 0
44636b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
44646b4cac81SBjoern A. Zeeb #else
44656b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
44666b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
44676b4cac81SBjoern A. Zeeb #endif
44686b4cac81SBjoern A. Zeeb 	/* Save the ni. */
44696b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
44706b4cac81SBjoern A. Zeeb 
44716b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
44726b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
44736b4cac81SBjoern A. Zeeb 
4474e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4475e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4476e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
44771665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
44781665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
44791665ef97SBjoern A. Zeeb 			skb->priority = 7;
44801665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
44811665ef97SBjoern A. Zeeb 		} else {
44821665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
44831665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4484e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4485e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
44861665ef97SBjoern A. Zeeb 		}
4487e3a0b120SBjoern A. Zeeb 	} else {
4488e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4489fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4490e3a0b120SBjoern A. Zeeb 	}
44916b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
44926b4cac81SBjoern A. Zeeb 
44936b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
44946b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
44956b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
44966b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
44976b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
44986b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4499e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
45006b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
45016b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
45026b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
45036b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
45049fb91463SBjoern A. Zeeb #ifdef __notyet__
45059fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45069fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
45079fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
45089fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
45099fb91463SBjoern A. Zeeb #endif
45109fb91463SBjoern A. Zeeb #endif
45116b4cac81SBjoern A. Zeeb 
45126b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4513b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
451411db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
451511db70b6SBjoern A. Zeeb 		int error;
451611db70b6SBjoern A. Zeeb 
451711db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
451811db70b6SBjoern A. Zeeb 		if (error != 0) {
451911db70b6SBjoern A. Zeeb 			/*
452011db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
452111db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
452211db70b6SBjoern A. Zeeb 			 */
452311db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
452411db70b6SBjoern A. Zeeb 			return;
452511db70b6SBjoern A. Zeeb 		}
452611db70b6SBjoern A. Zeeb 	}
45276b4cac81SBjoern A. Zeeb #endif
45286b4cac81SBjoern A. Zeeb 
45296b4cac81SBjoern A. Zeeb 	IMPROVE();
45306b4cac81SBjoern A. Zeeb 
4531e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4532e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4533e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4534e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4535e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4536d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4537e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4538e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4539e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4540e3a0b120SBjoern A. Zeeb 	}
4541e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4542d0d29110SBjoern A. Zeeb 		goto ops_tx;
4543e3a0b120SBjoern A. Zeeb 
4544d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4545d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4546d0d29110SBjoern A. Zeeb 
4547eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
45486b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
45499d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4551d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4552d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4553d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4554fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4555d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4556d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4557d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4558d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
45599d9ba2b7SBjoern A. Zeeb #endif
4560eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
456145bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4562d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
456345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
45646b4cac81SBjoern A. Zeeb 	return;
45656b4cac81SBjoern A. Zeeb 
45666b4cac81SBjoern A. Zeeb ops_tx:
45679d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45689d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4569d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4570d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
45716b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
45726b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
45739d9ba2b7SBjoern A. Zeeb #endif
45746b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
45756b4cac81SBjoern A. Zeeb 	control.sta = sta;
457645bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
45776b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
457845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
45796b4cac81SBjoern A. Zeeb }
45806b4cac81SBjoern A. Zeeb 
45816b4cac81SBjoern A. Zeeb static void
45826b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
45836b4cac81SBjoern A. Zeeb {
45846b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45856b4cac81SBjoern A. Zeeb 	struct mbufq mq;
45866b4cac81SBjoern A. Zeeb 	struct mbuf *m;
458788665349SBjoern A. Zeeb 	bool shall_tx;
45886b4cac81SBjoern A. Zeeb 
45896b4cac81SBjoern A. Zeeb 	lsta = ctx;
45906b4cac81SBjoern A. Zeeb 
45919d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45929d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
45936b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
45949d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
45956b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
45969d9ba2b7SBjoern A. Zeeb #endif
45976b4cac81SBjoern A. Zeeb 
45986b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
45996b4cac81SBjoern A. Zeeb 
4600fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4601fa4e4257SBjoern A. Zeeb 	/*
4602fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
460388665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
460488665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
460588665349SBjoern A. Zeeb 	 * point to try to TX.
460688665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
460788665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4608fa4e4257SBjoern A. Zeeb 	 */
46092372f8ccSBjoern A. Zeeb #if 0
461088665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
46112372f8ccSBjoern A. Zeeb #else
46122372f8ccSBjoern A. Zeeb 	/*
46132372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46142372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46152372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46162372f8ccSBjoern A. Zeeb 	 */
46172372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
46182372f8ccSBjoern A. Zeeb #endif
461988665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
46206b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
462188665349SBjoern A. Zeeb 	/*
462288665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
462388665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
462488665349SBjoern A. Zeeb 	 */
4625fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46266b4cac81SBjoern A. Zeeb 
46276b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
46286b4cac81SBjoern A. Zeeb 	while (m != NULL) {
46296b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
46306b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
46316b4cac81SBjoern A. Zeeb 	}
46326b4cac81SBjoern A. Zeeb }
46336b4cac81SBjoern A. Zeeb 
46346b4cac81SBjoern A. Zeeb static int
46356b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
46366b4cac81SBjoern A. Zeeb {
46376b4cac81SBjoern A. Zeeb 
46386b4cac81SBjoern A. Zeeb 	/* XXX TODO */
46396b4cac81SBjoern A. Zeeb 	IMPROVE();
46406b4cac81SBjoern A. Zeeb 
46416b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
46426b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
46436b4cac81SBjoern A. Zeeb 
46446b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
46456b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
46466b4cac81SBjoern A. Zeeb }
46476b4cac81SBjoern A. Zeeb 
46489fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46499fb91463SBjoern A. Zeeb static int
46509fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
46519fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
46529fb91463SBjoern A. Zeeb {
46539fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46549fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46559fb91463SBjoern A. Zeeb 
46569fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46579fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46589fb91463SBjoern A. Zeeb 
4659310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
46609fb91463SBjoern A. Zeeb 
46619fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
46629fb91463SBjoern A. Zeeb }
46639fb91463SBjoern A. Zeeb 
46649fb91463SBjoern A. Zeeb static int
46659fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
46669fb91463SBjoern A. Zeeb {
46679fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46689fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46699fb91463SBjoern A. Zeeb 
46709fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46719fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46729fb91463SBjoern A. Zeeb 
4673310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
46749fb91463SBjoern A. Zeeb 
46759fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
46769fb91463SBjoern A. Zeeb }
46779fb91463SBjoern A. Zeeb 
46789fb91463SBjoern A. Zeeb 
46799fb91463SBjoern A. Zeeb static int
46809fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
46819fb91463SBjoern A. Zeeb {
46829fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46839fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46849fb91463SBjoern A. Zeeb 
46859fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46869fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46879fb91463SBjoern A. Zeeb 
4688310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
46899fb91463SBjoern A. Zeeb 
46909fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
46919fb91463SBjoern A. Zeeb }
46929fb91463SBjoern A. Zeeb 
4693310743c4SBjoern A. Zeeb /*
4694310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4695310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4696310743c4SBjoern A. Zeeb  *
4697310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4698310743c4SBjoern A. Zeeb  */
46999fb91463SBjoern A. Zeeb static int
47009fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
47019fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
47029fb91463SBjoern A. Zeeb {
47039fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47049fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4705310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4706310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4707310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4708310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4709310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4710310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4711310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4712310743c4SBjoern A. Zeeb 	int error;
47139fb91463SBjoern A. Zeeb 
47149fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47159fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4716310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4717310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4718310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4719310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4720310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4721310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
47229fb91463SBjoern A. Zeeb 
4723310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4724310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4725310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4726310743c4SBjoern A. Zeeb 		return (0);
4727310743c4SBjoern A. Zeeb 	}
4728310743c4SBjoern A. Zeeb 
4729310743c4SBjoern A. Zeeb 	params.sta = sta;
4730310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4731310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4732310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4733310743c4SBjoern A. Zeeb 	params.timeout = 0;
4734310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4735310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4736310743c4SBjoern A. Zeeb 	params.amsdu = false;
4737310743c4SBjoern A. Zeeb 
4738310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4739310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4740310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4741310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4742310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4743310743c4SBjoern A. Zeeb 	if (error != 0) {
4744310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4745310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4746310743c4SBjoern A. Zeeb 		return (0);
4747310743c4SBjoern A. Zeeb 	}
47489fb91463SBjoern A. Zeeb 
47499fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
47509fb91463SBjoern A. Zeeb }
47519fb91463SBjoern A. Zeeb 
4752310743c4SBjoern A. Zeeb /*
4753310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4754310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4755310743c4SBjoern A. Zeeb  *
4756310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4757310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4758310743c4SBjoern A. Zeeb  */
47599fb91463SBjoern A. Zeeb static int
47609fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
47619fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
47629fb91463SBjoern A. Zeeb {
47639fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47649fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4765310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4766310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4767310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4768310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4769310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4770310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4771310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4772310743c4SBjoern A. Zeeb 	int error;
47739fb91463SBjoern A. Zeeb 
47749fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47759fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4776310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4777310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4778310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4779310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4780310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4781310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
47829fb91463SBjoern A. Zeeb 
4783310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4784310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4785310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4786310743c4SBjoern A. Zeeb 		return (0);
4787310743c4SBjoern A. Zeeb 	}
4788310743c4SBjoern A. Zeeb 
4789310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4790310743c4SBjoern A. Zeeb 		params.sta = sta;
4791310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4792310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4793310743c4SBjoern A. Zeeb 		params.timeout = 0;
4794310743c4SBjoern A. Zeeb 		params.ssn = 0;
4795310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4796310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4797310743c4SBjoern A. Zeeb 			params.amsdu = true;
4798310743c4SBjoern A. Zeeb 		else
4799310743c4SBjoern A. Zeeb 			params.amsdu = false;
4800310743c4SBjoern A. Zeeb 	} else {
4801310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4802310743c4SBjoern A. Zeeb 		params.sta = sta;
4803310743c4SBjoern A. Zeeb 		switch (status) {
4804310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4805310743c4SBjoern A. Zeeb 		default:
4806310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4807310743c4SBjoern A. Zeeb 			break;
4808310743c4SBjoern A. Zeeb 		}
4809310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4810310743c4SBjoern A. Zeeb 		params.timeout = 0;
4811310743c4SBjoern A. Zeeb 		params.ssn = 0;
4812310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4813310743c4SBjoern A. Zeeb 		params.amsdu = false;
4814310743c4SBjoern A. Zeeb 	}
4815310743c4SBjoern A. Zeeb 
4816310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4817310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4818310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4819310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4820310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4821310743c4SBjoern A. Zeeb 	if (error != 0) {
4822310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4823310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4824310743c4SBjoern A. Zeeb 		return (0);
4825310743c4SBjoern A. Zeeb 	}
4826310743c4SBjoern A. Zeeb 
4827310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
48289fb91463SBjoern A. Zeeb 
48299fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
48309fb91463SBjoern A. Zeeb }
48319fb91463SBjoern A. Zeeb 
4832310743c4SBjoern A. Zeeb /*
4833310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4834310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4835310743c4SBjoern A. Zeeb  */
48369fb91463SBjoern A. Zeeb static void
48379fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
48389fb91463SBjoern A. Zeeb {
48399fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48409fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4841310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4842310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4843310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4844310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4845310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4846310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4847310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4848310743c4SBjoern A. Zeeb 	int error;
48499fb91463SBjoern A. Zeeb 
48509fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48519fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4852310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4853310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4854310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4855310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4856310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4857310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48589fb91463SBjoern A. Zeeb 
4859310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4860310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4861310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4862310743c4SBjoern A. Zeeb 		goto n80211;
4863310743c4SBjoern A. Zeeb 	}
48649fb91463SBjoern A. Zeeb 
4865310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4866310743c4SBjoern A. Zeeb 	params.sta = sta;
4867310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4868310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4869310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4870310743c4SBjoern A. Zeeb 	params.timeout = 0;
4871310743c4SBjoern A. Zeeb 	params.ssn = 0;
4872310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4873310743c4SBjoern A. Zeeb 	params.amsdu = false;
4874310743c4SBjoern A. Zeeb 
4875310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4876310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4877310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4878310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4879310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4880310743c4SBjoern A. Zeeb 	if (error != 0) {
4881310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4882310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4883310743c4SBjoern A. Zeeb 		goto n80211;
4884310743c4SBjoern A. Zeeb 	}
4885310743c4SBjoern A. Zeeb 
4886310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4887310743c4SBjoern A. Zeeb 
4888310743c4SBjoern A. Zeeb n80211:
48899fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
48909fb91463SBjoern A. Zeeb }
48919fb91463SBjoern A. Zeeb 
48929fb91463SBjoern A. Zeeb static void
48939fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
48949fb91463SBjoern A. Zeeb {
48959fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48969fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48979fb91463SBjoern A. Zeeb 
48989fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48999fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49009fb91463SBjoern A. Zeeb 
49019fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49029fb91463SBjoern A. Zeeb 
49039fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
49049fb91463SBjoern A. Zeeb }
49059fb91463SBjoern A. Zeeb 
49069fb91463SBjoern A. Zeeb static void
49079fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
49089fb91463SBjoern A. Zeeb     int status)
49099fb91463SBjoern A. Zeeb {
49109fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49119fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49129fb91463SBjoern A. Zeeb 
49139fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49149fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49159fb91463SBjoern A. Zeeb 
49169fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49179fb91463SBjoern A. Zeeb 
49189fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
49199fb91463SBjoern A. Zeeb }
49209fb91463SBjoern A. Zeeb 
49219fb91463SBjoern A. Zeeb static int
49229fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
49239fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
49249fb91463SBjoern A. Zeeb {
49259fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49269fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49279fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
49289fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
49299fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
49309fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
49319fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49329fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4933310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
49349fb91463SBjoern A. Zeeb 	int error;
49359fb91463SBjoern A. Zeeb 
49369fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49379fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49389fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
49399fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
49409fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
49419fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
49429fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
49439fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49449fb91463SBjoern A. Zeeb 
4945310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
4946310743c4SBjoern A. Zeeb 
4947310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4948310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
4949310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
4950310743c4SBjoern A. Zeeb 		return (-ENXIO);
4951310743c4SBjoern A. Zeeb 	}
4952310743c4SBjoern A. Zeeb 
49539fb91463SBjoern A. Zeeb 	params.sta = sta;
49549fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
49559fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
49569fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
49579fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
49589fb91463SBjoern A. Zeeb 	else
49599fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4960310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
4961310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
49629fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
49639fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
49649fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
49659fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4966310743c4SBjoern A. Zeeb 
4967310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
4968310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
4969310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
4970310743c4SBjoern A. Zeeb 		params.amsdu = true;
4971310743c4SBjoern A. Zeeb 	else
49729fb91463SBjoern A. Zeeb 		params.amsdu = false;
49739fb91463SBjoern A. Zeeb 
4974310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
49759fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4976310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
49779fb91463SBjoern A. Zeeb 	if (error != 0) {
49789fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
49799fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
49809fb91463SBjoern A. Zeeb 		return (error);
49819fb91463SBjoern A. Zeeb 	}
4982310743c4SBjoern A. Zeeb 
4983310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
4984310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
4985310743c4SBjoern A. Zeeb 	}
4986310743c4SBjoern A. Zeeb 
49879fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
49889fb91463SBjoern A. Zeeb 
49899fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
49909fb91463SBjoern A. Zeeb 	return (error);
49919fb91463SBjoern A. Zeeb }
49929fb91463SBjoern A. Zeeb 
49939fb91463SBjoern A. Zeeb static void
49949fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
49959fb91463SBjoern A. Zeeb {
49969fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49979fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49989fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
49999fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50009fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50019fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50029fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50039fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5004310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50059fb91463SBjoern A. Zeeb 	int error;
50069fb91463SBjoern A. Zeeb 	uint8_t tid;
50079fb91463SBjoern A. Zeeb 
50089fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50099fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50109fb91463SBjoern A. Zeeb 
50119fb91463SBjoern A. Zeeb 	/*
50129fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
50139fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
50149fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
50159fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
50169fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
50179fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
50189fb91463SBjoern A. Zeeb 	 * track some state itself.
50199fb91463SBjoern A. Zeeb 	 */
50209fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
50219fb91463SBjoern A. Zeeb 		goto net80211_only;
50229fb91463SBjoern A. Zeeb 
50239fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
50249fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
50259fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
50269fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50279fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
50289fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50299fb91463SBjoern A. Zeeb 
50309fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
50319fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
50329fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
50339fb91463SBjoern A. Zeeb 			break;
50349fb91463SBjoern A. Zeeb 	}
50359fb91463SBjoern A. Zeeb 
50369fb91463SBjoern A. Zeeb 	params.sta = sta;
50379fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
50389fb91463SBjoern A. Zeeb 	params.buf_size = 0;
50399fb91463SBjoern A. Zeeb 	params.timeout = 0;
50409fb91463SBjoern A. Zeeb 	params.ssn = 0;
50419fb91463SBjoern A. Zeeb 	params.tid = tid;
50429fb91463SBjoern A. Zeeb 	params.amsdu = false;
50439fb91463SBjoern A. Zeeb 
5044310743c4SBjoern A. Zeeb 	// IEEE80211_UNLOCK(ic);
5045310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
50469fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5047310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
5048310743c4SBjoern A. Zeeb 	// IEEE80211_LOCK(ic);
50499fb91463SBjoern A. Zeeb 	if (error != 0)
50509fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
50519fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50529fb91463SBjoern A. Zeeb 
50539fb91463SBjoern A. Zeeb net80211_only:
50549fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
50559fb91463SBjoern A. Zeeb }
50569fb91463SBjoern A. Zeeb #endif
50579fb91463SBjoern A. Zeeb 
50589fb91463SBjoern A. Zeeb static void
50599fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
50609fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
50619fb91463SBjoern A. Zeeb {
50629fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50639fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
50649fb91463SBjoern A. Zeeb 
50659fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
50669fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
50679fb91463SBjoern A. Zeeb 		return;
50689fb91463SBjoern A. Zeeb 
50699fb91463SBjoern A. Zeeb 	switch (band) {
50709fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
50719fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
50729fb91463SBjoern A. Zeeb 		break;
50739fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
50749fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
50759fb91463SBjoern A. Zeeb 		break;
50769fb91463SBjoern A. Zeeb 	default:
50779fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
50789fb91463SBjoern A. Zeeb 		return;
50799fb91463SBjoern A. Zeeb 	}
50809fb91463SBjoern A. Zeeb 
50819fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
50829fb91463SBjoern A. Zeeb 
50839fb91463SBjoern A. Zeeb 	/*
50849fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
50859fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
50869fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
50879fb91463SBjoern A. Zeeb 	 */
50889fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
50899fb91463SBjoern A. Zeeb 
50909fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
50919fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
50929fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
50939fb91463SBjoern A. Zeeb #ifdef __notyet__
50949fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
50959fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
50969fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
50979fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
50989fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
50999fb91463SBjoern A. Zeeb #endif
51009fb91463SBjoern A. Zeeb 
51019fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
51029fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
51039fb91463SBjoern A. Zeeb 
51049fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
51059fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
51069fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
51079fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
51089fb91463SBjoern A. Zeeb #endif
51099fb91463SBjoern A. Zeeb }
51109fb91463SBjoern A. Zeeb 
51116b4cac81SBjoern A. Zeeb static void
51126b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
51136b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
51146b4cac81SBjoern A. Zeeb {
51156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51166b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
51176b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
51186b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
51196b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
51206b4cac81SBjoern A. Zeeb 
51216b4cac81SBjoern A. Zeeb 	/* Channels */
51226b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
51236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51246b4cac81SBjoern A. Zeeb 
51256b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
51266b4cac81SBjoern A. Zeeb 	nchans = 0;
51276b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
51286b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
51296b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
51306b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
51316b4cac81SBjoern A. Zeeb 		chan_flags = 0;
51326b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
51336b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
51349fb91463SBjoern A. Zeeb 
51359fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
51366b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
51379fb91463SBjoern A. Zeeb 
51389fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
51399fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
51406b4cac81SBjoern A. Zeeb 
51416b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5142cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
51436b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
51446b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
51456b4cac81SBjoern A. Zeeb 
51466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
51473f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
51483f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
51496b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
51506b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
51516b4cac81SBjoern A. Zeeb 				continue;
51526b4cac81SBjoern A. Zeeb 			}
51536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
51546b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
51556b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
51566b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
51576b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
51586b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
51596b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
51606b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
51616b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
51626b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
51636b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
51646b4cac81SBjoern A. Zeeb 
51656b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
51666b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
51676b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
51685856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5169cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5170cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
51713f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
51723f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
51736b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
51746b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
51756b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5176cee56e77SBjoern A. Zeeb 			if (error != 0)
51776b4cac81SBjoern A. Zeeb 				break;
51786b4cac81SBjoern A. Zeeb 		}
51796b4cac81SBjoern A. Zeeb 	}
51806b4cac81SBjoern A. Zeeb 
51816b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
51826b4cac81SBjoern A. Zeeb 	nchans = 0;
51836b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
51846b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
51856b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
51866b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
51876b4cac81SBjoern A. Zeeb 		chan_flags = 0;
51886b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
51899fb91463SBjoern A. Zeeb 
51909fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
51919fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
51929fb91463SBjoern A. Zeeb 
51939fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
51946b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
51956b4cac81SBjoern A. Zeeb 
51966b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5197562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
51986b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5199ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5200ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
52016b4cac81SBjoern A. Zeeb 
52026b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
52036b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
52046b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5205562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52066b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
52076b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5208562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52096b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
52106b4cac81SBjoern A. Zeeb 		}
52116b4cac81SBjoern A. Zeeb #endif
52126b4cac81SBjoern A. Zeeb 
52136b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5214cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52156b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
52166b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
52176b4cac81SBjoern A. Zeeb 
52186b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
52193f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
52203f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
52216b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
52226b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
52236b4cac81SBjoern A. Zeeb 				continue;
52246b4cac81SBjoern A. Zeeb 			}
52256b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
52266b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
52276b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
52286b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
52296b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
52306b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
52316b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
52326b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
52336b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
52346b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
52356b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
52366b4cac81SBjoern A. Zeeb 
52376b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
52386b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
52396b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
52405856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5241cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5242cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
52433f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
52443f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
52456b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
52466b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
52476b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5248cee56e77SBjoern A. Zeeb 			if (error != 0)
52496b4cac81SBjoern A. Zeeb 				break;
52506b4cac81SBjoern A. Zeeb 		}
52516b4cac81SBjoern A. Zeeb 	}
52526b4cac81SBjoern A. Zeeb }
52536b4cac81SBjoern A. Zeeb 
52546b4cac81SBjoern A. Zeeb static void *
52556b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
52566b4cac81SBjoern A. Zeeb {
52576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
52586b4cac81SBjoern A. Zeeb 
52596b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
52606b4cac81SBjoern A. Zeeb 
52616b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
52626b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
52636b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
52646b4cac81SBjoern A. Zeeb 
52656b4cac81SBjoern A. Zeeb 	return (ic);
52666b4cac81SBjoern A. Zeeb }
52676b4cac81SBjoern A. Zeeb 
52686b4cac81SBjoern A. Zeeb struct ieee80211_hw *
52696b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
52706b4cac81SBjoern A. Zeeb {
52716b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52736b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5274a5ae63edSBjoern A. Zeeb 	int ac;
52756b4cac81SBjoern A. Zeeb 
52766b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
52776b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
52786b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
52796b4cac81SBjoern A. Zeeb 		return (NULL);
52806b4cac81SBjoern A. Zeeb 
52816b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
52826b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5283652e22d3SBjoern A. Zeeb 
52848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
52858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5286eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
52878891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
52886b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5289a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5290a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5291a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5292a5ae63edSBjoern A. Zeeb 	}
52936b4cac81SBjoern A. Zeeb 
5294759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5295759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5296759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5297759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5298759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5299759a996dSBjoern A. Zeeb 
53006b4cac81SBjoern A. Zeeb 	/*
53016b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
53026b4cac81SBjoern A. Zeeb 	 * not initialized.
53036b4cac81SBjoern A. Zeeb 	 */
53046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53056b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5306086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
53076b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
53086b4cac81SBjoern A. Zeeb 
53096b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
53106b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
53116b4cac81SBjoern A. Zeeb 
53126b4cac81SBjoern A. Zeeb 	IMPROVE();
53136b4cac81SBjoern A. Zeeb 
53146b4cac81SBjoern A. Zeeb 	return (hw);
53156b4cac81SBjoern A. Zeeb }
53166b4cac81SBjoern A. Zeeb 
53176b4cac81SBjoern A. Zeeb void
53186b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
53196b4cac81SBjoern A. Zeeb {
53206b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5321759a996dSBjoern A. Zeeb 	struct mbuf *m;
53226b4cac81SBjoern A. Zeeb 
53236b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53246b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
53256b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
53266b4cac81SBjoern A. Zeeb 
5327759a996dSBjoern A. Zeeb 	/*
5328759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5329759a996dSBjoern A. Zeeb 	 */
5330759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5331759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5332759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5333759a996dSBjoern A. Zeeb 
5334759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5335759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5336759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5337759a996dSBjoern A. Zeeb 
5338759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5339759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5340759a996dSBjoern A. Zeeb 	while (m != NULL) {
5341759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5342759a996dSBjoern A. Zeeb 
5343759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5344759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5345759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5346759a996dSBjoern A. Zeeb 
5347759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5348759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5349759a996dSBjoern A. Zeeb 		}
5350759a996dSBjoern A. Zeeb 		m_freem(m);
5351759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5352759a996dSBjoern A. Zeeb 	}
5353759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5354759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5355759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5356759a996dSBjoern A. Zeeb 
53576b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5358eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
53598ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5360eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5361eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
53626b4cac81SBjoern A. Zeeb 	IMPROVE();
53636b4cac81SBjoern A. Zeeb }
53646b4cac81SBjoern A. Zeeb 
53656b4cac81SBjoern A. Zeeb void
53666b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
53676b4cac81SBjoern A. Zeeb {
53686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53696b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53706b4cac81SBjoern A. Zeeb 
53716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53726b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
53736b4cac81SBjoern A. Zeeb 
53746b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
53756b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
53766b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
53776b4cac81SBjoern A. Zeeb 
53786b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
53796b4cac81SBjoern A. Zeeb }
53806b4cac81SBjoern A. Zeeb 
53816b4cac81SBjoern A. Zeeb struct ieee80211_hw *
53826b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
53836b4cac81SBjoern A. Zeeb {
53846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53856b4cac81SBjoern A. Zeeb 
53866b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
53876b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
53886b4cac81SBjoern A. Zeeb }
53896b4cac81SBjoern A. Zeeb 
53906b4cac81SBjoern A. Zeeb static void
53916b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
53926b4cac81SBjoern A. Zeeb {
53936b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53946b4cac81SBjoern A. Zeeb 
53956b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
53966b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
53976b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
53986b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
53996b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
54006b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
54016b4cac81SBjoern A. Zeeb }
54026b4cac81SBjoern A. Zeeb 
54032e183d99SBjoern A. Zeeb int
54046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
54056b4cac81SBjoern A. Zeeb {
54066b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54076b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5408c5b96b3eSBjoern A. Zeeb 	int band, i;
54096b4cac81SBjoern A. Zeeb 
54106b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54116b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54126b4cac81SBjoern A. Zeeb 
5413652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5414652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5415652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5416652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5417652e22d3SBjoern A. Zeeb 
54186b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
54196b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
54206b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
54216b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
54226b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
54236b4cac81SBjoern A. Zeeb 		/* We take the first one. */
54246b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
54256b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
54266b4cac81SBjoern A. Zeeb 	} else {
54276b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
54286b4cac81SBjoern A. Zeeb 	}
54296b4cac81SBjoern A. Zeeb 
54303d09d310SBjoern A. Zeeb #ifdef __not_yet__
54313d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
54326b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
54333d09d310SBjoern A. Zeeb #endif
54346b4cac81SBjoern A. Zeeb 
54356b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
54366b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
54376b4cac81SBjoern A. Zeeb 
54386b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
54396b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
54406b4cac81SBjoern A. Zeeb 	ic->ic_caps =
54416b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
54426b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
54436b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
54444a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
54456b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
54464a67f1dfSBjoern A. Zeeb #endif
54476b4cac81SBjoern A. Zeeb #if 0
54486b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
54496b4cac81SBjoern A. Zeeb #endif
54506b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
54516b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
54526b4cac81SBjoern A. Zeeb 	    ;
54536b4cac81SBjoern A. Zeeb #if 0
54546b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
54556b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
54566b4cac81SBjoern A. Zeeb #endif
5457cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5458cc4e78d5SBjoern A. Zeeb 		/*
5459cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5460cc4e78d5SBjoern A. Zeeb 		 *
5461cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5462cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5463cc4e78d5SBjoern A. Zeeb 		 * the flag.
5464cc4e78d5SBjoern A. Zeeb 		 */
54656b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5466a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
54676b4cac81SBjoern A. Zeeb 	}
54686b4cac81SBjoern A. Zeeb 
5469527687a9SBjoern A. Zeeb 	/*
5470527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5471527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5472527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5473527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5474527687a9SBjoern A. Zeeb 	 */
5475527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5476527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5477527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5478527687a9SBjoern A. Zeeb 
5479527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5480527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5481527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5482527687a9SBjoern A. Zeeb 		}
5483527687a9SBjoern A. Zeeb 	}
5484527687a9SBjoern A. Zeeb 
54856b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5486b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
548711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
54886b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
54896b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
54906b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
54916b4cac81SBjoern A. Zeeb 	}
54926b4cac81SBjoern A. Zeeb #endif
54936b4cac81SBjoern A. Zeeb 
54946b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
54956b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
54966b4cac81SBjoern A. Zeeb 
54976b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
54986b4cac81SBjoern A. Zeeb 
54996b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
55006b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
55016b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
55026b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
55036b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
55046b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
55056b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
55066b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
55076b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
55086b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
55096b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
55106b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
55116b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
55126b4cac81SBjoern A. Zeeb 
5513a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5514a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5515a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5516a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5517a486fbbdSBjoern A. Zeeb 
55186b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
55196b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
55206b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
55216b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
55226b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
55236b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
55246b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
55256b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
55266b4cac81SBjoern A. Zeeb 
55279fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
552886bc7259SBjoern A. Zeeb 	/*
552986bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
553086bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
553186bc7259SBjoern A. Zeeb 	 */
553286bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
55339fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
55349fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
55359fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
55369fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
55379fb91463SBjoern A. Zeeb 
55389fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
55399fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
55409fb91463SBjoern A. Zeeb 
55419fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
55429fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
55439fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
55449fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
55459fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
55469fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
55479fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
55489fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
55499fb91463SBjoern A. Zeeb 
55509fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
55519fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
55529fb91463SBjoern A. Zeeb 
55539fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
55549fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
55559fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
55569fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
555786bc7259SBjoern A. Zeeb 	}
55589fb91463SBjoern A. Zeeb #endif
55599fb91463SBjoern A. Zeeb 
55606b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
55616b4cac81SBjoern A. Zeeb 
5562c5b96b3eSBjoern A. Zeeb 	/*
5563c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5564c5b96b3eSBjoern A. Zeeb 	 * expect one.
5565d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5566d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5567c5b96b3eSBjoern A. Zeeb 	 */
5568d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5569f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5570c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5571c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5572c5b96b3eSBjoern A. Zeeb 
5573c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5574c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5575c5b96b3eSBjoern A. Zeeb 			continue;
5576c5b96b3eSBjoern A. Zeeb 
5577d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5578d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5579d9945d78SBjoern A. Zeeb 
5580f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5581f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5582f454a4a1SBjoern A. Zeeb 			continue;
5583f454a4a1SBjoern A. Zeeb 
5584c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5585c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5586c5b96b3eSBjoern A. Zeeb 
5587c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5588c5b96b3eSBjoern A. Zeeb 				continue;
5589c5b96b3eSBjoern A. Zeeb 
55904a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
55919fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
559211450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
55939fb91463SBjoern A. Zeeb #endif
5594c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5595c5b96b3eSBjoern A. Zeeb 			break;
5596c5b96b3eSBjoern A. Zeeb 		}
5597c5b96b3eSBjoern A. Zeeb 	}
5598c5b96b3eSBjoern A. Zeeb 
5599d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5600d9945d78SBjoern A. Zeeb 
5601d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5602d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5603d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5604d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5605d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5606d9945d78SBjoern A. Zeeb 	}
5607d9945d78SBjoern A. Zeeb 
5608d9945d78SBjoern A. Zeeb 	/*
5609d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5610d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5611d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5612d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5613d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5614d9945d78SBjoern A. Zeeb 	 */
5615d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5616d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5617d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
561878ca45dfSBjoern A. Zeeb 
561978ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5620d9945d78SBjoern A. Zeeb 		/*
562178ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
562278ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
562378ca45dfSBjoern A. Zeeb 		 * space in.
5624d9945d78SBjoern A. Zeeb 		 */
5625d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
562678ca45dfSBjoern A. Zeeb 	}
5627d9945d78SBjoern A. Zeeb 
56289fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
56299fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
56309fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
56319fb91463SBjoern A. Zeeb #endif
56329fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5633*1f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
56349fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
56359fb91463SBjoern A. Zeeb #endif
56369fb91463SBjoern A. Zeeb 
5637d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
563878ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
563978ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
564078ca45dfSBjoern A. Zeeb 			goto err;
5641d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
564278ca45dfSBjoern A. Zeeb 	}
5643d9945d78SBjoern A. Zeeb 
56446b4cac81SBjoern A. Zeeb 	if (bootverbose)
56456b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
56462e183d99SBjoern A. Zeeb 
56472e183d99SBjoern A. Zeeb 	return (0);
564878ca45dfSBjoern A. Zeeb err:
564978ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
565078ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
56516b4cac81SBjoern A. Zeeb }
56526b4cac81SBjoern A. Zeeb 
56536b4cac81SBjoern A. Zeeb void
56546b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
56556b4cac81SBjoern A. Zeeb {
56566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
56586b4cac81SBjoern A. Zeeb 
56596b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56606b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
56616b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
56626b4cac81SBjoern A. Zeeb }
56636b4cac81SBjoern A. Zeeb 
56646b4cac81SBjoern A. Zeeb void
56656b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
56666b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
56676b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
56686b4cac81SBjoern A. Zeeb     void *arg)
56696b4cac81SBjoern A. Zeeb {
56706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
567361a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
56746b4cac81SBjoern A. Zeeb 
56756b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56766b4cac81SBjoern A. Zeeb 
56776b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
56786b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
567961a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5680adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
56816b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
56826b4cac81SBjoern A. Zeeb 		    __func__, flags);
56836b4cac81SBjoern A. Zeeb 	}
56846b4cac81SBjoern A. Zeeb 
5685adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
56866b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
568761a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
56886b4cac81SBjoern A. Zeeb 
56896b4cac81SBjoern A. Zeeb 	if (atomic)
56908891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
56916b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
56926b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
56936b4cac81SBjoern A. Zeeb 
56946b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
56956b4cac81SBjoern A. Zeeb 
56966b4cac81SBjoern A. Zeeb 		/*
56976b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
56986b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
56996b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
57006b4cac81SBjoern A. Zeeb 		 * driver does not know about.
57016b4cac81SBjoern A. Zeeb 		 */
57026b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
57036b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
57046b4cac81SBjoern A. Zeeb 			continue;
57056b4cac81SBjoern A. Zeeb 
57066b4cac81SBjoern A. Zeeb 		/*
570761a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
570861a68e50SBjoern A. Zeeb 		 * if we haven't yet.
570961a68e50SBjoern A. Zeeb 		 */
571061a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
571161a68e50SBjoern A. Zeeb 			continue;
571261a68e50SBjoern A. Zeeb 
571361a68e50SBjoern A. Zeeb 		/*
57146b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
57156b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
57166b4cac81SBjoern A. Zeeb 		 */
57176b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
57186b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
57196b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
57206b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
57216b4cac81SBjoern A. Zeeb 	}
57226b4cac81SBjoern A. Zeeb 	if (atomic)
57238891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
57246b4cac81SBjoern A. Zeeb }
57256b4cac81SBjoern A. Zeeb 
572611db70b6SBjoern A. Zeeb static void
572711db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
572811db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
572911db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
573011db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
573111db70b6SBjoern A. Zeeb     void *arg)
573211db70b6SBjoern A. Zeeb {
573311db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
573411db70b6SBjoern A. Zeeb 		return;
573511db70b6SBjoern A. Zeeb 
573611db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
573711db70b6SBjoern A. Zeeb 		return;
573811db70b6SBjoern A. Zeeb 
573911db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
574011db70b6SBjoern A. Zeeb }
574111db70b6SBjoern A. Zeeb 
57426b4cac81SBjoern A. Zeeb void
57436b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
57446b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
57456b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
57466b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
574711db70b6SBjoern A. Zeeb     void *arg, bool rcu)
57486b4cac81SBjoern A. Zeeb {
574911db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
575011db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57516b4cac81SBjoern A. Zeeb 
575211db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
575311db70b6SBjoern A. Zeeb 
575411db70b6SBjoern A. Zeeb 	if (rcu) {
5755a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5756a8f735a6SBjoern A. Zeeb 
575711db70b6SBjoern A. Zeeb 		if (vif == NULL) {
575811db70b6SBjoern A. Zeeb 			TODO();
575911db70b6SBjoern A. Zeeb 		} else {
5760a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
576111db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
576211db70b6SBjoern A. Zeeb 				    keyix++)
576311db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
576411db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
576511db70b6SBjoern A. Zeeb 			}
576611db70b6SBjoern A. Zeeb 		}
576711db70b6SBjoern A. Zeeb 	} else {
576811db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
576911db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
577011db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
577111db70b6SBjoern A. Zeeb 
577211db70b6SBjoern A. Zeeb 		if (vif == NULL) {
577311db70b6SBjoern A. Zeeb 			TODO();
577411db70b6SBjoern A. Zeeb 		} else {
5775a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
577611db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
577711db70b6SBjoern A. Zeeb 				    keyix++)
577811db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
577911db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
578011db70b6SBjoern A. Zeeb 			}
578111db70b6SBjoern A. Zeeb 		}
578211db70b6SBjoern A. Zeeb 	}
57836b4cac81SBjoern A. Zeeb }
57846b4cac81SBjoern A. Zeeb 
57856b4cac81SBjoern A. Zeeb void
57866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
57876b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
57886b4cac81SBjoern A. Zeeb 	void *),
57896b4cac81SBjoern A. Zeeb     void *arg)
57906b4cac81SBjoern A. Zeeb {
5791c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5792c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5793c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5794c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
57956b4cac81SBjoern A. Zeeb 
5796c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5797c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5798c5e25798SBjoern A. Zeeb 
5799c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5800c5e25798SBjoern A. Zeeb 
5801c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5802c5e25798SBjoern A. Zeeb 
5803c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5804c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5805c5e25798SBjoern A. Zeeb 
5806c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5807c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5808c5e25798SBjoern A. Zeeb 			continue;
5809c5e25798SBjoern A. Zeeb 
5810c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5811c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5812c5e25798SBjoern A. Zeeb 			continue;
5813c5e25798SBjoern A. Zeeb 
5814d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5815c5e25798SBjoern A. Zeeb 	}
5816c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58176b4cac81SBjoern A. Zeeb }
58186b4cac81SBjoern A. Zeeb 
58196b4cac81SBjoern A. Zeeb void
58206b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
58216b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
58226b4cac81SBjoern A. Zeeb {
58236b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58246b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58256b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
58266b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
58276b4cac81SBjoern A. Zeeb 
58286b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
58296b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
58306b4cac81SBjoern A. Zeeb 
58316b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58326b4cac81SBjoern A. Zeeb 
58338891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
58346b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58356b4cac81SBjoern A. Zeeb 
5836a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
5837a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
58386b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
58396b4cac81SBjoern A. Zeeb 				continue;
58406b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
58416b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
58426b4cac81SBjoern A. Zeeb 		}
5843a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
58446b4cac81SBjoern A. Zeeb 	}
58458891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58466b4cac81SBjoern A. Zeeb }
58476b4cac81SBjoern A. Zeeb 
5848673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5849673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5850673d62fcSBjoern A. Zeeb {
5851673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5852673d62fcSBjoern A. Zeeb 
5853673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5854673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5855673d62fcSBjoern A. Zeeb 	return (regd);
5856673d62fcSBjoern A. Zeeb }
5857673d62fcSBjoern A. Zeeb 
58586b4cac81SBjoern A. Zeeb int
58596b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
58606b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
58616b4cac81SBjoern A. Zeeb {
58626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58636b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58646b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
58656b4cac81SBjoern A. Zeeb 
58666b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58676b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58686b4cac81SBjoern A. Zeeb 
58696b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
58706b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
58716b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
58726b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
58736b4cac81SBjoern A. Zeeb 	}
58746b4cac81SBjoern A. Zeeb 
58756b4cac81SBjoern A. Zeeb 	TODO();
58766b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
58776b4cac81SBjoern A. Zeeb 
58786b4cac81SBjoern A. Zeeb 	return (0);
58796b4cac81SBjoern A. Zeeb }
58806b4cac81SBjoern A. Zeeb 
58816b4cac81SBjoern A. Zeeb void
58826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
58836b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
58846b4cac81SBjoern A. Zeeb {
58856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58866b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58876b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
58886b4cac81SBjoern A. Zeeb 
58896b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
58906b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58916b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
58926b4cac81SBjoern A. Zeeb 
58936b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
58946b4cac81SBjoern A. Zeeb 
58958ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
58966b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
58976b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5898a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
58996b4cac81SBjoern A. Zeeb 	wakeup(lhw);
59008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
59016b4cac81SBjoern A. Zeeb 
59026b4cac81SBjoern A. Zeeb 	return;
59036b4cac81SBjoern A. Zeeb }
59046b4cac81SBjoern A. Zeeb 
5905759a996dSBjoern A. Zeeb static void
5906759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5907759a996dSBjoern A. Zeeb {
5908759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5909759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5910759a996dSBjoern A. Zeeb 	int ok;
5911759a996dSBjoern A. Zeeb 
5912759a996dSBjoern A. Zeeb 	ni = NULL;
5913759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5914759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5915759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5916759a996dSBjoern A. Zeeb 
5917759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5918759a996dSBjoern A. Zeeb 		ni = rxni->ni;
5919759a996dSBjoern A. Zeeb 	}
5920759a996dSBjoern A. Zeeb 
5921759a996dSBjoern A. Zeeb 	if (ni != NULL) {
5922759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
5923759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
5924759a996dSBjoern A. Zeeb 		if (ok < 0)
5925759a996dSBjoern A. Zeeb 			m_freem(m);
5926759a996dSBjoern A. Zeeb 	} else {
5927759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5928759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
5929759a996dSBjoern A. Zeeb 	}
5930759a996dSBjoern A. Zeeb 
5931759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5932759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5933bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5934759a996dSBjoern A. Zeeb #endif
5935759a996dSBjoern A. Zeeb }
5936759a996dSBjoern A. Zeeb 
5937759a996dSBjoern A. Zeeb static void
5938759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5939759a996dSBjoern A. Zeeb {
5940759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5941759a996dSBjoern A. Zeeb 	struct mbufq mq;
5942759a996dSBjoern A. Zeeb 	struct mbuf *m;
5943759a996dSBjoern A. Zeeb 
5944759a996dSBjoern A. Zeeb 	lhw = ctx;
5945759a996dSBjoern A. Zeeb 
5946759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5947759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5948bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5949bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5950759a996dSBjoern A. Zeeb #endif
5951759a996dSBjoern A. Zeeb 
5952759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
5953759a996dSBjoern A. Zeeb 
5954759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5955759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
5956759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5957759a996dSBjoern A. Zeeb 
5958759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
5959759a996dSBjoern A. Zeeb 	while (m != NULL) {
5960759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5961759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
5962759a996dSBjoern A. Zeeb 	}
5963759a996dSBjoern A. Zeeb }
5964759a996dSBjoern A. Zeeb 
596549010ba7SBjoern A. Zeeb static void
596649010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
596749010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
596849010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
596949010ba7SBjoern A. Zeeb     uint8_t *rssip)
597049010ba7SBjoern A. Zeeb {
597149010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
597249010ba7SBjoern A. Zeeb 	int i;
597349010ba7SBjoern A. Zeeb 	uint8_t rssi;
597449010ba7SBjoern A. Zeeb 
597549010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
597649010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
597749010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
597849010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
597949010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
598049010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
598149010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
598249010ba7SBjoern A. Zeeb 	else
598349010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
598449010ba7SBjoern A. Zeeb 	/*
598549010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
598649010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
598749010ba7SBjoern A. Zeeb 	 */
598849010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
598949010ba7SBjoern A. Zeeb 	if (rssip != NULL)
599049010ba7SBjoern A. Zeeb 		*rssip = rssi;
599149010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
599249010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
599349010ba7SBjoern A. Zeeb 	rx_stats->c_band =
599449010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
599549010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
599649010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
599749010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
599849010ba7SBjoern A. Zeeb 
599949010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
600049010ba7SBjoern A. Zeeb 
600149010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
600249010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
600349010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
600449010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
600549010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
600649010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
600749010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
600849010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
600949010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
601049010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
601149010ba7SBjoern A. Zeeb 	}
601249010ba7SBjoern A. Zeeb 
601349010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
601449010ba7SBjoern A. Zeeb 		int cc;
601549010ba7SBjoern A. Zeeb 		int8_t crssi;
601649010ba7SBjoern A. Zeeb 
601749010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
601849010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
601949010ba7SBjoern A. Zeeb 
602049010ba7SBjoern A. Zeeb 		cc = 0;
602149010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
602249010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
602349010ba7SBjoern A. Zeeb 				continue;
602449010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
602549010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
602649010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
602749010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
602849010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
602949010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
603049010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
603149010ba7SBjoern A. Zeeb 			cc++;
603249010ba7SBjoern A. Zeeb 		}
603349010ba7SBjoern A. Zeeb 		if (cc > 0)
603449010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
603549010ba7SBjoern A. Zeeb 	}
603649010ba7SBjoern A. Zeeb 
603749010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
603849010ba7SBjoern A. Zeeb 
603949010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
604049010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
604149010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
604249010ba7SBjoern A. Zeeb 		if (supband != NULL)
604349010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
604449010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
604549010ba7SBjoern A. Zeeb 		break;
604649010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
604749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
604849010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
604949010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
605049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
605149010ba7SBjoern A. Zeeb 		break;
605249010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
605349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
605449010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
605549010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
605649010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
605749010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
605849010ba7SBjoern A. Zeeb 		break;
605949010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
606049010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
606149010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
606249010ba7SBjoern A. Zeeb 		break;
606349010ba7SBjoern A. Zeeb 	}
606449010ba7SBjoern A. Zeeb 
606549010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
606649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
606749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
606849010ba7SBjoern A. Zeeb 		break;
606949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
607049010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
607149010ba7SBjoern A. Zeeb 		break;
607249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
607349010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
607449010ba7SBjoern A. Zeeb 		break;
607549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
607649010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
607749010ba7SBjoern A. Zeeb 		break;
607849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
607949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
608049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
608149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
608249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
608349010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
608449010ba7SBjoern A. Zeeb 		break;
608549010ba7SBjoern A. Zeeb 	}
608649010ba7SBjoern A. Zeeb 
608749010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
608849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
608949010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
609049010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
609149010ba7SBjoern A. Zeeb 
609249010ba7SBjoern A. Zeeb 	/*
609349010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
609449010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
609549010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
609649010ba7SBjoern A. Zeeb 	 */
609749010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
609849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
609949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
610049010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
610149010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
610249010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
610349010ba7SBjoern A. Zeeb 	}
610449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
610549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
610649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
610749010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
610849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
610949010ba7SBjoern A. Zeeb 	}
611049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
611149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
611249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
611349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
611449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
611549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
611649010ba7SBjoern A. Zeeb #endif
611749010ba7SBjoern A. Zeeb }
611849010ba7SBjoern A. Zeeb 
6119e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
61206b4cac81SBjoern A. Zeeb void
61216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6122e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6123e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
61246b4cac81SBjoern A. Zeeb {
61256b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61266b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61276b4cac81SBjoern A. Zeeb 	struct mbuf *m;
61286b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
61296b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
61306b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
61316b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
61326b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
61336b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
61346b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
61359d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
613649010ba7SBjoern A. Zeeb 	uint8_t rssi;
6137c0cadd99SBjoern A. Zeeb 	bool is_beacon;
61386b4cac81SBjoern A. Zeeb 
61396b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
61406b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
61416b4cac81SBjoern A. Zeeb 		IMPROVE();
61426b4cac81SBjoern A. Zeeb 		goto err;
61436b4cac81SBjoern A. Zeeb 	}
61446b4cac81SBjoern A. Zeeb 
61456b4cac81SBjoern A. Zeeb 	/*
61466b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
61476b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
61486b4cac81SBjoern A. Zeeb 	 */
61496b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
61506b4cac81SBjoern A. Zeeb 	if (m == NULL)
61516b4cac81SBjoern A. Zeeb 		goto err;
61526b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
61536b4cac81SBjoern A. Zeeb 
61546b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
61556b4cac81SBjoern A. Zeeb 	offset = m->m_len;
61566b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
61576b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
61586b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
61596b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
61606b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
61616b4cac81SBjoern A. Zeeb 	}
61626b4cac81SBjoern A. Zeeb 
61636b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
61646b4cac81SBjoern A. Zeeb 
61656b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6166c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6167c0cadd99SBjoern A. Zeeb 
6168c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
61699d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
61706b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
61716b4cac81SBjoern A. Zeeb 
61729d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
61736b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6174c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
61756b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
61766b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
61776b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6178c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
61796b4cac81SBjoern A. Zeeb 
61809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
61816b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
61826b4cac81SBjoern A. Zeeb 
61836b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
61849d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6185f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
618660970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
61876b4cac81SBjoern A. Zeeb 			__func__,
6188c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6189c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
61906b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6191f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
61926b4cac81SBjoern A. Zeeb 			rx_status->freq,
61936b4cac81SBjoern A. Zeeb 			rx_status->bw,
61946b4cac81SBjoern A. Zeeb 			rx_status->encoding,
61956b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
61966b4cac81SBjoern A. Zeeb 			rx_status->band,
61976b4cac81SBjoern A. Zeeb 			rx_status->chains,
61986b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
61996b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
62006b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
620160970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
62026b4cac81SBjoern A. Zeeb 			rx_status->signal,
62036b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
62046b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
62056b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
62066b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
62076b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
62086b4cac81SBjoern A. Zeeb 			rx_status->nss,
62096b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
62106b4cac81SBjoern A. Zeeb no_trace_beacons:
62116b4cac81SBjoern A. Zeeb #endif
62126b4cac81SBjoern A. Zeeb 
621349010ba7SBjoern A. Zeeb 	rssi = 0;
621449010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
62156b4cac81SBjoern A. Zeeb 
62166b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62176b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
62186b4cac81SBjoern A. Zeeb 
62196b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
62206b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6221dbc06dd9SBjoern A. Zeeb 		m_freem(m);
62226b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
62236b4cac81SBjoern A. Zeeb 		goto err;
62246b4cac81SBjoern A. Zeeb 	}
62256b4cac81SBjoern A. Zeeb 
622658246901SBjoern A. Zeeb 	lsta = NULL;
62276b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
62286b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
62296b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
62306b4cac81SBjoern A. Zeeb 	} else {
6231c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6232c8dafefaSBjoern A. Zeeb 
62336b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
62346b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
62356b4cac81SBjoern A. Zeeb 		if (ni != NULL)
62366b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
62376b4cac81SBjoern A. Zeeb 	}
62386b4cac81SBjoern A. Zeeb 
62396b4cac81SBjoern A. Zeeb 	if (ni != NULL)
62406b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
62416b4cac81SBjoern A. Zeeb 	else
62426b4cac81SBjoern A. Zeeb 		/*
62436b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
62446b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
62456b4cac81SBjoern A. Zeeb 		 */
62466b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
62476b4cac81SBjoern A. Zeeb 
62489d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
62499d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6250bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6251c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6252c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
62539d9ba2b7SBjoern A. Zeeb #endif
62546b4cac81SBjoern A. Zeeb 
6255c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6256c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6257c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6258c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6259c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6260c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6261c8dafefaSBjoern A. Zeeb 
6262c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6263c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6264c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6265c8dafefaSBjoern A. Zeeb 
6266c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6267c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6268c8dafefaSBjoern A. Zeeb 
6269c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6270c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6271c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6272c8dafefaSBjoern A. Zeeb 		/*
6273c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6274c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6275c8dafefaSBjoern A. Zeeb 		 */
6276c8dafefaSBjoern A. Zeeb skip_device_ts:
62779fb91463SBjoern A. Zeeb 		;
62789fb91463SBjoern A. Zeeb 	}
6279c8dafefaSBjoern A. Zeeb 
62806b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
62816b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
62826b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
62836b4cac81SBjoern A. Zeeb 
62846b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
62856b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
62866b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
62876b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
62886b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
62896b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
62906b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
62916b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
62926b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
62936b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
62946b4cac81SBjoern A. Zeeb #endif
62956b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
62966b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
62976b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
62986b4cac81SBjoern A. Zeeb 		IMPROVE();
62996b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
63006b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
63016b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
63026b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6303170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
63046b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
63056b4cac81SBjoern A. Zeeb 	}
63066b4cac81SBjoern A. Zeeb 
63076b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
63086b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
63096b4cac81SBjoern A. Zeeb 
6310e30e05d3SBjoern A. Zeeb #if 0
6311e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6312e30e05d3SBjoern A. Zeeb 		/*
6313e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6314e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6315e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6316e30e05d3SBjoern A. Zeeb 		*/
6317e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6318e30e05d3SBjoern A. Zeeb 	}
6319e30e05d3SBjoern A. Zeeb #endif
6320e30e05d3SBjoern A. Zeeb 
6321759a996dSBjoern A. Zeeb 	/*
6322759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6323759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6324759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6325759a996dSBjoern A. Zeeb 	 */
63266b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6327759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6328759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6329759a996dSBjoern A. Zeeb 
6330759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6331759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6332759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6333759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6334759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6335759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6336759a996dSBjoern A. Zeeb 		}
63376b4cac81SBjoern A. Zeeb 	}
63386b4cac81SBjoern A. Zeeb 
6339759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6340759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6341759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6342759a996dSBjoern A. Zeeb 		m_freem(m);
6343759a996dSBjoern A. Zeeb 		goto err;
6344759a996dSBjoern A. Zeeb 	}
6345759a996dSBjoern A. Zeeb 
6346759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6347759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6348759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
63496b4cac81SBjoern A. Zeeb 
63506b4cac81SBjoern A. Zeeb 	IMPROVE();
63516b4cac81SBjoern A. Zeeb 
63526b4cac81SBjoern A. Zeeb err:
63536b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
63546b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
63556b4cac81SBjoern A. Zeeb }
63566b4cac81SBjoern A. Zeeb 
63576b4cac81SBjoern A. Zeeb uint8_t
6358ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
63596b4cac81SBjoern A. Zeeb {
63606b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6361ec190d91SBjoern A. Zeeb 	uint8_t tid;
6362ec190d91SBjoern A. Zeeb 
6363ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6364ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6365ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6366ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
63676b4cac81SBjoern A. Zeeb 
63686b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6369ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6370ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6371ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6372ec190d91SBjoern A. Zeeb 
6373ec190d91SBjoern A. Zeeb 	return (tid);
63746b4cac81SBjoern A. Zeeb }
63756b4cac81SBjoern A. Zeeb 
6376ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6377ac1d519cSBjoern A. Zeeb 
6378ac1d519cSBjoern A. Zeeb static void
6379ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6380ac1d519cSBjoern A. Zeeb {
6381ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6382ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6383ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6384ac1d519cSBjoern A. Zeeb 
6385ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6386ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6387ac1d519cSBjoern A. Zeeb 
6388ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6389ac1d519cSBjoern A. Zeeb 
6390ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6391ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6392ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6393ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6394ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6395ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6396ac1d519cSBjoern A. Zeeb 		return;
6397ac1d519cSBjoern A. Zeeb 	}
6398ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6399ac1d519cSBjoern A. Zeeb 
6400ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6401ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6402ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6403ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6404ac1d519cSBjoern A. Zeeb 
6405ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6406ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6407ac1d519cSBjoern A. Zeeb 
6408ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6409ac1d519cSBjoern A. Zeeb }
6410ac1d519cSBjoern A. Zeeb 
6411ac1d519cSBjoern A. Zeeb void
6412ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6413ac1d519cSBjoern A. Zeeb {
6414ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6415ac1d519cSBjoern A. Zeeb 
6416ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6417ac1d519cSBjoern A. Zeeb 
6418ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6419ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6420ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6421ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6422ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6423ac1d519cSBjoern A. Zeeb 
6424ac1d519cSBjoern A. Zeeb 	/*
6425ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6426ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6427ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6428ac1d519cSBjoern A. Zeeb 	 */
6429ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6430ac1d519cSBjoern A. Zeeb }
6431ac1d519cSBjoern A. Zeeb 
6432ac1d519cSBjoern A. Zeeb void
6433ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6434ac1d519cSBjoern A. Zeeb {
6435ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6436ac1d519cSBjoern A. Zeeb 
6437ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6438ac1d519cSBjoern A. Zeeb 
6439ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6440ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6441ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6442ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6443ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6444ac1d519cSBjoern A. Zeeb }
6445ac1d519cSBjoern A. Zeeb 
6446ac1d519cSBjoern A. Zeeb void
6447ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6448ac1d519cSBjoern A. Zeeb {
6449ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6450ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6451ac1d519cSBjoern A. Zeeb 
6452ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6453ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6454ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6455ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6456ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6457ac1d519cSBjoern A. Zeeb 		return;
6458ac1d519cSBjoern A. Zeeb 	}
6459ac1d519cSBjoern A. Zeeb 
6460ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6461ac1d519cSBjoern A. Zeeb 
6462ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6463ac1d519cSBjoern A. Zeeb 		    entry);
6464ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6465ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6466ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6467ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6468ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6469ac1d519cSBjoern A. Zeeb 			break;
6470ac1d519cSBjoern A. Zeeb 	}
6471ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6472ac1d519cSBjoern A. Zeeb }
6473ac1d519cSBjoern A. Zeeb 
6474ac1d519cSBjoern A. Zeeb void
6475ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6476ac1d519cSBjoern A. Zeeb {
6477ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6478ac1d519cSBjoern A. Zeeb 
6479ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6480ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6481ac1d519cSBjoern A. Zeeb }
6482ac1d519cSBjoern A. Zeeb 
6483ac1d519cSBjoern A. Zeeb void
6484ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6485ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6486ac1d519cSBjoern A. Zeeb {
6487ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6488ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6489ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6490ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6491ac1d519cSBjoern A. Zeeb 	} else {
6492ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6493ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6494ac1d519cSBjoern A. Zeeb 	}
6495ac1d519cSBjoern A. Zeeb }
6496ac1d519cSBjoern A. Zeeb 
6497ac1d519cSBjoern A. Zeeb void
6498ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6499ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6500ac1d519cSBjoern A. Zeeb {
6501ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6502ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6503ac1d519cSBjoern A. Zeeb }
6504ac1d519cSBjoern A. Zeeb 
6505ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6506ac1d519cSBjoern A. Zeeb 
65076b4cac81SBjoern A. Zeeb struct wiphy *
65086b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
65096b4cac81SBjoern A. Zeeb {
65106b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6511ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
65126b4cac81SBjoern A. Zeeb 
65136b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
65146b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
65156b4cac81SBjoern A. Zeeb 		return (NULL);
65166b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
65176b4cac81SBjoern A. Zeeb 
6518ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6519ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6520ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6521ac1d519cSBjoern A. Zeeb 
6522ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6523ac1d519cSBjoern A. Zeeb 
6524ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6525ac1d519cSBjoern A. Zeeb 	TODO();
6526ac1d519cSBjoern A. Zeeb 
6527ac1d519cSBjoern A. Zeeb 	return (wiphy);
65286b4cac81SBjoern A. Zeeb }
65296b4cac81SBjoern A. Zeeb 
65306b4cac81SBjoern A. Zeeb void
65316b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
65326b4cac81SBjoern A. Zeeb {
65336b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
65346b4cac81SBjoern A. Zeeb 
65356b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
65366b4cac81SBjoern A. Zeeb 		return;
65376b4cac81SBjoern A. Zeeb 
6538ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6539ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6540ac1d519cSBjoern A. Zeeb 
65416b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6542ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6543ac1d519cSBjoern A. Zeeb 
65446b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
65456b4cac81SBjoern A. Zeeb }
65466b4cac81SBjoern A. Zeeb 
6547a7c19b8aSBjoern A. Zeeb static uint32_t
6548a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6549a7c19b8aSBjoern A. Zeeb {
6550a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6551a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6552a7c19b8aSBjoern A. Zeeb }
6553a7c19b8aSBjoern A. Zeeb 
6554a7c19b8aSBjoern A. Zeeb static uint32_t
6555a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6556a7c19b8aSBjoern A. Zeeb {
6557a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6558a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6559a7c19b8aSBjoern A. Zeeb }
6560a7c19b8aSBjoern A. Zeeb 
6561a7c19b8aSBjoern A. Zeeb uint32_t
6562a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6563a7c19b8aSBjoern A. Zeeb {
6564a7c19b8aSBjoern A. Zeeb 
6565a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6566a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6567a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6568a7c19b8aSBjoern A. Zeeb 
6569a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6570a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6571a7c19b8aSBjoern A. Zeeb 
6572a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6573a7c19b8aSBjoern A. Zeeb 
6574a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6575a7c19b8aSBjoern A. Zeeb }
6576a7c19b8aSBjoern A. Zeeb 
65776b4cac81SBjoern A. Zeeb uint32_t
65786b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
65796b4cac81SBjoern A. Zeeb     enum nl80211_band band)
65806b4cac81SBjoern A. Zeeb {
65816b4cac81SBjoern A. Zeeb 
65826b4cac81SBjoern A. Zeeb 	switch (band) {
65836b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
65846b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
65856b4cac81SBjoern A. Zeeb 		break;
65866b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
65876b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
65886b4cac81SBjoern A. Zeeb 		break;
65896b4cac81SBjoern A. Zeeb 	default:
65906b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
65916b4cac81SBjoern A. Zeeb 		break;
65926b4cac81SBjoern A. Zeeb 	}
65936b4cac81SBjoern A. Zeeb 
65946b4cac81SBjoern A. Zeeb 	return (0);
65956b4cac81SBjoern A. Zeeb }
65966b4cac81SBjoern A. Zeeb 
65976b4cac81SBjoern A. Zeeb uint32_t
65986b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
65996b4cac81SBjoern A. Zeeb {
66006b4cac81SBjoern A. Zeeb 
66016b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
66026b4cac81SBjoern A. Zeeb }
66036b4cac81SBjoern A. Zeeb 
6604ac867c20SBjoern A. Zeeb #if 0
66056b4cac81SBjoern A. Zeeb static struct lkpi_sta *
66066b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
66076b4cac81SBjoern A. Zeeb {
66086b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
66096b4cac81SBjoern A. Zeeb 
6610a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6611a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
66126b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6613a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
66146b4cac81SBjoern A. Zeeb 			return (lsta);
66156b4cac81SBjoern A. Zeeb 		}
66166b4cac81SBjoern A. Zeeb 	}
6617a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
66186b4cac81SBjoern A. Zeeb 
66196b4cac81SBjoern A. Zeeb 	return (NULL);
66206b4cac81SBjoern A. Zeeb }
6621ac867c20SBjoern A. Zeeb #endif
66226b4cac81SBjoern A. Zeeb 
66236b4cac81SBjoern A. Zeeb struct ieee80211_sta *
66246b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
66256b4cac81SBjoern A. Zeeb {
66266b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6627a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
66286b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
66296b4cac81SBjoern A. Zeeb 
66306b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
66316b4cac81SBjoern A. Zeeb 
6632a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6633a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
66346b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
66356b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6636a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
66376b4cac81SBjoern A. Zeeb 			return (sta);
66386b4cac81SBjoern A. Zeeb 		}
66396b4cac81SBjoern A. Zeeb 	}
6640a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
66416b4cac81SBjoern A. Zeeb 	return (NULL);
66426b4cac81SBjoern A. Zeeb }
66436b4cac81SBjoern A. Zeeb 
66446b4cac81SBjoern A. Zeeb struct ieee80211_sta *
66452e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
66462e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
66476b4cac81SBjoern A. Zeeb {
66486b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
66496b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66506b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
66516b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
66526b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
66536b4cac81SBjoern A. Zeeb 
66546b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
66556b4cac81SBjoern A. Zeeb 	sta = NULL;
66566b4cac81SBjoern A. Zeeb 
66578891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
66586b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
66596b4cac81SBjoern A. Zeeb 
66606b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
66616b4cac81SBjoern A. Zeeb 
66626b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
66636b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
66646b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
66656b4cac81SBjoern A. Zeeb 			continue;
66666b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
66676b4cac81SBjoern A. Zeeb 		if (sta != NULL)
66686b4cac81SBjoern A. Zeeb 			break;
66696b4cac81SBjoern A. Zeeb 	}
66708891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
66716b4cac81SBjoern A. Zeeb 
66726b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
66736b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
66746b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
66756b4cac81SBjoern A. Zeeb 			return (NULL);
66766b4cac81SBjoern A. Zeeb 	}
66776b4cac81SBjoern A. Zeeb 
66786b4cac81SBjoern A. Zeeb 	return (sta);
66796b4cac81SBjoern A. Zeeb }
66806b4cac81SBjoern A. Zeeb 
66816b4cac81SBjoern A. Zeeb struct sk_buff *
66826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
66836b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
66846b4cac81SBjoern A. Zeeb {
66856b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
66860cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66876b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
66886b4cac81SBjoern A. Zeeb 
66890cbcfa19SBjoern A. Zeeb 	skb = NULL;
66906b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
66916b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
66926b4cac81SBjoern A. Zeeb 
66930cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
66940cbcfa19SBjoern A. Zeeb 		goto stopped;
66950cbcfa19SBjoern A. Zeeb 
66960cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
66970cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
66980cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
66990cbcfa19SBjoern A. Zeeb 		goto stopped;
67000cbcfa19SBjoern A. Zeeb 	}
67010cbcfa19SBjoern A. Zeeb 
6702eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6703eac3646fSBjoern A. Zeeb 
6704eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
67056b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6706eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
67076b4cac81SBjoern A. Zeeb 
67080cbcfa19SBjoern A. Zeeb stopped:
67096b4cac81SBjoern A. Zeeb 	return (skb);
67106b4cac81SBjoern A. Zeeb }
67116b4cac81SBjoern A. Zeeb 
67126b4cac81SBjoern A. Zeeb void
67136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
671486220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
67156b4cac81SBjoern A. Zeeb {
67166b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
67176b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
671886220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
67196b4cac81SBjoern A. Zeeb 
67206b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
67216b4cac81SBjoern A. Zeeb 
67226b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6723eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
67246b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
67256b4cac81SBjoern A. Zeeb 		fc++;
67266b4cac81SBjoern A. Zeeb 		bc += skb->len;
67276b4cac81SBjoern A. Zeeb 	}
6728eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
67296b4cac81SBjoern A. Zeeb 	if (frame_cnt)
67306b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
67316b4cac81SBjoern A. Zeeb 	if (byte_cnt)
67326b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
67336b4cac81SBjoern A. Zeeb 
67346b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
67356b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
67366b4cac81SBjoern A. Zeeb 	IMPROVE();
67376b4cac81SBjoern A. Zeeb }
67386b4cac81SBjoern A. Zeeb 
67396b4cac81SBjoern A. Zeeb /*
67406b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
67416b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
67426b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
67436b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
67446b4cac81SBjoern A. Zeeb  */
6745a8397571SBjoern A. Zeeb static void
6746a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
67476b4cac81SBjoern A. Zeeb     int status)
67486b4cac81SBjoern A. Zeeb {
67496b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
67506b4cac81SBjoern A. Zeeb 	struct mbuf *m;
67516b4cac81SBjoern A. Zeeb 
67526b4cac81SBjoern A. Zeeb 	m = skb->m;
67536b4cac81SBjoern A. Zeeb 	skb->m = NULL;
67546b4cac81SBjoern A. Zeeb 
67556b4cac81SBjoern A. Zeeb 	if (m != NULL) {
67566b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
67576b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
67586b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
67596b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
67606b4cac81SBjoern A. Zeeb 	}
6761a8397571SBjoern A. Zeeb }
67626b4cac81SBjoern A. Zeeb 
6763a8397571SBjoern A. Zeeb void
6764a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6765a8397571SBjoern A. Zeeb     int status)
6766a8397571SBjoern A. Zeeb {
6767a8397571SBjoern A. Zeeb 
6768a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
67696b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
67706b4cac81SBjoern A. Zeeb }
67716b4cac81SBjoern A. Zeeb 
6772383b3e8fSBjoern A. Zeeb void
6773a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6774a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6775383b3e8fSBjoern A. Zeeb {
6776a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6777383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6778383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6779383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6780383b3e8fSBjoern A. Zeeb 	int status;
6781383b3e8fSBjoern A. Zeeb 
6782a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6783383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6784383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6785383b3e8fSBjoern A. Zeeb 
6786383b3e8fSBjoern A. Zeeb 		m = skb->m;
6787383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6788383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6789383b3e8fSBjoern A. Zeeb 	} else {
6790383b3e8fSBjoern A. Zeeb 		ni = NULL;
6791383b3e8fSBjoern A. Zeeb 	}
6792383b3e8fSBjoern A. Zeeb 
6793a8397571SBjoern A. Zeeb 	info = txstat->info;
6794383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6795383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6796383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6797383b3e8fSBjoern A. Zeeb 	} else {
6798383b3e8fSBjoern A. Zeeb 		status = 1;
6799383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6800383b3e8fSBjoern A. Zeeb 	}
6801383b3e8fSBjoern A. Zeeb 
6802383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6803e2c5ab09SJohn Baldwin 		int ridx __unused;
6804383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6805383b3e8fSBjoern A. Zeeb 		int old_rate;
6806383b3e8fSBjoern A. Zeeb 
6807383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6808383b3e8fSBjoern A. Zeeb #endif
6809383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6810383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6811383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6812383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6813383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6814383b3e8fSBjoern A. Zeeb 		}
6815383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6816a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6817383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6818383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6819383b3e8fSBjoern A. Zeeb #endif
6820adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6821383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6822383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6823383b3e8fSBjoern A. Zeeb 		}
6824383b3e8fSBjoern A. Zeeb 
6825383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6826383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6827383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6828383b3e8fSBjoern A. Zeeb 
6829383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6830383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6831383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6832383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6833383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6834383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6835383b3e8fSBjoern A. Zeeb 		}
6836383b3e8fSBjoern A. Zeeb #endif
6837383b3e8fSBjoern A. Zeeb 	}
6838383b3e8fSBjoern A. Zeeb 
6839383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6840383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6841383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6842383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6843383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6844383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6845adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6846383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6847383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6848383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6849383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6850383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6851383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6852383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6853383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6854383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6855383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6856383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6857383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6858383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6859adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6860383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6861383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6862383b3e8fSBjoern A. Zeeb #endif
6863383b3e8fSBjoern A. Zeeb 
6864a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6865a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6866a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6867a8397571SBjoern A. Zeeb 	} else {
6868383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6869383b3e8fSBjoern A. Zeeb 	}
6870a8397571SBjoern A. Zeeb }
6871a8397571SBjoern A. Zeeb 
6872a8397571SBjoern A. Zeeb void
6873a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6874a8397571SBjoern A. Zeeb {
6875a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6876a8397571SBjoern A. Zeeb 
6877a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6878a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6879a8397571SBjoern A. Zeeb 	status.skb = skb;
6880a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6881a8397571SBjoern A. Zeeb 
6882a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6883a8397571SBjoern A. Zeeb }
6884383b3e8fSBjoern A. Zeeb 
68856b4cac81SBjoern A. Zeeb /*
68866b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
68876b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
68886b4cac81SBjoern A. Zeeb  * mbufs this should go away.
68896b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
68906b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
68916b4cac81SBjoern A. Zeeb  */
68926b4cac81SBjoern A. Zeeb static void
68936b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
68946b4cac81SBjoern A. Zeeb {
68956b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
68966b4cac81SBjoern A. Zeeb 	struct mbuf *m;
68976b4cac81SBjoern A. Zeeb 
68986b4cac81SBjoern A. Zeeb 	if (p == NULL)
68996b4cac81SBjoern A. Zeeb 		return;
69006b4cac81SBjoern A. Zeeb 
69016b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
69026b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
69036b4cac81SBjoern A. Zeeb 
69046b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
69056b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
69066b4cac81SBjoern A. Zeeb 	if (ni != NULL)
69076b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
69086b4cac81SBjoern A. Zeeb 	m_freem(m);
69096b4cac81SBjoern A. Zeeb }
69106b4cac81SBjoern A. Zeeb 
69116b4cac81SBjoern A. Zeeb void
69126b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
69136b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
69146b4cac81SBjoern A. Zeeb {
69156b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
69166b4cac81SBjoern A. Zeeb 
69176b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
69186b4cac81SBjoern A. Zeeb 	IMPROVE();
69196b4cac81SBjoern A. Zeeb 
69206b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
69216b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
69226b4cac81SBjoern A. Zeeb }
69236b4cac81SBjoern A. Zeeb 
69246b4cac81SBjoern A. Zeeb void
69256b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
69266b4cac81SBjoern A. Zeeb     struct work_struct *w)
69276b4cac81SBjoern A. Zeeb {
69286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
69296b4cac81SBjoern A. Zeeb 
69306b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
69316b4cac81SBjoern A. Zeeb 	IMPROVE();
69326b4cac81SBjoern A. Zeeb 
69336b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
69346b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
69356b4cac81SBjoern A. Zeeb }
69366b4cac81SBjoern A. Zeeb 
69376b4cac81SBjoern A. Zeeb struct sk_buff *
6938ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
6939ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
6940ade774b1SBjoern A. Zeeb {
6941ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
6942ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
6943ade774b1SBjoern A. Zeeb 	uint8_t *p;
6944ade774b1SBjoern A. Zeeb 	size_t len;
6945ade774b1SBjoern A. Zeeb 
6946ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
6947ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
6948ade774b1SBjoern A. Zeeb 
6949ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
6950ade774b1SBjoern A. Zeeb 	if (skb == NULL)
6951ade774b1SBjoern A. Zeeb 		return (NULL);
6952ade774b1SBjoern A. Zeeb 
6953ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
6954ade774b1SBjoern A. Zeeb 
6955ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
6956ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
6957ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
6958ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
6959ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
6960ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
6961ade774b1SBjoern A. Zeeb 
6962ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
6963ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
6964ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
6965ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
6966ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
6967ade774b1SBjoern A. Zeeb 
6968ade774b1SBjoern A. Zeeb 	return (skb);
6969ade774b1SBjoern A. Zeeb }
6970ade774b1SBjoern A. Zeeb 
6971ade774b1SBjoern A. Zeeb struct sk_buff *
69726b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
69736b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
69746b4cac81SBjoern A. Zeeb {
69756b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
69766b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
69776b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
69786b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
69796b4cac81SBjoern A. Zeeb 	uint16_t v;
69806b4cac81SBjoern A. Zeeb 
69816b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
69826b4cac81SBjoern A. Zeeb 	if (skb == NULL)
69836b4cac81SBjoern A. Zeeb 		return (NULL);
69846b4cac81SBjoern A. Zeeb 
69856b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
69866b4cac81SBjoern A. Zeeb 
69876b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
69886b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
69896b4cac81SBjoern A. Zeeb 
69906b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
69916b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
69926b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
6993616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
69946b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
69956b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
69966b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
69976b4cac81SBjoern A. Zeeb 
69986b4cac81SBjoern A. Zeeb 	return (skb);
69996b4cac81SBjoern A. Zeeb }
70006b4cac81SBjoern A. Zeeb 
70016b4cac81SBjoern A. Zeeb struct sk_buff *
70026b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7003adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
70046b4cac81SBjoern A. Zeeb {
70056b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70066b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70076b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70086b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
70096b4cac81SBjoern A. Zeeb 
7010adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7011adff403fSBjoern A. Zeeb 
70126b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
70136b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70146b4cac81SBjoern A. Zeeb 		return (NULL);
70156b4cac81SBjoern A. Zeeb 
70166b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
70176b4cac81SBjoern A. Zeeb 
70186b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70196b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70206b4cac81SBjoern A. Zeeb 
70216b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
70226b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
70236b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
70246b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
70256b4cac81SBjoern A. Zeeb 
70266b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
70276b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
70286b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
70296b4cac81SBjoern A. Zeeb 
70306b4cac81SBjoern A. Zeeb 	return (skb);
70316b4cac81SBjoern A. Zeeb }
70326b4cac81SBjoern A. Zeeb 
70336b4cac81SBjoern A. Zeeb struct wireless_dev *
70346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
70356b4cac81SBjoern A. Zeeb {
70366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70376b4cac81SBjoern A. Zeeb 
70386b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70396b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
70406b4cac81SBjoern A. Zeeb }
70416b4cac81SBjoern A. Zeeb 
70426b4cac81SBjoern A. Zeeb void
70436b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
70446b4cac81SBjoern A. Zeeb {
70456b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70466b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70476b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
70486b4cac81SBjoern A. Zeeb 	int arg;
70496b4cac81SBjoern A. Zeeb 
70506b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70516b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70526b4cac81SBjoern A. Zeeb 
70536b4cac81SBjoern A. Zeeb 	/*
7054f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
70556b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
70566b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
70576b4cac81SBjoern A. Zeeb 	 */
7058f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7059bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
70606b4cac81SBjoern A. Zeeb 
7061018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7062018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
70636b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
70646b4cac81SBjoern A. Zeeb }
70656b4cac81SBjoern A. Zeeb 
7066bb81db90SBjoern A. Zeeb void
7067bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7068bb81db90SBjoern A. Zeeb {
7069bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7070bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7071bb81db90SBjoern A. Zeeb 
7072bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7073bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7074bb81db90SBjoern A. Zeeb 
7075bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7076bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
70773540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7078bb81db90SBjoern A. Zeeb }
7079bb81db90SBjoern A. Zeeb 
70805edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
70815edde07cSBjoern A. Zeeb 
70825a9a0d78SBjoern A. Zeeb void
70835a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
70845a9a0d78SBjoern A. Zeeb {
70855a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70865a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70875a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
70885a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
70895a9a0d78SBjoern A. Zeeb 
70905a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
70915a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
70925a9a0d78SBjoern A. Zeeb 
70935a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
70945a9a0d78SBjoern A. Zeeb 
70955a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
70965a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
70975a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
70985a9a0d78SBjoern A. Zeeb 	else
70995a9a0d78SBjoern A. Zeeb 		ac_count = 1;
71005a9a0d78SBjoern A. Zeeb 
71015a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71025a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71035a9a0d78SBjoern A. Zeeb 
71045a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71055a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
71065a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
71075a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
71080d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
71095a9a0d78SBjoern A. Zeeb 				/*
71105a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
71115a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
71125a9a0d78SBjoern A. Zeeb 				 */
71130d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
71140d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
71155a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
71165a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
71175a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
71185a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
71190d2cb6a6SBjoern A. Zeeb #endif
71205a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
71215a9a0d78SBjoern A. Zeeb 			}
71225a9a0d78SBjoern A. Zeeb 		}
71235a9a0d78SBjoern A. Zeeb 	}
71245a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
71255a9a0d78SBjoern A. Zeeb }
71265a9a0d78SBjoern A. Zeeb 
71275a9a0d78SBjoern A. Zeeb void
71285a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
71295a9a0d78SBjoern A. Zeeb {
71305a9a0d78SBjoern A. Zeeb 	int i;
71315a9a0d78SBjoern A. Zeeb 
71325a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
71335a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
71345a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
71355a9a0d78SBjoern A. Zeeb }
71365a9a0d78SBjoern A. Zeeb 
71375a9a0d78SBjoern A. Zeeb 
71385a9a0d78SBjoern A. Zeeb static void
71395a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
71405a9a0d78SBjoern A. Zeeb {
71415a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71425a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71435a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71445a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
71455a9a0d78SBjoern A. Zeeb 
71465a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
71475a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
71485a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
71495a9a0d78SBjoern A. Zeeb 	else
71505a9a0d78SBjoern A. Zeeb 		ac_count = 1;
71515a9a0d78SBjoern A. Zeeb 
71525a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
71535a9a0d78SBjoern A. Zeeb 
71545a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
71555a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71565a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71575a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
71585a9a0d78SBjoern A. Zeeb 
71595a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71605a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
71615a9a0d78SBjoern A. Zeeb 
71625a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
71635a9a0d78SBjoern A. Zeeb 
71645a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
71655a9a0d78SBjoern A. Zeeb 
71660d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
71675a9a0d78SBjoern A. Zeeb 				/*
71685a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
71695a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
71705a9a0d78SBjoern A. Zeeb 				 */
71710d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
71720d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
71735a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
71745a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
71755a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
71765a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
71770d2cb6a6SBjoern A. Zeeb #endif
71785a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
71795a9a0d78SBjoern A. Zeeb 
7180a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7181a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
71825a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
71835a9a0d78SBjoern A. Zeeb 
71845a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
71855a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
71865a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
71875a9a0d78SBjoern A. Zeeb 
71885a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
71895a9a0d78SBjoern A. Zeeb 							continue;
71905a9a0d78SBjoern A. Zeeb 
71915a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
71925a9a0d78SBjoern A. Zeeb 							continue;
71935a9a0d78SBjoern A. Zeeb 
71945a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
71955a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
71965a9a0d78SBjoern A. Zeeb 							continue;
71975a9a0d78SBjoern A. Zeeb 
71985a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
71995a9a0d78SBjoern A. Zeeb 
72005a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
72015a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
72025a9a0d78SBjoern A. Zeeb 					}
72035a9a0d78SBjoern A. Zeeb 				}
7204a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
72055a9a0d78SBjoern A. Zeeb 			}
72065a9a0d78SBjoern A. Zeeb 		}
72075a9a0d78SBjoern A. Zeeb 	}
72085a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72095a9a0d78SBjoern A. Zeeb }
72105a9a0d78SBjoern A. Zeeb 
72115a9a0d78SBjoern A. Zeeb void
72125a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
72135a9a0d78SBjoern A. Zeeb {
72145a9a0d78SBjoern A. Zeeb 	int i;
72155a9a0d78SBjoern A. Zeeb 
72165a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
72175a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72185a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
72195a9a0d78SBjoern A. Zeeb }
72205a9a0d78SBjoern A. Zeeb 
72215a9a0d78SBjoern A. Zeeb void
72225a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
72235a9a0d78SBjoern A. Zeeb {
72245a9a0d78SBjoern A. Zeeb 
72255a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
72265a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
72275a9a0d78SBjoern A. Zeeb 
72285a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
72295a9a0d78SBjoern A. Zeeb }
72305a9a0d78SBjoern A. Zeeb 
72315a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
72325a9a0d78SBjoern A. Zeeb void
72335a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
72345a9a0d78SBjoern A. Zeeb {
72355a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72365a9a0d78SBjoern A. Zeeb 
72375a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72385a9a0d78SBjoern A. Zeeb 
72395a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
72405a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
72415a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
72425a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
72435a9a0d78SBjoern A. Zeeb }
72445a9a0d78SBjoern A. Zeeb 
72455a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
72465a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
72475a9a0d78SBjoern A. Zeeb {
72485a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72495a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
72505a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
72515a9a0d78SBjoern A. Zeeb 
72525a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72535a9a0d78SBjoern A. Zeeb 	txq = NULL;
72545a9a0d78SBjoern A. Zeeb 
72555a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
72565a9a0d78SBjoern A. Zeeb 
72575a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
72585a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
72595a9a0d78SBjoern A. Zeeb 		goto out;
72605a9a0d78SBjoern A. Zeeb 
72615a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
72625a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
72635a9a0d78SBjoern A. Zeeb 		goto out;
72645a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
72655a9a0d78SBjoern A. Zeeb 		goto out;
72665a9a0d78SBjoern A. Zeeb 
72675a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
72685a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
72695a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
72705a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
72715a9a0d78SBjoern A. Zeeb 
72725a9a0d78SBjoern A. Zeeb out:
72735a9a0d78SBjoern A. Zeeb 	return (txq);
72745a9a0d78SBjoern A. Zeeb }
72755a9a0d78SBjoern A. Zeeb 
72765a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
72775a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
72785a9a0d78SBjoern A. Zeeb {
72795a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72805a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7281eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
72825a9a0d78SBjoern A. Zeeb 
72835a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
72845a9a0d78SBjoern A. Zeeb 
72855a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
72865a9a0d78SBjoern A. Zeeb 
72875a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7288eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7289eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7290eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7291eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
72925a9a0d78SBjoern A. Zeeb 		goto out;
72935a9a0d78SBjoern A. Zeeb 
729441b746e0SAustin Shafer 	/*
729541b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
729641b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
729741b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
729841b746e0SAustin Shafer 	 */
729941b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
73005a9a0d78SBjoern A. Zeeb 		goto out;
73015a9a0d78SBjoern A. Zeeb 
73025a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73035a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
73045a9a0d78SBjoern A. Zeeb out:
73055a9a0d78SBjoern A. Zeeb 	return;
73065a9a0d78SBjoern A. Zeeb }
73075a9a0d78SBjoern A. Zeeb 
7308eac3646fSBjoern A. Zeeb void
7309eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7310eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7311eac3646fSBjoern A. Zeeb {
7312eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7313eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7314eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7315eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7316eac3646fSBjoern A. Zeeb 
7317eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7318eac3646fSBjoern A. Zeeb 
7319eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7320eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7321eac3646fSBjoern A. Zeeb 	do {
7322eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7323eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7324eac3646fSBjoern A. Zeeb 			break;
7325eac3646fSBjoern A. Zeeb 
7326eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7327eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7328eac3646fSBjoern A. Zeeb 		do {
7329eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7330eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7331eac3646fSBjoern A. Zeeb 				break;
7332eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7333eac3646fSBjoern A. Zeeb 		} while(1);
7334eac3646fSBjoern A. Zeeb 
7335eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7336eac3646fSBjoern A. Zeeb 	} while (1);
7337eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7338eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7339eac3646fSBjoern A. Zeeb }
7340eac3646fSBjoern A. Zeeb 
73415a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
73425a9a0d78SBjoern A. Zeeb 
73435edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
73445edde07cSBjoern A. Zeeb 	u_int refcnt;
73455edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
73465edde07cSBjoern A. Zeeb };
73475edde07cSBjoern A. Zeeb 
73485edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
73495edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
73505edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
73515edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
73525edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
73535edde07cSBjoern A. Zeeb 	size_t ssid_len;
73545edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
73555edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
73565edde07cSBjoern A. Zeeb 
73575edde07cSBjoern A. Zeeb 	/*
73585edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
73595edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
73605edde07cSBjoern A. Zeeb 	 */
73615edde07cSBjoern A. Zeeb 	bool match;
73625edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
73635edde07cSBjoern A. Zeeb };
73645edde07cSBjoern A. Zeeb 
73655edde07cSBjoern A. Zeeb static void
73665edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
73675edde07cSBjoern A. Zeeb {
73685edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
73695edde07cSBjoern A. Zeeb 	size_t ielen;
73705edde07cSBjoern A. Zeeb 
73715edde07cSBjoern A. Zeeb 	lookup = arg;
73725edde07cSBjoern A. Zeeb 
73735edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
73745edde07cSBjoern A. Zeeb 	if (lookup->match)
73755edde07cSBjoern A. Zeeb 		return;
73765edde07cSBjoern A. Zeeb 
73775edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
73785edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
73795edde07cSBjoern A. Zeeb 		return;
73805edde07cSBjoern A. Zeeb 
73815edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
73825edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
73835edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
73845edde07cSBjoern A. Zeeb 		return;
73855edde07cSBjoern A. Zeeb 	}
73865edde07cSBjoern A. Zeeb 
73875edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
73885edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
73895edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
73905edde07cSBjoern A. Zeeb 		return;
73915edde07cSBjoern A. Zeeb 	}
73925edde07cSBjoern A. Zeeb 
73935edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
73945edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
73955edde07cSBjoern A. Zeeb 
73965edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
73975edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
73985edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
73995edde07cSBjoern A. Zeeb 			return;
74005edde07cSBjoern A. Zeeb 	}
74015edde07cSBjoern A. Zeeb 
74025edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
74035edde07cSBjoern A. Zeeb 		return;
74045edde07cSBjoern A. Zeeb 
74055edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
74065edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
74075edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
74085edde07cSBjoern A. Zeeb 			return;
74095edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
74105edde07cSBjoern A. Zeeb 			return;
74115edde07cSBjoern A. Zeeb 	}
74125edde07cSBjoern A. Zeeb 
74135edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
74145edde07cSBjoern A. Zeeb 
74155edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
74165edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
74175edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
74185edde07cSBjoern A. Zeeb 		return;
74195edde07cSBjoern A. Zeeb 
74205edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
74215edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
74225edde07cSBjoern A. Zeeb 	if (ielen)
74235edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
74245edde07cSBjoern A. Zeeb 
74255edde07cSBjoern A. Zeeb 	lookup->match = true;
74265edde07cSBjoern A. Zeeb }
74275edde07cSBjoern A. Zeeb 
74285edde07cSBjoern A. Zeeb struct cfg80211_bss *
74295edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
74305edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
74315edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
74325edde07cSBjoern A. Zeeb {
74335edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
74345edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
74355edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
74365edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
74375edde07cSBjoern A. Zeeb 
74385edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
74395edde07cSBjoern A. Zeeb 
74405edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
74415edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
74425edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
74435edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
74445edde07cSBjoern A. Zeeb 		return (NULL);
74455edde07cSBjoern A. Zeeb 	}
74465edde07cSBjoern A. Zeeb 
74475edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
74485edde07cSBjoern A. Zeeb 	lookup.chan = chan;
74495edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
74505edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
74515edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
74525edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
74535edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
74545edde07cSBjoern A. Zeeb 	lookup.match = false;
74555edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
74565edde07cSBjoern A. Zeeb 
74575edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
74585edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
74595edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
74605edde07cSBjoern A. Zeeb 	if (!lookup.match) {
74615edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
74625edde07cSBjoern A. Zeeb 		return (NULL);
74635edde07cSBjoern A. Zeeb 	}
74645edde07cSBjoern A. Zeeb 
74655edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
74665edde07cSBjoern A. Zeeb 	return (&lbss->bss);
74675edde07cSBjoern A. Zeeb }
74685edde07cSBjoern A. Zeeb 
74695edde07cSBjoern A. Zeeb void
74705edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
74715edde07cSBjoern A. Zeeb {
74725edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
74735edde07cSBjoern A. Zeeb 
74745edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
74755edde07cSBjoern A. Zeeb 
74765edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
74775edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
74785edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
74795edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
74805edde07cSBjoern A. Zeeb 	}
74815edde07cSBjoern A. Zeeb }
74825edde07cSBjoern A. Zeeb 
74835edde07cSBjoern A. Zeeb void
74845edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
74855edde07cSBjoern A. Zeeb {
74865edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
74875edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
74885edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
74895edde07cSBjoern A. Zeeb 
74905edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
74915edde07cSBjoern A. Zeeb 	ic = lhw->ic;
74925edde07cSBjoern A. Zeeb 
74935edde07cSBjoern A. Zeeb 	/*
74945edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
74955edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
74965edde07cSBjoern A. Zeeb 	 */
74975edde07cSBjoern A. Zeeb 	if (ic == NULL ||
74985edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
74995edde07cSBjoern A. Zeeb 		return;
75005edde07cSBjoern A. Zeeb 
75015edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
75025edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
75035edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
75045edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
75055edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
75065edde07cSBjoern A. Zeeb }
75075edde07cSBjoern A. Zeeb 
75085edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
75095edde07cSBjoern A. Zeeb 
7510ac1d519cSBjoern A. Zeeb /*
7511ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7512ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7513ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7514ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7515ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7516ac1d519cSBjoern A. Zeeb  */
7517ac1d519cSBjoern A. Zeeb 
7518ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7519ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7520ac1d519cSBjoern A. Zeeb {
7521ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7522ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7523ac1d519cSBjoern A. Zeeb 	int error;
7524ac1d519cSBjoern A. Zeeb 
7525ac1d519cSBjoern A. Zeeb 	changed = 0;
7526ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7527ac1d519cSBjoern A. Zeeb 		cd = NULL;
7528ac1d519cSBjoern A. Zeeb 	else
7529ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7530ac1d519cSBjoern A. Zeeb 
7531ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7532ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7533ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7534ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7535ac1d519cSBjoern A. Zeeb 	}
7536ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7537ac1d519cSBjoern A. Zeeb 
7538ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7539ac1d519cSBjoern A. Zeeb 		return (0);
7540ac1d519cSBjoern A. Zeeb 
7541ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7542ac1d519cSBjoern A. Zeeb 	return (error);
7543ac1d519cSBjoern A. Zeeb }
7544ac1d519cSBjoern A. Zeeb 
7545ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7546ac1d519cSBjoern A. Zeeb 
75476b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
75486b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
75496b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7550