xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 65c573e47c40e3f167f3d7e41bd8db40b6b8f91e)
16b4cac81SBjoern A. Zeeb /*-
2ec6185c5SBjoern A. Zeeb  * Copyright (c) 2020-2025 The FreeBSD Foundation
3c272abc5SBjoern A. Zeeb  * Copyright (c) 2020-2025 Bjoern A. Zeeb
46b4cac81SBjoern A. Zeeb  *
56b4cac81SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
66b4cac81SBjoern A. Zeeb  * the FreeBSD Foundation.
76b4cac81SBjoern A. Zeeb  *
86b4cac81SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
96b4cac81SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
106b4cac81SBjoern A. Zeeb  * are met:
116b4cac81SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
126b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
136b4cac81SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
146b4cac81SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
156b4cac81SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
166b4cac81SBjoern A. Zeeb  *
176b4cac81SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186b4cac81SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196b4cac81SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206b4cac81SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216b4cac81SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226b4cac81SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236b4cac81SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246b4cac81SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256b4cac81SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266b4cac81SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276b4cac81SBjoern A. Zeeb  * SUCH DAMAGE.
286b4cac81SBjoern A. Zeeb  */
296b4cac81SBjoern A. Zeeb 
306b4cac81SBjoern A. Zeeb /*
316b4cac81SBjoern A. Zeeb  * Public functions are called linuxkpi_*().
326b4cac81SBjoern A. Zeeb  * Internal (static) functions are called lkpi_*().
336b4cac81SBjoern A. Zeeb  *
346b4cac81SBjoern A. Zeeb  * The internal structures holding metadata over public structures are also
356b4cac81SBjoern A. Zeeb  * called lkpi_xxx (usually with a member at the end called xxx).
366b4cac81SBjoern A. Zeeb  * Note: we do not replicate the structure names but the general variable names
376b4cac81SBjoern A. Zeeb  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
386b4cac81SBjoern A. Zeeb  * There are macros to access one from the other.
396b4cac81SBjoern A. Zeeb  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
406b4cac81SBjoern A. Zeeb  */
416b4cac81SBjoern A. Zeeb 
4211db70b6SBjoern A. Zeeb /*
4311db70b6SBjoern A. Zeeb  * TODO:
4411db70b6SBjoern A. Zeeb  * - lots :)
4511db70b6SBjoern A. Zeeb  * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
4611db70b6SBjoern A. Zeeb  */
4711db70b6SBjoern A. Zeeb 
486b4cac81SBjoern A. Zeeb #include <sys/param.h>
496b4cac81SBjoern A. Zeeb #include <sys/types.h>
506b4cac81SBjoern A. Zeeb #include <sys/kernel.h>
516b4cac81SBjoern A. Zeeb #include <sys/errno.h>
526b4cac81SBjoern A. Zeeb #include <sys/malloc.h>
536b4cac81SBjoern A. Zeeb #include <sys/module.h>
546b4cac81SBjoern A. Zeeb #include <sys/mutex.h>
5540839418SBjoern A. Zeeb #include <sys/sbuf.h>
566b4cac81SBjoern A. Zeeb #include <sys/socket.h>
576b4cac81SBjoern A. Zeeb #include <sys/sysctl.h>
586b4cac81SBjoern A. Zeeb #include <sys/queue.h>
596b4cac81SBjoern A. Zeeb #include <sys/taskqueue.h>
60527687a9SBjoern A. Zeeb #include <sys/libkern.h>
616b4cac81SBjoern A. Zeeb 
626b4cac81SBjoern A. Zeeb #include <net/if.h>
636b4cac81SBjoern A. Zeeb #include <net/if_var.h>
646b4cac81SBjoern A. Zeeb #include <net/if_media.h>
656b4cac81SBjoern A. Zeeb #include <net/ethernet.h>
666b4cac81SBjoern A. Zeeb 
676b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_var.h>
686b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_proto.h>
696b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_ratectl.h>
706b4cac81SBjoern A. Zeeb #include <net80211/ieee80211_radiotap.h>
719fb91463SBjoern A. Zeeb #include <net80211/ieee80211_vht.h>
726b4cac81SBjoern A. Zeeb 
736b4cac81SBjoern A. Zeeb #define	LINUXKPI_NET80211
746b4cac81SBjoern A. Zeeb #include <net/mac80211.h>
756b4cac81SBjoern A. Zeeb 
766b4cac81SBjoern A. Zeeb #include <linux/workqueue.h>
77a8f735a6SBjoern A. Zeeb #include <linux/rculist.h>
786b4cac81SBjoern A. Zeeb #include "linux_80211.h"
796b4cac81SBjoern A. Zeeb 
804a67f1dfSBjoern A. Zeeb #define	LKPI_80211_WME
8111db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
829fb91463SBjoern A. Zeeb /* #define	LKPI_80211_HT */
8311db70b6SBjoern A. Zeeb /* #define	LKPI_80211_VHT */
8411db70b6SBjoern A. Zeeb 
859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
869fb91463SBjoern A. Zeeb #define	LKPI_80211_HT
879fb91463SBjoern A. Zeeb #endif
8811db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
8911db70b6SBjoern A. Zeeb #define	LKPI_80211_HW_CRYPTO
9011db70b6SBjoern A. Zeeb #endif
91b35f6cd0SBjoern A. Zeeb 
926b4cac81SBjoern A. Zeeb static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
936b4cac81SBjoern A. Zeeb 
945a9a0d78SBjoern A. Zeeb /* XXX-BZ really want this and others in queue.h */
955a9a0d78SBjoern A. Zeeb #define	TAILQ_ELEM_INIT(elm, field) do {				\
965a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_next = NULL;					\
975a9a0d78SBjoern A. Zeeb 	(elm)->field.tqe_prev = NULL;					\
985a9a0d78SBjoern A. Zeeb } while (0)
995a9a0d78SBjoern A. Zeeb 
1006b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
1016b4cac81SBjoern A. Zeeb 
1029d9ba2b7SBjoern A. Zeeb SYSCTL_DECL(_compat_linuxkpi);
1039d9ba2b7SBjoern A. Zeeb SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1049d9ba2b7SBjoern A. Zeeb     "LinuxKPI 802.11 compatibility layer");
1059d9ba2b7SBjoern A. Zeeb 
10611db70b6SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO)
10711db70b6SBjoern A. Zeeb static bool lkpi_hwcrypto = false;
10811db70b6SBjoern A. Zeeb SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
10911db70b6SBjoern A. Zeeb     &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
11011db70b6SBjoern A. Zeeb #endif
11111db70b6SBjoern A. Zeeb 
11240839418SBjoern A. Zeeb /* Keep public for as long as header files are using it too. */
11340839418SBjoern A. Zeeb int linuxkpi_debug_80211;
11440839418SBjoern A. Zeeb 
11540839418SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
1169d9ba2b7SBjoern A. Zeeb SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
1179d9ba2b7SBjoern A. Zeeb     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
1189d9ba2b7SBjoern A. Zeeb 
1199d9ba2b7SBjoern A. Zeeb #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
1206b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
1219d9ba2b7SBjoern A. Zeeb #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
1226b4cac81SBjoern A. Zeeb     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
1236b4cac81SBjoern A. Zeeb #else
1246b4cac81SBjoern A. Zeeb #define	UNIMPLEMENTED		do { } while (0)
1256b4cac81SBjoern A. Zeeb #define	TRACEOK()		do { } while (0)
1266b4cac81SBjoern A. Zeeb #endif
1276b4cac81SBjoern A. Zeeb 
1286b4cac81SBjoern A. Zeeb /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
1296b4cac81SBjoern A. Zeeb #ifndef PREP_TX_INFO_DURATION
1306b4cac81SBjoern A. Zeeb #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
1316b4cac81SBjoern A. Zeeb #endif
1326b4cac81SBjoern A. Zeeb 
1336b4cac81SBjoern A. Zeeb /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
1346b4cac81SBjoern A. Zeeb const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1356b4cac81SBjoern A. Zeeb 
136b0f73768SBjoern A. Zeeb /* IEEE 802.11-05/0257r1 */
137b0f73768SBjoern A. Zeeb const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
138b0f73768SBjoern A. Zeeb 
139fb3c249eSBjoern A. Zeeb /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
140fb3c249eSBjoern A. Zeeb static const uint8_t ieee80211e_up_to_ac[] = {
1414f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1424f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1434f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BK,
1444f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_BE,
1454f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1464f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VI,
1474f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1484f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO,
1494f61ef8bSBjoern A. Zeeb #if 0
1504f61ef8bSBjoern A. Zeeb 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1514f61ef8bSBjoern A. Zeeb #endif
1524f61ef8bSBjoern A. Zeeb };
1534f61ef8bSBjoern A. Zeeb 
1546b4cac81SBjoern A. Zeeb const struct cfg80211_ops linuxkpi_mac80211cfgops = {
1556b4cac81SBjoern A. Zeeb 	/*
1566b4cac81SBjoern A. Zeeb 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
1576b4cac81SBjoern A. Zeeb 	 * mac80211 and to the driver or net80211.
1586b4cac81SBjoern A. Zeeb 	 * Can we pass some on 1:1? Need to compare the (*f)().
1596b4cac81SBjoern A. Zeeb 	 */
1606b4cac81SBjoern A. Zeeb };
1616b4cac81SBjoern A. Zeeb 
162ac867c20SBjoern A. Zeeb #if 0
1636b4cac81SBjoern A. Zeeb static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
1646b4cac81SBjoern A. Zeeb     struct ieee80211_node *);
165ac867c20SBjoern A. Zeeb #endif
16688665349SBjoern A. Zeeb static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
1676b4cac81SBjoern A. Zeeb static void lkpi_80211_txq_task(void *, int);
168759a996dSBjoern A. Zeeb static void lkpi_80211_lhw_rxq_task(void *, int);
1696b4cac81SBjoern A. Zeeb static void lkpi_ieee80211_free_skb_mbuf(void *);
1704a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
1714a67f1dfSBjoern A. Zeeb static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
1724a67f1dfSBjoern A. Zeeb #endif
1736b4cac81SBjoern A. Zeeb 
17440839418SBjoern A. Zeeb static const char *
17540839418SBjoern A. Zeeb lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
17640839418SBjoern A. Zeeb {
17740839418SBjoern A. Zeeb 
17840839418SBjoern A. Zeeb 	switch (bw) {
17940839418SBjoern A. Zeeb 
18040839418SBjoern A. Zeeb         case RATE_INFO_BW_20:
18140839418SBjoern A. Zeeb 		return ("20");
18240839418SBjoern A. Zeeb 		break;
18340839418SBjoern A. Zeeb         case RATE_INFO_BW_5:
18440839418SBjoern A. Zeeb 		return ("5");
18540839418SBjoern A. Zeeb 		break;
18640839418SBjoern A. Zeeb         case RATE_INFO_BW_10:
18740839418SBjoern A. Zeeb 		return ("10");
18840839418SBjoern A. Zeeb 		break;
18940839418SBjoern A. Zeeb         case RATE_INFO_BW_40:
19040839418SBjoern A. Zeeb 		return ("40");
19140839418SBjoern A. Zeeb 		break;
19240839418SBjoern A. Zeeb         case RATE_INFO_BW_80:
19340839418SBjoern A. Zeeb 		return ("80");
19440839418SBjoern A. Zeeb 		break;
19540839418SBjoern A. Zeeb         case RATE_INFO_BW_160:
19640839418SBjoern A. Zeeb 		return ("160");
19740839418SBjoern A. Zeeb 		break;
19840839418SBjoern A. Zeeb         case RATE_INFO_BW_HE_RU:
19940839418SBjoern A. Zeeb 		IMPROVE("nl80211_he_ru_alloc");
20040839418SBjoern A. Zeeb 		return ("HE_RU");
20140839418SBjoern A. Zeeb 		break;
20240839418SBjoern A. Zeeb         case RATE_INFO_BW_320:
20340839418SBjoern A. Zeeb 		return ("320");
20440839418SBjoern A. Zeeb 		break;
20540839418SBjoern A. Zeeb         case RATE_INFO_BW_EHT_RU:
20640839418SBjoern A. Zeeb 		IMPROVE("nl80211_eht_ru_alloc");
20740839418SBjoern A. Zeeb 		return ("EHT_RU");
20840839418SBjoern A. Zeeb 		break;
20940839418SBjoern A. Zeeb 	default:
21040839418SBjoern A. Zeeb 		return ("?");
21140839418SBjoern A. Zeeb 		break;
21240839418SBjoern A. Zeeb 	}
21340839418SBjoern A. Zeeb }
21440839418SBjoern A. Zeeb 
21540839418SBjoern A. Zeeb static void
21640839418SBjoern A. Zeeb lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
21740839418SBjoern A. Zeeb     const uint64_t flags)
21840839418SBjoern A. Zeeb {
21940839418SBjoern A. Zeeb 	int bit, i;
22040839418SBjoern A. Zeeb 
22140839418SBjoern A. Zeeb 	sbuf_printf(s, "%s %#010jx", prefix, flags);
22240839418SBjoern A. Zeeb 
22340839418SBjoern A. Zeeb 	i = 0;
22440839418SBjoern A. Zeeb 	for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
22540839418SBjoern A. Zeeb 
22640839418SBjoern A. Zeeb 		if ((flags & BIT_ULL(bit)) == 0)
22740839418SBjoern A. Zeeb 			continue;
22840839418SBjoern A. Zeeb 
22940839418SBjoern A. Zeeb #define	EXPAND_CASE(_flag)						\
23040839418SBjoern A. Zeeb 	case NL80211_STA_INFO_ ## _flag:				\
23140839418SBjoern A. Zeeb 		sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag);	\
23240839418SBjoern A. Zeeb 		i++;							\
23340839418SBjoern A. Zeeb 		break;
23440839418SBjoern A. Zeeb 
23540839418SBjoern A. Zeeb 		switch (bit) {
23640839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_RX)
23740839418SBjoern A. Zeeb 		EXPAND_CASE(BEACON_SIGNAL_AVG)
23840839418SBjoern A. Zeeb 		EXPAND_CASE(BSS_PARAM)
23940839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL)
24040839418SBjoern A. Zeeb 		EXPAND_CASE(CHAIN_SIGNAL_AVG)
24140839418SBjoern A. Zeeb 		EXPAND_CASE(CONNECTED_TIME)
24240839418SBjoern A. Zeeb 		EXPAND_CASE(INACTIVE_TIME)
24340839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL)
24440839418SBjoern A. Zeeb 		EXPAND_CASE(SIGNAL_AVG)
24540839418SBjoern A. Zeeb 		EXPAND_CASE(STA_FLAGS)
24640839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BITRATE)
24740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_PACKETS)
24840839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES)
24940839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DROP_MISC)
25040839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BITRATE)
25140839418SBjoern A. Zeeb 		EXPAND_CASE(TX_PACKETS)
25240839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES)
25340839418SBjoern A. Zeeb 		EXPAND_CASE(TX_BYTES64)
25440839418SBjoern A. Zeeb 		EXPAND_CASE(RX_BYTES64)
25540839418SBjoern A. Zeeb 		EXPAND_CASE(TX_FAILED)
25640839418SBjoern A. Zeeb 		EXPAND_CASE(TX_RETRIES)
25740839418SBjoern A. Zeeb 		EXPAND_CASE(RX_DURATION)
25840839418SBjoern A. Zeeb 		EXPAND_CASE(TX_DURATION)
25940839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL)
26040839418SBjoern A. Zeeb 		EXPAND_CASE(ACK_SIGNAL_AVG)
26140839418SBjoern A. Zeeb 		default:
26240839418SBjoern A. Zeeb 			sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
26340839418SBjoern A. Zeeb 			break;
26440839418SBjoern A. Zeeb 		}
26540839418SBjoern A. Zeeb 	}
26640839418SBjoern A. Zeeb #undef	EXPAND_CASE
26740839418SBjoern A. Zeeb 	if (i > 0)
26840839418SBjoern A. Zeeb 		sbuf_printf(s, ">");
26940839418SBjoern A. Zeeb 	sbuf_printf(s, "\n");
27040839418SBjoern A. Zeeb }
27140839418SBjoern A. Zeeb 
27240839418SBjoern A. Zeeb static int
27340839418SBjoern A. Zeeb lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
27440839418SBjoern A. Zeeb {
27540839418SBjoern A. Zeeb 	struct lkpi_hw *lhw;
27640839418SBjoern A. Zeeb 	struct ieee80211_hw *hw;
27740839418SBjoern A. Zeeb 	struct ieee80211vap *vap;
27840839418SBjoern A. Zeeb 	struct lkpi_vif *lvif;
27940839418SBjoern A. Zeeb 	struct ieee80211_vif *vif;
28040839418SBjoern A. Zeeb 	struct lkpi_sta *lsta;
28140839418SBjoern A. Zeeb 	struct ieee80211_sta *sta;
28240839418SBjoern A. Zeeb 	struct station_info sinfo;
28340839418SBjoern A. Zeeb 	struct sbuf s;
28440839418SBjoern A. Zeeb 	int error;
28540839418SBjoern A. Zeeb 
28640839418SBjoern A. Zeeb 	if (req->newptr)
28740839418SBjoern A. Zeeb 		return (EPERM);
28840839418SBjoern A. Zeeb 
28940839418SBjoern A. Zeeb 	lvif = (struct lkpi_vif *)arg1;
29040839418SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
29140839418SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
29240839418SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
29340839418SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
29440839418SBjoern A. Zeeb 
29540839418SBjoern A. Zeeb 	sbuf_new_for_sysctl(&s, NULL, 1024, req);
29640839418SBjoern A. Zeeb 
29740839418SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
298a8f735a6SBjoern A. Zeeb 	list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
29940839418SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
30040839418SBjoern A. Zeeb 
30140839418SBjoern A. Zeeb 		sbuf_putc(&s, '\n');
30240839418SBjoern A. Zeeb 		sbuf_printf(&s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
30340839418SBjoern A. Zeeb 
30440839418SBjoern A. Zeeb 		memset(&sinfo, 0, sizeof(sinfo));
30540839418SBjoern A. Zeeb 		error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
30640839418SBjoern A. Zeeb 		if (error == EEXIST)	/* Not added to driver. */
30740839418SBjoern A. Zeeb 			continue;
30840839418SBjoern A. Zeeb 		if (error == ENOTSUPP) {
30940839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics not supported\n");
31040839418SBjoern A. Zeeb 			continue;
31140839418SBjoern A. Zeeb 		}
31240839418SBjoern A. Zeeb 		if (error != 0) {
31340839418SBjoern A. Zeeb 			sbuf_printf(&s, " sta_statistics failed: %d\n", error);
31440839418SBjoern A. Zeeb 			continue;
31540839418SBjoern A. Zeeb 		}
31640839418SBjoern A. Zeeb 
31740839418SBjoern A. Zeeb 		lkpi_nl80211_sta_info_to_str(&s, " nl80211_sta_info (valid fields)", sinfo.filled);
31840839418SBjoern A. Zeeb 		sbuf_printf(&s, " connected_time %u inactive_time %u\n",
31940839418SBjoern A. Zeeb 		    sinfo.connected_time, sinfo.inactive_time);
32040839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
32140839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
32240839418SBjoern A. Zeeb 		sbuf_printf(&s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
32340839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
32440839418SBjoern A. Zeeb 
32540839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
32640839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
32740839418SBjoern A. Zeeb 		sbuf_printf(&s, " tx_duration %ju tx_retries %u\n",
32840839418SBjoern A. Zeeb 		    (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
32940839418SBjoern A. Zeeb 
33040839418SBjoern A. Zeeb 		sbuf_printf(&s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
33140839418SBjoern A. Zeeb 		    sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
33240839418SBjoern A. Zeeb 
33340839418SBjoern A. Zeeb 		sbuf_printf(&s, " generation %d assoc_req_ies_len %zu chains %d\n",
33440839418SBjoern A. Zeeb 		    sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
33540839418SBjoern A. Zeeb 
33640839418SBjoern A. Zeeb 		for (int i = 0; i < sinfo.chains && i < IEEE80211_MAX_CHAINS; i++) {
33740839418SBjoern A. Zeeb 			sbuf_printf(&s, "  chain[%d] signal %d signal_avg %d\n",
33840839418SBjoern A. Zeeb 			    i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
33940839418SBjoern A. Zeeb 		}
34040839418SBjoern A. Zeeb 
34140839418SBjoern A. Zeeb 		/* assoc_req_ies, bss_param, sta_flags */
34240839418SBjoern A. Zeeb 
34340839418SBjoern A. Zeeb 		sbuf_printf(&s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
34440839418SBjoern A. Zeeb 		    sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
34540839418SBjoern A. Zeeb 		    sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
34640839418SBjoern A. Zeeb 		    sinfo.rxrate.legacy * 100,
34740839418SBjoern A. Zeeb 		    sinfo.rxrate.mcs, sinfo.rxrate.nss);
34840839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
34940839418SBjoern A. Zeeb 		    sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
35040839418SBjoern A. Zeeb 		    sinfo.rxrate.eht_gi);
35140839418SBjoern A. Zeeb 		sbuf_printf(&s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
35240839418SBjoern A. Zeeb 		    sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
35340839418SBjoern A. Zeeb 		    sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
35440839418SBjoern A. Zeeb 		    sinfo.txrate.legacy * 100,
35540839418SBjoern A. Zeeb 		    sinfo.txrate.mcs, sinfo.txrate.nss);
35640839418SBjoern A. Zeeb 		sbuf_printf(&s, "         he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
35740839418SBjoern A. Zeeb 		    sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
35840839418SBjoern A. Zeeb 		    sinfo.txrate.eht_gi);
35940839418SBjoern A. Zeeb 	}
36040839418SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
36140839418SBjoern A. Zeeb 
36240839418SBjoern A. Zeeb 	sbuf_finish(&s);
36340839418SBjoern A. Zeeb 	sbuf_delete(&s);
36440839418SBjoern A. Zeeb 
36540839418SBjoern A. Zeeb 	return (0);
36640839418SBjoern A. Zeeb }
36740839418SBjoern A. Zeeb 
3689fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3699fb91463SBjoern A. Zeeb static void
3709fb91463SBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
3719fb91463SBjoern A. Zeeb {
3729fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
3739fb91463SBjoern A. Zeeb 	uint8_t *ie;
3749fb91463SBjoern A. Zeeb 	struct ieee80211_ht_cap *htcap;
3759fb91463SBjoern A. Zeeb 	int i, rx_nss;
3769fb91463SBjoern A. Zeeb 
3779fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
3789fb91463SBjoern A. Zeeb 		return;
3799fb91463SBjoern A. Zeeb 
3809fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3819fb91463SBjoern A. Zeeb 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3829fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
3839fb91463SBjoern A. Zeeb 
3849fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
3859fb91463SBjoern A. Zeeb 
3869fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
3879fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
3889fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3899fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
3909fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
3919fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3929fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
3939fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
3949fb91463SBjoern A. Zeeb 
3959fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
3969fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
3979fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
3989fb91463SBjoern A. Zeeb 		ie += 4;
3999fb91463SBjoern A. Zeeb 	ie += 2;
4009fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
4019fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4029fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4039fb91463SBjoern A. Zeeb 
4049fb91463SBjoern A. Zeeb 	rx_nss = 0;
4059fb91463SBjoern A. Zeeb 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
4069fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4079fb91463SBjoern A. Zeeb 			rx_nss++;
4089fb91463SBjoern A. Zeeb 	}
4099fb91463SBjoern A. Zeeb 	if (ht_rx_nss != NULL)
4109fb91463SBjoern A. Zeeb 		*ht_rx_nss = rx_nss;
4119fb91463SBjoern A. Zeeb 
4129fb91463SBjoern A. Zeeb 	IMPROVE("sta->wme, sta->deflink.agg.max*");
4139fb91463SBjoern A. Zeeb }
4149fb91463SBjoern A. Zeeb #endif
4159fb91463SBjoern A. Zeeb 
4169fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4179fb91463SBjoern A. Zeeb static void
4189fb91463SBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
4199fb91463SBjoern A. Zeeb {
4209fb91463SBjoern A. Zeeb 
4219fb91463SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
4229fb91463SBjoern A. Zeeb 		return;
4239fb91463SBjoern A. Zeeb 
4249fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
4259fb91463SBjoern A. Zeeb #ifdef __notyet__
4269fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
4279fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
4289fb91463SBjoern A. Zeeb 		} else
4299fb91463SBjoern A. Zeeb #endif
4309fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
4319fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
4329fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
4339fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4349fb91463SBjoern A. Zeeb 	}
4359fb91463SBjoern A. Zeeb 
4369fb91463SBjoern A. Zeeb 	IMPROVE("VHT sync ni to sta");
4379fb91463SBjoern A. Zeeb 	return;
4389fb91463SBjoern A. Zeeb }
4399fb91463SBjoern A. Zeeb #endif
4409fb91463SBjoern A. Zeeb 
441d9f59799SBjoern A. Zeeb static void
44267674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
44367674c1cSBjoern A. Zeeb     const char *_f, int _l)
444d9f59799SBjoern A. Zeeb {
445d9f59799SBjoern A. Zeeb 
4469d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
4479d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
448d9f59799SBjoern A. Zeeb 		return;
449d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
450d9f59799SBjoern A. Zeeb 		return;
451d9f59799SBjoern A. Zeeb 
452d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
45367674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
45467674c1cSBjoern A. Zeeb 	if (ni != NULL)
45567674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
456d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
457d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
45811db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
4599d9ba2b7SBjoern A. Zeeb #endif
460d9f59799SBjoern A. Zeeb }
461d9f59799SBjoern A. Zeeb 
462d9f59799SBjoern A. Zeeb static void
463d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
464d9f59799SBjoern A. Zeeb {
465d9f59799SBjoern A. Zeeb 
466d9f59799SBjoern A. Zeeb 
467a8f735a6SBjoern A. Zeeb 	wiphy_lock(lsta->hw->wiphy);
468a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
469a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
470a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
471a8f735a6SBjoern A. Zeeb 	wiphy_unlock(lsta->hw->wiphy);
472d9f59799SBjoern A. Zeeb }
473d9f59799SBjoern A. Zeeb 
4744f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
4754f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
4764f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
4774f61ef8bSBjoern A. Zeeb {
4784f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
4794f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
4804f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
4814f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
482b6b352e4SBjoern A. Zeeb 	int band, i, tid;
4839fb91463SBjoern A. Zeeb 	int ht_rx_nss;
4849fb91463SBjoern A. Zeeb 	int vht_rx_nss;
4854f61ef8bSBjoern A. Zeeb 
4864f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
4874f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
4884f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
4894f61ef8bSBjoern A. Zeeb 		return (NULL);
4904f61ef8bSBjoern A. Zeeb 
491a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
4924f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
4934f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
4944f61ef8bSBjoern A. Zeeb 	/*
4950936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
4960936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
4970936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
4980936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
4990936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
5000936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
5010936c648SBjoern A. Zeeb 	 * two separate allocations.
5024f61ef8bSBjoern A. Zeeb 	 */
5030936c648SBjoern A. Zeeb 	lsta->ni = ni;
5044f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
5054f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
5064f61ef8bSBjoern A. Zeeb 
5074f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5084f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5094f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
5104f61ef8bSBjoern A. Zeeb 
5114f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
512b6b352e4SBjoern A. Zeeb 
513b6b352e4SBjoern A. Zeeb 	/* TXQ */
5144f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
5154f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
5164f61ef8bSBjoern A. Zeeb 
517d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
5184f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
5194f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
5204f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
5214f61ef8bSBjoern A. Zeeb 			goto cleanup;
5224f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
5234f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
524d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
525d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
526d0d29110SBjoern A. Zeeb 				continue;
527d0d29110SBjoern A. Zeeb 			}
528d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
5294f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
5304f61ef8bSBjoern A. Zeeb 		} else {
531fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
5324f61ef8bSBjoern A. Zeeb 		}
533d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
5340cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
535d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
5364f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
5374f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
5380cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
539d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
540eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
5414f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
5424f61ef8bSBjoern A. Zeeb 	}
5434f61ef8bSBjoern A. Zeeb 
544b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
545b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
546b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
547b6b352e4SBjoern A. Zeeb 
548b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
549b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
550b6b352e4SBjoern A. Zeeb 			continue;
551b6b352e4SBjoern A. Zeeb 
552b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
55373cd1c5dSBjoern A. Zeeb 			switch (band) {
55473cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
55573cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
55673cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
55773cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
55873cd1c5dSBjoern A. Zeeb 				case 110:
55973cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
56073cd1c5dSBjoern A. Zeeb 				case 55:
56173cd1c5dSBjoern A. Zeeb 				case 20:
56273cd1c5dSBjoern A. Zeeb 				case 10:
563b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
56473cd1c5dSBjoern A. Zeeb 					break;
56573cd1c5dSBjoern A. Zeeb 				}
56673cd1c5dSBjoern A. Zeeb 				break;
56773cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
56873cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
56973cd1c5dSBjoern A. Zeeb 				case 240:
57073cd1c5dSBjoern A. Zeeb 				case 120:
57173cd1c5dSBjoern A. Zeeb 				case 60:
57273cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
57373cd1c5dSBjoern A. Zeeb 					break;
57473cd1c5dSBjoern A. Zeeb 				}
57573cd1c5dSBjoern A. Zeeb 				break;
57673cd1c5dSBjoern A. Zeeb 			}
577b6b352e4SBjoern A. Zeeb 		}
578b6b352e4SBjoern A. Zeeb 	}
5799fb91463SBjoern A. Zeeb 
5806ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
5819fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
582f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
5839fb91463SBjoern A. Zeeb 
5849fb91463SBjoern A. Zeeb 	ht_rx_nss = 0;
5859fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
5869fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
5879fb91463SBjoern A. Zeeb #endif
5889fb91463SBjoern A. Zeeb 	vht_rx_nss = 0;
5899fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
5909fb91463SBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
5919fb91463SBjoern A. Zeeb #endif
5929fb91463SBjoern A. Zeeb 
5939fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
5949fb91463SBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
5959fb91463SBjoern A. Zeeb 	IMPROVE("he, ... smps_mode, ..");
596b6b352e4SBjoern A. Zeeb 
5976ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
5986ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
5996ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
6006ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
6016ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
6026ffb7bd4SBjoern A. Zeeb 	}
6036ffb7bd4SBjoern A. Zeeb 
6044f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
605fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
6064f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
6074f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
6080936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
6094f61ef8bSBjoern A. Zeeb 
6104f61ef8bSBjoern A. Zeeb 	return (lsta);
6114f61ef8bSBjoern A. Zeeb 
6124f61ef8bSBjoern A. Zeeb cleanup:
613eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
614eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
615eac3646fSBjoern A. Zeeb 
616eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
617eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
6184f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
619eac3646fSBjoern A. Zeeb 	}
6204f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6214f61ef8bSBjoern A. Zeeb 	return (NULL);
6224f61ef8bSBjoern A. Zeeb }
6234f61ef8bSBjoern A. Zeeb 
6240936c648SBjoern A. Zeeb static void
6250936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
6260936c648SBjoern A. Zeeb {
6270936c648SBjoern A. Zeeb 	struct mbuf *m;
6280936c648SBjoern A. Zeeb 
6290936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
6300936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
6310936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
6320936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
6330936c648SBjoern A. Zeeb 
6340936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
6350936c648SBjoern A. Zeeb 	IMPROVE();
6360936c648SBjoern A. Zeeb 
637fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
638fa4e4257SBjoern A. Zeeb 
639fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
6400936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
641fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
6420936c648SBjoern A. Zeeb 
6430936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
6440936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
6450936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
6460936c648SBjoern A. Zeeb 
6470936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
6480936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
6490936c648SBjoern A. Zeeb 	while (m != NULL) {
6500936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
6510936c648SBjoern A. Zeeb 
6520936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
6530936c648SBjoern A. Zeeb 		if (nim != NULL)
6540936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
6550936c648SBjoern A. Zeeb 		m_freem(m);
6560936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
6570936c648SBjoern A. Zeeb 	}
6580936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
6590936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
660fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
6610936c648SBjoern A. Zeeb 
6620936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
6630936c648SBjoern A. Zeeb 
6640936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
6650936c648SBjoern A. Zeeb 
6660936c648SBjoern A. Zeeb 	/* Free lsta. */
6670936c648SBjoern A. Zeeb 	lsta->ni = NULL;
6680936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
6690936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6700936c648SBjoern A. Zeeb }
6710936c648SBjoern A. Zeeb 
6720936c648SBjoern A. Zeeb 
6736b4cac81SBjoern A. Zeeb static enum nl80211_band
6746b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
6756b4cac81SBjoern A. Zeeb {
6766b4cac81SBjoern A. Zeeb 
6776b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
6786b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
6796b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
6806b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
6816b4cac81SBjoern A. Zeeb #ifdef __notyet__
6826b4cac81SBjoern A. Zeeb 	else if ()
6836b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
6846b4cac81SBjoern A. Zeeb 	else if ()
6856b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
6866b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
6876b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
6886b4cac81SBjoern A. Zeeb #endif
6896b4cac81SBjoern A. Zeeb 	else
6906b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
6916b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
6926b4cac81SBjoern A. Zeeb }
6936b4cac81SBjoern A. Zeeb 
6946b4cac81SBjoern A. Zeeb static uint32_t
6956b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
6966b4cac81SBjoern A. Zeeb {
6976b4cac81SBjoern A. Zeeb 
6986b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
6996b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
7006b4cac81SBjoern A. Zeeb 	switch (band) {
7016b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
7026b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
7036b4cac81SBjoern A. Zeeb 		break;
7046b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
7056b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
7066b4cac81SBjoern A. Zeeb 		break;
7076b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
7086b4cac81SBjoern A. Zeeb 		break;
7096b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
7106b4cac81SBjoern A. Zeeb 		break;
7116b4cac81SBjoern A. Zeeb 	default:
7126b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
7136b4cac81SBjoern A. Zeeb 		break;
7146b4cac81SBjoern A. Zeeb 	}
7156b4cac81SBjoern A. Zeeb 
7166b4cac81SBjoern A. Zeeb 	IMPROVE();
7176b4cac81SBjoern A. Zeeb 	return (0x00);
7186b4cac81SBjoern A. Zeeb }
7196b4cac81SBjoern A. Zeeb 
720e3a0b120SBjoern A. Zeeb #if 0
7216b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
7226b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
7236b4cac81SBjoern A. Zeeb {
7246b4cac81SBjoern A. Zeeb 
7256b4cac81SBjoern A. Zeeb 	switch (ac) {
7266b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
7276b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
7286b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
7296b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
7306b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
7316b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7326b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
7336b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
7346b4cac81SBjoern A. Zeeb 	default:
7356b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
7366b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7376b4cac81SBjoern A. Zeeb 	}
7386b4cac81SBjoern A. Zeeb }
739e3a0b120SBjoern A. Zeeb #endif
7406b4cac81SBjoern A. Zeeb 
7416b4cac81SBjoern A. Zeeb static enum nl80211_iftype
7426b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
7436b4cac81SBjoern A. Zeeb {
7446b4cac81SBjoern A. Zeeb 
7456b4cac81SBjoern A. Zeeb 	switch (opmode) {
7466b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
7476b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
7486b4cac81SBjoern A. Zeeb 		break;
7496b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
7506b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
7516b4cac81SBjoern A. Zeeb 		break;
7526b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
7536b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
7546b4cac81SBjoern A. Zeeb 		break;
7556b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
7566b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
7576b4cac81SBjoern A. Zeeb 		break;
7586b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
7596b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
7606b4cac81SBjoern A. Zeeb 		break;
7616b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
7626b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
7636b4cac81SBjoern A. Zeeb 		break;
7646b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
7656b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7666b4cac81SBjoern A. Zeeb 	default:
7676b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
7686b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
7696b4cac81SBjoern A. Zeeb 	}
7706b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
7716b4cac81SBjoern A. Zeeb }
7726b4cac81SBjoern A. Zeeb 
773b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
77412a511c8SBjoern A. Zeeb static const char *
77512a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
77612a511c8SBjoern A. Zeeb {
77712a511c8SBjoern A. Zeeb 
77812a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
77912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
78012a511c8SBjoern A. Zeeb 		return ("WEP40");
78112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
78212a511c8SBjoern A. Zeeb 		return ("TKIP");
78312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
78412a511c8SBjoern A. Zeeb 		return ("CCMP");
78512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
78612a511c8SBjoern A. Zeeb 		return ("WEP104");
78712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
78812a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
78912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
79012a511c8SBjoern A. Zeeb 		return ("GCMP");
79112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
79212a511c8SBjoern A. Zeeb 		return ("GCMP_256");
79312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
79412a511c8SBjoern A. Zeeb 		return ("CCMP_256");
79512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
79612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
79712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
79812a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
79912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
80012a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
80112a511c8SBjoern A. Zeeb 	default:
80212a511c8SBjoern A. Zeeb 		return ("??");
80312a511c8SBjoern A. Zeeb 	}
80412a511c8SBjoern A. Zeeb }
80512a511c8SBjoern A. Zeeb 
8066b4cac81SBjoern A. Zeeb static uint32_t
8076b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
8086b4cac81SBjoern A. Zeeb {
8096b4cac81SBjoern A. Zeeb 
8106b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
8116b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
8126b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
8136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
8146b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
8156b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
816906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
8176b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
8186b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
8196b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
8206b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
8216b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
8226b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
8236b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
8246b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
8256b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
82612a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
82712a511c8SBjoern A. Zeeb 		    __func__,
82812a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
82912a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8306b4cac81SBjoern A. Zeeb 		break;
8316b4cac81SBjoern A. Zeeb 	default:
83212a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
83312a511c8SBjoern A. Zeeb 		    __func__,
83412a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
83512a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8366b4cac81SBjoern A. Zeeb 	}
8376b4cac81SBjoern A. Zeeb 
8386b4cac81SBjoern A. Zeeb 	return (0);
8396b4cac81SBjoern A. Zeeb }
8406b4cac81SBjoern A. Zeeb 
8416b4cac81SBjoern A. Zeeb static uint32_t
8426b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
8436b4cac81SBjoern A. Zeeb {
8446b4cac81SBjoern A. Zeeb 
8456b4cac81SBjoern A. Zeeb 	switch (cipher) {
8466b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
8476b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
8486b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
8496b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
8506b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
8516b4cac81SBjoern A. Zeeb 		if (keylen < 8)
8526b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
8536b4cac81SBjoern A. Zeeb 		else
8546b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
8556b4cac81SBjoern A. Zeeb 		break;
8566b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
8576b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
8586b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
8596b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
8606b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
8616b4cac81SBjoern A. Zeeb 		break;
8626b4cac81SBjoern A. Zeeb 	default:
8636b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
8646b4cac81SBjoern A. Zeeb 	};
8656b4cac81SBjoern A. Zeeb 	return (0);
8666b4cac81SBjoern A. Zeeb }
8676b4cac81SBjoern A. Zeeb #endif
8686b4cac81SBjoern A. Zeeb 
8696b4cac81SBjoern A. Zeeb #ifdef __notyet__
8706b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
8716b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
8726b4cac81SBjoern A. Zeeb {
8736b4cac81SBjoern A. Zeeb 
8746b4cac81SBjoern A. Zeeb 	/*
8756b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
8766b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
8776b4cac81SBjoern A. Zeeb 	 */
8786b4cac81SBjoern A. Zeeb 	switch (state) {
8796b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
8806b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
8816b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
8826b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
8836b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
8846b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
8856b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
8866b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
8876b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
8886b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
8896b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
8906b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
8916b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
8926b4cac81SBjoern A. Zeeb 	default:
8936b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
8946b4cac81SBjoern A. Zeeb 	};
8956b4cac81SBjoern A. Zeeb 
8966b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
8976b4cac81SBjoern A. Zeeb }
8986b4cac81SBjoern A. Zeeb #endif
8996b4cac81SBjoern A. Zeeb 
9006b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9016b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
9026b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
9036b4cac81SBjoern A. Zeeb {
9046b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9056b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
9066b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
9076b4cac81SBjoern A. Zeeb 	int i, nchans;
9086b4cac81SBjoern A. Zeeb 
9096b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9106b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
9116b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
9126b4cac81SBjoern A. Zeeb 		return (NULL);
9136b4cac81SBjoern A. Zeeb 
9146b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
9156b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
9166b4cac81SBjoern A. Zeeb 		return (NULL);
9176b4cac81SBjoern A. Zeeb 
9186b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
9196b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
9206b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
9216b4cac81SBjoern A. Zeeb 			return (&channels[i]);
9226b4cac81SBjoern A. Zeeb 	}
9236b4cac81SBjoern A. Zeeb 
9246b4cac81SBjoern A. Zeeb 	return (NULL);
9256b4cac81SBjoern A. Zeeb }
9266b4cac81SBjoern A. Zeeb 
9272ac8a218SBjoern A. Zeeb #if 0
9286b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9296b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
9306b4cac81SBjoern A. Zeeb {
9316b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
9326b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
9336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9346b4cac81SBjoern A. Zeeb 
9356b4cac81SBjoern A. Zeeb 	chan = NULL;
9366b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
9376b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
9386b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
9396b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
9406b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
9416b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
9426b4cac81SBjoern A. Zeeb 	else
9436b4cac81SBjoern A. Zeeb 		c = NULL;
9446b4cac81SBjoern A. Zeeb 
9456b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
9466b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
9476b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
9486b4cac81SBjoern A. Zeeb 	}
9496b4cac81SBjoern A. Zeeb 
9506b4cac81SBjoern A. Zeeb 	return (chan);
9516b4cac81SBjoern A. Zeeb }
9522ac8a218SBjoern A. Zeeb #endif
9536b4cac81SBjoern A. Zeeb 
9542e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
9552e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
9562e183d99SBjoern A. Zeeb {
9572e183d99SBjoern A. Zeeb 	enum nl80211_band band;
9582e183d99SBjoern A. Zeeb 
9592e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9602e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
9612e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
9622e183d99SBjoern A. Zeeb 		int i;
9632e183d99SBjoern A. Zeeb 
9642e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
9652e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
9662e183d99SBjoern A. Zeeb 			continue;
9672e183d99SBjoern A. Zeeb 
9682e183d99SBjoern A. Zeeb 		channels = supband->channels;
9692e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
9702e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
9712e183d99SBjoern A. Zeeb 				return (&channels[i]);
9722e183d99SBjoern A. Zeeb 		}
9732e183d99SBjoern A. Zeeb 	}
9742e183d99SBjoern A. Zeeb 
9752e183d99SBjoern A. Zeeb 	return (NULL);
9762e183d99SBjoern A. Zeeb }
9772e183d99SBjoern A. Zeeb 
978b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
9796b4cac81SBjoern A. Zeeb static int
98011db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
98111db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
98211db70b6SBjoern A. Zeeb {
98311db70b6SBjoern A. Zeeb 	int error;
98411db70b6SBjoern A. Zeeb 
98511db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
98611db70b6SBjoern A. Zeeb 		return (0);
98711db70b6SBjoern A. Zeeb 
98811db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
98911db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
99011db70b6SBjoern A. Zeeb 
99111db70b6SBjoern A. Zeeb 	error = 0;
99211db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
99311db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
99411db70b6SBjoern A. Zeeb 		int err;
99511db70b6SBjoern A. Zeeb 
99611db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
99711db70b6SBjoern A. Zeeb 			continue;
99811db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
99911db70b6SBjoern A. Zeeb 
100011db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
100111db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
100211db70b6SBjoern A. Zeeb 		if (err != 0) {
100311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
100411db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
100511db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
100611db70b6SBjoern A. Zeeb 			error++;
100711db70b6SBjoern A. Zeeb 
100811db70b6SBjoern A. Zeeb 			/*
100911db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
101011db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
101111db70b6SBjoern A. Zeeb 			 * crash (firmware).
101211db70b6SBjoern A. Zeeb 			 */
101311db70b6SBjoern A. Zeeb 			continue;
101411db70b6SBjoern A. Zeeb 		}
101511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
101611db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
101711db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
101811db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
101911db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
102011db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
102111db70b6SBjoern A. Zeeb #endif
102211db70b6SBjoern A. Zeeb 
102311db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
102411db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
102511db70b6SBjoern A. Zeeb 	}
102611db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
102711db70b6SBjoern A. Zeeb 	return (error);
102811db70b6SBjoern A. Zeeb }
102911db70b6SBjoern A. Zeeb 
103011db70b6SBjoern A. Zeeb static int
103111db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
10326b4cac81SBjoern A. Zeeb {
10336b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
10346b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10356b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10366b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
103711db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10386b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10396b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
10406b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
10416b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
104211db70b6SBjoern A. Zeeb 	struct ieee80211_node_table *nt;
10436b4cac81SBjoern A. Zeeb 	int error;
104411db70b6SBjoern A. Zeeb 	bool islocked;
10456b4cac81SBjoern A. Zeeb 
10466b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
10476b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
10486b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
10496b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
10506b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
10516b4cac81SBjoern A. Zeeb 
105211db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
105311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
105411db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
105511db70b6SBjoern A. Zeeb 		return (0);
105611db70b6SBjoern A. Zeeb 	}
105711db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
105811db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
105911db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
106011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
106111db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
106211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
106311db70b6SBjoern A. Zeeb 		return (0);
106411db70b6SBjoern A. Zeeb 	}
106511db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
106611db70b6SBjoern A. Zeeb 
106711db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
106811db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
106911db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
107011db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
107111db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
107211db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
107311db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
107411db70b6SBjoern A. Zeeb #endif
107511db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
107611db70b6SBjoern A. Zeeb 		return (1);
107711db70b6SBjoern A. Zeeb 	}
107811db70b6SBjoern A. Zeeb 
107911db70b6SBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
108011db70b6SBjoern A. Zeeb 	nt = &ic->ic_sta;
108111db70b6SBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
108211db70b6SBjoern A. Zeeb 	if (islocked)
108311db70b6SBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
108411db70b6SBjoern A. Zeeb 
108511db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
108611db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
108711db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
108811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
108911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
109011db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
109111db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
109211db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
109311db70b6SBjoern A. Zeeb #endif
109411db70b6SBjoern A. Zeeb 		error = 1;
109511db70b6SBjoern A. Zeeb 		goto out;
109611db70b6SBjoern A. Zeeb 	}
109711db70b6SBjoern A. Zeeb 
109811db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
109911db70b6SBjoern A. Zeeb 	if (error != 0) {
110011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
110111db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
110211db70b6SBjoern A. Zeeb 		error = 0;
110311db70b6SBjoern A. Zeeb 		goto out;
110411db70b6SBjoern A. Zeeb 	}
110511db70b6SBjoern A. Zeeb 
110611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
110711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
110811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
110911db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
111011db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
111111db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
111211db70b6SBjoern A. Zeeb #endif
111311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
111411db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
111511db70b6SBjoern A. Zeeb 	error = 1;
111611db70b6SBjoern A. Zeeb out:
111711db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
111811db70b6SBjoern A. Zeeb 	if (islocked)
111911db70b6SBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
112011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
112111db70b6SBjoern A. Zeeb 	return (error);
112211db70b6SBjoern A. Zeeb }
112311db70b6SBjoern A. Zeeb 
112411db70b6SBjoern A. Zeeb static int
112511db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
112611db70b6SBjoern A. Zeeb {
112711db70b6SBjoern A. Zeeb 
112811db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
112911db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
113011db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
113111db70b6SBjoern A. Zeeb }
113211db70b6SBjoern A. Zeeb 
113311db70b6SBjoern A. Zeeb static int
113411db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
113511db70b6SBjoern A. Zeeb {
113611db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
113711db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
113811db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
113911db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
114011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
114111db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
114211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
114311db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
114411db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
114511db70b6SBjoern A. Zeeb 	uint32_t lcipher;
114611db70b6SBjoern A. Zeeb 	int error;
114711db70b6SBjoern A. Zeeb 
114811db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
114911db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
115011db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
115111db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
115211db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
115311db70b6SBjoern A. Zeeb 
115411db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
115511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
115611db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
115711db70b6SBjoern A. Zeeb 		return (0);
115811db70b6SBjoern A. Zeeb 	}
115911db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
116011db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
116111db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
116211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
116311db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
116411db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
116511db70b6SBjoern A. Zeeb 		return (0);
116611db70b6SBjoern A. Zeeb 	}
116711db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
116811db70b6SBjoern A. Zeeb 
116911db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
117011db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
117111db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
117211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
117311db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
117411db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
117511db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
117611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
117711db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
117811db70b6SBjoern A. Zeeb 	}
117911db70b6SBjoern A. Zeeb 
118011db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
11816b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
118211db70b6SBjoern A. Zeeb 	switch (lcipher) {
118311db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
118411db70b6SBjoern A. Zeeb 		break;
118511db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
118611db70b6SBjoern A. Zeeb 	default:
118712a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
118812a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
118911db70b6SBjoern A. Zeeb 		IMPROVE();
119011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
119111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
119211db70b6SBjoern A. Zeeb 		return (0);
119311db70b6SBjoern A. Zeeb 	}
119411db70b6SBjoern A. Zeeb 
119511db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
119611db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
119711db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
119811db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
11996b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
12006b4cac81SBjoern A. Zeeb #if 0
12016b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
12026b4cac81SBjoern A. Zeeb #endif
12036b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
12046b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
12056b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
12066b4cac81SBjoern A. Zeeb 
120711db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
120811db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
120911db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
121011db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
121111db70b6SBjoern A. Zeeb 
12126b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
12136b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
12146b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
12156b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
12166b4cac81SBjoern A. Zeeb 		break;
12176b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
12186b4cac81SBjoern A. Zeeb 	default:
121911db70b6SBjoern A. Zeeb 		/* currently UNREACH */
12206b4cac81SBjoern A. Zeeb 		IMPROVE();
122111db70b6SBjoern A. Zeeb 		break;
12226b4cac81SBjoern A. Zeeb 	};
122311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
12246b4cac81SBjoern A. Zeeb 
122511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
122611db70b6SBjoern A. Zeeb 	if (error != 0) {
122711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
122811db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
122911db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
123011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
123111db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
123211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
123311db70b6SBjoern A. Zeeb 		return (0);
12346b4cac81SBjoern A. Zeeb 	}
12356b4cac81SBjoern A. Zeeb 
123611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
123711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
123811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
123911db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
124011db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
124111db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
124211db70b6SBjoern A. Zeeb #endif
124311db70b6SBjoern A. Zeeb 
124411db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
124511db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
12466b4cac81SBjoern A. Zeeb 	return (1);
12476b4cac81SBjoern A. Zeeb }
12486b4cac81SBjoern A. Zeeb 
12496b4cac81SBjoern A. Zeeb static  int
12506b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
12516b4cac81SBjoern A. Zeeb {
12526b4cac81SBjoern A. Zeeb 
125311db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
12546b4cac81SBjoern A. Zeeb }
12556b4cac81SBjoern A. Zeeb #endif
12566b4cac81SBjoern A. Zeeb 
12576b4cac81SBjoern A. Zeeb static u_int
12586b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
12596b4cac81SBjoern A. Zeeb {
12606b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
12616b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
12626b4cac81SBjoern A. Zeeb 
12636b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
12646b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
12656b4cac81SBjoern A. Zeeb 
12666b4cac81SBjoern A. Zeeb 	mc_list = arg;
12676b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
12686b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
12696b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
12706b4cac81SBjoern A. Zeeb 			return (0);
12716b4cac81SBjoern A. Zeeb 	}
12726b4cac81SBjoern A. Zeeb 
12736b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
12746b4cac81SBjoern A. Zeeb 	if (addr == NULL)
12756b4cac81SBjoern A. Zeeb 		return (0);
12766b4cac81SBjoern A. Zeeb 
12776b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
12786b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
12796b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
12806b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
12816b4cac81SBjoern A. Zeeb 	mc_list->count++;
12826b4cac81SBjoern A. Zeeb 
12839d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
12849d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
12856b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
12866b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
12879d9ba2b7SBjoern A. Zeeb #endif
12886b4cac81SBjoern A. Zeeb 
12896b4cac81SBjoern A. Zeeb 	return (1);
12906b4cac81SBjoern A. Zeeb }
12916b4cac81SBjoern A. Zeeb 
12926b4cac81SBjoern A. Zeeb static void
12936b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
12946b4cac81SBjoern A. Zeeb {
12956b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
12966b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
12976b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
12986b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
12996b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13006b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
13016b4cac81SBjoern A. Zeeb 	u64 mc;
13026b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
13036b4cac81SBjoern A. Zeeb 
13046b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
13056b4cac81SBjoern A. Zeeb 
13066b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
13076b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
13086b4cac81SBjoern A. Zeeb 		return;
13096b4cac81SBjoern A. Zeeb 
13106b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
13116b4cac81SBjoern A. Zeeb 		return;
13126b4cac81SBjoern A. Zeeb 
13136b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
13146b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
13156b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
13166b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
13176b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
13186b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
13196b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
13206b4cac81SBjoern A. Zeeb 	} else {
13216b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
13226b4cac81SBjoern A. Zeeb 	}
13236b4cac81SBjoern A. Zeeb 
13246b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13256b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
13266b4cac81SBjoern A. Zeeb 	/*
13276b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
13286b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
13296b4cac81SBjoern A. Zeeb 	 */
13306b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
13316b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
13326b4cac81SBjoern A. Zeeb 
13339d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13349d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13356b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
13366b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
13379d9ba2b7SBjoern A. Zeeb #endif
13386b4cac81SBjoern A. Zeeb 
13396b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
13406b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
13416b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
13426b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
13436b4cac81SBjoern A. Zeeb 			mc_list.count--;
13446b4cac81SBjoern A. Zeeb 		}
13456b4cac81SBjoern A. Zeeb 	}
13466b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
13476b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
13486b4cac81SBjoern A. Zeeb }
13496b4cac81SBjoern A. Zeeb 
1350fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1351fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1352fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1353fa8f007dSBjoern A. Zeeb {
1354fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1355fa8f007dSBjoern A. Zeeb 
1356fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1357fa8f007dSBjoern A. Zeeb 
13589d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13599d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1360fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1361fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1362ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1363fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1364616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1365fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1366fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1367fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1368fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1369ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
13709d9ba2b7SBjoern A. Zeeb #endif
1371fa8f007dSBjoern A. Zeeb 
1372fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1373fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1374fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1375fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1376fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1377fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1378fa8f007dSBjoern A. Zeeb 	}
1379fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1380fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1381fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1382fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1383fa8f007dSBjoern A. Zeeb 	}
1384fa8f007dSBjoern A. Zeeb 
1385fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1386fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1387fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1388fa8f007dSBjoern A. Zeeb 
13899d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13909d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1391fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1392fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1393ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1394fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1395616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1396fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1397fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1398fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1399fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1400ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14019d9ba2b7SBjoern A. Zeeb #endif
1402fa8f007dSBjoern A. Zeeb 
1403fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1404fa8f007dSBjoern A. Zeeb }
1405fa8f007dSBjoern A. Zeeb 
14066b4cac81SBjoern A. Zeeb static void
14076b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
14086b4cac81SBjoern A. Zeeb {
14096b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14106b4cac81SBjoern A. Zeeb 	int error;
14118ac540d3SBjoern A. Zeeb 	bool cancel;
14126b4cac81SBjoern A. Zeeb 
14138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
14148ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
14158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
14168ac540d3SBjoern A. Zeeb 	if (!cancel)
14176b4cac81SBjoern A. Zeeb 		return;
14186b4cac81SBjoern A. Zeeb 
14196b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14206b4cac81SBjoern A. Zeeb 
1421bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1422bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14236b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
14246b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
14258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14266b4cac81SBjoern A. Zeeb 
14276b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
14288ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
14298ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
14308ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
14318ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
14328ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
14338ac540d3SBjoern A. Zeeb 
1434bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
14356b4cac81SBjoern A. Zeeb 
14368ac540d3SBjoern A. Zeeb 	if (cancel)
14376b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
14386b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
14396b4cac81SBjoern A. Zeeb }
14406b4cac81SBjoern A. Zeeb 
14416b4cac81SBjoern A. Zeeb static void
1442086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1443086be6a8SBjoern A. Zeeb {
1444086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1445086be6a8SBjoern A. Zeeb 	int error;
1446086be6a8SBjoern A. Zeeb 	bool old;
1447086be6a8SBjoern A. Zeeb 
1448086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1449086be6a8SBjoern A. Zeeb 	if (old == new)
1450086be6a8SBjoern A. Zeeb 		return;
1451086be6a8SBjoern A. Zeeb 
1452086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1453086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1454086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1455086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1456086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1457086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1458086be6a8SBjoern A. Zeeb 	}
1459086be6a8SBjoern A. Zeeb }
1460086be6a8SBjoern A. Zeeb 
14615a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
14626b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
14636b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
14646b4cac81SBjoern A. Zeeb {
14655a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
14665a4d2461SBjoern A. Zeeb 
14675a4d2461SBjoern A. Zeeb 	changed = 0;
14686b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1469616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
14706b4cac81SBjoern A. Zeeb 
14716b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
14726b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
14736b4cac81SBjoern A. Zeeb 
1474616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1475616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
14766b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
14776b4cac81SBjoern A. Zeeb 		IMPROVE();
1478086be6a8SBjoern A. Zeeb 
14795a4d2461SBjoern A. Zeeb 		/*
14805a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
14815a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
14825a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
14835a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
14845a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
14855a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
14865a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
14875a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
14885a4d2461SBjoern A. Zeeb 		 */
14896b4cac81SBjoern A. Zeeb 	}
14905a4d2461SBjoern A. Zeeb 
14915a4d2461SBjoern A. Zeeb 	return (changed);
14926b4cac81SBjoern A. Zeeb }
14936b4cac81SBjoern A. Zeeb 
14946b4cac81SBjoern A. Zeeb static void
14956b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
14966b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
14976b4cac81SBjoern A. Zeeb {
14986b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
14996b4cac81SBjoern A. Zeeb 	int tid;
1500eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
15016b4cac81SBjoern A. Zeeb 
15026b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
15036b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
15046b4cac81SBjoern A. Zeeb 
15056b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
15066b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
15076b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
15086b4cac81SBjoern A. Zeeb 				continue;
15096b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
15106b4cac81SBjoern A. Zeeb 			continue;
15116b4cac81SBjoern A. Zeeb 
15126b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
15136b4cac81SBjoern A. Zeeb 			continue;
15146b4cac81SBjoern A. Zeeb 
15156b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
15166b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
15176b4cac81SBjoern A. Zeeb 			continue;
15186b4cac81SBjoern A. Zeeb 
1519eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1520eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1521eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1522eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
15236b4cac81SBjoern A. Zeeb 			continue;
15246b4cac81SBjoern A. Zeeb 
15256b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
15266b4cac81SBjoern A. Zeeb 	}
15276b4cac81SBjoern A. Zeeb }
15286b4cac81SBjoern A. Zeeb 
152988665349SBjoern A. Zeeb /*
153088665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
153188665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
153288665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
153388665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
153488665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
153588665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
153688665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
153788665349SBjoern A. Zeeb  * re-used.
153888665349SBjoern A. Zeeb  */
153988665349SBjoern A. Zeeb static void
154088665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
154188665349SBjoern A. Zeeb {
154288665349SBjoern A. Zeeb 	struct mbufq mq;
154388665349SBjoern A. Zeeb 	struct mbuf *m;
154488665349SBjoern A. Zeeb 	int len;
154588665349SBjoern A. Zeeb 
154688665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
154788665349SBjoern A. Zeeb 
154888665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
154988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
155088665349SBjoern A. Zeeb 	lsta->txq_ready = false;
155188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
155288665349SBjoern A. Zeeb 
155388665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
155488665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
155588665349SBjoern A. Zeeb 
155688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
155788665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
155888665349SBjoern A. Zeeb 	if (len <= 0) {
155988665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
156088665349SBjoern A. Zeeb 		return;
156188665349SBjoern A. Zeeb 	}
156288665349SBjoern A. Zeeb 
156388665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
156488665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
156588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
156688665349SBjoern A. Zeeb 
156788665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
156888665349SBjoern A. Zeeb 	while (m != NULL) {
156988665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
157088665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
157188665349SBjoern A. Zeeb 	}
157288665349SBjoern A. Zeeb }
157388665349SBjoern A. Zeeb 
15746b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
15756b4cac81SBjoern A. Zeeb 
15766b4cac81SBjoern A. Zeeb static int
15776b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15786b4cac81SBjoern A. Zeeb {
15796b4cac81SBjoern A. Zeeb 
15806b4cac81SBjoern A. Zeeb 	return (0);
15816b4cac81SBjoern A. Zeeb }
15826b4cac81SBjoern A. Zeeb 
15836b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
15846b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
15856b4cac81SBjoern A. Zeeb 
15866b4cac81SBjoern A. Zeeb static int
15876b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15886b4cac81SBjoern A. Zeeb {
15896b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1590c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1591d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
15926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
15936b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
15946b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
15956b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
15966b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
15976b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
15986b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
15996b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16006b4cac81SBjoern A. Zeeb 	uint32_t changed;
16016b4cac81SBjoern A. Zeeb 	int error;
16026b4cac81SBjoern A. Zeeb 
16032ac8a218SBjoern A. Zeeb 	/*
16042ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
16052ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
16062ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
16072ac8a218SBjoern A. Zeeb 	 */
16082ac8a218SBjoern A. Zeeb 
16092ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
16102ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
16112ac8a218SBjoern A. Zeeb 		return (EINVAL);
16122ac8a218SBjoern A. Zeeb 	}
16130936c648SBjoern A. Zeeb 	/*
16140936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
16150936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
16160936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
16170936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
16180936c648SBjoern A. Zeeb 	 */
16192ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
16202ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
16212ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
16222ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
16230936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16242ac8a218SBjoern A. Zeeb 		return (EINVAL);
16256b4cac81SBjoern A. Zeeb 	}
16266b4cac81SBjoern A. Zeeb 
16276b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16282ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
16292ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
16302ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
16312ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
16320936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16332ac8a218SBjoern A. Zeeb 		return (ESRCH);
16342ac8a218SBjoern A. Zeeb 	}
16352ac8a218SBjoern A. Zeeb 
16366b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16376b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16386b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16396b4cac81SBjoern A. Zeeb 
16400936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
16410936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
16420936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
16430936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
16440936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
16450936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
16460936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
16470936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
164892690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
164992690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16500936c648SBjoern A. Zeeb 		return (EBUSY);
16510936c648SBjoern A. Zeeb 	}
16520936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
16530936c648SBjoern A. Zeeb 
16546b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
16558ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
16566b4cac81SBjoern A. Zeeb 
16576b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
16586b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1659d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1660d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
16616b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
16626b4cac81SBjoern A. Zeeb 	} else {
16636b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1664c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
16656b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1666d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
16676b4cac81SBjoern A. Zeeb 	}
16686b4cac81SBjoern A. Zeeb 
1669d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1670d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1671d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
16726b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1673d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1674d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1675d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = chan->center_freq;
1676d1af434dSBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = 0;
16779fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
16782ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
16792ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
16809fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
16819fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
16829fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1683d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
16849fb91463SBjoern A. Zeeb 		} else
1685d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
16869fb91463SBjoern A. Zeeb 	}
16879fb91463SBjoern A. Zeeb #endif
16889fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
16899fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
16909fb91463SBjoern A. Zeeb #ifdef __notyet__
16919fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1692d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1693d1af434dSBjoern A. Zeeb 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
16949fb91463SBjoern A. Zeeb 		} else
16959fb91463SBjoern A. Zeeb #endif
16969fb91463SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1697d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
16989fb91463SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1699d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
17009fb91463SBjoern A. Zeeb 	}
17019fb91463SBjoern A. Zeeb #endif
17026b4cac81SBjoern A. Zeeb 	/* Responder ... */
1703d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.chan = chan;
1704d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1705d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1706d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = 0;
17079fb91463SBjoern A. Zeeb 	IMPROVE("currently 20_NOHT min_def only");
17086b4cac81SBjoern A. Zeeb 
17096ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
17106ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
17116ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
17126ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
17136ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
17146ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
17156ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
17166ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
17176ffb7bd4SBjoern A. Zeeb 
17186ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
17196ffb7bd4SBjoern A. Zeeb 
17206ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
17216ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
17226ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
17236ffb7bd4SBjoern A. Zeeb 
17246ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
17256ffb7bd4SBjoern A. Zeeb 
17266b4cac81SBjoern A. Zeeb 	error = 0;
17276b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
17286b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
17296b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
17306b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
17316b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1732d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
17336b4cac81SBjoern A. Zeeb 	} else {
1734d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
17356b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
17367b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
17377b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
17387b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1739d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
17409fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
1741943a19c6SBjoern A. Zeeb 			if (vif->bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_40) {
17429fb91463SBjoern A. Zeeb 				/* Note: it is 10 not 20. */
17439fb91463SBjoern A. Zeeb 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1744943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 += 10;
17459fb91463SBjoern A. Zeeb 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1746943a19c6SBjoern A. Zeeb 					vif->bss_conf.chanreq.oper.center_freq1 -= 10;
17479fb91463SBjoern A. Zeeb 			}
17489fb91463SBjoern A. Zeeb #endif
17497b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1750d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
17516b4cac81SBjoern A. Zeeb 		} else {
1752018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1753018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
17546b4cac81SBjoern A. Zeeb 			goto out;
17556b4cac81SBjoern A. Zeeb 		}
17566ffb7bd4SBjoern A. Zeeb 
1757d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
17586ffb7bd4SBjoern A. Zeeb 
17596b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
17606b4cac81SBjoern A. Zeeb 		if (error == 0)
176168541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1762d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
17636b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
17646b4cac81SBjoern A. Zeeb 			error = 0;
17656b4cac81SBjoern A. Zeeb 		if (error != 0) {
1766018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1767018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1768d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1769d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1770c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
17716b4cac81SBjoern A. Zeeb 			goto out;
17726b4cac81SBjoern A. Zeeb 		}
17736b4cac81SBjoern A. Zeeb 	}
17746b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
17756b4cac81SBjoern A. Zeeb 
17766b4cac81SBjoern A. Zeeb 	/* RATES */
17776b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
17786b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
17796b4cac81SBjoern A. Zeeb 
1780d9f59799SBjoern A. Zeeb 	/*
17810936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
17820936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
17830936c648SBjoern A. Zeeb 	 * under the hoods.
1784d9f59799SBjoern A. Zeeb 	 */
17850936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
17860936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
17876b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1788e24e8103SBjoern A. Zeeb 
178988665349SBjoern A. Zeeb 	/*
179088665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
179188665349SBjoern A. Zeeb 	 * that we can tx again.
179288665349SBjoern A. Zeeb 	 */
179388665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
179488665349SBjoern A. Zeeb 	lsta->txq_ready = true;
179588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
179688665349SBjoern A. Zeeb 
1797a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
17982ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1799a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1800a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1801e24e8103SBjoern A. Zeeb 
1802d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
18036b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18046b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
18056b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1806e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
18076b4cac81SBjoern A. Zeeb 	if (error != 0) {
18086b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1809018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1810018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18116b4cac81SBjoern A. Zeeb 		goto out;
18126b4cac81SBjoern A. Zeeb 	}
18136b4cac81SBjoern A. Zeeb #if 0
18146b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
18156b4cac81SBjoern A. Zeeb #endif
18166b4cac81SBjoern A. Zeeb 
18179d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
18189d9ba2b7SBjoern A. Zeeb 
18191665ef97SBjoern A. Zeeb #if 0
18206b4cac81SBjoern A. Zeeb 	/*
18216b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
18226b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
18236b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
18246b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1825d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1826d9f59799SBjoern A. Zeeb 	 * for all queues.
18276b4cac81SBjoern A. Zeeb 	 */
1828e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
18291665ef97SBjoern A. Zeeb #endif
18306b4cac81SBjoern A. Zeeb 
18316b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
18326b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18336b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
18346b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
18356b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
18366b4cac81SBjoern A. Zeeb 
18376b4cac81SBjoern A. Zeeb 	/*
18386b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
18396b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
18406b4cac81SBjoern A. Zeeb 	 * - event_callback
18416b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
18426b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
18436b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
18446b4cac81SBjoern A. Zeeb 	 */
18456b4cac81SBjoern A. Zeeb 
1846105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1847105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1848105b9df2SBjoern A. Zeeb 
1849105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1850105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1851105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1852105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1853105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1854105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1855105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1856105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1857105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1858105b9df2SBjoern A. Zeeb 
1859105b9df2SBjoern A. Zeeb 	/*
1860105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1861105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1862105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1863105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1864105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1865105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1866105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1867105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1868105b9df2SBjoern A. Zeeb 	 */
1869105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1870105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1871105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1872105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1873105b9df2SBjoern A. Zeeb 	} else {
1874105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1875105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1876105b9df2SBjoern A. Zeeb 		/*
1877105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1878105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1879105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1880105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1881105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1882105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1883105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1884105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1885105b9df2SBjoern A. Zeeb 		 */
1886105b9df2SBjoern A. Zeeb 	}
1887105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1888105b9df2SBjoern A. Zeeb 	goto out_relocked;
1889105b9df2SBjoern A. Zeeb 
18906b4cac81SBjoern A. Zeeb out:
18918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
18926b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1893105b9df2SBjoern A. Zeeb out_relocked:
18940936c648SBjoern A. Zeeb 	/*
1895105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
18960936c648SBjoern A. Zeeb 	 * during the work of this function.
18970936c648SBjoern A. Zeeb 	 */
18986b4cac81SBjoern A. Zeeb 	if (ni != NULL)
18996b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
19006b4cac81SBjoern A. Zeeb 	return (error);
19016b4cac81SBjoern A. Zeeb }
19026b4cac81SBjoern A. Zeeb 
19036b4cac81SBjoern A. Zeeb static int
19046b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19056b4cac81SBjoern A. Zeeb {
19066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19076b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19086b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19096b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19106b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19116b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19126b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19136b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19146b4cac81SBjoern A. Zeeb 	int error;
19156b4cac81SBjoern A. Zeeb 
19166b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19176b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19186b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19196b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19206b4cac81SBjoern A. Zeeb 
19212ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19222ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19232ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
19242ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
19252ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19262ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19272ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19282ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19292ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19302ac8a218SBjoern A. Zeeb #endif
19312ac8a218SBjoern A. Zeeb 
19322ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19332ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19342ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19352ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19362ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19372ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
19386b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19396b4cac81SBjoern A. Zeeb 
194067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19416b4cac81SBjoern A. Zeeb 
19426b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
19438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
19446b4cac81SBjoern A. Zeeb 
1945d9f59799SBjoern A. Zeeb 	/* flush, drop. */
1946d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1947d9f59799SBjoern A. Zeeb 
19486b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
194988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
19506b4cac81SBjoern A. Zeeb 
19516b4cac81SBjoern A. Zeeb 	/* flush, no drop */
19526b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
19536b4cac81SBjoern A. Zeeb 
19546b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
19556b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
19566b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
19576b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
19586b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
19596b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
19606b4cac81SBjoern A. Zeeb 	}
19616b4cac81SBjoern A. Zeeb 
19626b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
19636b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
19646b4cac81SBjoern A. Zeeb 
19656b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
19666b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1967d9f59799SBjoern A. Zeeb 
1968d9f59799SBjoern A. Zeeb 	/* Take the station down. */
19696b4cac81SBjoern A. Zeeb 
19706b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
19716b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
19726b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1973d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1974e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
19756b4cac81SBjoern A. Zeeb 	if (error != 0) {
19766b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1977018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1978018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
19796b4cac81SBjoern A. Zeeb 		goto out;
19806b4cac81SBjoern A. Zeeb 	}
19816b4cac81SBjoern A. Zeeb #if 0
19826b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
19836b4cac81SBjoern A. Zeeb #endif
19846b4cac81SBjoern A. Zeeb 
198567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19866b4cac81SBjoern A. Zeeb 
19872ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19882ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
19892ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
19902ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
19912ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1992d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
19930936c648SBjoern A. Zeeb 	/*
19940936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
19950936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
19960936c648SBjoern A. Zeeb 	 * and potentially freed.
19970936c648SBjoern A. Zeeb 	 */
19980936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
1999d9f59799SBjoern A. Zeeb 
2000d9f59799SBjoern A. Zeeb 	/* conf_tx */
2001d9f59799SBjoern A. Zeeb 
2002d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
20036b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2004c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2005d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
20066b4cac81SBjoern A. Zeeb 
2007d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
20086b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
200968541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
20106b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
20116b4cac81SBjoern A. Zeeb 
20125a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
20135a4d2461SBjoern A. Zeeb 
20146b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
2015d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2016d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2017c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
20186b4cac81SBjoern A. Zeeb 	}
20196b4cac81SBjoern A. Zeeb 
20206b4cac81SBjoern A. Zeeb out:
20218ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20226b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
20236b4cac81SBjoern A. Zeeb 	return (error);
20246b4cac81SBjoern A. Zeeb }
20256b4cac81SBjoern A. Zeeb 
20266b4cac81SBjoern A. Zeeb static int
20276b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20286b4cac81SBjoern A. Zeeb {
20296b4cac81SBjoern A. Zeeb 	int error;
20306b4cac81SBjoern A. Zeeb 
20316b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
20326b4cac81SBjoern A. Zeeb 	if (error == 0)
20336b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
20346b4cac81SBjoern A. Zeeb 	return (error);
20356b4cac81SBjoern A. Zeeb }
20366b4cac81SBjoern A. Zeeb 
20376b4cac81SBjoern A. Zeeb static int
20386b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20396b4cac81SBjoern A. Zeeb {
20406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20416b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20426b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
20436b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
20446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
20456b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
20466b4cac81SBjoern A. Zeeb 	int error;
20476b4cac81SBjoern A. Zeeb 
20486b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
20496b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
20506b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
20516b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
20526b4cac81SBjoern A. Zeeb 
20536b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
20552ac8a218SBjoern A. Zeeb 
20562ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20572ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
20582ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
20592ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
20602ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
20612ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
20622ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
20632ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
20642ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
20652ac8a218SBjoern A. Zeeb #endif
20662ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
20672ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
20682ac8a218SBjoern A. Zeeb 		goto out;
20692ac8a218SBjoern A. Zeeb 	}
20702ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
20712ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
20722ac8a218SBjoern A. Zeeb 
20732ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
20746b4cac81SBjoern A. Zeeb 
20756b4cac81SBjoern A. Zeeb 	/* Finish auth. */
20766b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
20776b4cac81SBjoern A. Zeeb 
20786b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
20796b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
20806b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2081e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2082018d93ecSBjoern A. Zeeb 	if (error != 0) {
2083018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2084018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20856b4cac81SBjoern A. Zeeb 		goto out;
2086018d93ecSBjoern A. Zeeb 	}
20876b4cac81SBjoern A. Zeeb 
20886b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20896b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20906b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20916b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
20926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20936b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20946b4cac81SBjoern A. Zeeb 	}
20956b4cac81SBjoern A. Zeeb 
20966b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
20976b4cac81SBjoern A. Zeeb 
20986b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
20996b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21006b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21016b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21026b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21036b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21046b4cac81SBjoern A. Zeeb 	}
21056b4cac81SBjoern A. Zeeb 
21066b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
210788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
21086b4cac81SBjoern A. Zeeb 
21096b4cac81SBjoern A. Zeeb 	/*
21106b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
21116b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
21126b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
21136b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
21146b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
21156b4cac81SBjoern A. Zeeb 	 * - event_callback
21166b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
21176b4cac81SBjoern A. Zeeb 	 */
21186b4cac81SBjoern A. Zeeb 
21196b4cac81SBjoern A. Zeeb out:
21208ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21216b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21226b4cac81SBjoern A. Zeeb 	return (error);
21236b4cac81SBjoern A. Zeeb }
21246b4cac81SBjoern A. Zeeb 
21256b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
21266b4cac81SBjoern A. Zeeb static int
21276b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21286b4cac81SBjoern A. Zeeb {
21296b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21306b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21316b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21326b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21336b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21346b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21352ac8a218SBjoern A. Zeeb 	int error;
21366b4cac81SBjoern A. Zeeb 
21376b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21396b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21406b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21416b4cac81SBjoern A. Zeeb 
21426b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21442ac8a218SBjoern A. Zeeb 
21452ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21462ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21472ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21482ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21492ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21502ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21512ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21522ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21532ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21542ac8a218SBjoern A. Zeeb #endif
21552ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21562ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21572ac8a218SBjoern A. Zeeb 		goto out;
21582ac8a218SBjoern A. Zeeb 	}
21592ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21602ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21612ac8a218SBjoern A. Zeeb 
21622ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
21632ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
21646b4cac81SBjoern A. Zeeb 
21656b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
21666b4cac81SBjoern A. Zeeb 
21676b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21686b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21696b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21706b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
21716b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21726b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21736b4cac81SBjoern A. Zeeb 	}
21746b4cac81SBjoern A. Zeeb 
21756b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
21766b4cac81SBjoern A. Zeeb 
21776b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21786b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21796b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21806b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21816b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21826b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21836b4cac81SBjoern A. Zeeb 	}
21846b4cac81SBjoern A. Zeeb 
21852ac8a218SBjoern A. Zeeb 	error = 0;
21862ac8a218SBjoern A. Zeeb out:
21878ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21886b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21896b4cac81SBjoern A. Zeeb 
21902ac8a218SBjoern A. Zeeb 	return (error);
21916b4cac81SBjoern A. Zeeb }
21926b4cac81SBjoern A. Zeeb 
21936b4cac81SBjoern A. Zeeb static int
2194d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21956b4cac81SBjoern A. Zeeb {
21966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21976b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21986b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21996b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22006b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22026b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22036b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2204d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
22056b4cac81SBjoern A. Zeeb 	int error;
22066b4cac81SBjoern A. Zeeb 
22076b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22116b4cac81SBjoern A. Zeeb 
22122ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22132ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22142ac8a218SBjoern A. Zeeb 
22152ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22162ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22172ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22182ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22192ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22202ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22212ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22222ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22232ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22242ac8a218SBjoern A. Zeeb #endif
22252ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22262ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22272ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22282ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22292ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22302ac8a218SBjoern A. Zeeb 
22312ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
22326b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22336b4cac81SBjoern A. Zeeb 
223467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2235d9f59799SBjoern A. Zeeb 
2236d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2237d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2238d9f59799SBjoern A. Zeeb 
2239d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2240d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2241d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2242d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2243d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2244ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2245d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2246d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2247d9f59799SBjoern A. Zeeb 	}
2248d9f59799SBjoern A. Zeeb 
22498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2250d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2251d9f59799SBjoern A. Zeeb 
225288665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2253d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2254018d93ecSBjoern A. Zeeb 	if (error != 0) {
2255018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2256018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2257d9f59799SBjoern A. Zeeb 		goto outni;
2258018d93ecSBjoern A. Zeeb 	}
2259d9f59799SBjoern A. Zeeb 
2260d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
226188665349SBjoern A. Zeeb 
226288665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
226388665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
226488665349SBjoern A. Zeeb 
22658ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2266d9f59799SBjoern A. Zeeb 
226767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2268d9f59799SBjoern A. Zeeb 
2269d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
227088665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2271d9f59799SBjoern A. Zeeb 
2272d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2273d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2274d9f59799SBjoern A. Zeeb 
22756b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22766b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22776b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22786b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2279ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
22806b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22816b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22826b4cac81SBjoern A. Zeeb 	}
22836b4cac81SBjoern A. Zeeb 
2284d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2285d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2286d9f59799SBjoern A. Zeeb 
2287d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2288d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2289d9f59799SBjoern A. Zeeb 
2290d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2291d9f59799SBjoern A. Zeeb 
22926b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
22936b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
22946b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
22956b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2296e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2297018d93ecSBjoern A. Zeeb 	if (error != 0) {
2298018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2299018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23006b4cac81SBjoern A. Zeeb 		goto out;
2301018d93ecSBjoern A. Zeeb 	}
23026b4cac81SBjoern A. Zeeb 
23035a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
23045a4d2461SBjoern A. Zeeb 	bss_changed = 0;
23055a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
23066b4cac81SBjoern A. Zeeb 
23075a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
230816e688b2SBjoern A. Zeeb 
2309d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2310d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2311d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2312d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2313e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2314d9f59799SBjoern A. Zeeb 	if (error != 0) {
2315d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2316018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2317018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2318d9f59799SBjoern A. Zeeb 		goto out;
2319d9f59799SBjoern A. Zeeb 	}
2320d9f59799SBjoern A. Zeeb 
232116e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2322d9f59799SBjoern A. Zeeb 
2323d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2324d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2325d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2326616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2327616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2328d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2329d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2330d9f59799SBjoern A. Zeeb 
23312ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23322ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23332ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23342ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23352ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2336d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23370936c648SBjoern A. Zeeb 	/*
23380936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23390936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23400936c648SBjoern A. Zeeb 	 * and potentially freed.
23410936c648SBjoern A. Zeeb 	 */
23420936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2343d9f59799SBjoern A. Zeeb 
2344d9f59799SBjoern A. Zeeb 	/* conf_tx */
2345d9f59799SBjoern A. Zeeb 
2346d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2347d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2348c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2349d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2350d9f59799SBjoern A. Zeeb 
2351d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2352d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
235368541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2354d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2355d9f59799SBjoern A. Zeeb 
23565a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
23575a4d2461SBjoern A. Zeeb 
2358d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2359d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2360d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2361c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2362d9f59799SBjoern A. Zeeb 	}
2363d9f59799SBjoern A. Zeeb 
2364d9f59799SBjoern A. Zeeb 	error = EALREADY;
23656b4cac81SBjoern A. Zeeb out:
23668ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
23676b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2368d9f59799SBjoern A. Zeeb outni:
23696b4cac81SBjoern A. Zeeb 	return (error);
23706b4cac81SBjoern A. Zeeb }
23716b4cac81SBjoern A. Zeeb 
23726b4cac81SBjoern A. Zeeb static int
2373d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2374d9f59799SBjoern A. Zeeb {
2375d9f59799SBjoern A. Zeeb 	int error;
2376d9f59799SBjoern A. Zeeb 
2377d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2378d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2379d9f59799SBjoern A. Zeeb 		return (error);
2380d9f59799SBjoern A. Zeeb 
2381d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2382d9f59799SBjoern A. Zeeb 
2383d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2384d9f59799SBjoern A. Zeeb 	return (error);
2385d9f59799SBjoern A. Zeeb }
2386d9f59799SBjoern A. Zeeb 
2387d9f59799SBjoern A. Zeeb static int
23886b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23896b4cac81SBjoern A. Zeeb {
23906b4cac81SBjoern A. Zeeb 	int error;
23916b4cac81SBjoern A. Zeeb 
2392d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
23936b4cac81SBjoern A. Zeeb 	return (error);
23946b4cac81SBjoern A. Zeeb }
23956b4cac81SBjoern A. Zeeb 
23966b4cac81SBjoern A. Zeeb static int
23976b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
23986b4cac81SBjoern A. Zeeb {
23996b4cac81SBjoern A. Zeeb 	int error;
24006b4cac81SBjoern A. Zeeb 
2401d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24026b4cac81SBjoern A. Zeeb 	return (error);
24036b4cac81SBjoern A. Zeeb }
24046b4cac81SBjoern A. Zeeb 
24056b4cac81SBjoern A. Zeeb static int
24066b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24076b4cac81SBjoern A. Zeeb {
24086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24096b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24116b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24126b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
24136b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24146b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
24156b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24166b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
24176b4cac81SBjoern A. Zeeb 	int error;
24186b4cac81SBjoern A. Zeeb 
24196b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24206b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24216b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24226b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24236b4cac81SBjoern A. Zeeb 
24246b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
24258ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
24262ac8a218SBjoern A. Zeeb 
24272ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24282ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24292ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24302ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24312ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24322ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24332ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24342ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24352ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24362ac8a218SBjoern A. Zeeb #endif
24372ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24382ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24392ac8a218SBjoern A. Zeeb 		goto out;
24402ac8a218SBjoern A. Zeeb 	}
24412ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24422ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
24432ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
24442ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
24452ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
24462ac8a218SBjoern A. Zeeb 
24472ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
24486b4cac81SBjoern A. Zeeb 
24496b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
24506b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
24516b4cac81SBjoern A. Zeeb 
24529597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
24539597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
24549597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
24559597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
24566b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
24579597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
24584a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
24594a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
24604a67f1dfSBjoern A. Zeeb 		sta->wme = true;
24614a67f1dfSBjoern A. Zeeb #endif
2462e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2463018d93ecSBjoern A. Zeeb 	if (error != 0) {
2464018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2465018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
24669597f7cbSBjoern A. Zeeb 		goto out;
2467018d93ecSBjoern A. Zeeb 	}
24686b4cac81SBjoern A. Zeeb 
24696b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
24706b4cac81SBjoern A. Zeeb 
24716b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
24726b4cac81SBjoern A. Zeeb 	bss_changed = 0;
24734a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
24744a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
24754a67f1dfSBjoern A. Zeeb #endif
2476616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2477616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2478616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
24796b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
24806b4cac81SBjoern A. Zeeb 	}
24816b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2482616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2483616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
24846b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
24856b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
24866b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
24876b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
24886b4cac81SBjoern A. Zeeb 	}
24896b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
24906b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
24916b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
24926b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
24936b4cac81SBjoern A. Zeeb 	}
24946b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
24956b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
24966b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
24976b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
24986b4cac81SBjoern A. Zeeb 	}
2499fa8f007dSBjoern A. Zeeb 
2500fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2501fa8f007dSBjoern A. Zeeb 
25026b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
25036b4cac81SBjoern A. Zeeb 
25046b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
25056b4cac81SBjoern A. Zeeb 	 * - event_callback
25066b4cac81SBjoern A. Zeeb 	 */
25076b4cac81SBjoern A. Zeeb 
25086b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25096b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25106b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25116b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
25126b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25136b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25146b4cac81SBjoern A. Zeeb 	}
25156b4cac81SBjoern A. Zeeb 
2516086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2517086be6a8SBjoern A. Zeeb 
25186b4cac81SBjoern A. Zeeb 	/*
25196b4cac81SBjoern A. Zeeb 	 * And then:
25206b4cac81SBjoern A. Zeeb 	 * - (more packets)?
25216b4cac81SBjoern A. Zeeb 	 * - set_key
25226b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
25236b4cac81SBjoern A. Zeeb 	 * - set_key (?)
25246b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
25256b4cac81SBjoern A. Zeeb 	 */
25266b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
25276b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
25286b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
25296b4cac81SBjoern A. Zeeb 
25306b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
25316b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
25326b4cac81SBjoern A. Zeeb 	}
25336b4cac81SBjoern A. Zeeb 
25349fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
25359fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
25369fb91463SBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
25379fb91463SBjoern A. Zeeb #endif
25389fb91463SBjoern A. Zeeb 
25396b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
25406b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
25416b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
25426b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2543e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
25446b4cac81SBjoern A. Zeeb 	if (error != 0) {
25456b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2546018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2547018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25486b4cac81SBjoern A. Zeeb 		goto out;
25496b4cac81SBjoern A. Zeeb 	}
25506b4cac81SBjoern A. Zeeb 
25516b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
25526b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
25536b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
25546b4cac81SBjoern A. Zeeb 	 *
25556b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
25566b4cac81SBjoern A. Zeeb 	 */
25576b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
25586b4cac81SBjoern A. Zeeb 
2559fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2560fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2561fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2562fa8f007dSBjoern A. Zeeb 
25636b4cac81SBjoern A. Zeeb out:
25648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
25656b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
25666b4cac81SBjoern A. Zeeb 	return (error);
25676b4cac81SBjoern A. Zeeb }
25686b4cac81SBjoern A. Zeeb 
25696b4cac81SBjoern A. Zeeb static int
25706b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25716b4cac81SBjoern A. Zeeb {
25726b4cac81SBjoern A. Zeeb 	int error;
25736b4cac81SBjoern A. Zeeb 
25746b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
25756b4cac81SBjoern A. Zeeb 	if (error == 0)
25766b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
25776b4cac81SBjoern A. Zeeb 	return (error);
25786b4cac81SBjoern A. Zeeb }
25796b4cac81SBjoern A. Zeeb 
25806b4cac81SBjoern A. Zeeb static int
25816b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
25826b4cac81SBjoern A. Zeeb {
25836b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
25846b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
25856b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
25866b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
25876b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
25886b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
25896b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2590d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2591d9f59799SBjoern A. Zeeb #if 0
2592d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2593d9f59799SBjoern A. Zeeb #endif
25946b4cac81SBjoern A. Zeeb 	int error;
25956b4cac81SBjoern A. Zeeb 
25966b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
25976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
25986b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
25996b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26006b4cac81SBjoern A. Zeeb 
26012ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26022ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26032ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26042ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26052ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26062ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26072ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26082ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26092ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
26102ac8a218SBjoern A. Zeeb #endif
26112ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26122ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26132ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26142ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26152ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
26162ac8a218SBjoern A. Zeeb 
26172ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
26186b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
26196b4cac81SBjoern A. Zeeb 
262067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2621d9f59799SBjoern A. Zeeb 
2622d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
26238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2624d9f59799SBjoern A. Zeeb 
2625d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2626d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2627d9f59799SBjoern A. Zeeb 
2628d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2629d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2630d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2631d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2632d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2633ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2634d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2635d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2636d9f59799SBjoern A. Zeeb 	}
2637d9f59799SBjoern A. Zeeb 
26388ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2639d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2640d9f59799SBjoern A. Zeeb 
2641d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2642d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2643018d93ecSBjoern A. Zeeb 	if (error != 0) {
2644018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2645018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2646d9f59799SBjoern A. Zeeb 		goto outni;
2647018d93ecSBjoern A. Zeeb 	}
2648d9f59799SBjoern A. Zeeb 
2649d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
265088665349SBjoern A. Zeeb 
265188665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
265288665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
265388665349SBjoern A. Zeeb 
26548ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2655d9f59799SBjoern A. Zeeb 
265667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2657d9f59799SBjoern A. Zeeb 
2658d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
265988665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2660d9f59799SBjoern A. Zeeb 
2661d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2662d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2663d9f59799SBjoern A. Zeeb 
2664d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2665d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2666d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2667d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2668ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2669d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2670d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2671d9f59799SBjoern A. Zeeb 	}
2672d9f59799SBjoern A. Zeeb 
2673d9f59799SBjoern A. Zeeb #if 0
2674d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2675d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2676d9f59799SBjoern A. Zeeb 
2677d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2678d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2679d9f59799SBjoern A. Zeeb #endif
2680d9f59799SBjoern A. Zeeb 
2681d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2682d9f59799SBjoern A. Zeeb 
26836b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
26846b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
26856b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
26866b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2687e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2688018d93ecSBjoern A. Zeeb 	if (error != 0) {
2689018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2690018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26916b4cac81SBjoern A. Zeeb 		goto out;
2692018d93ecSBjoern A. Zeeb 	}
26936b4cac81SBjoern A. Zeeb 
269467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
26956b4cac81SBjoern A. Zeeb 
269611db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
269711db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
269811db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
269911db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
270011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
270111db70b6SBjoern A. Zeeb 		if (error != 0) {
270211db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
270311db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
270411db70b6SBjoern A. Zeeb 			/*
270511db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
270611db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
270711db70b6SBjoern A. Zeeb 			 * less appropriate time).
270811db70b6SBjoern A. Zeeb 			 */
270911db70b6SBjoern A. Zeeb 			/* goto out; */
271011db70b6SBjoern A. Zeeb 		}
271111db70b6SBjoern A. Zeeb 	}
271211db70b6SBjoern A. Zeeb #endif
271311db70b6SBjoern A. Zeeb 
27146b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
27156b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27166b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
27176b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2718e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2719018d93ecSBjoern A. Zeeb 	if (error != 0) {
2720018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2721018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27226b4cac81SBjoern A. Zeeb 		goto out;
2723018d93ecSBjoern A. Zeeb 	}
27246b4cac81SBjoern A. Zeeb 
272567674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27266b4cac81SBjoern A. Zeeb 
2727d9f59799SBjoern A. Zeeb #if 0
2728d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2729d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2730d9f59799SBjoern A. Zeeb #endif
2731d9f59799SBjoern A. Zeeb 
2732d9f59799SBjoern A. Zeeb 	error = EALREADY;
27336b4cac81SBjoern A. Zeeb out:
27348ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27356b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2736d9f59799SBjoern A. Zeeb outni:
27376b4cac81SBjoern A. Zeeb 	return (error);
27386b4cac81SBjoern A. Zeeb }
27396b4cac81SBjoern A. Zeeb 
27406b4cac81SBjoern A. Zeeb static int
2741d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2742d9f59799SBjoern A. Zeeb {
2743d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2744d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2745d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2746d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2747d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2748d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2749d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2750d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2751d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2752d9f59799SBjoern A. Zeeb 	int error;
2753d9f59799SBjoern A. Zeeb 
2754d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2755d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2756d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2757d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2758d9f59799SBjoern A. Zeeb 
27592ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
27602ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
27612ac8a218SBjoern A. Zeeb 
27622ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
27632ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
27642ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
27652ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
27662ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
27672ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
27682ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
27692ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
27702ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
27712ac8a218SBjoern A. Zeeb #endif
27722ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
27732ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
27742ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
27752ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
27762ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
27772ac8a218SBjoern A. Zeeb 
27782ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2779d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2780d9f59799SBjoern A. Zeeb 
278167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2782d9f59799SBjoern A. Zeeb 
2783d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2784d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2785d9f59799SBjoern A. Zeeb 
2786d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2787d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2788d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2789d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2790d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2791ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2792d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2793d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2794d9f59799SBjoern A. Zeeb 	}
2795d9f59799SBjoern A. Zeeb 
27968ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2797d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2798d9f59799SBjoern A. Zeeb 
2799d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2800d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2801018d93ecSBjoern A. Zeeb 	if (error != 0) {
2802018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2803018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2804d9f59799SBjoern A. Zeeb 		goto outni;
2805018d93ecSBjoern A. Zeeb 	}
2806d9f59799SBjoern A. Zeeb 
2807d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
280888665349SBjoern A. Zeeb 
280988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
281088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
281188665349SBjoern A. Zeeb 
28128ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2813d9f59799SBjoern A. Zeeb 
281467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2815d9f59799SBjoern A. Zeeb 
2816d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
281788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2818d9f59799SBjoern A. Zeeb 
2819d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2820d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2821d9f59799SBjoern A. Zeeb 
2822d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2823d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2824d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2825d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2826ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2827d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2828d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2829d9f59799SBjoern A. Zeeb 	}
2830d9f59799SBjoern A. Zeeb 
2831d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2832d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2833d9f59799SBjoern A. Zeeb 
2834d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2835d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2836d9f59799SBjoern A. Zeeb 
2837d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2838d9f59799SBjoern A. Zeeb 
2839d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2840d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2841d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2842d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2843e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2844018d93ecSBjoern A. Zeeb 	if (error != 0) {
2845018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2846018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2847d9f59799SBjoern A. Zeeb 		goto out;
2848018d93ecSBjoern A. Zeeb 	}
2849d9f59799SBjoern A. Zeeb 
285067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2851d9f59799SBjoern A. Zeeb 
285211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
285311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
285411db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
285511db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
285611db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
285711db70b6SBjoern A. Zeeb 		if (error != 0) {
285811db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
285911db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
286011db70b6SBjoern A. Zeeb 			/*
286111db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
286211db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
286311db70b6SBjoern A. Zeeb 			 * less appropriate time).
286411db70b6SBjoern A. Zeeb 			 */
286511db70b6SBjoern A. Zeeb 			/* goto out; */
286611db70b6SBjoern A. Zeeb 		}
286711db70b6SBjoern A. Zeeb 	}
286811db70b6SBjoern A. Zeeb #endif
286911db70b6SBjoern A. Zeeb 
2870d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2871d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2872d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2873d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2874e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2875018d93ecSBjoern A. Zeeb 	if (error != 0) {
2876018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2877018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2878d9f59799SBjoern A. Zeeb 		goto out;
2879018d93ecSBjoern A. Zeeb 	}
2880d9f59799SBjoern A. Zeeb 
288167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2882d9f59799SBjoern A. Zeeb 
2883d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2884d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2885d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2886d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2887e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2888018d93ecSBjoern A. Zeeb 	if (error != 0) {
2889018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2890018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2891d9f59799SBjoern A. Zeeb 		goto out;
2892018d93ecSBjoern A. Zeeb 	}
2893d9f59799SBjoern A. Zeeb 
28945a4d2461SBjoern A. Zeeb 	bss_changed = 0;
289516e688b2SBjoern A. Zeeb 	/*
28965a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
28975a4d2461SBjoern A. Zeeb 	 *
289816e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
28995a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
29005a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
29015a4d2461SBjoern A. Zeeb 	 *
29025a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
29035a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
29045a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
29055a4d2461SBjoern A. Zeeb 	 * a fw assert.
29065a4d2461SBjoern A. Zeeb 	 *
29075a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
29085a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
29095a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
29105a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
29115a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
29125a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
29135a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
29145a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
29155a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
29165a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
291716e688b2SBjoern A. Zeeb 	 */
29185a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
29195a4d2461SBjoern A. Zeeb 
29205a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
292116e688b2SBjoern A. Zeeb 
2922d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2923d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2924d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2925d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2926e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2927d9f59799SBjoern A. Zeeb 	if (error != 0) {
2928d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2929018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2930018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2931d9f59799SBjoern A. Zeeb 		goto out;
2932d9f59799SBjoern A. Zeeb 	}
2933d9f59799SBjoern A. Zeeb 
29345a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
29355a4d2461SBjoern A. Zeeb 
293616e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2937d9f59799SBjoern A. Zeeb 
2938d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2939d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2940d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2941616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2942616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2943d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
29445a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
29455a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
29465a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
2947d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2948d9f59799SBjoern A. Zeeb 
29492ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
29502ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
29512ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
29522ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
29532ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
29540936c648SBjoern A. Zeeb 	/*
29550936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
29560936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
29570936c648SBjoern A. Zeeb 	 * and potentially freed.
29580936c648SBjoern A. Zeeb 	 */
29590936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2960d9f59799SBjoern A. Zeeb 
2961d9f59799SBjoern A. Zeeb 	/* conf_tx */
2962d9f59799SBjoern A. Zeeb 
2963d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2964d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2965c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2966d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2967d9f59799SBjoern A. Zeeb 
2968d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2969d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
297068541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2971d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2972d9f59799SBjoern A. Zeeb 
29735a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
29745a4d2461SBjoern A. Zeeb 
2975d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2976d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2977d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2978c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2979d9f59799SBjoern A. Zeeb 	}
2980d9f59799SBjoern A. Zeeb 
2981d9f59799SBjoern A. Zeeb 	error = EALREADY;
2982d9f59799SBjoern A. Zeeb out:
29838ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2984d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2985d9f59799SBjoern A. Zeeb outni:
2986d9f59799SBjoern A. Zeeb 	return (error);
2987d9f59799SBjoern A. Zeeb }
2988d9f59799SBjoern A. Zeeb 
2989d9f59799SBjoern A. Zeeb static int
2990d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2991d9f59799SBjoern A. Zeeb {
2992d9f59799SBjoern A. Zeeb 
2993d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2994d9f59799SBjoern A. Zeeb }
2995d9f59799SBjoern A. Zeeb 
2996d9f59799SBjoern A. Zeeb static int
29976b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
29986b4cac81SBjoern A. Zeeb {
29996b4cac81SBjoern A. Zeeb 	int error;
30006b4cac81SBjoern A. Zeeb 
3001d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3002d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3003d9f59799SBjoern A. Zeeb 		return (error);
3004d9f59799SBjoern A. Zeeb 
3005d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3006d9f59799SBjoern A. Zeeb 
3007d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
30086b4cac81SBjoern A. Zeeb 	return (error);
30096b4cac81SBjoern A. Zeeb }
30106b4cac81SBjoern A. Zeeb 
3011d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
30126b4cac81SBjoern A. Zeeb 
30136b4cac81SBjoern A. Zeeb /*
30146b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
30156b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
30166b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
30176b4cac81SBjoern A. Zeeb  */
30186b4cac81SBjoern A. Zeeb struct fsm_state {
30196b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
30206b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
30216b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
30226b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
30236b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
30246b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
30256b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
30266b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3027d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3028d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
30296b4cac81SBjoern A. Zeeb 
30306b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30316b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30326b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
30336b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3034d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
30356b4cac81SBjoern A. Zeeb 
3036d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3037d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3038d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3039d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3040d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
30416b4cac81SBjoern A. Zeeb 
3042d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3043d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3044d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
30456b4cac81SBjoern A. Zeeb 
30466b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
30476b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
30486b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
30496b4cac81SBjoern A. Zeeb 
30506b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
30516b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
30526b4cac81SBjoern A. Zeeb };
30536b4cac81SBjoern A. Zeeb 
30546b4cac81SBjoern A. Zeeb static int
30556b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
30566b4cac81SBjoern A. Zeeb {
30576b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
30586b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
30596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
30606b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
30616b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
30626b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
30636b4cac81SBjoern A. Zeeb 	int error;
30646b4cac81SBjoern A. Zeeb 
30656b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
30666b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
30676b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
30686b4cac81SBjoern A. Zeeb 
30699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
30716b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
30726b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
30739d9ba2b7SBjoern A. Zeeb #endif
30746b4cac81SBjoern A. Zeeb 
30756b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
30766b4cac81SBjoern A. Zeeb 
30776b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
30786b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
30796b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
30806b4cac81SBjoern A. Zeeb 
30816b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
30826b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
30836b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
30846b4cac81SBjoern A. Zeeb 
30856b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
30866b4cac81SBjoern A. Zeeb 
30876b4cac81SBjoern A. Zeeb 	} else {
30886b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
30896b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
30906b4cac81SBjoern A. Zeeb 		return (ENOSYS);
30916b4cac81SBjoern A. Zeeb 	}
30926b4cac81SBjoern A. Zeeb 
30936b4cac81SBjoern A. Zeeb 	error = 0;
30946b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
30956b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
30969d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
30979d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3098d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3099d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3100d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3101d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
31029d9ba2b7SBjoern A. Zeeb #endif
31036b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
31046b4cac81SBjoern A. Zeeb 			break;
31056b4cac81SBjoern A. Zeeb 		}
31066b4cac81SBjoern A. Zeeb 	}
31076b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
31086b4cac81SBjoern A. Zeeb 
31096b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3110d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
31116b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
31126b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
31136b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31146b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
31156b4cac81SBjoern A. Zeeb 		return (ENOSYS);
31166b4cac81SBjoern A. Zeeb 	}
31176b4cac81SBjoern A. Zeeb 
31186b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
31199d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31209d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3121d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3122d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3123d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3124d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
31259d9ba2b7SBjoern A. Zeeb #endif
31266b4cac81SBjoern A. Zeeb 		return (0);
31276b4cac81SBjoern A. Zeeb 	}
31286b4cac81SBjoern A. Zeeb 
31296b4cac81SBjoern A. Zeeb 	if (error != 0) {
31306b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
31316b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
31326b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31336b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
313445c27ad5SBjoern A. Zeeb 		return (error);
31356b4cac81SBjoern A. Zeeb 	}
31366b4cac81SBjoern A. Zeeb 
31379d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31389d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3139d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3140d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
31416b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31429d9ba2b7SBjoern A. Zeeb #endif
31436b4cac81SBjoern A. Zeeb 
31446b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
31456b4cac81SBjoern A. Zeeb }
31466b4cac81SBjoern A. Zeeb 
31476b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
31486b4cac81SBjoern A. Zeeb 
3149d9f59799SBjoern A. Zeeb /*
3150d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3151d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3152d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3153d9f59799SBjoern A. Zeeb  */
3154d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3155d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3156d9f59799SBjoern A. Zeeb {
3157d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31582ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3159d9f59799SBjoern A. Zeeb 
31602ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3161d9f59799SBjoern A. Zeeb 
3162ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
31632ac8a218SBjoern A. Zeeb 
31642ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
31652ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
31662ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
31672ac8a218SBjoern A. Zeeb 
31682ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
31692ac8a218SBjoern A. Zeeb 	return (rni);
3170d9f59799SBjoern A. Zeeb }
3171d9f59799SBjoern A. Zeeb 
31724a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
31736b4cac81SBjoern A. Zeeb static int
31744a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
31756b4cac81SBjoern A. Zeeb {
31764a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
31776b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
31786b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31796b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31806b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
31816b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
31826b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
31836b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
31846b4cac81SBjoern A. Zeeb 	int error;
31856b4cac81SBjoern A. Zeeb 	uint16_t ac;
31866b4cac81SBjoern A. Zeeb 
31876b4cac81SBjoern A. Zeeb 	IMPROVE();
31886b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
31896b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
31906b4cac81SBjoern A. Zeeb 
31916b4cac81SBjoern A. Zeeb 	if (vap == NULL)
31926b4cac81SBjoern A. Zeeb 		return (0);
31936b4cac81SBjoern A. Zeeb 
31944a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
31954a67f1dfSBjoern A. Zeeb 		return (0);
31964a67f1dfSBjoern A. Zeeb 
31976b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
31986b4cac81SBjoern A. Zeeb 		return (0);
31996b4cac81SBjoern A. Zeeb 
32004a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
32014a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
32024a67f1dfSBjoern A. Zeeb 		return (0);
32034a67f1dfSBjoern A. Zeeb 	}
32044a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
32056b4cac81SBjoern A. Zeeb 
32064a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
32076b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
32086b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
32096b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
32106b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
32116b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
32126b4cac81SBjoern A. Zeeb 
32134a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32144a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32154a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
32164a67f1dfSBjoern A. Zeeb 
32176b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
32186b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
32196b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
32206b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
32216b4cac81SBjoern A. Zeeb 
32226b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
32236b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
32246b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
32256b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
32266b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
32276b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
322868541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
32296b4cac81SBjoern A. Zeeb 		if (error != 0)
32303f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
32316b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
32326b4cac81SBjoern A. Zeeb 	}
32336b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
32346b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
32354a67f1dfSBjoern A. Zeeb 	if (!planned)
32366b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
32374a67f1dfSBjoern A. Zeeb 
32384a67f1dfSBjoern A. Zeeb 	return (changed);
32394a67f1dfSBjoern A. Zeeb }
32406b4cac81SBjoern A. Zeeb #endif
32416b4cac81SBjoern A. Zeeb 
32424a67f1dfSBjoern A. Zeeb static int
32434a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
32444a67f1dfSBjoern A. Zeeb {
32454a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
32464a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
32474a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
32484a67f1dfSBjoern A. Zeeb 
32494a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
32504a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
32514a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
32526b4cac81SBjoern A. Zeeb 		return (0);
32534a67f1dfSBjoern A. Zeeb 
32544a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
32554a67f1dfSBjoern A. Zeeb 
32564a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
32574a67f1dfSBjoern A. Zeeb #endif
32584a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
32596b4cac81SBjoern A. Zeeb }
32606b4cac81SBjoern A. Zeeb 
32614aff4048SBjoern A. Zeeb /*
32624aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
32634aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
32644aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
32654aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
32664aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
32674aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3268edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3269edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3270edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3271edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
32724aff4048SBjoern A. Zeeb  */
32734aff4048SBjoern A. Zeeb static void
32744aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
32754aff4048SBjoern A. Zeeb {
32764aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
32774aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32784aff4048SBjoern A. Zeeb 
32794aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3280edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3281edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3282edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
32834aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
32844aff4048SBjoern A. Zeeb 		return;
32854aff4048SBjoern A. Zeeb 	}
32864aff4048SBjoern A. Zeeb 
32874aff4048SBjoern A. Zeeb 	vif = arg;
328857609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
32894aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
32904aff4048SBjoern A. Zeeb }
32914aff4048SBjoern A. Zeeb 
32926b4cac81SBjoern A. Zeeb static struct ieee80211vap *
32936b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
32946b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
32956b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
32966b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
32976b4cac81SBjoern A. Zeeb {
32986b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
32996b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33006b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33016b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
33026b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3303a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
33046b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
330540839418SBjoern A. Zeeb 	struct sysctl_oid *node;
33066b4cac81SBjoern A. Zeeb 	size_t len;
3307e3a0b120SBjoern A. Zeeb 	int error, i;
3308a6042e17SBjoern A. Zeeb 	uint16_t ac;
33096b4cac81SBjoern A. Zeeb 
33106b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
33116b4cac81SBjoern A. Zeeb 		return (NULL);
33126b4cac81SBjoern A. Zeeb 
33136b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33146b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33156b4cac81SBjoern A. Zeeb 
33166b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
33176b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
33186b4cac81SBjoern A. Zeeb 
33196b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
33206b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3321a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
33222ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
33232ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33246b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
33256b4cac81SBjoern A. Zeeb 
33266b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33276b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
33286b4cac81SBjoern A. Zeeb 	vif->p2p = false;
33296b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
33306b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
33316b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
33326b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
33336b4cac81SBjoern A. Zeeb 	IMPROVE();
33346b4cac81SBjoern A. Zeeb 
33356b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
33366b4cac81SBjoern A. Zeeb #if 1
33376b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3338adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
33396ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
33406ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
33414aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
33424aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
33436ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
33447b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
33456b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
33466b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
33476b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
33486b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
33496b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3350616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3351616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3352616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3353616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
33546ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3355caaa79c3SBjoern A. Zeeb 	/*
3356caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3357caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3358caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3359caaa79c3SBjoern A. Zeeb 	 */
33606ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
33616b4cac81SBjoern A. Zeeb #endif
33626b4cac81SBjoern A. Zeeb #if 0
33636b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
33646b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
33656b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
33666b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
33676b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
33686b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
33696b4cac81SBjoern A. Zeeb #endif
3370e3a0b120SBjoern A. Zeeb 
33716ffb7bd4SBjoern A. Zeeb 	/* Link Config */
33726ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
33736ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
33746ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
33756ffb7bd4SBjoern A. Zeeb 	}
33766ffb7bd4SBjoern A. Zeeb 
3377e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3378e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3379e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3380e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3381e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3382e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3383e3a0b120SBjoern A. Zeeb 		else
3384e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
33850cbcfa19SBjoern A. Zeeb 
33860cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
33870cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3388e3a0b120SBjoern A. Zeeb 	}
3389e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3390e3a0b120SBjoern A. Zeeb 
33916b4cac81SBjoern A. Zeeb 	IMPROVE();
33926b4cac81SBjoern A. Zeeb 
33936b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
33946b4cac81SBjoern A. Zeeb 	if (error != 0) {
33953f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
33966b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
33976b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
33986b4cac81SBjoern A. Zeeb 		return (NULL);
33996b4cac81SBjoern A. Zeeb 	}
34006b4cac81SBjoern A. Zeeb 
34016b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
34026b4cac81SBjoern A. Zeeb 	if (error != 0) {
34036b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
34043f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
34056b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34066b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
34076b4cac81SBjoern A. Zeeb 		return (NULL);
34086b4cac81SBjoern A. Zeeb 	}
34096b4cac81SBjoern A. Zeeb 
34108891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
34116b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
34128891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
34136b4cac81SBjoern A. Zeeb 
34146b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
34156b4cac81SBjoern A. Zeeb 	changed = 0;
34166b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34176b4cac81SBjoern A. Zeeb 
3418a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3419a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3420a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3421a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3422a6042e17SBjoern A. Zeeb 
3423a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3424a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3425a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3426a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3427a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3428a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3429a6042e17SBjoern A. Zeeb 		if (error != 0)
3430a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3431a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3432a6042e17SBjoern A. Zeeb 	}
3433a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3434a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3435a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34366b4cac81SBjoern A. Zeeb 
34376b4cac81SBjoern A. Zeeb 	/* Force MC init. */
34386b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
34396b4cac81SBjoern A. Zeeb 
34406b4cac81SBjoern A. Zeeb 	IMPROVE();
34416b4cac81SBjoern A. Zeeb 
34426b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
34436b4cac81SBjoern A. Zeeb 
34446b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
34456b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
34466b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3447d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3448d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
34496b4cac81SBjoern A. Zeeb 
3450b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
345111db70b6SBjoern A. Zeeb 	/* Key management. */
345211db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
34536b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
34546b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
34556b4cac81SBjoern A. Zeeb 	}
345611db70b6SBjoern A. Zeeb #endif
34576b4cac81SBjoern A. Zeeb 
34589fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
34599fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
34609fb91463SBjoern A. Zeeb #endif
34619fb91463SBjoern A. Zeeb 
34626b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
34636b4cac81SBjoern A. Zeeb 
34646b4cac81SBjoern A. Zeeb 	/* Complete setup. */
34656b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
34666b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
34676b4cac81SBjoern A. Zeeb 
346811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
346911db70b6SBjoern A. Zeeb 	/*
347011db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
347111db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
347211db70b6SBjoern A. Zeeb 	 */
347311db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
347411db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
347511db70b6SBjoern A. Zeeb #endif
347611db70b6SBjoern A. Zeeb 
34776b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
34786b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
34796b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
34806b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
34816b4cac81SBjoern A. Zeeb 
34826b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
34836b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
34846b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
34856b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
34866b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
34876b4cac81SBjoern A. Zeeb 	/* any others? */
348840839418SBjoern A. Zeeb 
348940839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
349040839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
349140839418SBjoern A. Zeeb 
349240839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
349340839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
349440839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
349540839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
349640839418SBjoern A. Zeeb 
349740839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
349840839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
349940839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
350040839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
350140839418SBjoern A. Zeeb 
35026b4cac81SBjoern A. Zeeb 	IMPROVE();
35036b4cac81SBjoern A. Zeeb 
35046b4cac81SBjoern A. Zeeb 	return (vap);
35056b4cac81SBjoern A. Zeeb }
35066b4cac81SBjoern A. Zeeb 
35074b0af114SBjoern A. Zeeb void
35084b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
35094b0af114SBjoern A. Zeeb {
35104b0af114SBjoern A. Zeeb 
35114b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
35124b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
35134b0af114SBjoern A. Zeeb 
35144b0af114SBjoern A. Zeeb 	IMPROVE();
35154b0af114SBjoern A. Zeeb }
35164b0af114SBjoern A. Zeeb 
35174b0af114SBjoern A. Zeeb void
35184b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
35194b0af114SBjoern A. Zeeb {
35204b0af114SBjoern A. Zeeb 
35214b0af114SBjoern A. Zeeb 	TODO();
35224b0af114SBjoern A. Zeeb }
35234b0af114SBjoern A. Zeeb 
35246b4cac81SBjoern A. Zeeb static void
35256b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
35266b4cac81SBjoern A. Zeeb {
35276b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35286b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35296b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35306b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35316b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35326b4cac81SBjoern A. Zeeb 
35336b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35346b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35356b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
35366b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35376b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35386b4cac81SBjoern A. Zeeb 
35394aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
35404aff4048SBjoern A. Zeeb 
354140839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
354240839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
354340839418SBjoern A. Zeeb 
35448891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
35456b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
35468891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
35476b4cac81SBjoern A. Zeeb 
35486b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
35496b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3550dbf76919SBjoern A. Zeeb 
3551dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3552dbf76919SBjoern A. Zeeb 
3553dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3554dbf76919SBjoern A. Zeeb 
35556c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
35567b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
35576c38c6b1SBjoern A. Zeeb 
35586b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
35596b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
35606b4cac81SBjoern A. Zeeb }
35616b4cac81SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb static void
35636b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
35646b4cac81SBjoern A. Zeeb {
35656b4cac81SBjoern A. Zeeb 
35666b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
35676b4cac81SBjoern A. Zeeb 	TRACEOK();
35686b4cac81SBjoern A. Zeeb }
35696b4cac81SBjoern A. Zeeb 
35706b4cac81SBjoern A. Zeeb static void
35716b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
35726b4cac81SBjoern A. Zeeb {
35736b4cac81SBjoern A. Zeeb 
35746b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35756b4cac81SBjoern A. Zeeb }
35766b4cac81SBjoern A. Zeeb 
35776b4cac81SBjoern A. Zeeb static void
35786b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
35796b4cac81SBjoern A. Zeeb {
35806b4cac81SBjoern A. Zeeb 
35816b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
35826b4cac81SBjoern A. Zeeb }
35836b4cac81SBjoern A. Zeeb 
35846b4cac81SBjoern A. Zeeb /* Start / stop device. */
35856b4cac81SBjoern A. Zeeb static void
35866b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
35876b4cac81SBjoern A. Zeeb {
35886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35898d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35906b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35916b4cac81SBjoern A. Zeeb 	int error;
35928d58a057SBjoern A. Zeeb #endif
35936b4cac81SBjoern A. Zeeb 	bool start_all;
35946b4cac81SBjoern A. Zeeb 
35956b4cac81SBjoern A. Zeeb 	IMPROVE();
35966b4cac81SBjoern A. Zeeb 
35976b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35988d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
35996b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36008d58a057SBjoern A. Zeeb #endif
36016b4cac81SBjoern A. Zeeb 	start_all = false;
36026b4cac81SBjoern A. Zeeb 
36038ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
36048ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
36056b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
36068d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36076b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
36086b4cac81SBjoern A. Zeeb 		if (error == 0)
36098d58a057SBjoern A. Zeeb #endif
36106b4cac81SBjoern A. Zeeb 			start_all = true;
36116b4cac81SBjoern A. Zeeb 	} else {
36128d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36137b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
36148d58a057SBjoern A. Zeeb #endif
36156b4cac81SBjoern A. Zeeb 	}
36168ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
36178ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
36186b4cac81SBjoern A. Zeeb 
36196b4cac81SBjoern A. Zeeb 	if (start_all)
36206b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
36216b4cac81SBjoern A. Zeeb }
36226b4cac81SBjoern A. Zeeb 
36236b4cac81SBjoern A. Zeeb bool
36246b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
36256b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
36266b4cac81SBjoern A. Zeeb {
36276b4cac81SBjoern A. Zeeb 	int i;
36286b4cac81SBjoern A. Zeeb 
36296b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
36306b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
36316b4cac81SBjoern A. Zeeb 			return (true);
36326b4cac81SBjoern A. Zeeb 	}
36336b4cac81SBjoern A. Zeeb 
36346b4cac81SBjoern A. Zeeb 	return (false);
36356b4cac81SBjoern A. Zeeb }
36366b4cac81SBjoern A. Zeeb 
36376b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
36386b4cac81SBjoern A. Zeeb bool
36396b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
36406b4cac81SBjoern A. Zeeb {
36416b4cac81SBjoern A. Zeeb 	size_t x;
36426b4cac81SBjoern A. Zeeb 	uint8_t l;
36436b4cac81SBjoern A. Zeeb 
36446b4cac81SBjoern A. Zeeb 	x = *xp;
36456b4cac81SBjoern A. Zeeb 
36466b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
36476b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
36486b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
36496b4cac81SBjoern A. Zeeb 	x += 2 + l;
36506b4cac81SBjoern A. Zeeb 
36516b4cac81SBjoern A. Zeeb 	if (x > ies_len)
36526b4cac81SBjoern A. Zeeb 		return (false);
36536b4cac81SBjoern A. Zeeb 
36546b4cac81SBjoern A. Zeeb 	*xp = x;
36556b4cac81SBjoern A. Zeeb 	return (true);
36566b4cac81SBjoern A. Zeeb }
36576b4cac81SBjoern A. Zeeb 
3658d9945d78SBjoern A. Zeeb static uint8_t *
3659d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3660d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
36616b4cac81SBjoern A. Zeeb {
3662d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3663d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
36643dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3665d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3666d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3667d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3668d9945d78SBjoern A. Zeeb 	int band, i;
36696b4cac81SBjoern A. Zeeb 
36703dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3671d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3672d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3673d9945d78SBjoern A. Zeeb 			continue;
3674d9945d78SBjoern A. Zeeb 
3675d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3676d9945d78SBjoern A. Zeeb 		/*
3677d9945d78SBjoern A. Zeeb 		 * This should not happen;
3678d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3679d9945d78SBjoern A. Zeeb 		 */
3680d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3681d9945d78SBjoern A. Zeeb 			continue;
3682d9945d78SBjoern A. Zeeb 
3683d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3684d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3685d9945d78SBjoern A. Zeeb 		chan = NULL;
3686d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3687d9945d78SBjoern A. Zeeb 
3688d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3689d9945d78SBjoern A. Zeeb 				continue;
3690d9945d78SBjoern A. Zeeb 
36913dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3692d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3693d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3694d9945d78SBjoern A. Zeeb 				break;
3695d9945d78SBjoern A. Zeeb 		}
3696d9945d78SBjoern A. Zeeb 
3697d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3698d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3699d9945d78SBjoern A. Zeeb 			continue;
3700d9945d78SBjoern A. Zeeb 
3701d9945d78SBjoern A. Zeeb 		pb = p;
37023dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3703d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3704d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3705d9945d78SBjoern A. Zeeb 
37063dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
37073dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
37083dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
37093dd98026SBjoern A. Zeeb 
37103dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37113dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
37123dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
37133dd98026SBjoern A. Zeeb 		}
37143dd98026SBjoern A. Zeeb #endif
37153dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
37163dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
37173dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
37183dd98026SBjoern A. Zeeb 
37193dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37203dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
37213dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
37223dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
37233dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
37243dd98026SBjoern A. Zeeb 		}
37253dd98026SBjoern A. Zeeb #endif
37263dd98026SBjoern A. Zeeb 
3727d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3728d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3729d9945d78SBjoern A. Zeeb 	}
3730d9945d78SBjoern A. Zeeb 
3731d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3732d9945d78SBjoern A. Zeeb 	pb = p;
3733d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3734d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3735d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3736d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3737d9945d78SBjoern A. Zeeb 	}
3738d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3739d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3740d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3741d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3742d9945d78SBjoern A. Zeeb 	}
3743d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3744d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3745d9945d78SBjoern A. Zeeb 
3746d9945d78SBjoern A. Zeeb 	return (p);
37476b4cac81SBjoern A. Zeeb }
37486b4cac81SBjoern A. Zeeb 
37496b4cac81SBjoern A. Zeeb static void
37506b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
37516b4cac81SBjoern A. Zeeb {
37526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
37536b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
37546b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
37556b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
37566b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
37576b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
37586b4cac81SBjoern A. Zeeb 	int error;
37598ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
37606b4cac81SBjoern A. Zeeb 
37616b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
37628ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3763a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
37646b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
37658ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37666b4cac81SBjoern A. Zeeb 		return;
37676b4cac81SBjoern A. Zeeb 	}
37688ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
37698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
37706b4cac81SBjoern A. Zeeb 
37716b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
37726b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
37736b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3774d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
37756b4cac81SBjoern A. Zeeb 		return;
37766b4cac81SBjoern A. Zeeb 	}
37776b4cac81SBjoern A. Zeeb 
37786b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
37798ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3780a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3781a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3782d3ef7fb4SBjoern A. Zeeb sw_scan:
37836b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
37846b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3785086be6a8SBjoern A. Zeeb 
3786086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3787086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3788086be6a8SBjoern A. Zeeb 
37896b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
37906b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
37916b4cac81SBjoern A. Zeeb 		IMPROVE();
37926b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
37936b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
37946b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
37956b4cac81SBjoern A. Zeeb 
37966b4cac81SBjoern A. Zeeb 	} else {
37976b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
37986b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
37996b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
38006b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
38016b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3802d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3803d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3804d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
38053206587aSBjoern A. Zeeb 		bool running;
3806d9945d78SBjoern A. Zeeb 
3807d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3808d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
38096b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
38106b4cac81SBjoern A. Zeeb 
3811d9945d78SBjoern A. Zeeb 		band_mask = 0;
38126b4cac81SBjoern A. Zeeb 		nchan = 0;
3813c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3814c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3815d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
38166b4cac81SBjoern A. Zeeb 				nchan++;
3817d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3818d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3819d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3820d9945d78SBjoern A. Zeeb 			}
3821c272abc5SBjoern A. Zeeb #else
3822c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3823c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3824c272abc5SBjoern A. Zeeb 				switch (band) {
3825c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3826c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3827c272abc5SBjoern A. Zeeb 					break;
3828c272abc5SBjoern A. Zeeb 				default:
3829c272abc5SBjoern A. Zeeb 					continue;
3830c272abc5SBjoern A. Zeeb 				}
3831c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3832c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3833c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3834c272abc5SBjoern A. Zeeb 				}
3835c272abc5SBjoern A. Zeeb 			}
3836c272abc5SBjoern A. Zeeb #endif
3837c272abc5SBjoern A. Zeeb 		} else {
38383206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
38393206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
38403206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
38413206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
38423206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
38433206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
38443206587aSBjoern A. Zeeb 			 */
38453206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
38463206587aSBjoern A. Zeeb 		}
38473206587aSBjoern A. Zeeb 
38486b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
38496b4cac81SBjoern A. Zeeb 
3850d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3851d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3852d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3853d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3854d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3855d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3856d9945d78SBjoern A. Zeeb 
3857d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3858d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3859d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3860d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3861d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3862d9945d78SBjoern A. Zeeb 		}
3863d9945d78SBjoern A. Zeeb 
38643206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3865d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3866d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
38676b4cac81SBjoern A. Zeeb 
38686b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
38696b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
38706b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
38716b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
38726b4cac81SBjoern A. Zeeb #if 0
38736b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
38746b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
38756b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
38766b4cac81SBjoern A. Zeeb #endif
38776b4cac81SBjoern A. Zeeb #ifdef __notyet__
38786b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
38796b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
38806b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
38816b4cac81SBjoern A. Zeeb #endif
3882e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
38836b4cac81SBjoern A. Zeeb 
38846b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
38856b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
38866b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
38876b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
38886b4cac81SBjoern A. Zeeb 			*(cpp + i) =
38896b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
38906b4cac81SBjoern A. Zeeb 		}
3891c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
38926b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3893c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
38946b4cac81SBjoern A. Zeeb 
3895c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
38966b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
38973206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
38986b4cac81SBjoern A. Zeeb 			/* lc->flags */
38996b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
39006b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
39016b4cac81SBjoern A. Zeeb 			/* lc-> ... */
39026b4cac81SBjoern A. Zeeb 			lc++;
39036b4cac81SBjoern A. Zeeb 		}
3904c272abc5SBjoern A. Zeeb #else
3905c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3906c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3907c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3908c272abc5SBjoern A. Zeeb 
3909c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3910c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3911c272abc5SBjoern A. Zeeb 				continue;
3912c272abc5SBjoern A. Zeeb 
3913c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
3914c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
3915c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
3916c272abc5SBjoern A. Zeeb 				continue;
3917c272abc5SBjoern A. Zeeb 
3918c272abc5SBjoern A. Zeeb 			channels = supband->channels;
3919c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
3920c272abc5SBjoern A. Zeeb 				*lc = channels[i];
3921c272abc5SBjoern A. Zeeb 				lc++;
3922c272abc5SBjoern A. Zeeb 			}
3923c272abc5SBjoern A. Zeeb 		}
3924c272abc5SBjoern A. Zeeb #endif
39256b4cac81SBjoern A. Zeeb 
3926d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
39276b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
39286b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
39296b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3930d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
39316b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
39326b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
39336b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
39346b4cac81SBjoern A. Zeeb 				ssids++;
39356b4cac81SBjoern A. Zeeb 			}
39366b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
39376b4cac81SBjoern A. Zeeb 		} else {
39386b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
39396b4cac81SBjoern A. Zeeb 		}
39406b4cac81SBjoern A. Zeeb 
39416b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
39426b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
39436b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
39446b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
39456b4cac81SBjoern A. Zeeb 		/* s6gp->... */
39466b4cac81SBjoern A. Zeeb 
3947d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
3948d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
3949d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3950d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
3951d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
3952d9945d78SBjoern A. Zeeb 
39536b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
39546b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
39553206587aSBjoern A. Zeeb 
39563206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
39573206587aSBjoern A. Zeeb 		/* Re-check under lock. */
39583206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
39593206587aSBjoern A. Zeeb 		if (!running) {
39603206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
39613206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
39623206587aSBjoern A. Zeeb 
39633206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
39643206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
39653206587aSBjoern A. Zeeb 		}
39663206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
39673206587aSBjoern A. Zeeb 		if (running) {
39683206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
39693206587aSBjoern A. Zeeb 			return;
39703206587aSBjoern A. Zeeb 		}
39713206587aSBjoern A. Zeeb 
39726b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
39736b4cac81SBjoern A. Zeeb 		if (error != 0) {
3974d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
3975d9945d78SBjoern A. Zeeb 
39763206587aSBjoern A. Zeeb 			/*
39773206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
39783206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
39793206587aSBjoern A. Zeeb 			 * and only there.
39803206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
39813206587aSBjoern A. Zeeb 			 * behave differently:
39823206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
39833206587aSBjoern A. Zeeb 			 *        and can then still return an error.
39843206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
39853206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
39863206587aSBjoern A. Zeeb 			 * ...
39873206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
39883206587aSBjoern A. Zeeb 			 * and balance between both code paths.
39893206587aSBjoern A. Zeeb 			 */
39903206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
39913206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
39923206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
39936b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
39943206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
39953206587aSBjoern A. Zeeb 			}
39963206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3997d3ef7fb4SBjoern A. Zeeb 
3998d3ef7fb4SBjoern A. Zeeb 			/*
3999d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4000d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4001d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4002d3ef7fb4SBjoern A. Zeeb 			 */
4003196cfd0bSBjoern A. Zeeb 			if (error == 1) {
40048ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4005a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
40068ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40073206587aSBjoern A. Zeeb 				/*
40083206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
40093206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
40103206587aSBjoern A. Zeeb 				 * currently not re-enable it?
40113206587aSBjoern A. Zeeb 				 */
40123206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4013196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4014196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4015196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4016196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4017196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4018196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4019196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4020196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4021d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4022196cfd0bSBjoern A. Zeeb 			}
4023d3ef7fb4SBjoern A. Zeeb 
4024d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4025d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
40266b4cac81SBjoern A. Zeeb 		}
40276b4cac81SBjoern A. Zeeb 	}
40286b4cac81SBjoern A. Zeeb }
40296b4cac81SBjoern A. Zeeb 
40306b4cac81SBjoern A. Zeeb static void
40316b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
40326b4cac81SBjoern A. Zeeb {
40336b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40348ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40356b4cac81SBjoern A. Zeeb 
40366b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4038a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
40398ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40406b4cac81SBjoern A. Zeeb 		return;
40416b4cac81SBjoern A. Zeeb 	}
40428ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40446b4cac81SBjoern A. Zeeb 
40458ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4046a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4047a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
40486b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
40496b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
40506b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
40516b4cac81SBjoern A. Zeeb 
4052a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4053a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
40546b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
40556b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40566b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4057a486fbbdSBjoern A. Zeeb 
40586b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
40596b4cac81SBjoern A. Zeeb 
40606b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4061086be6a8SBjoern A. Zeeb 
4062086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4063086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
40646b4cac81SBjoern A. Zeeb 	}
40656b4cac81SBjoern A. Zeeb }
40666b4cac81SBjoern A. Zeeb 
40676b4cac81SBjoern A. Zeeb static void
4068a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4069a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4070a486fbbdSBjoern A. Zeeb {
4071a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
40728ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4073a486fbbdSBjoern A. Zeeb 
4074a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
40758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
40768ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40788ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4079a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4080a486fbbdSBjoern A. Zeeb }
4081a486fbbdSBjoern A. Zeeb 
4082a486fbbdSBjoern A. Zeeb static void
4083a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4084a486fbbdSBjoern A. Zeeb {
4085a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
40868ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4087a486fbbdSBjoern A. Zeeb 
4088a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
40898ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
40908ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
40918ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40928ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4093a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4094a486fbbdSBjoern A. Zeeb }
4095a486fbbdSBjoern A. Zeeb 
4096a486fbbdSBjoern A. Zeeb static void
40976b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
40986b4cac81SBjoern A. Zeeb {
40996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41006b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4101b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4102b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
41036b4cac81SBjoern A. Zeeb 	int error;
41048ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
41056b4cac81SBjoern A. Zeeb 
41066b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4107b2cf3c21SBjoern A. Zeeb 
4108b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4109b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
41106b4cac81SBjoern A. Zeeb 		return;
41116b4cac81SBjoern A. Zeeb 
4112a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
41138ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41148ac540d3SBjoern A. Zeeb 	hw_scan_running =
41158ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
41168ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
41178ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41188ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4119a486fbbdSBjoern A. Zeeb 		return;
4120a486fbbdSBjoern A. Zeeb 
4121b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4122b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
41236b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
41246b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
41256b4cac81SBjoern A. Zeeb 		return;
41266b4cac81SBjoern A. Zeeb 	}
41276b4cac81SBjoern A. Zeeb 
41286b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
41296b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
41306b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
41316b4cac81SBjoern A. Zeeb 		    c, chan);
41326b4cac81SBjoern A. Zeeb 		return;
41336b4cac81SBjoern A. Zeeb 	}
41346b4cac81SBjoern A. Zeeb 
41356b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
41366b4cac81SBjoern A. Zeeb 	IMPROVE();
41376b4cac81SBjoern A. Zeeb 
41386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41394a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
41409fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
414111450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
41429fb91463SBjoern A. Zeeb #endif
41434a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
41446b4cac81SBjoern A. Zeeb 
41456b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
41466b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
41476b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
41486b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
41496b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
41506b4cac81SBjoern A. Zeeb 		IMPROVE();
41516b4cac81SBjoern A. Zeeb 	} else {
41526b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
41536b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
41546b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
41556b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
41566b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
41576b4cac81SBjoern A. Zeeb 	}
41586b4cac81SBjoern A. Zeeb 
41596b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
41606b4cac81SBjoern A. Zeeb 	IMPROVE();
41616b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
41626b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
41636b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
41646b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
41656b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
41666b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
41676b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
41686b4cac81SBjoern A. Zeeb 			    error);
41696b4cac81SBjoern A. Zeeb 	}
41706b4cac81SBjoern A. Zeeb }
41716b4cac81SBjoern A. Zeeb 
41726b4cac81SBjoern A. Zeeb static struct ieee80211_node *
41736b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
41746b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
41756b4cac81SBjoern A. Zeeb {
41766b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
41776b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41784f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
41796b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
41806b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
41816b4cac81SBjoern A. Zeeb 
41826b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
41836b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
41846b4cac81SBjoern A. Zeeb 
41856b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
41864f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
41874f61ef8bSBjoern A. Zeeb 		return (NULL);
41884f61ef8bSBjoern A. Zeeb 
41896b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
41906b4cac81SBjoern A. Zeeb 	if (ni == NULL)
41916b4cac81SBjoern A. Zeeb 		return (NULL);
41926b4cac81SBjoern A. Zeeb 
41936b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41944f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
41956b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
41966b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
41976b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
41986b4cac81SBjoern A. Zeeb 		return (NULL);
41996b4cac81SBjoern A. Zeeb 	}
42006b4cac81SBjoern A. Zeeb 
42016b4cac81SBjoern A. Zeeb 	return (ni);
42026b4cac81SBjoern A. Zeeb }
42036b4cac81SBjoern A. Zeeb 
42046b4cac81SBjoern A. Zeeb static int
42056b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
42066b4cac81SBjoern A. Zeeb {
42076b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42096b4cac81SBjoern A. Zeeb 	int error;
42106b4cac81SBjoern A. Zeeb 
42116b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42126b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42136b4cac81SBjoern A. Zeeb 
42146b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
42156b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
42166b4cac81SBjoern A. Zeeb 		if (error != 0)
42176b4cac81SBjoern A. Zeeb 			return (error);
42186b4cac81SBjoern A. Zeeb 	}
42196b4cac81SBjoern A. Zeeb 
42206b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
42216b4cac81SBjoern A. Zeeb 	IMPROVE();
42226b4cac81SBjoern A. Zeeb 
42236b4cac81SBjoern A. Zeeb 	return (0);
42246b4cac81SBjoern A. Zeeb }
42256b4cac81SBjoern A. Zeeb 
42266b4cac81SBjoern A. Zeeb static void
42276b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
42286b4cac81SBjoern A. Zeeb {
42296b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42306b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42316b4cac81SBjoern A. Zeeb 
42326b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42336b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42346b4cac81SBjoern A. Zeeb 
42356b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
42366b4cac81SBjoern A. Zeeb 	IMPROVE();
42376b4cac81SBjoern A. Zeeb 
42386b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
42396b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
42406b4cac81SBjoern A. Zeeb }
42416b4cac81SBjoern A. Zeeb 
42426b4cac81SBjoern A. Zeeb static void
42436b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
42446b4cac81SBjoern A. Zeeb {
42456b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42476b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42486b4cac81SBjoern A. Zeeb 
42496b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42506b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42516b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
42526b4cac81SBjoern A. Zeeb 
42530936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
42546b4cac81SBjoern A. Zeeb 
42550936c648SBjoern A. Zeeb 	/*
42560936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
42570936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
42580936c648SBjoern A. Zeeb 	 */
42590936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
42606b4cac81SBjoern A. Zeeb 
42616b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
42626b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
42636b4cac81SBjoern A. Zeeb }
42646b4cac81SBjoern A. Zeeb 
42656b4cac81SBjoern A. Zeeb static int
42666b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
42676b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
42686b4cac81SBjoern A. Zeeb {
42696b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42706b4cac81SBjoern A. Zeeb 
42716b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4272fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
42732372f8ccSBjoern A. Zeeb #if 0
427488665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
42752372f8ccSBjoern A. Zeeb #else
42762372f8ccSBjoern A. Zeeb 	/*
42772372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
42782372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
42792372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
42802372f8ccSBjoern A. Zeeb 	 */
42812372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
42822372f8ccSBjoern A. Zeeb #endif
4283fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4284fa4e4257SBjoern A. Zeeb 		/*
4285fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4286fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4287fa4e4257SBjoern A. Zeeb 		 */
42880936c648SBjoern A. Zeeb 		m_free(m);
42890936c648SBjoern A. Zeeb 		return (ENETDOWN);
42900936c648SBjoern A. Zeeb 	}
42916b4cac81SBjoern A. Zeeb 
42926b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
42936b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4294fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4295fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
42966b4cac81SBjoern A. Zeeb 
42979d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
42989d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
42996b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
43006b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
43016b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
43029d9ba2b7SBjoern A. Zeeb #endif
43036b4cac81SBjoern A. Zeeb 
43046b4cac81SBjoern A. Zeeb 	return (0);
43056b4cac81SBjoern A. Zeeb }
43066b4cac81SBjoern A. Zeeb 
430711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
430811db70b6SBjoern A. Zeeb static int
430911db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
431011db70b6SBjoern A. Zeeb     struct sk_buff *skb)
431111db70b6SBjoern A. Zeeb {
431211db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
431311db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
431411db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
431511db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
431611db70b6SBjoern A. Zeeb 	uint8_t *p;
431711db70b6SBjoern A. Zeeb 
431811db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
431911db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
432011db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
432111db70b6SBjoern A. Zeeb 
432211db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
432311db70b6SBjoern A. Zeeb 
432411db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
432511db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
432611db70b6SBjoern A. Zeeb 
432711db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
432811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
432911db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
433011db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
433111db70b6SBjoern A. Zeeb 		return (ENXIO);
433211db70b6SBjoern A. Zeeb 	}
433311db70b6SBjoern A. Zeeb 
433411db70b6SBjoern A. Zeeb 
433511db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
433611db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
433711db70b6SBjoern A. Zeeb 
433811db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
433911db70b6SBjoern A. Zeeb 
434011db70b6SBjoern A. Zeeb 	/*
434111db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
434211db70b6SBjoern A. Zeeb 	 * or if we are done?
434311db70b6SBjoern A. Zeeb 	 */
434411db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
434511db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
434611db70b6SBjoern A. Zeeb 	    /* MFP */
434711db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
434811db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
434911db70b6SBjoern A. Zeeb 			return (0);
435011db70b6SBjoern A. Zeeb 
435111db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
435211db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
435311db70b6SBjoern A. Zeeb 		return (ENOSPC);
435411db70b6SBjoern A. Zeeb 
435511db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
435611db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
435711db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
435811db70b6SBjoern A. Zeeb 
435911db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
436011db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
436111db70b6SBjoern A. Zeeb 		return (0);
436211db70b6SBjoern A. Zeeb 
436311db70b6SBjoern A. Zeeb 	p += hdrlen;
436411db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
436511db70b6SBjoern A. Zeeb 
436611db70b6SBjoern A. Zeeb 	return (0);
436711db70b6SBjoern A. Zeeb }
436811db70b6SBjoern A. Zeeb #endif
436911db70b6SBjoern A. Zeeb 
43706b4cac81SBjoern A. Zeeb static void
43716b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
43726b4cac81SBjoern A. Zeeb {
43736b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
43746b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
43756b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
43766b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
43776b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43786b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43796b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
43806b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
43816b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
43826b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
43836b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
43846b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
43856b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4386e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4387ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
43886b4cac81SBjoern A. Zeeb 	void *buf;
438911db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4390e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
43916b4cac81SBjoern A. Zeeb 
43926b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
43936b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
43949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
43956b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
43966b4cac81SBjoern A. Zeeb #endif
43976b4cac81SBjoern A. Zeeb 
43986b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4399b35f6cd0SBjoern A. Zeeb 	k = NULL;
440011db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
44016b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
44026b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
440311db70b6SBjoern A. Zeeb 
440411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
440511db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
440611db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
440711db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
440811db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
440911db70b6SBjoern A. Zeeb 		}
441011db70b6SBjoern A. Zeeb #endif
441111db70b6SBjoern A. Zeeb 
441211db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
441311db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
44146b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
44156b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
44166b4cac81SBjoern A. Zeeb 			if (k == NULL) {
44176b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
44186b4cac81SBjoern A. Zeeb 				m_freem(m);
44196b4cac81SBjoern A. Zeeb 				return;
44206b4cac81SBjoern A. Zeeb 			}
44216b4cac81SBjoern A. Zeeb 		}
442211db70b6SBjoern A. Zeeb 	}
44236b4cac81SBjoern A. Zeeb 
44246b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44256b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44266b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44276b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
44286b4cac81SBjoern A. Zeeb 
44296b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
44306b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
44316b4cac81SBjoern A. Zeeb 
44326b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
44336b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
44346b4cac81SBjoern A. Zeeb 		if (k != NULL)
44356b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
44366b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
44376b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
44386b4cac81SBjoern A. Zeeb 		IMPROVE();
44396b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
44406b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
44416b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
44426b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
44436b4cac81SBjoern A. Zeeb 		}
44446b4cac81SBjoern A. Zeeb 
44456b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
44466b4cac81SBjoern A. Zeeb 	}
44476b4cac81SBjoern A. Zeeb 
44486b4cac81SBjoern A. Zeeb 	/*
44496b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
44506b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
44513d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
44523d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
44536b4cac81SBjoern A. Zeeb 	 */
44546b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
44556b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4456bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4457bcf1d8eeSBjoern A. Zeeb 
4458bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4459bcf1d8eeSBjoern A. Zeeb 			int tid;
4460bcf1d8eeSBjoern A. Zeeb 
4461bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4462bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4463bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4464bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4465bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4466bcf1d8eeSBjoern A. Zeeb 					continue;
4467bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4468bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4469bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4470bcf1d8eeSBjoern A. Zeeb 			}
4471bcf1d8eeSBjoern A. Zeeb 		}
44726b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
44736b4cac81SBjoern A. Zeeb 		m_freem(m);
44746b4cac81SBjoern A. Zeeb 		return;
44756b4cac81SBjoern A. Zeeb 	}
44766b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
44776b4cac81SBjoern A. Zeeb 
44786b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
44796b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
44806b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
44816b4cac81SBjoern A. Zeeb 	skb->m = m;
44826b4cac81SBjoern A. Zeeb #if 0
44836b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
44846b4cac81SBjoern A. Zeeb #else
44856b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
44866b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
44876b4cac81SBjoern A. Zeeb #endif
44886b4cac81SBjoern A. Zeeb 	/* Save the ni. */
44896b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
44906b4cac81SBjoern A. Zeeb 
44916b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
44926b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
44936b4cac81SBjoern A. Zeeb 
4494e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4495e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4496e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
44971665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
44981665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
44991665ef97SBjoern A. Zeeb 			skb->priority = 7;
45001665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
45011665ef97SBjoern A. Zeeb 		} else {
45021665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
45031665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4504e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4505e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
45061665ef97SBjoern A. Zeeb 		}
4507e3a0b120SBjoern A. Zeeb 	} else {
4508e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4509fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4510e3a0b120SBjoern A. Zeeb 	}
45116b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
45126b4cac81SBjoern A. Zeeb 
45136b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
45146b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
45156b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
45166b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
45176b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
45186b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4519e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
45206b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
45216b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
45226b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
45236b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
45249fb91463SBjoern A. Zeeb #ifdef __notyet__
45259fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45269fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
45279fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
45289fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
45299fb91463SBjoern A. Zeeb #endif
45309fb91463SBjoern A. Zeeb #endif
45316b4cac81SBjoern A. Zeeb 
45326b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4533b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
453411db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
453511db70b6SBjoern A. Zeeb 		int error;
453611db70b6SBjoern A. Zeeb 
453711db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
453811db70b6SBjoern A. Zeeb 		if (error != 0) {
453911db70b6SBjoern A. Zeeb 			/*
454011db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
454111db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
454211db70b6SBjoern A. Zeeb 			 */
454311db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
454411db70b6SBjoern A. Zeeb 			return;
454511db70b6SBjoern A. Zeeb 		}
454611db70b6SBjoern A. Zeeb 	}
45476b4cac81SBjoern A. Zeeb #endif
45486b4cac81SBjoern A. Zeeb 
45496b4cac81SBjoern A. Zeeb 	IMPROVE();
45506b4cac81SBjoern A. Zeeb 
4551e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4552e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4553e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4554e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4555e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4556d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4557e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4558e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4559e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4560e3a0b120SBjoern A. Zeeb 	}
4561e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4562d0d29110SBjoern A. Zeeb 		goto ops_tx;
4563e3a0b120SBjoern A. Zeeb 
4564d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4565d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4566d0d29110SBjoern A. Zeeb 
4567eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
45686b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
45699d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45709d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4571d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4572d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4573d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4574fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4575d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4576d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4577d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4578d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
45799d9ba2b7SBjoern A. Zeeb #endif
4580eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
458145bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4582d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
458345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
45846b4cac81SBjoern A. Zeeb 	return;
45856b4cac81SBjoern A. Zeeb 
45866b4cac81SBjoern A. Zeeb ops_tx:
45879d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
45889d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4589d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4590d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
45916b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
45926b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
45939d9ba2b7SBjoern A. Zeeb #endif
45946b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
45956b4cac81SBjoern A. Zeeb 	control.sta = sta;
459645bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
45976b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
459845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
45996b4cac81SBjoern A. Zeeb }
46006b4cac81SBjoern A. Zeeb 
46016b4cac81SBjoern A. Zeeb static void
46026b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
46036b4cac81SBjoern A. Zeeb {
46046b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46056b4cac81SBjoern A. Zeeb 	struct mbufq mq;
46066b4cac81SBjoern A. Zeeb 	struct mbuf *m;
460788665349SBjoern A. Zeeb 	bool shall_tx;
46086b4cac81SBjoern A. Zeeb 
46096b4cac81SBjoern A. Zeeb 	lsta = ctx;
46106b4cac81SBjoern A. Zeeb 
46119d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46129d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
46136b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
46149d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
46156b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
46169d9ba2b7SBjoern A. Zeeb #endif
46176b4cac81SBjoern A. Zeeb 
46186b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
46196b4cac81SBjoern A. Zeeb 
4620fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4621fa4e4257SBjoern A. Zeeb 	/*
4622fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
462388665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
462488665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
462588665349SBjoern A. Zeeb 	 * point to try to TX.
462688665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
462788665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4628fa4e4257SBjoern A. Zeeb 	 */
46292372f8ccSBjoern A. Zeeb #if 0
463088665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
46312372f8ccSBjoern A. Zeeb #else
46322372f8ccSBjoern A. Zeeb 	/*
46332372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46342372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46352372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46362372f8ccSBjoern A. Zeeb 	 */
46372372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
46382372f8ccSBjoern A. Zeeb #endif
463988665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
46406b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
464188665349SBjoern A. Zeeb 	/*
464288665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
464388665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
464488665349SBjoern A. Zeeb 	 */
4645fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
46466b4cac81SBjoern A. Zeeb 
46476b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
46486b4cac81SBjoern A. Zeeb 	while (m != NULL) {
46496b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
46506b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
46516b4cac81SBjoern A. Zeeb 	}
46526b4cac81SBjoern A. Zeeb }
46536b4cac81SBjoern A. Zeeb 
46546b4cac81SBjoern A. Zeeb static int
46556b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
46566b4cac81SBjoern A. Zeeb {
46576b4cac81SBjoern A. Zeeb 
46586b4cac81SBjoern A. Zeeb 	/* XXX TODO */
46596b4cac81SBjoern A. Zeeb 	IMPROVE();
46606b4cac81SBjoern A. Zeeb 
46616b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
46626b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
46636b4cac81SBjoern A. Zeeb 
46646b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
46656b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
46666b4cac81SBjoern A. Zeeb }
46676b4cac81SBjoern A. Zeeb 
46689fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
46699fb91463SBjoern A. Zeeb static int
46709fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
46719fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
46729fb91463SBjoern A. Zeeb {
46739fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46749fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46759fb91463SBjoern A. Zeeb 
46769fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46779fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46789fb91463SBjoern A. Zeeb 
4679310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
46809fb91463SBjoern A. Zeeb 
46819fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
46829fb91463SBjoern A. Zeeb }
46839fb91463SBjoern A. Zeeb 
46849fb91463SBjoern A. Zeeb static int
46859fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
46869fb91463SBjoern A. Zeeb {
46879fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
46889fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
46899fb91463SBjoern A. Zeeb 
46909fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
46919fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
46929fb91463SBjoern A. Zeeb 
4693310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
46949fb91463SBjoern A. Zeeb 
46959fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
46969fb91463SBjoern A. Zeeb }
46979fb91463SBjoern A. Zeeb 
46989fb91463SBjoern A. Zeeb 
46999fb91463SBjoern A. Zeeb static int
47009fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
47019fb91463SBjoern A. Zeeb {
47029fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47039fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47049fb91463SBjoern A. Zeeb 
47059fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47069fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47079fb91463SBjoern A. Zeeb 
4708310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
47099fb91463SBjoern A. Zeeb 
47109fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
47119fb91463SBjoern A. Zeeb }
47129fb91463SBjoern A. Zeeb 
4713310743c4SBjoern A. Zeeb /*
4714310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4715310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4716310743c4SBjoern A. Zeeb  *
4717310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4718310743c4SBjoern A. Zeeb  */
47199fb91463SBjoern A. Zeeb static int
47209fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
47219fb91463SBjoern A. Zeeb     int dialogtoken, int baparamset, int batimeout)
47229fb91463SBjoern A. Zeeb {
47239fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47249fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4725310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4726310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4727310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4728310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4729310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4730310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4731310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4732310743c4SBjoern A. Zeeb 	int error;
47339fb91463SBjoern A. Zeeb 
47349fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47359fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4736310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4737310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4738310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4739310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4740310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4741310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
47429fb91463SBjoern A. Zeeb 
4743310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4744310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4745310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4746310743c4SBjoern A. Zeeb 		return (0);
4747310743c4SBjoern A. Zeeb 	}
4748310743c4SBjoern A. Zeeb 
4749310743c4SBjoern A. Zeeb 	params.sta = sta;
4750310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4751310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4752310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4753310743c4SBjoern A. Zeeb 	params.timeout = 0;
4754310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4755310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4756310743c4SBjoern A. Zeeb 	params.amsdu = false;
4757310743c4SBjoern A. Zeeb 
4758310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4759310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4760310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4761310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4762310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4763310743c4SBjoern A. Zeeb 	if (error != 0) {
4764310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4765310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4766310743c4SBjoern A. Zeeb 		return (0);
4767310743c4SBjoern A. Zeeb 	}
47689fb91463SBjoern A. Zeeb 
47699fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
47709fb91463SBjoern A. Zeeb }
47719fb91463SBjoern A. Zeeb 
4772310743c4SBjoern A. Zeeb /*
4773310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4774310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4775310743c4SBjoern A. Zeeb  *
4776310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4777310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4778310743c4SBjoern A. Zeeb  */
47799fb91463SBjoern A. Zeeb static int
47809fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
47819fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
47829fb91463SBjoern A. Zeeb {
47839fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47849fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4785310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4786310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4787310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4788310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4789310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4790310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4791310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4792310743c4SBjoern A. Zeeb 	int error;
47939fb91463SBjoern A. Zeeb 
47949fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47959fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4796310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4797310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4798310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4799310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4800310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4801310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48029fb91463SBjoern A. Zeeb 
4803310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4804310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4805310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4806310743c4SBjoern A. Zeeb 		return (0);
4807310743c4SBjoern A. Zeeb 	}
4808310743c4SBjoern A. Zeeb 
4809310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4810310743c4SBjoern A. Zeeb 		params.sta = sta;
4811310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4812310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4813310743c4SBjoern A. Zeeb 		params.timeout = 0;
4814310743c4SBjoern A. Zeeb 		params.ssn = 0;
4815310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4816310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4817310743c4SBjoern A. Zeeb 			params.amsdu = true;
4818310743c4SBjoern A. Zeeb 		else
4819310743c4SBjoern A. Zeeb 			params.amsdu = false;
4820310743c4SBjoern A. Zeeb 	} else {
4821310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4822310743c4SBjoern A. Zeeb 		params.sta = sta;
4823310743c4SBjoern A. Zeeb 		switch (status) {
4824310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4825310743c4SBjoern A. Zeeb 		default:
4826310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4827310743c4SBjoern A. Zeeb 			break;
4828310743c4SBjoern A. Zeeb 		}
4829310743c4SBjoern A. Zeeb 		params.buf_size = 0;
4830310743c4SBjoern A. Zeeb 		params.timeout = 0;
4831310743c4SBjoern A. Zeeb 		params.ssn = 0;
4832310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4833310743c4SBjoern A. Zeeb 		params.amsdu = false;
4834310743c4SBjoern A. Zeeb 	}
4835310743c4SBjoern A. Zeeb 
4836310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4837310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4838310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4839310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4840310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4841310743c4SBjoern A. Zeeb 	if (error != 0) {
4842310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4843310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4844310743c4SBjoern A. Zeeb 		return (0);
4845310743c4SBjoern A. Zeeb 	}
4846310743c4SBjoern A. Zeeb 
4847310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
48489fb91463SBjoern A. Zeeb 
48499fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
48509fb91463SBjoern A. Zeeb }
48519fb91463SBjoern A. Zeeb 
4852310743c4SBjoern A. Zeeb /*
4853310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4854310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4855310743c4SBjoern A. Zeeb  */
48569fb91463SBjoern A. Zeeb static void
48579fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
48589fb91463SBjoern A. Zeeb {
48599fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48609fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4861310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4862310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4863310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4864310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4865310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4866310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4867310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4868310743c4SBjoern A. Zeeb 	int error;
48699fb91463SBjoern A. Zeeb 
48709fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48719fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4872310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4873310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4874310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4875310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4876310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4877310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48789fb91463SBjoern A. Zeeb 
4879310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4880310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4881310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4882310743c4SBjoern A. Zeeb 		goto n80211;
4883310743c4SBjoern A. Zeeb 	}
48849fb91463SBjoern A. Zeeb 
4885310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4886310743c4SBjoern A. Zeeb 	params.sta = sta;
4887310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4888310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4889310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4890310743c4SBjoern A. Zeeb 	params.timeout = 0;
4891310743c4SBjoern A. Zeeb 	params.ssn = 0;
4892310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4893310743c4SBjoern A. Zeeb 	params.amsdu = false;
4894310743c4SBjoern A. Zeeb 
4895310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4896310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4897310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4898310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4899310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4900310743c4SBjoern A. Zeeb 	if (error != 0) {
4901310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4902310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4903310743c4SBjoern A. Zeeb 		goto n80211;
4904310743c4SBjoern A. Zeeb 	}
4905310743c4SBjoern A. Zeeb 
4906310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4907310743c4SBjoern A. Zeeb 
4908310743c4SBjoern A. Zeeb n80211:
49099fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
49109fb91463SBjoern A. Zeeb }
49119fb91463SBjoern A. Zeeb 
49129fb91463SBjoern A. Zeeb static void
49139fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49149fb91463SBjoern A. Zeeb {
49159fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49169fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49179fb91463SBjoern A. Zeeb 
49189fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49199fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49209fb91463SBjoern A. Zeeb 
49219fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49229fb91463SBjoern A. Zeeb 
49239fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
49249fb91463SBjoern A. Zeeb }
49259fb91463SBjoern A. Zeeb 
49269fb91463SBjoern A. Zeeb static void
49279fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
49289fb91463SBjoern A. Zeeb     int status)
49299fb91463SBjoern A. Zeeb {
49309fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49319fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49329fb91463SBjoern A. Zeeb 
49339fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49349fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49359fb91463SBjoern A. Zeeb 
49369fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49379fb91463SBjoern A. Zeeb 
49389fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
49399fb91463SBjoern A. Zeeb }
49409fb91463SBjoern A. Zeeb 
49419fb91463SBjoern A. Zeeb static int
49429fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
49439fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
49449fb91463SBjoern A. Zeeb {
49459fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49469fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49479fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
49489fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
49499fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
49509fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
49519fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
49529fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4953310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
49549fb91463SBjoern A. Zeeb 	int error;
49559fb91463SBjoern A. Zeeb 
49569fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49579fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49589fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
49599fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
49609fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
49619fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
49629fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
49639fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49649fb91463SBjoern A. Zeeb 
4965310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
4966310743c4SBjoern A. Zeeb 
4967310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4968310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
4969310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
4970310743c4SBjoern A. Zeeb 		return (-ENXIO);
4971310743c4SBjoern A. Zeeb 	}
4972310743c4SBjoern A. Zeeb 
49739fb91463SBjoern A. Zeeb 	params.sta = sta;
49749fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
49759fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
49769fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
49779fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
49789fb91463SBjoern A. Zeeb 	else
49799fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4980310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
4981310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
49829fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
49839fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
49849fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
49859fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4986310743c4SBjoern A. Zeeb 
4987310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
4988310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
4989310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
4990310743c4SBjoern A. Zeeb 		params.amsdu = true;
4991310743c4SBjoern A. Zeeb 	else
49929fb91463SBjoern A. Zeeb 		params.amsdu = false;
49939fb91463SBjoern A. Zeeb 
4994310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
49959fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4996310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
49979fb91463SBjoern A. Zeeb 	if (error != 0) {
49989fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
49999fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50009fb91463SBjoern A. Zeeb 		return (error);
50019fb91463SBjoern A. Zeeb 	}
5002310743c4SBjoern A. Zeeb 
5003310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5004310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5005310743c4SBjoern A. Zeeb 	}
5006310743c4SBjoern A. Zeeb 
50079fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
50089fb91463SBjoern A. Zeeb 
50099fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
50109fb91463SBjoern A. Zeeb 	return (error);
50119fb91463SBjoern A. Zeeb }
50129fb91463SBjoern A. Zeeb 
50139fb91463SBjoern A. Zeeb static void
50149fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
50159fb91463SBjoern A. Zeeb {
50169fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50179fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50189fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50199fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50209fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50219fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50229fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50239fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5024310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50259fb91463SBjoern A. Zeeb 	int error;
50269fb91463SBjoern A. Zeeb 	uint8_t tid;
5027*65c573e4SBjoern A. Zeeb 	bool ic_locked;
50289fb91463SBjoern A. Zeeb 
50299fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50309fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50319fb91463SBjoern A. Zeeb 
50329fb91463SBjoern A. Zeeb 	/*
50339fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
50349fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
50359fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
50369fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
50379fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
50389fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
50399fb91463SBjoern A. Zeeb 	 * track some state itself.
50409fb91463SBjoern A. Zeeb 	 */
50419fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
50429fb91463SBjoern A. Zeeb 		goto net80211_only;
50439fb91463SBjoern A. Zeeb 
50449fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
50459fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
50469fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
50479fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50489fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
50499fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50509fb91463SBjoern A. Zeeb 
50519fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
50529fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
50539fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
50549fb91463SBjoern A. Zeeb 			break;
50559fb91463SBjoern A. Zeeb 	}
50569fb91463SBjoern A. Zeeb 
50579fb91463SBjoern A. Zeeb 	params.sta = sta;
50589fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
50599fb91463SBjoern A. Zeeb 	params.buf_size = 0;
50609fb91463SBjoern A. Zeeb 	params.timeout = 0;
50619fb91463SBjoern A. Zeeb 	params.ssn = 0;
50629fb91463SBjoern A. Zeeb 	params.tid = tid;
50639fb91463SBjoern A. Zeeb 	params.amsdu = false;
50649fb91463SBjoern A. Zeeb 
5065*65c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
5066*65c573e4SBjoern A. Zeeb 	if (ic_locked)
5067*65c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5068310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
50699fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5070310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
5071*65c573e4SBjoern A. Zeeb 	if (ic_locked)
5072*65c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
50739fb91463SBjoern A. Zeeb 	if (error != 0)
50749fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
50759fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50769fb91463SBjoern A. Zeeb 
50779fb91463SBjoern A. Zeeb net80211_only:
50789fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
50799fb91463SBjoern A. Zeeb }
50809fb91463SBjoern A. Zeeb #endif
50819fb91463SBjoern A. Zeeb 
50829fb91463SBjoern A. Zeeb static void
50839fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
50849fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
50859fb91463SBjoern A. Zeeb {
50869fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
50879fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
50889fb91463SBjoern A. Zeeb 
50899fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
50909fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
50919fb91463SBjoern A. Zeeb 		return;
50929fb91463SBjoern A. Zeeb 
50939fb91463SBjoern A. Zeeb 	switch (band) {
50949fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
50959fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
50969fb91463SBjoern A. Zeeb 		break;
50979fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
50989fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
50999fb91463SBjoern A. Zeeb 		break;
51009fb91463SBjoern A. Zeeb 	default:
51019fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
51029fb91463SBjoern A. Zeeb 		return;
51039fb91463SBjoern A. Zeeb 	}
51049fb91463SBjoern A. Zeeb 
51059fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
51069fb91463SBjoern A. Zeeb 
51079fb91463SBjoern A. Zeeb 	/*
51089fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
51099fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
51109fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
51119fb91463SBjoern A. Zeeb 	 */
51129fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
51139fb91463SBjoern A. Zeeb 
51149fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
51159fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
51169fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
51179fb91463SBjoern A. Zeeb #ifdef __notyet__
51189fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
51199fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
51209fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
51219fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
51229fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
51239fb91463SBjoern A. Zeeb #endif
51249fb91463SBjoern A. Zeeb 
51259fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
51269fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
51279fb91463SBjoern A. Zeeb 
51289fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
51299fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
51309fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
51319fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
51329fb91463SBjoern A. Zeeb #endif
51339fb91463SBjoern A. Zeeb }
51349fb91463SBjoern A. Zeeb 
51356b4cac81SBjoern A. Zeeb static void
51366b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
51376b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
51386b4cac81SBjoern A. Zeeb {
51396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
51406b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
51416b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
51426b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
51436b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
51446b4cac81SBjoern A. Zeeb 
51456b4cac81SBjoern A. Zeeb 	/* Channels */
51466b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
51476b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51486b4cac81SBjoern A. Zeeb 
51496b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
51506b4cac81SBjoern A. Zeeb 	nchans = 0;
51516b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
51526b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
51536b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
51546b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
51556b4cac81SBjoern A. Zeeb 		chan_flags = 0;
51566b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
51576b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
51589fb91463SBjoern A. Zeeb 
51599fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
51606b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
51619fb91463SBjoern A. Zeeb 
51629fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
51639fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
51646b4cac81SBjoern A. Zeeb 
51656b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5166cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
51676b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
51686b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
51696b4cac81SBjoern A. Zeeb 
51706b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
51713f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
51723f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
51736b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
51746b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
51756b4cac81SBjoern A. Zeeb 				continue;
51766b4cac81SBjoern A. Zeeb 			}
51776b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
51786b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
51796b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
51806b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
51816b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
51826b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
51836b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
51846b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
51856b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
51866b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
51876b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
51886b4cac81SBjoern A. Zeeb 
51896b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
51906b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
51916b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
51925856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5193cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5194cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
51953f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
51963f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
51976b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
51986b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
51996b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5200cee56e77SBjoern A. Zeeb 			if (error != 0)
52016b4cac81SBjoern A. Zeeb 				break;
52026b4cac81SBjoern A. Zeeb 		}
52036b4cac81SBjoern A. Zeeb 	}
52046b4cac81SBjoern A. Zeeb 
52056b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
52066b4cac81SBjoern A. Zeeb 	nchans = 0;
52076b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
52086b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
52096b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52106b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52116b4cac81SBjoern A. Zeeb 		chan_flags = 0;
52126b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
52139fb91463SBjoern A. Zeeb 
52149fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
52159fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
52169fb91463SBjoern A. Zeeb 
52179fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
52186b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
52196b4cac81SBjoern A. Zeeb 
52206b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5221562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
52226b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5223ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5224ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
52256b4cac81SBjoern A. Zeeb 
52266b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
52276b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
52286b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5229562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52306b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
52316b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5232562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52336b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
52346b4cac81SBjoern A. Zeeb 		}
52356b4cac81SBjoern A. Zeeb #endif
52366b4cac81SBjoern A. Zeeb 
52376b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5238cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52396b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
52406b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
52416b4cac81SBjoern A. Zeeb 
52426b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
52433f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
52443f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
52456b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
52466b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
52476b4cac81SBjoern A. Zeeb 				continue;
52486b4cac81SBjoern A. Zeeb 			}
52496b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
52506b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
52516b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
52526b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
52536b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
52546b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
52556b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
52566b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
52576b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
52586b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
52596b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
52606b4cac81SBjoern A. Zeeb 
52616b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
52626b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
52636b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
52645856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5265cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5266cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
52673f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
52683f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
52696b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
52706b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
52716b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5272cee56e77SBjoern A. Zeeb 			if (error != 0)
52736b4cac81SBjoern A. Zeeb 				break;
52746b4cac81SBjoern A. Zeeb 		}
52756b4cac81SBjoern A. Zeeb 	}
52766b4cac81SBjoern A. Zeeb }
52776b4cac81SBjoern A. Zeeb 
52786b4cac81SBjoern A. Zeeb static void *
52796b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
52806b4cac81SBjoern A. Zeeb {
52816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
52826b4cac81SBjoern A. Zeeb 
52836b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
52846b4cac81SBjoern A. Zeeb 
52856b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
52866b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
52876b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
52886b4cac81SBjoern A. Zeeb 
52896b4cac81SBjoern A. Zeeb 	return (ic);
52906b4cac81SBjoern A. Zeeb }
52916b4cac81SBjoern A. Zeeb 
52926b4cac81SBjoern A. Zeeb struct ieee80211_hw *
52936b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
52946b4cac81SBjoern A. Zeeb {
52956b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52966b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52976b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5298a5ae63edSBjoern A. Zeeb 	int ac;
52996b4cac81SBjoern A. Zeeb 
53006b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
53016b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
53026b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
53036b4cac81SBjoern A. Zeeb 		return (NULL);
53046b4cac81SBjoern A. Zeeb 
53056b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
53066b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5307652e22d3SBjoern A. Zeeb 
53088ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
53098ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5310eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
53118891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
53126b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5313a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5314a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5315a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5316a5ae63edSBjoern A. Zeeb 	}
53176b4cac81SBjoern A. Zeeb 
5318759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5319759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5320759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5321759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5322759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5323759a996dSBjoern A. Zeeb 
53246b4cac81SBjoern A. Zeeb 	/*
53256b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
53266b4cac81SBjoern A. Zeeb 	 * not initialized.
53276b4cac81SBjoern A. Zeeb 	 */
53286b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53296b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5330086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
53316b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
53326b4cac81SBjoern A. Zeeb 
53336b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
53346b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
53356b4cac81SBjoern A. Zeeb 
53366b4cac81SBjoern A. Zeeb 	IMPROVE();
53376b4cac81SBjoern A. Zeeb 
53386b4cac81SBjoern A. Zeeb 	return (hw);
53396b4cac81SBjoern A. Zeeb }
53406b4cac81SBjoern A. Zeeb 
53416b4cac81SBjoern A. Zeeb void
53426b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
53436b4cac81SBjoern A. Zeeb {
53446b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5345759a996dSBjoern A. Zeeb 	struct mbuf *m;
53466b4cac81SBjoern A. Zeeb 
53476b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53486b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
53496b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
53506b4cac81SBjoern A. Zeeb 
5351759a996dSBjoern A. Zeeb 	/*
5352759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5353759a996dSBjoern A. Zeeb 	 */
5354759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5355759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5356759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5357759a996dSBjoern A. Zeeb 
5358759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5359759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5360759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5361759a996dSBjoern A. Zeeb 
5362759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5363759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5364759a996dSBjoern A. Zeeb 	while (m != NULL) {
5365759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5366759a996dSBjoern A. Zeeb 
5367759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5368759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5369759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5370759a996dSBjoern A. Zeeb 
5371759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5372759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5373759a996dSBjoern A. Zeeb 		}
5374759a996dSBjoern A. Zeeb 		m_freem(m);
5375759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5376759a996dSBjoern A. Zeeb 	}
5377759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5378759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5379759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5380759a996dSBjoern A. Zeeb 
53816b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5382eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
53838ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5384eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5385eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
53866b4cac81SBjoern A. Zeeb 	IMPROVE();
53876b4cac81SBjoern A. Zeeb }
53886b4cac81SBjoern A. Zeeb 
53896b4cac81SBjoern A. Zeeb void
53906b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
53916b4cac81SBjoern A. Zeeb {
53926b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53936b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53946b4cac81SBjoern A. Zeeb 
53956b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
53966b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
53976b4cac81SBjoern A. Zeeb 
53986b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
53996b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
54006b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
54016b4cac81SBjoern A. Zeeb 
54026b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
54036b4cac81SBjoern A. Zeeb }
54046b4cac81SBjoern A. Zeeb 
54056b4cac81SBjoern A. Zeeb struct ieee80211_hw *
54066b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
54076b4cac81SBjoern A. Zeeb {
54086b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54096b4cac81SBjoern A. Zeeb 
54106b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54116b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
54126b4cac81SBjoern A. Zeeb }
54136b4cac81SBjoern A. Zeeb 
54146b4cac81SBjoern A. Zeeb static void
54156b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
54166b4cac81SBjoern A. Zeeb {
54176b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54186b4cac81SBjoern A. Zeeb 
54196b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54206b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
54216b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
54226b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
54236b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
54246b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
54256b4cac81SBjoern A. Zeeb }
54266b4cac81SBjoern A. Zeeb 
54272e183d99SBjoern A. Zeeb int
54286b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
54296b4cac81SBjoern A. Zeeb {
54306b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54316b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5432c5b96b3eSBjoern A. Zeeb 	int band, i;
54336b4cac81SBjoern A. Zeeb 
54346b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54356b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54366b4cac81SBjoern A. Zeeb 
5437652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5438652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5439652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5440652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5441652e22d3SBjoern A. Zeeb 
54426b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
54436b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
54446b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
54456b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
54466b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
54476b4cac81SBjoern A. Zeeb 		/* We take the first one. */
54486b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
54496b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
54506b4cac81SBjoern A. Zeeb 	} else {
54516b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
54526b4cac81SBjoern A. Zeeb 	}
54536b4cac81SBjoern A. Zeeb 
54543d09d310SBjoern A. Zeeb #ifdef __not_yet__
54553d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
54566b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
54573d09d310SBjoern A. Zeeb #endif
54586b4cac81SBjoern A. Zeeb 
54596b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
54606b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
54616b4cac81SBjoern A. Zeeb 
54626b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
54636b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
54646b4cac81SBjoern A. Zeeb 	ic->ic_caps =
54656b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
54666b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
54676b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
54684a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
54696b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
54704a67f1dfSBjoern A. Zeeb #endif
54716b4cac81SBjoern A. Zeeb #if 0
54726b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
54736b4cac81SBjoern A. Zeeb #endif
54746b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
54756b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
54766b4cac81SBjoern A. Zeeb 	    ;
54776b4cac81SBjoern A. Zeeb #if 0
54786b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
54796b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
54806b4cac81SBjoern A. Zeeb #endif
5481cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5482cc4e78d5SBjoern A. Zeeb 		/*
5483cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5484cc4e78d5SBjoern A. Zeeb 		 *
5485cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5486cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5487cc4e78d5SBjoern A. Zeeb 		 * the flag.
5488cc4e78d5SBjoern A. Zeeb 		 */
54896b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5490a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
54916b4cac81SBjoern A. Zeeb 	}
54926b4cac81SBjoern A. Zeeb 
5493527687a9SBjoern A. Zeeb 	/*
5494527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5495527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5496527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5497527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5498527687a9SBjoern A. Zeeb 	 */
5499527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5500527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5501527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5502527687a9SBjoern A. Zeeb 
5503527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5504527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5505527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5506527687a9SBjoern A. Zeeb 		}
5507527687a9SBjoern A. Zeeb 	}
5508527687a9SBjoern A. Zeeb 
55096b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5510b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
551111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
55126b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
55136b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
55146b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
55156b4cac81SBjoern A. Zeeb 	}
55166b4cac81SBjoern A. Zeeb #endif
55176b4cac81SBjoern A. Zeeb 
55186b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
55196b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
55206b4cac81SBjoern A. Zeeb 
55216b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
55226b4cac81SBjoern A. Zeeb 
55236b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
55246b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
55256b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
55266b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
55276b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
55286b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
55296b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
55306b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
55316b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
55326b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
55336b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
55346b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
55356b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
55366b4cac81SBjoern A. Zeeb 
5537a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5538a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5539a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5540a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5541a486fbbdSBjoern A. Zeeb 
55426b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
55436b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
55446b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
55456b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
55466b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
55476b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
55486b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
55496b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
55506b4cac81SBjoern A. Zeeb 
55519fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
555286bc7259SBjoern A. Zeeb 	/*
555386bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
555486bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
555586bc7259SBjoern A. Zeeb 	 */
555686bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
55579fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
55589fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
55599fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
55609fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
55619fb91463SBjoern A. Zeeb 
55629fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
55639fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
55649fb91463SBjoern A. Zeeb 
55659fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
55669fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
55679fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
55689fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
55699fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
55709fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
55719fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
55729fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
55739fb91463SBjoern A. Zeeb 
55749fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
55759fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
55769fb91463SBjoern A. Zeeb 
55779fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
55789fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
55799fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
55809fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
558186bc7259SBjoern A. Zeeb 	}
55829fb91463SBjoern A. Zeeb #endif
55839fb91463SBjoern A. Zeeb 
55846b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
55856b4cac81SBjoern A. Zeeb 
5586c5b96b3eSBjoern A. Zeeb 	/*
5587c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5588c5b96b3eSBjoern A. Zeeb 	 * expect one.
5589d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5590d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5591c5b96b3eSBjoern A. Zeeb 	 */
5592d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5593f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5594c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5595c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5596c5b96b3eSBjoern A. Zeeb 
5597c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5598c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5599c5b96b3eSBjoern A. Zeeb 			continue;
5600c5b96b3eSBjoern A. Zeeb 
5601d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5602d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5603d9945d78SBjoern A. Zeeb 
5604f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5605f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5606f454a4a1SBjoern A. Zeeb 			continue;
5607f454a4a1SBjoern A. Zeeb 
5608c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5609c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5610c5b96b3eSBjoern A. Zeeb 
5611c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5612c5b96b3eSBjoern A. Zeeb 				continue;
5613c5b96b3eSBjoern A. Zeeb 
56144a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
56159fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
561611450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
56179fb91463SBjoern A. Zeeb #endif
5618c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5619c5b96b3eSBjoern A. Zeeb 			break;
5620c5b96b3eSBjoern A. Zeeb 		}
5621c5b96b3eSBjoern A. Zeeb 	}
5622c5b96b3eSBjoern A. Zeeb 
5623d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5624d9945d78SBjoern A. Zeeb 
5625d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5626d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5627d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5628d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5629d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5630d9945d78SBjoern A. Zeeb 	}
5631d9945d78SBjoern A. Zeeb 
5632d9945d78SBjoern A. Zeeb 	/*
5633d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5634d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5635d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5636d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5637d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5638d9945d78SBjoern A. Zeeb 	 */
5639d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5640d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5641d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
564278ca45dfSBjoern A. Zeeb 
564378ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5644d9945d78SBjoern A. Zeeb 		/*
564578ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
564678ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
564778ca45dfSBjoern A. Zeeb 		 * space in.
5648d9945d78SBjoern A. Zeeb 		 */
5649d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
565078ca45dfSBjoern A. Zeeb 	}
5651d9945d78SBjoern A. Zeeb 
56529fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
56539fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
56549fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
56559fb91463SBjoern A. Zeeb #endif
56569fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
56571f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
56589fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
56599fb91463SBjoern A. Zeeb #endif
56609fb91463SBjoern A. Zeeb 
5661d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
566278ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
566378ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
566478ca45dfSBjoern A. Zeeb 			goto err;
5665d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
566678ca45dfSBjoern A. Zeeb 	}
5667d9945d78SBjoern A. Zeeb 
56686b4cac81SBjoern A. Zeeb 	if (bootverbose)
56696b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
56702e183d99SBjoern A. Zeeb 
56712e183d99SBjoern A. Zeeb 	return (0);
567278ca45dfSBjoern A. Zeeb err:
567378ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
567478ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
56756b4cac81SBjoern A. Zeeb }
56766b4cac81SBjoern A. Zeeb 
56776b4cac81SBjoern A. Zeeb void
56786b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
56796b4cac81SBjoern A. Zeeb {
56806b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56816b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
56826b4cac81SBjoern A. Zeeb 
56836b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
56846b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
56856b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
56866b4cac81SBjoern A. Zeeb }
56876b4cac81SBjoern A. Zeeb 
56886b4cac81SBjoern A. Zeeb void
56896b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
56906b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
56916b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
56926b4cac81SBjoern A. Zeeb     void *arg)
56936b4cac81SBjoern A. Zeeb {
56946b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
56956b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
56966b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
569761a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
56986b4cac81SBjoern A. Zeeb 
56996b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57006b4cac81SBjoern A. Zeeb 
57016b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
57026b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
570361a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5704adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
57056b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
57066b4cac81SBjoern A. Zeeb 		    __func__, flags);
57076b4cac81SBjoern A. Zeeb 	}
57086b4cac81SBjoern A. Zeeb 
5709adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
57106b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
571161a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
57126b4cac81SBjoern A. Zeeb 
57136b4cac81SBjoern A. Zeeb 	if (atomic)
57148891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
57156b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
57166b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
57176b4cac81SBjoern A. Zeeb 
57186b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
57196b4cac81SBjoern A. Zeeb 
57206b4cac81SBjoern A. Zeeb 		/*
57216b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
57226b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
57236b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
57246b4cac81SBjoern A. Zeeb 		 * driver does not know about.
57256b4cac81SBjoern A. Zeeb 		 */
57266b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
57276b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
57286b4cac81SBjoern A. Zeeb 			continue;
57296b4cac81SBjoern A. Zeeb 
57306b4cac81SBjoern A. Zeeb 		/*
573161a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
573261a68e50SBjoern A. Zeeb 		 * if we haven't yet.
573361a68e50SBjoern A. Zeeb 		 */
573461a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
573561a68e50SBjoern A. Zeeb 			continue;
573661a68e50SBjoern A. Zeeb 
573761a68e50SBjoern A. Zeeb 		/*
57386b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
57396b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
57406b4cac81SBjoern A. Zeeb 		 */
57416b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
57426b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
57436b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
57446b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
57456b4cac81SBjoern A. Zeeb 	}
57466b4cac81SBjoern A. Zeeb 	if (atomic)
57478891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
57486b4cac81SBjoern A. Zeeb }
57496b4cac81SBjoern A. Zeeb 
575011db70b6SBjoern A. Zeeb static void
575111db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
575211db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
575311db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
575411db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
575511db70b6SBjoern A. Zeeb     void *arg)
575611db70b6SBjoern A. Zeeb {
575711db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
575811db70b6SBjoern A. Zeeb 		return;
575911db70b6SBjoern A. Zeeb 
576011db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
576111db70b6SBjoern A. Zeeb 		return;
576211db70b6SBjoern A. Zeeb 
576311db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
576411db70b6SBjoern A. Zeeb }
576511db70b6SBjoern A. Zeeb 
57666b4cac81SBjoern A. Zeeb void
57676b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
57686b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
57696b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
57706b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
577111db70b6SBjoern A. Zeeb     void *arg, bool rcu)
57726b4cac81SBjoern A. Zeeb {
577311db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
577411db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57756b4cac81SBjoern A. Zeeb 
577611db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
577711db70b6SBjoern A. Zeeb 
577811db70b6SBjoern A. Zeeb 	if (rcu) {
5779a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5780a8f735a6SBjoern A. Zeeb 
578111db70b6SBjoern A. Zeeb 		if (vif == NULL) {
578211db70b6SBjoern A. Zeeb 			TODO();
578311db70b6SBjoern A. Zeeb 		} else {
5784a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
578511db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
578611db70b6SBjoern A. Zeeb 				    keyix++)
578711db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
578811db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
578911db70b6SBjoern A. Zeeb 			}
579011db70b6SBjoern A. Zeeb 		}
579111db70b6SBjoern A. Zeeb 	} else {
579211db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
579311db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
579411db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
579511db70b6SBjoern A. Zeeb 
579611db70b6SBjoern A. Zeeb 		if (vif == NULL) {
579711db70b6SBjoern A. Zeeb 			TODO();
579811db70b6SBjoern A. Zeeb 		} else {
5799a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
580011db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
580111db70b6SBjoern A. Zeeb 				    keyix++)
580211db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
580311db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
580411db70b6SBjoern A. Zeeb 			}
580511db70b6SBjoern A. Zeeb 		}
580611db70b6SBjoern A. Zeeb 	}
58076b4cac81SBjoern A. Zeeb }
58086b4cac81SBjoern A. Zeeb 
58096b4cac81SBjoern A. Zeeb void
58106b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
58116b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
58126b4cac81SBjoern A. Zeeb 	void *),
58136b4cac81SBjoern A. Zeeb     void *arg)
58146b4cac81SBjoern A. Zeeb {
5815c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5816c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5817c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5818c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
58196b4cac81SBjoern A. Zeeb 
5820c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5821c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5822c5e25798SBjoern A. Zeeb 
5823c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5824c5e25798SBjoern A. Zeeb 
5825c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5826c5e25798SBjoern A. Zeeb 
5827c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5828c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5829c5e25798SBjoern A. Zeeb 
5830c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5831c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5832c5e25798SBjoern A. Zeeb 			continue;
5833c5e25798SBjoern A. Zeeb 
5834c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5835c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5836c5e25798SBjoern A. Zeeb 			continue;
5837c5e25798SBjoern A. Zeeb 
5838d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5839c5e25798SBjoern A. Zeeb 	}
5840c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58416b4cac81SBjoern A. Zeeb }
58426b4cac81SBjoern A. Zeeb 
58436b4cac81SBjoern A. Zeeb void
58446b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
58456b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
58466b4cac81SBjoern A. Zeeb {
58476b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58486b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58496b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
58506b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
58516b4cac81SBjoern A. Zeeb 
58526b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
58536b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
58546b4cac81SBjoern A. Zeeb 
58556b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
58566b4cac81SBjoern A. Zeeb 
58578891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
58586b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
58596b4cac81SBjoern A. Zeeb 
5860a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
5861a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
58626b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
58636b4cac81SBjoern A. Zeeb 				continue;
58646b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
58656b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
58666b4cac81SBjoern A. Zeeb 		}
5867a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
58686b4cac81SBjoern A. Zeeb 	}
58698891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58706b4cac81SBjoern A. Zeeb }
58716b4cac81SBjoern A. Zeeb 
5872673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5873673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5874673d62fcSBjoern A. Zeeb {
5875673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5876673d62fcSBjoern A. Zeeb 
5877673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5878673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5879673d62fcSBjoern A. Zeeb 	return (regd);
5880673d62fcSBjoern A. Zeeb }
5881673d62fcSBjoern A. Zeeb 
58826b4cac81SBjoern A. Zeeb int
58836b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
58846b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
58856b4cac81SBjoern A. Zeeb {
58866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
58876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
58886b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
58896b4cac81SBjoern A. Zeeb 
58906b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
58916b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
58926b4cac81SBjoern A. Zeeb 
58936b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
58946b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
58956b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
58966b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
58976b4cac81SBjoern A. Zeeb 	}
58986b4cac81SBjoern A. Zeeb 
58996b4cac81SBjoern A. Zeeb 	TODO();
59006b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
59016b4cac81SBjoern A. Zeeb 
59026b4cac81SBjoern A. Zeeb 	return (0);
59036b4cac81SBjoern A. Zeeb }
59046b4cac81SBjoern A. Zeeb 
59056b4cac81SBjoern A. Zeeb void
59066b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
59076b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
59086b4cac81SBjoern A. Zeeb {
59096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59106b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59116b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
59126b4cac81SBjoern A. Zeeb 
59136b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
59146b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59156b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
59166b4cac81SBjoern A. Zeeb 
59176b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
59186b4cac81SBjoern A. Zeeb 
59198ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
59206b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
59216b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5922a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
59236b4cac81SBjoern A. Zeeb 	wakeup(lhw);
59248ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
59256b4cac81SBjoern A. Zeeb 
59266b4cac81SBjoern A. Zeeb 	return;
59276b4cac81SBjoern A. Zeeb }
59286b4cac81SBjoern A. Zeeb 
5929759a996dSBjoern A. Zeeb static void
5930759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5931759a996dSBjoern A. Zeeb {
5932759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5933759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5934759a996dSBjoern A. Zeeb 	int ok;
5935759a996dSBjoern A. Zeeb 
5936759a996dSBjoern A. Zeeb 	ni = NULL;
5937759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5938759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5939759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
5940759a996dSBjoern A. Zeeb 
5941759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5942759a996dSBjoern A. Zeeb 		ni = rxni->ni;
5943759a996dSBjoern A. Zeeb 	}
5944759a996dSBjoern A. Zeeb 
5945759a996dSBjoern A. Zeeb 	if (ni != NULL) {
5946759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
5947759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
5948759a996dSBjoern A. Zeeb 		if (ok < 0)
5949759a996dSBjoern A. Zeeb 			m_freem(m);
5950759a996dSBjoern A. Zeeb 	} else {
5951759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5952759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
5953759a996dSBjoern A. Zeeb 	}
5954759a996dSBjoern A. Zeeb 
5955759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5956759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5957bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5958759a996dSBjoern A. Zeeb #endif
5959759a996dSBjoern A. Zeeb }
5960759a996dSBjoern A. Zeeb 
5961759a996dSBjoern A. Zeeb static void
5962759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5963759a996dSBjoern A. Zeeb {
5964759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
5965759a996dSBjoern A. Zeeb 	struct mbufq mq;
5966759a996dSBjoern A. Zeeb 	struct mbuf *m;
5967759a996dSBjoern A. Zeeb 
5968759a996dSBjoern A. Zeeb 	lhw = ctx;
5969759a996dSBjoern A. Zeeb 
5970759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5971759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5972bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5973bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5974759a996dSBjoern A. Zeeb #endif
5975759a996dSBjoern A. Zeeb 
5976759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
5977759a996dSBjoern A. Zeeb 
5978759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5979759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
5980759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5981759a996dSBjoern A. Zeeb 
5982759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
5983759a996dSBjoern A. Zeeb 	while (m != NULL) {
5984759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5985759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
5986759a996dSBjoern A. Zeeb 	}
5987759a996dSBjoern A. Zeeb }
5988759a996dSBjoern A. Zeeb 
598949010ba7SBjoern A. Zeeb static void
599049010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
599149010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
599249010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
599349010ba7SBjoern A. Zeeb     uint8_t *rssip)
599449010ba7SBjoern A. Zeeb {
599549010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
599649010ba7SBjoern A. Zeeb 	int i;
599749010ba7SBjoern A. Zeeb 	uint8_t rssi;
599849010ba7SBjoern A. Zeeb 
599949010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
600049010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
600149010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
600249010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
600349010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
600449010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
600549010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
600649010ba7SBjoern A. Zeeb 	else
600749010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
600849010ba7SBjoern A. Zeeb 	/*
600949010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
601049010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
601149010ba7SBjoern A. Zeeb 	 */
601249010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
601349010ba7SBjoern A. Zeeb 	if (rssip != NULL)
601449010ba7SBjoern A. Zeeb 		*rssip = rssi;
601549010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
601649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
601749010ba7SBjoern A. Zeeb 	rx_stats->c_band =
601849010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
601949010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
602049010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
602149010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
602249010ba7SBjoern A. Zeeb 
602349010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
602449010ba7SBjoern A. Zeeb 
602549010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
602649010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
602749010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
602849010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
602949010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
603049010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
603149010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
603249010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
603349010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
603449010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
603549010ba7SBjoern A. Zeeb 	}
603649010ba7SBjoern A. Zeeb 
603749010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
603849010ba7SBjoern A. Zeeb 		int cc;
603949010ba7SBjoern A. Zeeb 		int8_t crssi;
604049010ba7SBjoern A. Zeeb 
604149010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
604249010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
604349010ba7SBjoern A. Zeeb 
604449010ba7SBjoern A. Zeeb 		cc = 0;
604549010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
604649010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
604749010ba7SBjoern A. Zeeb 				continue;
604849010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
604949010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
605049010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
605149010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
605249010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
605349010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
605449010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
605549010ba7SBjoern A. Zeeb 			cc++;
605649010ba7SBjoern A. Zeeb 		}
605749010ba7SBjoern A. Zeeb 		if (cc > 0)
605849010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
605949010ba7SBjoern A. Zeeb 	}
606049010ba7SBjoern A. Zeeb 
606149010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
606249010ba7SBjoern A. Zeeb 
606349010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
606449010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
606549010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
606649010ba7SBjoern A. Zeeb 		if (supband != NULL)
606749010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
606849010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
606949010ba7SBjoern A. Zeeb 		break;
607049010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
607149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
607249010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
607349010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
607449010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
607549010ba7SBjoern A. Zeeb 		break;
607649010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
607749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
607849010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
607949010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
608049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
608149010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
608249010ba7SBjoern A. Zeeb 		break;
608349010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
608449010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
608549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
608649010ba7SBjoern A. Zeeb 		break;
608749010ba7SBjoern A. Zeeb 	}
608849010ba7SBjoern A. Zeeb 
608949010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
609049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
609149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
609249010ba7SBjoern A. Zeeb 		break;
609349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
609449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
609549010ba7SBjoern A. Zeeb 		break;
609649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
609749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
609849010ba7SBjoern A. Zeeb 		break;
609949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
610049010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
610149010ba7SBjoern A. Zeeb 		break;
610249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
610349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
610449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
610549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
610649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
610749010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
610849010ba7SBjoern A. Zeeb 		break;
610949010ba7SBjoern A. Zeeb 	}
611049010ba7SBjoern A. Zeeb 
611149010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
611249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
611349010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
611449010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
611549010ba7SBjoern A. Zeeb 
611649010ba7SBjoern A. Zeeb 	/*
611749010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
611849010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
611949010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
612049010ba7SBjoern A. Zeeb 	 */
612149010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
612249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
612349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
612449010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
612549010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
612649010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
612749010ba7SBjoern A. Zeeb 	}
612849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
612949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
613049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
613149010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
613249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
613349010ba7SBjoern A. Zeeb 	}
613449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
613549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
613649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
613749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
613849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
613949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
614049010ba7SBjoern A. Zeeb #endif
614149010ba7SBjoern A. Zeeb }
614249010ba7SBjoern A. Zeeb 
6143e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
61446b4cac81SBjoern A. Zeeb void
61456b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6146e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6147e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
61486b4cac81SBjoern A. Zeeb {
61496b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
61506b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
61516b4cac81SBjoern A. Zeeb 	struct mbuf *m;
61526b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
61536b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
61546b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
61556b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
61566b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
61576b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
61586b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
61599d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
616049010ba7SBjoern A. Zeeb 	uint8_t rssi;
6161c0cadd99SBjoern A. Zeeb 	bool is_beacon;
61626b4cac81SBjoern A. Zeeb 
61636b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
61646b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
61656b4cac81SBjoern A. Zeeb 		IMPROVE();
61666b4cac81SBjoern A. Zeeb 		goto err;
61676b4cac81SBjoern A. Zeeb 	}
61686b4cac81SBjoern A. Zeeb 
61696b4cac81SBjoern A. Zeeb 	/*
61706b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
61716b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
61726b4cac81SBjoern A. Zeeb 	 */
61736b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
61746b4cac81SBjoern A. Zeeb 	if (m == NULL)
61756b4cac81SBjoern A. Zeeb 		goto err;
61766b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
61776b4cac81SBjoern A. Zeeb 
61786b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
61796b4cac81SBjoern A. Zeeb 	offset = m->m_len;
61806b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
61816b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
61826b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
61836b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
61846b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
61856b4cac81SBjoern A. Zeeb 	}
61866b4cac81SBjoern A. Zeeb 
61876b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
61886b4cac81SBjoern A. Zeeb 
61896b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6190c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6191c0cadd99SBjoern A. Zeeb 
6192c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
61939d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
61946b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
61956b4cac81SBjoern A. Zeeb 
61969d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
61976b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6198c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
61996b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
62006b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
62016b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6202c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
62036b4cac81SBjoern A. Zeeb 
62049d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
62056b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
62066b4cac81SBjoern A. Zeeb 
62076b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
62089d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6209f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
621060970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
62116b4cac81SBjoern A. Zeeb 			__func__,
6212c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6213c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
62146b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6215f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
62166b4cac81SBjoern A. Zeeb 			rx_status->freq,
62176b4cac81SBjoern A. Zeeb 			rx_status->bw,
62186b4cac81SBjoern A. Zeeb 			rx_status->encoding,
62196b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
62206b4cac81SBjoern A. Zeeb 			rx_status->band,
62216b4cac81SBjoern A. Zeeb 			rx_status->chains,
62226b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
62236b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
62246b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
622560970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
62266b4cac81SBjoern A. Zeeb 			rx_status->signal,
62276b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
62286b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
62296b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
62306b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
62316b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
62326b4cac81SBjoern A. Zeeb 			rx_status->nss,
62336b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
62346b4cac81SBjoern A. Zeeb no_trace_beacons:
62356b4cac81SBjoern A. Zeeb #endif
62366b4cac81SBjoern A. Zeeb 
623749010ba7SBjoern A. Zeeb 	rssi = 0;
623849010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
62396b4cac81SBjoern A. Zeeb 
62406b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
62416b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
62426b4cac81SBjoern A. Zeeb 
62436b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
62446b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6245dbc06dd9SBjoern A. Zeeb 		m_freem(m);
62466b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
62476b4cac81SBjoern A. Zeeb 		goto err;
62486b4cac81SBjoern A. Zeeb 	}
62496b4cac81SBjoern A. Zeeb 
625058246901SBjoern A. Zeeb 	lsta = NULL;
62516b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
62526b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
62536b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
62546b4cac81SBjoern A. Zeeb 	} else {
6255c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6256c8dafefaSBjoern A. Zeeb 
62576b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
62586b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
62596b4cac81SBjoern A. Zeeb 		if (ni != NULL)
62606b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
62616b4cac81SBjoern A. Zeeb 	}
62626b4cac81SBjoern A. Zeeb 
62636b4cac81SBjoern A. Zeeb 	if (ni != NULL)
62646b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
62656b4cac81SBjoern A. Zeeb 	else
62666b4cac81SBjoern A. Zeeb 		/*
62676b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
62686b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
62696b4cac81SBjoern A. Zeeb 		 */
62706b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
62716b4cac81SBjoern A. Zeeb 
62729d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
62739d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6274bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6275c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6276c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
62779d9ba2b7SBjoern A. Zeeb #endif
62786b4cac81SBjoern A. Zeeb 
6279c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6280c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6281c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6282c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6283c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6284c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6285c8dafefaSBjoern A. Zeeb 
6286c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6287c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6288c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6289c8dafefaSBjoern A. Zeeb 
6290c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6291c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6292c8dafefaSBjoern A. Zeeb 
6293c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6294c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6295c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6296c8dafefaSBjoern A. Zeeb 		/*
6297c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6298c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6299c8dafefaSBjoern A. Zeeb 		 */
6300c8dafefaSBjoern A. Zeeb skip_device_ts:
63019fb91463SBjoern A. Zeeb 		;
63029fb91463SBjoern A. Zeeb 	}
6303c8dafefaSBjoern A. Zeeb 
63046b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
63056b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
63066b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
63076b4cac81SBjoern A. Zeeb 
63086b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
63096b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
63106b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
63116b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
63126b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
63136b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
63146b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
63156b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
63166b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
63176b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
63186b4cac81SBjoern A. Zeeb #endif
63196b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
63206b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
63216b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
63226b4cac81SBjoern A. Zeeb 		IMPROVE();
63236b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
63246b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
63256b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
63266b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6327170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
63286b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
63296b4cac81SBjoern A. Zeeb 	}
63306b4cac81SBjoern A. Zeeb 
63316b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
63326b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
63336b4cac81SBjoern A. Zeeb 
6334e30e05d3SBjoern A. Zeeb #if 0
6335e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6336e30e05d3SBjoern A. Zeeb 		/*
6337e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6338e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6339e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6340e30e05d3SBjoern A. Zeeb 		*/
6341e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6342e30e05d3SBjoern A. Zeeb 	}
6343e30e05d3SBjoern A. Zeeb #endif
6344e30e05d3SBjoern A. Zeeb 
6345759a996dSBjoern A. Zeeb 	/*
6346759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6347759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6348759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6349759a996dSBjoern A. Zeeb 	 */
63506b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6351759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6352759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6353759a996dSBjoern A. Zeeb 
6354759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6355759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6356759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6357759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6358759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6359759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6360759a996dSBjoern A. Zeeb 		}
63616b4cac81SBjoern A. Zeeb 	}
63626b4cac81SBjoern A. Zeeb 
6363759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6364759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6365759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6366759a996dSBjoern A. Zeeb 		m_freem(m);
6367759a996dSBjoern A. Zeeb 		goto err;
6368759a996dSBjoern A. Zeeb 	}
6369759a996dSBjoern A. Zeeb 
6370759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6371759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6372759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
63736b4cac81SBjoern A. Zeeb 
63746b4cac81SBjoern A. Zeeb 	IMPROVE();
63756b4cac81SBjoern A. Zeeb 
63766b4cac81SBjoern A. Zeeb err:
63776b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
63786b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
63796b4cac81SBjoern A. Zeeb }
63806b4cac81SBjoern A. Zeeb 
63816b4cac81SBjoern A. Zeeb uint8_t
6382ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
63836b4cac81SBjoern A. Zeeb {
63846b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6385ec190d91SBjoern A. Zeeb 	uint8_t tid;
6386ec190d91SBjoern A. Zeeb 
6387ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6388ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6389ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6390ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
63916b4cac81SBjoern A. Zeeb 
63926b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6393ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6394ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6395ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6396ec190d91SBjoern A. Zeeb 
6397ec190d91SBjoern A. Zeeb 	return (tid);
63986b4cac81SBjoern A. Zeeb }
63996b4cac81SBjoern A. Zeeb 
6400ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6401ac1d519cSBjoern A. Zeeb 
6402ac1d519cSBjoern A. Zeeb static void
6403ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6404ac1d519cSBjoern A. Zeeb {
6405ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6406ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6407ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6408ac1d519cSBjoern A. Zeeb 
6409ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6410ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6411ac1d519cSBjoern A. Zeeb 
6412ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6413ac1d519cSBjoern A. Zeeb 
6414ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6415ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6416ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6417ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6418ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6419ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6420ac1d519cSBjoern A. Zeeb 		return;
6421ac1d519cSBjoern A. Zeeb 	}
6422ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6423ac1d519cSBjoern A. Zeeb 
6424ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6425ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6426ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6427ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6428ac1d519cSBjoern A. Zeeb 
6429ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6430ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6431ac1d519cSBjoern A. Zeeb 
6432ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6433ac1d519cSBjoern A. Zeeb }
6434ac1d519cSBjoern A. Zeeb 
6435ac1d519cSBjoern A. Zeeb void
6436ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6437ac1d519cSBjoern A. Zeeb {
6438ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6439ac1d519cSBjoern A. Zeeb 
6440ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6441ac1d519cSBjoern A. Zeeb 
6442ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6443ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6444ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6445ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6446ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6447ac1d519cSBjoern A. Zeeb 
6448ac1d519cSBjoern A. Zeeb 	/*
6449ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6450ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6451ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6452ac1d519cSBjoern A. Zeeb 	 */
6453ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6454ac1d519cSBjoern A. Zeeb }
6455ac1d519cSBjoern A. Zeeb 
6456ac1d519cSBjoern A. Zeeb void
6457ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6458ac1d519cSBjoern A. Zeeb {
6459ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6460ac1d519cSBjoern A. Zeeb 
6461ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6462ac1d519cSBjoern A. Zeeb 
6463ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6464ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6465ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6466ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6467ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6468ac1d519cSBjoern A. Zeeb }
6469ac1d519cSBjoern A. Zeeb 
6470ac1d519cSBjoern A. Zeeb void
6471ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6472ac1d519cSBjoern A. Zeeb {
6473ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6474ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6475ac1d519cSBjoern A. Zeeb 
6476ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6477ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6478ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6479ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6480ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6481ac1d519cSBjoern A. Zeeb 		return;
6482ac1d519cSBjoern A. Zeeb 	}
6483ac1d519cSBjoern A. Zeeb 
6484ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6485ac1d519cSBjoern A. Zeeb 
6486ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6487ac1d519cSBjoern A. Zeeb 		    entry);
6488ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6489ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6490ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6491ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6492ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6493ac1d519cSBjoern A. Zeeb 			break;
6494ac1d519cSBjoern A. Zeeb 	}
6495ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6496ac1d519cSBjoern A. Zeeb }
6497ac1d519cSBjoern A. Zeeb 
6498ac1d519cSBjoern A. Zeeb void
6499ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6500ac1d519cSBjoern A. Zeeb {
6501ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6502ac1d519cSBjoern A. Zeeb 
6503ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6504ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6505ac1d519cSBjoern A. Zeeb }
6506ac1d519cSBjoern A. Zeeb 
6507ac1d519cSBjoern A. Zeeb void
6508ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6509ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6510ac1d519cSBjoern A. Zeeb {
6511ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6512ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6513ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6514ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6515ac1d519cSBjoern A. Zeeb 	} else {
6516ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6517ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6518ac1d519cSBjoern A. Zeeb 	}
6519ac1d519cSBjoern A. Zeeb }
6520ac1d519cSBjoern A. Zeeb 
6521ac1d519cSBjoern A. Zeeb void
6522ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6523ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6524ac1d519cSBjoern A. Zeeb {
6525ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6526ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6527ac1d519cSBjoern A. Zeeb }
6528ac1d519cSBjoern A. Zeeb 
6529ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6530ac1d519cSBjoern A. Zeeb 
65316b4cac81SBjoern A. Zeeb struct wiphy *
65326b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
65336b4cac81SBjoern A. Zeeb {
65346b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6535ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
65366b4cac81SBjoern A. Zeeb 
65376b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
65386b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
65396b4cac81SBjoern A. Zeeb 		return (NULL);
65406b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
65416b4cac81SBjoern A. Zeeb 
6542ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6543ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6544ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6545ac1d519cSBjoern A. Zeeb 
6546ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6547ac1d519cSBjoern A. Zeeb 
6548ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6549ac1d519cSBjoern A. Zeeb 	TODO();
6550ac1d519cSBjoern A. Zeeb 
6551ac1d519cSBjoern A. Zeeb 	return (wiphy);
65526b4cac81SBjoern A. Zeeb }
65536b4cac81SBjoern A. Zeeb 
65546b4cac81SBjoern A. Zeeb void
65556b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
65566b4cac81SBjoern A. Zeeb {
65576b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
65586b4cac81SBjoern A. Zeeb 
65596b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
65606b4cac81SBjoern A. Zeeb 		return;
65616b4cac81SBjoern A. Zeeb 
6562ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6563ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6564ac1d519cSBjoern A. Zeeb 
65656b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6566ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6567ac1d519cSBjoern A. Zeeb 
65686b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
65696b4cac81SBjoern A. Zeeb }
65706b4cac81SBjoern A. Zeeb 
6571a7c19b8aSBjoern A. Zeeb static uint32_t
6572a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6573a7c19b8aSBjoern A. Zeeb {
6574a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6575a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6576a7c19b8aSBjoern A. Zeeb }
6577a7c19b8aSBjoern A. Zeeb 
6578a7c19b8aSBjoern A. Zeeb static uint32_t
6579a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6580a7c19b8aSBjoern A. Zeeb {
6581a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6582a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6583a7c19b8aSBjoern A. Zeeb }
6584a7c19b8aSBjoern A. Zeeb 
6585a7c19b8aSBjoern A. Zeeb uint32_t
6586a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6587a7c19b8aSBjoern A. Zeeb {
6588a7c19b8aSBjoern A. Zeeb 
6589a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6590a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6591a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6592a7c19b8aSBjoern A. Zeeb 
6593a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6594a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6595a7c19b8aSBjoern A. Zeeb 
6596a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6597a7c19b8aSBjoern A. Zeeb 
6598a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6599a7c19b8aSBjoern A. Zeeb }
6600a7c19b8aSBjoern A. Zeeb 
66016b4cac81SBjoern A. Zeeb uint32_t
66026b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
66036b4cac81SBjoern A. Zeeb     enum nl80211_band band)
66046b4cac81SBjoern A. Zeeb {
66056b4cac81SBjoern A. Zeeb 
66066b4cac81SBjoern A. Zeeb 	switch (band) {
66076b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
66086b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
66096b4cac81SBjoern A. Zeeb 		break;
66106b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
66116b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
66126b4cac81SBjoern A. Zeeb 		break;
66136b4cac81SBjoern A. Zeeb 	default:
66146b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
66156b4cac81SBjoern A. Zeeb 		break;
66166b4cac81SBjoern A. Zeeb 	}
66176b4cac81SBjoern A. Zeeb 
66186b4cac81SBjoern A. Zeeb 	return (0);
66196b4cac81SBjoern A. Zeeb }
66206b4cac81SBjoern A. Zeeb 
66216b4cac81SBjoern A. Zeeb uint32_t
66226b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
66236b4cac81SBjoern A. Zeeb {
66246b4cac81SBjoern A. Zeeb 
66256b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
66266b4cac81SBjoern A. Zeeb }
66276b4cac81SBjoern A. Zeeb 
6628ac867c20SBjoern A. Zeeb #if 0
66296b4cac81SBjoern A. Zeeb static struct lkpi_sta *
66306b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
66316b4cac81SBjoern A. Zeeb {
66326b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
66336b4cac81SBjoern A. Zeeb 
6634a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6635a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
66366b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6637a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
66386b4cac81SBjoern A. Zeeb 			return (lsta);
66396b4cac81SBjoern A. Zeeb 		}
66406b4cac81SBjoern A. Zeeb 	}
6641a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
66426b4cac81SBjoern A. Zeeb 
66436b4cac81SBjoern A. Zeeb 	return (NULL);
66446b4cac81SBjoern A. Zeeb }
6645ac867c20SBjoern A. Zeeb #endif
66466b4cac81SBjoern A. Zeeb 
66476b4cac81SBjoern A. Zeeb struct ieee80211_sta *
66486b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
66496b4cac81SBjoern A. Zeeb {
66506b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6651a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
66526b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
66536b4cac81SBjoern A. Zeeb 
66546b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
66556b4cac81SBjoern A. Zeeb 
6656a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6657a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
66586b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
66596b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6660a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
66616b4cac81SBjoern A. Zeeb 			return (sta);
66626b4cac81SBjoern A. Zeeb 		}
66636b4cac81SBjoern A. Zeeb 	}
6664a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
66656b4cac81SBjoern A. Zeeb 	return (NULL);
66666b4cac81SBjoern A. Zeeb }
66676b4cac81SBjoern A. Zeeb 
66686b4cac81SBjoern A. Zeeb struct ieee80211_sta *
66692e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
66702e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
66716b4cac81SBjoern A. Zeeb {
66726b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
66736b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
66746b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
66756b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
66766b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
66776b4cac81SBjoern A. Zeeb 
66786b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
66796b4cac81SBjoern A. Zeeb 	sta = NULL;
66806b4cac81SBjoern A. Zeeb 
66818891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
66826b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
66836b4cac81SBjoern A. Zeeb 
66846b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
66856b4cac81SBjoern A. Zeeb 
66866b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
66876b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
66886b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
66896b4cac81SBjoern A. Zeeb 			continue;
66906b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
66916b4cac81SBjoern A. Zeeb 		if (sta != NULL)
66926b4cac81SBjoern A. Zeeb 			break;
66936b4cac81SBjoern A. Zeeb 	}
66948891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
66956b4cac81SBjoern A. Zeeb 
66966b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
66976b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
66986b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
66996b4cac81SBjoern A. Zeeb 			return (NULL);
67006b4cac81SBjoern A. Zeeb 	}
67016b4cac81SBjoern A. Zeeb 
67026b4cac81SBjoern A. Zeeb 	return (sta);
67036b4cac81SBjoern A. Zeeb }
67046b4cac81SBjoern A. Zeeb 
67056b4cac81SBjoern A. Zeeb struct sk_buff *
67066b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
67076b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
67086b4cac81SBjoern A. Zeeb {
67096b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
67100cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67116b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
67126b4cac81SBjoern A. Zeeb 
67130cbcfa19SBjoern A. Zeeb 	skb = NULL;
67146b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
67156b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
67166b4cac81SBjoern A. Zeeb 
67170cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
67180cbcfa19SBjoern A. Zeeb 		goto stopped;
67190cbcfa19SBjoern A. Zeeb 
67200cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
67210cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
67220cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
67230cbcfa19SBjoern A. Zeeb 		goto stopped;
67240cbcfa19SBjoern A. Zeeb 	}
67250cbcfa19SBjoern A. Zeeb 
6726eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6727eac3646fSBjoern A. Zeeb 
6728eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
67296b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6730eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
67316b4cac81SBjoern A. Zeeb 
67320cbcfa19SBjoern A. Zeeb stopped:
67336b4cac81SBjoern A. Zeeb 	return (skb);
67346b4cac81SBjoern A. Zeeb }
67356b4cac81SBjoern A. Zeeb 
67366b4cac81SBjoern A. Zeeb void
67376b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
673886220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
67396b4cac81SBjoern A. Zeeb {
67406b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
67416b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
674286220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
67436b4cac81SBjoern A. Zeeb 
67446b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
67456b4cac81SBjoern A. Zeeb 
67466b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6747eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
67486b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
67496b4cac81SBjoern A. Zeeb 		fc++;
67506b4cac81SBjoern A. Zeeb 		bc += skb->len;
67516b4cac81SBjoern A. Zeeb 	}
6752eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
67536b4cac81SBjoern A. Zeeb 	if (frame_cnt)
67546b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
67556b4cac81SBjoern A. Zeeb 	if (byte_cnt)
67566b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
67576b4cac81SBjoern A. Zeeb 
67586b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
67596b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
67606b4cac81SBjoern A. Zeeb 	IMPROVE();
67616b4cac81SBjoern A. Zeeb }
67626b4cac81SBjoern A. Zeeb 
67636b4cac81SBjoern A. Zeeb /*
67646b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
67656b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
67666b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
67676b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
67686b4cac81SBjoern A. Zeeb  */
6769a8397571SBjoern A. Zeeb static void
6770a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
67716b4cac81SBjoern A. Zeeb     int status)
67726b4cac81SBjoern A. Zeeb {
67736b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
67746b4cac81SBjoern A. Zeeb 	struct mbuf *m;
67756b4cac81SBjoern A. Zeeb 
67766b4cac81SBjoern A. Zeeb 	m = skb->m;
67776b4cac81SBjoern A. Zeeb 	skb->m = NULL;
67786b4cac81SBjoern A. Zeeb 
67796b4cac81SBjoern A. Zeeb 	if (m != NULL) {
67806b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
67816b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
67826b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
67836b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
67846b4cac81SBjoern A. Zeeb 	}
6785a8397571SBjoern A. Zeeb }
67866b4cac81SBjoern A. Zeeb 
6787a8397571SBjoern A. Zeeb void
6788a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6789a8397571SBjoern A. Zeeb     int status)
6790a8397571SBjoern A. Zeeb {
6791a8397571SBjoern A. Zeeb 
6792a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
67936b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
67946b4cac81SBjoern A. Zeeb }
67956b4cac81SBjoern A. Zeeb 
6796383b3e8fSBjoern A. Zeeb void
6797a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6798a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6799383b3e8fSBjoern A. Zeeb {
6800a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6801383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6802383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6803383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6804383b3e8fSBjoern A. Zeeb 	int status;
6805383b3e8fSBjoern A. Zeeb 
6806a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6807383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6808383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6809383b3e8fSBjoern A. Zeeb 
6810383b3e8fSBjoern A. Zeeb 		m = skb->m;
6811383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6812383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6813383b3e8fSBjoern A. Zeeb 	} else {
6814383b3e8fSBjoern A. Zeeb 		ni = NULL;
6815383b3e8fSBjoern A. Zeeb 	}
6816383b3e8fSBjoern A. Zeeb 
6817a8397571SBjoern A. Zeeb 	info = txstat->info;
6818383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6819383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6820383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6821383b3e8fSBjoern A. Zeeb 	} else {
6822383b3e8fSBjoern A. Zeeb 		status = 1;
6823383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6824383b3e8fSBjoern A. Zeeb 	}
6825383b3e8fSBjoern A. Zeeb 
6826383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6827e2c5ab09SJohn Baldwin 		int ridx __unused;
6828383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6829383b3e8fSBjoern A. Zeeb 		int old_rate;
6830383b3e8fSBjoern A. Zeeb 
6831383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6832383b3e8fSBjoern A. Zeeb #endif
6833383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6834383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6835383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6836383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6837383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6838383b3e8fSBjoern A. Zeeb 		}
6839383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6840a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6841383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6842383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6843383b3e8fSBjoern A. Zeeb #endif
6844adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6845383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6846383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6847383b3e8fSBjoern A. Zeeb 		}
6848383b3e8fSBjoern A. Zeeb 
6849383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6850383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6851383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6852383b3e8fSBjoern A. Zeeb 
6853383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6854383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6855383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6856383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6857383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6858383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6859383b3e8fSBjoern A. Zeeb 		}
6860383b3e8fSBjoern A. Zeeb #endif
6861383b3e8fSBjoern A. Zeeb 	}
6862383b3e8fSBjoern A. Zeeb 
6863383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6864383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6865383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6866383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6867383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6868383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6869adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6870383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6871383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6872383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6873383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6874383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6875383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6876383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6877383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6878383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6879383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6880383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6881383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6882383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6883adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6884383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6885383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6886383b3e8fSBjoern A. Zeeb #endif
6887383b3e8fSBjoern A. Zeeb 
6888a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6889a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6890a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6891a8397571SBjoern A. Zeeb 	} else {
6892383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6893383b3e8fSBjoern A. Zeeb 	}
6894a8397571SBjoern A. Zeeb }
6895a8397571SBjoern A. Zeeb 
6896a8397571SBjoern A. Zeeb void
6897a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6898a8397571SBjoern A. Zeeb {
6899a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6900a8397571SBjoern A. Zeeb 
6901a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6902a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6903a8397571SBjoern A. Zeeb 	status.skb = skb;
6904a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6905a8397571SBjoern A. Zeeb 
6906a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6907a8397571SBjoern A. Zeeb }
6908383b3e8fSBjoern A. Zeeb 
69096b4cac81SBjoern A. Zeeb /*
69106b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
69116b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
69126b4cac81SBjoern A. Zeeb  * mbufs this should go away.
69136b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
69146b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
69156b4cac81SBjoern A. Zeeb  */
69166b4cac81SBjoern A. Zeeb static void
69176b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
69186b4cac81SBjoern A. Zeeb {
69196b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
69206b4cac81SBjoern A. Zeeb 	struct mbuf *m;
69216b4cac81SBjoern A. Zeeb 
69226b4cac81SBjoern A. Zeeb 	if (p == NULL)
69236b4cac81SBjoern A. Zeeb 		return;
69246b4cac81SBjoern A. Zeeb 
69256b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
69266b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
69276b4cac81SBjoern A. Zeeb 
69286b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
69296b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
69306b4cac81SBjoern A. Zeeb 	if (ni != NULL)
69316b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
69326b4cac81SBjoern A. Zeeb 	m_freem(m);
69336b4cac81SBjoern A. Zeeb }
69346b4cac81SBjoern A. Zeeb 
69356b4cac81SBjoern A. Zeeb void
69366b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
69376b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
69386b4cac81SBjoern A. Zeeb {
69396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
69406b4cac81SBjoern A. Zeeb 
69416b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
69426b4cac81SBjoern A. Zeeb 	IMPROVE();
69436b4cac81SBjoern A. Zeeb 
69446b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
69456b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
69466b4cac81SBjoern A. Zeeb }
69476b4cac81SBjoern A. Zeeb 
69486b4cac81SBjoern A. Zeeb void
69496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
69506b4cac81SBjoern A. Zeeb     struct work_struct *w)
69516b4cac81SBjoern A. Zeeb {
69526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
69536b4cac81SBjoern A. Zeeb 
69546b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
69556b4cac81SBjoern A. Zeeb 	IMPROVE();
69566b4cac81SBjoern A. Zeeb 
69576b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
69586b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
69596b4cac81SBjoern A. Zeeb }
69606b4cac81SBjoern A. Zeeb 
69616b4cac81SBjoern A. Zeeb struct sk_buff *
6962ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
6963ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
6964ade774b1SBjoern A. Zeeb {
6965ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
6966ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
6967ade774b1SBjoern A. Zeeb 	uint8_t *p;
6968ade774b1SBjoern A. Zeeb 	size_t len;
6969ade774b1SBjoern A. Zeeb 
6970ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
6971ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
6972ade774b1SBjoern A. Zeeb 
6973ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
6974ade774b1SBjoern A. Zeeb 	if (skb == NULL)
6975ade774b1SBjoern A. Zeeb 		return (NULL);
6976ade774b1SBjoern A. Zeeb 
6977ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
6978ade774b1SBjoern A. Zeeb 
6979ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
6980ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
6981ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
6982ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
6983ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
6984ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
6985ade774b1SBjoern A. Zeeb 
6986ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
6987ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
6988ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
6989ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
6990ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
6991ade774b1SBjoern A. Zeeb 
6992ade774b1SBjoern A. Zeeb 	return (skb);
6993ade774b1SBjoern A. Zeeb }
6994ade774b1SBjoern A. Zeeb 
6995ade774b1SBjoern A. Zeeb struct sk_buff *
69966b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
69976b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
69986b4cac81SBjoern A. Zeeb {
69996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70006b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70016b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70026b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
70036b4cac81SBjoern A. Zeeb 	uint16_t v;
70046b4cac81SBjoern A. Zeeb 
70056b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
70066b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70076b4cac81SBjoern A. Zeeb 		return (NULL);
70086b4cac81SBjoern A. Zeeb 
70096b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
70106b4cac81SBjoern A. Zeeb 
70116b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70126b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70136b4cac81SBjoern A. Zeeb 
70146b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
70156b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
70166b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7017616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
70186b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
70196b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
70206b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
70216b4cac81SBjoern A. Zeeb 
70226b4cac81SBjoern A. Zeeb 	return (skb);
70236b4cac81SBjoern A. Zeeb }
70246b4cac81SBjoern A. Zeeb 
70256b4cac81SBjoern A. Zeeb struct sk_buff *
70266b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7027adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
70286b4cac81SBjoern A. Zeeb {
70296b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70306b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70316b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70326b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
70336b4cac81SBjoern A. Zeeb 
7034adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7035adff403fSBjoern A. Zeeb 
70366b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
70376b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70386b4cac81SBjoern A. Zeeb 		return (NULL);
70396b4cac81SBjoern A. Zeeb 
70406b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
70416b4cac81SBjoern A. Zeeb 
70426b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70436b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70446b4cac81SBjoern A. Zeeb 
70456b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
70466b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
70476b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
70486b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
70496b4cac81SBjoern A. Zeeb 
70506b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
70516b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
70526b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
70536b4cac81SBjoern A. Zeeb 
70546b4cac81SBjoern A. Zeeb 	return (skb);
70556b4cac81SBjoern A. Zeeb }
70566b4cac81SBjoern A. Zeeb 
70576b4cac81SBjoern A. Zeeb struct wireless_dev *
70586b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
70596b4cac81SBjoern A. Zeeb {
70606b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70616b4cac81SBjoern A. Zeeb 
70626b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70636b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
70646b4cac81SBjoern A. Zeeb }
70656b4cac81SBjoern A. Zeeb 
70666b4cac81SBjoern A. Zeeb void
70676b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
70686b4cac81SBjoern A. Zeeb {
70696b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70706b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70716b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
70726b4cac81SBjoern A. Zeeb 	int arg;
70736b4cac81SBjoern A. Zeeb 
70746b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70756b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70766b4cac81SBjoern A. Zeeb 
70776b4cac81SBjoern A. Zeeb 	/*
7078f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
70796b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
70806b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
70816b4cac81SBjoern A. Zeeb 	 */
7082f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7083bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
70846b4cac81SBjoern A. Zeeb 
7085018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7086018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
70876b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
70886b4cac81SBjoern A. Zeeb }
70896b4cac81SBjoern A. Zeeb 
7090bb81db90SBjoern A. Zeeb void
7091bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7092bb81db90SBjoern A. Zeeb {
7093bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7094bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7095bb81db90SBjoern A. Zeeb 
7096bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7097bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7098bb81db90SBjoern A. Zeeb 
7099bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7100bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71013540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7102bb81db90SBjoern A. Zeeb }
7103bb81db90SBjoern A. Zeeb 
71045edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
71055edde07cSBjoern A. Zeeb 
71065a9a0d78SBjoern A. Zeeb void
71075a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
71085a9a0d78SBjoern A. Zeeb {
71095a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71105a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71115a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
71125a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
71135a9a0d78SBjoern A. Zeeb 
71145a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
71155a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
71165a9a0d78SBjoern A. Zeeb 
71175a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
71185a9a0d78SBjoern A. Zeeb 
71195a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
71205a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
71215a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
71225a9a0d78SBjoern A. Zeeb 	else
71235a9a0d78SBjoern A. Zeeb 		ac_count = 1;
71245a9a0d78SBjoern A. Zeeb 
71255a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71265a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71275a9a0d78SBjoern A. Zeeb 
71285a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71295a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
71305a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
71315a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
71320d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
71335a9a0d78SBjoern A. Zeeb 				/*
71345a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
71355a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
71365a9a0d78SBjoern A. Zeeb 				 */
71370d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
71380d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
71395a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
71405a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
71415a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
71425a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
71430d2cb6a6SBjoern A. Zeeb #endif
71445a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
71455a9a0d78SBjoern A. Zeeb 			}
71465a9a0d78SBjoern A. Zeeb 		}
71475a9a0d78SBjoern A. Zeeb 	}
71485a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
71495a9a0d78SBjoern A. Zeeb }
71505a9a0d78SBjoern A. Zeeb 
71515a9a0d78SBjoern A. Zeeb void
71525a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
71535a9a0d78SBjoern A. Zeeb {
71545a9a0d78SBjoern A. Zeeb 	int i;
71555a9a0d78SBjoern A. Zeeb 
71565a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
71575a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
71585a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
71595a9a0d78SBjoern A. Zeeb }
71605a9a0d78SBjoern A. Zeeb 
71615a9a0d78SBjoern A. Zeeb 
71625a9a0d78SBjoern A. Zeeb static void
71635a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
71645a9a0d78SBjoern A. Zeeb {
71655a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71665a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71675a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
71685a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
71695a9a0d78SBjoern A. Zeeb 
71705a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
71715a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
71725a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
71735a9a0d78SBjoern A. Zeeb 	else
71745a9a0d78SBjoern A. Zeeb 		ac_count = 1;
71755a9a0d78SBjoern A. Zeeb 
71765a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
71775a9a0d78SBjoern A. Zeeb 
71785a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
71795a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71805a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71815a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
71825a9a0d78SBjoern A. Zeeb 
71835a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71845a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
71855a9a0d78SBjoern A. Zeeb 
71865a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
71875a9a0d78SBjoern A. Zeeb 
71885a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
71895a9a0d78SBjoern A. Zeeb 
71900d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
71915a9a0d78SBjoern A. Zeeb 				/*
71925a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
71935a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
71945a9a0d78SBjoern A. Zeeb 				 */
71950d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
71960d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
71975a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
71985a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
71995a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
72005a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
72010d2cb6a6SBjoern A. Zeeb #endif
72025a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
72035a9a0d78SBjoern A. Zeeb 
7204a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7205a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72065a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
72075a9a0d78SBjoern A. Zeeb 
72085a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
72095a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
72105a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
72115a9a0d78SBjoern A. Zeeb 
72125a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
72135a9a0d78SBjoern A. Zeeb 							continue;
72145a9a0d78SBjoern A. Zeeb 
72155a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
72165a9a0d78SBjoern A. Zeeb 							continue;
72175a9a0d78SBjoern A. Zeeb 
72185a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
72195a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
72205a9a0d78SBjoern A. Zeeb 							continue;
72215a9a0d78SBjoern A. Zeeb 
72225a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
72235a9a0d78SBjoern A. Zeeb 
72245a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
72255a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
72265a9a0d78SBjoern A. Zeeb 					}
72275a9a0d78SBjoern A. Zeeb 				}
7228a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
72295a9a0d78SBjoern A. Zeeb 			}
72305a9a0d78SBjoern A. Zeeb 		}
72315a9a0d78SBjoern A. Zeeb 	}
72325a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72335a9a0d78SBjoern A. Zeeb }
72345a9a0d78SBjoern A. Zeeb 
72355a9a0d78SBjoern A. Zeeb void
72365a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
72375a9a0d78SBjoern A. Zeeb {
72385a9a0d78SBjoern A. Zeeb 	int i;
72395a9a0d78SBjoern A. Zeeb 
72405a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
72415a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72425a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
72435a9a0d78SBjoern A. Zeeb }
72445a9a0d78SBjoern A. Zeeb 
72455a9a0d78SBjoern A. Zeeb void
72465a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
72475a9a0d78SBjoern A. Zeeb {
72485a9a0d78SBjoern A. Zeeb 
72495a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
72505a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
72515a9a0d78SBjoern A. Zeeb 
72525a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
72535a9a0d78SBjoern A. Zeeb }
72545a9a0d78SBjoern A. Zeeb 
72555a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
72565a9a0d78SBjoern A. Zeeb void
72575a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
72585a9a0d78SBjoern A. Zeeb {
72595a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72605a9a0d78SBjoern A. Zeeb 
72615a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72625a9a0d78SBjoern A. Zeeb 
72635a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
72645a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
72655a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
72665a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
72675a9a0d78SBjoern A. Zeeb }
72685a9a0d78SBjoern A. Zeeb 
72695a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
72705a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
72715a9a0d78SBjoern A. Zeeb {
72725a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72735a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
72745a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
72755a9a0d78SBjoern A. Zeeb 
72765a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
72775a9a0d78SBjoern A. Zeeb 	txq = NULL;
72785a9a0d78SBjoern A. Zeeb 
72795a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
72805a9a0d78SBjoern A. Zeeb 
72815a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
72825a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
72835a9a0d78SBjoern A. Zeeb 		goto out;
72845a9a0d78SBjoern A. Zeeb 
72855a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
72865a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
72875a9a0d78SBjoern A. Zeeb 		goto out;
72885a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
72895a9a0d78SBjoern A. Zeeb 		goto out;
72905a9a0d78SBjoern A. Zeeb 
72915a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
72925a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
72935a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
72945a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
72955a9a0d78SBjoern A. Zeeb 
72965a9a0d78SBjoern A. Zeeb out:
72975a9a0d78SBjoern A. Zeeb 	return (txq);
72985a9a0d78SBjoern A. Zeeb }
72995a9a0d78SBjoern A. Zeeb 
73005a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
73015a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
73025a9a0d78SBjoern A. Zeeb {
73035a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73045a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7305eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
73065a9a0d78SBjoern A. Zeeb 
73075a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73085a9a0d78SBjoern A. Zeeb 
73095a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73105a9a0d78SBjoern A. Zeeb 
73115a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7312eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7313eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7314eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7315eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
73165a9a0d78SBjoern A. Zeeb 		goto out;
73175a9a0d78SBjoern A. Zeeb 
731841b746e0SAustin Shafer 	/*
731941b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
732041b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
732141b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
732241b746e0SAustin Shafer 	 */
732341b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
73245a9a0d78SBjoern A. Zeeb 		goto out;
73255a9a0d78SBjoern A. Zeeb 
73265a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73275a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
73285a9a0d78SBjoern A. Zeeb out:
73295a9a0d78SBjoern A. Zeeb 	return;
73305a9a0d78SBjoern A. Zeeb }
73315a9a0d78SBjoern A. Zeeb 
7332eac3646fSBjoern A. Zeeb void
7333eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7334eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7335eac3646fSBjoern A. Zeeb {
7336eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7337eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7338eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7339eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7340eac3646fSBjoern A. Zeeb 
7341eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7342eac3646fSBjoern A. Zeeb 
7343eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7344eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7345eac3646fSBjoern A. Zeeb 	do {
7346eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7347eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7348eac3646fSBjoern A. Zeeb 			break;
7349eac3646fSBjoern A. Zeeb 
7350eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7351eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7352eac3646fSBjoern A. Zeeb 		do {
7353eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7354eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7355eac3646fSBjoern A. Zeeb 				break;
7356eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7357eac3646fSBjoern A. Zeeb 		} while(1);
7358eac3646fSBjoern A. Zeeb 
7359eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7360eac3646fSBjoern A. Zeeb 	} while (1);
7361eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7362eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7363eac3646fSBjoern A. Zeeb }
7364eac3646fSBjoern A. Zeeb 
73655a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
73665a9a0d78SBjoern A. Zeeb 
73675edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
73685edde07cSBjoern A. Zeeb 	u_int refcnt;
73695edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
73705edde07cSBjoern A. Zeeb };
73715edde07cSBjoern A. Zeeb 
73725edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
73735edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
73745edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
73755edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
73765edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
73775edde07cSBjoern A. Zeeb 	size_t ssid_len;
73785edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
73795edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
73805edde07cSBjoern A. Zeeb 
73815edde07cSBjoern A. Zeeb 	/*
73825edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
73835edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
73845edde07cSBjoern A. Zeeb 	 */
73855edde07cSBjoern A. Zeeb 	bool match;
73865edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
73875edde07cSBjoern A. Zeeb };
73885edde07cSBjoern A. Zeeb 
73895edde07cSBjoern A. Zeeb static void
73905edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
73915edde07cSBjoern A. Zeeb {
73925edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
73935edde07cSBjoern A. Zeeb 	size_t ielen;
73945edde07cSBjoern A. Zeeb 
73955edde07cSBjoern A. Zeeb 	lookup = arg;
73965edde07cSBjoern A. Zeeb 
73975edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
73985edde07cSBjoern A. Zeeb 	if (lookup->match)
73995edde07cSBjoern A. Zeeb 		return;
74005edde07cSBjoern A. Zeeb 
74015edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
74025edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
74035edde07cSBjoern A. Zeeb 		return;
74045edde07cSBjoern A. Zeeb 
74055edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
74065edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
74075edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
74085edde07cSBjoern A. Zeeb 		return;
74095edde07cSBjoern A. Zeeb 	}
74105edde07cSBjoern A. Zeeb 
74115edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
74125edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
74135edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
74145edde07cSBjoern A. Zeeb 		return;
74155edde07cSBjoern A. Zeeb 	}
74165edde07cSBjoern A. Zeeb 
74175edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
74185edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
74195edde07cSBjoern A. Zeeb 
74205edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
74215edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
74225edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
74235edde07cSBjoern A. Zeeb 			return;
74245edde07cSBjoern A. Zeeb 	}
74255edde07cSBjoern A. Zeeb 
74265edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
74275edde07cSBjoern A. Zeeb 		return;
74285edde07cSBjoern A. Zeeb 
74295edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
74305edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
74315edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
74325edde07cSBjoern A. Zeeb 			return;
74335edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
74345edde07cSBjoern A. Zeeb 			return;
74355edde07cSBjoern A. Zeeb 	}
74365edde07cSBjoern A. Zeeb 
74375edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
74385edde07cSBjoern A. Zeeb 
74395edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
74405edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
74415edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
74425edde07cSBjoern A. Zeeb 		return;
74435edde07cSBjoern A. Zeeb 
74445edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
74455edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
74465edde07cSBjoern A. Zeeb 	if (ielen)
74475edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
74485edde07cSBjoern A. Zeeb 
74495edde07cSBjoern A. Zeeb 	lookup->match = true;
74505edde07cSBjoern A. Zeeb }
74515edde07cSBjoern A. Zeeb 
74525edde07cSBjoern A. Zeeb struct cfg80211_bss *
74535edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
74545edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
74555edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
74565edde07cSBjoern A. Zeeb {
74575edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
74585edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
74595edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
74605edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
74615edde07cSBjoern A. Zeeb 
74625edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
74635edde07cSBjoern A. Zeeb 
74645edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
74655edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
74665edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
74675edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
74685edde07cSBjoern A. Zeeb 		return (NULL);
74695edde07cSBjoern A. Zeeb 	}
74705edde07cSBjoern A. Zeeb 
74715edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
74725edde07cSBjoern A. Zeeb 	lookup.chan = chan;
74735edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
74745edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
74755edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
74765edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
74775edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
74785edde07cSBjoern A. Zeeb 	lookup.match = false;
74795edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
74805edde07cSBjoern A. Zeeb 
74815edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
74825edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
74835edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
74845edde07cSBjoern A. Zeeb 	if (!lookup.match) {
74855edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
74865edde07cSBjoern A. Zeeb 		return (NULL);
74875edde07cSBjoern A. Zeeb 	}
74885edde07cSBjoern A. Zeeb 
74895edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
74905edde07cSBjoern A. Zeeb 	return (&lbss->bss);
74915edde07cSBjoern A. Zeeb }
74925edde07cSBjoern A. Zeeb 
74935edde07cSBjoern A. Zeeb void
74945edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
74955edde07cSBjoern A. Zeeb {
74965edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
74975edde07cSBjoern A. Zeeb 
74985edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
74995edde07cSBjoern A. Zeeb 
75005edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
75015edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
75025edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
75035edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75045edde07cSBjoern A. Zeeb 	}
75055edde07cSBjoern A. Zeeb }
75065edde07cSBjoern A. Zeeb 
75075edde07cSBjoern A. Zeeb void
75085edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
75095edde07cSBjoern A. Zeeb {
75105edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75115edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
75125edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
75135edde07cSBjoern A. Zeeb 
75145edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
75155edde07cSBjoern A. Zeeb 	ic = lhw->ic;
75165edde07cSBjoern A. Zeeb 
75175edde07cSBjoern A. Zeeb 	/*
75185edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
75195edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
75205edde07cSBjoern A. Zeeb 	 */
75215edde07cSBjoern A. Zeeb 	if (ic == NULL ||
75225edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
75235edde07cSBjoern A. Zeeb 		return;
75245edde07cSBjoern A. Zeeb 
75255edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
75265edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
75275edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
75285edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
75295edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
75305edde07cSBjoern A. Zeeb }
75315edde07cSBjoern A. Zeeb 
75325edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
75335edde07cSBjoern A. Zeeb 
7534ac1d519cSBjoern A. Zeeb /*
7535ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7536ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7537ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7538ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7539ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7540ac1d519cSBjoern A. Zeeb  */
7541ac1d519cSBjoern A. Zeeb 
7542ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7543ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7544ac1d519cSBjoern A. Zeeb {
7545ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7546ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7547ac1d519cSBjoern A. Zeeb 	int error;
7548ac1d519cSBjoern A. Zeeb 
7549ac1d519cSBjoern A. Zeeb 	changed = 0;
7550ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7551ac1d519cSBjoern A. Zeeb 		cd = NULL;
7552ac1d519cSBjoern A. Zeeb 	else
7553ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7554ac1d519cSBjoern A. Zeeb 
7555ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7556ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7557ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7558ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7559ac1d519cSBjoern A. Zeeb 	}
7560ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7561ac1d519cSBjoern A. Zeeb 
7562ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7563ac1d519cSBjoern A. Zeeb 		return (0);
7564ac1d519cSBjoern A. Zeeb 
7565ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7566ac1d519cSBjoern A. Zeeb 	return (error);
7567ac1d519cSBjoern A. Zeeb }
7568ac1d519cSBjoern A. Zeeb 
7569ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7570ac1d519cSBjoern A. Zeeb 
75716b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
75726b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
75736b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7574