xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 98e55905ab8ac34a0b88a1863c1b88e6fe3a8c13)
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 
120611db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
120711db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
120811db70b6SBjoern A. Zeeb 		if (err != 0) {
120911db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
121011db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
121111db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
121211db70b6SBjoern A. Zeeb 			error++;
121311db70b6SBjoern A. Zeeb 
121411db70b6SBjoern A. Zeeb 			/*
121511db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
121611db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
121711db70b6SBjoern A. Zeeb 			 * crash (firmware).
121811db70b6SBjoern A. Zeeb 			 */
121911db70b6SBjoern A. Zeeb 			continue;
122011db70b6SBjoern A. Zeeb 		}
122111db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
122211db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
122311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
122411db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
122511db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
122611db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
122711db70b6SBjoern A. Zeeb #endif
122811db70b6SBjoern A. Zeeb 
122911db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
123011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
123111db70b6SBjoern A. Zeeb 	}
123211db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
123311db70b6SBjoern A. Zeeb 	return (error);
123411db70b6SBjoern A. Zeeb }
123511db70b6SBjoern A. Zeeb 
123611db70b6SBjoern A. Zeeb static int
123711db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
12386b4cac81SBjoern A. Zeeb {
12396b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
12406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
124311db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
12446b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
12456b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
12466b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
12476b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
12486b4cac81SBjoern A. Zeeb 	int error;
12496b4cac81SBjoern A. Zeeb 
12506b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
12516b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
12526b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
12536b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
12546b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
12556b4cac81SBjoern A. Zeeb 
125611db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
125711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
125811db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
125911db70b6SBjoern A. Zeeb 		return (0);
126011db70b6SBjoern A. Zeeb 	}
126111db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
126211db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
126311db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
126411db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
126511db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
126611db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
126711db70b6SBjoern A. Zeeb 		return (0);
126811db70b6SBjoern A. Zeeb 	}
126911db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
127011db70b6SBjoern A. Zeeb 
127111db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
127211db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
127311db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
127411db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
127511db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
127611db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
127711db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
127811db70b6SBjoern A. Zeeb #endif
127911db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
128011db70b6SBjoern A. Zeeb 		return (1);
128111db70b6SBjoern A. Zeeb 	}
128211db70b6SBjoern A. Zeeb 
128311db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
128411db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
128511db70b6SBjoern A. Zeeb 	if (kc == NULL) {
128611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
128711db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
128811db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
128911db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
129011db70b6SBjoern A. Zeeb #endif
129111db70b6SBjoern A. Zeeb 		error = 1;
129211db70b6SBjoern A. Zeeb 		goto out;
129311db70b6SBjoern A. Zeeb 	}
129411db70b6SBjoern A. Zeeb 
129511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
129611db70b6SBjoern A. Zeeb 	if (error != 0) {
129711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
129811db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
129911db70b6SBjoern A. Zeeb 		error = 0;
130011db70b6SBjoern A. Zeeb 		goto out;
130111db70b6SBjoern A. Zeeb 	}
130211db70b6SBjoern A. Zeeb 
130311db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
130411db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
130511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
130611db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
130711db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
130811db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
130911db70b6SBjoern A. Zeeb #endif
131011db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
131111db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
131211db70b6SBjoern A. Zeeb 	error = 1;
131311db70b6SBjoern A. Zeeb out:
131411db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
131511db70b6SBjoern A. Zeeb 	return (error);
131611db70b6SBjoern A. Zeeb }
131711db70b6SBjoern A. Zeeb 
131811db70b6SBjoern A. Zeeb static int
131911db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
132011db70b6SBjoern A. Zeeb {
132111db70b6SBjoern A. Zeeb 
132211db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
132311db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
132411db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
132511db70b6SBjoern A. Zeeb }
132611db70b6SBjoern A. Zeeb 
132711db70b6SBjoern A. Zeeb static int
132811db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
132911db70b6SBjoern A. Zeeb {
133011db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
133111db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
133211db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
133311db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
133411db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
133511db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
133611db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
133711db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
133811db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
133911db70b6SBjoern A. Zeeb 	uint32_t lcipher;
134011db70b6SBjoern A. Zeeb 	int error;
134111db70b6SBjoern A. Zeeb 
134211db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
134311db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
134411db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
134511db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
134611db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
134711db70b6SBjoern A. Zeeb 
134811db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
134911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
135011db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
135111db70b6SBjoern A. Zeeb 		return (0);
135211db70b6SBjoern A. Zeeb 	}
135311db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
135411db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
135511db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
135611db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
135711db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
135811db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
135911db70b6SBjoern A. Zeeb 		return (0);
136011db70b6SBjoern A. Zeeb 	}
136111db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
136211db70b6SBjoern A. Zeeb 
136311db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
136411db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
136511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
136611db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
136711db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
136811db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
136911db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
137011db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
137111db70b6SBjoern A. Zeeb 	}
137211db70b6SBjoern A. Zeeb 
137311db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
13746b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
137511db70b6SBjoern A. Zeeb 	switch (lcipher) {
137611db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
137711db70b6SBjoern A. Zeeb 		break;
137811db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
137911db70b6SBjoern A. Zeeb 	default:
138012a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
138112a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
138211db70b6SBjoern A. Zeeb 		IMPROVE();
138311db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
138411db70b6SBjoern A. Zeeb 		return (0);
138511db70b6SBjoern A. Zeeb 	}
138611db70b6SBjoern A. Zeeb 
138711db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
138811db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
138911db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
139011db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
13916b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
13926b4cac81SBjoern A. Zeeb #if 0
13936b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
13946b4cac81SBjoern A. Zeeb #endif
13956b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
13966b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
13976b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
13986b4cac81SBjoern A. Zeeb 
139911db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
140011db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
140111db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
140211db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
140311db70b6SBjoern A. Zeeb 
14046b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
14056b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
14066b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
14076b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
14086b4cac81SBjoern A. Zeeb 		break;
14096b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
14106b4cac81SBjoern A. Zeeb 	default:
141111db70b6SBjoern A. Zeeb 		/* currently UNREACH */
14126b4cac81SBjoern A. Zeeb 		IMPROVE();
141311db70b6SBjoern A. Zeeb 		break;
14146b4cac81SBjoern A. Zeeb 	};
141511db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
14166b4cac81SBjoern A. Zeeb 
141711db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
141811db70b6SBjoern A. Zeeb 	if (error != 0) {
141911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
142011db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
142111db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
142211db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
142311db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
142411db70b6SBjoern A. Zeeb 		return (0);
14256b4cac81SBjoern A. Zeeb 	}
14266b4cac81SBjoern A. Zeeb 
142711db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
142811db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
142911db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
143011db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
143111db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
143211db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
143311db70b6SBjoern A. Zeeb #endif
143411db70b6SBjoern A. Zeeb 
143511db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
14366b4cac81SBjoern A. Zeeb 	return (1);
14376b4cac81SBjoern A. Zeeb }
14386b4cac81SBjoern A. Zeeb 
14396b4cac81SBjoern A. Zeeb static  int
14406b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
14416b4cac81SBjoern A. Zeeb {
14426b4cac81SBjoern A. Zeeb 
144311db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
14446b4cac81SBjoern A. Zeeb }
1445b8dfc3ecSBjoern A. Zeeb 
1446b8dfc3ecSBjoern A. Zeeb static void
1447b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1448b8dfc3ecSBjoern A. Zeeb {
1449b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1450b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1451b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1452b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1453b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1454a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1455b8dfc3ecSBjoern A. Zeeb 
1456b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1457b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1458b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1459b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1460b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1461b8dfc3ecSBjoern A. Zeeb 
1462a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1463a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1464b8dfc3ecSBjoern A. Zeeb 
1465b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1466b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1467a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1468a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1469a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1470a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1471a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1472b8dfc3ecSBjoern A. Zeeb #endif
1473b8dfc3ecSBjoern A. Zeeb 
1474b8dfc3ecSBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
1475a6165709SBjoern A. Zeeb 	if (icislocked)
1476a6165709SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
1477a6165709SBjoern A. Zeeb 	if (ntislocked)
1478b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
1479b8dfc3ecSBjoern A. Zeeb 
1480b8dfc3ecSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
1481b8dfc3ecSBjoern A. Zeeb 
1482b8dfc3ecSBjoern A. Zeeb 	/*
1483a6165709SBjoern A. Zeeb 	 * ic/nt_unlocked could be a bool given we are under the lock and there
1484b8dfc3ecSBjoern A. Zeeb 	 * must only be a single thread.
1485b8dfc3ecSBjoern A. Zeeb 	 * In case anything in the future disturbs the order the refcnt will
1486b8dfc3ecSBjoern A. Zeeb 	 * help us catching problems a lot easier.
1487b8dfc3ecSBjoern A. Zeeb 	 */
1488a6165709SBjoern A. Zeeb 	if (icislocked)
1489a6165709SBjoern A. Zeeb 		refcount_acquire(&lvif->ic_unlocked);
1490a6165709SBjoern A. Zeeb 	if (ntislocked)
1491b8dfc3ecSBjoern A. Zeeb 		refcount_acquire(&lvif->nt_unlocked);
1492b8dfc3ecSBjoern A. Zeeb }
1493b8dfc3ecSBjoern A. Zeeb 
1494b8dfc3ecSBjoern A. Zeeb static void
1495b8dfc3ecSBjoern A. Zeeb lkpi_iv_key_update_end(struct ieee80211vap *vap)
1496b8dfc3ecSBjoern A. Zeeb {
1497b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_node_table *nt;
1498b8dfc3ecSBjoern A. Zeeb 	struct ieee80211com *ic;
1499b8dfc3ecSBjoern A. Zeeb 	struct lkpi_hw *lhw;
1500b8dfc3ecSBjoern A. Zeeb 	struct ieee80211_hw *hw;
1501b8dfc3ecSBjoern A. Zeeb 	struct lkpi_vif *lvif;
1502a6165709SBjoern A. Zeeb 	bool icislocked, ntislocked;
1503b8dfc3ecSBjoern A. Zeeb 
1504b8dfc3ecSBjoern A. Zeeb 	ic = vap->iv_ic;
1505b8dfc3ecSBjoern A. Zeeb 	lhw = ic->ic_softc;
1506b8dfc3ecSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1507b8dfc3ecSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
1508b8dfc3ecSBjoern A. Zeeb 	nt = &ic->ic_sta;
1509b8dfc3ecSBjoern A. Zeeb 
1510a6165709SBjoern A. Zeeb 	icislocked = IEEE80211_IS_LOCKED(ic);
1511a6165709SBjoern A. Zeeb 	MPASS(!icislocked);
1512a6165709SBjoern A. Zeeb 	ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1513a6165709SBjoern A. Zeeb 	MPASS(!ntislocked);
1514b8dfc3ecSBjoern A. Zeeb 
1515b8dfc3ecSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1516b8dfc3ecSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1517a6165709SBjoern A. Zeeb 		ic_printf(ic, "%s: tid %d vap %p ic %p %slocked nt %p %slocked "
1518a6165709SBjoern A. Zeeb 		    "lvif ic_unlocked %d nt_unlocked %d\n", __func__,
1519a6165709SBjoern A. Zeeb 		    curthread->td_tid, vap,
1520a6165709SBjoern A. Zeeb 		    ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1521a6165709SBjoern A. Zeeb 		    lvif->ic_unlocked, lvif->nt_unlocked);
1522b8dfc3ecSBjoern A. Zeeb #endif
1523b8dfc3ecSBjoern A. Zeeb 
1524b8dfc3ecSBjoern A. Zeeb 	/*
1525b8dfc3ecSBjoern A. Zeeb 	 * Check under lock; see comment in lkpi_iv_key_update_begin().
1526b8dfc3ecSBjoern A. Zeeb 	 * In case the refcnt gets out of sync locking in net80211 will
1527b8dfc3ecSBjoern A. Zeeb 	 * quickly barf as well (trying to unlock a lock not held).
1528b8dfc3ecSBjoern A. Zeeb 	 */
1529a6165709SBjoern A. Zeeb 	icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1530a6165709SBjoern A. Zeeb 	ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1531b8dfc3ecSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1532b8dfc3ecSBjoern A. Zeeb 
1533a6165709SBjoern A. Zeeb 	/*
1534a6165709SBjoern A. Zeeb 	 * This is inconsistent net80211 locking to be fixed one day.
1535a6165709SBjoern A. Zeeb 	 * ic before nt to avoid a LOR.
1536a6165709SBjoern A. Zeeb 	 */
1537a6165709SBjoern A. Zeeb 	if (icislocked)
1538a6165709SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
1539a6165709SBjoern A. Zeeb 	if (ntislocked)
1540b8dfc3ecSBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
1541b8dfc3ecSBjoern A. Zeeb }
15426b4cac81SBjoern A. Zeeb #endif
15436b4cac81SBjoern A. Zeeb 
15446b4cac81SBjoern A. Zeeb static u_int
15456b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15466b4cac81SBjoern A. Zeeb {
15476b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
15486b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
15496b4cac81SBjoern A. Zeeb 
15506b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
15516b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
15526b4cac81SBjoern A. Zeeb 
15536b4cac81SBjoern A. Zeeb 	mc_list = arg;
15546b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
15556b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
15566b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
15576b4cac81SBjoern A. Zeeb 			return (0);
15586b4cac81SBjoern A. Zeeb 	}
15596b4cac81SBjoern A. Zeeb 
15606b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
15616b4cac81SBjoern A. Zeeb 	if (addr == NULL)
15626b4cac81SBjoern A. Zeeb 		return (0);
15636b4cac81SBjoern A. Zeeb 
15646b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
15656b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
15666b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
15676b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
15686b4cac81SBjoern A. Zeeb 	mc_list->count++;
15696b4cac81SBjoern A. Zeeb 
15709d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
15719d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
15726b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
15736b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
15749d9ba2b7SBjoern A. Zeeb #endif
15756b4cac81SBjoern A. Zeeb 
15766b4cac81SBjoern A. Zeeb 	return (1);
15776b4cac81SBjoern A. Zeeb }
15786b4cac81SBjoern A. Zeeb 
15796b4cac81SBjoern A. Zeeb static void
15806b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
15816b4cac81SBjoern A. Zeeb {
15826b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15836b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15846b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
15856b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
15866b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
15876b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
15886b4cac81SBjoern A. Zeeb 	u64 mc;
15896b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
15906b4cac81SBjoern A. Zeeb 
15916b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
15926b4cac81SBjoern A. Zeeb 
15936b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
15946b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
15956b4cac81SBjoern A. Zeeb 		return;
15966b4cac81SBjoern A. Zeeb 
15976b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
15986b4cac81SBjoern A. Zeeb 		return;
15996b4cac81SBjoern A. Zeeb 
16006b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
16016b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
16026b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
16036b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
16046b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
16056b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
16066b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
16076b4cac81SBjoern A. Zeeb 	} else {
16086b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
16096b4cac81SBjoern A. Zeeb 	}
16106b4cac81SBjoern A. Zeeb 
16116b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16126b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
16136b4cac81SBjoern A. Zeeb 	/*
16146b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
16156b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
16166b4cac81SBjoern A. Zeeb 	 */
16176b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
16186b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
16196b4cac81SBjoern A. Zeeb 
16209d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16219d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
16226b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
16236b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
16249d9ba2b7SBjoern A. Zeeb #endif
16256b4cac81SBjoern A. Zeeb 
16266b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
16276b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
16286b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
16296b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
16306b4cac81SBjoern A. Zeeb 			mc_list.count--;
16316b4cac81SBjoern A. Zeeb 		}
16326b4cac81SBjoern A. Zeeb 	}
16336b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
16346b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
16356b4cac81SBjoern A. Zeeb }
16366b4cac81SBjoern A. Zeeb 
1637fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1638fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1639fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1640fa8f007dSBjoern A. Zeeb {
1641fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1642fa8f007dSBjoern A. Zeeb 
1643fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1644fa8f007dSBjoern A. Zeeb 
16459d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16469d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1647fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1648fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1649ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1650fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1651616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1652fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1653fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1654fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1655fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1656ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
16579d9ba2b7SBjoern A. Zeeb #endif
1658fa8f007dSBjoern A. Zeeb 
1659fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1660fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1661fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1662fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1663fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1664fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1665fa8f007dSBjoern A. Zeeb 	}
1666fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1667fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1668fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1669fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1670fa8f007dSBjoern A. Zeeb 	}
1671fa8f007dSBjoern A. Zeeb 
1672fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1673fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1674fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1675fa8f007dSBjoern A. Zeeb 
16769d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
16779d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1678fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1679fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1680ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1681fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1682616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1683fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1684fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1685fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1686fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1687ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
16889d9ba2b7SBjoern A. Zeeb #endif
1689fa8f007dSBjoern A. Zeeb 
1690fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1691fa8f007dSBjoern A. Zeeb }
1692fa8f007dSBjoern A. Zeeb 
16936b4cac81SBjoern A. Zeeb static void
16946b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
16956b4cac81SBjoern A. Zeeb {
16966b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16976b4cac81SBjoern A. Zeeb 	int error;
16988ac540d3SBjoern A. Zeeb 	bool cancel;
16996b4cac81SBjoern A. Zeeb 
17008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
17018ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
17028ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
17038ac540d3SBjoern A. Zeeb 	if (!cancel)
17046b4cac81SBjoern A. Zeeb 		return;
17056b4cac81SBjoern A. Zeeb 
17066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
17076b4cac81SBjoern A. Zeeb 
1708bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1709cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
17106b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
17116b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
1712cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
17136b4cac81SBjoern A. Zeeb 
17146b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
17158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
17168ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
17178ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
17188ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
17198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
17208ac540d3SBjoern A. Zeeb 
1721bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
17226b4cac81SBjoern A. Zeeb 
17238ac540d3SBjoern A. Zeeb 	if (cancel)
17246b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
17256b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
17266b4cac81SBjoern A. Zeeb }
17276b4cac81SBjoern A. Zeeb 
17286b4cac81SBjoern A. Zeeb static void
1729086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1730086be6a8SBjoern A. Zeeb {
1731086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1732086be6a8SBjoern A. Zeeb 	int error;
1733086be6a8SBjoern A. Zeeb 	bool old;
1734086be6a8SBjoern A. Zeeb 
1735086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1736086be6a8SBjoern A. Zeeb 	if (old == new)
1737086be6a8SBjoern A. Zeeb 		return;
1738086be6a8SBjoern A. Zeeb 
1739086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1740086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1741086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1742086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1743086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1744086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1745086be6a8SBjoern A. Zeeb 	}
1746086be6a8SBjoern A. Zeeb }
1747086be6a8SBjoern A. Zeeb 
17485a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
17496b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
17506b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
17516b4cac81SBjoern A. Zeeb {
17525a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
17535a4d2461SBjoern A. Zeeb 
17545a4d2461SBjoern A. Zeeb 	changed = 0;
17556b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1756616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
17576b4cac81SBjoern A. Zeeb 
17586b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
17596b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
17606b4cac81SBjoern A. Zeeb 
1761616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1762616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
17636b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
17646b4cac81SBjoern A. Zeeb 		IMPROVE();
1765086be6a8SBjoern A. Zeeb 
17665a4d2461SBjoern A. Zeeb 		/*
17675a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
17685a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
17695a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
17705a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
17715a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
17725a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
17735a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
17745a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
17755a4d2461SBjoern A. Zeeb 		 */
17766b4cac81SBjoern A. Zeeb 	}
17775a4d2461SBjoern A. Zeeb 
17785a4d2461SBjoern A. Zeeb 	return (changed);
17796b4cac81SBjoern A. Zeeb }
17806b4cac81SBjoern A. Zeeb 
17816b4cac81SBjoern A. Zeeb static void
17826b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
17836b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
17846b4cac81SBjoern A. Zeeb {
17856b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
17866b4cac81SBjoern A. Zeeb 	int tid;
1787eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
17886b4cac81SBjoern A. Zeeb 
17896b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
17906b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
17916b4cac81SBjoern A. Zeeb 
17926b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
17936b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
17946b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
17956b4cac81SBjoern A. Zeeb 				continue;
17966b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
17976b4cac81SBjoern A. Zeeb 			continue;
17986b4cac81SBjoern A. Zeeb 
17996b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
18006b4cac81SBjoern A. Zeeb 			continue;
18016b4cac81SBjoern A. Zeeb 
18026b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
18036b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
18046b4cac81SBjoern A. Zeeb 			continue;
18056b4cac81SBjoern A. Zeeb 
1806eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1807eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1808eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1809eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
18106b4cac81SBjoern A. Zeeb 			continue;
18116b4cac81SBjoern A. Zeeb 
18126b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
18136b4cac81SBjoern A. Zeeb 	}
18146b4cac81SBjoern A. Zeeb }
18156b4cac81SBjoern A. Zeeb 
181688665349SBjoern A. Zeeb /*
181788665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
181888665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
181988665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
182088665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
182188665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
182288665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
182388665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
182488665349SBjoern A. Zeeb  * re-used.
182588665349SBjoern A. Zeeb  */
182688665349SBjoern A. Zeeb static void
182788665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
182888665349SBjoern A. Zeeb {
1829cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
183088665349SBjoern A. Zeeb 	struct mbufq mq;
183188665349SBjoern A. Zeeb 	struct mbuf *m;
183288665349SBjoern A. Zeeb 	int len;
183388665349SBjoern A. Zeeb 
1834cd0fcf9fSBjoern A. Zeeb 	/* There is no lockdep_assert_not_held_wiphy(). */
1835cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
1836cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_not_held(&hw->wiphy->mtx);
183788665349SBjoern A. Zeeb 
183888665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
183988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
184088665349SBjoern A. Zeeb 	lsta->txq_ready = false;
184188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
184288665349SBjoern A. Zeeb 
184388665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
184488665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
184588665349SBjoern A. Zeeb 
184688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
184788665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
184888665349SBjoern A. Zeeb 	if (len <= 0) {
184988665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
185088665349SBjoern A. Zeeb 		return;
185188665349SBjoern A. Zeeb 	}
185288665349SBjoern A. Zeeb 
185388665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
185488665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
185588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
185688665349SBjoern A. Zeeb 
185788665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
185888665349SBjoern A. Zeeb 	while (m != NULL) {
185988665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
186088665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
186188665349SBjoern A. Zeeb 	}
186288665349SBjoern A. Zeeb }
186388665349SBjoern A. Zeeb 
186450d826beSBjoern A. Zeeb 
186550d826beSBjoern A. Zeeb static void
186650d826beSBjoern A. Zeeb lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
186750d826beSBjoern A. Zeeb {
186850d826beSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
1869231168c7SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
187050d826beSBjoern A. Zeeb 
1871231168c7SBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1872231168c7SBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1873231168c7SBjoern A. Zeeb 
1874231168c7SBjoern A. Zeeb 	if (chanctx_conf == NULL)
1875231168c7SBjoern A. Zeeb 		return;
1876231168c7SBjoern A. Zeeb 
187750d826beSBjoern A. Zeeb 	/* Remove vif context. */
1878231168c7SBjoern A. Zeeb 	lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
187950d826beSBjoern A. Zeeb 
188050d826beSBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, true);
188150d826beSBjoern A. Zeeb 
188250d826beSBjoern A. Zeeb 	/* Remove chan ctx. */
188350d826beSBjoern A. Zeeb 	lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1884231168c7SBjoern A. Zeeb 
1885231168c7SBjoern A. Zeeb 	/* Cleanup. */
1886231168c7SBjoern A. Zeeb 	rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
188750d826beSBjoern A. Zeeb 	lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1888a8a47a41SBjoern A. Zeeb 	list_del(&lchanctx->entry);
188950d826beSBjoern A. Zeeb 	free(lchanctx, M_LKPI80211);
189050d826beSBjoern A. Zeeb }
189150d826beSBjoern A. Zeeb 
189250d826beSBjoern A. Zeeb 
18936b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
18946b4cac81SBjoern A. Zeeb 
18956b4cac81SBjoern A. Zeeb static int
18966b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
18976b4cac81SBjoern A. Zeeb {
18986b4cac81SBjoern A. Zeeb 
18996b4cac81SBjoern A. Zeeb 	return (0);
19006b4cac81SBjoern A. Zeeb }
19016b4cac81SBjoern A. Zeeb 
19026b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
19036b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
19046b4cac81SBjoern A. Zeeb 
19056b4cac81SBjoern A. Zeeb static int
19066b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19076b4cac81SBjoern A. Zeeb {
19086b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1909c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1910d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
19116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19126b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19136b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19146b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19156b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19166b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19176b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
19186b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19196b4cac81SBjoern A. Zeeb 	uint32_t changed;
19206b4cac81SBjoern A. Zeeb 	int error;
19216b4cac81SBjoern A. Zeeb 
19222ac8a218SBjoern A. Zeeb 	/*
19232ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
19242ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
19252ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
19262ac8a218SBjoern A. Zeeb 	 */
19272ac8a218SBjoern A. Zeeb 
19282ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
19292ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
19302ac8a218SBjoern A. Zeeb 		return (EINVAL);
19312ac8a218SBjoern A. Zeeb 	}
19320936c648SBjoern A. Zeeb 	/*
19330936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
19340936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
19350936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
19360936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
19370936c648SBjoern A. Zeeb 	 */
19382ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
19392ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
19402ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
19412ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
19420936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19432ac8a218SBjoern A. Zeeb 		return (EINVAL);
19446b4cac81SBjoern A. Zeeb 	}
19456b4cac81SBjoern A. Zeeb 
19466b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19472ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
19482ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
19492ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
19502ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
19510936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19522ac8a218SBjoern A. Zeeb 		return (ESRCH);
19532ac8a218SBjoern A. Zeeb 	}
19542ac8a218SBjoern A. Zeeb 
19556b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19566b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19576b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19586b4cac81SBjoern A. Zeeb 
19590936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19600936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
19610936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
19620936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19630936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19640936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19650936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19660936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
196792690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
196892690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
19690936c648SBjoern A. Zeeb 		return (EBUSY);
19700936c648SBjoern A. Zeeb 	}
19710936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19720936c648SBjoern A. Zeeb 
19736b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
1974cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
19756b4cac81SBjoern A. Zeeb 
19766b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
1977560708cbSBjoern A. Zeeb 	chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
1978560708cbSBjoern A. Zeeb 	    lockdep_is_held(&hw->wiphy->mtx));
1979560708cbSBjoern A. Zeeb 	if (chanctx_conf != NULL) {
1980d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
19816b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
19826b4cac81SBjoern A. Zeeb 	} else {
19836b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1984c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
19856b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1986d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
19876b4cac81SBjoern A. Zeeb 	}
19886b4cac81SBjoern A. Zeeb 
1989d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1990389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1991d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
19926b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1993d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1994d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
199590d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
199690d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
19979fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
19982ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
19992ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
200062d51a43SBjoern A. Zeeb 
20019fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
20029fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
200390d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2004d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
200590d8e307SBjoern A. Zeeb 		else
2006d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
20079fb91463SBjoern A. Zeeb 	}
20089fb91463SBjoern A. Zeeb #endif
20099fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
20105393cd34SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
20119fb91463SBjoern A. Zeeb #ifdef __notyet__
201290d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2013d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
201490d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2015d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
201690d8e307SBjoern A. Zeeb 		else
201790d8e307SBjoern A. Zeeb #endif
201890d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2019d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
20209fb91463SBjoern A. Zeeb 	}
20219fb91463SBjoern A. Zeeb #endif
2022389265e3SBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
20236b4cac81SBjoern A. Zeeb 	/* Responder ... */
202490d8e307SBjoern A. Zeeb #if 0
202590d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2026d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
202790d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
202890d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
202990d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
203090d8e307SBjoern A. Zeeb #endif
203190d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
203290d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
203390d8e307SBjoern A. Zeeb #else
203490d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
203590d8e307SBjoern A. Zeeb #endif
20366b4cac81SBjoern A. Zeeb 
20376ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
20386ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
20396ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
20406ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
20416ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
20426ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
20436ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
20446ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
20456ffb7bd4SBjoern A. Zeeb 
20466ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
20476ffb7bd4SBjoern A. Zeeb 
20486ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
20496ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
20506ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
20516ffb7bd4SBjoern A. Zeeb 
20526ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
20536ffb7bd4SBjoern A. Zeeb 
20546b4cac81SBjoern A. Zeeb 	error = 0;
2055560708cbSBjoern A. Zeeb 	if (vif->bss_conf.chanctx_conf == chanctx_conf) {
20566b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
20576b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
20586b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
20596b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2060d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
20616b4cac81SBjoern A. Zeeb 	} else {
2062d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
20636b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
20647b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
20657b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
20667b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
2067d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
20687b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
2069d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
20706b4cac81SBjoern A. Zeeb 		} else {
2071018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2072018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
20736b4cac81SBjoern A. Zeeb 			goto out;
20746b4cac81SBjoern A. Zeeb 		}
20756ffb7bd4SBjoern A. Zeeb 
2076a8a47a41SBjoern A. Zeeb 		list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2077560708cbSBjoern A. Zeeb 		rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
20786ffb7bd4SBjoern A. Zeeb 
20796b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
20806b4cac81SBjoern A. Zeeb 		if (error == 0)
208168541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2082d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
20836b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
20846b4cac81SBjoern A. Zeeb 			error = 0;
20856b4cac81SBjoern A. Zeeb 		if (error != 0) {
2086018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2087018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
2088d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2089560708cbSBjoern A. Zeeb 			rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2090d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2091a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
2092c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
20936b4cac81SBjoern A. Zeeb 			goto out;
20946b4cac81SBjoern A. Zeeb 		}
20956b4cac81SBjoern A. Zeeb 	}
20966b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
20976b4cac81SBjoern A. Zeeb 
20986b4cac81SBjoern A. Zeeb 	/* RATES */
20996b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
21006b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
21016b4cac81SBjoern A. Zeeb 
2102d9f59799SBjoern A. Zeeb 	/*
21030936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
21040936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
21050936c648SBjoern A. Zeeb 	 * under the hoods.
2106d9f59799SBjoern A. Zeeb 	 */
21070936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
21080936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
21096b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
2110e24e8103SBjoern A. Zeeb 
211188665349SBjoern A. Zeeb 	/*
211288665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
211388665349SBjoern A. Zeeb 	 * that we can tx again.
211488665349SBjoern A. Zeeb 	 */
211588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
211688665349SBjoern A. Zeeb 	lsta->txq_ready = true;
211788665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
211888665349SBjoern A. Zeeb 
21192ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
2120a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2121e24e8103SBjoern A. Zeeb 
2122d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
21236b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
21246b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
21256b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2126e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
21276b4cac81SBjoern A. Zeeb 	if (error != 0) {
21286b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2129018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2130018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21316b4cac81SBjoern A. Zeeb 		goto out;
21326b4cac81SBjoern A. Zeeb 	}
21336b4cac81SBjoern A. Zeeb #if 0
21346b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
21356b4cac81SBjoern A. Zeeb #endif
21366b4cac81SBjoern A. Zeeb 
21379d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
21389d9ba2b7SBjoern A. Zeeb 
21391665ef97SBjoern A. Zeeb #if 0
21406b4cac81SBjoern A. Zeeb 	/*
21416b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
21426b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
21436b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
21446b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
2145d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
2146d9f59799SBjoern A. Zeeb 	 * for all queues.
21476b4cac81SBjoern A. Zeeb 	 */
2148e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
21491665ef97SBjoern A. Zeeb #endif
21506b4cac81SBjoern A. Zeeb 
21516b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21526b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21536b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
21546b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21556b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
21566b4cac81SBjoern A. Zeeb 
21576b4cac81SBjoern A. Zeeb 	/*
21586b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
21596b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
21606b4cac81SBjoern A. Zeeb 	 * - event_callback
21616b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
21626b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
21636b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
21646b4cac81SBjoern A. Zeeb 	 */
21656b4cac81SBjoern A. Zeeb 
2166cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2167105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2168105b9df2SBjoern A. Zeeb 
2169105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
2170105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2171105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2172105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
2173105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2174105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2175105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2176105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2177105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
2178105b9df2SBjoern A. Zeeb 
2179105b9df2SBjoern A. Zeeb 	/*
2180105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2181105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
2182105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
2183105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
2184105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
2185105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
2186105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
2187105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
2188105b9df2SBjoern A. Zeeb 	 */
2189105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
2190105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
2191105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
2192105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
2193105b9df2SBjoern A. Zeeb 	} else {
2194105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
2195105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
2196105b9df2SBjoern A. Zeeb 		/*
2197105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
2198105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
2199105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2200105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
2201105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
2202105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
2203105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
2204105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
2205105b9df2SBjoern A. Zeeb 		 */
2206105b9df2SBjoern A. Zeeb 	}
2207105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2208105b9df2SBjoern A. Zeeb 	goto out_relocked;
2209105b9df2SBjoern A. Zeeb 
22106b4cac81SBjoern A. Zeeb out:
2211cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
22126b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2213105b9df2SBjoern A. Zeeb out_relocked:
22140936c648SBjoern A. Zeeb 	/*
2215105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
22160936c648SBjoern A. Zeeb 	 * during the work of this function.
22170936c648SBjoern A. Zeeb 	 */
22186b4cac81SBjoern A. Zeeb 	if (ni != NULL)
22196b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
22206b4cac81SBjoern A. Zeeb 	return (error);
22216b4cac81SBjoern A. Zeeb }
22226b4cac81SBjoern A. Zeeb 
22236b4cac81SBjoern A. Zeeb static int
22246b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22256b4cac81SBjoern A. Zeeb {
22266b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22276b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22286b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22296b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22306b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22316b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22326b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22336b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
22346b4cac81SBjoern A. Zeeb 	int error;
22356b4cac81SBjoern A. Zeeb 
22366b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22376b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22386b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22396b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22406b4cac81SBjoern A. Zeeb 
22412ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22422ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22432ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22442ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22452ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22462ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22472ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22482ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22492ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22502ac8a218SBjoern A. Zeeb #endif
22512ac8a218SBjoern A. Zeeb 
22522ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22532ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22542ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22552ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22562ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22572ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
22586b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22596b4cac81SBjoern A. Zeeb 
226067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
22616b4cac81SBjoern A. Zeeb 
22626b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2263cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
22646b4cac81SBjoern A. Zeeb 
2265d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2266d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2267d9f59799SBjoern A. Zeeb 
22686b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
226988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
22706b4cac81SBjoern A. Zeeb 
22716b4cac81SBjoern A. Zeeb 	/* flush, no drop */
22726b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
22736b4cac81SBjoern A. Zeeb 
22746b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22756b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22766b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22776b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
22786b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22796b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22806b4cac81SBjoern A. Zeeb 	}
22816b4cac81SBjoern A. Zeeb 
22826b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
22836b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
22846b4cac81SBjoern A. Zeeb 
22856b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
22866b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2287d9f59799SBjoern A. Zeeb 
2288d9f59799SBjoern A. Zeeb 	/* Take the station down. */
22896b4cac81SBjoern A. Zeeb 
22906b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
22916b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22926b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2293d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2294e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
22956b4cac81SBjoern A. Zeeb 	if (error != 0) {
22966b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2297018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2298018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
22996b4cac81SBjoern A. Zeeb 		goto out;
23006b4cac81SBjoern A. Zeeb 	}
23016b4cac81SBjoern A. Zeeb #if 0
23026b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
23036b4cac81SBjoern A. Zeeb #endif
23046b4cac81SBjoern A. Zeeb 
230567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
23066b4cac81SBjoern A. Zeeb 
23072ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23082ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23092ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23102ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23112ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2312d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23130936c648SBjoern A. Zeeb 	/*
23140936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23150936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23160936c648SBjoern A. Zeeb 	 * and potentially freed.
23170936c648SBjoern A. Zeeb 	 */
23180936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2319d9f59799SBjoern A. Zeeb 
2320d9f59799SBjoern A. Zeeb 	/* conf_tx */
2321d9f59799SBjoern A. Zeeb 
232250d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
23236b4cac81SBjoern A. Zeeb 
23246b4cac81SBjoern A. Zeeb out:
2325cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
23266b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
23276b4cac81SBjoern A. Zeeb 	return (error);
23286b4cac81SBjoern A. Zeeb }
23296b4cac81SBjoern A. Zeeb 
23306b4cac81SBjoern A. Zeeb static int
23316b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23326b4cac81SBjoern A. Zeeb {
23336b4cac81SBjoern A. Zeeb 	int error;
23346b4cac81SBjoern A. Zeeb 
23356b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
23366b4cac81SBjoern A. Zeeb 	if (error == 0)
23376b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
23386b4cac81SBjoern A. Zeeb 	return (error);
23396b4cac81SBjoern A. Zeeb }
23406b4cac81SBjoern A. Zeeb 
23416b4cac81SBjoern A. Zeeb static int
23426b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23436b4cac81SBjoern A. Zeeb {
23446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
23456b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
23466b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
23476b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
23486b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
23496b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
23506b4cac81SBjoern A. Zeeb 	int error;
23516b4cac81SBjoern A. Zeeb 
23526b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
23536b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
23546b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
23556b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
23566b4cac81SBjoern A. Zeeb 
23576b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2358cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
23592ac8a218SBjoern A. Zeeb 
23602ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23612ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
23622ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
23632ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
23642ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
23652ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
23662ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
23672ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
23682ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
23692ac8a218SBjoern A. Zeeb #endif
23702ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
23712ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
23722ac8a218SBjoern A. Zeeb 		goto out;
23732ac8a218SBjoern A. Zeeb 	}
23742ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
23752ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
23762ac8a218SBjoern A. Zeeb 
23772ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
23786b4cac81SBjoern A. Zeeb 
23796b4cac81SBjoern A. Zeeb 	/* Finish auth. */
23806b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
23816b4cac81SBjoern A. Zeeb 
23826b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
23836b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
23846b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2385e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2386018d93ecSBjoern A. Zeeb 	if (error != 0) {
2387018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2388018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23896b4cac81SBjoern A. Zeeb 		goto out;
2390018d93ecSBjoern A. Zeeb 	}
23916b4cac81SBjoern A. Zeeb 
23926b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23936b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23946b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23956b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
23966b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23976b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23986b4cac81SBjoern A. Zeeb 	}
23996b4cac81SBjoern A. Zeeb 
24006b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
24016b4cac81SBjoern A. Zeeb 
24026b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
24036b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
24046b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24056b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
24066b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
24076b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
24086b4cac81SBjoern A. Zeeb 	}
24096b4cac81SBjoern A. Zeeb 
24106b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
241188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
24126b4cac81SBjoern A. Zeeb 
24136b4cac81SBjoern A. Zeeb 	/*
24146b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
24156b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
24166b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
24176b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
24186b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
24196b4cac81SBjoern A. Zeeb 	 * - event_callback
24206b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
24216b4cac81SBjoern A. Zeeb 	 */
24226b4cac81SBjoern A. Zeeb 
24236b4cac81SBjoern A. Zeeb out:
2424cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24256b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24266b4cac81SBjoern A. Zeeb 	return (error);
24276b4cac81SBjoern A. Zeeb }
24286b4cac81SBjoern A. Zeeb 
24296b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
24306b4cac81SBjoern A. Zeeb static int
24316b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24326b4cac81SBjoern A. Zeeb {
24336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24346b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24356b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24366b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24376b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24386b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24392ac8a218SBjoern A. Zeeb 	int error;
24406b4cac81SBjoern A. Zeeb 
24416b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24426b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24436b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24446b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24456b4cac81SBjoern A. Zeeb 
24466b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2447cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
24482ac8a218SBjoern A. Zeeb 
24492ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24502ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24512ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24522ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24532ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24542ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24552ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24562ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24572ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24582ac8a218SBjoern A. Zeeb #endif
24592ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24602ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24612ac8a218SBjoern A. Zeeb 		goto out;
24622ac8a218SBjoern A. Zeeb 	}
24632ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24642ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24652ac8a218SBjoern A. Zeeb 
24662ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
24672ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
24686b4cac81SBjoern A. Zeeb 
24696b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
24706b4cac81SBjoern A. Zeeb 
24716b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
24726b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
24736b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24746b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
24756b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
24766b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
24776b4cac81SBjoern A. Zeeb 	}
24786b4cac81SBjoern A. Zeeb 
24796b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
24806b4cac81SBjoern A. Zeeb 
24816b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
24826b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
24836b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
24846b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
24856b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
24866b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
24876b4cac81SBjoern A. Zeeb 	}
24886b4cac81SBjoern A. Zeeb 
24892ac8a218SBjoern A. Zeeb 	error = 0;
24902ac8a218SBjoern A. Zeeb out:
2491cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
24926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
24936b4cac81SBjoern A. Zeeb 
24942ac8a218SBjoern A. Zeeb 	return (error);
24956b4cac81SBjoern A. Zeeb }
24966b4cac81SBjoern A. Zeeb 
24976b4cac81SBjoern A. Zeeb static int
2498d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24996b4cac81SBjoern A. Zeeb {
25006b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25016b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25026b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25036b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25046b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25056b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25066b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
25076b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2508d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
25096b4cac81SBjoern A. Zeeb 	int error;
25106b4cac81SBjoern A. Zeeb 
25116b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25126b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25136b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25146b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
25156b4cac81SBjoern A. Zeeb 
25162ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2517cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
25182ac8a218SBjoern A. Zeeb 
25192ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
25202ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
25212ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
25222ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
25232ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
25242ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
25252ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
25262ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
25272ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
25282ac8a218SBjoern A. Zeeb #endif
25292ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
25302ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25312ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25322ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25332ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25342ac8a218SBjoern A. Zeeb 
25352ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25366b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25376b4cac81SBjoern A. Zeeb 
253867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2539d9f59799SBjoern A. Zeeb 
2540d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2541d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2542d9f59799SBjoern A. Zeeb 
2543d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2544d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2545d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2546d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2547d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2548ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2549d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2550d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2551d9f59799SBjoern A. Zeeb 	}
2552d9f59799SBjoern A. Zeeb 
2553cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2554d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2555d9f59799SBjoern A. Zeeb 
255688665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2557d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2558018d93ecSBjoern A. Zeeb 	if (error != 0) {
2559018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2560018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2561d9f59799SBjoern A. Zeeb 		goto outni;
2562018d93ecSBjoern A. Zeeb 	}
2563d9f59799SBjoern A. Zeeb 
2564d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
256588665349SBjoern A. Zeeb 
256688665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
256788665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
256888665349SBjoern A. Zeeb 
2569cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2570d9f59799SBjoern A. Zeeb 
257167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2572d9f59799SBjoern A. Zeeb 
2573d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
257488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2575d9f59799SBjoern A. Zeeb 
2576d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2577d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2578d9f59799SBjoern A. Zeeb 
25796b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25806b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25816b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25826b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2583ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
25846b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25856b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25866b4cac81SBjoern A. Zeeb 	}
25876b4cac81SBjoern A. Zeeb 
2588d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2589d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2590d9f59799SBjoern A. Zeeb 
2591d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2592d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2593d9f59799SBjoern A. Zeeb 
2594d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2595d9f59799SBjoern A. Zeeb 
25966b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
25976b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
25986b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
25996b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2600e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2601018d93ecSBjoern A. Zeeb 	if (error != 0) {
2602018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2603018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26046b4cac81SBjoern A. Zeeb 		goto out;
2605018d93ecSBjoern A. Zeeb 	}
26066b4cac81SBjoern A. Zeeb 
26075a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
26085a4d2461SBjoern A. Zeeb 	bss_changed = 0;
26095a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
26106b4cac81SBjoern A. Zeeb 
26115a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
261216e688b2SBjoern A. Zeeb 
2613d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2614d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2615d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2616d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2617e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2618d9f59799SBjoern A. Zeeb 	if (error != 0) {
2619d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2620018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2621018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2622d9f59799SBjoern A. Zeeb 		goto out;
2623d9f59799SBjoern A. Zeeb 	}
2624d9f59799SBjoern A. Zeeb 
262516e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2626d9f59799SBjoern A. Zeeb 
2627d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2628d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2629d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2630616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2631616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2632d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2633d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2634d9f59799SBjoern A. Zeeb 
26352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26362ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
26372ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
26382ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
26392ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2640d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
26410936c648SBjoern A. Zeeb 	/*
26420936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
26430936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
26440936c648SBjoern A. Zeeb 	 * and potentially freed.
26450936c648SBjoern A. Zeeb 	 */
26460936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2647d9f59799SBjoern A. Zeeb 
2648d9f59799SBjoern A. Zeeb 	/* conf_tx */
2649d9f59799SBjoern A. Zeeb 
265050d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
2651d9f59799SBjoern A. Zeeb 
2652d9f59799SBjoern A. Zeeb 	error = EALREADY;
26536b4cac81SBjoern A. Zeeb out:
2654cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
26556b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2656d9f59799SBjoern A. Zeeb outni:
26576b4cac81SBjoern A. Zeeb 	return (error);
26586b4cac81SBjoern A. Zeeb }
26596b4cac81SBjoern A. Zeeb 
26606b4cac81SBjoern A. Zeeb static int
2661d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2662d9f59799SBjoern A. Zeeb {
2663d9f59799SBjoern A. Zeeb 	int error;
2664d9f59799SBjoern A. Zeeb 
2665d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2666d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2667d9f59799SBjoern A. Zeeb 		return (error);
2668d9f59799SBjoern A. Zeeb 
2669d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2670d9f59799SBjoern A. Zeeb 
2671d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2672d9f59799SBjoern A. Zeeb 	return (error);
2673d9f59799SBjoern A. Zeeb }
2674d9f59799SBjoern A. Zeeb 
2675d9f59799SBjoern A. Zeeb static int
26766b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26776b4cac81SBjoern A. Zeeb {
26786b4cac81SBjoern A. Zeeb 	int error;
26796b4cac81SBjoern A. Zeeb 
2680d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
26816b4cac81SBjoern A. Zeeb 	return (error);
26826b4cac81SBjoern A. Zeeb }
26836b4cac81SBjoern A. Zeeb 
26846b4cac81SBjoern A. Zeeb static int
26856b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26866b4cac81SBjoern A. Zeeb {
26876b4cac81SBjoern A. Zeeb 	int error;
26886b4cac81SBjoern A. Zeeb 
2689d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
26906b4cac81SBjoern A. Zeeb 	return (error);
26916b4cac81SBjoern A. Zeeb }
26926b4cac81SBjoern A. Zeeb 
26936b4cac81SBjoern A. Zeeb static int
26946b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26956b4cac81SBjoern A. Zeeb {
26966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26976b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26986b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26996b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
27006b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
27016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
27026b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
27036b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
27046b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
27056b4cac81SBjoern A. Zeeb 	int error;
27066b4cac81SBjoern A. Zeeb 
27076b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
27086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
27096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
27106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
27116b4cac81SBjoern A. Zeeb 
27126b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2713cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
27142ac8a218SBjoern A. Zeeb 
27152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27162ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
27172ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
27182ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27192ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
27202ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27212ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27222ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27232ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27242ac8a218SBjoern A. Zeeb #endif
27252ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
27262ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
27272ac8a218SBjoern A. Zeeb 		goto out;
27282ac8a218SBjoern A. Zeeb 	}
27292ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27302ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27312ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27322ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27332ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27342ac8a218SBjoern A. Zeeb 
27352ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
27366b4cac81SBjoern A. Zeeb 
27376b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
27386b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
27396b4cac81SBjoern A. Zeeb 
27409597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
27419597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
27429597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
27439597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
27446b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
27459597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
27464a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27474a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
27484a67f1dfSBjoern A. Zeeb 		sta->wme = true;
27494a67f1dfSBjoern A. Zeeb #endif
2750e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2751018d93ecSBjoern A. Zeeb 	if (error != 0) {
2752018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2753018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27549597f7cbSBjoern A. Zeeb 		goto out;
2755018d93ecSBjoern A. Zeeb 	}
27566b4cac81SBjoern A. Zeeb 
27576b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
27586b4cac81SBjoern A. Zeeb 
27596b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
27606b4cac81SBjoern A. Zeeb 	bss_changed = 0;
27614a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
27624a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
27634a67f1dfSBjoern A. Zeeb #endif
2764616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2765616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2766616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
27676b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
27686b4cac81SBjoern A. Zeeb 	}
27696b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2770616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2771616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
27726b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
27736b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
27746b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
27756b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
27766b4cac81SBjoern A. Zeeb 	}
27776b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
27786b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
27796b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
27806b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
27816b4cac81SBjoern A. Zeeb 	}
27826b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
27836b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
27846b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
27856b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
27866b4cac81SBjoern A. Zeeb 	}
2787fa8f007dSBjoern A. Zeeb 
2788fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
27896b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
27906b4cac81SBjoern A. Zeeb 
27916b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
27926b4cac81SBjoern A. Zeeb 	 * - event_callback
27936b4cac81SBjoern A. Zeeb 	 */
27946b4cac81SBjoern A. Zeeb 
27956b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
27966b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
27976b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
27986b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
27996b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
28006b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
28016b4cac81SBjoern A. Zeeb 	}
28026b4cac81SBjoern A. Zeeb 
2803086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2804086be6a8SBjoern A. Zeeb 
28056b4cac81SBjoern A. Zeeb 	/*
28066b4cac81SBjoern A. Zeeb 	 * And then:
28076b4cac81SBjoern A. Zeeb 	 * - (more packets)?
28086b4cac81SBjoern A. Zeeb 	 * - set_key
28096b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
28106b4cac81SBjoern A. Zeeb 	 * - set_key (?)
28116b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
28126b4cac81SBjoern A. Zeeb 	 */
28136b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
28146b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
28156b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
28166b4cac81SBjoern A. Zeeb 
28176b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
28186b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
28196b4cac81SBjoern A. Zeeb 	}
28206b4cac81SBjoern A. Zeeb 
2821f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
28229fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
282362d51a43SBjoern A. Zeeb 	lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
28249fb91463SBjoern A. Zeeb 
28256b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
28266b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
28276b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
28286b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2829e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
28306b4cac81SBjoern A. Zeeb 	if (error != 0) {
28316b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2832018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2833018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
28346b4cac81SBjoern A. Zeeb 		goto out;
28356b4cac81SBjoern A. Zeeb 	}
28366b4cac81SBjoern A. Zeeb 
28376b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
28386b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
28396b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
28406b4cac81SBjoern A. Zeeb 	 *
28416b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
28426b4cac81SBjoern A. Zeeb 	 */
28436b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
28446b4cac81SBjoern A. Zeeb 
2845fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2846fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2847fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2848fa8f007dSBjoern A. Zeeb 
28496b4cac81SBjoern A. Zeeb out:
2850cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
28516b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
28526b4cac81SBjoern A. Zeeb 	return (error);
28536b4cac81SBjoern A. Zeeb }
28546b4cac81SBjoern A. Zeeb 
28556b4cac81SBjoern A. Zeeb static int
28566b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28576b4cac81SBjoern A. Zeeb {
28586b4cac81SBjoern A. Zeeb 	int error;
28596b4cac81SBjoern A. Zeeb 
28606b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
28616b4cac81SBjoern A. Zeeb 	if (error == 0)
28626b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
28636b4cac81SBjoern A. Zeeb 	return (error);
28646b4cac81SBjoern A. Zeeb }
28656b4cac81SBjoern A. Zeeb 
28666b4cac81SBjoern A. Zeeb static int
28676b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
28686b4cac81SBjoern A. Zeeb {
28696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
28706b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
28716b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
28726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28736b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
28746b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28756b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2876d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2877d9f59799SBjoern A. Zeeb #if 0
2878d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2879d9f59799SBjoern A. Zeeb #endif
28806b4cac81SBjoern A. Zeeb 	int error;
28816b4cac81SBjoern A. Zeeb 
28826b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
28836b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
28846b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
28856b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
28866b4cac81SBjoern A. Zeeb 
28872ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28882ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28892ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
28902ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
28912ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28922ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28932ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28942ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28952ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28962ac8a218SBjoern A. Zeeb #endif
28972ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28982ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28992ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
29002ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
29012ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
29022ac8a218SBjoern A. Zeeb 
29032ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
29046b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
29056b4cac81SBjoern A. Zeeb 
290667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2907d9f59799SBjoern A. Zeeb 
2908d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
2909cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2910d9f59799SBjoern A. Zeeb 
2911d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2912d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2913d9f59799SBjoern A. Zeeb 
2914d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2915d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2916d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2917d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2918d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2919ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2920d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2921d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2922d9f59799SBjoern A. Zeeb 	}
2923d9f59799SBjoern A. Zeeb 
2924cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
2925d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2926d9f59799SBjoern A. Zeeb 
2927d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2928d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2929018d93ecSBjoern A. Zeeb 	if (error != 0) {
2930018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2931018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2932d9f59799SBjoern A. Zeeb 		goto outni;
2933018d93ecSBjoern A. Zeeb 	}
2934d9f59799SBjoern A. Zeeb 
2935d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
293688665349SBjoern A. Zeeb 
293788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
293888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
293988665349SBjoern A. Zeeb 
2940cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
2941d9f59799SBjoern A. Zeeb 
294267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2943d9f59799SBjoern A. Zeeb 
2944d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
294588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2946d9f59799SBjoern A. Zeeb 
2947d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2948d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2949d9f59799SBjoern A. Zeeb 
2950d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2951d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2952d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2953d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2954ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2955d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2956d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2957d9f59799SBjoern A. Zeeb 	}
2958d9f59799SBjoern A. Zeeb 
2959d9f59799SBjoern A. Zeeb #if 0
2960d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2961d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2962d9f59799SBjoern A. Zeeb 
2963d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2964d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2965d9f59799SBjoern A. Zeeb #endif
2966d9f59799SBjoern A. Zeeb 
2967d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2968d9f59799SBjoern A. Zeeb 
29696b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
29706b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
29716b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
29726b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2973e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2974018d93ecSBjoern A. Zeeb 	if (error != 0) {
2975018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2976018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
29776b4cac81SBjoern A. Zeeb 		goto out;
2978018d93ecSBjoern A. Zeeb 	}
29796b4cac81SBjoern A. Zeeb 
298067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
29816b4cac81SBjoern A. Zeeb 
298211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
298311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
298411db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
298511db70b6SBjoern A. Zeeb 		if (error != 0) {
298611db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
298711db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
298811db70b6SBjoern A. Zeeb 			/*
298911db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
299011db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
299111db70b6SBjoern A. Zeeb 			 * less appropriate time).
299211db70b6SBjoern A. Zeeb 			 */
299311db70b6SBjoern A. Zeeb 			/* goto out; */
299411db70b6SBjoern A. Zeeb 		}
299511db70b6SBjoern A. Zeeb 	}
299611db70b6SBjoern A. Zeeb #endif
299711db70b6SBjoern A. Zeeb 
29986b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
29996b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
30006b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
30016b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3002e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3003018d93ecSBjoern A. Zeeb 	if (error != 0) {
3004018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3005018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
30066b4cac81SBjoern A. Zeeb 		goto out;
3007018d93ecSBjoern A. Zeeb 	}
30086b4cac81SBjoern A. Zeeb 
300967674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
30106b4cac81SBjoern A. Zeeb 
3011d9f59799SBjoern A. Zeeb #if 0
3012d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
3013d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
3014d9f59799SBjoern A. Zeeb #endif
3015d9f59799SBjoern A. Zeeb 
3016d9f59799SBjoern A. Zeeb 	error = EALREADY;
30176b4cac81SBjoern A. Zeeb out:
3018cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
30196b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3020d9f59799SBjoern A. Zeeb outni:
30216b4cac81SBjoern A. Zeeb 	return (error);
30226b4cac81SBjoern A. Zeeb }
30236b4cac81SBjoern A. Zeeb 
30246b4cac81SBjoern A. Zeeb static int
3025d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3026d9f59799SBjoern A. Zeeb {
3027d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
3028d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3029d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
3030d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3031d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
3032d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
3033d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
3034d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
3035d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
3036d9f59799SBjoern A. Zeeb 	int error;
3037d9f59799SBjoern A. Zeeb 
3038d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
3039d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3040d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
3041d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
3042d9f59799SBjoern A. Zeeb 
30432ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
3044cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
30452ac8a218SBjoern A. Zeeb 
30462ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30472ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30482ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
30492ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
30502ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
30512ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
30522ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
30532ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
30542ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
30552ac8a218SBjoern A. Zeeb #endif
30562ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
30572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30582ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
30592ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
30602ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
30612ac8a218SBjoern A. Zeeb 
30622ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
3063d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
3064d9f59799SBjoern A. Zeeb 
306567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3066d9f59799SBjoern A. Zeeb 
3067d9f59799SBjoern A. Zeeb 	/* flush, drop. */
3068d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
3069d9f59799SBjoern A. Zeeb 
3070d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3071d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3072d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
3073d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3074d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
3075ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3076d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3077d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
3078d9f59799SBjoern A. Zeeb 	}
3079d9f59799SBjoern A. Zeeb 
3080cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3081d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3082d9f59799SBjoern A. Zeeb 
3083d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
3084d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
3085018d93ecSBjoern A. Zeeb 	if (error != 0) {
3086018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3087018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3088d9f59799SBjoern A. Zeeb 		goto outni;
3089018d93ecSBjoern A. Zeeb 	}
3090d9f59799SBjoern A. Zeeb 
3091d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
309288665349SBjoern A. Zeeb 
309388665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
309488665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
309588665349SBjoern A. Zeeb 
3096cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3097d9f59799SBjoern A. Zeeb 
309867674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3099d9f59799SBjoern A. Zeeb 
3100d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
310188665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
3102d9f59799SBjoern A. Zeeb 
3103d9f59799SBjoern A. Zeeb 	/* flush, no drop */
3104d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
3105d9f59799SBjoern A. Zeeb 
3106d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
3107d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
3108d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3109d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
3110ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
3111d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3112d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
3113d9f59799SBjoern A. Zeeb 	}
3114d9f59799SBjoern A. Zeeb 
3115d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
3116d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
3117d9f59799SBjoern A. Zeeb 
3118d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
3119d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3120d9f59799SBjoern A. Zeeb 
3121d9f59799SBjoern A. Zeeb 	/* Take the station down. */
3122d9f59799SBjoern A. Zeeb 
3123d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3124d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3125d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3126d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3127e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3128018d93ecSBjoern A. Zeeb 	if (error != 0) {
3129018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3130018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3131d9f59799SBjoern A. Zeeb 		goto out;
3132018d93ecSBjoern A. Zeeb 	}
3133d9f59799SBjoern A. Zeeb 
313467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3135d9f59799SBjoern A. Zeeb 
313611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
313711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
313811db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
313911db70b6SBjoern A. Zeeb 		if (error != 0) {
314011db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
314111db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
314211db70b6SBjoern A. Zeeb 			/*
314311db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
314411db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
314511db70b6SBjoern A. Zeeb 			 * less appropriate time).
314611db70b6SBjoern A. Zeeb 			 */
314711db70b6SBjoern A. Zeeb 			/* goto out; */
314811db70b6SBjoern A. Zeeb 		}
314911db70b6SBjoern A. Zeeb 	}
315011db70b6SBjoern A. Zeeb #endif
315111db70b6SBjoern A. Zeeb 
3152d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
3153d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3154d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3155d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
3156e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3157018d93ecSBjoern A. Zeeb 	if (error != 0) {
3158018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3159018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3160d9f59799SBjoern A. Zeeb 		goto out;
3161018d93ecSBjoern A. Zeeb 	}
3162d9f59799SBjoern A. Zeeb 
316367674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3164d9f59799SBjoern A. Zeeb 
3165d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
3166d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3167d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3168d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
3169e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3170018d93ecSBjoern A. Zeeb 	if (error != 0) {
3171018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3172018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3173d9f59799SBjoern A. Zeeb 		goto out;
3174018d93ecSBjoern A. Zeeb 	}
3175d9f59799SBjoern A. Zeeb 
31765a4d2461SBjoern A. Zeeb 	bss_changed = 0;
317716e688b2SBjoern A. Zeeb 	/*
31785a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
31795a4d2461SBjoern A. Zeeb 	 *
318016e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
31815a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
31825a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
31835a4d2461SBjoern A. Zeeb 	 *
31845a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
31855a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
31865a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
31875a4d2461SBjoern A. Zeeb 	 * a fw assert.
31885a4d2461SBjoern A. Zeeb 	 *
31895a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
31905a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
31915a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
31925a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
31935a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
31945a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
31955a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
31965a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
31975a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
31985a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
319916e688b2SBjoern A. Zeeb 	 */
32005a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
32015a4d2461SBjoern A. Zeeb 
32025a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
320316e688b2SBjoern A. Zeeb 
3204d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
3205d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3206d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3207d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3208e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3209d9f59799SBjoern A. Zeeb 	if (error != 0) {
3210d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
3211018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3212018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
3213d9f59799SBjoern A. Zeeb 		goto out;
3214d9f59799SBjoern A. Zeeb 	}
3215d9f59799SBjoern A. Zeeb 
32165a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
32175a4d2461SBjoern A. Zeeb 
321816e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
3219d9f59799SBjoern A. Zeeb 
3220d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
3221d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
3222d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
3223616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
3224616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3225d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
32265a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
32275a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
32285a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3229d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3230d9f59799SBjoern A. Zeeb 
32312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32322ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
32332ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
32342ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32360936c648SBjoern A. Zeeb 	/*
32370936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
32380936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
32390936c648SBjoern A. Zeeb 	 * and potentially freed.
32400936c648SBjoern A. Zeeb 	 */
32410936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3242d9f59799SBjoern A. Zeeb 
3243d9f59799SBjoern A. Zeeb 	/* conf_tx */
3244d9f59799SBjoern A. Zeeb 
324550d826beSBjoern A. Zeeb 	lkpi_remove_chanctx(hw, vif);
3246d9f59799SBjoern A. Zeeb 
3247d9f59799SBjoern A. Zeeb 	error = EALREADY;
3248d9f59799SBjoern A. Zeeb out:
3249cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3250d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3251d9f59799SBjoern A. Zeeb outni:
3252d9f59799SBjoern A. Zeeb 	return (error);
3253d9f59799SBjoern A. Zeeb }
3254d9f59799SBjoern A. Zeeb 
3255d9f59799SBjoern A. Zeeb static int
3256d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3257d9f59799SBjoern A. Zeeb {
3258d9f59799SBjoern A. Zeeb 
3259d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3260d9f59799SBjoern A. Zeeb }
3261d9f59799SBjoern A. Zeeb 
3262d9f59799SBjoern A. Zeeb static int
32636b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
32646b4cac81SBjoern A. Zeeb {
32656b4cac81SBjoern A. Zeeb 	int error;
32666b4cac81SBjoern A. Zeeb 
3267d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3268d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3269d9f59799SBjoern A. Zeeb 		return (error);
3270d9f59799SBjoern A. Zeeb 
3271d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3272d9f59799SBjoern A. Zeeb 
3273d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
32746b4cac81SBjoern A. Zeeb 	return (error);
32756b4cac81SBjoern A. Zeeb }
32766b4cac81SBjoern A. Zeeb 
3277d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32786b4cac81SBjoern A. Zeeb 
32796b4cac81SBjoern A. Zeeb /*
32806b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
32816b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
32826b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
32836b4cac81SBjoern A. Zeeb  */
32846b4cac81SBjoern A. Zeeb struct fsm_state {
32856b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
32866b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
32876b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
32886b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
32896b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
32906b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
32916b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
32926b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3293d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3294d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
32956b4cac81SBjoern A. Zeeb 
32966b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
32976b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
32986b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
32996b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3300d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
33016b4cac81SBjoern A. Zeeb 
3302d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3303d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3304d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3305d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3306d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
33076b4cac81SBjoern A. Zeeb 
3308d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3309d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3310d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
33116b4cac81SBjoern A. Zeeb 
33126b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
33136b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
33146b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
33156b4cac81SBjoern A. Zeeb 
33166b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
33176b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
33186b4cac81SBjoern A. Zeeb };
33196b4cac81SBjoern A. Zeeb 
33206b4cac81SBjoern A. Zeeb static int
33216b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
33226b4cac81SBjoern A. Zeeb {
33236b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
33246b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33256b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33266b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33276b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
33286b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
33296b4cac81SBjoern A. Zeeb 	int error;
33306b4cac81SBjoern A. Zeeb 
33316b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
33326b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
33336b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
33346b4cac81SBjoern A. Zeeb 
33359d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33369d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
33376b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
33386b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
33399d9ba2b7SBjoern A. Zeeb #endif
33406b4cac81SBjoern A. Zeeb 
33416b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
33426b4cac81SBjoern A. Zeeb 
33436b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
33446b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
33456b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
33466b4cac81SBjoern A. Zeeb 
33476b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
33486b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
33496b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
33506b4cac81SBjoern A. Zeeb 
33516b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
33526b4cac81SBjoern A. Zeeb 
33536b4cac81SBjoern A. Zeeb 	} else {
33546b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
33556b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
33566b4cac81SBjoern A. Zeeb 		return (ENOSYS);
33576b4cac81SBjoern A. Zeeb 	}
33586b4cac81SBjoern A. Zeeb 
33596b4cac81SBjoern A. Zeeb 	error = 0;
33606b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
33616b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
33629d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33639d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3364d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3365d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3366d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3367d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
33689d9ba2b7SBjoern A. Zeeb #endif
33696b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
33706b4cac81SBjoern A. Zeeb 			break;
33716b4cac81SBjoern A. Zeeb 		}
33726b4cac81SBjoern A. Zeeb 	}
33736b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3376d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
33776b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
33786b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
33796b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
33806b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
33816b4cac81SBjoern A. Zeeb 		return (ENOSYS);
33826b4cac81SBjoern A. Zeeb 	}
33836b4cac81SBjoern A. Zeeb 
33846b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
33859d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
33869d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3387d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3388d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3389d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3390d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
33919d9ba2b7SBjoern A. Zeeb #endif
33926b4cac81SBjoern A. Zeeb 		return (0);
33936b4cac81SBjoern A. Zeeb 	}
33946b4cac81SBjoern A. Zeeb 
33956b4cac81SBjoern A. Zeeb 	if (error != 0) {
33966b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
33976b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
33986b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
33996b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
340045c27ad5SBjoern A. Zeeb 		return (error);
34016b4cac81SBjoern A. Zeeb 	}
34026b4cac81SBjoern A. Zeeb 
34039d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
34049d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3405d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3406d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
34076b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
34089d9ba2b7SBjoern A. Zeeb #endif
34096b4cac81SBjoern A. Zeeb 
34106b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
34116b4cac81SBjoern A. Zeeb }
34126b4cac81SBjoern A. Zeeb 
34136b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
34146b4cac81SBjoern A. Zeeb 
3415d9f59799SBjoern A. Zeeb /*
3416d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3417d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3418d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3419d9f59799SBjoern A. Zeeb  */
3420d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3421d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3422d9f59799SBjoern A. Zeeb {
3423d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34242ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3425d9f59799SBjoern A. Zeeb 
34262ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3427d9f59799SBjoern A. Zeeb 
3428ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
34292ac8a218SBjoern A. Zeeb 
34302ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
34312ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
34322ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
34332ac8a218SBjoern A. Zeeb 
34342ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
34352ac8a218SBjoern A. Zeeb 	return (rni);
3436d9f59799SBjoern A. Zeeb }
3437d9f59799SBjoern A. Zeeb 
34384a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
34396b4cac81SBjoern A. Zeeb static int
34404a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
34416b4cac81SBjoern A. Zeeb {
34424a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
34436b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
34446b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
34456b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
34466b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
34476b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
34486b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
34496b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
34506b4cac81SBjoern A. Zeeb 	int error;
34516b4cac81SBjoern A. Zeeb 	uint16_t ac;
34526b4cac81SBjoern A. Zeeb 
3453cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
3454cd0fcf9fSBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
3455cd0fcf9fSBjoern A. Zeeb 
34566b4cac81SBjoern A. Zeeb 	IMPROVE();
34576b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
34586b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
34596b4cac81SBjoern A. Zeeb 
34606b4cac81SBjoern A. Zeeb 	if (vap == NULL)
34616b4cac81SBjoern A. Zeeb 		return (0);
34626b4cac81SBjoern A. Zeeb 
34634a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
34644a67f1dfSBjoern A. Zeeb 		return (0);
34654a67f1dfSBjoern A. Zeeb 
34666b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
34676b4cac81SBjoern A. Zeeb 		return (0);
34686b4cac81SBjoern A. Zeeb 
34694a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
34704a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
34714a67f1dfSBjoern A. Zeeb 		return (0);
34724a67f1dfSBjoern A. Zeeb 	}
34734a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
34746b4cac81SBjoern A. Zeeb 
34754a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
34766b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
34776b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
34786b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
34796b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
34806b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
34816b4cac81SBjoern A. Zeeb 
34824a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
34834a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
34844a67f1dfSBjoern A. Zeeb 
34856b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
34866b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
34876b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
34886b4cac81SBjoern A. Zeeb 
34896b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
34906b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
34916b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
34926b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
34936b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
34946b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
349568541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
34966b4cac81SBjoern A. Zeeb 		if (error != 0)
34973f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
34986b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
34996b4cac81SBjoern A. Zeeb 	}
35006b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
35014a67f1dfSBjoern A. Zeeb 	if (!planned)
35026b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
35034a67f1dfSBjoern A. Zeeb 
35044a67f1dfSBjoern A. Zeeb 	return (changed);
35054a67f1dfSBjoern A. Zeeb }
35066b4cac81SBjoern A. Zeeb #endif
35076b4cac81SBjoern A. Zeeb 
35084a67f1dfSBjoern A. Zeeb static int
35094a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
35104a67f1dfSBjoern A. Zeeb {
35114a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
35124a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
35134a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
3514cd0fcf9fSBjoern A. Zeeb 	struct ieee80211_hw *hw;
35154a67f1dfSBjoern A. Zeeb 
35164a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
35174a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
35184a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
35196b4cac81SBjoern A. Zeeb 		return (0);
35204a67f1dfSBjoern A. Zeeb 
35214a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
3522cd0fcf9fSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35234a67f1dfSBjoern A. Zeeb 
3524cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
35254a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
3526cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
35274a67f1dfSBjoern A. Zeeb #endif
35284a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
35296b4cac81SBjoern A. Zeeb }
35306b4cac81SBjoern A. Zeeb 
35314aff4048SBjoern A. Zeeb /*
35324aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
35334aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
35344aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
35354aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
35364aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
35374aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3538edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3539edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3540edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3541edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
35424aff4048SBjoern A. Zeeb  */
35434aff4048SBjoern A. Zeeb static void
35444aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
35454aff4048SBjoern A. Zeeb {
35464aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
35474aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35484aff4048SBjoern A. Zeeb 
35494aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3550edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3551edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3552edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
35534aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
35544aff4048SBjoern A. Zeeb 		return;
35554aff4048SBjoern A. Zeeb 	}
35564aff4048SBjoern A. Zeeb 
35574aff4048SBjoern A. Zeeb 	vif = arg;
355857609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
35594aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
35604aff4048SBjoern A. Zeeb }
35614aff4048SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb static struct ieee80211vap *
35636b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
35646b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
35656b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
35666b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
35676b4cac81SBjoern A. Zeeb {
35686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35706b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35716b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
35726b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3573a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
35746b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
357540839418SBjoern A. Zeeb 	struct sysctl_oid *node;
35766b4cac81SBjoern A. Zeeb 	size_t len;
3577e3a0b120SBjoern A. Zeeb 	int error, i;
3578a6042e17SBjoern A. Zeeb 	uint16_t ac;
35796b4cac81SBjoern A. Zeeb 
35806b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
35816b4cac81SBjoern A. Zeeb 		return (NULL);
35826b4cac81SBjoern A. Zeeb 
35836b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35846b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35856b4cac81SBjoern A. Zeeb 
35866b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
35876b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
35886b4cac81SBjoern A. Zeeb 
35896b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
35906b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3591a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
35922ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
3593b8dfc3ecSBjoern A. Zeeb 	refcount_init(&lvif->nt_unlocked, 0);
35942ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
35956b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
35966b4cac81SBjoern A. Zeeb 
35976b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35986b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
35996b4cac81SBjoern A. Zeeb 	vif->p2p = false;
36006b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
36016b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
36026b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
36036b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
36046b4cac81SBjoern A. Zeeb 	IMPROVE();
36056b4cac81SBjoern A. Zeeb 
36066b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
36076b4cac81SBjoern A. Zeeb #if 1
3608560708cbSBjoern A. Zeeb 	RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3609adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
36106ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
36116ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
36124aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
36134aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
36146ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
36157b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
36166b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
36176b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
36186b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
36196b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
36206b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3621616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3622616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3623616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3624616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
36256ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3626caaa79c3SBjoern A. Zeeb 	/*
3627caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3628caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3629caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3630caaa79c3SBjoern A. Zeeb 	 */
36316ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
36326b4cac81SBjoern A. Zeeb #endif
36336b4cac81SBjoern A. Zeeb #if 0
36346b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
36356b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
36366b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
36376b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
36386b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
36396b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
36406b4cac81SBjoern A. Zeeb #endif
3641e3a0b120SBjoern A. Zeeb 
36426ffb7bd4SBjoern A. Zeeb 	/* Link Config */
36436ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
36446ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
36456ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
36466ffb7bd4SBjoern A. Zeeb 	}
36476ffb7bd4SBjoern A. Zeeb 
3648e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3649e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3650e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3651e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3652e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3653e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3654e3a0b120SBjoern A. Zeeb 		else
3655e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
36560cbcfa19SBjoern A. Zeeb 
36570cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
36580cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3659e3a0b120SBjoern A. Zeeb 	}
3660e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3661e3a0b120SBjoern A. Zeeb 
36626b4cac81SBjoern A. Zeeb 	IMPROVE();
36636b4cac81SBjoern A. Zeeb 
36646b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
36656b4cac81SBjoern A. Zeeb 	if (error != 0) {
36663f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
36676b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
36686b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
36696b4cac81SBjoern A. Zeeb 		return (NULL);
36706b4cac81SBjoern A. Zeeb 	}
36716b4cac81SBjoern A. Zeeb 
36726b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
36736b4cac81SBjoern A. Zeeb 	if (error != 0) {
36746b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
36753f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
36766b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
36776b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
36786b4cac81SBjoern A. Zeeb 		return (NULL);
36796b4cac81SBjoern A. Zeeb 	}
36806b4cac81SBjoern A. Zeeb 
36818891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36826b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
36838891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36846b4cac81SBjoern A. Zeeb 
36856b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
36866b4cac81SBjoern A. Zeeb 	changed = 0;
36876b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
36886b4cac81SBjoern A. Zeeb 
3689a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3690a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3691cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
3692a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3693a6042e17SBjoern A. Zeeb 
3694a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3695a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3696a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3697a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3698a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3699a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3700a6042e17SBjoern A. Zeeb 		if (error != 0)
3701a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3702a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3703a6042e17SBjoern A. Zeeb 	}
3704cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
3705a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3706a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
37076b4cac81SBjoern A. Zeeb 
37086b4cac81SBjoern A. Zeeb 	/* Force MC init. */
37096b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
37106b4cac81SBjoern A. Zeeb 
37116b4cac81SBjoern A. Zeeb 	IMPROVE();
37126b4cac81SBjoern A. Zeeb 
37136b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
37146b4cac81SBjoern A. Zeeb 
37156b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
37166b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
37176b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3718d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3719d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
37206b4cac81SBjoern A. Zeeb 
3721b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
372211db70b6SBjoern A. Zeeb 	/* Key management. */
372311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
37246b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
37256b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
3726b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_begin = lkpi_iv_key_update_begin;
3727b8dfc3ecSBjoern A. Zeeb 		vap->iv_key_update_end = lkpi_iv_key_update_end;
37286b4cac81SBjoern A. Zeeb 	}
372911db70b6SBjoern A. Zeeb #endif
37306b4cac81SBjoern A. Zeeb 
37319fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
37329fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
37339fb91463SBjoern A. Zeeb #endif
37349fb91463SBjoern A. Zeeb 
37356b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
37366b4cac81SBjoern A. Zeeb 
37376b4cac81SBjoern A. Zeeb 	/* Complete setup. */
37386b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
37396b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
37406b4cac81SBjoern A. Zeeb 
374111db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
374211db70b6SBjoern A. Zeeb 	/*
374311db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
374411db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
374511db70b6SBjoern A. Zeeb 	 */
374611db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
374711db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
374811db70b6SBjoern A. Zeeb #endif
3749ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3750ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3751ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3752ac2c7271SBjoern A. Zeeb #endif
375311db70b6SBjoern A. Zeeb 
37546b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
37556b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
37566b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
37576b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
37586b4cac81SBjoern A. Zeeb 
37596b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
37606b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
37616b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
37626b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
37636b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
37646b4cac81SBjoern A. Zeeb 	/* any others? */
376540839418SBjoern A. Zeeb 
376640839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
376740839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
376840839418SBjoern A. Zeeb 
376940839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
377040839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
377140839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
377240839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
377340839418SBjoern A. Zeeb 
377440839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
377540839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
377640839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
377740839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
377840839418SBjoern A. Zeeb 
37796b4cac81SBjoern A. Zeeb 	IMPROVE();
37806b4cac81SBjoern A. Zeeb 
37816b4cac81SBjoern A. Zeeb 	return (vap);
37826b4cac81SBjoern A. Zeeb }
37836b4cac81SBjoern A. Zeeb 
37844b0af114SBjoern A. Zeeb void
37854b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
37864b0af114SBjoern A. Zeeb {
37874b0af114SBjoern A. Zeeb 
37884b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
37894b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
37904b0af114SBjoern A. Zeeb 
37914b0af114SBjoern A. Zeeb 	IMPROVE();
37924b0af114SBjoern A. Zeeb }
37934b0af114SBjoern A. Zeeb 
37944b0af114SBjoern A. Zeeb void
37954b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
37964b0af114SBjoern A. Zeeb {
37974b0af114SBjoern A. Zeeb 
37984b0af114SBjoern A. Zeeb 	TODO();
37994b0af114SBjoern A. Zeeb }
38004b0af114SBjoern A. Zeeb 
38016b4cac81SBjoern A. Zeeb static void
38026b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
38036b4cac81SBjoern A. Zeeb {
38046b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
38056b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38066b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38076b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38086b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38096b4cac81SBjoern A. Zeeb 
38106b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
38116b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
38126b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
38136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38156b4cac81SBjoern A. Zeeb 
38164aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
38174aff4048SBjoern A. Zeeb 
381840839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
381940839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
382040839418SBjoern A. Zeeb 
38218891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
38226b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
38238891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
38246b4cac81SBjoern A. Zeeb 
38256b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
38266b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3827dbf76919SBjoern A. Zeeb 
3828dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3829dbf76919SBjoern A. Zeeb 
3830dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3831dbf76919SBjoern A. Zeeb 
38326c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
38337b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
38346c38c6b1SBjoern A. Zeeb 
38356b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
38366b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
38376b4cac81SBjoern A. Zeeb }
38386b4cac81SBjoern A. Zeeb 
38396b4cac81SBjoern A. Zeeb static void
38406b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
38416b4cac81SBjoern A. Zeeb {
38426b4cac81SBjoern A. Zeeb 
38436b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
38446b4cac81SBjoern A. Zeeb 	TRACEOK();
38456b4cac81SBjoern A. Zeeb }
38466b4cac81SBjoern A. Zeeb 
38476b4cac81SBjoern A. Zeeb static void
38486b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
38496b4cac81SBjoern A. Zeeb {
38506b4cac81SBjoern A. Zeeb 
38516b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38526b4cac81SBjoern A. Zeeb }
38536b4cac81SBjoern A. Zeeb 
38546b4cac81SBjoern A. Zeeb static void
38556b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
38566b4cac81SBjoern A. Zeeb {
38576b4cac81SBjoern A. Zeeb 
38586b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
38596b4cac81SBjoern A. Zeeb }
38606b4cac81SBjoern A. Zeeb 
38616b4cac81SBjoern A. Zeeb /* Start / stop device. */
38626b4cac81SBjoern A. Zeeb static void
38636b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
38646b4cac81SBjoern A. Zeeb {
38656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38666b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
3867cd0fcf9fSBjoern A. Zeeb #ifdef HW_START_STOP
38686b4cac81SBjoern A. Zeeb 	int error;
38698d58a057SBjoern A. Zeeb #endif
38706b4cac81SBjoern A. Zeeb 	bool start_all;
38716b4cac81SBjoern A. Zeeb 
38726b4cac81SBjoern A. Zeeb 	IMPROVE();
38736b4cac81SBjoern A. Zeeb 
38746b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38756b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38766b4cac81SBjoern A. Zeeb 	start_all = false;
38776b4cac81SBjoern A. Zeeb 
38788ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
3879cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
38806b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
38818d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
38826b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
38836b4cac81SBjoern A. Zeeb 		if (error == 0)
38848d58a057SBjoern A. Zeeb #endif
38856b4cac81SBjoern A. Zeeb 			start_all = true;
38866b4cac81SBjoern A. Zeeb 	} else {
38878d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
38887b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
38898d58a057SBjoern A. Zeeb #endif
38906b4cac81SBjoern A. Zeeb 	}
3891cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
38928ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
38936b4cac81SBjoern A. Zeeb 
38946b4cac81SBjoern A. Zeeb 	if (start_all)
38956b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
38966b4cac81SBjoern A. Zeeb }
38976b4cac81SBjoern A. Zeeb 
38986b4cac81SBjoern A. Zeeb bool
38996b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
39006b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
39016b4cac81SBjoern A. Zeeb {
39026b4cac81SBjoern A. Zeeb 	int i;
39036b4cac81SBjoern A. Zeeb 
39046b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
39056b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
39066b4cac81SBjoern A. Zeeb 			return (true);
39076b4cac81SBjoern A. Zeeb 	}
39086b4cac81SBjoern A. Zeeb 
39096b4cac81SBjoern A. Zeeb 	return (false);
39106b4cac81SBjoern A. Zeeb }
39116b4cac81SBjoern A. Zeeb 
39126b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
39136b4cac81SBjoern A. Zeeb bool
39146b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
39156b4cac81SBjoern A. Zeeb {
39166b4cac81SBjoern A. Zeeb 	size_t x;
39176b4cac81SBjoern A. Zeeb 	uint8_t l;
39186b4cac81SBjoern A. Zeeb 
39196b4cac81SBjoern A. Zeeb 	x = *xp;
39206b4cac81SBjoern A. Zeeb 
39216b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
39226b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
39236b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
39246b4cac81SBjoern A. Zeeb 	x += 2 + l;
39256b4cac81SBjoern A. Zeeb 
39266b4cac81SBjoern A. Zeeb 	if (x > ies_len)
39276b4cac81SBjoern A. Zeeb 		return (false);
39286b4cac81SBjoern A. Zeeb 
39296b4cac81SBjoern A. Zeeb 	*xp = x;
39306b4cac81SBjoern A. Zeeb 	return (true);
39316b4cac81SBjoern A. Zeeb }
39326b4cac81SBjoern A. Zeeb 
3933d9945d78SBjoern A. Zeeb static uint8_t *
3934d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3935d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
39366b4cac81SBjoern A. Zeeb {
3937d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3938d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
39393dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3940d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3941d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3942d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3943d9945d78SBjoern A. Zeeb 	int band, i;
39446b4cac81SBjoern A. Zeeb 
39453dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3946d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3947d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3948d9945d78SBjoern A. Zeeb 			continue;
3949d9945d78SBjoern A. Zeeb 
3950d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3951d9945d78SBjoern A. Zeeb 		/*
3952d9945d78SBjoern A. Zeeb 		 * This should not happen;
3953d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3954d9945d78SBjoern A. Zeeb 		 */
3955d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3956d9945d78SBjoern A. Zeeb 			continue;
3957d9945d78SBjoern A. Zeeb 
3958d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3959d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3960d9945d78SBjoern A. Zeeb 		chan = NULL;
3961d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3962d9945d78SBjoern A. Zeeb 
3963d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3964d9945d78SBjoern A. Zeeb 				continue;
3965d9945d78SBjoern A. Zeeb 
39663dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3967d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3968d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3969d9945d78SBjoern A. Zeeb 				break;
3970d9945d78SBjoern A. Zeeb 		}
3971d9945d78SBjoern A. Zeeb 
3972d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3973d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3974d9945d78SBjoern A. Zeeb 			continue;
3975d9945d78SBjoern A. Zeeb 
3976d9945d78SBjoern A. Zeeb 		pb = p;
39773dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3978d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3979d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3980d9945d78SBjoern A. Zeeb 
39813dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
39823dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
39833dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
39843dd98026SBjoern A. Zeeb 
39853dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
39863dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
39873dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
39883dd98026SBjoern A. Zeeb 		}
39893dd98026SBjoern A. Zeeb #endif
39903dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
39915393cd34SBjoern A. Zeeb 		if (band == NL80211_BAND_5GHZ &&
39925393cd34SBjoern A. Zeeb 		    (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
39933dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
39943dd98026SBjoern A. Zeeb 
39953dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
39963dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
39973dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
39983dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
39993dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
40003dd98026SBjoern A. Zeeb 		}
40013dd98026SBjoern A. Zeeb #endif
40023dd98026SBjoern A. Zeeb 
4003d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
4004d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
4005d9945d78SBjoern A. Zeeb 	}
4006d9945d78SBjoern A. Zeeb 
4007d9945d78SBjoern A. Zeeb 	/* Add common_ies */
4008d9945d78SBjoern A. Zeeb 	pb = p;
4009d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4010d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
4011d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4012d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
4013d9945d78SBjoern A. Zeeb 	}
4014d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
4015d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
4016d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
4017d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
4018d9945d78SBjoern A. Zeeb 	}
4019d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
4020d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
4021d9945d78SBjoern A. Zeeb 
4022d9945d78SBjoern A. Zeeb 	return (p);
40236b4cac81SBjoern A. Zeeb }
40246b4cac81SBjoern A. Zeeb 
40256b4cac81SBjoern A. Zeeb static void
40266b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
40276b4cac81SBjoern A. Zeeb {
40286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40296b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
40306b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
40316b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
40326b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
40336b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
40346b4cac81SBjoern A. Zeeb 	int error;
40358ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40366b4cac81SBjoern A. Zeeb 
40376b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4039a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40406b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
40418ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40426b4cac81SBjoern A. Zeeb 		return;
40436b4cac81SBjoern A. Zeeb 	}
40448ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40466b4cac81SBjoern A. Zeeb 
40476b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
40486b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
40496b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
4050d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
40516b4cac81SBjoern A. Zeeb 		return;
40526b4cac81SBjoern A. Zeeb 	}
40536b4cac81SBjoern A. Zeeb 
40546b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
40558ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4056a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4057a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4058d3ef7fb4SBjoern A. Zeeb sw_scan:
40596b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40606b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4061086be6a8SBjoern A. Zeeb 
4062086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4063086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
4064086be6a8SBjoern A. Zeeb 
40656b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
40666b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
40676b4cac81SBjoern A. Zeeb 		IMPROVE();
40686b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
40696b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
40706b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
40716b4cac81SBjoern A. Zeeb 
40726b4cac81SBjoern A. Zeeb 	} else {
40736b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
40746b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
40756b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
40766b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
40776b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
4078d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
4079d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
4080d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
40813206587aSBjoern A. Zeeb 		bool running;
4082d9945d78SBjoern A. Zeeb 
4083d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4084d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
40856b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
40866b4cac81SBjoern A. Zeeb 
4087d9945d78SBjoern A. Zeeb 		band_mask = 0;
40886b4cac81SBjoern A. Zeeb 		nchan = 0;
4089c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4090c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
4091d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
40926b4cac81SBjoern A. Zeeb 				nchan++;
4093d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
4094d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
4095d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
4096d9945d78SBjoern A. Zeeb 			}
4097c272abc5SBjoern A. Zeeb #else
4098c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
4099c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
4100c272abc5SBjoern A. Zeeb 				switch (band) {
4101c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
4102c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
4103c272abc5SBjoern A. Zeeb 					break;
4104c272abc5SBjoern A. Zeeb 				default:
4105c272abc5SBjoern A. Zeeb 					continue;
4106c272abc5SBjoern A. Zeeb 				}
4107c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
4108c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
4109c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
4110c272abc5SBjoern A. Zeeb 				}
4111c272abc5SBjoern A. Zeeb 			}
4112c272abc5SBjoern A. Zeeb #endif
4113c272abc5SBjoern A. Zeeb 		} else {
41143206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
41153206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
41163206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
41173206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
41183206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
41193206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
41203206587aSBjoern A. Zeeb 			 */
41213206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
41223206587aSBjoern A. Zeeb 		}
41233206587aSBjoern A. Zeeb 
41246b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
41256b4cac81SBjoern A. Zeeb 
4126d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
4127d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4128d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
4129d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
4130d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
4131d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
4132d9945d78SBjoern A. Zeeb 
4133d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
4134d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
4135d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4136d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
4137d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
4138d9945d78SBjoern A. Zeeb 		}
4139d9945d78SBjoern A. Zeeb 
41403206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
4141d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4142d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
41436b4cac81SBjoern A. Zeeb 
41446b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
41456b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
41466b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
41476b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
41486b4cac81SBjoern A. Zeeb #if 0
41496b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
41506b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
41516b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
41526b4cac81SBjoern A. Zeeb #endif
41536b4cac81SBjoern A. Zeeb #ifdef __notyet__
41546b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
41556b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
41566b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
41576b4cac81SBjoern A. Zeeb #endif
4158e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
41596b4cac81SBjoern A. Zeeb 
41606b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
41616b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
41626b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
41636b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
41646b4cac81SBjoern A. Zeeb 			*(cpp + i) =
41656b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
41666b4cac81SBjoern A. Zeeb 		}
4167c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
41686b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
4169c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
41706b4cac81SBjoern A. Zeeb 
4171c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
41726b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
41733206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
41746b4cac81SBjoern A. Zeeb 			/* lc->flags */
41756b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
41766b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
41776b4cac81SBjoern A. Zeeb 			/* lc-> ... */
41786b4cac81SBjoern A. Zeeb 			lc++;
41796b4cac81SBjoern A. Zeeb 		}
4180c272abc5SBjoern A. Zeeb #else
4181c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
4182c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
4183c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
4184c272abc5SBjoern A. Zeeb 
4185c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
4186c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
4187c272abc5SBjoern A. Zeeb 				continue;
4188c272abc5SBjoern A. Zeeb 
4189c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
4190c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
4191c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
4192c272abc5SBjoern A. Zeeb 				continue;
4193c272abc5SBjoern A. Zeeb 
4194c272abc5SBjoern A. Zeeb 			channels = supband->channels;
4195c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
4196c272abc5SBjoern A. Zeeb 				*lc = channels[i];
4197c272abc5SBjoern A. Zeeb 				lc++;
4198c272abc5SBjoern A. Zeeb 			}
4199c272abc5SBjoern A. Zeeb 		}
4200c272abc5SBjoern A. Zeeb #endif
42016b4cac81SBjoern A. Zeeb 
4202d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
42036b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
42046b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
42056b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
4206d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
42076b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
42086b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
42096b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
42106b4cac81SBjoern A. Zeeb 				ssids++;
42116b4cac81SBjoern A. Zeeb 			}
42126b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
42136b4cac81SBjoern A. Zeeb 		} else {
42146b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
42156b4cac81SBjoern A. Zeeb 		}
42166b4cac81SBjoern A. Zeeb 
42176b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
42186b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
42196b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
42206b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
42216b4cac81SBjoern A. Zeeb 		/* s6gp->... */
42226b4cac81SBjoern A. Zeeb 
4223d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4224d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4225d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4226d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4227d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4228d9945d78SBjoern A. Zeeb 
42296b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
42306b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
42313206587aSBjoern A. Zeeb 
42323206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
42333206587aSBjoern A. Zeeb 		/* Re-check under lock. */
42343206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
42353206587aSBjoern A. Zeeb 		if (!running) {
42363206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
42373206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
42383206587aSBjoern A. Zeeb 
42393206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
42403206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
42413206587aSBjoern A. Zeeb 		}
42423206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42433206587aSBjoern A. Zeeb 		if (running) {
42443206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
42453206587aSBjoern A. Zeeb 			return;
42463206587aSBjoern A. Zeeb 		}
42473206587aSBjoern A. Zeeb 
42486b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
42496b4cac81SBjoern A. Zeeb 		if (error != 0) {
4250d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4251d9945d78SBjoern A. Zeeb 
42523206587aSBjoern A. Zeeb 			/*
42533206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
42543206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
42553206587aSBjoern A. Zeeb 			 * and only there.
42563206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
42573206587aSBjoern A. Zeeb 			 * behave differently:
42583206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
42593206587aSBjoern A. Zeeb 			 *        and can then still return an error.
42603206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
42613206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
42623206587aSBjoern A. Zeeb 			 * ...
42633206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
42643206587aSBjoern A. Zeeb 			 * and balance between both code paths.
42653206587aSBjoern A. Zeeb 			 */
42663206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
42673206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
42683206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
42696b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
42703206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
42713206587aSBjoern A. Zeeb 			}
42723206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4273d3ef7fb4SBjoern A. Zeeb 
4274d3ef7fb4SBjoern A. Zeeb 			/*
4275d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4276d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4277d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4278d3ef7fb4SBjoern A. Zeeb 			 */
4279196cfd0bSBjoern A. Zeeb 			if (error == 1) {
42808ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4281a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
42828ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
42833206587aSBjoern A. Zeeb 				/*
42843206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
42853206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
42863206587aSBjoern A. Zeeb 				 * currently not re-enable it?
42873206587aSBjoern A. Zeeb 				 */
42883206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4289196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4290196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4291196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4292196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4293196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4294196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4295196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4296196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4297d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4298196cfd0bSBjoern A. Zeeb 			}
4299d3ef7fb4SBjoern A. Zeeb 
4300d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4301d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
43026b4cac81SBjoern A. Zeeb 		}
43036b4cac81SBjoern A. Zeeb 	}
43046b4cac81SBjoern A. Zeeb }
43056b4cac81SBjoern A. Zeeb 
43066b4cac81SBjoern A. Zeeb static void
43076b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
43086b4cac81SBjoern A. Zeeb {
43096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43108ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
43116b4cac81SBjoern A. Zeeb 
43126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4314a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
43158ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43166b4cac81SBjoern A. Zeeb 		return;
43176b4cac81SBjoern A. Zeeb 	}
43188ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43206b4cac81SBjoern A. Zeeb 
43218ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4322a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4323a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
43246b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
43256b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
43266b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
43276b4cac81SBjoern A. Zeeb 
4328a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4329a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
43306b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
43316b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
43326b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4333a486fbbdSBjoern A. Zeeb 
43346b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
43356b4cac81SBjoern A. Zeeb 
43366b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4337086be6a8SBjoern A. Zeeb 
4338086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4339086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
43406b4cac81SBjoern A. Zeeb 	}
43416b4cac81SBjoern A. Zeeb }
43426b4cac81SBjoern A. Zeeb 
43436b4cac81SBjoern A. Zeeb static void
4344a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4345a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4346a486fbbdSBjoern A. Zeeb {
4347a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43488ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4349a486fbbdSBjoern A. Zeeb 
4350a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
43518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43528ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43538ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43548ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4355a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4356a486fbbdSBjoern A. Zeeb }
4357a486fbbdSBjoern A. Zeeb 
4358a486fbbdSBjoern A. Zeeb static void
4359a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4360a486fbbdSBjoern A. Zeeb {
4361a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
43628ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4363a486fbbdSBjoern A. Zeeb 
4364a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
43658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43668ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
43678ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43688ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4369a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4370a486fbbdSBjoern A. Zeeb }
4371a486fbbdSBjoern A. Zeeb 
4372a486fbbdSBjoern A. Zeeb static void
43736b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
43746b4cac81SBjoern A. Zeeb {
43756b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43766b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4377b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4378b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
43796b4cac81SBjoern A. Zeeb 	int error;
43808ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
43816b4cac81SBjoern A. Zeeb 
43826b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4383b2cf3c21SBjoern A. Zeeb 
4384b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4385b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
43866b4cac81SBjoern A. Zeeb 		return;
43876b4cac81SBjoern A. Zeeb 
4388a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
43898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
43908ac540d3SBjoern A. Zeeb 	hw_scan_running =
43918ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
43928ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
43938ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
43948ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4395a486fbbdSBjoern A. Zeeb 		return;
4396a486fbbdSBjoern A. Zeeb 
4397b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4398b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
43996b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
44006b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
44016b4cac81SBjoern A. Zeeb 		return;
44026b4cac81SBjoern A. Zeeb 	}
44036b4cac81SBjoern A. Zeeb 
44046b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
44056b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
44066b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
44076b4cac81SBjoern A. Zeeb 		    c, chan);
44086b4cac81SBjoern A. Zeeb 		return;
44096b4cac81SBjoern A. Zeeb 	}
44106b4cac81SBjoern A. Zeeb 
44116b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
44126b4cac81SBjoern A. Zeeb 	IMPROVE();
44136b4cac81SBjoern A. Zeeb 
44146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44154a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
44169fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
441711450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
44189fb91463SBjoern A. Zeeb #endif
44194a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
44206b4cac81SBjoern A. Zeeb 
44216b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
44226b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
44236b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
44246b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
44256b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
44266b4cac81SBjoern A. Zeeb 		IMPROVE();
44276b4cac81SBjoern A. Zeeb 	} else {
44286b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
44296b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
44306b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
44316b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
44326b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
44336b4cac81SBjoern A. Zeeb 	}
44346b4cac81SBjoern A. Zeeb 
44356b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
44366b4cac81SBjoern A. Zeeb 	IMPROVE();
44376b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
44386b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
44396b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
44406b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
44416b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
44426b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
44436b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
44446b4cac81SBjoern A. Zeeb 			    error);
44456b4cac81SBjoern A. Zeeb 	}
44466b4cac81SBjoern A. Zeeb }
44476b4cac81SBjoern A. Zeeb 
44486b4cac81SBjoern A. Zeeb static struct ieee80211_node *
44496b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
44506b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
44516b4cac81SBjoern A. Zeeb {
44526b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44544f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
44556b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44566b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
44576b4cac81SBjoern A. Zeeb 
44586b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
44596b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44606b4cac81SBjoern A. Zeeb 
44616b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
44624f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
44634f61ef8bSBjoern A. Zeeb 		return (NULL);
44644f61ef8bSBjoern A. Zeeb 
44656b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
44666b4cac81SBjoern A. Zeeb 	if (ni == NULL)
44676b4cac81SBjoern A. Zeeb 		return (NULL);
44686b4cac81SBjoern A. Zeeb 
44696b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44704f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
44716b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
44726b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
44736b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
44746b4cac81SBjoern A. Zeeb 		return (NULL);
44756b4cac81SBjoern A. Zeeb 	}
44766b4cac81SBjoern A. Zeeb 
44776b4cac81SBjoern A. Zeeb 	return (ni);
44786b4cac81SBjoern A. Zeeb }
44796b4cac81SBjoern A. Zeeb 
44806b4cac81SBjoern A. Zeeb static int
44816b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
44826b4cac81SBjoern A. Zeeb {
44836b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44846b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44856b4cac81SBjoern A. Zeeb 	int error;
44866b4cac81SBjoern A. Zeeb 
44876b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44886b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44896b4cac81SBjoern A. Zeeb 
44906b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
44916b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
44926b4cac81SBjoern A. Zeeb 		if (error != 0)
44936b4cac81SBjoern A. Zeeb 			return (error);
44946b4cac81SBjoern A. Zeeb 	}
44956b4cac81SBjoern A. Zeeb 
44966b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
44976b4cac81SBjoern A. Zeeb 	IMPROVE();
44986b4cac81SBjoern A. Zeeb 
44996b4cac81SBjoern A. Zeeb 	return (0);
45006b4cac81SBjoern A. Zeeb }
45016b4cac81SBjoern A. Zeeb 
45026b4cac81SBjoern A. Zeeb static void
45036b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
45046b4cac81SBjoern A. Zeeb {
45056b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45076b4cac81SBjoern A. Zeeb 
45086b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45096b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45106b4cac81SBjoern A. Zeeb 
45116b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
45126b4cac81SBjoern A. Zeeb 	IMPROVE();
45136b4cac81SBjoern A. Zeeb 
45146b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
45156b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
45166b4cac81SBjoern A. Zeeb }
45176b4cac81SBjoern A. Zeeb 
45186b4cac81SBjoern A. Zeeb static void
45196b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
45206b4cac81SBjoern A. Zeeb {
45216b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
45226b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
45236b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
45246b4cac81SBjoern A. Zeeb 
45256b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
45266b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
45276b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
45286b4cac81SBjoern A. Zeeb 
45290936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
45306b4cac81SBjoern A. Zeeb 
45310936c648SBjoern A. Zeeb 	/*
45320936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
45330936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
45340936c648SBjoern A. Zeeb 	 */
45350936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
45366b4cac81SBjoern A. Zeeb 
45376b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
45386b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
45396b4cac81SBjoern A. Zeeb }
45406b4cac81SBjoern A. Zeeb 
45419a45c3caSBjoern A. Zeeb /*
45429a45c3caSBjoern A. Zeeb  * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
45439a45c3caSBjoern A. Zeeb  * call path.
45449a45c3caSBjoern A. Zeeb  * Unfortunately they have slightly different invariants.  See
45459a45c3caSBjoern A. Zeeb  * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
45469a45c3caSBjoern A. Zeeb  * Both take care of the ni reference in case of error, and otherwise during
45479a45c3caSBjoern A. Zeeb  * the callback after transmit.
45489a45c3caSBjoern A. Zeeb  * The difference is that in case of error (*ic_raw_xmit) needs us to release
45499a45c3caSBjoern A. Zeeb  * the mbuf, while (*ic_transmit) will free the mbuf itself.
45509a45c3caSBjoern A. Zeeb  */
45516b4cac81SBjoern A. Zeeb static int
45529a45c3caSBjoern A. Zeeb lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
45539a45c3caSBjoern A. Zeeb     const struct ieee80211_bpf_params *params __unused,
45549a45c3caSBjoern A. Zeeb     bool freem)
45556b4cac81SBjoern A. Zeeb {
45566b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4557c816f64eSBjoern A. Zeeb 	int error;
45586b4cac81SBjoern A. Zeeb 
45596b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4560fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
45612372f8ccSBjoern A. Zeeb #if 0
456288665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
45632372f8ccSBjoern A. Zeeb #else
45642372f8ccSBjoern A. Zeeb 	/*
45652372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
45662372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
45672372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
45682372f8ccSBjoern A. Zeeb 	 */
45692372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
45702372f8ccSBjoern A. Zeeb #endif
4571fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
45729a45c3caSBjoern A. Zeeb 		if (freem)
45730936c648SBjoern A. Zeeb 			m_free(m);
45740936c648SBjoern A. Zeeb 		return (ENETDOWN);
45750936c648SBjoern A. Zeeb 	}
45766b4cac81SBjoern A. Zeeb 
45776b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
4578c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lsta->txq, m);
4579c816f64eSBjoern A. Zeeb 	if (error != 0) {
4580c816f64eSBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
45819a45c3caSBjoern A. Zeeb 		if (freem)
4582c816f64eSBjoern A. Zeeb 			m_free(m);
4583c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4584c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4585c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
4586c816f64eSBjoern A. Zeeb 			    __func__, error);
4587c816f64eSBjoern A. Zeeb #endif
4588c816f64eSBjoern A. Zeeb 		return (ENETDOWN);
4589c816f64eSBjoern A. Zeeb 	}
4590fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4591fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
45926b4cac81SBjoern A. Zeeb 
45939d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
45956b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
45966b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
45976b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
45989d9ba2b7SBjoern A. Zeeb #endif
45996b4cac81SBjoern A. Zeeb 
46006b4cac81SBjoern A. Zeeb 	return (0);
46016b4cac81SBjoern A. Zeeb }
46026b4cac81SBjoern A. Zeeb 
46039a45c3caSBjoern A. Zeeb static int
46049a45c3caSBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
46059a45c3caSBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
46069a45c3caSBjoern A. Zeeb {
46079a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, true));
46089a45c3caSBjoern A. Zeeb }
46099a45c3caSBjoern A. Zeeb 
461011db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
461111db70b6SBjoern A. Zeeb static int
4612*98e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
4613*98e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
461411db70b6SBjoern A. Zeeb {
4615*98e55905SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4616*98e55905SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
4617*98e55905SBjoern A. Zeeb 	uint8_t *p, *m;
4618*98e55905SBjoern A. Zeeb 
4619*98e55905SBjoern A. Zeeb 	/*
4620*98e55905SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
4621*98e55905SBjoern A. Zeeb 	 * or if we are done?
4622*98e55905SBjoern A. Zeeb 	 */
4623*98e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
4624*98e55905SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
4625*98e55905SBjoern A. Zeeb 			return (0);
4626*98e55905SBjoern A. Zeeb 
4627*98e55905SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
4628*98e55905SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
4629*98e55905SBjoern A. Zeeb 		return (ENOSPC);
4630*98e55905SBjoern A. Zeeb 
4631*98e55905SBjoern A. Zeeb 	hdr = (void *)skb->data;
4632*98e55905SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4633*98e55905SBjoern A. Zeeb 	p = skb_push(skb, hlen);
4634*98e55905SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
4635*98e55905SBjoern A. Zeeb 
4636*98e55905SBjoern A. Zeeb 	/*
4637*98e55905SBjoern A. Zeeb 	 * Put in zeroed space for the MMIC if requested.
4638*98e55905SBjoern A. Zeeb 	 * XXX-BZ in theory this is not the right place but given we
4639*98e55905SBjoern A. Zeeb 	 * are here we know we do hw_crypto so not much missing.
4640*98e55905SBjoern A. Zeeb 	 */
4641*98e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0) {
4642*98e55905SBjoern A. Zeeb 		m = skb_put(skb, 8);
4643*98e55905SBjoern A. Zeeb 		memset(m, 0, 8);
4644*98e55905SBjoern A. Zeeb 	}
4645*98e55905SBjoern A. Zeeb 
4646*98e55905SBjoern A. Zeeb 	/* If driver request space only we are done. */
4647*98e55905SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
4648*98e55905SBjoern A. Zeeb 		return (0);
4649*98e55905SBjoern A. Zeeb 
4650*98e55905SBjoern A. Zeeb 	p += hdrlen;
4651*98e55905SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
4652*98e55905SBjoern A. Zeeb 
4653*98e55905SBjoern A. Zeeb 	return (ENXIO);
4654*98e55905SBjoern A. Zeeb }
4655*98e55905SBjoern A. Zeeb static int
4656*98e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
4657*98e55905SBjoern A. Zeeb     struct ieee80211_key_conf *kc, struct sk_buff *skb)
4658*98e55905SBjoern A. Zeeb {
465911db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
466011db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
466111db70b6SBjoern A. Zeeb 	uint8_t *p;
466211db70b6SBjoern A. Zeeb 
466311db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
466411db70b6SBjoern A. Zeeb 
466511db70b6SBjoern A. Zeeb 	/*
466611db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
466711db70b6SBjoern A. Zeeb 	 * or if we are done?
466811db70b6SBjoern A. Zeeb 	 */
466911db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
467011db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
467111db70b6SBjoern A. Zeeb 	    /* MFP */
467211db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
467311db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
467411db70b6SBjoern A. Zeeb 			return (0);
467511db70b6SBjoern A. Zeeb 
467611db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
467711db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
467811db70b6SBjoern A. Zeeb 		return (ENOSPC);
467911db70b6SBjoern A. Zeeb 
468011db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
468111db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
468211db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
468311db70b6SBjoern A. Zeeb 
468411db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
468511db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
468611db70b6SBjoern A. Zeeb 		return (0);
468711db70b6SBjoern A. Zeeb 
468811db70b6SBjoern A. Zeeb 	p += hdrlen;
468911db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
469011db70b6SBjoern A. Zeeb 
469111db70b6SBjoern A. Zeeb 	return (0);
469211db70b6SBjoern A. Zeeb }
4693*98e55905SBjoern A. Zeeb 
4694*98e55905SBjoern A. Zeeb static int
4695*98e55905SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
4696*98e55905SBjoern A. Zeeb     struct sk_buff *skb)
4697*98e55905SBjoern A. Zeeb {
4698*98e55905SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
4699*98e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
4700*98e55905SBjoern A. Zeeb 
4701*98e55905SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
4702*98e55905SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
4703*98e55905SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
4704*98e55905SBjoern A. Zeeb 
4705*98e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
4706*98e55905SBjoern A. Zeeb 
4707*98e55905SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
4708*98e55905SBjoern A. Zeeb 	info->control.hw_key = kc;
4709*98e55905SBjoern A. Zeeb 
4710*98e55905SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
4711*98e55905SBjoern A. Zeeb 	if (kc == NULL) {
4712*98e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
4713*98e55905SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
4714*98e55905SBjoern A. Zeeb 		return (ENXIO);
4715*98e55905SBjoern A. Zeeb 	}
4716*98e55905SBjoern A. Zeeb 
4717*98e55905SBjoern A. Zeeb 	switch (kc->cipher) {
4718*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
4719*98e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
4720*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
4721*98e55905SBjoern A. Zeeb 		return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
4722*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
4723*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
4724*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
4725*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
4726*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
4727*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
4728*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
4729*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4730*98e55905SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4731*98e55905SBjoern A. Zeeb 	default:
4732*98e55905SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
4733*98e55905SBjoern A. Zeeb 		    "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
4734*98e55905SBjoern A. Zeeb 		    skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
4735*98e55905SBjoern A. Zeeb 		return (EOPNOTSUPP);
4736*98e55905SBjoern A. Zeeb 	}
4737*98e55905SBjoern A. Zeeb }
4738*98e55905SBjoern A. Zeeb 
4739*98e55905SBjoern A. Zeeb static uint8_t
4740*98e55905SBjoern A. Zeeb lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
4741*98e55905SBjoern A. Zeeb {
4742*98e55905SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
4743*98e55905SBjoern A. Zeeb 
4744*98e55905SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
4745*98e55905SBjoern A. Zeeb 	if (kc == NULL)
4746*98e55905SBjoern A. Zeeb 		return (0);
4747*98e55905SBjoern A. Zeeb 
4748*98e55905SBjoern A. Zeeb 	IMPROVE("which other flags need tailroom?");
4749*98e55905SBjoern A. Zeeb 	if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
4750*98e55905SBjoern A. Zeeb 		return (32);	/* Large enough to hold everything and pow2. */
4751*98e55905SBjoern A. Zeeb 
4752*98e55905SBjoern A. Zeeb 	return (0);
4753*98e55905SBjoern A. Zeeb }
475411db70b6SBjoern A. Zeeb #endif
475511db70b6SBjoern A. Zeeb 
47566b4cac81SBjoern A. Zeeb static void
47576b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
47586b4cac81SBjoern A. Zeeb {
47596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47606b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
47616b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
47626b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
47636b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
47646b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47656b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
47666b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
47676b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
47686b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
47696b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
47706b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
47716b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4772e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4773ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
47746b4cac81SBjoern A. Zeeb 	void *buf;
477511db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4776*98e55905SBjoern A. Zeeb 	uint8_t ac, tid, tailroom;
47776b4cac81SBjoern A. Zeeb 
47786b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
47796b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
47809d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
47816b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
47826b4cac81SBjoern A. Zeeb #endif
47836b4cac81SBjoern A. Zeeb 
47846b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4785b35f6cd0SBjoern A. Zeeb 	k = NULL;
478611db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
47876b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
47886b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
478911db70b6SBjoern A. Zeeb 
479011db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
479111db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
479211db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
479311db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
479411db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
479511db70b6SBjoern A. Zeeb 		}
479611db70b6SBjoern A. Zeeb #endif
479711db70b6SBjoern A. Zeeb 
479811db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
479911db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
48006b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
48016b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
48026b4cac81SBjoern A. Zeeb 			if (k == NULL) {
48036b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
48046b4cac81SBjoern A. Zeeb 				m_freem(m);
48056b4cac81SBjoern A. Zeeb 				return;
48066b4cac81SBjoern A. Zeeb 			}
48076b4cac81SBjoern A. Zeeb 		}
480811db70b6SBjoern A. Zeeb 	}
48096b4cac81SBjoern A. Zeeb 
48106b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
48116b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
48126b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
48136b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
48146b4cac81SBjoern A. Zeeb 
48156b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
48166b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
48176b4cac81SBjoern A. Zeeb 
48186b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
48196b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
48206b4cac81SBjoern A. Zeeb 		if (k != NULL)
48216b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
48226b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
48236b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
48246b4cac81SBjoern A. Zeeb 		IMPROVE();
48256b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
48266b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
48276b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
48286b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
48296b4cac81SBjoern A. Zeeb 		}
48306b4cac81SBjoern A. Zeeb 
48316b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
48326b4cac81SBjoern A. Zeeb 	}
48336b4cac81SBjoern A. Zeeb 
4834*98e55905SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
4835*98e55905SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
4836*98e55905SBjoern A. Zeeb 		tailroom = lkpi_hw_crypto_tailroom(lsta, k);
4837*98e55905SBjoern A. Zeeb 	else
4838*98e55905SBjoern A. Zeeb #endif
4839*98e55905SBjoern A. Zeeb 		tailroom = 0;
4840*98e55905SBjoern A. Zeeb 
48416b4cac81SBjoern A. Zeeb 	/*
48426b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
48436b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
48443d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
48453d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
48466b4cac81SBjoern A. Zeeb 	 */
4847*98e55905SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
48486b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4849bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4850bcf1d8eeSBjoern A. Zeeb 
4851bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4852bcf1d8eeSBjoern A. Zeeb 			int tid;
4853bcf1d8eeSBjoern A. Zeeb 
4854bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4855bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4856bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4857bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4858bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4859bcf1d8eeSBjoern A. Zeeb 					continue;
4860bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4861bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4862bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4863bcf1d8eeSBjoern A. Zeeb 			}
4864bcf1d8eeSBjoern A. Zeeb 		}
48656b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
48666b4cac81SBjoern A. Zeeb 		m_freem(m);
48676b4cac81SBjoern A. Zeeb 		return;
48686b4cac81SBjoern A. Zeeb 	}
48696b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
48706b4cac81SBjoern A. Zeeb 
48716b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
48726b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
48736b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
48746b4cac81SBjoern A. Zeeb 	skb->m = m;
48756b4cac81SBjoern A. Zeeb #if 0
48766b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
48776b4cac81SBjoern A. Zeeb #else
48786b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
48796b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
48806b4cac81SBjoern A. Zeeb #endif
48816b4cac81SBjoern A. Zeeb 	/* Save the ni. */
48826b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
48836b4cac81SBjoern A. Zeeb 
48846b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
48856b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
48866b4cac81SBjoern A. Zeeb 
4887e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4888e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4889e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
48901665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
48911665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
48921665ef97SBjoern A. Zeeb 			skb->priority = 7;
48931665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
48941665ef97SBjoern A. Zeeb 		} else {
48951665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
48961665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4897e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4898e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
48991665ef97SBjoern A. Zeeb 		}
4900e3a0b120SBjoern A. Zeeb 	} else {
4901e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4902fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4903e3a0b120SBjoern A. Zeeb 	}
49046b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
49056b4cac81SBjoern A. Zeeb 
49066b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
49076b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
49086b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
49096b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
49106b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
49116b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4912e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
49136b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
49146b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
49156b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
49166b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
49179fb91463SBjoern A. Zeeb #ifdef __notyet__
49189fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
49199fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
49209fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
49219fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
49229fb91463SBjoern A. Zeeb #endif
49239fb91463SBjoern A. Zeeb #endif
49246b4cac81SBjoern A. Zeeb 
49256b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4926b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
492711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
492811db70b6SBjoern A. Zeeb 		int error;
492911db70b6SBjoern A. Zeeb 
493011db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
493111db70b6SBjoern A. Zeeb 		if (error != 0) {
493211db70b6SBjoern A. Zeeb 			/*
493311db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
493411db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
493511db70b6SBjoern A. Zeeb 			 */
493611db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
493711db70b6SBjoern A. Zeeb 			return;
493811db70b6SBjoern A. Zeeb 		}
493911db70b6SBjoern A. Zeeb 	}
49406b4cac81SBjoern A. Zeeb #endif
49416b4cac81SBjoern A. Zeeb 
49426b4cac81SBjoern A. Zeeb 	IMPROVE();
49436b4cac81SBjoern A. Zeeb 
4944e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4945e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4946e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4947e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4948e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4949d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4950e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4951e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4952e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4953e3a0b120SBjoern A. Zeeb 	}
4954e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4955d0d29110SBjoern A. Zeeb 		goto ops_tx;
4956e3a0b120SBjoern A. Zeeb 
4957d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4958d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4959d0d29110SBjoern A. Zeeb 
4960eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
49616b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
49629d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49639d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4964d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4965d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4966d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4967fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4968d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4969d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4970d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4971d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
49729d9ba2b7SBjoern A. Zeeb #endif
4973eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
4974cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
4975d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
4976cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
49776b4cac81SBjoern A. Zeeb 	return;
49786b4cac81SBjoern A. Zeeb 
49796b4cac81SBjoern A. Zeeb ops_tx:
49809d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
49819d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4982d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4983d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
49846b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
49856b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
49869d9ba2b7SBjoern A. Zeeb #endif
49876b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
49886b4cac81SBjoern A. Zeeb 	control.sta = sta;
4989cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
49906b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
4991cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
49926b4cac81SBjoern A. Zeeb }
49936b4cac81SBjoern A. Zeeb 
49946b4cac81SBjoern A. Zeeb static void
49956b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
49966b4cac81SBjoern A. Zeeb {
49976b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49986b4cac81SBjoern A. Zeeb 	struct mbufq mq;
49996b4cac81SBjoern A. Zeeb 	struct mbuf *m;
500088665349SBjoern A. Zeeb 	bool shall_tx;
50016b4cac81SBjoern A. Zeeb 
50026b4cac81SBjoern A. Zeeb 	lsta = ctx;
50036b4cac81SBjoern A. Zeeb 
50049d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
50059d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
50066b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
50079d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
50086b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
50099d9ba2b7SBjoern A. Zeeb #endif
50106b4cac81SBjoern A. Zeeb 
50116b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
50126b4cac81SBjoern A. Zeeb 
5013fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
5014fa4e4257SBjoern A. Zeeb 	/*
5015fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
501688665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
501788665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
501888665349SBjoern A. Zeeb 	 * point to try to TX.
501988665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
502088665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
5021fa4e4257SBjoern A. Zeeb 	 */
50222372f8ccSBjoern A. Zeeb #if 0
502388665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
50242372f8ccSBjoern A. Zeeb #else
50252372f8ccSBjoern A. Zeeb 	/*
50262372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
50272372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
50282372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
50292372f8ccSBjoern A. Zeeb 	 */
50302372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
50312372f8ccSBjoern A. Zeeb #endif
503288665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
50336b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
503488665349SBjoern A. Zeeb 	/*
503588665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
503688665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
503788665349SBjoern A. Zeeb 	 */
5038fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
50396b4cac81SBjoern A. Zeeb 
50406b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
50416b4cac81SBjoern A. Zeeb 	while (m != NULL) {
50426b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
50436b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
50446b4cac81SBjoern A. Zeeb 	}
50456b4cac81SBjoern A. Zeeb }
50466b4cac81SBjoern A. Zeeb 
50476b4cac81SBjoern A. Zeeb static int
50486b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
50496b4cac81SBjoern A. Zeeb {
50506b4cac81SBjoern A. Zeeb 
50516b4cac81SBjoern A. Zeeb 	/* XXX TODO */
50526b4cac81SBjoern A. Zeeb 	IMPROVE();
50536b4cac81SBjoern A. Zeeb 
50546b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
50556b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
50566b4cac81SBjoern A. Zeeb 
50576b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
50589a45c3caSBjoern A. Zeeb 	return (lkpi_xmit(ni, m, NULL, false));
50596b4cac81SBjoern A. Zeeb }
50606b4cac81SBjoern A. Zeeb 
50619fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50629fb91463SBjoern A. Zeeb static int
50639fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
50649fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
50659fb91463SBjoern A. Zeeb {
50669fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50679fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50689fb91463SBjoern A. Zeeb 
50699fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50709fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50719fb91463SBjoern A. Zeeb 
5072310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
50739fb91463SBjoern A. Zeeb 
50749fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
50759fb91463SBjoern A. Zeeb }
50769fb91463SBjoern A. Zeeb 
50779fb91463SBjoern A. Zeeb static int
50789fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
50799fb91463SBjoern A. Zeeb {
50809fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50819fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50829fb91463SBjoern A. Zeeb 
50839fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50849fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50859fb91463SBjoern A. Zeeb 
5086310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
50879fb91463SBjoern A. Zeeb 
50889fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
50899fb91463SBjoern A. Zeeb }
50909fb91463SBjoern A. Zeeb 
50919fb91463SBjoern A. Zeeb 
50929fb91463SBjoern A. Zeeb static int
50939fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
50949fb91463SBjoern A. Zeeb {
50959fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50969fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50979fb91463SBjoern A. Zeeb 
50989fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50999fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
51009fb91463SBjoern A. Zeeb 
5101310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
51029fb91463SBjoern A. Zeeb 
51039fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
51049fb91463SBjoern A. Zeeb }
51059fb91463SBjoern A. Zeeb 
5106310743c4SBjoern A. Zeeb /*
5107310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5108310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5109310743c4SBjoern A. Zeeb  *
5110310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
5111310743c4SBjoern A. Zeeb  */
51129fb91463SBjoern A. Zeeb static int
51139fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
51149fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
51159fb91463SBjoern A. Zeeb {
51169fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51179fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5118310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5119310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5120310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5121310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5122310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5123310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5124310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5125310743c4SBjoern A. Zeeb 	int error;
51269fb91463SBjoern A. Zeeb 
51279fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51289fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5129310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5130310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5131310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5132310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5133310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5134310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51359fb91463SBjoern A. Zeeb 
5136310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5137310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5138310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5139310743c4SBjoern A. Zeeb 		return (0);
5140310743c4SBjoern A. Zeeb 	}
5141310743c4SBjoern A. Zeeb 
5142310743c4SBjoern A. Zeeb 	params.sta = sta;
5143310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
5144310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
5145310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5146310743c4SBjoern A. Zeeb 	params.timeout = 0;
5147310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5148310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5149310743c4SBjoern A. Zeeb 	params.amsdu = false;
5150310743c4SBjoern A. Zeeb 
5151310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5152cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5153310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5154cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5155310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5156310743c4SBjoern A. Zeeb 	if (error != 0) {
5157310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5158310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5159310743c4SBjoern A. Zeeb 		return (0);
5160310743c4SBjoern A. Zeeb 	}
51619fb91463SBjoern A. Zeeb 
51629fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
51639fb91463SBjoern A. Zeeb }
51649fb91463SBjoern A. Zeeb 
5165310743c4SBjoern A. Zeeb /*
5166310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5167310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
5168310743c4SBjoern A. Zeeb  *
5169310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
5170310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
5171310743c4SBjoern A. Zeeb  */
51729fb91463SBjoern A. Zeeb static int
51739fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
51749fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
51759fb91463SBjoern A. Zeeb {
51769fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
51779fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5178310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5179310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5180310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5181310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5182310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5183310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5184310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5185310743c4SBjoern A. Zeeb 	int error;
51869fb91463SBjoern A. Zeeb 
51879fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
51889fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5189310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5190310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5191310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5192310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5193310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5194310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51959fb91463SBjoern A. Zeeb 
5196310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5197310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5198310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5199310743c4SBjoern A. Zeeb 		return (0);
5200310743c4SBjoern A. Zeeb 	}
5201310743c4SBjoern A. Zeeb 
5202310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
5203310743c4SBjoern A. Zeeb 		params.sta = sta;
5204310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5205310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
5206310743c4SBjoern A. Zeeb 		params.timeout = 0;
5207310743c4SBjoern A. Zeeb 		params.ssn = 0;
5208310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5209310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5210310743c4SBjoern A. Zeeb 			params.amsdu = true;
5211310743c4SBjoern A. Zeeb 		else
5212310743c4SBjoern A. Zeeb 			params.amsdu = false;
5213310743c4SBjoern A. Zeeb 	} else {
5214310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
5215310743c4SBjoern A. Zeeb 		params.sta = sta;
5216310743c4SBjoern A. Zeeb 		switch (status) {
5217310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
5218310743c4SBjoern A. Zeeb 		default:
5219310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5220310743c4SBjoern A. Zeeb 			break;
5221310743c4SBjoern A. Zeeb 		}
5222310743c4SBjoern A. Zeeb 		params.buf_size = 0;
5223310743c4SBjoern A. Zeeb 		params.timeout = 0;
5224310743c4SBjoern A. Zeeb 		params.ssn = 0;
5225310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
5226310743c4SBjoern A. Zeeb 		params.amsdu = false;
5227310743c4SBjoern A. Zeeb 	}
5228310743c4SBjoern A. Zeeb 
5229310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5230cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5231310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5232cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5233310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5234310743c4SBjoern A. Zeeb 	if (error != 0) {
5235310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5236310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5237310743c4SBjoern A. Zeeb 		return (0);
5238310743c4SBjoern A. Zeeb 	}
5239310743c4SBjoern A. Zeeb 
5240310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
52419fb91463SBjoern A. Zeeb 
52429fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
52439fb91463SBjoern A. Zeeb }
52449fb91463SBjoern A. Zeeb 
5245310743c4SBjoern A. Zeeb /*
5246310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5247310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5248310743c4SBjoern A. Zeeb  */
52499fb91463SBjoern A. Zeeb static void
52509fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
52519fb91463SBjoern A. Zeeb {
52529fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
52539fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5254310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
5255310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
5256310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5257310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5258310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
5259310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5260310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
5261310743c4SBjoern A. Zeeb 	int error;
52629fb91463SBjoern A. Zeeb 
52639fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
52649fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
5265310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
5266310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
5267310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5268310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5269310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
5270310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
52719fb91463SBjoern A. Zeeb 
5272310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5273310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5274310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
5275310743c4SBjoern A. Zeeb 		goto n80211;
5276310743c4SBjoern A. Zeeb 	}
52779fb91463SBjoern A. Zeeb 
5278310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
5279310743c4SBjoern A. Zeeb 	params.sta = sta;
5280310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
5281310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5282310743c4SBjoern A. Zeeb 	params.buf_size = 0;
5283310743c4SBjoern A. Zeeb 	params.timeout = 0;
5284310743c4SBjoern A. Zeeb 	params.ssn = 0;
5285310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
5286310743c4SBjoern A. Zeeb 	params.amsdu = false;
5287310743c4SBjoern A. Zeeb 
5288310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
5289cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
5290310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5291cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
5292310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
5293310743c4SBjoern A. Zeeb 	if (error != 0) {
5294310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5295310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
5296310743c4SBjoern A. Zeeb 		goto n80211;
5297310743c4SBjoern A. Zeeb 	}
5298310743c4SBjoern A. Zeeb 
5299310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
5300310743c4SBjoern A. Zeeb 
5301310743c4SBjoern A. Zeeb n80211:
53029fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
53039fb91463SBjoern A. Zeeb }
53049fb91463SBjoern A. Zeeb 
53059fb91463SBjoern A. Zeeb static void
53069fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
53079fb91463SBjoern A. Zeeb {
53089fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53099fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53109fb91463SBjoern A. Zeeb 
53119fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53129fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53139fb91463SBjoern A. Zeeb 
53149fb91463SBjoern A. Zeeb 	IMPROVE_HT();
53159fb91463SBjoern A. Zeeb 
53169fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
53179fb91463SBjoern A. Zeeb }
53189fb91463SBjoern A. Zeeb 
53199fb91463SBjoern A. Zeeb static void
53209fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
53219fb91463SBjoern A. Zeeb     int status)
53229fb91463SBjoern A. Zeeb {
53239fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53249fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53259fb91463SBjoern A. Zeeb 
53269fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53279fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53289fb91463SBjoern A. Zeeb 
53299fb91463SBjoern A. Zeeb 	IMPROVE_HT();
53309fb91463SBjoern A. Zeeb 
53319fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
53329fb91463SBjoern A. Zeeb }
53339fb91463SBjoern A. Zeeb 
53349fb91463SBjoern A. Zeeb static int
53359fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
53369fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
53379fb91463SBjoern A. Zeeb {
53389fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
53399fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53409fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
53419fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
53429fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
53439fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
53449fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
53459fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5346310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
53479fb91463SBjoern A. Zeeb 	int error;
53489fb91463SBjoern A. Zeeb 
53499fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
53509fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
53519fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53529fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
53539fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
53549fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
53559fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
53569fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
53579fb91463SBjoern A. Zeeb 
5358310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5359310743c4SBjoern A. Zeeb 
5360310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5361310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5362310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5363310743c4SBjoern A. Zeeb 		return (-ENXIO);
5364310743c4SBjoern A. Zeeb 	}
5365310743c4SBjoern A. Zeeb 
53669fb91463SBjoern A. Zeeb 	params.sta = sta;
53679fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
53689fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
53699fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
53709fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
53719fb91463SBjoern A. Zeeb 	else
53729fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5373310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5374310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
53759fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
53769fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
53779fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
53789fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5379310743c4SBjoern A. Zeeb 
5380310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5381310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5382310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5383310743c4SBjoern A. Zeeb 		params.amsdu = true;
5384310743c4SBjoern A. Zeeb 	else
53859fb91463SBjoern A. Zeeb 		params.amsdu = false;
53869fb91463SBjoern A. Zeeb 
5387cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
53889fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5389cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
53909fb91463SBjoern A. Zeeb 	if (error != 0) {
53919fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
53929fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
53939fb91463SBjoern A. Zeeb 		return (error);
53949fb91463SBjoern A. Zeeb 	}
5395310743c4SBjoern A. Zeeb 
5396310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5397310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5398310743c4SBjoern A. Zeeb 	}
5399310743c4SBjoern A. Zeeb 
54009fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
54019fb91463SBjoern A. Zeeb 
54029fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
54039fb91463SBjoern A. Zeeb 	return (error);
54049fb91463SBjoern A. Zeeb }
54059fb91463SBjoern A. Zeeb 
54069fb91463SBjoern A. Zeeb static void
54079fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
54089fb91463SBjoern A. Zeeb {
54099fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
54109fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54119fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
54129fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
54139fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
54149fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
54159fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
54169fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5417310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
54189fb91463SBjoern A. Zeeb 	int error;
54199fb91463SBjoern A. Zeeb 	uint8_t tid;
542065c573e4SBjoern A. Zeeb 	bool ic_locked;
54219fb91463SBjoern A. Zeeb 
54229fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
54239fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
54249fb91463SBjoern A. Zeeb 
54259fb91463SBjoern A. Zeeb 	/*
54269fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
54279fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
54289fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
54299fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
54309fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
54319fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
54329fb91463SBjoern A. Zeeb 	 * track some state itself.
54339fb91463SBjoern A. Zeeb 	 */
54349fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
54359fb91463SBjoern A. Zeeb 		goto net80211_only;
54369fb91463SBjoern A. Zeeb 
54379fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
54389fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
54399fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
54409fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
54419fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
54429fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
54439fb91463SBjoern A. Zeeb 
54449fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
54459fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
54469fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
54479fb91463SBjoern A. Zeeb 			break;
54489fb91463SBjoern A. Zeeb 	}
54499fb91463SBjoern A. Zeeb 
54509fb91463SBjoern A. Zeeb 	params.sta = sta;
54519fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
54529fb91463SBjoern A. Zeeb 	params.buf_size = 0;
54539fb91463SBjoern A. Zeeb 	params.timeout = 0;
54549fb91463SBjoern A. Zeeb 	params.ssn = 0;
54559fb91463SBjoern A. Zeeb 	params.tid = tid;
54569fb91463SBjoern A. Zeeb 	params.amsdu = false;
54579fb91463SBjoern A. Zeeb 
545865c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
545965c573e4SBjoern A. Zeeb 	if (ic_locked)
546065c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5461cd0fcf9fSBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
54629fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5463cd0fcf9fSBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
546465c573e4SBjoern A. Zeeb 	if (ic_locked)
546565c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
54669fb91463SBjoern A. Zeeb 	if (error != 0)
54679fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
54689fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
54699fb91463SBjoern A. Zeeb 
54709fb91463SBjoern A. Zeeb net80211_only:
54719fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
54729fb91463SBjoern A. Zeeb }
54739fb91463SBjoern A. Zeeb #endif
54749fb91463SBjoern A. Zeeb 
54759fb91463SBjoern A. Zeeb static void
54769fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
54779fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
54789fb91463SBjoern A. Zeeb {
54799fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
54809fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
54819fb91463SBjoern A. Zeeb 
54829fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
54839fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
54849fb91463SBjoern A. Zeeb 		return;
54859fb91463SBjoern A. Zeeb 
54869fb91463SBjoern A. Zeeb 	switch (band) {
54879fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
54889fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
54899fb91463SBjoern A. Zeeb 		break;
54909fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
54919fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
54929fb91463SBjoern A. Zeeb 		break;
54939fb91463SBjoern A. Zeeb 	default:
54949fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
54959fb91463SBjoern A. Zeeb 		return;
54969fb91463SBjoern A. Zeeb 	}
54979fb91463SBjoern A. Zeeb 
54989fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
54999fb91463SBjoern A. Zeeb 
55009fb91463SBjoern A. Zeeb 	/*
55019fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
55029fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
55039fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
55049fb91463SBjoern A. Zeeb 	 */
55059fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
55069fb91463SBjoern A. Zeeb 
55079fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
55089fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
55099fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
55109fb91463SBjoern A. Zeeb #ifdef __notyet__
55119fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
55129fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
55139fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
55149fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
55159fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
55169fb91463SBjoern A. Zeeb #endif
55179fb91463SBjoern A. Zeeb 
55189fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
55199fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
55209fb91463SBjoern A. Zeeb 
55219fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
55229fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
55239fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
55249fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
55259fb91463SBjoern A. Zeeb #endif
55269fb91463SBjoern A. Zeeb }
55279fb91463SBjoern A. Zeeb 
55286b4cac81SBjoern A. Zeeb static void
55296b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
55306b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
55316b4cac81SBjoern A. Zeeb {
55326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
55336b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
55346b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
55356b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
55366b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
55376b4cac81SBjoern A. Zeeb 
55386b4cac81SBjoern A. Zeeb 	/* Channels */
55396b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
55406b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
55416b4cac81SBjoern A. Zeeb 
55426b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
55436b4cac81SBjoern A. Zeeb 	nchans = 0;
55446b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
55456b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
55466b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
55476b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
55486b4cac81SBjoern A. Zeeb 		chan_flags = 0;
55496b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
55506b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
55519fb91463SBjoern A. Zeeb 
55529fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
55536b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
55549fb91463SBjoern A. Zeeb 
55559fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
55569fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
55576b4cac81SBjoern A. Zeeb 
55586b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5559cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
55606b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
55616b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
55626b4cac81SBjoern A. Zeeb 
55636b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
55643f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
55653f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
55666b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
55676b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
55686b4cac81SBjoern A. Zeeb 				continue;
55696b4cac81SBjoern A. Zeeb 			}
55706b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
55716b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
55726b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
55736b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
55746b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
55756b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
55766b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
55776b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
55786b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
55796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
55806b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
55816b4cac81SBjoern A. Zeeb 
55826b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
55836b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
55846b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
55855856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5586cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5587cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
55883f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
55893f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
55906b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
55916b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
55926b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5593cee56e77SBjoern A. Zeeb 			if (error != 0)
55946b4cac81SBjoern A. Zeeb 				break;
55956b4cac81SBjoern A. Zeeb 		}
55966b4cac81SBjoern A. Zeeb 	}
55976b4cac81SBjoern A. Zeeb 
55986b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
55996b4cac81SBjoern A. Zeeb 	nchans = 0;
56006b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
56016b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
56026b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
56036b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
56046b4cac81SBjoern A. Zeeb 		chan_flags = 0;
56056b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
56069fb91463SBjoern A. Zeeb 
56079fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
56089fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
56099fb91463SBjoern A. Zeeb 
56109fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
56116b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
56126b4cac81SBjoern A. Zeeb 
56136b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5614562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
56156b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5616ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5617ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
56186b4cac81SBjoern A. Zeeb 
56196b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
56206b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
56216b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5622562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
56236b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
56246b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5625562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
56266b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
56276b4cac81SBjoern A. Zeeb 		}
56286b4cac81SBjoern A. Zeeb #endif
56296b4cac81SBjoern A. Zeeb 
56306b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5631cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
56326b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
56336b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
56346b4cac81SBjoern A. Zeeb 
56356b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
56363f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
56373f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
56386b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
56396b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
56406b4cac81SBjoern A. Zeeb 				continue;
56416b4cac81SBjoern A. Zeeb 			}
56426b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
56436b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
56446b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
56456b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
56466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
56476b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
56486b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
56496b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
56506b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
56516b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
56526b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
56536b4cac81SBjoern A. Zeeb 
56546b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
56556b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
56566b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
56575856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5658cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5659cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
56603f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
56613f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
56626b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
56636b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
56646b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5665cee56e77SBjoern A. Zeeb 			if (error != 0)
56666b4cac81SBjoern A. Zeeb 				break;
56676b4cac81SBjoern A. Zeeb 		}
56686b4cac81SBjoern A. Zeeb 	}
56696b4cac81SBjoern A. Zeeb }
56706b4cac81SBjoern A. Zeeb 
56716b4cac81SBjoern A. Zeeb static void *
56726b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
56736b4cac81SBjoern A. Zeeb {
56746b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
56756b4cac81SBjoern A. Zeeb 
56766b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
56776b4cac81SBjoern A. Zeeb 
56786b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
56796b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
56806b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
56816b4cac81SBjoern A. Zeeb 
56826b4cac81SBjoern A. Zeeb 	return (ic);
56836b4cac81SBjoern A. Zeeb }
56846b4cac81SBjoern A. Zeeb 
56856b4cac81SBjoern A. Zeeb struct ieee80211_hw *
56866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
56876b4cac81SBjoern A. Zeeb {
56886b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
56896b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56906b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5691a5ae63edSBjoern A. Zeeb 	int ac;
56926b4cac81SBjoern A. Zeeb 
56936b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
56946b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
56956b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
56966b4cac81SBjoern A. Zeeb 		return (NULL);
56976b4cac81SBjoern A. Zeeb 
56986b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
56996b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5700652e22d3SBjoern A. Zeeb 
57018ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5702eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
57038891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
57046b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5705a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5706a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5707a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5708a5ae63edSBjoern A. Zeeb 	}
57096b4cac81SBjoern A. Zeeb 
5710a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf */
5711a8a47a41SBjoern A. Zeeb 	INIT_LIST_HEAD(&lhw->lchanctx_list);
5712a8a47a41SBjoern A. Zeeb 
5713759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5714759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5715759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5716832b8e98SBjoern A. Zeeb 	mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
5717759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5718759a996dSBjoern A. Zeeb 
57196b4cac81SBjoern A. Zeeb 	/*
57206b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
57216b4cac81SBjoern A. Zeeb 	 * not initialized.
57226b4cac81SBjoern A. Zeeb 	 */
57236b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
57246b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5725086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
57266b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
57276b4cac81SBjoern A. Zeeb 
57286b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
57296b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
57306b4cac81SBjoern A. Zeeb 
57316b4cac81SBjoern A. Zeeb 	IMPROVE();
57326b4cac81SBjoern A. Zeeb 
57336b4cac81SBjoern A. Zeeb 	return (hw);
57346b4cac81SBjoern A. Zeeb }
57356b4cac81SBjoern A. Zeeb 
57366b4cac81SBjoern A. Zeeb void
57376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
57386b4cac81SBjoern A. Zeeb {
57396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5740759a996dSBjoern A. Zeeb 	struct mbuf *m;
57416b4cac81SBjoern A. Zeeb 
57426b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57436b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
57446b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
57456b4cac81SBjoern A. Zeeb 
5746759a996dSBjoern A. Zeeb 	/*
5747759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5748759a996dSBjoern A. Zeeb 	 */
5749759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5750759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5751759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5752759a996dSBjoern A. Zeeb 
5753759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5754759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5755759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5756759a996dSBjoern A. Zeeb 
5757759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5758759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5759759a996dSBjoern A. Zeeb 	while (m != NULL) {
5760dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
5761759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5762759a996dSBjoern A. Zeeb 
5763759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5764759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5765759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5766759a996dSBjoern A. Zeeb 
5767759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5768759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5769759a996dSBjoern A. Zeeb 		}
5770dbae3dcfSBjoern A. Zeeb #else
5771dbae3dcfSBjoern A. Zeeb 		if (m->m_pkthdr.PH_loc.ptr != NULL) {
5772dbae3dcfSBjoern A. Zeeb 			struct ieee80211_node *ni;
5773dbae3dcfSBjoern A. Zeeb 
5774dbae3dcfSBjoern A. Zeeb 			ni = m->m_pkthdr.PH_loc.ptr;
5775dbae3dcfSBjoern A. Zeeb 			ieee80211_free_node(ni);
5776dbae3dcfSBjoern A. Zeeb 		}
5777dbae3dcfSBjoern A. Zeeb #endif
5778759a996dSBjoern A. Zeeb 		m_freem(m);
5779759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5780759a996dSBjoern A. Zeeb 	}
5781759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5782759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5783759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5784759a996dSBjoern A. Zeeb 
5785a8a47a41SBjoern A. Zeeb 	/* Chanctx_conf. */
5786a8a47a41SBjoern A. Zeeb 	if (!list_empty_careful(&lhw->lchanctx_list)) {
5787a8a47a41SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx, *next;
5788a8a47a41SBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
5789a8a47a41SBjoern A. Zeeb 
5790a8a47a41SBjoern A. Zeeb 		list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
5791a8a47a41SBjoern A. Zeeb 			if (lchanctx->added_to_drv) {
5792a8a47a41SBjoern A. Zeeb 				/* In reality we should panic? */
5793a8a47a41SBjoern A. Zeeb 				chanctx_conf = &lchanctx->chanctx_conf;
5794a8a47a41SBjoern A. Zeeb 				lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
5795a8a47a41SBjoern A. Zeeb 			}
5796a8a47a41SBjoern A. Zeeb 			list_del(&lchanctx->entry);
5797a8a47a41SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
5798a8a47a41SBjoern A. Zeeb 		}
5799a8a47a41SBjoern A. Zeeb 	}
5800a8a47a41SBjoern A. Zeeb 
58016b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5802eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
58038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5804eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
58056b4cac81SBjoern A. Zeeb 	IMPROVE();
58066b4cac81SBjoern A. Zeeb }
58076b4cac81SBjoern A. Zeeb 
58086b4cac81SBjoern A. Zeeb void
58096b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
58106b4cac81SBjoern A. Zeeb {
58116b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58126b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58136b4cac81SBjoern A. Zeeb 
58146b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58156b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58166b4cac81SBjoern A. Zeeb 
58176b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
58186b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
58196b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
58206b4cac81SBjoern A. Zeeb 
58216b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
58226b4cac81SBjoern A. Zeeb }
58236b4cac81SBjoern A. Zeeb 
58246b4cac81SBjoern A. Zeeb struct ieee80211_hw *
58256b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
58266b4cac81SBjoern A. Zeeb {
58276b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58286b4cac81SBjoern A. Zeeb 
58296b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58306b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
58316b4cac81SBjoern A. Zeeb }
58326b4cac81SBjoern A. Zeeb 
58336b4cac81SBjoern A. Zeeb static void
58346b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
58356b4cac81SBjoern A. Zeeb {
58366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58376b4cac81SBjoern A. Zeeb 
58386b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58396b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
58406b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
58416b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
58426b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
58436b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
58446b4cac81SBjoern A. Zeeb }
58456b4cac81SBjoern A. Zeeb 
58462e183d99SBjoern A. Zeeb int
58476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
58486b4cac81SBjoern A. Zeeb {
58496b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58506b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5851c5b96b3eSBjoern A. Zeeb 	int band, i;
58526b4cac81SBjoern A. Zeeb 
58536b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58546b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58556b4cac81SBjoern A. Zeeb 
5856652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5857652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5858652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5859652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5860652e22d3SBjoern A. Zeeb 
58616b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
58626b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
58636b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
58646b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
58656b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
58666b4cac81SBjoern A. Zeeb 		/* We take the first one. */
58676b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
58686b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
58696b4cac81SBjoern A. Zeeb 	} else {
58706b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
58716b4cac81SBjoern A. Zeeb 	}
58726b4cac81SBjoern A. Zeeb 
58733d09d310SBjoern A. Zeeb #ifdef __not_yet__
58743d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
58756b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
58763d09d310SBjoern A. Zeeb #endif
58776b4cac81SBjoern A. Zeeb 
58786b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
58796b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
58806b4cac81SBjoern A. Zeeb 
58816b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
58826b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
58836b4cac81SBjoern A. Zeeb 	ic->ic_caps =
58846b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
58856b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
58866b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
58874a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
58886b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
58894a67f1dfSBjoern A. Zeeb #endif
58906b4cac81SBjoern A. Zeeb #if 0
58916b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
58926b4cac81SBjoern A. Zeeb #endif
58936b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
58946b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
58956b4cac81SBjoern A. Zeeb 	    ;
58966b4cac81SBjoern A. Zeeb #if 0
58976b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
58986b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
58996b4cac81SBjoern A. Zeeb #endif
5900cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5901cc4e78d5SBjoern A. Zeeb 		/*
5902cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5903cc4e78d5SBjoern A. Zeeb 		 *
5904cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5905cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5906cc4e78d5SBjoern A. Zeeb 		 * the flag.
5907cc4e78d5SBjoern A. Zeeb 		 */
59086b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5909a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
59106b4cac81SBjoern A. Zeeb 	}
59116b4cac81SBjoern A. Zeeb 
591263578bf2SBjoern A. Zeeb 	/* Does HW support Fragmentation offload? */
591363578bf2SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
591463578bf2SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
591563578bf2SBjoern A. Zeeb 
5916527687a9SBjoern A. Zeeb 	/*
5917527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5918527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5919527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5920527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5921527687a9SBjoern A. Zeeb 	 */
5922527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5923527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5924527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5925527687a9SBjoern A. Zeeb 
5926527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5927527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5928527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5929527687a9SBjoern A. Zeeb 		}
5930527687a9SBjoern A. Zeeb 	}
5931527687a9SBjoern A. Zeeb 
59326b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5933b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
593411db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
5935bf8c25f1SBjoern A. Zeeb 		uint32_t hwciphers;
5936bf8c25f1SBjoern A. Zeeb 
5937bf8c25f1SBjoern A. Zeeb 		hwciphers = 0;
59386b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
5939bf8c25f1SBjoern A. Zeeb 			hwciphers |= lkpi_l80211_to_net80211_cyphers(
5940bf8c25f1SBjoern A. Zeeb 			    ic, hw->wiphy->cipher_suites[i]);
5941bf8c25f1SBjoern A. Zeeb 		/*
5942bf8c25f1SBjoern A. Zeeb 		 * (20250415) nothing anywhere in the path checks we actually
5943bf8c25f1SBjoern A. Zeeb 		 * support all these in net80211.
5944bf8c25f1SBjoern A. Zeeb 		 * net80211 supports _256 variants but the ioctl does not.
5945bf8c25f1SBjoern A. Zeeb 		 */
5946bf8c25f1SBjoern A. Zeeb 		IMPROVE("as net80211 grows more support, enable them");
5947bf8c25f1SBjoern A. Zeeb 		hwciphers &= (IEEE80211_CRYPTO_WEP | IEEE80211_CRYPTO_TKIP |
5948bf8c25f1SBjoern A. Zeeb 		    IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
5949bf8c25f1SBjoern A. Zeeb 		/* We only support CCMP here, so further filter. */
5950bf8c25f1SBjoern A. Zeeb 		hwciphers &= IEEE80211_CRYPTO_AES_CCM;
5951bf8c25f1SBjoern A. Zeeb 		ieee80211_set_hardware_ciphers(ic, hwciphers);
59526b4cac81SBjoern A. Zeeb 	}
59536b4cac81SBjoern A. Zeeb #endif
59546b4cac81SBjoern A. Zeeb 
59556b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
59566b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
59576b4cac81SBjoern A. Zeeb 
59586b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
59596b4cac81SBjoern A. Zeeb 
59606b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
59616b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
59626b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
59636b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
59646b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
59656b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
59666b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
59676b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
59686b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
59696b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
59706b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
59716b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
59726b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
59736b4cac81SBjoern A. Zeeb 
5974a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5975a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5976a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5977a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5978a486fbbdSBjoern A. Zeeb 
59796b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
59806b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
59816b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
59826b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
59836b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
59846b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
59856b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
59866b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
59876b4cac81SBjoern A. Zeeb 
59889fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
598986bc7259SBjoern A. Zeeb 	/*
599086bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
599186bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
599286bc7259SBjoern A. Zeeb 	 */
599386bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
59949fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
59959fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
59969fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
59979fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
59989fb91463SBjoern A. Zeeb 
59999fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
60009fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
60019fb91463SBjoern A. Zeeb 
60029fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
60039fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
60049fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
60059fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
60069fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
60079fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
60089fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
60099fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
60109fb91463SBjoern A. Zeeb 
60119fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
60129fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
60139fb91463SBjoern A. Zeeb 
60149fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
60159fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
60169fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
60179fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
601886bc7259SBjoern A. Zeeb 	}
60199fb91463SBjoern A. Zeeb #endif
60209fb91463SBjoern A. Zeeb 
60216b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
60226b4cac81SBjoern A. Zeeb 
6023c5b96b3eSBjoern A. Zeeb 	/*
6024c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
6025c5b96b3eSBjoern A. Zeeb 	 * expect one.
6026d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
6027d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6028c5b96b3eSBjoern A. Zeeb 	 */
6029d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
6030f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
6031c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
6032c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
6033c5b96b3eSBjoern A. Zeeb 
6034c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
6035c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
6036c5b96b3eSBjoern A. Zeeb 			continue;
6037c5b96b3eSBjoern A. Zeeb 
6038d9945d78SBjoern A. Zeeb 		lhw->supbands++;
6039d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6040d9945d78SBjoern A. Zeeb 
6041f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
6042f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
6043f454a4a1SBjoern A. Zeeb 			continue;
6044f454a4a1SBjoern A. Zeeb 
6045c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
6046c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
6047c5b96b3eSBjoern A. Zeeb 
6048c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6049c5b96b3eSBjoern A. Zeeb 				continue;
6050c5b96b3eSBjoern A. Zeeb 
60514a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
60529fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
605311450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
60549fb91463SBjoern A. Zeeb #endif
6055c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
6056c5b96b3eSBjoern A. Zeeb 			break;
6057c5b96b3eSBjoern A. Zeeb 		}
6058c5b96b3eSBjoern A. Zeeb 	}
6059c5b96b3eSBjoern A. Zeeb 
6060d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6061d9945d78SBjoern A. Zeeb 
6062d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
6063d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6064d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6065d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6066d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6067d9945d78SBjoern A. Zeeb 	}
6068d9945d78SBjoern A. Zeeb 
6069d9945d78SBjoern A. Zeeb 	/*
6070d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
6071d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
6072d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
6073d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
6074d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
6075d9945d78SBjoern A. Zeeb 	 */
6076d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6077d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
6078d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
607978ca45dfSBjoern A. Zeeb 
608078ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6081d9945d78SBjoern A. Zeeb 		/*
608278ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
608378ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
608478ca45dfSBjoern A. Zeeb 		 * space in.
6085d9945d78SBjoern A. Zeeb 		 */
6086d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
608778ca45dfSBjoern A. Zeeb 	}
6088d9945d78SBjoern A. Zeeb 
60899fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
60909fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
60919fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
60929fb91463SBjoern A. Zeeb #endif
60939fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
60941f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
60959fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
60969fb91463SBjoern A. Zeeb #endif
60979fb91463SBjoern A. Zeeb 
6098d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
609978ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
610078ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
610178ca45dfSBjoern A. Zeeb 			goto err;
6102d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
610378ca45dfSBjoern A. Zeeb 	}
6104d9945d78SBjoern A. Zeeb 
61056b4cac81SBjoern A. Zeeb 	if (bootverbose)
61066b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
61072e183d99SBjoern A. Zeeb 
61082e183d99SBjoern A. Zeeb 	return (0);
610978ca45dfSBjoern A. Zeeb err:
611078ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
611178ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
61126b4cac81SBjoern A. Zeeb }
61136b4cac81SBjoern A. Zeeb 
61146b4cac81SBjoern A. Zeeb void
61156b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
61166b4cac81SBjoern A. Zeeb {
61176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61196b4cac81SBjoern A. Zeeb 
61206b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61216b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
61226b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
61236b4cac81SBjoern A. Zeeb }
61246b4cac81SBjoern A. Zeeb 
61256b4cac81SBjoern A. Zeeb void
61266b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
61276b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
61286b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
61296b4cac81SBjoern A. Zeeb     void *arg)
61306b4cac81SBjoern A. Zeeb {
61316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61326b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
61336b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
613461a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
61356b4cac81SBjoern A. Zeeb 
61366b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
61376b4cac81SBjoern A. Zeeb 
61386b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
61396b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
614061a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6141adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
61426b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
61436b4cac81SBjoern A. Zeeb 		    __func__, flags);
61446b4cac81SBjoern A. Zeeb 	}
61456b4cac81SBjoern A. Zeeb 
6146adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
61476b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
614861a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
61496b4cac81SBjoern A. Zeeb 
61506b4cac81SBjoern A. Zeeb 	if (atomic)
61518891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
61526b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
61536b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
61546b4cac81SBjoern A. Zeeb 
61556b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
61566b4cac81SBjoern A. Zeeb 
61576b4cac81SBjoern A. Zeeb 		/*
61586b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
61596b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
61606b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
61616b4cac81SBjoern A. Zeeb 		 * driver does not know about.
61626b4cac81SBjoern A. Zeeb 		 */
61636b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
61646b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
61656b4cac81SBjoern A. Zeeb 			continue;
61666b4cac81SBjoern A. Zeeb 
61676b4cac81SBjoern A. Zeeb 		/*
616861a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
616961a68e50SBjoern A. Zeeb 		 * if we haven't yet.
617061a68e50SBjoern A. Zeeb 		 */
617161a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
617261a68e50SBjoern A. Zeeb 			continue;
617361a68e50SBjoern A. Zeeb 
617461a68e50SBjoern A. Zeeb 		/*
61756b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
61766b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
61776b4cac81SBjoern A. Zeeb 		 */
61786b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
61796b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
61806b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
61816b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
61826b4cac81SBjoern A. Zeeb 	}
61836b4cac81SBjoern A. Zeeb 	if (atomic)
61848891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
61856b4cac81SBjoern A. Zeeb }
61866b4cac81SBjoern A. Zeeb 
618711db70b6SBjoern A. Zeeb static void
618811db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
618911db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
619011db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
619111db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
619211db70b6SBjoern A. Zeeb     void *arg)
619311db70b6SBjoern A. Zeeb {
619411db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
619511db70b6SBjoern A. Zeeb 		return;
619611db70b6SBjoern A. Zeeb 
619711db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
619811db70b6SBjoern A. Zeeb 		return;
619911db70b6SBjoern A. Zeeb 
620011db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
620111db70b6SBjoern A. Zeeb }
620211db70b6SBjoern A. Zeeb 
62036b4cac81SBjoern A. Zeeb void
62046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
62056b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
62066b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
62076b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
620811db70b6SBjoern A. Zeeb     void *arg, bool rcu)
62096b4cac81SBjoern A. Zeeb {
621011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
621111db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62126b4cac81SBjoern A. Zeeb 
621311db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
621411db70b6SBjoern A. Zeeb 
621511db70b6SBjoern A. Zeeb 	if (rcu) {
6216a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
6217a8f735a6SBjoern A. Zeeb 
621811db70b6SBjoern A. Zeeb 		if (vif == NULL) {
621911db70b6SBjoern A. Zeeb 			TODO();
622011db70b6SBjoern A. Zeeb 		} else {
6221a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
622211db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
622311db70b6SBjoern A. Zeeb 				    keyix++)
622411db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
622511db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
622611db70b6SBjoern A. Zeeb 			}
622711db70b6SBjoern A. Zeeb 		}
622811db70b6SBjoern A. Zeeb 	} else {
622911db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
623011db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
623111db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
623211db70b6SBjoern A. Zeeb 
623311db70b6SBjoern A. Zeeb 		if (vif == NULL) {
623411db70b6SBjoern A. Zeeb 			TODO();
623511db70b6SBjoern A. Zeeb 		} else {
6236a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
623711db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
623811db70b6SBjoern A. Zeeb 				    keyix++)
623911db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
624011db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
624111db70b6SBjoern A. Zeeb 			}
624211db70b6SBjoern A. Zeeb 		}
624311db70b6SBjoern A. Zeeb 	}
62446b4cac81SBjoern A. Zeeb }
62456b4cac81SBjoern A. Zeeb 
62466b4cac81SBjoern A. Zeeb void
62476b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
62486b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
62496b4cac81SBjoern A. Zeeb 	void *),
62506b4cac81SBjoern A. Zeeb     void *arg)
62516b4cac81SBjoern A. Zeeb {
6252c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
6253c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
62546b4cac81SBjoern A. Zeeb 
6255c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
6256c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6257c5e25798SBjoern A. Zeeb 
6258c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6259c5e25798SBjoern A. Zeeb 
6260a8a47a41SBjoern A. Zeeb 	rcu_read_lock();
6261a8a47a41SBjoern A. Zeeb 	list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6262c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
6263c5e25798SBjoern A. Zeeb 			continue;
6264d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
6265c5e25798SBjoern A. Zeeb 	}
6266a8a47a41SBjoern A. Zeeb 	rcu_read_unlock();
62676b4cac81SBjoern A. Zeeb }
62686b4cac81SBjoern A. Zeeb 
62696b4cac81SBjoern A. Zeeb void
62706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
62716b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
62726b4cac81SBjoern A. Zeeb {
62736b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62746b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
62756b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
62766b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
62776b4cac81SBjoern A. Zeeb 
62786b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
62796b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
62806b4cac81SBjoern A. Zeeb 
62816b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62826b4cac81SBjoern A. Zeeb 
62838891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
62846b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
62856b4cac81SBjoern A. Zeeb 
6286a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
6287a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
62886b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
62896b4cac81SBjoern A. Zeeb 				continue;
62906b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
62916b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
62926b4cac81SBjoern A. Zeeb 		}
6293a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
62946b4cac81SBjoern A. Zeeb 	}
62958891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
62966b4cac81SBjoern A. Zeeb }
62976b4cac81SBjoern A. Zeeb 
6298673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
6299673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
6300673d62fcSBjoern A. Zeeb {
6301673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
6302673d62fcSBjoern A. Zeeb 
6303673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
6304673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
6305673d62fcSBjoern A. Zeeb 	return (regd);
6306673d62fcSBjoern A. Zeeb }
6307673d62fcSBjoern A. Zeeb 
63086b4cac81SBjoern A. Zeeb int
63096b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
63106b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
63116b4cac81SBjoern A. Zeeb {
63126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63136b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
63146b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
63156b4cac81SBjoern A. Zeeb 
63166b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
63176b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63186b4cac81SBjoern A. Zeeb 
63196b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
63206b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
63216b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
63226b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
63236b4cac81SBjoern A. Zeeb 	}
63246b4cac81SBjoern A. Zeeb 
63256b4cac81SBjoern A. Zeeb 	TODO();
63266b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
63276b4cac81SBjoern A. Zeeb 
63286b4cac81SBjoern A. Zeeb 	return (0);
63296b4cac81SBjoern A. Zeeb }
63306b4cac81SBjoern A. Zeeb 
63316b4cac81SBjoern A. Zeeb void
63326b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
63336b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
63346b4cac81SBjoern A. Zeeb {
63356b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
63366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
63376b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
63386b4cac81SBjoern A. Zeeb 
63396b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
63406b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63416b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
63426b4cac81SBjoern A. Zeeb 
63436b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
63446b4cac81SBjoern A. Zeeb 
63458ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
63466b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
63476b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
6348a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
63496b4cac81SBjoern A. Zeeb 	wakeup(lhw);
63508ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
63516b4cac81SBjoern A. Zeeb 
63526b4cac81SBjoern A. Zeeb 	return;
63536b4cac81SBjoern A. Zeeb }
63546b4cac81SBjoern A. Zeeb 
6355759a996dSBjoern A. Zeeb static void
6356759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
6357759a996dSBjoern A. Zeeb {
6358759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
6359dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6360759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
6361dbae3dcfSBjoern A. Zeeb #endif
6362759a996dSBjoern A. Zeeb 	int ok;
6363759a996dSBjoern A. Zeeb 
6364759a996dSBjoern A. Zeeb 	ni = NULL;
6365dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6366759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6367759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
6368759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6369759a996dSBjoern A. Zeeb 
6370759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6371759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6372759a996dSBjoern A. Zeeb 	}
6373dbae3dcfSBjoern A. Zeeb #else
6374dbae3dcfSBjoern A. Zeeb 	if (m->m_pkthdr.PH_loc.ptr != NULL) {
6375dbae3dcfSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6376dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = NULL;
6377dbae3dcfSBjoern A. Zeeb 	}
6378dbae3dcfSBjoern A. Zeeb #endif
6379759a996dSBjoern A. Zeeb 
6380759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6381759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6382759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6383759a996dSBjoern A. Zeeb 		if (ok < 0)
6384759a996dSBjoern A. Zeeb 			m_freem(m);
6385759a996dSBjoern A. Zeeb 	} else {
6386759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6387759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6388759a996dSBjoern A. Zeeb 	}
6389759a996dSBjoern A. Zeeb 
6390759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6391759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6392bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6393759a996dSBjoern A. Zeeb #endif
6394759a996dSBjoern A. Zeeb }
6395759a996dSBjoern A. Zeeb 
6396759a996dSBjoern A. Zeeb static void
6397759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6398759a996dSBjoern A. Zeeb {
6399759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6400759a996dSBjoern A. Zeeb 	struct mbufq mq;
6401759a996dSBjoern A. Zeeb 	struct mbuf *m;
6402759a996dSBjoern A. Zeeb 
6403759a996dSBjoern A. Zeeb 	lhw = ctx;
6404759a996dSBjoern A. Zeeb 
6405759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6406759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6407bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6408bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6409759a996dSBjoern A. Zeeb #endif
6410759a996dSBjoern A. Zeeb 
6411759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6412759a996dSBjoern A. Zeeb 
6413759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6414759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6415759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6416759a996dSBjoern A. Zeeb 
6417759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6418759a996dSBjoern A. Zeeb 	while (m != NULL) {
6419759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6420759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6421759a996dSBjoern A. Zeeb 	}
6422759a996dSBjoern A. Zeeb }
6423759a996dSBjoern A. Zeeb 
642449010ba7SBjoern A. Zeeb static void
6425a1adefb1SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
642649010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
642749010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
642849010ba7SBjoern A. Zeeb     uint8_t *rssip)
642949010ba7SBjoern A. Zeeb {
643049010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
6431a1adefb1SBjoern A. Zeeb 	struct rate_info rxrate;
643249010ba7SBjoern A. Zeeb 	int i;
643349010ba7SBjoern A. Zeeb 	uint8_t rssi;
643449010ba7SBjoern A. Zeeb 
6435a1adefb1SBjoern A. Zeeb 	memset(&rxrate, 0, sizeof(rxrate));
643649010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
643749010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
643849010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
643949010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
644049010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
644149010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
644249010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
644349010ba7SBjoern A. Zeeb 	else
644449010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
644549010ba7SBjoern A. Zeeb 	/*
644649010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
644749010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
644849010ba7SBjoern A. Zeeb 	 */
644949010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
645049010ba7SBjoern A. Zeeb 	if (rssip != NULL)
645149010ba7SBjoern A. Zeeb 		*rssip = rssi;
645249010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
645349010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
645449010ba7SBjoern A. Zeeb 	rx_stats->c_band =
645549010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
645649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
645749010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
645849010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
645949010ba7SBjoern A. Zeeb 
646049010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
646149010ba7SBjoern A. Zeeb 
646249010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
646349010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
646449010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
646549010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
646649010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
646749010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
646849010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
646949010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
647049010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
647149010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
647249010ba7SBjoern A. Zeeb 	}
647349010ba7SBjoern A. Zeeb 
647449010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
647549010ba7SBjoern A. Zeeb 		int cc;
647649010ba7SBjoern A. Zeeb 		int8_t crssi;
647749010ba7SBjoern A. Zeeb 
647849010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
647949010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
648049010ba7SBjoern A. Zeeb 
648149010ba7SBjoern A. Zeeb 		cc = 0;
648249010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
648349010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
648449010ba7SBjoern A. Zeeb 				continue;
648549010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
648649010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
648749010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
648849010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
648949010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
649049010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
649149010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
649249010ba7SBjoern A. Zeeb 			cc++;
649349010ba7SBjoern A. Zeeb 		}
649449010ba7SBjoern A. Zeeb 		if (cc > 0)
649549010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
649649010ba7SBjoern A. Zeeb 	}
649749010ba7SBjoern A. Zeeb 
649849010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
649949010ba7SBjoern A. Zeeb 
650049010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
650149010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
6502a1adefb1SBjoern A. Zeeb 	{
6503a1adefb1SBjoern A. Zeeb 		uint32_t legacy = 0;
6504a1adefb1SBjoern A. Zeeb 
650549010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
650649010ba7SBjoern A. Zeeb 		if (supband != NULL)
6507a1adefb1SBjoern A. Zeeb 			legacy = supband->bitrates[rx_status->rate_idx].bitrate;
6508a1adefb1SBjoern A. Zeeb 		rx_stats->c_rate = legacy;
6509a1adefb1SBjoern A. Zeeb 		rxrate.legacy = legacy;
651049010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
651149010ba7SBjoern A. Zeeb 		break;
6512a1adefb1SBjoern A. Zeeb 	}
651349010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
651449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
651549010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
6516a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_MCS;
6517a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6518a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6519a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6520a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6521a1adefb1SBjoern A. Zeeb 		}
652249010ba7SBjoern A. Zeeb 		break;
652349010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
652449010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
652549010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
652649010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
6527a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
6528a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6529a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6530a1adefb1SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
6531a1adefb1SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
6532a1adefb1SBjoern A. Zeeb 			rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
6533a1adefb1SBjoern A. Zeeb 		}
653449010ba7SBjoern A. Zeeb 		break;
653549010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
6536a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
6537a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6538a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6539a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
6540a1adefb1SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
6541a1adefb1SBjoern A. Zeeb 		break;
654249010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
6543a1adefb1SBjoern A. Zeeb 		rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
6544a1adefb1SBjoern A. Zeeb 		rxrate.mcs = rx_status->rate_idx;
6545a1adefb1SBjoern A. Zeeb 		rxrate.nss = rx_status->nss;
6546a1adefb1SBjoern A. Zeeb 		/* XXX TODO */
654749010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
654849010ba7SBjoern A. Zeeb 		break;
654949010ba7SBjoern A. Zeeb 	}
655049010ba7SBjoern A. Zeeb 
6551a1adefb1SBjoern A. Zeeb 	rxrate.bw = rx_status->bw;
655249010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
655349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
655449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
655549010ba7SBjoern A. Zeeb 		break;
655649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
655749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
655849010ba7SBjoern A. Zeeb 		break;
655949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
656049010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
656149010ba7SBjoern A. Zeeb 		break;
656249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
656349010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
656449010ba7SBjoern A. Zeeb 		break;
656549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
656649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
656749010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
656849010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
656949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
657049010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
657149010ba7SBjoern A. Zeeb 		break;
657249010ba7SBjoern A. Zeeb 	}
657349010ba7SBjoern A. Zeeb 
657449010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
657549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
657649010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
657749010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
657849010ba7SBjoern A. Zeeb 
657949010ba7SBjoern A. Zeeb 	/*
658049010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
658149010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
658249010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
658349010ba7SBjoern A. Zeeb 	 */
658449010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
658549010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
658649010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
658749010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
658849010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
658949010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
659049010ba7SBjoern A. Zeeb 	}
6591731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
6592731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
6593731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
6594731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
6595731ff400SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
6596731ff400SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
659749010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
659849010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
6599394a9a5bSBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
6600394a9a5bSBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
660149010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
660249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
660349010ba7SBjoern A. Zeeb #endif
6604a1adefb1SBjoern A. Zeeb 
6605a1adefb1SBjoern A. Zeeb 	if (lsta != NULL) {
6606a1adefb1SBjoern A. Zeeb 		memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
6607a1adefb1SBjoern A. Zeeb 		lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
6608a1adefb1SBjoern A. Zeeb 	}
660949010ba7SBjoern A. Zeeb }
661049010ba7SBjoern A. Zeeb 
6611e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
66126b4cac81SBjoern A. Zeeb void
66136b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6614e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6615e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
66166b4cac81SBjoern A. Zeeb {
66176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
66186b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
66196b4cac81SBjoern A. Zeeb 	struct mbuf *m;
66206b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
66216b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
66226b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
66236b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
66246b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
66256b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
66266b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
6627c816f64eSBjoern A. Zeeb 	int i, offset, ok, error;
662849010ba7SBjoern A. Zeeb 	uint8_t rssi;
6629c0cadd99SBjoern A. Zeeb 	bool is_beacon;
66306b4cac81SBjoern A. Zeeb 
6631c013f810SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
6632c013f810SBjoern A. Zeeb 	ic = lhw->ic;
6633c013f810SBjoern A. Zeeb 
66346b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
66356b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
6636c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
66376b4cac81SBjoern A. Zeeb 		IMPROVE();
66386b4cac81SBjoern A. Zeeb 		goto err;
66396b4cac81SBjoern A. Zeeb 	}
66406b4cac81SBjoern A. Zeeb 
66416b4cac81SBjoern A. Zeeb 	/*
66426b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
66436b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
66446b4cac81SBjoern A. Zeeb 	 */
66456b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
6646c013f810SBjoern A. Zeeb 	if (m == NULL) {
6647c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
66486b4cac81SBjoern A. Zeeb 		goto err;
6649c013f810SBjoern A. Zeeb 	}
66506b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
66516b4cac81SBjoern A. Zeeb 
66526b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
66536b4cac81SBjoern A. Zeeb 	offset = m->m_len;
66546b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
66556b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
66566b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
66576b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
66586b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
66596b4cac81SBjoern A. Zeeb 	}
66606b4cac81SBjoern A. Zeeb 
66616b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
66626b4cac81SBjoern A. Zeeb 
66636b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6664c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6665c0cadd99SBjoern A. Zeeb 
6666c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
66679d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
66686b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
66696b4cac81SBjoern A. Zeeb 
66709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
667173e3969fSBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
6672c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
667373e3969fSBjoern A. Zeeb 		    __func__, skb, skb->len, skb->data_len,
66746b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
66756b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6676c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
66776b4cac81SBjoern A. Zeeb 
66789d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
66796b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
66806b4cac81SBjoern A. Zeeb 
66816b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
66829d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6683f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
668460970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
66856b4cac81SBjoern A. Zeeb 			__func__,
6686c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6687c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
66886b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6689f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
66906b4cac81SBjoern A. Zeeb 			rx_status->freq,
66916b4cac81SBjoern A. Zeeb 			rx_status->bw,
66926b4cac81SBjoern A. Zeeb 			rx_status->encoding,
66936b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
66946b4cac81SBjoern A. Zeeb 			rx_status->band,
66956b4cac81SBjoern A. Zeeb 			rx_status->chains,
66966b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
66976b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
66986b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
669960970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
67006b4cac81SBjoern A. Zeeb 			rx_status->signal,
67016b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
67026b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
67036b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
67046b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
67056b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
67066b4cac81SBjoern A. Zeeb 			rx_status->nss,
67076b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
67086b4cac81SBjoern A. Zeeb no_trace_beacons:
67096b4cac81SBjoern A. Zeeb #endif
67106b4cac81SBjoern A. Zeeb 
671158246901SBjoern A. Zeeb 	lsta = NULL;
67126b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
67136b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
67146b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
67156b4cac81SBjoern A. Zeeb 	} else {
6716c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6717c8dafefaSBjoern A. Zeeb 
67186b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
67196b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
67206b4cac81SBjoern A. Zeeb 		if (ni != NULL)
67216b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
67226b4cac81SBjoern A. Zeeb 	}
67236b4cac81SBjoern A. Zeeb 
6724a1adefb1SBjoern A. Zeeb 	rssi = 0;
6725a1adefb1SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
6726a1adefb1SBjoern A. Zeeb 
6727a1adefb1SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
6728a1adefb1SBjoern A. Zeeb 	if (ok == 0) {
6729a1adefb1SBjoern A. Zeeb 		m_freem(m);
6730a1adefb1SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6731a1adefb1SBjoern A. Zeeb 		goto err;
6732a1adefb1SBjoern A. Zeeb 	}
6733a1adefb1SBjoern A. Zeeb 
67346b4cac81SBjoern A. Zeeb 	if (ni != NULL)
67356b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
67366b4cac81SBjoern A. Zeeb 	else
67376b4cac81SBjoern A. Zeeb 		/*
67386b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
67396b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
67406b4cac81SBjoern A. Zeeb 		 */
67416b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
67426b4cac81SBjoern A. Zeeb 
67439d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
67449d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6745bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6746c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6747c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
67489d9ba2b7SBjoern A. Zeeb #endif
67496b4cac81SBjoern A. Zeeb 
6750c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6751c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6752c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6753c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6754c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6755c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6756c8dafefaSBjoern A. Zeeb 
6757c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6758c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6759c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6760c8dafefaSBjoern A. Zeeb 
6761c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6762c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6763c8dafefaSBjoern A. Zeeb 
6764c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6765c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6766c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6767c8dafefaSBjoern A. Zeeb 		/*
6768c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6769c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6770c8dafefaSBjoern A. Zeeb 		 */
6771c8dafefaSBjoern A. Zeeb skip_device_ts:
67729fb91463SBjoern A. Zeeb 		;
67739fb91463SBjoern A. Zeeb 	}
6774c8dafefaSBjoern A. Zeeb 
67756b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
67766b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
67776b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
67786b4cac81SBjoern A. Zeeb 
67796b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
67806b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
67816b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
67826b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
67836b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
67846b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
67856b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
67866b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
67876b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
67886b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
67896b4cac81SBjoern A. Zeeb #endif
67906b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
67916b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
67926b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
67936b4cac81SBjoern A. Zeeb 		IMPROVE();
67946b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
67956b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
67966b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
67976b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6798170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
67996b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
68006b4cac81SBjoern A. Zeeb 	}
68016b4cac81SBjoern A. Zeeb 
68026b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
68036b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
68046b4cac81SBjoern A. Zeeb 
6805e30e05d3SBjoern A. Zeeb #if 0
6806e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6807e30e05d3SBjoern A. Zeeb 		/*
6808e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6809e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6810e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6811e30e05d3SBjoern A. Zeeb 		*/
6812e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6813e30e05d3SBjoern A. Zeeb 	}
6814e30e05d3SBjoern A. Zeeb #endif
6815e30e05d3SBjoern A. Zeeb 
6816c013f810SBjoern A. Zeeb 	/* Attach meta-information to the mbuf for the deferred RX path. */
68176b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6818dbae3dcfSBjoern A. Zeeb #ifdef LKPI_80211_USE_MTAG
6819759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6820759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6821759a996dSBjoern A. Zeeb 
6822759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6823759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6824c013f810SBjoern A. Zeeb 		if (mtag == NULL) {
6825c013f810SBjoern A. Zeeb 			m_freem(m);
6826c013f810SBjoern A. Zeeb 			counter_u64_add(ic->ic_ierrors, 1);
6827c013f810SBjoern A. Zeeb 			goto err;
6828c013f810SBjoern A. Zeeb 		}
6829759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6830759a996dSBjoern A. Zeeb 		rxni->ni = ni;		/* We hold a reference. */
6831759a996dSBjoern A. Zeeb 		m_tag_prepend(m, mtag);
6832dbae3dcfSBjoern A. Zeeb #else
6833dbae3dcfSBjoern A. Zeeb 		m->m_pkthdr.PH_loc.ptr = ni;	/* We hold a reference. */
6834dbae3dcfSBjoern A. Zeeb #endif
6835759a996dSBjoern A. Zeeb 	}
68366b4cac81SBjoern A. Zeeb 
6837759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6838759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6839759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6840759a996dSBjoern A. Zeeb 		m_freem(m);
6841c013f810SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6842759a996dSBjoern A. Zeeb 		goto err;
6843759a996dSBjoern A. Zeeb 	}
6844759a996dSBjoern A. Zeeb 
6845c816f64eSBjoern A. Zeeb 	error = mbufq_enqueue(&lhw->rxq, m);
6846c816f64eSBjoern A. Zeeb 	if (error != 0) {
6847c816f64eSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6848c816f64eSBjoern A. Zeeb 		m_freem(m);
6849c816f64eSBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
6850c816f64eSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6851c816f64eSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6852c816f64eSBjoern A. Zeeb 			ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
6853c816f64eSBjoern A. Zeeb 			    __func__, error);
6854c816f64eSBjoern A. Zeeb #endif
6855c816f64eSBjoern A. Zeeb 		goto err;
6856c816f64eSBjoern A. Zeeb 	}
6857759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6858759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
68596b4cac81SBjoern A. Zeeb 
68606b4cac81SBjoern A. Zeeb 	IMPROVE();
68616b4cac81SBjoern A. Zeeb 
68626b4cac81SBjoern A. Zeeb err:
68636b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
68646b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
68656b4cac81SBjoern A. Zeeb }
68666b4cac81SBjoern A. Zeeb 
68676b4cac81SBjoern A. Zeeb uint8_t
6868ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
68696b4cac81SBjoern A. Zeeb {
68706b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6871ec190d91SBjoern A. Zeeb 	uint8_t tid;
6872ec190d91SBjoern A. Zeeb 
6873ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6874ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6875ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6876ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
68776b4cac81SBjoern A. Zeeb 
68786b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6879ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6880ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6881ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6882ec190d91SBjoern A. Zeeb 
6883ec190d91SBjoern A. Zeeb 	return (tid);
68846b4cac81SBjoern A. Zeeb }
68856b4cac81SBjoern A. Zeeb 
6886ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6887ac1d519cSBjoern A. Zeeb 
6888ac1d519cSBjoern A. Zeeb static void
6889ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6890ac1d519cSBjoern A. Zeeb {
6891ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6892ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6893ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6894ac1d519cSBjoern A. Zeeb 
6895ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6896ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6897ac1d519cSBjoern A. Zeeb 
6898ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6899ac1d519cSBjoern A. Zeeb 
6900ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6901ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6902ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6903ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6904ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6905ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6906ac1d519cSBjoern A. Zeeb 		return;
6907ac1d519cSBjoern A. Zeeb 	}
6908ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6909ac1d519cSBjoern A. Zeeb 
6910ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6911ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6912ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6913ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6914ac1d519cSBjoern A. Zeeb 
6915ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6916ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6917ac1d519cSBjoern A. Zeeb 
6918ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6919ac1d519cSBjoern A. Zeeb }
6920ac1d519cSBjoern A. Zeeb 
6921ac1d519cSBjoern A. Zeeb void
6922ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6923ac1d519cSBjoern A. Zeeb {
6924ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6925ac1d519cSBjoern A. Zeeb 
6926ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6927ac1d519cSBjoern A. Zeeb 
6928ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6929ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6930ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6931ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6932ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6933ac1d519cSBjoern A. Zeeb 
6934ac1d519cSBjoern A. Zeeb 	/*
6935ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6936ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6937ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6938ac1d519cSBjoern A. Zeeb 	 */
6939ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6940ac1d519cSBjoern A. Zeeb }
6941ac1d519cSBjoern A. Zeeb 
6942ac1d519cSBjoern A. Zeeb void
6943ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6944ac1d519cSBjoern A. Zeeb {
6945ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6946ac1d519cSBjoern A. Zeeb 
6947ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6948ac1d519cSBjoern A. Zeeb 
6949ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6950ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6951ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6952ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6953ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6954ac1d519cSBjoern A. Zeeb }
6955ac1d519cSBjoern A. Zeeb 
6956ac1d519cSBjoern A. Zeeb void
6957ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6958ac1d519cSBjoern A. Zeeb {
6959ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6960ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6961ac1d519cSBjoern A. Zeeb 
6962ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6963ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6964ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6965ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6966ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6967ac1d519cSBjoern A. Zeeb 		return;
6968ac1d519cSBjoern A. Zeeb 	}
6969ac1d519cSBjoern A. Zeeb 
6970ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6971ac1d519cSBjoern A. Zeeb 
6972ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6973ac1d519cSBjoern A. Zeeb 		    entry);
6974ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6975ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6976ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6977ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6978ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6979ac1d519cSBjoern A. Zeeb 			break;
6980ac1d519cSBjoern A. Zeeb 	}
6981ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6982ac1d519cSBjoern A. Zeeb }
6983ac1d519cSBjoern A. Zeeb 
6984ac1d519cSBjoern A. Zeeb void
6985ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6986ac1d519cSBjoern A. Zeeb {
6987ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6988ac1d519cSBjoern A. Zeeb 
6989ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6990ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6991ac1d519cSBjoern A. Zeeb }
6992ac1d519cSBjoern A. Zeeb 
6993ac1d519cSBjoern A. Zeeb void
6994ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6995ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6996ac1d519cSBjoern A. Zeeb {
6997ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6998ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6999ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
7000ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
7001ac1d519cSBjoern A. Zeeb 	} else {
7002ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
7003ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
7004ac1d519cSBjoern A. Zeeb 	}
7005ac1d519cSBjoern A. Zeeb }
7006ac1d519cSBjoern A. Zeeb 
7007ac1d519cSBjoern A. Zeeb void
7008ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7009ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
7010ac1d519cSBjoern A. Zeeb {
7011ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
7012ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
7013ac1d519cSBjoern A. Zeeb }
7014ac1d519cSBjoern A. Zeeb 
7015ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7016ac1d519cSBjoern A. Zeeb 
70176b4cac81SBjoern A. Zeeb struct wiphy *
70186b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
70196b4cac81SBjoern A. Zeeb {
70206b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
7021ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
70226b4cac81SBjoern A. Zeeb 
70236b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
70246b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
70256b4cac81SBjoern A. Zeeb 		return (NULL);
70266b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
70276b4cac81SBjoern A. Zeeb 
7028ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7029ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
7030ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7031ac1d519cSBjoern A. Zeeb 
7032ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
7033ac1d519cSBjoern A. Zeeb 
7034ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
7035ac1d519cSBjoern A. Zeeb 	TODO();
7036ac1d519cSBjoern A. Zeeb 
7037ac1d519cSBjoern A. Zeeb 	return (wiphy);
70386b4cac81SBjoern A. Zeeb }
70396b4cac81SBjoern A. Zeeb 
70406b4cac81SBjoern A. Zeeb void
70416b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
70426b4cac81SBjoern A. Zeeb {
70436b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
70446b4cac81SBjoern A. Zeeb 
70456b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
70466b4cac81SBjoern A. Zeeb 		return;
70476b4cac81SBjoern A. Zeeb 
7048ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
7049ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
7050ac1d519cSBjoern A. Zeeb 
70516b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
7052ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7053ac1d519cSBjoern A. Zeeb 
70546b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
70556b4cac81SBjoern A. Zeeb }
70566b4cac81SBjoern A. Zeeb 
7057a7c19b8aSBjoern A. Zeeb static uint32_t
7058a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7059a7c19b8aSBjoern A. Zeeb {
7060a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
7061a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7062a7c19b8aSBjoern A. Zeeb }
7063a7c19b8aSBjoern A. Zeeb 
7064a7c19b8aSBjoern A. Zeeb static uint32_t
7065a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7066a7c19b8aSBjoern A. Zeeb {
7067a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
7068a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7069a7c19b8aSBjoern A. Zeeb }
7070a7c19b8aSBjoern A. Zeeb 
7071a7c19b8aSBjoern A. Zeeb uint32_t
7072a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7073a7c19b8aSBjoern A. Zeeb {
7074a7c19b8aSBjoern A. Zeeb 
7075a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
7076a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
7077a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7078a7c19b8aSBjoern A. Zeeb 
7079a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7080a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7081a7c19b8aSBjoern A. Zeeb 
7082a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
7083a7c19b8aSBjoern A. Zeeb 
7084a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
7085a7c19b8aSBjoern A. Zeeb }
7086a7c19b8aSBjoern A. Zeeb 
70876b4cac81SBjoern A. Zeeb uint32_t
70886b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
70896b4cac81SBjoern A. Zeeb     enum nl80211_band band)
70906b4cac81SBjoern A. Zeeb {
70916b4cac81SBjoern A. Zeeb 
70926b4cac81SBjoern A. Zeeb 	switch (band) {
70936b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
70946b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
70956b4cac81SBjoern A. Zeeb 		break;
70966b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
70976b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
70986b4cac81SBjoern A. Zeeb 		break;
70996b4cac81SBjoern A. Zeeb 	default:
71006b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
71016b4cac81SBjoern A. Zeeb 		break;
71026b4cac81SBjoern A. Zeeb 	}
71036b4cac81SBjoern A. Zeeb 
71046b4cac81SBjoern A. Zeeb 	return (0);
71056b4cac81SBjoern A. Zeeb }
71066b4cac81SBjoern A. Zeeb 
71076b4cac81SBjoern A. Zeeb uint32_t
71086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
71096b4cac81SBjoern A. Zeeb {
71106b4cac81SBjoern A. Zeeb 
71116b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
71126b4cac81SBjoern A. Zeeb }
71136b4cac81SBjoern A. Zeeb 
7114ac867c20SBjoern A. Zeeb #if 0
71156b4cac81SBjoern A. Zeeb static struct lkpi_sta *
71166b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
71176b4cac81SBjoern A. Zeeb {
71186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
71196b4cac81SBjoern A. Zeeb 
7120a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7121a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
71226b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
7123a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
71246b4cac81SBjoern A. Zeeb 			return (lsta);
71256b4cac81SBjoern A. Zeeb 		}
71266b4cac81SBjoern A. Zeeb 	}
7127a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
71286b4cac81SBjoern A. Zeeb 
71296b4cac81SBjoern A. Zeeb 	return (NULL);
71306b4cac81SBjoern A. Zeeb }
7131ac867c20SBjoern A. Zeeb #endif
71326b4cac81SBjoern A. Zeeb 
71336b4cac81SBjoern A. Zeeb struct ieee80211_sta *
71346b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
71356b4cac81SBjoern A. Zeeb {
71366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7137a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71386b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
71396b4cac81SBjoern A. Zeeb 
71406b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71416b4cac81SBjoern A. Zeeb 
7142a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
7143a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
71446b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
71456b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7146a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
71476b4cac81SBjoern A. Zeeb 			return (sta);
71486b4cac81SBjoern A. Zeeb 		}
71496b4cac81SBjoern A. Zeeb 	}
7150a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
71516b4cac81SBjoern A. Zeeb 	return (NULL);
71526b4cac81SBjoern A. Zeeb }
71536b4cac81SBjoern A. Zeeb 
71546b4cac81SBjoern A. Zeeb struct ieee80211_sta *
71552e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
71562e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
71576b4cac81SBjoern A. Zeeb {
71586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71606b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71616b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
71626b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
71636b4cac81SBjoern A. Zeeb 
71646b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
71656b4cac81SBjoern A. Zeeb 	sta = NULL;
71666b4cac81SBjoern A. Zeeb 
71678891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71686b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71696b4cac81SBjoern A. Zeeb 
71706b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
71716b4cac81SBjoern A. Zeeb 
71726b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71736b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
71746b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
71756b4cac81SBjoern A. Zeeb 			continue;
71766b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
71776b4cac81SBjoern A. Zeeb 		if (sta != NULL)
71786b4cac81SBjoern A. Zeeb 			break;
71796b4cac81SBjoern A. Zeeb 	}
71808891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
71816b4cac81SBjoern A. Zeeb 
71826b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
71836b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
71846b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
71856b4cac81SBjoern A. Zeeb 			return (NULL);
71866b4cac81SBjoern A. Zeeb 	}
71876b4cac81SBjoern A. Zeeb 
71886b4cac81SBjoern A. Zeeb 	return (sta);
71896b4cac81SBjoern A. Zeeb }
71906b4cac81SBjoern A. Zeeb 
71916b4cac81SBjoern A. Zeeb struct sk_buff *
71926b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
71936b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
71946b4cac81SBjoern A. Zeeb {
71956b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
71960cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71976b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
71986b4cac81SBjoern A. Zeeb 
71990cbcfa19SBjoern A. Zeeb 	skb = NULL;
72006b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
72016b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
72026b4cac81SBjoern A. Zeeb 
72030cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
72040cbcfa19SBjoern A. Zeeb 		goto stopped;
72050cbcfa19SBjoern A. Zeeb 
72060cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
72070cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
72080cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
72090cbcfa19SBjoern A. Zeeb 		goto stopped;
72100cbcfa19SBjoern A. Zeeb 	}
72110cbcfa19SBjoern A. Zeeb 
7212eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
7213eac3646fSBjoern A. Zeeb 
7214eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
72156b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
7216eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
72176b4cac81SBjoern A. Zeeb 
72180cbcfa19SBjoern A. Zeeb stopped:
72196b4cac81SBjoern A. Zeeb 	return (skb);
72206b4cac81SBjoern A. Zeeb }
72216b4cac81SBjoern A. Zeeb 
72226b4cac81SBjoern A. Zeeb void
72236b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
722486220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
72256b4cac81SBjoern A. Zeeb {
72266b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
72276b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
722886220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
72296b4cac81SBjoern A. Zeeb 
72306b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
72316b4cac81SBjoern A. Zeeb 
72326b4cac81SBjoern A. Zeeb 	fc = bc = 0;
7233eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
72346b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
72356b4cac81SBjoern A. Zeeb 		fc++;
72366b4cac81SBjoern A. Zeeb 		bc += skb->len;
72376b4cac81SBjoern A. Zeeb 	}
7238eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
72396b4cac81SBjoern A. Zeeb 	if (frame_cnt)
72406b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
72416b4cac81SBjoern A. Zeeb 	if (byte_cnt)
72426b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
72436b4cac81SBjoern A. Zeeb 
72446b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
72456b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
72466b4cac81SBjoern A. Zeeb 	IMPROVE();
72476b4cac81SBjoern A. Zeeb }
72486b4cac81SBjoern A. Zeeb 
72496b4cac81SBjoern A. Zeeb /*
72506b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
72516b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
72526b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
72536b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
72546b4cac81SBjoern A. Zeeb  */
7255a8397571SBjoern A. Zeeb static void
7256a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
72576b4cac81SBjoern A. Zeeb     int status)
72586b4cac81SBjoern A. Zeeb {
72596b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
72606b4cac81SBjoern A. Zeeb 	struct mbuf *m;
72616b4cac81SBjoern A. Zeeb 
72626b4cac81SBjoern A. Zeeb 	m = skb->m;
72636b4cac81SBjoern A. Zeeb 	skb->m = NULL;
72646b4cac81SBjoern A. Zeeb 
72656b4cac81SBjoern A. Zeeb 	if (m != NULL) {
72666b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
72676b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
72686b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
72696b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
72706b4cac81SBjoern A. Zeeb 	}
7271a8397571SBjoern A. Zeeb }
72726b4cac81SBjoern A. Zeeb 
7273a8397571SBjoern A. Zeeb void
7274a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
7275a8397571SBjoern A. Zeeb     int status)
7276a8397571SBjoern A. Zeeb {
7277a8397571SBjoern A. Zeeb 
7278a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
72796b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
72806b4cac81SBjoern A. Zeeb }
72816b4cac81SBjoern A. Zeeb 
7282383b3e8fSBjoern A. Zeeb void
7283a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
7284a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
7285383b3e8fSBjoern A. Zeeb {
7286a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
7287383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
7288383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
7289383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
7290383b3e8fSBjoern A. Zeeb 	int status;
7291383b3e8fSBjoern A. Zeeb 
7292a8397571SBjoern A. Zeeb 	skb = txstat->skb;
7293383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
7294383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
7295383b3e8fSBjoern A. Zeeb 
7296383b3e8fSBjoern A. Zeeb 		m = skb->m;
7297383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
7298383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
7299383b3e8fSBjoern A. Zeeb 	} else {
7300383b3e8fSBjoern A. Zeeb 		ni = NULL;
7301383b3e8fSBjoern A. Zeeb 	}
7302383b3e8fSBjoern A. Zeeb 
7303a8397571SBjoern A. Zeeb 	info = txstat->info;
7304383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
7305383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
7306383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
7307383b3e8fSBjoern A. Zeeb 	} else {
7308383b3e8fSBjoern A. Zeeb 		status = 1;
7309383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
7310383b3e8fSBjoern A. Zeeb 	}
7311383b3e8fSBjoern A. Zeeb 
7312383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
7313383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
7314383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
7315383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
7316383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
7317383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
7318383b3e8fSBjoern A. Zeeb 		}
7319383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
7320a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
7321383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
7322383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
7323383b3e8fSBjoern A. Zeeb #endif
7324adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
7325383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
7326383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
7327383b3e8fSBjoern A. Zeeb 		}
7328383b3e8fSBjoern A. Zeeb 
7329d1a37f28SBjoern A. Zeeb 		IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
7330383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
733146de4d9fSAdrian Chadd 		ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
7332383b3e8fSBjoern A. Zeeb 
7333383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7334383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
7335d1a37f28SBjoern A. Zeeb 			printf("TX-RATE: %s: long_retries %d\n", __func__,
733646de4d9fSAdrian Chadd 			    txs.long_retries);
7337383b3e8fSBjoern A. Zeeb 		}
7338383b3e8fSBjoern A. Zeeb #endif
7339383b3e8fSBjoern A. Zeeb 	}
7340383b3e8fSBjoern A. Zeeb 
7341383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
7342383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
7343383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
7344383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
7345383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
7346383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
7347adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
7348383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
7349383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
7350383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
7351383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
7352383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
7353383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
7354383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
7355383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
7356383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
7357383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
7358383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
7359383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
7360383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
7361adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
7362383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
7363383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
7364383b3e8fSBjoern A. Zeeb #endif
7365383b3e8fSBjoern A. Zeeb 
7366a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
7367a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
7368a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
7369a8397571SBjoern A. Zeeb 	} else {
7370383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
7371383b3e8fSBjoern A. Zeeb 	}
7372a8397571SBjoern A. Zeeb }
7373a8397571SBjoern A. Zeeb 
7374a8397571SBjoern A. Zeeb void
7375a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
7376a8397571SBjoern A. Zeeb {
7377a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
7378a8397571SBjoern A. Zeeb 
7379a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
7380a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
7381a8397571SBjoern A. Zeeb 	status.skb = skb;
7382a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
7383a8397571SBjoern A. Zeeb 
7384a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
7385a8397571SBjoern A. Zeeb }
7386383b3e8fSBjoern A. Zeeb 
73876b4cac81SBjoern A. Zeeb /*
73886b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
73896b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
73906b4cac81SBjoern A. Zeeb  * mbufs this should go away.
73916b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
73926b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
73936b4cac81SBjoern A. Zeeb  */
73946b4cac81SBjoern A. Zeeb static void
73956b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
73966b4cac81SBjoern A. Zeeb {
73976b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
73986b4cac81SBjoern A. Zeeb 	struct mbuf *m;
73996b4cac81SBjoern A. Zeeb 
74006b4cac81SBjoern A. Zeeb 	if (p == NULL)
74016b4cac81SBjoern A. Zeeb 		return;
74026b4cac81SBjoern A. Zeeb 
74036b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
74046b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
74056b4cac81SBjoern A. Zeeb 
74066b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
74076b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
74086b4cac81SBjoern A. Zeeb 	if (ni != NULL)
74096b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
74106b4cac81SBjoern A. Zeeb 	m_freem(m);
74116b4cac81SBjoern A. Zeeb }
74126b4cac81SBjoern A. Zeeb 
74136b4cac81SBjoern A. Zeeb void
74146b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
74156b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
74166b4cac81SBjoern A. Zeeb {
74176b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74186b4cac81SBjoern A. Zeeb 
74196b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
74206b4cac81SBjoern A. Zeeb 	IMPROVE();
74216b4cac81SBjoern A. Zeeb 
74226b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74236b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
74246b4cac81SBjoern A. Zeeb }
74256b4cac81SBjoern A. Zeeb 
74266b4cac81SBjoern A. Zeeb void
74276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
74286b4cac81SBjoern A. Zeeb     struct work_struct *w)
74296b4cac81SBjoern A. Zeeb {
74306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
74316b4cac81SBjoern A. Zeeb 
74326b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
74336b4cac81SBjoern A. Zeeb 	IMPROVE();
74346b4cac81SBjoern A. Zeeb 
74356b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
74366b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
74376b4cac81SBjoern A. Zeeb }
74386b4cac81SBjoern A. Zeeb 
74396b4cac81SBjoern A. Zeeb struct sk_buff *
7440ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7441ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7442ade774b1SBjoern A. Zeeb {
7443ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7444ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7445ade774b1SBjoern A. Zeeb 	uint8_t *p;
7446ade774b1SBjoern A. Zeeb 	size_t len;
7447ade774b1SBjoern A. Zeeb 
7448ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7449ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7450ade774b1SBjoern A. Zeeb 
7451ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7452ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7453ade774b1SBjoern A. Zeeb 		return (NULL);
7454ade774b1SBjoern A. Zeeb 
7455ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7456ade774b1SBjoern A. Zeeb 
7457ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7458ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7459ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7460ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7461ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7462ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7463ade774b1SBjoern A. Zeeb 
7464ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7465ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7466ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7467ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7468ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7469ade774b1SBjoern A. Zeeb 
7470ade774b1SBjoern A. Zeeb 	return (skb);
7471ade774b1SBjoern A. Zeeb }
7472ade774b1SBjoern A. Zeeb 
7473ade774b1SBjoern A. Zeeb struct sk_buff *
74746b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
74756b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
74766b4cac81SBjoern A. Zeeb {
74776b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
74786b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
74796b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
74806b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
74816b4cac81SBjoern A. Zeeb 	uint16_t v;
74826b4cac81SBjoern A. Zeeb 
74836b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
74846b4cac81SBjoern A. Zeeb 	if (skb == NULL)
74856b4cac81SBjoern A. Zeeb 		return (NULL);
74866b4cac81SBjoern A. Zeeb 
74876b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
74886b4cac81SBjoern A. Zeeb 
74896b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
74906b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
74916b4cac81SBjoern A. Zeeb 
74926b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
74936b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
74946b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7495616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
74966b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
74976b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
74986b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
74996b4cac81SBjoern A. Zeeb 
75006b4cac81SBjoern A. Zeeb 	return (skb);
75016b4cac81SBjoern A. Zeeb }
75026b4cac81SBjoern A. Zeeb 
75036b4cac81SBjoern A. Zeeb struct sk_buff *
75046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7505adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
75066b4cac81SBjoern A. Zeeb {
75076b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75086b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
75096b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
75106b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
75116b4cac81SBjoern A. Zeeb 
7512adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7513adff403fSBjoern A. Zeeb 
75146b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
75156b4cac81SBjoern A. Zeeb 	if (skb == NULL)
75166b4cac81SBjoern A. Zeeb 		return (NULL);
75176b4cac81SBjoern A. Zeeb 
75186b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
75196b4cac81SBjoern A. Zeeb 
75206b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75216b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
75226b4cac81SBjoern A. Zeeb 
75236b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
75246b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
75256b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
75266b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
75276b4cac81SBjoern A. Zeeb 
75286b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
75296b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
75306b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
75316b4cac81SBjoern A. Zeeb 
75326b4cac81SBjoern A. Zeeb 	return (skb);
75336b4cac81SBjoern A. Zeeb }
75346b4cac81SBjoern A. Zeeb 
75356b4cac81SBjoern A. Zeeb struct wireless_dev *
75366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
75376b4cac81SBjoern A. Zeeb {
75386b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75396b4cac81SBjoern A. Zeeb 
75406b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75416b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
75426b4cac81SBjoern A. Zeeb }
75436b4cac81SBjoern A. Zeeb 
75446b4cac81SBjoern A. Zeeb void
75456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
75466b4cac81SBjoern A. Zeeb {
75476b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75486b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
75496b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
75506b4cac81SBjoern A. Zeeb 	int arg;
75516b4cac81SBjoern A. Zeeb 
75526b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
75536b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
75546b4cac81SBjoern A. Zeeb 
75556b4cac81SBjoern A. Zeeb 	/*
7556f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
75576b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
75586b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
75596b4cac81SBjoern A. Zeeb 	 */
7560f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7561bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
75626b4cac81SBjoern A. Zeeb 
7563018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7564018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
75656b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
75666b4cac81SBjoern A. Zeeb }
75676b4cac81SBjoern A. Zeeb 
7568bb81db90SBjoern A. Zeeb void
7569bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7570bb81db90SBjoern A. Zeeb {
7571bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7572bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7573bb81db90SBjoern A. Zeeb 
7574bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7575bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7576bb81db90SBjoern A. Zeeb 
7577bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7578bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
75793540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7580bb81db90SBjoern A. Zeeb }
7581bb81db90SBjoern A. Zeeb 
75825edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
75835edde07cSBjoern A. Zeeb 
75845a9a0d78SBjoern A. Zeeb void
75855a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
75865a9a0d78SBjoern A. Zeeb {
75875a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
75885a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
75895a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
75905a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
75915a9a0d78SBjoern A. Zeeb 
75925a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
75935a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
75945a9a0d78SBjoern A. Zeeb 
75955a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
75965a9a0d78SBjoern A. Zeeb 
75975a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
75985a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
75995a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
76005a9a0d78SBjoern A. Zeeb 	else
76015a9a0d78SBjoern A. Zeeb 		ac_count = 1;
76025a9a0d78SBjoern A. Zeeb 
76035a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
76045a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
76055a9a0d78SBjoern A. Zeeb 
76065a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
76075a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
76085a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
76095a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
76100d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
76115a9a0d78SBjoern A. Zeeb 				/*
76125a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
76135a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
76145a9a0d78SBjoern A. Zeeb 				 */
76150d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
76160d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
76175a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
76185a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
76195a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
76205a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
76210d2cb6a6SBjoern A. Zeeb #endif
76225a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
76235a9a0d78SBjoern A. Zeeb 			}
76245a9a0d78SBjoern A. Zeeb 		}
76255a9a0d78SBjoern A. Zeeb 	}
76265a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
76275a9a0d78SBjoern A. Zeeb }
76285a9a0d78SBjoern A. Zeeb 
76295a9a0d78SBjoern A. Zeeb void
76305a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
76315a9a0d78SBjoern A. Zeeb {
76325a9a0d78SBjoern A. Zeeb 	int i;
76335a9a0d78SBjoern A. Zeeb 
76345a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
76355a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
76365a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
76375a9a0d78SBjoern A. Zeeb }
76385a9a0d78SBjoern A. Zeeb 
76395a9a0d78SBjoern A. Zeeb 
76405a9a0d78SBjoern A. Zeeb static void
76415a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
76425a9a0d78SBjoern A. Zeeb {
76435a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
76445a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
76455a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
76465a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
76475a9a0d78SBjoern A. Zeeb 
76485a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
76495a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
76505a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
76515a9a0d78SBjoern A. Zeeb 	else
76525a9a0d78SBjoern A. Zeeb 		ac_count = 1;
76535a9a0d78SBjoern A. Zeeb 
76545a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
76555a9a0d78SBjoern A. Zeeb 
76565a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
76575a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
76585a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
76595a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
76605a9a0d78SBjoern A. Zeeb 
76615a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
76625a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
76635a9a0d78SBjoern A. Zeeb 
76645a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
76655a9a0d78SBjoern A. Zeeb 
76665a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
76675a9a0d78SBjoern A. Zeeb 
76680d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
76695a9a0d78SBjoern A. Zeeb 				/*
76705a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
76715a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
76725a9a0d78SBjoern A. Zeeb 				 */
76730d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
76740d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
76755a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
76765a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
76775a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
76785a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
76790d2cb6a6SBjoern A. Zeeb #endif
76805a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
76815a9a0d78SBjoern A. Zeeb 
7682a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7683a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
76845a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
76855a9a0d78SBjoern A. Zeeb 
76865a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
76875a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
76885a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
76895a9a0d78SBjoern A. Zeeb 
76905a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
76915a9a0d78SBjoern A. Zeeb 							continue;
76925a9a0d78SBjoern A. Zeeb 
76935a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
76945a9a0d78SBjoern A. Zeeb 							continue;
76955a9a0d78SBjoern A. Zeeb 
76965a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
76975a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
76985a9a0d78SBjoern A. Zeeb 							continue;
76995a9a0d78SBjoern A. Zeeb 
77005a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
77015a9a0d78SBjoern A. Zeeb 
77025a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
77035a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
77045a9a0d78SBjoern A. Zeeb 					}
77055a9a0d78SBjoern A. Zeeb 				}
7706a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
77075a9a0d78SBjoern A. Zeeb 			}
77085a9a0d78SBjoern A. Zeeb 		}
77095a9a0d78SBjoern A. Zeeb 	}
77105a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
77115a9a0d78SBjoern A. Zeeb }
77125a9a0d78SBjoern A. Zeeb 
77135a9a0d78SBjoern A. Zeeb void
77145a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
77155a9a0d78SBjoern A. Zeeb {
77165a9a0d78SBjoern A. Zeeb 	int i;
77175a9a0d78SBjoern A. Zeeb 
77185a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
77195a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
77205a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
77215a9a0d78SBjoern A. Zeeb }
77225a9a0d78SBjoern A. Zeeb 
77235a9a0d78SBjoern A. Zeeb void
77245a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
77255a9a0d78SBjoern A. Zeeb {
77265a9a0d78SBjoern A. Zeeb 
77275a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
77285a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
77295a9a0d78SBjoern A. Zeeb 
77305a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
77315a9a0d78SBjoern A. Zeeb }
77325a9a0d78SBjoern A. Zeeb 
77335a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
77345a9a0d78SBjoern A. Zeeb void
77355a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
77365a9a0d78SBjoern A. Zeeb {
77375a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77385a9a0d78SBjoern A. Zeeb 
77395a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
77405a9a0d78SBjoern A. Zeeb 
77415a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
77425a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
77435a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
77445a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
77455a9a0d78SBjoern A. Zeeb }
77465a9a0d78SBjoern A. Zeeb 
77475a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
77485a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
77495a9a0d78SBjoern A. Zeeb {
77505a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77515a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
77525a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
77535a9a0d78SBjoern A. Zeeb 
77545a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
77555a9a0d78SBjoern A. Zeeb 	txq = NULL;
77565a9a0d78SBjoern A. Zeeb 
77575a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
77585a9a0d78SBjoern A. Zeeb 
77595a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
77605a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
77615a9a0d78SBjoern A. Zeeb 		goto out;
77625a9a0d78SBjoern A. Zeeb 
77635a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
77645a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
77655a9a0d78SBjoern A. Zeeb 		goto out;
77665a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
77675a9a0d78SBjoern A. Zeeb 		goto out;
77685a9a0d78SBjoern A. Zeeb 
77695a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
77705a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
77715a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
77725a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
77735a9a0d78SBjoern A. Zeeb 
77745a9a0d78SBjoern A. Zeeb out:
77755a9a0d78SBjoern A. Zeeb 	return (txq);
77765a9a0d78SBjoern A. Zeeb }
77775a9a0d78SBjoern A. Zeeb 
77785a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
77795a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
77805a9a0d78SBjoern A. Zeeb {
77815a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
77825a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7783eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
77845a9a0d78SBjoern A. Zeeb 
77855a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
77865a9a0d78SBjoern A. Zeeb 
77875a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
77885a9a0d78SBjoern A. Zeeb 
77895a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7790eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7791eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7792eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7793eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
77945a9a0d78SBjoern A. Zeeb 		goto out;
77955a9a0d78SBjoern A. Zeeb 
779641b746e0SAustin Shafer 	/*
779741b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
779841b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
779941b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
780041b746e0SAustin Shafer 	 */
780141b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
78025a9a0d78SBjoern A. Zeeb 		goto out;
78035a9a0d78SBjoern A. Zeeb 
78045a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
78055a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
78065a9a0d78SBjoern A. Zeeb out:
78075a9a0d78SBjoern A. Zeeb 	return;
78085a9a0d78SBjoern A. Zeeb }
78095a9a0d78SBjoern A. Zeeb 
7810eac3646fSBjoern A. Zeeb void
7811eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7812eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7813eac3646fSBjoern A. Zeeb {
7814eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7815eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7816eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7817eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7818eac3646fSBjoern A. Zeeb 
7819eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7820eac3646fSBjoern A. Zeeb 
7821eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7822eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7823eac3646fSBjoern A. Zeeb 	do {
7824eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7825eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7826eac3646fSBjoern A. Zeeb 			break;
7827eac3646fSBjoern A. Zeeb 
7828eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7829eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7830eac3646fSBjoern A. Zeeb 		do {
7831eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7832eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7833eac3646fSBjoern A. Zeeb 				break;
7834eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7835eac3646fSBjoern A. Zeeb 		} while(1);
7836eac3646fSBjoern A. Zeeb 
7837eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7838eac3646fSBjoern A. Zeeb 	} while (1);
7839eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7840eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7841eac3646fSBjoern A. Zeeb }
7842eac3646fSBjoern A. Zeeb 
78435a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
78445a9a0d78SBjoern A. Zeeb 
78455edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
78465edde07cSBjoern A. Zeeb 	u_int refcnt;
78475edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
78485edde07cSBjoern A. Zeeb };
78495edde07cSBjoern A. Zeeb 
78505edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
78515edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
78525edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
78535edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
78545edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
78555edde07cSBjoern A. Zeeb 	size_t ssid_len;
78565edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
78575edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
78585edde07cSBjoern A. Zeeb 
78595edde07cSBjoern A. Zeeb 	/*
78605edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
78615edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
78625edde07cSBjoern A. Zeeb 	 */
78635edde07cSBjoern A. Zeeb 	bool match;
78645edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
78655edde07cSBjoern A. Zeeb };
78665edde07cSBjoern A. Zeeb 
78675edde07cSBjoern A. Zeeb static void
78685edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
78695edde07cSBjoern A. Zeeb {
78705edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
78715edde07cSBjoern A. Zeeb 	size_t ielen;
78725edde07cSBjoern A. Zeeb 
78735edde07cSBjoern A. Zeeb 	lookup = arg;
78745edde07cSBjoern A. Zeeb 
78755edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
78765edde07cSBjoern A. Zeeb 	if (lookup->match)
78775edde07cSBjoern A. Zeeb 		return;
78785edde07cSBjoern A. Zeeb 
78795edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
78805edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
78815edde07cSBjoern A. Zeeb 		return;
78825edde07cSBjoern A. Zeeb 
78835edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
78845edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
78855edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
78865edde07cSBjoern A. Zeeb 		return;
78875edde07cSBjoern A. Zeeb 	}
78885edde07cSBjoern A. Zeeb 
78895edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
78905edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
78915edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
78925edde07cSBjoern A. Zeeb 		return;
78935edde07cSBjoern A. Zeeb 	}
78945edde07cSBjoern A. Zeeb 
78955edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
78965edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
78975edde07cSBjoern A. Zeeb 
78985edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
78995edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
79005edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
79015edde07cSBjoern A. Zeeb 			return;
79025edde07cSBjoern A. Zeeb 	}
79035edde07cSBjoern A. Zeeb 
79045edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
79055edde07cSBjoern A. Zeeb 		return;
79065edde07cSBjoern A. Zeeb 
79075edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
79085edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
79095edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
79105edde07cSBjoern A. Zeeb 			return;
79115edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
79125edde07cSBjoern A. Zeeb 			return;
79135edde07cSBjoern A. Zeeb 	}
79145edde07cSBjoern A. Zeeb 
79155edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
79165edde07cSBjoern A. Zeeb 
79175edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
79185edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
79195edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
79205edde07cSBjoern A. Zeeb 		return;
79215edde07cSBjoern A. Zeeb 
79225edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
79235edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
79245edde07cSBjoern A. Zeeb 	if (ielen)
79255edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
79265edde07cSBjoern A. Zeeb 
79275edde07cSBjoern A. Zeeb 	lookup->match = true;
79285edde07cSBjoern A. Zeeb }
79295edde07cSBjoern A. Zeeb 
79305edde07cSBjoern A. Zeeb struct cfg80211_bss *
79315edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
79325edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
79335edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
79345edde07cSBjoern A. Zeeb {
79355edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
79365edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
79375edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
79385edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
79395edde07cSBjoern A. Zeeb 
79405edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
79415edde07cSBjoern A. Zeeb 
79425edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
79435edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
79445edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
79455edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
79465edde07cSBjoern A. Zeeb 		return (NULL);
79475edde07cSBjoern A. Zeeb 	}
79485edde07cSBjoern A. Zeeb 
79495edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
79505edde07cSBjoern A. Zeeb 	lookup.chan = chan;
79515edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
79525edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
79535edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
79545edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
79555edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
79565edde07cSBjoern A. Zeeb 	lookup.match = false;
79575edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
79585edde07cSBjoern A. Zeeb 
79595edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
79605edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
79615edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
79625edde07cSBjoern A. Zeeb 	if (!lookup.match) {
79635edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
79645edde07cSBjoern A. Zeeb 		return (NULL);
79655edde07cSBjoern A. Zeeb 	}
79665edde07cSBjoern A. Zeeb 
79675edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
79685edde07cSBjoern A. Zeeb 	return (&lbss->bss);
79695edde07cSBjoern A. Zeeb }
79705edde07cSBjoern A. Zeeb 
79715edde07cSBjoern A. Zeeb void
79725edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
79735edde07cSBjoern A. Zeeb {
79745edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
79755edde07cSBjoern A. Zeeb 
79765edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
79775edde07cSBjoern A. Zeeb 
79785edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
79795edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
79805edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
79815edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
79825edde07cSBjoern A. Zeeb 	}
79835edde07cSBjoern A. Zeeb }
79845edde07cSBjoern A. Zeeb 
79855edde07cSBjoern A. Zeeb void
79865edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
79875edde07cSBjoern A. Zeeb {
79885edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
79895edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
79905edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
79915edde07cSBjoern A. Zeeb 
79925edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
79935edde07cSBjoern A. Zeeb 	ic = lhw->ic;
79945edde07cSBjoern A. Zeeb 
79955edde07cSBjoern A. Zeeb 	/*
79965edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
79975edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
79985edde07cSBjoern A. Zeeb 	 */
79995edde07cSBjoern A. Zeeb 	if (ic == NULL ||
80005edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
80015edde07cSBjoern A. Zeeb 		return;
80025edde07cSBjoern A. Zeeb 
80035edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
80045edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
80055edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
80065edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
80075edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
80085edde07cSBjoern A. Zeeb }
80095edde07cSBjoern A. Zeeb 
80105edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
80115edde07cSBjoern A. Zeeb 
8012ac1d519cSBjoern A. Zeeb /*
8013ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
8014ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
8015ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
8016ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
8017ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
8018ac1d519cSBjoern A. Zeeb  */
8019ac1d519cSBjoern A. Zeeb 
8020ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8021ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
8022ac1d519cSBjoern A. Zeeb {
8023ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
8024ac1d519cSBjoern A. Zeeb 	uint32_t changed;
8025ac1d519cSBjoern A. Zeeb 	int error;
8026ac1d519cSBjoern A. Zeeb 
8027ac1d519cSBjoern A. Zeeb 	changed = 0;
8028ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
8029ac1d519cSBjoern A. Zeeb 		cd = NULL;
8030ac1d519cSBjoern A. Zeeb 	else
8031ac1d519cSBjoern A. Zeeb 		cd = &new->def;
8032ac1d519cSBjoern A. Zeeb 
8033ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
8034ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
8035ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
8036ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8037ac1d519cSBjoern A. Zeeb 	}
8038ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8039ac1d519cSBjoern A. Zeeb 
8040ac1d519cSBjoern A. Zeeb 	if (changed == 0)
8041ac1d519cSBjoern A. Zeeb 		return (0);
8042ac1d519cSBjoern A. Zeeb 
8043ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
8044ac1d519cSBjoern A. Zeeb 	return (error);
8045ac1d519cSBjoern A. Zeeb }
8046ac1d519cSBjoern A. Zeeb 
8047ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
8048ac1d519cSBjoern A. Zeeb 
80496b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
80506b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
80516b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8052