xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision ac2c7271b7c2624d7cf3af6bdaf82b00323edfd3)
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
370f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
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 
377f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
378f9c7a07dSBjoern A. Zeeb 		sta->deflink.ht_cap.ht_supported = false;
3799fb91463SBjoern A. Zeeb 		return;
380f9c7a07dSBjoern A. Zeeb 	}
3819fb91463SBjoern A. Zeeb 
3829fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ht_supported = true;
3839fb91463SBjoern A. Zeeb 
3849fb91463SBjoern A. Zeeb 	/* htcap->ampdu_params_info */
3859fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
3869fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3879fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
3889fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
3899fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3909fb91463SBjoern A. Zeeb 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
3919fb91463SBjoern A. Zeeb 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
3929fb91463SBjoern A. Zeeb 
3939fb91463SBjoern A. Zeeb 	ie = ni->ni_ies.htcap_ie;
3949fb91463SBjoern A. Zeeb 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
3959fb91463SBjoern A. Zeeb 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
3969fb91463SBjoern A. Zeeb 		ie += 4;
3979fb91463SBjoern A. Zeeb 	ie += 2;
3989fb91463SBjoern A. Zeeb 	htcap = (struct ieee80211_ht_cap *)ie;
3999fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.cap = htcap->cap_info;
4009fb91463SBjoern A. Zeeb 	sta->deflink.ht_cap.mcs = htcap->mcs;
4019fb91463SBjoern A. Zeeb 
402f9c7a07dSBjoern A. Zeeb 	if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0)
403f9c7a07dSBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
404f9c7a07dSBjoern A. Zeeb 
405f9c7a07dSBjoern A. Zeeb 	/*
406f9c7a07dSBjoern A. Zeeb 	 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
407f9c7a07dSBjoern A. Zeeb 	 * optional MCS for Nss=1..4.  We need to check the first four
408f9c7a07dSBjoern A. Zeeb 	 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
409f9c7a07dSBjoern A. Zeeb 	 * MCS33.. is UEQM.
410f9c7a07dSBjoern A. Zeeb 	 */
4119fb91463SBjoern A. Zeeb 	rx_nss = 0;
412f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
4139fb91463SBjoern A. Zeeb 		if (htcap->mcs.rx_mask[i])
4149fb91463SBjoern A. Zeeb 			rx_nss++;
4159fb91463SBjoern A. Zeeb 	}
416f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
417f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
4189fb91463SBjoern A. Zeeb 
419f9c7a07dSBjoern A. Zeeb 	IMPROVE("sta->wme");
420f9c7a07dSBjoern A. Zeeb 
421f9c7a07dSBjoern A. Zeeb 	if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
422f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
423f9c7a07dSBjoern A. Zeeb 	else
424f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
425f9c7a07dSBjoern A. Zeeb 	sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
426f9c7a07dSBjoern A. Zeeb #ifdef __handled_by_driver__	/* iwlwifi only? actually unused? */
427f9c7a07dSBjoern A. Zeeb 	for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
428f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_tid_amsdu_len[j] = ;
429f9c7a07dSBjoern A. Zeeb 	}
430f9c7a07dSBjoern A. Zeeb #endif
4319fb91463SBjoern A. Zeeb }
4329fb91463SBjoern A. Zeeb #endif
4339fb91463SBjoern A. Zeeb 
4349fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
4359fb91463SBjoern A. Zeeb static void
436f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
4379fb91463SBjoern A. Zeeb {
438f9c7a07dSBjoern A. Zeeb 	uint32_t width;
439f9c7a07dSBjoern A. Zeeb 	int rx_nss;
440f9c7a07dSBjoern A. Zeeb 	uint16_t rx_mcs_map;
441f9c7a07dSBjoern A. Zeeb 	uint8_t mcs;
4429fb91463SBjoern A. Zeeb 
443f9c7a07dSBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) {
444f9c7a07dSBjoern A. Zeeb 		sta->deflink.vht_cap.vht_supported = false;
4459fb91463SBjoern A. Zeeb 		return;
446f9c7a07dSBjoern A. Zeeb 	}
4479fb91463SBjoern A. Zeeb 
448f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_supported = true;
449f9c7a07dSBjoern A. Zeeb 
450f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.cap = ni->ni_vhtcap;
451f9c7a07dSBjoern A. Zeeb 	sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
452f9c7a07dSBjoern A. Zeeb 
453f9c7a07dSBjoern A. Zeeb 	width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
454f9c7a07dSBjoern A. Zeeb 	switch (width) {
455f9c7a07dSBjoern A. Zeeb #if 0
456f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
457f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
4589fb91463SBjoern A. Zeeb 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
459f9c7a07dSBjoern A. Zeeb 		break;
460f9c7a07dSBjoern A. Zeeb #endif
461f9c7a07dSBjoern A. Zeeb 	default:
462f9c7a07dSBjoern A. Zeeb 		/* Check if we do support 160Mhz somehow after all. */
463f9c7a07dSBjoern A. Zeeb #if 0
464f9c7a07dSBjoern A. Zeeb 		if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
465f9c7a07dSBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
466f9c7a07dSBjoern A. Zeeb 		else
467f9c7a07dSBjoern A. Zeeb #endif
4689fb91463SBjoern A. Zeeb 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
4699fb91463SBjoern A. Zeeb 	}
4709fb91463SBjoern A. Zeeb 
471f9c7a07dSBjoern A. Zeeb 
472f9c7a07dSBjoern A. Zeeb 	rx_nss = 0;
473f9c7a07dSBjoern A. Zeeb 	rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
474f9c7a07dSBjoern A. Zeeb 	for (int i = 7; i >= 0; i--) {
475f9c7a07dSBjoern A. Zeeb 		mcs = rx_mcs_map >> (2 * i);
476f9c7a07dSBjoern A. Zeeb 		mcs &= 0x3;
477f9c7a07dSBjoern A. Zeeb 		if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
478f9c7a07dSBjoern A. Zeeb 			rx_nss = i + 1;
479f9c7a07dSBjoern A. Zeeb 			break;
480f9c7a07dSBjoern A. Zeeb 		}
481f9c7a07dSBjoern A. Zeeb 	}
482f9c7a07dSBjoern A. Zeeb 	if (rx_nss > 0)
483f9c7a07dSBjoern A. Zeeb 		sta->deflink.rx_nss = rx_nss;
484f9c7a07dSBjoern A. Zeeb 
485f9c7a07dSBjoern A. Zeeb 	switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
486f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
487f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
488f9c7a07dSBjoern A. Zeeb 		break;
489f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
490f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
491f9c7a07dSBjoern A. Zeeb 		break;
492f9c7a07dSBjoern A. Zeeb 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
493f9c7a07dSBjoern A. Zeeb 	default:
494f9c7a07dSBjoern A. Zeeb 		sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
495f9c7a07dSBjoern A. Zeeb 		break;
496f9c7a07dSBjoern A. Zeeb 	}
4979fb91463SBjoern A. Zeeb }
4989fb91463SBjoern A. Zeeb #endif
4999fb91463SBjoern A. Zeeb 
500d9f59799SBjoern A. Zeeb static void
501f9c7a07dSBjoern A. Zeeb lkpi_sta_sync_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni)
502f9c7a07dSBjoern A. Zeeb {
503f9c7a07dSBjoern A. Zeeb 
504f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_HT)
505f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_ht_from_ni(sta, ni);
506f9c7a07dSBjoern A. Zeeb #endif
507f9c7a07dSBjoern A. Zeeb #if defined(LKPI_80211_VHT)
508f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_vht_from_ni(sta, ni);
509f9c7a07dSBjoern A. Zeeb #endif
510f9c7a07dSBjoern A. Zeeb }
511f9c7a07dSBjoern A. Zeeb 
512f9c7a07dSBjoern A. Zeeb static void
51367674c1cSBjoern A. Zeeb lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
51467674c1cSBjoern A. Zeeb     const char *_f, int _l)
515d9f59799SBjoern A. Zeeb {
516d9f59799SBjoern A. Zeeb 
5179d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
5189d9ba2b7SBjoern A. Zeeb 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
519d9f59799SBjoern A. Zeeb 		return;
520d9f59799SBjoern A. Zeeb 	if (lsta == NULL)
521d9f59799SBjoern A. Zeeb 		return;
522d9f59799SBjoern A. Zeeb 
523d9f59799SBjoern A. Zeeb 	printf("%s:%d lsta %p ni %p sta %p\n",
52467674c1cSBjoern A. Zeeb 	    _f, _l, lsta, ni, &lsta->sta);
52567674c1cSBjoern A. Zeeb 	if (ni != NULL)
52667674c1cSBjoern A. Zeeb 		ieee80211_dump_node(NULL, ni);
527d9f59799SBjoern A. Zeeb 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
528d9f59799SBjoern A. Zeeb 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
52911db70b6SBjoern A. Zeeb 		&lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
5309d9ba2b7SBjoern A. Zeeb #endif
531d9f59799SBjoern A. Zeeb }
532d9f59799SBjoern A. Zeeb 
533d9f59799SBjoern A. Zeeb static void
534d9f59799SBjoern A. Zeeb lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
535d9f59799SBjoern A. Zeeb {
536d9f59799SBjoern A. Zeeb 
537d9f59799SBjoern A. Zeeb 
538a8f735a6SBjoern A. Zeeb 	wiphy_lock(lsta->hw->wiphy);
539a8f735a6SBjoern A. Zeeb 	KASSERT(!list_empty(&lsta->lsta_list),
540a8f735a6SBjoern A. Zeeb 	    ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
541a8f735a6SBjoern A. Zeeb 	list_del_init(&lsta->lsta_list);
542a8f735a6SBjoern A. Zeeb 	wiphy_unlock(lsta->hw->wiphy);
543d9f59799SBjoern A. Zeeb }
544d9f59799SBjoern A. Zeeb 
5454f61ef8bSBjoern A. Zeeb static struct lkpi_sta *
5464f61ef8bSBjoern A. Zeeb lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
5474f61ef8bSBjoern A. Zeeb     struct ieee80211_hw *hw, struct ieee80211_node *ni)
5484f61ef8bSBjoern A. Zeeb {
5494f61ef8bSBjoern A. Zeeb 	struct lkpi_sta *lsta;
5504f61ef8bSBjoern A. Zeeb 	struct lkpi_vif *lvif;
5514f61ef8bSBjoern A. Zeeb 	struct ieee80211_vif *vif;
5524f61ef8bSBjoern A. Zeeb 	struct ieee80211_sta *sta;
553b6b352e4SBjoern A. Zeeb 	int band, i, tid;
5544f61ef8bSBjoern A. Zeeb 
5554f61ef8bSBjoern A. Zeeb 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
5564f61ef8bSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
5574f61ef8bSBjoern A. Zeeb 	if (lsta == NULL)
5584f61ef8bSBjoern A. Zeeb 		return (NULL);
5594f61ef8bSBjoern A. Zeeb 
560a8f735a6SBjoern A. Zeeb 	lsta->hw = hw;
5614f61ef8bSBjoern A. Zeeb 	lsta->added_to_drv = false;
5624f61ef8bSBjoern A. Zeeb 	lsta->state = IEEE80211_STA_NOTEXIST;
5634f61ef8bSBjoern A. Zeeb 	/*
5640936c648SBjoern A. Zeeb 	 * Link the ni to the lsta here without taking a reference.
5650936c648SBjoern A. Zeeb 	 * For one we would have to take the reference in node_init()
5660936c648SBjoern A. Zeeb 	 * as ieee80211_alloc_node() will initialise the refcount after us.
5670936c648SBjoern A. Zeeb 	 * For the other a ni and an lsta are 1:1 mapped and always together
5680936c648SBjoern A. Zeeb 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
5690936c648SBjoern A. Zeeb 	 * using the ni references for the lsta as well despite it being
5700936c648SBjoern A. Zeeb 	 * two separate allocations.
5714f61ef8bSBjoern A. Zeeb 	 */
5720936c648SBjoern A. Zeeb 	lsta->ni = ni;
5734f61ef8bSBjoern A. Zeeb 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
5744f61ef8bSBjoern A. Zeeb 	ni->ni_drv_data = lsta;
5754f61ef8bSBjoern A. Zeeb 
5764f61ef8bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
5774f61ef8bSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
5784f61ef8bSBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
5794f61ef8bSBjoern A. Zeeb 
5804f61ef8bSBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->addr, mac);
581b6b352e4SBjoern A. Zeeb 
582b6b352e4SBjoern A. Zeeb 	/* TXQ */
5834f61ef8bSBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
5844f61ef8bSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
5854f61ef8bSBjoern A. Zeeb 
586d0d29110SBjoern A. Zeeb 		/* We are not limiting ourselves to hw.queues here. */
5874f61ef8bSBjoern A. Zeeb 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
5884f61ef8bSBjoern A. Zeeb 		    M_LKPI80211, M_NOWAIT | M_ZERO);
5894f61ef8bSBjoern A. Zeeb 		if (ltxq == NULL)
5904f61ef8bSBjoern A. Zeeb 			goto cleanup;
5914f61ef8bSBjoern A. Zeeb 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
5924f61ef8bSBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
593d0d29110SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
594d0d29110SBjoern A. Zeeb 				free(ltxq, M_LKPI80211);
595d0d29110SBjoern A. Zeeb 				continue;
596d0d29110SBjoern A. Zeeb 			}
597d0d29110SBjoern A. Zeeb 			IMPROVE("AP/if we support non-STA here too");
5984f61ef8bSBjoern A. Zeeb 			ltxq->txq.ac = IEEE80211_AC_VO;
5994f61ef8bSBjoern A. Zeeb 		} else {
600fb3c249eSBjoern A. Zeeb 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
6014f61ef8bSBjoern A. Zeeb 		}
602d0d29110SBjoern A. Zeeb 		ltxq->seen_dequeue = false;
6030cbcfa19SBjoern A. Zeeb 		ltxq->stopped = false;
604d0d29110SBjoern A. Zeeb 		ltxq->txq.vif = vif;
6054f61ef8bSBjoern A. Zeeb 		ltxq->txq.tid = tid;
6064f61ef8bSBjoern A. Zeeb 		ltxq->txq.sta = sta;
6070cbcfa19SBjoern A. Zeeb 		TAILQ_ELEM_INIT(ltxq, txq_entry);
608d0d29110SBjoern A. Zeeb 		skb_queue_head_init(&ltxq->skbq);
609eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
6104f61ef8bSBjoern A. Zeeb 		sta->txq[tid] = &ltxq->txq;
6114f61ef8bSBjoern A. Zeeb 	}
6124f61ef8bSBjoern A. Zeeb 
613b6b352e4SBjoern A. Zeeb 	/* Deflink information. */
614b6b352e4SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
615b6b352e4SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
616b6b352e4SBjoern A. Zeeb 
617b6b352e4SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
618b6b352e4SBjoern A. Zeeb 		if (supband == NULL)
619b6b352e4SBjoern A. Zeeb 			continue;
620b6b352e4SBjoern A. Zeeb 
621b6b352e4SBjoern A. Zeeb 		for (i = 0; i < supband->n_bitrates; i++) {
62273cd1c5dSBjoern A. Zeeb 			switch (band) {
62373cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_2GHZ:
62473cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
62573cd1c5dSBjoern A. Zeeb 				case 240:	/* 11g only */
62673cd1c5dSBjoern A. Zeeb 				case 120:	/* 11g only */
62773cd1c5dSBjoern A. Zeeb 				case 110:
62873cd1c5dSBjoern A. Zeeb 				case 60:	/* 11g only */
62973cd1c5dSBjoern A. Zeeb 				case 55:
63073cd1c5dSBjoern A. Zeeb 				case 20:
63173cd1c5dSBjoern A. Zeeb 				case 10:
632b6b352e4SBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
63373cd1c5dSBjoern A. Zeeb 					break;
63473cd1c5dSBjoern A. Zeeb 				}
63573cd1c5dSBjoern A. Zeeb 				break;
63673cd1c5dSBjoern A. Zeeb 			case NL80211_BAND_5GHZ:
63773cd1c5dSBjoern A. Zeeb 				switch (supband->bitrates[i].bitrate) {
63873cd1c5dSBjoern A. Zeeb 				case 240:
63973cd1c5dSBjoern A. Zeeb 				case 120:
64073cd1c5dSBjoern A. Zeeb 				case 60:
64173cd1c5dSBjoern A. Zeeb 					sta->deflink.supp_rates[band] |= BIT(i);
64273cd1c5dSBjoern A. Zeeb 					break;
64373cd1c5dSBjoern A. Zeeb 				}
64473cd1c5dSBjoern A. Zeeb 				break;
64573cd1c5dSBjoern A. Zeeb 			}
646b6b352e4SBjoern A. Zeeb 		}
647b6b352e4SBjoern A. Zeeb 	}
6489fb91463SBjoern A. Zeeb 
6496ffb7bd4SBjoern A. Zeeb 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
6509fb91463SBjoern A. Zeeb 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
651f5a58c2dSMikhail Pchelin 	sta->deflink.rx_nss = 1;
6529fb91463SBjoern A. Zeeb 
653f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
6549fb91463SBjoern A. Zeeb 
655f9c7a07dSBjoern A. Zeeb 	IMPROVE("he, eht, bw_320, ... smps_mode, ..");
656b6b352e4SBjoern A. Zeeb 
6576ffb7bd4SBjoern A. Zeeb 	/* Link configuration. */
6586ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
6596ffb7bd4SBjoern A. Zeeb 	sta->link[0] = &sta->deflink;
6606ffb7bd4SBjoern A. Zeeb 	for (i = 1; i < nitems(sta->link); i++) {
6616ffb7bd4SBjoern A. Zeeb 		IMPROVE("more links; only link[0] = deflink currently.");
6626ffb7bd4SBjoern A. Zeeb 	}
6636ffb7bd4SBjoern A. Zeeb 
6644f61ef8bSBjoern A. Zeeb 	/* Deferred TX path. */
665fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
6664f61ef8bSBjoern A. Zeeb 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
6674f61ef8bSBjoern A. Zeeb 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
6680936c648SBjoern A. Zeeb 	lsta->txq_ready = true;
6694f61ef8bSBjoern A. Zeeb 
6704f61ef8bSBjoern A. Zeeb 	return (lsta);
6714f61ef8bSBjoern A. Zeeb 
6724f61ef8bSBjoern A. Zeeb cleanup:
673eac3646fSBjoern A. Zeeb 	for (; tid >= 0; tid--) {
674eac3646fSBjoern A. Zeeb 		struct lkpi_txq *ltxq;
675eac3646fSBjoern A. Zeeb 
676eac3646fSBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
677eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
6784f61ef8bSBjoern A. Zeeb 		free(sta->txq[tid], M_LKPI80211);
679eac3646fSBjoern A. Zeeb 	}
6804f61ef8bSBjoern A. Zeeb 	free(lsta, M_LKPI80211);
6814f61ef8bSBjoern A. Zeeb 	return (NULL);
6824f61ef8bSBjoern A. Zeeb }
6834f61ef8bSBjoern A. Zeeb 
6840936c648SBjoern A. Zeeb static void
6850936c648SBjoern A. Zeeb lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
6860936c648SBjoern A. Zeeb {
6870936c648SBjoern A. Zeeb 	struct mbuf *m;
6880936c648SBjoern A. Zeeb 
6890936c648SBjoern A. Zeeb 	if (lsta->added_to_drv)
6900936c648SBjoern A. Zeeb 		panic("%s: Trying to free an lsta still known to firmware: "
6910936c648SBjoern A. Zeeb 		    "lsta %p ni %p added_to_drv %d\n",
6920936c648SBjoern A. Zeeb 		    __func__, lsta, ni, lsta->added_to_drv);
6930936c648SBjoern A. Zeeb 
6940936c648SBjoern A. Zeeb 	/* XXX-BZ free resources, ... */
6950936c648SBjoern A. Zeeb 	IMPROVE();
6960936c648SBjoern A. Zeeb 
697fa4e4257SBjoern A. Zeeb 	/* Drain sta->txq[] */
698fa4e4257SBjoern A. Zeeb 
699fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
7000936c648SBjoern A. Zeeb 	lsta->txq_ready = false;
701fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
7020936c648SBjoern A. Zeeb 
7030936c648SBjoern A. Zeeb 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
7040936c648SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
7050936c648SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
7060936c648SBjoern A. Zeeb 
7070936c648SBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
7080936c648SBjoern A. Zeeb 	m = mbufq_dequeue(&lsta->txq);
7090936c648SBjoern A. Zeeb 	while (m != NULL) {
7100936c648SBjoern A. Zeeb 		struct ieee80211_node *nim;
7110936c648SBjoern A. Zeeb 
7120936c648SBjoern A. Zeeb 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
7130936c648SBjoern A. Zeeb 		if (nim != NULL)
7140936c648SBjoern A. Zeeb 			ieee80211_free_node(nim);
7150936c648SBjoern A. Zeeb 		m_freem(m);
7160936c648SBjoern A. Zeeb 		m = mbufq_dequeue(&lsta->txq);
7170936c648SBjoern A. Zeeb 	}
7180936c648SBjoern A. Zeeb 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
7190936c648SBjoern A. Zeeb 	    __func__, lsta, mbufq_len(&lsta->txq)));
720fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
7210936c648SBjoern A. Zeeb 
7220936c648SBjoern A. Zeeb 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
7230936c648SBjoern A. Zeeb 
7240936c648SBjoern A. Zeeb 	IMPROVE("Make sure everything is cleaned up.");
7250936c648SBjoern A. Zeeb 
7260936c648SBjoern A. Zeeb 	/* Free lsta. */
7270936c648SBjoern A. Zeeb 	lsta->ni = NULL;
7280936c648SBjoern A. Zeeb 	ni->ni_drv_data = NULL;
7290936c648SBjoern A. Zeeb 	free(lsta, M_LKPI80211);
7300936c648SBjoern A. Zeeb }
7310936c648SBjoern A. Zeeb 
7320936c648SBjoern A. Zeeb 
7336b4cac81SBjoern A. Zeeb static enum nl80211_band
7346b4cac81SBjoern A. Zeeb lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
7356b4cac81SBjoern A. Zeeb {
7366b4cac81SBjoern A. Zeeb 
7376b4cac81SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_2GHZ(c))
7386b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_2GHZ);
7396b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_5GHZ(c))
7406b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_5GHZ);
7416b4cac81SBjoern A. Zeeb #ifdef __notyet__
7426b4cac81SBjoern A. Zeeb 	else if ()
7436b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_6GHZ);
7446b4cac81SBjoern A. Zeeb 	else if ()
7456b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_60GHZ);
7466b4cac81SBjoern A. Zeeb 	else if (IEEE80211_IS_CHAN_GSM(c))
7476b4cac81SBjoern A. Zeeb 		return (NL80211_BAND_XXX);
7486b4cac81SBjoern A. Zeeb #endif
7496b4cac81SBjoern A. Zeeb 	else
7506b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band. c %p flags %#x\n",
7516b4cac81SBjoern A. Zeeb 		    __func__, c, c->ic_flags);
7526b4cac81SBjoern A. Zeeb }
7536b4cac81SBjoern A. Zeeb 
7546b4cac81SBjoern A. Zeeb static uint32_t
7556b4cac81SBjoern A. Zeeb lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
7566b4cac81SBjoern A. Zeeb {
7576b4cac81SBjoern A. Zeeb 
7586b4cac81SBjoern A. Zeeb 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
7596b4cac81SBjoern A. Zeeb 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
7606b4cac81SBjoern A. Zeeb 	switch (band) {
7616b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
7626b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_2GHZ);
7636b4cac81SBjoern A. Zeeb 		break;
7646b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
7656b4cac81SBjoern A. Zeeb 		return (IEEE80211_CHAN_5GHZ);
7666b4cac81SBjoern A. Zeeb 		break;
7676b4cac81SBjoern A. Zeeb 	case NL80211_BAND_60GHZ:
7686b4cac81SBjoern A. Zeeb 		break;
7696b4cac81SBjoern A. Zeeb 	case NL80211_BAND_6GHZ:
7706b4cac81SBjoern A. Zeeb 		break;
7716b4cac81SBjoern A. Zeeb 	default:
7726b4cac81SBjoern A. Zeeb 		panic("%s: unsupported band %u\n", __func__, band);
7736b4cac81SBjoern A. Zeeb 		break;
7746b4cac81SBjoern A. Zeeb 	}
7756b4cac81SBjoern A. Zeeb 
7766b4cac81SBjoern A. Zeeb 	IMPROVE();
7776b4cac81SBjoern A. Zeeb 	return (0x00);
7786b4cac81SBjoern A. Zeeb }
7796b4cac81SBjoern A. Zeeb 
780e3a0b120SBjoern A. Zeeb #if 0
7816b4cac81SBjoern A. Zeeb static enum ieee80211_ac_numbers
7826b4cac81SBjoern A. Zeeb lkpi_ac_net_to_l80211(int ac)
7836b4cac81SBjoern A. Zeeb {
7846b4cac81SBjoern A. Zeeb 
7856b4cac81SBjoern A. Zeeb 	switch (ac) {
7866b4cac81SBjoern A. Zeeb 	case WME_AC_VO:
7876b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VO);
7886b4cac81SBjoern A. Zeeb 	case WME_AC_VI:
7896b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_VI);
7906b4cac81SBjoern A. Zeeb 	case WME_AC_BE:
7916b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7926b4cac81SBjoern A. Zeeb 	case WME_AC_BK:
7936b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BK);
7946b4cac81SBjoern A. Zeeb 	default:
7956b4cac81SBjoern A. Zeeb 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
7966b4cac81SBjoern A. Zeeb 		return (IEEE80211_AC_BE);
7976b4cac81SBjoern A. Zeeb 	}
7986b4cac81SBjoern A. Zeeb }
799e3a0b120SBjoern A. Zeeb #endif
8006b4cac81SBjoern A. Zeeb 
8016b4cac81SBjoern A. Zeeb static enum nl80211_iftype
8026b4cac81SBjoern A. Zeeb lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
8036b4cac81SBjoern A. Zeeb {
8046b4cac81SBjoern A. Zeeb 
8056b4cac81SBjoern A. Zeeb 	switch (opmode) {
8066b4cac81SBjoern A. Zeeb 	case IEEE80211_M_IBSS:
8076b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_ADHOC);
8086b4cac81SBjoern A. Zeeb 		break;
8096b4cac81SBjoern A. Zeeb 	case IEEE80211_M_STA:
8106b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_STATION);
8116b4cac81SBjoern A. Zeeb 		break;
8126b4cac81SBjoern A. Zeeb 	case IEEE80211_M_WDS:
8136b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_WDS);
8146b4cac81SBjoern A. Zeeb 		break;
8156b4cac81SBjoern A. Zeeb 	case IEEE80211_M_HOSTAP:
8166b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_AP);
8176b4cac81SBjoern A. Zeeb 		break;
8186b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MONITOR:
8196b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MONITOR);
8206b4cac81SBjoern A. Zeeb 		break;
8216b4cac81SBjoern A. Zeeb 	case IEEE80211_M_MBSS:
8226b4cac81SBjoern A. Zeeb 		return (NL80211_IFTYPE_MESH_POINT);
8236b4cac81SBjoern A. Zeeb 		break;
8246b4cac81SBjoern A. Zeeb 	case IEEE80211_M_AHDEMO:
8256b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8266b4cac81SBjoern A. Zeeb 	default:
8276b4cac81SBjoern A. Zeeb 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
8286b4cac81SBjoern A. Zeeb 		/* FALLTHROUGH */
8296b4cac81SBjoern A. Zeeb 	}
8306b4cac81SBjoern A. Zeeb 	return (NL80211_IFTYPE_UNSPECIFIED);
8316b4cac81SBjoern A. Zeeb }
8326b4cac81SBjoern A. Zeeb 
833b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
83412a511c8SBjoern A. Zeeb static const char *
83512a511c8SBjoern A. Zeeb lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
83612a511c8SBjoern A. Zeeb {
83712a511c8SBjoern A. Zeeb 
83812a511c8SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
83912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
84012a511c8SBjoern A. Zeeb 		return ("WEP40");
84112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
84212a511c8SBjoern A. Zeeb 		return ("TKIP");
84312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
84412a511c8SBjoern A. Zeeb 		return ("CCMP");
84512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
84612a511c8SBjoern A. Zeeb 		return ("WEP104");
84712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
84812a511c8SBjoern A. Zeeb 		return ("AES_CMAC");
84912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
85012a511c8SBjoern A. Zeeb 		return ("GCMP");
85112a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
85212a511c8SBjoern A. Zeeb 		return ("GCMP_256");
85312a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
85412a511c8SBjoern A. Zeeb 		return ("CCMP_256");
85512a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
85612a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_128");
85712a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
85812a511c8SBjoern A. Zeeb 		return ("BIP_GMAC_256");
85912a511c8SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
86012a511c8SBjoern A. Zeeb 		return ("BIP_CMAC_256");
86112a511c8SBjoern A. Zeeb 	default:
86212a511c8SBjoern A. Zeeb 		return ("??");
86312a511c8SBjoern A. Zeeb 	}
86412a511c8SBjoern A. Zeeb }
86512a511c8SBjoern A. Zeeb 
8666b4cac81SBjoern A. Zeeb static uint32_t
8676b4cac81SBjoern A. Zeeb lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
8686b4cac81SBjoern A. Zeeb {
8696b4cac81SBjoern A. Zeeb 
8706b4cac81SBjoern A. Zeeb 	switch (wlan_cipher_suite) {
8716b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP40:
8726b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
8736b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
8746b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_TKIP);
8756b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
876906521f8SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_AES_CCM);
8776b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_WEP104:
8786b4cac81SBjoern A. Zeeb 		return (IEEE80211_CRYPTO_WEP);
8796b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_AES_CMAC:
8806b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP:
8816b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_GCMP_256:
8826b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP_256:
8836b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
8846b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
8856b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
88612a511c8SBjoern A. Zeeb 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
88712a511c8SBjoern A. Zeeb 		    __func__,
88812a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
88912a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8906b4cac81SBjoern A. Zeeb 		break;
8916b4cac81SBjoern A. Zeeb 	default:
89212a511c8SBjoern A. Zeeb 		printf("%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
89312a511c8SBjoern A. Zeeb 		    __func__,
89412a511c8SBjoern A. Zeeb 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
89512a511c8SBjoern A. Zeeb 		    lkpi_cipher_suite_to_name(wlan_cipher_suite));
8966b4cac81SBjoern A. Zeeb 	}
8976b4cac81SBjoern A. Zeeb 
8986b4cac81SBjoern A. Zeeb 	return (0);
8996b4cac81SBjoern A. Zeeb }
9006b4cac81SBjoern A. Zeeb 
9016b4cac81SBjoern A. Zeeb static uint32_t
9026b4cac81SBjoern A. Zeeb lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
9036b4cac81SBjoern A. Zeeb {
9046b4cac81SBjoern A. Zeeb 
9056b4cac81SBjoern A. Zeeb 	switch (cipher) {
9066b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIP:
9076b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_TKIP);
9086b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_CCM:
9096b4cac81SBjoern A. Zeeb 		return (WLAN_CIPHER_SUITE_CCMP);
9106b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_WEP:
9116b4cac81SBjoern A. Zeeb 		if (keylen < 8)
9126b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP40);
9136b4cac81SBjoern A. Zeeb 		else
9146b4cac81SBjoern A. Zeeb 			return (WLAN_CIPHER_SUITE_WEP104);
9156b4cac81SBjoern A. Zeeb 		break;
9166b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_AES_OCB:
9176b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_TKIPMIC:
9186b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_CKIP:
9196b4cac81SBjoern A. Zeeb 	case IEEE80211_CIPHER_NONE:
9206b4cac81SBjoern A. Zeeb 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
9216b4cac81SBjoern A. Zeeb 		break;
9226b4cac81SBjoern A. Zeeb 	default:
9236b4cac81SBjoern A. Zeeb 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
9246b4cac81SBjoern A. Zeeb 	};
9256b4cac81SBjoern A. Zeeb 	return (0);
9266b4cac81SBjoern A. Zeeb }
9276b4cac81SBjoern A. Zeeb #endif
9286b4cac81SBjoern A. Zeeb 
9296b4cac81SBjoern A. Zeeb #ifdef __notyet__
9306b4cac81SBjoern A. Zeeb static enum ieee80211_sta_state
9316b4cac81SBjoern A. Zeeb lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
9326b4cac81SBjoern A. Zeeb {
9336b4cac81SBjoern A. Zeeb 
9346b4cac81SBjoern A. Zeeb 	/*
9356b4cac81SBjoern A. Zeeb 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
9366b4cac81SBjoern A. Zeeb 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
9376b4cac81SBjoern A. Zeeb 	 */
9386b4cac81SBjoern A. Zeeb 	switch (state) {
9396b4cac81SBjoern A. Zeeb 	case IEEE80211_S_INIT:
9406b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NOTEXIST);
9416b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SCAN:
9426b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_NONE);
9436b4cac81SBjoern A. Zeeb 	case IEEE80211_S_AUTH:
9446b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTH);
9456b4cac81SBjoern A. Zeeb 	case IEEE80211_S_ASSOC:
9466b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_ASSOC);
9476b4cac81SBjoern A. Zeeb 	case IEEE80211_S_RUN:
9486b4cac81SBjoern A. Zeeb 		return (IEEE80211_STA_AUTHORIZED);
9496b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CAC:
9506b4cac81SBjoern A. Zeeb 	case IEEE80211_S_CSA:
9516b4cac81SBjoern A. Zeeb 	case IEEE80211_S_SLEEP:
9526b4cac81SBjoern A. Zeeb 	default:
9536b4cac81SBjoern A. Zeeb 		UNIMPLEMENTED;
9546b4cac81SBjoern A. Zeeb 	};
9556b4cac81SBjoern A. Zeeb 
9566b4cac81SBjoern A. Zeeb 	return (IEEE80211_STA_NOTEXIST);
9576b4cac81SBjoern A. Zeeb }
9586b4cac81SBjoern A. Zeeb #endif
9596b4cac81SBjoern A. Zeeb 
9606b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9616b4cac81SBjoern A. Zeeb lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
9626b4cac81SBjoern A. Zeeb     struct ieee80211_channel *c)
9636b4cac81SBjoern A. Zeeb {
9646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
9656b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
9666b4cac81SBjoern A. Zeeb 	enum nl80211_band band;
9676b4cac81SBjoern A. Zeeb 	int i, nchans;
9686b4cac81SBjoern A. Zeeb 
9696b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
9706b4cac81SBjoern A. Zeeb 	band = lkpi_net80211_chan_to_nl80211_band(c);
9716b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[band] == NULL)
9726b4cac81SBjoern A. Zeeb 		return (NULL);
9736b4cac81SBjoern A. Zeeb 
9746b4cac81SBjoern A. Zeeb 	nchans = hw->wiphy->bands[band]->n_channels;
9756b4cac81SBjoern A. Zeeb 	if (nchans <= 0)
9766b4cac81SBjoern A. Zeeb 		return (NULL);
9776b4cac81SBjoern A. Zeeb 
9786b4cac81SBjoern A. Zeeb 	channels = hw->wiphy->bands[band]->channels;
9796b4cac81SBjoern A. Zeeb 	for (i = 0; i < nchans; i++) {
9806b4cac81SBjoern A. Zeeb 		if (channels[i].hw_value == c->ic_ieee)
9816b4cac81SBjoern A. Zeeb 			return (&channels[i]);
9826b4cac81SBjoern A. Zeeb 	}
9836b4cac81SBjoern A. Zeeb 
9846b4cac81SBjoern A. Zeeb 	return (NULL);
9856b4cac81SBjoern A. Zeeb }
9866b4cac81SBjoern A. Zeeb 
9872ac8a218SBjoern A. Zeeb #if 0
9886b4cac81SBjoern A. Zeeb static struct linuxkpi_ieee80211_channel *
9896b4cac81SBjoern A. Zeeb lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
9906b4cac81SBjoern A. Zeeb {
9916b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
9926b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
9936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
9946b4cac81SBjoern A. Zeeb 
9956b4cac81SBjoern A. Zeeb 	chan = NULL;
9966b4cac81SBjoern A. Zeeb 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
9976b4cac81SBjoern A. Zeeb 		c = ni->ni_chan;
9986b4cac81SBjoern A. Zeeb 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
9996b4cac81SBjoern A. Zeeb 		c = ic->ic_bsschan;
10006b4cac81SBjoern A. Zeeb 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
10016b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
10026b4cac81SBjoern A. Zeeb 	else
10036b4cac81SBjoern A. Zeeb 		c = NULL;
10046b4cac81SBjoern A. Zeeb 
10056b4cac81SBjoern A. Zeeb 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
10066b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
10076b4cac81SBjoern A. Zeeb 		chan = lkpi_find_lkpi80211_chan(lhw, c);
10086b4cac81SBjoern A. Zeeb 	}
10096b4cac81SBjoern A. Zeeb 
10106b4cac81SBjoern A. Zeeb 	return (chan);
10116b4cac81SBjoern A. Zeeb }
10122ac8a218SBjoern A. Zeeb #endif
10136b4cac81SBjoern A. Zeeb 
10142e183d99SBjoern A. Zeeb struct linuxkpi_ieee80211_channel *
10152e183d99SBjoern A. Zeeb linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
10162e183d99SBjoern A. Zeeb {
10172e183d99SBjoern A. Zeeb 	enum nl80211_band band;
10182e183d99SBjoern A. Zeeb 
10192e183d99SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
10202e183d99SBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
10212e183d99SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
10222e183d99SBjoern A. Zeeb 		int i;
10232e183d99SBjoern A. Zeeb 
10242e183d99SBjoern A. Zeeb 		supband = wiphy->bands[band];
10252e183d99SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
10262e183d99SBjoern A. Zeeb 			continue;
10272e183d99SBjoern A. Zeeb 
10282e183d99SBjoern A. Zeeb 		channels = supband->channels;
10292e183d99SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
10302e183d99SBjoern A. Zeeb 			if (channels[i].center_freq == freq)
10312e183d99SBjoern A. Zeeb 				return (&channels[i]);
10322e183d99SBjoern A. Zeeb 		}
10332e183d99SBjoern A. Zeeb 	}
10342e183d99SBjoern A. Zeeb 
10352e183d99SBjoern A. Zeeb 	return (NULL);
10362e183d99SBjoern A. Zeeb }
10372e183d99SBjoern A. Zeeb 
1038b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
10396b4cac81SBjoern A. Zeeb static int
104011db70b6SBjoern A. Zeeb lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
104111db70b6SBjoern A. Zeeb     struct lkpi_sta *lsta)
104211db70b6SBjoern A. Zeeb {
104311db70b6SBjoern A. Zeeb 	int error;
104411db70b6SBjoern A. Zeeb 
104511db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto)
104611db70b6SBjoern A. Zeeb 		return (0);
104711db70b6SBjoern A. Zeeb 
104811db70b6SBjoern A. Zeeb 	lockdep_assert_wiphy(hw->wiphy);
104911db70b6SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
105011db70b6SBjoern A. Zeeb 
105111db70b6SBjoern A. Zeeb 	error = 0;
105211db70b6SBjoern A. Zeeb 	for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
105311db70b6SBjoern A. Zeeb 		struct ieee80211_key_conf *kc;
105411db70b6SBjoern A. Zeeb 		int err;
105511db70b6SBjoern A. Zeeb 
105611db70b6SBjoern A. Zeeb 		if (lsta->kc[keyix] == NULL)
105711db70b6SBjoern A. Zeeb 			continue;
105811db70b6SBjoern A. Zeeb 		kc = lsta->kc[keyix];
105911db70b6SBjoern A. Zeeb 
106011db70b6SBjoern A. Zeeb 		err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
106111db70b6SBjoern A. Zeeb 		    LSTA_TO_STA(lsta), kc);
106211db70b6SBjoern A. Zeeb 		if (err != 0) {
106311db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
106411db70b6SBjoern A. Zeeb 			    "sta %6D failed: %d\n", __func__, DISABLE_KEY,
106511db70b6SBjoern A. Zeeb 			    "DISABLE", lsta->sta.addr, ":", err);
106611db70b6SBjoern A. Zeeb 			error++;
106711db70b6SBjoern A. Zeeb 
106811db70b6SBjoern A. Zeeb 			/*
106911db70b6SBjoern A. Zeeb 			 * If we free the key here we will never be able to get it
107011db70b6SBjoern A. Zeeb 			 * removed from the driver/fw which will likely make us
107111db70b6SBjoern A. Zeeb 			 * crash (firmware).
107211db70b6SBjoern A. Zeeb 			 */
107311db70b6SBjoern A. Zeeb 			continue;
107411db70b6SBjoern A. Zeeb 		}
107511db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
107611db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
107711db70b6SBjoern A. Zeeb 			ic_printf(lsta->ni->ni_ic, "%s: set_key cmd %d(%s) for "
107811db70b6SBjoern A. Zeeb 			    "sta %6D succeeded: keyidx %u hw_key_idx %u flags %#10x\n",
107911db70b6SBjoern A. Zeeb 			    __func__, DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
108011db70b6SBjoern A. Zeeb 			    kc->keyidx, kc->hw_key_idx, kc->flags);
108111db70b6SBjoern A. Zeeb #endif
108211db70b6SBjoern A. Zeeb 
108311db70b6SBjoern A. Zeeb 		lsta->kc[keyix] = NULL;
108411db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
108511db70b6SBjoern A. Zeeb 	}
108611db70b6SBjoern A. Zeeb 	ieee80211_free_node(lsta->ni);
108711db70b6SBjoern A. Zeeb 	return (error);
108811db70b6SBjoern A. Zeeb }
108911db70b6SBjoern A. Zeeb 
109011db70b6SBjoern A. Zeeb static int
109111db70b6SBjoern A. Zeeb _lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
10926b4cac81SBjoern A. Zeeb {
10936b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
10946b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
10956b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
10966b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
109711db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
10986b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
10996b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
11006b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
11016b4cac81SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
110211db70b6SBjoern A. Zeeb 	struct ieee80211_node_table *nt;
11036b4cac81SBjoern A. Zeeb 	int error;
110411db70b6SBjoern A. Zeeb 	bool islocked;
11056b4cac81SBjoern A. Zeeb 
11066b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
11076b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
11086b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
11096b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
11106b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
11116b4cac81SBjoern A. Zeeb 
111211db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
111311db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
111411db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
111511db70b6SBjoern A. Zeeb 		return (0);
111611db70b6SBjoern A. Zeeb 	}
111711db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
111811db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
111911db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
112011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
112111db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
112211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
112311db70b6SBjoern A. Zeeb 		return (0);
112411db70b6SBjoern A. Zeeb 	}
112511db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
112611db70b6SBjoern A. Zeeb 
112711db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] == NULL) {
112811db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
112911db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
113011db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and no key information, "
113111db70b6SBjoern A. Zeeb 			    "keyidx %u wk_macaddr %6D; returning success\n",
113211db70b6SBjoern A. Zeeb 			    __func__, sta->addr, ":",
113311db70b6SBjoern A. Zeeb 			    k->wk_keyix, k->wk_macaddr, ":");
113411db70b6SBjoern A. Zeeb #endif
113511db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
113611db70b6SBjoern A. Zeeb 		return (1);
113711db70b6SBjoern A. Zeeb 	}
113811db70b6SBjoern A. Zeeb 
113911db70b6SBjoern A. Zeeb 	/* This is inconsistent net80211 locking to be fixed one day. */
114011db70b6SBjoern A. Zeeb 	nt = &ic->ic_sta;
114111db70b6SBjoern A. Zeeb 	islocked = IEEE80211_NODE_IS_LOCKED(nt);
114211db70b6SBjoern A. Zeeb 	if (islocked)
114311db70b6SBjoern A. Zeeb 		IEEE80211_NODE_UNLOCK(nt);
114411db70b6SBjoern A. Zeeb 
114511db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
114611db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
114711db70b6SBjoern A. Zeeb 	/* Re-check under lock. */
114811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
114911db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
115011db70b6SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
115111db70b6SBjoern A. Zeeb 			ic_printf(ic, "%s: sta %6D and key information vanished, "
115211db70b6SBjoern A. Zeeb 			    "returning success\n", __func__, sta->addr, ":");
115311db70b6SBjoern A. Zeeb #endif
115411db70b6SBjoern A. Zeeb 		error = 1;
115511db70b6SBjoern A. Zeeb 		goto out;
115611db70b6SBjoern A. Zeeb 	}
115711db70b6SBjoern A. Zeeb 
115811db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
115911db70b6SBjoern A. Zeeb 	if (error != 0) {
116011db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
116111db70b6SBjoern A. Zeeb 		    __func__, DISABLE_KEY, "DISABLE", sta->addr, ":", error);
116211db70b6SBjoern A. Zeeb 		error = 0;
116311db70b6SBjoern A. Zeeb 		goto out;
116411db70b6SBjoern A. Zeeb 	}
116511db70b6SBjoern A. Zeeb 
116611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
116711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
116811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
116911db70b6SBjoern A. Zeeb 		    "keyidx %u hw_key_idx %u flags %#10x\n", __func__,
117011db70b6SBjoern A. Zeeb 		    DISABLE_KEY, "DISABLE", sta->addr, ":",
117111db70b6SBjoern A. Zeeb 		    kc->keyidx, kc->hw_key_idx, kc->flags);
117211db70b6SBjoern A. Zeeb #endif
117311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = NULL;
117411db70b6SBjoern A. Zeeb 	free(kc, M_LKPI80211);
117511db70b6SBjoern A. Zeeb 	error = 1;
117611db70b6SBjoern A. Zeeb out:
117711db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
117811db70b6SBjoern A. Zeeb 	if (islocked)
117911db70b6SBjoern A. Zeeb 		IEEE80211_NODE_LOCK(nt);
118011db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
118111db70b6SBjoern A. Zeeb 	return (error);
118211db70b6SBjoern A. Zeeb }
118311db70b6SBjoern A. Zeeb 
118411db70b6SBjoern A. Zeeb static int
118511db70b6SBjoern A. Zeeb lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
118611db70b6SBjoern A. Zeeb {
118711db70b6SBjoern A. Zeeb 
118811db70b6SBjoern A. Zeeb 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
118911db70b6SBjoern A. Zeeb 	/* See also lkpi_sta_del_keys() these days. */
119011db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_delete(vap, k));
119111db70b6SBjoern A. Zeeb }
119211db70b6SBjoern A. Zeeb 
119311db70b6SBjoern A. Zeeb static int
119411db70b6SBjoern A. Zeeb _lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
119511db70b6SBjoern A. Zeeb {
119611db70b6SBjoern A. Zeeb 	struct ieee80211com *ic;
119711db70b6SBjoern A. Zeeb 	struct lkpi_hw *lhw;
119811db70b6SBjoern A. Zeeb 	struct ieee80211_hw *hw;
119911db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
120011db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
120111db70b6SBjoern A. Zeeb 	struct ieee80211_vif *vif;
120211db70b6SBjoern A. Zeeb 	struct ieee80211_sta *sta;
120311db70b6SBjoern A. Zeeb 	struct ieee80211_node *ni;
120411db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
120511db70b6SBjoern A. Zeeb 	uint32_t lcipher;
120611db70b6SBjoern A. Zeeb 	int error;
120711db70b6SBjoern A. Zeeb 
120811db70b6SBjoern A. Zeeb 	ic = vap->iv_ic;
120911db70b6SBjoern A. Zeeb 	lhw = ic->ic_softc;
121011db70b6SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
121111db70b6SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
121211db70b6SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
121311db70b6SBjoern A. Zeeb 
121411db70b6SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
121511db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
121611db70b6SBjoern A. Zeeb 		    __func__, vap->iv_bss, vap);
121711db70b6SBjoern A. Zeeb 		return (0);
121811db70b6SBjoern A. Zeeb 	}
121911db70b6SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
122011db70b6SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
122111db70b6SBjoern A. Zeeb 	if (lsta == NULL) {
122211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
122311db70b6SBjoern A. Zeeb 		    __func__, ni, ni->ni_bssid, ":");
122411db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
122511db70b6SBjoern A. Zeeb 		return (0);
122611db70b6SBjoern A. Zeeb 	}
122711db70b6SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
122811db70b6SBjoern A. Zeeb 
122911db70b6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
123011db70b6SBjoern A. Zeeb 	if (lsta->kc[k->wk_keyix] != NULL) {
123111db70b6SBjoern A. Zeeb 		IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
123211db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: sta %6D found with key information\n",
123311db70b6SBjoern A. Zeeb 		    __func__, sta->addr, ":");
123411db70b6SBjoern A. Zeeb 		kc = lsta->kc[k->wk_keyix];
123511db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
123611db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
123711db70b6SBjoern A. Zeeb 		kc = NULL;	/* safeguard */
123811db70b6SBjoern A. Zeeb 	}
123911db70b6SBjoern A. Zeeb 
124011db70b6SBjoern A. Zeeb 	lcipher = lkpi_net80211_to_l80211_cipher_suite(
12416b4cac81SBjoern A. Zeeb 	    k->wk_cipher->ic_cipher, k->wk_keylen);
124211db70b6SBjoern A. Zeeb 	switch (lcipher) {
124311db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
124411db70b6SBjoern A. Zeeb 		break;
124511db70b6SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
124611db70b6SBjoern A. Zeeb 	default:
124712a511c8SBjoern A. Zeeb 		ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
124812a511c8SBjoern A. Zeeb 		    __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
124911db70b6SBjoern A. Zeeb 		IMPROVE();
125011db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
125111db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
125211db70b6SBjoern A. Zeeb 		return (0);
125311db70b6SBjoern A. Zeeb 	}
125411db70b6SBjoern A. Zeeb 
125511db70b6SBjoern A. Zeeb 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
125611db70b6SBjoern A. Zeeb 	kc->_k = k;		/* Save the pointer to net80211. */
125711db70b6SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
125811db70b6SBjoern A. Zeeb 	kc->cipher = lcipher;
12596b4cac81SBjoern A. Zeeb 	kc->keyidx = k->wk_keyix;
12606b4cac81SBjoern A. Zeeb #if 0
12616b4cac81SBjoern A. Zeeb 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
12626b4cac81SBjoern A. Zeeb #endif
12636b4cac81SBjoern A. Zeeb 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
12646b4cac81SBjoern A. Zeeb 	kc->keylen = k->wk_keylen;
12656b4cac81SBjoern A. Zeeb 	memcpy(kc->key, k->wk_key, k->wk_keylen);
12666b4cac81SBjoern A. Zeeb 
126711db70b6SBjoern A. Zeeb 	if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
126811db70b6SBjoern A. Zeeb 		kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
126911db70b6SBjoern A. Zeeb 	if (k->wk_flags & IEEE80211_KEY_GROUP)
127011db70b6SBjoern A. Zeeb 		kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
127111db70b6SBjoern A. Zeeb 
12726b4cac81SBjoern A. Zeeb 	switch (kc->cipher) {
12736b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_CCMP:
12746b4cac81SBjoern A. Zeeb 		kc->iv_len = k->wk_cipher->ic_header;
12756b4cac81SBjoern A. Zeeb 		kc->icv_len = k->wk_cipher->ic_trailer;
12766b4cac81SBjoern A. Zeeb 		break;
12776b4cac81SBjoern A. Zeeb 	case WLAN_CIPHER_SUITE_TKIP:
12786b4cac81SBjoern A. Zeeb 	default:
127911db70b6SBjoern A. Zeeb 		/* currently UNREACH */
12806b4cac81SBjoern A. Zeeb 		IMPROVE();
128111db70b6SBjoern A. Zeeb 		break;
12826b4cac81SBjoern A. Zeeb 	};
128311db70b6SBjoern A. Zeeb 	lsta->kc[k->wk_keyix] = kc;
12846b4cac81SBjoern A. Zeeb 
128511db70b6SBjoern A. Zeeb 	error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
128611db70b6SBjoern A. Zeeb 	if (error != 0) {
128711db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D failed: %d\n",
128811db70b6SBjoern A. Zeeb 		    __func__, SET_KEY, "SET", sta->addr, ":", error);
128911db70b6SBjoern A. Zeeb 		lsta->kc[k->wk_keyix] = NULL;
129011db70b6SBjoern A. Zeeb 		free(kc, M_LKPI80211);
129111db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
129211db70b6SBjoern A. Zeeb 		ieee80211_free_node(ni);
129311db70b6SBjoern A. Zeeb 		return (0);
12946b4cac81SBjoern A. Zeeb 	}
12956b4cac81SBjoern A. Zeeb 
129611db70b6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
129711db70b6SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
129811db70b6SBjoern A. Zeeb 		ic_printf(ic, "%s: set_key cmd %d(%s) for sta %6D succeeded: "
129911db70b6SBjoern A. Zeeb 		    "kc %p keyidx %u hw_key_idx %u flags %#010x\n", __func__,
130011db70b6SBjoern A. Zeeb 		    SET_KEY, "SET", sta->addr, ":",
130111db70b6SBjoern A. Zeeb 		    kc, kc->keyidx, kc->hw_key_idx, kc->flags);
130211db70b6SBjoern A. Zeeb #endif
130311db70b6SBjoern A. Zeeb 
130411db70b6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
130511db70b6SBjoern A. Zeeb 	ieee80211_free_node(ni);
13066b4cac81SBjoern A. Zeeb 	return (1);
13076b4cac81SBjoern A. Zeeb }
13086b4cac81SBjoern A. Zeeb 
13096b4cac81SBjoern A. Zeeb static  int
13106b4cac81SBjoern A. Zeeb lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
13116b4cac81SBjoern A. Zeeb {
13126b4cac81SBjoern A. Zeeb 
131311db70b6SBjoern A. Zeeb 	return (_lkpi_iv_key_set(vap, k));
13146b4cac81SBjoern A. Zeeb }
13156b4cac81SBjoern A. Zeeb #endif
13166b4cac81SBjoern A. Zeeb 
13176b4cac81SBjoern A. Zeeb static u_int
13186b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
13196b4cac81SBjoern A. Zeeb {
13206b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list *mc_list;
13216b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13226b4cac81SBjoern A. Zeeb 
13236b4cac81SBjoern A. Zeeb 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
13246b4cac81SBjoern A. Zeeb 	    __func__, arg, sdl, cnt));
13256b4cac81SBjoern A. Zeeb 
13266b4cac81SBjoern A. Zeeb 	mc_list = arg;
13276b4cac81SBjoern A. Zeeb 	/* If it is on the list already skip it. */
13286b4cac81SBjoern A. Zeeb 	netdev_hw_addr_list_for_each(addr, mc_list) {
13296b4cac81SBjoern A. Zeeb 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
13306b4cac81SBjoern A. Zeeb 			return (0);
13316b4cac81SBjoern A. Zeeb 	}
13326b4cac81SBjoern A. Zeeb 
13336b4cac81SBjoern A. Zeeb 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
13346b4cac81SBjoern A. Zeeb 	if (addr == NULL)
13356b4cac81SBjoern A. Zeeb 		return (0);
13366b4cac81SBjoern A. Zeeb 
13376b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&addr->addr_list);
13386b4cac81SBjoern A. Zeeb 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
13396b4cac81SBjoern A. Zeeb 	/* XXX this should be a netdev function? */
13406b4cac81SBjoern A. Zeeb 	list_add(&addr->addr_list, &mc_list->addr_list);
13416b4cac81SBjoern A. Zeeb 	mc_list->count++;
13426b4cac81SBjoern A. Zeeb 
13439d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13449d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13456b4cac81SBjoern A. Zeeb 		printf("%s:%d: mc_list count %d: added %6D\n",
13466b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
13479d9ba2b7SBjoern A. Zeeb #endif
13486b4cac81SBjoern A. Zeeb 
13496b4cac81SBjoern A. Zeeb 	return (1);
13506b4cac81SBjoern A. Zeeb }
13516b4cac81SBjoern A. Zeeb 
13526b4cac81SBjoern A. Zeeb static void
13536b4cac81SBjoern A. Zeeb lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
13546b4cac81SBjoern A. Zeeb {
13556b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
13566b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
13576b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr_list mc_list;
13586b4cac81SBjoern A. Zeeb 	struct list_head *le, *next;
13596b4cac81SBjoern A. Zeeb 	struct netdev_hw_addr *addr;
13606b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
13616b4cac81SBjoern A. Zeeb 	u64 mc;
13626b4cac81SBjoern A. Zeeb 	unsigned int changed_flags, total_flags;
13636b4cac81SBjoern A. Zeeb 
13646b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
13656b4cac81SBjoern A. Zeeb 
13666b4cac81SBjoern A. Zeeb 	if (lhw->ops->prepare_multicast == NULL ||
13676b4cac81SBjoern A. Zeeb 	    lhw->ops->configure_filter == NULL)
13686b4cac81SBjoern A. Zeeb 		return;
13696b4cac81SBjoern A. Zeeb 
13706b4cac81SBjoern A. Zeeb 	if (!lhw->update_mc && !force)
13716b4cac81SBjoern A. Zeeb 		return;
13726b4cac81SBjoern A. Zeeb 
13736b4cac81SBjoern A. Zeeb 	changed_flags = total_flags = 0;
13746b4cac81SBjoern A. Zeeb 	mc_list.count = 0;
13756b4cac81SBjoern A. Zeeb 	INIT_LIST_HEAD(&mc_list.addr_list);
13766b4cac81SBjoern A. Zeeb 	if (ic->ic_allmulti == 0) {
13776b4cac81SBjoern A. Zeeb 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
13786b4cac81SBjoern A. Zeeb 			if_foreach_llmaddr(vap->iv_ifp,
13796b4cac81SBjoern A. Zeeb 			    lkpi_ic_update_mcast_copy, &mc_list);
13806b4cac81SBjoern A. Zeeb 	} else {
13816b4cac81SBjoern A. Zeeb 		changed_flags |= FIF_ALLMULTI;
13826b4cac81SBjoern A. Zeeb 	}
13836b4cac81SBjoern A. Zeeb 
13846b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
13856b4cac81SBjoern A. Zeeb 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
13866b4cac81SBjoern A. Zeeb 	/*
13876b4cac81SBjoern A. Zeeb 	 * XXX-BZ make sure to get this sorted what is a change,
13886b4cac81SBjoern A. Zeeb 	 * what gets all set; what was already set?
13896b4cac81SBjoern A. Zeeb 	 */
13906b4cac81SBjoern A. Zeeb 	total_flags = changed_flags;
13916b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
13926b4cac81SBjoern A. Zeeb 
13939d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
13949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
13956b4cac81SBjoern A. Zeeb 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
13966b4cac81SBjoern A. Zeeb 		    __func__, changed_flags, mc_list.count, total_flags);
13979d9ba2b7SBjoern A. Zeeb #endif
13986b4cac81SBjoern A. Zeeb 
13996b4cac81SBjoern A. Zeeb 	if (mc_list.count != 0) {
14006b4cac81SBjoern A. Zeeb 		list_for_each_safe(le, next, &mc_list.addr_list) {
14016b4cac81SBjoern A. Zeeb 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
14026b4cac81SBjoern A. Zeeb 			free(addr, M_LKPI80211);
14036b4cac81SBjoern A. Zeeb 			mc_list.count--;
14046b4cac81SBjoern A. Zeeb 		}
14056b4cac81SBjoern A. Zeeb 	}
14066b4cac81SBjoern A. Zeeb 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
14076b4cac81SBjoern A. Zeeb 	    __func__, &mc_list, mc_list.count));
14086b4cac81SBjoern A. Zeeb }
14096b4cac81SBjoern A. Zeeb 
1410fa8f007dSBjoern A. Zeeb static enum ieee80211_bss_changed
1411fa8f007dSBjoern A. Zeeb lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1412fa8f007dSBjoern A. Zeeb     struct ieee80211vap *vap, const char *_f, int _l)
1413fa8f007dSBjoern A. Zeeb {
1414fa8f007dSBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
1415fa8f007dSBjoern A. Zeeb 
1416fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
1417fa8f007dSBjoern A. Zeeb 
14189d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14199d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1420fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1421fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1422ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1423fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1424616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1425fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1426fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1427fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1428fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1429ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14309d9ba2b7SBjoern A. Zeeb #endif
1431fa8f007dSBjoern A. Zeeb 
1432fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
1433fa8f007dSBjoern A. Zeeb 		vif->bss_conf.beacon_int = ni->ni_intval;
1434fa8f007dSBjoern A. Zeeb 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1435fa8f007dSBjoern A. Zeeb 		if (vif->bss_conf.beacon_int < 16)
1436fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int = 16;
1437fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INT;
1438fa8f007dSBjoern A. Zeeb 	}
1439fa8f007dSBjoern A. Zeeb 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
1440fa8f007dSBjoern A. Zeeb 	    vap->iv_dtim_period > 0) {
1441fa8f007dSBjoern A. Zeeb 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
1442fa8f007dSBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_BEACON_INFO;
1443fa8f007dSBjoern A. Zeeb 	}
1444fa8f007dSBjoern A. Zeeb 
1445fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
1446fa8f007dSBjoern A. Zeeb 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1447fa8f007dSBjoern A. Zeeb 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1448fa8f007dSBjoern A. Zeeb 
14499d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
14509d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
1451fa8f007dSBjoern A. Zeeb 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1452fa8f007dSBjoern A. Zeeb 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1453ac1d519cSBjoern A. Zeeb 		    "sync_device_ts %u bss_changed %#010jx\n",
1454fa8f007dSBjoern A. Zeeb 			__func__, __LINE__, _f, _l,
1455616e1330SBjoern A. Zeeb 			vif->cfg.assoc, vif->cfg.aid,
1456fa8f007dSBjoern A. Zeeb 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1457fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_dtim_count,
1458fa8f007dSBjoern A. Zeeb 			(uintmax_t)vif->bss_conf.sync_tsf,
1459fa8f007dSBjoern A. Zeeb 			vif->bss_conf.sync_device_ts,
1460ac1d519cSBjoern A. Zeeb 			(uintmax_t)bss_changed);
14619d9ba2b7SBjoern A. Zeeb #endif
1462fa8f007dSBjoern A. Zeeb 
1463fa8f007dSBjoern A. Zeeb 	return (bss_changed);
1464fa8f007dSBjoern A. Zeeb }
1465fa8f007dSBjoern A. Zeeb 
14666b4cac81SBjoern A. Zeeb static void
14676b4cac81SBjoern A. Zeeb lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
14686b4cac81SBjoern A. Zeeb {
14696b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
14706b4cac81SBjoern A. Zeeb 	int error;
14718ac540d3SBjoern A. Zeeb 	bool cancel;
14726b4cac81SBjoern A. Zeeb 
14738ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
14748ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
14758ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
14768ac540d3SBjoern A. Zeeb 	if (!cancel)
14776b4cac81SBjoern A. Zeeb 		return;
14786b4cac81SBjoern A. Zeeb 
14796b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
14806b4cac81SBjoern A. Zeeb 
1481bec76628SBjoern A. Zeeb 	IEEE80211_UNLOCK(lhw->ic);
1482bec76628SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
14836b4cac81SBjoern A. Zeeb 	/* Need to cancel the scan. */
14846b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
14858ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
14866b4cac81SBjoern A. Zeeb 
14876b4cac81SBjoern A. Zeeb 	/* Need to make sure we see ieee80211_scan_completed. */
14888ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
14898ac540d3SBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
14908ac540d3SBjoern A. Zeeb 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
14918ac540d3SBjoern A. Zeeb 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
14928ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
14938ac540d3SBjoern A. Zeeb 
1494bec76628SBjoern A. Zeeb 	IEEE80211_LOCK(lhw->ic);
14956b4cac81SBjoern A. Zeeb 
14968ac540d3SBjoern A. Zeeb 	if (cancel)
14976b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
14986b4cac81SBjoern A. Zeeb 		    __func__, error, lhw, vif);
14996b4cac81SBjoern A. Zeeb }
15006b4cac81SBjoern A. Zeeb 
15016b4cac81SBjoern A. Zeeb static void
1502086be6a8SBjoern A. Zeeb lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1503086be6a8SBjoern A. Zeeb {
1504086be6a8SBjoern A. Zeeb 	struct lkpi_hw *lhw;
1505086be6a8SBjoern A. Zeeb 	int error;
1506086be6a8SBjoern A. Zeeb 	bool old;
1507086be6a8SBjoern A. Zeeb 
1508086be6a8SBjoern A. Zeeb 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
1509086be6a8SBjoern A. Zeeb 	if (old == new)
1510086be6a8SBjoern A. Zeeb 		return;
1511086be6a8SBjoern A. Zeeb 
1512086be6a8SBjoern A. Zeeb 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
1513086be6a8SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1514086be6a8SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
1515086be6a8SBjoern A. Zeeb 		lhw = HW_TO_LHW(hw);
1516086be6a8SBjoern A. Zeeb 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1517086be6a8SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1518086be6a8SBjoern A. Zeeb 	}
1519086be6a8SBjoern A. Zeeb }
1520086be6a8SBjoern A. Zeeb 
15215a4d2461SBjoern A. Zeeb static enum ieee80211_bss_changed
15226b4cac81SBjoern A. Zeeb lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
15236b4cac81SBjoern A. Zeeb     struct lkpi_hw *lhw)
15246b4cac81SBjoern A. Zeeb {
15255a4d2461SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
15265a4d2461SBjoern A. Zeeb 
15275a4d2461SBjoern A. Zeeb 	changed = 0;
15286b4cac81SBjoern A. Zeeb 	sta->aid = 0;
1529616e1330SBjoern A. Zeeb 	if (vif->cfg.assoc) {
15306b4cac81SBjoern A. Zeeb 
15316b4cac81SBjoern A. Zeeb 		lhw->update_mc = true;
15326b4cac81SBjoern A. Zeeb 		lkpi_update_mcast_filter(lhw->ic, true);
15336b4cac81SBjoern A. Zeeb 
1534616e1330SBjoern A. Zeeb 		vif->cfg.assoc = false;
1535616e1330SBjoern A. Zeeb 		vif->cfg.aid = 0;
15366b4cac81SBjoern A. Zeeb 		changed |= BSS_CHANGED_ASSOC;
15376b4cac81SBjoern A. Zeeb 		IMPROVE();
1538086be6a8SBjoern A. Zeeb 
15395a4d2461SBjoern A. Zeeb 		/*
15405a4d2461SBjoern A. Zeeb 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
15415a4d2461SBjoern A. Zeeb 		 * assoc = false right away here will remove the sta from
15425a4d2461SBjoern A. Zeeb 		 * firmware for iwlwifi.
15435a4d2461SBjoern A. Zeeb 		 * We no longer do this but only return the BSS_CHNAGED value.
15445a4d2461SBjoern A. Zeeb 		 * The caller is responsible for removing the sta gong to
15455a4d2461SBjoern A. Zeeb 		 * IEEE80211_STA_NOTEXIST and then executing the
15465a4d2461SBjoern A. Zeeb 		 * bss_info_changed() update.
15475a4d2461SBjoern A. Zeeb 		 * See lkpi_sta_run_to_init() for more detailed comment.
15485a4d2461SBjoern A. Zeeb 		 */
15496b4cac81SBjoern A. Zeeb 	}
15505a4d2461SBjoern A. Zeeb 
15515a4d2461SBjoern A. Zeeb 	return (changed);
15526b4cac81SBjoern A. Zeeb }
15536b4cac81SBjoern A. Zeeb 
15546b4cac81SBjoern A. Zeeb static void
15556b4cac81SBjoern A. Zeeb lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
15566b4cac81SBjoern A. Zeeb     bool dequeue_seen, bool no_emptyq)
15576b4cac81SBjoern A. Zeeb {
15586b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
15596b4cac81SBjoern A. Zeeb 	int tid;
1560eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
15616b4cac81SBjoern A. Zeeb 
15626b4cac81SBjoern A. Zeeb 	/* Wake up all queues to know they are allocated in the driver. */
15636b4cac81SBjoern A. Zeeb 	for (tid = 0; tid < nitems(sta->txq); tid++) {
15646b4cac81SBjoern A. Zeeb 
15656b4cac81SBjoern A. Zeeb 		if (tid == IEEE80211_NUM_TIDS) {
15666b4cac81SBjoern A. Zeeb 			IMPROVE("station specific?");
15676b4cac81SBjoern A. Zeeb 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
15686b4cac81SBjoern A. Zeeb 				continue;
15696b4cac81SBjoern A. Zeeb 		} else if (tid >= hw->queues)
15706b4cac81SBjoern A. Zeeb 			continue;
15716b4cac81SBjoern A. Zeeb 
15726b4cac81SBjoern A. Zeeb 		if (sta->txq[tid] == NULL)
15736b4cac81SBjoern A. Zeeb 			continue;
15746b4cac81SBjoern A. Zeeb 
15756b4cac81SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
15766b4cac81SBjoern A. Zeeb 		if (dequeue_seen && !ltxq->seen_dequeue)
15776b4cac81SBjoern A. Zeeb 			continue;
15786b4cac81SBjoern A. Zeeb 
1579eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_LOCK(ltxq);
1580eac3646fSBjoern A. Zeeb 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1581eac3646fSBjoern A. Zeeb 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1582eac3646fSBjoern A. Zeeb 		if (no_emptyq && ltxq_empty)
15836b4cac81SBjoern A. Zeeb 			continue;
15846b4cac81SBjoern A. Zeeb 
15856b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
15866b4cac81SBjoern A. Zeeb 	}
15876b4cac81SBjoern A. Zeeb }
15886b4cac81SBjoern A. Zeeb 
158988665349SBjoern A. Zeeb /*
159088665349SBjoern A. Zeeb  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
159188665349SBjoern A. Zeeb  * packet.  The problem is that the state machine functions tend to hold the
159288665349SBjoern A. Zeeb  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
159388665349SBjoern A. Zeeb  * We call this after dropping the ic lock and before acquiring the LHW lock.
159488665349SBjoern A. Zeeb  * we make sure no further packets are queued and if they are queued the task
159588665349SBjoern A. Zeeb  * will finish or be cancelled.  At the end if a packet is left we manually
159688665349SBjoern A. Zeeb  * send it.  scan_to_auth() would re-enable sending if the lsta would be
159788665349SBjoern A. Zeeb  * re-used.
159888665349SBjoern A. Zeeb  */
159988665349SBjoern A. Zeeb static void
160088665349SBjoern A. Zeeb lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
160188665349SBjoern A. Zeeb {
160288665349SBjoern A. Zeeb 	struct mbufq mq;
160388665349SBjoern A. Zeeb 	struct mbuf *m;
160488665349SBjoern A. Zeeb 	int len;
160588665349SBjoern A. Zeeb 
160688665349SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
160788665349SBjoern A. Zeeb 
160888665349SBjoern A. Zeeb 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
160988665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
161088665349SBjoern A. Zeeb 	lsta->txq_ready = false;
161188665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
161288665349SBjoern A. Zeeb 
161388665349SBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
161488665349SBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
161588665349SBjoern A. Zeeb 
161688665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
161788665349SBjoern A. Zeeb 	len = mbufq_len(&lsta->txq);
161888665349SBjoern A. Zeeb 	if (len <= 0) {
161988665349SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
162088665349SBjoern A. Zeeb 		return;
162188665349SBjoern A. Zeeb 	}
162288665349SBjoern A. Zeeb 
162388665349SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
162488665349SBjoern A. Zeeb 	mbufq_concat(&mq, &lsta->txq);
162588665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
162688665349SBjoern A. Zeeb 
162788665349SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
162888665349SBjoern A. Zeeb 	while (m != NULL) {
162988665349SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
163088665349SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
163188665349SBjoern A. Zeeb 	}
163288665349SBjoern A. Zeeb }
163388665349SBjoern A. Zeeb 
16346b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
16356b4cac81SBjoern A. Zeeb 
16366b4cac81SBjoern A. Zeeb static int
16376b4cac81SBjoern A. Zeeb lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16386b4cac81SBjoern A. Zeeb {
16396b4cac81SBjoern A. Zeeb 
16406b4cac81SBjoern A. Zeeb 	return (0);
16416b4cac81SBjoern A. Zeeb }
16426b4cac81SBjoern A. Zeeb 
16436b4cac81SBjoern A. Zeeb /* lkpi_iv_newstate() handles the stop scan case generally. */
16446b4cac81SBjoern A. Zeeb #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
16456b4cac81SBjoern A. Zeeb 
16466b4cac81SBjoern A. Zeeb static int
16476b4cac81SBjoern A. Zeeb lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
16486b4cac81SBjoern A. Zeeb {
16496b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
1650c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
1651d1af434dSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx_conf;
16526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
16536b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
16546b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
16556b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
16566b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
16576b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
16586b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
16596b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
16606b4cac81SBjoern A. Zeeb 	uint32_t changed;
16616b4cac81SBjoern A. Zeeb 	int error;
16626b4cac81SBjoern A. Zeeb 
16632ac8a218SBjoern A. Zeeb 	/*
16642ac8a218SBjoern A. Zeeb 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
16652ac8a218SBjoern A. Zeeb 	 * For all later (STATE >= AUTH) functions we need to use the lvif
16662ac8a218SBjoern A. Zeeb 	 * cache which will be tracked even through (*iv_update_bss)().
16672ac8a218SBjoern A. Zeeb 	 */
16682ac8a218SBjoern A. Zeeb 
16692ac8a218SBjoern A. Zeeb 	if (vap->iv_bss == NULL) {
16702ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
16712ac8a218SBjoern A. Zeeb 		return (EINVAL);
16722ac8a218SBjoern A. Zeeb 	}
16730936c648SBjoern A. Zeeb 	/*
16740936c648SBjoern A. Zeeb 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
16750936c648SBjoern A. Zeeb 	 * once we unlock here.  This is due to net80211 allowing state changes
16760936c648SBjoern A. Zeeb 	 * and new join1() despite having an active node as well as due to
16770936c648SBjoern A. Zeeb 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
16780936c648SBjoern A. Zeeb 	 */
16792ac8a218SBjoern A. Zeeb 	ni = ieee80211_ref_node(vap->iv_bss);
16802ac8a218SBjoern A. Zeeb 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
16812ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
16822ac8a218SBjoern A. Zeeb 		    "on vap %p\n", __func__, ni, vap);
16830936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16842ac8a218SBjoern A. Zeeb 		return (EINVAL);
16856b4cac81SBjoern A. Zeeb 	}
16866b4cac81SBjoern A. Zeeb 
16876b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
16882ac8a218SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
16892ac8a218SBjoern A. Zeeb 	if (chan == NULL) {
16902ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
16912ac8a218SBjoern A. Zeeb 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
16920936c648SBjoern A. Zeeb 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
16932ac8a218SBjoern A. Zeeb 		return (ESRCH);
16942ac8a218SBjoern A. Zeeb 	}
16952ac8a218SBjoern A. Zeeb 
16966b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
16976b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
16986b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
16996b4cac81SBjoern A. Zeeb 
17000936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
17010936c648SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
17020936c648SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
17030936c648SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
17040936c648SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
17050936c648SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
17060936c648SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
17070936c648SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
170892690579SMark Johnston 		LKPI_80211_LVIF_UNLOCK(lvif);
170992690579SMark Johnston 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
17100936c648SBjoern A. Zeeb 		return (EBUSY);
17110936c648SBjoern A. Zeeb 	}
17120936c648SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
17130936c648SBjoern A. Zeeb 
17146b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
17158ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
17166b4cac81SBjoern A. Zeeb 
17176b4cac81SBjoern A. Zeeb 	/* Add chanctx (or if exists, change it). */
17186b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
1719d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
1720d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
17216b4cac81SBjoern A. Zeeb 		IMPROVE("diff changes for changed, working on live copy, rcu");
17226b4cac81SBjoern A. Zeeb 	} else {
17236b4cac81SBjoern A. Zeeb 		/* Keep separate alloc as in Linux this is rcu managed? */
1724c5e25798SBjoern A. Zeeb 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
17256b4cac81SBjoern A. Zeeb 		    M_LKPI80211, M_WAITOK | M_ZERO);
1726d1af434dSBjoern A. Zeeb 		chanctx_conf = &lchanctx->chanctx_conf;
17276b4cac81SBjoern A. Zeeb 	}
17286b4cac81SBjoern A. Zeeb 
1729d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_dynamic = 1;
1730d1af434dSBjoern A. Zeeb 	chanctx_conf->rx_chains_static = 1;
1731d1af434dSBjoern A. Zeeb 	chanctx_conf->radar_enabled =
17326b4cac81SBjoern A. Zeeb 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1733d1af434dSBjoern A. Zeeb 	chanctx_conf->def.chan = chan;
1734d1af434dSBjoern A. Zeeb 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
173590d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
173690d8e307SBjoern A. Zeeb 	chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
17379fb91463SBjoern A. Zeeb 	IMPROVE("Check vht_cap from band not just chan?");
17382ac8a218SBjoern A. Zeeb 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
17392ac8a218SBjoern A. Zeeb 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
17409fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
17419fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
174290d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1743d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
174490d8e307SBjoern A. Zeeb 		else
1745d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
17469fb91463SBjoern A. Zeeb 	}
17479fb91463SBjoern A. Zeeb #endif
17489fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
17499fb91463SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
17509fb91463SBjoern A. Zeeb #ifdef __notyet__
175190d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1752d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
175390d8e307SBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1754d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
175590d8e307SBjoern A. Zeeb 		else
175690d8e307SBjoern A. Zeeb #endif
175790d8e307SBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1758d1af434dSBjoern A. Zeeb 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
17599fb91463SBjoern A. Zeeb 	}
17609fb91463SBjoern A. Zeeb #endif
17616b4cac81SBjoern A. Zeeb 	/* Responder ... */
176290d8e307SBjoern A. Zeeb #if 0
176390d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.chan = chanctx_conf->def.chan;
1764d1af434dSBjoern A. Zeeb 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
176590d8e307SBjoern A. Zeeb #ifdef LKPI_80211_HT
176690d8e307SBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
176790d8e307SBjoern A. Zeeb 		chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
176890d8e307SBjoern A. Zeeb #endif
176990d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
177090d8e307SBjoern A. Zeeb 	chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
177190d8e307SBjoern A. Zeeb #else
177290d8e307SBjoern A. Zeeb 	chanctx_conf->min_def = chanctx_conf->def;
177390d8e307SBjoern A. Zeeb #endif
17746b4cac81SBjoern A. Zeeb 
17756ffb7bd4SBjoern A. Zeeb 	/* Set bss info (bss_info_changed). */
17766ffb7bd4SBjoern A. Zeeb 	bss_changed = 0;
17776ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ni->ni_bssid;
17786ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
17796ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.txpower = ni->ni_txpower;
17806ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_TXPOWER;
17816ffb7bd4SBjoern A. Zeeb 	vif->cfg.idle = false;
17826ffb7bd4SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_IDLE;
17836ffb7bd4SBjoern A. Zeeb 
17846ffb7bd4SBjoern A. Zeeb 	/* vif->bss_conf.basic_rates ? Where exactly? */
17856ffb7bd4SBjoern A. Zeeb 
17866ffb7bd4SBjoern A. Zeeb 	/* Should almost assert it is this. */
17876ffb7bd4SBjoern A. Zeeb 	vif->cfg.assoc = false;
17886ffb7bd4SBjoern A. Zeeb 	vif->cfg.aid = 0;
17896ffb7bd4SBjoern A. Zeeb 
17906ffb7bd4SBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
17916ffb7bd4SBjoern A. Zeeb 
17926b4cac81SBjoern A. Zeeb 	error = 0;
17936b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
17946b4cac81SBjoern A. Zeeb 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
17956b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
17966b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
17976b4cac81SBjoern A. Zeeb 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1798d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
17996b4cac81SBjoern A. Zeeb 	} else {
1800d1af434dSBjoern A. Zeeb 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
18016b4cac81SBjoern A. Zeeb 		if (error == 0 || error == EOPNOTSUPP) {
18027b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
18037b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
18047b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq1 =
1805d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq1;
18067b43f4d0SBjoern A. Zeeb 			vif->bss_conf.chanreq.oper.center_freq2 =
1807d1af434dSBjoern A. Zeeb 			    chanctx_conf->def.center_freq2;
18086b4cac81SBjoern A. Zeeb 		} else {
1809018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1810018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
18116b4cac81SBjoern A. Zeeb 			goto out;
18126b4cac81SBjoern A. Zeeb 		}
18136ffb7bd4SBjoern A. Zeeb 
1814d1af434dSBjoern A. Zeeb 		vif->bss_conf.chanctx_conf = chanctx_conf;
18156ffb7bd4SBjoern A. Zeeb 
18166b4cac81SBjoern A. Zeeb 		/* Assign vif chanctx. */
18176b4cac81SBjoern A. Zeeb 		if (error == 0)
181868541546SBjoern A. Zeeb 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1819d1af434dSBjoern A. Zeeb 			    &vif->bss_conf, chanctx_conf);
18206b4cac81SBjoern A. Zeeb 		if (error == EOPNOTSUPP)
18216b4cac81SBjoern A. Zeeb 			error = 0;
18226b4cac81SBjoern A. Zeeb 		if (error != 0) {
1823018d93ecSBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1824018d93ecSBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
1825d1af434dSBjoern A. Zeeb 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1826d1af434dSBjoern A. Zeeb 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1827c5e25798SBjoern A. Zeeb 			free(lchanctx, M_LKPI80211);
18286b4cac81SBjoern A. Zeeb 			goto out;
18296b4cac81SBjoern A. Zeeb 		}
18306b4cac81SBjoern A. Zeeb 	}
18316b4cac81SBjoern A. Zeeb 	IMPROVE("update radiotap chan fields too");
18326b4cac81SBjoern A. Zeeb 
18336b4cac81SBjoern A. Zeeb 	/* RATES */
18346b4cac81SBjoern A. Zeeb 	IMPROVE("bss info: not all needs to come now and rates are missing");
18356b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
18366b4cac81SBjoern A. Zeeb 
1837d9f59799SBjoern A. Zeeb 	/*
18380936c648SBjoern A. Zeeb 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
18390936c648SBjoern A. Zeeb 	 * ni always has lsta data attach despite net80211 node swapping
18400936c648SBjoern A. Zeeb 	 * under the hoods.
1841d9f59799SBjoern A. Zeeb 	 */
18420936c648SBjoern A. Zeeb 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
18430936c648SBjoern A. Zeeb 	    __func__, ni, ni->ni_drv_data));
18446b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
1845e24e8103SBjoern A. Zeeb 
184688665349SBjoern A. Zeeb 	/*
184788665349SBjoern A. Zeeb 	 * Make sure in case the sta did not change and we re-add it,
184888665349SBjoern A. Zeeb 	 * that we can tx again.
184988665349SBjoern A. Zeeb 	 */
185088665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
185188665349SBjoern A. Zeeb 	lsta->txq_ready = true;
185288665349SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
185388665349SBjoern A. Zeeb 
1854a8f735a6SBjoern A. Zeeb 	wiphy_lock(hw->wiphy);
18552ac8a218SBjoern A. Zeeb 	/* Insert the [l]sta into the list of known stations. */
1856a8f735a6SBjoern A. Zeeb 	list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
1857a8f735a6SBjoern A. Zeeb 	wiphy_unlock(hw->wiphy);
1858e24e8103SBjoern A. Zeeb 
1859d9f59799SBjoern A. Zeeb 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
18606b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
18616b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
18626b4cac81SBjoern A. Zeeb 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1863e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
18646b4cac81SBjoern A. Zeeb 	if (error != 0) {
18656b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
1866018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1867018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
18686b4cac81SBjoern A. Zeeb 		goto out;
18696b4cac81SBjoern A. Zeeb 	}
18706b4cac81SBjoern A. Zeeb #if 0
18716b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = true;	/* mo manages. */
18726b4cac81SBjoern A. Zeeb #endif
18736b4cac81SBjoern A. Zeeb 
18749d9ba2b7SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
18759d9ba2b7SBjoern A. Zeeb 
18761665ef97SBjoern A. Zeeb #if 0
18776b4cac81SBjoern A. Zeeb 	/*
18786b4cac81SBjoern A. Zeeb 	 * Wakeup all queues now that sta is there so we have as much time to
18796b4cac81SBjoern A. Zeeb 	 * possibly prepare the queue in the driver to be ready for the 1st
18806b4cac81SBjoern A. Zeeb 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
18816b4cac81SBjoern A. Zeeb 	 * is no guarantee or way to check.
1882d9f59799SBjoern A. Zeeb 	 * XXX-BZ and by now we know that this does not work on all drivers
1883d9f59799SBjoern A. Zeeb 	 * for all queues.
18846b4cac81SBjoern A. Zeeb 	 */
1885e7fe0373SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
18861665ef97SBjoern A. Zeeb #endif
18876b4cac81SBjoern A. Zeeb 
18886b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
18896b4cac81SBjoern A. Zeeb 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
18906b4cac81SBjoern A. Zeeb 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
18916b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
18926b4cac81SBjoern A. Zeeb 	lsta->in_mgd = true;
18936b4cac81SBjoern A. Zeeb 
18946b4cac81SBjoern A. Zeeb 	/*
18956b4cac81SBjoern A. Zeeb 	 * What is going to happen next:
18966b4cac81SBjoern A. Zeeb 	 * - <twiddle> .. we should end up in "auth_to_assoc"
18976b4cac81SBjoern A. Zeeb 	 * - event_callback
18986b4cac81SBjoern A. Zeeb 	 * - update sta_state (NONE to AUTH)
18996b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
19006b4cac81SBjoern A. Zeeb 	 * (ideally we'd do that on a callback for something else ...)
19016b4cac81SBjoern A. Zeeb 	 */
19026b4cac81SBjoern A. Zeeb 
1903105b9df2SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
1904105b9df2SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1905105b9df2SBjoern A. Zeeb 
1906105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
1907105b9df2SBjoern A. Zeeb 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1908105b9df2SBjoern A. Zeeb 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1909105b9df2SBjoern A. Zeeb 	    lsta->ni != vap->iv_bss)
1910105b9df2SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1911105b9df2SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1912105b9df2SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1913105b9df2SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1914105b9df2SBjoern A. Zeeb 		    lvif->lvif_bss_synched, ni, lsta);
1915105b9df2SBjoern A. Zeeb 
1916105b9df2SBjoern A. Zeeb 	/*
1917105b9df2SBjoern A. Zeeb 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1918105b9df2SBjoern A. Zeeb 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1919105b9df2SBjoern A. Zeeb 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1920105b9df2SBjoern A. Zeeb 	 * assume that ni == vap->iv_bss which it may or may not be.
1921105b9df2SBjoern A. Zeeb 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1922105b9df2SBjoern A. Zeeb 	 * function local ni already while ic was unlocked and would lead to
1923105b9df2SBjoern A. Zeeb 	 * inconsistencies.  Go and see if we lost a race and do not update
1924105b9df2SBjoern A. Zeeb 	 * lvif_bss_synched in that case.
1925105b9df2SBjoern A. Zeeb 	 */
1926105b9df2SBjoern A. Zeeb 	ieee80211_ref_node(lsta->ni);
1927105b9df2SBjoern A. Zeeb 	lvif->lvif_bss = lsta;
1928105b9df2SBjoern A. Zeeb 	if (lsta->ni == vap->iv_bss) {
1929105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = true;
1930105b9df2SBjoern A. Zeeb 	} else {
1931105b9df2SBjoern A. Zeeb 		/* Set to un-synched no matter what. */
1932105b9df2SBjoern A. Zeeb 		lvif->lvif_bss_synched = false;
1933105b9df2SBjoern A. Zeeb 		/*
1934105b9df2SBjoern A. Zeeb 		 * We do not error as someone has to take us down.
1935105b9df2SBjoern A. Zeeb 		 * If we are followed by a 2nd, new net80211::join1() going to
1936105b9df2SBjoern A. Zeeb 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1937105b9df2SBjoern A. Zeeb 		 * will take the lvif->lvif_bss node down eventually.
1938105b9df2SBjoern A. Zeeb 		 * What happens with the vap->iv_bss node will entirely be up
1939105b9df2SBjoern A. Zeeb 		 * to net80211 as we never used the node beyond alloc()/free()
1940105b9df2SBjoern A. Zeeb 		 * and we do not hold an extra reference for that anymore given
1941105b9df2SBjoern A. Zeeb 		 * ni : lsta == 1:1.
1942105b9df2SBjoern A. Zeeb 		 */
1943105b9df2SBjoern A. Zeeb 	}
1944105b9df2SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
1945105b9df2SBjoern A. Zeeb 	goto out_relocked;
1946105b9df2SBjoern A. Zeeb 
19476b4cac81SBjoern A. Zeeb out:
19488ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
19496b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
1950105b9df2SBjoern A. Zeeb out_relocked:
19510936c648SBjoern A. Zeeb 	/*
1952105b9df2SBjoern A. Zeeb 	 * Release the reference that kept the ni stable locally
19530936c648SBjoern A. Zeeb 	 * during the work of this function.
19540936c648SBjoern A. Zeeb 	 */
19556b4cac81SBjoern A. Zeeb 	if (ni != NULL)
19566b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
19576b4cac81SBjoern A. Zeeb 	return (error);
19586b4cac81SBjoern A. Zeeb }
19596b4cac81SBjoern A. Zeeb 
19606b4cac81SBjoern A. Zeeb static int
19616b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
19626b4cac81SBjoern A. Zeeb {
19636b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
19646b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
19656b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
19666b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
19676b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
19686b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
19696b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
19706b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
19716b4cac81SBjoern A. Zeeb 	int error;
19726b4cac81SBjoern A. Zeeb 
19736b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
19746b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
19756b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
19766b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
19776b4cac81SBjoern A. Zeeb 
19782ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
19792ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
19802ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
19812ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
19822ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
19832ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
19842ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
19852ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
19862ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
19872ac8a218SBjoern A. Zeeb #endif
19882ac8a218SBjoern A. Zeeb 
19892ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
19902ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
19912ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
19922ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
19932ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
19942ac8a218SBjoern A. Zeeb 	ni = lsta->ni;			/* Reference held for lvif_bss. */
19956b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
19966b4cac81SBjoern A. Zeeb 
199767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
19986b4cac81SBjoern A. Zeeb 
19996b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
20008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
20016b4cac81SBjoern A. Zeeb 
2002d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2003d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2004d9f59799SBjoern A. Zeeb 
20056b4cac81SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
200688665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
20076b4cac81SBjoern A. Zeeb 
20086b4cac81SBjoern A. Zeeb 	/* flush, no drop */
20096b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
20106b4cac81SBjoern A. Zeeb 
20116b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
20126b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
20136b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
20146b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
20156b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
20166b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
20176b4cac81SBjoern A. Zeeb 	}
20186b4cac81SBjoern A. Zeeb 
20196b4cac81SBjoern A. Zeeb 	/* sync_rx_queues */
20206b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
20216b4cac81SBjoern A. Zeeb 
20226b4cac81SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
20236b4cac81SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2024d9f59799SBjoern A. Zeeb 
2025d9f59799SBjoern A. Zeeb 	/* Take the station down. */
20266b4cac81SBjoern A. Zeeb 
20276b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
20286b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
20296b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2030d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2031e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
20326b4cac81SBjoern A. Zeeb 	if (error != 0) {
20336b4cac81SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2034018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2035018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
20366b4cac81SBjoern A. Zeeb 		goto out;
20376b4cac81SBjoern A. Zeeb 	}
20386b4cac81SBjoern A. Zeeb #if 0
20396b4cac81SBjoern A. Zeeb 	lsta->added_to_drv = false;	/* mo manages. */
20406b4cac81SBjoern A. Zeeb #endif
20416b4cac81SBjoern A. Zeeb 
204267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
20436b4cac81SBjoern A. Zeeb 
20442ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
20452ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
20462ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
20472ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
20482ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2049d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
20500936c648SBjoern A. Zeeb 	/*
20510936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
20520936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
20530936c648SBjoern A. Zeeb 	 * and potentially freed.
20540936c648SBjoern A. Zeeb 	 */
20550936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2056d9f59799SBjoern A. Zeeb 
2057d9f59799SBjoern A. Zeeb 	/* conf_tx */
2058d9f59799SBjoern A. Zeeb 
2059d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
20606b4cac81SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2061c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2062d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
20636b4cac81SBjoern A. Zeeb 
2064d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
20656b4cac81SBjoern A. Zeeb 		/* Remove vif context. */
206668541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
20676b4cac81SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
20686b4cac81SBjoern A. Zeeb 
20695a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
20705a4d2461SBjoern A. Zeeb 
20716b4cac81SBjoern A. Zeeb 		/* Remove chan ctx. */
2072d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2073d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2074c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
20756b4cac81SBjoern A. Zeeb 	}
20766b4cac81SBjoern A. Zeeb 
20776b4cac81SBjoern A. Zeeb out:
20788ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
20796b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
20806b4cac81SBjoern A. Zeeb 	return (error);
20816b4cac81SBjoern A. Zeeb }
20826b4cac81SBjoern A. Zeeb 
20836b4cac81SBjoern A. Zeeb static int
20846b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20856b4cac81SBjoern A. Zeeb {
20866b4cac81SBjoern A. Zeeb 	int error;
20876b4cac81SBjoern A. Zeeb 
20886b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
20896b4cac81SBjoern A. Zeeb 	if (error == 0)
20906b4cac81SBjoern A. Zeeb 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
20916b4cac81SBjoern A. Zeeb 	return (error);
20926b4cac81SBjoern A. Zeeb }
20936b4cac81SBjoern A. Zeeb 
20946b4cac81SBjoern A. Zeeb static int
20956b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
20966b4cac81SBjoern A. Zeeb {
20976b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
20986b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
20996b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21006b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21016b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21026b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21036b4cac81SBjoern A. Zeeb 	int error;
21046b4cac81SBjoern A. Zeeb 
21056b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21066b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21076b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21086b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21096b4cac81SBjoern A. Zeeb 
21106b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
21118ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
21122ac8a218SBjoern A. Zeeb 
21132ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
21142ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
21152ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
21162ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
21172ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
21182ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
21192ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
21202ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
21212ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
21222ac8a218SBjoern A. Zeeb #endif
21232ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
21242ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
21252ac8a218SBjoern A. Zeeb 		goto out;
21262ac8a218SBjoern A. Zeeb 	}
21272ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
21282ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
21292ac8a218SBjoern A. Zeeb 
21302ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
21316b4cac81SBjoern A. Zeeb 
21326b4cac81SBjoern A. Zeeb 	/* Finish auth. */
21336b4cac81SBjoern A. Zeeb 	IMPROVE("event callback");
21346b4cac81SBjoern A. Zeeb 
21356b4cac81SBjoern A. Zeeb 	/* Update sta_state (NONE to AUTH). */
21366b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
21376b4cac81SBjoern A. Zeeb 	    "NONE: %#x\n", __func__, lsta, lsta->state));
2138e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2139018d93ecSBjoern A. Zeeb 	if (error != 0) {
2140018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2141018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
21426b4cac81SBjoern A. Zeeb 		goto out;
2143018d93ecSBjoern A. Zeeb 	}
21446b4cac81SBjoern A. Zeeb 
21456b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
21466b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
21476b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21486b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
21496b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
21506b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
21516b4cac81SBjoern A. Zeeb 	}
21526b4cac81SBjoern A. Zeeb 
21536b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
21546b4cac81SBjoern A. Zeeb 
21556b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
21566b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
21576b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
21586b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
21596b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
21606b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
21616b4cac81SBjoern A. Zeeb 	}
21626b4cac81SBjoern A. Zeeb 
21636b4cac81SBjoern A. Zeeb 	/* Wake tx queue to get packet out. */
216488665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
21656b4cac81SBjoern A. Zeeb 
21666b4cac81SBjoern A. Zeeb 	/*
21676b4cac81SBjoern A. Zeeb 	 * <twiddle> .. we end up in "assoc_to_run"
21686b4cac81SBjoern A. Zeeb 	 * - update sta_state (AUTH to ASSOC)
21696b4cac81SBjoern A. Zeeb 	 * - conf_tx [all]
21706b4cac81SBjoern A. Zeeb 	 * - bss_info_changed (assoc, aid, ssid, ..)
21716b4cac81SBjoern A. Zeeb 	 * - change_chanctx (if needed)
21726b4cac81SBjoern A. Zeeb 	 * - event_callback
21736b4cac81SBjoern A. Zeeb 	 * - mgd_complete_tx
21746b4cac81SBjoern A. Zeeb 	 */
21756b4cac81SBjoern A. Zeeb 
21766b4cac81SBjoern A. Zeeb out:
21778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
21786b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
21796b4cac81SBjoern A. Zeeb 	return (error);
21806b4cac81SBjoern A. Zeeb }
21816b4cac81SBjoern A. Zeeb 
21826b4cac81SBjoern A. Zeeb /* auth_to_auth, assoc_to_assoc. */
21836b4cac81SBjoern A. Zeeb static int
21846b4cac81SBjoern A. Zeeb lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
21856b4cac81SBjoern A. Zeeb {
21866b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
21876b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
21886b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
21896b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
21906b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
21916b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
21922ac8a218SBjoern A. Zeeb 	int error;
21936b4cac81SBjoern A. Zeeb 
21946b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
21956b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
21966b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
21976b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
21986b4cac81SBjoern A. Zeeb 
21996b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22008ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22012ac8a218SBjoern A. Zeeb 
22022ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22032ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
22042ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
22052ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22062ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22072ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22082ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22092ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22102ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22112ac8a218SBjoern A. Zeeb #endif
22122ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
22132ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
22142ac8a218SBjoern A. Zeeb 		goto out;
22152ac8a218SBjoern A. Zeeb 	}
22162ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22172ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22182ac8a218SBjoern A. Zeeb 
22192ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
22202ac8a218SBjoern A. Zeeb 	    lsta, lvif, vap));
22216b4cac81SBjoern A. Zeeb 
22226b4cac81SBjoern A. Zeeb 	IMPROVE("event callback?");
22236b4cac81SBjoern A. Zeeb 
22246b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
22256b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
22266b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22276b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
22286b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
22296b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
22306b4cac81SBjoern A. Zeeb 	}
22316b4cac81SBjoern A. Zeeb 
22326b4cac81SBjoern A. Zeeb 	/* Now start assoc. */
22336b4cac81SBjoern A. Zeeb 
22346b4cac81SBjoern A. Zeeb 	/* Start mgd_prepare_tx. */
22356b4cac81SBjoern A. Zeeb 	if (!lsta->in_mgd) {
22366b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
22376b4cac81SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
22386b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
22396b4cac81SBjoern A. Zeeb 		lsta->in_mgd = true;
22406b4cac81SBjoern A. Zeeb 	}
22416b4cac81SBjoern A. Zeeb 
22422ac8a218SBjoern A. Zeeb 	error = 0;
22432ac8a218SBjoern A. Zeeb out:
22448ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
22456b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
22466b4cac81SBjoern A. Zeeb 
22472ac8a218SBjoern A. Zeeb 	return (error);
22486b4cac81SBjoern A. Zeeb }
22496b4cac81SBjoern A. Zeeb 
22506b4cac81SBjoern A. Zeeb static int
2251d9f59799SBjoern A. Zeeb _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
22526b4cac81SBjoern A. Zeeb {
22536b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
22546b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
22556b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
22566b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
22576b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
22586b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
22596b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
22606b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2261d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
22626b4cac81SBjoern A. Zeeb 	int error;
22636b4cac81SBjoern A. Zeeb 
22646b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
22656b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
22666b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
22676b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
22686b4cac81SBjoern A. Zeeb 
22692ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
22702ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
22712ac8a218SBjoern A. Zeeb 
22722ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
22732ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
22742ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
22752ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
22762ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
22772ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
22782ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
22792ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
22802ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
22812ac8a218SBjoern A. Zeeb #endif
22822ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
22832ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
22842ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
22852ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
22862ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
22872ac8a218SBjoern A. Zeeb 
22882ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
22896b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
22906b4cac81SBjoern A. Zeeb 
229167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2292d9f59799SBjoern A. Zeeb 
2293d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2294d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2295d9f59799SBjoern A. Zeeb 
2296d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2297d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2298d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2299d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2300d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2301ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2302d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2303d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2304d9f59799SBjoern A. Zeeb 	}
2305d9f59799SBjoern A. Zeeb 
23068ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2307d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2308d9f59799SBjoern A. Zeeb 
230988665349SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
2310d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2311018d93ecSBjoern A. Zeeb 	if (error != 0) {
2312018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2313018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2314d9f59799SBjoern A. Zeeb 		goto outni;
2315018d93ecSBjoern A. Zeeb 	}
2316d9f59799SBjoern A. Zeeb 
2317d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
231888665349SBjoern A. Zeeb 
231988665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
232088665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
232188665349SBjoern A. Zeeb 
23228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2323d9f59799SBjoern A. Zeeb 
232467674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2325d9f59799SBjoern A. Zeeb 
2326d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
232788665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2328d9f59799SBjoern A. Zeeb 
2329d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2330d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2331d9f59799SBjoern A. Zeeb 
23326b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
23336b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
23346b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
23356b4cac81SBjoern A. Zeeb 		prep_tx_info.success = false;
2336ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
23376b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
23386b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
23396b4cac81SBjoern A. Zeeb 	}
23406b4cac81SBjoern A. Zeeb 
2341d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2342d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2343d9f59799SBjoern A. Zeeb 
2344d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2345d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2346d9f59799SBjoern A. Zeeb 
2347d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2348d9f59799SBjoern A. Zeeb 
23496b4cac81SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
23506b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
23516b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
23526b4cac81SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2353e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2354018d93ecSBjoern A. Zeeb 	if (error != 0) {
2355018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2356018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
23576b4cac81SBjoern A. Zeeb 		goto out;
2358018d93ecSBjoern A. Zeeb 	}
23596b4cac81SBjoern A. Zeeb 
23605a4d2461SBjoern A. Zeeb 	/* See comment in lkpi_sta_run_to_init(). */
23615a4d2461SBjoern A. Zeeb 	bss_changed = 0;
23625a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
23636b4cac81SBjoern A. Zeeb 
23645a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
236516e688b2SBjoern A. Zeeb 
2366d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2367d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2368d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2369d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2370e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2371d9f59799SBjoern A. Zeeb 	if (error != 0) {
2372d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2373018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2374018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2375d9f59799SBjoern A. Zeeb 		goto out;
2376d9f59799SBjoern A. Zeeb 	}
2377d9f59799SBjoern A. Zeeb 
237816e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2379d9f59799SBjoern A. Zeeb 
2380d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2381d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2382d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2383616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2384616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2385d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
2386d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2387d9f59799SBjoern A. Zeeb 
23882ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
23892ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
23902ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
23912ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
23922ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
2393d9f59799SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
23940936c648SBjoern A. Zeeb 	/*
23950936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
23960936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
23970936c648SBjoern A. Zeeb 	 * and potentially freed.
23980936c648SBjoern A. Zeeb 	 */
23990936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
2400d9f59799SBjoern A. Zeeb 
2401d9f59799SBjoern A. Zeeb 	/* conf_tx */
2402d9f59799SBjoern A. Zeeb 
2403d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
2404d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
2405c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
2406d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
2407d9f59799SBjoern A. Zeeb 
2408d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
2409d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
241068541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2411d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
2412d9f59799SBjoern A. Zeeb 
24135a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
24145a4d2461SBjoern A. Zeeb 
2415d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
2416d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2417d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2418c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
2419d9f59799SBjoern A. Zeeb 	}
2420d9f59799SBjoern A. Zeeb 
2421d9f59799SBjoern A. Zeeb 	error = EALREADY;
24226b4cac81SBjoern A. Zeeb out:
24238ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
24246b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2425d9f59799SBjoern A. Zeeb outni:
24266b4cac81SBjoern A. Zeeb 	return (error);
24276b4cac81SBjoern A. Zeeb }
24286b4cac81SBjoern A. Zeeb 
24296b4cac81SBjoern A. Zeeb static int
2430d9f59799SBjoern A. Zeeb lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2431d9f59799SBjoern A. Zeeb {
2432d9f59799SBjoern A. Zeeb 	int error;
2433d9f59799SBjoern A. Zeeb 
2434d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2435d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
2436d9f59799SBjoern A. Zeeb 		return (error);
2437d9f59799SBjoern A. Zeeb 
2438d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
2439d9f59799SBjoern A. Zeeb 
2440d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2441d9f59799SBjoern A. Zeeb 	return (error);
2442d9f59799SBjoern A. Zeeb }
2443d9f59799SBjoern A. Zeeb 
2444d9f59799SBjoern A. Zeeb static int
24456b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24466b4cac81SBjoern A. Zeeb {
24476b4cac81SBjoern A. Zeeb 	int error;
24486b4cac81SBjoern A. Zeeb 
2449d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24506b4cac81SBjoern A. Zeeb 	return (error);
24516b4cac81SBjoern A. Zeeb }
24526b4cac81SBjoern A. Zeeb 
24536b4cac81SBjoern A. Zeeb static int
24546b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24556b4cac81SBjoern A. Zeeb {
24566b4cac81SBjoern A. Zeeb 	int error;
24576b4cac81SBjoern A. Zeeb 
2458d9f59799SBjoern A. Zeeb 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
24596b4cac81SBjoern A. Zeeb 	return (error);
24606b4cac81SBjoern A. Zeeb }
24616b4cac81SBjoern A. Zeeb 
24626b4cac81SBjoern A. Zeeb static int
24636b4cac81SBjoern A. Zeeb lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
24646b4cac81SBjoern A. Zeeb {
24656b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
24666b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
24676b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
24686b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
24696b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
24706b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
24716b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
24726b4cac81SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
24736b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
24746b4cac81SBjoern A. Zeeb 	int error;
24756b4cac81SBjoern A. Zeeb 
24766b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
24776b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
24786b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
24796b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
24806b4cac81SBjoern A. Zeeb 
24816b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
24828ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
24832ac8a218SBjoern A. Zeeb 
24842ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
24852ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later? */
24862ac8a218SBjoern A. Zeeb 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
24872ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
24882ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
24892ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
24902ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
24912ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
24922ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
24932ac8a218SBjoern A. Zeeb #endif
24942ac8a218SBjoern A. Zeeb 		LKPI_80211_LVIF_UNLOCK(lvif);
24952ac8a218SBjoern A. Zeeb 		error = ENOTRECOVERABLE;
24962ac8a218SBjoern A. Zeeb 		goto out;
24972ac8a218SBjoern A. Zeeb 	}
24982ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
24992ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
25002ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
25012ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
25022ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
25032ac8a218SBjoern A. Zeeb 
25042ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
25056b4cac81SBjoern A. Zeeb 
25066b4cac81SBjoern A. Zeeb 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
25076b4cac81SBjoern A. Zeeb 	    "and to lesser extend ieee80211_notify_node_join");
25086b4cac81SBjoern A. Zeeb 
25099597f7cbSBjoern A. Zeeb 	/* Finish assoc. */
25109597f7cbSBjoern A. Zeeb 	/* Update sta_state (AUTH to ASSOC) and set aid. */
25119597f7cbSBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
25129597f7cbSBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
25136b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
25149597f7cbSBjoern A. Zeeb 	sta->aid = IEEE80211_NODE_AID(ni);
25154a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25164a67f1dfSBjoern A. Zeeb 	if (vap->iv_flags & IEEE80211_F_WME)
25174a67f1dfSBjoern A. Zeeb 		sta->wme = true;
25184a67f1dfSBjoern A. Zeeb #endif
2519e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2520018d93ecSBjoern A. Zeeb 	if (error != 0) {
2521018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2522018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
25239597f7cbSBjoern A. Zeeb 		goto out;
2524018d93ecSBjoern A. Zeeb 	}
25256b4cac81SBjoern A. Zeeb 
25266b4cac81SBjoern A. Zeeb 	IMPROVE("wme / conf_tx [all]");
25276b4cac81SBjoern A. Zeeb 
25286b4cac81SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
25296b4cac81SBjoern A. Zeeb 	bss_changed = 0;
25304a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
25314a67f1dfSBjoern A. Zeeb 	bss_changed |= lkpi_wme_update(lhw, vap, true);
25324a67f1dfSBjoern A. Zeeb #endif
2533616e1330SBjoern A. Zeeb 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2534616e1330SBjoern A. Zeeb 		vif->cfg.assoc = true;
2535616e1330SBjoern A. Zeeb 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
25366b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_ASSOC;
25376b4cac81SBjoern A. Zeeb 	}
25386b4cac81SBjoern A. Zeeb 	/* We set SSID but this is not BSSID! */
2539616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = ni->ni_esslen;
2540616e1330SBjoern A. Zeeb 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
25416b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
25426b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_preamble) {
25436b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_preamble ^= 1;
25446b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25456b4cac81SBjoern A. Zeeb 	}
25466b4cac81SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
25476b4cac81SBjoern A. Zeeb 	    vif->bss_conf.use_short_slot) {
25486b4cac81SBjoern A. Zeeb 		vif->bss_conf.use_short_slot ^= 1;
25496b4cac81SBjoern A. Zeeb 		/* bss_changed |= BSS_CHANGED_??? */
25506b4cac81SBjoern A. Zeeb 	}
25516b4cac81SBjoern A. Zeeb 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
25526b4cac81SBjoern A. Zeeb 	    vif->bss_conf.qos) {
25536b4cac81SBjoern A. Zeeb 		vif->bss_conf.qos ^= 1;
25546b4cac81SBjoern A. Zeeb 		bss_changed |= BSS_CHANGED_QOS;
25556b4cac81SBjoern A. Zeeb 	}
2556fa8f007dSBjoern A. Zeeb 
2557fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2558fa8f007dSBjoern A. Zeeb 
25596b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
25606b4cac81SBjoern A. Zeeb 
25616b4cac81SBjoern A. Zeeb 	/* - change_chanctx (if needed)
25626b4cac81SBjoern A. Zeeb 	 * - event_callback
25636b4cac81SBjoern A. Zeeb 	 */
25646b4cac81SBjoern A. Zeeb 
25656b4cac81SBjoern A. Zeeb 	/* End mgd_complete_tx. */
25666b4cac81SBjoern A. Zeeb 	if (lsta->in_mgd) {
25676b4cac81SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
25686b4cac81SBjoern A. Zeeb 		prep_tx_info.success = true;
25696b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
25706b4cac81SBjoern A. Zeeb 		lsta->in_mgd = false;
25716b4cac81SBjoern A. Zeeb 	}
25726b4cac81SBjoern A. Zeeb 
2573086be6a8SBjoern A. Zeeb 	lkpi_hw_conf_idle(hw, false);
2574086be6a8SBjoern A. Zeeb 
25756b4cac81SBjoern A. Zeeb 	/*
25766b4cac81SBjoern A. Zeeb 	 * And then:
25776b4cac81SBjoern A. Zeeb 	 * - (more packets)?
25786b4cac81SBjoern A. Zeeb 	 * - set_key
25796b4cac81SBjoern A. Zeeb 	 * - set_default_unicast_key
25806b4cac81SBjoern A. Zeeb 	 * - set_key (?)
25816b4cac81SBjoern A. Zeeb 	 * - ipv6_addr_change (?)
25826b4cac81SBjoern A. Zeeb 	 */
25836b4cac81SBjoern A. Zeeb 	/* Prepare_multicast && configure_filter. */
25846b4cac81SBjoern A. Zeeb 	lhw->update_mc = true;
25856b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(vap->iv_ic, true);
25866b4cac81SBjoern A. Zeeb 
25876b4cac81SBjoern A. Zeeb 	if (!ieee80211_node_is_authorized(ni)) {
25886b4cac81SBjoern A. Zeeb 		IMPROVE("net80211 does not consider node authorized");
25896b4cac81SBjoern A. Zeeb 	}
25906b4cac81SBjoern A. Zeeb 
2591f9c7a07dSBjoern A. Zeeb 	sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
25929fb91463SBjoern A. Zeeb 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
2593f9c7a07dSBjoern A. Zeeb 	lkpi_sta_sync_from_ni(sta, ni);
25949fb91463SBjoern A. Zeeb 
25956b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTHORIZED). */
25966b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
25976b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
25986b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2599e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
26006b4cac81SBjoern A. Zeeb 	if (error != 0) {
26016b4cac81SBjoern A. Zeeb 		IMPROVE("undo some changes?");
2602018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2603018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
26046b4cac81SBjoern A. Zeeb 		goto out;
26056b4cac81SBjoern A. Zeeb 	}
26066b4cac81SBjoern A. Zeeb 
26076b4cac81SBjoern A. Zeeb 	/* - drv_config (?)
26086b4cac81SBjoern A. Zeeb 	 * - bss_info_changed
26096b4cac81SBjoern A. Zeeb 	 * - set_rekey_data (?)
26106b4cac81SBjoern A. Zeeb 	 *
26116b4cac81SBjoern A. Zeeb 	 * And now we should be passing packets.
26126b4cac81SBjoern A. Zeeb 	 */
26136b4cac81SBjoern A. Zeeb 	IMPROVE("Need that bssid setting, and the keys");
26146b4cac81SBjoern A. Zeeb 
2615fa8f007dSBjoern A. Zeeb 	bss_changed = 0;
2616fa8f007dSBjoern A. Zeeb 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2617fa8f007dSBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2618fa8f007dSBjoern A. Zeeb 
26196b4cac81SBjoern A. Zeeb out:
26208ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
26216b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
26226b4cac81SBjoern A. Zeeb 	return (error);
26236b4cac81SBjoern A. Zeeb }
26246b4cac81SBjoern A. Zeeb 
26256b4cac81SBjoern A. Zeeb static int
26266b4cac81SBjoern A. Zeeb lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26276b4cac81SBjoern A. Zeeb {
26286b4cac81SBjoern A. Zeeb 	int error;
26296b4cac81SBjoern A. Zeeb 
26306b4cac81SBjoern A. Zeeb 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
26316b4cac81SBjoern A. Zeeb 	if (error == 0)
26326b4cac81SBjoern A. Zeeb 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
26336b4cac81SBjoern A. Zeeb 	return (error);
26346b4cac81SBjoern A. Zeeb }
26356b4cac81SBjoern A. Zeeb 
26366b4cac81SBjoern A. Zeeb static int
26376b4cac81SBjoern A. Zeeb lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
26386b4cac81SBjoern A. Zeeb {
26396b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
26406b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
26416b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
26426b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
26436b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
26446b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
26456b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2646d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2647d9f59799SBjoern A. Zeeb #if 0
2648d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2649d9f59799SBjoern A. Zeeb #endif
26506b4cac81SBjoern A. Zeeb 	int error;
26516b4cac81SBjoern A. Zeeb 
26526b4cac81SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
26536b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
26546b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
26556b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
26566b4cac81SBjoern A. Zeeb 
26572ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
26582ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
26592ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
26602ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
26612ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
26622ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
26632ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
26642ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
26652ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
26662ac8a218SBjoern A. Zeeb #endif
26672ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
26682ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
26692ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
26702ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
26712ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
26722ac8a218SBjoern A. Zeeb 
26732ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
26746b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
26756b4cac81SBjoern A. Zeeb 
267667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2677d9f59799SBjoern A. Zeeb 
2678d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
26798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2680d9f59799SBjoern A. Zeeb 
2681d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2682d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2683d9f59799SBjoern A. Zeeb 
2684d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2685d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2686d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2687d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2688d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2689ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2690d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2691d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2692d9f59799SBjoern A. Zeeb 	}
2693d9f59799SBjoern A. Zeeb 
26948ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2695d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2696d9f59799SBjoern A. Zeeb 
2697d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2698d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2699018d93ecSBjoern A. Zeeb 	if (error != 0) {
2700018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2701018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2702d9f59799SBjoern A. Zeeb 		goto outni;
2703018d93ecSBjoern A. Zeeb 	}
2704d9f59799SBjoern A. Zeeb 
2705d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
270688665349SBjoern A. Zeeb 
270788665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
270888665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
270988665349SBjoern A. Zeeb 
27108ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2711d9f59799SBjoern A. Zeeb 
271267674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2713d9f59799SBjoern A. Zeeb 
2714d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
271588665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2716d9f59799SBjoern A. Zeeb 
2717d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2718d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2719d9f59799SBjoern A. Zeeb 
2720d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2721d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2722d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2723d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2724ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2725d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2726d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2727d9f59799SBjoern A. Zeeb 	}
2728d9f59799SBjoern A. Zeeb 
2729d9f59799SBjoern A. Zeeb #if 0
2730d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2731d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2732d9f59799SBjoern A. Zeeb 
2733d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2734d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2735d9f59799SBjoern A. Zeeb #endif
2736d9f59799SBjoern A. Zeeb 
2737d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2738d9f59799SBjoern A. Zeeb 
27396b4cac81SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
27406b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27416b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
27426b4cac81SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2743e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2744018d93ecSBjoern A. Zeeb 	if (error != 0) {
2745018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2746018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27476b4cac81SBjoern A. Zeeb 		goto out;
2748018d93ecSBjoern A. Zeeb 	}
27496b4cac81SBjoern A. Zeeb 
275067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27516b4cac81SBjoern A. Zeeb 
275211db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
275311db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
275411db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
275511db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
275611db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
275711db70b6SBjoern A. Zeeb 		if (error != 0) {
275811db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
275911db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
276011db70b6SBjoern A. Zeeb 			/*
276111db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
276211db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
276311db70b6SBjoern A. Zeeb 			 * less appropriate time).
276411db70b6SBjoern A. Zeeb 			 */
276511db70b6SBjoern A. Zeeb 			/* goto out; */
276611db70b6SBjoern A. Zeeb 		}
276711db70b6SBjoern A. Zeeb 	}
276811db70b6SBjoern A. Zeeb #endif
276911db70b6SBjoern A. Zeeb 
27706b4cac81SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
27716b4cac81SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
27726b4cac81SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
27736b4cac81SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2774e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2775018d93ecSBjoern A. Zeeb 	if (error != 0) {
2776018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2777018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
27786b4cac81SBjoern A. Zeeb 		goto out;
2779018d93ecSBjoern A. Zeeb 	}
27806b4cac81SBjoern A. Zeeb 
278167674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
27826b4cac81SBjoern A. Zeeb 
2783d9f59799SBjoern A. Zeeb #if 0
2784d9f59799SBjoern A. Zeeb 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2785d9f59799SBjoern A. Zeeb 	lkpi_disassoc(sta, vif, lhw);
2786d9f59799SBjoern A. Zeeb #endif
2787d9f59799SBjoern A. Zeeb 
2788d9f59799SBjoern A. Zeeb 	error = EALREADY;
27896b4cac81SBjoern A. Zeeb out:
27908ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
27916b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2792d9f59799SBjoern A. Zeeb outni:
27936b4cac81SBjoern A. Zeeb 	return (error);
27946b4cac81SBjoern A. Zeeb }
27956b4cac81SBjoern A. Zeeb 
27966b4cac81SBjoern A. Zeeb static int
2797d9f59799SBjoern A. Zeeb lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2798d9f59799SBjoern A. Zeeb {
2799d9f59799SBjoern A. Zeeb 	struct lkpi_hw *lhw;
2800d9f59799SBjoern A. Zeeb 	struct ieee80211_hw *hw;
2801d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
2802d9f59799SBjoern A. Zeeb 	struct ieee80211_vif *vif;
2803d9f59799SBjoern A. Zeeb 	struct ieee80211_node *ni;
2804d9f59799SBjoern A. Zeeb 	struct lkpi_sta *lsta;
2805d9f59799SBjoern A. Zeeb 	struct ieee80211_sta *sta;
2806d9f59799SBjoern A. Zeeb 	struct ieee80211_prep_tx_info prep_tx_info;
2807d9f59799SBjoern A. Zeeb 	enum ieee80211_bss_changed bss_changed;
2808d9f59799SBjoern A. Zeeb 	int error;
2809d9f59799SBjoern A. Zeeb 
2810d9f59799SBjoern A. Zeeb 	lhw = vap->iv_ic->ic_softc;
2811d9f59799SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
2812d9f59799SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
2813d9f59799SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
2814d9f59799SBjoern A. Zeeb 
28152ac8a218SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
28162ac8a218SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
28172ac8a218SBjoern A. Zeeb 
28182ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
28192ac8a218SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
28202ac8a218SBjoern A. Zeeb 	/* XXX-BZ KASSERT later; state going down so no action. */
28212ac8a218SBjoern A. Zeeb 	if (lvif->lvif_bss == NULL)
28222ac8a218SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
28232ac8a218SBjoern A. Zeeb 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
28242ac8a218SBjoern A. Zeeb 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
28252ac8a218SBjoern A. Zeeb 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
28262ac8a218SBjoern A. Zeeb 		    lvif->lvif_bss_synched);
28272ac8a218SBjoern A. Zeeb #endif
28282ac8a218SBjoern A. Zeeb 	lsta = lvif->lvif_bss;
28292ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
28302ac8a218SBjoern A. Zeeb 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
28312ac8a218SBjoern A. Zeeb 	    "lvif %p vap %p\n", __func__,
28322ac8a218SBjoern A. Zeeb 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
28332ac8a218SBjoern A. Zeeb 
28342ac8a218SBjoern A. Zeeb 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2835d9f59799SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
2836d9f59799SBjoern A. Zeeb 
283767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2838d9f59799SBjoern A. Zeeb 
2839d9f59799SBjoern A. Zeeb 	/* flush, drop. */
2840d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2841d9f59799SBjoern A. Zeeb 
2842d9f59799SBjoern A. Zeeb 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2843d9f59799SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2844d9f59799SBjoern A. Zeeb 	    !lsta->in_mgd) {
2845d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2846d9f59799SBjoern A. Zeeb 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2847ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2848d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2849d9f59799SBjoern A. Zeeb 		lsta->in_mgd = true;
2850d9f59799SBjoern A. Zeeb 	}
2851d9f59799SBjoern A. Zeeb 
28528ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
2853d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
2854d9f59799SBjoern A. Zeeb 
2855d9f59799SBjoern A. Zeeb 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2856d9f59799SBjoern A. Zeeb 	error = lvif->iv_newstate(vap, nstate, arg);
2857018d93ecSBjoern A. Zeeb 	if (error != 0) {
2858018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2859018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2860d9f59799SBjoern A. Zeeb 		goto outni;
2861018d93ecSBjoern A. Zeeb 	}
2862d9f59799SBjoern A. Zeeb 
2863d9f59799SBjoern A. Zeeb 	IEEE80211_UNLOCK(vap->iv_ic);
286488665349SBjoern A. Zeeb 
286588665349SBjoern A. Zeeb 	/* Ensure the packets get out. */
286688665349SBjoern A. Zeeb 	lkpi_80211_flush_tx(lhw, lsta);
286788665349SBjoern A. Zeeb 
28688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
2869d9f59799SBjoern A. Zeeb 
287067674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2871d9f59799SBjoern A. Zeeb 
2872d9f59799SBjoern A. Zeeb 	/* Wake tx queues to get packet(s) out. */
287388665349SBjoern A. Zeeb 	lkpi_wake_tx_queues(hw, sta, false, true);
2874d9f59799SBjoern A. Zeeb 
2875d9f59799SBjoern A. Zeeb 	/* flush, no drop */
2876d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2877d9f59799SBjoern A. Zeeb 
2878d9f59799SBjoern A. Zeeb 	/* End mgd_complete_tx. */
2879d9f59799SBjoern A. Zeeb 	if (lsta->in_mgd) {
2880d9f59799SBjoern A. Zeeb 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2881d9f59799SBjoern A. Zeeb 		prep_tx_info.success = false;
2882ac1d519cSBjoern A. Zeeb 		prep_tx_info.was_assoc = true;
2883d9f59799SBjoern A. Zeeb 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2884d9f59799SBjoern A. Zeeb 		lsta->in_mgd = false;
2885d9f59799SBjoern A. Zeeb 	}
2886d9f59799SBjoern A. Zeeb 
2887d9f59799SBjoern A. Zeeb 	/* sync_rx_queues */
2888d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_sync_rx_queues(hw);
2889d9f59799SBjoern A. Zeeb 
2890d9f59799SBjoern A. Zeeb 	/* sta_pre_rcu_remove */
2891d9f59799SBjoern A. Zeeb         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2892d9f59799SBjoern A. Zeeb 
2893d9f59799SBjoern A. Zeeb 	/* Take the station down. */
2894d9f59799SBjoern A. Zeeb 
2895d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2896d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2897d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2898d9f59799SBjoern A. Zeeb 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2899e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2900018d93ecSBjoern A. Zeeb 	if (error != 0) {
2901018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2902018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2903d9f59799SBjoern A. Zeeb 		goto out;
2904018d93ecSBjoern A. Zeeb 	}
2905d9f59799SBjoern A. Zeeb 
290667674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2907d9f59799SBjoern A. Zeeb 
290811db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
290911db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto) {
291011db70b6SBjoern A. Zeeb 		wiphy_lock(hw->wiphy);
291111db70b6SBjoern A. Zeeb 		error = lkpi_sta_del_keys(hw, vif, lsta);
291211db70b6SBjoern A. Zeeb 		wiphy_unlock(hw->wiphy);
291311db70b6SBjoern A. Zeeb 		if (error != 0) {
291411db70b6SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
291511db70b6SBjoern A. Zeeb 			    "failed: %d\n", __func__, __LINE__, error);
291611db70b6SBjoern A. Zeeb 			/*
291711db70b6SBjoern A. Zeeb 			 * Either drv/fw will crash or cleanup itself,
291811db70b6SBjoern A. Zeeb 			 * otherwise net80211 will delete the keys (at a
291911db70b6SBjoern A. Zeeb 			 * less appropriate time).
292011db70b6SBjoern A. Zeeb 			 */
292111db70b6SBjoern A. Zeeb 			/* goto out; */
292211db70b6SBjoern A. Zeeb 		}
292311db70b6SBjoern A. Zeeb 	}
292411db70b6SBjoern A. Zeeb #endif
292511db70b6SBjoern A. Zeeb 
2926d9f59799SBjoern A. Zeeb 	/* Update sta_state (ASSOC to AUTH). */
2927d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2928d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2929d9f59799SBjoern A. Zeeb 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2930e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2931018d93ecSBjoern A. Zeeb 	if (error != 0) {
2932018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2933018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2934d9f59799SBjoern A. Zeeb 		goto out;
2935018d93ecSBjoern A. Zeeb 	}
2936d9f59799SBjoern A. Zeeb 
293767674c1cSBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2938d9f59799SBjoern A. Zeeb 
2939d9f59799SBjoern A. Zeeb 	/* Update sta and change state (from AUTH) to NONE. */
2940d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2941d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2942d9f59799SBjoern A. Zeeb 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2943e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2944018d93ecSBjoern A. Zeeb 	if (error != 0) {
2945018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2946018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2947d9f59799SBjoern A. Zeeb 		goto out;
2948018d93ecSBjoern A. Zeeb 	}
2949d9f59799SBjoern A. Zeeb 
29505a4d2461SBjoern A. Zeeb 	bss_changed = 0;
295116e688b2SBjoern A. Zeeb 	/*
29525a4d2461SBjoern A. Zeeb 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
29535a4d2461SBjoern A. Zeeb 	 *
295416e688b2SBjoern A. Zeeb 	 * One would expect this to happen when going off AUTHORIZED.
29555a4d2461SBjoern A. Zeeb 	 * See comment there; removes the sta from fw if not careful
29565a4d2461SBjoern A. Zeeb 	 * (bss_info_changed() change is executed right away).
29575a4d2461SBjoern A. Zeeb 	 *
29585a4d2461SBjoern A. Zeeb 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
29595a4d2461SBjoern A. Zeeb 	 * as otherwise drivers (iwlwifi at least) will silently not remove
29605a4d2461SBjoern A. Zeeb 	 * the sta from the firmware and when we will add a new one trigger
29615a4d2461SBjoern A. Zeeb 	 * a fw assert.
29625a4d2461SBjoern A. Zeeb 	 *
29635a4d2461SBjoern A. Zeeb 	 * The order which works best so far avoiding early removal or silent
29645a4d2461SBjoern A. Zeeb 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
29655a4d2461SBjoern A. Zeeb 	 * the iwlwifi:mac80211.c case still to be tested):
29665a4d2461SBjoern A. Zeeb 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
29675a4d2461SBjoern A. Zeeb 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
29685a4d2461SBjoern A. Zeeb 	 *    (removes the sta given assoc is false)
29695a4d2461SBjoern A. Zeeb 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
29705a4d2461SBjoern A. Zeeb 	 * 4) call unassign_vif_chanctx
29715a4d2461SBjoern A. Zeeb 	 * 5) call lkpi_hw_conf_idle
29725a4d2461SBjoern A. Zeeb 	 * 6) call remove_chanctx
297316e688b2SBjoern A. Zeeb 	 */
29745a4d2461SBjoern A. Zeeb 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
29755a4d2461SBjoern A. Zeeb 
29765a4d2461SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
297716e688b2SBjoern A. Zeeb 
2978d9f59799SBjoern A. Zeeb 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2979d9f59799SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2980d9f59799SBjoern A. Zeeb 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2981d9f59799SBjoern A. Zeeb 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2982e7fe0373SBjoern A. Zeeb 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2983d9f59799SBjoern A. Zeeb 	if (error != 0) {
2984d9f59799SBjoern A. Zeeb 		IMPROVE("do we need to undo the chan ctx?");
2985018d93ecSBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2986018d93ecSBjoern A. Zeeb 		    "failed: %d\n", __func__, __LINE__, error);
2987d9f59799SBjoern A. Zeeb 		goto out;
2988d9f59799SBjoern A. Zeeb 	}
2989d9f59799SBjoern A. Zeeb 
29905a4d2461SBjoern A. Zeeb 	lkpi_lsta_remove(lsta, lvif);
29915a4d2461SBjoern A. Zeeb 
299216e688b2SBjoern A. Zeeb 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2993d9f59799SBjoern A. Zeeb 
2994d9f59799SBjoern A. Zeeb 	IMPROVE("Any bss_info changes to announce?");
2995d9f59799SBjoern A. Zeeb 	vif->bss_conf.qos = 0;
2996d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_QOS;
2997616e1330SBjoern A. Zeeb 	vif->cfg.ssid_len = 0;
2998616e1330SBjoern A. Zeeb 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2999d9f59799SBjoern A. Zeeb 	bss_changed |= BSS_CHANGED_BSSID;
30005a4d2461SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;
30015a4d2461SBjoern A. Zeeb 	vif->bss_conf.qos = false;
30025a4d2461SBjoern A. Zeeb 	/* XXX BSS_CHANGED_???? */
3003d9f59799SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3004d9f59799SBjoern A. Zeeb 
30052ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
30062ac8a218SBjoern A. Zeeb 	/* Remove ni reference for this cache of lsta. */
30072ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
30082ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
30092ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
30100936c648SBjoern A. Zeeb 	/*
30110936c648SBjoern A. Zeeb 	 * The very last release the reference on the ni for the ni/lsta on
30120936c648SBjoern A. Zeeb 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
30130936c648SBjoern A. Zeeb 	 * and potentially freed.
30140936c648SBjoern A. Zeeb 	 */
30150936c648SBjoern A. Zeeb 	ieee80211_free_node(ni);
3016d9f59799SBjoern A. Zeeb 
3017d9f59799SBjoern A. Zeeb 	/* conf_tx */
3018d9f59799SBjoern A. Zeeb 
3019d9f59799SBjoern A. Zeeb 	/* Take the chan ctx down. */
3020d9f59799SBjoern A. Zeeb 	if (vif->chanctx_conf != NULL) {
3021c5e25798SBjoern A. Zeeb 		struct lkpi_chanctx *lchanctx;
3022d1af434dSBjoern A. Zeeb 		struct ieee80211_chanctx_conf *chanctx_conf;
3023d9f59799SBjoern A. Zeeb 
3024d1af434dSBjoern A. Zeeb 		chanctx_conf = vif->chanctx_conf;
3025d9f59799SBjoern A. Zeeb 		/* Remove vif context. */
302668541546SBjoern A. Zeeb 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
3027d9f59799SBjoern A. Zeeb 		/* NB: vif->chanctx_conf is NULL now. */
3028d9f59799SBjoern A. Zeeb 
30295a4d2461SBjoern A. Zeeb 		lkpi_hw_conf_idle(hw, true);
30305a4d2461SBjoern A. Zeeb 
3031d9f59799SBjoern A. Zeeb 		/* Remove chan ctx. */
3032d1af434dSBjoern A. Zeeb 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
3033d1af434dSBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
3034c5e25798SBjoern A. Zeeb 		free(lchanctx, M_LKPI80211);
3035d9f59799SBjoern A. Zeeb 	}
3036d9f59799SBjoern A. Zeeb 
3037d9f59799SBjoern A. Zeeb 	error = EALREADY;
3038d9f59799SBjoern A. Zeeb out:
30398ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3040d9f59799SBjoern A. Zeeb 	IEEE80211_LOCK(vap->iv_ic);
3041d9f59799SBjoern A. Zeeb outni:
3042d9f59799SBjoern A. Zeeb 	return (error);
3043d9f59799SBjoern A. Zeeb }
3044d9f59799SBjoern A. Zeeb 
3045d9f59799SBjoern A. Zeeb static int
3046d9f59799SBjoern A. Zeeb lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3047d9f59799SBjoern A. Zeeb {
3048d9f59799SBjoern A. Zeeb 
3049d9f59799SBjoern A. Zeeb 	return (lkpi_sta_run_to_init(vap, nstate, arg));
3050d9f59799SBjoern A. Zeeb }
3051d9f59799SBjoern A. Zeeb 
3052d9f59799SBjoern A. Zeeb static int
30536b4cac81SBjoern A. Zeeb lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
30546b4cac81SBjoern A. Zeeb {
30556b4cac81SBjoern A. Zeeb 	int error;
30566b4cac81SBjoern A. Zeeb 
3057d9f59799SBjoern A. Zeeb 	error = lkpi_sta_run_to_init(vap, nstate, arg);
3058d9f59799SBjoern A. Zeeb 	if (error != 0 && error != EALREADY)
3059d9f59799SBjoern A. Zeeb 		return (error);
3060d9f59799SBjoern A. Zeeb 
3061d9f59799SBjoern A. Zeeb 	/* At this point iv_bss is long a new node! */
3062d9f59799SBjoern A. Zeeb 
3063d9f59799SBjoern A. Zeeb 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
30646b4cac81SBjoern A. Zeeb 	return (error);
30656b4cac81SBjoern A. Zeeb }
30666b4cac81SBjoern A. Zeeb 
3067d9f59799SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
30686b4cac81SBjoern A. Zeeb 
30696b4cac81SBjoern A. Zeeb /*
30706b4cac81SBjoern A. Zeeb  * The matches the documented state changes in net80211::sta_newstate().
30716b4cac81SBjoern A. Zeeb  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
30726b4cac81SBjoern A. Zeeb  * there are "invalid" (so there is a room for failure here).
30736b4cac81SBjoern A. Zeeb  */
30746b4cac81SBjoern A. Zeeb struct fsm_state {
30756b4cac81SBjoern A. Zeeb 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
30766b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
30776b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
30786b4cac81SBjoern A. Zeeb 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
30796b4cac81SBjoern A. Zeeb } sta_state_fsm[] = {
30806b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
30816b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
30826b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
3083d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
3084d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
30856b4cac81SBjoern A. Zeeb 
30866b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30876b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
30886b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
30896b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3090d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
30916b4cac81SBjoern A. Zeeb 
3092d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3093d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
3094d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
3095d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
3096d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
30976b4cac81SBjoern A. Zeeb 
3098d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
3099d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
3100d9f59799SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
31016b4cac81SBjoern A. Zeeb 
31026b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
31036b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
31046b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
31056b4cac81SBjoern A. Zeeb 
31066b4cac81SBjoern A. Zeeb 	/* Dummy at the end without handler. */
31076b4cac81SBjoern A. Zeeb 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
31086b4cac81SBjoern A. Zeeb };
31096b4cac81SBjoern A. Zeeb 
31106b4cac81SBjoern A. Zeeb static int
31116b4cac81SBjoern A. Zeeb lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
31126b4cac81SBjoern A. Zeeb {
31136b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
31146b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
31156b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
31166b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
31176b4cac81SBjoern A. Zeeb 	struct fsm_state *s;
31186b4cac81SBjoern A. Zeeb 	enum ieee80211_state ostate;
31196b4cac81SBjoern A. Zeeb 	int error;
31206b4cac81SBjoern A. Zeeb 
31216b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
31226b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(ic);
31236b4cac81SBjoern A. Zeeb 	ostate = vap->iv_state;
31246b4cac81SBjoern A. Zeeb 
31259d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31269d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
31276b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
31286b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31299d9ba2b7SBjoern A. Zeeb #endif
31306b4cac81SBjoern A. Zeeb 
31316b4cac81SBjoern A. Zeeb 	if (vap->iv_opmode == IEEE80211_M_STA) {
31326b4cac81SBjoern A. Zeeb 
31336b4cac81SBjoern A. Zeeb 		lhw = ic->ic_softc;
31346b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
31356b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
31366b4cac81SBjoern A. Zeeb 
31376b4cac81SBjoern A. Zeeb 		/* No need to replicate this in most state handlers. */
31386b4cac81SBjoern A. Zeeb 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
31396b4cac81SBjoern A. Zeeb 			lkpi_stop_hw_scan(lhw, vif);
31406b4cac81SBjoern A. Zeeb 
31416b4cac81SBjoern A. Zeeb 		s = sta_state_fsm;
31426b4cac81SBjoern A. Zeeb 
31436b4cac81SBjoern A. Zeeb 	} else {
31446b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
31456b4cac81SBjoern A. Zeeb 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
31466b4cac81SBjoern A. Zeeb 		return (ENOSYS);
31476b4cac81SBjoern A. Zeeb 	}
31486b4cac81SBjoern A. Zeeb 
31496b4cac81SBjoern A. Zeeb 	error = 0;
31506b4cac81SBjoern A. Zeeb 	for (; s->handler != NULL; s++) {
31516b4cac81SBjoern A. Zeeb 		if (ostate == s->ostate && nstate == s->nstate) {
31529d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31539d9ba2b7SBjoern A. Zeeb 			if (linuxkpi_debug_80211 & D80211_TRACE)
3154d9f59799SBjoern A. Zeeb 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3155d9f59799SBjoern A. Zeeb 				    " %d (%s): arg %d.\n", __func__,
3156d9f59799SBjoern A. Zeeb 				    ostate, ieee80211_state_name[ostate],
3157d9f59799SBjoern A. Zeeb 				    nstate, ieee80211_state_name[nstate], arg);
31589d9ba2b7SBjoern A. Zeeb #endif
31596b4cac81SBjoern A. Zeeb 			error = s->handler(vap, nstate, arg);
31606b4cac81SBjoern A. Zeeb 			break;
31616b4cac81SBjoern A. Zeeb 		}
31626b4cac81SBjoern A. Zeeb 	}
31636b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
31646b4cac81SBjoern A. Zeeb 
31656b4cac81SBjoern A. Zeeb 	if (s->handler == NULL) {
3166d9f59799SBjoern A. Zeeb 		IMPROVE("turn this into a KASSERT\n");
31676b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
31686b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__,
31696b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31706b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
31716b4cac81SBjoern A. Zeeb 		return (ENOSYS);
31726b4cac81SBjoern A. Zeeb 	}
31736b4cac81SBjoern A. Zeeb 
31746b4cac81SBjoern A. Zeeb 	if (error == EALREADY) {
31759d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31769d9ba2b7SBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE)
3177d9f59799SBjoern A. Zeeb 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3178d9f59799SBjoern A. Zeeb 			    "%d (%s): iv_newstate already handled: %d.\n",
3179d9f59799SBjoern A. Zeeb 			    __func__, ostate, ieee80211_state_name[ostate],
3180d9f59799SBjoern A. Zeeb 			    nstate, ieee80211_state_name[nstate], error);
31819d9ba2b7SBjoern A. Zeeb #endif
31826b4cac81SBjoern A. Zeeb 		return (0);
31836b4cac81SBjoern A. Zeeb 	}
31846b4cac81SBjoern A. Zeeb 
31856b4cac81SBjoern A. Zeeb 	if (error != 0) {
31866b4cac81SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
31876b4cac81SBjoern A. Zeeb 		    "%d (%s) -> %d (%s)\n", __func__, error,
31886b4cac81SBjoern A. Zeeb 		    ostate, ieee80211_state_name[ostate],
31896b4cac81SBjoern A. Zeeb 		    nstate, ieee80211_state_name[nstate]);
319045c27ad5SBjoern A. Zeeb 		return (error);
31916b4cac81SBjoern A. Zeeb 	}
31926b4cac81SBjoern A. Zeeb 
31939d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
31949d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE)
3195d9f59799SBjoern A. Zeeb 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3196d9f59799SBjoern A. Zeeb 		    "calling net80211 parent\n",
31976b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, vap, nstate, arg);
31989d9ba2b7SBjoern A. Zeeb #endif
31996b4cac81SBjoern A. Zeeb 
32006b4cac81SBjoern A. Zeeb 	return (lvif->iv_newstate(vap, nstate, arg));
32016b4cac81SBjoern A. Zeeb }
32026b4cac81SBjoern A. Zeeb 
32036b4cac81SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
32046b4cac81SBjoern A. Zeeb 
3205d9f59799SBjoern A. Zeeb /*
3206d9f59799SBjoern A. Zeeb  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3207d9f59799SBjoern A. Zeeb  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3208d9f59799SBjoern A. Zeeb  * new node without us knowing and thus our ni/lsta are out of sync.
3209d9f59799SBjoern A. Zeeb  */
3210d9f59799SBjoern A. Zeeb static struct ieee80211_node *
3211d9f59799SBjoern A. Zeeb lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3212d9f59799SBjoern A. Zeeb {
3213d9f59799SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32142ac8a218SBjoern A. Zeeb 	struct ieee80211_node *rni;
3215d9f59799SBjoern A. Zeeb 
32162ac8a218SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
3217d9f59799SBjoern A. Zeeb 
3218ed3ef56bSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32192ac8a218SBjoern A. Zeeb 
32202ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_LOCK(lvif);
32212ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
32222ac8a218SBjoern A. Zeeb 	LKPI_80211_LVIF_UNLOCK(lvif);
32232ac8a218SBjoern A. Zeeb 
32242ac8a218SBjoern A. Zeeb 	rni = lvif->iv_update_bss(vap, ni);
32252ac8a218SBjoern A. Zeeb 	return (rni);
3226d9f59799SBjoern A. Zeeb }
3227d9f59799SBjoern A. Zeeb 
32284a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
32296b4cac81SBjoern A. Zeeb static int
32304a67f1dfSBjoern A. Zeeb lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
32316b4cac81SBjoern A. Zeeb {
32324a67f1dfSBjoern A. Zeeb 	struct ieee80211com *ic;
32336b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
32346b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
32356b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
32366b4cac81SBjoern A. Zeeb 	struct chanAccParams chp;
32376b4cac81SBjoern A. Zeeb 	struct wmeParams wmeparr[WME_NUM_AC];
32386b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
32396b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
32406b4cac81SBjoern A. Zeeb 	int error;
32416b4cac81SBjoern A. Zeeb 	uint16_t ac;
32426b4cac81SBjoern A. Zeeb 
32436b4cac81SBjoern A. Zeeb 	IMPROVE();
32446b4cac81SBjoern A. Zeeb 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
32456b4cac81SBjoern A. Zeeb 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
32466b4cac81SBjoern A. Zeeb 
32476b4cac81SBjoern A. Zeeb 	if (vap == NULL)
32486b4cac81SBjoern A. Zeeb 		return (0);
32496b4cac81SBjoern A. Zeeb 
32504a67f1dfSBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
32514a67f1dfSBjoern A. Zeeb 		return (0);
32524a67f1dfSBjoern A. Zeeb 
32536b4cac81SBjoern A. Zeeb 	if (lhw->ops->conf_tx == NULL)
32546b4cac81SBjoern A. Zeeb 		return (0);
32556b4cac81SBjoern A. Zeeb 
32564a67f1dfSBjoern A. Zeeb 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
32574a67f1dfSBjoern A. Zeeb 		lhw->update_wme = true;
32584a67f1dfSBjoern A. Zeeb 		return (0);
32594a67f1dfSBjoern A. Zeeb 	}
32604a67f1dfSBjoern A. Zeeb 	lhw->update_wme = false;
32616b4cac81SBjoern A. Zeeb 
32624a67f1dfSBjoern A. Zeeb 	ic = lhw->ic;
32636b4cac81SBjoern A. Zeeb 	ieee80211_wme_ic_getparams(ic, &chp);
32646b4cac81SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
32656b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < WME_NUM_AC; ac++)
32666b4cac81SBjoern A. Zeeb 		wmeparr[ac] = chp.cap_wmeParams[ac];
32676b4cac81SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
32686b4cac81SBjoern A. Zeeb 
32694a67f1dfSBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
32704a67f1dfSBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
32714a67f1dfSBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
32724a67f1dfSBjoern A. Zeeb 
32736b4cac81SBjoern A. Zeeb 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
32746b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
32756b4cac81SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
32766b4cac81SBjoern A. Zeeb 		struct wmeParams *wmep;
32776b4cac81SBjoern A. Zeeb 
32786b4cac81SBjoern A. Zeeb 		wmep = &wmeparr[ac];
32796b4cac81SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
32806b4cac81SBjoern A. Zeeb 		txqp.cw_min = wmep->wmep_logcwmin;
32816b4cac81SBjoern A. Zeeb 		txqp.cw_max = wmep->wmep_logcwmax;
32826b4cac81SBjoern A. Zeeb 		txqp.txop = wmep->wmep_txopLimit;
32836b4cac81SBjoern A. Zeeb 		txqp.aifs = wmep->wmep_aifsn;
328468541546SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
32856b4cac81SBjoern A. Zeeb 		if (error != 0)
32863f0083c4SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
32876b4cac81SBjoern A. Zeeb 			    __func__, ac, error);
32886b4cac81SBjoern A. Zeeb 	}
32896b4cac81SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
32906b4cac81SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
32914a67f1dfSBjoern A. Zeeb 	if (!planned)
32926b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
32934a67f1dfSBjoern A. Zeeb 
32944a67f1dfSBjoern A. Zeeb 	return (changed);
32954a67f1dfSBjoern A. Zeeb }
32966b4cac81SBjoern A. Zeeb #endif
32976b4cac81SBjoern A. Zeeb 
32984a67f1dfSBjoern A. Zeeb static int
32994a67f1dfSBjoern A. Zeeb lkpi_ic_wme_update(struct ieee80211com *ic)
33004a67f1dfSBjoern A. Zeeb {
33014a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
33024a67f1dfSBjoern A. Zeeb 	struct ieee80211vap *vap;
33034a67f1dfSBjoern A. Zeeb 	struct lkpi_hw *lhw;
33044a67f1dfSBjoern A. Zeeb 
33054a67f1dfSBjoern A. Zeeb 	IMPROVE("Use the per-VAP callback in net80211.");
33064a67f1dfSBjoern A. Zeeb 	vap = TAILQ_FIRST(&ic->ic_vaps);
33074a67f1dfSBjoern A. Zeeb 	if (vap == NULL)
33086b4cac81SBjoern A. Zeeb 		return (0);
33094a67f1dfSBjoern A. Zeeb 
33104a67f1dfSBjoern A. Zeeb 	lhw = ic->ic_softc;
33114a67f1dfSBjoern A. Zeeb 
33124a67f1dfSBjoern A. Zeeb 	lkpi_wme_update(lhw, vap, false);
33134a67f1dfSBjoern A. Zeeb #endif
33144a67f1dfSBjoern A. Zeeb 	return (0);	/* unused */
33156b4cac81SBjoern A. Zeeb }
33166b4cac81SBjoern A. Zeeb 
33174aff4048SBjoern A. Zeeb /*
33184aff4048SBjoern A. Zeeb  * Change link-layer address on the vif (if the vap is not started/"UP").
33194aff4048SBjoern A. Zeeb  * This can happen if a user changes 'ether' using ifconfig.
33204aff4048SBjoern A. Zeeb  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
33214aff4048SBjoern A. Zeeb  * we do use a per-[l]vif event handler to be sure we exist as we
33224aff4048SBjoern A. Zeeb  * cannot assume that from every vap derives a vif and we have a hard
33234aff4048SBjoern A. Zeeb  * time checking based on net80211 information.
3324edab5a28SBjoern A. Zeeb  * Should this ever become a real problem we could add a callback function
3325edab5a28SBjoern A. Zeeb  * to wlan_iflladdr() to be set optionally but that would be for a
3326edab5a28SBjoern A. Zeeb  * single-consumer (or needs a list) -- was just too complicated for an
3327edab5a28SBjoern A. Zeeb  * otherwise perfect mechanism FreeBSD already provides.
33284aff4048SBjoern A. Zeeb  */
33294aff4048SBjoern A. Zeeb static void
33304aff4048SBjoern A. Zeeb lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
33314aff4048SBjoern A. Zeeb {
33324aff4048SBjoern A. Zeeb 	struct epoch_tracker et;
33334aff4048SBjoern A. Zeeb 	struct ieee80211_vif *vif;
33344aff4048SBjoern A. Zeeb 
33354aff4048SBjoern A. Zeeb 	NET_EPOCH_ENTER(et);
3336edab5a28SBjoern A. Zeeb 	/* NB: identify vap's by if_transmit; left as an extra check. */
3337edab5a28SBjoern A. Zeeb 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3338edab5a28SBjoern A. Zeeb 	    (if_getflags(ifp) & IFF_UP) != 0) {
33394aff4048SBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
33404aff4048SBjoern A. Zeeb 		return;
33414aff4048SBjoern A. Zeeb 	}
33424aff4048SBjoern A. Zeeb 
33434aff4048SBjoern A. Zeeb 	vif = arg;
334457609cb2SJustin Hibbits 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
33454aff4048SBjoern A. Zeeb 	NET_EPOCH_EXIT(et);
33464aff4048SBjoern A. Zeeb }
33474aff4048SBjoern A. Zeeb 
33486b4cac81SBjoern A. Zeeb static struct ieee80211vap *
33496b4cac81SBjoern A. Zeeb lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
33506b4cac81SBjoern A. Zeeb     int unit, enum ieee80211_opmode opmode, int flags,
33516b4cac81SBjoern A. Zeeb     const uint8_t bssid[IEEE80211_ADDR_LEN],
33526b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
33536b4cac81SBjoern A. Zeeb {
33546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
33556b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
33566b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
33576b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
33586b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
3359a6042e17SBjoern A. Zeeb 	struct ieee80211_tx_queue_params txqp;
33606b4cac81SBjoern A. Zeeb 	enum ieee80211_bss_changed changed;
336140839418SBjoern A. Zeeb 	struct sysctl_oid *node;
33626b4cac81SBjoern A. Zeeb 	size_t len;
3363e3a0b120SBjoern A. Zeeb 	int error, i;
3364a6042e17SBjoern A. Zeeb 	uint16_t ac;
33656b4cac81SBjoern A. Zeeb 
33666b4cac81SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
33676b4cac81SBjoern A. Zeeb 		return (NULL);
33686b4cac81SBjoern A. Zeeb 
33696b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
33706b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
33716b4cac81SBjoern A. Zeeb 
33726b4cac81SBjoern A. Zeeb 	len = sizeof(*lvif);
33736b4cac81SBjoern A. Zeeb 	len += hw->vif_data_size;	/* vif->drv_priv */
33746b4cac81SBjoern A. Zeeb 
33756b4cac81SBjoern A. Zeeb 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
33766b4cac81SBjoern A. Zeeb 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3377a8f735a6SBjoern A. Zeeb 	INIT_LIST_HEAD(&lvif->lsta_list);
33782ac8a218SBjoern A. Zeeb 	lvif->lvif_bss = NULL;
33792ac8a218SBjoern A. Zeeb 	lvif->lvif_bss_synched = false;
33806b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
33816b4cac81SBjoern A. Zeeb 
33826b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
33836b4cac81SBjoern A. Zeeb 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
33846b4cac81SBjoern A. Zeeb 	vif->p2p = false;
33856b4cac81SBjoern A. Zeeb 	vif->probe_req_reg = false;
33866b4cac81SBjoern A. Zeeb 	vif->type = lkpi_opmode_to_vif_type(opmode);
33876b4cac81SBjoern A. Zeeb 	lvif->wdev.iftype = vif->type;
33886b4cac81SBjoern A. Zeeb 	/* Need to fill in other fields as well. */
33896b4cac81SBjoern A. Zeeb 	IMPROVE();
33906b4cac81SBjoern A. Zeeb 
33916b4cac81SBjoern A. Zeeb 	/* XXX-BZ hardcoded for now! */
33926b4cac81SBjoern A. Zeeb #if 1
33936b4cac81SBjoern A. Zeeb 	vif->chanctx_conf = NULL;
3394adff403fSBjoern A. Zeeb 	vif->bss_conf.vif = vif;
33956ffb7bd4SBjoern A. Zeeb 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
33966ffb7bd4SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
33974aff4048SBjoern A. Zeeb 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
33984aff4048SBjoern A. Zeeb 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
33996ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
34007b43f4d0SBjoern A. Zeeb 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
34016b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
34026b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
34036b4cac81SBjoern A. Zeeb 	vif->bss_conf.qos = false;
34046b4cac81SBjoern A. Zeeb 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
34056b4cac81SBjoern A. Zeeb 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3406616e1330SBjoern A. Zeeb 	vif->cfg.aid = 0;
3407616e1330SBjoern A. Zeeb 	vif->cfg.assoc = false;
3408616e1330SBjoern A. Zeeb 	vif->cfg.idle = true;
3409616e1330SBjoern A. Zeeb 	vif->cfg.ps = false;
34106ffb7bd4SBjoern A. Zeeb 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3411caaa79c3SBjoern A. Zeeb 	/*
3412caaa79c3SBjoern A. Zeeb 	 * We need to initialize it to something as the bss_info_changed call
3413caaa79c3SBjoern A. Zeeb 	 * will try to copy from it in iwlwifi and NULL is a panic.
3414caaa79c3SBjoern A. Zeeb 	 * We will set the proper one in scan_to_auth() before being assoc.
3415caaa79c3SBjoern A. Zeeb 	 */
34166ffb7bd4SBjoern A. Zeeb 	vif->bss_conf.bssid = ieee80211broadcastaddr;
34176b4cac81SBjoern A. Zeeb #endif
34186b4cac81SBjoern A. Zeeb #if 0
34196b4cac81SBjoern A. Zeeb 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
34206b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
34216b4cac81SBjoern A. Zeeb 	vif->bss_conf.beacon_int = ic->ic_bintval;
34226b4cac81SBjoern A. Zeeb 	/* iwlwifi bug. */
34236b4cac81SBjoern A. Zeeb 	if (vif->bss_conf.beacon_int < 16)
34246b4cac81SBjoern A. Zeeb 		vif->bss_conf.beacon_int = 16;
34256b4cac81SBjoern A. Zeeb #endif
3426e3a0b120SBjoern A. Zeeb 
34276ffb7bd4SBjoern A. Zeeb 	/* Link Config */
34286ffb7bd4SBjoern A. Zeeb 	vif->link_conf[0] = &vif->bss_conf;
34296ffb7bd4SBjoern A. Zeeb 	for (i = 0; i < nitems(vif->link_conf); i++) {
34306ffb7bd4SBjoern A. Zeeb 		IMPROVE("more than 1 link one day");
34316ffb7bd4SBjoern A. Zeeb 	}
34326ffb7bd4SBjoern A. Zeeb 
3433e3a0b120SBjoern A. Zeeb 	/* Setup queue defaults; driver may override in (*add_interface). */
3434e3a0b120SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3435e3a0b120SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3436e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3437e3a0b120SBjoern A. Zeeb 		else if (hw->queues >= IEEE80211_NUM_ACS)
3438e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = i;
3439e3a0b120SBjoern A. Zeeb 		else
3440e3a0b120SBjoern A. Zeeb 			vif->hw_queue[i] = 0;
34410cbcfa19SBjoern A. Zeeb 
34420cbcfa19SBjoern A. Zeeb 		/* Initialize the queue to running. Stopped? */
34430cbcfa19SBjoern A. Zeeb 		lvif->hw_queue_stopped[i] = false;
3444e3a0b120SBjoern A. Zeeb 	}
3445e3a0b120SBjoern A. Zeeb 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3446e3a0b120SBjoern A. Zeeb 
34476b4cac81SBjoern A. Zeeb 	IMPROVE();
34486b4cac81SBjoern A. Zeeb 
34496b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_start(hw);
34506b4cac81SBjoern A. Zeeb 	if (error != 0) {
34513f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
34526b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34536b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
34546b4cac81SBjoern A. Zeeb 		return (NULL);
34556b4cac81SBjoern A. Zeeb 	}
34566b4cac81SBjoern A. Zeeb 
34576b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_add_interface(hw, vif);
34586b4cac81SBjoern A. Zeeb 	if (error != 0) {
34596b4cac81SBjoern A. Zeeb 		IMPROVE();	/* XXX-BZ mo_stop()? */
34603f0083c4SBjoern A. Zeeb 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
34616b4cac81SBjoern A. Zeeb 		mtx_destroy(&lvif->mtx);
34626b4cac81SBjoern A. Zeeb 		free(lvif, M_80211_VAP);
34636b4cac81SBjoern A. Zeeb 		return (NULL);
34646b4cac81SBjoern A. Zeeb 	}
34656b4cac81SBjoern A. Zeeb 
34668891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
34676b4cac81SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
34688891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
34696b4cac81SBjoern A. Zeeb 
34706b4cac81SBjoern A. Zeeb 	/* Set bss_info. */
34716b4cac81SBjoern A. Zeeb 	changed = 0;
34726b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34736b4cac81SBjoern A. Zeeb 
3474a6042e17SBjoern A. Zeeb 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
3475a6042e17SBjoern A. Zeeb 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
3476a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
3477a6042e17SBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3478a6042e17SBjoern A. Zeeb 
3479a6042e17SBjoern A. Zeeb 		bzero(&txqp, sizeof(txqp));
3480a6042e17SBjoern A. Zeeb 		txqp.cw_min = 15;
3481a6042e17SBjoern A. Zeeb 		txqp.cw_max = 1023;
3482a6042e17SBjoern A. Zeeb 		txqp.txop = 0;
3483a6042e17SBjoern A. Zeeb 		txqp.aifs = 2;
3484a6042e17SBjoern A. Zeeb 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3485a6042e17SBjoern A. Zeeb 		if (error != 0)
3486a6042e17SBjoern A. Zeeb 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3487a6042e17SBjoern A. Zeeb 			    __func__, ac, error);
3488a6042e17SBjoern A. Zeeb 	}
3489a6042e17SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
3490a6042e17SBjoern A. Zeeb 	changed = BSS_CHANGED_QOS;
3491a6042e17SBjoern A. Zeeb 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
34926b4cac81SBjoern A. Zeeb 
34936b4cac81SBjoern A. Zeeb 	/* Force MC init. */
34946b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, true);
34956b4cac81SBjoern A. Zeeb 
34966b4cac81SBjoern A. Zeeb 	IMPROVE();
34976b4cac81SBjoern A. Zeeb 
34986b4cac81SBjoern A. Zeeb 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
34996b4cac81SBjoern A. Zeeb 
35006b4cac81SBjoern A. Zeeb 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
35016b4cac81SBjoern A. Zeeb 	lvif->iv_newstate = vap->iv_newstate;
35026b4cac81SBjoern A. Zeeb 	vap->iv_newstate = lkpi_iv_newstate;
3503d9f59799SBjoern A. Zeeb 	lvif->iv_update_bss = vap->iv_update_bss;
3504d9f59799SBjoern A. Zeeb 	vap->iv_update_bss = lkpi_iv_update_bss;
35056b4cac81SBjoern A. Zeeb 
3506b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
350711db70b6SBjoern A. Zeeb 	/* Key management. */
350811db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
35096b4cac81SBjoern A. Zeeb 		vap->iv_key_set = lkpi_iv_key_set;
35106b4cac81SBjoern A. Zeeb 		vap->iv_key_delete = lkpi_iv_key_delete;
35116b4cac81SBjoern A. Zeeb 	}
351211db70b6SBjoern A. Zeeb #endif
35136b4cac81SBjoern A. Zeeb 
35149fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
35159fb91463SBjoern A. Zeeb 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
35169fb91463SBjoern A. Zeeb #endif
35179fb91463SBjoern A. Zeeb 
35186b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_init(vap);
35196b4cac81SBjoern A. Zeeb 
35206b4cac81SBjoern A. Zeeb 	/* Complete setup. */
35216b4cac81SBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
35226b4cac81SBjoern A. Zeeb 	    ieee80211_media_status, mac);
35236b4cac81SBjoern A. Zeeb 
352411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HT
352511db70b6SBjoern A. Zeeb 	/*
352611db70b6SBjoern A. Zeeb 	 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
352711db70b6SBjoern A. Zeeb 	 * to do so if they cannot do the crypto too.
352811db70b6SBjoern A. Zeeb 	 */
352911db70b6SBjoern A. Zeeb 	if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
353011db70b6SBjoern A. Zeeb 		vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
353111db70b6SBjoern A. Zeeb #endif
3532*ac2c7271SBjoern A. Zeeb #if defined(LKPI_80211_HT)
3533*ac2c7271SBjoern A. Zeeb 	/* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
3534*ac2c7271SBjoern A. Zeeb 	vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
3535*ac2c7271SBjoern A. Zeeb #endif
353611db70b6SBjoern A. Zeeb 
35376b4cac81SBjoern A. Zeeb 	if (hw->max_listen_interval == 0)
35386b4cac81SBjoern A. Zeeb 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
35396b4cac81SBjoern A. Zeeb 	hw->conf.listen_interval = hw->max_listen_interval;
35406b4cac81SBjoern A. Zeeb 	ic->ic_set_channel(ic);
35416b4cac81SBjoern A. Zeeb 
35426b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we need to be able to update these? */
35436b4cac81SBjoern A. Zeeb 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
35446b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
35456b4cac81SBjoern A. Zeeb 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
35466b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
35476b4cac81SBjoern A. Zeeb 	/* any others? */
354840839418SBjoern A. Zeeb 
354940839418SBjoern A. Zeeb 	/* Add per-VIF/VAP sysctls. */
355040839418SBjoern A. Zeeb 	sysctl_ctx_init(&lvif->sysctl_ctx);
355140839418SBjoern A. Zeeb 
355240839418SBjoern A. Zeeb 	node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
355340839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
355440839418SBjoern A. Zeeb 	    OID_AUTO, if_name(vap->iv_ifp),
355540839418SBjoern A. Zeeb 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
355640839418SBjoern A. Zeeb 
355740839418SBjoern A. Zeeb 	SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
355840839418SBjoern A. Zeeb 	    SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
355940839418SBjoern A. Zeeb 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
356040839418SBjoern A. Zeeb 	    lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
356140839418SBjoern A. Zeeb 
35626b4cac81SBjoern A. Zeeb 	IMPROVE();
35636b4cac81SBjoern A. Zeeb 
35646b4cac81SBjoern A. Zeeb 	return (vap);
35656b4cac81SBjoern A. Zeeb }
35666b4cac81SBjoern A. Zeeb 
35674b0af114SBjoern A. Zeeb void
35684b0af114SBjoern A. Zeeb linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
35694b0af114SBjoern A. Zeeb {
35704b0af114SBjoern A. Zeeb 
35714b0af114SBjoern A. Zeeb 	wiphy_unregister(hw->wiphy);
35724b0af114SBjoern A. Zeeb 	linuxkpi_ieee80211_ifdetach(hw);
35734b0af114SBjoern A. Zeeb 
35744b0af114SBjoern A. Zeeb 	IMPROVE();
35754b0af114SBjoern A. Zeeb }
35764b0af114SBjoern A. Zeeb 
35774b0af114SBjoern A. Zeeb void
35784b0af114SBjoern A. Zeeb linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
35794b0af114SBjoern A. Zeeb {
35804b0af114SBjoern A. Zeeb 
35814b0af114SBjoern A. Zeeb 	TODO();
35824b0af114SBjoern A. Zeeb }
35834b0af114SBjoern A. Zeeb 
35846b4cac81SBjoern A. Zeeb static void
35856b4cac81SBjoern A. Zeeb lkpi_ic_vap_delete(struct ieee80211vap *vap)
35866b4cac81SBjoern A. Zeeb {
35876b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
35886b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
35896b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
35906b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
35916b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
35926b4cac81SBjoern A. Zeeb 
35936b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
35946b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
35956b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
35966b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
35976b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
35986b4cac81SBjoern A. Zeeb 
35994aff4048SBjoern A. Zeeb 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
36004aff4048SBjoern A. Zeeb 
360140839418SBjoern A. Zeeb 	/* Clear up per-VIF/VAP sysctls. */
360240839418SBjoern A. Zeeb 	sysctl_ctx_free(&lvif->sysctl_ctx);
360340839418SBjoern A. Zeeb 
36048891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
36056b4cac81SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
36068891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
36076b4cac81SBjoern A. Zeeb 
36086b4cac81SBjoern A. Zeeb 	ieee80211_ratectl_deinit(vap);
36096b4cac81SBjoern A. Zeeb 	ieee80211_vap_detach(vap);
3610dbf76919SBjoern A. Zeeb 
3611dbf76919SBjoern A. Zeeb 	IMPROVE("clear up other bits in this state");
3612dbf76919SBjoern A. Zeeb 
3613dbf76919SBjoern A. Zeeb 	lkpi_80211_mo_remove_interface(hw, vif);
3614dbf76919SBjoern A. Zeeb 
36156c38c6b1SBjoern A. Zeeb 	/* Single VAP, so we can do this here. */
36167b43f4d0SBjoern A. Zeeb 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
36176c38c6b1SBjoern A. Zeeb 
36186b4cac81SBjoern A. Zeeb 	mtx_destroy(&lvif->mtx);
36196b4cac81SBjoern A. Zeeb 	free(lvif, M_80211_VAP);
36206b4cac81SBjoern A. Zeeb }
36216b4cac81SBjoern A. Zeeb 
36226b4cac81SBjoern A. Zeeb static void
36236b4cac81SBjoern A. Zeeb lkpi_ic_update_mcast(struct ieee80211com *ic)
36246b4cac81SBjoern A. Zeeb {
36256b4cac81SBjoern A. Zeeb 
36266b4cac81SBjoern A. Zeeb 	lkpi_update_mcast_filter(ic, false);
36276b4cac81SBjoern A. Zeeb 	TRACEOK();
36286b4cac81SBjoern A. Zeeb }
36296b4cac81SBjoern A. Zeeb 
36306b4cac81SBjoern A. Zeeb static void
36316b4cac81SBjoern A. Zeeb lkpi_ic_update_promisc(struct ieee80211com *ic)
36326b4cac81SBjoern A. Zeeb {
36336b4cac81SBjoern A. Zeeb 
36346b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36356b4cac81SBjoern A. Zeeb }
36366b4cac81SBjoern A. Zeeb 
36376b4cac81SBjoern A. Zeeb static void
36386b4cac81SBjoern A. Zeeb lkpi_ic_update_chw(struct ieee80211com *ic)
36396b4cac81SBjoern A. Zeeb {
36406b4cac81SBjoern A. Zeeb 
36416b4cac81SBjoern A. Zeeb 	UNIMPLEMENTED;
36426b4cac81SBjoern A. Zeeb }
36436b4cac81SBjoern A. Zeeb 
36446b4cac81SBjoern A. Zeeb /* Start / stop device. */
36456b4cac81SBjoern A. Zeeb static void
36466b4cac81SBjoern A. Zeeb lkpi_ic_parent(struct ieee80211com *ic)
36476b4cac81SBjoern A. Zeeb {
36486b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
36498d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36506b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
36516b4cac81SBjoern A. Zeeb 	int error;
36528d58a057SBjoern A. Zeeb #endif
36536b4cac81SBjoern A. Zeeb 	bool start_all;
36546b4cac81SBjoern A. Zeeb 
36556b4cac81SBjoern A. Zeeb 	IMPROVE();
36566b4cac81SBjoern A. Zeeb 
36576b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
36588d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36596b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
36608d58a057SBjoern A. Zeeb #endif
36616b4cac81SBjoern A. Zeeb 	start_all = false;
36626b4cac81SBjoern A. Zeeb 
36638ac540d3SBjoern A. Zeeb 	/* IEEE80211_UNLOCK(ic); */
36648ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
36656b4cac81SBjoern A. Zeeb 	if (ic->ic_nrunning > 0) {
36668d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36676b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_start(hw);
36686b4cac81SBjoern A. Zeeb 		if (error == 0)
36698d58a057SBjoern A. Zeeb #endif
36706b4cac81SBjoern A. Zeeb 			start_all = true;
36716b4cac81SBjoern A. Zeeb 	} else {
36728d58a057SBjoern A. Zeeb #ifdef HW_START_STOP
36737b43f4d0SBjoern A. Zeeb 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
36748d58a057SBjoern A. Zeeb #endif
36756b4cac81SBjoern A. Zeeb 	}
36768ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
36778ac540d3SBjoern A. Zeeb 	/* IEEE80211_LOCK(ic); */
36786b4cac81SBjoern A. Zeeb 
36796b4cac81SBjoern A. Zeeb 	if (start_all)
36806b4cac81SBjoern A. Zeeb 		ieee80211_start_all(ic);
36816b4cac81SBjoern A. Zeeb }
36826b4cac81SBjoern A. Zeeb 
36836b4cac81SBjoern A. Zeeb bool
36846b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
36856b4cac81SBjoern A. Zeeb     size_t ie_ids_len)
36866b4cac81SBjoern A. Zeeb {
36876b4cac81SBjoern A. Zeeb 	int i;
36886b4cac81SBjoern A. Zeeb 
36896b4cac81SBjoern A. Zeeb 	for (i = 0; i < ie_ids_len; i++) {
36906b4cac81SBjoern A. Zeeb 		if (ie == *ie_ids)
36916b4cac81SBjoern A. Zeeb 			return (true);
36926b4cac81SBjoern A. Zeeb 	}
36936b4cac81SBjoern A. Zeeb 
36946b4cac81SBjoern A. Zeeb 	return (false);
36956b4cac81SBjoern A. Zeeb }
36966b4cac81SBjoern A. Zeeb 
36976b4cac81SBjoern A. Zeeb /* Return true if skipped; false if error. */
36986b4cac81SBjoern A. Zeeb bool
36996b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
37006b4cac81SBjoern A. Zeeb {
37016b4cac81SBjoern A. Zeeb 	size_t x;
37026b4cac81SBjoern A. Zeeb 	uint8_t l;
37036b4cac81SBjoern A. Zeeb 
37046b4cac81SBjoern A. Zeeb 	x = *xp;
37056b4cac81SBjoern A. Zeeb 
37066b4cac81SBjoern A. Zeeb 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
37076b4cac81SBjoern A. Zeeb 	    __func__, x, ies_len, ies));
37086b4cac81SBjoern A. Zeeb 	l = ies[x + 1];
37096b4cac81SBjoern A. Zeeb 	x += 2 + l;
37106b4cac81SBjoern A. Zeeb 
37116b4cac81SBjoern A. Zeeb 	if (x > ies_len)
37126b4cac81SBjoern A. Zeeb 		return (false);
37136b4cac81SBjoern A. Zeeb 
37146b4cac81SBjoern A. Zeeb 	*xp = x;
37156b4cac81SBjoern A. Zeeb 	return (true);
37166b4cac81SBjoern A. Zeeb }
37176b4cac81SBjoern A. Zeeb 
3718d9945d78SBjoern A. Zeeb static uint8_t *
3719d9945d78SBjoern A. Zeeb lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3720d9945d78SBjoern A. Zeeb     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
37216b4cac81SBjoern A. Zeeb {
3722d9945d78SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
3723d9945d78SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
37243dd98026SBjoern A. Zeeb 	struct ieee80211com *ic;
3725d9945d78SBjoern A. Zeeb 	const struct ieee80211_channel *chan;
3726d9945d78SBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
3727d9945d78SBjoern A. Zeeb 	uint8_t *pb;
3728d9945d78SBjoern A. Zeeb 	int band, i;
37296b4cac81SBjoern A. Zeeb 
37303dd98026SBjoern A. Zeeb 	ic = vap->iv_ic;
3731d9945d78SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3732d9945d78SBjoern A. Zeeb 		if ((band_mask & (1 << band)) == 0)
3733d9945d78SBjoern A. Zeeb 			continue;
3734d9945d78SBjoern A. Zeeb 
3735d9945d78SBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
3736d9945d78SBjoern A. Zeeb 		/*
3737d9945d78SBjoern A. Zeeb 		 * This should not happen;
3738d9945d78SBjoern A. Zeeb 		 * band_mask is a bitmask of valid bands to scan on.
3739d9945d78SBjoern A. Zeeb 		 */
3740d9945d78SBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
3741d9945d78SBjoern A. Zeeb 			continue;
3742d9945d78SBjoern A. Zeeb 
3743d9945d78SBjoern A. Zeeb 		/* Find a first channel to get the mode and rates from. */
3744d9945d78SBjoern A. Zeeb 		channels = supband->channels;
3745d9945d78SBjoern A. Zeeb 		chan = NULL;
3746d9945d78SBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
3747d9945d78SBjoern A. Zeeb 
3748d9945d78SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3749d9945d78SBjoern A. Zeeb 				continue;
3750d9945d78SBjoern A. Zeeb 
37513dd98026SBjoern A. Zeeb 			chan = ieee80211_find_channel(ic,
3752d9945d78SBjoern A. Zeeb 			    channels[i].center_freq, 0);
3753d9945d78SBjoern A. Zeeb 			if (chan != NULL)
3754d9945d78SBjoern A. Zeeb 				break;
3755d9945d78SBjoern A. Zeeb 		}
3756d9945d78SBjoern A. Zeeb 
3757d9945d78SBjoern A. Zeeb 		/* This really should not happen. */
3758d9945d78SBjoern A. Zeeb 		if (chan == NULL)
3759d9945d78SBjoern A. Zeeb 			continue;
3760d9945d78SBjoern A. Zeeb 
3761d9945d78SBjoern A. Zeeb 		pb = p;
37623dd98026SBjoern A. Zeeb 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3763d9945d78SBjoern A. Zeeb 		p = ieee80211_add_rates(p, rs);
3764d9945d78SBjoern A. Zeeb 		p = ieee80211_add_xrates(p, rs);
3765d9945d78SBjoern A. Zeeb 
37663dd98026SBjoern A. Zeeb #if defined(LKPI_80211_HT)
37673dd98026SBjoern A. Zeeb 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
37683dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
37693dd98026SBjoern A. Zeeb 
37703dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37713dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
37723dd98026SBjoern A. Zeeb 			p = ieee80211_add_htcap_ch(p, vap, c);
37733dd98026SBjoern A. Zeeb 		}
37743dd98026SBjoern A. Zeeb #endif
37753dd98026SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
37763dd98026SBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
37773dd98026SBjoern A. Zeeb 			struct ieee80211_channel *c;
37783dd98026SBjoern A. Zeeb 
37793dd98026SBjoern A. Zeeb 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
37803dd98026SBjoern A. Zeeb 			    vap->iv_flags_ht);
37813dd98026SBjoern A. Zeeb 			c = ieee80211_vht_adjust_channel(ic, c,
37823dd98026SBjoern A. Zeeb 			    vap->iv_vht_flags);
37833dd98026SBjoern A. Zeeb 			p = ieee80211_add_vhtcap_ch(p, vap, c);
37843dd98026SBjoern A. Zeeb 		}
37853dd98026SBjoern A. Zeeb #endif
37863dd98026SBjoern A. Zeeb 
3787d9945d78SBjoern A. Zeeb 		scan_ies->ies[band] = pb;
3788d9945d78SBjoern A. Zeeb 		scan_ies->len[band] = p - pb;
3789d9945d78SBjoern A. Zeeb 	}
3790d9945d78SBjoern A. Zeeb 
3791d9945d78SBjoern A. Zeeb 	/* Add common_ies */
3792d9945d78SBjoern A. Zeeb 	pb = p;
3793d9945d78SBjoern A. Zeeb 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3794d9945d78SBjoern A. Zeeb 	    vap->iv_wpa_ie != NULL) {
3795d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3796d9945d78SBjoern A. Zeeb 		p += 2 + vap->iv_wpa_ie[1];
3797d9945d78SBjoern A. Zeeb 	}
3798d9945d78SBjoern A. Zeeb 	if (vap->iv_appie_probereq != NULL) {
3799d9945d78SBjoern A. Zeeb 		memcpy(p, vap->iv_appie_probereq->ie_data,
3800d9945d78SBjoern A. Zeeb 		    vap->iv_appie_probereq->ie_len);
3801d9945d78SBjoern A. Zeeb 		p += vap->iv_appie_probereq->ie_len;
3802d9945d78SBjoern A. Zeeb 	}
3803d9945d78SBjoern A. Zeeb 	scan_ies->common_ies = pb;
3804d9945d78SBjoern A. Zeeb 	scan_ies->common_ie_len = p - pb;
3805d9945d78SBjoern A. Zeeb 
3806d9945d78SBjoern A. Zeeb 	return (p);
38076b4cac81SBjoern A. Zeeb }
38086b4cac81SBjoern A. Zeeb 
38096b4cac81SBjoern A. Zeeb static void
38106b4cac81SBjoern A. Zeeb lkpi_ic_scan_start(struct ieee80211com *ic)
38116b4cac81SBjoern A. Zeeb {
38126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
38136b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
38146b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
38156b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
38166b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
38176b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
38186b4cac81SBjoern A. Zeeb 	int error;
38198ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
38206b4cac81SBjoern A. Zeeb 
38216b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
38228ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3823a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
38246b4cac81SBjoern A. Zeeb 		/* A scan is still running. */
38258ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38266b4cac81SBjoern A. Zeeb 		return;
38276b4cac81SBjoern A. Zeeb 	}
38288ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
38298ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
38306b4cac81SBjoern A. Zeeb 
38316b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
38326b4cac81SBjoern A. Zeeb 	vap = ss->ss_vap;
38336b4cac81SBjoern A. Zeeb 	if (vap->iv_state != IEEE80211_S_SCAN) {
3834d3ef7fb4SBjoern A. Zeeb 		IMPROVE("We need to be able to scan if not in S_SCAN");
38356b4cac81SBjoern A. Zeeb 		return;
38366b4cac81SBjoern A. Zeeb 	}
38376b4cac81SBjoern A. Zeeb 
38386b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
38398ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
3840a486fbbdSBjoern A. Zeeb 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3841a486fbbdSBjoern A. Zeeb 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3842d3ef7fb4SBjoern A. Zeeb sw_scan:
38436b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
38446b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
3845086be6a8SBjoern A. Zeeb 
3846086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
3847086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, false);
3848086be6a8SBjoern A. Zeeb 
38496b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
38506b4cac81SBjoern A. Zeeb 		/* net80211::scan_start() handled PS for us. */
38516b4cac81SBjoern A. Zeeb 		IMPROVE();
38526b4cac81SBjoern A. Zeeb 		/* XXX Also means it is too late to flush queues?
38536b4cac81SBjoern A. Zeeb 		 * need to check iv_sta_ps or overload? */
38546b4cac81SBjoern A. Zeeb 		/* XXX want to adjust ss end time/ maxdwell? */
38556b4cac81SBjoern A. Zeeb 
38566b4cac81SBjoern A. Zeeb 	} else {
38576b4cac81SBjoern A. Zeeb 		struct ieee80211_scan_request *hw_req;
38586b4cac81SBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *lc, **cpp;
38596b4cac81SBjoern A. Zeeb 		struct cfg80211_ssid *ssids;
38606b4cac81SBjoern A. Zeeb 		struct cfg80211_scan_6ghz_params *s6gp;
38616b4cac81SBjoern A. Zeeb 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3862d9945d78SBjoern A. Zeeb 		int band, i, ssid_count, common_ie_len;
3863d9945d78SBjoern A. Zeeb 		uint32_t band_mask;
3864d9945d78SBjoern A. Zeeb 		uint8_t *ie, *ieend;
38653206587aSBjoern A. Zeeb 		bool running;
3866d9945d78SBjoern A. Zeeb 
3867d9945d78SBjoern A. Zeeb 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3868d9945d78SBjoern A. Zeeb 		ssids_len = ssid_count * sizeof(*ssids);
38696b4cac81SBjoern A. Zeeb 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
38706b4cac81SBjoern A. Zeeb 
3871d9945d78SBjoern A. Zeeb 		band_mask = 0;
38726b4cac81SBjoern A. Zeeb 		nchan = 0;
3873c272abc5SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3874c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
3875d9945d78SBjoern A. Zeeb 			for (i = ss->ss_next; i < ss->ss_last; i++) {
38766b4cac81SBjoern A. Zeeb 				nchan++;
3877d9945d78SBjoern A. Zeeb 				band = lkpi_net80211_chan_to_nl80211_band(
3878d9945d78SBjoern A. Zeeb 				    ss->ss_chans[ss->ss_next + i]);
3879d9945d78SBjoern A. Zeeb 				band_mask |= (1 << band);
3880d9945d78SBjoern A. Zeeb 			}
3881c272abc5SBjoern A. Zeeb #else
3882c272abc5SBjoern A. Zeeb 			/* Instead we scan for all channels all the time. */
3883c272abc5SBjoern A. Zeeb 			for (band = 0; band < NUM_NL80211_BANDS; band++) {
3884c272abc5SBjoern A. Zeeb 				switch (band) {
3885c272abc5SBjoern A. Zeeb 				case NL80211_BAND_2GHZ:
3886c272abc5SBjoern A. Zeeb 				case NL80211_BAND_5GHZ:
3887c272abc5SBjoern A. Zeeb 					break;
3888c272abc5SBjoern A. Zeeb 				default:
3889c272abc5SBjoern A. Zeeb 					continue;
3890c272abc5SBjoern A. Zeeb 				}
3891c272abc5SBjoern A. Zeeb 				if (hw->wiphy->bands[band] != NULL) {
3892c272abc5SBjoern A. Zeeb 					nchan += hw->wiphy->bands[band]->n_channels;
3893c272abc5SBjoern A. Zeeb 					band_mask |= (1 << band);
3894c272abc5SBjoern A. Zeeb 				}
3895c272abc5SBjoern A. Zeeb 			}
3896c272abc5SBjoern A. Zeeb #endif
3897c272abc5SBjoern A. Zeeb 		} else {
38983206587aSBjoern A. Zeeb 			IMPROVE("individual band scans not yet supported, only scanning first band");
38993206587aSBjoern A. Zeeb 			/* In theory net80211 should drive this. */
39003206587aSBjoern A. Zeeb 			/* Probably we need to add local logic for now;
39013206587aSBjoern A. Zeeb 			 * need to deal with scan_complete
39023206587aSBjoern A. Zeeb 			 * and cancel_scan and keep local state.
39033206587aSBjoern A. Zeeb 			 * Also cut the nchan down above.
39043206587aSBjoern A. Zeeb 			 */
39053206587aSBjoern A. Zeeb 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
39063206587aSBjoern A. Zeeb 		}
39073206587aSBjoern A. Zeeb 
39086b4cac81SBjoern A. Zeeb 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
39096b4cac81SBjoern A. Zeeb 
3910d9945d78SBjoern A. Zeeb 		common_ie_len = 0;
3911d9945d78SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3912d9945d78SBjoern A. Zeeb 		    vap->iv_wpa_ie != NULL)
3913d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_wpa_ie[1];
3914d9945d78SBjoern A. Zeeb 		if (vap->iv_appie_probereq != NULL)
3915d9945d78SBjoern A. Zeeb 			common_ie_len += vap->iv_appie_probereq->ie_len;
3916d9945d78SBjoern A. Zeeb 
3917d9945d78SBjoern A. Zeeb 		/* We would love to check this at an earlier stage... */
3918d9945d78SBjoern A. Zeeb 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3919d9945d78SBjoern A. Zeeb 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3920d9945d78SBjoern A. Zeeb 			    "wiphy->max_scan_ie_len %d\n", __func__,
3921d9945d78SBjoern A. Zeeb 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3922d9945d78SBjoern A. Zeeb 		}
3923d9945d78SBjoern A. Zeeb 
39243206587aSBjoern A. Zeeb 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3925d9945d78SBjoern A. Zeeb 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3926d9945d78SBjoern A. Zeeb 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
39276b4cac81SBjoern A. Zeeb 
39286b4cac81SBjoern A. Zeeb 		hw_req->req.flags = 0;			/* XXX ??? */
39296b4cac81SBjoern A. Zeeb 		/* hw_req->req.wdev */
39306b4cac81SBjoern A. Zeeb 		hw_req->req.wiphy = hw->wiphy;
39316b4cac81SBjoern A. Zeeb 		hw_req->req.no_cck = false;		/* XXX */
39326b4cac81SBjoern A. Zeeb #if 0
39336b4cac81SBjoern A. Zeeb 		/* This seems to pessimise default scanning behaviour. */
39346b4cac81SBjoern A. Zeeb 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
39356b4cac81SBjoern A. Zeeb 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
39366b4cac81SBjoern A. Zeeb #endif
39376b4cac81SBjoern A. Zeeb #ifdef __notyet__
39386b4cac81SBjoern A. Zeeb 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
39396b4cac81SBjoern A. Zeeb 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
39406b4cac81SBjoern A. Zeeb 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
39416b4cac81SBjoern A. Zeeb #endif
3942e1e90be0SBjoern A. Zeeb 		eth_broadcast_addr(hw_req->req.bssid);
39436b4cac81SBjoern A. Zeeb 
39446b4cac81SBjoern A. Zeeb 		hw_req->req.n_channels = nchan;
39456b4cac81SBjoern A. Zeeb 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
39466b4cac81SBjoern A. Zeeb 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
39476b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
39486b4cac81SBjoern A. Zeeb 			*(cpp + i) =
39496b4cac81SBjoern A. Zeeb 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
39506b4cac81SBjoern A. Zeeb 		}
3951c272abc5SBjoern A. Zeeb #if 0	/* Avoid net80211 scan lists until it has proper scan offload support. */
39526b4cac81SBjoern A. Zeeb 		for (i = 0; i < nchan; i++) {
3953c272abc5SBjoern A. Zeeb 			struct ieee80211_channel *c;
39546b4cac81SBjoern A. Zeeb 
3955c272abc5SBjoern A. Zeeb 			c = ss->ss_chans[ss->ss_next + i];
39566b4cac81SBjoern A. Zeeb 			lc->hw_value = c->ic_ieee;
39573206587aSBjoern A. Zeeb 			lc->center_freq = c->ic_freq;	/* XXX */
39586b4cac81SBjoern A. Zeeb 			/* lc->flags */
39596b4cac81SBjoern A. Zeeb 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
39606b4cac81SBjoern A. Zeeb 			lc->max_power = c->ic_maxpower;
39616b4cac81SBjoern A. Zeeb 			/* lc-> ... */
39626b4cac81SBjoern A. Zeeb 			lc++;
39636b4cac81SBjoern A. Zeeb 		}
3964c272abc5SBjoern A. Zeeb #else
3965c272abc5SBjoern A. Zeeb 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3966c272abc5SBjoern A. Zeeb 			struct ieee80211_supported_band *supband;
3967c272abc5SBjoern A. Zeeb 			struct linuxkpi_ieee80211_channel *channels;
3968c272abc5SBjoern A. Zeeb 
3969c272abc5SBjoern A. Zeeb 			/* Band disabled for scanning? */
3970c272abc5SBjoern A. Zeeb 			if ((band_mask & (1 << band)) == 0)
3971c272abc5SBjoern A. Zeeb 				continue;
3972c272abc5SBjoern A. Zeeb 
3973c272abc5SBjoern A. Zeeb 			/* Nothing to scan in band? */
3974c272abc5SBjoern A. Zeeb 			supband = hw->wiphy->bands[band];
3975c272abc5SBjoern A. Zeeb 			if (supband == NULL || supband->n_channels == 0)
3976c272abc5SBjoern A. Zeeb 				continue;
3977c272abc5SBjoern A. Zeeb 
3978c272abc5SBjoern A. Zeeb 			channels = supband->channels;
3979c272abc5SBjoern A. Zeeb 			for (i = 0; i < supband->n_channels; i++) {
3980c272abc5SBjoern A. Zeeb 				*lc = channels[i];
3981c272abc5SBjoern A. Zeeb 				lc++;
3982c272abc5SBjoern A. Zeeb 			}
3983c272abc5SBjoern A. Zeeb 		}
3984c272abc5SBjoern A. Zeeb #endif
39856b4cac81SBjoern A. Zeeb 
3986d9945d78SBjoern A. Zeeb 		hw_req->req.n_ssids = ssid_count;
39876b4cac81SBjoern A. Zeeb 		if (hw_req->req.n_ssids > 0) {
39886b4cac81SBjoern A. Zeeb 			ssids = (struct cfg80211_ssid *)lc;
39896b4cac81SBjoern A. Zeeb 			hw_req->req.ssids = ssids;
3990d9945d78SBjoern A. Zeeb 			for (i = 0; i < ssid_count; i++) {
39916b4cac81SBjoern A. Zeeb 				ssids->ssid_len = ss->ss_ssid[i].len;
39926b4cac81SBjoern A. Zeeb 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
39936b4cac81SBjoern A. Zeeb 				    ss->ss_ssid[i].len);
39946b4cac81SBjoern A. Zeeb 				ssids++;
39956b4cac81SBjoern A. Zeeb 			}
39966b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
39976b4cac81SBjoern A. Zeeb 		} else {
39986b4cac81SBjoern A. Zeeb 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
39996b4cac81SBjoern A. Zeeb 		}
40006b4cac81SBjoern A. Zeeb 
40016b4cac81SBjoern A. Zeeb 		/* 6GHz one day. */
40026b4cac81SBjoern A. Zeeb 		hw_req->req.n_6ghz_params = 0;
40036b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz_params = NULL;
40046b4cac81SBjoern A. Zeeb 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
40056b4cac81SBjoern A. Zeeb 		/* s6gp->... */
40066b4cac81SBjoern A. Zeeb 
4007d9945d78SBjoern A. Zeeb 		ie = ieend = (uint8_t *)s6gp;
4008d9945d78SBjoern A. Zeeb 		/* Copy per-band IEs, copy common IEs */
4009d9945d78SBjoern A. Zeeb 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4010d9945d78SBjoern A. Zeeb 		hw_req->req.ie = ie;
4011d9945d78SBjoern A. Zeeb 		hw_req->req.ie_len = ieend - ie;
4012d9945d78SBjoern A. Zeeb 
40136b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
40146b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
40153206587aSBjoern A. Zeeb 
40163206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_LOCK(lhw);
40173206587aSBjoern A. Zeeb 		/* Re-check under lock. */
40183206587aSBjoern A. Zeeb 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
40193206587aSBjoern A. Zeeb 		if (!running) {
40203206587aSBjoern A. Zeeb 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
40213206587aSBjoern A. Zeeb 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
40223206587aSBjoern A. Zeeb 
40233206587aSBjoern A. Zeeb 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
40243206587aSBjoern A. Zeeb 			lhw->hw_req = hw_req;
40253206587aSBjoern A. Zeeb 		}
40263206587aSBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40273206587aSBjoern A. Zeeb 		if (running) {
40283206587aSBjoern A. Zeeb 			free(hw_req, M_LKPI80211);
40293206587aSBjoern A. Zeeb 			return;
40303206587aSBjoern A. Zeeb 		}
40313206587aSBjoern A. Zeeb 
40326b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
40336b4cac81SBjoern A. Zeeb 		if (error != 0) {
4034d9945d78SBjoern A. Zeeb 			ieee80211_cancel_scan(vap);
4035d9945d78SBjoern A. Zeeb 
40363206587aSBjoern A. Zeeb 			/*
40373206587aSBjoern A. Zeeb 			 * ieee80211_scan_completed must be called in either
40383206587aSBjoern A. Zeeb 			 * case of error or none.  So let the free happen there
40393206587aSBjoern A. Zeeb 			 * and only there.
40403206587aSBjoern A. Zeeb 			 * That would be fine in theory but in practice drivers
40413206587aSBjoern A. Zeeb 			 * behave differently:
40423206587aSBjoern A. Zeeb 			 * ath10k does not return hw_scan until after scan_complete
40433206587aSBjoern A. Zeeb 			 *        and can then still return an error.
40443206587aSBjoern A. Zeeb 			 * rtw88 can return 1 or -EBUSY without scan_complete
40453206587aSBjoern A. Zeeb 			 * iwlwifi can return various errors before scan starts
40463206587aSBjoern A. Zeeb 			 * ...
40473206587aSBjoern A. Zeeb 			 * So we cannot rely on that behaviour and have to check
40483206587aSBjoern A. Zeeb 			 * and balance between both code paths.
40493206587aSBjoern A. Zeeb 			 */
40503206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_LOCK(lhw);
40513206587aSBjoern A. Zeeb 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
40523206587aSBjoern A. Zeeb 				free(lhw->hw_req, M_LKPI80211);
40536b4cac81SBjoern A. Zeeb 				lhw->hw_req = NULL;
40543206587aSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
40553206587aSBjoern A. Zeeb 			}
40563206587aSBjoern A. Zeeb 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4057d3ef7fb4SBjoern A. Zeeb 
4058d3ef7fb4SBjoern A. Zeeb 			/*
4059d3ef7fb4SBjoern A. Zeeb 			 * XXX-SIGH magic number.
4060d3ef7fb4SBjoern A. Zeeb 			 * rtw88 has a magic "return 1" if offloading scan is
4061d3ef7fb4SBjoern A. Zeeb 			 * not possible.  Fall back to sw scan in that case.
4062d3ef7fb4SBjoern A. Zeeb 			 */
4063196cfd0bSBjoern A. Zeeb 			if (error == 1) {
40648ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_LOCK(lhw);
4065a486fbbdSBjoern A. Zeeb 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
40668ac540d3SBjoern A. Zeeb 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
40673206587aSBjoern A. Zeeb 				/*
40683206587aSBjoern A. Zeeb 				 * XXX If we clear this now and later a driver
40693206587aSBjoern A. Zeeb 				 * thinks it * can do a hw_scan again, we will
40703206587aSBjoern A. Zeeb 				 * currently not re-enable it?
40713206587aSBjoern A. Zeeb 				 */
40723206587aSBjoern A. Zeeb 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4073196cfd0bSBjoern A. Zeeb 				ieee80211_start_scan(vap,
4074196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ACTIVE |
4075196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_NOPICK |
4076196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_ONCE,
4077196cfd0bSBjoern A. Zeeb 				    IEEE80211_SCAN_FOREVER,
4078196cfd0bSBjoern A. Zeeb 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4079196cfd0bSBjoern A. Zeeb 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4080196cfd0bSBjoern A. Zeeb 				    vap->iv_des_nssid, vap->iv_des_ssid);
4081d3ef7fb4SBjoern A. Zeeb 				goto sw_scan;
4082196cfd0bSBjoern A. Zeeb 			}
4083d3ef7fb4SBjoern A. Zeeb 
4084d3ef7fb4SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4085d3ef7fb4SBjoern A. Zeeb 			    __func__, error);
40866b4cac81SBjoern A. Zeeb 		}
40876b4cac81SBjoern A. Zeeb 	}
40886b4cac81SBjoern A. Zeeb }
40896b4cac81SBjoern A. Zeeb 
40906b4cac81SBjoern A. Zeeb static void
40916b4cac81SBjoern A. Zeeb lkpi_ic_scan_end(struct ieee80211com *ic)
40926b4cac81SBjoern A. Zeeb {
40936b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
40948ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
40956b4cac81SBjoern A. Zeeb 
40966b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
40978ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
4098a486fbbdSBjoern A. Zeeb 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
40998ac540d3SBjoern A. Zeeb 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41006b4cac81SBjoern A. Zeeb 		return;
41016b4cac81SBjoern A. Zeeb 	}
41028ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41038ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41046b4cac81SBjoern A. Zeeb 
41058ac540d3SBjoern A. Zeeb 	if (!is_hw_scan) {
4106a486fbbdSBjoern A. Zeeb 		struct ieee80211_scan_state *ss;
4107a486fbbdSBjoern A. Zeeb 		struct ieee80211vap *vap;
41086b4cac81SBjoern A. Zeeb 		struct ieee80211_hw *hw;
41096b4cac81SBjoern A. Zeeb 		struct lkpi_vif *lvif;
41106b4cac81SBjoern A. Zeeb 		struct ieee80211_vif *vif;
41116b4cac81SBjoern A. Zeeb 
4112a486fbbdSBjoern A. Zeeb 		ss = ic->ic_scan;
4113a486fbbdSBjoern A. Zeeb 		vap = ss->ss_vap;
41146b4cac81SBjoern A. Zeeb 		hw = LHW_TO_HW(lhw);
41156b4cac81SBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
41166b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
4117a486fbbdSBjoern A. Zeeb 
41186b4cac81SBjoern A. Zeeb 		lkpi_80211_mo_sw_scan_complete(hw, vif);
41196b4cac81SBjoern A. Zeeb 
41206b4cac81SBjoern A. Zeeb 		/* Send PS to stop buffering if n80211 does not for us? */
4121086be6a8SBjoern A. Zeeb 
4122086be6a8SBjoern A. Zeeb 		if (vap->iv_state == IEEE80211_S_SCAN)
4123086be6a8SBjoern A. Zeeb 			lkpi_hw_conf_idle(hw, true);
41246b4cac81SBjoern A. Zeeb 	}
41256b4cac81SBjoern A. Zeeb }
41266b4cac81SBjoern A. Zeeb 
41276b4cac81SBjoern A. Zeeb static void
4128a486fbbdSBjoern A. Zeeb lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4129a486fbbdSBjoern A. Zeeb     unsigned long maxdwell)
4130a486fbbdSBjoern A. Zeeb {
4131a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41328ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4133a486fbbdSBjoern A. Zeeb 
4134a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41358ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41368ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41378ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41388ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4139a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_curchan(ss, maxdwell);
4140a486fbbdSBjoern A. Zeeb }
4141a486fbbdSBjoern A. Zeeb 
4142a486fbbdSBjoern A. Zeeb static void
4143a486fbbdSBjoern A. Zeeb lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4144a486fbbdSBjoern A. Zeeb {
4145a486fbbdSBjoern A. Zeeb 	struct lkpi_hw *lhw;
41468ac540d3SBjoern A. Zeeb 	bool is_hw_scan;
4147a486fbbdSBjoern A. Zeeb 
4148a486fbbdSBjoern A. Zeeb 	lhw = ss->ss_ic->ic_softc;
41498ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41508ac540d3SBjoern A. Zeeb 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
41518ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41528ac540d3SBjoern A. Zeeb 	if (!is_hw_scan)
4153a486fbbdSBjoern A. Zeeb 		lhw->ic_scan_mindwell(ss);
4154a486fbbdSBjoern A. Zeeb }
4155a486fbbdSBjoern A. Zeeb 
4156a486fbbdSBjoern A. Zeeb static void
41576b4cac81SBjoern A. Zeeb lkpi_ic_set_channel(struct ieee80211com *ic)
41586b4cac81SBjoern A. Zeeb {
41596b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
41606b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4161b2cf3c21SBjoern A. Zeeb 	struct ieee80211_channel *c;
4162b2cf3c21SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
41636b4cac81SBjoern A. Zeeb 	int error;
41648ac540d3SBjoern A. Zeeb 	bool hw_scan_running;
41656b4cac81SBjoern A. Zeeb 
41666b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
4167b2cf3c21SBjoern A. Zeeb 
4168b2cf3c21SBjoern A. Zeeb 	/* If we do not support (*config)() save us the work. */
4169b2cf3c21SBjoern A. Zeeb 	if (lhw->ops->config == NULL)
41706b4cac81SBjoern A. Zeeb 		return;
41716b4cac81SBjoern A. Zeeb 
4172a486fbbdSBjoern A. Zeeb 	/* If we have a hw_scan running do not switch channels. */
41738ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
41748ac540d3SBjoern A. Zeeb 	hw_scan_running =
41758ac540d3SBjoern A. Zeeb 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
41768ac540d3SBjoern A. Zeeb 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
41778ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
41788ac540d3SBjoern A. Zeeb 	if (hw_scan_running)
4179a486fbbdSBjoern A. Zeeb 		return;
4180a486fbbdSBjoern A. Zeeb 
4181b2cf3c21SBjoern A. Zeeb 	c = ic->ic_curchan;
4182b2cf3c21SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
41836b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
41846b4cac81SBjoern A. Zeeb 		    c, lhw->ops->config);
41856b4cac81SBjoern A. Zeeb 		return;
41866b4cac81SBjoern A. Zeeb 	}
41876b4cac81SBjoern A. Zeeb 
41886b4cac81SBjoern A. Zeeb 	chan = lkpi_find_lkpi80211_chan(lhw, c);
41896b4cac81SBjoern A. Zeeb 	if (chan == NULL) {
41906b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
41916b4cac81SBjoern A. Zeeb 		    c, chan);
41926b4cac81SBjoern A. Zeeb 		return;
41936b4cac81SBjoern A. Zeeb 	}
41946b4cac81SBjoern A. Zeeb 
41956b4cac81SBjoern A. Zeeb 	/* XXX max power for scanning? */
41966b4cac81SBjoern A. Zeeb 	IMPROVE();
41976b4cac81SBjoern A. Zeeb 
41986b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
41994a07abdeSBjoern A. Zeeb 	cfg80211_chandef_create(&hw->conf.chandef, chan,
42009fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
420111450726SBjoern A. Zeeb 	    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
42029fb91463SBjoern A. Zeeb #endif
42034a07abdeSBjoern A. Zeeb 	    NL80211_CHAN_NO_HT);
42046b4cac81SBjoern A. Zeeb 
42056b4cac81SBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
42066b4cac81SBjoern A. Zeeb 	if (error != 0 && error != EOPNOTSUPP) {
42076b4cac81SBjoern A. Zeeb 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
42086b4cac81SBjoern A. Zeeb 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
42096b4cac81SBjoern A. Zeeb 		/* XXX should we unroll to the previous chandef? */
42106b4cac81SBjoern A. Zeeb 		IMPROVE();
42116b4cac81SBjoern A. Zeeb 	} else {
42126b4cac81SBjoern A. Zeeb 		/* Update radiotap channels as well. */
42136b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
42146b4cac81SBjoern A. Zeeb 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
42156b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
42166b4cac81SBjoern A. Zeeb 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
42176b4cac81SBjoern A. Zeeb 	}
42186b4cac81SBjoern A. Zeeb 
42196b4cac81SBjoern A. Zeeb 	/* Currently PS is hard coded off! Not sure it belongs here. */
42206b4cac81SBjoern A. Zeeb 	IMPROVE();
42216b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
42226b4cac81SBjoern A. Zeeb 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
42236b4cac81SBjoern A. Zeeb 		hw->conf.flags &= ~IEEE80211_CONF_PS;
42246b4cac81SBjoern A. Zeeb 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
42256b4cac81SBjoern A. Zeeb 		if (error != 0 && error != EOPNOTSUPP)
42266b4cac81SBjoern A. Zeeb 			ic_printf(ic, "ERROR: %s: config %#0x returned "
42276b4cac81SBjoern A. Zeeb 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
42286b4cac81SBjoern A. Zeeb 			    error);
42296b4cac81SBjoern A. Zeeb 	}
42306b4cac81SBjoern A. Zeeb }
42316b4cac81SBjoern A. Zeeb 
42326b4cac81SBjoern A. Zeeb static struct ieee80211_node *
42336b4cac81SBjoern A. Zeeb lkpi_ic_node_alloc(struct ieee80211vap *vap,
42346b4cac81SBjoern A. Zeeb     const uint8_t mac[IEEE80211_ADDR_LEN])
42356b4cac81SBjoern A. Zeeb {
42366b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42376b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42384f61ef8bSBjoern A. Zeeb 	struct ieee80211_node *ni;
42396b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
42406b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
42416b4cac81SBjoern A. Zeeb 
42426b4cac81SBjoern A. Zeeb 	ic = vap->iv_ic;
42436b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42446b4cac81SBjoern A. Zeeb 
42456b4cac81SBjoern A. Zeeb 	/* We keep allocations de-coupled so we can deal with the two worlds. */
42464f61ef8bSBjoern A. Zeeb 	if (lhw->ic_node_alloc == NULL)
42474f61ef8bSBjoern A. Zeeb 		return (NULL);
42484f61ef8bSBjoern A. Zeeb 
42496b4cac81SBjoern A. Zeeb 	ni = lhw->ic_node_alloc(vap, mac);
42506b4cac81SBjoern A. Zeeb 	if (ni == NULL)
42516b4cac81SBjoern A. Zeeb 		return (NULL);
42526b4cac81SBjoern A. Zeeb 
42536b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
42544f61ef8bSBjoern A. Zeeb 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
42556b4cac81SBjoern A. Zeeb 	if (lsta == NULL) {
42566b4cac81SBjoern A. Zeeb 		if (lhw->ic_node_free != NULL)
42576b4cac81SBjoern A. Zeeb 			lhw->ic_node_free(ni);
42586b4cac81SBjoern A. Zeeb 		return (NULL);
42596b4cac81SBjoern A. Zeeb 	}
42606b4cac81SBjoern A. Zeeb 
42616b4cac81SBjoern A. Zeeb 	return (ni);
42626b4cac81SBjoern A. Zeeb }
42636b4cac81SBjoern A. Zeeb 
42646b4cac81SBjoern A. Zeeb static int
42656b4cac81SBjoern A. Zeeb lkpi_ic_node_init(struct ieee80211_node *ni)
42666b4cac81SBjoern A. Zeeb {
42676b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42696b4cac81SBjoern A. Zeeb 	int error;
42706b4cac81SBjoern A. Zeeb 
42716b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42726b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42736b4cac81SBjoern A. Zeeb 
42746b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_init != NULL) {
42756b4cac81SBjoern A. Zeeb 		error = lhw->ic_node_init(ni);
42766b4cac81SBjoern A. Zeeb 		if (error != 0)
42776b4cac81SBjoern A. Zeeb 			return (error);
42786b4cac81SBjoern A. Zeeb 	}
42796b4cac81SBjoern A. Zeeb 
42806b4cac81SBjoern A. Zeeb 	/* XXX-BZ Sync other state over. */
42816b4cac81SBjoern A. Zeeb 	IMPROVE();
42826b4cac81SBjoern A. Zeeb 
42836b4cac81SBjoern A. Zeeb 	return (0);
42846b4cac81SBjoern A. Zeeb }
42856b4cac81SBjoern A. Zeeb 
42866b4cac81SBjoern A. Zeeb static void
42876b4cac81SBjoern A. Zeeb lkpi_ic_node_cleanup(struct ieee80211_node *ni)
42886b4cac81SBjoern A. Zeeb {
42896b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
42906b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
42916b4cac81SBjoern A. Zeeb 
42926b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
42936b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
42946b4cac81SBjoern A. Zeeb 
42956b4cac81SBjoern A. Zeeb 	/* XXX-BZ remove from driver, ... */
42966b4cac81SBjoern A. Zeeb 	IMPROVE();
42976b4cac81SBjoern A. Zeeb 
42986b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_cleanup != NULL)
42996b4cac81SBjoern A. Zeeb 		lhw->ic_node_cleanup(ni);
43006b4cac81SBjoern A. Zeeb }
43016b4cac81SBjoern A. Zeeb 
43026b4cac81SBjoern A. Zeeb static void
43036b4cac81SBjoern A. Zeeb lkpi_ic_node_free(struct ieee80211_node *ni)
43046b4cac81SBjoern A. Zeeb {
43056b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
43066b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
43076b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43086b4cac81SBjoern A. Zeeb 
43096b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
43106b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
43116b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
43126b4cac81SBjoern A. Zeeb 
43130936c648SBjoern A. Zeeb 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
43146b4cac81SBjoern A. Zeeb 
43150936c648SBjoern A. Zeeb 	/*
43160936c648SBjoern A. Zeeb 	 * Pass in the original ni just in case of error we could check that
43170936c648SBjoern A. Zeeb 	 * it is the same as lsta->ni.
43180936c648SBjoern A. Zeeb 	 */
43190936c648SBjoern A. Zeeb 	lkpi_lsta_free(lsta, ni);
43206b4cac81SBjoern A. Zeeb 
43216b4cac81SBjoern A. Zeeb 	if (lhw->ic_node_free != NULL)
43226b4cac81SBjoern A. Zeeb 		lhw->ic_node_free(ni);
43236b4cac81SBjoern A. Zeeb }
43246b4cac81SBjoern A. Zeeb 
43256b4cac81SBjoern A. Zeeb static int
43266b4cac81SBjoern A. Zeeb lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
43276b4cac81SBjoern A. Zeeb         const struct ieee80211_bpf_params *params __unused)
43286b4cac81SBjoern A. Zeeb {
43296b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
43306b4cac81SBjoern A. Zeeb 
43316b4cac81SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4332fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
43332372f8ccSBjoern A. Zeeb #if 0
433488665349SBjoern A. Zeeb 	if (!lsta->added_to_drv || !lsta->txq_ready) {
43352372f8ccSBjoern A. Zeeb #else
43362372f8ccSBjoern A. Zeeb 	/*
43372372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
43382372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
43392372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
43402372f8ccSBjoern A. Zeeb 	 */
43412372f8ccSBjoern A. Zeeb 	if (!lsta->txq_ready) {
43422372f8ccSBjoern A. Zeeb #endif
4343fa4e4257SBjoern A. Zeeb 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
4344fa4e4257SBjoern A. Zeeb 		/*
4345fa4e4257SBjoern A. Zeeb 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
4346fa4e4257SBjoern A. Zeeb 		 * ieee80211_raw_output() does that in case of error).
4347fa4e4257SBjoern A. Zeeb 		 */
43480936c648SBjoern A. Zeeb 		m_free(m);
43490936c648SBjoern A. Zeeb 		return (ENETDOWN);
43500936c648SBjoern A. Zeeb 	}
43516b4cac81SBjoern A. Zeeb 
43526b4cac81SBjoern A. Zeeb 	/* Queue the packet and enqueue the task to handle it. */
43536b4cac81SBjoern A. Zeeb 	mbufq_enqueue(&lsta->txq, m);
4354fa4e4257SBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
4355fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
43566b4cac81SBjoern A. Zeeb 
43579d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
43589d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
43596b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
43606b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
43616b4cac81SBjoern A. Zeeb 		    mbufq_len(&lsta->txq));
43629d9ba2b7SBjoern A. Zeeb #endif
43636b4cac81SBjoern A. Zeeb 
43646b4cac81SBjoern A. Zeeb 	return (0);
43656b4cac81SBjoern A. Zeeb }
43666b4cac81SBjoern A. Zeeb 
436711db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
436811db70b6SBjoern A. Zeeb static int
436911db70b6SBjoern A. Zeeb lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
437011db70b6SBjoern A. Zeeb     struct sk_buff *skb)
437111db70b6SBjoern A. Zeeb {
437211db70b6SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
437311db70b6SBjoern A. Zeeb 	struct ieee80211_key_conf *kc;
437411db70b6SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
437511db70b6SBjoern A. Zeeb 	uint32_t hlen, hdrlen;
437611db70b6SBjoern A. Zeeb 	uint8_t *p;
437711db70b6SBjoern A. Zeeb 
437811db70b6SBjoern A. Zeeb 	KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
437911db70b6SBjoern A. Zeeb 	KASSERT(k != NULL, ("%s: key is NULL", __func__));
438011db70b6SBjoern A. Zeeb 	KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
438111db70b6SBjoern A. Zeeb 
438211db70b6SBjoern A. Zeeb 	kc = lsta->kc[k->wk_keyix];
438311db70b6SBjoern A. Zeeb 
438411db70b6SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
438511db70b6SBjoern A. Zeeb 	info->control.hw_key = kc;
438611db70b6SBjoern A. Zeeb 
438711db70b6SBjoern A. Zeeb 	/* MUST NOT happen. KASSERT? */
438811db70b6SBjoern A. Zeeb 	if (kc == NULL) {
438911db70b6SBjoern A. Zeeb 		ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
439011db70b6SBjoern A. Zeeb 		    "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
439111db70b6SBjoern A. Zeeb 		return (ENXIO);
439211db70b6SBjoern A. Zeeb 	}
439311db70b6SBjoern A. Zeeb 
439411db70b6SBjoern A. Zeeb 
439511db70b6SBjoern A. Zeeb 	IMPROVE("the following should be WLAN_CIPHER_SUITE specific");
439611db70b6SBjoern A. Zeeb 	/* We currently only support CCMP so we hardcode things here. */
439711db70b6SBjoern A. Zeeb 
439811db70b6SBjoern A. Zeeb 	hdr = (void *)skb->data;
439911db70b6SBjoern A. Zeeb 
440011db70b6SBjoern A. Zeeb 	/*
440111db70b6SBjoern A. Zeeb 	 * Check if we have anythig to do as requested by driver
440211db70b6SBjoern A. Zeeb 	 * or if we are done?
440311db70b6SBjoern A. Zeeb 	 */
440411db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
440511db70b6SBjoern A. Zeeb 	    (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
440611db70b6SBjoern A. Zeeb 	    /* MFP */
440711db70b6SBjoern A. Zeeb 	    !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
440811db70b6SBjoern A. Zeeb 		ieee80211_is_mgmt(hdr->frame_control)))
440911db70b6SBjoern A. Zeeb 			return (0);
441011db70b6SBjoern A. Zeeb 
441111db70b6SBjoern A. Zeeb 	hlen = k->wk_cipher->ic_header;
441211db70b6SBjoern A. Zeeb 	if (skb_headroom(skb) < hlen)
441311db70b6SBjoern A. Zeeb 		return (ENOSPC);
441411db70b6SBjoern A. Zeeb 
441511db70b6SBjoern A. Zeeb 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
441611db70b6SBjoern A. Zeeb 	p = skb_push(skb, hlen);
441711db70b6SBjoern A. Zeeb 	memmove(p, p + hlen, hdrlen);
441811db70b6SBjoern A. Zeeb 
441911db70b6SBjoern A. Zeeb 	/* If driver request space only we are done. */
442011db70b6SBjoern A. Zeeb 	if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
442111db70b6SBjoern A. Zeeb 		return (0);
442211db70b6SBjoern A. Zeeb 
442311db70b6SBjoern A. Zeeb 	p += hdrlen;
442411db70b6SBjoern A. Zeeb 	k->wk_cipher->ic_setiv(k, p);
442511db70b6SBjoern A. Zeeb 
442611db70b6SBjoern A. Zeeb 	return (0);
442711db70b6SBjoern A. Zeeb }
442811db70b6SBjoern A. Zeeb #endif
442911db70b6SBjoern A. Zeeb 
44306b4cac81SBjoern A. Zeeb static void
44316b4cac81SBjoern A. Zeeb lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
44326b4cac81SBjoern A. Zeeb {
44336b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
44346b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *wh;
44356b4cac81SBjoern A. Zeeb 	struct ieee80211_key *k;
44366b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
44376b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
44386b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
44396b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
44406b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
44416b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
44426b4cac81SBjoern A. Zeeb 	struct ieee80211_channel *c;
44436b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_control control;
44446b4cac81SBjoern A. Zeeb 	struct ieee80211_tx_info *info;
44456b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4446e3a0b120SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
4447ac867c20SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
44486b4cac81SBjoern A. Zeeb 	void *buf;
444911db70b6SBjoern A. Zeeb 	ieee80211_keyix keyix;
4450e3a0b120SBjoern A. Zeeb 	uint8_t ac, tid;
44516b4cac81SBjoern A. Zeeb 
44526b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
44536b4cac81SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
44549d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
44556b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
44566b4cac81SBjoern A. Zeeb #endif
44576b4cac81SBjoern A. Zeeb 
44586b4cac81SBjoern A. Zeeb 	ni = lsta->ni;
4459b35f6cd0SBjoern A. Zeeb 	k = NULL;
446011db70b6SBjoern A. Zeeb 	keyix = IEEE80211_KEYIX_NONE;
44616b4cac81SBjoern A. Zeeb 	wh = mtod(m, struct ieee80211_frame *);
44626b4cac81SBjoern A. Zeeb 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
446311db70b6SBjoern A. Zeeb 
446411db70b6SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
446511db70b6SBjoern A. Zeeb 		if (lkpi_hwcrypto) {
446611db70b6SBjoern A. Zeeb 			k = ieee80211_crypto_get_txkey(ni, m);
446711db70b6SBjoern A. Zeeb 			if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
446811db70b6SBjoern A. Zeeb 				keyix = k->wk_keyix;
446911db70b6SBjoern A. Zeeb 		}
447011db70b6SBjoern A. Zeeb #endif
447111db70b6SBjoern A. Zeeb 
447211db70b6SBjoern A. Zeeb 		/* Encrypt the frame if need be. */
447311db70b6SBjoern A. Zeeb 		if (keyix == IEEE80211_KEYIX_NONE) {
44746b4cac81SBjoern A. Zeeb 			/* Retrieve key for TX && do software encryption. */
44756b4cac81SBjoern A. Zeeb 			k = ieee80211_crypto_encap(ni, m);
44766b4cac81SBjoern A. Zeeb 			if (k == NULL) {
44776b4cac81SBjoern A. Zeeb 				ieee80211_free_node(ni);
44786b4cac81SBjoern A. Zeeb 				m_freem(m);
44796b4cac81SBjoern A. Zeeb 				return;
44806b4cac81SBjoern A. Zeeb 			}
44816b4cac81SBjoern A. Zeeb 		}
448211db70b6SBjoern A. Zeeb 	}
44836b4cac81SBjoern A. Zeeb 
44846b4cac81SBjoern A. Zeeb 	ic = ni->ni_ic;
44856b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
44866b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
44876b4cac81SBjoern A. Zeeb 	c = ni->ni_chan;
44886b4cac81SBjoern A. Zeeb 
44896b4cac81SBjoern A. Zeeb 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
44906b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_tx_hdr *rtap;
44916b4cac81SBjoern A. Zeeb 
44926b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_tx;
44936b4cac81SBjoern A. Zeeb 		rtap->wt_flags = 0;
44946b4cac81SBjoern A. Zeeb 		if (k != NULL)
44956b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
44966b4cac81SBjoern A. Zeeb 		if (m->m_flags & M_FRAG)
44976b4cac81SBjoern A. Zeeb 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
44986b4cac81SBjoern A. Zeeb 		IMPROVE();
44996b4cac81SBjoern A. Zeeb 		rtap->wt_rate = 0;
45006b4cac81SBjoern A. Zeeb 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
45016b4cac81SBjoern A. Zeeb 			rtap->wt_chan_freq = htole16(c->ic_freq);
45026b4cac81SBjoern A. Zeeb 			rtap->wt_chan_flags = htole16(c->ic_flags);
45036b4cac81SBjoern A. Zeeb 		}
45046b4cac81SBjoern A. Zeeb 
45056b4cac81SBjoern A. Zeeb 		ieee80211_radiotap_tx(ni->ni_vap, m);
45066b4cac81SBjoern A. Zeeb 	}
45076b4cac81SBjoern A. Zeeb 
45086b4cac81SBjoern A. Zeeb 	/*
45096b4cac81SBjoern A. Zeeb 	 * net80211 should handle hw->extra_tx_headroom.
45106b4cac81SBjoern A. Zeeb 	 * Though for as long as we are copying we don't mind.
45113d09d310SBjoern A. Zeeb 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
45123d09d310SBjoern A. Zeeb 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
45136b4cac81SBjoern A. Zeeb 	 */
45146b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
45156b4cac81SBjoern A. Zeeb 	if (skb == NULL) {
4516bcf1d8eeSBjoern A. Zeeb 		static uint8_t skb_alloc_failures = 0;
4517bcf1d8eeSBjoern A. Zeeb 
4518bcf1d8eeSBjoern A. Zeeb 		if (skb_alloc_failures++ == 0) {
4519bcf1d8eeSBjoern A. Zeeb 			int tid;
4520bcf1d8eeSBjoern A. Zeeb 
4521bcf1d8eeSBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
4522bcf1d8eeSBjoern A. Zeeb 			ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
4523bcf1d8eeSBjoern A. Zeeb 			    __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
4524bcf1d8eeSBjoern A. Zeeb 			for (tid = 0; tid < nitems(sta->txq); tid++) {
4525bcf1d8eeSBjoern A. Zeeb 				if (sta->txq[tid] == NULL)
4526bcf1d8eeSBjoern A. Zeeb 					continue;
4527bcf1d8eeSBjoern A. Zeeb 				ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
4528bcf1d8eeSBjoern A. Zeeb 				ic_printf(ic, "  tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
4529bcf1d8eeSBjoern A. Zeeb 				    tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(&ltxq->skbq));
4530bcf1d8eeSBjoern A. Zeeb 			}
4531bcf1d8eeSBjoern A. Zeeb 		}
45326b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
45336b4cac81SBjoern A. Zeeb 		m_freem(m);
45346b4cac81SBjoern A. Zeeb 		return;
45356b4cac81SBjoern A. Zeeb 	}
45366b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
45376b4cac81SBjoern A. Zeeb 
45386b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need a SKB version understanding mbuf. */
45396b4cac81SBjoern A. Zeeb 	/* Save the mbuf for ieee80211_tx_complete(). */
45406b4cac81SBjoern A. Zeeb 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
45416b4cac81SBjoern A. Zeeb 	skb->m = m;
45426b4cac81SBjoern A. Zeeb #if 0
45436b4cac81SBjoern A. Zeeb 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
45446b4cac81SBjoern A. Zeeb #else
45456b4cac81SBjoern A. Zeeb 	buf = skb_put(skb, m->m_pkthdr.len);
45466b4cac81SBjoern A. Zeeb 	m_copydata(m, 0, m->m_pkthdr.len, buf);
45476b4cac81SBjoern A. Zeeb #endif
45486b4cac81SBjoern A. Zeeb 	/* Save the ni. */
45496b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = ni;
45506b4cac81SBjoern A. Zeeb 
45516b4cac81SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(ni->ni_vap);
45526b4cac81SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
45536b4cac81SBjoern A. Zeeb 
4554e3a0b120SBjoern A. Zeeb 	hdr = (void *)skb->data;
4555e3a0b120SBjoern A. Zeeb 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
4556e3a0b120SBjoern A. Zeeb 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
45571665ef97SBjoern A. Zeeb 		if (!ieee80211_is_data(hdr->frame_control)) {
45581665ef97SBjoern A. Zeeb 			/* MGMT and CTRL frames go on TID 7/VO. */
45591665ef97SBjoern A. Zeeb 			skb->priority = 7;
45601665ef97SBjoern A. Zeeb 			ac = IEEE80211_AC_VO;
45611665ef97SBjoern A. Zeeb 		} else {
45621665ef97SBjoern A. Zeeb 			/* Other non-QOS traffic goes to BE. */
45631665ef97SBjoern A. Zeeb 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
4564e3a0b120SBjoern A. Zeeb 			skb->priority = 0;
4565e3a0b120SBjoern A. Zeeb 			ac = IEEE80211_AC_BE;
45661665ef97SBjoern A. Zeeb 		}
4567e3a0b120SBjoern A. Zeeb 	} else {
4568e3a0b120SBjoern A. Zeeb 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
4569fb3c249eSBjoern A. Zeeb 		ac = ieee80211e_up_to_ac[tid & 7];
4570e3a0b120SBjoern A. Zeeb 	}
45716b4cac81SBjoern A. Zeeb 	skb_set_queue_mapping(skb, ac);
45726b4cac81SBjoern A. Zeeb 
45736b4cac81SBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
45746b4cac81SBjoern A. Zeeb 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
45756b4cac81SBjoern A. Zeeb 	/* Slight delay; probably only happens on scanning so fine? */
45766b4cac81SBjoern A. Zeeb 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
45776b4cac81SBjoern A. Zeeb 		c = ic->ic_curchan;
45786b4cac81SBjoern A. Zeeb 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
4579e3a0b120SBjoern A. Zeeb 	info->hw_queue = vif->hw_queue[ac];
45806b4cac81SBjoern A. Zeeb 	if (m->m_flags & M_EAPOL)
45816b4cac81SBjoern A. Zeeb 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
45826b4cac81SBjoern A. Zeeb 	info->control.vif = vif;
45836b4cac81SBjoern A. Zeeb 	/* XXX-BZ info->control.rates */
45849fb91463SBjoern A. Zeeb #ifdef __notyet__
45859fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
45869fb91463SBjoern A. Zeeb 	info->control.rts_cts_rate_idx=
45879fb91463SBjoern A. Zeeb 	info->control.use_rts= /* RTS */
45889fb91463SBjoern A. Zeeb 	info->control.use_cts_prot= /* RTS/CTS*/
45899fb91463SBjoern A. Zeeb #endif
45909fb91463SBjoern A. Zeeb #endif
45916b4cac81SBjoern A. Zeeb 
45926b4cac81SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
4593b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
459411db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
459511db70b6SBjoern A. Zeeb 		int error;
459611db70b6SBjoern A. Zeeb 
459711db70b6SBjoern A. Zeeb 		error = lkpi_hw_crypto_prepare(lsta, k, skb);
459811db70b6SBjoern A. Zeeb 		if (error != 0) {
459911db70b6SBjoern A. Zeeb 			/*
460011db70b6SBjoern A. Zeeb 			 * We only have to free the skb which will free the
460111db70b6SBjoern A. Zeeb 			 * mbuf and release the reference on the ni.
460211db70b6SBjoern A. Zeeb 			 */
460311db70b6SBjoern A. Zeeb 			dev_kfree_skb(skb);
460411db70b6SBjoern A. Zeeb 			return;
460511db70b6SBjoern A. Zeeb 		}
460611db70b6SBjoern A. Zeeb 	}
46076b4cac81SBjoern A. Zeeb #endif
46086b4cac81SBjoern A. Zeeb 
46096b4cac81SBjoern A. Zeeb 	IMPROVE();
46106b4cac81SBjoern A. Zeeb 
4611e3a0b120SBjoern A. Zeeb 	ltxq = NULL;
4612e3a0b120SBjoern A. Zeeb 	if (!ieee80211_is_data_present(hdr->frame_control)) {
4613e3a0b120SBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_STATION &&
4614e3a0b120SBjoern A. Zeeb 		    lsta->added_to_drv &&
4615e3a0b120SBjoern A. Zeeb 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
4616d0d29110SBjoern A. Zeeb 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
4617e3a0b120SBjoern A. Zeeb 	} else if (lsta->added_to_drv &&
4618e3a0b120SBjoern A. Zeeb 	    sta->txq[skb->priority] != NULL) {
4619e3a0b120SBjoern A. Zeeb 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
4620e3a0b120SBjoern A. Zeeb 	}
4621e3a0b120SBjoern A. Zeeb 	if (ltxq == NULL)
4622d0d29110SBjoern A. Zeeb 		goto ops_tx;
4623e3a0b120SBjoern A. Zeeb 
4624d0d29110SBjoern A. Zeeb 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
4625d0d29110SBjoern A. Zeeb 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
4626d0d29110SBjoern A. Zeeb 
4627eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
46286b4cac81SBjoern A. Zeeb 	skb_queue_tail(&ltxq->skbq, skb);
46299d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46309d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4631d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
4632d0d29110SBjoern A. Zeeb 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
4633d0d29110SBjoern A. Zeeb 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
4634fb6eaf74SBjoern A. Zeeb 		    __func__, __LINE__,
4635d0d29110SBjoern A. Zeeb 		    curthread->td_tid, (unsigned int)ticks,
4636d0d29110SBjoern A. Zeeb 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
4637d0d29110SBjoern A. Zeeb 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
4638d0d29110SBjoern A. Zeeb 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
46399d9ba2b7SBjoern A. Zeeb #endif
4640eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
464145bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4642d0d29110SBjoern A. Zeeb 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
464345bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46446b4cac81SBjoern A. Zeeb 	return;
46456b4cac81SBjoern A. Zeeb 
46466b4cac81SBjoern A. Zeeb ops_tx:
46479d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46489d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
4649d0d29110SBjoern A. Zeeb 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
4650d0d29110SBjoern A. Zeeb 		    "TX ac %d prio %u qmap %u\n",
46516b4cac81SBjoern A. Zeeb 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
46526b4cac81SBjoern A. Zeeb 		    skb, ac, skb->priority, skb->qmap);
46539d9ba2b7SBjoern A. Zeeb #endif
46546b4cac81SBjoern A. Zeeb 	memset(&control, 0, sizeof(control));
46556b4cac81SBjoern A. Zeeb 	control.sta = sta;
465645bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
46576b4cac81SBjoern A. Zeeb 	lkpi_80211_mo_tx(hw, &control, skb);
465845bce6faSBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
46596b4cac81SBjoern A. Zeeb }
46606b4cac81SBjoern A. Zeeb 
46616b4cac81SBjoern A. Zeeb static void
46626b4cac81SBjoern A. Zeeb lkpi_80211_txq_task(void *ctx, int pending)
46636b4cac81SBjoern A. Zeeb {
46646b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
46656b4cac81SBjoern A. Zeeb 	struct mbufq mq;
46666b4cac81SBjoern A. Zeeb 	struct mbuf *m;
466788665349SBjoern A. Zeeb 	bool shall_tx;
46686b4cac81SBjoern A. Zeeb 
46696b4cac81SBjoern A. Zeeb 	lsta = ctx;
46706b4cac81SBjoern A. Zeeb 
46719d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
46729d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
46736b4cac81SBjoern A. Zeeb 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
46749d9ba2b7SBjoern A. Zeeb 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
46756b4cac81SBjoern A. Zeeb 		    pending, mbufq_len(&lsta->txq));
46769d9ba2b7SBjoern A. Zeeb #endif
46776b4cac81SBjoern A. Zeeb 
46786b4cac81SBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
46796b4cac81SBjoern A. Zeeb 
4680fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
4681fa4e4257SBjoern A. Zeeb 	/*
4682fa4e4257SBjoern A. Zeeb 	 * Do not re-check lsta->txq_ready here; we may have a pending
468388665349SBjoern A. Zeeb 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
468488665349SBjoern A. Zeeb 	 * false we do not have a valid sta anymore in the firmware so no
468588665349SBjoern A. Zeeb 	 * point to try to TX.
468688665349SBjoern A. Zeeb 	 * We also use txq_ready as a semaphore and will drain the txq manually
468788665349SBjoern A. Zeeb 	 * if needed on our way towards SCAN/INIT in the state machine.
4688fa4e4257SBjoern A. Zeeb 	 */
46892372f8ccSBjoern A. Zeeb #if 0
469088665349SBjoern A. Zeeb 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
46912372f8ccSBjoern A. Zeeb #else
46922372f8ccSBjoern A. Zeeb 	/*
46932372f8ccSBjoern A. Zeeb 	 * Backout this part of 886653492945f which breaks rtw88 or
46942372f8ccSBjoern A. Zeeb 	 * in general drivers without (*sta_state)() but only the
46952372f8ccSBjoern A. Zeeb 	 * legacy fallback to (*sta_add)().
46962372f8ccSBjoern A. Zeeb 	 */
46972372f8ccSBjoern A. Zeeb 	shall_tx = lsta->txq_ready;
46982372f8ccSBjoern A. Zeeb #endif
469988665349SBjoern A. Zeeb 	if (__predict_true(shall_tx))
47006b4cac81SBjoern A. Zeeb 		mbufq_concat(&mq, &lsta->txq);
470188665349SBjoern A. Zeeb 	/*
470288665349SBjoern A. Zeeb 	 * else a state change will push the packets out manually or
470388665349SBjoern A. Zeeb 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
470488665349SBjoern A. Zeeb 	 */
4705fa4e4257SBjoern A. Zeeb 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
47066b4cac81SBjoern A. Zeeb 
47076b4cac81SBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
47086b4cac81SBjoern A. Zeeb 	while (m != NULL) {
47096b4cac81SBjoern A. Zeeb 		lkpi_80211_txq_tx_one(lsta, m);
47106b4cac81SBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
47116b4cac81SBjoern A. Zeeb 	}
47126b4cac81SBjoern A. Zeeb }
47136b4cac81SBjoern A. Zeeb 
47146b4cac81SBjoern A. Zeeb static int
47156b4cac81SBjoern A. Zeeb lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
47166b4cac81SBjoern A. Zeeb {
47176b4cac81SBjoern A. Zeeb 
47186b4cac81SBjoern A. Zeeb 	/* XXX TODO */
47196b4cac81SBjoern A. Zeeb 	IMPROVE();
47206b4cac81SBjoern A. Zeeb 
47216b4cac81SBjoern A. Zeeb 	/* Quick and dirty cheating hack. */
47226b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
47236b4cac81SBjoern A. Zeeb 
47246b4cac81SBjoern A. Zeeb 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
47256b4cac81SBjoern A. Zeeb 	return (lkpi_ic_raw_xmit(ni, m, NULL));
47266b4cac81SBjoern A. Zeeb }
47276b4cac81SBjoern A. Zeeb 
47289fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
47299fb91463SBjoern A. Zeeb static int
47309fb91463SBjoern A. Zeeb lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
47319fb91463SBjoern A. Zeeb     const uint8_t *frm, const uint8_t *efrm)
47329fb91463SBjoern A. Zeeb {
47339fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47349fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47359fb91463SBjoern A. Zeeb 
47369fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47379fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47389fb91463SBjoern A. Zeeb 
4739310743c4SBjoern A. Zeeb 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
47409fb91463SBjoern A. Zeeb 
47419fb91463SBjoern A. Zeeb 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
47429fb91463SBjoern A. Zeeb }
47439fb91463SBjoern A. Zeeb 
47449fb91463SBjoern A. Zeeb static int
47459fb91463SBjoern A. Zeeb lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
47469fb91463SBjoern A. Zeeb {
47479fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47489fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47499fb91463SBjoern A. Zeeb 
47509fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47519fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47529fb91463SBjoern A. Zeeb 
4753310743c4SBjoern A. Zeeb 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
47549fb91463SBjoern A. Zeeb 
47559fb91463SBjoern A. Zeeb 	return (lhw->ic_send_action(ni, category, action, sa));
47569fb91463SBjoern A. Zeeb }
47579fb91463SBjoern A. Zeeb 
47589fb91463SBjoern A. Zeeb 
47599fb91463SBjoern A. Zeeb static int
47609fb91463SBjoern A. Zeeb lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
47619fb91463SBjoern A. Zeeb {
47629fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
47639fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
47649fb91463SBjoern A. Zeeb 
47659fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
47669fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
47679fb91463SBjoern A. Zeeb 
4768310743c4SBjoern A. Zeeb 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
47699fb91463SBjoern A. Zeeb 
47709fb91463SBjoern A. Zeeb 	return (lhw->ic_ampdu_enable(ni, tap));
47719fb91463SBjoern A. Zeeb }
47729fb91463SBjoern A. Zeeb 
4773310743c4SBjoern A. Zeeb /*
4774310743c4SBjoern A. Zeeb  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4775310743c4SBjoern A. Zeeb  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4776310743c4SBjoern A. Zeeb  *
4777310743c4SBjoern A. Zeeb  * NB: returns 0 on ERROR!
4778310743c4SBjoern A. Zeeb  */
47799fb91463SBjoern A. Zeeb static int
47809fb91463SBjoern A. Zeeb lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
47819fb91463SBjoern A. Zeeb     int dialogtoken, 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 	params.sta = sta;
4810310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_START;
4811310743c4SBjoern A. Zeeb 	/* Keep 0 here! */
4812310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4813310743c4SBjoern A. Zeeb 	params.timeout = 0;
4814310743c4SBjoern A. Zeeb 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4815310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4816310743c4SBjoern A. Zeeb 	params.amsdu = false;
4817310743c4SBjoern A. Zeeb 
4818310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4819310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4820310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4821310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4822310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4823310743c4SBjoern A. Zeeb 	if (error != 0) {
4824310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4825310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4826310743c4SBjoern A. Zeeb 		return (0);
4827310743c4SBjoern A. Zeeb 	}
48289fb91463SBjoern A. Zeeb 
48299fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
48309fb91463SBjoern A. Zeeb }
48319fb91463SBjoern A. Zeeb 
4832310743c4SBjoern A. Zeeb /*
4833310743c4SBjoern A. Zeeb  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4834310743c4SBjoern A. Zeeb  * and calls the default ieee80211_addba_response() which always returns 1.
4835310743c4SBjoern A. Zeeb  *
4836310743c4SBjoern A. Zeeb  * NB: No error checking in net80211!
4837310743c4SBjoern A. Zeeb  * Staying with 0 is an error.
4838310743c4SBjoern A. Zeeb  */
48399fb91463SBjoern A. Zeeb static int
48409fb91463SBjoern A. Zeeb lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
48419fb91463SBjoern A. Zeeb     int status, int baparamset, int batimeout)
48429fb91463SBjoern A. Zeeb {
48439fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
48449fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4845310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4846310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4847310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4848310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4849310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4850310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4851310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4852310743c4SBjoern A. Zeeb 	int error;
48539fb91463SBjoern A. Zeeb 
48549fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
48559fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4856310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4857310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4858310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4859310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4860310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4861310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
48629fb91463SBjoern A. Zeeb 
4863310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4864310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4865310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4866310743c4SBjoern A. Zeeb 		return (0);
4867310743c4SBjoern A. Zeeb 	}
4868310743c4SBjoern A. Zeeb 
4869310743c4SBjoern A. Zeeb 	if (status == IEEE80211_STATUS_SUCCESS) {
4870310743c4SBjoern A. Zeeb 		params.sta = sta;
4871310743c4SBjoern A. Zeeb 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4872310743c4SBjoern A. Zeeb 		params.buf_size = tap->txa_wnd;
4873310743c4SBjoern A. Zeeb 		params.timeout = 0;
4874310743c4SBjoern A. Zeeb 		params.ssn = 0;
4875310743c4SBjoern A. Zeeb 		params.tid = tap->txa_tid;
4876310743c4SBjoern A. Zeeb 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4877310743c4SBjoern A. Zeeb 			params.amsdu = true;
4878310743c4SBjoern A. Zeeb 		else
4879310743c4SBjoern A. Zeeb 			params.amsdu = false;
4880310743c4SBjoern A. Zeeb 	} else {
4881310743c4SBjoern A. Zeeb 		/* We need to free the allocated resources. */
4882310743c4SBjoern A. Zeeb 		params.sta = sta;
4883310743c4SBjoern A. Zeeb 		switch (status) {
4884310743c4SBjoern A. Zeeb 			/* params.action = FLUSH, FLUSH_CONT */
4885310743c4SBjoern A. Zeeb 		default:
4886310743c4SBjoern A. Zeeb 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4887310743c4SBjoern A. Zeeb 			break;
4888310743c4SBjoern A. Zeeb 		}
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 
4896310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4897310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4898310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4899310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4900310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4901310743c4SBjoern A. Zeeb 	if (error != 0) {
4902310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4903310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4904310743c4SBjoern A. Zeeb 		return (0);
4905310743c4SBjoern A. Zeeb 	}
4906310743c4SBjoern A. Zeeb 
4907310743c4SBjoern A. Zeeb 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
49089fb91463SBjoern A. Zeeb 
49099fb91463SBjoern A. Zeeb 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
49109fb91463SBjoern A. Zeeb }
49119fb91463SBjoern A. Zeeb 
4912310743c4SBjoern A. Zeeb /*
4913310743c4SBjoern A. Zeeb  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4914310743c4SBjoern A. Zeeb  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4915310743c4SBjoern A. Zeeb  */
49169fb91463SBjoern A. Zeeb static void
49179fb91463SBjoern A. Zeeb lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49189fb91463SBjoern A. Zeeb {
49199fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49209fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
4921310743c4SBjoern A. Zeeb 	struct ieee80211_hw *hw;
4922310743c4SBjoern A. Zeeb 	struct ieee80211vap *vap;
4923310743c4SBjoern A. Zeeb 	struct lkpi_vif *lvif;
4924310743c4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
4925310743c4SBjoern A. Zeeb 	struct lkpi_sta *lsta;
4926310743c4SBjoern A. Zeeb 	struct ieee80211_sta *sta;
4927310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
4928310743c4SBjoern A. Zeeb 	int error;
49299fb91463SBjoern A. Zeeb 
49309fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49319fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
4932310743c4SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
4933310743c4SBjoern A. Zeeb 	vap = ni->ni_vap;
4934310743c4SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
4935310743c4SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
4936310743c4SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
4937310743c4SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
49389fb91463SBjoern A. Zeeb 
4939310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
4940310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4941310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, sta);
4942310743c4SBjoern A. Zeeb 		goto n80211;
4943310743c4SBjoern A. Zeeb 	}
49449fb91463SBjoern A. Zeeb 
4945310743c4SBjoern A. Zeeb 	/* We need to free the allocated resources. */
4946310743c4SBjoern A. Zeeb 	params.sta = sta;
4947310743c4SBjoern A. Zeeb 	IMPROVE("net80211 does not provide a reason to us");
4948310743c4SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4949310743c4SBjoern A. Zeeb 	params.buf_size = 0;
4950310743c4SBjoern A. Zeeb 	params.timeout = 0;
4951310743c4SBjoern A. Zeeb 	params.ssn = 0;
4952310743c4SBjoern A. Zeeb 	params.tid = tap->txa_tid;
4953310743c4SBjoern A. Zeeb 	params.amsdu = false;
4954310743c4SBjoern A. Zeeb 
4955310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
4956310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
4957310743c4SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4958310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
4959310743c4SBjoern A. Zeeb 	IEEE80211_LOCK(ic);
4960310743c4SBjoern A. Zeeb 	if (error != 0) {
4961310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4962310743c4SBjoern A. Zeeb 		    __func__, error, ni, tap);
4963310743c4SBjoern A. Zeeb 		goto n80211;
4964310743c4SBjoern A. Zeeb 	}
4965310743c4SBjoern A. Zeeb 
4966310743c4SBjoern A. Zeeb 	IMPROVE_HT("anyting else?");
4967310743c4SBjoern A. Zeeb 
4968310743c4SBjoern A. Zeeb n80211:
49699fb91463SBjoern A. Zeeb 	lhw->ic_addba_stop(ni, tap);
49709fb91463SBjoern A. Zeeb }
49719fb91463SBjoern A. Zeeb 
49729fb91463SBjoern A. Zeeb static void
49739fb91463SBjoern A. Zeeb lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
49749fb91463SBjoern A. Zeeb {
49759fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49769fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49779fb91463SBjoern A. Zeeb 
49789fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49799fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49809fb91463SBjoern A. Zeeb 
49819fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49829fb91463SBjoern A. Zeeb 
49839fb91463SBjoern A. Zeeb 	lhw->ic_addba_response_timeout(ni, tap);
49849fb91463SBjoern A. Zeeb }
49859fb91463SBjoern A. Zeeb 
49869fb91463SBjoern A. Zeeb static void
49879fb91463SBjoern A. Zeeb lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
49889fb91463SBjoern A. Zeeb     int status)
49899fb91463SBjoern A. Zeeb {
49909fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
49919fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
49929fb91463SBjoern A. Zeeb 
49939fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
49949fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
49959fb91463SBjoern A. Zeeb 
49969fb91463SBjoern A. Zeeb 	IMPROVE_HT();
49979fb91463SBjoern A. Zeeb 
49989fb91463SBjoern A. Zeeb 	lhw->ic_bar_response(ni, tap, status);
49999fb91463SBjoern A. Zeeb }
50009fb91463SBjoern A. Zeeb 
50019fb91463SBjoern A. Zeeb static int
50029fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
50039fb91463SBjoern A. Zeeb     int baparamset, int batimeout, int baseqctl)
50049fb91463SBjoern A. Zeeb {
50059fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50069fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50079fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50089fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50099fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50109fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50119fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50129fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5013310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50149fb91463SBjoern A. Zeeb 	int error;
50159fb91463SBjoern A. Zeeb 
50169fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50179fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50189fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
50199fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
50209fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
50219fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
50229fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
50239fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
50249fb91463SBjoern A. Zeeb 
5025310743c4SBjoern A. Zeeb 	IEEE80211_UNLOCK_ASSERT(ic);
5026310743c4SBjoern A. Zeeb 
5027310743c4SBjoern A. Zeeb 	if (!lsta->added_to_drv) {
5028310743c4SBjoern A. Zeeb 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
5029310743c4SBjoern A. Zeeb 		    __func__, lsta, ni, vap, sta);
5030310743c4SBjoern A. Zeeb 		return (-ENXIO);
5031310743c4SBjoern A. Zeeb 	}
5032310743c4SBjoern A. Zeeb 
50339fb91463SBjoern A. Zeeb 	params.sta = sta;
50349fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_START;
50359fb91463SBjoern A. Zeeb 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
50369fb91463SBjoern A. Zeeb 	if (params.buf_size == 0)
50379fb91463SBjoern A. Zeeb 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
50389fb91463SBjoern A. Zeeb 	else
50399fb91463SBjoern A. Zeeb 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
5040310743c4SBjoern A. Zeeb 	if (hw->max_rx_aggregation_subframes > 0 &&
5041310743c4SBjoern A. Zeeb 	    params.buf_size > hw->max_rx_aggregation_subframes)
50429fb91463SBjoern A. Zeeb 		params.buf_size = hw->max_rx_aggregation_subframes;
50439fb91463SBjoern A. Zeeb 	params.timeout = le16toh(batimeout);
50449fb91463SBjoern A. Zeeb 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
50459fb91463SBjoern A. Zeeb 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
5046310743c4SBjoern A. Zeeb 
5047310743c4SBjoern A. Zeeb 	/* Based on net80211::ampdu_rx_start(). */
5048310743c4SBjoern A. Zeeb 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
5049310743c4SBjoern A. Zeeb 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
5050310743c4SBjoern A. Zeeb 		params.amsdu = true;
5051310743c4SBjoern A. Zeeb 	else
50529fb91463SBjoern A. Zeeb 		params.amsdu = false;
50539fb91463SBjoern A. Zeeb 
5054310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
50559fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5056310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
50579fb91463SBjoern A. Zeeb 	if (error != 0) {
50589fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
50599fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
50609fb91463SBjoern A. Zeeb 		return (error);
50619fb91463SBjoern A. Zeeb 	}
5062310743c4SBjoern A. Zeeb 
5063310743c4SBjoern A. Zeeb 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
5064310743c4SBjoern A. Zeeb 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
5065310743c4SBjoern A. Zeeb 	}
5066310743c4SBjoern A. Zeeb 
50679fb91463SBjoern A. Zeeb 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
50689fb91463SBjoern A. Zeeb 
50699fb91463SBjoern A. Zeeb 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
50709fb91463SBjoern A. Zeeb 	return (error);
50719fb91463SBjoern A. Zeeb }
50729fb91463SBjoern A. Zeeb 
50739fb91463SBjoern A. Zeeb static void
50749fb91463SBjoern A. Zeeb lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
50759fb91463SBjoern A. Zeeb {
50769fb91463SBjoern A. Zeeb 	struct ieee80211com *ic;
50779fb91463SBjoern A. Zeeb 	struct lkpi_hw *lhw;
50789fb91463SBjoern A. Zeeb 	struct ieee80211_hw *hw;
50799fb91463SBjoern A. Zeeb 	struct ieee80211vap *vap;
50809fb91463SBjoern A. Zeeb 	struct lkpi_vif *lvif;
50819fb91463SBjoern A. Zeeb 	struct ieee80211_vif *vif;
50829fb91463SBjoern A. Zeeb 	struct lkpi_sta *lsta;
50839fb91463SBjoern A. Zeeb 	struct ieee80211_sta *sta;
5084310743c4SBjoern A. Zeeb 	struct ieee80211_ampdu_params params = { };
50859fb91463SBjoern A. Zeeb 	int error;
50869fb91463SBjoern A. Zeeb 	uint8_t tid;
508765c573e4SBjoern A. Zeeb 	bool ic_locked;
50889fb91463SBjoern A. Zeeb 
50899fb91463SBjoern A. Zeeb 	ic = ni->ni_ic;
50909fb91463SBjoern A. Zeeb 	lhw = ic->ic_softc;
50919fb91463SBjoern A. Zeeb 
50929fb91463SBjoern A. Zeeb 	/*
50939fb91463SBjoern A. Zeeb 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
50949fb91463SBjoern A. Zeeb 	 * we did not START.  Some drivers pass it down to firmware which will
50959fb91463SBjoern A. Zeeb 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
50969fb91463SBjoern A. Zeeb 	 * ieee80211_ht_node_init() amongst others which will iterate over all
50979fb91463SBjoern A. Zeeb 	 * tid and call ic_ampdu_rx_stop() unconditionally.
50989fb91463SBjoern A. Zeeb 	 * XXX net80211 should probably be more "gentle" in these cases and
50999fb91463SBjoern A. Zeeb 	 * track some state itself.
51009fb91463SBjoern A. Zeeb 	 */
51019fb91463SBjoern A. Zeeb 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
51029fb91463SBjoern A. Zeeb 		goto net80211_only;
51039fb91463SBjoern A. Zeeb 
51049fb91463SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
51059fb91463SBjoern A. Zeeb 	vap = ni->ni_vap;
51069fb91463SBjoern A. Zeeb 	lvif = VAP_TO_LVIF(vap);
51079fb91463SBjoern A. Zeeb 	vif = LVIF_TO_VIF(lvif);
51089fb91463SBjoern A. Zeeb 	lsta = ni->ni_drv_data;
51099fb91463SBjoern A. Zeeb 	sta = LSTA_TO_STA(lsta);
51109fb91463SBjoern A. Zeeb 
51119fb91463SBjoern A. Zeeb 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
51129fb91463SBjoern A. Zeeb 	for (tid = 0; tid < WME_NUM_TID; tid++) {
51139fb91463SBjoern A. Zeeb 		if (&ni->ni_rx_ampdu[tid] == rap)
51149fb91463SBjoern A. Zeeb 			break;
51159fb91463SBjoern A. Zeeb 	}
51169fb91463SBjoern A. Zeeb 
51179fb91463SBjoern A. Zeeb 	params.sta = sta;
51189fb91463SBjoern A. Zeeb 	params.action = IEEE80211_AMPDU_RX_STOP;
51199fb91463SBjoern A. Zeeb 	params.buf_size = 0;
51209fb91463SBjoern A. Zeeb 	params.timeout = 0;
51219fb91463SBjoern A. Zeeb 	params.ssn = 0;
51229fb91463SBjoern A. Zeeb 	params.tid = tid;
51239fb91463SBjoern A. Zeeb 	params.amsdu = false;
51249fb91463SBjoern A. Zeeb 
512565c573e4SBjoern A. Zeeb 	ic_locked = IEEE80211_IS_LOCKED(ic);
512665c573e4SBjoern A. Zeeb 	if (ic_locked)
512765c573e4SBjoern A. Zeeb 		IEEE80211_UNLOCK(ic);
5128310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK(lhw);
51299fb91463SBjoern A. Zeeb 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
5130310743c4SBjoern A. Zeeb 	LKPI_80211_LHW_UNLOCK(lhw);
513165c573e4SBjoern A. Zeeb 	if (ic_locked)
513265c573e4SBjoern A. Zeeb 		IEEE80211_LOCK(ic);
51339fb91463SBjoern A. Zeeb 	if (error != 0)
51349fb91463SBjoern A. Zeeb 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
51359fb91463SBjoern A. Zeeb 		    __func__, error, ni, rap);
51369fb91463SBjoern A. Zeeb 
51379fb91463SBjoern A. Zeeb net80211_only:
51389fb91463SBjoern A. Zeeb 	lhw->ic_ampdu_rx_stop(ni, rap);
51399fb91463SBjoern A. Zeeb }
51409fb91463SBjoern A. Zeeb #endif
51419fb91463SBjoern A. Zeeb 
51429fb91463SBjoern A. Zeeb static void
51439fb91463SBjoern A. Zeeb lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
51449fb91463SBjoern A. Zeeb     uint8_t *bands, int *chan_flags, enum nl80211_band band)
51459fb91463SBjoern A. Zeeb {
51469fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
51479fb91463SBjoern A. Zeeb 	struct ieee80211_sta_ht_cap *ht_cap;
51489fb91463SBjoern A. Zeeb 
51499fb91463SBjoern A. Zeeb 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
51509fb91463SBjoern A. Zeeb 	if (!ht_cap->ht_supported)
51519fb91463SBjoern A. Zeeb 		return;
51529fb91463SBjoern A. Zeeb 
51539fb91463SBjoern A. Zeeb 	switch (band) {
51549fb91463SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
51559fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NG);
51569fb91463SBjoern A. Zeeb 		break;
51579fb91463SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
51589fb91463SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11NA);
51599fb91463SBjoern A. Zeeb 		break;
51609fb91463SBjoern A. Zeeb 	default:
51619fb91463SBjoern A. Zeeb 		IMPROVE("Unsupported band %d", band);
51629fb91463SBjoern A. Zeeb 		return;
51639fb91463SBjoern A. Zeeb 	}
51649fb91463SBjoern A. Zeeb 
51659fb91463SBjoern A. Zeeb 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
51669fb91463SBjoern A. Zeeb 
51679fb91463SBjoern A. Zeeb 	/*
51689fb91463SBjoern A. Zeeb 	 * Rather than manually checking each flag and
51699fb91463SBjoern A. Zeeb 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
51709fb91463SBjoern A. Zeeb 	 * simply copy the 16bits.
51719fb91463SBjoern A. Zeeb 	 */
51729fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= ht_cap->cap;
51739fb91463SBjoern A. Zeeb 
51749fb91463SBjoern A. Zeeb 	/* Then deal with the other flags. */
51759fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
51769fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
51779fb91463SBjoern A. Zeeb #ifdef __notyet__
51789fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, TX_AMSDU))
51799fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
51809fb91463SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
51819fb91463SBjoern A. Zeeb 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
51829fb91463SBjoern A. Zeeb 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
51839fb91463SBjoern A. Zeeb #endif
51849fb91463SBjoern A. Zeeb 
51859fb91463SBjoern A. Zeeb 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
51869fb91463SBjoern A. Zeeb 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
51879fb91463SBjoern A. Zeeb 
51889fb91463SBjoern A. Zeeb 	/* Only add HT40 channels if supported. */
51899fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
51909fb91463SBjoern A. Zeeb 	    chan_flags != NULL)
51919fb91463SBjoern A. Zeeb 		*chan_flags |= NET80211_CBW_FLAG_HT40;
51929fb91463SBjoern A. Zeeb #endif
51939fb91463SBjoern A. Zeeb }
51949fb91463SBjoern A. Zeeb 
51956b4cac81SBjoern A. Zeeb static void
51966b4cac81SBjoern A. Zeeb lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
51976b4cac81SBjoern A. Zeeb     int *n, struct ieee80211_channel *c)
51986b4cac81SBjoern A. Zeeb {
51996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
52006b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
52016b4cac81SBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *channels;
52026b4cac81SBjoern A. Zeeb 	uint8_t bands[IEEE80211_MODE_BYTES];
52036b4cac81SBjoern A. Zeeb 	int chan_flags, error, i, nchans;
52046b4cac81SBjoern A. Zeeb 
52056b4cac81SBjoern A. Zeeb 	/* Channels */
52066b4cac81SBjoern A. Zeeb 	lhw = ic->ic_softc;
52076b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
52086b4cac81SBjoern A. Zeeb 
52096b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_2GHZ */
52106b4cac81SBjoern A. Zeeb 	nchans = 0;
52116b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
52126b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
52136b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52146b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52156b4cac81SBjoern A. Zeeb 		chan_flags = 0;
52166b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11B);
52176b4cac81SBjoern A. Zeeb 		/* XXX-BZ unclear how to check for 11g. */
52189fb91463SBjoern A. Zeeb 
52199fb91463SBjoern A. Zeeb 		IMPROVE("the bitrates may have flags?");
52206b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11G);
52219fb91463SBjoern A. Zeeb 
52229fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
52239fb91463SBjoern A. Zeeb 		    NL80211_BAND_2GHZ);
52246b4cac81SBjoern A. Zeeb 
52256b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
5226cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52276b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
52286b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
52296b4cac81SBjoern A. Zeeb 
52306b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
52313f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
52323f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
52336b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
52346b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
52356b4cac81SBjoern A. Zeeb 				continue;
52366b4cac81SBjoern A. Zeeb 			}
52376b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
52386b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
52396b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
52406b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
52416b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
52426b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
52436b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
52446b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
52456b4cac81SBjoern A. Zeeb 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
52466b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
52476b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
52486b4cac81SBjoern A. Zeeb 
52496b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
52506b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
52516b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
52525856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5253cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5254cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
52553f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
52563f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
52576b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
52586b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
52596b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5260cee56e77SBjoern A. Zeeb 			if (error != 0)
52616b4cac81SBjoern A. Zeeb 				break;
52626b4cac81SBjoern A. Zeeb 		}
52636b4cac81SBjoern A. Zeeb 	}
52646b4cac81SBjoern A. Zeeb 
52656b4cac81SBjoern A. Zeeb 	/* NL80211_BAND_5GHZ */
52666b4cac81SBjoern A. Zeeb 	nchans = 0;
52676b4cac81SBjoern A. Zeeb 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
52686b4cac81SBjoern A. Zeeb 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
52696b4cac81SBjoern A. Zeeb 	if (nchans > 0) {
52706b4cac81SBjoern A. Zeeb 		memset(bands, 0, sizeof(bands));
52716b4cac81SBjoern A. Zeeb 		chan_flags = 0;
52726b4cac81SBjoern A. Zeeb 		setbit(bands, IEEE80211_MODE_11A);
52739fb91463SBjoern A. Zeeb 
52749fb91463SBjoern A. Zeeb 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
52759fb91463SBjoern A. Zeeb 		    NL80211_BAND_5GHZ);
52769fb91463SBjoern A. Zeeb 
52779fb91463SBjoern A. Zeeb #ifdef LKPI_80211_VHT
52786b4cac81SBjoern A. Zeeb 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
52796b4cac81SBjoern A. Zeeb 
52806b4cac81SBjoern A. Zeeb 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
5281562adbe1SBjoern A. Zeeb 			ic->ic_vht_cap.vht_cap_info =
52826b4cac81SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
5283ecb2e5f9SBjoern A. Zeeb 			ic->ic_vht_cap.supp_mcs =
5284ecb2e5f9SBjoern A. Zeeb 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
52856b4cac81SBjoern A. Zeeb 
52866b4cac81SBjoern A. Zeeb 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
52876b4cac81SBjoern A. Zeeb 			chan_flags |= NET80211_CBW_FLAG_VHT80;
52886b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
5289562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52906b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT160;
52916b4cac81SBjoern A. Zeeb 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
5292562adbe1SBjoern A. Zeeb 			    ic->ic_vht_cap.vht_cap_info))
52936b4cac81SBjoern A. Zeeb 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
52946b4cac81SBjoern A. Zeeb 		}
52956b4cac81SBjoern A. Zeeb #endif
52966b4cac81SBjoern A. Zeeb 
52976b4cac81SBjoern A. Zeeb 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
5298cee56e77SBjoern A. Zeeb 		for (i = 0; i < nchans && *n < maxchan; i++) {
52996b4cac81SBjoern A. Zeeb 			uint32_t nflags = 0;
53006b4cac81SBjoern A. Zeeb 			int cflags = chan_flags;
53016b4cac81SBjoern A. Zeeb 
53026b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
53033f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Skipping disabled chan "
53043f0083c4SBjoern A. Zeeb 				    "[%u/%u/%#x]\n", __func__,
53056b4cac81SBjoern A. Zeeb 				    channels[i].hw_value,
53066b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags);
53076b4cac81SBjoern A. Zeeb 				continue;
53086b4cac81SBjoern A. Zeeb 			}
53096b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
53106b4cac81SBjoern A. Zeeb 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
53116b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
53126b4cac81SBjoern A. Zeeb 				nflags |= IEEE80211_CHAN_DFS;
53136b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
53146b4cac81SBjoern A. Zeeb 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
53156b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
53166b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_VHT80;
53176b4cac81SBjoern A. Zeeb 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
53186b4cac81SBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
53196b4cac81SBjoern A. Zeeb 				cflags &= ~NET80211_CBW_FLAG_HT40;
53206b4cac81SBjoern A. Zeeb 
53216b4cac81SBjoern A. Zeeb 			error = ieee80211_add_channel_cbw(c, maxchan, n,
53226b4cac81SBjoern A. Zeeb 			    channels[i].hw_value, channels[i].center_freq,
53236b4cac81SBjoern A. Zeeb 			    channels[i].max_power,
53245856761fSBjoern A. Zeeb 			    nflags, bands, cflags);
5325cee56e77SBjoern A. Zeeb 			/* net80211::ENOBUFS: *n >= maxchans */
5326cee56e77SBjoern A. Zeeb 			if (error != 0 && error != ENOBUFS)
53273f0083c4SBjoern A. Zeeb 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
53283f0083c4SBjoern A. Zeeb 				    "returned error %d\n",
53296b4cac81SBjoern A. Zeeb 				    __func__, channels[i].hw_value,
53306b4cac81SBjoern A. Zeeb 				    channels[i].center_freq, channels[i].flags,
53316b4cac81SBjoern A. Zeeb 				    nflags, chan_flags, cflags, error);
5332cee56e77SBjoern A. Zeeb 			if (error != 0)
53336b4cac81SBjoern A. Zeeb 				break;
53346b4cac81SBjoern A. Zeeb 		}
53356b4cac81SBjoern A. Zeeb 	}
53366b4cac81SBjoern A. Zeeb }
53376b4cac81SBjoern A. Zeeb 
53386b4cac81SBjoern A. Zeeb static void *
53396b4cac81SBjoern A. Zeeb lkpi_ieee80211_ifalloc(void)
53406b4cac81SBjoern A. Zeeb {
53416b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
53426b4cac81SBjoern A. Zeeb 
53436b4cac81SBjoern A. Zeeb 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
53446b4cac81SBjoern A. Zeeb 
53456b4cac81SBjoern A. Zeeb 	/* Setting these happens later when we have device information. */
53466b4cac81SBjoern A. Zeeb 	ic->ic_softc = NULL;
53476b4cac81SBjoern A. Zeeb 	ic->ic_name = "linuxkpi";
53486b4cac81SBjoern A. Zeeb 
53496b4cac81SBjoern A. Zeeb 	return (ic);
53506b4cac81SBjoern A. Zeeb }
53516b4cac81SBjoern A. Zeeb 
53526b4cac81SBjoern A. Zeeb struct ieee80211_hw *
53536b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
53546b4cac81SBjoern A. Zeeb {
53556b4cac81SBjoern A. Zeeb 	struct ieee80211_hw *hw;
53566b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
53576b4cac81SBjoern A. Zeeb 	struct wiphy *wiphy;
5358a5ae63edSBjoern A. Zeeb 	int ac;
53596b4cac81SBjoern A. Zeeb 
53606b4cac81SBjoern A. Zeeb 	/* Get us and the driver data also allocated. */
53616b4cac81SBjoern A. Zeeb 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
53626b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
53636b4cac81SBjoern A. Zeeb 		return (NULL);
53646b4cac81SBjoern A. Zeeb 
53656b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
53666b4cac81SBjoern A. Zeeb 	lhw->ops = ops;
5367652e22d3SBjoern A. Zeeb 
53688ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_INIT(lhw);
53698ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
5370eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
53718891c455SBjoern A. Zeeb 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
53726b4cac81SBjoern A. Zeeb 	TAILQ_INIT(&lhw->lvif_head);
5373a5ae63edSBjoern A. Zeeb 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5374a5ae63edSBjoern A. Zeeb 		lhw->txq_generation[ac] = 1;
5375a5ae63edSBjoern A. Zeeb 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
5376a5ae63edSBjoern A. Zeeb 	}
53776b4cac81SBjoern A. Zeeb 
5378759a996dSBjoern A. Zeeb 	/* Deferred RX path. */
5379759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
5380759a996dSBjoern A. Zeeb 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
5381759a996dSBjoern A. Zeeb 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
5382759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = false;
5383759a996dSBjoern A. Zeeb 
53846b4cac81SBjoern A. Zeeb 	/*
53856b4cac81SBjoern A. Zeeb 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
53866b4cac81SBjoern A. Zeeb 	 * not initialized.
53876b4cac81SBjoern A. Zeeb 	 */
53886b4cac81SBjoern A. Zeeb 	hw = LHW_TO_HW(lhw);
53896b4cac81SBjoern A. Zeeb 	hw->wiphy = wiphy;
5390086be6a8SBjoern A. Zeeb 	hw->conf.flags |= IEEE80211_CONF_IDLE;
53916b4cac81SBjoern A. Zeeb 	hw->priv = (void *)(lhw + 1);
53926b4cac81SBjoern A. Zeeb 
53936b4cac81SBjoern A. Zeeb 	/* BSD Specific. */
53946b4cac81SBjoern A. Zeeb 	lhw->ic = lkpi_ieee80211_ifalloc();
53956b4cac81SBjoern A. Zeeb 
53966b4cac81SBjoern A. Zeeb 	IMPROVE();
53976b4cac81SBjoern A. Zeeb 
53986b4cac81SBjoern A. Zeeb 	return (hw);
53996b4cac81SBjoern A. Zeeb }
54006b4cac81SBjoern A. Zeeb 
54016b4cac81SBjoern A. Zeeb void
54026b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
54036b4cac81SBjoern A. Zeeb {
54046b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5405759a996dSBjoern A. Zeeb 	struct mbuf *m;
54066b4cac81SBjoern A. Zeeb 
54076b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54086b4cac81SBjoern A. Zeeb 	free(lhw->ic, M_LKPI80211);
54096b4cac81SBjoern A. Zeeb 	lhw->ic = NULL;
54106b4cac81SBjoern A. Zeeb 
5411759a996dSBjoern A. Zeeb 	/*
5412759a996dSBjoern A. Zeeb 	 * Drain the deferred RX path.
5413759a996dSBjoern A. Zeeb 	 */
5414759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5415759a996dSBjoern A. Zeeb 	lhw->rxq_stopped = true;
5416759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5417759a996dSBjoern A. Zeeb 
5418759a996dSBjoern A. Zeeb 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
5419759a996dSBjoern A. Zeeb 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
5420759a996dSBjoern A. Zeeb 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
5421759a996dSBjoern A. Zeeb 
5422759a996dSBjoern A. Zeeb 	/* Flush mbufq (make sure to release ni refs!). */
5423759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&lhw->rxq);
5424759a996dSBjoern A. Zeeb 	while (m != NULL) {
5425759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
5426759a996dSBjoern A. Zeeb 
5427759a996dSBjoern A. Zeeb 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5428759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
5429759a996dSBjoern A. Zeeb 			struct lkpi_80211_tag_rxni *rxni;
5430759a996dSBjoern A. Zeeb 
5431759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5432759a996dSBjoern A. Zeeb 			ieee80211_free_node(rxni->ni);
5433759a996dSBjoern A. Zeeb 		}
5434759a996dSBjoern A. Zeeb 		m_freem(m);
5435759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&lhw->rxq);
5436759a996dSBjoern A. Zeeb 	}
5437759a996dSBjoern A. Zeeb 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
5438759a996dSBjoern A. Zeeb 	    __func__, lhw, mbufq_len(&lhw->rxq)));
5439759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
5440759a996dSBjoern A. Zeeb 
54416b4cac81SBjoern A. Zeeb 	/* Cleanup more of lhw here or in wiphy_free()? */
5442eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
54438ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
5444eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
5445eac3646fSBjoern A. Zeeb 	sx_destroy(&lhw->lvif_sx);
54466b4cac81SBjoern A. Zeeb 	IMPROVE();
54476b4cac81SBjoern A. Zeeb }
54486b4cac81SBjoern A. Zeeb 
54496b4cac81SBjoern A. Zeeb void
54506b4cac81SBjoern A. Zeeb linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
54516b4cac81SBjoern A. Zeeb {
54526b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54536b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54546b4cac81SBjoern A. Zeeb 
54556b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54566b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54576b4cac81SBjoern A. Zeeb 
54586b4cac81SBjoern A. Zeeb 	/* Now set a proper name before ieee80211_ifattach(). */
54596b4cac81SBjoern A. Zeeb 	ic->ic_softc = lhw;
54606b4cac81SBjoern A. Zeeb 	ic->ic_name = name;
54616b4cac81SBjoern A. Zeeb 
54626b4cac81SBjoern A. Zeeb 	/* XXX-BZ do we also need to set wiphy name? */
54636b4cac81SBjoern A. Zeeb }
54646b4cac81SBjoern A. Zeeb 
54656b4cac81SBjoern A. Zeeb struct ieee80211_hw *
54666b4cac81SBjoern A. Zeeb linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
54676b4cac81SBjoern A. Zeeb {
54686b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
54696b4cac81SBjoern A. Zeeb 
54706b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
54716b4cac81SBjoern A. Zeeb 	return (LHW_TO_HW(lhw));
54726b4cac81SBjoern A. Zeeb }
54736b4cac81SBjoern A. Zeeb 
54746b4cac81SBjoern A. Zeeb static void
54756b4cac81SBjoern A. Zeeb lkpi_radiotap_attach(struct lkpi_hw *lhw)
54766b4cac81SBjoern A. Zeeb {
54776b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54786b4cac81SBjoern A. Zeeb 
54796b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54806b4cac81SBjoern A. Zeeb 	ieee80211_radiotap_attach(ic,
54816b4cac81SBjoern A. Zeeb 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
54826b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_TX_FLAGS_PRESENT,
54836b4cac81SBjoern A. Zeeb 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
54846b4cac81SBjoern A. Zeeb 	    LKPI_RTAP_RX_FLAGS_PRESENT);
54856b4cac81SBjoern A. Zeeb }
54866b4cac81SBjoern A. Zeeb 
54872e183d99SBjoern A. Zeeb int
54886b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
54896b4cac81SBjoern A. Zeeb {
54906b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
54916b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5492c5b96b3eSBjoern A. Zeeb 	int band, i;
54936b4cac81SBjoern A. Zeeb 
54946b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
54956b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
54966b4cac81SBjoern A. Zeeb 
5497652e22d3SBjoern A. Zeeb 	/* We do it this late as wiphy->dev should be set for the name. */
5498652e22d3SBjoern A. Zeeb 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
5499652e22d3SBjoern A. Zeeb 	if (lhw->workq == NULL)
5500652e22d3SBjoern A. Zeeb 		return (-EAGAIN);
5501652e22d3SBjoern A. Zeeb 
55026b4cac81SBjoern A. Zeeb 	/* XXX-BZ figure this out how they count his... */
55036b4cac81SBjoern A. Zeeb 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
55046b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55056b4cac81SBjoern A. Zeeb 		    hw->wiphy->perm_addr);
55066b4cac81SBjoern A. Zeeb 	} else if (hw->wiphy->n_addresses > 0) {
55076b4cac81SBjoern A. Zeeb 		/* We take the first one. */
55086b4cac81SBjoern A. Zeeb 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
55096b4cac81SBjoern A. Zeeb 		    hw->wiphy->addresses[0].addr);
55106b4cac81SBjoern A. Zeeb 	} else {
55116b4cac81SBjoern A. Zeeb 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
55126b4cac81SBjoern A. Zeeb 	}
55136b4cac81SBjoern A. Zeeb 
55143d09d310SBjoern A. Zeeb #ifdef __not_yet__
55153d09d310SBjoern A. Zeeb 	/* See comment in lkpi_80211_txq_tx_one(). */
55166b4cac81SBjoern A. Zeeb 	ic->ic_headroom = hw->extra_tx_headroom;
55173d09d310SBjoern A. Zeeb #endif
55186b4cac81SBjoern A. Zeeb 
55196b4cac81SBjoern A. Zeeb 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
55206b4cac81SBjoern A. Zeeb 	ic->ic_opmode = IEEE80211_M_STA;
55216b4cac81SBjoern A. Zeeb 
55226b4cac81SBjoern A. Zeeb 	/* Set device capabilities. */
55236b4cac81SBjoern A. Zeeb 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
55246b4cac81SBjoern A. Zeeb 	ic->ic_caps =
55256b4cac81SBjoern A. Zeeb 	    IEEE80211_C_STA |
55266b4cac81SBjoern A. Zeeb 	    IEEE80211_C_MONITOR |
55276b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WPA |		/* WPA/RSN */
55284a67f1dfSBjoern A. Zeeb #ifdef LKPI_80211_WME
55296b4cac81SBjoern A. Zeeb 	    IEEE80211_C_WME |
55304a67f1dfSBjoern A. Zeeb #endif
55316b4cac81SBjoern A. Zeeb #if 0
55326b4cac81SBjoern A. Zeeb 	    IEEE80211_C_PMGT |
55336b4cac81SBjoern A. Zeeb #endif
55346b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
55356b4cac81SBjoern A. Zeeb 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
55366b4cac81SBjoern A. Zeeb 	    ;
55376b4cac81SBjoern A. Zeeb #if 0
55386b4cac81SBjoern A. Zeeb 	/* Scanning is a different kind of beast to re-work. */
55396b4cac81SBjoern A. Zeeb 	ic->ic_caps |= IEEE80211_C_BGSCAN;
55406b4cac81SBjoern A. Zeeb #endif
5541cc4e78d5SBjoern A. Zeeb 	if (lhw->ops->hw_scan) {
5542cc4e78d5SBjoern A. Zeeb 		/*
5543cc4e78d5SBjoern A. Zeeb 		 * Advertise full-offload scanning.
5544cc4e78d5SBjoern A. Zeeb 		 *
5545cc4e78d5SBjoern A. Zeeb 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
5546cc4e78d5SBjoern A. Zeeb 		 * we essentially disable hw_scan for all drivers not setting
5547cc4e78d5SBjoern A. Zeeb 		 * the flag.
5548cc4e78d5SBjoern A. Zeeb 		 */
55496b4cac81SBjoern A. Zeeb 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
5550a486fbbdSBjoern A. Zeeb 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
55516b4cac81SBjoern A. Zeeb 	}
55526b4cac81SBjoern A. Zeeb 
5553527687a9SBjoern A. Zeeb 	/*
5554527687a9SBjoern A. Zeeb 	 * The wiphy variables report bitmasks of avail antennas.
5555527687a9SBjoern A. Zeeb 	 * (*get_antenna) get the current bitmask sets which can be
5556527687a9SBjoern A. Zeeb 	 * altered by (*set_antenna) for some drivers.
5557527687a9SBjoern A. Zeeb 	 * XXX-BZ will the count alone do us much good long-term in net80211?
5558527687a9SBjoern A. Zeeb 	 */
5559527687a9SBjoern A. Zeeb 	if (hw->wiphy->available_antennas_rx ||
5560527687a9SBjoern A. Zeeb 	    hw->wiphy->available_antennas_tx) {
5561527687a9SBjoern A. Zeeb 		uint32_t rxs, txs;
5562527687a9SBjoern A. Zeeb 
5563527687a9SBjoern A. Zeeb 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
5564527687a9SBjoern A. Zeeb 			ic->ic_rxstream = bitcount32(rxs);
5565527687a9SBjoern A. Zeeb 			ic->ic_txstream = bitcount32(txs);
5566527687a9SBjoern A. Zeeb 		}
5567527687a9SBjoern A. Zeeb 	}
5568527687a9SBjoern A. Zeeb 
55696b4cac81SBjoern A. Zeeb 	ic->ic_cryptocaps = 0;
5570b35f6cd0SBjoern A. Zeeb #ifdef LKPI_80211_HW_CRYPTO
557111db70b6SBjoern A. Zeeb 	if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
55726b4cac81SBjoern A. Zeeb 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
55736b4cac81SBjoern A. Zeeb 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
55746b4cac81SBjoern A. Zeeb 			    hw->wiphy->cipher_suites[i]);
55756b4cac81SBjoern A. Zeeb 	}
55766b4cac81SBjoern A. Zeeb #endif
55776b4cac81SBjoern A. Zeeb 
55786b4cac81SBjoern A. Zeeb 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
55796b4cac81SBjoern A. Zeeb 	    ic->ic_channels);
55806b4cac81SBjoern A. Zeeb 
55816b4cac81SBjoern A. Zeeb 	ieee80211_ifattach(ic);
55826b4cac81SBjoern A. Zeeb 
55836b4cac81SBjoern A. Zeeb 	ic->ic_update_mcast = lkpi_ic_update_mcast;
55846b4cac81SBjoern A. Zeeb 	ic->ic_update_promisc = lkpi_ic_update_promisc;
55856b4cac81SBjoern A. Zeeb 	ic->ic_update_chw = lkpi_ic_update_chw;
55866b4cac81SBjoern A. Zeeb 	ic->ic_parent = lkpi_ic_parent;
55876b4cac81SBjoern A. Zeeb 	ic->ic_scan_start = lkpi_ic_scan_start;
55886b4cac81SBjoern A. Zeeb 	ic->ic_scan_end = lkpi_ic_scan_end;
55896b4cac81SBjoern A. Zeeb 	ic->ic_set_channel = lkpi_ic_set_channel;
55906b4cac81SBjoern A. Zeeb 	ic->ic_transmit = lkpi_ic_transmit;
55916b4cac81SBjoern A. Zeeb 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
55926b4cac81SBjoern A. Zeeb 	ic->ic_vap_create = lkpi_ic_vap_create;
55936b4cac81SBjoern A. Zeeb 	ic->ic_vap_delete = lkpi_ic_vap_delete;
55946b4cac81SBjoern A. Zeeb 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
55956b4cac81SBjoern A. Zeeb 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
55966b4cac81SBjoern A. Zeeb 
5597a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
5598a486fbbdSBjoern A. Zeeb 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
5599a486fbbdSBjoern A. Zeeb 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
5600a486fbbdSBjoern A. Zeeb 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
5601a486fbbdSBjoern A. Zeeb 
56026b4cac81SBjoern A. Zeeb 	lhw->ic_node_alloc = ic->ic_node_alloc;
56036b4cac81SBjoern A. Zeeb 	ic->ic_node_alloc = lkpi_ic_node_alloc;
56046b4cac81SBjoern A. Zeeb 	lhw->ic_node_init = ic->ic_node_init;
56056b4cac81SBjoern A. Zeeb 	ic->ic_node_init = lkpi_ic_node_init;
56066b4cac81SBjoern A. Zeeb 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
56076b4cac81SBjoern A. Zeeb 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
56086b4cac81SBjoern A. Zeeb 	lhw->ic_node_free = ic->ic_node_free;
56096b4cac81SBjoern A. Zeeb 	ic->ic_node_free = lkpi_ic_node_free;
56106b4cac81SBjoern A. Zeeb 
56119fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
561286bc7259SBjoern A. Zeeb 	/*
561386bc7259SBjoern A. Zeeb 	 * Only attach if the driver/firmware supports (*ampdu_action)().
561486bc7259SBjoern A. Zeeb 	 * Otherwise it is in the hands of net80211.
561586bc7259SBjoern A. Zeeb 	 */
561686bc7259SBjoern A. Zeeb 	if (lhw->ops->ampdu_action != NULL) {
56179fb91463SBjoern A. Zeeb 		lhw->ic_recv_action = ic->ic_recv_action;
56189fb91463SBjoern A. Zeeb 		ic->ic_recv_action = lkpi_ic_recv_action;
56199fb91463SBjoern A. Zeeb 		lhw->ic_send_action = ic->ic_send_action;
56209fb91463SBjoern A. Zeeb 		ic->ic_send_action = lkpi_ic_send_action;
56219fb91463SBjoern A. Zeeb 
56229fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
56239fb91463SBjoern A. Zeeb 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
56249fb91463SBjoern A. Zeeb 
56259fb91463SBjoern A. Zeeb 		lhw->ic_addba_request = ic->ic_addba_request;
56269fb91463SBjoern A. Zeeb 		ic->ic_addba_request = lkpi_ic_addba_request;
56279fb91463SBjoern A. Zeeb 		lhw->ic_addba_response = ic->ic_addba_response;
56289fb91463SBjoern A. Zeeb 		ic->ic_addba_response = lkpi_ic_addba_response;
56299fb91463SBjoern A. Zeeb 		lhw->ic_addba_stop = ic->ic_addba_stop;
56309fb91463SBjoern A. Zeeb 		ic->ic_addba_stop = lkpi_ic_addba_stop;
56319fb91463SBjoern A. Zeeb 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
56329fb91463SBjoern A. Zeeb 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
56339fb91463SBjoern A. Zeeb 
56349fb91463SBjoern A. Zeeb 		lhw->ic_bar_response = ic->ic_bar_response;
56359fb91463SBjoern A. Zeeb 		ic->ic_bar_response = lkpi_ic_bar_response;
56369fb91463SBjoern A. Zeeb 
56379fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
56389fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
56399fb91463SBjoern A. Zeeb 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
56409fb91463SBjoern A. Zeeb 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
564186bc7259SBjoern A. Zeeb 	}
56429fb91463SBjoern A. Zeeb #endif
56439fb91463SBjoern A. Zeeb 
56446b4cac81SBjoern A. Zeeb 	lkpi_radiotap_attach(lhw);
56456b4cac81SBjoern A. Zeeb 
5646c5b96b3eSBjoern A. Zeeb 	/*
5647c5b96b3eSBjoern A. Zeeb 	 * Assign the first possible channel for now;  seems Realtek drivers
5648c5b96b3eSBjoern A. Zeeb 	 * expect one.
5649d9945d78SBjoern A. Zeeb 	 * Also remember the amount of bands we support and the most rates
5650d9945d78SBjoern A. Zeeb 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
5651c5b96b3eSBjoern A. Zeeb 	 */
5652d9945d78SBjoern A. Zeeb 	lhw->supbands = lhw->max_rates = 0;
5653f454a4a1SBjoern A. Zeeb 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5654c5b96b3eSBjoern A. Zeeb 		struct ieee80211_supported_band *supband;
5655c5b96b3eSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *channels;
5656c5b96b3eSBjoern A. Zeeb 
5657c5b96b3eSBjoern A. Zeeb 		supband = hw->wiphy->bands[band];
5658c5b96b3eSBjoern A. Zeeb 		if (supband == NULL || supband->n_channels == 0)
5659c5b96b3eSBjoern A. Zeeb 			continue;
5660c5b96b3eSBjoern A. Zeeb 
5661d9945d78SBjoern A. Zeeb 		lhw->supbands++;
5662d9945d78SBjoern A. Zeeb 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
5663d9945d78SBjoern A. Zeeb 
5664f454a4a1SBjoern A. Zeeb 		/* If we have a channel, we need to keep counting supbands. */
5665f454a4a1SBjoern A. Zeeb 		if (hw->conf.chandef.chan != NULL)
5666f454a4a1SBjoern A. Zeeb 			continue;
5667f454a4a1SBjoern A. Zeeb 
5668c5b96b3eSBjoern A. Zeeb 		channels = supband->channels;
5669c5b96b3eSBjoern A. Zeeb 		for (i = 0; i < supband->n_channels; i++) {
5670c5b96b3eSBjoern A. Zeeb 
5671c5b96b3eSBjoern A. Zeeb 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
5672c5b96b3eSBjoern A. Zeeb 				continue;
5673c5b96b3eSBjoern A. Zeeb 
56744a07abdeSBjoern A. Zeeb 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
56759fb91463SBjoern A. Zeeb #ifdef LKPI_80211_HT
567611450726SBjoern A. Zeeb 			    (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
56779fb91463SBjoern A. Zeeb #endif
5678c5b96b3eSBjoern A. Zeeb 			    NL80211_CHAN_NO_HT);
5679c5b96b3eSBjoern A. Zeeb 			break;
5680c5b96b3eSBjoern A. Zeeb 		}
5681c5b96b3eSBjoern A. Zeeb 	}
5682c5b96b3eSBjoern A. Zeeb 
5683d9945d78SBjoern A. Zeeb 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
5684d9945d78SBjoern A. Zeeb 
5685d9945d78SBjoern A. Zeeb 	/* Make sure we do not support more than net80211 is willing to take. */
5686d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
5687d9945d78SBjoern A. Zeeb 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
5688d9945d78SBjoern A. Zeeb 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
5689d9945d78SBjoern A. Zeeb 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
5690d9945d78SBjoern A. Zeeb 	}
5691d9945d78SBjoern A. Zeeb 
5692d9945d78SBjoern A. Zeeb 	/*
5693d9945d78SBjoern A. Zeeb 	 * The maximum supported bitrates on any band + size for
5694d9945d78SBjoern A. Zeeb 	 * DSSS Parameter Set give our per-band IE size.
5695d9945d78SBjoern A. Zeeb 	 * SSID is the responsibility of the driver and goes on the side.
5696d9945d78SBjoern A. Zeeb 	 * The user specified bits coming from the vap go into the
5697d9945d78SBjoern A. Zeeb 	 * "common ies" fields.
5698d9945d78SBjoern A. Zeeb 	 */
5699d9945d78SBjoern A. Zeeb 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
5700d9945d78SBjoern A. Zeeb 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
5701d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
570278ca45dfSBjoern A. Zeeb 
570378ca45dfSBjoern A. Zeeb 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
5704d9945d78SBjoern A. Zeeb 		/*
570578ca45dfSBjoern A. Zeeb 		 * net80211 does not seem to support the DSSS Parameter Set but
570678ca45dfSBjoern A. Zeeb 		 * some of the drivers insert it so calculate the extra fixed
570778ca45dfSBjoern A. Zeeb 		 * space in.
5708d9945d78SBjoern A. Zeeb 		 */
5709d9945d78SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + 1;
571078ca45dfSBjoern A. Zeeb 	}
5711d9945d78SBjoern A. Zeeb 
57129fb91463SBjoern A. Zeeb #if defined(LKPI_80211_HT)
57139fb91463SBjoern A. Zeeb 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
57149fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
57159fb91463SBjoern A. Zeeb #endif
57169fb91463SBjoern A. Zeeb #if defined(LKPI_80211_VHT)
57171f73e0edSBjoern A. Zeeb 	if (IEEE80211_CONF_VHT(ic))
57189fb91463SBjoern A. Zeeb 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
57199fb91463SBjoern A. Zeeb #endif
57209fb91463SBjoern A. Zeeb 
5721d9945d78SBjoern A. Zeeb 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
572278ca45dfSBjoern A. Zeeb 	if (hw->wiphy->max_scan_ie_len > 0) {
572378ca45dfSBjoern A. Zeeb 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
572478ca45dfSBjoern A. Zeeb 			goto err;
5725d9945d78SBjoern A. Zeeb 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
572678ca45dfSBjoern A. Zeeb 	}
5727d9945d78SBjoern A. Zeeb 
57286b4cac81SBjoern A. Zeeb 	if (bootverbose)
57296b4cac81SBjoern A. Zeeb 		ieee80211_announce(ic);
57302e183d99SBjoern A. Zeeb 
57312e183d99SBjoern A. Zeeb 	return (0);
573278ca45dfSBjoern A. Zeeb err:
573378ca45dfSBjoern A. Zeeb 	IMPROVE("TODO FIXME CLEANUP");
573478ca45dfSBjoern A. Zeeb 	return (-EAGAIN);
57356b4cac81SBjoern A. Zeeb }
57366b4cac81SBjoern A. Zeeb 
57376b4cac81SBjoern A. Zeeb void
57386b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
57396b4cac81SBjoern A. Zeeb {
57406b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57416b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
57426b4cac81SBjoern A. Zeeb 
57436b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57446b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
57456b4cac81SBjoern A. Zeeb 	ieee80211_ifdetach(ic);
57466b4cac81SBjoern A. Zeeb }
57476b4cac81SBjoern A. Zeeb 
57486b4cac81SBjoern A. Zeeb void
57496b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
57506b4cac81SBjoern A. Zeeb     enum ieee80211_iface_iter flags,
57516b4cac81SBjoern A. Zeeb     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
57526b4cac81SBjoern A. Zeeb     void *arg)
57536b4cac81SBjoern A. Zeeb {
57546b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
57556b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
57566b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
575761a68e50SBjoern A. Zeeb 	bool active, atomic, nin_drv;
57586b4cac81SBjoern A. Zeeb 
57596b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
57606b4cac81SBjoern A. Zeeb 
57616b4cac81SBjoern A. Zeeb 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
57626b4cac81SBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_RESUME_ALL|
576361a68e50SBjoern A. Zeeb 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5764adff403fSBjoern A. Zeeb 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
57656b4cac81SBjoern A. Zeeb 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
57666b4cac81SBjoern A. Zeeb 		    __func__, flags);
57676b4cac81SBjoern A. Zeeb 	}
57686b4cac81SBjoern A. Zeeb 
5769adff403fSBjoern A. Zeeb 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
57706b4cac81SBjoern A. Zeeb 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
577161a68e50SBjoern A. Zeeb 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
57726b4cac81SBjoern A. Zeeb 
57736b4cac81SBjoern A. Zeeb 	if (atomic)
57748891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_LOCK(lhw);
57756b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
57766b4cac81SBjoern A. Zeeb 		struct ieee80211vap *vap;
57776b4cac81SBjoern A. Zeeb 
57786b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
57796b4cac81SBjoern A. Zeeb 
57806b4cac81SBjoern A. Zeeb 		/*
57816b4cac81SBjoern A. Zeeb 		 * If we want "active" interfaces, we need to distinguish on
57826b4cac81SBjoern A. Zeeb 		 * whether the driver knows about them or not to be able to
57836b4cac81SBjoern A. Zeeb 		 * handle the "resume" case correctly.  Skip the ones the
57846b4cac81SBjoern A. Zeeb 		 * driver does not know about.
57856b4cac81SBjoern A. Zeeb 		 */
57866b4cac81SBjoern A. Zeeb 		if (active && !lvif->added_to_drv &&
57876b4cac81SBjoern A. Zeeb 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
57886b4cac81SBjoern A. Zeeb 			continue;
57896b4cac81SBjoern A. Zeeb 
57906b4cac81SBjoern A. Zeeb 		/*
579161a68e50SBjoern A. Zeeb 		 * If we shall skip interfaces not added to the driver do so
579261a68e50SBjoern A. Zeeb 		 * if we haven't yet.
579361a68e50SBjoern A. Zeeb 		 */
579461a68e50SBjoern A. Zeeb 		if (nin_drv && !lvif->added_to_drv)
579561a68e50SBjoern A. Zeeb 			continue;
579661a68e50SBjoern A. Zeeb 
579761a68e50SBjoern A. Zeeb 		/*
57986b4cac81SBjoern A. Zeeb 		 * Run the iterator function if we are either not asking
57996b4cac81SBjoern A. Zeeb 		 * asking for active only or if the VAP is "running".
58006b4cac81SBjoern A. Zeeb 		 */
58016b4cac81SBjoern A. Zeeb 		/* XXX-BZ probably should have state in the lvif as well. */
58026b4cac81SBjoern A. Zeeb 		vap = LVIF_TO_VAP(lvif);
58036b4cac81SBjoern A. Zeeb 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
58046b4cac81SBjoern A. Zeeb 			iterfunc(arg, vif->addr, vif);
58056b4cac81SBjoern A. Zeeb 	}
58066b4cac81SBjoern A. Zeeb 	if (atomic)
58078891c455SBjoern A. Zeeb 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
58086b4cac81SBjoern A. Zeeb }
58096b4cac81SBjoern A. Zeeb 
581011db70b6SBjoern A. Zeeb static void
581111db70b6SBjoern A. Zeeb lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
581211db70b6SBjoern A. Zeeb     ieee80211_keyix keyix, struct lkpi_sta *lsta,
581311db70b6SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
581411db70b6SBjoern A. Zeeb 	struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
581511db70b6SBjoern A. Zeeb     void *arg)
581611db70b6SBjoern A. Zeeb {
581711db70b6SBjoern A. Zeeb 	if (!lsta->added_to_drv)
581811db70b6SBjoern A. Zeeb 		return;
581911db70b6SBjoern A. Zeeb 
582011db70b6SBjoern A. Zeeb 	if (lsta->kc[keyix] == NULL)
582111db70b6SBjoern A. Zeeb 		return;
582211db70b6SBjoern A. Zeeb 
582311db70b6SBjoern A. Zeeb 	iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
582411db70b6SBjoern A. Zeeb }
582511db70b6SBjoern A. Zeeb 
58266b4cac81SBjoern A. Zeeb void
58276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
58286b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif,
58296b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
58306b4cac81SBjoern A. Zeeb         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
583111db70b6SBjoern A. Zeeb     void *arg, bool rcu)
58326b4cac81SBjoern A. Zeeb {
583311db70b6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
583411db70b6SBjoern A. Zeeb 	struct lkpi_vif *lvif;
58356b4cac81SBjoern A. Zeeb 
583611db70b6SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
583711db70b6SBjoern A. Zeeb 
583811db70b6SBjoern A. Zeeb 	if (rcu) {
5839a8f735a6SBjoern A. Zeeb 		rcu_read_lock_held();		/* XXX-BZ is this correct? */
5840a8f735a6SBjoern A. Zeeb 
584111db70b6SBjoern A. Zeeb 		if (vif == NULL) {
584211db70b6SBjoern A. Zeeb 			TODO();
584311db70b6SBjoern A. Zeeb 		} else {
5844a8f735a6SBjoern A. Zeeb 			list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
584511db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
584611db70b6SBjoern A. Zeeb 				    keyix++)
584711db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
584811db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
584911db70b6SBjoern A. Zeeb 			}
585011db70b6SBjoern A. Zeeb 		}
585111db70b6SBjoern A. Zeeb 	} else {
585211db70b6SBjoern A. Zeeb 		TODO("Used by suspend/resume; order of keys as installed to "
585311db70b6SBjoern A. Zeeb 		"firmware is important; we'll need to rewrite some code for that");
585411db70b6SBjoern A. Zeeb 		lockdep_assert_wiphy(hw->wiphy);
585511db70b6SBjoern A. Zeeb 
585611db70b6SBjoern A. Zeeb 		if (vif == NULL) {
585711db70b6SBjoern A. Zeeb 			TODO();
585811db70b6SBjoern A. Zeeb 		} else {
5859a8f735a6SBjoern A. Zeeb 			list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
586011db70b6SBjoern A. Zeeb 				for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
586111db70b6SBjoern A. Zeeb 				    keyix++)
586211db70b6SBjoern A. Zeeb 					lkpi_ieee80211_iterate_keys(hw, vif,
586311db70b6SBjoern A. Zeeb 					    keyix, lsta, iterfunc, arg);
586411db70b6SBjoern A. Zeeb 			}
586511db70b6SBjoern A. Zeeb 		}
586611db70b6SBjoern A. Zeeb 	}
58676b4cac81SBjoern A. Zeeb }
58686b4cac81SBjoern A. Zeeb 
58696b4cac81SBjoern A. Zeeb void
58706b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
58716b4cac81SBjoern A. Zeeb     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
58726b4cac81SBjoern A. Zeeb 	void *),
58736b4cac81SBjoern A. Zeeb     void *arg)
58746b4cac81SBjoern A. Zeeb {
5875c5e25798SBjoern A. Zeeb 	struct lkpi_hw *lhw;
5876c5e25798SBjoern A. Zeeb 	struct lkpi_vif *lvif;
5877c5e25798SBjoern A. Zeeb 	struct ieee80211_vif *vif;
5878c5e25798SBjoern A. Zeeb 	struct lkpi_chanctx *lchanctx;
58796b4cac81SBjoern A. Zeeb 
5880c5e25798SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
5881c5e25798SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5882c5e25798SBjoern A. Zeeb 
5883c5e25798SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
5884c5e25798SBjoern A. Zeeb 
5885c5e25798SBjoern A. Zeeb 	IMPROVE("lchanctx should be its own list somewhere");
5886c5e25798SBjoern A. Zeeb 
5887c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5888c5e25798SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5889c5e25798SBjoern A. Zeeb 
5890c5e25798SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
5891c5e25798SBjoern A. Zeeb 		if (vif->chanctx_conf == NULL)
5892c5e25798SBjoern A. Zeeb 			continue;
5893c5e25798SBjoern A. Zeeb 
5894c5e25798SBjoern A. Zeeb 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5895c5e25798SBjoern A. Zeeb 		if (!lchanctx->added_to_drv)
5896c5e25798SBjoern A. Zeeb 			continue;
5897c5e25798SBjoern A. Zeeb 
5898d1af434dSBjoern A. Zeeb 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5899c5e25798SBjoern A. Zeeb 	}
5900c5e25798SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59016b4cac81SBjoern A. Zeeb }
59026b4cac81SBjoern A. Zeeb 
59036b4cac81SBjoern A. Zeeb void
59046b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
59056b4cac81SBjoern A. Zeeb    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
59066b4cac81SBjoern A. Zeeb {
59076b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59086b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
59096b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
59106b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
59116b4cac81SBjoern A. Zeeb 
59126b4cac81SBjoern A. Zeeb 	KASSERT(hw != NULL && iterfunc != NULL,
59136b4cac81SBjoern A. Zeeb 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
59146b4cac81SBjoern A. Zeeb 
59156b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
59166b4cac81SBjoern A. Zeeb 
59178891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
59186b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
59196b4cac81SBjoern A. Zeeb 
5920a8f735a6SBjoern A. Zeeb 		rcu_read_lock();
5921a8f735a6SBjoern A. Zeeb 		list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
59226b4cac81SBjoern A. Zeeb 			if (!lsta->added_to_drv)
59236b4cac81SBjoern A. Zeeb 				continue;
59246b4cac81SBjoern A. Zeeb 			sta = LSTA_TO_STA(lsta);
59256b4cac81SBjoern A. Zeeb 			iterfunc(arg, sta);
59266b4cac81SBjoern A. Zeeb 		}
5927a8f735a6SBjoern A. Zeeb 		rcu_read_unlock();
59286b4cac81SBjoern A. Zeeb 	}
59298891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
59306b4cac81SBjoern A. Zeeb }
59316b4cac81SBjoern A. Zeeb 
5932673d62fcSBjoern A. Zeeb struct linuxkpi_ieee80211_regdomain *
5933673d62fcSBjoern A. Zeeb lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5934673d62fcSBjoern A. Zeeb {
5935673d62fcSBjoern A. Zeeb 	struct linuxkpi_ieee80211_regdomain *regd;
5936673d62fcSBjoern A. Zeeb 
5937673d62fcSBjoern A. Zeeb 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5938673d62fcSBjoern A. Zeeb 	    GFP_KERNEL);
5939673d62fcSBjoern A. Zeeb 	return (regd);
5940673d62fcSBjoern A. Zeeb }
5941673d62fcSBjoern A. Zeeb 
59426b4cac81SBjoern A. Zeeb int
59436b4cac81SBjoern A. Zeeb linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
59446b4cac81SBjoern A. Zeeb     struct linuxkpi_ieee80211_regdomain *regd)
59456b4cac81SBjoern A. Zeeb {
59466b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59476b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59486b4cac81SBjoern A. Zeeb 	struct ieee80211_regdomain *rd;
59496b4cac81SBjoern A. Zeeb 
59506b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
59516b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59526b4cac81SBjoern A. Zeeb 
59536b4cac81SBjoern A. Zeeb 	rd = &ic->ic_regdomain;
59546b4cac81SBjoern A. Zeeb 	if (rd->isocc[0] == '\0') {
59556b4cac81SBjoern A. Zeeb 		rd->isocc[0] = regd->alpha2[0];
59566b4cac81SBjoern A. Zeeb 		rd->isocc[1] = regd->alpha2[1];
59576b4cac81SBjoern A. Zeeb 	}
59586b4cac81SBjoern A. Zeeb 
59596b4cac81SBjoern A. Zeeb 	TODO();
59606b4cac81SBjoern A. Zeeb 	/* XXX-BZ finish the rest. */
59616b4cac81SBjoern A. Zeeb 
59626b4cac81SBjoern A. Zeeb 	return (0);
59636b4cac81SBjoern A. Zeeb }
59646b4cac81SBjoern A. Zeeb 
59656b4cac81SBjoern A. Zeeb void
59666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
59676b4cac81SBjoern A. Zeeb     struct cfg80211_scan_info *info)
59686b4cac81SBjoern A. Zeeb {
59696b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
59706b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
59716b4cac81SBjoern A. Zeeb 	struct ieee80211_scan_state *ss;
59726b4cac81SBjoern A. Zeeb 
59736b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
59746b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
59756b4cac81SBjoern A. Zeeb 	ss = ic->ic_scan;
59766b4cac81SBjoern A. Zeeb 
59776b4cac81SBjoern A. Zeeb 	ieee80211_scan_done(ss->ss_vap);
59786b4cac81SBjoern A. Zeeb 
59798ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_LOCK(lhw);
59806b4cac81SBjoern A. Zeeb 	free(lhw->hw_req, M_LKPI80211);
59816b4cac81SBjoern A. Zeeb 	lhw->hw_req = NULL;
5982a486fbbdSBjoern A. Zeeb 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
59836b4cac81SBjoern A. Zeeb 	wakeup(lhw);
59848ac540d3SBjoern A. Zeeb 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
59856b4cac81SBjoern A. Zeeb 
59866b4cac81SBjoern A. Zeeb 	return;
59876b4cac81SBjoern A. Zeeb }
59886b4cac81SBjoern A. Zeeb 
5989759a996dSBjoern A. Zeeb static void
5990759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5991759a996dSBjoern A. Zeeb {
5992759a996dSBjoern A. Zeeb 	struct ieee80211_node *ni;
5993759a996dSBjoern A. Zeeb 	struct m_tag *mtag;
5994759a996dSBjoern A. Zeeb 	int ok;
5995759a996dSBjoern A. Zeeb 
5996759a996dSBjoern A. Zeeb 	ni = NULL;
5997759a996dSBjoern A. Zeeb         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5998759a996dSBjoern A. Zeeb 	if (mtag != NULL) {
5999759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6000759a996dSBjoern A. Zeeb 
6001759a996dSBjoern A. Zeeb 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6002759a996dSBjoern A. Zeeb 		ni = rxni->ni;
6003759a996dSBjoern A. Zeeb 	}
6004759a996dSBjoern A. Zeeb 
6005759a996dSBjoern A. Zeeb 	if (ni != NULL) {
6006759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo(ni, m);
6007759a996dSBjoern A. Zeeb 		ieee80211_free_node(ni);		/* Release the reference. */
6008759a996dSBjoern A. Zeeb 		if (ok < 0)
6009759a996dSBjoern A. Zeeb 			m_freem(m);
6010759a996dSBjoern A. Zeeb 	} else {
6011759a996dSBjoern A. Zeeb 		ok = ieee80211_input_mimo_all(lhw->ic, m);
6012759a996dSBjoern A. Zeeb 		/* mbuf got consumed. */
6013759a996dSBjoern A. Zeeb 	}
6014759a996dSBjoern A. Zeeb 
6015759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6016759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6017bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
6018759a996dSBjoern A. Zeeb #endif
6019759a996dSBjoern A. Zeeb }
6020759a996dSBjoern A. Zeeb 
6021759a996dSBjoern A. Zeeb static void
6022759a996dSBjoern A. Zeeb lkpi_80211_lhw_rxq_task(void *ctx, int pending)
6023759a996dSBjoern A. Zeeb {
6024759a996dSBjoern A. Zeeb 	struct lkpi_hw *lhw;
6025759a996dSBjoern A. Zeeb 	struct mbufq mq;
6026759a996dSBjoern A. Zeeb 	struct mbuf *m;
6027759a996dSBjoern A. Zeeb 
6028759a996dSBjoern A. Zeeb 	lhw = ctx;
6029759a996dSBjoern A. Zeeb 
6030759a996dSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6031759a996dSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6032bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
6033bd206a6fSBjoern A. Zeeb 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
6034759a996dSBjoern A. Zeeb #endif
6035759a996dSBjoern A. Zeeb 
6036759a996dSBjoern A. Zeeb 	mbufq_init(&mq, IFQ_MAXLEN);
6037759a996dSBjoern A. Zeeb 
6038759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6039759a996dSBjoern A. Zeeb 	mbufq_concat(&mq, &lhw->rxq);
6040759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6041759a996dSBjoern A. Zeeb 
6042759a996dSBjoern A. Zeeb 	m = mbufq_dequeue(&mq);
6043759a996dSBjoern A. Zeeb 	while (m != NULL) {
6044759a996dSBjoern A. Zeeb 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
6045759a996dSBjoern A. Zeeb 		m = mbufq_dequeue(&mq);
6046759a996dSBjoern A. Zeeb 	}
6047759a996dSBjoern A. Zeeb }
6048759a996dSBjoern A. Zeeb 
604949010ba7SBjoern A. Zeeb static void
605049010ba7SBjoern A. Zeeb lkpi_convert_rx_status(struct ieee80211_hw *hw,
605149010ba7SBjoern A. Zeeb     struct ieee80211_rx_status *rx_status,
605249010ba7SBjoern A. Zeeb     struct ieee80211_rx_stats *rx_stats,
605349010ba7SBjoern A. Zeeb     uint8_t *rssip)
605449010ba7SBjoern A. Zeeb {
605549010ba7SBjoern A. Zeeb 	struct ieee80211_supported_band *supband;
605649010ba7SBjoern A. Zeeb 	int i;
605749010ba7SBjoern A. Zeeb 	uint8_t rssi;
605849010ba7SBjoern A. Zeeb 
605949010ba7SBjoern A. Zeeb 	memset(rx_stats, 0, sizeof(*rx_stats));
606049010ba7SBjoern A. Zeeb 	rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
606149010ba7SBjoern A. Zeeb 	/* XXX-BZ correct hardcoded noise floor, survey data? */
606249010ba7SBjoern A. Zeeb 	rx_stats->c_nf = -96;
606349010ba7SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
606449010ba7SBjoern A. Zeeb 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
606549010ba7SBjoern A. Zeeb 		rssi = rx_status->signal;
606649010ba7SBjoern A. Zeeb 	else
606749010ba7SBjoern A. Zeeb 		rssi = rx_stats->c_nf;
606849010ba7SBjoern A. Zeeb 	/*
606949010ba7SBjoern A. Zeeb 	 * net80211 signal strength data are in .5 dBm units relative to
607049010ba7SBjoern A. Zeeb 	 * the current noise floor (see comment in ieee80211_node.h).
607149010ba7SBjoern A. Zeeb 	 */
607249010ba7SBjoern A. Zeeb 	rssi -= rx_stats->c_nf;
607349010ba7SBjoern A. Zeeb 	if (rssip != NULL)
607449010ba7SBjoern A. Zeeb 		*rssip = rssi;
607549010ba7SBjoern A. Zeeb 	rx_stats->c_rssi = rssi * 2;
607649010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_BAND;
607749010ba7SBjoern A. Zeeb 	rx_stats->c_band =
607849010ba7SBjoern A. Zeeb 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
607949010ba7SBjoern A. Zeeb 	rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
608049010ba7SBjoern A. Zeeb 	rx_stats->c_freq = rx_status->freq;
608149010ba7SBjoern A. Zeeb 	rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
608249010ba7SBjoern A. Zeeb 
608349010ba7SBjoern A. Zeeb 	rx_stats->c_rx_tsf = rx_status->mactime;
608449010ba7SBjoern A. Zeeb 
608549010ba7SBjoern A. Zeeb 	/* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
608649010ba7SBjoern A. Zeeb 	if ((rx_status->flag & RX_FLAG_MACTIME) ==
608749010ba7SBjoern A. Zeeb 	    (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
608849010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_TSF64;
608949010ba7SBjoern A. Zeeb 		/* XXX RX_FLAG_MACTIME_PLCP_START ? */
609049010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
609149010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_START;
609249010ba7SBjoern A. Zeeb 		if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
609349010ba7SBjoern A. Zeeb 			rx_stats->r_flags |= IEEE80211_R_TSF_END;
609449010ba7SBjoern A. Zeeb 		/* XXX-BZ if TSF_END will net80211 do the unwind of time? */
609549010ba7SBjoern A. Zeeb 	}
609649010ba7SBjoern A. Zeeb 
609749010ba7SBjoern A. Zeeb 	if (rx_status->chains != 0) {
609849010ba7SBjoern A. Zeeb 		int cc;
609949010ba7SBjoern A. Zeeb 		int8_t crssi;
610049010ba7SBjoern A. Zeeb 
610149010ba7SBjoern A. Zeeb 		rx_stats->c_chain = rx_status->chains;
610249010ba7SBjoern A. Zeeb 		rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
610349010ba7SBjoern A. Zeeb 
610449010ba7SBjoern A. Zeeb 		cc = 0;
610549010ba7SBjoern A. Zeeb 		for (i = 0; i < nitems(rx_status->chain_signal); i++) {
610649010ba7SBjoern A. Zeeb 			if (!(rx_status->chains & BIT(i)))
610749010ba7SBjoern A. Zeeb 				continue;
610849010ba7SBjoern A. Zeeb 			crssi = rx_status->chain_signal[i];
610949010ba7SBjoern A. Zeeb 			crssi -= rx_stats->c_nf;
611049010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ctl[i] = crssi * 2;
611149010ba7SBjoern A. Zeeb 			rx_stats->c_rssi_ext[i] = crssi * 2;	/* XXX _ext ??? ATH thing? */
611249010ba7SBjoern A. Zeeb 			/* We currently only have the global noise floor value. */
611349010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
611449010ba7SBjoern A. Zeeb 			rx_stats->c_nf_ext[i] = rx_stats->c_nf;
611549010ba7SBjoern A. Zeeb 			cc++;
611649010ba7SBjoern A. Zeeb 		}
611749010ba7SBjoern A. Zeeb 		if (cc > 0)
611849010ba7SBjoern A. Zeeb 			 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
611949010ba7SBjoern A. Zeeb 	}
612049010ba7SBjoern A. Zeeb 
612149010ba7SBjoern A. Zeeb 	/* XXX-NET80211 We are not going to populate c_phytype! */
612249010ba7SBjoern A. Zeeb 
612349010ba7SBjoern A. Zeeb 	switch (rx_status->encoding) {
612449010ba7SBjoern A. Zeeb 	case RX_ENC_LEGACY:
612549010ba7SBjoern A. Zeeb 		supband = hw->wiphy->bands[rx_status->band];
612649010ba7SBjoern A. Zeeb 		if (supband != NULL)
612749010ba7SBjoern A. Zeeb 			rx_stats->c_rate = supband->bitrates[rx_status->rate_idx].bitrate;
612849010ba7SBjoern A. Zeeb 		/* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
612949010ba7SBjoern A. Zeeb 		break;
613049010ba7SBjoern A. Zeeb 	case RX_ENC_HT:
613149010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
613249010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
613349010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
613449010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
613549010ba7SBjoern A. Zeeb 		break;
613649010ba7SBjoern A. Zeeb 	case RX_ENC_VHT:
613749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
613849010ba7SBjoern A. Zeeb 		if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0)
613949010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
614049010ba7SBjoern A. Zeeb 		rx_stats->c_rate = rx_status->rate_idx;		/* mcs */
614149010ba7SBjoern A. Zeeb 		rx_stats->c_vhtnss = rx_status->nss;
614249010ba7SBjoern A. Zeeb 		break;
614349010ba7SBjoern A. Zeeb 	case RX_ENC_HE:
614449010ba7SBjoern A. Zeeb 	case RX_ENC_EHT:
614549010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching encoding for %u", rx_status->encoding);
614649010ba7SBjoern A. Zeeb 		break;
614749010ba7SBjoern A. Zeeb 	}
614849010ba7SBjoern A. Zeeb 
614949010ba7SBjoern A. Zeeb 	switch (rx_status->bw) {
615049010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_20:
615149010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
615249010ba7SBjoern A. Zeeb 		break;
615349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_40:
615449010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
615549010ba7SBjoern A. Zeeb 		break;
615649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_80:
615749010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
615849010ba7SBjoern A. Zeeb 		break;
615949010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_160:
616049010ba7SBjoern A. Zeeb 		rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
616149010ba7SBjoern A. Zeeb 		break;
616249010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_320:
616349010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_HE_RU:
616449010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_EHT_RU:
616549010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_5:
616649010ba7SBjoern A. Zeeb 	case RATE_INFO_BW_10:
616749010ba7SBjoern A. Zeeb 		TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
616849010ba7SBjoern A. Zeeb 		break;
616949010ba7SBjoern A. Zeeb 	}
617049010ba7SBjoern A. Zeeb 
617149010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
617249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
617349010ba7SBjoern A. Zeeb 	if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
617449010ba7SBjoern A. Zeeb 		 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
617549010ba7SBjoern A. Zeeb 
617649010ba7SBjoern A. Zeeb 	/*
617749010ba7SBjoern A. Zeeb 	 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
617849010ba7SBjoern A. Zeeb 	 * case the hardware does something we do not expect always leave
617949010ba7SBjoern A. Zeeb 	 * these enabled.  Leaving this commant as documentation for the || 1.
618049010ba7SBjoern A. Zeeb 	 */
618149010ba7SBjoern A. Zeeb #if defined(LKPI_80211_HW_CRYPTO) || 1
618249010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_DECRYPTED) {
618349010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
618449010ba7SBjoern A. Zeeb 		/* Only valid if decrypted is set. */
618549010ba7SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_PN_VALIDATED)
618649010ba7SBjoern A. Zeeb 			rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
618749010ba7SBjoern A. Zeeb 	}
618849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
618949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
619049010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MIC_STRIPPED) {
619149010ba7SBjoern A. Zeeb 		/* net80211 re-uses M[ichael]MIC for MIC too. Confusing. */
619249010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
619349010ba7SBjoern A. Zeeb 	}
619449010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_IV_STRIPPED)
619549010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
619649010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_MMIC_ERROR)
619749010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MIC;
619849010ba7SBjoern A. Zeeb 	if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
619949010ba7SBjoern A. Zeeb 		rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
620049010ba7SBjoern A. Zeeb #endif
620149010ba7SBjoern A. Zeeb }
620249010ba7SBjoern A. Zeeb 
6203e30e05d3SBjoern A. Zeeb /* For %list see comment towards the end of the function. */
62046b4cac81SBjoern A. Zeeb void
62056b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
6206e30e05d3SBjoern A. Zeeb     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
6207e30e05d3SBjoern A. Zeeb     struct list_head *list __unused)
62086b4cac81SBjoern A. Zeeb {
62096b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
62106b4cac81SBjoern A. Zeeb 	struct ieee80211com *ic;
62116b4cac81SBjoern A. Zeeb 	struct mbuf *m;
62126b4cac81SBjoern A. Zeeb 	struct skb_shared_info *shinfo;
62136b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_status *rx_status;
62146b4cac81SBjoern A. Zeeb 	struct ieee80211_rx_stats rx_stats;
62156b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
62166b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
62176b4cac81SBjoern A. Zeeb 	struct ieee80211_hdr *hdr;
62186b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
62199d9ba2b7SBjoern A. Zeeb 	int i, offset, ok;
622049010ba7SBjoern A. Zeeb 	uint8_t rssi;
6221c0cadd99SBjoern A. Zeeb 	bool is_beacon;
62226b4cac81SBjoern A. Zeeb 
62236b4cac81SBjoern A. Zeeb 	if (skb->len < 2) {
62246b4cac81SBjoern A. Zeeb 		/* Need 80211 stats here. */
62256b4cac81SBjoern A. Zeeb 		IMPROVE();
62266b4cac81SBjoern A. Zeeb 		goto err;
62276b4cac81SBjoern A. Zeeb 	}
62286b4cac81SBjoern A. Zeeb 
62296b4cac81SBjoern A. Zeeb 	/*
62306b4cac81SBjoern A. Zeeb 	 * For now do the data copy; we can later improve things. Might even
62316b4cac81SBjoern A. Zeeb 	 * have an mbuf backing the skb data then?
62326b4cac81SBjoern A. Zeeb 	 */
62336b4cac81SBjoern A. Zeeb 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
62346b4cac81SBjoern A. Zeeb 	if (m == NULL)
62356b4cac81SBjoern A. Zeeb 		goto err;
62366b4cac81SBjoern A. Zeeb 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
62376b4cac81SBjoern A. Zeeb 
62386b4cac81SBjoern A. Zeeb 	shinfo = skb_shinfo(skb);
62396b4cac81SBjoern A. Zeeb 	offset = m->m_len;
62406b4cac81SBjoern A. Zeeb 	for (i = 0; i < shinfo->nr_frags; i++) {
62416b4cac81SBjoern A. Zeeb 		m_copyback(m, offset, shinfo->frags[i].size,
62426b4cac81SBjoern A. Zeeb 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
62436b4cac81SBjoern A. Zeeb 		    shinfo->frags[i].offset);
62446b4cac81SBjoern A. Zeeb 		offset += shinfo->frags[i].size;
62456b4cac81SBjoern A. Zeeb 	}
62466b4cac81SBjoern A. Zeeb 
62476b4cac81SBjoern A. Zeeb 	rx_status = IEEE80211_SKB_RXCB(skb);
62486b4cac81SBjoern A. Zeeb 
62496b4cac81SBjoern A. Zeeb 	hdr = (void *)skb->data;
6250c0cadd99SBjoern A. Zeeb 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
6251c0cadd99SBjoern A. Zeeb 
6252c0cadd99SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
62539d9ba2b7SBjoern A. Zeeb 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
62546b4cac81SBjoern A. Zeeb 		goto no_trace_beacons;
62556b4cac81SBjoern A. Zeeb 
62569d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
62576b4cac81SBjoern A. Zeeb 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
6258c0cadd99SBjoern A. Zeeb 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
62596b4cac81SBjoern A. Zeeb 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
62606b4cac81SBjoern A. Zeeb 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
62616b4cac81SBjoern A. Zeeb 		    shinfo, shinfo->nr_frags,
6262c0cadd99SBjoern A. Zeeb 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
62636b4cac81SBjoern A. Zeeb 
62649d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
62656b4cac81SBjoern A. Zeeb 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
62666b4cac81SBjoern A. Zeeb 
62676b4cac81SBjoern A. Zeeb 	/* Implement a dump_rxcb() !!! */
62689d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6269f1aeb5d8SBjoern A. Zeeb 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
627060970a32SBjoern A. Zeeb 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
62716b4cac81SBjoern A. Zeeb 			__func__,
6272c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->boottime_ns,
6273c8dafefaSBjoern A. Zeeb 			(uintmax_t)rx_status->mactime,
62746b4cac81SBjoern A. Zeeb 			rx_status->device_timestamp,
6275f1aeb5d8SBjoern A. Zeeb 			rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
62766b4cac81SBjoern A. Zeeb 			rx_status->freq,
62776b4cac81SBjoern A. Zeeb 			rx_status->bw,
62786b4cac81SBjoern A. Zeeb 			rx_status->encoding,
62796b4cac81SBjoern A. Zeeb 			rx_status->ampdu_reference,
62806b4cac81SBjoern A. Zeeb 			rx_status->band,
62816b4cac81SBjoern A. Zeeb 			rx_status->chains,
62826b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[0],
62836b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[1],
62846b4cac81SBjoern A. Zeeb 			rx_status->chain_signal[2],
628560970a32SBjoern A. Zeeb 			rx_status->chain_signal[3],
62866b4cac81SBjoern A. Zeeb 			rx_status->signal,
62876b4cac81SBjoern A. Zeeb 			rx_status->enc_flags,
62886b4cac81SBjoern A. Zeeb 			rx_status->he_dcm,
62896b4cac81SBjoern A. Zeeb 			rx_status->he_gi,
62906b4cac81SBjoern A. Zeeb 			rx_status->he_ru,
62916b4cac81SBjoern A. Zeeb 			rx_status->zero_length_psdu_type,
62926b4cac81SBjoern A. Zeeb 			rx_status->nss,
62936b4cac81SBjoern A. Zeeb 			rx_status->rate_idx);
62946b4cac81SBjoern A. Zeeb no_trace_beacons:
62956b4cac81SBjoern A. Zeeb #endif
62966b4cac81SBjoern A. Zeeb 
629749010ba7SBjoern A. Zeeb 	rssi = 0;
629849010ba7SBjoern A. Zeeb 	lkpi_convert_rx_status(hw, rx_status, &rx_stats, &rssi);
62996b4cac81SBjoern A. Zeeb 
63006b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
63016b4cac81SBjoern A. Zeeb 	ic = lhw->ic;
63026b4cac81SBjoern A. Zeeb 
63036b4cac81SBjoern A. Zeeb 	ok = ieee80211_add_rx_params(m, &rx_stats);
63046b4cac81SBjoern A. Zeeb 	if (ok == 0) {
6305dbc06dd9SBjoern A. Zeeb 		m_freem(m);
63066b4cac81SBjoern A. Zeeb 		counter_u64_add(ic->ic_ierrors, 1);
63076b4cac81SBjoern A. Zeeb 		goto err;
63086b4cac81SBjoern A. Zeeb 	}
63096b4cac81SBjoern A. Zeeb 
631058246901SBjoern A. Zeeb 	lsta = NULL;
63116b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
63126b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
63136b4cac81SBjoern A. Zeeb 		ni = ieee80211_ref_node(lsta->ni);
63146b4cac81SBjoern A. Zeeb 	} else {
6315c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame_min *wh;
6316c8dafefaSBjoern A. Zeeb 
63176b4cac81SBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame_min *);
63186b4cac81SBjoern A. Zeeb 		ni = ieee80211_find_rxnode(ic, wh);
63196b4cac81SBjoern A. Zeeb 		if (ni != NULL)
63206b4cac81SBjoern A. Zeeb 			lsta = ni->ni_drv_data;
63216b4cac81SBjoern A. Zeeb 	}
63226b4cac81SBjoern A. Zeeb 
63236b4cac81SBjoern A. Zeeb 	if (ni != NULL)
63246b4cac81SBjoern A. Zeeb 		vap = ni->ni_vap;
63256b4cac81SBjoern A. Zeeb 	else
63266b4cac81SBjoern A. Zeeb 		/*
63276b4cac81SBjoern A. Zeeb 		 * XXX-BZ can we improve this by looking at the frame hdr
63286b4cac81SBjoern A. Zeeb 		 * or other meta-data passed up?
63296b4cac81SBjoern A. Zeeb 		 */
63306b4cac81SBjoern A. Zeeb 		vap = TAILQ_FIRST(&ic->ic_vaps);
63316b4cac81SBjoern A. Zeeb 
63329d9ba2b7SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
63339d9ba2b7SBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
6334bd206a6fSBjoern A. Zeeb 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
6335c0cadd99SBjoern A. Zeeb 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
6336c0cadd99SBjoern A. Zeeb 		    ni, vap, is_beacon ? " beacon" : "");
63379d9ba2b7SBjoern A. Zeeb #endif
63386b4cac81SBjoern A. Zeeb 
6339c0cadd99SBjoern A. Zeeb 	if (ni != NULL && vap != NULL && is_beacon &&
6340c8dafefaSBjoern A. Zeeb 	    rx_status->device_timestamp > 0 &&
6341c8dafefaSBjoern A. Zeeb 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
6342c8dafefaSBjoern A. Zeeb 		struct lkpi_vif *lvif;
6343c8dafefaSBjoern A. Zeeb 		struct ieee80211_vif *vif;
6344c8dafefaSBjoern A. Zeeb 		struct ieee80211_frame *wh;
6345c8dafefaSBjoern A. Zeeb 
6346c8dafefaSBjoern A. Zeeb 		wh = mtod(m, struct ieee80211_frame *);
6347c8dafefaSBjoern A. Zeeb 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
6348c8dafefaSBjoern A. Zeeb 			goto skip_device_ts;
6349c8dafefaSBjoern A. Zeeb 
6350c8dafefaSBjoern A. Zeeb 		lvif = VAP_TO_LVIF(vap);
6351c8dafefaSBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
6352c8dafefaSBjoern A. Zeeb 
6353c8dafefaSBjoern A. Zeeb 		IMPROVE("TIMING_BEACON_ONLY?");
6354c8dafefaSBjoern A. Zeeb 		/* mac80211 specific (not net80211) so keep it here. */
6355c8dafefaSBjoern A. Zeeb 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
6356c8dafefaSBjoern A. Zeeb 		/*
6357c8dafefaSBjoern A. Zeeb 		 * net80211 should take care of the other information (sync_tsf,
6358c8dafefaSBjoern A. Zeeb 		 * sync_dtim_count) as otherwise we need to parse the beacon.
6359c8dafefaSBjoern A. Zeeb 		 */
6360c8dafefaSBjoern A. Zeeb skip_device_ts:
63619fb91463SBjoern A. Zeeb 		;
63629fb91463SBjoern A. Zeeb 	}
6363c8dafefaSBjoern A. Zeeb 
63646b4cac81SBjoern A. Zeeb 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
63656b4cac81SBjoern A. Zeeb 	    ieee80211_radiotap_active_vap(vap)) {
63666b4cac81SBjoern A. Zeeb 		struct lkpi_radiotap_rx_hdr *rtap;
63676b4cac81SBjoern A. Zeeb 
63686b4cac81SBjoern A. Zeeb 		rtap = &lhw->rtap_rx;
63696b4cac81SBjoern A. Zeeb 		rtap->wr_tsft = rx_status->device_timestamp;
63706b4cac81SBjoern A. Zeeb 		rtap->wr_flags = 0;
63716b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
63726b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
63736b4cac81SBjoern A. Zeeb 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
63746b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
63756b4cac81SBjoern A. Zeeb #if 0	/* .. or it does not given we strip it below. */
63766b4cac81SBjoern A. Zeeb 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
63776b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
63786b4cac81SBjoern A. Zeeb #endif
63796b4cac81SBjoern A. Zeeb 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
63806b4cac81SBjoern A. Zeeb 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
63816b4cac81SBjoern A. Zeeb 		rtap->wr_rate = 0;
63826b4cac81SBjoern A. Zeeb 		IMPROVE();
63836b4cac81SBjoern A. Zeeb 		/* XXX TODO status->encoding / rate_index / bw */
63846b4cac81SBjoern A. Zeeb 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
63856b4cac81SBjoern A. Zeeb 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
63866b4cac81SBjoern A. Zeeb 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
6387170acccfSBjoern A. Zeeb 		rtap->wr_dbm_antsignal = rssi;
63886b4cac81SBjoern A. Zeeb 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
63896b4cac81SBjoern A. Zeeb 	}
63906b4cac81SBjoern A. Zeeb 
63916b4cac81SBjoern A. Zeeb 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
63926b4cac81SBjoern A. Zeeb 		m_adj(m, -IEEE80211_CRC_LEN);
63936b4cac81SBjoern A. Zeeb 
6394e30e05d3SBjoern A. Zeeb #if 0
6395e30e05d3SBjoern A. Zeeb 	if (list != NULL) {
6396e30e05d3SBjoern A. Zeeb 		/*
6397e30e05d3SBjoern A. Zeeb 		* Normally this would be queued up and delivered by
6398e30e05d3SBjoern A. Zeeb 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
6399e30e05d3SBjoern A. Zeeb 		* See mt76::mac80211.c as only current possible consumer.
6400e30e05d3SBjoern A. Zeeb 		*/
6401e30e05d3SBjoern A. Zeeb 		IMPROVE("we simply pass the packet to net80211 to deal with.");
6402e30e05d3SBjoern A. Zeeb 	}
6403e30e05d3SBjoern A. Zeeb #endif
6404e30e05d3SBjoern A. Zeeb 
6405759a996dSBjoern A. Zeeb 	/*
6406759a996dSBjoern A. Zeeb 	 * Attach meta-information to the mbuf for the deferred RX path.
6407759a996dSBjoern A. Zeeb 	 * Currently this is best-effort.  Should we need to be hard,
6408759a996dSBjoern A. Zeeb 	 * drop the frame and goto err;
6409759a996dSBjoern A. Zeeb 	 */
64106b4cac81SBjoern A. Zeeb 	if (ni != NULL) {
6411759a996dSBjoern A. Zeeb 		struct m_tag *mtag;
6412759a996dSBjoern A. Zeeb 		struct lkpi_80211_tag_rxni *rxni;
6413759a996dSBjoern A. Zeeb 
6414759a996dSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
6415759a996dSBjoern A. Zeeb 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
6416759a996dSBjoern A. Zeeb 		if (mtag != NULL) {
6417759a996dSBjoern A. Zeeb 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6418759a996dSBjoern A. Zeeb 			rxni->ni = ni;		/* We hold a reference. */
6419759a996dSBjoern A. Zeeb 			m_tag_prepend(m, mtag);
6420759a996dSBjoern A. Zeeb 		}
64216b4cac81SBjoern A. Zeeb 	}
64226b4cac81SBjoern A. Zeeb 
6423759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_LOCK(lhw);
6424759a996dSBjoern A. Zeeb 	if (lhw->rxq_stopped) {
6425759a996dSBjoern A. Zeeb 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6426759a996dSBjoern A. Zeeb 		m_freem(m);
6427759a996dSBjoern A. Zeeb 		goto err;
6428759a996dSBjoern A. Zeeb 	}
6429759a996dSBjoern A. Zeeb 
6430759a996dSBjoern A. Zeeb 	mbufq_enqueue(&lhw->rxq, m);
6431759a996dSBjoern A. Zeeb 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
6432759a996dSBjoern A. Zeeb 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
64336b4cac81SBjoern A. Zeeb 
64346b4cac81SBjoern A. Zeeb 	IMPROVE();
64356b4cac81SBjoern A. Zeeb 
64366b4cac81SBjoern A. Zeeb err:
64376b4cac81SBjoern A. Zeeb 	/* The skb is ours so we can free it :-) */
64386b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
64396b4cac81SBjoern A. Zeeb }
64406b4cac81SBjoern A. Zeeb 
64416b4cac81SBjoern A. Zeeb uint8_t
6442ec190d91SBjoern A. Zeeb linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
64436b4cac81SBjoern A. Zeeb {
64446b4cac81SBjoern A. Zeeb 	const struct ieee80211_frame *wh;
6445ec190d91SBjoern A. Zeeb 	uint8_t tid;
6446ec190d91SBjoern A. Zeeb 
6447ec190d91SBjoern A. Zeeb 	/* Linux seems to assume this is a QOS-Data-Frame */
6448ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
6449ec190d91SBjoern A. Zeeb 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
6450ec190d91SBjoern A. Zeeb 	   hdr->frame_control));
64516b4cac81SBjoern A. Zeeb 
64526b4cac81SBjoern A. Zeeb 	wh = (const struct ieee80211_frame *)hdr;
6453ec190d91SBjoern A. Zeeb 	tid = ieee80211_gettid(wh);
6454ec190d91SBjoern A. Zeeb 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
6455ec190d91SBjoern A. Zeeb 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
6456ec190d91SBjoern A. Zeeb 
6457ec190d91SBjoern A. Zeeb 	return (tid);
64586b4cac81SBjoern A. Zeeb }
64596b4cac81SBjoern A. Zeeb 
6460ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6461ac1d519cSBjoern A. Zeeb 
6462ac1d519cSBjoern A. Zeeb static void
6463ac1d519cSBjoern A. Zeeb lkpi_wiphy_work(struct work_struct *work)
6464ac1d519cSBjoern A. Zeeb {
6465ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6466ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
6467ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6468ac1d519cSBjoern A. Zeeb 
6469ac1d519cSBjoern A. Zeeb 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
6470ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6471ac1d519cSBjoern A. Zeeb 
6472ac1d519cSBjoern A. Zeeb 	wiphy_lock(wiphy);
6473ac1d519cSBjoern A. Zeeb 
6474ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6475ac1d519cSBjoern A. Zeeb 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
6476ac1d519cSBjoern A. Zeeb 	/* If there is nothing we do nothing. */
6477ac1d519cSBjoern A. Zeeb 	if (wk == NULL) {
6478ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6479ac1d519cSBjoern A. Zeeb 		wiphy_unlock(wiphy);
6480ac1d519cSBjoern A. Zeeb 		return;
6481ac1d519cSBjoern A. Zeeb 	}
6482ac1d519cSBjoern A. Zeeb 	list_del_init(&wk->entry);
6483ac1d519cSBjoern A. Zeeb 
6484ac1d519cSBjoern A. Zeeb 	/* More work to do? */
6485ac1d519cSBjoern A. Zeeb 	if (!list_empty(&lwiphy->wwk_list))
6486ac1d519cSBjoern A. Zeeb 		schedule_work(work);
6487ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6488ac1d519cSBjoern A. Zeeb 
6489ac1d519cSBjoern A. Zeeb 	/* Finally call the (*wiphy_work_fn)() function. */
6490ac1d519cSBjoern A. Zeeb 	wk->fn(wiphy, wk);
6491ac1d519cSBjoern A. Zeeb 
6492ac1d519cSBjoern A. Zeeb 	wiphy_unlock(wiphy);
6493ac1d519cSBjoern A. Zeeb }
6494ac1d519cSBjoern A. Zeeb 
6495ac1d519cSBjoern A. Zeeb void
6496ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
6497ac1d519cSBjoern A. Zeeb {
6498ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6499ac1d519cSBjoern A. Zeeb 
6500ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6501ac1d519cSBjoern A. Zeeb 
6502ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6503ac1d519cSBjoern A. Zeeb 	/* Do not double-queue. */
6504ac1d519cSBjoern A. Zeeb 	if (list_empty(&wwk->entry))
6505ac1d519cSBjoern A. Zeeb 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
6506ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6507ac1d519cSBjoern A. Zeeb 
6508ac1d519cSBjoern A. Zeeb 	/*
6509ac1d519cSBjoern A. Zeeb 	 * See how ieee80211_queue_work() work continues in Linux or if things
6510ac1d519cSBjoern A. Zeeb 	 * migrate here over time?
6511ac1d519cSBjoern A. Zeeb 	 * Use a system queue from linux/workqueue.h for now.
6512ac1d519cSBjoern A. Zeeb 	 */
6513ac1d519cSBjoern A. Zeeb 	queue_work(system_wq, &lwiphy->wwk);
6514ac1d519cSBjoern A. Zeeb }
6515ac1d519cSBjoern A. Zeeb 
6516ac1d519cSBjoern A. Zeeb void
6517ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
6518ac1d519cSBjoern A. Zeeb {
6519ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6520ac1d519cSBjoern A. Zeeb 
6521ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6522ac1d519cSBjoern A. Zeeb 
6523ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6524ac1d519cSBjoern A. Zeeb 	/* Only cancel if queued. */
6525ac1d519cSBjoern A. Zeeb 	if (!list_empty(&wwk->entry))
6526ac1d519cSBjoern A. Zeeb 		list_del_init(&wwk->entry);
6527ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6528ac1d519cSBjoern A. Zeeb }
6529ac1d519cSBjoern A. Zeeb 
6530ac1d519cSBjoern A. Zeeb void
6531ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
6532ac1d519cSBjoern A. Zeeb {
6533ac1d519cSBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6534ac1d519cSBjoern A. Zeeb 	struct wiphy_work *wk;
6535ac1d519cSBjoern A. Zeeb 
6536ac1d519cSBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6537ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6538ac1d519cSBjoern A. Zeeb 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
6539ac1d519cSBjoern A. Zeeb 	if (wwk != NULL && list_empty(&wwk->entry)) {
6540ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6541ac1d519cSBjoern A. Zeeb 		return;
6542ac1d519cSBjoern A. Zeeb 	}
6543ac1d519cSBjoern A. Zeeb 
6544ac1d519cSBjoern A. Zeeb 	while (!list_empty(&lwiphy->wwk_list)) {
6545ac1d519cSBjoern A. Zeeb 
6546ac1d519cSBjoern A. Zeeb 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
6547ac1d519cSBjoern A. Zeeb 		    entry);
6548ac1d519cSBjoern A. Zeeb 		list_del_init(&wk->entry);
6549ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6550ac1d519cSBjoern A. Zeeb 		wk->fn(wiphy, wk);
6551ac1d519cSBjoern A. Zeeb 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
6552ac1d519cSBjoern A. Zeeb 		if (wk == wwk)
6553ac1d519cSBjoern A. Zeeb 			break;
6554ac1d519cSBjoern A. Zeeb 	}
6555ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
6556ac1d519cSBjoern A. Zeeb }
6557ac1d519cSBjoern A. Zeeb 
6558ac1d519cSBjoern A. Zeeb void
6559ac1d519cSBjoern A. Zeeb lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
6560ac1d519cSBjoern A. Zeeb {
6561ac1d519cSBjoern A. Zeeb 	struct wiphy_delayed_work *wdwk;
6562ac1d519cSBjoern A. Zeeb 
6563ac1d519cSBjoern A. Zeeb 	wdwk = from_timer(wdwk, tl, timer);
6564ac1d519cSBjoern A. Zeeb         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
6565ac1d519cSBjoern A. Zeeb }
6566ac1d519cSBjoern A. Zeeb 
6567ac1d519cSBjoern A. Zeeb void
6568ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
6569ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk, unsigned long delay)
6570ac1d519cSBjoern A. Zeeb {
6571ac1d519cSBjoern A. Zeeb 	if (delay == 0) {
6572ac1d519cSBjoern A. Zeeb 		/* Run right away. */
6573ac1d519cSBjoern A. Zeeb 		del_timer(&wdwk->timer);
6574ac1d519cSBjoern A. Zeeb 		wiphy_work_queue(wiphy, &wdwk->work);
6575ac1d519cSBjoern A. Zeeb 	} else {
6576ac1d519cSBjoern A. Zeeb 		wdwk->wiphy = wiphy;
6577ac1d519cSBjoern A. Zeeb 		mod_timer(&wdwk->timer, jiffies + delay);
6578ac1d519cSBjoern A. Zeeb 	}
6579ac1d519cSBjoern A. Zeeb }
6580ac1d519cSBjoern A. Zeeb 
6581ac1d519cSBjoern A. Zeeb void
6582ac1d519cSBjoern A. Zeeb linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
6583ac1d519cSBjoern A. Zeeb     struct wiphy_delayed_work *wdwk)
6584ac1d519cSBjoern A. Zeeb {
6585ac1d519cSBjoern A. Zeeb 	del_timer_sync(&wdwk->timer);
6586ac1d519cSBjoern A. Zeeb 	wiphy_work_cancel(wiphy, &wdwk->work);
6587ac1d519cSBjoern A. Zeeb }
6588ac1d519cSBjoern A. Zeeb 
6589ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
6590ac1d519cSBjoern A. Zeeb 
65916b4cac81SBjoern A. Zeeb struct wiphy *
65926b4cac81SBjoern A. Zeeb linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
65936b4cac81SBjoern A. Zeeb {
65946b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
6595ac1d519cSBjoern A. Zeeb 	struct wiphy *wiphy;
65966b4cac81SBjoern A. Zeeb 
65976b4cac81SBjoern A. Zeeb 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
65986b4cac81SBjoern A. Zeeb 	if (lwiphy == NULL)
65996b4cac81SBjoern A. Zeeb 		return (NULL);
66006b4cac81SBjoern A. Zeeb 	lwiphy->ops = ops;
66016b4cac81SBjoern A. Zeeb 
6602ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
6603ac1d519cSBjoern A. Zeeb 	INIT_LIST_HEAD(&lwiphy->wwk_list);
6604ac1d519cSBjoern A. Zeeb 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
6605ac1d519cSBjoern A. Zeeb 
6606ac1d519cSBjoern A. Zeeb 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
6607ac1d519cSBjoern A. Zeeb 
6608ac1d519cSBjoern A. Zeeb 	mutex_init(&wiphy->mtx);
6609ac1d519cSBjoern A. Zeeb 	TODO();
6610ac1d519cSBjoern A. Zeeb 
6611ac1d519cSBjoern A. Zeeb 	return (wiphy);
66126b4cac81SBjoern A. Zeeb }
66136b4cac81SBjoern A. Zeeb 
66146b4cac81SBjoern A. Zeeb void
66156b4cac81SBjoern A. Zeeb linuxkpi_wiphy_free(struct wiphy *wiphy)
66166b4cac81SBjoern A. Zeeb {
66176b4cac81SBjoern A. Zeeb 	struct lkpi_wiphy *lwiphy;
66186b4cac81SBjoern A. Zeeb 
66196b4cac81SBjoern A. Zeeb 	if (wiphy == NULL)
66206b4cac81SBjoern A. Zeeb 		return;
66216b4cac81SBjoern A. Zeeb 
6622ac1d519cSBjoern A. Zeeb 	linuxkpi_wiphy_work_flush(wiphy, NULL);
6623ac1d519cSBjoern A. Zeeb 	mutex_destroy(&wiphy->mtx);
6624ac1d519cSBjoern A. Zeeb 
66256b4cac81SBjoern A. Zeeb 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
6626ac1d519cSBjoern A. Zeeb 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
6627ac1d519cSBjoern A. Zeeb 
66286b4cac81SBjoern A. Zeeb 	kfree(lwiphy);
66296b4cac81SBjoern A. Zeeb }
66306b4cac81SBjoern A. Zeeb 
6631a7c19b8aSBjoern A. Zeeb static uint32_t
6632a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
6633a7c19b8aSBjoern A. Zeeb {
6634a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_ht");
6635a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6636a7c19b8aSBjoern A. Zeeb }
6637a7c19b8aSBjoern A. Zeeb 
6638a7c19b8aSBjoern A. Zeeb static uint32_t
6639a7c19b8aSBjoern A. Zeeb lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
6640a7c19b8aSBjoern A. Zeeb {
6641a7c19b8aSBjoern A. Zeeb 	TODO("cfg80211_calculate_bitrate_vht");
6642a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6643a7c19b8aSBjoern A. Zeeb }
6644a7c19b8aSBjoern A. Zeeb 
6645a7c19b8aSBjoern A. Zeeb uint32_t
6646a7c19b8aSBjoern A. Zeeb linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
6647a7c19b8aSBjoern A. Zeeb {
6648a7c19b8aSBjoern A. Zeeb 
6649a7c19b8aSBjoern A. Zeeb 	/* Beware: order! */
6650a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_MCS)
6651a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_ht(rate));
6652a7c19b8aSBjoern A. Zeeb 
6653a7c19b8aSBjoern A. Zeeb 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
6654a7c19b8aSBjoern A. Zeeb 		return (lkpi_cfg80211_calculate_bitrate_vht(rate));
6655a7c19b8aSBjoern A. Zeeb 
6656a7c19b8aSBjoern A. Zeeb 	IMPROVE("HE/EHT/...");
6657a7c19b8aSBjoern A. Zeeb 
6658a7c19b8aSBjoern A. Zeeb 	return (rate->legacy);
6659a7c19b8aSBjoern A. Zeeb }
6660a7c19b8aSBjoern A. Zeeb 
66616b4cac81SBjoern A. Zeeb uint32_t
66626b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
66636b4cac81SBjoern A. Zeeb     enum nl80211_band band)
66646b4cac81SBjoern A. Zeeb {
66656b4cac81SBjoern A. Zeeb 
66666b4cac81SBjoern A. Zeeb 	switch (band) {
66676b4cac81SBjoern A. Zeeb 	case NL80211_BAND_2GHZ:
66686b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
66696b4cac81SBjoern A. Zeeb 		break;
66706b4cac81SBjoern A. Zeeb 	case NL80211_BAND_5GHZ:
66716b4cac81SBjoern A. Zeeb 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
66726b4cac81SBjoern A. Zeeb 		break;
66736b4cac81SBjoern A. Zeeb 	default:
66746b4cac81SBjoern A. Zeeb 		/* XXX abort, retry, error, panic? */
66756b4cac81SBjoern A. Zeeb 		break;
66766b4cac81SBjoern A. Zeeb 	}
66776b4cac81SBjoern A. Zeeb 
66786b4cac81SBjoern A. Zeeb 	return (0);
66796b4cac81SBjoern A. Zeeb }
66806b4cac81SBjoern A. Zeeb 
66816b4cac81SBjoern A. Zeeb uint32_t
66826b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
66836b4cac81SBjoern A. Zeeb {
66846b4cac81SBjoern A. Zeeb 
66856b4cac81SBjoern A. Zeeb 	return (ieee80211_mhz2ieee(freq, 0));
66866b4cac81SBjoern A. Zeeb }
66876b4cac81SBjoern A. Zeeb 
6688ac867c20SBjoern A. Zeeb #if 0
66896b4cac81SBjoern A. Zeeb static struct lkpi_sta *
66906b4cac81SBjoern A. Zeeb lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
66916b4cac81SBjoern A. Zeeb {
66926b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta, *temp;
66936b4cac81SBjoern A. Zeeb 
6694a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6695a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
66966b4cac81SBjoern A. Zeeb 		if (lsta->ni == ni) {
6697a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
66986b4cac81SBjoern A. Zeeb 			return (lsta);
66996b4cac81SBjoern A. Zeeb 		}
67006b4cac81SBjoern A. Zeeb 	}
6701a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67026b4cac81SBjoern A. Zeeb 
67036b4cac81SBjoern A. Zeeb 	return (NULL);
67046b4cac81SBjoern A. Zeeb }
6705ac867c20SBjoern A. Zeeb #endif
67066b4cac81SBjoern A. Zeeb 
67076b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67086b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
67096b4cac81SBjoern A. Zeeb {
67106b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
6711a8f735a6SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67126b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67136b4cac81SBjoern A. Zeeb 
67146b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
67156b4cac81SBjoern A. Zeeb 
6716a8f735a6SBjoern A. Zeeb 	rcu_read_lock();
6717a8f735a6SBjoern A. Zeeb 	list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
67186b4cac81SBjoern A. Zeeb 		sta = LSTA_TO_STA(lsta);
67196b4cac81SBjoern A. Zeeb 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
6720a8f735a6SBjoern A. Zeeb 			rcu_read_unlock();
67216b4cac81SBjoern A. Zeeb 			return (sta);
67226b4cac81SBjoern A. Zeeb 		}
67236b4cac81SBjoern A. Zeeb 	}
6724a8f735a6SBjoern A. Zeeb 	rcu_read_unlock();
67256b4cac81SBjoern A. Zeeb 	return (NULL);
67266b4cac81SBjoern A. Zeeb }
67276b4cac81SBjoern A. Zeeb 
67286b4cac81SBjoern A. Zeeb struct ieee80211_sta *
67292e183d99SBjoern A. Zeeb linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
67302e183d99SBjoern A. Zeeb     const uint8_t *addr, const uint8_t *ourvifaddr)
67316b4cac81SBjoern A. Zeeb {
67326b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
67336b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67346b4cac81SBjoern A. Zeeb 	struct lkpi_sta *lsta;
67356b4cac81SBjoern A. Zeeb 	struct ieee80211_vif *vif;
67366b4cac81SBjoern A. Zeeb 	struct ieee80211_sta *sta;
67376b4cac81SBjoern A. Zeeb 
67386b4cac81SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
67396b4cac81SBjoern A. Zeeb 	sta = NULL;
67406b4cac81SBjoern A. Zeeb 
67418891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
67426b4cac81SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
67436b4cac81SBjoern A. Zeeb 
67446b4cac81SBjoern A. Zeeb 		/* XXX-BZ check our address from the vif. */
67456b4cac81SBjoern A. Zeeb 
67466b4cac81SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
67476b4cac81SBjoern A. Zeeb 		if (ourvifaddr != NULL &&
67486b4cac81SBjoern A. Zeeb 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
67496b4cac81SBjoern A. Zeeb 			continue;
67506b4cac81SBjoern A. Zeeb 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
67516b4cac81SBjoern A. Zeeb 		if (sta != NULL)
67526b4cac81SBjoern A. Zeeb 			break;
67536b4cac81SBjoern A. Zeeb 	}
67548891c455SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
67556b4cac81SBjoern A. Zeeb 
67566b4cac81SBjoern A. Zeeb 	if (sta != NULL) {
67576b4cac81SBjoern A. Zeeb 		lsta = STA_TO_LSTA(sta);
67586b4cac81SBjoern A. Zeeb 		if (!lsta->added_to_drv)
67596b4cac81SBjoern A. Zeeb 			return (NULL);
67606b4cac81SBjoern A. Zeeb 	}
67616b4cac81SBjoern A. Zeeb 
67626b4cac81SBjoern A. Zeeb 	return (sta);
67636b4cac81SBjoern A. Zeeb }
67646b4cac81SBjoern A. Zeeb 
67656b4cac81SBjoern A. Zeeb struct sk_buff *
67666b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
67676b4cac81SBjoern A. Zeeb     struct ieee80211_txq *txq)
67686b4cac81SBjoern A. Zeeb {
67696b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
67700cbcfa19SBjoern A. Zeeb 	struct lkpi_vif *lvif;
67716b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
67726b4cac81SBjoern A. Zeeb 
67730cbcfa19SBjoern A. Zeeb 	skb = NULL;
67746b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
67756b4cac81SBjoern A. Zeeb 	ltxq->seen_dequeue = true;
67766b4cac81SBjoern A. Zeeb 
67770cbcfa19SBjoern A. Zeeb 	if (ltxq->stopped)
67780cbcfa19SBjoern A. Zeeb 		goto stopped;
67790cbcfa19SBjoern A. Zeeb 
67800cbcfa19SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
67810cbcfa19SBjoern A. Zeeb 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
67820cbcfa19SBjoern A. Zeeb 		ltxq->stopped = true;
67830cbcfa19SBjoern A. Zeeb 		goto stopped;
67840cbcfa19SBjoern A. Zeeb 	}
67850cbcfa19SBjoern A. Zeeb 
6786eac3646fSBjoern A. Zeeb 	IMPROVE("hw(TX_FRAG_LIST)");
6787eac3646fSBjoern A. Zeeb 
6788eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
67896b4cac81SBjoern A. Zeeb 	skb = skb_dequeue(&ltxq->skbq);
6790eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
67916b4cac81SBjoern A. Zeeb 
67920cbcfa19SBjoern A. Zeeb stopped:
67936b4cac81SBjoern A. Zeeb 	return (skb);
67946b4cac81SBjoern A. Zeeb }
67956b4cac81SBjoern A. Zeeb 
67966b4cac81SBjoern A. Zeeb void
67976b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
679886220d3cSBjoern A. Zeeb     unsigned long *frame_cnt, unsigned long *byte_cnt)
67996b4cac81SBjoern A. Zeeb {
68006b4cac81SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
68016b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
680286220d3cSBjoern A. Zeeb 	unsigned long fc, bc;
68036b4cac81SBjoern A. Zeeb 
68046b4cac81SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
68056b4cac81SBjoern A. Zeeb 
68066b4cac81SBjoern A. Zeeb 	fc = bc = 0;
6807eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
68086b4cac81SBjoern A. Zeeb 	skb_queue_walk(&ltxq->skbq, skb) {
68096b4cac81SBjoern A. Zeeb 		fc++;
68106b4cac81SBjoern A. Zeeb 		bc += skb->len;
68116b4cac81SBjoern A. Zeeb 	}
6812eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
68136b4cac81SBjoern A. Zeeb 	if (frame_cnt)
68146b4cac81SBjoern A. Zeeb 		*frame_cnt = fc;
68156b4cac81SBjoern A. Zeeb 	if (byte_cnt)
68166b4cac81SBjoern A. Zeeb 		*byte_cnt = bc;
68176b4cac81SBjoern A. Zeeb 
68186b4cac81SBjoern A. Zeeb 	/* Validate that this is doing the correct thing. */
68196b4cac81SBjoern A. Zeeb 	/* Should we keep track on en/dequeue? */
68206b4cac81SBjoern A. Zeeb 	IMPROVE();
68216b4cac81SBjoern A. Zeeb }
68226b4cac81SBjoern A. Zeeb 
68236b4cac81SBjoern A. Zeeb /*
68246b4cac81SBjoern A. Zeeb  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
68256b4cac81SBjoern A. Zeeb  * The latter tries to derive the success status from the info flags
68266b4cac81SBjoern A. Zeeb  * passed back from the driver.  rawx_mit() saves the ni on the m and the
68276b4cac81SBjoern A. Zeeb  * m on the skb for us to be able to give feedback to net80211.
68286b4cac81SBjoern A. Zeeb  */
6829a8397571SBjoern A. Zeeb static void
6830a8397571SBjoern A. Zeeb _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
68316b4cac81SBjoern A. Zeeb     int status)
68326b4cac81SBjoern A. Zeeb {
68336b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
68346b4cac81SBjoern A. Zeeb 	struct mbuf *m;
68356b4cac81SBjoern A. Zeeb 
68366b4cac81SBjoern A. Zeeb 	m = skb->m;
68376b4cac81SBjoern A. Zeeb 	skb->m = NULL;
68386b4cac81SBjoern A. Zeeb 
68396b4cac81SBjoern A. Zeeb 	if (m != NULL) {
68406b4cac81SBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
68416b4cac81SBjoern A. Zeeb 		/* Status: 0 is ok, != 0 is error. */
68426b4cac81SBjoern A. Zeeb 		ieee80211_tx_complete(ni, m, status);
68436b4cac81SBjoern A. Zeeb 		/* ni & mbuf were consumed. */
68446b4cac81SBjoern A. Zeeb 	}
6845a8397571SBjoern A. Zeeb }
68466b4cac81SBjoern A. Zeeb 
6847a8397571SBjoern A. Zeeb void
6848a8397571SBjoern A. Zeeb linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
6849a8397571SBjoern A. Zeeb     int status)
6850a8397571SBjoern A. Zeeb {
6851a8397571SBjoern A. Zeeb 
6852a8397571SBjoern A. Zeeb 	_lkpi_ieee80211_free_txskb(hw, skb, status);
68536b4cac81SBjoern A. Zeeb 	kfree_skb(skb);
68546b4cac81SBjoern A. Zeeb }
68556b4cac81SBjoern A. Zeeb 
6856383b3e8fSBjoern A. Zeeb void
6857a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
6858a8397571SBjoern A. Zeeb     struct ieee80211_tx_status *txstat)
6859383b3e8fSBjoern A. Zeeb {
6860a8397571SBjoern A. Zeeb 	struct sk_buff *skb;
6861383b3e8fSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
6862383b3e8fSBjoern A. Zeeb 	struct ieee80211_ratectl_tx_status txs;
6863383b3e8fSBjoern A. Zeeb 	struct ieee80211_node *ni;
6864383b3e8fSBjoern A. Zeeb 	int status;
6865383b3e8fSBjoern A. Zeeb 
6866a8397571SBjoern A. Zeeb 	skb = txstat->skb;
6867383b3e8fSBjoern A. Zeeb 	if (skb->m != NULL) {
6868383b3e8fSBjoern A. Zeeb 		struct mbuf *m;
6869383b3e8fSBjoern A. Zeeb 
6870383b3e8fSBjoern A. Zeeb 		m = skb->m;
6871383b3e8fSBjoern A. Zeeb 		ni = m->m_pkthdr.PH_loc.ptr;
6872383b3e8fSBjoern A. Zeeb 		memset(&txs, 0, sizeof(txs));
6873383b3e8fSBjoern A. Zeeb 	} else {
6874383b3e8fSBjoern A. Zeeb 		ni = NULL;
6875383b3e8fSBjoern A. Zeeb 	}
6876383b3e8fSBjoern A. Zeeb 
6877a8397571SBjoern A. Zeeb 	info = txstat->info;
6878383b3e8fSBjoern A. Zeeb 	if (info->flags & IEEE80211_TX_STAT_ACK) {
6879383b3e8fSBjoern A. Zeeb 		status = 0;	/* No error. */
6880383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
6881383b3e8fSBjoern A. Zeeb 	} else {
6882383b3e8fSBjoern A. Zeeb 		status = 1;
6883383b3e8fSBjoern A. Zeeb 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
6884383b3e8fSBjoern A. Zeeb 	}
6885383b3e8fSBjoern A. Zeeb 
6886383b3e8fSBjoern A. Zeeb 	if (ni != NULL) {
6887e2c5ab09SJohn Baldwin 		int ridx __unused;
6888383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6889383b3e8fSBjoern A. Zeeb 		int old_rate;
6890383b3e8fSBjoern A. Zeeb 
6891383b3e8fSBjoern A. Zeeb 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
6892383b3e8fSBjoern A. Zeeb #endif
6893383b3e8fSBjoern A. Zeeb 		txs.pktlen = skb->len;
6894383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
6895383b3e8fSBjoern A. Zeeb 		if (info->status.rates[0].count > 1) {
6896383b3e8fSBjoern A. Zeeb 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
6897383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
6898383b3e8fSBjoern A. Zeeb 		}
6899383b3e8fSBjoern A. Zeeb #if 0		/* Unused in net80211 currently. */
6900a8397571SBjoern A. Zeeb 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
6901383b3e8fSBjoern A. Zeeb 		txs.final_rate = info->status.rates[0].idx;
6902383b3e8fSBjoern A. Zeeb 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
6903383b3e8fSBjoern A. Zeeb #endif
6904adff403fSBjoern A. Zeeb 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
6905383b3e8fSBjoern A. Zeeb 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
6906383b3e8fSBjoern A. Zeeb 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
6907383b3e8fSBjoern A. Zeeb 		}
6908383b3e8fSBjoern A. Zeeb 
6909383b3e8fSBjoern A. Zeeb 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
6910383b3e8fSBjoern A. Zeeb 		ieee80211_ratectl_tx_complete(ni, &txs);
6911383b3e8fSBjoern A. Zeeb 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
6912383b3e8fSBjoern A. Zeeb 
6913383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6914383b3e8fSBjoern A. Zeeb 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
6915383b3e8fSBjoern A. Zeeb 			printf("TX-RATE: %s: old %d new %d ridx %d, "
6916383b3e8fSBjoern A. Zeeb 			    "long_retries %d\n", __func__,
6917383b3e8fSBjoern A. Zeeb 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
6918383b3e8fSBjoern A. Zeeb 			    ridx, txs.long_retries);
6919383b3e8fSBjoern A. Zeeb 		}
6920383b3e8fSBjoern A. Zeeb #endif
6921383b3e8fSBjoern A. Zeeb 	}
6922383b3e8fSBjoern A. Zeeb 
6923383b3e8fSBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
6924383b3e8fSBjoern A. Zeeb 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6925383b3e8fSBjoern A. Zeeb 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
6926383b3e8fSBjoern A. Zeeb 		    "band %u hw_queue %u tx_time_est %d : "
6927383b3e8fSBjoern A. Zeeb 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
6928383b3e8fSBjoern A. Zeeb 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
6929adff403fSBjoern A. Zeeb 		    "tx_time %u flags %#x "
6930383b3e8fSBjoern A. Zeeb 		    "status_driver_data [ %p %p ]\n",
6931383b3e8fSBjoern A. Zeeb 		    __func__, hw, skb, status, info->flags,
6932383b3e8fSBjoern A. Zeeb 		    info->band, info->hw_queue, info->tx_time_est,
6933383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].idx, info->status.rates[0].count,
6934383b3e8fSBjoern A. Zeeb 		    info->status.rates[0].flags,
6935383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].idx, info->status.rates[1].count,
6936383b3e8fSBjoern A. Zeeb 		    info->status.rates[1].flags,
6937383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].idx, info->status.rates[2].count,
6938383b3e8fSBjoern A. Zeeb 		    info->status.rates[2].flags,
6939383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].idx, info->status.rates[3].count,
6940383b3e8fSBjoern A. Zeeb 		    info->status.rates[3].flags,
6941383b3e8fSBjoern A. Zeeb 		    info->status.ack_signal, info->status.ampdu_ack_len,
6942383b3e8fSBjoern A. Zeeb 		    info->status.ampdu_len, info->status.antenna,
6943adff403fSBjoern A. Zeeb 		    info->status.tx_time, info->status.flags,
6944383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[0],
6945383b3e8fSBjoern A. Zeeb 		    info->status.status_driver_data[1]);
6946383b3e8fSBjoern A. Zeeb #endif
6947383b3e8fSBjoern A. Zeeb 
6948a8397571SBjoern A. Zeeb 	if (txstat->free_list) {
6949a8397571SBjoern A. Zeeb 		_lkpi_ieee80211_free_txskb(hw, skb, status);
6950a8397571SBjoern A. Zeeb 		list_add_tail(&skb->list, txstat->free_list);
6951a8397571SBjoern A. Zeeb 	} else {
6952383b3e8fSBjoern A. Zeeb 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
6953383b3e8fSBjoern A. Zeeb 	}
6954a8397571SBjoern A. Zeeb }
6955a8397571SBjoern A. Zeeb 
6956a8397571SBjoern A. Zeeb void
6957a8397571SBjoern A. Zeeb linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
6958a8397571SBjoern A. Zeeb {
6959a8397571SBjoern A. Zeeb 	struct ieee80211_tx_status status;
6960a8397571SBjoern A. Zeeb 
6961a8397571SBjoern A. Zeeb 	memset(&status, 0, sizeof(status));
6962a8397571SBjoern A. Zeeb 	status.info = IEEE80211_SKB_CB(skb);
6963a8397571SBjoern A. Zeeb 	status.skb = skb;
6964a8397571SBjoern A. Zeeb 	/* sta, n_rates, rates, free_list? */
6965a8397571SBjoern A. Zeeb 
6966a8397571SBjoern A. Zeeb 	ieee80211_tx_status_ext(hw, &status);
6967a8397571SBjoern A. Zeeb }
6968383b3e8fSBjoern A. Zeeb 
69696b4cac81SBjoern A. Zeeb /*
69706b4cac81SBjoern A. Zeeb  * This is an internal bandaid for the moment for the way we glue
69716b4cac81SBjoern A. Zeeb  * skbs and mbufs together for TX.  Once we have skbs backed by
69726b4cac81SBjoern A. Zeeb  * mbufs this should go away.
69736b4cac81SBjoern A. Zeeb  * This is a public function but kept on the private KPI (lkpi_)
69746b4cac81SBjoern A. Zeeb  * and is not exposed by a header file.
69756b4cac81SBjoern A. Zeeb  */
69766b4cac81SBjoern A. Zeeb static void
69776b4cac81SBjoern A. Zeeb lkpi_ieee80211_free_skb_mbuf(void *p)
69786b4cac81SBjoern A. Zeeb {
69796b4cac81SBjoern A. Zeeb 	struct ieee80211_node *ni;
69806b4cac81SBjoern A. Zeeb 	struct mbuf *m;
69816b4cac81SBjoern A. Zeeb 
69826b4cac81SBjoern A. Zeeb 	if (p == NULL)
69836b4cac81SBjoern A. Zeeb 		return;
69846b4cac81SBjoern A. Zeeb 
69856b4cac81SBjoern A. Zeeb 	m = (struct mbuf *)p;
69866b4cac81SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
69876b4cac81SBjoern A. Zeeb 
69886b4cac81SBjoern A. Zeeb 	ni = m->m_pkthdr.PH_loc.ptr;
69896b4cac81SBjoern A. Zeeb 	m->m_pkthdr.PH_loc.ptr = NULL;
69906b4cac81SBjoern A. Zeeb 	if (ni != NULL)
69916b4cac81SBjoern A. Zeeb 		ieee80211_free_node(ni);
69926b4cac81SBjoern A. Zeeb 	m_freem(m);
69936b4cac81SBjoern A. Zeeb }
69946b4cac81SBjoern A. Zeeb 
69956b4cac81SBjoern A. Zeeb void
69966b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
69976b4cac81SBjoern A. Zeeb     struct delayed_work *w, int delay)
69986b4cac81SBjoern A. Zeeb {
69996b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70006b4cac81SBjoern A. Zeeb 
70016b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70026b4cac81SBjoern A. Zeeb 	IMPROVE();
70036b4cac81SBjoern A. Zeeb 
70046b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70056b4cac81SBjoern A. Zeeb 	queue_delayed_work(lhw->workq, w, delay);
70066b4cac81SBjoern A. Zeeb }
70076b4cac81SBjoern A. Zeeb 
70086b4cac81SBjoern A. Zeeb void
70096b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
70106b4cac81SBjoern A. Zeeb     struct work_struct *w)
70116b4cac81SBjoern A. Zeeb {
70126b4cac81SBjoern A. Zeeb 	struct lkpi_hw *lhw;
70136b4cac81SBjoern A. Zeeb 
70146b4cac81SBjoern A. Zeeb 	/* Need to make sure hw is in a stable (non-suspended) state. */
70156b4cac81SBjoern A. Zeeb 	IMPROVE();
70166b4cac81SBjoern A. Zeeb 
70176b4cac81SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
70186b4cac81SBjoern A. Zeeb 	queue_work(lhw->workq, w);
70196b4cac81SBjoern A. Zeeb }
70206b4cac81SBjoern A. Zeeb 
70216b4cac81SBjoern A. Zeeb struct sk_buff *
7022ade774b1SBjoern A. Zeeb linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
7023ade774b1SBjoern A. Zeeb     uint8_t *ssid, size_t ssid_len, size_t tailroom)
7024ade774b1SBjoern A. Zeeb {
7025ade774b1SBjoern A. Zeeb 	struct sk_buff *skb;
7026ade774b1SBjoern A. Zeeb 	struct ieee80211_frame *wh;
7027ade774b1SBjoern A. Zeeb 	uint8_t *p;
7028ade774b1SBjoern A. Zeeb 	size_t len;
7029ade774b1SBjoern A. Zeeb 
7030ade774b1SBjoern A. Zeeb 	len = sizeof(*wh);
7031ade774b1SBjoern A. Zeeb 	len += 2 + ssid_len;
7032ade774b1SBjoern A. Zeeb 
7033ade774b1SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
7034ade774b1SBjoern A. Zeeb 	if (skb == NULL)
7035ade774b1SBjoern A. Zeeb 		return (NULL);
7036ade774b1SBjoern A. Zeeb 
7037ade774b1SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
7038ade774b1SBjoern A. Zeeb 
7039ade774b1SBjoern A. Zeeb 	wh = skb_put_zero(skb, sizeof(*wh));
7040ade774b1SBjoern A. Zeeb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
7041ade774b1SBjoern A. Zeeb 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
7042ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
7043ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
7044ade774b1SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
7045ade774b1SBjoern A. Zeeb 
7046ade774b1SBjoern A. Zeeb 	p = skb_put(skb, 2 + ssid_len);
7047ade774b1SBjoern A. Zeeb 	*p++ = IEEE80211_ELEMID_SSID;
7048ade774b1SBjoern A. Zeeb 	*p++ = ssid_len;
7049ade774b1SBjoern A. Zeeb 	if (ssid_len > 0)
7050ade774b1SBjoern A. Zeeb 		memcpy(p, ssid, ssid_len);
7051ade774b1SBjoern A. Zeeb 
7052ade774b1SBjoern A. Zeeb 	return (skb);
7053ade774b1SBjoern A. Zeeb }
7054ade774b1SBjoern A. Zeeb 
7055ade774b1SBjoern A. Zeeb struct sk_buff *
70566b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
70576b4cac81SBjoern A. Zeeb     struct ieee80211_vif *vif)
70586b4cac81SBjoern A. Zeeb {
70596b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70606b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70616b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70626b4cac81SBjoern A. Zeeb 	struct ieee80211_frame_pspoll *psp;
70636b4cac81SBjoern A. Zeeb 	uint16_t v;
70646b4cac81SBjoern A. Zeeb 
70656b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
70666b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70676b4cac81SBjoern A. Zeeb 		return (NULL);
70686b4cac81SBjoern A. Zeeb 
70696b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
70706b4cac81SBjoern A. Zeeb 
70716b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
70726b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
70736b4cac81SBjoern A. Zeeb 
70746b4cac81SBjoern A. Zeeb 	psp = skb_put_zero(skb, sizeof(*psp));
70756b4cac81SBjoern A. Zeeb 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
70766b4cac81SBjoern A. Zeeb 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
7077616e1330SBjoern A. Zeeb 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
70786b4cac81SBjoern A. Zeeb 	memcpy(&psp->i_aid, &v, sizeof(v));
70796b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
70806b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
70816b4cac81SBjoern A. Zeeb 
70826b4cac81SBjoern A. Zeeb 	return (skb);
70836b4cac81SBjoern A. Zeeb }
70846b4cac81SBjoern A. Zeeb 
70856b4cac81SBjoern A. Zeeb struct sk_buff *
70866b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7087adff403fSBjoern A. Zeeb     struct ieee80211_vif *vif, int linkid, bool qos)
70886b4cac81SBjoern A. Zeeb {
70896b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
70906b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
70916b4cac81SBjoern A. Zeeb 	struct sk_buff *skb;
70926b4cac81SBjoern A. Zeeb 	struct ieee80211_frame *nullf;
70936b4cac81SBjoern A. Zeeb 
7094adff403fSBjoern A. Zeeb 	IMPROVE("linkid");
7095adff403fSBjoern A. Zeeb 
70966b4cac81SBjoern A. Zeeb 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
70976b4cac81SBjoern A. Zeeb 	if (skb == NULL)
70986b4cac81SBjoern A. Zeeb 		return (NULL);
70996b4cac81SBjoern A. Zeeb 
71006b4cac81SBjoern A. Zeeb 	skb_reserve(skb, hw->extra_tx_headroom);
71016b4cac81SBjoern A. Zeeb 
71026b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71036b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71046b4cac81SBjoern A. Zeeb 
71056b4cac81SBjoern A. Zeeb 	nullf = skb_put_zero(skb, sizeof(*nullf));
71066b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
71076b4cac81SBjoern A. Zeeb 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
71086b4cac81SBjoern A. Zeeb 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
71096b4cac81SBjoern A. Zeeb 
71106b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
71116b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
71126b4cac81SBjoern A. Zeeb 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
71136b4cac81SBjoern A. Zeeb 
71146b4cac81SBjoern A. Zeeb 	return (skb);
71156b4cac81SBjoern A. Zeeb }
71166b4cac81SBjoern A. Zeeb 
71176b4cac81SBjoern A. Zeeb struct wireless_dev *
71186b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
71196b4cac81SBjoern A. Zeeb {
71206b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71216b4cac81SBjoern A. Zeeb 
71226b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71236b4cac81SBjoern A. Zeeb 	return (&lvif->wdev);
71246b4cac81SBjoern A. Zeeb }
71256b4cac81SBjoern A. Zeeb 
71266b4cac81SBjoern A. Zeeb void
71276b4cac81SBjoern A. Zeeb linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
71286b4cac81SBjoern A. Zeeb {
71296b4cac81SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71306b4cac81SBjoern A. Zeeb 	struct ieee80211vap *vap;
71316b4cac81SBjoern A. Zeeb 	enum ieee80211_state nstate;
71326b4cac81SBjoern A. Zeeb 	int arg;
71336b4cac81SBjoern A. Zeeb 
71346b4cac81SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
71356b4cac81SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
71366b4cac81SBjoern A. Zeeb 
71376b4cac81SBjoern A. Zeeb 	/*
7138f3229b62SBjoern A. Zeeb 	 * Go to init; otherwise we need to elaborately check state and
71396b4cac81SBjoern A. Zeeb 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
71406b4cac81SBjoern A. Zeeb 	 * Let the statemachine handle all neccessary changes.
71416b4cac81SBjoern A. Zeeb 	 */
7142f3229b62SBjoern A. Zeeb 	nstate = IEEE80211_S_INIT;
7143bb81db90SBjoern A. Zeeb 	arg = 0;	/* Not a valid reason. */
71446b4cac81SBjoern A. Zeeb 
7145018d93ecSBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7146018d93ecSBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71476b4cac81SBjoern A. Zeeb 	ieee80211_new_state(vap, nstate, arg);
71486b4cac81SBjoern A. Zeeb }
71496b4cac81SBjoern A. Zeeb 
7150bb81db90SBjoern A. Zeeb void
7151bb81db90SBjoern A. Zeeb linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
7152bb81db90SBjoern A. Zeeb {
7153bb81db90SBjoern A. Zeeb 	struct lkpi_vif *lvif;
7154bb81db90SBjoern A. Zeeb 	struct ieee80211vap *vap;
7155bb81db90SBjoern A. Zeeb 
7156bb81db90SBjoern A. Zeeb 	lvif = VIF_TO_LVIF(vif);
7157bb81db90SBjoern A. Zeeb 	vap = LVIF_TO_VAP(lvif);
7158bb81db90SBjoern A. Zeeb 
7159bb81db90SBjoern A. Zeeb 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
7160bb81db90SBjoern A. Zeeb 	    vif, vap, ieee80211_state_name[vap->iv_state]);
71613540911bSBjoern A. Zeeb 	ieee80211_beacon_miss(vap->iv_ic);
7162bb81db90SBjoern A. Zeeb }
7163bb81db90SBjoern A. Zeeb 
71645edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
71655edde07cSBjoern A. Zeeb 
71665a9a0d78SBjoern A. Zeeb void
71675a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
71685a9a0d78SBjoern A. Zeeb {
71695a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
71705a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
71715a9a0d78SBjoern A. Zeeb 	struct ieee80211_vif *vif;
71725a9a0d78SBjoern A. Zeeb 	int ac_count, ac;
71735a9a0d78SBjoern A. Zeeb 
71745a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
71755a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
71765a9a0d78SBjoern A. Zeeb 
71775a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
71785a9a0d78SBjoern A. Zeeb 
71795a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
71805a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
71815a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
71825a9a0d78SBjoern A. Zeeb 	else
71835a9a0d78SBjoern A. Zeeb 		ac_count = 1;
71845a9a0d78SBjoern A. Zeeb 
71855a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
71865a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
71875a9a0d78SBjoern A. Zeeb 
71885a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
71895a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
71905a9a0d78SBjoern A. Zeeb 			IMPROVE_TXQ("LOCKING");
71915a9a0d78SBjoern A. Zeeb 			if (qnum == vif->hw_queue[ac]) {
71920d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
71935a9a0d78SBjoern A. Zeeb 				/*
71945a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
71955a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
71965a9a0d78SBjoern A. Zeeb 				 */
71970d2cb6a6SBjoern A. Zeeb 				if (lvif->hw_queue_stopped[ac] &&
71980d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
71995a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72005a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d qnum %d already "
72015a9a0d78SBjoern A. Zeeb 					    "stopped\n", __func__, __LINE__,
72025a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac, qnum);
72030d2cb6a6SBjoern A. Zeeb #endif
72045a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = true;
72055a9a0d78SBjoern A. Zeeb 			}
72065a9a0d78SBjoern A. Zeeb 		}
72075a9a0d78SBjoern A. Zeeb 	}
72085a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72095a9a0d78SBjoern A. Zeeb }
72105a9a0d78SBjoern A. Zeeb 
72115a9a0d78SBjoern A. Zeeb void
72125a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
72135a9a0d78SBjoern A. Zeeb {
72145a9a0d78SBjoern A. Zeeb 	int i;
72155a9a0d78SBjoern A. Zeeb 
72165a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking; do we need further info?");
72175a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
72185a9a0d78SBjoern A. Zeeb 		linuxkpi_ieee80211_stop_queue(hw, i);
72195a9a0d78SBjoern A. Zeeb }
72205a9a0d78SBjoern A. Zeeb 
72215a9a0d78SBjoern A. Zeeb 
72225a9a0d78SBjoern A. Zeeb static void
72235a9a0d78SBjoern A. Zeeb lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
72245a9a0d78SBjoern A. Zeeb {
72255a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
72265a9a0d78SBjoern A. Zeeb 	struct lkpi_vif *lvif;
72275a9a0d78SBjoern A. Zeeb 	struct lkpi_sta *lsta;
72285a9a0d78SBjoern A. Zeeb 	int ac_count, ac, tid;
72295a9a0d78SBjoern A. Zeeb 
72305a9a0d78SBjoern A. Zeeb 	/* See lkpi_ic_vap_create(). */
72315a9a0d78SBjoern A. Zeeb 	if (hw->queues >= IEEE80211_NUM_ACS)
72325a9a0d78SBjoern A. Zeeb 		ac_count = IEEE80211_NUM_ACS;
72335a9a0d78SBjoern A. Zeeb 	else
72345a9a0d78SBjoern A. Zeeb 		ac_count = 1;
72355a9a0d78SBjoern A. Zeeb 
72365a9a0d78SBjoern A. Zeeb 	lhw = wiphy_priv(hw->wiphy);
72375a9a0d78SBjoern A. Zeeb 
72385a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Locking");
72395a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_LOCK(lhw);
72405a9a0d78SBjoern A. Zeeb 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
72415a9a0d78SBjoern A. Zeeb 		struct ieee80211_vif *vif;
72425a9a0d78SBjoern A. Zeeb 
72435a9a0d78SBjoern A. Zeeb 		vif = LVIF_TO_VIF(lvif);
72445a9a0d78SBjoern A. Zeeb 		for (ac = 0; ac < ac_count; ac++) {
72455a9a0d78SBjoern A. Zeeb 
72465a9a0d78SBjoern A. Zeeb 			if (hwq == vif->hw_queue[ac]) {
72475a9a0d78SBjoern A. Zeeb 
72485a9a0d78SBjoern A. Zeeb 				/* XXX-BZ what about software scan? */
72495a9a0d78SBjoern A. Zeeb 
72500d2cb6a6SBjoern A. Zeeb #ifdef LINUXKPI_DEBUG_80211
72515a9a0d78SBjoern A. Zeeb 				/*
72525a9a0d78SBjoern A. Zeeb 				 * For now log this to better understand
72535a9a0d78SBjoern A. Zeeb 				 * how this is supposed to work.
72545a9a0d78SBjoern A. Zeeb 				 */
72550d2cb6a6SBjoern A. Zeeb 				if (!lvif->hw_queue_stopped[ac] &&
72560d2cb6a6SBjoern A. Zeeb 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
72575a9a0d78SBjoern A. Zeeb 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
72585a9a0d78SBjoern A. Zeeb 					    "lvif %p vif %p ac %d hw_q not stopped\n",
72595a9a0d78SBjoern A. Zeeb 					    __func__, __LINE__,
72605a9a0d78SBjoern A. Zeeb 					    lhw, hw, lvif, vif, ac);
72610d2cb6a6SBjoern A. Zeeb #endif
72625a9a0d78SBjoern A. Zeeb 				lvif->hw_queue_stopped[ac] = false;
72635a9a0d78SBjoern A. Zeeb 
7264a8f735a6SBjoern A. Zeeb 				rcu_read_lock();
7265a8f735a6SBjoern A. Zeeb 				list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
72665a9a0d78SBjoern A. Zeeb 					struct ieee80211_sta *sta;
72675a9a0d78SBjoern A. Zeeb 
72685a9a0d78SBjoern A. Zeeb 					sta = LSTA_TO_STA(lsta);
72695a9a0d78SBjoern A. Zeeb 					for (tid = 0; tid < nitems(sta->txq); tid++) {
72705a9a0d78SBjoern A. Zeeb 						struct lkpi_txq *ltxq;
72715a9a0d78SBjoern A. Zeeb 
72725a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid] == NULL)
72735a9a0d78SBjoern A. Zeeb 							continue;
72745a9a0d78SBjoern A. Zeeb 
72755a9a0d78SBjoern A. Zeeb 						if (sta->txq[tid]->ac != ac)
72765a9a0d78SBjoern A. Zeeb 							continue;
72775a9a0d78SBjoern A. Zeeb 
72785a9a0d78SBjoern A. Zeeb 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
72795a9a0d78SBjoern A. Zeeb 						if (!ltxq->stopped)
72805a9a0d78SBjoern A. Zeeb 							continue;
72815a9a0d78SBjoern A. Zeeb 
72825a9a0d78SBjoern A. Zeeb 						ltxq->stopped = false;
72835a9a0d78SBjoern A. Zeeb 
72845a9a0d78SBjoern A. Zeeb 						/* XXX-BZ see when this explodes with all the locking. taskq? */
72855a9a0d78SBjoern A. Zeeb 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
72865a9a0d78SBjoern A. Zeeb 					}
72875a9a0d78SBjoern A. Zeeb 				}
7288a8f735a6SBjoern A. Zeeb 				rcu_read_unlock();
72895a9a0d78SBjoern A. Zeeb 			}
72905a9a0d78SBjoern A. Zeeb 		}
72915a9a0d78SBjoern A. Zeeb 	}
72925a9a0d78SBjoern A. Zeeb 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
72935a9a0d78SBjoern A. Zeeb }
72945a9a0d78SBjoern A. Zeeb 
72955a9a0d78SBjoern A. Zeeb void
72965a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
72975a9a0d78SBjoern A. Zeeb {
72985a9a0d78SBjoern A. Zeeb 	int i;
72995a9a0d78SBjoern A. Zeeb 
73005a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Is this all/enough here?");
73015a9a0d78SBjoern A. Zeeb 	for (i = 0; i < hw->queues; i++)
73025a9a0d78SBjoern A. Zeeb 		lkpi_ieee80211_wake_queues(hw, i);
73035a9a0d78SBjoern A. Zeeb }
73045a9a0d78SBjoern A. Zeeb 
73055a9a0d78SBjoern A. Zeeb void
73065a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
73075a9a0d78SBjoern A. Zeeb {
73085a9a0d78SBjoern A. Zeeb 
73095a9a0d78SBjoern A. Zeeb 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
73105a9a0d78SBjoern A. Zeeb 	    __func__, qnum, hw->queues, hw));
73115a9a0d78SBjoern A. Zeeb 
73125a9a0d78SBjoern A. Zeeb 	lkpi_ieee80211_wake_queues(hw, qnum);
73135a9a0d78SBjoern A. Zeeb }
73145a9a0d78SBjoern A. Zeeb 
73155a9a0d78SBjoern A. Zeeb /* This is just hardware queues. */
73165a9a0d78SBjoern A. Zeeb void
73175a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
73185a9a0d78SBjoern A. Zeeb {
73195a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73205a9a0d78SBjoern A. Zeeb 
73215a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73225a9a0d78SBjoern A. Zeeb 
73235a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
73245a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73255a9a0d78SBjoern A. Zeeb 	if (++lhw->txq_generation[ac] == 0)
73265a9a0d78SBjoern A. Zeeb 		lhw->txq_generation[ac]++;
73275a9a0d78SBjoern A. Zeeb }
73285a9a0d78SBjoern A. Zeeb 
73295a9a0d78SBjoern A. Zeeb struct ieee80211_txq *
73305a9a0d78SBjoern A. Zeeb linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
73315a9a0d78SBjoern A. Zeeb {
73325a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73335a9a0d78SBjoern A. Zeeb 	struct ieee80211_txq *txq;
73345a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
73355a9a0d78SBjoern A. Zeeb 
73365a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73375a9a0d78SBjoern A. Zeeb 	txq = NULL;
73385a9a0d78SBjoern A. Zeeb 
73395a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73405a9a0d78SBjoern A. Zeeb 
73415a9a0d78SBjoern A. Zeeb 	/* Check that we are scheduled. */
73425a9a0d78SBjoern A. Zeeb 	if (lhw->txq_generation[ac] == 0)
73435a9a0d78SBjoern A. Zeeb 		goto out;
73445a9a0d78SBjoern A. Zeeb 
73455a9a0d78SBjoern A. Zeeb 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
73465a9a0d78SBjoern A. Zeeb 	if (ltxq == NULL)
73475a9a0d78SBjoern A. Zeeb 		goto out;
73485a9a0d78SBjoern A. Zeeb 	if (ltxq->txq_generation == lhw->txq_generation[ac])
73495a9a0d78SBjoern A. Zeeb 		goto out;
73505a9a0d78SBjoern A. Zeeb 
73515a9a0d78SBjoern A. Zeeb 	ltxq->txq_generation = lhw->txq_generation[ac];
73525a9a0d78SBjoern A. Zeeb 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
73535a9a0d78SBjoern A. Zeeb 	txq = &ltxq->txq;
73545a9a0d78SBjoern A. Zeeb 	TAILQ_ELEM_INIT(ltxq, txq_entry);
73555a9a0d78SBjoern A. Zeeb 
73565a9a0d78SBjoern A. Zeeb out:
73575a9a0d78SBjoern A. Zeeb 	return (txq);
73585a9a0d78SBjoern A. Zeeb }
73595a9a0d78SBjoern A. Zeeb 
73605a9a0d78SBjoern A. Zeeb void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
73615a9a0d78SBjoern A. Zeeb     struct ieee80211_txq *txq, bool withoutpkts)
73625a9a0d78SBjoern A. Zeeb {
73635a9a0d78SBjoern A. Zeeb 	struct lkpi_hw *lhw;
73645a9a0d78SBjoern A. Zeeb 	struct lkpi_txq *ltxq;
7365eac3646fSBjoern A. Zeeb 	bool ltxq_empty;
73665a9a0d78SBjoern A. Zeeb 
73675a9a0d78SBjoern A. Zeeb 	ltxq = TXQ_TO_LTXQ(txq);
73685a9a0d78SBjoern A. Zeeb 
73695a9a0d78SBjoern A. Zeeb 	IMPROVE_TXQ("LOCKING");
73705a9a0d78SBjoern A. Zeeb 
73715a9a0d78SBjoern A. Zeeb 	/* Only schedule if work to do or asked to anyway. */
7372eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_LOCK(ltxq);
7373eac3646fSBjoern A. Zeeb 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
7374eac3646fSBjoern A. Zeeb 	LKPI_80211_LTXQ_UNLOCK(ltxq);
7375eac3646fSBjoern A. Zeeb 	if (!withoutpkts && ltxq_empty)
73765a9a0d78SBjoern A. Zeeb 		goto out;
73775a9a0d78SBjoern A. Zeeb 
737841b746e0SAustin Shafer 	/*
737941b746e0SAustin Shafer 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
738041b746e0SAustin Shafer 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
738141b746e0SAustin Shafer 	 * is queued, tqe_next may be NULL if this is the only element in the list.
738241b746e0SAustin Shafer 	 */
738341b746e0SAustin Shafer 	if (ltxq->txq_entry.tqe_prev != NULL)
73845a9a0d78SBjoern A. Zeeb 		goto out;
73855a9a0d78SBjoern A. Zeeb 
73865a9a0d78SBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
73875a9a0d78SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
73885a9a0d78SBjoern A. Zeeb out:
73895a9a0d78SBjoern A. Zeeb 	return;
73905a9a0d78SBjoern A. Zeeb }
73915a9a0d78SBjoern A. Zeeb 
7392eac3646fSBjoern A. Zeeb void
7393eac3646fSBjoern A. Zeeb linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
7394eac3646fSBjoern A. Zeeb     struct ieee80211_txq *txq)
7395eac3646fSBjoern A. Zeeb {
7396eac3646fSBjoern A. Zeeb 	struct lkpi_hw *lhw;
7397eac3646fSBjoern A. Zeeb 	struct ieee80211_txq *ntxq;
7398eac3646fSBjoern A. Zeeb 	struct ieee80211_tx_control control;
7399eac3646fSBjoern A. Zeeb         struct sk_buff *skb;
7400eac3646fSBjoern A. Zeeb 
7401eac3646fSBjoern A. Zeeb 	lhw = HW_TO_LHW(hw);
7402eac3646fSBjoern A. Zeeb 
7403eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_LOCK(lhw);
7404eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_start(hw, txq->ac);
7405eac3646fSBjoern A. Zeeb 	do {
7406eac3646fSBjoern A. Zeeb 		ntxq = ieee80211_next_txq(hw, txq->ac);
7407eac3646fSBjoern A. Zeeb 		if (ntxq == NULL)
7408eac3646fSBjoern A. Zeeb 			break;
7409eac3646fSBjoern A. Zeeb 
7410eac3646fSBjoern A. Zeeb 		memset(&control, 0, sizeof(control));
7411eac3646fSBjoern A. Zeeb 		control.sta = ntxq->sta;
7412eac3646fSBjoern A. Zeeb 		do {
7413eac3646fSBjoern A. Zeeb 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
7414eac3646fSBjoern A. Zeeb 			if (skb == NULL)
7415eac3646fSBjoern A. Zeeb 				break;
7416eac3646fSBjoern A. Zeeb 			lkpi_80211_mo_tx(hw, &control, skb);
7417eac3646fSBjoern A. Zeeb 		} while(1);
7418eac3646fSBjoern A. Zeeb 
7419eac3646fSBjoern A. Zeeb 		ieee80211_return_txq(hw, ntxq, false);
7420eac3646fSBjoern A. Zeeb 	} while (1);
7421eac3646fSBjoern A. Zeeb 	ieee80211_txq_schedule_end(hw, txq->ac);
7422eac3646fSBjoern A. Zeeb 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
7423eac3646fSBjoern A. Zeeb }
7424eac3646fSBjoern A. Zeeb 
74255a9a0d78SBjoern A. Zeeb /* -------------------------------------------------------------------------- */
74265a9a0d78SBjoern A. Zeeb 
74275edde07cSBjoern A. Zeeb struct lkpi_cfg80211_bss {
74285edde07cSBjoern A. Zeeb 	u_int refcnt;
74295edde07cSBjoern A. Zeeb 	struct cfg80211_bss bss;
74305edde07cSBjoern A. Zeeb };
74315edde07cSBjoern A. Zeeb 
74325edde07cSBjoern A. Zeeb struct lkpi_cfg80211_get_bss_iter_lookup {
74335edde07cSBjoern A. Zeeb 	struct wiphy *wiphy;
74345edde07cSBjoern A. Zeeb 	struct linuxkpi_ieee80211_channel *chan;
74355edde07cSBjoern A. Zeeb 	const uint8_t *bssid;
74365edde07cSBjoern A. Zeeb 	const uint8_t *ssid;
74375edde07cSBjoern A. Zeeb 	size_t ssid_len;
74385edde07cSBjoern A. Zeeb 	enum ieee80211_bss_type bss_type;
74395edde07cSBjoern A. Zeeb 	enum ieee80211_privacy privacy;
74405edde07cSBjoern A. Zeeb 
74415edde07cSBjoern A. Zeeb 	/*
74425edde07cSBjoern A. Zeeb 	 * Something to store a copy of the result as the net80211 scan cache
74435edde07cSBjoern A. Zeeb 	 * is not refoucnted so a scan entry might go away any time.
74445edde07cSBjoern A. Zeeb 	 */
74455edde07cSBjoern A. Zeeb 	bool match;
74465edde07cSBjoern A. Zeeb 	struct cfg80211_bss *bss;
74475edde07cSBjoern A. Zeeb };
74485edde07cSBjoern A. Zeeb 
74495edde07cSBjoern A. Zeeb static void
74505edde07cSBjoern A. Zeeb lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
74515edde07cSBjoern A. Zeeb {
74525edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
74535edde07cSBjoern A. Zeeb 	size_t ielen;
74545edde07cSBjoern A. Zeeb 
74555edde07cSBjoern A. Zeeb 	lookup = arg;
74565edde07cSBjoern A. Zeeb 
74575edde07cSBjoern A. Zeeb 	/* Do not try to find another match. */
74585edde07cSBjoern A. Zeeb 	if (lookup->match)
74595edde07cSBjoern A. Zeeb 		return;
74605edde07cSBjoern A. Zeeb 
74615edde07cSBjoern A. Zeeb 	/* Nothing to store result. */
74625edde07cSBjoern A. Zeeb 	if (lookup->bss == NULL)
74635edde07cSBjoern A. Zeeb 		return;
74645edde07cSBjoern A. Zeeb 
74655edde07cSBjoern A. Zeeb 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
74665edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
74675edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
74685edde07cSBjoern A. Zeeb 		return;
74695edde07cSBjoern A. Zeeb 	}
74705edde07cSBjoern A. Zeeb 
74715edde07cSBjoern A. Zeeb 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
74725edde07cSBjoern A. Zeeb 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
74735edde07cSBjoern A. Zeeb 		/* We have no idea what to compare to as the drivers only request ANY */
74745edde07cSBjoern A. Zeeb 		return;
74755edde07cSBjoern A. Zeeb 	}
74765edde07cSBjoern A. Zeeb 
74775edde07cSBjoern A. Zeeb 	if (lookup->chan != NULL) {
74785edde07cSBjoern A. Zeeb 		struct linuxkpi_ieee80211_channel *chan;
74795edde07cSBjoern A. Zeeb 
74805edde07cSBjoern A. Zeeb 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
74815edde07cSBjoern A. Zeeb 		    se->se_chan->ic_freq);
74825edde07cSBjoern A. Zeeb 		if (chan == NULL || chan != lookup->chan)
74835edde07cSBjoern A. Zeeb 			return;
74845edde07cSBjoern A. Zeeb 	}
74855edde07cSBjoern A. Zeeb 
74865edde07cSBjoern A. Zeeb 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
74875edde07cSBjoern A. Zeeb 		return;
74885edde07cSBjoern A. Zeeb 
74895edde07cSBjoern A. Zeeb 	if (lookup->ssid) {
74905edde07cSBjoern A. Zeeb 		if (lookup->ssid_len != se->se_ssid[1] ||
74915edde07cSBjoern A. Zeeb 		    se->se_ssid[1] == 0)
74925edde07cSBjoern A. Zeeb 			return;
74935edde07cSBjoern A. Zeeb 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
74945edde07cSBjoern A. Zeeb 			return;
74955edde07cSBjoern A. Zeeb 	}
74965edde07cSBjoern A. Zeeb 
74975edde07cSBjoern A. Zeeb 	ielen = se->se_ies.len;
74985edde07cSBjoern A. Zeeb 
74995edde07cSBjoern A. Zeeb 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
75005edde07cSBjoern A. Zeeb 	    M_LKPI80211, M_NOWAIT | M_ZERO);
75015edde07cSBjoern A. Zeeb 	if (lookup->bss->ies == NULL)
75025edde07cSBjoern A. Zeeb 		return;
75035edde07cSBjoern A. Zeeb 
75045edde07cSBjoern A. Zeeb 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
75055edde07cSBjoern A. Zeeb 	lookup->bss->ies->len = ielen;
75065edde07cSBjoern A. Zeeb 	if (ielen)
75075edde07cSBjoern A. Zeeb 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
75085edde07cSBjoern A. Zeeb 
75095edde07cSBjoern A. Zeeb 	lookup->match = true;
75105edde07cSBjoern A. Zeeb }
75115edde07cSBjoern A. Zeeb 
75125edde07cSBjoern A. Zeeb struct cfg80211_bss *
75135edde07cSBjoern A. Zeeb linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
75145edde07cSBjoern A. Zeeb     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
75155edde07cSBjoern A. Zeeb     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
75165edde07cSBjoern A. Zeeb {
75175edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75185edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
75195edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75205edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
75215edde07cSBjoern A. Zeeb 
75225edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
75235edde07cSBjoern A. Zeeb 
75245edde07cSBjoern A. Zeeb 	/* Let's hope we can alloc. */
75255edde07cSBjoern A. Zeeb 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
75265edde07cSBjoern A. Zeeb 	if (lbss == NULL) {
75275edde07cSBjoern A. Zeeb 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
75285edde07cSBjoern A. Zeeb 		return (NULL);
75295edde07cSBjoern A. Zeeb 	}
75305edde07cSBjoern A. Zeeb 
75315edde07cSBjoern A. Zeeb 	lookup.wiphy = wiphy;
75325edde07cSBjoern A. Zeeb 	lookup.chan = chan;
75335edde07cSBjoern A. Zeeb 	lookup.bssid = bssid;
75345edde07cSBjoern A. Zeeb 	lookup.ssid = ssid;
75355edde07cSBjoern A. Zeeb 	lookup.ssid_len = ssid_len;
75365edde07cSBjoern A. Zeeb 	lookup.bss_type = bss_type;
75375edde07cSBjoern A. Zeeb 	lookup.privacy = privacy;
75385edde07cSBjoern A. Zeeb 	lookup.match = false;
75395edde07cSBjoern A. Zeeb 	lookup.bss = &lbss->bss;
75405edde07cSBjoern A. Zeeb 
75415edde07cSBjoern A. Zeeb 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
75425edde07cSBjoern A. Zeeb 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
75435edde07cSBjoern A. Zeeb 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
75445edde07cSBjoern A. Zeeb 	if (!lookup.match) {
75455edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75465edde07cSBjoern A. Zeeb 		return (NULL);
75475edde07cSBjoern A. Zeeb 	}
75485edde07cSBjoern A. Zeeb 
75495edde07cSBjoern A. Zeeb 	refcount_init(&lbss->refcnt, 1);
75505edde07cSBjoern A. Zeeb 	return (&lbss->bss);
75515edde07cSBjoern A. Zeeb }
75525edde07cSBjoern A. Zeeb 
75535edde07cSBjoern A. Zeeb void
75545edde07cSBjoern A. Zeeb linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
75555edde07cSBjoern A. Zeeb {
75565edde07cSBjoern A. Zeeb 	struct lkpi_cfg80211_bss *lbss;
75575edde07cSBjoern A. Zeeb 
75585edde07cSBjoern A. Zeeb 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
75595edde07cSBjoern A. Zeeb 
75605edde07cSBjoern A. Zeeb 	/* Free everything again on refcount ... */
75615edde07cSBjoern A. Zeeb 	if (refcount_release(&lbss->refcnt)) {
75625edde07cSBjoern A. Zeeb 		free(lbss->bss.ies, M_LKPI80211);
75635edde07cSBjoern A. Zeeb 		free(lbss, M_LKPI80211);
75645edde07cSBjoern A. Zeeb 	}
75655edde07cSBjoern A. Zeeb }
75665edde07cSBjoern A. Zeeb 
75675edde07cSBjoern A. Zeeb void
75685edde07cSBjoern A. Zeeb linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
75695edde07cSBjoern A. Zeeb {
75705edde07cSBjoern A. Zeeb 	struct lkpi_hw *lhw;
75715edde07cSBjoern A. Zeeb 	struct ieee80211com *ic;
75725edde07cSBjoern A. Zeeb 	struct ieee80211vap *vap;
75735edde07cSBjoern A. Zeeb 
75745edde07cSBjoern A. Zeeb 	lhw = wiphy_priv(wiphy);
75755edde07cSBjoern A. Zeeb 	ic = lhw->ic;
75765edde07cSBjoern A. Zeeb 
75775edde07cSBjoern A. Zeeb 	/*
75785edde07cSBjoern A. Zeeb 	 * If we haven't called ieee80211_ifattach() yet
75795edde07cSBjoern A. Zeeb 	 * or there is no VAP, there are no scans to flush.
75805edde07cSBjoern A. Zeeb 	 */
75815edde07cSBjoern A. Zeeb 	if (ic == NULL ||
75825edde07cSBjoern A. Zeeb 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
75835edde07cSBjoern A. Zeeb 		return;
75845edde07cSBjoern A. Zeeb 
75855edde07cSBjoern A. Zeeb 	/* Should only happen on the current one? Not seen it late enough. */
75865edde07cSBjoern A. Zeeb 	IEEE80211_LOCK(ic);
75875edde07cSBjoern A. Zeeb 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
75885edde07cSBjoern A. Zeeb 		ieee80211_scan_flush(vap);
75895edde07cSBjoern A. Zeeb 	IEEE80211_UNLOCK(ic);
75905edde07cSBjoern A. Zeeb }
75915edde07cSBjoern A. Zeeb 
75925edde07cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
75935edde07cSBjoern A. Zeeb 
7594ac1d519cSBjoern A. Zeeb /*
7595ac1d519cSBjoern A. Zeeb  * hw->conf get initialized/set in various places for us:
7596ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_alloc_hw(): flags
7597ac1d519cSBjoern A. Zeeb  * - linuxkpi_ieee80211_ifattach(): chandef
7598ac1d519cSBjoern A. Zeeb  * - lkpi_ic_vap_create(): listen_interval
7599ac1d519cSBjoern A. Zeeb  * - lkpi_ic_set_channel(): chandef, flags
7600ac1d519cSBjoern A. Zeeb  */
7601ac1d519cSBjoern A. Zeeb 
7602ac1d519cSBjoern A. Zeeb int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
7603ac1d519cSBjoern A. Zeeb     struct ieee80211_chanctx_conf *new)
7604ac1d519cSBjoern A. Zeeb {
7605ac1d519cSBjoern A. Zeeb 	struct cfg80211_chan_def *cd;
7606ac1d519cSBjoern A. Zeeb 	uint32_t changed;
7607ac1d519cSBjoern A. Zeeb 	int error;
7608ac1d519cSBjoern A. Zeeb 
7609ac1d519cSBjoern A. Zeeb 	changed = 0;
7610ac1d519cSBjoern A. Zeeb 	if (new == NULL || new->def.chan == NULL)
7611ac1d519cSBjoern A. Zeeb 		cd = NULL;
7612ac1d519cSBjoern A. Zeeb 	else
7613ac1d519cSBjoern A. Zeeb 		cd = &new->def;
7614ac1d519cSBjoern A. Zeeb 
7615ac1d519cSBjoern A. Zeeb 	if (cd && cd->chan != hw->conf.chandef.chan) {
7616ac1d519cSBjoern A. Zeeb 		/* Copy; the chan pointer is fine and will stay valid. */
7617ac1d519cSBjoern A. Zeeb 		hw->conf.chandef = *cd;
7618ac1d519cSBjoern A. Zeeb 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
7619ac1d519cSBjoern A. Zeeb 	}
7620ac1d519cSBjoern A. Zeeb 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
7621ac1d519cSBjoern A. Zeeb 
7622ac1d519cSBjoern A. Zeeb 	if (changed == 0)
7623ac1d519cSBjoern A. Zeeb 		return (0);
7624ac1d519cSBjoern A. Zeeb 
7625ac1d519cSBjoern A. Zeeb 	error = lkpi_80211_mo_config(hw, changed);
7626ac1d519cSBjoern A. Zeeb 	return (error);
7627ac1d519cSBjoern A. Zeeb }
7628ac1d519cSBjoern A. Zeeb 
7629ac1d519cSBjoern A. Zeeb /* -------------------------------------------------------------------------- */
7630ac1d519cSBjoern A. Zeeb 
76316b4cac81SBjoern A. Zeeb MODULE_VERSION(linuxkpi_wlan, 1);
76326b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
76336b4cac81SBjoern A. Zeeb MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
7634