xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision a6f6329c92a993ddaaa4a7daf2b53155f805d50f)
16b4cac81SBjoern A. Zeeb /*-
2ec6185c5SBjoern A. Zeeb  * Copyright (c) 2020-2025 The FreeBSD Foundation
3c272abc5SBjoern A. Zeeb  * Copyright (c) 2020-2025 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
4211db70b6SBjoern A. Zeeb /*
4311db70b6SBjoern A. Zeeb  * TODO:
4411db70b6SBjoern A. Zeeb  * - lots :)
4511db70b6SBjoern A. Zeeb  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
4611db70b6SBjoern A. Zeeb  */
4711db70b6SBjoern A. Zeeb 
486b4cac81SBjoern A. Zeeb #include <sys/param.h>
496b4cac81SBjoern A. Zeeb #include <sys/types.h>
506b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
516b4cac81SBjoern A. Zeeb #include <sys/errno.h>
526b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
536b4cac81SBjoern A. Zeeb #include <sys/module.h>
546b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
5540839418SBjoern A. Zeeb #include <sys/sbuf.h>
566b4cac81SBjoern A. Zeeb #include <sys/socket.h>
576b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
586b4cac81SBjoern A. Zeeb #include <sys/queue.h>
596b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
60527687a9SBjoern A. Zeeb #include <sys/libkern.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net/if.h>
636b4cac81SBjoern A. Zeeb #include <net/if_var.h>
646b4cac81SBjoern A. Zeeb #include <net/if_media.h>
656b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
686b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
696b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
706b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
719fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
746b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
756b4cac81SBjoern A. Zeeb 
766b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
77a8f735a6SBjoern A. Zeeb #include <linux/rculist.h>
786b4cac81SBjoern A. Zeeb #include "linux_80211.h"
796b4cac81SBjoern A. Zeeb 
804a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
8111db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
8275d23d88SBjoern A. Zeeb #define	LKPI_80211_HT
832c44f1ffSBjoern A. Zeeb #define	LKPI_80211_VHT
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
317a1adefb1SBjoern A. Zeeb 		/* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
318a1adefb1SBjoern A. Zeeb 		if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
319a1adefb1SBjoern A. Zeeb 		    (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
320a1adefb1SBjoern A. Zeeb 			memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
321a1adefb1SBjoern A. Zeeb 			sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
322a1adefb1SBjoern A. Zeeb 		}
323a1adefb1SBjoern A. Zeeb 
32440839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
32640839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32940839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
33040839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
33140839418SBjoern A. Zeeb 
33240839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
33340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
33440839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
33540839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
33640839418SBjoern A. Zeeb 
33740839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33840839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33940839418SBjoern A. Zeeb 
34040839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
34140839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
34440839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
34540839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
34640839418SBjoern A. Zeeb 		}
34740839418SBjoern A. Zeeb 
34840839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34940839418SBjoern A. Zeeb 
35040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35140839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35240839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
35340839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
35440839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
35540839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35640839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35740839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35840839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35940839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
36040839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
36140839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
36240839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
36340839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
36440839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
36540839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
36640839418SBjoern A. Zeeb 	}
36740839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36840839418SBjoern A. Zeeb 
36940839418SBjoern A. Zeeb 	sbuf_finish(&s);
37040839418SBjoern A. Zeeb 	sbuf_delete(&s);
37140839418SBjoern A. Zeeb 
37240839418SBjoern A. Zeeb 	return (0);
37340839418SBjoern A. Zeeb }
37440839418SBjoern A. Zeeb 
37562d51a43SBjoern A. Zeeb static enum ieee80211_sta_rx_bw
37662d51a43SBjoern A. Zeeb lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
37762d51a43SBjoern A. Zeeb {
37862d51a43SBjoern A. Zeeb 	switch (cw) {
37962d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_320:
38062d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_320);
38162d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_160:
38262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80P80:
38362d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_160);
38462d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80:
38562d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_80);
38662d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_40:
38762d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_40);
38862d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20:
38962d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20_NOHT:
39062d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39162d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_5:
39262d51a43SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_10:
39362d51a43SBjoern A. Zeeb 		/* Unsupported input. */
39462d51a43SBjoern A. Zeeb 		return (IEEE80211_STA_RX_BW_20);
39562d51a43SBjoern A. Zeeb 	}
39662d51a43SBjoern A. Zeeb }
39762d51a43SBjoern A. Zeeb 
39862d51a43SBjoern A. Zeeb static enum nl80211_chan_width
39962d51a43SBjoern A. Zeeb lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bw rx_bw)
40062d51a43SBjoern A. Zeeb {
40162d51a43SBjoern A. Zeeb 	switch (rx_bw) {
40262d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_20:
40362d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_20);	/* _NOHT */
40462d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_40:
40562d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_40);
40662d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_80:
40762d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_80);
40862d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_160:
40962d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_160); /* 80P80 */
41062d51a43SBjoern A. Zeeb 	case IEEE80211_STA_RX_BW_320:
41162d51a43SBjoern A. Zeeb 		return (NL80211_CHAN_WIDTH_320);
41262d51a43SBjoern A. Zeeb 	}
41362d51a43SBjoern A. Zeeb }
41462d51a43SBjoern A. Zeeb 
41562d51a43SBjoern A. Zeeb static void
41662d51a43SBjoern A. Zeeb lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
41762d51a43SBjoern A. Zeeb     struct ieee80211_vif *vif, struct ieee80211_sta *sta)
41862d51a43SBjoern A. Zeeb {
41962d51a43SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
42062d51a43SBjoern A. Zeeb 	enum ieee80211_sta_rx_bw old_bw;
42162d51a43SBjoern A. Zeeb 	uint32_t changed;
42262d51a43SBjoern A. Zeeb 
42362d51a43SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
42462d51a43SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
42562d51a43SBjoern A. Zeeb 	if (chanctx_conf == NULL)
42662d51a43SBjoern A. Zeeb 		return;
42762d51a43SBjoern A. Zeeb 
42862d51a43SBjoern A. Zeeb 	old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
42962d51a43SBjoern A. Zeeb 	if (old_bw == sta->deflink.bandwidth)
43062d51a43SBjoern A. Zeeb 		return;
43162d51a43SBjoern A. Zeeb 
43262d51a43SBjoern A. Zeeb 	chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
43362d51a43SBjoern A. Zeeb 	if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
43462d51a43SBjoern A. Zeeb 	    !sta->deflink.ht_cap.ht_supported)
43562d51a43SBjoern A. Zeeb 		chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
43662d51a43SBjoern A. Zeeb 
43762d51a43SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
43862d51a43SBjoern A. Zeeb 
43962d51a43SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
44062d51a43SBjoern A. Zeeb 
44162d51a43SBjoern A. Zeeb 	changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
44262d51a43SBjoern A. Zeeb 	changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
44362d51a43SBjoern A. Zeeb 	lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
44462d51a43SBjoern A. Zeeb }
44562d51a43SBjoern A. Zeeb 
4469fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
4479fb91463SBjoern A. Zeeb static void
44862d51a43SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
44962d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
4509fb91463SBjoern A. Zeeb {
4519fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
4529fb91463SBjoern A. Zeeb 	uint8_t *ie;
4539fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
4549fb91463SBjoern A. Zeeb 	int i, rx_nss;
4559fb91463SBjoern A. Zeeb 
456f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
457f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
4589fb91463SBjoern A. Zeeb 		return;
459f9c7a07dSBjoern A. Zeeb 	}
4609fb91463SBjoern A. Zeeb 
4619fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
4629fb91463SBjoern A. Zeeb 
4639fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
4649fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
4659fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4669fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
4679fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
4689fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
4699fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
4709fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
4719fb91463SBjoern A. Zeeb 
4729fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
4739fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
4749fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
4759fb91463SBjoern A. Zeeb 		ie += 4;
4769fb91463SBjoern A. Zeeb 	ie += 2;
4779fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4789fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4799fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4809fb91463SBjoern A. Zeeb 
48162d51a43SBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
48262d51a43SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
483f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
4843e022a91SBjoern A. Zeeb 	else
4853e022a91SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
486f9c7a07dSBjoern A. Zeeb 
487f9c7a07dSBjoern A. Zeeb 	/*
488f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
489f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
490f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
491f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
492f9c7a07dSBjoern A. Zeeb 	 */
4939fb91463SBjoern A. Zeeb 	rx_nss = 0;
494f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4959fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4969fb91463SBjoern A. Zeeb 			rx_nss++;
4979fb91463SBjoern A. Zeeb 	}
498f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
499f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
5009fb91463SBjoern A. Zeeb 
501f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
502f9c7a07dSBjoern A. Zeeb 
503f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
504f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
505f9c7a07dSBjoern A. Zeeb 	else
506f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
507f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
508f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
509f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
510f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
511f9c7a07dSBjoern A. Zeeb 	}
512f9c7a07dSBjoern A. Zeeb #endif
5139fb91463SBjoern A. Zeeb }
5149fb91463SBjoern A. Zeeb #endif
5159fb91463SBjoern A. Zeeb 
5169fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5179fb91463SBjoern A. Zeeb static void
51862d51a43SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
51962d51a43SBjoern A. Zeeb     struct ieee80211_node *ni)
5209fb91463SBjoern A. Zeeb {
521f9c7a07dSBjoern A. Zeeb 	uint32_t width;
522f9c7a07dSBjoern A. Zeeb 	int rx_nss;
523f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
524f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
5259fb91463SBjoern A. Zeeb 
5265393cd34SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
5275393cd34SBjoern A. Zeeb 	    !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
528f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
5299fb91463SBjoern A. Zeeb 		return;
530f9c7a07dSBjoern A. Zeeb 	}
5319fb91463SBjoern A. Zeeb 
532f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
533f9c7a07dSBjoern A. Zeeb 
534f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
535f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
536f9c7a07dSBjoern A. Zeeb 
5373e022a91SBjoern A. Zeeb 	/*
5383e022a91SBjoern A. Zeeb 	 * If VHT20/40 are selected do not update the bandwidth
5393e022a91SBjoern A. Zeeb 	 * from HT but stya on VHT.
5403e022a91SBjoern A. Zeeb 	 */
5413e022a91SBjoern A. Zeeb 	if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
5423e022a91SBjoern A. Zeeb 		goto skip_bw;
5433e022a91SBjoern A. Zeeb 
544f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
545f9c7a07dSBjoern A. Zeeb 	switch (width) {
546f9c7a07dSBjoern A. Zeeb #if 0
547f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
548f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
5499fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
550f9c7a07dSBjoern A. Zeeb 		break;
551f9c7a07dSBjoern A. Zeeb #endif
552f9c7a07dSBjoern A. Zeeb 	default:
553f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
554f9c7a07dSBjoern A. Zeeb #if 0
555f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
556f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
557f9c7a07dSBjoern A. Zeeb 		else
558f9c7a07dSBjoern A. Zeeb #endif
5599fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
5609fb91463SBjoern A. Zeeb 	}
5613e022a91SBjoern A. Zeeb skip_bw:
562f9c7a07dSBjoern A. Zeeb 
563f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
564f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
565f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
566f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
567f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
568f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
569f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
570f9c7a07dSBjoern A. Zeeb 			break;
571f9c7a07dSBjoern A. Zeeb 		}
572f9c7a07dSBjoern A. Zeeb 	}
573f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
574f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
575f9c7a07dSBjoern A. Zeeb 
576f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
577f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
578f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
579f9c7a07dSBjoern A. Zeeb 		break;
580f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
581f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
582f9c7a07dSBjoern A. Zeeb 		break;
583f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
584f9c7a07dSBjoern A. Zeeb 	default:
585f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
586f9c7a07dSBjoern A. Zeeb 		break;
587f9c7a07dSBjoern A. Zeeb 	}
5889fb91463SBjoern A. Zeeb }
5899fb91463SBjoern A. Zeeb #endif
5909fb91463SBjoern A. Zeeb 
591d9f59799SBjoern A. Zeeb static void
59262d51a43SBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
59362d51a43SBjoern A. Zeeb     struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
594f9c7a07dSBjoern A. Zeeb {
595f9c7a07dSBjoern A. Zeeb 
596f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
59762d51a43SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(vif, sta, ni);
598f9c7a07dSBjoern A. Zeeb #endif
599f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
60062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(vif, sta, ni);
601f9c7a07dSBjoern A. Zeeb #endif
60262d51a43SBjoern A. Zeeb 	/*
60362d51a43SBjoern A. Zeeb 	 * We are also called from node allocation which net80211
60462d51a43SBjoern A. Zeeb 	 * can do even on `ifconfig down`; in that case the chanctx
60562d51a43SBjoern A. Zeeb 	 * may still be valid and we get a discrepancy between
60662d51a43SBjoern A. Zeeb 	 * sta and chanctx.  Thus do not try to update the chanctx
60762d51a43SBjoern A. Zeeb 	 * when called from lkpi_lsta_alloc().
60862d51a43SBjoern A. Zeeb 	 */
60962d51a43SBjoern A. Zeeb 	if (updchnctx)
61062d51a43SBjoern A. Zeeb 		lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
611f9c7a07dSBjoern A. Zeeb }
612f9c7a07dSBjoern A. Zeeb 
613389265e3SBjoern A. Zeeb static uint8_t
614389265e3SBjoern A. Zeeb lkpi_get_max_rx_chains(struct ieee80211_node *ni)
615389265e3SBjoern A. Zeeb {
616389265e3SBjoern A. Zeeb 	uint8_t chains;
617389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
618389265e3SBjoern A. Zeeb 	struct lkpi_sta *lsta;
619389265e3SBjoern A. Zeeb 	struct ieee80211_sta *sta;
620389265e3SBjoern A. Zeeb 
621389265e3SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
622389265e3SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
623389265e3SBjoern A. Zeeb #endif
624389265e3SBjoern A. Zeeb 
625389265e3SBjoern A. Zeeb 	chains = 1;
626389265e3SBjoern A. Zeeb #if defined(LKPI_80211_HT)
627389265e3SBjoern A. Zeeb 	IMPROVE("We should factor counting MCS/NSS out for sync and here");
628389265e3SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ht_supported)
629389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
630389265e3SBjoern A. Zeeb #endif
631389265e3SBjoern A. Zeeb 
632389265e3SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
633389265e3SBjoern A. Zeeb 	if (sta->deflink.vht_cap.vht_supported)
634389265e3SBjoern A. Zeeb 		chains = MAX(chains, sta->deflink.rx_nss);
635389265e3SBjoern A. Zeeb #endif
636389265e3SBjoern A. Zeeb 
637389265e3SBjoern A. Zeeb 	return (chains);
638389265e3SBjoern A. Zeeb }
639389265e3SBjoern A. Zeeb 
640f9c7a07dSBjoern A. Zeeb static void
64167674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
64267674c1cSBjoern A. Zeeb     const char *_f, int _l)
643d9f59799SBjoern A. Zeeb {
644d9f59799SBjoern A. Zeeb 
6459d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6469d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
647d9f59799SBjoern A. Zeeb 		return;
648d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
649d9f59799SBjoern A. Zeeb 		return;
650d9f59799SBjoern A. Zeeb 
651d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
65267674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
65367674c1cSBjoern A. Zeeb 	if (ni != NULL)
65467674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
655d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
656d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
65711db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
6589d9ba2b7SBjoern A. Zeeb #endif
659d9f59799SBjoern A. Zeeb }
660d9f59799SBjoern A. Zeeb 
661d9f59799SBjoern A. Zeeb static void
662d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
663d9f59799SBjoern A. Zeeb {
664d9f59799SBjoern A. Zeeb 
665cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(lsta->hw->wiphy);
666d9f59799SBjoern A. Zeeb 
667a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
668a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
669a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
670d9f59799SBjoern A. Zeeb }
671d9f59799SBjoern A. Zeeb 
6724f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
6734f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
6744f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
6754f61ef8bSBjoern A. Zeeb {
6764f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
6774f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
6784f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
6794f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
680b6b352e4SBjoern A. Zeeb 	int band, i, tid;
6814f61ef8bSBjoern A. Zeeb 
6824f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
6834f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
6844f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
6854f61ef8bSBjoern A. Zeeb 		return (NULL);
6864f61ef8bSBjoern A. Zeeb 
687a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
6884f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
6894f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
6904f61ef8bSBjoern A. Zeeb 	/*
6910936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
6920936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
6930936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
6940936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
6950936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
6960936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
6970936c648SBjoern A. Zeeb 	 * two separate allocations.
6984f61ef8bSBjoern A. Zeeb 	 */
6990936c648SBjoern A. Zeeb 	lsta->ni = ni;
7004f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
7014f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
7024f61ef8bSBjoern A. Zeeb 
7034f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
7044f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
7054f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
7064f61ef8bSBjoern A. Zeeb 
7074f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
708b6b352e4SBjoern A. Zeeb 
709b6b352e4SBjoern A. Zeeb 	/* TXQ */
7104f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
7114f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
7124f61ef8bSBjoern A. Zeeb 
713d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
7144f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
7154f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
7164f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
7174f61ef8bSBjoern A. Zeeb 			goto cleanup;
7184f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
7194f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
720d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
721d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
722d0d29110SBjoern A. Zeeb 				continue;
723d0d29110SBjoern A. Zeeb 			}
724d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
7254f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
7264f61ef8bSBjoern A. Zeeb 		} else {
727fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
7284f61ef8bSBjoern A. Zeeb 		}
729d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
7300cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
731d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
7324f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
7334f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
7340cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
735d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
736eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
7374f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
7384f61ef8bSBjoern A. Zeeb 	}
7394f61ef8bSBjoern A. Zeeb 
740b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
741b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
742b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
743b6b352e4SBjoern A. Zeeb 
744b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
745b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
746b6b352e4SBjoern A. Zeeb 			continue;
747b6b352e4SBjoern A. Zeeb 
748b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
74973cd1c5dSBjoern A. Zeeb 			switch (band) {
75073cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
75173cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
75273cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
75373cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
75473cd1c5dSBjoern A. Zeeb 				case 110:
75573cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
75673cd1c5dSBjoern A. Zeeb 				case 55:
75773cd1c5dSBjoern A. Zeeb 				case 20:
75873cd1c5dSBjoern A. Zeeb 				case 10:
759b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
76073cd1c5dSBjoern A. Zeeb 					break;
76173cd1c5dSBjoern A. Zeeb 				}
76273cd1c5dSBjoern A. Zeeb 				break;
76373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
76473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
76573cd1c5dSBjoern A. Zeeb 				case 240:
76673cd1c5dSBjoern A. Zeeb 				case 120:
76773cd1c5dSBjoern A. Zeeb 				case 60:
76873cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
76973cd1c5dSBjoern A. Zeeb 					break;
77073cd1c5dSBjoern A. Zeeb 				}
77173cd1c5dSBjoern A. Zeeb 				break;
77273cd1c5dSBjoern A. Zeeb 			}
773b6b352e4SBjoern A. Zeeb 		}
774b6b352e4SBjoern A. Zeeb 	}
7759fb91463SBjoern A. Zeeb 
7766ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
7779fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
778f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
7799fb91463SBjoern A. Zeeb 
78062d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
7819fb91463SBjoern A. Zeeb 
782f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
783b6b352e4SBjoern A. Zeeb 
7846ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
7856ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
7866ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
7876ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
7886ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
7896ffb7bd4SBjoern A. Zeeb 	}
7906ffb7bd4SBjoern A. Zeeb 
7914f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
792fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
7934f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
794832b8e98SBjoern A. Zeeb 	mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
7950936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
7964f61ef8bSBjoern A. Zeeb 
7974f61ef8bSBjoern A. Zeeb 	return (lsta);
7984f61ef8bSBjoern A. Zeeb 
7994f61ef8bSBjoern A. Zeeb cleanup:
800eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
801eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
802eac3646fSBjoern A. Zeeb 
803eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
804eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
8054f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
806eac3646fSBjoern A. Zeeb 	}
8074f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8084f61ef8bSBjoern A. Zeeb 	return (NULL);
8094f61ef8bSBjoern A. Zeeb }
8104f61ef8bSBjoern A. Zeeb 
8110936c648SBjoern A. Zeeb static void
8120936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
8130936c648SBjoern A. Zeeb {
8140936c648SBjoern A. Zeeb 	struct mbuf *m;
8150936c648SBjoern A. Zeeb 
8160936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
8170936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
8180936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
8190936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
8200936c648SBjoern A. Zeeb 
8210936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
8220936c648SBjoern A. Zeeb 	IMPROVE();
8230936c648SBjoern A. Zeeb 
824fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
825fa4e4257SBjoern A. Zeeb 
826fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
8270936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
828fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
8290936c648SBjoern A. Zeeb 
8300936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
8310936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
8320936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
8330936c648SBjoern A. Zeeb 
8340936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
8350936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
8360936c648SBjoern A. Zeeb 	while (m != NULL) {
8370936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
8380936c648SBjoern A. Zeeb 
8390936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
8400936c648SBjoern A. Zeeb 		if (nim != NULL)
8410936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
8420936c648SBjoern A. Zeeb 		m_freem(m);
8430936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
8440936c648SBjoern A. Zeeb 	}
8450936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
8460936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
847fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
8480936c648SBjoern A. Zeeb 
8490936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
8500936c648SBjoern A. Zeeb 
8510936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
8520936c648SBjoern A. Zeeb 
8530936c648SBjoern A. Zeeb 	/* Free lsta. */
8540936c648SBjoern A. Zeeb 	lsta->ni = NULL;
8550936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
8560936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
8570936c648SBjoern A. Zeeb }
8580936c648SBjoern A. Zeeb 
8590936c648SBjoern A. Zeeb 
8606b4cac81SBjoern A. Zeeb static enum nl80211_band
8616b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
8626b4cac81SBjoern A. Zeeb {
8636b4cac81SBjoern A. Zeeb 
8646b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
8656b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
8666b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
8676b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
8686b4cac81SBjoern A. Zeeb #ifdef __notyet__
8696b4cac81SBjoern A. Zeeb 	else if ()
8706b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
8716b4cac81SBjoern A. Zeeb 	else if ()
8726b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
8736b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
8746b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
8756b4cac81SBjoern A. Zeeb #endif
8766b4cac81SBjoern A. Zeeb 	else
8776b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
8786b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
8796b4cac81SBjoern A. Zeeb }
8806b4cac81SBjoern A. Zeeb 
8816b4cac81SBjoern A. Zeeb static uint32_t
8826b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
8836b4cac81SBjoern A. Zeeb {
8846b4cac81SBjoern A. Zeeb 
8856b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
8866b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
8876b4cac81SBjoern A. Zeeb 	switch (band) {
8886b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
8896b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
8906b4cac81SBjoern A. Zeeb 		break;
8916b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
8926b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
8936b4cac81SBjoern A. Zeeb 		break;
8946b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
8956b4cac81SBjoern A. Zeeb 		break;
8966b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
8976b4cac81SBjoern A. Zeeb 		break;
8986b4cac81SBjoern A. Zeeb 	default:
8996b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
9006b4cac81SBjoern A. Zeeb 		break;
9016b4cac81SBjoern A. Zeeb 	}
9026b4cac81SBjoern A. Zeeb 
9036b4cac81SBjoern A. Zeeb 	IMPROVE();
9046b4cac81SBjoern A. Zeeb 	return (0x00);
9056b4cac81SBjoern A. Zeeb }
9066b4cac81SBjoern A. Zeeb 
907e3a0b120SBjoern A. Zeeb #if 0
9086b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
9096b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
9106b4cac81SBjoern A. Zeeb {
9116b4cac81SBjoern A. Zeeb 
9126b4cac81SBjoern A. Zeeb 	switch (ac) {
9136b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
9146b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
9156b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
9166b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
9176b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
9186b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9196b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
9206b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
9216b4cac81SBjoern A. Zeeb 	default:
9226b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
9236b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
9246b4cac81SBjoern A. Zeeb 	}
9256b4cac81SBjoern A. Zeeb }
926e3a0b120SBjoern A. Zeeb #endif
9276b4cac81SBjoern A. Zeeb 
9286b4cac81SBjoern A. Zeeb static enum nl80211_iftype
9296b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
9306b4cac81SBjoern A. Zeeb {
9316b4cac81SBjoern A. Zeeb 
9326b4cac81SBjoern A. Zeeb 	switch (opmode) {
9336b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
9346b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
9356b4cac81SBjoern A. Zeeb 		break;
9366b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
9376b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
9386b4cac81SBjoern A. Zeeb 		break;
9396b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
9406b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
9416b4cac81SBjoern A. Zeeb 		break;
9426b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
9436b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
9446b4cac81SBjoern A. Zeeb 		break;
9456b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
9466b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
9476b4cac81SBjoern A. Zeeb 		break;
9486b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
9496b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
9506b4cac81SBjoern A. Zeeb 		break;
9516b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
9526b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9536b4cac81SBjoern A. Zeeb 	default:
9546b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
9556b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
9566b4cac81SBjoern A. Zeeb 	}
9576b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
9586b4cac81SBjoern A. Zeeb }
9596b4cac81SBjoern A. Zeeb 
960b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
96112a511c8SBjoern A. Zeeb static const char *
96212a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
96312a511c8SBjoern A. Zeeb {
96412a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
96512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
96612a511c8SBjoern A. Zeeb 		return ("WEP40");
967bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
968bf8c25f1SBjoern A. Zeeb 		return ("WEP104");
96912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
97012a511c8SBjoern A. Zeeb 		return ("TKIP");
97112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
97212a511c8SBjoern A. Zeeb 		return ("CCMP");
973bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
974bf8c25f1SBjoern A. Zeeb 		return ("CCMP_256");
97512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
97612a511c8SBjoern A. Zeeb 		return ("GCMP");
97712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
97812a511c8SBjoern A. Zeeb 		return ("GCMP_256");
979bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
980bf8c25f1SBjoern A. Zeeb 		return ("AES_CMAC");
981bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
982bf8c25f1SBjoern A. Zeeb 		return ("BIP_CMAC_256");
98312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
98412a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
98512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
98612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
98712a511c8SBjoern A. Zeeb 	default:
98812a511c8SBjoern A. Zeeb 		return ("??");
98912a511c8SBjoern A. Zeeb 	}
99012a511c8SBjoern A. Zeeb }
99112a511c8SBjoern A. Zeeb 
9926b4cac81SBjoern A. Zeeb static uint32_t
993bf8c25f1SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
994bf8c25f1SBjoern A. Zeeb     uint32_t wlan_cipher_suite)
9956b4cac81SBjoern A. Zeeb {
9966b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
9976b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
9986b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
999bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
1000bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
10016b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
10026b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
10036b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
1004906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
10056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
1006bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM_256);
1007bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
1008bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_128);
1009bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
1010bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_GCM_256);
1011bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
1012bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_128);
10136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1014bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_CMAC_256);
1015bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1016bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_128);
1017bf8c25f1SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1018bf8c25f1SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_BIP_GMAC_256);
10196b4cac81SBjoern A. Zeeb 	default:
1020bf8c25f1SBjoern A. Zeeb 		ic_printf(ic, "%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
102112a511c8SBjoern A. Zeeb 		    __func__,
102212a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
102312a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
10246b4cac81SBjoern A. Zeeb 		return (0);
10256b4cac81SBjoern A. Zeeb 	}
1026bf8c25f1SBjoern A. Zeeb }
10276b4cac81SBjoern A. Zeeb 
10286b4cac81SBjoern A. Zeeb static uint32_t
10296b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
10306b4cac81SBjoern A. Zeeb {
10316b4cac81SBjoern A. Zeeb 
10326b4cac81SBjoern A. Zeeb 	switch (cipher) {
10336b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
10346b4cac81SBjoern A. Zeeb 		if (keylen < 8)
10356b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
10366b4cac81SBjoern A. Zeeb 		else
10376b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
10386b4cac81SBjoern A. Zeeb 		break;
1039bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
1040bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
1041bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
1042bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
1043bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM_256:
1044bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP_256);
1045bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_128:
1046bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP);
1047bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_GCM_256:
1048bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_GCMP_256);
1049bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_128:
1050bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_AES_CMAC);
1051bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_CMAC_256:
1052bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1053bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_128:
1054bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1055bf8c25f1SBjoern A. Zeeb 	case IEEE80211_CIPHER_BIP_GMAC_256:
1056bf8c25f1SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1057bf8c25f1SBjoern A. Zeeb 
10586b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
10596b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
1060bf8c25f1SBjoern A. Zeeb 		/*
1061bf8c25f1SBjoern A. Zeeb 		 * TKIP w/ hw MIC support
1062bf8c25f1SBjoern A. Zeeb 		 * (gone wrong; should really be a crypto flag in net80211).
1063bf8c25f1SBjoern A. Zeeb 		 */
10646b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
10656b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
10666b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
10676b4cac81SBjoern A. Zeeb 		break;
10686b4cac81SBjoern A. Zeeb 	default:
10696b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
10706b4cac81SBjoern A. Zeeb 	};
10716b4cac81SBjoern A. Zeeb 	return (0);
10726b4cac81SBjoern A. Zeeb }
10736b4cac81SBjoern A. Zeeb #endif
10746b4cac81SBjoern A. Zeeb 
10756b4cac81SBjoern A. Zeeb #ifdef __notyet__
10766b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
10776b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
10786b4cac81SBjoern A. Zeeb {
10796b4cac81SBjoern A. Zeeb 
10806b4cac81SBjoern A. Zeeb 	/*
10816b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
10826b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
10836b4cac81SBjoern A. Zeeb 	 */
10846b4cac81SBjoern A. Zeeb 	switch (state) {
10856b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
10866b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
10876b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
10886b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
10896b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
10906b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
10916b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
10926b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
10936b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
10946b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
10956b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
10966b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
10976b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
10986b4cac81SBjoern A. Zeeb 	default:
10996b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
11006b4cac81SBjoern A. Zeeb 	};
11016b4cac81SBjoern A. Zeeb 
11026b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
11036b4cac81SBjoern A. Zeeb }
11046b4cac81SBjoern A. Zeeb #endif
11056b4cac81SBjoern A. Zeeb 
11066b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11076b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
11086b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
11096b4cac81SBjoern A. Zeeb {
11106b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
11116b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
11126b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
11136b4cac81SBjoern A. Zeeb 	int i, nchans;
11146b4cac81SBjoern A. Zeeb 
11156b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11166b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
11176b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
11186b4cac81SBjoern A. Zeeb 		return (NULL);
11196b4cac81SBjoern A. Zeeb 
11206b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
11216b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
11226b4cac81SBjoern A. Zeeb 		return (NULL);
11236b4cac81SBjoern A. Zeeb 
11246b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
11256b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
11266b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
11276b4cac81SBjoern A. Zeeb 			return (&channels[i]);
11286b4cac81SBjoern A. Zeeb 	}
11296b4cac81SBjoern A. Zeeb 
11306b4cac81SBjoern A. Zeeb 	return (NULL);
11316b4cac81SBjoern A. Zeeb }
11326b4cac81SBjoern A. Zeeb 
11332ac8a218SBjoern A. Zeeb #if 0
11346b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
11356b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
11366b4cac81SBjoern A. Zeeb {
11376b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
11386b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
11396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
11406b4cac81SBjoern A. Zeeb 
11416b4cac81SBjoern A. Zeeb 	chan = NULL;
11426b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
11436b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
11446b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
11456b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
11466b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
11476b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
11486b4cac81SBjoern A. Zeeb 	else
11496b4cac81SBjoern A. Zeeb 		c = NULL;
11506b4cac81SBjoern A. Zeeb 
11516b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
11526b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
11536b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
11546b4cac81SBjoern A. Zeeb 	}
11556b4cac81SBjoern A. Zeeb 
11566b4cac81SBjoern A. Zeeb 	return (chan);
11576b4cac81SBjoern A. Zeeb }
11582ac8a218SBjoern A. Zeeb #endif
11596b4cac81SBjoern A. Zeeb 
11602e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
11612e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
11622e183d99SBjoern A. Zeeb {
11632e183d99SBjoern A. Zeeb 	enum nl80211_band band;
11642e183d99SBjoern A. Zeeb 
11652e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11662e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
11672e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
11682e183d99SBjoern A. Zeeb 		int i;
11692e183d99SBjoern A. Zeeb 
11702e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
11712e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
11722e183d99SBjoern A. Zeeb 			continue;
11732e183d99SBjoern A. Zeeb 
11742e183d99SBjoern A. Zeeb 		channels = supband->channels;
11752e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
11762e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
11772e183d99SBjoern A. Zeeb 				return (&channels[i]);
11782e183d99SBjoern A. Zeeb 		}
11792e183d99SBjoern A. Zeeb 	}
11802e183d99SBjoern A. Zeeb 
11812e183d99SBjoern A. Zeeb 	return (NULL);
11822e183d99SBjoern A. Zeeb }
11832e183d99SBjoern A. Zeeb 
1184b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
11856b4cac81SBjoern A. Zeeb static int
118611db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
118711db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
118811db70b6SBjoern A. Zeeb {
118911db70b6SBjoern A. Zeeb 	int error;
119011db70b6SBjoern A. Zeeb 
119111db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
119211db70b6SBjoern A. Zeeb 		return (0);
119311db70b6SBjoern A. Zeeb 
119411db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
119511db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
119611db70b6SBjoern A. Zeeb 
119711db70b6SBjoern A. Zeeb 	error = 0;
119811db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
119911db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
120011db70b6SBjoern A. Zeeb 		int err;
120111db70b6SBjoern A. Zeeb 
120211db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
120311db70b6SBjoern A. Zeeb 			continue;
120411db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
120511db70b6SBjoern A. Zeeb 
1206*a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1207*a6f6329cSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1208*a6f6329cSBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: running set_key cmd %d(%s) for "
1209*a6f6329cSBjoern A. Zeeb 			    "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1210*a6f6329cSBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1211*a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1212*a6f6329cSBjoern A. Zeeb #endif
1213*a6f6329cSBjoern A. Zeeb 
121411db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
121511db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
121611db70b6SBjoern A. Zeeb 		if (err != 0) {
121711db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
121811db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
121911db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
122011db70b6SBjoern A. Zeeb 			error++;
122111db70b6SBjoern A. Zeeb 
122211db70b6SBjoern A. Zeeb 			/*
122311db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
122411db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
122511db70b6SBjoern A. Zeeb 			 * crash (firmware).
122611db70b6SBjoern A. Zeeb 			 */
122711db70b6SBjoern A. Zeeb 			continue;
122811db70b6SBjoern A. Zeeb 		}
122911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
123011db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
123111db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
1232*a6f6329cSBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
123311db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1234*a6f6329cSBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
123511db70b6SBjoern A. Zeeb #endif
123611db70b6SBjoern A. Zeeb 
123711db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
123811db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
123911db70b6SBjoern A. Zeeb 	}
124011db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
124111db70b6SBjoern A. Zeeb 	return (error);
124211db70b6SBjoern A. Zeeb }
124311db70b6SBjoern A. Zeeb 
124411db70b6SBjoern A. Zeeb static int
124511db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
12466b4cac81SBjoern A. Zeeb {
12476b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
12486b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12496b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12506b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
125111db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12526b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12536b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12546b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12556b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
12566b4cac81SBjoern A. Zeeb 	int error;
12576b4cac81SBjoern A. Zeeb 
12586b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
1259*a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1260*a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1261*a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1262*a6f6329cSBjoern A. Zeeb 		return (0);
1263*a6f6329cSBjoern A. Zeeb 	}
12646b4cac81SBjoern A. Zeeb 
126511db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
126611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
126711db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
126811db70b6SBjoern A. Zeeb 		return (0);
126911db70b6SBjoern A. Zeeb 	}
1270*a6f6329cSBjoern A. Zeeb 
127111db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
127211db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
127311db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
127411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
127511db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
127611db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
127711db70b6SBjoern A. Zeeb 		return (0);
127811db70b6SBjoern A. Zeeb 	}
127911db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
128011db70b6SBjoern A. Zeeb 
128111db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
128211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
128311db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
128411db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
128511db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
128611db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
128711db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
128811db70b6SBjoern A. Zeeb #endif
128911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
129011db70b6SBjoern A. Zeeb 		return (1);
129111db70b6SBjoern A. Zeeb 	}
129211db70b6SBjoern A. Zeeb 
129311db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
129411db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
129511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
129611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
129711db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
129811db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
129911db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
130011db70b6SBjoern A. Zeeb #endif
130111db70b6SBjoern A. Zeeb 		error = 1;
130211db70b6SBjoern A. Zeeb 		goto out;
130311db70b6SBjoern A. Zeeb 	}
130411db70b6SBjoern A. Zeeb 
1305*a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1306*a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1307*a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running  set_key cmd %d(%s) for sta %6D: "
1308*a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
1309*a6f6329cSBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1310*a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1311*a6f6329cSBjoern A. Zeeb #endif
1312*a6f6329cSBjoern A. Zeeb 
1313*a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1314*a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1315*a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1316*a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
131711db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
131811db70b6SBjoern A. Zeeb 	if (error != 0) {
131911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
132011db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
132111db70b6SBjoern A. Zeeb 		error = 0;
132211db70b6SBjoern A. Zeeb 		goto out;
132311db70b6SBjoern A. Zeeb 	}
132411db70b6SBjoern A. Zeeb 
132511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
132611db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
132711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1328*a6f6329cSBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %b\n", __func__,
132911db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
1330*a6f6329cSBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
133111db70b6SBjoern A. Zeeb #endif
133211db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
133311db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
133411db70b6SBjoern A. Zeeb 	error = 1;
133511db70b6SBjoern A. Zeeb out:
133611db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
133711db70b6SBjoern A. Zeeb 	return (error);
133811db70b6SBjoern A. Zeeb }
133911db70b6SBjoern A. Zeeb 
134011db70b6SBjoern A. Zeeb static int
134111db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
134211db70b6SBjoern A. Zeeb {
134311db70b6SBjoern A. Zeeb 
134411db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
134511db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
134611db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
134711db70b6SBjoern A. Zeeb }
134811db70b6SBjoern A. Zeeb 
134911db70b6SBjoern A. Zeeb static int
135011db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
135111db70b6SBjoern A. Zeeb {
135211db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
135311db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
135411db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
135511db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
135611db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
135711db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
135811db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
135911db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
136011db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
136111db70b6SBjoern A. Zeeb 	uint32_t lcipher;
136211db70b6SBjoern A. Zeeb 	int error;
136311db70b6SBjoern A. Zeeb 
136411db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
1365*a6f6329cSBjoern A. Zeeb 	if (IEEE80211_KEY_UNDEFINED(k)) {
1366*a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1367*a6f6329cSBjoern A. Zeeb 		    __func__, vap, k, k->wk_cipher, k->wk_keyix);
1368*a6f6329cSBjoern A. Zeeb 		return (0);
1369*a6f6329cSBjoern A. Zeeb 	}
137011db70b6SBjoern A. Zeeb 
137111db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
137211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
137311db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
137411db70b6SBjoern A. Zeeb 		return (0);
137511db70b6SBjoern A. Zeeb 	}
137611db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
137711db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
137811db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
137911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
138011db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
138111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
138211db70b6SBjoern A. Zeeb 		return (0);
138311db70b6SBjoern A. Zeeb 	}
138411db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
138511db70b6SBjoern A. Zeeb 
138611db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
138711db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
138811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
138911db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
139011db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
139111db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
139211db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
139311db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
139411db70b6SBjoern A. Zeeb 	}
139511db70b6SBjoern A. Zeeb 
139611db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
13976b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
139811db70b6SBjoern A. Zeeb 	switch (lcipher) {
139911db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
140011db70b6SBjoern A. Zeeb 		break;
140111db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
140211db70b6SBjoern A. Zeeb 	default:
140312a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
140412a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
140511db70b6SBjoern A. Zeeb 		IMPROVE();
140611db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
140711db70b6SBjoern A. Zeeb 		return (0);
140811db70b6SBjoern A. Zeeb 	}
140911db70b6SBjoern A. Zeeb 
141011db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
141111db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
141211db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
141311db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
14146b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
14156b4cac81SBjoern A. Zeeb #if 0
14166b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
14176b4cac81SBjoern A. Zeeb #endif
14186b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
14196b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
14206b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
14216b4cac81SBjoern A. Zeeb 
142211db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
142311db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
142411db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
142511db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
142611db70b6SBjoern A. Zeeb 
14276b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
14286b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
14296b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
14306b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
14316b4cac81SBjoern A. Zeeb 		break;
14326b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
14336b4cac81SBjoern A. Zeeb 	default:
143411db70b6SBjoern A. Zeeb 		/* currently UNREACH */
14356b4cac81SBjoern A. Zeeb 		IMPROVE();
143611db70b6SBjoern A. Zeeb 		break;
14376b4cac81SBjoern A. Zeeb 	};
143811db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
14396b4cac81SBjoern A. Zeeb 
1440*a6f6329cSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1441*a6f6329cSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1442*a6f6329cSBjoern A. Zeeb 		ic_printf(ic, "%s: running set_key cmd %d(%s) for sta %6D: "
1443*a6f6329cSBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %b\n", __func__,
1444*a6f6329cSBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
1445*a6f6329cSBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1446*a6f6329cSBjoern A. Zeeb #endif
1447*a6f6329cSBjoern A. Zeeb 
1448*a6f6329cSBjoern A. Zeeb 	lhw = ic->ic_softc;
1449*a6f6329cSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1450*a6f6329cSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1451*a6f6329cSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
145211db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
145311db70b6SBjoern A. Zeeb 	if (error != 0) {
145411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
145511db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
145611db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
145711db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
145811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
145911db70b6SBjoern A. Zeeb 		return (0);
14606b4cac81SBjoern A. Zeeb 	}
14616b4cac81SBjoern A. Zeeb 
146211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
146311db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
146411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
1465*a6f6329cSBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %b\n", __func__,
146611db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
1467*a6f6329cSBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
146811db70b6SBjoern A. Zeeb #endif
146911db70b6SBjoern A. Zeeb 
147011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
14716b4cac81SBjoern A. Zeeb 	return (1);
14726b4cac81SBjoern A. Zeeb }
14736b4cac81SBjoern A. Zeeb 
14746b4cac81SBjoern A. Zeeb static  int
14756b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
14766b4cac81SBjoern A. Zeeb {
14776b4cac81SBjoern A. Zeeb 
147811db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
14796b4cac81SBjoern A. Zeeb }
1480b8dfc3ecSBjoern A. Zeeb 
1481b8dfc3ecSBjoern A. Zeeb static void
1482b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1483b8dfc3ecSBjoern A. Zeeb {
1484b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1485b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1486b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1487b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1488b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1489a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1490b8dfc3ecSBjoern A. Zeeb 
1491b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1492b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1493b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1494b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1495b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1496b8dfc3ecSBjoern A. Zeeb 
1497a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1498a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1499b8dfc3ecSBjoern A. Zeeb 
1500b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1501b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1502a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1503a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1504a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1505a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1506a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1507b8dfc3ecSBjoern A. Zeeb #endif
1508b8dfc3ecSBjoern A. Zeeb 
1509b8dfc3ecSBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
1510a6165709SBjoern A. Zeeb 	if (icislocked)
1511a6165709SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
1512a6165709SBjoern A. Zeeb 	if (ntislocked)
1513b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1514b8dfc3ecSBjoern A. Zeeb 
1515b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1516b8dfc3ecSBjoern A. Zeeb 
1517b8dfc3ecSBjoern A. Zeeb 	/*
1518a6165709SBjoern A. Zeeb 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1519b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1520b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1521b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1522b8dfc3ecSBjoern A. Zeeb 	 */
1523a6165709SBjoern A. Zeeb 	if (icislocked)
1524a6165709SBjoern A. Zeeb 		refcount_acquire(&lvif->ic_unlocked);
1525a6165709SBjoern A. Zeeb 	if (ntislocked)
1526b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1527b8dfc3ecSBjoern A. Zeeb }
1528b8dfc3ecSBjoern A. Zeeb 
1529b8dfc3ecSBjoern A. Zeeb static void
1530b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1531b8dfc3ecSBjoern A. Zeeb {
1532b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1533b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1534b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1535b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1536b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1537a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1538b8dfc3ecSBjoern A. Zeeb 
1539b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1540b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1541b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1542b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1543b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1544b8dfc3ecSBjoern A. Zeeb 
1545a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1546a6165709SBjoern A. Zeeb 	MPASS(!icislocked);
1547a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1548a6165709SBjoern A. Zeeb 	MPASS(!ntislocked);
1549b8dfc3ecSBjoern A. Zeeb 
1550b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1551b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1552a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1553a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1554a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1555a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1556a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1557b8dfc3ecSBjoern A. Zeeb #endif
1558b8dfc3ecSBjoern A. Zeeb 
1559b8dfc3ecSBjoern A. Zeeb 	/*
1560b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1561b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1562b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1563b8dfc3ecSBjoern A. Zeeb 	 */
1564a6165709SBjoern A. Zeeb 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1565a6165709SBjoern A. Zeeb 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1566b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1567b8dfc3ecSBjoern A. Zeeb 
1568a6165709SBjoern A. Zeeb 	/*
1569a6165709SBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1570a6165709SBjoern A. Zeeb 	 * ic before nt to avoid a LOR.
1571a6165709SBjoern A. Zeeb 	 */
1572a6165709SBjoern A. Zeeb 	if (icislocked)
1573a6165709SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
1574a6165709SBjoern A. Zeeb 	if (ntislocked)
1575b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1576b8dfc3ecSBjoern A. Zeeb }
15776b4cac81SBjoern A. Zeeb #endif
15786b4cac81SBjoern A. Zeeb 
15796b4cac81SBjoern A. Zeeb static u_int
15806b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15816b4cac81SBjoern A. Zeeb {
15826b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
15836b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
15846b4cac81SBjoern A. Zeeb 
15856b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
15866b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
15876b4cac81SBjoern A. Zeeb 
15886b4cac81SBjoern A. Zeeb 	mc_list = arg;
15896b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
15906b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
15916b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
15926b4cac81SBjoern A. Zeeb 			return (0);
15936b4cac81SBjoern A. Zeeb 	}
15946b4cac81SBjoern A. Zeeb 
15956b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
15966b4cac81SBjoern A. Zeeb 	if (addr == NULL)
15976b4cac81SBjoern A. Zeeb 		return (0);
15986b4cac81SBjoern A. Zeeb 
15996b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
16006b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
16016b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
16026b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
16036b4cac81SBjoern A. Zeeb 	mc_list->count++;
16046b4cac81SBjoern A. Zeeb 
16059d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16069d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
16076b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
16086b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
16099d9ba2b7SBjoern A. Zeeb #endif
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 	return (1);
16126b4cac81SBjoern A. Zeeb }
16136b4cac81SBjoern A. Zeeb 
16146b4cac81SBjoern A. Zeeb static void
16156b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
16166b4cac81SBjoern A. Zeeb {
16176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16186b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16196b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
16206b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
16216b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
16226b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
16236b4cac81SBjoern A. Zeeb 	u64 mc;
16246b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
16256b4cac81SBjoern A. Zeeb 
16266b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
16276b4cac81SBjoern A. Zeeb 
16286b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
16296b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
16306b4cac81SBjoern A. Zeeb 		return;
16316b4cac81SBjoern A. Zeeb 
16326b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
16336b4cac81SBjoern A. Zeeb 		return;
16346b4cac81SBjoern A. Zeeb 
16356b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
16366b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
16376b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
16386b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
16396b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
16406b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
16416b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
16426b4cac81SBjoern A. Zeeb 	} else {
16436b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
16446b4cac81SBjoern A. Zeeb 	}
16456b4cac81SBjoern A. Zeeb 
16466b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16476b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
16486b4cac81SBjoern A. Zeeb 	/*
16496b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
16506b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
16516b4cac81SBjoern A. Zeeb 	 */
16526b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
16536b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
16546b4cac81SBjoern A. Zeeb 
16559d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16569d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
16576b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
16586b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
16599d9ba2b7SBjoern A. Zeeb #endif
16606b4cac81SBjoern A. Zeeb 
16616b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
16626b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
16636b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
16646b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
16656b4cac81SBjoern A. Zeeb 			mc_list.count--;
16666b4cac81SBjoern A. Zeeb 		}
16676b4cac81SBjoern A. Zeeb 	}
16686b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
16696b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
16706b4cac81SBjoern A. Zeeb }
16716b4cac81SBjoern A. Zeeb 
1672fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1673fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1674fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1675fa8f007dSBjoern A. Zeeb {
1676fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1677fa8f007dSBjoern A. Zeeb 
1678fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1679fa8f007dSBjoern A. Zeeb 
16809d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16819d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1682fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1683fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1684ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1685fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1686616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1687fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1688fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1689fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1690fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1691ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
16929d9ba2b7SBjoern A. Zeeb #endif
1693fa8f007dSBjoern A. Zeeb 
1694fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1695fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1696fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1697fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1698fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1699fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1700fa8f007dSBjoern A. Zeeb 	}
1701fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1702fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1703fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1704fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1705fa8f007dSBjoern A. Zeeb 	}
1706fa8f007dSBjoern A. Zeeb 
1707fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1708fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1709fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1710fa8f007dSBjoern A. Zeeb 
17119d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
17129d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1713fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1714fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1715ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1716fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1717616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1718fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1719fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1720fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1721fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1722ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
17239d9ba2b7SBjoern A. Zeeb #endif
1724fa8f007dSBjoern A. Zeeb 
1725fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1726fa8f007dSBjoern A. Zeeb }
1727fa8f007dSBjoern A. Zeeb 
17286b4cac81SBjoern A. Zeeb static void
17296b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
17306b4cac81SBjoern A. Zeeb {
17316b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
17326b4cac81SBjoern A. Zeeb 	int error;
17338ac540d3SBjoern A. Zeeb 	bool cancel;
17346b4cac81SBjoern A. Zeeb 
17358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
17368ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
17378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
17388ac540d3SBjoern A. Zeeb 	if (!cancel)
17396b4cac81SBjoern A. Zeeb 		return;
17406b4cac81SBjoern A. Zeeb 
17416b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17426b4cac81SBjoern A. Zeeb 
1743bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1744cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
17456b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
17466b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1747cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
17486b4cac81SBjoern A. Zeeb 
17496b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
17508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
17518ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
17528ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
17538ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
17548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
17558ac540d3SBjoern A. Zeeb 
1756bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
17576b4cac81SBjoern A. Zeeb 
17588ac540d3SBjoern A. Zeeb 	if (cancel)
17596b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
17606b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
17616b4cac81SBjoern A. Zeeb }
17626b4cac81SBjoern A. Zeeb 
17636b4cac81SBjoern A. Zeeb static void
1764086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1765086be6a8SBjoern A. Zeeb {
1766086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1767086be6a8SBjoern A. Zeeb 	int error;
1768086be6a8SBjoern A. Zeeb 	bool old;
1769086be6a8SBjoern A. Zeeb 
1770086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1771086be6a8SBjoern A. Zeeb 	if (old == new)
1772086be6a8SBjoern A. Zeeb 		return;
1773086be6a8SBjoern A. Zeeb 
1774086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1775086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1776086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1777086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1778086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1779086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1780086be6a8SBjoern A. Zeeb 	}
1781086be6a8SBjoern A. Zeeb }
1782086be6a8SBjoern A. Zeeb 
17835a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
17846b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
17856b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
17866b4cac81SBjoern A. Zeeb {
17875a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
17885a4d2461SBjoern A. Zeeb 
17895a4d2461SBjoern A. Zeeb 	changed = 0;
17906b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1791616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
17926b4cac81SBjoern A. Zeeb 
17936b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
17946b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
17956b4cac81SBjoern A. Zeeb 
1796616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1797616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
17986b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
17996b4cac81SBjoern A. Zeeb 		IMPROVE();
1800086be6a8SBjoern A. Zeeb 
18015a4d2461SBjoern A. Zeeb 		/*
18025a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
18035a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
18045a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
18055a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
18065a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
18075a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
18085a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
18095a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
18105a4d2461SBjoern A. Zeeb 		 */
18116b4cac81SBjoern A. Zeeb 	}
18125a4d2461SBjoern A. Zeeb 
18135a4d2461SBjoern A. Zeeb 	return (changed);
18146b4cac81SBjoern A. Zeeb }
18156b4cac81SBjoern A. Zeeb 
18166b4cac81SBjoern A. Zeeb static void
18176b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
18186b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
18196b4cac81SBjoern A. Zeeb {
18206b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
18216b4cac81SBjoern A. Zeeb 	int tid;
1822eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
18236b4cac81SBjoern A. Zeeb 
18246b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
18256b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
18266b4cac81SBjoern A. Zeeb 
18276b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
18286b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
18296b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
18306b4cac81SBjoern A. Zeeb 				continue;
18316b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
18326b4cac81SBjoern A. Zeeb 			continue;
18336b4cac81SBjoern A. Zeeb 
18346b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
18356b4cac81SBjoern A. Zeeb 			continue;
18366b4cac81SBjoern A. Zeeb 
18376b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
18386b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
18396b4cac81SBjoern A. Zeeb 			continue;
18406b4cac81SBjoern A. Zeeb 
1841eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1842eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1843eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1844eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
18456b4cac81SBjoern A. Zeeb 			continue;
18466b4cac81SBjoern A. Zeeb 
18476b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
18486b4cac81SBjoern A. Zeeb 	}
18496b4cac81SBjoern A. Zeeb }
18506b4cac81SBjoern A. Zeeb 
185188665349SBjoern A. Zeeb /*
185288665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
185388665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
185488665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
185588665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
185688665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
185788665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
185888665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
185988665349SBjoern A. Zeeb  * re-used.
186088665349SBjoern A. Zeeb  */
186188665349SBjoern A. Zeeb static void
186288665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
186388665349SBjoern A. Zeeb {
1864cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
186588665349SBjoern A. Zeeb 	struct mbufq mq;
186688665349SBjoern A. Zeeb 	struct mbuf *m;
186788665349SBjoern A. Zeeb 	int len;
186888665349SBjoern A. Zeeb 
1869cd0fcf9fSBjoern A. Zeeb 	/* There is no lockdep_assert_not_held_wiphy(). */
1870cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1871cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_not_held(&hw->wiphy->mtx);
187288665349SBjoern A. Zeeb 
187388665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
187488665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
187588665349SBjoern A. Zeeb 	lsta->txq_ready = false;
187688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
187788665349SBjoern A. Zeeb 
187888665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
187988665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
188088665349SBjoern A. Zeeb 
188188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
188288665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
188388665349SBjoern A. Zeeb 	if (len <= 0) {
188488665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
188588665349SBjoern A. Zeeb 		return;
188688665349SBjoern A. Zeeb 	}
188788665349SBjoern A. Zeeb 
188888665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
188988665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
189088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
189188665349SBjoern A. Zeeb 
189288665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
189388665349SBjoern A. Zeeb 	while (m != NULL) {
189488665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
189588665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
189688665349SBjoern A. Zeeb 	}
189788665349SBjoern A. Zeeb }
189888665349SBjoern A. Zeeb 
189950d826beSBjoern A. Zeeb 
190050d826beSBjoern A. Zeeb static void
190150d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
190250d826beSBjoern A. Zeeb {
190350d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1904231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
190550d826beSBjoern A. Zeeb 
1906231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1907231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1908231168c7SBjoern A. Zeeb 
1909231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1910231168c7SBjoern A. Zeeb 		return;
1911231168c7SBjoern A. Zeeb 
191250d826beSBjoern A. Zeeb 	/* Remove vif context. */
1913231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
191450d826beSBjoern A. Zeeb 
191550d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
191650d826beSBjoern A. Zeeb 
191750d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
191850d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1919231168c7SBjoern A. Zeeb 
1920231168c7SBjoern A. Zeeb 	/* Cleanup. */
1921231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
192250d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1923a8a47a41SBjoern A. Zeeb 	list_del(&lchanctx->entry);
192450d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
192550d826beSBjoern A. Zeeb }
192650d826beSBjoern A. Zeeb 
192750d826beSBjoern A. Zeeb 
19286b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
19296b4cac81SBjoern A. Zeeb 
19306b4cac81SBjoern A. Zeeb static int
19316b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19326b4cac81SBjoern A. Zeeb {
19336b4cac81SBjoern A. Zeeb 
19346b4cac81SBjoern A. Zeeb 	return (0);
19356b4cac81SBjoern A. Zeeb }
19366b4cac81SBjoern A. Zeeb 
19376b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
19386b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
19396b4cac81SBjoern A. Zeeb 
19406b4cac81SBjoern A. Zeeb static int
19416b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19426b4cac81SBjoern A. Zeeb {
19436b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1944c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1945d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
19466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19476b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19496b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19506b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19516b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19526b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
19536b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19546b4cac81SBjoern A. Zeeb 	uint32_t changed;
19556b4cac81SBjoern A. Zeeb 	int error;
19566b4cac81SBjoern A. Zeeb 
19572ac8a218SBjoern A. Zeeb 	/*
19582ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
19592ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
19602ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
19612ac8a218SBjoern A. Zeeb 	 */
19622ac8a218SBjoern A. Zeeb 
19632ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
19642ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
19652ac8a218SBjoern A. Zeeb 		return (EINVAL);
19662ac8a218SBjoern A. Zeeb 	}
19670936c648SBjoern A. Zeeb 	/*
19680936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
19690936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
19700936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
19710936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
19720936c648SBjoern A. Zeeb 	 */
19732ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
19742ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
19752ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
19762ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
19770936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19782ac8a218SBjoern A. Zeeb 		return (EINVAL);
19796b4cac81SBjoern A. Zeeb 	}
19806b4cac81SBjoern A. Zeeb 
19816b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19822ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
19832ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
19842ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
19852ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
19860936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19872ac8a218SBjoern A. Zeeb 		return (ESRCH);
19882ac8a218SBjoern A. Zeeb 	}
19892ac8a218SBjoern A. Zeeb 
19906b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19916b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19926b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19936b4cac81SBjoern A. Zeeb 
19940936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19950936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19960936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
19970936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19980936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19990936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20000936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20010936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
200292690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
200392690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
20040936c648SBjoern A. Zeeb 		return (EBUSY);
20050936c648SBjoern A. Zeeb 	}
20060936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20070936c648SBjoern A. Zeeb 
20086b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2009cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
20106b4cac81SBjoern A. Zeeb 
20116b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
2012560708cbSBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2013560708cbSBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
2014560708cbSBjoern A. Zeeb 	if (chanctx_conf != NULL) {
2015d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
20166b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
20176b4cac81SBjoern A. Zeeb 	} else {
20186b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
2019c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
20206b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
2021d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
20226b4cac81SBjoern A. Zeeb 	}
20236b4cac81SBjoern A. Zeeb 
2024d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
2025389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
2026d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
20276b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2028d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
2029d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
203090d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
203190d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
20329fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
20332ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
20342ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
203562d51a43SBjoern A. Zeeb 
20369fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
20379fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
203890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2039d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
204090d8e307SBjoern A. Zeeb 		else
2041d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
20429fb91463SBjoern A. Zeeb 	}
20439fb91463SBjoern A. Zeeb #endif
20449fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
20455393cd34SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
20469fb91463SBjoern A. Zeeb #ifdef __notyet__
204790d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2048d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
204990d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2050d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
205190d8e307SBjoern A. Zeeb 		else
205290d8e307SBjoern A. Zeeb #endif
205390d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2054d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
20559fb91463SBjoern A. Zeeb 	}
20569fb91463SBjoern A. Zeeb #endif
2057389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
20586b4cac81SBjoern A. Zeeb 	/* Responder ... */
205990d8e307SBjoern A. Zeeb #if 0
206090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2061d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
206290d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
206390d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
206490d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
206590d8e307SBjoern A. Zeeb #endif
206690d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
206790d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
206890d8e307SBjoern A. Zeeb #else
206990d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
207090d8e307SBjoern A. Zeeb #endif
20716b4cac81SBjoern A. Zeeb 
20726ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
20736ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
20746ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
20756ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
20766ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
20776ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
20786ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
20796ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
20806ffb7bd4SBjoern A. Zeeb 
20816ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
20826ffb7bd4SBjoern A. Zeeb 
20836ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
20846ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
20856ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
20866ffb7bd4SBjoern A. Zeeb 
20876ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
20886ffb7bd4SBjoern A. Zeeb 
20896b4cac81SBjoern A. Zeeb 	error = 0;
2090560708cbSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
20916b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
20926b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
20936b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
20946b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2095d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
20966b4cac81SBjoern A. Zeeb 	} else {
2097d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
20986b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
20997b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
21007b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
21017b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
2102d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
21037b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
2104d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
21056b4cac81SBjoern A. Zeeb 		} else {
2106018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2107018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
21086b4cac81SBjoern A. Zeeb 			goto out;
21096b4cac81SBjoern A. Zeeb 		}
21106ffb7bd4SBjoern A. Zeeb 
2111a8a47a41SBjoern A. Zeeb 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2112560708cbSBjoern A. Zeeb 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
21136ffb7bd4SBjoern A. Zeeb 
21146b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
21156b4cac81SBjoern A. Zeeb 		if (error == 0)
211668541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2117d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
21186b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
21196b4cac81SBjoern A. Zeeb 			error = 0;
21206b4cac81SBjoern A. Zeeb 		if (error != 0) {
2121018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2122018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
2123d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2124560708cbSBjoern A. Zeeb 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2125d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2126a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
2127c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
21286b4cac81SBjoern A. Zeeb 			goto out;
21296b4cac81SBjoern A. Zeeb 		}
21306b4cac81SBjoern A. Zeeb 	}
21316b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
21326b4cac81SBjoern A. Zeeb 
21336b4cac81SBjoern A. Zeeb 	/* RATES */
21346b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
21356b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
21366b4cac81SBjoern A. Zeeb 
2137d9f59799SBjoern A. Zeeb 	/*
21380936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
21390936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
21400936c648SBjoern A. Zeeb 	 * under the hoods.
2141d9f59799SBjoern A. Zeeb 	 */
21420936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
21430936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
21446b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2145e24e8103SBjoern A. Zeeb 
214688665349SBjoern A. Zeeb 	/*
214788665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
214888665349SBjoern A. Zeeb 	 * that we can tx again.
214988665349SBjoern A. Zeeb 	 */
215088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
215188665349SBjoern A. Zeeb 	lsta->txq_ready = true;
215288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
215388665349SBjoern A. Zeeb 
21542ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
2155a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2156e24e8103SBjoern A. Zeeb 
2157d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
21586b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21596b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
21606b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2161e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
21626b4cac81SBjoern A. Zeeb 	if (error != 0) {
21636b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2164018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2165018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21666b4cac81SBjoern A. Zeeb 		goto out;
21676b4cac81SBjoern A. Zeeb 	}
21686b4cac81SBjoern A. Zeeb #if 0
21696b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
21706b4cac81SBjoern A. Zeeb #endif
21716b4cac81SBjoern A. Zeeb 
21729d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21739d9ba2b7SBjoern A. Zeeb 
21741665ef97SBjoern A. Zeeb #if 0
21756b4cac81SBjoern A. Zeeb 	/*
21766b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
21776b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
21786b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
21796b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2180d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2181d9f59799SBjoern A. Zeeb 	 * for all queues.
21826b4cac81SBjoern A. Zeeb 	 */
2183e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
21841665ef97SBjoern A. Zeeb #endif
21856b4cac81SBjoern A. Zeeb 
21866b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21876b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21886b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
21896b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21906b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
21916b4cac81SBjoern A. Zeeb 
21926b4cac81SBjoern A. Zeeb 	/*
21936b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
21946b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
21956b4cac81SBjoern A. Zeeb 	 * - event_callback
21966b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
21976b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
21986b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
21996b4cac81SBjoern A. Zeeb 	 */
22006b4cac81SBjoern A. Zeeb 
2201cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2202105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2203105b9df2SBjoern A. Zeeb 
2204105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2205105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2206105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2207105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2208105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2209105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2210105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2211105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2212105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2213105b9df2SBjoern A. Zeeb 
2214105b9df2SBjoern A. Zeeb 	/*
2215105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2216105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2217105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2218105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2219105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2220105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2221105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2222105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2223105b9df2SBjoern A. Zeeb 	 */
2224105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2225105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2226105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
2227105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
2228105b9df2SBjoern A. Zeeb 	} else {
2229105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
2230105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
2231105b9df2SBjoern A. Zeeb 		/*
2232105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2233105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2234105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2235105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2236105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2237105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2238105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2239105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
2240105b9df2SBjoern A. Zeeb 		 */
2241105b9df2SBjoern A. Zeeb 	}
2242105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2243105b9df2SBjoern A. Zeeb 	goto out_relocked;
2244105b9df2SBjoern A. Zeeb 
22456b4cac81SBjoern A. Zeeb out:
2246cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
22476b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2248105b9df2SBjoern A. Zeeb out_relocked:
22490936c648SBjoern A. Zeeb 	/*
2250105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
22510936c648SBjoern A. Zeeb 	 * during the work of this function.
22520936c648SBjoern A. Zeeb 	 */
22536b4cac81SBjoern A. Zeeb 	if (ni != NULL)
22546b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
22556b4cac81SBjoern A. Zeeb 	return (error);
22566b4cac81SBjoern A. Zeeb }
22576b4cac81SBjoern A. Zeeb 
22586b4cac81SBjoern A. Zeeb static int
22596b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22606b4cac81SBjoern A. Zeeb {
22616b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22626b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22636b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22646b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22656b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22666b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22676b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22686b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22696b4cac81SBjoern A. Zeeb 	int error;
22706b4cac81SBjoern A. Zeeb 
22716b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22726b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22736b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22746b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22756b4cac81SBjoern A. Zeeb 
22762ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22772ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22782ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22792ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22802ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22812ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22822ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22832ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22842ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22852ac8a218SBjoern A. Zeeb #endif
22862ac8a218SBjoern A. Zeeb 
22872ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22882ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22892ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22902ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22912ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22922ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
22936b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22946b4cac81SBjoern A. Zeeb 
229567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22966b4cac81SBjoern A. Zeeb 
22976b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2298cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
22996b4cac81SBjoern A. Zeeb 
2300d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2301d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2302d9f59799SBjoern A. Zeeb 
23036b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
230488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
23056b4cac81SBjoern A. Zeeb 
23066b4cac81SBjoern A. Zeeb 	/* flush, no drop */
23076b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
23086b4cac81SBjoern A. Zeeb 
23096b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23106b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23116b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23126b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
23136b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23146b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23156b4cac81SBjoern A. Zeeb 	}
23166b4cac81SBjoern A. Zeeb 
23176b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
23186b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
23196b4cac81SBjoern A. Zeeb 
23206b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
23216b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2322d9f59799SBjoern A. Zeeb 
2323d9f59799SBjoern A. Zeeb 	/* Take the station down. */
23246b4cac81SBjoern A. Zeeb 
23256b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
23266b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
23276b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2328d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2329e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
23306b4cac81SBjoern A. Zeeb 	if (error != 0) {
23316b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2332018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2333018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23346b4cac81SBjoern A. Zeeb 		goto out;
23356b4cac81SBjoern A. Zeeb 	}
23366b4cac81SBjoern A. Zeeb #if 0
23376b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
23386b4cac81SBjoern A. Zeeb #endif
23396b4cac81SBjoern A. Zeeb 
234067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
23416b4cac81SBjoern A. Zeeb 
23422ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23432ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23442ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23452ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2347d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23480936c648SBjoern A. Zeeb 	/*
23490936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23500936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23510936c648SBjoern A. Zeeb 	 * and potentially freed.
23520936c648SBjoern A. Zeeb 	 */
23530936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2354d9f59799SBjoern A. Zeeb 
2355d9f59799SBjoern A. Zeeb 	/* conf_tx */
2356d9f59799SBjoern A. Zeeb 
235750d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
23586b4cac81SBjoern A. Zeeb 
23596b4cac81SBjoern A. Zeeb out:
2360cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
23616b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
23626b4cac81SBjoern A. Zeeb 	return (error);
23636b4cac81SBjoern A. Zeeb }
23646b4cac81SBjoern A. Zeeb 
23656b4cac81SBjoern A. Zeeb static int
23666b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23676b4cac81SBjoern A. Zeeb {
23686b4cac81SBjoern A. Zeeb 	int error;
23696b4cac81SBjoern A. Zeeb 
23706b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
23716b4cac81SBjoern A. Zeeb 	if (error == 0)
23726b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
23736b4cac81SBjoern A. Zeeb 	return (error);
23746b4cac81SBjoern A. Zeeb }
23756b4cac81SBjoern A. Zeeb 
23766b4cac81SBjoern A. Zeeb static int
23776b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23786b4cac81SBjoern A. Zeeb {
23796b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23806b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23816b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23826b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23836b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23846b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23856b4cac81SBjoern A. Zeeb 	int error;
23866b4cac81SBjoern A. Zeeb 
23876b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23896b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23906b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23916b4cac81SBjoern A. Zeeb 
23926b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2393cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
23942ac8a218SBjoern A. Zeeb 
23952ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23962ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
23972ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
23982ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23992ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24002ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24012ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24022ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24032ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24042ac8a218SBjoern A. Zeeb #endif
24052ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24062ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24072ac8a218SBjoern A. Zeeb 		goto out;
24082ac8a218SBjoern A. Zeeb 	}
24092ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24102ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24112ac8a218SBjoern A. Zeeb 
24122ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
24136b4cac81SBjoern A. Zeeb 
24146b4cac81SBjoern A. Zeeb 	/* Finish auth. */
24156b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
24166b4cac81SBjoern A. Zeeb 
24176b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
24186b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
24196b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2420e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2421018d93ecSBjoern A. Zeeb 	if (error != 0) {
2422018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2423018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24246b4cac81SBjoern A. Zeeb 		goto out;
2425018d93ecSBjoern A. Zeeb 	}
24266b4cac81SBjoern A. Zeeb 
24276b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
24286b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
24296b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24306b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
24316b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
24326b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24336b4cac81SBjoern A. Zeeb 	}
24346b4cac81SBjoern A. Zeeb 
24356b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
24366b4cac81SBjoern A. Zeeb 
24376b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
24386b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
24396b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24406b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
24416b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
24426b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
24436b4cac81SBjoern A. Zeeb 	}
24446b4cac81SBjoern A. Zeeb 
24456b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
244688665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
24476b4cac81SBjoern A. Zeeb 
24486b4cac81SBjoern A. Zeeb 	/*
24496b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
24506b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
24516b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
24526b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
24536b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
24546b4cac81SBjoern A. Zeeb 	 * - event_callback
24556b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
24566b4cac81SBjoern A. Zeeb 	 */
24576b4cac81SBjoern A. Zeeb 
24586b4cac81SBjoern A. Zeeb out:
2459cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24606b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24616b4cac81SBjoern A. Zeeb 	return (error);
24626b4cac81SBjoern A. Zeeb }
24636b4cac81SBjoern A. Zeeb 
24646b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
24656b4cac81SBjoern A. Zeeb static int
24666b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24676b4cac81SBjoern A. Zeeb {
24686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24716b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24726b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24736b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24742ac8a218SBjoern A. Zeeb 	int error;
24756b4cac81SBjoern A. Zeeb 
24766b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24776b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24786b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24796b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24806b4cac81SBjoern A. Zeeb 
24816b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2482cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24832ac8a218SBjoern A. Zeeb 
24842ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24852ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24862ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24872ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24882ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24892ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24902ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24912ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24922ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24932ac8a218SBjoern A. Zeeb #endif
24942ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24952ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24962ac8a218SBjoern A. Zeeb 		goto out;
24972ac8a218SBjoern A. Zeeb 	}
24982ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24992ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25002ac8a218SBjoern A. Zeeb 
25012ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
25022ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
25036b4cac81SBjoern A. Zeeb 
25046b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
25056b4cac81SBjoern A. Zeeb 
25066b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25076b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25086b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25096b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
25106b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25116b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25126b4cac81SBjoern A. Zeeb 	}
25136b4cac81SBjoern A. Zeeb 
25146b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
25156b4cac81SBjoern A. Zeeb 
25166b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
25176b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
25186b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25196b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
25206b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
25216b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
25226b4cac81SBjoern A. Zeeb 	}
25236b4cac81SBjoern A. Zeeb 
25242ac8a218SBjoern A. Zeeb 	error = 0;
25252ac8a218SBjoern A. Zeeb out:
2526cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
25276b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25286b4cac81SBjoern A. Zeeb 
25292ac8a218SBjoern A. Zeeb 	return (error);
25306b4cac81SBjoern A. Zeeb }
25316b4cac81SBjoern A. Zeeb 
25326b4cac81SBjoern A. Zeeb static int
2533d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25346b4cac81SBjoern A. Zeeb {
25356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25366b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25376b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25386b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25396b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25406b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25416b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
25426b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2543d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
25446b4cac81SBjoern A. Zeeb 	int error;
25456b4cac81SBjoern A. Zeeb 
25466b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25476b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25486b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25496b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25506b4cac81SBjoern A. Zeeb 
25512ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2552cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
25532ac8a218SBjoern A. Zeeb 
25542ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25552ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25562ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
25572ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
25582ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25592ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25602ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25612ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25622ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25632ac8a218SBjoern A. Zeeb #endif
25642ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25652ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25662ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25672ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25682ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25692ac8a218SBjoern A. Zeeb 
25702ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25716b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25726b4cac81SBjoern A. Zeeb 
257367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2574d9f59799SBjoern A. Zeeb 
2575d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2576d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2577d9f59799SBjoern A. Zeeb 
2578d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2579d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2580d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2581d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2582d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2583ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2584d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2585d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2586d9f59799SBjoern A. Zeeb 	}
2587d9f59799SBjoern A. Zeeb 
2588cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2589d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2590d9f59799SBjoern A. Zeeb 
259188665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2592d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2593018d93ecSBjoern A. Zeeb 	if (error != 0) {
2594018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2595018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2596d9f59799SBjoern A. Zeeb 		goto outni;
2597018d93ecSBjoern A. Zeeb 	}
2598d9f59799SBjoern A. Zeeb 
2599d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
260088665349SBjoern A. Zeeb 
260188665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
260288665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
260388665349SBjoern A. Zeeb 
2604cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2605d9f59799SBjoern A. Zeeb 
260667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2607d9f59799SBjoern A. Zeeb 
2608d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
260988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2610d9f59799SBjoern A. Zeeb 
2611d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2612d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2613d9f59799SBjoern A. Zeeb 
26146b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
26156b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
26166b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
26176b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2618ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
26196b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
26206b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
26216b4cac81SBjoern A. Zeeb 	}
26226b4cac81SBjoern A. Zeeb 
2623d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2624d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2625d9f59799SBjoern A. Zeeb 
2626d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2627d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2628d9f59799SBjoern A. Zeeb 
2629d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2630d9f59799SBjoern A. Zeeb 
26316b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
26326b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26336b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
26346b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2635e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2636018d93ecSBjoern A. Zeeb 	if (error != 0) {
2637018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2638018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26396b4cac81SBjoern A. Zeeb 		goto out;
2640018d93ecSBjoern A. Zeeb 	}
26416b4cac81SBjoern A. Zeeb 
26425a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
26435a4d2461SBjoern A. Zeeb 	bss_changed = 0;
26445a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
26456b4cac81SBjoern A. Zeeb 
26465a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
264716e688b2SBjoern A. Zeeb 
2648d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2649d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2650d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2651d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2652e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2653d9f59799SBjoern A. Zeeb 	if (error != 0) {
2654d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2655018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2656018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2657d9f59799SBjoern A. Zeeb 		goto out;
2658d9f59799SBjoern A. Zeeb 	}
2659d9f59799SBjoern A. Zeeb 
266016e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2661d9f59799SBjoern A. Zeeb 
2662d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2663d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2664d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2665616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2666616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2667d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2668d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2669d9f59799SBjoern A. Zeeb 
26702ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26712ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
26722ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
26732ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26742ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2675d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
26760936c648SBjoern A. Zeeb 	/*
26770936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
26780936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
26790936c648SBjoern A. Zeeb 	 * and potentially freed.
26800936c648SBjoern A. Zeeb 	 */
26810936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2682d9f59799SBjoern A. Zeeb 
2683d9f59799SBjoern A. Zeeb 	/* conf_tx */
2684d9f59799SBjoern A. Zeeb 
268550d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2686d9f59799SBjoern A. Zeeb 
2687d9f59799SBjoern A. Zeeb 	error = EALREADY;
26886b4cac81SBjoern A. Zeeb out:
2689cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
26906b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2691d9f59799SBjoern A. Zeeb outni:
26926b4cac81SBjoern A. Zeeb 	return (error);
26936b4cac81SBjoern A. Zeeb }
26946b4cac81SBjoern A. Zeeb 
26956b4cac81SBjoern A. Zeeb static int
2696d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2697d9f59799SBjoern A. Zeeb {
2698d9f59799SBjoern A. Zeeb 	int error;
2699d9f59799SBjoern A. Zeeb 
2700d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2701d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2702d9f59799SBjoern A. Zeeb 		return (error);
2703d9f59799SBjoern A. Zeeb 
2704d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2705d9f59799SBjoern A. Zeeb 
2706d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2707d9f59799SBjoern A. Zeeb 	return (error);
2708d9f59799SBjoern A. Zeeb }
2709d9f59799SBjoern A. Zeeb 
2710d9f59799SBjoern A. Zeeb static int
27116b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27126b4cac81SBjoern A. Zeeb {
27136b4cac81SBjoern A. Zeeb 	int error;
27146b4cac81SBjoern A. Zeeb 
2715d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
27166b4cac81SBjoern A. Zeeb 	return (error);
27176b4cac81SBjoern A. Zeeb }
27186b4cac81SBjoern A. Zeeb 
27196b4cac81SBjoern A. Zeeb static int
27206b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27216b4cac81SBjoern A. Zeeb {
27226b4cac81SBjoern A. Zeeb 	int error;
27236b4cac81SBjoern A. Zeeb 
2724d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
27256b4cac81SBjoern A. Zeeb 	return (error);
27266b4cac81SBjoern A. Zeeb }
27276b4cac81SBjoern A. Zeeb 
27286b4cac81SBjoern A. Zeeb static int
27296b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
27306b4cac81SBjoern A. Zeeb {
27316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27326b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27336b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27346b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27356b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
27366b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27376b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
27386b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
27396b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
27406b4cac81SBjoern A. Zeeb 	int error;
27416b4cac81SBjoern A. Zeeb 
27426b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
27436b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27446b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27456b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27466b4cac81SBjoern A. Zeeb 
27476b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2748cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
27492ac8a218SBjoern A. Zeeb 
27502ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27512ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
27522ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
27532ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27542ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
27552ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27562ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27572ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27582ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27592ac8a218SBjoern A. Zeeb #endif
27602ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
27612ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
27622ac8a218SBjoern A. Zeeb 		goto out;
27632ac8a218SBjoern A. Zeeb 	}
27642ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27652ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27662ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27672ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27682ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27692ac8a218SBjoern A. Zeeb 
27702ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27716b4cac81SBjoern A. Zeeb 
27726b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
27736b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
27746b4cac81SBjoern A. Zeeb 
27759597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
27769597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
27779597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
27789597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
27796b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27809597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
27814a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27824a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
27834a67f1dfSBjoern A. Zeeb 		sta->wme = true;
27844a67f1dfSBjoern A. Zeeb #endif
2785e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2786018d93ecSBjoern A. Zeeb 	if (error != 0) {
2787018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2788018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27899597f7cbSBjoern A. Zeeb 		goto out;
2790018d93ecSBjoern A. Zeeb 	}
27916b4cac81SBjoern A. Zeeb 
27926b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
27936b4cac81SBjoern A. Zeeb 
27946b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
27956b4cac81SBjoern A. Zeeb 	bss_changed = 0;
27964a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27974a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
27984a67f1dfSBjoern A. Zeeb #endif
2799616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2800616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2801616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
28026b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
28036b4cac81SBjoern A. Zeeb 	}
28046b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2805616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2806616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
28076b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
28086b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
28096b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
28106b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
28116b4cac81SBjoern A. Zeeb 	}
28126b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
28136b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
28146b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
28156b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
28166b4cac81SBjoern A. Zeeb 	}
28176b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
28186b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
28196b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
28206b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
28216b4cac81SBjoern A. Zeeb 	}
2822fa8f007dSBjoern A. Zeeb 
2823fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
28246b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
28256b4cac81SBjoern A. Zeeb 
28266b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
28276b4cac81SBjoern A. Zeeb 	 * - event_callback
28286b4cac81SBjoern A. Zeeb 	 */
28296b4cac81SBjoern A. Zeeb 
28306b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
28316b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
28326b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
28336b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
28346b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
28356b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
28366b4cac81SBjoern A. Zeeb 	}
28376b4cac81SBjoern A. Zeeb 
2838086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2839086be6a8SBjoern A. Zeeb 
28406b4cac81SBjoern A. Zeeb 	/*
28416b4cac81SBjoern A. Zeeb 	 * And then:
28426b4cac81SBjoern A. Zeeb 	 * - (more packets)?
28436b4cac81SBjoern A. Zeeb 	 * - set_key
28446b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
28456b4cac81SBjoern A. Zeeb 	 * - set_key (?)
28466b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
28476b4cac81SBjoern A. Zeeb 	 */
28486b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
28496b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
28506b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
28516b4cac81SBjoern A. Zeeb 
28526b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
28536b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
28546b4cac81SBjoern A. Zeeb 	}
28556b4cac81SBjoern A. Zeeb 
2856f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
28579fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
285862d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
28599fb91463SBjoern A. Zeeb 
28606b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
28616b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28626b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28636b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2864e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
28656b4cac81SBjoern A. Zeeb 	if (error != 0) {
28666b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2867018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2868018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28696b4cac81SBjoern A. Zeeb 		goto out;
28706b4cac81SBjoern A. Zeeb 	}
28716b4cac81SBjoern A. Zeeb 
28726b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
28736b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
28746b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
28756b4cac81SBjoern A. Zeeb 	 *
28766b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
28776b4cac81SBjoern A. Zeeb 	 */
28786b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
28796b4cac81SBjoern A. Zeeb 
2880fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2881fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2882fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2883fa8f007dSBjoern A. Zeeb 
28846b4cac81SBjoern A. Zeeb out:
2885cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
28866b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
28876b4cac81SBjoern A. Zeeb 	return (error);
28886b4cac81SBjoern A. Zeeb }
28896b4cac81SBjoern A. Zeeb 
28906b4cac81SBjoern A. Zeeb static int
28916b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28926b4cac81SBjoern A. Zeeb {
28936b4cac81SBjoern A. Zeeb 	int error;
28946b4cac81SBjoern A. Zeeb 
28956b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
28966b4cac81SBjoern A. Zeeb 	if (error == 0)
28976b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
28986b4cac81SBjoern A. Zeeb 	return (error);
28996b4cac81SBjoern A. Zeeb }
29006b4cac81SBjoern A. Zeeb 
29016b4cac81SBjoern A. Zeeb static int
29026b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29036b4cac81SBjoern A. Zeeb {
29046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
29056b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
29066b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
29076b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
29086b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
29096b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
29106b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2911d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2912d9f59799SBjoern A. Zeeb #if 0
2913d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2914d9f59799SBjoern A. Zeeb #endif
29156b4cac81SBjoern A. Zeeb 	int error;
29166b4cac81SBjoern A. Zeeb 
29176b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29186b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
29206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29216b4cac81SBjoern A. Zeeb 
29222ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
29232ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
29242ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
29252ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
29262ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
29272ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
29282ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
29292ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
29302ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
29312ac8a218SBjoern A. Zeeb #endif
29322ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
29332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
29342ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
29352ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
29362ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
29372ac8a218SBjoern A. Zeeb 
29382ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
29396b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
29406b4cac81SBjoern A. Zeeb 
294167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2942d9f59799SBjoern A. Zeeb 
2943d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2944cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2945d9f59799SBjoern A. Zeeb 
2946d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2947d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2948d9f59799SBjoern A. Zeeb 
2949d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2950d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2951d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2952d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2953d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2954ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2955d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2956d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2957d9f59799SBjoern A. Zeeb 	}
2958d9f59799SBjoern A. Zeeb 
2959cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2960d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2961d9f59799SBjoern A. Zeeb 
2962d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2963d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2964018d93ecSBjoern A. Zeeb 	if (error != 0) {
2965018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2966018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2967d9f59799SBjoern A. Zeeb 		goto outni;
2968018d93ecSBjoern A. Zeeb 	}
2969d9f59799SBjoern A. Zeeb 
2970d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
297188665349SBjoern A. Zeeb 
297288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
297388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
297488665349SBjoern A. Zeeb 
2975cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2976d9f59799SBjoern A. Zeeb 
297767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2978d9f59799SBjoern A. Zeeb 
2979d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
298088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2981d9f59799SBjoern A. Zeeb 
2982d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2983d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2984d9f59799SBjoern A. Zeeb 
2985d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2986d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2987d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2988d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2989ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2990d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2991d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2992d9f59799SBjoern A. Zeeb 	}
2993d9f59799SBjoern A. Zeeb 
2994d9f59799SBjoern A. Zeeb #if 0
2995d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2996d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2997d9f59799SBjoern A. Zeeb 
2998d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2999d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3000d9f59799SBjoern A. Zeeb #endif
3001d9f59799SBjoern A. Zeeb 
3002d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3003d9f59799SBjoern A. Zeeb 
30046b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
30056b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30066b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
30076b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3008e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3009018d93ecSBjoern A. Zeeb 	if (error != 0) {
3010018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3011018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
30126b4cac81SBjoern A. Zeeb 		goto out;
3013018d93ecSBjoern A. Zeeb 	}
30146b4cac81SBjoern A. Zeeb 
301567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
30166b4cac81SBjoern A. Zeeb 
301711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
301811db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
301911db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
302011db70b6SBjoern A. Zeeb 		if (error != 0) {
302111db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
302211db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
302311db70b6SBjoern A. Zeeb 			/*
302411db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
302511db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
302611db70b6SBjoern A. Zeeb 			 * less appropriate time).
302711db70b6SBjoern A. Zeeb 			 */
302811db70b6SBjoern A. Zeeb 			/* goto out; */
302911db70b6SBjoern A. Zeeb 		}
303011db70b6SBjoern A. Zeeb 	}
303111db70b6SBjoern A. Zeeb #endif
303211db70b6SBjoern A. Zeeb 
30336b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
30346b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30356b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
30366b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3037e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3038018d93ecSBjoern A. Zeeb 	if (error != 0) {
3039018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3040018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
30416b4cac81SBjoern A. Zeeb 		goto out;
3042018d93ecSBjoern A. Zeeb 	}
30436b4cac81SBjoern A. Zeeb 
304467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
30456b4cac81SBjoern A. Zeeb 
3046d9f59799SBjoern A. Zeeb #if 0
3047d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
3048d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
3049d9f59799SBjoern A. Zeeb #endif
3050d9f59799SBjoern A. Zeeb 
3051d9f59799SBjoern A. Zeeb 	error = EALREADY;
30526b4cac81SBjoern A. Zeeb out:
3053cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
30546b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3055d9f59799SBjoern A. Zeeb outni:
30566b4cac81SBjoern A. Zeeb 	return (error);
30576b4cac81SBjoern A. Zeeb }
30586b4cac81SBjoern A. Zeeb 
30596b4cac81SBjoern A. Zeeb static int
3060d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3061d9f59799SBjoern A. Zeeb {
3062d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3063d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3064d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
3065d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3066d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
3067d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
3068d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3069d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
3070d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
3071d9f59799SBjoern A. Zeeb 	int error;
3072d9f59799SBjoern A. Zeeb 
3073d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
3074d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3075d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3076d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3077d9f59799SBjoern A. Zeeb 
30782ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3079cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
30802ac8a218SBjoern A. Zeeb 
30812ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30822ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30832ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
30842ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
30852ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
30862ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
30872ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
30882ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
30892ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
30902ac8a218SBjoern A. Zeeb #endif
30912ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
30922ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30932ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
30942ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
30952ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
30962ac8a218SBjoern A. Zeeb 
30972ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3098d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3099d9f59799SBjoern A. Zeeb 
310067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3101d9f59799SBjoern A. Zeeb 
3102d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3103d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3104d9f59799SBjoern A. Zeeb 
3105d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3106d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3107d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3108d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3109d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3110ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3111d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3112d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3113d9f59799SBjoern A. Zeeb 	}
3114d9f59799SBjoern A. Zeeb 
3115cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3116d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3117d9f59799SBjoern A. Zeeb 
3118d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3119d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3120018d93ecSBjoern A. Zeeb 	if (error != 0) {
3121018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3122018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3123d9f59799SBjoern A. Zeeb 		goto outni;
3124018d93ecSBjoern A. Zeeb 	}
3125d9f59799SBjoern A. Zeeb 
3126d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
312788665349SBjoern A. Zeeb 
312888665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
312988665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
313088665349SBjoern A. Zeeb 
3131cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3132d9f59799SBjoern A. Zeeb 
313367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3134d9f59799SBjoern A. Zeeb 
3135d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
313688665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3137d9f59799SBjoern A. Zeeb 
3138d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3139d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3140d9f59799SBjoern A. Zeeb 
3141d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3142d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3143d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3144d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3145ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3146d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3147d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3148d9f59799SBjoern A. Zeeb 	}
3149d9f59799SBjoern A. Zeeb 
3150d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3151d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3152d9f59799SBjoern A. Zeeb 
3153d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3154d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3155d9f59799SBjoern A. Zeeb 
3156d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3157d9f59799SBjoern A. Zeeb 
3158d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3159d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3160d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3161d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3162e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3163018d93ecSBjoern A. Zeeb 	if (error != 0) {
3164018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3165018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3166d9f59799SBjoern A. Zeeb 		goto out;
3167018d93ecSBjoern A. Zeeb 	}
3168d9f59799SBjoern A. Zeeb 
316967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3170d9f59799SBjoern A. Zeeb 
317111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
317211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
317311db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
317411db70b6SBjoern A. Zeeb 		if (error != 0) {
317511db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
317611db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
317711db70b6SBjoern A. Zeeb 			/*
317811db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
317911db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
318011db70b6SBjoern A. Zeeb 			 * less appropriate time).
318111db70b6SBjoern A. Zeeb 			 */
318211db70b6SBjoern A. Zeeb 			/* goto out; */
318311db70b6SBjoern A. Zeeb 		}
318411db70b6SBjoern A. Zeeb 	}
318511db70b6SBjoern A. Zeeb #endif
318611db70b6SBjoern A. Zeeb 
3187d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3188d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3189d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3190d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3191e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3192018d93ecSBjoern A. Zeeb 	if (error != 0) {
3193018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3194018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3195d9f59799SBjoern A. Zeeb 		goto out;
3196018d93ecSBjoern A. Zeeb 	}
3197d9f59799SBjoern A. Zeeb 
319867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3199d9f59799SBjoern A. Zeeb 
3200d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3201d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3202d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3203d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3204e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3205018d93ecSBjoern A. Zeeb 	if (error != 0) {
3206018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3207018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3208d9f59799SBjoern A. Zeeb 		goto out;
3209018d93ecSBjoern A. Zeeb 	}
3210d9f59799SBjoern A. Zeeb 
32115a4d2461SBjoern A. Zeeb 	bss_changed = 0;
321216e688b2SBjoern A. Zeeb 	/*
32135a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
32145a4d2461SBjoern A. Zeeb 	 *
321516e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
32165a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
32175a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
32185a4d2461SBjoern A. Zeeb 	 *
32195a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
32205a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
32215a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
32225a4d2461SBjoern A. Zeeb 	 * a fw assert.
32235a4d2461SBjoern A. Zeeb 	 *
32245a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
32255a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
32265a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
32275a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
32285a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
32295a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
32305a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
32315a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
32325a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
32335a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
323416e688b2SBjoern A. Zeeb 	 */
32355a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
32365a4d2461SBjoern A. Zeeb 
32375a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
323816e688b2SBjoern A. Zeeb 
3239d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3240d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3241d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3242d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3243e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3244d9f59799SBjoern A. Zeeb 	if (error != 0) {
3245d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3246018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3247018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3248d9f59799SBjoern A. Zeeb 		goto out;
3249d9f59799SBjoern A. Zeeb 	}
3250d9f59799SBjoern A. Zeeb 
32515a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
32525a4d2461SBjoern A. Zeeb 
325316e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3254d9f59799SBjoern A. Zeeb 
3255d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3256d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3257d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3258616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3259616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3260d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
32615a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
32625a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
32635a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3264d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3265d9f59799SBjoern A. Zeeb 
32662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32672ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
32682ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
32692ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32702ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32710936c648SBjoern A. Zeeb 	/*
32720936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
32730936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
32740936c648SBjoern A. Zeeb 	 * and potentially freed.
32750936c648SBjoern A. Zeeb 	 */
32760936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3277d9f59799SBjoern A. Zeeb 
3278d9f59799SBjoern A. Zeeb 	/* conf_tx */
3279d9f59799SBjoern A. Zeeb 
328050d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3281d9f59799SBjoern A. Zeeb 
3282d9f59799SBjoern A. Zeeb 	error = EALREADY;
3283d9f59799SBjoern A. Zeeb out:
3284cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3285d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3286d9f59799SBjoern A. Zeeb outni:
3287d9f59799SBjoern A. Zeeb 	return (error);
3288d9f59799SBjoern A. Zeeb }
3289d9f59799SBjoern A. Zeeb 
3290d9f59799SBjoern A. Zeeb static int
3291d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3292d9f59799SBjoern A. Zeeb {
3293d9f59799SBjoern A. Zeeb 
3294d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3295d9f59799SBjoern A. Zeeb }
3296d9f59799SBjoern A. Zeeb 
3297d9f59799SBjoern A. Zeeb static int
32986b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
32996b4cac81SBjoern A. Zeeb {
33006b4cac81SBjoern A. Zeeb 	int error;
33016b4cac81SBjoern A. Zeeb 
3302d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3303d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3304d9f59799SBjoern A. Zeeb 		return (error);
3305d9f59799SBjoern A. Zeeb 
3306d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3307d9f59799SBjoern A. Zeeb 
3308d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
33096b4cac81SBjoern A. Zeeb 	return (error);
33106b4cac81SBjoern A. Zeeb }
33116b4cac81SBjoern A. Zeeb 
3312d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
33136b4cac81SBjoern A. Zeeb 
33146b4cac81SBjoern A. Zeeb /*
33156b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
33166b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
33176b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
33186b4cac81SBjoern A. Zeeb  */
33196b4cac81SBjoern A. Zeeb struct fsm_state {
33206b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
33216b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
33226b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
33236b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
33246b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
33256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
33266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
33276b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3328d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3329d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
33306b4cac81SBjoern A. Zeeb 
33316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
33326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
33336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
33346b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3335d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
33366b4cac81SBjoern A. Zeeb 
3337d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3338d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3339d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3340d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3341d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
33426b4cac81SBjoern A. Zeeb 
3343d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3344d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3345d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
33466b4cac81SBjoern A. Zeeb 
33476b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
33486b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
33496b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
33506b4cac81SBjoern A. Zeeb 
33516b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
33526b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
33536b4cac81SBjoern A. Zeeb };
33546b4cac81SBjoern A. Zeeb 
33556b4cac81SBjoern A. Zeeb static int
33566b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
33576b4cac81SBjoern A. Zeeb {
33586b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33596b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33606b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33616b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33626b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
33636b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
33646b4cac81SBjoern A. Zeeb 	int error;
33656b4cac81SBjoern A. Zeeb 
33666b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
33676b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
33686b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
33696b4cac81SBjoern A. Zeeb 
33709d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33719d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
33726b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
33736b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
33749d9ba2b7SBjoern A. Zeeb #endif
33756b4cac81SBjoern A. Zeeb 
33766b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
33776b4cac81SBjoern A. Zeeb 
33786b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
33796b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33806b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
33816b4cac81SBjoern A. Zeeb 
33826b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
33836b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
33846b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
33856b4cac81SBjoern A. Zeeb 
33866b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
33876b4cac81SBjoern A. Zeeb 
33886b4cac81SBjoern A. Zeeb 	} else {
33896b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
33906b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
33916b4cac81SBjoern A. Zeeb 		return (ENOSYS);
33926b4cac81SBjoern A. Zeeb 	}
33936b4cac81SBjoern A. Zeeb 
33946b4cac81SBjoern A. Zeeb 	error = 0;
33956b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
33966b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
33979d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33989d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3399d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3400d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3401d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3402d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
34039d9ba2b7SBjoern A. Zeeb #endif
34046b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
34056b4cac81SBjoern A. Zeeb 			break;
34066b4cac81SBjoern A. Zeeb 		}
34076b4cac81SBjoern A. Zeeb 	}
34086b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
34096b4cac81SBjoern A. Zeeb 
34106b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3411d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
34126b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
34136b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
34146b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
34156b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
34166b4cac81SBjoern A. Zeeb 		return (ENOSYS);
34176b4cac81SBjoern A. Zeeb 	}
34186b4cac81SBjoern A. Zeeb 
34196b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
34209d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34219d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3422d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3423d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3424d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3425d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
34269d9ba2b7SBjoern A. Zeeb #endif
34276b4cac81SBjoern A. Zeeb 		return (0);
34286b4cac81SBjoern A. Zeeb 	}
34296b4cac81SBjoern A. Zeeb 
34306b4cac81SBjoern A. Zeeb 	if (error != 0) {
34316b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
34326b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
34336b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
34346b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
343545c27ad5SBjoern A. Zeeb 		return (error);
34366b4cac81SBjoern A. Zeeb 	}
34376b4cac81SBjoern A. Zeeb 
34389d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34399d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3440d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3441d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
34426b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
34439d9ba2b7SBjoern A. Zeeb #endif
34446b4cac81SBjoern A. Zeeb 
34456b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
34466b4cac81SBjoern A. Zeeb }
34476b4cac81SBjoern A. Zeeb 
34486b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
34496b4cac81SBjoern A. Zeeb 
3450d9f59799SBjoern A. Zeeb /*
3451d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3452d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3453d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3454d9f59799SBjoern A. Zeeb  */
3455d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3456d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3457d9f59799SBjoern A. Zeeb {
3458d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34592ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3460d9f59799SBjoern A. Zeeb 
34612ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3462d9f59799SBjoern A. Zeeb 
3463ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
34642ac8a218SBjoern A. Zeeb 
34652ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
34662ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34672ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
34682ac8a218SBjoern A. Zeeb 
34692ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
34702ac8a218SBjoern A. Zeeb 	return (rni);
3471d9f59799SBjoern A. Zeeb }
3472d9f59799SBjoern A. Zeeb 
34734a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
34746b4cac81SBjoern A. Zeeb static int
34754a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
34766b4cac81SBjoern A. Zeeb {
34774a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
34786b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34796b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34806b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34816b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
34826b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
34836b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
34846b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
34856b4cac81SBjoern A. Zeeb 	int error;
34866b4cac81SBjoern A. Zeeb 	uint16_t ac;
34876b4cac81SBjoern A. Zeeb 
3488cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3489cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
3490cd0fcf9fSBjoern A. Zeeb 
34916b4cac81SBjoern A. Zeeb 	IMPROVE();
34926b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
34936b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
34946b4cac81SBjoern A. Zeeb 
34956b4cac81SBjoern A. Zeeb 	if (vap == NULL)
34966b4cac81SBjoern A. Zeeb 		return (0);
34976b4cac81SBjoern A. Zeeb 
34984a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
34994a67f1dfSBjoern A. Zeeb 		return (0);
35004a67f1dfSBjoern A. Zeeb 
35016b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
35026b4cac81SBjoern A. Zeeb 		return (0);
35036b4cac81SBjoern A. Zeeb 
35044a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
35054a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
35064a67f1dfSBjoern A. Zeeb 		return (0);
35074a67f1dfSBjoern A. Zeeb 	}
35084a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
35096b4cac81SBjoern A. Zeeb 
35104a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
35116b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
35126b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
35136b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
35146b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
35156b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
35166b4cac81SBjoern A. Zeeb 
35174a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35184a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35194a67f1dfSBjoern A. Zeeb 
35206b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
35216b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
35226b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
35236b4cac81SBjoern A. Zeeb 
35246b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
35256b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
35266b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
35276b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
35286b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
35296b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
353068541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
35316b4cac81SBjoern A. Zeeb 		if (error != 0)
35323f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
35336b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
35346b4cac81SBjoern A. Zeeb 	}
35356b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
35364a67f1dfSBjoern A. Zeeb 	if (!planned)
35376b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35384a67f1dfSBjoern A. Zeeb 
35394a67f1dfSBjoern A. Zeeb 	return (changed);
35404a67f1dfSBjoern A. Zeeb }
35416b4cac81SBjoern A. Zeeb #endif
35426b4cac81SBjoern A. Zeeb 
35434a67f1dfSBjoern A. Zeeb static int
35444a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
35454a67f1dfSBjoern A. Zeeb {
35464a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
35474a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
35484a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
3549cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
35504a67f1dfSBjoern A. Zeeb 
35514a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
35524a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
35534a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
35546b4cac81SBjoern A. Zeeb 		return (0);
35554a67f1dfSBjoern A. Zeeb 
35564a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
3557cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35584a67f1dfSBjoern A. Zeeb 
3559cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
35604a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
3561cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
35624a67f1dfSBjoern A. Zeeb #endif
35634a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
35646b4cac81SBjoern A. Zeeb }
35656b4cac81SBjoern A. Zeeb 
35664aff4048SBjoern A. Zeeb /*
35674aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
35684aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
35694aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
35704aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
35714aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
35724aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3573edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3574edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3575edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3576edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
35774aff4048SBjoern A. Zeeb  */
35784aff4048SBjoern A. Zeeb static void
35794aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
35804aff4048SBjoern A. Zeeb {
35814aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
35824aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35834aff4048SBjoern A. Zeeb 
35844aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3585edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3586edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3587edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
35884aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
35894aff4048SBjoern A. Zeeb 		return;
35904aff4048SBjoern A. Zeeb 	}
35914aff4048SBjoern A. Zeeb 
35924aff4048SBjoern A. Zeeb 	vif = arg;
359357609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
35944aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
35954aff4048SBjoern A. Zeeb }
35964aff4048SBjoern A. Zeeb 
35976b4cac81SBjoern A. Zeeb static struct ieee80211vap *
35986b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
35996b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
36006b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
36016b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
36026b4cac81SBjoern A. Zeeb {
36036b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36046b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36056b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
36066b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
36076b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3608a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
36096b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
361040839418SBjoern A. Zeeb 	struct sysctl_oid *node;
36116b4cac81SBjoern A. Zeeb 	size_t len;
3612e3a0b120SBjoern A. Zeeb 	int error, i;
3613a6042e17SBjoern A. Zeeb 	uint16_t ac;
36146b4cac81SBjoern A. Zeeb 
36156b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
36166b4cac81SBjoern A. Zeeb 		return (NULL);
36176b4cac81SBjoern A. Zeeb 
36186b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36196b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36206b4cac81SBjoern A. Zeeb 
36216b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
36226b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
36236b4cac81SBjoern A. Zeeb 
36246b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
36256b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3626a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
36272ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3628b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
36292ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
36306b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
36316b4cac81SBjoern A. Zeeb 
36326b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
36336b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
36346b4cac81SBjoern A. Zeeb 	vif->p2p = false;
36356b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
36366b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
36376b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
36386b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
36396b4cac81SBjoern A. Zeeb 	IMPROVE();
36406b4cac81SBjoern A. Zeeb 
36416b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
36426b4cac81SBjoern A. Zeeb #if 1
3643560708cbSBjoern A. Zeeb 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3644adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
36456ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
36466ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
36474aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
36484aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
36496ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
36507b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
36516b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
36526b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
36536b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
36546b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
36556b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3656616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3657616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3658616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3659616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
36606ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3661caaa79c3SBjoern A. Zeeb 	/*
3662caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3663caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3664caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3665caaa79c3SBjoern A. Zeeb 	 */
36666ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
36676b4cac81SBjoern A. Zeeb #endif
36686b4cac81SBjoern A. Zeeb #if 0
36696b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
36706b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
36716b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
36726b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
36736b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
36746b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
36756b4cac81SBjoern A. Zeeb #endif
3676e3a0b120SBjoern A. Zeeb 
36776ffb7bd4SBjoern A. Zeeb 	/* Link Config */
36786ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
36796ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
36806ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
36816ffb7bd4SBjoern A. Zeeb 	}
36826ffb7bd4SBjoern A. Zeeb 
3683e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3684e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3685e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3686e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3687e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3688e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3689e3a0b120SBjoern A. Zeeb 		else
3690e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
36910cbcfa19SBjoern A. Zeeb 
36920cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
36930cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3694e3a0b120SBjoern A. Zeeb 	}
3695e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3696e3a0b120SBjoern A. Zeeb 
36976b4cac81SBjoern A. Zeeb 	IMPROVE();
36986b4cac81SBjoern A. Zeeb 
36996b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
37006b4cac81SBjoern A. Zeeb 	if (error != 0) {
37013f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
37026b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37036b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37046b4cac81SBjoern A. Zeeb 		return (NULL);
37056b4cac81SBjoern A. Zeeb 	}
37066b4cac81SBjoern A. Zeeb 
37076b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
37086b4cac81SBjoern A. Zeeb 	if (error != 0) {
37096b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
37103f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
37116b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
37126b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
37136b4cac81SBjoern A. Zeeb 		return (NULL);
37146b4cac81SBjoern A. Zeeb 	}
37156b4cac81SBjoern A. Zeeb 
37168891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
37176b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
37188891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
37196b4cac81SBjoern A. Zeeb 
37206b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
37216b4cac81SBjoern A. Zeeb 	changed = 0;
37226b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
37236b4cac81SBjoern A. Zeeb 
3724a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3725a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3726cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3727a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3728a6042e17SBjoern A. Zeeb 
3729a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3730a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3731a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3732a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3733a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3734a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3735a6042e17SBjoern A. Zeeb 		if (error != 0)
3736a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3737a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3738a6042e17SBjoern A. Zeeb 	}
3739cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3740a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3741a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
37426b4cac81SBjoern A. Zeeb 
37436b4cac81SBjoern A. Zeeb 	/* Force MC init. */
37446b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
37456b4cac81SBjoern A. Zeeb 
37466b4cac81SBjoern A. Zeeb 	IMPROVE();
37476b4cac81SBjoern A. Zeeb 
37486b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
37496b4cac81SBjoern A. Zeeb 
37506b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
37516b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
37526b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3753d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3754d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
37556b4cac81SBjoern A. Zeeb 
3756b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
375711db70b6SBjoern A. Zeeb 	/* Key management. */
375811db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
37596b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
37606b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3761b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3762b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
37636b4cac81SBjoern A. Zeeb 	}
376411db70b6SBjoern A. Zeeb #endif
37656b4cac81SBjoern A. Zeeb 
37669fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
37679fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
37689fb91463SBjoern A. Zeeb #endif
37699fb91463SBjoern A. Zeeb 
37706b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
37716b4cac81SBjoern A. Zeeb 
37726b4cac81SBjoern A. Zeeb 	/* Complete setup. */
37736b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
37746b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
37756b4cac81SBjoern A. Zeeb 
377611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
377711db70b6SBjoern A. Zeeb 	/*
377811db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
377911db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
378011db70b6SBjoern A. Zeeb 	 */
378111db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
378211db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
378311db70b6SBjoern A. Zeeb #endif
3784ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3785ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3786ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3787ac2c7271SBjoern A. Zeeb #endif
378811db70b6SBjoern A. Zeeb 
37896b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
37906b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
37916b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
37926b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
37936b4cac81SBjoern A. Zeeb 
37946b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
37956b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
37966b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
37976b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
37986b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
37996b4cac81SBjoern A. Zeeb 	/* any others? */
380040839418SBjoern A. Zeeb 
380140839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
380240839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
380340839418SBjoern A. Zeeb 
380440839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
380540839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
380640839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
380740839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
380840839418SBjoern A. Zeeb 
380940839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
381040839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
381140839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
381240839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
381340839418SBjoern A. Zeeb 
38146b4cac81SBjoern A. Zeeb 	IMPROVE();
38156b4cac81SBjoern A. Zeeb 
38166b4cac81SBjoern A. Zeeb 	return (vap);
38176b4cac81SBjoern A. Zeeb }
38186b4cac81SBjoern A. Zeeb 
38194b0af114SBjoern A. Zeeb void
38204b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
38214b0af114SBjoern A. Zeeb {
38224b0af114SBjoern A. Zeeb 
38234b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
38244b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
38254b0af114SBjoern A. Zeeb 
38264b0af114SBjoern A. Zeeb 	IMPROVE();
38274b0af114SBjoern A. Zeeb }
38284b0af114SBjoern A. Zeeb 
38294b0af114SBjoern A. Zeeb void
38304b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
38314b0af114SBjoern A. Zeeb {
38324b0af114SBjoern A. Zeeb 
38334b0af114SBjoern A. Zeeb 	TODO();
38344b0af114SBjoern A. Zeeb }
38354b0af114SBjoern A. Zeeb 
38366b4cac81SBjoern A. Zeeb static void
38376b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
38386b4cac81SBjoern A. Zeeb {
38396b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38446b4cac81SBjoern A. Zeeb 
38456b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
38466b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
38476b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
38486b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38496b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38506b4cac81SBjoern A. Zeeb 
38514aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
38524aff4048SBjoern A. Zeeb 
385340839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
385440839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
385540839418SBjoern A. Zeeb 
38568891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
38576b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
38588891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
38596b4cac81SBjoern A. Zeeb 
38606b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
38616b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3862dbf76919SBjoern A. Zeeb 
3863dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3864dbf76919SBjoern A. Zeeb 
3865dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3866dbf76919SBjoern A. Zeeb 
38676c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
38687b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
38696c38c6b1SBjoern A. Zeeb 
38706b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
38716b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
38726b4cac81SBjoern A. Zeeb }
38736b4cac81SBjoern A. Zeeb 
38746b4cac81SBjoern A. Zeeb static void
38756b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
38766b4cac81SBjoern A. Zeeb {
38776b4cac81SBjoern A. Zeeb 
38786b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
38796b4cac81SBjoern A. Zeeb 	TRACEOK();
38806b4cac81SBjoern A. Zeeb }
38816b4cac81SBjoern A. Zeeb 
38826b4cac81SBjoern A. Zeeb static void
38836b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
38846b4cac81SBjoern A. Zeeb {
38856b4cac81SBjoern A. Zeeb 
38866b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38876b4cac81SBjoern A. Zeeb }
38886b4cac81SBjoern A. Zeeb 
38896b4cac81SBjoern A. Zeeb static void
38906b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
38916b4cac81SBjoern A. Zeeb {
38926b4cac81SBjoern A. Zeeb 
38936b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38946b4cac81SBjoern A. Zeeb }
38956b4cac81SBjoern A. Zeeb 
38966b4cac81SBjoern A. Zeeb /* Start / stop device. */
38976b4cac81SBjoern A. Zeeb static void
38986b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
38996b4cac81SBjoern A. Zeeb {
39006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
39016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3902cd0fcf9fSBjoern A. Zeeb #ifdef HW_START_STOP
39036b4cac81SBjoern A. Zeeb 	int error;
39048d58a057SBjoern A. Zeeb #endif
39056b4cac81SBjoern A. Zeeb 	bool start_all;
39066b4cac81SBjoern A. Zeeb 
39076b4cac81SBjoern A. Zeeb 	IMPROVE();
39086b4cac81SBjoern A. Zeeb 
39096b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
39106b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
39116b4cac81SBjoern A. Zeeb 	start_all = false;
39126b4cac81SBjoern A. Zeeb 
39138ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
3914cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
39156b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
39168d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
39176b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
39186b4cac81SBjoern A. Zeeb 		if (error == 0)
39198d58a057SBjoern A. Zeeb #endif
39206b4cac81SBjoern A. Zeeb 			start_all = true;
39216b4cac81SBjoern A. Zeeb 	} else {
39228d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
39237b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
39248d58a057SBjoern A. Zeeb #endif
39256b4cac81SBjoern A. Zeeb 	}
3926cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
39278ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
39286b4cac81SBjoern A. Zeeb 
39296b4cac81SBjoern A. Zeeb 	if (start_all)
39306b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
39316b4cac81SBjoern A. Zeeb }
39326b4cac81SBjoern A. Zeeb 
39336b4cac81SBjoern A. Zeeb bool
39346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
39356b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
39366b4cac81SBjoern A. Zeeb {
39376b4cac81SBjoern A. Zeeb 	int i;
39386b4cac81SBjoern A. Zeeb 
39396b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
39406b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
39416b4cac81SBjoern A. Zeeb 			return (true);
39426b4cac81SBjoern A. Zeeb 	}
39436b4cac81SBjoern A. Zeeb 
39446b4cac81SBjoern A. Zeeb 	return (false);
39456b4cac81SBjoern A. Zeeb }
39466b4cac81SBjoern A. Zeeb 
39476b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
39486b4cac81SBjoern A. Zeeb bool
39496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
39506b4cac81SBjoern A. Zeeb {
39516b4cac81SBjoern A. Zeeb 	size_t x;
39526b4cac81SBjoern A. Zeeb 	uint8_t l;
39536b4cac81SBjoern A. Zeeb 
39546b4cac81SBjoern A. Zeeb 	x = *xp;
39556b4cac81SBjoern A. Zeeb 
39566b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
39576b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
39586b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
39596b4cac81SBjoern A. Zeeb 	x += 2 + l;
39606b4cac81SBjoern A. Zeeb 
39616b4cac81SBjoern A. Zeeb 	if (x > ies_len)
39626b4cac81SBjoern A. Zeeb 		return (false);
39636b4cac81SBjoern A. Zeeb 
39646b4cac81SBjoern A. Zeeb 	*xp = x;
39656b4cac81SBjoern A. Zeeb 	return (true);
39666b4cac81SBjoern A. Zeeb }
39676b4cac81SBjoern A. Zeeb 
3968d9945d78SBjoern A. Zeeb static uint8_t *
3969d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3970d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
39716b4cac81SBjoern A. Zeeb {
3972d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3973d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
39743dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3975d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3976d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3977d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3978d9945d78SBjoern A. Zeeb 	int band, i;
39796b4cac81SBjoern A. Zeeb 
39803dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3981d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3982d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3983d9945d78SBjoern A. Zeeb 			continue;
3984d9945d78SBjoern A. Zeeb 
3985d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3986d9945d78SBjoern A. Zeeb 		/*
3987d9945d78SBjoern A. Zeeb 		 * This should not happen;
3988d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3989d9945d78SBjoern A. Zeeb 		 */
3990d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3991d9945d78SBjoern A. Zeeb 			continue;
3992d9945d78SBjoern A. Zeeb 
3993d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3994d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3995d9945d78SBjoern A. Zeeb 		chan = NULL;
3996d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3997d9945d78SBjoern A. Zeeb 
3998d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3999d9945d78SBjoern A. Zeeb 				continue;
4000d9945d78SBjoern A. Zeeb 
40013dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
4002d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
4003d9945d78SBjoern A. Zeeb 			if (chan != NULL)
4004d9945d78SBjoern A. Zeeb 				break;
4005d9945d78SBjoern A. Zeeb 		}
4006d9945d78SBjoern A. Zeeb 
4007d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
4008d9945d78SBjoern A. Zeeb 		if (chan == NULL)
4009d9945d78SBjoern A. Zeeb 			continue;
4010d9945d78SBjoern A. Zeeb 
4011d9945d78SBjoern A. Zeeb 		pb = p;
40123dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
4013d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
4014d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
4015d9945d78SBjoern A. Zeeb 
40163dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
40173dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
40183dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
40193dd98026SBjoern A. Zeeb 
40203dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
40213dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
40223dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
40233dd98026SBjoern A. Zeeb 		}
40243dd98026SBjoern A. Zeeb #endif
40253dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
40265393cd34SBjoern A. Zeeb 		if (band == NL80211_BAND_5GHZ &&
40275393cd34SBjoern A. Zeeb 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
40283dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
40293dd98026SBjoern A. Zeeb 
40303dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
40313dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
40323dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
40333dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
40343dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
40353dd98026SBjoern A. Zeeb 		}
40363dd98026SBjoern A. Zeeb #endif
40373dd98026SBjoern A. Zeeb 
4038d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
4039d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
4040d9945d78SBjoern A. Zeeb 	}
4041d9945d78SBjoern A. Zeeb 
4042d9945d78SBjoern A. Zeeb 	/* Add common_ies */
4043d9945d78SBjoern A. Zeeb 	pb = p;
4044d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4045d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
4046d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4047d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
4048d9945d78SBjoern A. Zeeb 	}
4049d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
4050d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
4051d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
4052d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
4053d9945d78SBjoern A. Zeeb 	}
4054d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
4055d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
4056d9945d78SBjoern A. Zeeb 
4057d9945d78SBjoern A. Zeeb 	return (p);
40586b4cac81SBjoern A. Zeeb }
40596b4cac81SBjoern A. Zeeb 
40606b4cac81SBjoern A. Zeeb static void
40616b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
40626b4cac81SBjoern A. Zeeb {
40636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
40666b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
40676b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
40686b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
40696b4cac81SBjoern A. Zeeb 	int error;
40708ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40716b4cac81SBjoern A. Zeeb 
40726b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40738ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4074a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40756b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
40768ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40776b4cac81SBjoern A. Zeeb 		return;
40786b4cac81SBjoern A. Zeeb 	}
40798ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40816b4cac81SBjoern A. Zeeb 
40826b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
40836b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
40846b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
4085d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
40866b4cac81SBjoern A. Zeeb 		return;
40876b4cac81SBjoern A. Zeeb 	}
40886b4cac81SBjoern A. Zeeb 
40896b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40908ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4091a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4092a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4093d3ef7fb4SBjoern A. Zeeb sw_scan:
40946b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40956b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4096086be6a8SBjoern A. Zeeb 
4097086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4098086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
4099086be6a8SBjoern A. Zeeb 
41006b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
41016b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
41026b4cac81SBjoern A. Zeeb 		IMPROVE();
41036b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
41046b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
41056b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
41066b4cac81SBjoern A. Zeeb 
41076b4cac81SBjoern A. Zeeb 	} else {
41086b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
41096b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
41106b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
41116b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
41126b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4113d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
4114d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
4115d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
41163206587aSBjoern A. Zeeb 		bool running;
4117d9945d78SBjoern A. Zeeb 
4118d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4119d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
41206b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
41216b4cac81SBjoern A. Zeeb 
4122d9945d78SBjoern A. Zeeb 		band_mask = 0;
41236b4cac81SBjoern A. Zeeb 		nchan = 0;
4124c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4125c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4126d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
41276b4cac81SBjoern A. Zeeb 				nchan++;
4128d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
4129d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
4130d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
4131d9945d78SBjoern A. Zeeb 			}
4132c272abc5SBjoern A. Zeeb #else
4133c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
4134c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4135c272abc5SBjoern A. Zeeb 				switch (band) {
4136c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
4137c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
4138c272abc5SBjoern A. Zeeb 					break;
4139c272abc5SBjoern A. Zeeb 				default:
4140c272abc5SBjoern A. Zeeb 					continue;
4141c272abc5SBjoern A. Zeeb 				}
4142c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
4143c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
4144c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
4145c272abc5SBjoern A. Zeeb 				}
4146c272abc5SBjoern A. Zeeb 			}
4147c272abc5SBjoern A. Zeeb #endif
4148c272abc5SBjoern A. Zeeb 		} else {
41493206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
41503206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
41513206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
41523206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
41533206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
41543206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
41553206587aSBjoern A. Zeeb 			 */
41563206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
41573206587aSBjoern A. Zeeb 		}
41583206587aSBjoern A. Zeeb 
41596b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
41606b4cac81SBjoern A. Zeeb 
4161d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4162d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4163d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4164d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4165d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4166d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4167d9945d78SBjoern A. Zeeb 
4168d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4169d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4170d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4171d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4172d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4173d9945d78SBjoern A. Zeeb 		}
4174d9945d78SBjoern A. Zeeb 
41753206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4176d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4177d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
41786b4cac81SBjoern A. Zeeb 
41796b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
41806b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
41816b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
41826b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
41836b4cac81SBjoern A. Zeeb #if 0
41846b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
41856b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
41866b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
41876b4cac81SBjoern A. Zeeb #endif
41886b4cac81SBjoern A. Zeeb #ifdef __notyet__
41896b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
41906b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
41916b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
41926b4cac81SBjoern A. Zeeb #endif
4193e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
41946b4cac81SBjoern A. Zeeb 
41956b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
41966b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
41976b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
41986b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
41996b4cac81SBjoern A. Zeeb 			*(cpp + i) =
42006b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
42016b4cac81SBjoern A. Zeeb 		}
4202c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
42036b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4204c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
42056b4cac81SBjoern A. Zeeb 
4206c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
42076b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
42083206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
42096b4cac81SBjoern A. Zeeb 			/* lc->flags */
42106b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
42116b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
42126b4cac81SBjoern A. Zeeb 			/* lc-> ... */
42136b4cac81SBjoern A. Zeeb 			lc++;
42146b4cac81SBjoern A. Zeeb 		}
4215c272abc5SBjoern A. Zeeb #else
4216c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4217c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4218c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4219c272abc5SBjoern A. Zeeb 
4220c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4221c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4222c272abc5SBjoern A. Zeeb 				continue;
4223c272abc5SBjoern A. Zeeb 
4224c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4225c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4226c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4227c272abc5SBjoern A. Zeeb 				continue;
4228c272abc5SBjoern A. Zeeb 
4229c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4230c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4231c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4232c272abc5SBjoern A. Zeeb 				lc++;
4233c272abc5SBjoern A. Zeeb 			}
4234c272abc5SBjoern A. Zeeb 		}
4235c272abc5SBjoern A. Zeeb #endif
42366b4cac81SBjoern A. Zeeb 
4237d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
42386b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
42396b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
42406b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4241d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
42426b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
42436b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
42446b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
42456b4cac81SBjoern A. Zeeb 				ssids++;
42466b4cac81SBjoern A. Zeeb 			}
42476b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
42486b4cac81SBjoern A. Zeeb 		} else {
42496b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
42506b4cac81SBjoern A. Zeeb 		}
42516b4cac81SBjoern A. Zeeb 
42526b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
42536b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
42546b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
42556b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
42566b4cac81SBjoern A. Zeeb 		/* s6gp->... */
42576b4cac81SBjoern A. Zeeb 
4258d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4259d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4260d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4261d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4262d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4263d9945d78SBjoern A. Zeeb 
42646b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
42656b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
42663206587aSBjoern A. Zeeb 
42673206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
42683206587aSBjoern A. Zeeb 		/* Re-check under lock. */
42693206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
42703206587aSBjoern A. Zeeb 		if (!running) {
42713206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
42723206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
42733206587aSBjoern A. Zeeb 
42743206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
42753206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
42763206587aSBjoern A. Zeeb 		}
42773206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42783206587aSBjoern A. Zeeb 		if (running) {
42793206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
42803206587aSBjoern A. Zeeb 			return;
42813206587aSBjoern A. Zeeb 		}
42823206587aSBjoern A. Zeeb 
42836b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
42846b4cac81SBjoern A. Zeeb 		if (error != 0) {
4285d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4286d9945d78SBjoern A. Zeeb 
42873206587aSBjoern A. Zeeb 			/*
42883206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
42893206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
42903206587aSBjoern A. Zeeb 			 * and only there.
42913206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
42923206587aSBjoern A. Zeeb 			 * behave differently:
42933206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
42943206587aSBjoern A. Zeeb 			 *        and can then still return an error.
42953206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
42963206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
42973206587aSBjoern A. Zeeb 			 * ...
42983206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
42993206587aSBjoern A. Zeeb 			 * and balance between both code paths.
43003206587aSBjoern A. Zeeb 			 */
43013206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
43023206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
43033206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
43046b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
43053206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
43063206587aSBjoern A. Zeeb 			}
43073206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4308d3ef7fb4SBjoern A. Zeeb 
4309d3ef7fb4SBjoern A. Zeeb 			/*
4310d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4311d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4312d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4313d3ef7fb4SBjoern A. Zeeb 			 */
4314196cfd0bSBjoern A. Zeeb 			if (error == 1) {
43158ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4316a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
43178ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43183206587aSBjoern A. Zeeb 				/*
43193206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
43203206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
43213206587aSBjoern A. Zeeb 				 * currently not re-enable it?
43223206587aSBjoern A. Zeeb 				 */
43233206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4324196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4325196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4326196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4327196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4328196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4329196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4330196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4331196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4332d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4333196cfd0bSBjoern A. Zeeb 			}
4334d3ef7fb4SBjoern A. Zeeb 
4335d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4336d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
43376b4cac81SBjoern A. Zeeb 		}
43386b4cac81SBjoern A. Zeeb 	}
43396b4cac81SBjoern A. Zeeb }
43406b4cac81SBjoern A. Zeeb 
43416b4cac81SBjoern A. Zeeb static void
43426b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
43436b4cac81SBjoern A. Zeeb {
43446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43458ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
43466b4cac81SBjoern A. Zeeb 
43476b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4349a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
43508ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43516b4cac81SBjoern A. Zeeb 		return;
43526b4cac81SBjoern A. Zeeb 	}
43538ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43556b4cac81SBjoern A. Zeeb 
43568ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4357a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4358a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
43596b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
43606b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
43616b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
43626b4cac81SBjoern A. Zeeb 
4363a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4364a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
43656b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
43666b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
43676b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4368a486fbbdSBjoern A. Zeeb 
43696b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
43706b4cac81SBjoern A. Zeeb 
43716b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4372086be6a8SBjoern A. Zeeb 
4373086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4374086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
43756b4cac81SBjoern A. Zeeb 	}
43766b4cac81SBjoern A. Zeeb }
43776b4cac81SBjoern A. Zeeb 
43786b4cac81SBjoern A. Zeeb static void
4379a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4380a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4381a486fbbdSBjoern A. Zeeb {
4382a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43838ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4384a486fbbdSBjoern A. Zeeb 
4385a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
43868ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43878ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43898ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4390a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4391a486fbbdSBjoern A. Zeeb }
4392a486fbbdSBjoern A. Zeeb 
4393a486fbbdSBjoern A. Zeeb static void
4394a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4395a486fbbdSBjoern A. Zeeb {
4396a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43978ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4398a486fbbdSBjoern A. Zeeb 
4399a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
44008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44018ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
44028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44038ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4404a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4405a486fbbdSBjoern A. Zeeb }
4406a486fbbdSBjoern A. Zeeb 
4407a486fbbdSBjoern A. Zeeb static void
44086b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
44096b4cac81SBjoern A. Zeeb {
44106b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44116b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4412b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4413b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
44146b4cac81SBjoern A. Zeeb 	int error;
44158ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
44166b4cac81SBjoern A. Zeeb 
44176b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4418b2cf3c21SBjoern A. Zeeb 
4419b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4420b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
44216b4cac81SBjoern A. Zeeb 		return;
44226b4cac81SBjoern A. Zeeb 
4423a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
44248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
44258ac540d3SBjoern A. Zeeb 	hw_scan_running =
44268ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
44278ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
44288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
44298ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4430a486fbbdSBjoern A. Zeeb 		return;
4431a486fbbdSBjoern A. Zeeb 
4432b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4433b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
44346b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
44356b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
44366b4cac81SBjoern A. Zeeb 		return;
44376b4cac81SBjoern A. Zeeb 	}
44386b4cac81SBjoern A. Zeeb 
44396b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
44406b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
44416b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
44426b4cac81SBjoern A. Zeeb 		    c, chan);
44436b4cac81SBjoern A. Zeeb 		return;
44446b4cac81SBjoern A. Zeeb 	}
44456b4cac81SBjoern A. Zeeb 
44466b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
44476b4cac81SBjoern A. Zeeb 	IMPROVE();
44486b4cac81SBjoern A. Zeeb 
44496b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44504a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
44519fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
445211450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
44539fb91463SBjoern A. Zeeb #endif
44544a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
44556b4cac81SBjoern A. Zeeb 
44566b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
44576b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
44586b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
44596b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
44606b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
44616b4cac81SBjoern A. Zeeb 		IMPROVE();
44626b4cac81SBjoern A. Zeeb 	} else {
44636b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
44646b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
44656b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
44666b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
44676b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
44686b4cac81SBjoern A. Zeeb 	}
44696b4cac81SBjoern A. Zeeb 
44706b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
44716b4cac81SBjoern A. Zeeb 	IMPROVE();
44726b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
44736b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
44746b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
44756b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
44766b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
44776b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
44786b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
44796b4cac81SBjoern A. Zeeb 			    error);
44806b4cac81SBjoern A. Zeeb 	}
44816b4cac81SBjoern A. Zeeb }
44826b4cac81SBjoern A. Zeeb 
44836b4cac81SBjoern A. Zeeb static struct ieee80211_node *
44846b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
44856b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
44866b4cac81SBjoern A. Zeeb {
44876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44894f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
44906b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44916b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
44926b4cac81SBjoern A. Zeeb 
44936b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
44946b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44956b4cac81SBjoern A. Zeeb 
44966b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
44974f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
44984f61ef8bSBjoern A. Zeeb 		return (NULL);
44994f61ef8bSBjoern A. Zeeb 
45006b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
45016b4cac81SBjoern A. Zeeb 	if (ni == NULL)
45026b4cac81SBjoern A. Zeeb 		return (NULL);
45036b4cac81SBjoern A. Zeeb 
45046b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
45054f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
45066b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
45076b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
45086b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
45096b4cac81SBjoern A. Zeeb 		return (NULL);
45106b4cac81SBjoern A. Zeeb 	}
45116b4cac81SBjoern A. Zeeb 
45126b4cac81SBjoern A. Zeeb 	return (ni);
45136b4cac81SBjoern A. Zeeb }
45146b4cac81SBjoern A. Zeeb 
45156b4cac81SBjoern A. Zeeb static int
45166b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
45176b4cac81SBjoern A. Zeeb {
45186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45196b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45206b4cac81SBjoern A. Zeeb 	int error;
45216b4cac81SBjoern A. Zeeb 
45226b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45236b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45246b4cac81SBjoern A. Zeeb 
45256b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
45266b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
45276b4cac81SBjoern A. Zeeb 		if (error != 0)
45286b4cac81SBjoern A. Zeeb 			return (error);
45296b4cac81SBjoern A. Zeeb 	}
45306b4cac81SBjoern A. Zeeb 
45316b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
45326b4cac81SBjoern A. Zeeb 	IMPROVE();
45336b4cac81SBjoern A. Zeeb 
45346b4cac81SBjoern A. Zeeb 	return (0);
45356b4cac81SBjoern A. Zeeb }
45366b4cac81SBjoern A. Zeeb 
45376b4cac81SBjoern A. Zeeb static void
45386b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
45396b4cac81SBjoern A. Zeeb {
45406b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45416b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45426b4cac81SBjoern A. Zeeb 
45436b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45446b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45456b4cac81SBjoern A. Zeeb 
45466b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
45476b4cac81SBjoern A. Zeeb 	IMPROVE();
45486b4cac81SBjoern A. Zeeb 
45496b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
45506b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
45516b4cac81SBjoern A. Zeeb }
45526b4cac81SBjoern A. Zeeb 
45536b4cac81SBjoern A. Zeeb static void
45546b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
45556b4cac81SBjoern A. Zeeb {
45566b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45576b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45586b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45596b4cac81SBjoern A. Zeeb 
45606b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45626b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
45636b4cac81SBjoern A. Zeeb 
45640936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
45656b4cac81SBjoern A. Zeeb 
45660936c648SBjoern A. Zeeb 	/*
45670936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
45680936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
45690936c648SBjoern A. Zeeb 	 */
45700936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
45716b4cac81SBjoern A. Zeeb 
45726b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
45736b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
45746b4cac81SBjoern A. Zeeb }
45756b4cac81SBjoern A. Zeeb 
45769a45c3caSBjoern A. Zeeb /*
45779a45c3caSBjoern A. Zeeb  * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
45789a45c3caSBjoern A. Zeeb  * call path.
45799a45c3caSBjoern A. Zeeb  * Unfortunately they have slightly different invariants.  See
45809a45c3caSBjoern A. Zeeb  * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
45819a45c3caSBjoern A. Zeeb  * Both take care of the ni reference in case of error, and otherwise during
45829a45c3caSBjoern A. Zeeb  * the callback after transmit.
45839a45c3caSBjoern A. Zeeb  * The difference is that in case of error (*ic_raw_xmit) needs us to release
45849a45c3caSBjoern A. Zeeb  * the mbuf, while (*ic_transmit) will free the mbuf itself.
45859a45c3caSBjoern A. Zeeb  */
45866b4cac81SBjoern A. Zeeb static int
45879a45c3caSBjoern A. Zeeb lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
45889a45c3caSBjoern A. Zeeb     const struct ieee80211_bpf_params *params __unused,
45899a45c3caSBjoern A. Zeeb     bool freem)
45906b4cac81SBjoern A. Zeeb {
45916b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4592c816f64eSBjoern A. Zeeb 	int error;
45936b4cac81SBjoern A. Zeeb 
45946b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4595fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
45962372f8ccSBjoern A. Zeeb #if 0
459788665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
45982372f8ccSBjoern A. Zeeb #else
45992372f8ccSBjoern A. Zeeb 	/*
46002372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46012372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46022372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46032372f8ccSBjoern A. Zeeb 	 */
46042372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
46052372f8ccSBjoern A. Zeeb #endif
4606fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46079a45c3caSBjoern A. Zeeb 		if (freem)
46080936c648SBjoern A. Zeeb 			m_free(m);
46090936c648SBjoern A. Zeeb 		return (ENETDOWN);
46100936c648SBjoern A. Zeeb 	}
46116b4cac81SBjoern A. Zeeb 
46126b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
4613c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lsta->txq, m);
4614c816f64eSBjoern A. Zeeb 	if (error != 0) {
4615c816f64eSBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46169a45c3caSBjoern A. Zeeb 		if (freem)
4617c816f64eSBjoern A. Zeeb 			m_free(m);
4618c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4619c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4620c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4621c816f64eSBjoern A. Zeeb 			    __func__, error);
4622c816f64eSBjoern A. Zeeb #endif
4623c816f64eSBjoern A. Zeeb 		return (ENETDOWN);
4624c816f64eSBjoern A. Zeeb 	}
4625fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4626fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46276b4cac81SBjoern A. Zeeb 
46289d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46299d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
46306b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
46316b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
46326b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
46339d9ba2b7SBjoern A. Zeeb #endif
46346b4cac81SBjoern A. Zeeb 
46356b4cac81SBjoern A. Zeeb 	return (0);
46366b4cac81SBjoern A. Zeeb }
46376b4cac81SBjoern A. Zeeb 
46389a45c3caSBjoern A. Zeeb static int
46399a45c3caSBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
46409a45c3caSBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
46419a45c3caSBjoern A. Zeeb {
46429a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, true));
46439a45c3caSBjoern A. Zeeb }
46449a45c3caSBjoern A. Zeeb 
464511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
464611db70b6SBjoern A. Zeeb static int
464798e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
464898e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
464911db70b6SBjoern A. Zeeb {
465098e55905SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
465198e55905SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
465298e55905SBjoern A. Zeeb 	uint8_t *p, *m;
465398e55905SBjoern A. Zeeb 
465498e55905SBjoern A. Zeeb 	/*
465598e55905SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
465698e55905SBjoern A. Zeeb 	 * or if we are done?
465798e55905SBjoern A. Zeeb 	 */
465898e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
465998e55905SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
466098e55905SBjoern A. Zeeb 			return (0);
466198e55905SBjoern A. Zeeb 
466298e55905SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
466398e55905SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
466498e55905SBjoern A. Zeeb 		return (ENOSPC);
466598e55905SBjoern A. Zeeb 
466698e55905SBjoern A. Zeeb 	hdr = (void *)skb->data;
466798e55905SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
466898e55905SBjoern A. Zeeb 	p = skb_push(skb, hlen);
466998e55905SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
467098e55905SBjoern A. Zeeb 
467198e55905SBjoern A. Zeeb 	/*
467298e55905SBjoern A. Zeeb 	 * Put in zeroed space for the MMIC if requested.
467398e55905SBjoern A. Zeeb 	 * XXX-BZ in theory this is not the right place but given we
467498e55905SBjoern A. Zeeb 	 * are here we know we do hw_crypto so not much missing.
467598e55905SBjoern A. Zeeb 	 */
467698e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0) {
467798e55905SBjoern A. Zeeb 		m = skb_put(skb, 8);
467898e55905SBjoern A. Zeeb 		memset(m, 0, 8);
467998e55905SBjoern A. Zeeb 	}
468098e55905SBjoern A. Zeeb 
468198e55905SBjoern A. Zeeb 	/* If driver request space only we are done. */
468298e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
468398e55905SBjoern A. Zeeb 		return (0);
468498e55905SBjoern A. Zeeb 
468598e55905SBjoern A. Zeeb 	p += hdrlen;
468698e55905SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
468798e55905SBjoern A. Zeeb 
468898e55905SBjoern A. Zeeb 	return (ENXIO);
468998e55905SBjoern A. Zeeb }
469098e55905SBjoern A. Zeeb static int
469198e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
469298e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
469398e55905SBjoern A. Zeeb {
469411db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
469511db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
469611db70b6SBjoern A. Zeeb 	uint8_t *p;
469711db70b6SBjoern A. Zeeb 
469811db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
469911db70b6SBjoern A. Zeeb 
470011db70b6SBjoern A. Zeeb 	/*
470111db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
470211db70b6SBjoern A. Zeeb 	 * or if we are done?
470311db70b6SBjoern A. Zeeb 	 */
470411db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
470511db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
470611db70b6SBjoern A. Zeeb 	    /* MFP */
470711db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
470811db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
470911db70b6SBjoern A. Zeeb 			return (0);
471011db70b6SBjoern A. Zeeb 
471111db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
471211db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
471311db70b6SBjoern A. Zeeb 		return (ENOSPC);
471411db70b6SBjoern A. Zeeb 
471511db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
471611db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
471711db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
471811db70b6SBjoern A. Zeeb 
471911db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
472011db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
472111db70b6SBjoern A. Zeeb 		return (0);
472211db70b6SBjoern A. Zeeb 
472311db70b6SBjoern A. Zeeb 	p += hdrlen;
472411db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
472511db70b6SBjoern A. Zeeb 
472611db70b6SBjoern A. Zeeb 	return (0);
472711db70b6SBjoern A. Zeeb }
472898e55905SBjoern A. Zeeb 
472998e55905SBjoern A. Zeeb static int
473098e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
473198e55905SBjoern A. Zeeb     struct sk_buff *skb)
473298e55905SBjoern A. Zeeb {
473398e55905SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
473498e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
473598e55905SBjoern A. Zeeb 
473698e55905SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
473798e55905SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
473898e55905SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
473998e55905SBjoern A. Zeeb 
474098e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
474198e55905SBjoern A. Zeeb 
474298e55905SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
474398e55905SBjoern A. Zeeb 	info->control.hw_key = kc;
474498e55905SBjoern A. Zeeb 
474598e55905SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
474698e55905SBjoern A. Zeeb 	if (kc == NULL) {
474798e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
474898e55905SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
474998e55905SBjoern A. Zeeb 		return (ENXIO);
475098e55905SBjoern A. Zeeb 	}
475198e55905SBjoern A. Zeeb 
475298e55905SBjoern A. Zeeb 	switch (kc->cipher) {
475398e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
475498e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
475598e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
475698e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
475798e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
475898e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
475998e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
476098e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
476198e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
476298e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
476398e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
476498e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
476598e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
476698e55905SBjoern A. Zeeb 	default:
476798e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
476898e55905SBjoern A. Zeeb 		    "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
476998e55905SBjoern A. Zeeb 		    skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
477098e55905SBjoern A. Zeeb 		return (EOPNOTSUPP);
477198e55905SBjoern A. Zeeb 	}
477298e55905SBjoern A. Zeeb }
477398e55905SBjoern A. Zeeb 
477498e55905SBjoern A. Zeeb static uint8_t
477598e55905SBjoern A. Zeeb lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
477698e55905SBjoern A. Zeeb {
477798e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
477898e55905SBjoern A. Zeeb 
477998e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
478098e55905SBjoern A. Zeeb 	if (kc == NULL)
478198e55905SBjoern A. Zeeb 		return (0);
478298e55905SBjoern A. Zeeb 
478398e55905SBjoern A. Zeeb 	IMPROVE("which other flags need tailroom?");
478498e55905SBjoern A. Zeeb 	if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
478598e55905SBjoern A. Zeeb 		return (32);	/* Large enough to hold everything and pow2. */
478698e55905SBjoern A. Zeeb 
478798e55905SBjoern A. Zeeb 	return (0);
478898e55905SBjoern A. Zeeb }
478911db70b6SBjoern A. Zeeb #endif
479011db70b6SBjoern A. Zeeb 
47916b4cac81SBjoern A. Zeeb static void
47926b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
47936b4cac81SBjoern A. Zeeb {
47946b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47956b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
47966b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
47976b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
47986b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
48006b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
48016b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
48026b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
48036b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
48046b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
48056b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
48066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4807e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4808ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
48096b4cac81SBjoern A. Zeeb 	void *buf;
481011db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
481198e55905SBjoern A. Zeeb 	uint8_t ac, tid, tailroom;
48126b4cac81SBjoern A. Zeeb 
48136b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
48146b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
48159d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
48166b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
48176b4cac81SBjoern A. Zeeb #endif
48186b4cac81SBjoern A. Zeeb 
48196b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4820b35f6cd0SBjoern A. Zeeb 	k = NULL;
482111db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
48226b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
48236b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
482411db70b6SBjoern A. Zeeb 
482511db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
482611db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
482711db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
482811db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
482911db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
483011db70b6SBjoern A. Zeeb 		}
483111db70b6SBjoern A. Zeeb #endif
483211db70b6SBjoern A. Zeeb 
483311db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
483411db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
48356b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
48366b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
48376b4cac81SBjoern A. Zeeb 			if (k == NULL) {
48386b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
48396b4cac81SBjoern A. Zeeb 				m_freem(m);
48406b4cac81SBjoern A. Zeeb 				return;
48416b4cac81SBjoern A. Zeeb 			}
48426b4cac81SBjoern A. Zeeb 		}
484311db70b6SBjoern A. Zeeb 	}
48446b4cac81SBjoern A. Zeeb 
48456b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
48466b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
48476b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
48486b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
48496b4cac81SBjoern A. Zeeb 
48506b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
48516b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
48526b4cac81SBjoern A. Zeeb 
48536b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
48546b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
48556b4cac81SBjoern A. Zeeb 		if (k != NULL)
48566b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
48576b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
48586b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
48596b4cac81SBjoern A. Zeeb 		IMPROVE();
48606b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
48616b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
48626b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
48636b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
48646b4cac81SBjoern A. Zeeb 		}
48656b4cac81SBjoern A. Zeeb 
48666b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
48676b4cac81SBjoern A. Zeeb 	}
48686b4cac81SBjoern A. Zeeb 
486998e55905SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
487098e55905SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
487198e55905SBjoern A. Zeeb 		tailroom = lkpi_hw_crypto_tailroom(lsta, k);
487298e55905SBjoern A. Zeeb 	else
487398e55905SBjoern A. Zeeb #endif
487498e55905SBjoern A. Zeeb 		tailroom = 0;
487598e55905SBjoern A. Zeeb 
48766b4cac81SBjoern A. Zeeb 	/*
48776b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
48786b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
48793d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
48803d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
48816b4cac81SBjoern A. Zeeb 	 */
488298e55905SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
48836b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4884bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4885bcf1d8eeSBjoern A. Zeeb 
4886bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4887bcf1d8eeSBjoern A. Zeeb 			int tid;
4888bcf1d8eeSBjoern A. Zeeb 
4889bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4890bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4891bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4892bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4893bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4894bcf1d8eeSBjoern A. Zeeb 					continue;
4895bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4896bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4897bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4898bcf1d8eeSBjoern A. Zeeb 			}
4899bcf1d8eeSBjoern A. Zeeb 		}
49006b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
49016b4cac81SBjoern A. Zeeb 		m_freem(m);
49026b4cac81SBjoern A. Zeeb 		return;
49036b4cac81SBjoern A. Zeeb 	}
49046b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
49056b4cac81SBjoern A. Zeeb 
49066b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
49076b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
49086b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
49096b4cac81SBjoern A. Zeeb 	skb->m = m;
49106b4cac81SBjoern A. Zeeb #if 0
49116b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
49126b4cac81SBjoern A. Zeeb #else
49136b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
49146b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
49156b4cac81SBjoern A. Zeeb #endif
49166b4cac81SBjoern A. Zeeb 	/* Save the ni. */
49176b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
49186b4cac81SBjoern A. Zeeb 
49196b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
49206b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
49216b4cac81SBjoern A. Zeeb 
4922e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4923e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4924e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
49251665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
49261665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
49271665ef97SBjoern A. Zeeb 			skb->priority = 7;
49281665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
49291665ef97SBjoern A. Zeeb 		} else {
49301665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
49311665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4932e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4933e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
49341665ef97SBjoern A. Zeeb 		}
4935e3a0b120SBjoern A. Zeeb 	} else {
4936e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4937fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4938e3a0b120SBjoern A. Zeeb 	}
49396b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
49406b4cac81SBjoern A. Zeeb 
49416b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
49426b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
49436b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
49446b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
49456b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
49466b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4947e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
49486b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
49496b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
49506b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
49516b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
49529fb91463SBjoern A. Zeeb #ifdef __notyet__
49539fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
49549fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
49559fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
49569fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
49579fb91463SBjoern A. Zeeb #endif
49589fb91463SBjoern A. Zeeb #endif
49596b4cac81SBjoern A. Zeeb 
49606b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4961b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
496211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
496311db70b6SBjoern A. Zeeb 		int error;
496411db70b6SBjoern A. Zeeb 
496511db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
496611db70b6SBjoern A. Zeeb 		if (error != 0) {
496711db70b6SBjoern A. Zeeb 			/*
496811db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
496911db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
497011db70b6SBjoern A. Zeeb 			 */
497111db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
497211db70b6SBjoern A. Zeeb 			return;
497311db70b6SBjoern A. Zeeb 		}
497411db70b6SBjoern A. Zeeb 	}
49756b4cac81SBjoern A. Zeeb #endif
49766b4cac81SBjoern A. Zeeb 
49776b4cac81SBjoern A. Zeeb 	IMPROVE();
49786b4cac81SBjoern A. Zeeb 
4979e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4980e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4981e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4982e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4983e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4984d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4985e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4986e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4987e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4988e3a0b120SBjoern A. Zeeb 	}
4989e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4990d0d29110SBjoern A. Zeeb 		goto ops_tx;
4991e3a0b120SBjoern A. Zeeb 
4992d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4993d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4994d0d29110SBjoern A. Zeeb 
4995eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
49966b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
49979d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49989d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4999d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
5000d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5001d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
5002fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
5003d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
5004d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5005d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
5006d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
50079d9ba2b7SBjoern A. Zeeb #endif
5008eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5009cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5010d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
5011cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
50126b4cac81SBjoern A. Zeeb 	return;
50136b4cac81SBjoern A. Zeeb 
50146b4cac81SBjoern A. Zeeb ops_tx:
50159d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50169d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5017d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5018d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
50196b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
50206b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
50219d9ba2b7SBjoern A. Zeeb #endif
50226b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
50236b4cac81SBjoern A. Zeeb 	control.sta = sta;
5024cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
50256b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
5026cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
50276b4cac81SBjoern A. Zeeb }
50286b4cac81SBjoern A. Zeeb 
50296b4cac81SBjoern A. Zeeb static void
50306b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
50316b4cac81SBjoern A. Zeeb {
50326b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50336b4cac81SBjoern A. Zeeb 	struct mbufq mq;
50346b4cac81SBjoern A. Zeeb 	struct mbuf *m;
503588665349SBjoern A. Zeeb 	bool shall_tx;
50366b4cac81SBjoern A. Zeeb 
50376b4cac81SBjoern A. Zeeb 	lsta = ctx;
50386b4cac81SBjoern A. Zeeb 
50399d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50409d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
50416b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
50429d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
50436b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
50449d9ba2b7SBjoern A. Zeeb #endif
50456b4cac81SBjoern A. Zeeb 
50466b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
50476b4cac81SBjoern A. Zeeb 
5048fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
5049fa4e4257SBjoern A. Zeeb 	/*
5050fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
505188665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
505288665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
505388665349SBjoern A. Zeeb 	 * point to try to TX.
505488665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
505588665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
5056fa4e4257SBjoern A. Zeeb 	 */
50572372f8ccSBjoern A. Zeeb #if 0
505888665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
50592372f8ccSBjoern A. Zeeb #else
50602372f8ccSBjoern A. Zeeb 	/*
50612372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
50622372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
50632372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
50642372f8ccSBjoern A. Zeeb 	 */
50652372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
50662372f8ccSBjoern A. Zeeb #endif
506788665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
50686b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
506988665349SBjoern A. Zeeb 	/*
507088665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
507188665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
507288665349SBjoern A. Zeeb 	 */
5073fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
50746b4cac81SBjoern A. Zeeb 
50756b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
50766b4cac81SBjoern A. Zeeb 	while (m != NULL) {
50776b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
50786b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
50796b4cac81SBjoern A. Zeeb 	}
50806b4cac81SBjoern A. Zeeb }
50816b4cac81SBjoern A. Zeeb 
50826b4cac81SBjoern A. Zeeb static int
50836b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
50846b4cac81SBjoern A. Zeeb {
50856b4cac81SBjoern A. Zeeb 
50866b4cac81SBjoern A. Zeeb 	/* XXX TODO */
50876b4cac81SBjoern A. Zeeb 	IMPROVE();
50886b4cac81SBjoern A. Zeeb 
50896b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
50906b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
50916b4cac81SBjoern A. Zeeb 
50926b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
50939a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, false));
50946b4cac81SBjoern A. Zeeb }
50956b4cac81SBjoern A. Zeeb 
50969fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50979fb91463SBjoern A. Zeeb static int
50989fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
50999fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
51009fb91463SBjoern A. Zeeb {
51019fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51029fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51039fb91463SBjoern A. Zeeb 
51049fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51059fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51069fb91463SBjoern A. Zeeb 
5107310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
51089fb91463SBjoern A. Zeeb 
51099fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
51109fb91463SBjoern A. Zeeb }
51119fb91463SBjoern A. Zeeb 
51129fb91463SBjoern A. Zeeb static int
51139fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
51149fb91463SBjoern A. Zeeb {
51159fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51169fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51179fb91463SBjoern A. Zeeb 
51189fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51199fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51209fb91463SBjoern A. Zeeb 
5121310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
51229fb91463SBjoern A. Zeeb 
51239fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
51249fb91463SBjoern A. Zeeb }
51259fb91463SBjoern A. Zeeb 
51269fb91463SBjoern A. Zeeb 
51279fb91463SBjoern A. Zeeb static int
51289fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
51299fb91463SBjoern A. Zeeb {
51309fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51319fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51329fb91463SBjoern A. Zeeb 
51339fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51349fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51359fb91463SBjoern A. Zeeb 
5136310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
51379fb91463SBjoern A. Zeeb 
51389fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
51399fb91463SBjoern A. Zeeb }
51409fb91463SBjoern A. Zeeb 
5141310743c4SBjoern A. Zeeb /*
5142310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5143310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5144310743c4SBjoern A. Zeeb  *
5145310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
5146310743c4SBjoern A. Zeeb  */
51479fb91463SBjoern A. Zeeb static int
51489fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
51499fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
51509fb91463SBjoern A. Zeeb {
51519fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51529fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5153310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5154310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5155310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5156310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5157310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5158310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5159310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5160310743c4SBjoern A. Zeeb 	int error;
51619fb91463SBjoern A. Zeeb 
51629fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51639fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5164310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5165310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5166310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5167310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5168310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5169310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51709fb91463SBjoern A. Zeeb 
5171310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5172310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5173310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5174310743c4SBjoern A. Zeeb 		return (0);
5175310743c4SBjoern A. Zeeb 	}
5176310743c4SBjoern A. Zeeb 
5177310743c4SBjoern A. Zeeb 	params.sta = sta;
5178310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
5179310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
5180310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5181310743c4SBjoern A. Zeeb 	params.timeout = 0;
5182310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5183310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5184310743c4SBjoern A. Zeeb 	params.amsdu = false;
5185310743c4SBjoern A. Zeeb 
5186310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5187cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5188310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5189cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5190310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5191310743c4SBjoern A. Zeeb 	if (error != 0) {
5192310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5193310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5194310743c4SBjoern A. Zeeb 		return (0);
5195310743c4SBjoern A. Zeeb 	}
51969fb91463SBjoern A. Zeeb 
51979fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
51989fb91463SBjoern A. Zeeb }
51999fb91463SBjoern A. Zeeb 
5200310743c4SBjoern A. Zeeb /*
5201310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5202310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
5203310743c4SBjoern A. Zeeb  *
5204310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
5205310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
5206310743c4SBjoern A. Zeeb  */
52079fb91463SBjoern A. Zeeb static int
52089fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
52099fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
52109fb91463SBjoern A. Zeeb {
52119fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52129fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5213310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5214310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5215310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5216310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5217310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5218310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5219310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5220310743c4SBjoern A. Zeeb 	int error;
52219fb91463SBjoern A. Zeeb 
52229fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52239fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5224310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5225310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5226310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5227310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5228310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5229310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52309fb91463SBjoern A. Zeeb 
5231310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5232310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5233310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5234310743c4SBjoern A. Zeeb 		return (0);
5235310743c4SBjoern A. Zeeb 	}
5236310743c4SBjoern A. Zeeb 
5237310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
5238310743c4SBjoern A. Zeeb 		params.sta = sta;
5239310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5240310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
5241310743c4SBjoern A. Zeeb 		params.timeout = 0;
5242310743c4SBjoern A. Zeeb 		params.ssn = 0;
5243310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5244310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5245310743c4SBjoern A. Zeeb 			params.amsdu = true;
5246310743c4SBjoern A. Zeeb 		else
5247310743c4SBjoern A. Zeeb 			params.amsdu = false;
5248310743c4SBjoern A. Zeeb 	} else {
5249310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
5250310743c4SBjoern A. Zeeb 		params.sta = sta;
5251310743c4SBjoern A. Zeeb 		switch (status) {
5252310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
5253310743c4SBjoern A. Zeeb 		default:
5254310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5255310743c4SBjoern A. Zeeb 			break;
5256310743c4SBjoern A. Zeeb 		}
5257310743c4SBjoern A. Zeeb 		params.buf_size = 0;
5258310743c4SBjoern A. Zeeb 		params.timeout = 0;
5259310743c4SBjoern A. Zeeb 		params.ssn = 0;
5260310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5261310743c4SBjoern A. Zeeb 		params.amsdu = false;
5262310743c4SBjoern A. Zeeb 	}
5263310743c4SBjoern A. Zeeb 
5264310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5265cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5266310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5267cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5268310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5269310743c4SBjoern A. Zeeb 	if (error != 0) {
5270310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5271310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5272310743c4SBjoern A. Zeeb 		return (0);
5273310743c4SBjoern A. Zeeb 	}
5274310743c4SBjoern A. Zeeb 
5275310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
52769fb91463SBjoern A. Zeeb 
52779fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
52789fb91463SBjoern A. Zeeb }
52799fb91463SBjoern A. Zeeb 
5280310743c4SBjoern A. Zeeb /*
5281310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5282310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5283310743c4SBjoern A. Zeeb  */
52849fb91463SBjoern A. Zeeb static void
52859fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
52869fb91463SBjoern A. Zeeb {
52879fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52889fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5289310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5290310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5291310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5292310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5293310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5294310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5295310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5296310743c4SBjoern A. Zeeb 	int error;
52979fb91463SBjoern A. Zeeb 
52989fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52999fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5300310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5301310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5302310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5303310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5304310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5305310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53069fb91463SBjoern A. Zeeb 
5307310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5308310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5309310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5310310743c4SBjoern A. Zeeb 		goto n80211;
5311310743c4SBjoern A. Zeeb 	}
53129fb91463SBjoern A. Zeeb 
5313310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5314310743c4SBjoern A. Zeeb 	params.sta = sta;
5315310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5316310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5317310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5318310743c4SBjoern A. Zeeb 	params.timeout = 0;
5319310743c4SBjoern A. Zeeb 	params.ssn = 0;
5320310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5321310743c4SBjoern A. Zeeb 	params.amsdu = false;
5322310743c4SBjoern A. Zeeb 
5323310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5324cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5325310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5326cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5327310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5328310743c4SBjoern A. Zeeb 	if (error != 0) {
5329310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5330310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5331310743c4SBjoern A. Zeeb 		goto n80211;
5332310743c4SBjoern A. Zeeb 	}
5333310743c4SBjoern A. Zeeb 
5334310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5335310743c4SBjoern A. Zeeb 
5336310743c4SBjoern A. Zeeb n80211:
53379fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
53389fb91463SBjoern A. Zeeb }
53399fb91463SBjoern A. Zeeb 
53409fb91463SBjoern A. Zeeb static void
53419fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
53429fb91463SBjoern A. Zeeb {
53439fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53449fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53459fb91463SBjoern A. Zeeb 
53469fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53479fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53489fb91463SBjoern A. Zeeb 
53499fb91463SBjoern A. Zeeb 	IMPROVE_HT();
53509fb91463SBjoern A. Zeeb 
53519fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
53529fb91463SBjoern A. Zeeb }
53539fb91463SBjoern A. Zeeb 
53549fb91463SBjoern A. Zeeb static void
53559fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
53569fb91463SBjoern A. Zeeb     int status)
53579fb91463SBjoern A. Zeeb {
53589fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53599fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53609fb91463SBjoern A. Zeeb 
53619fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53629fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53639fb91463SBjoern A. Zeeb 
53649fb91463SBjoern A. Zeeb 	IMPROVE_HT();
53659fb91463SBjoern A. Zeeb 
53669fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
53679fb91463SBjoern A. Zeeb }
53689fb91463SBjoern A. Zeeb 
53699fb91463SBjoern A. Zeeb static int
53709fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
53719fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
53729fb91463SBjoern A. Zeeb {
53739fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53749fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53759fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
53769fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
53779fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53789fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
53799fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
53809fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5381310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
53829fb91463SBjoern A. Zeeb 	int error;
53839fb91463SBjoern A. Zeeb 
53849fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53859fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53869fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53879fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
53889fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
53899fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
53909fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
53919fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53929fb91463SBjoern A. Zeeb 
5393310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5394310743c4SBjoern A. Zeeb 
5395310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5396310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5397310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5398310743c4SBjoern A. Zeeb 		return (-ENXIO);
5399310743c4SBjoern A. Zeeb 	}
5400310743c4SBjoern A. Zeeb 
54019fb91463SBjoern A. Zeeb 	params.sta = sta;
54029fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
54039fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
54049fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
54059fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
54069fb91463SBjoern A. Zeeb 	else
54079fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5408310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5409310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
54109fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
54119fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
54129fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
54139fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5414310743c4SBjoern A. Zeeb 
5415310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5416310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5417310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5418310743c4SBjoern A. Zeeb 		params.amsdu = true;
5419310743c4SBjoern A. Zeeb 	else
54209fb91463SBjoern A. Zeeb 		params.amsdu = false;
54219fb91463SBjoern A. Zeeb 
5422cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
54239fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5424cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
54259fb91463SBjoern A. Zeeb 	if (error != 0) {
54269fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
54279fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
54289fb91463SBjoern A. Zeeb 		return (error);
54299fb91463SBjoern A. Zeeb 	}
5430310743c4SBjoern A. Zeeb 
5431310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5432310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5433310743c4SBjoern A. Zeeb 	}
5434310743c4SBjoern A. Zeeb 
54359fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
54369fb91463SBjoern A. Zeeb 
54379fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
54389fb91463SBjoern A. Zeeb 	return (error);
54399fb91463SBjoern A. Zeeb }
54409fb91463SBjoern A. Zeeb 
54419fb91463SBjoern A. Zeeb static void
54429fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
54439fb91463SBjoern A. Zeeb {
54449fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54459fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54469fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54479fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
54489fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54499fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
54509fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
54519fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5452310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
54539fb91463SBjoern A. Zeeb 	int error;
54549fb91463SBjoern A. Zeeb 	uint8_t tid;
545565c573e4SBjoern A. Zeeb 	bool ic_locked;
54569fb91463SBjoern A. Zeeb 
54579fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54589fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54599fb91463SBjoern A. Zeeb 
54609fb91463SBjoern A. Zeeb 	/*
54619fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
54629fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
54639fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
54649fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
54659fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
54669fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
54679fb91463SBjoern A. Zeeb 	 * track some state itself.
54689fb91463SBjoern A. Zeeb 	 */
54699fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
54709fb91463SBjoern A. Zeeb 		goto net80211_only;
54719fb91463SBjoern A. Zeeb 
54729fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54739fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
54749fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
54759fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
54769fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
54779fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
54789fb91463SBjoern A. Zeeb 
54799fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
54809fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
54819fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
54829fb91463SBjoern A. Zeeb 			break;
54839fb91463SBjoern A. Zeeb 	}
54849fb91463SBjoern A. Zeeb 
54859fb91463SBjoern A. Zeeb 	params.sta = sta;
54869fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
54879fb91463SBjoern A. Zeeb 	params.buf_size = 0;
54889fb91463SBjoern A. Zeeb 	params.timeout = 0;
54899fb91463SBjoern A. Zeeb 	params.ssn = 0;
54909fb91463SBjoern A. Zeeb 	params.tid = tid;
54919fb91463SBjoern A. Zeeb 	params.amsdu = false;
54929fb91463SBjoern A. Zeeb 
549365c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
549465c573e4SBjoern A. Zeeb 	if (ic_locked)
549565c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5496cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
54979fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5498cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
549965c573e4SBjoern A. Zeeb 	if (ic_locked)
550065c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
55019fb91463SBjoern A. Zeeb 	if (error != 0)
55029fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
55039fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
55049fb91463SBjoern A. Zeeb 
55059fb91463SBjoern A. Zeeb net80211_only:
55069fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
55079fb91463SBjoern A. Zeeb }
55089fb91463SBjoern A. Zeeb #endif
55099fb91463SBjoern A. Zeeb 
55109fb91463SBjoern A. Zeeb static void
55119fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
55129fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
55139fb91463SBjoern A. Zeeb {
55149fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
55159fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
55169fb91463SBjoern A. Zeeb 
55179fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
55189fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
55199fb91463SBjoern A. Zeeb 		return;
55209fb91463SBjoern A. Zeeb 
55219fb91463SBjoern A. Zeeb 	switch (band) {
55229fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
55239fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
55249fb91463SBjoern A. Zeeb 		break;
55259fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
55269fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
55279fb91463SBjoern A. Zeeb 		break;
55289fb91463SBjoern A. Zeeb 	default:
55299fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
55309fb91463SBjoern A. Zeeb 		return;
55319fb91463SBjoern A. Zeeb 	}
55329fb91463SBjoern A. Zeeb 
55339fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
55349fb91463SBjoern A. Zeeb 
55359fb91463SBjoern A. Zeeb 	/*
55369fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
55379fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
55389fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
55399fb91463SBjoern A. Zeeb 	 */
55409fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
55419fb91463SBjoern A. Zeeb 
55429fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
55439fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
55449fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
55459fb91463SBjoern A. Zeeb #ifdef __notyet__
55469fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
55479fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
55489fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
55499fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
55509fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
55519fb91463SBjoern A. Zeeb #endif
55529fb91463SBjoern A. Zeeb 
55539fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
55549fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
55559fb91463SBjoern A. Zeeb 
55569fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
55579fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
55589fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
55599fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
55609fb91463SBjoern A. Zeeb #endif
55619fb91463SBjoern A. Zeeb }
55629fb91463SBjoern A. Zeeb 
55636b4cac81SBjoern A. Zeeb static void
55646b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
55656b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
55666b4cac81SBjoern A. Zeeb {
55676b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55686b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
55696b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
55706b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
55716b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
55726b4cac81SBjoern A. Zeeb 
55736b4cac81SBjoern A. Zeeb 	/* Channels */
55746b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
55756b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
55766b4cac81SBjoern A. Zeeb 
55776b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
55786b4cac81SBjoern A. Zeeb 	nchans = 0;
55796b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
55806b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
55816b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
55826b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
55836b4cac81SBjoern A. Zeeb 		chan_flags = 0;
55846b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
55856b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
55869fb91463SBjoern A. Zeeb 
55879fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
55886b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
55899fb91463SBjoern A. Zeeb 
55909fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
55919fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
55926b4cac81SBjoern A. Zeeb 
55936b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5594cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
55956b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
55966b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
55976b4cac81SBjoern A. Zeeb 
55986b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
55993f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
56003f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
56016b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
56026b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
56036b4cac81SBjoern A. Zeeb 				continue;
56046b4cac81SBjoern A. Zeeb 			}
56056b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
56066b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
56076b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
56086b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
56096b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
56106b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
56116b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
56126b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
56136b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
56146b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
56156b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
56166b4cac81SBjoern A. Zeeb 
56176b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
56186b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
56196b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
56205856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5621cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5622cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
56233f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
56243f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
56256b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
56266b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
56276b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5628cee56e77SBjoern A. Zeeb 			if (error != 0)
56296b4cac81SBjoern A. Zeeb 				break;
56306b4cac81SBjoern A. Zeeb 		}
56316b4cac81SBjoern A. Zeeb 	}
56326b4cac81SBjoern A. Zeeb 
56336b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
56346b4cac81SBjoern A. Zeeb 	nchans = 0;
56356b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
56366b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
56376b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
56386b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
56396b4cac81SBjoern A. Zeeb 		chan_flags = 0;
56406b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
56419fb91463SBjoern A. Zeeb 
56429fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
56439fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
56449fb91463SBjoern A. Zeeb 
56459fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
56466b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
56476b4cac81SBjoern A. Zeeb 
56486b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5649562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
56506b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5651ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5652ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
56536b4cac81SBjoern A. Zeeb 
56546b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
56556b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
56566b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5657562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
56586b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
56596b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5660562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
56616b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
56626b4cac81SBjoern A. Zeeb 		}
56636b4cac81SBjoern A. Zeeb #endif
56646b4cac81SBjoern A. Zeeb 
56656b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5666cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
56676b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
56686b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
56696b4cac81SBjoern A. Zeeb 
56706b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
56713f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
56723f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
56736b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
56746b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
56756b4cac81SBjoern A. Zeeb 				continue;
56766b4cac81SBjoern A. Zeeb 			}
56776b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
56786b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
56796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
56806b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
56816b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
56826b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
56836b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
56846b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
56856b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
56866b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
56876b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
56886b4cac81SBjoern A. Zeeb 
56896b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
56906b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
56916b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
56925856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5693cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5694cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
56953f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
56963f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
56976b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
56986b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
56996b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5700cee56e77SBjoern A. Zeeb 			if (error != 0)
57016b4cac81SBjoern A. Zeeb 				break;
57026b4cac81SBjoern A. Zeeb 		}
57036b4cac81SBjoern A. Zeeb 	}
57046b4cac81SBjoern A. Zeeb }
57056b4cac81SBjoern A. Zeeb 
57066b4cac81SBjoern A. Zeeb static void *
57076b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
57086b4cac81SBjoern A. Zeeb {
57096b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57106b4cac81SBjoern A. Zeeb 
57116b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
57126b4cac81SBjoern A. Zeeb 
57136b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
57146b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
57156b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
57166b4cac81SBjoern A. Zeeb 
57176b4cac81SBjoern A. Zeeb 	return (ic);
57186b4cac81SBjoern A. Zeeb }
57196b4cac81SBjoern A. Zeeb 
57206b4cac81SBjoern A. Zeeb struct ieee80211_hw *
57216b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
57226b4cac81SBjoern A. Zeeb {
57236b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
57246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57256b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5726a5ae63edSBjoern A. Zeeb 	int ac;
57276b4cac81SBjoern A. Zeeb 
57286b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
57296b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
57306b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
57316b4cac81SBjoern A. Zeeb 		return (NULL);
57326b4cac81SBjoern A. Zeeb 
57336b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
57346b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5735652e22d3SBjoern A. Zeeb 
57368ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5737eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
57388891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
57396b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5740a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5741a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5742a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5743a5ae63edSBjoern A. Zeeb 	}
57446b4cac81SBjoern A. Zeeb 
5745a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf */
5746a8a47a41SBjoern A. Zeeb 	INIT_LIST_HEAD(&lhw->lchanctx_list);
5747a8a47a41SBjoern A. Zeeb 
5748759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5749759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5750759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5751832b8e98SBjoern A. Zeeb 	mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
5752759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5753759a996dSBjoern A. Zeeb 
57546b4cac81SBjoern A. Zeeb 	/*
57556b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
57566b4cac81SBjoern A. Zeeb 	 * not initialized.
57576b4cac81SBjoern A. Zeeb 	 */
57586b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
57596b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5760086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
57616b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
57626b4cac81SBjoern A. Zeeb 
57636b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
57646b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
57656b4cac81SBjoern A. Zeeb 
57666b4cac81SBjoern A. Zeeb 	IMPROVE();
57676b4cac81SBjoern A. Zeeb 
57686b4cac81SBjoern A. Zeeb 	return (hw);
57696b4cac81SBjoern A. Zeeb }
57706b4cac81SBjoern A. Zeeb 
57716b4cac81SBjoern A. Zeeb void
57726b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
57736b4cac81SBjoern A. Zeeb {
57746b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5775759a996dSBjoern A. Zeeb 	struct mbuf *m;
57766b4cac81SBjoern A. Zeeb 
57776b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57786b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
57796b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
57806b4cac81SBjoern A. Zeeb 
5781759a996dSBjoern A. Zeeb 	/*
5782759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5783759a996dSBjoern A. Zeeb 	 */
5784759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5785759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5786759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5787759a996dSBjoern A. Zeeb 
5788759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5789759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5790759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5791759a996dSBjoern A. Zeeb 
5792759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5793759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5794759a996dSBjoern A. Zeeb 	while (m != NULL) {
5795dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
5796759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5797759a996dSBjoern A. Zeeb 
5798759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5799759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5800759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5801759a996dSBjoern A. Zeeb 
5802759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5803759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5804759a996dSBjoern A. Zeeb 		}
5805dbae3dcfSBjoern A. Zeeb #else
5806dbae3dcfSBjoern A. Zeeb 		if (m->m_pkthdr.PH_loc.ptr != NULL) {
5807dbae3dcfSBjoern A. Zeeb 			struct ieee80211_node *ni;
5808dbae3dcfSBjoern A. Zeeb 
5809dbae3dcfSBjoern A. Zeeb 			ni = m->m_pkthdr.PH_loc.ptr;
5810dbae3dcfSBjoern A. Zeeb 			ieee80211_free_node(ni);
5811dbae3dcfSBjoern A. Zeeb 		}
5812dbae3dcfSBjoern A. Zeeb #endif
5813759a996dSBjoern A. Zeeb 		m_freem(m);
5814759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5815759a996dSBjoern A. Zeeb 	}
5816759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5817759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5818759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5819759a996dSBjoern A. Zeeb 
5820a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf. */
5821a8a47a41SBjoern A. Zeeb 	if (!list_empty_careful(&lhw->lchanctx_list)) {
5822a8a47a41SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx, *next;
5823a8a47a41SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
5824a8a47a41SBjoern A. Zeeb 
5825a8a47a41SBjoern A. Zeeb 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
5826a8a47a41SBjoern A. Zeeb 			if (lchanctx->added_to_drv) {
5827a8a47a41SBjoern A. Zeeb 				/* In reality we should panic? */
5828a8a47a41SBjoern A. Zeeb 				chanctx_conf = &lchanctx->chanctx_conf;
5829a8a47a41SBjoern A. Zeeb 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
5830a8a47a41SBjoern A. Zeeb 			}
5831a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
5832a8a47a41SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
5833a8a47a41SBjoern A. Zeeb 		}
5834a8a47a41SBjoern A. Zeeb 	}
5835a8a47a41SBjoern A. Zeeb 
58366b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5837eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
58388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5839eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
58406b4cac81SBjoern A. Zeeb 	IMPROVE();
58416b4cac81SBjoern A. Zeeb }
58426b4cac81SBjoern A. Zeeb 
58436b4cac81SBjoern A. Zeeb void
58446b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
58456b4cac81SBjoern A. Zeeb {
58466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58476b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58486b4cac81SBjoern A. Zeeb 
58496b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58506b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58516b4cac81SBjoern A. Zeeb 
58526b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
58536b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
58546b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
58556b4cac81SBjoern A. Zeeb 
58566b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
58576b4cac81SBjoern A. Zeeb }
58586b4cac81SBjoern A. Zeeb 
58596b4cac81SBjoern A. Zeeb struct ieee80211_hw *
58606b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
58616b4cac81SBjoern A. Zeeb {
58626b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58636b4cac81SBjoern A. Zeeb 
58646b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58656b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
58666b4cac81SBjoern A. Zeeb }
58676b4cac81SBjoern A. Zeeb 
58686b4cac81SBjoern A. Zeeb static void
58696b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
58706b4cac81SBjoern A. Zeeb {
58716b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58726b4cac81SBjoern A. Zeeb 
58736b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58746b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
58756b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
58766b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
58776b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
58786b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
58796b4cac81SBjoern A. Zeeb }
58806b4cac81SBjoern A. Zeeb 
58812e183d99SBjoern A. Zeeb int
58826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
58836b4cac81SBjoern A. Zeeb {
58846b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58856b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5886c5b96b3eSBjoern A. Zeeb 	int band, i;
58876b4cac81SBjoern A. Zeeb 
58886b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58896b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58906b4cac81SBjoern A. Zeeb 
5891652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5892652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5893652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5894652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5895652e22d3SBjoern A. Zeeb 
58966b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
58976b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
58986b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
58996b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
59006b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
59016b4cac81SBjoern A. Zeeb 		/* We take the first one. */
59026b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
59036b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
59046b4cac81SBjoern A. Zeeb 	} else {
59056b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
59066b4cac81SBjoern A. Zeeb 	}
59076b4cac81SBjoern A. Zeeb 
59083d09d310SBjoern A. Zeeb #ifdef __not_yet__
59093d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
59106b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
59113d09d310SBjoern A. Zeeb #endif
59126b4cac81SBjoern A. Zeeb 
59136b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
59146b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
59156b4cac81SBjoern A. Zeeb 
59166b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
59176b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
59186b4cac81SBjoern A. Zeeb 	ic->ic_caps =
59196b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
59206b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
59216b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
59224a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
59236b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
59244a67f1dfSBjoern A. Zeeb #endif
59256b4cac81SBjoern A. Zeeb #if 0
59266b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
59276b4cac81SBjoern A. Zeeb #endif
59286b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
59296b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
59306b4cac81SBjoern A. Zeeb 	    ;
59316b4cac81SBjoern A. Zeeb #if 0
59326b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
59336b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
59346b4cac81SBjoern A. Zeeb #endif
5935cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5936cc4e78d5SBjoern A. Zeeb 		/*
5937cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5938cc4e78d5SBjoern A. Zeeb 		 *
5939cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5940cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5941cc4e78d5SBjoern A. Zeeb 		 * the flag.
5942cc4e78d5SBjoern A. Zeeb 		 */
59436b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5944a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
59456b4cac81SBjoern A. Zeeb 	}
59466b4cac81SBjoern A. Zeeb 
594763578bf2SBjoern A. Zeeb 	/* Does HW support Fragmentation offload? */
594863578bf2SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
594963578bf2SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
595063578bf2SBjoern A. Zeeb 
5951527687a9SBjoern A. Zeeb 	/*
5952527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5953527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5954527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5955527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5956527687a9SBjoern A. Zeeb 	 */
5957527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5958527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5959527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5960527687a9SBjoern A. Zeeb 
5961527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5962527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5963527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5964527687a9SBjoern A. Zeeb 		}
5965527687a9SBjoern A. Zeeb 	}
5966527687a9SBjoern A. Zeeb 
59676b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5968b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
596911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
5970bf8c25f1SBjoern A. Zeeb 		uint32_t hwciphers;
5971bf8c25f1SBjoern A. Zeeb 
5972bf8c25f1SBjoern A. Zeeb 		hwciphers = 0;
59736b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
5974bf8c25f1SBjoern A. Zeeb 			hwciphers |= lkpi_l80211_to_net80211_cyphers(
5975bf8c25f1SBjoern A. Zeeb 			    ic, hw->wiphy->cipher_suites[i]);
5976bf8c25f1SBjoern A. Zeeb 		/*
5977bf8c25f1SBjoern A. Zeeb 		 * (20250415) nothing anywhere in the path checks we actually
5978bf8c25f1SBjoern A. Zeeb 		 * support all these in net80211.
5979bf8c25f1SBjoern A. Zeeb 		 * net80211 supports _256 variants but the ioctl does not.
5980bf8c25f1SBjoern A. Zeeb 		 */
5981bf8c25f1SBjoern A. Zeeb 		IMPROVE("as net80211 grows more support, enable them");
5982bf8c25f1SBjoern A. Zeeb 		hwciphers &= (IEEE80211_CRYPTO_WEP | IEEE80211_CRYPTO_TKIP |
5983bf8c25f1SBjoern A. Zeeb 		    IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
5984bf8c25f1SBjoern A. Zeeb 		/* We only support CCMP here, so further filter. */
5985bf8c25f1SBjoern A. Zeeb 		hwciphers &= IEEE80211_CRYPTO_AES_CCM;
5986bf8c25f1SBjoern A. Zeeb 		ieee80211_set_hardware_ciphers(ic, hwciphers);
59876b4cac81SBjoern A. Zeeb 	}
59886b4cac81SBjoern A. Zeeb #endif
59896b4cac81SBjoern A. Zeeb 
59906b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
59916b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
59926b4cac81SBjoern A. Zeeb 
59936b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
59946b4cac81SBjoern A. Zeeb 
59956b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
59966b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
59976b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
59986b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
59996b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
60006b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
60016b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
60026b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
60036b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
60046b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
60056b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
60066b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
60076b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
60086b4cac81SBjoern A. Zeeb 
6009a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
6010a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6011a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6012a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6013a486fbbdSBjoern A. Zeeb 
60146b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
60156b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
60166b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
60176b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
60186b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
60196b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
60206b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
60216b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
60226b4cac81SBjoern A. Zeeb 
60239fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
602486bc7259SBjoern A. Zeeb 	/*
602586bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
602686bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
602786bc7259SBjoern A. Zeeb 	 */
602886bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
60299fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
60309fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
60319fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
60329fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
60339fb91463SBjoern A. Zeeb 
60349fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
60359fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
60369fb91463SBjoern A. Zeeb 
60379fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
60389fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
60399fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
60409fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
60419fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
60429fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
60439fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
60449fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
60459fb91463SBjoern A. Zeeb 
60469fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
60479fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
60489fb91463SBjoern A. Zeeb 
60499fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
60509fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
60519fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
60529fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
605386bc7259SBjoern A. Zeeb 	}
60549fb91463SBjoern A. Zeeb #endif
60559fb91463SBjoern A. Zeeb 
60566b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
60576b4cac81SBjoern A. Zeeb 
6058c5b96b3eSBjoern A. Zeeb 	/*
6059c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
6060c5b96b3eSBjoern A. Zeeb 	 * expect one.
6061d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
6062d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6063c5b96b3eSBjoern A. Zeeb 	 */
6064d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
6065f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6066c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6067c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6068c5b96b3eSBjoern A. Zeeb 
6069c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
6070c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6071c5b96b3eSBjoern A. Zeeb 			continue;
6072c5b96b3eSBjoern A. Zeeb 
6073d9945d78SBjoern A. Zeeb 		lhw->supbands++;
6074d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6075d9945d78SBjoern A. Zeeb 
6076f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
6077f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
6078f454a4a1SBjoern A. Zeeb 			continue;
6079f454a4a1SBjoern A. Zeeb 
6080c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
6081c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6082c5b96b3eSBjoern A. Zeeb 
6083c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6084c5b96b3eSBjoern A. Zeeb 				continue;
6085c5b96b3eSBjoern A. Zeeb 
60864a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
60879fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
608811450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
60899fb91463SBjoern A. Zeeb #endif
6090c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
6091c5b96b3eSBjoern A. Zeeb 			break;
6092c5b96b3eSBjoern A. Zeeb 		}
6093c5b96b3eSBjoern A. Zeeb 	}
6094c5b96b3eSBjoern A. Zeeb 
6095d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6096d9945d78SBjoern A. Zeeb 
6097d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
6098d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6099d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6100d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6101d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6102d9945d78SBjoern A. Zeeb 	}
6103d9945d78SBjoern A. Zeeb 
6104d9945d78SBjoern A. Zeeb 	/*
6105d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
6106d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
6107d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
6108d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
6109d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
6110d9945d78SBjoern A. Zeeb 	 */
6111d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6112d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
6113d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
611478ca45dfSBjoern A. Zeeb 
611578ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6116d9945d78SBjoern A. Zeeb 		/*
611778ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
611878ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
611978ca45dfSBjoern A. Zeeb 		 * space in.
6120d9945d78SBjoern A. Zeeb 		 */
6121d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
612278ca45dfSBjoern A. Zeeb 	}
6123d9945d78SBjoern A. Zeeb 
61249fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
61259fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
61269fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
61279fb91463SBjoern A. Zeeb #endif
61289fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
61291f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
61309fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
61319fb91463SBjoern A. Zeeb #endif
61329fb91463SBjoern A. Zeeb 
6133d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
613478ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
613578ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
613678ca45dfSBjoern A. Zeeb 			goto err;
6137d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
613878ca45dfSBjoern A. Zeeb 	}
6139d9945d78SBjoern A. Zeeb 
61406b4cac81SBjoern A. Zeeb 	if (bootverbose)
61416b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
61422e183d99SBjoern A. Zeeb 
61432e183d99SBjoern A. Zeeb 	return (0);
614478ca45dfSBjoern A. Zeeb err:
614578ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
614678ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
61476b4cac81SBjoern A. Zeeb }
61486b4cac81SBjoern A. Zeeb 
61496b4cac81SBjoern A. Zeeb void
61506b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
61516b4cac81SBjoern A. Zeeb {
61526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61536b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61546b4cac81SBjoern A. Zeeb 
61556b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61566b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
61576b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
61586b4cac81SBjoern A. Zeeb }
61596b4cac81SBjoern A. Zeeb 
61606b4cac81SBjoern A. Zeeb void
61616b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
61626b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
61636b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
61646b4cac81SBjoern A. Zeeb     void *arg)
61656b4cac81SBjoern A. Zeeb {
61666b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
61686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
616961a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
61706b4cac81SBjoern A. Zeeb 
61716b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61726b4cac81SBjoern A. Zeeb 
61736b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
61746b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
617561a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6176adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
61776b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
61786b4cac81SBjoern A. Zeeb 		    __func__, flags);
61796b4cac81SBjoern A. Zeeb 	}
61806b4cac81SBjoern A. Zeeb 
6181adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
61826b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
618361a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
61846b4cac81SBjoern A. Zeeb 
61856b4cac81SBjoern A. Zeeb 	if (atomic)
61868891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
61876b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
61886b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
61896b4cac81SBjoern A. Zeeb 
61906b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
61916b4cac81SBjoern A. Zeeb 
61926b4cac81SBjoern A. Zeeb 		/*
61936b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
61946b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
61956b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
61966b4cac81SBjoern A. Zeeb 		 * driver does not know about.
61976b4cac81SBjoern A. Zeeb 		 */
61986b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
61996b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
62006b4cac81SBjoern A. Zeeb 			continue;
62016b4cac81SBjoern A. Zeeb 
62026b4cac81SBjoern A. Zeeb 		/*
620361a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
620461a68e50SBjoern A. Zeeb 		 * if we haven't yet.
620561a68e50SBjoern A. Zeeb 		 */
620661a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
620761a68e50SBjoern A. Zeeb 			continue;
620861a68e50SBjoern A. Zeeb 
620961a68e50SBjoern A. Zeeb 		/*
62106b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
62116b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
62126b4cac81SBjoern A. Zeeb 		 */
62136b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
62146b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
62156b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
62166b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
62176b4cac81SBjoern A. Zeeb 	}
62186b4cac81SBjoern A. Zeeb 	if (atomic)
62198891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
62206b4cac81SBjoern A. Zeeb }
62216b4cac81SBjoern A. Zeeb 
622211db70b6SBjoern A. Zeeb static void
622311db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
622411db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
622511db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
622611db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
622711db70b6SBjoern A. Zeeb     void *arg)
622811db70b6SBjoern A. Zeeb {
622911db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
623011db70b6SBjoern A. Zeeb 		return;
623111db70b6SBjoern A. Zeeb 
623211db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
623311db70b6SBjoern A. Zeeb 		return;
623411db70b6SBjoern A. Zeeb 
623511db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
623611db70b6SBjoern A. Zeeb }
623711db70b6SBjoern A. Zeeb 
62386b4cac81SBjoern A. Zeeb void
62396b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
62406b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
62416b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
62426b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
624311db70b6SBjoern A. Zeeb     void *arg, bool rcu)
62446b4cac81SBjoern A. Zeeb {
624511db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
624611db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62476b4cac81SBjoern A. Zeeb 
624811db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
624911db70b6SBjoern A. Zeeb 
625011db70b6SBjoern A. Zeeb 	if (rcu) {
6251a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6252a8f735a6SBjoern A. Zeeb 
625311db70b6SBjoern A. Zeeb 		if (vif == NULL) {
625411db70b6SBjoern A. Zeeb 			TODO();
625511db70b6SBjoern A. Zeeb 		} else {
6256a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
625711db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
625811db70b6SBjoern A. Zeeb 				    keyix++)
625911db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
626011db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
626111db70b6SBjoern A. Zeeb 			}
626211db70b6SBjoern A. Zeeb 		}
626311db70b6SBjoern A. Zeeb 	} else {
626411db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
626511db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
626611db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
626711db70b6SBjoern A. Zeeb 
626811db70b6SBjoern A. Zeeb 		if (vif == NULL) {
626911db70b6SBjoern A. Zeeb 			TODO();
627011db70b6SBjoern A. Zeeb 		} else {
6271a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
627211db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
627311db70b6SBjoern A. Zeeb 				    keyix++)
627411db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
627511db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
627611db70b6SBjoern A. Zeeb 			}
627711db70b6SBjoern A. Zeeb 		}
627811db70b6SBjoern A. Zeeb 	}
62796b4cac81SBjoern A. Zeeb }
62806b4cac81SBjoern A. Zeeb 
62816b4cac81SBjoern A. Zeeb void
62826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
62836b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
62846b4cac81SBjoern A. Zeeb 	void *),
62856b4cac81SBjoern A. Zeeb     void *arg)
62866b4cac81SBjoern A. Zeeb {
6287c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6288c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
62896b4cac81SBjoern A. Zeeb 
6290c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
6291c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6292c5e25798SBjoern A. Zeeb 
6293c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6294c5e25798SBjoern A. Zeeb 
6295a8a47a41SBjoern A. Zeeb 	rcu_read_lock();
6296a8a47a41SBjoern A. Zeeb 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6297c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
6298c5e25798SBjoern A. Zeeb 			continue;
6299d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6300c5e25798SBjoern A. Zeeb 	}
6301a8a47a41SBjoern A. Zeeb 	rcu_read_unlock();
63026b4cac81SBjoern A. Zeeb }
63036b4cac81SBjoern A. Zeeb 
63046b4cac81SBjoern A. Zeeb void
63056b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
63066b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
63076b4cac81SBjoern A. Zeeb {
63086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63096b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
63106b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
63116b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
63126b4cac81SBjoern A. Zeeb 
63136b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
63146b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
63156b4cac81SBjoern A. Zeeb 
63166b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63176b4cac81SBjoern A. Zeeb 
63188891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
63196b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
63206b4cac81SBjoern A. Zeeb 
6321a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6322a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
63236b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
63246b4cac81SBjoern A. Zeeb 				continue;
63256b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
63266b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
63276b4cac81SBjoern A. Zeeb 		}
6328a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
63296b4cac81SBjoern A. Zeeb 	}
63308891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
63316b4cac81SBjoern A. Zeeb }
63326b4cac81SBjoern A. Zeeb 
6333673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6334673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6335673d62fcSBjoern A. Zeeb {
6336673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6337673d62fcSBjoern A. Zeeb 
6338673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6339673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6340673d62fcSBjoern A. Zeeb 	return (regd);
6341673d62fcSBjoern A. Zeeb }
6342673d62fcSBjoern A. Zeeb 
63436b4cac81SBjoern A. Zeeb int
63446b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
63456b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
63466b4cac81SBjoern A. Zeeb {
63476b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63486b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
63496b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
63506b4cac81SBjoern A. Zeeb 
63516b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
63526b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63536b4cac81SBjoern A. Zeeb 
63546b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
63556b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
63566b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
63576b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
63586b4cac81SBjoern A. Zeeb 	}
63596b4cac81SBjoern A. Zeeb 
63606b4cac81SBjoern A. Zeeb 	TODO();
63616b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
63626b4cac81SBjoern A. Zeeb 
63636b4cac81SBjoern A. Zeeb 	return (0);
63646b4cac81SBjoern A. Zeeb }
63656b4cac81SBjoern A. Zeeb 
63666b4cac81SBjoern A. Zeeb void
63676b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
63686b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
63696b4cac81SBjoern A. Zeeb {
63706b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63716b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
63726b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
63736b4cac81SBjoern A. Zeeb 
63746b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
63756b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63766b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
63776b4cac81SBjoern A. Zeeb 
63786b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
63796b4cac81SBjoern A. Zeeb 
63808ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
63816b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
63826b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6383a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
63846b4cac81SBjoern A. Zeeb 	wakeup(lhw);
63858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
63866b4cac81SBjoern A. Zeeb 
63876b4cac81SBjoern A. Zeeb 	return;
63886b4cac81SBjoern A. Zeeb }
63896b4cac81SBjoern A. Zeeb 
6390759a996dSBjoern A. Zeeb static void
6391759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6392759a996dSBjoern A. Zeeb {
6393759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6394dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6395759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6396dbae3dcfSBjoern A. Zeeb #endif
6397759a996dSBjoern A. Zeeb 	int ok;
6398759a996dSBjoern A. Zeeb 
6399759a996dSBjoern A. Zeeb 	ni = NULL;
6400dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6401759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6402759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6403759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6404759a996dSBjoern A. Zeeb 
6405759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6406759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6407759a996dSBjoern A. Zeeb 	}
6408dbae3dcfSBjoern A. Zeeb #else
6409dbae3dcfSBjoern A. Zeeb 	if (m->m_pkthdr.PH_loc.ptr != NULL) {
6410dbae3dcfSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6411dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = NULL;
6412dbae3dcfSBjoern A. Zeeb 	}
6413dbae3dcfSBjoern A. Zeeb #endif
6414759a996dSBjoern A. Zeeb 
6415759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6416759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6417759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6418759a996dSBjoern A. Zeeb 		if (ok < 0)
6419759a996dSBjoern A. Zeeb 			m_freem(m);
6420759a996dSBjoern A. Zeeb 	} else {
6421759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6422759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6423759a996dSBjoern A. Zeeb 	}
6424759a996dSBjoern A. Zeeb 
6425759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6426759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6427bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6428759a996dSBjoern A. Zeeb #endif
6429759a996dSBjoern A. Zeeb }
6430759a996dSBjoern A. Zeeb 
6431759a996dSBjoern A. Zeeb static void
6432759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6433759a996dSBjoern A. Zeeb {
6434759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6435759a996dSBjoern A. Zeeb 	struct mbufq mq;
6436759a996dSBjoern A. Zeeb 	struct mbuf *m;
6437759a996dSBjoern A. Zeeb 
6438759a996dSBjoern A. Zeeb 	lhw = ctx;
6439759a996dSBjoern A. Zeeb 
6440759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6441759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6442bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6443bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6444759a996dSBjoern A. Zeeb #endif
6445759a996dSBjoern A. Zeeb 
6446759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6447759a996dSBjoern A. Zeeb 
6448759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6449759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6450759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6451759a996dSBjoern A. Zeeb 
6452759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6453759a996dSBjoern A. Zeeb 	while (m != NULL) {
6454759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6455759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6456759a996dSBjoern A. Zeeb 	}
6457759a996dSBjoern A. Zeeb }
6458759a996dSBjoern A. Zeeb 
645949010ba7SBjoern A. Zeeb static void
6460a1adefb1SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
646149010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
646249010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
646349010ba7SBjoern A. Zeeb     uint8_t *rssip)
646449010ba7SBjoern A. Zeeb {
646549010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
6466a1adefb1SBjoern A. Zeeb 	struct rate_info rxrate;
646749010ba7SBjoern A. Zeeb 	int i;
646849010ba7SBjoern A. Zeeb 	uint8_t rssi;
646949010ba7SBjoern A. Zeeb 
6470a1adefb1SBjoern A. Zeeb 	memset(&rxrate, 0, sizeof(rxrate));
647149010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
647249010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
647349010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
647449010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
647549010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
647649010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
647749010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
647849010ba7SBjoern A. Zeeb 	else
647949010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
648049010ba7SBjoern A. Zeeb 	/*
648149010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
648249010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
648349010ba7SBjoern A. Zeeb 	 */
648449010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
648549010ba7SBjoern A. Zeeb 	if (rssip != NULL)
648649010ba7SBjoern A. Zeeb 		*rssip = rssi;
648749010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
648849010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
648949010ba7SBjoern A. Zeeb 	rx_stats->c_band =
649049010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
649149010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
649249010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
649349010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
649449010ba7SBjoern A. Zeeb 
649549010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
649649010ba7SBjoern A. Zeeb 
649749010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
649849010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
649949010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
650049010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
650149010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
650249010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
650349010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
650449010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
650549010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
650649010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
650749010ba7SBjoern A. Zeeb 	}
650849010ba7SBjoern A. Zeeb 
650949010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
651049010ba7SBjoern A. Zeeb 		int cc;
651149010ba7SBjoern A. Zeeb 		int8_t crssi;
651249010ba7SBjoern A. Zeeb 
651349010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
651449010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
651549010ba7SBjoern A. Zeeb 
651649010ba7SBjoern A. Zeeb 		cc = 0;
651749010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
651849010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
651949010ba7SBjoern A. Zeeb 				continue;
652049010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
652149010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
652249010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
652349010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
652449010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
652549010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
652649010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
652749010ba7SBjoern A. Zeeb 			cc++;
652849010ba7SBjoern A. Zeeb 		}
652949010ba7SBjoern A. Zeeb 		if (cc > 0)
653049010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
653149010ba7SBjoern A. Zeeb 	}
653249010ba7SBjoern A. Zeeb 
653349010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
653449010ba7SBjoern A. Zeeb 
653549010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
653649010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
6537a1adefb1SBjoern A. Zeeb 	{
6538a1adefb1SBjoern A. Zeeb 		uint32_t legacy = 0;
6539a1adefb1SBjoern A. Zeeb 
654049010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
654149010ba7SBjoern A. Zeeb 		if (supband != NULL)
6542a1adefb1SBjoern A. Zeeb 			legacy = supband->bitrates[rx_status->rate_idx].bitrate;
6543a1adefb1SBjoern A. Zeeb 		rx_stats->c_rate = legacy;
6544a1adefb1SBjoern A. Zeeb 		rxrate.legacy = legacy;
654549010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
654649010ba7SBjoern A. Zeeb 		break;
6547a1adefb1SBjoern A. Zeeb 	}
654849010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
654949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
655049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6551a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_MCS;
6552a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6553a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6554a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6555a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6556a1adefb1SBjoern A. Zeeb 		}
655749010ba7SBjoern A. Zeeb 		break;
655849010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
655949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
656049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
656149010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
6562a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
6563a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6564a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6565a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6566a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6567a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6568a1adefb1SBjoern A. Zeeb 		}
656949010ba7SBjoern A. Zeeb 		break;
657049010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
6571a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
6572a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6573a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6574a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
6575a1adefb1SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6576a1adefb1SBjoern A. Zeeb 		break;
657749010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
6578a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
6579a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6580a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6581a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
658249010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
658349010ba7SBjoern A. Zeeb 		break;
658449010ba7SBjoern A. Zeeb 	}
658549010ba7SBjoern A. Zeeb 
6586a1adefb1SBjoern A. Zeeb 	rxrate.bw = rx_status->bw;
658749010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
658849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
658949010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
659049010ba7SBjoern A. Zeeb 		break;
659149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
659249010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
659349010ba7SBjoern A. Zeeb 		break;
659449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
659549010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
659649010ba7SBjoern A. Zeeb 		break;
659749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
659849010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
659949010ba7SBjoern A. Zeeb 		break;
660049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
660149010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
660249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
660349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
660449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
660549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
660649010ba7SBjoern A. Zeeb 		break;
660749010ba7SBjoern A. Zeeb 	}
660849010ba7SBjoern A. Zeeb 
660949010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
661049010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
661149010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
661249010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
661349010ba7SBjoern A. Zeeb 
661449010ba7SBjoern A. Zeeb 	/*
661549010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
661649010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
661749010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
661849010ba7SBjoern A. Zeeb 	 */
661949010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
662049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
662149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
662249010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
662349010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
662449010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
662549010ba7SBjoern A. Zeeb 	}
6626731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
6627731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
6628731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
6629731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
6630731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6631731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
663249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
663349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6634394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6635394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
663649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
663749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
663849010ba7SBjoern A. Zeeb #endif
6639a1adefb1SBjoern A. Zeeb 
6640a1adefb1SBjoern A. Zeeb 	if (lsta != NULL) {
6641a1adefb1SBjoern A. Zeeb 		memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
6642a1adefb1SBjoern A. Zeeb 		lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
6643a1adefb1SBjoern A. Zeeb 	}
664449010ba7SBjoern A. Zeeb }
664549010ba7SBjoern A. Zeeb 
6646e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
66476b4cac81SBjoern A. Zeeb void
66486b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6649e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6650e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
66516b4cac81SBjoern A. Zeeb {
66526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
66536b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
66546b4cac81SBjoern A. Zeeb 	struct mbuf *m;
66556b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
66566b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
66576b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
66586b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
66596b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
66606b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
66616b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
6662c816f64eSBjoern A. Zeeb 	int i, offset, ok, error;
666349010ba7SBjoern A. Zeeb 	uint8_t rssi;
6664c0cadd99SBjoern A. Zeeb 	bool is_beacon;
66656b4cac81SBjoern A. Zeeb 
6666c013f810SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6667c013f810SBjoern A. Zeeb 	ic = lhw->ic;
6668c013f810SBjoern A. Zeeb 
66696b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
66706b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
6671c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
66726b4cac81SBjoern A. Zeeb 		IMPROVE();
66736b4cac81SBjoern A. Zeeb 		goto err;
66746b4cac81SBjoern A. Zeeb 	}
66756b4cac81SBjoern A. Zeeb 
66766b4cac81SBjoern A. Zeeb 	/*
66776b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
66786b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
66796b4cac81SBjoern A. Zeeb 	 */
66806b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
6681c013f810SBjoern A. Zeeb 	if (m == NULL) {
6682c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
66836b4cac81SBjoern A. Zeeb 		goto err;
6684c013f810SBjoern A. Zeeb 	}
66856b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
66866b4cac81SBjoern A. Zeeb 
66876b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
66886b4cac81SBjoern A. Zeeb 	offset = m->m_len;
66896b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
66906b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
66916b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
66926b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
66936b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
66946b4cac81SBjoern A. Zeeb 	}
66956b4cac81SBjoern A. Zeeb 
66966b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
66976b4cac81SBjoern A. Zeeb 
66986b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6699c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6700c0cadd99SBjoern A. Zeeb 
6701c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
67029d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
67036b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
67046b4cac81SBjoern A. Zeeb 
67059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
670673e3969fSBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
6707c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
670873e3969fSBjoern A. Zeeb 		    __func__, skb, skb->len, skb->data_len,
67096b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
67106b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6711c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
67126b4cac81SBjoern A. Zeeb 
67139d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
67146b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
67156b4cac81SBjoern A. Zeeb 
67166b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
67179d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6718f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
671960970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
67206b4cac81SBjoern A. Zeeb 			__func__,
6721c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6722c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
67236b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6724f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
67256b4cac81SBjoern A. Zeeb 			rx_status->freq,
67266b4cac81SBjoern A. Zeeb 			rx_status->bw,
67276b4cac81SBjoern A. Zeeb 			rx_status->encoding,
67286b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
67296b4cac81SBjoern A. Zeeb 			rx_status->band,
67306b4cac81SBjoern A. Zeeb 			rx_status->chains,
67316b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
67326b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
67336b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
673460970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
67356b4cac81SBjoern A. Zeeb 			rx_status->signal,
67366b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
67376b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
67386b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
67396b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
67406b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
67416b4cac81SBjoern A. Zeeb 			rx_status->nss,
67426b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
67436b4cac81SBjoern A. Zeeb no_trace_beacons:
67446b4cac81SBjoern A. Zeeb #endif
67456b4cac81SBjoern A. Zeeb 
674658246901SBjoern A. Zeeb 	lsta = NULL;
67476b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
67486b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
67496b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
67506b4cac81SBjoern A. Zeeb 	} else {
6751c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6752c8dafefaSBjoern A. Zeeb 
67536b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
67546b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
67556b4cac81SBjoern A. Zeeb 		if (ni != NULL)
67566b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
67576b4cac81SBjoern A. Zeeb 	}
67586b4cac81SBjoern A. Zeeb 
6759a1adefb1SBjoern A. Zeeb 	rssi = 0;
6760a1adefb1SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
6761a1adefb1SBjoern A. Zeeb 
6762a1adefb1SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
6763a1adefb1SBjoern A. Zeeb 	if (ok == 0) {
6764a1adefb1SBjoern A. Zeeb 		m_freem(m);
6765a1adefb1SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6766a1adefb1SBjoern A. Zeeb 		goto err;
6767a1adefb1SBjoern A. Zeeb 	}
6768a1adefb1SBjoern A. Zeeb 
67696b4cac81SBjoern A. Zeeb 	if (ni != NULL)
67706b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
67716b4cac81SBjoern A. Zeeb 	else
67726b4cac81SBjoern A. Zeeb 		/*
67736b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
67746b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
67756b4cac81SBjoern A. Zeeb 		 */
67766b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
67776b4cac81SBjoern A. Zeeb 
67789d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
67799d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6780bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6781c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6782c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
67839d9ba2b7SBjoern A. Zeeb #endif
67846b4cac81SBjoern A. Zeeb 
6785c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6786c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6787c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6788c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6789c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6790c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6791c8dafefaSBjoern A. Zeeb 
6792c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6793c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6794c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6795c8dafefaSBjoern A. Zeeb 
6796c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6797c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6798c8dafefaSBjoern A. Zeeb 
6799c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6800c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6801c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6802c8dafefaSBjoern A. Zeeb 		/*
6803c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6804c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6805c8dafefaSBjoern A. Zeeb 		 */
6806c8dafefaSBjoern A. Zeeb skip_device_ts:
68079fb91463SBjoern A. Zeeb 		;
68089fb91463SBjoern A. Zeeb 	}
6809c8dafefaSBjoern A. Zeeb 
68106b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
68116b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
68126b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
68136b4cac81SBjoern A. Zeeb 
68146b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
68156b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
68166b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
68176b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
68186b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
68196b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
68206b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
68216b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
68226b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
68236b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
68246b4cac81SBjoern A. Zeeb #endif
68256b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
68266b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
68276b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
68286b4cac81SBjoern A. Zeeb 		IMPROVE();
68296b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
68306b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
68316b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
68326b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6833170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
68346b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
68356b4cac81SBjoern A. Zeeb 	}
68366b4cac81SBjoern A. Zeeb 
68376b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
68386b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
68396b4cac81SBjoern A. Zeeb 
6840e30e05d3SBjoern A. Zeeb #if 0
6841e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6842e30e05d3SBjoern A. Zeeb 		/*
6843e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6844e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6845e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6846e30e05d3SBjoern A. Zeeb 		*/
6847e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6848e30e05d3SBjoern A. Zeeb 	}
6849e30e05d3SBjoern A. Zeeb #endif
6850e30e05d3SBjoern A. Zeeb 
6851c013f810SBjoern A. Zeeb 	/* Attach meta-information to the mbuf for the deferred RX path. */
68526b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6853dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6854759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6855759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6856759a996dSBjoern A. Zeeb 
6857759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6858759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6859c013f810SBjoern A. Zeeb 		if (mtag == NULL) {
6860c013f810SBjoern A. Zeeb 			m_freem(m);
6861c013f810SBjoern A. Zeeb 			counter_u64_add(ic->ic_ierrors, 1);
6862c013f810SBjoern A. Zeeb 			goto err;
6863c013f810SBjoern A. Zeeb 		}
6864759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6865759a996dSBjoern A. Zeeb 		rxni->ni = ni;		/* We hold a reference. */
6866759a996dSBjoern A. Zeeb 		m_tag_prepend(m, mtag);
6867dbae3dcfSBjoern A. Zeeb #else
6868dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = ni;	/* We hold a reference. */
6869dbae3dcfSBjoern A. Zeeb #endif
6870759a996dSBjoern A. Zeeb 	}
68716b4cac81SBjoern A. Zeeb 
6872759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6873759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6874759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6875759a996dSBjoern A. Zeeb 		m_freem(m);
6876c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6877759a996dSBjoern A. Zeeb 		goto err;
6878759a996dSBjoern A. Zeeb 	}
6879759a996dSBjoern A. Zeeb 
6880c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lhw->rxq, m);
6881c816f64eSBjoern A. Zeeb 	if (error != 0) {
6882c816f64eSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6883c816f64eSBjoern A. Zeeb 		m_freem(m);
6884c816f64eSBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6885c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6886c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6887c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
6888c816f64eSBjoern A. Zeeb 			    __func__, error);
6889c816f64eSBjoern A. Zeeb #endif
6890c816f64eSBjoern A. Zeeb 		goto err;
6891c816f64eSBjoern A. Zeeb 	}
6892759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6893759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
68946b4cac81SBjoern A. Zeeb 
68956b4cac81SBjoern A. Zeeb 	IMPROVE();
68966b4cac81SBjoern A. Zeeb 
68976b4cac81SBjoern A. Zeeb err:
68986b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
68996b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
69006b4cac81SBjoern A. Zeeb }
69016b4cac81SBjoern A. Zeeb 
69026b4cac81SBjoern A. Zeeb uint8_t
6903ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
69046b4cac81SBjoern A. Zeeb {
69056b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6906ec190d91SBjoern A. Zeeb 	uint8_t tid;
6907ec190d91SBjoern A. Zeeb 
6908ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6909ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6910ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6911ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
69126b4cac81SBjoern A. Zeeb 
69136b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6914ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6915ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6916ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6917ec190d91SBjoern A. Zeeb 
6918ec190d91SBjoern A. Zeeb 	return (tid);
69196b4cac81SBjoern A. Zeeb }
69206b4cac81SBjoern A. Zeeb 
6921ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6922ac1d519cSBjoern A. Zeeb 
6923ac1d519cSBjoern A. Zeeb static void
6924ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6925ac1d519cSBjoern A. Zeeb {
6926ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6927ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6928ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6929ac1d519cSBjoern A. Zeeb 
6930ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6931ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6932ac1d519cSBjoern A. Zeeb 
6933ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6934ac1d519cSBjoern A. Zeeb 
6935ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6936ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6937ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6938ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6939ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6940ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6941ac1d519cSBjoern A. Zeeb 		return;
6942ac1d519cSBjoern A. Zeeb 	}
6943ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6944ac1d519cSBjoern A. Zeeb 
6945ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6946ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6947ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6948ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6949ac1d519cSBjoern A. Zeeb 
6950ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6951ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6952ac1d519cSBjoern A. Zeeb 
6953ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6954ac1d519cSBjoern A. Zeeb }
6955ac1d519cSBjoern A. Zeeb 
6956ac1d519cSBjoern A. Zeeb void
6957ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6958ac1d519cSBjoern A. Zeeb {
6959ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6960ac1d519cSBjoern A. Zeeb 
6961ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6962ac1d519cSBjoern A. Zeeb 
6963ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6964ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6965ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6966ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6967ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6968ac1d519cSBjoern A. Zeeb 
6969ac1d519cSBjoern A. Zeeb 	/*
6970ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6971ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6972ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6973ac1d519cSBjoern A. Zeeb 	 */
6974ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6975ac1d519cSBjoern A. Zeeb }
6976ac1d519cSBjoern A. Zeeb 
6977ac1d519cSBjoern A. Zeeb void
6978ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6979ac1d519cSBjoern A. Zeeb {
6980ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6981ac1d519cSBjoern A. Zeeb 
6982ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6983ac1d519cSBjoern A. Zeeb 
6984ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6985ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6986ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6987ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6988ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6989ac1d519cSBjoern A. Zeeb }
6990ac1d519cSBjoern A. Zeeb 
6991ac1d519cSBjoern A. Zeeb void
6992ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6993ac1d519cSBjoern A. Zeeb {
6994ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6995ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6996ac1d519cSBjoern A. Zeeb 
6997ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6998ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6999ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
7000ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
7001ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7002ac1d519cSBjoern A. Zeeb 		return;
7003ac1d519cSBjoern A. Zeeb 	}
7004ac1d519cSBjoern A. Zeeb 
7005ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
7006ac1d519cSBjoern A. Zeeb 
7007ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7008ac1d519cSBjoern A. Zeeb 		    entry);
7009ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
7010ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7011ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
7012ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7013ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
7014ac1d519cSBjoern A. Zeeb 			break;
7015ac1d519cSBjoern A. Zeeb 	}
7016ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7017ac1d519cSBjoern A. Zeeb }
7018ac1d519cSBjoern A. Zeeb 
7019ac1d519cSBjoern A. Zeeb void
7020ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7021ac1d519cSBjoern A. Zeeb {
7022ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
7023ac1d519cSBjoern A. Zeeb 
7024ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
7025ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7026ac1d519cSBjoern A. Zeeb }
7027ac1d519cSBjoern A. Zeeb 
7028ac1d519cSBjoern A. Zeeb void
7029ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7030ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
7031ac1d519cSBjoern A. Zeeb {
7032ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
7033ac1d519cSBjoern A. Zeeb 		/* Run right away. */
7034ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
7035ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
7036ac1d519cSBjoern A. Zeeb 	} else {
7037ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
7038ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
7039ac1d519cSBjoern A. Zeeb 	}
7040ac1d519cSBjoern A. Zeeb }
7041ac1d519cSBjoern A. Zeeb 
7042ac1d519cSBjoern A. Zeeb void
7043ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7044ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
7045ac1d519cSBjoern A. Zeeb {
7046ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
7047ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
7048ac1d519cSBjoern A. Zeeb }
7049ac1d519cSBjoern A. Zeeb 
7050ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7051ac1d519cSBjoern A. Zeeb 
70526b4cac81SBjoern A. Zeeb struct wiphy *
70536b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
70546b4cac81SBjoern A. Zeeb {
70556b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7056ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
70576b4cac81SBjoern A. Zeeb 
70586b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
70596b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
70606b4cac81SBjoern A. Zeeb 		return (NULL);
70616b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
70626b4cac81SBjoern A. Zeeb 
7063ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7064ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
7065ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7066ac1d519cSBjoern A. Zeeb 
7067ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7068ac1d519cSBjoern A. Zeeb 
7069ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
7070ac1d519cSBjoern A. Zeeb 	TODO();
7071ac1d519cSBjoern A. Zeeb 
7072ac1d519cSBjoern A. Zeeb 	return (wiphy);
70736b4cac81SBjoern A. Zeeb }
70746b4cac81SBjoern A. Zeeb 
70756b4cac81SBjoern A. Zeeb void
70766b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
70776b4cac81SBjoern A. Zeeb {
70786b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
70796b4cac81SBjoern A. Zeeb 
70806b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
70816b4cac81SBjoern A. Zeeb 		return;
70826b4cac81SBjoern A. Zeeb 
7083ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
7084ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
7085ac1d519cSBjoern A. Zeeb 
70866b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7087ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7088ac1d519cSBjoern A. Zeeb 
70896b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
70906b4cac81SBjoern A. Zeeb }
70916b4cac81SBjoern A. Zeeb 
7092a7c19b8aSBjoern A. Zeeb static uint32_t
7093a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7094a7c19b8aSBjoern A. Zeeb {
7095a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
7096a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7097a7c19b8aSBjoern A. Zeeb }
7098a7c19b8aSBjoern A. Zeeb 
7099a7c19b8aSBjoern A. Zeeb static uint32_t
7100a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7101a7c19b8aSBjoern A. Zeeb {
7102a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
7103a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7104a7c19b8aSBjoern A. Zeeb }
7105a7c19b8aSBjoern A. Zeeb 
7106a7c19b8aSBjoern A. Zeeb uint32_t
7107a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7108a7c19b8aSBjoern A. Zeeb {
7109a7c19b8aSBjoern A. Zeeb 
7110a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
7111a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
7112a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7113a7c19b8aSBjoern A. Zeeb 
7114a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7115a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7116a7c19b8aSBjoern A. Zeeb 
7117a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
7118a7c19b8aSBjoern A. Zeeb 
7119a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7120a7c19b8aSBjoern A. Zeeb }
7121a7c19b8aSBjoern A. Zeeb 
71226b4cac81SBjoern A. Zeeb uint32_t
71236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
71246b4cac81SBjoern A. Zeeb     enum nl80211_band band)
71256b4cac81SBjoern A. Zeeb {
71266b4cac81SBjoern A. Zeeb 
71276b4cac81SBjoern A. Zeeb 	switch (band) {
71286b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
71296b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
71306b4cac81SBjoern A. Zeeb 		break;
71316b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
71326b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
71336b4cac81SBjoern A. Zeeb 		break;
71346b4cac81SBjoern A. Zeeb 	default:
71356b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
71366b4cac81SBjoern A. Zeeb 		break;
71376b4cac81SBjoern A. Zeeb 	}
71386b4cac81SBjoern A. Zeeb 
71396b4cac81SBjoern A. Zeeb 	return (0);
71406b4cac81SBjoern A. Zeeb }
71416b4cac81SBjoern A. Zeeb 
71426b4cac81SBjoern A. Zeeb uint32_t
71436b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
71446b4cac81SBjoern A. Zeeb {
71456b4cac81SBjoern A. Zeeb 
71466b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
71476b4cac81SBjoern A. Zeeb }
71486b4cac81SBjoern A. Zeeb 
7149ac867c20SBjoern A. Zeeb #if 0
71506b4cac81SBjoern A. Zeeb static struct lkpi_sta *
71516b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
71526b4cac81SBjoern A. Zeeb {
71536b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
71546b4cac81SBjoern A. Zeeb 
7155a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7156a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
71576b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
7158a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
71596b4cac81SBjoern A. Zeeb 			return (lsta);
71606b4cac81SBjoern A. Zeeb 		}
71616b4cac81SBjoern A. Zeeb 	}
7162a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
71636b4cac81SBjoern A. Zeeb 
71646b4cac81SBjoern A. Zeeb 	return (NULL);
71656b4cac81SBjoern A. Zeeb }
7166ac867c20SBjoern A. Zeeb #endif
71676b4cac81SBjoern A. Zeeb 
71686b4cac81SBjoern A. Zeeb struct ieee80211_sta *
71696b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
71706b4cac81SBjoern A. Zeeb {
71716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7172a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71736b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
71746b4cac81SBjoern A. Zeeb 
71756b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71766b4cac81SBjoern A. Zeeb 
7177a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7178a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
71796b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
71806b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7181a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
71826b4cac81SBjoern A. Zeeb 			return (sta);
71836b4cac81SBjoern A. Zeeb 		}
71846b4cac81SBjoern A. Zeeb 	}
7185a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
71866b4cac81SBjoern A. Zeeb 	return (NULL);
71876b4cac81SBjoern A. Zeeb }
71886b4cac81SBjoern A. Zeeb 
71896b4cac81SBjoern A. Zeeb struct ieee80211_sta *
71902e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
71912e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
71926b4cac81SBjoern A. Zeeb {
71936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71946b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71956b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
71976b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
71986b4cac81SBjoern A. Zeeb 
71996b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72006b4cac81SBjoern A. Zeeb 	sta = NULL;
72016b4cac81SBjoern A. Zeeb 
72028891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72036b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72046b4cac81SBjoern A. Zeeb 
72056b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
72066b4cac81SBjoern A. Zeeb 
72076b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72086b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
72096b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
72106b4cac81SBjoern A. Zeeb 			continue;
72116b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
72126b4cac81SBjoern A. Zeeb 		if (sta != NULL)
72136b4cac81SBjoern A. Zeeb 			break;
72146b4cac81SBjoern A. Zeeb 	}
72158891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72166b4cac81SBjoern A. Zeeb 
72176b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
72186b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
72196b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
72206b4cac81SBjoern A. Zeeb 			return (NULL);
72216b4cac81SBjoern A. Zeeb 	}
72226b4cac81SBjoern A. Zeeb 
72236b4cac81SBjoern A. Zeeb 	return (sta);
72246b4cac81SBjoern A. Zeeb }
72256b4cac81SBjoern A. Zeeb 
72266b4cac81SBjoern A. Zeeb struct sk_buff *
72276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
72286b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
72296b4cac81SBjoern A. Zeeb {
72306b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
72310cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72326b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
72336b4cac81SBjoern A. Zeeb 
72340cbcfa19SBjoern A. Zeeb 	skb = NULL;
72356b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
72366b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
72376b4cac81SBjoern A. Zeeb 
72380cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
72390cbcfa19SBjoern A. Zeeb 		goto stopped;
72400cbcfa19SBjoern A. Zeeb 
72410cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
72420cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
72430cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
72440cbcfa19SBjoern A. Zeeb 		goto stopped;
72450cbcfa19SBjoern A. Zeeb 	}
72460cbcfa19SBjoern A. Zeeb 
7247eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
7248eac3646fSBjoern A. Zeeb 
7249eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
72506b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
7251eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
72526b4cac81SBjoern A. Zeeb 
72530cbcfa19SBjoern A. Zeeb stopped:
72546b4cac81SBjoern A. Zeeb 	return (skb);
72556b4cac81SBjoern A. Zeeb }
72566b4cac81SBjoern A. Zeeb 
72576b4cac81SBjoern A. Zeeb void
72586b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
725986220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
72606b4cac81SBjoern A. Zeeb {
72616b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
72626b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
726386220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
72646b4cac81SBjoern A. Zeeb 
72656b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
72666b4cac81SBjoern A. Zeeb 
72676b4cac81SBjoern A. Zeeb 	fc = bc = 0;
7268eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
72696b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
72706b4cac81SBjoern A. Zeeb 		fc++;
72716b4cac81SBjoern A. Zeeb 		bc += skb->len;
72726b4cac81SBjoern A. Zeeb 	}
7273eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
72746b4cac81SBjoern A. Zeeb 	if (frame_cnt)
72756b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
72766b4cac81SBjoern A. Zeeb 	if (byte_cnt)
72776b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
72786b4cac81SBjoern A. Zeeb 
72796b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
72806b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
72816b4cac81SBjoern A. Zeeb 	IMPROVE();
72826b4cac81SBjoern A. Zeeb }
72836b4cac81SBjoern A. Zeeb 
72846b4cac81SBjoern A. Zeeb /*
72856b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
72866b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
72876b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
72886b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
72896b4cac81SBjoern A. Zeeb  */
7290a8397571SBjoern A. Zeeb static void
7291a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
72926b4cac81SBjoern A. Zeeb     int status)
72936b4cac81SBjoern A. Zeeb {
72946b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
72956b4cac81SBjoern A. Zeeb 	struct mbuf *m;
72966b4cac81SBjoern A. Zeeb 
72976b4cac81SBjoern A. Zeeb 	m = skb->m;
72986b4cac81SBjoern A. Zeeb 	skb->m = NULL;
72996b4cac81SBjoern A. Zeeb 
73006b4cac81SBjoern A. Zeeb 	if (m != NULL) {
73016b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
73026b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
73036b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
73046b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
73056b4cac81SBjoern A. Zeeb 	}
7306a8397571SBjoern A. Zeeb }
73076b4cac81SBjoern A. Zeeb 
7308a8397571SBjoern A. Zeeb void
7309a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7310a8397571SBjoern A. Zeeb     int status)
7311a8397571SBjoern A. Zeeb {
7312a8397571SBjoern A. Zeeb 
7313a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
73146b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
73156b4cac81SBjoern A. Zeeb }
73166b4cac81SBjoern A. Zeeb 
7317383b3e8fSBjoern A. Zeeb void
7318a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7319a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
7320383b3e8fSBjoern A. Zeeb {
7321a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
7322383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
7323383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
7324383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
7325383b3e8fSBjoern A. Zeeb 	int status;
7326383b3e8fSBjoern A. Zeeb 
7327a8397571SBjoern A. Zeeb 	skb = txstat->skb;
7328383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
7329383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
7330383b3e8fSBjoern A. Zeeb 
7331383b3e8fSBjoern A. Zeeb 		m = skb->m;
7332383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
7333383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
7334383b3e8fSBjoern A. Zeeb 	} else {
7335383b3e8fSBjoern A. Zeeb 		ni = NULL;
7336383b3e8fSBjoern A. Zeeb 	}
7337383b3e8fSBjoern A. Zeeb 
7338a8397571SBjoern A. Zeeb 	info = txstat->info;
7339383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7340383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
7341383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7342383b3e8fSBjoern A. Zeeb 	} else {
7343383b3e8fSBjoern A. Zeeb 		status = 1;
7344383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7345383b3e8fSBjoern A. Zeeb 	}
7346383b3e8fSBjoern A. Zeeb 
7347383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
7348383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
7349383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7350383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
7351383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7352383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7353383b3e8fSBjoern A. Zeeb 		}
7354383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
7355a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7356383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
7357383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7358383b3e8fSBjoern A. Zeeb #endif
7359adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7360383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7361383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7362383b3e8fSBjoern A. Zeeb 		}
7363383b3e8fSBjoern A. Zeeb 
7364d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7365383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
736646de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7367383b3e8fSBjoern A. Zeeb 
7368383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7369383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7370d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
737146de4d9fSAdrian Chadd 			    txs.long_retries);
7372383b3e8fSBjoern A. Zeeb 		}
7373383b3e8fSBjoern A. Zeeb #endif
7374383b3e8fSBjoern A. Zeeb 	}
7375383b3e8fSBjoern A. Zeeb 
7376383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7377383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7378383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7379383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7380383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7381383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7382adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7383383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7384383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7385383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7386383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7387383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7388383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7389383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7390383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7391383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7392383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7393383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7394383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7395383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7396adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7397383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7398383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7399383b3e8fSBjoern A. Zeeb #endif
7400383b3e8fSBjoern A. Zeeb 
7401a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7402a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7403a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7404a8397571SBjoern A. Zeeb 	} else {
7405383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7406383b3e8fSBjoern A. Zeeb 	}
7407a8397571SBjoern A. Zeeb }
7408a8397571SBjoern A. Zeeb 
7409a8397571SBjoern A. Zeeb void
7410a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7411a8397571SBjoern A. Zeeb {
7412a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7413a8397571SBjoern A. Zeeb 
7414a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7415a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7416a8397571SBjoern A. Zeeb 	status.skb = skb;
7417a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7418a8397571SBjoern A. Zeeb 
7419a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7420a8397571SBjoern A. Zeeb }
7421383b3e8fSBjoern A. Zeeb 
74226b4cac81SBjoern A. Zeeb /*
74236b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
74246b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
74256b4cac81SBjoern A. Zeeb  * mbufs this should go away.
74266b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
74276b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
74286b4cac81SBjoern A. Zeeb  */
74296b4cac81SBjoern A. Zeeb static void
74306b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
74316b4cac81SBjoern A. Zeeb {
74326b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
74336b4cac81SBjoern A. Zeeb 	struct mbuf *m;
74346b4cac81SBjoern A. Zeeb 
74356b4cac81SBjoern A. Zeeb 	if (p == NULL)
74366b4cac81SBjoern A. Zeeb 		return;
74376b4cac81SBjoern A. Zeeb 
74386b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
74396b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
74406b4cac81SBjoern A. Zeeb 
74416b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
74426b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
74436b4cac81SBjoern A. Zeeb 	if (ni != NULL)
74446b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
74456b4cac81SBjoern A. Zeeb 	m_freem(m);
74466b4cac81SBjoern A. Zeeb }
74476b4cac81SBjoern A. Zeeb 
74486b4cac81SBjoern A. Zeeb void
74496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
74506b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
74516b4cac81SBjoern A. Zeeb {
74526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74536b4cac81SBjoern A. Zeeb 
74546b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
74556b4cac81SBjoern A. Zeeb 	IMPROVE();
74566b4cac81SBjoern A. Zeeb 
74576b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74586b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
74596b4cac81SBjoern A. Zeeb }
74606b4cac81SBjoern A. Zeeb 
74616b4cac81SBjoern A. Zeeb void
74626b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
74636b4cac81SBjoern A. Zeeb     struct work_struct *w)
74646b4cac81SBjoern A. Zeeb {
74656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74666b4cac81SBjoern A. Zeeb 
74676b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
74686b4cac81SBjoern A. Zeeb 	IMPROVE();
74696b4cac81SBjoern A. Zeeb 
74706b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74716b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
74726b4cac81SBjoern A. Zeeb }
74736b4cac81SBjoern A. Zeeb 
74746b4cac81SBjoern A. Zeeb struct sk_buff *
7475ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7476ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7477ade774b1SBjoern A. Zeeb {
7478ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7479ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7480ade774b1SBjoern A. Zeeb 	uint8_t *p;
7481ade774b1SBjoern A. Zeeb 	size_t len;
7482ade774b1SBjoern A. Zeeb 
7483ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7484ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7485ade774b1SBjoern A. Zeeb 
7486ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7487ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7488ade774b1SBjoern A. Zeeb 		return (NULL);
7489ade774b1SBjoern A. Zeeb 
7490ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7491ade774b1SBjoern A. Zeeb 
7492ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7493ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7494ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7495ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7496ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7497ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7498ade774b1SBjoern A. Zeeb 
7499ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7500ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7501ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7502ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7503ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7504ade774b1SBjoern A. Zeeb 
7505ade774b1SBjoern A. Zeeb 	return (skb);
7506ade774b1SBjoern A. Zeeb }
7507ade774b1SBjoern A. Zeeb 
7508ade774b1SBjoern A. Zeeb struct sk_buff *
75096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
75106b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
75116b4cac81SBjoern A. Zeeb {
75126b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75136b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
75146b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
75156b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
75166b4cac81SBjoern A. Zeeb 	uint16_t v;
75176b4cac81SBjoern A. Zeeb 
75186b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
75196b4cac81SBjoern A. Zeeb 	if (skb == NULL)
75206b4cac81SBjoern A. Zeeb 		return (NULL);
75216b4cac81SBjoern A. Zeeb 
75226b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
75236b4cac81SBjoern A. Zeeb 
75246b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75256b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
75266b4cac81SBjoern A. Zeeb 
75276b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
75286b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
75296b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7530616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
75316b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
75326b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
75336b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
75346b4cac81SBjoern A. Zeeb 
75356b4cac81SBjoern A. Zeeb 	return (skb);
75366b4cac81SBjoern A. Zeeb }
75376b4cac81SBjoern A. Zeeb 
75386b4cac81SBjoern A. Zeeb struct sk_buff *
75396b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7540adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
75416b4cac81SBjoern A. Zeeb {
75426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75436b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
75446b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
75456b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
75466b4cac81SBjoern A. Zeeb 
7547adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7548adff403fSBjoern A. Zeeb 
75496b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
75506b4cac81SBjoern A. Zeeb 	if (skb == NULL)
75516b4cac81SBjoern A. Zeeb 		return (NULL);
75526b4cac81SBjoern A. Zeeb 
75536b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
75546b4cac81SBjoern A. Zeeb 
75556b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75566b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
75576b4cac81SBjoern A. Zeeb 
75586b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
75596b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
75606b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
75616b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
75626b4cac81SBjoern A. Zeeb 
75636b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
75646b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
75656b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
75666b4cac81SBjoern A. Zeeb 
75676b4cac81SBjoern A. Zeeb 	return (skb);
75686b4cac81SBjoern A. Zeeb }
75696b4cac81SBjoern A. Zeeb 
75706b4cac81SBjoern A. Zeeb struct wireless_dev *
75716b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
75726b4cac81SBjoern A. Zeeb {
75736b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75746b4cac81SBjoern A. Zeeb 
75756b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75766b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
75776b4cac81SBjoern A. Zeeb }
75786b4cac81SBjoern A. Zeeb 
75796b4cac81SBjoern A. Zeeb void
75806b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
75816b4cac81SBjoern A. Zeeb {
75826b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75836b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
75846b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
75856b4cac81SBjoern A. Zeeb 	int arg;
75866b4cac81SBjoern A. Zeeb 
75876b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75886b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
75896b4cac81SBjoern A. Zeeb 
75906b4cac81SBjoern A. Zeeb 	/*
7591f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
75926b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
75936b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
75946b4cac81SBjoern A. Zeeb 	 */
7595f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7596bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
75976b4cac81SBjoern A. Zeeb 
7598018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7599018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
76006b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
76016b4cac81SBjoern A. Zeeb }
76026b4cac81SBjoern A. Zeeb 
7603bb81db90SBjoern A. Zeeb void
7604bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7605bb81db90SBjoern A. Zeeb {
7606bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7607bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7608bb81db90SBjoern A. Zeeb 
7609bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7610bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7611bb81db90SBjoern A. Zeeb 
7612bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7613bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
76143540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7615bb81db90SBjoern A. Zeeb }
7616bb81db90SBjoern A. Zeeb 
76175edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
76185edde07cSBjoern A. Zeeb 
76195a9a0d78SBjoern A. Zeeb void
76205a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
76215a9a0d78SBjoern A. Zeeb {
76225a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
76235a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76245a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
76255a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
76265a9a0d78SBjoern A. Zeeb 
76275a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
76285a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
76295a9a0d78SBjoern A. Zeeb 
76305a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
76315a9a0d78SBjoern A. Zeeb 
76325a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
76335a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
76345a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
76355a9a0d78SBjoern A. Zeeb 	else
76365a9a0d78SBjoern A. Zeeb 		ac_count = 1;
76375a9a0d78SBjoern A. Zeeb 
76385a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
76395a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
76405a9a0d78SBjoern A. Zeeb 
76415a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
76425a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
76435a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
76445a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
76450d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
76465a9a0d78SBjoern A. Zeeb 				/*
76475a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
76485a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
76495a9a0d78SBjoern A. Zeeb 				 */
76500d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
76510d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
76525a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
76535a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
76545a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
76555a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
76560d2cb6a6SBjoern A. Zeeb #endif
76575a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
76585a9a0d78SBjoern A. Zeeb 			}
76595a9a0d78SBjoern A. Zeeb 		}
76605a9a0d78SBjoern A. Zeeb 	}
76615a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
76625a9a0d78SBjoern A. Zeeb }
76635a9a0d78SBjoern A. Zeeb 
76645a9a0d78SBjoern A. Zeeb void
76655a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
76665a9a0d78SBjoern A. Zeeb {
76675a9a0d78SBjoern A. Zeeb 	int i;
76685a9a0d78SBjoern A. Zeeb 
76695a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
76705a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
76715a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
76725a9a0d78SBjoern A. Zeeb }
76735a9a0d78SBjoern A. Zeeb 
76745a9a0d78SBjoern A. Zeeb 
76755a9a0d78SBjoern A. Zeeb static void
76765a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
76775a9a0d78SBjoern A. Zeeb {
76785a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
76795a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76805a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
76815a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
76825a9a0d78SBjoern A. Zeeb 
76835a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
76845a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
76855a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
76865a9a0d78SBjoern A. Zeeb 	else
76875a9a0d78SBjoern A. Zeeb 		ac_count = 1;
76885a9a0d78SBjoern A. Zeeb 
76895a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
76905a9a0d78SBjoern A. Zeeb 
76915a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
76925a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
76935a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
76945a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
76955a9a0d78SBjoern A. Zeeb 
76965a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
76975a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
76985a9a0d78SBjoern A. Zeeb 
76995a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
77005a9a0d78SBjoern A. Zeeb 
77015a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
77025a9a0d78SBjoern A. Zeeb 
77030d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
77045a9a0d78SBjoern A. Zeeb 				/*
77055a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
77065a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
77075a9a0d78SBjoern A. Zeeb 				 */
77080d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
77090d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
77105a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
77115a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
77125a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
77135a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
77140d2cb6a6SBjoern A. Zeeb #endif
77155a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
77165a9a0d78SBjoern A. Zeeb 
7717a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7718a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
77195a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
77205a9a0d78SBjoern A. Zeeb 
77215a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
77225a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
77235a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
77245a9a0d78SBjoern A. Zeeb 
77255a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
77265a9a0d78SBjoern A. Zeeb 							continue;
77275a9a0d78SBjoern A. Zeeb 
77285a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
77295a9a0d78SBjoern A. Zeeb 							continue;
77305a9a0d78SBjoern A. Zeeb 
77315a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
77325a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
77335a9a0d78SBjoern A. Zeeb 							continue;
77345a9a0d78SBjoern A. Zeeb 
77355a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
77365a9a0d78SBjoern A. Zeeb 
77375a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
77385a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
77395a9a0d78SBjoern A. Zeeb 					}
77405a9a0d78SBjoern A. Zeeb 				}
7741a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
77425a9a0d78SBjoern A. Zeeb 			}
77435a9a0d78SBjoern A. Zeeb 		}
77445a9a0d78SBjoern A. Zeeb 	}
77455a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
77465a9a0d78SBjoern A. Zeeb }
77475a9a0d78SBjoern A. Zeeb 
77485a9a0d78SBjoern A. Zeeb void
77495a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
77505a9a0d78SBjoern A. Zeeb {
77515a9a0d78SBjoern A. Zeeb 	int i;
77525a9a0d78SBjoern A. Zeeb 
77535a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
77545a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
77555a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
77565a9a0d78SBjoern A. Zeeb }
77575a9a0d78SBjoern A. Zeeb 
77585a9a0d78SBjoern A. Zeeb void
77595a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
77605a9a0d78SBjoern A. Zeeb {
77615a9a0d78SBjoern A. Zeeb 
77625a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
77635a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
77645a9a0d78SBjoern A. Zeeb 
77655a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
77665a9a0d78SBjoern A. Zeeb }
77675a9a0d78SBjoern A. Zeeb 
77685a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
77695a9a0d78SBjoern A. Zeeb void
77705a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
77715a9a0d78SBjoern A. Zeeb {
77725a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77735a9a0d78SBjoern A. Zeeb 
77745a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
77755a9a0d78SBjoern A. Zeeb 
77765a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
77775a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
77785a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
77795a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
77805a9a0d78SBjoern A. Zeeb }
77815a9a0d78SBjoern A. Zeeb 
77825a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
77835a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
77845a9a0d78SBjoern A. Zeeb {
77855a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77865a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
77875a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
77885a9a0d78SBjoern A. Zeeb 
77895a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
77905a9a0d78SBjoern A. Zeeb 	txq = NULL;
77915a9a0d78SBjoern A. Zeeb 
77925a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
77935a9a0d78SBjoern A. Zeeb 
77945a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
77955a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
77965a9a0d78SBjoern A. Zeeb 		goto out;
77975a9a0d78SBjoern A. Zeeb 
77985a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
77995a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
78005a9a0d78SBjoern A. Zeeb 		goto out;
78015a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
78025a9a0d78SBjoern A. Zeeb 		goto out;
78035a9a0d78SBjoern A. Zeeb 
78045a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
78055a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
78065a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
78075a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
78085a9a0d78SBjoern A. Zeeb 
78095a9a0d78SBjoern A. Zeeb out:
78105a9a0d78SBjoern A. Zeeb 	return (txq);
78115a9a0d78SBjoern A. Zeeb }
78125a9a0d78SBjoern A. Zeeb 
78135a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
78145a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
78155a9a0d78SBjoern A. Zeeb {
78165a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
78175a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7818eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
78195a9a0d78SBjoern A. Zeeb 
78205a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
78215a9a0d78SBjoern A. Zeeb 
78225a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
78235a9a0d78SBjoern A. Zeeb 
78245a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7825eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7826eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7827eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7828eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
78295a9a0d78SBjoern A. Zeeb 		goto out;
78305a9a0d78SBjoern A. Zeeb 
783141b746e0SAustin Shafer 	/*
783241b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
783341b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
783441b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
783541b746e0SAustin Shafer 	 */
783641b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
78375a9a0d78SBjoern A. Zeeb 		goto out;
78385a9a0d78SBjoern A. Zeeb 
78395a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
78405a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
78415a9a0d78SBjoern A. Zeeb out:
78425a9a0d78SBjoern A. Zeeb 	return;
78435a9a0d78SBjoern A. Zeeb }
78445a9a0d78SBjoern A. Zeeb 
7845eac3646fSBjoern A. Zeeb void
7846eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7847eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7848eac3646fSBjoern A. Zeeb {
7849eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7850eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7851eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7852eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7853eac3646fSBjoern A. Zeeb 
7854eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7855eac3646fSBjoern A. Zeeb 
7856eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7857eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7858eac3646fSBjoern A. Zeeb 	do {
7859eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7860eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7861eac3646fSBjoern A. Zeeb 			break;
7862eac3646fSBjoern A. Zeeb 
7863eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7864eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7865eac3646fSBjoern A. Zeeb 		do {
7866eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7867eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7868eac3646fSBjoern A. Zeeb 				break;
7869eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7870eac3646fSBjoern A. Zeeb 		} while(1);
7871eac3646fSBjoern A. Zeeb 
7872eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7873eac3646fSBjoern A. Zeeb 	} while (1);
7874eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7875eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7876eac3646fSBjoern A. Zeeb }
7877eac3646fSBjoern A. Zeeb 
78785a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
78795a9a0d78SBjoern A. Zeeb 
78805edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
78815edde07cSBjoern A. Zeeb 	u_int refcnt;
78825edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
78835edde07cSBjoern A. Zeeb };
78845edde07cSBjoern A. Zeeb 
78855edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
78865edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
78875edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
78885edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
78895edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
78905edde07cSBjoern A. Zeeb 	size_t ssid_len;
78915edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
78925edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
78935edde07cSBjoern A. Zeeb 
78945edde07cSBjoern A. Zeeb 	/*
78955edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
78965edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
78975edde07cSBjoern A. Zeeb 	 */
78985edde07cSBjoern A. Zeeb 	bool match;
78995edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
79005edde07cSBjoern A. Zeeb };
79015edde07cSBjoern A. Zeeb 
79025edde07cSBjoern A. Zeeb static void
79035edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
79045edde07cSBjoern A. Zeeb {
79055edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
79065edde07cSBjoern A. Zeeb 	size_t ielen;
79075edde07cSBjoern A. Zeeb 
79085edde07cSBjoern A. Zeeb 	lookup = arg;
79095edde07cSBjoern A. Zeeb 
79105edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
79115edde07cSBjoern A. Zeeb 	if (lookup->match)
79125edde07cSBjoern A. Zeeb 		return;
79135edde07cSBjoern A. Zeeb 
79145edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
79155edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
79165edde07cSBjoern A. Zeeb 		return;
79175edde07cSBjoern A. Zeeb 
79185edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
79195edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
79205edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
79215edde07cSBjoern A. Zeeb 		return;
79225edde07cSBjoern A. Zeeb 	}
79235edde07cSBjoern A. Zeeb 
79245edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
79255edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
79265edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
79275edde07cSBjoern A. Zeeb 		return;
79285edde07cSBjoern A. Zeeb 	}
79295edde07cSBjoern A. Zeeb 
79305edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
79315edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
79325edde07cSBjoern A. Zeeb 
79335edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
79345edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
79355edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
79365edde07cSBjoern A. Zeeb 			return;
79375edde07cSBjoern A. Zeeb 	}
79385edde07cSBjoern A. Zeeb 
79395edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
79405edde07cSBjoern A. Zeeb 		return;
79415edde07cSBjoern A. Zeeb 
79425edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
79435edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
79445edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
79455edde07cSBjoern A. Zeeb 			return;
79465edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
79475edde07cSBjoern A. Zeeb 			return;
79485edde07cSBjoern A. Zeeb 	}
79495edde07cSBjoern A. Zeeb 
79505edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
79515edde07cSBjoern A. Zeeb 
79525edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
79535edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
79545edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
79555edde07cSBjoern A. Zeeb 		return;
79565edde07cSBjoern A. Zeeb 
79575edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
79585edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
79595edde07cSBjoern A. Zeeb 	if (ielen)
79605edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
79615edde07cSBjoern A. Zeeb 
79625edde07cSBjoern A. Zeeb 	lookup->match = true;
79635edde07cSBjoern A. Zeeb }
79645edde07cSBjoern A. Zeeb 
79655edde07cSBjoern A. Zeeb struct cfg80211_bss *
79665edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
79675edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
79685edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
79695edde07cSBjoern A. Zeeb {
79705edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
79715edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
79725edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
79735edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
79745edde07cSBjoern A. Zeeb 
79755edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
79765edde07cSBjoern A. Zeeb 
79775edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
79785edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
79795edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
79805edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
79815edde07cSBjoern A. Zeeb 		return (NULL);
79825edde07cSBjoern A. Zeeb 	}
79835edde07cSBjoern A. Zeeb 
79845edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
79855edde07cSBjoern A. Zeeb 	lookup.chan = chan;
79865edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
79875edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
79885edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
79895edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
79905edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
79915edde07cSBjoern A. Zeeb 	lookup.match = false;
79925edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
79935edde07cSBjoern A. Zeeb 
79945edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
79955edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
79965edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
79975edde07cSBjoern A. Zeeb 	if (!lookup.match) {
79985edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
79995edde07cSBjoern A. Zeeb 		return (NULL);
80005edde07cSBjoern A. Zeeb 	}
80015edde07cSBjoern A. Zeeb 
80025edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
80035edde07cSBjoern A. Zeeb 	return (&lbss->bss);
80045edde07cSBjoern A. Zeeb }
80055edde07cSBjoern A. Zeeb 
80065edde07cSBjoern A. Zeeb void
80075edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
80085edde07cSBjoern A. Zeeb {
80095edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
80105edde07cSBjoern A. Zeeb 
80115edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
80125edde07cSBjoern A. Zeeb 
80135edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
80145edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
80155edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
80165edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
80175edde07cSBjoern A. Zeeb 	}
80185edde07cSBjoern A. Zeeb }
80195edde07cSBjoern A. Zeeb 
80205edde07cSBjoern A. Zeeb void
80215edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
80225edde07cSBjoern A. Zeeb {
80235edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
80245edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
80255edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
80265edde07cSBjoern A. Zeeb 
80275edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
80285edde07cSBjoern A. Zeeb 	ic = lhw->ic;
80295edde07cSBjoern A. Zeeb 
80305edde07cSBjoern A. Zeeb 	/*
80315edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
80325edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
80335edde07cSBjoern A. Zeeb 	 */
80345edde07cSBjoern A. Zeeb 	if (ic == NULL ||
80355edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
80365edde07cSBjoern A. Zeeb 		return;
80375edde07cSBjoern A. Zeeb 
80385edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
80395edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
80405edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
80415edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
80425edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
80435edde07cSBjoern A. Zeeb }
80445edde07cSBjoern A. Zeeb 
80455edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
80465edde07cSBjoern A. Zeeb 
8047ac1d519cSBjoern A. Zeeb /*
8048ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
8049ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
8050ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
8051ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
8052ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
8053ac1d519cSBjoern A. Zeeb  */
8054ac1d519cSBjoern A. Zeeb 
8055ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8056ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
8057ac1d519cSBjoern A. Zeeb {
8058ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
8059ac1d519cSBjoern A. Zeeb 	uint32_t changed;
8060ac1d519cSBjoern A. Zeeb 	int error;
8061ac1d519cSBjoern A. Zeeb 
8062ac1d519cSBjoern A. Zeeb 	changed = 0;
8063ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
8064ac1d519cSBjoern A. Zeeb 		cd = NULL;
8065ac1d519cSBjoern A. Zeeb 	else
8066ac1d519cSBjoern A. Zeeb 		cd = &new->def;
8067ac1d519cSBjoern A. Zeeb 
8068ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
8069ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
8070ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
8071ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8072ac1d519cSBjoern A. Zeeb 	}
8073ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8074ac1d519cSBjoern A. Zeeb 
8075ac1d519cSBjoern A. Zeeb 	if (changed == 0)
8076ac1d519cSBjoern A. Zeeb 		return (0);
8077ac1d519cSBjoern A. Zeeb 
8078ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
8079ac1d519cSBjoern A. Zeeb 	return (error);
8080ac1d519cSBjoern A. Zeeb }
8081ac1d519cSBjoern A. Zeeb 
8082ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8083ac1d519cSBjoern A. Zeeb 
80846b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
80856b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
80866b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8087